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 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 8 | #include "SkAutoPixmapStorage.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 9 | #include "GrCaps.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" |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 12 | #include "GrImageIDTextureAdjuster.h" |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 13 | #include "effects/GrYUVEffect.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 14 | #include "SkCanvas.h" |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 15 | #include "SkBitmapCache.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 "SkImage_Gpu.h" |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 19 | #include "SkPixelRef.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 20 | |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 21 | 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] | 22 | SkBudgeted budgeted) |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 23 | : INHERITED(w, h, uniqueID) |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 24 | , fTexture(SkRef(tex)) |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 25 | , fAlphaType(at) |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 26 | , fBudgeted(budgeted) |
bsalomon | 1cd6311 | 2015-08-05 06:58:39 -0700 | [diff] [blame] | 27 | , fAddedRasterVersionToCache(false) |
reed | c9b5f8b | 2015-10-22 13:20:20 -0700 | [diff] [blame] | 28 | { |
| 29 | SkASSERT(tex->width() == w); |
| 30 | SkASSERT(tex->height() == h); |
| 31 | } |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 32 | |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 33 | SkImage_Gpu::~SkImage_Gpu() { |
| 34 | if (fAddedRasterVersionToCache.load()) { |
| 35 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 36 | } |
| 37 | } |
| 38 | |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 39 | extern void SkTextureImageApplyBudgetedDecision(SkImage* image) { |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 40 | if (as_IB(image)->peekTexture()) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 41 | ((SkImage_Gpu*)image)->applyBudgetDecision(); |
| 42 | } |
| 43 | } |
| 44 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 45 | static SkImageInfo make_info(int w, int h, bool isOpaque) { |
| 46 | return SkImageInfo::MakeN32(w, h, isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
| 47 | } |
| 48 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 49 | bool SkImage_Gpu::getROPixels(SkBitmap* dst, CachingHint chint) const { |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 50 | if (SkBitmapCache::Find(this->uniqueID(), dst)) { |
| 51 | SkASSERT(dst->getGenerationID() == this->uniqueID()); |
| 52 | SkASSERT(dst->isImmutable()); |
| 53 | SkASSERT(dst->getPixels()); |
| 54 | return true; |
| 55 | } |
| 56 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 57 | if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->isOpaque()))) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 58 | return false; |
| 59 | } |
| 60 | if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig, |
| 61 | dst->getPixels(), dst->rowBytes())) { |
| 62 | return false; |
| 63 | } |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 64 | |
| 65 | dst->pixelRef()->setImmutableWithID(this->uniqueID()); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 66 | if (kAllow_CachingHint == chint) { |
| 67 | SkBitmapCache::Add(this->uniqueID(), *dst); |
| 68 | fAddedRasterVersionToCache.store(true); |
| 69 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 70 | return true; |
| 71 | } |
| 72 | |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 73 | bool SkImage_Gpu::asBitmapForImageFilters(SkBitmap* bitmap) const { |
| 74 | bitmap->setInfo(make_info(this->width(), this->height(), this->isOpaque())); |
| 75 | bitmap->setPixelRef(new SkGrPixelRef(bitmap->info(), fTexture))->unref(); |
| 76 | bitmap->pixelRef()->setImmutableWithID(this->uniqueID()); |
| 77 | return true; |
| 78 | } |
| 79 | |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 80 | GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrTextureParams& params) const { |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 81 | return GrImageTextureAdjuster(as_IB(this)).refTextureSafeForParams(params, nullptr); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 82 | } |
| 83 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 84 | bool SkImage_Gpu::isOpaque() const { |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 85 | return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_SkAlphaType; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 89 | switch (info.colorType()) { |
| 90 | case kRGBA_8888_SkColorType: |
| 91 | case kBGRA_8888_SkColorType: |
| 92 | break; |
| 93 | default: |
| 94 | return; // nothing to do |
| 95 | } |
| 96 | |
| 97 | // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian, |
| 98 | // and in either case, the alpha-byte is always in the same place, so we can safely call |
| 99 | // SkPreMultiplyColor() |
| 100 | // |
| 101 | SkColor* row = (SkColor*)pixels; |
| 102 | for (int y = 0; y < info.height(); ++y) { |
| 103 | for (int x = 0; x < info.width(); ++x) { |
| 104 | row[x] = SkPreMultiplyColor(row[x]); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 110 | int srcX, int srcY, CachingHint) const { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 111 | GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType(), |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 112 | info.profileType(), |
| 113 | *fTexture->getContext()->caps()); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 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 | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 137 | sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const { |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 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)); |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 148 | return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, |
| 149 | fAlphaType, subTx, fBudgeted); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 150 | } |
| 151 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 152 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 153 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 154 | static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc, |
| 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 | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 170 | return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, |
| 171 | at, tex, budgeted); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 172 | } |
| 173 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 174 | sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc, |
| 175 | SkAlphaType at, TextureReleaseProc releaseP, |
| 176 | ReleaseContext releaseC) { |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 177 | return new_wrapped_texture_common(ctx, desc, at, kBorrow_GrWrapOwnership, releaseP, releaseC); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 178 | } |
| 179 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 180 | sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc, |
| 181 | SkAlphaType at) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 182 | return new_wrapped_texture_common(ctx, desc, at, kAdopt_GrWrapOwnership, nullptr, nullptr); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 183 | } |
| 184 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 185 | sk_sp<SkImage> SkImage::MakeFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& desc, |
| 186 | SkAlphaType at) { |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 187 | if (desc.fWidth <= 0 || desc.fHeight <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 188 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 189 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 190 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 191 | SkAutoTUnref<GrTexture> src(ctx->textureProvider()->wrapBackendTexture( |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 192 | desc, kBorrow_GrWrapOwnership)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 193 | if (!src) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 194 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 195 | } |
| 196 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 197 | SkAutoTUnref<GrTexture> dst(GrDeepCopyTexture(src, SkBudgeted::kYes)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 198 | if (!dst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 199 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 200 | } |
| 201 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 202 | return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, dst, |
| 203 | SkBudgeted::kYes); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 204 | } |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 205 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 206 | sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx , SkYUVColorSpace colorSpace, |
| 207 | const GrBackendObject yuvTextureHandles[3], |
| 208 | const SkISize yuvSizes[3], |
| 209 | GrSurfaceOrigin origin) { |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 210 | const SkBudgeted budgeted = SkBudgeted::kYes; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 211 | |
| 212 | if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || |
| 213 | yuvSizes[1].fWidth <= 0 || yuvSizes[1].fHeight <= 0 || |
| 214 | yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 215 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 216 | } |
| 217 | static const GrPixelConfig kConfig = kAlpha_8_GrPixelConfig; |
| 218 | GrBackendTextureDesc yDesc; |
| 219 | yDesc.fConfig = kConfig; |
| 220 | yDesc.fOrigin = origin; |
| 221 | yDesc.fSampleCnt = 0; |
| 222 | yDesc.fTextureHandle = yuvTextureHandles[0]; |
| 223 | yDesc.fWidth = yuvSizes[0].fWidth; |
| 224 | yDesc.fHeight = yuvSizes[0].fHeight; |
| 225 | |
| 226 | GrBackendTextureDesc uDesc; |
| 227 | uDesc.fConfig = kConfig; |
| 228 | uDesc.fOrigin = origin; |
| 229 | uDesc.fSampleCnt = 0; |
| 230 | uDesc.fTextureHandle = yuvTextureHandles[1]; |
| 231 | uDesc.fWidth = yuvSizes[1].fWidth; |
| 232 | uDesc.fHeight = yuvSizes[1].fHeight; |
| 233 | |
| 234 | GrBackendTextureDesc vDesc; |
| 235 | vDesc.fConfig = kConfig; |
| 236 | vDesc.fOrigin = origin; |
| 237 | vDesc.fSampleCnt = 0; |
| 238 | vDesc.fTextureHandle = yuvTextureHandles[2]; |
| 239 | vDesc.fWidth = yuvSizes[2].fWidth; |
| 240 | vDesc.fHeight = yuvSizes[2].fHeight; |
| 241 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 242 | SkAutoTUnref<GrTexture> yTex(ctx->textureProvider()->wrapBackendTexture( |
| 243 | yDesc, kBorrow_GrWrapOwnership)); |
| 244 | SkAutoTUnref<GrTexture> uTex(ctx->textureProvider()->wrapBackendTexture( |
| 245 | uDesc, kBorrow_GrWrapOwnership)); |
| 246 | SkAutoTUnref<GrTexture> vTex(ctx->textureProvider()->wrapBackendTexture( |
| 247 | vDesc, kBorrow_GrWrapOwnership)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 248 | if (!yTex || !uTex || !vTex) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 249 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 250 | } |
| 251 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame^] | 252 | const int width = yuvSizes[0].fWidth; |
| 253 | const int height = yuvSizes[0].fHeight; |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 254 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame^] | 255 | // Needs to be a render target in order to draw to it for the yuv->rgb conversion. |
| 256 | sk_sp<GrDrawContext> drawContext(ctx->newDrawContext(GrContext::kTight_BackingFit, |
| 257 | width, height, |
| 258 | kRGBA_8888_GrPixelConfig, |
| 259 | 0, |
| 260 | origin)); |
| 261 | if (!drawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 262 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | GrPaint paint; |
| 266 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 267 | paint.addColorFragmentProcessor(GrYUVEffect::CreateYUVToRGB(yTex, uTex, vTex, yuvSizes, |
| 268 | colorSpace))->unref(); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 269 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame^] | 270 | const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
robertphillips | c9a3706 | 2015-09-01 08:34:28 -0700 | [diff] [blame] | 271 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 272 | drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), rect); |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame^] | 273 | ctx->flushSurfaceWrites(drawContext->accessRenderTarget()); |
| 274 | return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID, |
| 275 | kOpaque_SkAlphaType, |
| 276 | drawContext->asTexture().get(), budgeted); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 277 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 278 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 279 | static sk_sp<SkImage> create_image_from_maker(GrTextureMaker* maker, SkAlphaType at, uint32_t id) { |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 280 | SkAutoTUnref<GrTexture> texture(maker->refTextureForParams(GrTextureParams::ClampNoFilter())); |
| 281 | if (!texture) { |
| 282 | return nullptr; |
| 283 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 284 | return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), id, at, texture, |
| 285 | SkBudgeted::kNo); |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 286 | } |
| 287 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 288 | sk_sp<SkImage> SkImage::makeTextureImage(GrContext *context) const { |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 289 | if (!context) { |
| 290 | return nullptr; |
| 291 | } |
| 292 | if (GrTexture* peek = as_IB(this)->peekTexture()) { |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 293 | return peek->getContext() == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr; |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 294 | } |
| 295 | // No way to check whether a image is premul or not? |
| 296 | SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 297 | |
| 298 | if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) { |
| 299 | GrImageTextureMaker maker(context, cacher, this, kDisallow_CachingHint); |
| 300 | return create_image_from_maker(&maker, at, this->uniqueID()); |
| 301 | } |
| 302 | SkBitmap bmp; |
| 303 | if (!this->asLegacyBitmap(&bmp, kRO_LegacyBitmapMode)) { |
| 304 | return nullptr; |
| 305 | } |
| 306 | GrBitmapTextureMaker maker(context, bmp); |
| 307 | return create_image_from_maker(&maker, at, this->uniqueID()); |
| 308 | } |
| 309 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 310 | sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap, |
| 311 | SkBudgeted budgeted) { |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 312 | if (!ctx) { |
| 313 | return nullptr; |
| 314 | } |
ericrk | 8bea890 | 2016-03-18 11:52:20 -0700 | [diff] [blame] | 315 | SkAutoTUnref<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted)); |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 316 | if (!texture) { |
| 317 | return nullptr; |
| 318 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 319 | return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID, |
| 320 | pixmap.alphaType(), texture, budgeted); |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 321 | } |
| 322 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 323 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 324 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 325 | class DeferredTextureImage { |
| 326 | public: |
| 327 | SkImage* newImage(GrContext* context, SkBudgeted) const; |
| 328 | |
| 329 | private: |
| 330 | uint32_t fContextUniqueID; |
| 331 | struct Data { |
| 332 | SkImageInfo fInfo; |
| 333 | void* fPixelData; |
| 334 | size_t fRowBytes; |
| 335 | int fColorTableCnt; |
| 336 | uint32_t* fColorTableData; |
| 337 | }; |
| 338 | Data fData; |
| 339 | |
| 340 | friend class SkImage; |
| 341 | }; |
| 342 | |
| 343 | size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy, |
| 344 | const DeferredTextureImageUsageParams[], |
| 345 | int paramCnt, void* buffer) const { |
| 346 | const bool fillMode = SkToBool(buffer); |
| 347 | if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) { |
| 348 | return 0; |
| 349 | } |
| 350 | |
ericrk | c429baf | 2016-03-24 15:35:45 -0700 | [diff] [blame] | 351 | const int maxTextureSize = proxy.fCaps->maxTextureSize(); |
| 352 | if (width() > maxTextureSize || height() > maxTextureSize) { |
| 353 | return 0; |
| 354 | } |
| 355 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 356 | SkAutoPixmapStorage pixmap; |
| 357 | SkImageInfo info; |
| 358 | size_t pixelSize = 0; |
| 359 | size_t ctSize = 0; |
| 360 | int ctCount = 0; |
| 361 | if (this->peekPixels(&pixmap)) { |
| 362 | info = pixmap.info(); |
| 363 | pixelSize = SkAlign8(pixmap.getSafeSize()); |
| 364 | if (pixmap.ctable()) { |
| 365 | ctCount = pixmap.ctable()->count(); |
| 366 | ctSize = SkAlign8(pixmap.ctable()->count() * 4); |
| 367 | } |
| 368 | } else { |
| 369 | // Here we're just using presence of data to know whether there is a codec behind the image. |
| 370 | // In the future we will access the cacherator and get the exact data that we want to (e.g. |
| 371 | // yuv planes) upload. |
| 372 | SkAutoTUnref<SkData> data(this->refEncoded()); |
| 373 | if (!data) { |
| 374 | return 0; |
| 375 | } |
| 376 | SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 377 | info = SkImageInfo::MakeN32(this->width(), this->height(), at); |
| 378 | pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr)); |
| 379 | if (fillMode) { |
| 380 | pixmap.alloc(info); |
| 381 | if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) { |
| 382 | return 0; |
| 383 | } |
| 384 | SkASSERT(!pixmap.ctable()); |
| 385 | } |
| 386 | } |
| 387 | size_t size = 0; |
| 388 | size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage)); |
| 389 | size += dtiSize; |
| 390 | size_t pixelOffset = size; |
| 391 | size += pixelSize; |
| 392 | size_t ctOffset = size; |
| 393 | size += ctSize; |
| 394 | if (!fillMode) { |
| 395 | return size; |
| 396 | } |
| 397 | intptr_t bufferAsInt = reinterpret_cast<intptr_t>(buffer); |
| 398 | void* pixels = reinterpret_cast<void*>(bufferAsInt + pixelOffset); |
| 399 | SkPMColor* ct = nullptr; |
| 400 | if (ctSize) { |
| 401 | ct = reinterpret_cast<SkPMColor*>(bufferAsInt + ctOffset); |
| 402 | } |
| 403 | |
| 404 | memcpy(pixels, pixmap.addr(), pixmap.getSafeSize()); |
| 405 | if (ctSize) { |
| 406 | memcpy(ct, pixmap.ctable()->readColors(), ctSize); |
| 407 | } |
| 408 | |
| 409 | SkASSERT(info == pixmap.info()); |
| 410 | size_t rowBytes = pixmap.rowBytes(); |
| 411 | DeferredTextureImage* dti = new (buffer) DeferredTextureImage(); |
| 412 | dti->fContextUniqueID = proxy.fContextUniqueID; |
| 413 | dti->fData.fInfo = info; |
| 414 | dti->fData.fPixelData = pixels; |
| 415 | dti->fData.fRowBytes = rowBytes; |
| 416 | dti->fData.fColorTableCnt = ctCount; |
| 417 | dti->fData.fColorTableData = ct; |
| 418 | return size; |
| 419 | } |
| 420 | |
| 421 | sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data, |
| 422 | SkBudgeted budgeted) { |
| 423 | if (!data) { |
| 424 | return nullptr; |
| 425 | } |
| 426 | const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data); |
| 427 | |
| 428 | if (!context || context->uniqueID() != dti->fContextUniqueID) { |
| 429 | return nullptr; |
| 430 | } |
| 431 | SkAutoTUnref<SkColorTable> colorTable; |
| 432 | if (dti->fData.fColorTableCnt) { |
| 433 | SkASSERT(dti->fData.fColorTableData); |
| 434 | colorTable.reset(new SkColorTable(dti->fData.fColorTableData, dti->fData.fColorTableCnt)); |
| 435 | } |
| 436 | SkPixmap pixmap; |
| 437 | pixmap.reset(dti->fData.fInfo, dti->fData.fPixelData, dti->fData.fRowBytes, colorTable.get()); |
| 438 | return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted); |
| 439 | } |
| 440 | |
| 441 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 442 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 443 | GrTexture* GrDeepCopyTexture(GrTexture* src, SkBudgeted budgeted) { |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 444 | GrContext* ctx = src->getContext(); |
| 445 | |
| 446 | GrSurfaceDesc desc = src->desc(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 447 | GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullptr, 0); |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 448 | if (!dst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 449 | return nullptr; |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 450 | } |
cblume | 6121405 | 2016-01-26 09:10:48 -0800 | [diff] [blame] | 451 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 452 | const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight); |
| 453 | const SkIPoint dstP = SkIPoint::Make(0, 0); |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 454 | ctx->copySurface(dst, src, srcR, dstP); |
| 455 | ctx->flushSurfaceWrites(dst); |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 456 | return dst; |
| 457 | } |
| 458 | |