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