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