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 | |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 8 | #include <cstddef> |
| 9 | #include <cstring> |
| 10 | #include <type_traits> |
| 11 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 12 | #include "SkAutoPixmapStorage.h" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 13 | #include "GrBitmapTextureMaker.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 14 | #include "GrCaps.h" |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 15 | #include "GrContext.h" |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 16 | #include "GrContextPriv.h" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 17 | #include "GrImageTextureMaker.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 18 | #include "GrRenderTargetContext.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 19 | #include "GrTextureAdjuster.h" |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 20 | #include "GrTexturePriv.h" |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 21 | #include "GrTextureProxy.h" |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 22 | #include "effects/GrYUVEffect.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 23 | #include "SkCanvas.h" |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 24 | #include "SkBitmapCache.h" |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 25 | #include "SkGrPriv.h" |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 26 | #include "SkImage_Gpu.h" |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 27 | #include "SkImageCacherator.h" |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 28 | #include "SkImageInfoPriv.h" |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 29 | #include "SkMipMap.h" |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 30 | #include "SkPixelRef.h" |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 31 | #include "SkReadPixelsRec.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 32 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 33 | SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, sk_sp<GrTexture> tex, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 34 | sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted) |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 35 | : INHERITED(w, h, uniqueID) |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 36 | , fTexture(std::move(tex)) |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 37 | , fAlphaType(at) |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 38 | , fBudgeted(budgeted) |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 39 | , fColorSpace(std::move(colorSpace)) |
bsalomon | 1cd6311 | 2015-08-05 06:58:39 -0700 | [diff] [blame] | 40 | , fAddedRasterVersionToCache(false) |
reed | c9b5f8b | 2015-10-22 13:20:20 -0700 | [diff] [blame] | 41 | { |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 42 | SkASSERT(fTexture->width() == w); |
| 43 | SkASSERT(fTexture->height() == h); |
reed | c9b5f8b | 2015-10-22 13:20:20 -0700 | [diff] [blame] | 44 | } |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 45 | |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 46 | SkImage_Gpu::~SkImage_Gpu() { |
| 47 | if (fAddedRasterVersionToCache.load()) { |
| 48 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 49 | } |
| 50 | } |
| 51 | |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 52 | extern void SkTextureImageApplyBudgetedDecision(SkImage* image) { |
robertphillips | ea70c4b | 2016-07-20 08:54:31 -0700 | [diff] [blame] | 53 | if (image->isTextureBacked()) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 54 | ((SkImage_Gpu*)image)->applyBudgetDecision(); |
| 55 | } |
| 56 | } |
| 57 | |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 58 | SkImageInfo SkImage_Gpu::onImageInfo() const { |
| 59 | SkColorType ct; |
| 60 | if (!GrPixelConfigToColorType(fTexture->config(), &ct)) { |
| 61 | ct = kUnknown_SkColorType; |
| 62 | } |
| 63 | return SkImageInfo::Make(fTexture->width(), fTexture->height(), ct, fAlphaType, fColorSpace); |
| 64 | } |
| 65 | |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 66 | static SkImageInfo make_info(int w, int h, SkAlphaType at, sk_sp<SkColorSpace> colorSpace) { |
| 67 | return SkImageInfo::MakeN32(w, h, at, std::move(colorSpace)); |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 70 | bool SkImage_Gpu::getROPixels(SkBitmap* dst, SkColorSpace* dstColorSpace, |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 71 | CachingHint chint) const { |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 72 | if (SkBitmapCache::Find(this->uniqueID(), dst)) { |
| 73 | SkASSERT(dst->getGenerationID() == this->uniqueID()); |
| 74 | SkASSERT(dst->isImmutable()); |
| 75 | SkASSERT(dst->getPixels()); |
| 76 | return true; |
| 77 | } |
| 78 | |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 79 | if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->alphaType(), |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 80 | this->fColorSpace))) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig, |
| 84 | dst->getPixels(), dst->rowBytes())) { |
| 85 | return false; |
| 86 | } |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 87 | |
| 88 | dst->pixelRef()->setImmutableWithID(this->uniqueID()); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 89 | if (kAllow_CachingHint == chint) { |
| 90 | SkBitmapCache::Add(this->uniqueID(), *dst); |
| 91 | fAddedRasterVersionToCache.store(true); |
| 92 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
Robert Phillips | 6de9904 | 2017-01-31 11:31:39 -0500 | [diff] [blame] | 96 | sk_sp<GrTextureProxy> SkImage_Gpu::asTextureProxyRef() const { |
| 97 | sk_sp<GrSurfaceProxy> sProxy = GrSurfaceProxy::MakeWrapped(fTexture); |
| 98 | if (!sProxy) { |
| 99 | return nullptr; |
| 100 | } |
| 101 | |
| 102 | return sk_ref_sp(sProxy->asTextureProxy()); |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 103 | } |
| 104 | |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 105 | GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrSamplerParams& params, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 106 | SkColorSpace* dstColorSpace, |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 107 | sk_sp<SkColorSpace>* texColorSpace, |
| 108 | SkScalar scaleAdjust[2]) const { |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 109 | if (texColorSpace) { |
| 110 | *texColorSpace = this->fColorSpace; |
| 111 | } |
Robert Phillips | 6de9904 | 2017-01-31 11:31:39 -0500 | [diff] [blame] | 112 | GrTextureAdjuster adjuster(fTexture.get(), this->alphaType(), this->bounds(), |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 113 | this->uniqueID(), this->fColorSpace.get()); |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 114 | return adjuster.refTextureSafeForParams(params, nullptr, scaleAdjust); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 115 | } |
| 116 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 117 | static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 118 | switch (info.colorType()) { |
| 119 | case kRGBA_8888_SkColorType: |
| 120 | case kBGRA_8888_SkColorType: |
| 121 | break; |
| 122 | default: |
| 123 | return; // nothing to do |
| 124 | } |
| 125 | |
| 126 | // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian, |
| 127 | // and in either case, the alpha-byte is always in the same place, so we can safely call |
| 128 | // SkPreMultiplyColor() |
| 129 | // |
| 130 | SkColor* row = (SkColor*)pixels; |
| 131 | for (int y = 0; y < info.height(); ++y) { |
| 132 | for (int x = 0; x < info.width(); ++x) { |
| 133 | row[x] = SkPreMultiplyColor(row[x]); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 138 | bool SkImage_Gpu::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 139 | int srcX, int srcY, CachingHint) const { |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 140 | if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) { |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 141 | return false; |
| 142 | } |
| 143 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 144 | SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 145 | if (!rec.trim(this->width(), this->height())) { |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | GrPixelConfig config = SkImageInfo2GrPixelConfig(rec.fInfo, *fTexture->getContext()->caps()); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 150 | uint32_t flags = 0; |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 151 | if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 152 | // let the GPU perform this transformation for us |
| 153 | flags = GrContext::kUnpremul_PixelOpsFlag; |
| 154 | } |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 155 | if (!fTexture->readPixels(fColorSpace.get(), rec.fX, rec.fY, rec.fInfo.width(), |
| 156 | rec.fInfo.height(), config, rec.fInfo.colorSpace(), rec.fPixels, |
| 157 | rec.fRowBytes, flags)) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 158 | return false; |
| 159 | } |
| 160 | // do we have to manually fix-up the alpha channel? |
| 161 | // src dst |
| 162 | // unpremul premul fix manually |
| 163 | // premul unpremul done by kUnpremul_PixelOpsFlag |
| 164 | // all other combos need to change. |
| 165 | // |
| 166 | // Should this be handled by Ganesh? todo:? |
| 167 | // |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 168 | if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) { |
| 169 | apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 170 | } |
| 171 | return true; |
| 172 | } |
| 173 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 174 | sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const { |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 175 | GrContext* ctx = fTexture->getContext(); |
| 176 | GrSurfaceDesc desc = fTexture->desc(); |
| 177 | desc.fWidth = subset.width(); |
| 178 | desc.fHeight = subset.height(); |
| 179 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 180 | sk_sp<GrSurfaceContext> sContext(ctx->contextPriv().makeDeferredSurfaceContext( |
| 181 | desc, |
| 182 | SkBackingFit::kExact, |
| 183 | fBudgeted)); |
| 184 | if (!sContext) { |
| 185 | return nullptr; |
| 186 | } |
| 187 | |
| 188 | // TODO: make gpu images be proxy-backed so we don't need to do this |
| 189 | sk_sp<GrSurfaceProxy> tmpSrc(GrSurfaceProxy::MakeWrapped(fTexture)); |
| 190 | if (!tmpSrc) { |
| 191 | return nullptr; |
| 192 | } |
| 193 | |
| 194 | if (!sContext->copy(tmpSrc.get(), subset, SkIPoint::Make(0, 0))) { |
| 195 | return nullptr; |
| 196 | } |
| 197 | |
| 198 | // TODO: make gpu images be proxy-backed so we don't need to do this |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 199 | GrSurface* subTx = sContext->asSurfaceProxy()->instantiate(ctx->textureProvider()); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 200 | if (!subTx) { |
| 201 | return nullptr; |
| 202 | } |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 203 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 204 | return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 205 | fAlphaType, sk_ref_sp(subTx->asTexture()), |
| 206 | fColorSpace, fBudgeted); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 207 | } |
| 208 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 209 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 210 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 211 | static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 212 | SkAlphaType at, sk_sp<SkColorSpace> colorSpace, |
| 213 | GrWrapOwnership ownership, |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 214 | SkImage::TextureReleaseProc releaseProc, |
| 215 | SkImage::ReleaseContext releaseCtx) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 216 | if (desc.fWidth <= 0 || desc.fHeight <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 217 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 218 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 219 | sk_sp<GrTexture> tex = ctx->textureProvider()->wrapBackendTexture(desc, ownership); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 220 | if (!tex) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 221 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 222 | } |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 223 | if (releaseProc) { |
| 224 | tex->setRelease(releaseProc, releaseCtx); |
| 225 | } |
| 226 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 227 | const SkBudgeted budgeted = SkBudgeted::kNo; |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 228 | return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 229 | at, std::move(tex), std::move(colorSpace), budgeted); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 230 | } |
| 231 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 232 | sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 233 | SkAlphaType at, sk_sp<SkColorSpace> cs, |
| 234 | TextureReleaseProc releaseP, ReleaseContext releaseC) { |
| 235 | return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kBorrow_GrWrapOwnership, |
| 236 | releaseP, releaseC); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 237 | } |
| 238 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 239 | sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 240 | SkAlphaType at, sk_sp<SkColorSpace> cs) { |
| 241 | return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kAdopt_GrWrapOwnership, |
| 242 | nullptr, nullptr); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 243 | } |
| 244 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 245 | static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace, |
| 246 | bool nv12, |
| 247 | const GrBackendObject yuvTextureHandles[], |
| 248 | const SkISize yuvSizes[], |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 249 | GrSurfaceOrigin origin, |
| 250 | sk_sp<SkColorSpace> imageColorSpace) { |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 251 | const SkBudgeted budgeted = SkBudgeted::kYes; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 252 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 253 | if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 || |
| 254 | yuvSizes[1].fHeight <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 255 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 256 | } |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 257 | if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) { |
| 258 | return nullptr; |
| 259 | } |
| 260 | |
| 261 | const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig; |
| 262 | |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 263 | GrBackendTextureDesc yDesc; |
| 264 | yDesc.fConfig = kConfig; |
| 265 | yDesc.fOrigin = origin; |
| 266 | yDesc.fSampleCnt = 0; |
| 267 | yDesc.fTextureHandle = yuvTextureHandles[0]; |
| 268 | yDesc.fWidth = yuvSizes[0].fWidth; |
| 269 | yDesc.fHeight = yuvSizes[0].fHeight; |
| 270 | |
| 271 | GrBackendTextureDesc uDesc; |
| 272 | uDesc.fConfig = kConfig; |
| 273 | uDesc.fOrigin = origin; |
| 274 | uDesc.fSampleCnt = 0; |
| 275 | uDesc.fTextureHandle = yuvTextureHandles[1]; |
| 276 | uDesc.fWidth = yuvSizes[1].fWidth; |
| 277 | uDesc.fHeight = yuvSizes[1].fHeight; |
| 278 | |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 279 | sk_sp<GrSurfaceProxy> yProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, yDesc); |
| 280 | sk_sp<GrSurfaceProxy> uProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, uDesc); |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 281 | sk_sp<GrSurfaceProxy> vProxy; |
| 282 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 283 | if (nv12) { |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 284 | vProxy = uProxy; |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 285 | } else { |
| 286 | GrBackendTextureDesc vDesc; |
| 287 | vDesc.fConfig = kConfig; |
| 288 | vDesc.fOrigin = origin; |
| 289 | vDesc.fSampleCnt = 0; |
| 290 | vDesc.fTextureHandle = yuvTextureHandles[2]; |
| 291 | vDesc.fWidth = yuvSizes[2].fWidth; |
| 292 | vDesc.fHeight = yuvSizes[2].fHeight; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 293 | |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 294 | vProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, vDesc); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 295 | } |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 296 | if (!yProxy || !uProxy || !vProxy) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 297 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 298 | } |
| 299 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 300 | const int width = yuvSizes[0].fWidth; |
| 301 | const int height = yuvSizes[0].fHeight; |
robertphillips | aa19a5f | 2016-04-28 06:21:55 -0700 | [diff] [blame] | 302 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 303 | // Needs to be a render target in order to draw to it for the yuv->rgb conversion. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 304 | sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeRenderTargetContext( |
| 305 | SkBackingFit::kExact, |
| 306 | width, height, |
| 307 | kRGBA_8888_GrPixelConfig, |
| 308 | std::move(imageColorSpace), |
| 309 | 0, |
| 310 | origin)); |
| 311 | if (!renderTargetContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 312 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | GrPaint paint; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 316 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 317 | paint.addColorFragmentProcessor( |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 318 | GrYUVEffect::MakeYUVToRGB(ctx, |
| 319 | sk_ref_sp(yProxy->asTextureProxy()), |
| 320 | sk_ref_sp(uProxy->asTextureProxy()), |
| 321 | sk_ref_sp(vProxy->asTextureProxy()), yuvSizes, colorSpace, nv12)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 322 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 323 | const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
robertphillips | c9a3706 | 2015-09-01 08:34:28 -0700 | [diff] [blame] | 324 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 325 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect); |
Robert Phillips | e60ad62 | 2016-11-17 10:22:48 -0500 | [diff] [blame] | 326 | |
| 327 | if (!renderTargetContext->accessRenderTarget()) { |
| 328 | return nullptr; |
| 329 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 330 | ctx->flushSurfaceWrites(renderTargetContext->accessRenderTarget()); |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 331 | return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 332 | kOpaque_SkAlphaType, renderTargetContext->asTexture(), |
Robert Phillips | 75a475c | 2017-01-13 09:18:59 -0500 | [diff] [blame] | 333 | renderTargetContext->refColorSpace(), budgeted); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 334 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 335 | |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 336 | sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace, |
| 337 | const GrBackendObject yuvTextureHandles[3], |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 338 | const SkISize yuvSizes[3], GrSurfaceOrigin origin, |
| 339 | sk_sp<SkColorSpace> imageColorSpace) { |
| 340 | return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin, |
| 341 | std::move(imageColorSpace)); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace, |
| 345 | const GrBackendObject yuvTextureHandles[2], |
| 346 | const SkISize yuvSizes[2], |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 347 | GrSurfaceOrigin origin, |
| 348 | sk_sp<SkColorSpace> imageColorSpace) { |
| 349 | return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin, |
| 350 | std::move(imageColorSpace)); |
jbauman | b445a57 | 2016-06-09 13:24:48 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame^] | 353 | static sk_sp<SkImage> create_image_from_maker(GrTextureMaker* maker, SkAlphaType at, uint32_t id, |
| 354 | SkColorSpace* dstColorSpace) { |
| 355 | sk_sp<SkColorSpace> texColorSpace; |
| 356 | sk_sp<GrTexture> texture(maker->refTextureForParams(GrSamplerParams::ClampNoFilter(), |
| 357 | dstColorSpace, &texColorSpace, nullptr)); |
| 358 | if (!texture) { |
| 359 | return nullptr; |
| 360 | } |
| 361 | return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), id, at, std::move(texture), |
| 362 | std::move(texColorSpace), SkBudgeted::kNo); |
| 363 | } |
| 364 | |
| 365 | sk_sp<SkImage> SkImage::makeTextureImage(GrContext *context, SkColorSpace* dstColorSpace) const { |
| 366 | if (!context) { |
| 367 | return nullptr; |
| 368 | } |
| 369 | if (GrTexture* peek = as_IB(this)->peekTexture()) { |
| 370 | return peek->getContext() == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr; |
| 371 | } |
| 372 | |
| 373 | if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) { |
| 374 | GrImageTextureMaker maker(context, cacher, this, kDisallow_CachingHint); |
| 375 | return create_image_from_maker(&maker, this->alphaType(), this->uniqueID(), dstColorSpace); |
| 376 | } |
| 377 | |
| 378 | if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) { |
| 379 | GrBitmapTextureMaker maker(context, *bmp); |
| 380 | return create_image_from_maker(&maker, this->alphaType(), this->uniqueID(), dstColorSpace); |
| 381 | } |
| 382 | return nullptr; |
| 383 | } |
| 384 | |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 385 | sk_sp<SkImage> SkImage::makeNonTextureImage() const { |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 386 | if (!this->isTextureBacked()) { |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 387 | return sk_ref_sp(const_cast<SkImage*>(this)); |
| 388 | } |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 389 | SkImageInfo info = as_IB(this)->onImageInfo(); |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 390 | size_t rowBytes = info.minRowBytes(); |
| 391 | size_t size = info.getSafeSize(rowBytes); |
| 392 | auto data = SkData::MakeUninitialized(size); |
| 393 | if (!data) { |
| 394 | return nullptr; |
| 395 | } |
| 396 | SkPixmap pm(info, data->writable_data(), rowBytes); |
| 397 | if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) { |
| 398 | return nullptr; |
| 399 | } |
| 400 | return MakeRasterData(info, data, rowBytes); |
| 401 | } |
| 402 | |
Brian Osman | b92234a | 2017-01-25 14:13:00 +0000 | [diff] [blame] | 403 | sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap, |
| 404 | SkBudgeted budgeted) { |
| 405 | if (!ctx) { |
| 406 | return nullptr; |
| 407 | } |
| 408 | sk_sp<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted)); |
| 409 | if (!texture) { |
| 410 | return nullptr; |
| 411 | } |
| 412 | return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID, |
| 413 | pixmap.alphaType(), std::move(texture), |
| 414 | sk_ref_sp(pixmap.info().colorSpace()), budgeted); |
| 415 | } |
| 416 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 417 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 418 | |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 419 | namespace { |
| 420 | struct MipMapLevelData { |
| 421 | void* fPixelData; |
| 422 | size_t fRowBytes; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 423 | }; |
| 424 | |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 425 | struct DeferredTextureImage { |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 426 | uint32_t fContextUniqueID; |
| 427 | // Right now, the destination color mode is only considered when generating mipmaps |
| 428 | SkDestinationSurfaceColorMode fColorMode; |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 429 | // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace. |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 430 | int fWidth; |
| 431 | int fHeight; |
| 432 | SkColorType fColorType; |
| 433 | SkAlphaType fAlphaType; |
| 434 | void* fColorSpace; |
| 435 | size_t fColorSpaceSize; |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 436 | int fMipMapLevelCount; |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 437 | // The fMipMapLevelData array may contain more than 1 element. |
| 438 | // It contains fMipMapLevelCount elements. |
| 439 | // That means this struct's size is not known at compile-time. |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 440 | MipMapLevelData fMipMapLevelData[1]; |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 441 | }; |
| 442 | } // anonymous namespace |
| 443 | |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 444 | static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) { |
Eric Karl | a422d70 | 2016-11-30 11:09:29 -0800 | [diff] [blame] | 445 | // There is a bug in the mipmap pre-generation logic in use in getDeferredTextureImageData. |
| 446 | // This can cause runaway memory leaks, so we are disabling this path until we can |
| 447 | // investigate further. crbug.com/669775 |
| 448 | return false; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | namespace { |
| 452 | |
| 453 | class DTIBufferFiller |
| 454 | { |
| 455 | public: |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 456 | explicit DTIBufferFiller(char* bufferAsCharPtr) |
| 457 | : bufferAsCharPtr_(bufferAsCharPtr) {} |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 458 | |
| 459 | void fillMember(const void* source, size_t memberOffset, size_t size) { |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 460 | memcpy(bufferAsCharPtr_ + memberOffset, source, size); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | private: |
| 464 | |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 465 | char* bufferAsCharPtr_; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 466 | }; |
| 467 | } |
| 468 | |
| 469 | #define FILL_MEMBER(bufferFiller, member, source) \ |
| 470 | bufferFiller.fillMember(source, \ |
| 471 | offsetof(DeferredTextureImage, member), \ |
| 472 | sizeof(DeferredTextureImage::member)); |
| 473 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 474 | size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy, |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 475 | const DeferredTextureImageUsageParams params[], |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 476 | int paramCnt, void* buffer, |
Brian Osman | 6c15cc7 | 2016-10-17 09:51:37 -0400 | [diff] [blame] | 477 | SkColorSpace* dstColorSpace) const { |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 478 | // Extract relevant min/max values from the params array. |
| 479 | int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel; |
| 480 | SkFilterQuality highestFilterQuality = params[0].fQuality; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 481 | bool useMipMaps = should_use_mip_maps(params[0]); |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 482 | for (int i = 1; i < paramCnt; ++i) { |
| 483 | if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel) |
| 484 | lowestPreScaleMipLevel = params[i].fPreScaleMipLevel; |
| 485 | if (highestFilterQuality < params[i].fQuality) |
| 486 | highestFilterQuality = params[i].fQuality; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 487 | useMipMaps |= should_use_mip_maps(params[i]); |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 488 | } |
| 489 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 490 | const bool fillMode = SkToBool(buffer); |
| 491 | if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) { |
| 492 | return 0; |
| 493 | } |
| 494 | |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 495 | // Calculate scaling parameters. |
| 496 | bool isScaled = lowestPreScaleMipLevel != 0; |
| 497 | |
| 498 | SkISize scaledSize; |
| 499 | if (isScaled) { |
| 500 | // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the |
| 501 | // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level. |
| 502 | scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(), |
| 503 | lowestPreScaleMipLevel - 1); |
| 504 | } else { |
| 505 | scaledSize = SkISize::Make(this->width(), this->height()); |
| 506 | } |
| 507 | |
| 508 | // We never want to scale at higher than SW medium quality, as SW medium matches GPU high. |
| 509 | SkFilterQuality scaleFilterQuality = highestFilterQuality; |
| 510 | if (scaleFilterQuality > kMedium_SkFilterQuality) { |
| 511 | scaleFilterQuality = kMedium_SkFilterQuality; |
| 512 | } |
| 513 | |
ericrk | c429baf | 2016-03-24 15:35:45 -0700 | [diff] [blame] | 514 | const int maxTextureSize = proxy.fCaps->maxTextureSize(); |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 515 | if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) { |
ericrk | c429baf | 2016-03-24 15:35:45 -0700 | [diff] [blame] | 516 | return 0; |
| 517 | } |
| 518 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 519 | SkAutoPixmapStorage pixmap; |
| 520 | SkImageInfo info; |
| 521 | size_t pixelSize = 0; |
Brian Osman | aaedae7 | 2017-01-20 13:21:56 -0500 | [diff] [blame] | 522 | if (!isScaled && this->peekPixels(&pixmap) && !pixmap.ctable()) { |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 523 | info = pixmap.info(); |
| 524 | pixelSize = SkAlign8(pixmap.getSafeSize()); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 525 | } else { |
| 526 | // Here we're just using presence of data to know whether there is a codec behind the image. |
| 527 | // In the future we will access the cacherator and get the exact data that we want to (e.g. |
| 528 | // yuv planes) upload. |
bungeman | ffae30d | 2016-08-03 13:32:32 -0700 | [diff] [blame] | 529 | sk_sp<SkData> data(this->refEncoded()); |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 530 | if (!data && !this->peekPixels(nullptr)) { |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 531 | return 0; |
| 532 | } |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 533 | if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) { |
| 534 | // Generator backed image. Tweak info to trigger correct kind of decode. |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 535 | SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat( |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 536 | dstColorSpace, proxy.fCaps.get()); |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 537 | info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(), |
| 538 | scaledSize.height()); |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 539 | } else { |
| 540 | info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height()); |
| 541 | } |
Brian Osman | aaedae7 | 2017-01-20 13:21:56 -0500 | [diff] [blame] | 542 | if (kIndex_8_SkColorType == info.colorType()) { |
| 543 | // Force Index8 to be N32 instead. Index8 is unsupported in Ganesh. |
| 544 | info = info.makeColorType(kN32_SkColorType); |
| 545 | } |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 546 | pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr)); |
| 547 | if (fillMode) { |
| 548 | pixmap.alloc(info); |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 549 | if (isScaled) { |
| 550 | if (!this->scalePixels(pixmap, scaleFilterQuality, |
| 551 | SkImage::kDisallow_CachingHint)) { |
| 552 | return 0; |
| 553 | } |
| 554 | } else { |
| 555 | if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) { |
| 556 | return 0; |
| 557 | } |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 558 | } |
| 559 | SkASSERT(!pixmap.ctable()); |
| 560 | } |
| 561 | } |
cblume | 2c05280 | 2016-05-31 09:55:08 -0700 | [diff] [blame] | 562 | int mipMapLevelCount = 1; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 563 | if (useMipMaps) { |
| 564 | // SkMipMap only deals with the mipmap levels it generates, which does |
| 565 | // not include the base level. |
| 566 | // That means it generates and holds levels 1-x instead of 0-x. |
| 567 | // So the total mipmap level count is 1 more than what |
| 568 | // SkMipMap::ComputeLevelCount returns. |
| 569 | mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1; |
| 570 | |
| 571 | // We already initialized pixelSize to the size of the base level. |
| 572 | // SkMipMap will generate the extra mipmap levels. Their sizes need to |
| 573 | // be added to the total. |
| 574 | // Index 0 here does not refer to the base mipmap level -- it is |
| 575 | // SkMipMap's first generated mipmap level (level 1). |
| 576 | for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0; |
| 577 | currentMipMapLevelIndex--) { |
| 578 | SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(), |
| 579 | currentMipMapLevelIndex); |
brianosman | 32b5e70 | 2016-10-13 06:44:23 -0700 | [diff] [blame] | 580 | SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 581 | pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr)); |
| 582 | } |
| 583 | } |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 584 | size_t size = 0; |
| 585 | size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage)); |
| 586 | size += dtiSize; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 587 | size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData); |
| 588 | // We subtract 1 because DeferredTextureImage already includes the base |
| 589 | // level in its size |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 590 | size_t pixelOffset = size; |
| 591 | size += pixelSize; |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 592 | size_t colorSpaceOffset = 0; |
| 593 | size_t colorSpaceSize = 0; |
| 594 | if (info.colorSpace()) { |
| 595 | colorSpaceOffset = size; |
| 596 | colorSpaceSize = info.colorSpace()->writeToMemory(nullptr); |
| 597 | size += colorSpaceSize; |
| 598 | } |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 599 | if (!fillMode) { |
| 600 | return size; |
| 601 | } |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 602 | char* bufferAsCharPtr = reinterpret_cast<char*>(buffer); |
| 603 | char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset; |
| 604 | void* pixels = pixelsAsCharPtr; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 605 | |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 606 | memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))), |
| 607 | pixmap.addr(), pixmap.getSafeSize()); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 608 | |
Brian Osman | 6c15cc7 | 2016-10-17 09:51:37 -0400 | [diff] [blame] | 609 | // If the context has sRGB support, and we're intending to render to a surface with an attached |
| 610 | // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB) |
| 611 | // aware mip-mapping. |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 612 | SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy; |
Brian Osman | 6c15cc7 | 2016-10-17 09:51:37 -0400 | [diff] [blame] | 613 | if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) && |
| 614 | info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) { |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 615 | colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware; |
Brian Osman | 6c15cc7 | 2016-10-17 09:51:37 -0400 | [diff] [blame] | 616 | } |
| 617 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 618 | SkASSERT(info == pixmap.info()); |
| 619 | size_t rowBytes = pixmap.rowBytes(); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 620 | static_assert(std::is_standard_layout<DeferredTextureImage>::value, |
| 621 | "offsetof, which we use below, requires the type have standard layout"); |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 622 | auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr}; |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 623 | FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 624 | FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID); |
| 625 | int width = info.width(); |
| 626 | FILL_MEMBER(dtiBufferFiller, fWidth, &width); |
| 627 | int height = info.height(); |
| 628 | FILL_MEMBER(dtiBufferFiller, fHeight, &height); |
| 629 | SkColorType colorType = info.colorType(); |
| 630 | FILL_MEMBER(dtiBufferFiller, fColorType, &colorType); |
| 631 | SkAlphaType alphaType = info.alphaType(); |
| 632 | FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 633 | FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount); |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 634 | memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData), |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 635 | &pixels, sizeof(pixels)); |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 636 | memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes), |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 637 | &rowBytes, sizeof(rowBytes)); |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 638 | if (colorSpaceSize) { |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 639 | void* colorSpace = bufferAsCharPtr + colorSpaceOffset; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 640 | FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace); |
| 641 | FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize); |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 642 | info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset); |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 643 | } else { |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 644 | memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace), |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 645 | 0, sizeof(DeferredTextureImage::fColorSpace)); |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 646 | memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize), |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 647 | 0, sizeof(DeferredTextureImage::fColorSpaceSize)); |
| 648 | } |
| 649 | |
| 650 | // Fill in the mipmap levels if they exist |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 651 | char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize()); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 652 | |
| 653 | if (useMipMaps) { |
cblume | 94ddf54 | 2016-09-16 10:07:32 -0700 | [diff] [blame] | 654 | static_assert(std::is_standard_layout<MipMapLevelData>::value, |
| 655 | "offsetof, which we use below, requires the type have a standard layout"); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 656 | |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 657 | std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr)); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 658 | // SkMipMap holds only the mipmap levels it generates. |
| 659 | // A programmer can use the data they provided to SkMipMap::Build as level 0. |
| 660 | // So the SkMipMap provides levels 1-x but it stores them in its own |
| 661 | // range 0-(x-1). |
| 662 | for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1; |
| 663 | generatedMipLevelIndex++) { |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 664 | SkMipMap::Level mipLevel; |
| 665 | mipmaps->getLevel(generatedMipLevelIndex, &mipLevel); |
| 666 | |
| 667 | // Make sure the mipmap data is after the start of the buffer |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 668 | SkASSERT(mipLevelPtr > bufferAsCharPtr); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 669 | // Make sure the mipmap data starts before the end of the buffer |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 670 | SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 671 | // Make sure the mipmap data ends before the end of the buffer |
cblume | 12f7527 | 2016-09-19 06:18:03 -0700 | [diff] [blame] | 672 | SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <= |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 673 | bufferAsCharPtr + pixelOffset + pixelSize); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 674 | |
| 675 | // getSafeSize includes rowbyte padding except for the last row, |
| 676 | // right? |
| 677 | |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 678 | memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize()); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 679 | |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 680 | memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) + |
| 681 | sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) + |
| 682 | offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*)); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 683 | size_t rowBytes = mipLevel.fPixmap.rowBytes(); |
cblume | 921bc67 | 2016-09-22 05:25:26 -0700 | [diff] [blame] | 684 | memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) + |
| 685 | sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) + |
| 686 | offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes)); |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 687 | |
| 688 | mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize()); |
| 689 | } |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 690 | } |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 691 | return size; |
| 692 | } |
| 693 | |
| 694 | sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data, |
| 695 | SkBudgeted budgeted) { |
| 696 | if (!data) { |
| 697 | return nullptr; |
| 698 | } |
| 699 | const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data); |
| 700 | |
| 701 | if (!context || context->uniqueID() != dti->fContextUniqueID) { |
| 702 | return nullptr; |
| 703 | } |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 704 | int mipLevelCount = dti->fMipMapLevelCount; |
| 705 | SkASSERT(mipLevelCount >= 1); |
bsalomon | 4d516a6 | 2016-07-28 13:37:31 -0700 | [diff] [blame] | 706 | sk_sp<SkColorSpace> colorSpace; |
| 707 | if (dti->fColorSpaceSize) { |
| 708 | colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize); |
| 709 | } |
| 710 | SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight, |
| 711 | dti->fColorType, dti->fAlphaType, colorSpace); |
Brian Osman | b92234a | 2017-01-25 14:13:00 +0000 | [diff] [blame] | 712 | if (mipLevelCount == 1) { |
| 713 | SkPixmap pixmap; |
| 714 | pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData, dti->fMipMapLevelData[0].fRowBytes); |
| 715 | return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted); |
| 716 | } else { |
| 717 | std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]); |
| 718 | for (int i = 0; i < mipLevelCount; i++) { |
| 719 | texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData; |
| 720 | texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes; |
| 721 | } |
Brian Osman | 8ccbbb4 | 2017-01-20 14:08:04 -0500 | [diff] [blame] | 722 | |
Brian Osman | b92234a | 2017-01-25 14:13:00 +0000 | [diff] [blame] | 723 | return SkImage::MakeTextureFromMipMap(context, info, texels.get(), |
| 724 | mipLevelCount, SkBudgeted::kYes, |
| 725 | dti->fColorMode); |
| 726 | } |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 730 | |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 731 | sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info, |
| 732 | const GrMipLevel* texels, int mipLevelCount, |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 733 | SkBudgeted budgeted, |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 734 | SkDestinationSurfaceColorMode colorMode) { |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 735 | if (!ctx) { |
| 736 | return nullptr; |
| 737 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 738 | sk_sp<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount)); |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 739 | if (!texture) { |
| 740 | return nullptr; |
| 741 | } |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 742 | texture->texturePriv().setMipColorMode(colorMode); |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 743 | return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID, |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 744 | info.alphaType(), std::move(texture), |
| 745 | sk_ref_sp(info.colorSpace()), budgeted); |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 746 | } |