blob: 02f53e7104f1254df6caab19b5584a69a065c2be [file] [log] [blame]
reed@google.com5d4ba882012-07-31 15:45:27 +00001/*
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
cblume33e0cb52016-08-30 12:09:23 -07008#include <cstddef>
9#include <cstring>
10#include <type_traits>
11
robertphillipsc5035e72016-03-17 06:58:39 -070012#include "SkAutoPixmapStorage.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050013#include "GrBitmapTextureMaker.h"
reed856e9d92015-09-30 12:21:45 -070014#include "GrCaps.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000015#include "GrContext.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050016#include "GrContextPriv.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050017#include "GrImageTextureMaker.h"
Brian Osman11052242016-10-27 14:47:55 -040018#include "GrRenderTargetContext.h"
Brian Osmane8e54582016-11-28 10:06:27 -050019#include "GrTextureAdjuster.h"
cblume33e0cb52016-08-30 12:09:23 -070020#include "GrTexturePriv.h"
bsalomonf267c1e2016-02-01 13:16:14 -080021#include "effects/GrYUVEffect.h"
bsalomon993a4212015-05-29 11:37:25 -070022#include "SkCanvas.h"
reed262a71b2015-12-05 13:07:27 -080023#include "SkBitmapCache.h"
bsalomon89fe56b2015-10-29 10:49:28 -070024#include "SkGrPriv.h"
reed262a71b2015-12-05 13:07:27 -080025#include "SkImage_Gpu.h"
Brian Osman7992da32016-11-18 11:28:24 -050026#include "SkImageCacherator.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050027#include "SkImageInfoPriv.h"
ericrkb4da01d2016-06-13 11:18:14 -070028#include "SkMipMap.h"
reed6f1216a2015-08-04 08:10:13 -070029#include "SkPixelRef.h"
bsalomon993a4212015-05-29 11:37:25 -070030
bungeman6bd52842016-10-27 09:30:08 -070031SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, sk_sp<GrTexture> tex,
brianosmandddbe382016-07-20 13:55:39 -070032 sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted)
reedaf3fbfc2015-10-04 11:28:36 -070033 : INHERITED(w, h, uniqueID)
bungeman6bd52842016-10-27 09:30:08 -070034 , fTexture(std::move(tex))
reed8b26b992015-05-07 15:36:17 -070035 , fAlphaType(at)
bsalomoneaaaf0b2015-01-23 08:08:04 -080036 , fBudgeted(budgeted)
brianosmandddbe382016-07-20 13:55:39 -070037 , fColorSpace(std::move(colorSpace))
bsalomon1cd63112015-08-05 06:58:39 -070038 , fAddedRasterVersionToCache(false)
reedc9b5f8b2015-10-22 13:20:20 -070039{
bungeman6bd52842016-10-27 09:30:08 -070040 SkASSERT(fTexture->width() == w);
41 SkASSERT(fTexture->height() == h);
reedc9b5f8b2015-10-22 13:20:20 -070042}
piotaixrcef04f82014-07-14 07:48:04 -070043
reed6f1216a2015-08-04 08:10:13 -070044SkImage_Gpu::~SkImage_Gpu() {
45 if (fAddedRasterVersionToCache.load()) {
46 SkNotifyBitmapGenIDIsStale(this->uniqueID());
47 }
48}
49
bsalomoneaaaf0b2015-01-23 08:08:04 -080050extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
robertphillipsea70c4b2016-07-20 08:54:31 -070051 if (image->isTextureBacked()) {
reed8b26b992015-05-07 15:36:17 -070052 ((SkImage_Gpu*)image)->applyBudgetDecision();
53 }
54}
55
brianosman396fcdb2016-07-22 06:26:11 -070056SkImageInfo 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
brianosman69c166d2016-08-17 14:01:05 -070064static SkImageInfo make_info(int w, int h, SkAlphaType at, sk_sp<SkColorSpace> colorSpace) {
65 return SkImageInfo::MakeN32(w, h, at, std::move(colorSpace));
reed88d064d2015-10-12 11:30:02 -070066}
67
Brian Osman61624f02016-12-09 14:51:59 -050068bool SkImage_Gpu::getROPixels(SkBitmap* dst, SkColorSpace* dstColorSpace,
Brian Osman7992da32016-11-18 11:28:24 -050069 CachingHint chint) const {
reed6f1216a2015-08-04 08:10:13 -070070 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
brianosman69c166d2016-08-17 14:01:05 -070077 if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->alphaType(),
brianosmandddbe382016-07-20 13:55:39 -070078 this->fColorSpace))) {
reed8b26b992015-05-07 15:36:17 -070079 return false;
80 }
81 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
82 dst->getPixels(), dst->rowBytes())) {
83 return false;
84 }
reed6f1216a2015-08-04 08:10:13 -070085
86 dst->pixelRef()->setImmutableWithID(this->uniqueID());
reed09553032015-11-23 12:32:16 -080087 if (kAllow_CachingHint == chint) {
88 SkBitmapCache::Add(this->uniqueID(), *dst);
89 fAddedRasterVersionToCache.store(true);
90 }
reed8b26b992015-05-07 15:36:17 -070091 return true;
92}
93
Robert Phillips2c862492017-01-18 10:08:39 -050094sk_sp<GrSurfaceProxy> SkImage_Gpu::refProxy() const {
95 return GrSurfaceProxy::MakeWrapped(fTexture);
96}
97
Brian Salomon514baff2016-11-17 15:17:07 -050098GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrSamplerParams& params,
Brian Osman61624f02016-12-09 14:51:59 -050099 SkColorSpace* dstColorSpace,
Brian Osman7992da32016-11-18 11:28:24 -0500100 sk_sp<SkColorSpace>* texColorSpace) const {
101 if (texColorSpace) {
102 *texColorSpace = this->fColorSpace;
103 }
104 GrTextureAdjuster adjuster(this->peekTexture(), this->alphaType(), this->bounds(),
105 this->uniqueID(), this->fColorSpace.get());
Brian Osman61624f02016-12-09 14:51:59 -0500106 return adjuster.refTextureSafeForParams(params, nullptr);
reed85d91782015-09-10 14:33:38 -0700107}
108
reed8b26b992015-05-07 15:36:17 -0700109static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
110 switch (info.colorType()) {
111 case kRGBA_8888_SkColorType:
112 case kBGRA_8888_SkColorType:
113 break;
114 default:
115 return; // nothing to do
116 }
117
118 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
119 // and in either case, the alpha-byte is always in the same place, so we can safely call
120 // SkPreMultiplyColor()
121 //
122 SkColor* row = (SkColor*)pixels;
123 for (int y = 0; y < info.height(); ++y) {
124 for (int x = 0; x < info.width(); ++x) {
125 row[x] = SkPreMultiplyColor(row[x]);
126 }
127 }
128}
129
130bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
reed09553032015-11-23 12:32:16 -0800131 int srcX, int srcY, CachingHint) const {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500132 if (!SkImageInfoValidConversion(info, this->onImageInfo())) {
133 return false;
134 }
135
brianosmanb109b8c2016-06-16 13:03:24 -0700136 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fTexture->getContext()->caps());
reed8b26b992015-05-07 15:36:17 -0700137 uint32_t flags = 0;
138 if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) {
139 // let the GPU perform this transformation for us
140 flags = GrContext::kUnpremul_PixelOpsFlag;
141 }
Brian Osmanb62ea222016-12-22 11:12:16 -0500142 if (!fTexture->readPixels(fColorSpace.get(), srcX, srcY, info.width(), info.height(), config,
143 info.colorSpace(), pixels, rowBytes, flags)) {
reed8b26b992015-05-07 15:36:17 -0700144 return false;
145 }
146 // do we have to manually fix-up the alpha channel?
147 // src dst
148 // unpremul premul fix manually
149 // premul unpremul done by kUnpremul_PixelOpsFlag
150 // all other combos need to change.
151 //
152 // Should this be handled by Ganesh? todo:?
153 //
154 if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
155 apply_premul(info, pixels, rowBytes);
156 }
157 return true;
158}
159
reed7fb4f8b2016-03-11 04:33:52 -0800160sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700161 GrContext* ctx = fTexture->getContext();
162 GrSurfaceDesc desc = fTexture->desc();
163 desc.fWidth = subset.width();
164 desc.fHeight = subset.height();
165
Robert Phillipse2f7d182016-12-15 09:23:05 -0500166 sk_sp<GrSurfaceContext> sContext(ctx->contextPriv().makeDeferredSurfaceContext(
167 desc,
168 SkBackingFit::kExact,
169 fBudgeted));
170 if (!sContext) {
171 return nullptr;
172 }
173
174 // TODO: make gpu images be proxy-backed so we don't need to do this
175 sk_sp<GrSurfaceProxy> tmpSrc(GrSurfaceProxy::MakeWrapped(fTexture));
176 if (!tmpSrc) {
177 return nullptr;
178 }
179
180 if (!sContext->copy(tmpSrc.get(), subset, SkIPoint::Make(0, 0))) {
181 return nullptr;
182 }
183
184 // TODO: make gpu images be proxy-backed so we don't need to do this
185 GrSurface* subTx = sContext->asDeferredSurface()->instantiate(ctx->textureProvider());
reed7b6945b2015-09-24 00:50:58 -0700186 if (!subTx) {
187 return nullptr;
188 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500189
reed7fb4f8b2016-03-11 04:33:52 -0800190 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500191 fAlphaType, sk_ref_sp(subTx->asTexture()),
192 fColorSpace, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700193}
194
reed8b26b992015-05-07 15:36:17 -0700195///////////////////////////////////////////////////////////////////////////////////////////////////
196
reed7fb4f8b2016-03-11 04:33:52 -0800197static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700198 SkAlphaType at, sk_sp<SkColorSpace> colorSpace,
199 GrWrapOwnership ownership,
reed7fb4f8b2016-03-11 04:33:52 -0800200 SkImage::TextureReleaseProc releaseProc,
201 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700202 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700203 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700204 }
bungeman6bd52842016-10-27 09:30:08 -0700205 sk_sp<GrTexture> tex = ctx->textureProvider()->wrapBackendTexture(desc, ownership);
reed8b26b992015-05-07 15:36:17 -0700206 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700207 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700208 }
reedde499882015-06-18 13:41:40 -0700209 if (releaseProc) {
210 tex->setRelease(releaseProc, releaseCtx);
211 }
212
bsalomon5ec26ae2016-02-25 08:33:02 -0800213 const SkBudgeted budgeted = SkBudgeted::kNo;
reed7fb4f8b2016-03-11 04:33:52 -0800214 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700215 at, std::move(tex), std::move(colorSpace), budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700216}
217
reed7fb4f8b2016-03-11 04:33:52 -0800218sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700219 SkAlphaType at, sk_sp<SkColorSpace> cs,
220 TextureReleaseProc releaseP, ReleaseContext releaseC) {
221 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kBorrow_GrWrapOwnership,
222 releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700223}
224
reed7fb4f8b2016-03-11 04:33:52 -0800225sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700226 SkAlphaType at, sk_sp<SkColorSpace> cs) {
227 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kAdopt_GrWrapOwnership,
228 nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700229}
230
jbaumanb445a572016-06-09 13:24:48 -0700231static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace,
232 bool nv12,
233 const GrBackendObject yuvTextureHandles[],
234 const SkISize yuvSizes[],
brianosmandddbe382016-07-20 13:55:39 -0700235 GrSurfaceOrigin origin,
236 sk_sp<SkColorSpace> imageColorSpace) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800237 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700238
jbaumanb445a572016-06-09 13:24:48 -0700239 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 ||
240 yuvSizes[1].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700241 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700242 }
jbaumanb445a572016-06-09 13:24:48 -0700243 if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) {
244 return nullptr;
245 }
246
247 const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig;
248
bsalomon993a4212015-05-29 11:37:25 -0700249 GrBackendTextureDesc yDesc;
250 yDesc.fConfig = kConfig;
251 yDesc.fOrigin = origin;
252 yDesc.fSampleCnt = 0;
253 yDesc.fTextureHandle = yuvTextureHandles[0];
254 yDesc.fWidth = yuvSizes[0].fWidth;
255 yDesc.fHeight = yuvSizes[0].fHeight;
256
257 GrBackendTextureDesc uDesc;
258 uDesc.fConfig = kConfig;
259 uDesc.fOrigin = origin;
260 uDesc.fSampleCnt = 0;
261 uDesc.fTextureHandle = yuvTextureHandles[1];
262 uDesc.fWidth = yuvSizes[1].fWidth;
263 uDesc.fHeight = yuvSizes[1].fHeight;
264
jbaumanb445a572016-06-09 13:24:48 -0700265 sk_sp<GrTexture> yTex(
266 ctx->textureProvider()->wrapBackendTexture(yDesc, kBorrow_GrWrapOwnership));
267 sk_sp<GrTexture> uTex(
268 ctx->textureProvider()->wrapBackendTexture(uDesc, kBorrow_GrWrapOwnership));
269 sk_sp<GrTexture> vTex;
270 if (nv12) {
271 vTex = uTex;
272 } else {
273 GrBackendTextureDesc vDesc;
274 vDesc.fConfig = kConfig;
275 vDesc.fOrigin = origin;
276 vDesc.fSampleCnt = 0;
277 vDesc.fTextureHandle = yuvTextureHandles[2];
278 vDesc.fWidth = yuvSizes[2].fWidth;
279 vDesc.fHeight = yuvSizes[2].fHeight;
bsalomon993a4212015-05-29 11:37:25 -0700280
jbaumanb445a572016-06-09 13:24:48 -0700281 vTex = sk_sp<GrTexture>(
282 ctx->textureProvider()->wrapBackendTexture(vDesc, kBorrow_GrWrapOwnership));
283 }
bsalomon993a4212015-05-29 11:37:25 -0700284 if (!yTex || !uTex || !vTex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700285 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700286 }
287
robertphillipsd4c741e2016-04-28 09:55:15 -0700288 const int width = yuvSizes[0].fWidth;
289 const int height = yuvSizes[0].fHeight;
robertphillipsaa19a5f2016-04-28 06:21:55 -0700290
robertphillipsd4c741e2016-04-28 09:55:15 -0700291 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
Brian Osman11052242016-10-27 14:47:55 -0400292 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeRenderTargetContext(
293 SkBackingFit::kExact,
294 width, height,
295 kRGBA_8888_GrPixelConfig,
296 std::move(imageColorSpace),
297 0,
298 origin));
299 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700300 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700301 }
302
303 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400304 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
jbaumanb445a572016-06-09 13:24:48 -0700305 paint.addColorFragmentProcessor(
306 GrYUVEffect::MakeYUVToRGB(yTex.get(), uTex.get(), vTex.get(), yuvSizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700307
robertphillipsd4c741e2016-04-28 09:55:15 -0700308 const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillipsc9a37062015-09-01 08:34:28 -0700309
Brian Salomon82f44312017-01-11 13:42:54 -0500310 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Robert Phillipse60ad622016-11-17 10:22:48 -0500311
312 if (!renderTargetContext->accessRenderTarget()) {
313 return nullptr;
314 }
Brian Osman11052242016-10-27 14:47:55 -0400315 ctx->flushSurfaceWrites(renderTargetContext->accessRenderTarget());
robertphillipsd4c741e2016-04-28 09:55:15 -0700316 return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID,
Brian Osman11052242016-10-27 14:47:55 -0400317 kOpaque_SkAlphaType, renderTargetContext->asTexture(),
Robert Phillips75a475c2017-01-13 09:18:59 -0500318 renderTargetContext->refColorSpace(), budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700319}
reed56179002015-07-07 06:11:19 -0700320
jbaumanb445a572016-06-09 13:24:48 -0700321sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
322 const GrBackendObject yuvTextureHandles[3],
brianosmandddbe382016-07-20 13:55:39 -0700323 const SkISize yuvSizes[3], GrSurfaceOrigin origin,
324 sk_sp<SkColorSpace> imageColorSpace) {
325 return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin,
326 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700327}
328
329sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
330 const GrBackendObject yuvTextureHandles[2],
331 const SkISize yuvSizes[2],
brianosmandddbe382016-07-20 13:55:39 -0700332 GrSurfaceOrigin origin,
333 sk_sp<SkColorSpace> imageColorSpace) {
334 return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin,
335 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700336}
337
bsalomon634b4302016-07-12 18:11:17 -0700338sk_sp<SkImage> SkImage::makeNonTextureImage() const {
brianosman396fcdb2016-07-22 06:26:11 -0700339 if (!this->isTextureBacked()) {
bsalomon634b4302016-07-12 18:11:17 -0700340 return sk_ref_sp(const_cast<SkImage*>(this));
341 }
brianosman396fcdb2016-07-22 06:26:11 -0700342 SkImageInfo info = as_IB(this)->onImageInfo();
bsalomon634b4302016-07-12 18:11:17 -0700343 size_t rowBytes = info.minRowBytes();
344 size_t size = info.getSafeSize(rowBytes);
345 auto data = SkData::MakeUninitialized(size);
346 if (!data) {
347 return nullptr;
348 }
349 SkPixmap pm(info, data->writable_data(), rowBytes);
350 if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
351 return nullptr;
352 }
353 return MakeRasterData(info, data, rowBytes);
354}
355
reed7fb4f8b2016-03-11 04:33:52 -0800356sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap,
357 SkBudgeted budgeted) {
bsalomon0d996862016-03-09 18:44:43 -0800358 if (!ctx) {
359 return nullptr;
360 }
bungeman6bd52842016-10-27 09:30:08 -0700361 sk_sp<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted));
bsalomon0d996862016-03-09 18:44:43 -0800362 if (!texture) {
363 return nullptr;
364 }
reed7fb4f8b2016-03-11 04:33:52 -0800365 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700366 pixmap.alphaType(), std::move(texture),
brianosmandddbe382016-07-20 13:55:39 -0700367 sk_ref_sp(pixmap.info().colorSpace()), budgeted);
bsalomon0d996862016-03-09 18:44:43 -0800368}
369
reed56179002015-07-07 06:11:19 -0700370///////////////////////////////////////////////////////////////////////////////////////////////////
371
bsalomon4d516a62016-07-28 13:37:31 -0700372namespace {
373struct MipMapLevelData {
374 void* fPixelData;
375 size_t fRowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800376};
377
bsalomon4d516a62016-07-28 13:37:31 -0700378struct DeferredTextureImage {
Brian Osman7b8400d2016-11-08 17:08:54 -0500379 uint32_t fContextUniqueID;
380 // Right now, the destination color mode is only considered when generating mipmaps
381 SkDestinationSurfaceColorMode fColorMode;
bsalomon4d516a62016-07-28 13:37:31 -0700382 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace.
Brian Osman7b8400d2016-11-08 17:08:54 -0500383 int fWidth;
384 int fHeight;
385 SkColorType fColorType;
386 SkAlphaType fAlphaType;
387 void* fColorSpace;
388 size_t fColorSpaceSize;
389 int fColorTableCnt;
390 uint32_t* fColorTableData;
391 int fMipMapLevelCount;
bsalomon4d516a62016-07-28 13:37:31 -0700392 // The fMipMapLevelData array may contain more than 1 element.
393 // It contains fMipMapLevelCount elements.
394 // That means this struct's size is not known at compile-time.
Brian Osman7b8400d2016-11-08 17:08:54 -0500395 MipMapLevelData fMipMapLevelData[1];
bsalomon4d516a62016-07-28 13:37:31 -0700396};
397} // anonymous namespace
398
cblume33e0cb52016-08-30 12:09:23 -0700399static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
Eric Karla422d702016-11-30 11:09:29 -0800400 // There is a bug in the mipmap pre-generation logic in use in getDeferredTextureImageData.
401 // This can cause runaway memory leaks, so we are disabling this path until we can
402 // investigate further. crbug.com/669775
403 return false;
cblume33e0cb52016-08-30 12:09:23 -0700404}
405
406namespace {
407
408class DTIBufferFiller
409{
410public:
cblume921bc672016-09-22 05:25:26 -0700411 explicit DTIBufferFiller(char* bufferAsCharPtr)
412 : bufferAsCharPtr_(bufferAsCharPtr) {}
cblume33e0cb52016-08-30 12:09:23 -0700413
414 void fillMember(const void* source, size_t memberOffset, size_t size) {
cblume921bc672016-09-22 05:25:26 -0700415 memcpy(bufferAsCharPtr_ + memberOffset, source, size);
cblume33e0cb52016-08-30 12:09:23 -0700416 }
417
418private:
419
cblume921bc672016-09-22 05:25:26 -0700420 char* bufferAsCharPtr_;
cblume33e0cb52016-08-30 12:09:23 -0700421};
422}
423
424#define FILL_MEMBER(bufferFiller, member, source) \
425 bufferFiller.fillMember(source, \
426 offsetof(DeferredTextureImage, member), \
427 sizeof(DeferredTextureImage::member));
428
bsalomon41b952c2016-03-11 06:46:33 -0800429size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
ericrkb4da01d2016-06-13 11:18:14 -0700430 const DeferredTextureImageUsageParams params[],
cblume33e0cb52016-08-30 12:09:23 -0700431 int paramCnt, void* buffer,
Brian Osman6c15cc72016-10-17 09:51:37 -0400432 SkColorSpace* dstColorSpace) const {
ericrkb4da01d2016-06-13 11:18:14 -0700433 // Extract relevant min/max values from the params array.
434 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
435 SkFilterQuality highestFilterQuality = params[0].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700436 bool useMipMaps = should_use_mip_maps(params[0]);
ericrkb4da01d2016-06-13 11:18:14 -0700437 for (int i = 1; i < paramCnt; ++i) {
438 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
439 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
440 if (highestFilterQuality < params[i].fQuality)
441 highestFilterQuality = params[i].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700442 useMipMaps |= should_use_mip_maps(params[i]);
ericrkb4da01d2016-06-13 11:18:14 -0700443 }
444
bsalomon41b952c2016-03-11 06:46:33 -0800445 const bool fillMode = SkToBool(buffer);
446 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
447 return 0;
448 }
449
ericrkb4da01d2016-06-13 11:18:14 -0700450 // Calculate scaling parameters.
451 bool isScaled = lowestPreScaleMipLevel != 0;
452
453 SkISize scaledSize;
454 if (isScaled) {
455 // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the
456 // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level.
457 scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(),
458 lowestPreScaleMipLevel - 1);
459 } else {
460 scaledSize = SkISize::Make(this->width(), this->height());
461 }
462
463 // We never want to scale at higher than SW medium quality, as SW medium matches GPU high.
464 SkFilterQuality scaleFilterQuality = highestFilterQuality;
465 if (scaleFilterQuality > kMedium_SkFilterQuality) {
466 scaleFilterQuality = kMedium_SkFilterQuality;
467 }
468
ericrkc429baf2016-03-24 15:35:45 -0700469 const int maxTextureSize = proxy.fCaps->maxTextureSize();
ericrkb4da01d2016-06-13 11:18:14 -0700470 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkc429baf2016-03-24 15:35:45 -0700471 return 0;
472 }
473
bsalomon41b952c2016-03-11 06:46:33 -0800474 SkAutoPixmapStorage pixmap;
475 SkImageInfo info;
476 size_t pixelSize = 0;
477 size_t ctSize = 0;
478 int ctCount = 0;
ericrkb4da01d2016-06-13 11:18:14 -0700479 if (!isScaled && this->peekPixels(&pixmap)) {
bsalomon41b952c2016-03-11 06:46:33 -0800480 info = pixmap.info();
481 pixelSize = SkAlign8(pixmap.getSafeSize());
482 if (pixmap.ctable()) {
483 ctCount = pixmap.ctable()->count();
484 ctSize = SkAlign8(pixmap.ctable()->count() * 4);
485 }
486 } else {
487 // Here we're just using presence of data to know whether there is a codec behind the image.
488 // In the future we will access the cacherator and get the exact data that we want to (e.g.
489 // yuv planes) upload.
bungemanffae30d2016-08-03 13:32:32 -0700490 sk_sp<SkData> data(this->refEncoded());
ericrkb4da01d2016-06-13 11:18:14 -0700491 if (!data && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800492 return 0;
493 }
Brian Osman7992da32016-11-18 11:28:24 -0500494 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
495 // Generator backed image. Tweak info to trigger correct kind of decode.
Brian Osman7992da32016-11-18 11:28:24 -0500496 SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat(
Brian Osman61624f02016-12-09 14:51:59 -0500497 dstColorSpace, proxy.fCaps.get());
Brian Osman7992da32016-11-18 11:28:24 -0500498 info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(),
499 scaledSize.height());
500
501 } else {
502 info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height());
503 }
bsalomon41b952c2016-03-11 06:46:33 -0800504 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
505 if (fillMode) {
506 pixmap.alloc(info);
ericrkb4da01d2016-06-13 11:18:14 -0700507 if (isScaled) {
508 if (!this->scalePixels(pixmap, scaleFilterQuality,
509 SkImage::kDisallow_CachingHint)) {
510 return 0;
511 }
512 } else {
513 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
514 return 0;
515 }
bsalomon41b952c2016-03-11 06:46:33 -0800516 }
517 SkASSERT(!pixmap.ctable());
518 }
519 }
cblume2c052802016-05-31 09:55:08 -0700520 int mipMapLevelCount = 1;
cblume33e0cb52016-08-30 12:09:23 -0700521 if (useMipMaps) {
522 // SkMipMap only deals with the mipmap levels it generates, which does
523 // not include the base level.
524 // That means it generates and holds levels 1-x instead of 0-x.
525 // So the total mipmap level count is 1 more than what
526 // SkMipMap::ComputeLevelCount returns.
527 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1;
528
529 // We already initialized pixelSize to the size of the base level.
530 // SkMipMap will generate the extra mipmap levels. Their sizes need to
531 // be added to the total.
532 // Index 0 here does not refer to the base mipmap level -- it is
533 // SkMipMap's first generated mipmap level (level 1).
534 for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0;
535 currentMipMapLevelIndex--) {
536 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(),
537 currentMipMapLevelIndex);
brianosman32b5e702016-10-13 06:44:23 -0700538 SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight);
cblume33e0cb52016-08-30 12:09:23 -0700539 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr));
540 }
541 }
bsalomon41b952c2016-03-11 06:46:33 -0800542 size_t size = 0;
543 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
544 size += dtiSize;
cblume33e0cb52016-08-30 12:09:23 -0700545 size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData);
546 // We subtract 1 because DeferredTextureImage already includes the base
547 // level in its size
bsalomon41b952c2016-03-11 06:46:33 -0800548 size_t pixelOffset = size;
549 size += pixelSize;
550 size_t ctOffset = size;
551 size += ctSize;
bsalomon4d516a62016-07-28 13:37:31 -0700552 size_t colorSpaceOffset = 0;
553 size_t colorSpaceSize = 0;
554 if (info.colorSpace()) {
555 colorSpaceOffset = size;
556 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
557 size += colorSpaceSize;
558 }
bsalomon41b952c2016-03-11 06:46:33 -0800559 if (!fillMode) {
560 return size;
561 }
cblume921bc672016-09-22 05:25:26 -0700562 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer);
563 char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset;
564 void* pixels = pixelsAsCharPtr;
cblume33e0cb52016-08-30 12:09:23 -0700565 void* ct = nullptr;
bsalomon41b952c2016-03-11 06:46:33 -0800566 if (ctSize) {
cblume921bc672016-09-22 05:25:26 -0700567 ct = bufferAsCharPtr + ctOffset;
bsalomon41b952c2016-03-11 06:46:33 -0800568 }
569
cblume921bc672016-09-22 05:25:26 -0700570 memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))),
571 pixmap.addr(), pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800572 if (ctSize) {
573 memcpy(ct, pixmap.ctable()->readColors(), ctSize);
574 }
575
Brian Osman6c15cc72016-10-17 09:51:37 -0400576 // If the context has sRGB support, and we're intending to render to a surface with an attached
577 // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB)
578 // aware mip-mapping.
Brian Osman7b8400d2016-11-08 17:08:54 -0500579 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
Brian Osman6c15cc72016-10-17 09:51:37 -0400580 if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) &&
581 info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) {
Brian Osman7b8400d2016-11-08 17:08:54 -0500582 colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware;
Brian Osman6c15cc72016-10-17 09:51:37 -0400583 }
584
bsalomon41b952c2016-03-11 06:46:33 -0800585 SkASSERT(info == pixmap.info());
586 size_t rowBytes = pixmap.rowBytes();
cblume33e0cb52016-08-30 12:09:23 -0700587 static_assert(std::is_standard_layout<DeferredTextureImage>::value,
588 "offsetof, which we use below, requires the type have standard layout");
cblume921bc672016-09-22 05:25:26 -0700589 auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr};
Brian Osman7b8400d2016-11-08 17:08:54 -0500590 FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode);
cblume33e0cb52016-08-30 12:09:23 -0700591 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID);
592 int width = info.width();
593 FILL_MEMBER(dtiBufferFiller, fWidth, &width);
594 int height = info.height();
595 FILL_MEMBER(dtiBufferFiller, fHeight, &height);
596 SkColorType colorType = info.colorType();
597 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType);
598 SkAlphaType alphaType = info.alphaType();
599 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType);
600 FILL_MEMBER(dtiBufferFiller, fColorTableCnt, &ctCount);
601 FILL_MEMBER(dtiBufferFiller, fColorTableData, &ct);
602 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount);
cblume921bc672016-09-22 05:25:26 -0700603 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData),
cblume33e0cb52016-08-30 12:09:23 -0700604 &pixels, sizeof(pixels));
cblume921bc672016-09-22 05:25:26 -0700605 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes),
cblume33e0cb52016-08-30 12:09:23 -0700606 &rowBytes, sizeof(rowBytes));
bsalomon4d516a62016-07-28 13:37:31 -0700607 if (colorSpaceSize) {
cblume921bc672016-09-22 05:25:26 -0700608 void* colorSpace = bufferAsCharPtr + colorSpaceOffset;
cblume33e0cb52016-08-30 12:09:23 -0700609 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace);
610 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize);
cblume921bc672016-09-22 05:25:26 -0700611 info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
bsalomon4d516a62016-07-28 13:37:31 -0700612 } else {
cblume921bc672016-09-22 05:25:26 -0700613 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace),
cblume33e0cb52016-08-30 12:09:23 -0700614 0, sizeof(DeferredTextureImage::fColorSpace));
cblume921bc672016-09-22 05:25:26 -0700615 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize),
cblume33e0cb52016-08-30 12:09:23 -0700616 0, sizeof(DeferredTextureImage::fColorSpaceSize));
617 }
618
619 // Fill in the mipmap levels if they exist
cblume921bc672016-09-22 05:25:26 -0700620 char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700621
622 if (useMipMaps) {
cblume94ddf542016-09-16 10:07:32 -0700623 static_assert(std::is_standard_layout<MipMapLevelData>::value,
624 "offsetof, which we use below, requires the type have a standard layout");
cblume33e0cb52016-08-30 12:09:23 -0700625
Brian Osman7b8400d2016-11-08 17:08:54 -0500626 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
cblume33e0cb52016-08-30 12:09:23 -0700627 // SkMipMap holds only the mipmap levels it generates.
628 // A programmer can use the data they provided to SkMipMap::Build as level 0.
629 // So the SkMipMap provides levels 1-x but it stores them in its own
630 // range 0-(x-1).
631 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1;
632 generatedMipLevelIndex++) {
cblume33e0cb52016-08-30 12:09:23 -0700633 SkMipMap::Level mipLevel;
634 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
635
636 // Make sure the mipmap data is after the start of the buffer
cblume921bc672016-09-22 05:25:26 -0700637 SkASSERT(mipLevelPtr > bufferAsCharPtr);
cblume33e0cb52016-08-30 12:09:23 -0700638 // Make sure the mipmap data starts before the end of the buffer
cblume921bc672016-09-22 05:25:26 -0700639 SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700640 // Make sure the mipmap data ends before the end of the buffer
cblume12f75272016-09-19 06:18:03 -0700641 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <=
cblume921bc672016-09-22 05:25:26 -0700642 bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700643
644 // getSafeSize includes rowbyte padding except for the last row,
645 // right?
646
cblume921bc672016-09-22 05:25:26 -0700647 memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700648
cblume921bc672016-09-22 05:25:26 -0700649 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
650 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
651 offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*));
cblume33e0cb52016-08-30 12:09:23 -0700652 size_t rowBytes = mipLevel.fPixmap.rowBytes();
cblume921bc672016-09-22 05:25:26 -0700653 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
654 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
655 offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes));
cblume33e0cb52016-08-30 12:09:23 -0700656
657 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize());
658 }
bsalomon4d516a62016-07-28 13:37:31 -0700659 }
bsalomon41b952c2016-03-11 06:46:33 -0800660 return size;
661}
662
663sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
664 SkBudgeted budgeted) {
665 if (!data) {
666 return nullptr;
667 }
668 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
669
670 if (!context || context->uniqueID() != dti->fContextUniqueID) {
671 return nullptr;
672 }
Hal Canary67b39de2016-11-07 11:47:44 -0500673 sk_sp<SkColorTable> colorTable;
bsalomon4d516a62016-07-28 13:37:31 -0700674 if (dti->fColorTableCnt) {
675 SkASSERT(dti->fColorTableData);
676 colorTable.reset(new SkColorTable(dti->fColorTableData, dti->fColorTableCnt));
bsalomon41b952c2016-03-11 06:46:33 -0800677 }
cblume33e0cb52016-08-30 12:09:23 -0700678 int mipLevelCount = dti->fMipMapLevelCount;
679 SkASSERT(mipLevelCount >= 1);
bsalomon4d516a62016-07-28 13:37:31 -0700680 sk_sp<SkColorSpace> colorSpace;
681 if (dti->fColorSpaceSize) {
682 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize);
683 }
684 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
685 dti->fColorType, dti->fAlphaType, colorSpace);
cblume33e0cb52016-08-30 12:09:23 -0700686 if (mipLevelCount == 1) {
687 SkPixmap pixmap;
688 pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData,
689 dti->fMipMapLevelData[0].fRowBytes, colorTable.get());
690 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted);
691 } else {
Ben Wagner7ecc5962016-11-02 17:07:33 -0400692 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
cblume33e0cb52016-08-30 12:09:23 -0700693 for (int i = 0; i < mipLevelCount; i++) {
694 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
695 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
696 }
697
698 return SkImage::MakeTextureFromMipMap(context, info, texels.get(),
699 mipLevelCount, SkBudgeted::kYes,
Brian Osman7b8400d2016-11-08 17:08:54 -0500700 dti->fColorMode);
cblume33e0cb52016-08-30 12:09:23 -0700701 }
bsalomon41b952c2016-03-11 06:46:33 -0800702}
703
704///////////////////////////////////////////////////////////////////////////////////////////////////
705
cblume186d2d42016-06-03 11:17:42 -0700706sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
707 const GrMipLevel* texels, int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700708 SkBudgeted budgeted,
Brian Osman7b8400d2016-11-08 17:08:54 -0500709 SkDestinationSurfaceColorMode colorMode) {
cblume186d2d42016-06-03 11:17:42 -0700710 if (!ctx) {
711 return nullptr;
712 }
bungeman6bd52842016-10-27 09:30:08 -0700713 sk_sp<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount));
cblume186d2d42016-06-03 11:17:42 -0700714 if (!texture) {
715 return nullptr;
716 }
Brian Osman7b8400d2016-11-08 17:08:54 -0500717 texture->texturePriv().setMipColorMode(colorMode);
cblume186d2d42016-06-03 11:17:42 -0700718 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700719 info.alphaType(), std::move(texture),
720 sk_ref_sp(info.colorSpace()), budgeted);
cblume186d2d42016-06-03 11:17:42 -0700721}