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