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