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