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 | |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 80 | GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrTextureParams& params, |
| 81 | SkSourceGammaTreatment gammaTreatment) const { |
| 82 | return GrImageTextureAdjuster(as_IB(this)).refTextureSafeForParams(params, gammaTreatment, |
| 83 | nullptr); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 84 | } |
| 85 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 86 | bool SkImage_Gpu::isOpaque() const { |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 87 | return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_SkAlphaType; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 91 | switch (info.colorType()) { |
| 92 | case kRGBA_8888_SkColorType: |
| 93 | case kBGRA_8888_SkColorType: |
| 94 | break; |
| 95 | default: |
| 96 | return; // nothing to do |
| 97 | } |
| 98 | |
| 99 | // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian, |
| 100 | // and in either case, the alpha-byte is always in the same place, so we can safely call |
| 101 | // SkPreMultiplyColor() |
| 102 | // |
| 103 | SkColor* row = (SkColor*)pixels; |
| 104 | for (int y = 0; y < info.height(); ++y) { |
| 105 | for (int x = 0; x < info.width(); ++x) { |
| 106 | row[x] = SkPreMultiplyColor(row[x]); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 112 | int srcX, int srcY, CachingHint) const { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 113 | GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType(), |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 114 | info.profileType(), |
| 115 | *fTexture->getContext()->caps()); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 116 | uint32_t flags = 0; |
| 117 | if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) { |
| 118 | // let the GPU perform this transformation for us |
| 119 | flags = GrContext::kUnpremul_PixelOpsFlag; |
| 120 | } |
| 121 | if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config, |
| 122 | pixels, rowBytes, flags)) { |
| 123 | return false; |
| 124 | } |
| 125 | // do we have to manually fix-up the alpha channel? |
| 126 | // src dst |
| 127 | // unpremul premul fix manually |
| 128 | // premul unpremul done by kUnpremul_PixelOpsFlag |
| 129 | // all other combos need to change. |
| 130 | // |
| 131 | // Should this be handled by Ganesh? todo:? |
| 132 | // |
| 133 | if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) { |
| 134 | apply_premul(info, pixels, rowBytes); |
| 135 | } |
| 136 | return true; |
| 137 | } |
| 138 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 139 | sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const { |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 140 | GrContext* ctx = fTexture->getContext(); |
| 141 | GrSurfaceDesc desc = fTexture->desc(); |
| 142 | desc.fWidth = subset.width(); |
| 143 | desc.fHeight = subset.height(); |
| 144 | |
senorblanco | 8775112 | 2016-05-20 07:27:38 -0700 | [diff] [blame] | 145 | sk_sp<GrTexture> subTx(ctx->textureProvider()->createTexture(desc, fBudgeted)); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 146 | if (!subTx) { |
| 147 | return nullptr; |
| 148 | } |
senorblanco | 8775112 | 2016-05-20 07:27:38 -0700 | [diff] [blame] | 149 | ctx->copySurface(subTx.get(), fTexture, subset, SkIPoint::Make(0, 0)); |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 150 | return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, |
senorblanco | 8775112 | 2016-05-20 07:27:38 -0700 | [diff] [blame] | 151 | fAlphaType, subTx.get(), fBudgeted); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 152 | } |
| 153 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 154 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 155 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 156 | static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc, |
| 157 | SkAlphaType at, GrWrapOwnership ownership, |
| 158 | SkImage::TextureReleaseProc releaseProc, |
| 159 | SkImage::ReleaseContext releaseCtx) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 160 | if (desc.fWidth <= 0 || desc.fHeight <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 161 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 162 | } |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 163 | SkAutoTUnref<GrTexture> tex(ctx->textureProvider()->wrapBackendTexture(desc, ownership)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 164 | if (!tex) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 165 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 166 | } |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 167 | if (releaseProc) { |
| 168 | tex->setRelease(releaseProc, releaseCtx); |
| 169 | } |
| 170 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 171 | const SkBudgeted budgeted = SkBudgeted::kNo; |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 172 | return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, |
| 173 | at, tex, budgeted); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 174 | } |
| 175 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 176 | sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc, |
| 177 | SkAlphaType at, TextureReleaseProc releaseP, |
| 178 | ReleaseContext releaseC) { |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 179 | return new_wrapped_texture_common(ctx, desc, at, kBorrow_GrWrapOwnership, releaseP, releaseC); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 180 | } |
| 181 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 182 | sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc, |
| 183 | SkAlphaType at) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 184 | return new_wrapped_texture_common(ctx, desc, at, kAdopt_GrWrapOwnership, nullptr, nullptr); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 185 | } |
| 186 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 187 | sk_sp<SkImage> SkImage::MakeFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& desc, |
| 188 | SkAlphaType at) { |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 189 | if (desc.fWidth <= 0 || desc.fHeight <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 190 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 191 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 192 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 193 | SkAutoTUnref<GrTexture> src(ctx->textureProvider()->wrapBackendTexture( |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 194 | desc, kBorrow_GrWrapOwnership)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 195 | if (!src) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 196 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 197 | } |
| 198 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 199 | SkAutoTUnref<GrTexture> dst(GrDeepCopyTexture(src, SkBudgeted::kYes)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 200 | if (!dst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 201 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 202 | } |
| 203 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 204 | return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, dst, |
| 205 | SkBudgeted::kYes); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 206 | } |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 207 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame^] | 208 | static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace, |
| 209 | bool nv12, |
| 210 | const GrBackendObject yuvTextureHandles[], |
| 211 | const SkISize yuvSizes[], |
| 212 | GrSurfaceOrigin origin) { |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 213 | const SkBudgeted budgeted = SkBudgeted::kYes; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 214 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame^] | 215 | if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 || |
| 216 | yuvSizes[1].fHeight <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 217 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 218 | } |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame^] | 219 | if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) { |
| 220 | return nullptr; |
| 221 | } |
| 222 | |
| 223 | const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig; |
| 224 | |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 225 | GrBackendTextureDesc yDesc; |
| 226 | yDesc.fConfig = kConfig; |
| 227 | yDesc.fOrigin = origin; |
| 228 | yDesc.fSampleCnt = 0; |
| 229 | yDesc.fTextureHandle = yuvTextureHandles[0]; |
| 230 | yDesc.fWidth = yuvSizes[0].fWidth; |
| 231 | yDesc.fHeight = yuvSizes[0].fHeight; |
| 232 | |
| 233 | GrBackendTextureDesc uDesc; |
| 234 | uDesc.fConfig = kConfig; |
| 235 | uDesc.fOrigin = origin; |
| 236 | uDesc.fSampleCnt = 0; |
| 237 | uDesc.fTextureHandle = yuvTextureHandles[1]; |
| 238 | uDesc.fWidth = yuvSizes[1].fWidth; |
| 239 | uDesc.fHeight = yuvSizes[1].fHeight; |
| 240 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame^] | 241 | sk_sp<GrTexture> yTex( |
| 242 | ctx->textureProvider()->wrapBackendTexture(yDesc, kBorrow_GrWrapOwnership)); |
| 243 | sk_sp<GrTexture> uTex( |
| 244 | ctx->textureProvider()->wrapBackendTexture(uDesc, kBorrow_GrWrapOwnership)); |
| 245 | sk_sp<GrTexture> vTex; |
| 246 | if (nv12) { |
| 247 | vTex = uTex; |
| 248 | } else { |
| 249 | GrBackendTextureDesc vDesc; |
| 250 | vDesc.fConfig = kConfig; |
| 251 | vDesc.fOrigin = origin; |
| 252 | vDesc.fSampleCnt = 0; |
| 253 | vDesc.fTextureHandle = yuvTextureHandles[2]; |
| 254 | vDesc.fWidth = yuvSizes[2].fWidth; |
| 255 | vDesc.fHeight = yuvSizes[2].fHeight; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 256 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame^] | 257 | vTex = sk_sp<GrTexture>( |
| 258 | ctx->textureProvider()->wrapBackendTexture(vDesc, kBorrow_GrWrapOwnership)); |
| 259 | } |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 260 | if (!yTex || !uTex || !vTex) { |
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 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 264 | const int width = yuvSizes[0].fWidth; |
| 265 | const int height = yuvSizes[0].fHeight; |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 266 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 267 | // Needs to be a render target in order to draw to it for the yuv->rgb conversion. |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 268 | sk_sp<GrDrawContext> drawContext(ctx->newDrawContext(SkBackingFit::kExact, |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 269 | width, height, |
| 270 | kRGBA_8888_GrPixelConfig, |
| 271 | 0, |
| 272 | origin)); |
| 273 | if (!drawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 274 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | GrPaint paint; |
| 278 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame^] | 279 | paint.addColorFragmentProcessor( |
| 280 | GrYUVEffect::MakeYUVToRGB(yTex.get(), uTex.get(), vTex.get(), yuvSizes, colorSpace, nv12)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 281 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 282 | const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
robertphillips | c9a3706 | 2015-09-01 08:34:28 -0700 | [diff] [blame] | 283 | |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 284 | drawContext->drawRect(GrNoClip(), paint, SkMatrix::I(), rect); |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 285 | ctx->flushSurfaceWrites(drawContext->accessRenderTarget()); |
| 286 | return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID, |
| 287 | kOpaque_SkAlphaType, |
| 288 | drawContext->asTexture().get(), budgeted); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 289 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 290 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame^] | 291 | sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace, |
| 292 | const GrBackendObject yuvTextureHandles[3], |
| 293 | const SkISize yuvSizes[3], GrSurfaceOrigin origin) { |
| 294 | return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin); |
| 295 | } |
| 296 | |
| 297 | sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace, |
| 298 | const GrBackendObject yuvTextureHandles[2], |
| 299 | const SkISize yuvSizes[2], |
| 300 | GrSurfaceOrigin origin) { |
| 301 | return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin); |
| 302 | } |
| 303 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 304 | static sk_sp<SkImage> create_image_from_maker(GrTextureMaker* maker, SkAlphaType at, uint32_t id) { |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 305 | SkAutoTUnref<GrTexture> texture(maker->refTextureForParams(GrTextureParams::ClampNoFilter(), |
| 306 | SkSourceGammaTreatment::kRespect)); |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 307 | if (!texture) { |
| 308 | return nullptr; |
| 309 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 310 | return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), id, at, texture, |
| 311 | SkBudgeted::kNo); |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 312 | } |
| 313 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 314 | sk_sp<SkImage> SkImage::makeTextureImage(GrContext *context) const { |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 315 | if (!context) { |
| 316 | return nullptr; |
| 317 | } |
| 318 | if (GrTexture* peek = as_IB(this)->peekTexture()) { |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 319 | return peek->getContext() == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr; |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 320 | } |
| 321 | // No way to check whether a image is premul or not? |
| 322 | SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 323 | |
| 324 | if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) { |
| 325 | GrImageTextureMaker maker(context, cacher, this, kDisallow_CachingHint); |
| 326 | return create_image_from_maker(&maker, at, this->uniqueID()); |
| 327 | } |
| 328 | SkBitmap bmp; |
| 329 | if (!this->asLegacyBitmap(&bmp, kRO_LegacyBitmapMode)) { |
| 330 | return nullptr; |
| 331 | } |
| 332 | GrBitmapTextureMaker maker(context, bmp); |
| 333 | return create_image_from_maker(&maker, at, this->uniqueID()); |
| 334 | } |
| 335 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 336 | sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap, |
| 337 | SkBudgeted budgeted) { |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 338 | if (!ctx) { |
| 339 | return nullptr; |
| 340 | } |
ericrk | 8bea890 | 2016-03-18 11:52:20 -0700 | [diff] [blame] | 341 | SkAutoTUnref<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted)); |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 342 | if (!texture) { |
| 343 | return nullptr; |
| 344 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 345 | return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID, |
| 346 | pixmap.alphaType(), texture, budgeted); |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 347 | } |
| 348 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 349 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 350 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 351 | class DeferredTextureImage { |
| 352 | public: |
| 353 | SkImage* newImage(GrContext* context, SkBudgeted) const; |
| 354 | |
| 355 | private: |
| 356 | uint32_t fContextUniqueID; |
cblume | 2c05280 | 2016-05-31 09:55:08 -0700 | [diff] [blame] | 357 | struct MipMapLevelData { |
| 358 | void* fPixelData; |
| 359 | size_t fRowBytes; |
| 360 | }; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 361 | struct Data { |
| 362 | SkImageInfo fInfo; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 363 | int fColorTableCnt; |
| 364 | uint32_t* fColorTableData; |
cblume | 2c05280 | 2016-05-31 09:55:08 -0700 | [diff] [blame] | 365 | int fMipMapLevelCount; |
| 366 | // The fMipMapLevelData array may contain more than 1 element. |
| 367 | // It contains fMipMapLevelCount elements. |
| 368 | // That means this struct's size is not known at compile-time. |
| 369 | MipMapLevelData fMipMapLevelData[1]; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 370 | }; |
| 371 | Data fData; |
| 372 | |
| 373 | friend class SkImage; |
| 374 | }; |
| 375 | |
| 376 | size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy, |
| 377 | const DeferredTextureImageUsageParams[], |
| 378 | int paramCnt, void* buffer) const { |
| 379 | const bool fillMode = SkToBool(buffer); |
| 380 | if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) { |
| 381 | return 0; |
| 382 | } |
| 383 | |
ericrk | c429baf | 2016-03-24 15:35:45 -0700 | [diff] [blame] | 384 | const int maxTextureSize = proxy.fCaps->maxTextureSize(); |
| 385 | if (width() > maxTextureSize || height() > maxTextureSize) { |
| 386 | return 0; |
| 387 | } |
| 388 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 389 | SkAutoPixmapStorage pixmap; |
| 390 | SkImageInfo info; |
| 391 | size_t pixelSize = 0; |
| 392 | size_t ctSize = 0; |
| 393 | int ctCount = 0; |
| 394 | if (this->peekPixels(&pixmap)) { |
| 395 | info = pixmap.info(); |
| 396 | pixelSize = SkAlign8(pixmap.getSafeSize()); |
| 397 | if (pixmap.ctable()) { |
| 398 | ctCount = pixmap.ctable()->count(); |
| 399 | ctSize = SkAlign8(pixmap.ctable()->count() * 4); |
| 400 | } |
| 401 | } else { |
| 402 | // Here we're just using presence of data to know whether there is a codec behind the image. |
| 403 | // In the future we will access the cacherator and get the exact data that we want to (e.g. |
| 404 | // yuv planes) upload. |
| 405 | SkAutoTUnref<SkData> data(this->refEncoded()); |
| 406 | if (!data) { |
| 407 | return 0; |
| 408 | } |
| 409 | SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 410 | info = SkImageInfo::MakeN32(this->width(), this->height(), at); |
| 411 | pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr)); |
| 412 | if (fillMode) { |
| 413 | pixmap.alloc(info); |
| 414 | if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) { |
| 415 | return 0; |
| 416 | } |
| 417 | SkASSERT(!pixmap.ctable()); |
| 418 | } |
| 419 | } |
cblume | 2c05280 | 2016-05-31 09:55:08 -0700 | [diff] [blame] | 420 | int mipMapLevelCount = 1; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 421 | size_t size = 0; |
| 422 | size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage)); |
| 423 | size += dtiSize; |
cblume | 2c05280 | 2016-05-31 09:55:08 -0700 | [diff] [blame] | 424 | size += mipMapLevelCount * sizeof(DeferredTextureImage::MipMapLevelData); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 425 | size_t pixelOffset = size; |
| 426 | size += pixelSize; |
| 427 | size_t ctOffset = size; |
| 428 | size += ctSize; |
| 429 | if (!fillMode) { |
| 430 | return size; |
| 431 | } |
| 432 | intptr_t bufferAsInt = reinterpret_cast<intptr_t>(buffer); |
| 433 | void* pixels = reinterpret_cast<void*>(bufferAsInt + pixelOffset); |
| 434 | SkPMColor* ct = nullptr; |
| 435 | if (ctSize) { |
| 436 | ct = reinterpret_cast<SkPMColor*>(bufferAsInt + ctOffset); |
| 437 | } |
| 438 | |
| 439 | memcpy(pixels, pixmap.addr(), pixmap.getSafeSize()); |
| 440 | if (ctSize) { |
| 441 | memcpy(ct, pixmap.ctable()->readColors(), ctSize); |
| 442 | } |
| 443 | |
| 444 | SkASSERT(info == pixmap.info()); |
| 445 | size_t rowBytes = pixmap.rowBytes(); |
| 446 | DeferredTextureImage* dti = new (buffer) DeferredTextureImage(); |
| 447 | dti->fContextUniqueID = proxy.fContextUniqueID; |
| 448 | dti->fData.fInfo = info; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 449 | dti->fData.fColorTableCnt = ctCount; |
| 450 | dti->fData.fColorTableData = ct; |
cblume | 2c05280 | 2016-05-31 09:55:08 -0700 | [diff] [blame] | 451 | dti->fData.fMipMapLevelCount = mipMapLevelCount; |
| 452 | dti->fData.fMipMapLevelData[0].fPixelData = pixels; |
| 453 | dti->fData.fMipMapLevelData[0].fRowBytes = rowBytes; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 454 | return size; |
| 455 | } |
| 456 | |
| 457 | sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data, |
| 458 | SkBudgeted budgeted) { |
| 459 | if (!data) { |
| 460 | return nullptr; |
| 461 | } |
| 462 | const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data); |
| 463 | |
| 464 | if (!context || context->uniqueID() != dti->fContextUniqueID) { |
| 465 | return nullptr; |
| 466 | } |
| 467 | SkAutoTUnref<SkColorTable> colorTable; |
| 468 | if (dti->fData.fColorTableCnt) { |
| 469 | SkASSERT(dti->fData.fColorTableData); |
| 470 | colorTable.reset(new SkColorTable(dti->fData.fColorTableData, dti->fData.fColorTableCnt)); |
| 471 | } |
cblume | 2c05280 | 2016-05-31 09:55:08 -0700 | [diff] [blame] | 472 | SkASSERT(dti->fData.fMipMapLevelCount == 1); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 473 | SkPixmap pixmap; |
cblume | 2c05280 | 2016-05-31 09:55:08 -0700 | [diff] [blame] | 474 | pixmap.reset(dti->fData.fInfo, dti->fData.fMipMapLevelData[0].fPixelData, |
| 475 | dti->fData.fMipMapLevelData[0].fRowBytes, colorTable.get()); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 476 | return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted); |
| 477 | } |
| 478 | |
| 479 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 480 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 481 | GrTexture* GrDeepCopyTexture(GrTexture* src, SkBudgeted budgeted) { |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 482 | GrContext* ctx = src->getContext(); |
| 483 | |
| 484 | GrSurfaceDesc desc = src->desc(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 485 | GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullptr, 0); |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 486 | if (!dst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 487 | return nullptr; |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 488 | } |
cblume | 6121405 | 2016-01-26 09:10:48 -0800 | [diff] [blame] | 489 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 490 | const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight); |
| 491 | const SkIPoint dstP = SkIPoint::Make(0, 0); |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 492 | ctx->copySurface(dst, src, srcR, dstP); |
| 493 | ctx->flushSurfaceWrites(dst); |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 494 | return dst; |
| 495 | } |
| 496 | |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 497 | sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info, |
| 498 | const GrMipLevel* texels, int mipLevelCount, |
| 499 | SkBudgeted budgeted) { |
| 500 | if (!ctx) { |
| 501 | return nullptr; |
| 502 | } |
| 503 | SkAutoTUnref<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount)); |
| 504 | if (!texture) { |
| 505 | return nullptr; |
| 506 | } |
| 507 | return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID, |
| 508 | info.alphaType(), texture, budgeted); |
| 509 | } |