blob: b56b1bab1532b4b0baf3016bd05c369b0ea4a6ae [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
Brian Salomon514baff2016-11-17 15:17:07 -050094GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrSamplerParams& params,
Brian Osman61624f02016-12-09 14:51:59 -050095 SkColorSpace* dstColorSpace,
Brian Osman7992da32016-11-18 11:28:24 -050096 sk_sp<SkColorSpace>* texColorSpace) const {
97 if (texColorSpace) {
98 *texColorSpace = this->fColorSpace;
99 }
100 GrTextureAdjuster adjuster(this->peekTexture(), this->alphaType(), this->bounds(),
101 this->uniqueID(), this->fColorSpace.get());
Brian Osman61624f02016-12-09 14:51:59 -0500102 return adjuster.refTextureSafeForParams(params, nullptr);
reed85d91782015-09-10 14:33:38 -0700103}
104
reed8b26b992015-05-07 15:36:17 -0700105static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
106 switch (info.colorType()) {
107 case kRGBA_8888_SkColorType:
108 case kBGRA_8888_SkColorType:
109 break;
110 default:
111 return; // nothing to do
112 }
113
114 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
115 // and in either case, the alpha-byte is always in the same place, so we can safely call
116 // SkPreMultiplyColor()
117 //
118 SkColor* row = (SkColor*)pixels;
119 for (int y = 0; y < info.height(); ++y) {
120 for (int x = 0; x < info.width(); ++x) {
121 row[x] = SkPreMultiplyColor(row[x]);
122 }
123 }
124}
125
126bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
reed09553032015-11-23 12:32:16 -0800127 int srcX, int srcY, CachingHint) const {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500128 if (!SkImageInfoValidConversion(info, this->onImageInfo())) {
129 return false;
130 }
131
brianosmanb109b8c2016-06-16 13:03:24 -0700132 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fTexture->getContext()->caps());
reed8b26b992015-05-07 15:36:17 -0700133 uint32_t flags = 0;
134 if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) {
135 // let the GPU perform this transformation for us
136 flags = GrContext::kUnpremul_PixelOpsFlag;
137 }
Brian Osmanb62ea222016-12-22 11:12:16 -0500138 if (!fTexture->readPixels(fColorSpace.get(), srcX, srcY, info.width(), info.height(), config,
139 info.colorSpace(), pixels, rowBytes, flags)) {
reed8b26b992015-05-07 15:36:17 -0700140 return false;
141 }
142 // do we have to manually fix-up the alpha channel?
143 // src dst
144 // unpremul premul fix manually
145 // premul unpremul done by kUnpremul_PixelOpsFlag
146 // all other combos need to change.
147 //
148 // Should this be handled by Ganesh? todo:?
149 //
150 if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
151 apply_premul(info, pixels, rowBytes);
152 }
153 return true;
154}
155
reed7fb4f8b2016-03-11 04:33:52 -0800156sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700157 GrContext* ctx = fTexture->getContext();
158 GrSurfaceDesc desc = fTexture->desc();
159 desc.fWidth = subset.width();
160 desc.fHeight = subset.height();
161
Robert Phillipse2f7d182016-12-15 09:23:05 -0500162 sk_sp<GrSurfaceContext> sContext(ctx->contextPriv().makeDeferredSurfaceContext(
163 desc,
164 SkBackingFit::kExact,
165 fBudgeted));
166 if (!sContext) {
167 return nullptr;
168 }
169
170 // TODO: make gpu images be proxy-backed so we don't need to do this
171 sk_sp<GrSurfaceProxy> tmpSrc(GrSurfaceProxy::MakeWrapped(fTexture));
172 if (!tmpSrc) {
173 return nullptr;
174 }
175
176 if (!sContext->copy(tmpSrc.get(), subset, SkIPoint::Make(0, 0))) {
177 return nullptr;
178 }
179
180 // TODO: make gpu images be proxy-backed so we don't need to do this
181 GrSurface* subTx = sContext->asDeferredSurface()->instantiate(ctx->textureProvider());
reed7b6945b2015-09-24 00:50:58 -0700182 if (!subTx) {
183 return nullptr;
184 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500185
reed7fb4f8b2016-03-11 04:33:52 -0800186 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500187 fAlphaType, sk_ref_sp(subTx->asTexture()),
188 fColorSpace, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700189}
190
reed8b26b992015-05-07 15:36:17 -0700191///////////////////////////////////////////////////////////////////////////////////////////////////
192
reed7fb4f8b2016-03-11 04:33:52 -0800193static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700194 SkAlphaType at, sk_sp<SkColorSpace> colorSpace,
195 GrWrapOwnership ownership,
reed7fb4f8b2016-03-11 04:33:52 -0800196 SkImage::TextureReleaseProc releaseProc,
197 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700198 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700199 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700200 }
bungeman6bd52842016-10-27 09:30:08 -0700201 sk_sp<GrTexture> tex = ctx->textureProvider()->wrapBackendTexture(desc, ownership);
reed8b26b992015-05-07 15:36:17 -0700202 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700203 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700204 }
reedde499882015-06-18 13:41:40 -0700205 if (releaseProc) {
206 tex->setRelease(releaseProc, releaseCtx);
207 }
208
bsalomon5ec26ae2016-02-25 08:33:02 -0800209 const SkBudgeted budgeted = SkBudgeted::kNo;
reed7fb4f8b2016-03-11 04:33:52 -0800210 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700211 at, std::move(tex), std::move(colorSpace), budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700212}
213
reed7fb4f8b2016-03-11 04:33:52 -0800214sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700215 SkAlphaType at, sk_sp<SkColorSpace> cs,
216 TextureReleaseProc releaseP, ReleaseContext releaseC) {
217 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kBorrow_GrWrapOwnership,
218 releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700219}
220
reed7fb4f8b2016-03-11 04:33:52 -0800221sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700222 SkAlphaType at, sk_sp<SkColorSpace> cs) {
223 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kAdopt_GrWrapOwnership,
224 nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700225}
226
jbaumanb445a572016-06-09 13:24:48 -0700227static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace,
228 bool nv12,
229 const GrBackendObject yuvTextureHandles[],
230 const SkISize yuvSizes[],
brianosmandddbe382016-07-20 13:55:39 -0700231 GrSurfaceOrigin origin,
232 sk_sp<SkColorSpace> imageColorSpace) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800233 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700234
jbaumanb445a572016-06-09 13:24:48 -0700235 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 ||
236 yuvSizes[1].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700237 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700238 }
jbaumanb445a572016-06-09 13:24:48 -0700239 if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) {
240 return nullptr;
241 }
242
243 const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig;
244
bsalomon993a4212015-05-29 11:37:25 -0700245 GrBackendTextureDesc yDesc;
246 yDesc.fConfig = kConfig;
247 yDesc.fOrigin = origin;
248 yDesc.fSampleCnt = 0;
249 yDesc.fTextureHandle = yuvTextureHandles[0];
250 yDesc.fWidth = yuvSizes[0].fWidth;
251 yDesc.fHeight = yuvSizes[0].fHeight;
252
253 GrBackendTextureDesc uDesc;
254 uDesc.fConfig = kConfig;
255 uDesc.fOrigin = origin;
256 uDesc.fSampleCnt = 0;
257 uDesc.fTextureHandle = yuvTextureHandles[1];
258 uDesc.fWidth = yuvSizes[1].fWidth;
259 uDesc.fHeight = yuvSizes[1].fHeight;
260
jbaumanb445a572016-06-09 13:24:48 -0700261 sk_sp<GrTexture> yTex(
262 ctx->textureProvider()->wrapBackendTexture(yDesc, kBorrow_GrWrapOwnership));
263 sk_sp<GrTexture> uTex(
264 ctx->textureProvider()->wrapBackendTexture(uDesc, kBorrow_GrWrapOwnership));
265 sk_sp<GrTexture> vTex;
266 if (nv12) {
267 vTex = uTex;
268 } else {
269 GrBackendTextureDesc vDesc;
270 vDesc.fConfig = kConfig;
271 vDesc.fOrigin = origin;
272 vDesc.fSampleCnt = 0;
273 vDesc.fTextureHandle = yuvTextureHandles[2];
274 vDesc.fWidth = yuvSizes[2].fWidth;
275 vDesc.fHeight = yuvSizes[2].fHeight;
bsalomon993a4212015-05-29 11:37:25 -0700276
jbaumanb445a572016-06-09 13:24:48 -0700277 vTex = sk_sp<GrTexture>(
278 ctx->textureProvider()->wrapBackendTexture(vDesc, kBorrow_GrWrapOwnership));
279 }
bsalomon993a4212015-05-29 11:37:25 -0700280 if (!yTex || !uTex || !vTex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700281 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700282 }
283
robertphillipsd4c741e2016-04-28 09:55:15 -0700284 const int width = yuvSizes[0].fWidth;
285 const int height = yuvSizes[0].fHeight;
robertphillipsaa19a5f2016-04-28 06:21:55 -0700286
robertphillipsd4c741e2016-04-28 09:55:15 -0700287 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
Brian Osman11052242016-10-27 14:47:55 -0400288 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeRenderTargetContext(
289 SkBackingFit::kExact,
290 width, height,
291 kRGBA_8888_GrPixelConfig,
292 std::move(imageColorSpace),
293 0,
294 origin));
295 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700296 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700297 }
298
299 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400300 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
jbaumanb445a572016-06-09 13:24:48 -0700301 paint.addColorFragmentProcessor(
302 GrYUVEffect::MakeYUVToRGB(yTex.get(), uTex.get(), vTex.get(), yuvSizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700303
robertphillipsd4c741e2016-04-28 09:55:15 -0700304 const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillipsc9a37062015-09-01 08:34:28 -0700305
Brian Salomon82f44312017-01-11 13:42:54 -0500306 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Robert Phillipse60ad622016-11-17 10:22:48 -0500307
308 if (!renderTargetContext->accessRenderTarget()) {
309 return nullptr;
310 }
Brian Osman11052242016-10-27 14:47:55 -0400311 ctx->flushSurfaceWrites(renderTargetContext->accessRenderTarget());
robertphillipsd4c741e2016-04-28 09:55:15 -0700312 return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID,
Brian Osman11052242016-10-27 14:47:55 -0400313 kOpaque_SkAlphaType, renderTargetContext->asTexture(),
Robert Phillips75a475c2017-01-13 09:18:59 -0500314 renderTargetContext->refColorSpace(), budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700315}
reed56179002015-07-07 06:11:19 -0700316
jbaumanb445a572016-06-09 13:24:48 -0700317sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
318 const GrBackendObject yuvTextureHandles[3],
brianosmandddbe382016-07-20 13:55:39 -0700319 const SkISize yuvSizes[3], GrSurfaceOrigin origin,
320 sk_sp<SkColorSpace> imageColorSpace) {
321 return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin,
322 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700323}
324
325sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
326 const GrBackendObject yuvTextureHandles[2],
327 const SkISize yuvSizes[2],
brianosmandddbe382016-07-20 13:55:39 -0700328 GrSurfaceOrigin origin,
329 sk_sp<SkColorSpace> imageColorSpace) {
330 return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin,
331 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700332}
333
bsalomon634b4302016-07-12 18:11:17 -0700334sk_sp<SkImage> SkImage::makeNonTextureImage() const {
brianosman396fcdb2016-07-22 06:26:11 -0700335 if (!this->isTextureBacked()) {
bsalomon634b4302016-07-12 18:11:17 -0700336 return sk_ref_sp(const_cast<SkImage*>(this));
337 }
brianosman396fcdb2016-07-22 06:26:11 -0700338 SkImageInfo info = as_IB(this)->onImageInfo();
bsalomon634b4302016-07-12 18:11:17 -0700339 size_t rowBytes = info.minRowBytes();
340 size_t size = info.getSafeSize(rowBytes);
341 auto data = SkData::MakeUninitialized(size);
342 if (!data) {
343 return nullptr;
344 }
345 SkPixmap pm(info, data->writable_data(), rowBytes);
346 if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
347 return nullptr;
348 }
349 return MakeRasterData(info, data, rowBytes);
350}
351
reed7fb4f8b2016-03-11 04:33:52 -0800352sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap,
353 SkBudgeted budgeted) {
bsalomon0d996862016-03-09 18:44:43 -0800354 if (!ctx) {
355 return nullptr;
356 }
bungeman6bd52842016-10-27 09:30:08 -0700357 sk_sp<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted));
bsalomon0d996862016-03-09 18:44:43 -0800358 if (!texture) {
359 return nullptr;
360 }
reed7fb4f8b2016-03-11 04:33:52 -0800361 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700362 pixmap.alphaType(), std::move(texture),
brianosmandddbe382016-07-20 13:55:39 -0700363 sk_ref_sp(pixmap.info().colorSpace()), budgeted);
bsalomon0d996862016-03-09 18:44:43 -0800364}
365
reed56179002015-07-07 06:11:19 -0700366///////////////////////////////////////////////////////////////////////////////////////////////////
367
bsalomon4d516a62016-07-28 13:37:31 -0700368namespace {
369struct MipMapLevelData {
370 void* fPixelData;
371 size_t fRowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800372};
373
bsalomon4d516a62016-07-28 13:37:31 -0700374struct DeferredTextureImage {
Brian Osman7b8400d2016-11-08 17:08:54 -0500375 uint32_t fContextUniqueID;
376 // Right now, the destination color mode is only considered when generating mipmaps
377 SkDestinationSurfaceColorMode fColorMode;
bsalomon4d516a62016-07-28 13:37:31 -0700378 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace.
Brian Osman7b8400d2016-11-08 17:08:54 -0500379 int fWidth;
380 int fHeight;
381 SkColorType fColorType;
382 SkAlphaType fAlphaType;
383 void* fColorSpace;
384 size_t fColorSpaceSize;
385 int fColorTableCnt;
386 uint32_t* fColorTableData;
387 int fMipMapLevelCount;
bsalomon4d516a62016-07-28 13:37:31 -0700388 // The fMipMapLevelData array may contain more than 1 element.
389 // It contains fMipMapLevelCount elements.
390 // That means this struct's size is not known at compile-time.
Brian Osman7b8400d2016-11-08 17:08:54 -0500391 MipMapLevelData fMipMapLevelData[1];
bsalomon4d516a62016-07-28 13:37:31 -0700392};
393} // anonymous namespace
394
cblume33e0cb52016-08-30 12:09:23 -0700395static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
Eric Karla422d702016-11-30 11:09:29 -0800396 // There is a bug in the mipmap pre-generation logic in use in getDeferredTextureImageData.
397 // This can cause runaway memory leaks, so we are disabling this path until we can
398 // investigate further. crbug.com/669775
399 return false;
cblume33e0cb52016-08-30 12:09:23 -0700400}
401
402namespace {
403
404class DTIBufferFiller
405{
406public:
cblume921bc672016-09-22 05:25:26 -0700407 explicit DTIBufferFiller(char* bufferAsCharPtr)
408 : bufferAsCharPtr_(bufferAsCharPtr) {}
cblume33e0cb52016-08-30 12:09:23 -0700409
410 void fillMember(const void* source, size_t memberOffset, size_t size) {
cblume921bc672016-09-22 05:25:26 -0700411 memcpy(bufferAsCharPtr_ + memberOffset, source, size);
cblume33e0cb52016-08-30 12:09:23 -0700412 }
413
414private:
415
cblume921bc672016-09-22 05:25:26 -0700416 char* bufferAsCharPtr_;
cblume33e0cb52016-08-30 12:09:23 -0700417};
418}
419
420#define FILL_MEMBER(bufferFiller, member, source) \
421 bufferFiller.fillMember(source, \
422 offsetof(DeferredTextureImage, member), \
423 sizeof(DeferredTextureImage::member));
424
bsalomon41b952c2016-03-11 06:46:33 -0800425size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
ericrkb4da01d2016-06-13 11:18:14 -0700426 const DeferredTextureImageUsageParams params[],
cblume33e0cb52016-08-30 12:09:23 -0700427 int paramCnt, void* buffer,
Brian Osman6c15cc72016-10-17 09:51:37 -0400428 SkColorSpace* dstColorSpace) const {
ericrkb4da01d2016-06-13 11:18:14 -0700429 // Extract relevant min/max values from the params array.
430 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
431 SkFilterQuality highestFilterQuality = params[0].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700432 bool useMipMaps = should_use_mip_maps(params[0]);
ericrkb4da01d2016-06-13 11:18:14 -0700433 for (int i = 1; i < paramCnt; ++i) {
434 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
435 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
436 if (highestFilterQuality < params[i].fQuality)
437 highestFilterQuality = params[i].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700438 useMipMaps |= should_use_mip_maps(params[i]);
ericrkb4da01d2016-06-13 11:18:14 -0700439 }
440
bsalomon41b952c2016-03-11 06:46:33 -0800441 const bool fillMode = SkToBool(buffer);
442 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
443 return 0;
444 }
445
ericrkb4da01d2016-06-13 11:18:14 -0700446 // Calculate scaling parameters.
447 bool isScaled = lowestPreScaleMipLevel != 0;
448
449 SkISize scaledSize;
450 if (isScaled) {
451 // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the
452 // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level.
453 scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(),
454 lowestPreScaleMipLevel - 1);
455 } else {
456 scaledSize = SkISize::Make(this->width(), this->height());
457 }
458
459 // We never want to scale at higher than SW medium quality, as SW medium matches GPU high.
460 SkFilterQuality scaleFilterQuality = highestFilterQuality;
461 if (scaleFilterQuality > kMedium_SkFilterQuality) {
462 scaleFilterQuality = kMedium_SkFilterQuality;
463 }
464
ericrkc429baf2016-03-24 15:35:45 -0700465 const int maxTextureSize = proxy.fCaps->maxTextureSize();
ericrkb4da01d2016-06-13 11:18:14 -0700466 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkc429baf2016-03-24 15:35:45 -0700467 return 0;
468 }
469
bsalomon41b952c2016-03-11 06:46:33 -0800470 SkAutoPixmapStorage pixmap;
471 SkImageInfo info;
472 size_t pixelSize = 0;
473 size_t ctSize = 0;
474 int ctCount = 0;
ericrkb4da01d2016-06-13 11:18:14 -0700475 if (!isScaled && this->peekPixels(&pixmap)) {
bsalomon41b952c2016-03-11 06:46:33 -0800476 info = pixmap.info();
477 pixelSize = SkAlign8(pixmap.getSafeSize());
478 if (pixmap.ctable()) {
479 ctCount = pixmap.ctable()->count();
480 ctSize = SkAlign8(pixmap.ctable()->count() * 4);
481 }
482 } else {
483 // Here we're just using presence of data to know whether there is a codec behind the image.
484 // In the future we will access the cacherator and get the exact data that we want to (e.g.
485 // yuv planes) upload.
bungemanffae30d2016-08-03 13:32:32 -0700486 sk_sp<SkData> data(this->refEncoded());
ericrkb4da01d2016-06-13 11:18:14 -0700487 if (!data && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800488 return 0;
489 }
Brian Osman7992da32016-11-18 11:28:24 -0500490 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
491 // Generator backed image. Tweak info to trigger correct kind of decode.
Brian Osman7992da32016-11-18 11:28:24 -0500492 SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat(
Brian Osman61624f02016-12-09 14:51:59 -0500493 dstColorSpace, proxy.fCaps.get());
Brian Osman7992da32016-11-18 11:28:24 -0500494 info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(),
495 scaledSize.height());
496
497 } else {
498 info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height());
499 }
bsalomon41b952c2016-03-11 06:46:33 -0800500 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
501 if (fillMode) {
502 pixmap.alloc(info);
ericrkb4da01d2016-06-13 11:18:14 -0700503 if (isScaled) {
504 if (!this->scalePixels(pixmap, scaleFilterQuality,
505 SkImage::kDisallow_CachingHint)) {
506 return 0;
507 }
508 } else {
509 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
510 return 0;
511 }
bsalomon41b952c2016-03-11 06:46:33 -0800512 }
513 SkASSERT(!pixmap.ctable());
514 }
515 }
cblume2c052802016-05-31 09:55:08 -0700516 int mipMapLevelCount = 1;
cblume33e0cb52016-08-30 12:09:23 -0700517 if (useMipMaps) {
518 // SkMipMap only deals with the mipmap levels it generates, which does
519 // not include the base level.
520 // That means it generates and holds levels 1-x instead of 0-x.
521 // So the total mipmap level count is 1 more than what
522 // SkMipMap::ComputeLevelCount returns.
523 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1;
524
525 // We already initialized pixelSize to the size of the base level.
526 // SkMipMap will generate the extra mipmap levels. Their sizes need to
527 // be added to the total.
528 // Index 0 here does not refer to the base mipmap level -- it is
529 // SkMipMap's first generated mipmap level (level 1).
530 for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0;
531 currentMipMapLevelIndex--) {
532 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(),
533 currentMipMapLevelIndex);
brianosman32b5e702016-10-13 06:44:23 -0700534 SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight);
cblume33e0cb52016-08-30 12:09:23 -0700535 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr));
536 }
537 }
bsalomon41b952c2016-03-11 06:46:33 -0800538 size_t size = 0;
539 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
540 size += dtiSize;
cblume33e0cb52016-08-30 12:09:23 -0700541 size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData);
542 // We subtract 1 because DeferredTextureImage already includes the base
543 // level in its size
bsalomon41b952c2016-03-11 06:46:33 -0800544 size_t pixelOffset = size;
545 size += pixelSize;
546 size_t ctOffset = size;
547 size += ctSize;
bsalomon4d516a62016-07-28 13:37:31 -0700548 size_t colorSpaceOffset = 0;
549 size_t colorSpaceSize = 0;
550 if (info.colorSpace()) {
551 colorSpaceOffset = size;
552 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
553 size += colorSpaceSize;
554 }
bsalomon41b952c2016-03-11 06:46:33 -0800555 if (!fillMode) {
556 return size;
557 }
cblume921bc672016-09-22 05:25:26 -0700558 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer);
559 char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset;
560 void* pixels = pixelsAsCharPtr;
cblume33e0cb52016-08-30 12:09:23 -0700561 void* ct = nullptr;
bsalomon41b952c2016-03-11 06:46:33 -0800562 if (ctSize) {
cblume921bc672016-09-22 05:25:26 -0700563 ct = bufferAsCharPtr + ctOffset;
bsalomon41b952c2016-03-11 06:46:33 -0800564 }
565
cblume921bc672016-09-22 05:25:26 -0700566 memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))),
567 pixmap.addr(), pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800568 if (ctSize) {
569 memcpy(ct, pixmap.ctable()->readColors(), ctSize);
570 }
571
Brian Osman6c15cc72016-10-17 09:51:37 -0400572 // 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 Osman7b8400d2016-11-08 17:08:54 -0500575 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
Brian Osman6c15cc72016-10-17 09:51:37 -0400576 if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) &&
577 info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) {
Brian Osman7b8400d2016-11-08 17:08:54 -0500578 colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware;
Brian Osman6c15cc72016-10-17 09:51:37 -0400579 }
580
bsalomon41b952c2016-03-11 06:46:33 -0800581 SkASSERT(info == pixmap.info());
582 size_t rowBytes = pixmap.rowBytes();
cblume33e0cb52016-08-30 12:09:23 -0700583 static_assert(std::is_standard_layout<DeferredTextureImage>::value,
584 "offsetof, which we use below, requires the type have standard layout");
cblume921bc672016-09-22 05:25:26 -0700585 auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr};
Brian Osman7b8400d2016-11-08 17:08:54 -0500586 FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode);
cblume33e0cb52016-08-30 12:09:23 -0700587 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);
596 FILL_MEMBER(dtiBufferFiller, fColorTableCnt, &ctCount);
597 FILL_MEMBER(dtiBufferFiller, fColorTableData, &ct);
598 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount);
cblume921bc672016-09-22 05:25:26 -0700599 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData),
cblume33e0cb52016-08-30 12:09:23 -0700600 &pixels, sizeof(pixels));
cblume921bc672016-09-22 05:25:26 -0700601 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes),
cblume33e0cb52016-08-30 12:09:23 -0700602 &rowBytes, sizeof(rowBytes));
bsalomon4d516a62016-07-28 13:37:31 -0700603 if (colorSpaceSize) {
cblume921bc672016-09-22 05:25:26 -0700604 void* colorSpace = bufferAsCharPtr + colorSpaceOffset;
cblume33e0cb52016-08-30 12:09:23 -0700605 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace);
606 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize);
cblume921bc672016-09-22 05:25:26 -0700607 info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
bsalomon4d516a62016-07-28 13:37:31 -0700608 } else {
cblume921bc672016-09-22 05:25:26 -0700609 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace),
cblume33e0cb52016-08-30 12:09:23 -0700610 0, sizeof(DeferredTextureImage::fColorSpace));
cblume921bc672016-09-22 05:25:26 -0700611 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize),
cblume33e0cb52016-08-30 12:09:23 -0700612 0, sizeof(DeferredTextureImage::fColorSpaceSize));
613 }
614
615 // Fill in the mipmap levels if they exist
cblume921bc672016-09-22 05:25:26 -0700616 char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700617
618 if (useMipMaps) {
cblume94ddf542016-09-16 10:07:32 -0700619 static_assert(std::is_standard_layout<MipMapLevelData>::value,
620 "offsetof, which we use below, requires the type have a standard layout");
cblume33e0cb52016-08-30 12:09:23 -0700621
Brian Osman7b8400d2016-11-08 17:08:54 -0500622 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
cblume33e0cb52016-08-30 12:09:23 -0700623 // SkMipMap holds only the mipmap levels it generates.
624 // A programmer can use the data they provided to SkMipMap::Build as level 0.
625 // So the SkMipMap provides levels 1-x but it stores them in its own
626 // range 0-(x-1).
627 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1;
628 generatedMipLevelIndex++) {
cblume33e0cb52016-08-30 12:09:23 -0700629 SkMipMap::Level mipLevel;
630 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
631
632 // Make sure the mipmap data is after the start of the buffer
cblume921bc672016-09-22 05:25:26 -0700633 SkASSERT(mipLevelPtr > bufferAsCharPtr);
cblume33e0cb52016-08-30 12:09:23 -0700634 // Make sure the mipmap data starts before the end of the buffer
cblume921bc672016-09-22 05:25:26 -0700635 SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700636 // Make sure the mipmap data ends before the end of the buffer
cblume12f75272016-09-19 06:18:03 -0700637 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <=
cblume921bc672016-09-22 05:25:26 -0700638 bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700639
640 // getSafeSize includes rowbyte padding except for the last row,
641 // right?
642
cblume921bc672016-09-22 05:25:26 -0700643 memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700644
cblume921bc672016-09-22 05:25:26 -0700645 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
646 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
647 offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*));
cblume33e0cb52016-08-30 12:09:23 -0700648 size_t rowBytes = mipLevel.fPixmap.rowBytes();
cblume921bc672016-09-22 05:25:26 -0700649 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
650 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
651 offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes));
cblume33e0cb52016-08-30 12:09:23 -0700652
653 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize());
654 }
bsalomon4d516a62016-07-28 13:37:31 -0700655 }
bsalomon41b952c2016-03-11 06:46:33 -0800656 return size;
657}
658
659sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
660 SkBudgeted budgeted) {
661 if (!data) {
662 return nullptr;
663 }
664 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
665
666 if (!context || context->uniqueID() != dti->fContextUniqueID) {
667 return nullptr;
668 }
Hal Canary67b39de2016-11-07 11:47:44 -0500669 sk_sp<SkColorTable> colorTable;
bsalomon4d516a62016-07-28 13:37:31 -0700670 if (dti->fColorTableCnt) {
671 SkASSERT(dti->fColorTableData);
672 colorTable.reset(new SkColorTable(dti->fColorTableData, dti->fColorTableCnt));
bsalomon41b952c2016-03-11 06:46:33 -0800673 }
cblume33e0cb52016-08-30 12:09:23 -0700674 int mipLevelCount = dti->fMipMapLevelCount;
675 SkASSERT(mipLevelCount >= 1);
bsalomon4d516a62016-07-28 13:37:31 -0700676 sk_sp<SkColorSpace> colorSpace;
677 if (dti->fColorSpaceSize) {
678 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize);
679 }
680 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
681 dti->fColorType, dti->fAlphaType, colorSpace);
cblume33e0cb52016-08-30 12:09:23 -0700682 if (mipLevelCount == 1) {
683 SkPixmap pixmap;
684 pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData,
685 dti->fMipMapLevelData[0].fRowBytes, colorTable.get());
686 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted);
687 } else {
Ben Wagner7ecc5962016-11-02 17:07:33 -0400688 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
cblume33e0cb52016-08-30 12:09:23 -0700689 for (int i = 0; i < mipLevelCount; i++) {
690 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
691 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
692 }
693
694 return SkImage::MakeTextureFromMipMap(context, info, texels.get(),
695 mipLevelCount, SkBudgeted::kYes,
Brian Osman7b8400d2016-11-08 17:08:54 -0500696 dti->fColorMode);
cblume33e0cb52016-08-30 12:09:23 -0700697 }
bsalomon41b952c2016-03-11 06:46:33 -0800698}
699
700///////////////////////////////////////////////////////////////////////////////////////////////////
701
cblume186d2d42016-06-03 11:17:42 -0700702sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
703 const GrMipLevel* texels, int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700704 SkBudgeted budgeted,
Brian Osman7b8400d2016-11-08 17:08:54 -0500705 SkDestinationSurfaceColorMode colorMode) {
cblume186d2d42016-06-03 11:17:42 -0700706 if (!ctx) {
707 return nullptr;
708 }
bungeman6bd52842016-10-27 09:30:08 -0700709 sk_sp<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount));
cblume186d2d42016-06-03 11:17:42 -0700710 if (!texture) {
711 return nullptr;
712 }
Brian Osman7b8400d2016-11-08 17:08:54 -0500713 texture->texturePriv().setMipColorMode(colorMode);
cblume186d2d42016-06-03 11:17:42 -0700714 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700715 info.alphaType(), std::move(texture),
716 sk_ref_sp(info.colorSpace()), budgeted);
cblume186d2d42016-06-03 11:17:42 -0700717}