Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkImage_Gpu.h" | 8 #include "SkImage_Gpu.h" |
| 9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "GrContext.h" | 10 #include "GrContext.h" |
| 11 #include "SkGpuDevice.h" | |
| 11 | 12 |
| 12 SkImage_Gpu::SkImage_Gpu(const SkBitmap& bitmap, int sampleCountForNewSurfaces, | 13 SkImage_Gpu::SkImage_Gpu(int w, int h, SkAlphaType at, GrTexture* tex, |
| 13 SkSurface::Budgeted budgeted) | 14 int sampleCountForNewSurfaces, SkSurface::Budgeted budg eted) |
| 14 : INHERITED(bitmap.width(), bitmap.height(), NULL) | 15 : INHERITED(w, h, NULL) |
| 15 , fBitmap(bitmap) | 16 , fTexture(SkRef(tex)) |
| 16 , fSampleCountForNewSurfaces(sampleCountForNewSurfaces) | 17 , fSampleCountForNewSurfaces(sampleCountForNewSurfaces) |
| 18 , fAlphaType(at) | |
| 17 , fBudgeted(budgeted) | 19 , fBudgeted(budgeted) |
| 18 { | 20 {} |
| 19 SkASSERT(fBitmap.getTexture()); | 21 |
| 22 SkSurface* SkImage_Gpu::onNewSurface(const SkImageInfo& info, const SkSurfacePro ps& props) const { | |
| 23 GrTexture* tex = this->getTexture(); | |
| 24 SkASSERT(tex); | |
| 25 GrContext* ctx = tex->getContext(); | |
| 26 if (!ctx) { | |
| 27 // the texture may have been abandoned, so we have to check | |
| 28 return NULL; | |
| 29 } | |
| 30 // TODO: Change signature of onNewSurface to take a budgeted param. | |
| 31 const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted; | |
| 32 return SkSurface::NewRenderTarget(ctx, budgeted, info, fSampleCountForNewSur faces, &props); | |
| 20 } | 33 } |
| 21 | 34 |
| 22 SkShader* SkImage_Gpu::onNewShader(SkShader::TileMode tileX, | 35 extern void SkTextureImageApplyBudgetedDecision(SkImage* image) { |
| 23 SkShader::TileMode tileY, | 36 if (image->getTexture()) { |
| 24 const SkMatrix* localMatrix) const | 37 ((SkImage_Gpu*)image)->applyBudgetDecision(); |
| 25 { | 38 } |
| 26 return SkShader::CreateBitmapShader(fBitmap, tileX, tileY, localMatrix); | |
| 27 } | 39 } |
| 28 | 40 |
| 29 SkSurface* SkImage_Gpu::onNewSurface(const SkImageInfo& info, const SkSurfacePro ps& props) const { | 41 SkShader* SkImage_Gpu::onNewShader(SkShader::TileMode tileX, SkShader::TileMode tileY, |
| 30 GrContext* ctx = this->getTexture()->getContext(); | 42 const SkMatrix* localMatrix) const { |
| 31 // TODO: Change signature of onNewSurface to take a budgeted param. | 43 SkBitmap bm; |
| 32 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted; | 44 GrWrapTextureInBitmap(fTexture, this->width(), this->height(), this->isOpaqu e(), &bm); |
| 33 return SkSurface::NewRenderTarget(ctx, kBudgeted, info, fSampleCountForNewSu rfaces, &props); | 45 return SkShader::CreateBitmapShader(bm, tileX, tileY, localMatrix); |
| 34 } | |
| 35 | |
| 36 GrTexture* SkImage_Gpu::onGetTexture() const { | |
| 37 return fBitmap.getTexture(); | |
| 38 } | 46 } |
| 39 | 47 |
| 40 bool SkImage_Gpu::getROPixels(SkBitmap* dst) const { | 48 bool SkImage_Gpu::getROPixels(SkBitmap* dst) const { |
| 41 return fBitmap.copyTo(dst, kN32_SkColorType); | 49 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaTyp e; |
| 50 if (!dst->tryAllocPixels(SkImageInfo::MakeN32(this->width(), this->height(), at))) { | |
| 51 return false; | |
| 52 } | |
| 53 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPix elConfig, | |
| 54 dst->getPixels(), dst->rowBytes())) { | |
| 55 return false; | |
| 56 } | |
| 57 return true; | |
| 42 } | 58 } |
| 43 | 59 |
|
robertphillips
2015/05/07 20:01:29
one line?
reed1
2015/05/07 20:27:59
hmmm, is that a pseudo standard for our code (othe
| |
| 44 bool SkImage_Gpu::isOpaque() const { | 60 bool SkImage_Gpu::isOpaque() const { |
| 45 return fBitmap.isOpaque(); | 61 return GrPixelConfigIsOpaque(fTexture->config()); |
| 46 } | 62 } |
| 47 | 63 |
| 48 /////////////////////////////////////////////////////////////////////////////// | 64 static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 65 switch (info.colorType()) { | |
| 66 case kRGBA_8888_SkColorType: | |
| 67 case kBGRA_8888_SkColorType: | |
| 68 break; | |
| 69 default: | |
| 70 return; // nothing to do | |
| 71 } | |
| 49 | 72 |
| 50 SkImage* SkNewImageFromBitmapTexture(const SkBitmap& bitmap, int sampleCountForN ewSurfaces, | 73 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-e ndian, |
| 51 SkSurface::Budgeted budgeted) { | 74 // and in either case, the alpha-byte is always in the same place, so we can safely call |
| 52 if (0 == bitmap.width() || 0 == bitmap.height() || NULL == bitmap.getTexture ()) { | 75 // SkPreMultiplyColor() |
| 76 // | |
| 77 SkColor* row = (SkColor*)pixels; | |
| 78 for (int y = 0; y < info.height(); ++y) { | |
| 79 for (int x = 0; x < info.width(); ++x) { | |
| 80 row[x] = SkPreMultiplyColor(row[x]); | |
| 81 } | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t row Bytes, | |
| 86 int srcX, int srcY) const { | |
| 87 GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alph aType(), | |
| 88 info.profileType()); | |
|
robertphillips
2015/05/07 20:01:29
init to 0 ?
reed1
2015/05/07 20:27:59
Done.
| |
| 89 uint32_t flags = GrContext::kFlushWrites_PixelOp; | |
|
robertphillips
2015/05/07 20:01:29
Do we test this premul/unpremul case ?
reed1
2015/05/07 20:27:59
Dunno for sure, but there is a gob of preexisting
| |
| 90 if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlp haType) { | |
|
robertphillips
2015/05/07 20:01:29
// Have the readPixels perform the conversion from
reed1
2015/05/07 20:28:00
Done.
| |
| 91 flags = GrContext::kUnpremul_PixelOpsFlag; | |
| 92 } | |
| 93 if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config, | |
| 94 pixels, rowBytes, flags)) { | |
| 95 return false; | |
| 96 } | |
| 97 // do we have to manually fix-up the alpha channel? | |
|
robertphillips
2015/05/07 20:01:29
Ganesh should also be able to do the unpremul to p
reed1
2015/05/07 20:27:59
Done.
| |
| 98 // src dst | |
| 99 // unpremul premul fix manually | |
| 100 // premul unpremul done by kUnpremul_PixelOpsFlag | |
| 101 // all other combos need to change. | |
| 102 // | |
| 103 if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlp haType) { | |
| 104 apply_premul(info, pixels, rowBytes); | |
| 105 } | |
| 106 return true; | |
| 107 } | |
| 108 | |
| 109 //////////////////////////////////////////////////////////////////////////////// /////////////////// | |
| 110 | |
| 111 SkImage* SkImage::NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& des c, SkAlphaType at) { | |
| 112 if (desc.fWidth <= 0 || desc.fHeight <= 0) { | |
| 53 return NULL; | 113 return NULL; |
| 54 } | 114 } |
| 55 return SkNEW_ARGS(SkImage_Gpu, (bitmap, sampleCountForNewSurfaces, budgeted) ); | 115 SkAutoTUnref<GrTexture> tex(ctx->textureProvider()->wrapBackendTexture(desc) ); |
| 116 if (!tex) { | |
| 117 return NULL; | |
| 118 } | |
| 119 const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted; | |
| 120 return SkNEW_ARGS(SkImage_Gpu, (desc.fWidth, desc.fHeight, at, tex, 0, budge ted)); | |
| 56 } | 121 } |
| 57 | 122 |
| 58 GrTexture* SkTextureImageGetTexture(SkImage* image) { | 123 SkImage* SkImage::NewFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& srcDesc, |
| 59 return ((SkImage_Gpu*)image)->getTexture(); | 124 SkAlphaType at) { |
| 125 const bool isBudgeted = true; | |
| 126 const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted; | |
| 127 | |
| 128 if (srcDesc.fWidth <= 0 || srcDesc.fHeight <= 0) { | |
| 129 return NULL; | |
| 130 } | |
| 131 SkAutoTUnref<GrTexture> src(ctx->textureProvider()->wrapBackendTexture(srcDe sc)); | |
| 132 if (!src) { | |
| 133 return NULL; | |
| 134 } | |
| 135 | |
| 136 GrSurfaceDesc dstDesc; | |
| 137 // need to be a rendertarget for readpixels to work | |
|
robertphillips
2015/05/07 20:01:29
rm the dead '//' comment/code ?
reed1
2015/05/07 20:27:59
Done.
| |
| 138 dstDesc.fFlags = kRenderTarget_GrSurfaceFlag; // kNone_GrSurfaceFlags; | |
| 139 dstDesc.fOrigin = srcDesc.fOrigin; | |
| 140 dstDesc.fWidth = srcDesc.fWidth; | |
| 141 dstDesc.fHeight = srcDesc.fHeight; | |
| 142 dstDesc.fConfig = srcDesc.fConfig; | |
| 143 dstDesc.fSampleCnt = srcDesc.fSampleCnt; | |
| 144 | |
|
robertphillips
2015/05/07 20:01:29
overlength
reed1
2015/05/07 20:28:00
Done.
| |
| 145 SkAutoTUnref<GrTexture> dst(ctx->textureProvider()->createTexture(dstDesc, i sBudgeted, NULL, 0)); | |
| 146 if (!dst) { | |
| 147 return NULL; | |
| 148 } | |
| 149 if (!ctx->copySurface(dst, src)) { | |
| 150 return NULL; | |
| 151 } | |
|
robertphillips
2015/05/07 20:01:29
Should we be passing the fSampleCnt parameter from
reed1
2015/05/07 20:27:59
Done.
| |
| 152 return SkNEW_ARGS(SkImage_Gpu, (dstDesc.fWidth, dstDesc.fHeight, at, dst, 0, budgeted)); | |
| 60 } | 153 } |
| 61 | |
| 62 extern void SkTextureImageApplyBudgetedDecision(SkImage* image) { | |
| 63 ((SkImage_Gpu*)image)->applyBudgetDecision(); | |
| 64 } | |
| OLD | NEW |