blob: c347a1f6060c1440db97bc48cfe36a10cb738157 [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"
ericrkb4da01d2016-06-13 11:18:14 -070027#include "SkMipMap.h"
reed6f1216a2015-08-04 08:10:13 -070028#include "SkPixelRef.h"
bsalomon993a4212015-05-29 11:37:25 -070029
bungeman6bd52842016-10-27 09:30:08 -070030SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, sk_sp<GrTexture> tex,
brianosmandddbe382016-07-20 13:55:39 -070031 sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted)
reedaf3fbfc2015-10-04 11:28:36 -070032 : INHERITED(w, h, uniqueID)
bungeman6bd52842016-10-27 09:30:08 -070033 , fTexture(std::move(tex))
reed8b26b992015-05-07 15:36:17 -070034 , fAlphaType(at)
bsalomoneaaaf0b2015-01-23 08:08:04 -080035 , fBudgeted(budgeted)
brianosmandddbe382016-07-20 13:55:39 -070036 , fColorSpace(std::move(colorSpace))
bsalomon1cd63112015-08-05 06:58:39 -070037 , fAddedRasterVersionToCache(false)
reedc9b5f8b2015-10-22 13:20:20 -070038{
bungeman6bd52842016-10-27 09:30:08 -070039 SkASSERT(fTexture->width() == w);
40 SkASSERT(fTexture->height() == h);
reedc9b5f8b2015-10-22 13:20:20 -070041}
piotaixrcef04f82014-07-14 07:48:04 -070042
reed6f1216a2015-08-04 08:10:13 -070043SkImage_Gpu::~SkImage_Gpu() {
44 if (fAddedRasterVersionToCache.load()) {
45 SkNotifyBitmapGenIDIsStale(this->uniqueID());
46 }
47}
48
bsalomoneaaaf0b2015-01-23 08:08:04 -080049extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
robertphillipsea70c4b2016-07-20 08:54:31 -070050 if (image->isTextureBacked()) {
reed8b26b992015-05-07 15:36:17 -070051 ((SkImage_Gpu*)image)->applyBudgetDecision();
52 }
53}
54
brianosman396fcdb2016-07-22 06:26:11 -070055SkImageInfo SkImage_Gpu::onImageInfo() const {
56 SkColorType ct;
57 if (!GrPixelConfigToColorType(fTexture->config(), &ct)) {
58 ct = kUnknown_SkColorType;
59 }
60 return SkImageInfo::Make(fTexture->width(), fTexture->height(), ct, fAlphaType, fColorSpace);
61}
62
brianosman69c166d2016-08-17 14:01:05 -070063static SkImageInfo make_info(int w, int h, SkAlphaType at, sk_sp<SkColorSpace> colorSpace) {
64 return SkImageInfo::MakeN32(w, h, at, std::move(colorSpace));
reed88d064d2015-10-12 11:30:02 -070065}
66
Brian Osman61624f02016-12-09 14:51:59 -050067bool SkImage_Gpu::getROPixels(SkBitmap* dst, SkColorSpace* dstColorSpace,
Brian Osman7992da32016-11-18 11:28:24 -050068 CachingHint chint) const {
reed6f1216a2015-08-04 08:10:13 -070069 if (SkBitmapCache::Find(this->uniqueID(), dst)) {
70 SkASSERT(dst->getGenerationID() == this->uniqueID());
71 SkASSERT(dst->isImmutable());
72 SkASSERT(dst->getPixels());
73 return true;
74 }
75
brianosman69c166d2016-08-17 14:01:05 -070076 if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->alphaType(),
brianosmandddbe382016-07-20 13:55:39 -070077 this->fColorSpace))) {
reed8b26b992015-05-07 15:36:17 -070078 return false;
79 }
80 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
81 dst->getPixels(), dst->rowBytes())) {
82 return false;
83 }
reed6f1216a2015-08-04 08:10:13 -070084
85 dst->pixelRef()->setImmutableWithID(this->uniqueID());
reed09553032015-11-23 12:32:16 -080086 if (kAllow_CachingHint == chint) {
87 SkBitmapCache::Add(this->uniqueID(), *dst);
88 fAddedRasterVersionToCache.store(true);
89 }
reed8b26b992015-05-07 15:36:17 -070090 return true;
91}
92
Brian Salomon514baff2016-11-17 15:17:07 -050093GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrSamplerParams& params,
Brian Osman61624f02016-12-09 14:51:59 -050094 SkColorSpace* dstColorSpace,
Brian Osman7992da32016-11-18 11:28:24 -050095 sk_sp<SkColorSpace>* texColorSpace) const {
96 if (texColorSpace) {
97 *texColorSpace = this->fColorSpace;
98 }
99 GrTextureAdjuster adjuster(this->peekTexture(), this->alphaType(), this->bounds(),
100 this->uniqueID(), this->fColorSpace.get());
Brian Osman61624f02016-12-09 14:51:59 -0500101 return adjuster.refTextureSafeForParams(params, nullptr);
reed85d91782015-09-10 14:33:38 -0700102}
103
reed8b26b992015-05-07 15:36:17 -0700104static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
105 switch (info.colorType()) {
106 case kRGBA_8888_SkColorType:
107 case kBGRA_8888_SkColorType:
108 break;
109 default:
110 return; // nothing to do
111 }
112
113 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
114 // and in either case, the alpha-byte is always in the same place, so we can safely call
115 // SkPreMultiplyColor()
116 //
117 SkColor* row = (SkColor*)pixels;
118 for (int y = 0; y < info.height(); ++y) {
119 for (int x = 0; x < info.width(); ++x) {
120 row[x] = SkPreMultiplyColor(row[x]);
121 }
122 }
123}
124
125bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
reed09553032015-11-23 12:32:16 -0800126 int srcX, int srcY, CachingHint) const {
brianosmanb109b8c2016-06-16 13:03:24 -0700127 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fTexture->getContext()->caps());
reed8b26b992015-05-07 15:36:17 -0700128 uint32_t flags = 0;
129 if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) {
130 // let the GPU perform this transformation for us
131 flags = GrContext::kUnpremul_PixelOpsFlag;
132 }
133 if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config,
134 pixels, rowBytes, flags)) {
135 return false;
136 }
137 // do we have to manually fix-up the alpha channel?
138 // src dst
139 // unpremul premul fix manually
140 // premul unpremul done by kUnpremul_PixelOpsFlag
141 // all other combos need to change.
142 //
143 // Should this be handled by Ganesh? todo:?
144 //
145 if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
146 apply_premul(info, pixels, rowBytes);
147 }
148 return true;
149}
150
reed7fb4f8b2016-03-11 04:33:52 -0800151sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700152 GrContext* ctx = fTexture->getContext();
153 GrSurfaceDesc desc = fTexture->desc();
154 desc.fWidth = subset.width();
155 desc.fHeight = subset.height();
156
Robert Phillipse2f7d182016-12-15 09:23:05 -0500157 sk_sp<GrSurfaceContext> sContext(ctx->contextPriv().makeDeferredSurfaceContext(
158 desc,
159 SkBackingFit::kExact,
160 fBudgeted));
161 if (!sContext) {
162 return nullptr;
163 }
164
165 // TODO: make gpu images be proxy-backed so we don't need to do this
166 sk_sp<GrSurfaceProxy> tmpSrc(GrSurfaceProxy::MakeWrapped(fTexture));
167 if (!tmpSrc) {
168 return nullptr;
169 }
170
171 if (!sContext->copy(tmpSrc.get(), subset, SkIPoint::Make(0, 0))) {
172 return nullptr;
173 }
174
175 // TODO: make gpu images be proxy-backed so we don't need to do this
176 GrSurface* subTx = sContext->asDeferredSurface()->instantiate(ctx->textureProvider());
reed7b6945b2015-09-24 00:50:58 -0700177 if (!subTx) {
178 return nullptr;
179 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500180
reed7fb4f8b2016-03-11 04:33:52 -0800181 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500182 fAlphaType, sk_ref_sp(subTx->asTexture()),
183 fColorSpace, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700184}
185
reed8b26b992015-05-07 15:36:17 -0700186///////////////////////////////////////////////////////////////////////////////////////////////////
187
reed7fb4f8b2016-03-11 04:33:52 -0800188static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700189 SkAlphaType at, sk_sp<SkColorSpace> colorSpace,
190 GrWrapOwnership ownership,
reed7fb4f8b2016-03-11 04:33:52 -0800191 SkImage::TextureReleaseProc releaseProc,
192 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700193 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700194 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700195 }
bungeman6bd52842016-10-27 09:30:08 -0700196 sk_sp<GrTexture> tex = ctx->textureProvider()->wrapBackendTexture(desc, ownership);
reed8b26b992015-05-07 15:36:17 -0700197 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700198 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700199 }
reedde499882015-06-18 13:41:40 -0700200 if (releaseProc) {
201 tex->setRelease(releaseProc, releaseCtx);
202 }
203
bsalomon5ec26ae2016-02-25 08:33:02 -0800204 const SkBudgeted budgeted = SkBudgeted::kNo;
reed7fb4f8b2016-03-11 04:33:52 -0800205 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700206 at, std::move(tex), std::move(colorSpace), budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700207}
208
reed7fb4f8b2016-03-11 04:33:52 -0800209sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700210 SkAlphaType at, sk_sp<SkColorSpace> cs,
211 TextureReleaseProc releaseP, ReleaseContext releaseC) {
212 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kBorrow_GrWrapOwnership,
213 releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700214}
215
reed7fb4f8b2016-03-11 04:33:52 -0800216sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700217 SkAlphaType at, sk_sp<SkColorSpace> cs) {
218 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kAdopt_GrWrapOwnership,
219 nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700220}
221
jbaumanb445a572016-06-09 13:24:48 -0700222static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace,
223 bool nv12,
224 const GrBackendObject yuvTextureHandles[],
225 const SkISize yuvSizes[],
brianosmandddbe382016-07-20 13:55:39 -0700226 GrSurfaceOrigin origin,
227 sk_sp<SkColorSpace> imageColorSpace) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800228 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700229
jbaumanb445a572016-06-09 13:24:48 -0700230 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 ||
231 yuvSizes[1].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700232 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700233 }
jbaumanb445a572016-06-09 13:24:48 -0700234 if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) {
235 return nullptr;
236 }
237
238 const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig;
239
bsalomon993a4212015-05-29 11:37:25 -0700240 GrBackendTextureDesc yDesc;
241 yDesc.fConfig = kConfig;
242 yDesc.fOrigin = origin;
243 yDesc.fSampleCnt = 0;
244 yDesc.fTextureHandle = yuvTextureHandles[0];
245 yDesc.fWidth = yuvSizes[0].fWidth;
246 yDesc.fHeight = yuvSizes[0].fHeight;
247
248 GrBackendTextureDesc uDesc;
249 uDesc.fConfig = kConfig;
250 uDesc.fOrigin = origin;
251 uDesc.fSampleCnt = 0;
252 uDesc.fTextureHandle = yuvTextureHandles[1];
253 uDesc.fWidth = yuvSizes[1].fWidth;
254 uDesc.fHeight = yuvSizes[1].fHeight;
255
jbaumanb445a572016-06-09 13:24:48 -0700256 sk_sp<GrTexture> yTex(
257 ctx->textureProvider()->wrapBackendTexture(yDesc, kBorrow_GrWrapOwnership));
258 sk_sp<GrTexture> uTex(
259 ctx->textureProvider()->wrapBackendTexture(uDesc, kBorrow_GrWrapOwnership));
260 sk_sp<GrTexture> vTex;
261 if (nv12) {
262 vTex = uTex;
263 } else {
264 GrBackendTextureDesc vDesc;
265 vDesc.fConfig = kConfig;
266 vDesc.fOrigin = origin;
267 vDesc.fSampleCnt = 0;
268 vDesc.fTextureHandle = yuvTextureHandles[2];
269 vDesc.fWidth = yuvSizes[2].fWidth;
270 vDesc.fHeight = yuvSizes[2].fHeight;
bsalomon993a4212015-05-29 11:37:25 -0700271
jbaumanb445a572016-06-09 13:24:48 -0700272 vTex = sk_sp<GrTexture>(
273 ctx->textureProvider()->wrapBackendTexture(vDesc, kBorrow_GrWrapOwnership));
274 }
bsalomon993a4212015-05-29 11:37:25 -0700275 if (!yTex || !uTex || !vTex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700276 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700277 }
278
robertphillipsd4c741e2016-04-28 09:55:15 -0700279 const int width = yuvSizes[0].fWidth;
280 const int height = yuvSizes[0].fHeight;
robertphillipsaa19a5f2016-04-28 06:21:55 -0700281
robertphillipsd4c741e2016-04-28 09:55:15 -0700282 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
Brian Osman11052242016-10-27 14:47:55 -0400283 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeRenderTargetContext(
284 SkBackingFit::kExact,
285 width, height,
286 kRGBA_8888_GrPixelConfig,
287 std::move(imageColorSpace),
288 0,
289 origin));
290 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700291 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700292 }
293
294 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400295 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
jbaumanb445a572016-06-09 13:24:48 -0700296 paint.addColorFragmentProcessor(
297 GrYUVEffect::MakeYUVToRGB(yTex.get(), uTex.get(), vTex.get(), yuvSizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700298
robertphillipsd4c741e2016-04-28 09:55:15 -0700299 const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillipsc9a37062015-09-01 08:34:28 -0700300
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500301 renderTargetContext->drawRect(GrNoClip(), paint, GrAA::kNo, SkMatrix::I(), rect);
Robert Phillipse60ad622016-11-17 10:22:48 -0500302
303 if (!renderTargetContext->accessRenderTarget()) {
304 return nullptr;
305 }
Brian Osman11052242016-10-27 14:47:55 -0400306 ctx->flushSurfaceWrites(renderTargetContext->accessRenderTarget());
robertphillipsd4c741e2016-04-28 09:55:15 -0700307 return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID,
Brian Osman11052242016-10-27 14:47:55 -0400308 kOpaque_SkAlphaType, renderTargetContext->asTexture(),
309 sk_ref_sp(renderTargetContext->getColorSpace()), budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700310}
reed56179002015-07-07 06:11:19 -0700311
jbaumanb445a572016-06-09 13:24:48 -0700312sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
313 const GrBackendObject yuvTextureHandles[3],
brianosmandddbe382016-07-20 13:55:39 -0700314 const SkISize yuvSizes[3], GrSurfaceOrigin origin,
315 sk_sp<SkColorSpace> imageColorSpace) {
316 return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin,
317 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700318}
319
320sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
321 const GrBackendObject yuvTextureHandles[2],
322 const SkISize yuvSizes[2],
brianosmandddbe382016-07-20 13:55:39 -0700323 GrSurfaceOrigin origin,
324 sk_sp<SkColorSpace> imageColorSpace) {
325 return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin,
326 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700327}
328
bsalomon634b4302016-07-12 18:11:17 -0700329sk_sp<SkImage> SkImage::makeNonTextureImage() const {
brianosman396fcdb2016-07-22 06:26:11 -0700330 if (!this->isTextureBacked()) {
bsalomon634b4302016-07-12 18:11:17 -0700331 return sk_ref_sp(const_cast<SkImage*>(this));
332 }
brianosman396fcdb2016-07-22 06:26:11 -0700333 SkImageInfo info = as_IB(this)->onImageInfo();
bsalomon634b4302016-07-12 18:11:17 -0700334 size_t rowBytes = info.minRowBytes();
335 size_t size = info.getSafeSize(rowBytes);
336 auto data = SkData::MakeUninitialized(size);
337 if (!data) {
338 return nullptr;
339 }
340 SkPixmap pm(info, data->writable_data(), rowBytes);
341 if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
342 return nullptr;
343 }
344 return MakeRasterData(info, data, rowBytes);
345}
346
reed7fb4f8b2016-03-11 04:33:52 -0800347sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap,
348 SkBudgeted budgeted) {
bsalomon0d996862016-03-09 18:44:43 -0800349 if (!ctx) {
350 return nullptr;
351 }
bungeman6bd52842016-10-27 09:30:08 -0700352 sk_sp<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted));
bsalomon0d996862016-03-09 18:44:43 -0800353 if (!texture) {
354 return nullptr;
355 }
reed7fb4f8b2016-03-11 04:33:52 -0800356 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700357 pixmap.alphaType(), std::move(texture),
brianosmandddbe382016-07-20 13:55:39 -0700358 sk_ref_sp(pixmap.info().colorSpace()), budgeted);
bsalomon0d996862016-03-09 18:44:43 -0800359}
360
reed56179002015-07-07 06:11:19 -0700361///////////////////////////////////////////////////////////////////////////////////////////////////
362
bsalomon4d516a62016-07-28 13:37:31 -0700363namespace {
364struct MipMapLevelData {
365 void* fPixelData;
366 size_t fRowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800367};
368
bsalomon4d516a62016-07-28 13:37:31 -0700369struct DeferredTextureImage {
Brian Osman7b8400d2016-11-08 17:08:54 -0500370 uint32_t fContextUniqueID;
371 // Right now, the destination color mode is only considered when generating mipmaps
372 SkDestinationSurfaceColorMode fColorMode;
bsalomon4d516a62016-07-28 13:37:31 -0700373 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace.
Brian Osman7b8400d2016-11-08 17:08:54 -0500374 int fWidth;
375 int fHeight;
376 SkColorType fColorType;
377 SkAlphaType fAlphaType;
378 void* fColorSpace;
379 size_t fColorSpaceSize;
380 int fColorTableCnt;
381 uint32_t* fColorTableData;
382 int fMipMapLevelCount;
bsalomon4d516a62016-07-28 13:37:31 -0700383 // The fMipMapLevelData array may contain more than 1 element.
384 // It contains fMipMapLevelCount elements.
385 // That means this struct's size is not known at compile-time.
Brian Osman7b8400d2016-11-08 17:08:54 -0500386 MipMapLevelData fMipMapLevelData[1];
bsalomon4d516a62016-07-28 13:37:31 -0700387};
388} // anonymous namespace
389
cblume33e0cb52016-08-30 12:09:23 -0700390static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
Eric Karla422d702016-11-30 11:09:29 -0800391 // There is a bug in the mipmap pre-generation logic in use in getDeferredTextureImageData.
392 // This can cause runaway memory leaks, so we are disabling this path until we can
393 // investigate further. crbug.com/669775
394 return false;
cblume33e0cb52016-08-30 12:09:23 -0700395}
396
397namespace {
398
399class DTIBufferFiller
400{
401public:
cblume921bc672016-09-22 05:25:26 -0700402 explicit DTIBufferFiller(char* bufferAsCharPtr)
403 : bufferAsCharPtr_(bufferAsCharPtr) {}
cblume33e0cb52016-08-30 12:09:23 -0700404
405 void fillMember(const void* source, size_t memberOffset, size_t size) {
cblume921bc672016-09-22 05:25:26 -0700406 memcpy(bufferAsCharPtr_ + memberOffset, source, size);
cblume33e0cb52016-08-30 12:09:23 -0700407 }
408
409private:
410
cblume921bc672016-09-22 05:25:26 -0700411 char* bufferAsCharPtr_;
cblume33e0cb52016-08-30 12:09:23 -0700412};
413}
414
415#define FILL_MEMBER(bufferFiller, member, source) \
416 bufferFiller.fillMember(source, \
417 offsetof(DeferredTextureImage, member), \
418 sizeof(DeferredTextureImage::member));
419
bsalomon41b952c2016-03-11 06:46:33 -0800420size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
ericrkb4da01d2016-06-13 11:18:14 -0700421 const DeferredTextureImageUsageParams params[],
cblume33e0cb52016-08-30 12:09:23 -0700422 int paramCnt, void* buffer,
Brian Osman6c15cc72016-10-17 09:51:37 -0400423 SkColorSpace* dstColorSpace) const {
ericrkb4da01d2016-06-13 11:18:14 -0700424 // Extract relevant min/max values from the params array.
425 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
426 SkFilterQuality highestFilterQuality = params[0].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700427 bool useMipMaps = should_use_mip_maps(params[0]);
ericrkb4da01d2016-06-13 11:18:14 -0700428 for (int i = 1; i < paramCnt; ++i) {
429 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
430 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
431 if (highestFilterQuality < params[i].fQuality)
432 highestFilterQuality = params[i].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700433 useMipMaps |= should_use_mip_maps(params[i]);
ericrkb4da01d2016-06-13 11:18:14 -0700434 }
435
bsalomon41b952c2016-03-11 06:46:33 -0800436 const bool fillMode = SkToBool(buffer);
437 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
438 return 0;
439 }
440
ericrkb4da01d2016-06-13 11:18:14 -0700441 // Calculate scaling parameters.
442 bool isScaled = lowestPreScaleMipLevel != 0;
443
444 SkISize scaledSize;
445 if (isScaled) {
446 // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the
447 // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level.
448 scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(),
449 lowestPreScaleMipLevel - 1);
450 } else {
451 scaledSize = SkISize::Make(this->width(), this->height());
452 }
453
454 // We never want to scale at higher than SW medium quality, as SW medium matches GPU high.
455 SkFilterQuality scaleFilterQuality = highestFilterQuality;
456 if (scaleFilterQuality > kMedium_SkFilterQuality) {
457 scaleFilterQuality = kMedium_SkFilterQuality;
458 }
459
ericrkc429baf2016-03-24 15:35:45 -0700460 const int maxTextureSize = proxy.fCaps->maxTextureSize();
ericrkb4da01d2016-06-13 11:18:14 -0700461 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkc429baf2016-03-24 15:35:45 -0700462 return 0;
463 }
464
bsalomon41b952c2016-03-11 06:46:33 -0800465 SkAutoPixmapStorage pixmap;
466 SkImageInfo info;
467 size_t pixelSize = 0;
468 size_t ctSize = 0;
469 int ctCount = 0;
ericrkb4da01d2016-06-13 11:18:14 -0700470 if (!isScaled && this->peekPixels(&pixmap)) {
bsalomon41b952c2016-03-11 06:46:33 -0800471 info = pixmap.info();
472 pixelSize = SkAlign8(pixmap.getSafeSize());
473 if (pixmap.ctable()) {
474 ctCount = pixmap.ctable()->count();
475 ctSize = SkAlign8(pixmap.ctable()->count() * 4);
476 }
477 } else {
478 // Here we're just using presence of data to know whether there is a codec behind the image.
479 // In the future we will access the cacherator and get the exact data that we want to (e.g.
480 // yuv planes) upload.
bungemanffae30d2016-08-03 13:32:32 -0700481 sk_sp<SkData> data(this->refEncoded());
ericrkb4da01d2016-06-13 11:18:14 -0700482 if (!data && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800483 return 0;
484 }
Brian Osman7992da32016-11-18 11:28:24 -0500485 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
486 // Generator backed image. Tweak info to trigger correct kind of decode.
Brian Osman7992da32016-11-18 11:28:24 -0500487 SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat(
Brian Osman61624f02016-12-09 14:51:59 -0500488 dstColorSpace, proxy.fCaps.get());
Brian Osman7992da32016-11-18 11:28:24 -0500489 info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(),
490 scaledSize.height());
491
492 } else {
493 info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height());
494 }
bsalomon41b952c2016-03-11 06:46:33 -0800495 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
496 if (fillMode) {
497 pixmap.alloc(info);
ericrkb4da01d2016-06-13 11:18:14 -0700498 if (isScaled) {
499 if (!this->scalePixels(pixmap, scaleFilterQuality,
500 SkImage::kDisallow_CachingHint)) {
501 return 0;
502 }
503 } else {
504 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
505 return 0;
506 }
bsalomon41b952c2016-03-11 06:46:33 -0800507 }
508 SkASSERT(!pixmap.ctable());
509 }
510 }
cblume2c052802016-05-31 09:55:08 -0700511 int mipMapLevelCount = 1;
cblume33e0cb52016-08-30 12:09:23 -0700512 if (useMipMaps) {
513 // SkMipMap only deals with the mipmap levels it generates, which does
514 // not include the base level.
515 // That means it generates and holds levels 1-x instead of 0-x.
516 // So the total mipmap level count is 1 more than what
517 // SkMipMap::ComputeLevelCount returns.
518 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1;
519
520 // We already initialized pixelSize to the size of the base level.
521 // SkMipMap will generate the extra mipmap levels. Their sizes need to
522 // be added to the total.
523 // Index 0 here does not refer to the base mipmap level -- it is
524 // SkMipMap's first generated mipmap level (level 1).
525 for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0;
526 currentMipMapLevelIndex--) {
527 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(),
528 currentMipMapLevelIndex);
brianosman32b5e702016-10-13 06:44:23 -0700529 SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight);
cblume33e0cb52016-08-30 12:09:23 -0700530 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr));
531 }
532 }
bsalomon41b952c2016-03-11 06:46:33 -0800533 size_t size = 0;
534 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
535 size += dtiSize;
cblume33e0cb52016-08-30 12:09:23 -0700536 size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData);
537 // We subtract 1 because DeferredTextureImage already includes the base
538 // level in its size
bsalomon41b952c2016-03-11 06:46:33 -0800539 size_t pixelOffset = size;
540 size += pixelSize;
541 size_t ctOffset = size;
542 size += ctSize;
bsalomon4d516a62016-07-28 13:37:31 -0700543 size_t colorSpaceOffset = 0;
544 size_t colorSpaceSize = 0;
545 if (info.colorSpace()) {
546 colorSpaceOffset = size;
547 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
548 size += colorSpaceSize;
549 }
bsalomon41b952c2016-03-11 06:46:33 -0800550 if (!fillMode) {
551 return size;
552 }
cblume921bc672016-09-22 05:25:26 -0700553 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer);
554 char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset;
555 void* pixels = pixelsAsCharPtr;
cblume33e0cb52016-08-30 12:09:23 -0700556 void* ct = nullptr;
bsalomon41b952c2016-03-11 06:46:33 -0800557 if (ctSize) {
cblume921bc672016-09-22 05:25:26 -0700558 ct = bufferAsCharPtr + ctOffset;
bsalomon41b952c2016-03-11 06:46:33 -0800559 }
560
cblume921bc672016-09-22 05:25:26 -0700561 memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))),
562 pixmap.addr(), pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800563 if (ctSize) {
564 memcpy(ct, pixmap.ctable()->readColors(), ctSize);
565 }
566
Brian Osman6c15cc72016-10-17 09:51:37 -0400567 // If the context has sRGB support, and we're intending to render to a surface with an attached
568 // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB)
569 // aware mip-mapping.
Brian Osman7b8400d2016-11-08 17:08:54 -0500570 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
Brian Osman6c15cc72016-10-17 09:51:37 -0400571 if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) &&
572 info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) {
Brian Osman7b8400d2016-11-08 17:08:54 -0500573 colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware;
Brian Osman6c15cc72016-10-17 09:51:37 -0400574 }
575
bsalomon41b952c2016-03-11 06:46:33 -0800576 SkASSERT(info == pixmap.info());
577 size_t rowBytes = pixmap.rowBytes();
cblume33e0cb52016-08-30 12:09:23 -0700578 static_assert(std::is_standard_layout<DeferredTextureImage>::value,
579 "offsetof, which we use below, requires the type have standard layout");
cblume921bc672016-09-22 05:25:26 -0700580 auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr};
Brian Osman7b8400d2016-11-08 17:08:54 -0500581 FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode);
cblume33e0cb52016-08-30 12:09:23 -0700582 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID);
583 int width = info.width();
584 FILL_MEMBER(dtiBufferFiller, fWidth, &width);
585 int height = info.height();
586 FILL_MEMBER(dtiBufferFiller, fHeight, &height);
587 SkColorType colorType = info.colorType();
588 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType);
589 SkAlphaType alphaType = info.alphaType();
590 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType);
591 FILL_MEMBER(dtiBufferFiller, fColorTableCnt, &ctCount);
592 FILL_MEMBER(dtiBufferFiller, fColorTableData, &ct);
593 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount);
cblume921bc672016-09-22 05:25:26 -0700594 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData),
cblume33e0cb52016-08-30 12:09:23 -0700595 &pixels, sizeof(pixels));
cblume921bc672016-09-22 05:25:26 -0700596 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes),
cblume33e0cb52016-08-30 12:09:23 -0700597 &rowBytes, sizeof(rowBytes));
bsalomon4d516a62016-07-28 13:37:31 -0700598 if (colorSpaceSize) {
cblume921bc672016-09-22 05:25:26 -0700599 void* colorSpace = bufferAsCharPtr + colorSpaceOffset;
cblume33e0cb52016-08-30 12:09:23 -0700600 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace);
601 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize);
cblume921bc672016-09-22 05:25:26 -0700602 info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
bsalomon4d516a62016-07-28 13:37:31 -0700603 } else {
cblume921bc672016-09-22 05:25:26 -0700604 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace),
cblume33e0cb52016-08-30 12:09:23 -0700605 0, sizeof(DeferredTextureImage::fColorSpace));
cblume921bc672016-09-22 05:25:26 -0700606 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize),
cblume33e0cb52016-08-30 12:09:23 -0700607 0, sizeof(DeferredTextureImage::fColorSpaceSize));
608 }
609
610 // Fill in the mipmap levels if they exist
cblume921bc672016-09-22 05:25:26 -0700611 char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700612
613 if (useMipMaps) {
cblume94ddf542016-09-16 10:07:32 -0700614 static_assert(std::is_standard_layout<MipMapLevelData>::value,
615 "offsetof, which we use below, requires the type have a standard layout");
cblume33e0cb52016-08-30 12:09:23 -0700616
Brian Osman7b8400d2016-11-08 17:08:54 -0500617 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
cblume33e0cb52016-08-30 12:09:23 -0700618 // SkMipMap holds only the mipmap levels it generates.
619 // A programmer can use the data they provided to SkMipMap::Build as level 0.
620 // So the SkMipMap provides levels 1-x but it stores them in its own
621 // range 0-(x-1).
622 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1;
623 generatedMipLevelIndex++) {
cblume33e0cb52016-08-30 12:09:23 -0700624 SkMipMap::Level mipLevel;
625 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
626
627 // Make sure the mipmap data is after the start of the buffer
cblume921bc672016-09-22 05:25:26 -0700628 SkASSERT(mipLevelPtr > bufferAsCharPtr);
cblume33e0cb52016-08-30 12:09:23 -0700629 // Make sure the mipmap data starts before the end of the buffer
cblume921bc672016-09-22 05:25:26 -0700630 SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700631 // Make sure the mipmap data ends before the end of the buffer
cblume12f75272016-09-19 06:18:03 -0700632 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <=
cblume921bc672016-09-22 05:25:26 -0700633 bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700634
635 // getSafeSize includes rowbyte padding except for the last row,
636 // right?
637
cblume921bc672016-09-22 05:25:26 -0700638 memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700639
cblume921bc672016-09-22 05:25:26 -0700640 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
641 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
642 offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*));
cblume33e0cb52016-08-30 12:09:23 -0700643 size_t rowBytes = mipLevel.fPixmap.rowBytes();
cblume921bc672016-09-22 05:25:26 -0700644 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
645 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
646 offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes));
cblume33e0cb52016-08-30 12:09:23 -0700647
648 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize());
649 }
bsalomon4d516a62016-07-28 13:37:31 -0700650 }
bsalomon41b952c2016-03-11 06:46:33 -0800651 return size;
652}
653
654sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
655 SkBudgeted budgeted) {
656 if (!data) {
657 return nullptr;
658 }
659 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
660
661 if (!context || context->uniqueID() != dti->fContextUniqueID) {
662 return nullptr;
663 }
Hal Canary67b39de2016-11-07 11:47:44 -0500664 sk_sp<SkColorTable> colorTable;
bsalomon4d516a62016-07-28 13:37:31 -0700665 if (dti->fColorTableCnt) {
666 SkASSERT(dti->fColorTableData);
667 colorTable.reset(new SkColorTable(dti->fColorTableData, dti->fColorTableCnt));
bsalomon41b952c2016-03-11 06:46:33 -0800668 }
cblume33e0cb52016-08-30 12:09:23 -0700669 int mipLevelCount = dti->fMipMapLevelCount;
670 SkASSERT(mipLevelCount >= 1);
bsalomon4d516a62016-07-28 13:37:31 -0700671 sk_sp<SkColorSpace> colorSpace;
672 if (dti->fColorSpaceSize) {
673 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize);
674 }
675 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
676 dti->fColorType, dti->fAlphaType, colorSpace);
cblume33e0cb52016-08-30 12:09:23 -0700677 if (mipLevelCount == 1) {
678 SkPixmap pixmap;
679 pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData,
680 dti->fMipMapLevelData[0].fRowBytes, colorTable.get());
681 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted);
682 } else {
Ben Wagner7ecc5962016-11-02 17:07:33 -0400683 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
cblume33e0cb52016-08-30 12:09:23 -0700684 for (int i = 0; i < mipLevelCount; i++) {
685 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
686 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
687 }
688
689 return SkImage::MakeTextureFromMipMap(context, info, texels.get(),
690 mipLevelCount, SkBudgeted::kYes,
Brian Osman7b8400d2016-11-08 17:08:54 -0500691 dti->fColorMode);
cblume33e0cb52016-08-30 12:09:23 -0700692 }
bsalomon41b952c2016-03-11 06:46:33 -0800693}
694
695///////////////////////////////////////////////////////////////////////////////////////////////////
696
cblume186d2d42016-06-03 11:17:42 -0700697sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
698 const GrMipLevel* texels, int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700699 SkBudgeted budgeted,
Brian Osman7b8400d2016-11-08 17:08:54 -0500700 SkDestinationSurfaceColorMode colorMode) {
cblume186d2d42016-06-03 11:17:42 -0700701 if (!ctx) {
702 return nullptr;
703 }
bungeman6bd52842016-10-27 09:30:08 -0700704 sk_sp<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount));
cblume186d2d42016-06-03 11:17:42 -0700705 if (!texture) {
706 return nullptr;
707 }
Brian Osman7b8400d2016-11-08 17:08:54 -0500708 texture->texturePriv().setMipColorMode(colorMode);
cblume186d2d42016-06-03 11:17:42 -0700709 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700710 info.alphaType(), std::move(texture),
711 sk_ref_sp(info.colorSpace()), budgeted);
cblume186d2d42016-06-03 11:17:42 -0700712}