reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 8 | #include "SkBitmapCache.h" |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 9 | #include "SkImage_Gpu.h" |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 10 | #include "GrContext.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 11 | #include "GrDrawContext.h" |
| 12 | #include "effects/GrYUVtoRGBEffect.h" |
| 13 | #include "SkCanvas.h" |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 14 | #include "SkGpuDevice.h" |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 15 | #include "SkPixelRef.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 16 | |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 17 | SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, GrTexture* tex, |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 18 | int sampleCountForNewSurfaces, SkSurface::Budgeted budgeted) |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 19 | : INHERITED(w, h, uniqueID, NULL) |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 20 | , fTexture(SkRef(tex)) |
reed | 3f10b9d | 2014-11-21 10:27:53 -0800 | [diff] [blame] | 21 | , fSampleCountForNewSurfaces(sampleCountForNewSurfaces) |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 22 | , fAlphaType(at) |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 23 | , fBudgeted(budgeted) |
bsalomon | 1cd6311 | 2015-08-05 06:58:39 -0700 | [diff] [blame^] | 24 | , fAddedRasterVersionToCache(false) |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 25 | {} |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 26 | |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 27 | SkImage_Gpu::~SkImage_Gpu() { |
| 28 | if (fAddedRasterVersionToCache.load()) { |
| 29 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 30 | } |
| 31 | } |
| 32 | |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 33 | SkSurface* SkImage_Gpu::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) const { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 34 | GrTexture* tex = this->getTexture(); |
| 35 | SkASSERT(tex); |
| 36 | GrContext* ctx = tex->getContext(); |
| 37 | if (!ctx) { |
| 38 | // the texture may have been abandoned, so we have to check |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 39 | return NULL; |
| 40 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 41 | // TODO: Change signature of onNewSurface to take a budgeted param. |
| 42 | const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted; |
| 43 | return SkSurface::NewRenderTarget(ctx, budgeted, info, fSampleCountForNewSurfaces, &props); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 44 | } |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 45 | |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 46 | extern void SkTextureImageApplyBudgetedDecision(SkImage* image) { |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 47 | if (as_IB(image)->getTexture()) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 48 | ((SkImage_Gpu*)image)->applyBudgetDecision(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | SkShader* SkImage_Gpu::onNewShader(SkShader::TileMode tileX, SkShader::TileMode tileY, |
| 53 | const SkMatrix* localMatrix) const { |
| 54 | SkBitmap bm; |
| 55 | GrWrapTextureInBitmap(fTexture, this->width(), this->height(), this->isOpaque(), &bm); |
| 56 | return SkShader::CreateBitmapShader(bm, tileX, tileY, localMatrix); |
| 57 | } |
| 58 | |
| 59 | bool SkImage_Gpu::getROPixels(SkBitmap* dst) const { |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 60 | if (SkBitmapCache::Find(this->uniqueID(), dst)) { |
| 61 | SkASSERT(dst->getGenerationID() == this->uniqueID()); |
| 62 | SkASSERT(dst->isImmutable()); |
| 63 | SkASSERT(dst->getPixels()); |
| 64 | return true; |
| 65 | } |
| 66 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 67 | SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 68 | if (!dst->tryAllocPixels(SkImageInfo::MakeN32(this->width(), this->height(), at))) { |
| 69 | return false; |
| 70 | } |
| 71 | if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig, |
| 72 | dst->getPixels(), dst->rowBytes())) { |
| 73 | return false; |
| 74 | } |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 75 | |
| 76 | dst->pixelRef()->setImmutableWithID(this->uniqueID()); |
| 77 | SkBitmapCache::Add(this->uniqueID(), *dst); |
| 78 | fAddedRasterVersionToCache.store(true); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | |
| 82 | bool SkImage_Gpu::isOpaque() const { |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 83 | return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_SkAlphaType; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 87 | switch (info.colorType()) { |
| 88 | case kRGBA_8888_SkColorType: |
| 89 | case kBGRA_8888_SkColorType: |
| 90 | break; |
| 91 | default: |
| 92 | return; // nothing to do |
| 93 | } |
| 94 | |
| 95 | // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian, |
| 96 | // and in either case, the alpha-byte is always in the same place, so we can safely call |
| 97 | // SkPreMultiplyColor() |
| 98 | // |
| 99 | SkColor* row = (SkColor*)pixels; |
| 100 | for (int y = 0; y < info.height(); ++y) { |
| 101 | for (int x = 0; x < info.width(); ++x) { |
| 102 | row[x] = SkPreMultiplyColor(row[x]); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 108 | int srcX, int srcY) const { |
| 109 | GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType(), |
| 110 | info.profileType()); |
| 111 | uint32_t flags = 0; |
| 112 | if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) { |
| 113 | // let the GPU perform this transformation for us |
| 114 | flags = GrContext::kUnpremul_PixelOpsFlag; |
| 115 | } |
| 116 | if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config, |
| 117 | pixels, rowBytes, flags)) { |
| 118 | return false; |
| 119 | } |
| 120 | // do we have to manually fix-up the alpha channel? |
| 121 | // src dst |
| 122 | // unpremul premul fix manually |
| 123 | // premul unpremul done by kUnpremul_PixelOpsFlag |
| 124 | // all other combos need to change. |
| 125 | // |
| 126 | // Should this be handled by Ganesh? todo:? |
| 127 | // |
| 128 | if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) { |
| 129 | apply_premul(info, pixels, rowBytes); |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 135 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 136 | static SkImage* new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc, |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 137 | SkAlphaType at, GrWrapOwnership ownership, |
| 138 | SkImage::TextureReleaseProc releaseProc, |
| 139 | SkImage::ReleaseContext releaseCtx) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 140 | if (desc.fWidth <= 0 || desc.fHeight <= 0) { |
| 141 | return NULL; |
| 142 | } |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 143 | SkAutoTUnref<GrTexture> tex(ctx->textureProvider()->wrapBackendTexture(desc, ownership)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 144 | if (!tex) { |
| 145 | return NULL; |
| 146 | } |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 147 | if (releaseProc) { |
| 148 | tex->setRelease(releaseProc, releaseCtx); |
| 149 | } |
| 150 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 151 | const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted; |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 152 | return SkNEW_ARGS(SkImage_Gpu, |
| 153 | (desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, tex, 0, budgeted)); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 154 | |
| 155 | } |
| 156 | |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 157 | SkImage* SkImage::NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc, SkAlphaType at, |
| 158 | TextureReleaseProc releaseP, ReleaseContext releaseC) { |
| 159 | return new_wrapped_texture_common(ctx, desc, at, kBorrow_GrWrapOwnership, releaseP, releaseC); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | SkImage* SkImage::NewFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc, |
| 163 | SkAlphaType at) { |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 164 | return new_wrapped_texture_common(ctx, desc, at, kAdopt_GrWrapOwnership, NULL, NULL); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 165 | } |
| 166 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 167 | SkImage* SkImage::NewFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& desc, |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 168 | SkAlphaType at) { |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 169 | if (desc.fWidth <= 0 || desc.fHeight <= 0) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 170 | return NULL; |
| 171 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 172 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 173 | SkAutoTUnref<GrTexture> src(ctx->textureProvider()->wrapBackendTexture( |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 174 | desc, kBorrow_GrWrapOwnership)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 175 | if (!src) { |
| 176 | return NULL; |
| 177 | } |
| 178 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 179 | const bool isBudgeted = true; |
| 180 | SkAutoTUnref<GrTexture> dst(GrDeepCopyTexture(src, isBudgeted)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 181 | if (!dst) { |
| 182 | return NULL; |
| 183 | } |
| 184 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 185 | const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 186 | const int sampleCount = 0; // todo: make this an explicit parameter to newSurface()? |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 187 | return SkNEW_ARGS(SkImage_Gpu, (desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, |
| 188 | at, dst, sampleCount, budgeted)); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 189 | } |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 190 | |
| 191 | SkImage* SkImage::NewFromYUVTexturesCopy(GrContext* ctx , SkYUVColorSpace colorSpace, |
| 192 | const GrBackendObject yuvTextureHandles[3], |
| 193 | const SkISize yuvSizes[3], |
| 194 | GrSurfaceOrigin origin) { |
| 195 | const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted; |
| 196 | |
| 197 | if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || |
| 198 | yuvSizes[1].fWidth <= 0 || yuvSizes[1].fHeight <= 0 || |
| 199 | yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0) { |
| 200 | return NULL; |
| 201 | } |
| 202 | static const GrPixelConfig kConfig = kAlpha_8_GrPixelConfig; |
| 203 | GrBackendTextureDesc yDesc; |
| 204 | yDesc.fConfig = kConfig; |
| 205 | yDesc.fOrigin = origin; |
| 206 | yDesc.fSampleCnt = 0; |
| 207 | yDesc.fTextureHandle = yuvTextureHandles[0]; |
| 208 | yDesc.fWidth = yuvSizes[0].fWidth; |
| 209 | yDesc.fHeight = yuvSizes[0].fHeight; |
| 210 | |
| 211 | GrBackendTextureDesc uDesc; |
| 212 | uDesc.fConfig = kConfig; |
| 213 | uDesc.fOrigin = origin; |
| 214 | uDesc.fSampleCnt = 0; |
| 215 | uDesc.fTextureHandle = yuvTextureHandles[1]; |
| 216 | uDesc.fWidth = yuvSizes[1].fWidth; |
| 217 | uDesc.fHeight = yuvSizes[1].fHeight; |
| 218 | |
| 219 | GrBackendTextureDesc vDesc; |
| 220 | vDesc.fConfig = kConfig; |
| 221 | vDesc.fOrigin = origin; |
| 222 | vDesc.fSampleCnt = 0; |
| 223 | vDesc.fTextureHandle = yuvTextureHandles[2]; |
| 224 | vDesc.fWidth = yuvSizes[2].fWidth; |
| 225 | vDesc.fHeight = yuvSizes[2].fHeight; |
| 226 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 227 | SkAutoTUnref<GrTexture> yTex(ctx->textureProvider()->wrapBackendTexture( |
| 228 | yDesc, kBorrow_GrWrapOwnership)); |
| 229 | SkAutoTUnref<GrTexture> uTex(ctx->textureProvider()->wrapBackendTexture( |
| 230 | uDesc, kBorrow_GrWrapOwnership)); |
| 231 | SkAutoTUnref<GrTexture> vTex(ctx->textureProvider()->wrapBackendTexture( |
| 232 | vDesc, kBorrow_GrWrapOwnership)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 233 | if (!yTex || !uTex || !vTex) { |
| 234 | return NULL; |
| 235 | } |
| 236 | |
| 237 | GrSurfaceDesc dstDesc; |
| 238 | // Needs to be a render target in order to draw to it for the yuv->rgb conversion. |
| 239 | dstDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 240 | dstDesc.fOrigin = origin; |
| 241 | dstDesc.fWidth = yuvSizes[0].fWidth; |
| 242 | dstDesc.fHeight = yuvSizes[0].fHeight; |
| 243 | dstDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 244 | dstDesc.fSampleCnt = 0; |
| 245 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 246 | SkAutoTUnref<GrTexture> dst(ctx->textureProvider()->createTexture(dstDesc, true)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 247 | if (!dst) { |
| 248 | return NULL; |
| 249 | } |
| 250 | |
| 251 | GrPaint paint; |
| 252 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
joshualitt | 2cdec31 | 2015-07-09 07:31:31 -0700 | [diff] [blame] | 253 | paint.addColorProcessor(GrYUVtoRGBEffect::Create(paint.getProcessorDataManager(), yTex, uTex, |
| 254 | vTex, yuvSizes, colorSpace))->unref(); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 255 | |
| 256 | const SkRect rect = SkRect::MakeWH(SkIntToScalar(dstDesc.fWidth), |
| 257 | SkIntToScalar(dstDesc.fHeight)); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 258 | GrDrawContext* drawContext = ctx->drawContext(); |
| 259 | drawContext->drawRect(dst->asRenderTarget(), GrClip::WideOpen(), paint, SkMatrix::I(), rect); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 260 | ctx->flushSurfaceWrites(dst); |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 261 | return SkNEW_ARGS(SkImage_Gpu, (dstDesc.fWidth, dstDesc.fHeight, kNeedNewImageUniqueID, |
| 262 | kOpaque_SkAlphaType, dst, 0, budgeted)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 263 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 264 | |
| 265 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 266 | |
| 267 | GrTexture* GrDeepCopyTexture(GrTexture* src, bool budgeted) { |
| 268 | GrContext* ctx = src->getContext(); |
| 269 | |
| 270 | GrSurfaceDesc desc = src->desc(); |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 271 | GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, NULL, 0); |
| 272 | if (!dst) { |
| 273 | return NULL; |
| 274 | } |
| 275 | |
| 276 | const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight); |
| 277 | const SkIPoint dstP = SkIPoint::Make(0, 0); |
| 278 | ctx->copySurface(dst, src, srcR, dstP, GrContext::kFlushWrites_PixelOp); |
| 279 | return dst; |
| 280 | } |
| 281 | |