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 | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 8 | #include "GrCaps.h" |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 9 | #include "GrContext.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 10 | #include "GrDrawContext.h" |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 11 | #include "GrImageIDTextureAdjuster.h" |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 12 | #include "effects/GrYUVEffect.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 13 | #include "SkCanvas.h" |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 14 | #include "SkBitmapCache.h" |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 15 | #include "SkGpuDevice.h" |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 16 | #include "SkGrPixelRef.h" |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 17 | #include "SkGrPriv.h" |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 18 | #include "SkImageFilter.h" |
| 19 | #include "SkImage_Gpu.h" |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 20 | #include "SkPixelRef.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 21 | |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 22 | SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, GrTexture* tex, |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 23 | SkBudgeted budgeted) |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 24 | : INHERITED(w, h, uniqueID) |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 25 | , fTexture(SkRef(tex)) |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 26 | , fAlphaType(at) |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 27 | , fBudgeted(budgeted) |
bsalomon | 1cd6311 | 2015-08-05 06:58:39 -0700 | [diff] [blame] | 28 | , fAddedRasterVersionToCache(false) |
reed | c9b5f8b | 2015-10-22 13:20:20 -0700 | [diff] [blame] | 29 | { |
| 30 | SkASSERT(tex->width() == w); |
| 31 | SkASSERT(tex->height() == h); |
| 32 | } |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 33 | |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 34 | SkImage_Gpu::~SkImage_Gpu() { |
| 35 | if (fAddedRasterVersionToCache.load()) { |
| 36 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 37 | } |
| 38 | } |
| 39 | |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 40 | extern void SkTextureImageApplyBudgetedDecision(SkImage* image) { |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 41 | if (as_IB(image)->getTexture()) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 42 | ((SkImage_Gpu*)image)->applyBudgetDecision(); |
| 43 | } |
| 44 | } |
| 45 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 46 | static SkImageInfo make_info(int w, int h, bool isOpaque) { |
| 47 | return SkImageInfo::MakeN32(w, h, isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
| 48 | } |
| 49 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 50 | bool SkImage_Gpu::getROPixels(SkBitmap* dst, CachingHint chint) const { |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 51 | if (SkBitmapCache::Find(this->uniqueID(), dst)) { |
| 52 | SkASSERT(dst->getGenerationID() == this->uniqueID()); |
| 53 | SkASSERT(dst->isImmutable()); |
| 54 | SkASSERT(dst->getPixels()); |
| 55 | return true; |
| 56 | } |
| 57 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 58 | if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->isOpaque()))) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 59 | return false; |
| 60 | } |
| 61 | if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig, |
| 62 | dst->getPixels(), dst->rowBytes())) { |
| 63 | return false; |
| 64 | } |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 65 | |
| 66 | dst->pixelRef()->setImmutableWithID(this->uniqueID()); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 67 | if (kAllow_CachingHint == chint) { |
| 68 | SkBitmapCache::Add(this->uniqueID(), *dst); |
| 69 | fAddedRasterVersionToCache.store(true); |
| 70 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 71 | return true; |
| 72 | } |
| 73 | |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 74 | bool SkImage_Gpu::asBitmapForImageFilters(SkBitmap* bitmap) const { |
| 75 | bitmap->setInfo(make_info(this->width(), this->height(), this->isOpaque())); |
| 76 | bitmap->setPixelRef(new SkGrPixelRef(bitmap->info(), fTexture))->unref(); |
| 77 | bitmap->pixelRef()->setImmutableWithID(this->uniqueID()); |
| 78 | return true; |
| 79 | } |
| 80 | |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 81 | GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrTextureParams& params) const { |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 82 | return GrImageTextureAdjuster(as_IB(this)).refTextureSafeForParams(params, nullptr); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 83 | } |
| 84 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 85 | bool SkImage_Gpu::isOpaque() const { |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 86 | return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_SkAlphaType; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 90 | switch (info.colorType()) { |
| 91 | case kRGBA_8888_SkColorType: |
| 92 | case kBGRA_8888_SkColorType: |
| 93 | break; |
| 94 | default: |
| 95 | return; // nothing to do |
| 96 | } |
| 97 | |
| 98 | // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian, |
| 99 | // and in either case, the alpha-byte is always in the same place, so we can safely call |
| 100 | // SkPreMultiplyColor() |
| 101 | // |
| 102 | SkColor* row = (SkColor*)pixels; |
| 103 | for (int y = 0; y < info.height(); ++y) { |
| 104 | for (int x = 0; x < info.width(); ++x) { |
| 105 | row[x] = SkPreMultiplyColor(row[x]); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 111 | int srcX, int srcY, CachingHint) const { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 112 | GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType(), |
| 113 | info.profileType()); |
| 114 | uint32_t flags = 0; |
| 115 | if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) { |
| 116 | // let the GPU perform this transformation for us |
| 117 | flags = GrContext::kUnpremul_PixelOpsFlag; |
| 118 | } |
| 119 | if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config, |
| 120 | pixels, rowBytes, flags)) { |
| 121 | return false; |
| 122 | } |
| 123 | // do we have to manually fix-up the alpha channel? |
| 124 | // src dst |
| 125 | // unpremul premul fix manually |
| 126 | // premul unpremul done by kUnpremul_PixelOpsFlag |
| 127 | // all other combos need to change. |
| 128 | // |
| 129 | // Should this be handled by Ganesh? todo:? |
| 130 | // |
| 131 | if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) { |
| 132 | apply_premul(info, pixels, rowBytes); |
| 133 | } |
| 134 | return true; |
| 135 | } |
| 136 | |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 137 | SkImage* SkImage_Gpu::onNewSubset(const SkIRect& subset) const { |
| 138 | GrContext* ctx = fTexture->getContext(); |
| 139 | GrSurfaceDesc desc = fTexture->desc(); |
| 140 | desc.fWidth = subset.width(); |
| 141 | desc.fHeight = subset.height(); |
| 142 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 143 | GrTexture* subTx = ctx->textureProvider()->createTexture(desc, fBudgeted); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 144 | if (!subTx) { |
| 145 | return nullptr; |
| 146 | } |
| 147 | ctx->copySurface(subTx, fTexture, subset, SkIPoint::Make(0, 0)); |
| 148 | return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, fAlphaType, subTx, |
| 149 | fBudgeted); |
| 150 | } |
| 151 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 152 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 153 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 154 | static SkImage* new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc, |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 155 | SkAlphaType at, GrWrapOwnership ownership, |
| 156 | SkImage::TextureReleaseProc releaseProc, |
| 157 | SkImage::ReleaseContext releaseCtx) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 158 | if (desc.fWidth <= 0 || desc.fHeight <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 159 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 160 | } |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 161 | SkAutoTUnref<GrTexture> tex(ctx->textureProvider()->wrapBackendTexture(desc, ownership)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 162 | if (!tex) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 163 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 164 | } |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 165 | if (releaseProc) { |
| 166 | tex->setRelease(releaseProc, releaseCtx); |
| 167 | } |
| 168 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 169 | const SkBudgeted budgeted = SkBudgeted::kNo; |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 170 | return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, tex, budgeted); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 171 | } |
| 172 | |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 173 | SkImage* SkImage::NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc, SkAlphaType at, |
| 174 | TextureReleaseProc releaseP, ReleaseContext releaseC) { |
| 175 | return new_wrapped_texture_common(ctx, desc, at, kBorrow_GrWrapOwnership, releaseP, releaseC); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | SkImage* SkImage::NewFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc, |
| 179 | SkAlphaType at) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 180 | return new_wrapped_texture_common(ctx, desc, at, kAdopt_GrWrapOwnership, nullptr, nullptr); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 181 | } |
| 182 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 183 | SkImage* SkImage::NewFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& desc, |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 184 | SkAlphaType at) { |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 185 | if (desc.fWidth <= 0 || desc.fHeight <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 186 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 187 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 188 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 189 | SkAutoTUnref<GrTexture> src(ctx->textureProvider()->wrapBackendTexture( |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 190 | desc, kBorrow_GrWrapOwnership)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 191 | if (!src) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 192 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 193 | } |
| 194 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 195 | SkAutoTUnref<GrTexture> dst(GrDeepCopyTexture(src, SkBudgeted::kYes)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 196 | if (!dst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 197 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 198 | } |
| 199 | |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 200 | return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, dst, |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 201 | SkBudgeted::kYes); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 202 | } |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 203 | |
| 204 | SkImage* SkImage::NewFromYUVTexturesCopy(GrContext* ctx , SkYUVColorSpace colorSpace, |
| 205 | const GrBackendObject yuvTextureHandles[3], |
| 206 | const SkISize yuvSizes[3], |
| 207 | GrSurfaceOrigin origin) { |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 208 | const SkBudgeted budgeted = SkBudgeted::kYes; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 209 | |
| 210 | if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || |
| 211 | yuvSizes[1].fWidth <= 0 || yuvSizes[1].fHeight <= 0 || |
| 212 | yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 213 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 214 | } |
| 215 | static const GrPixelConfig kConfig = kAlpha_8_GrPixelConfig; |
| 216 | GrBackendTextureDesc yDesc; |
| 217 | yDesc.fConfig = kConfig; |
| 218 | yDesc.fOrigin = origin; |
| 219 | yDesc.fSampleCnt = 0; |
| 220 | yDesc.fTextureHandle = yuvTextureHandles[0]; |
| 221 | yDesc.fWidth = yuvSizes[0].fWidth; |
| 222 | yDesc.fHeight = yuvSizes[0].fHeight; |
| 223 | |
| 224 | GrBackendTextureDesc uDesc; |
| 225 | uDesc.fConfig = kConfig; |
| 226 | uDesc.fOrigin = origin; |
| 227 | uDesc.fSampleCnt = 0; |
| 228 | uDesc.fTextureHandle = yuvTextureHandles[1]; |
| 229 | uDesc.fWidth = yuvSizes[1].fWidth; |
| 230 | uDesc.fHeight = yuvSizes[1].fHeight; |
| 231 | |
| 232 | GrBackendTextureDesc vDesc; |
| 233 | vDesc.fConfig = kConfig; |
| 234 | vDesc.fOrigin = origin; |
| 235 | vDesc.fSampleCnt = 0; |
| 236 | vDesc.fTextureHandle = yuvTextureHandles[2]; |
| 237 | vDesc.fWidth = yuvSizes[2].fWidth; |
| 238 | vDesc.fHeight = yuvSizes[2].fHeight; |
| 239 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 240 | SkAutoTUnref<GrTexture> yTex(ctx->textureProvider()->wrapBackendTexture( |
| 241 | yDesc, kBorrow_GrWrapOwnership)); |
| 242 | SkAutoTUnref<GrTexture> uTex(ctx->textureProvider()->wrapBackendTexture( |
| 243 | uDesc, kBorrow_GrWrapOwnership)); |
| 244 | SkAutoTUnref<GrTexture> vTex(ctx->textureProvider()->wrapBackendTexture( |
| 245 | vDesc, kBorrow_GrWrapOwnership)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 246 | if (!yTex || !uTex || !vTex) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 247 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | GrSurfaceDesc dstDesc; |
| 251 | // Needs to be a render target in order to draw to it for the yuv->rgb conversion. |
| 252 | dstDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 253 | dstDesc.fOrigin = origin; |
| 254 | dstDesc.fWidth = yuvSizes[0].fWidth; |
| 255 | dstDesc.fHeight = yuvSizes[0].fHeight; |
| 256 | dstDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 257 | dstDesc.fSampleCnt = 0; |
| 258 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 259 | SkAutoTUnref<GrTexture> dst(ctx->textureProvider()->createTexture(dstDesc, SkBudgeted::kYes)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 260 | if (!dst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 261 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | GrPaint paint; |
| 265 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 266 | paint.addColorFragmentProcessor(GrYUVEffect::CreateYUVToRGB(yTex, uTex, vTex, yuvSizes, |
| 267 | colorSpace))->unref(); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 268 | |
| 269 | const SkRect rect = SkRect::MakeWH(SkIntToScalar(dstDesc.fWidth), |
| 270 | SkIntToScalar(dstDesc.fHeight)); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 271 | SkAutoTUnref<GrDrawContext> drawContext(ctx->drawContext(dst->asRenderTarget())); |
robertphillips | c9a3706 | 2015-09-01 08:34:28 -0700 | [diff] [blame] | 272 | if (!drawContext) { |
| 273 | return nullptr; |
| 274 | } |
| 275 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 276 | drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), rect); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 277 | ctx->flushSurfaceWrites(dst); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 278 | return new SkImage_Gpu(dstDesc.fWidth, dstDesc.fHeight, kNeedNewImageUniqueID, |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 279 | kOpaque_SkAlphaType, dst, budgeted); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 280 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 281 | |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 282 | static SkImage* create_image_from_maker(GrTextureMaker* maker, SkAlphaType at, uint32_t id) { |
| 283 | SkAutoTUnref<GrTexture> texture(maker->refTextureForParams(GrTextureParams::ClampNoFilter())); |
| 284 | if (!texture) { |
| 285 | return nullptr; |
| 286 | } |
| 287 | return new SkImage_Gpu(texture->width(), texture->height(), id, at, texture, |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 288 | SkBudgeted::kNo); |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | SkImage* SkImage::newTextureImage(GrContext *context) const { |
| 292 | if (!context) { |
| 293 | return nullptr; |
| 294 | } |
| 295 | if (GrTexture* peek = as_IB(this)->peekTexture()) { |
| 296 | return peek->getContext() == context ? SkRef(const_cast<SkImage*>(this)) : nullptr; |
| 297 | } |
| 298 | // No way to check whether a image is premul or not? |
| 299 | SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 300 | |
| 301 | if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) { |
| 302 | GrImageTextureMaker maker(context, cacher, this, kDisallow_CachingHint); |
| 303 | return create_image_from_maker(&maker, at, this->uniqueID()); |
| 304 | } |
| 305 | SkBitmap bmp; |
| 306 | if (!this->asLegacyBitmap(&bmp, kRO_LegacyBitmapMode)) { |
| 307 | return nullptr; |
| 308 | } |
| 309 | GrBitmapTextureMaker maker(context, bmp); |
| 310 | return create_image_from_maker(&maker, at, this->uniqueID()); |
| 311 | } |
| 312 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 313 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 314 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 315 | GrTexture* GrDeepCopyTexture(GrTexture* src, SkBudgeted budgeted) { |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 316 | GrContext* ctx = src->getContext(); |
| 317 | |
| 318 | GrSurfaceDesc desc = src->desc(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 319 | GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullptr, 0); |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 320 | if (!dst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 321 | return nullptr; |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 322 | } |
cblume | 6121405 | 2016-01-26 09:10:48 -0800 | [diff] [blame] | 323 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 324 | const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight); |
| 325 | const SkIPoint dstP = SkIPoint::Make(0, 0); |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 326 | ctx->copySurface(dst, src, srcR, dstP); |
| 327 | ctx->flushSurfaceWrites(dst); |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 328 | return dst; |
| 329 | } |
| 330 | |