blob: 66ee5149ff8c79f3596d06664c42b7002f3b7abe [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,
Robert Phillips67c18d62017-01-20 12:44:06 -0500100 sk_sp<SkColorSpace>* texColorSpace,
101 SkScalar scaleAdjust[2]) const {
Brian Osman7992da32016-11-18 11:28:24 -0500102 if (texColorSpace) {
103 *texColorSpace = this->fColorSpace;
104 }
105 GrTextureAdjuster adjuster(this->peekTexture(), this->alphaType(), this->bounds(),
106 this->uniqueID(), this->fColorSpace.get());
Robert Phillips67c18d62017-01-20 12:44:06 -0500107 return adjuster.refTextureSafeForParams(params, nullptr, scaleAdjust);
reed85d91782015-09-10 14:33:38 -0700108}
109
reed8b26b992015-05-07 15:36:17 -0700110static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
111 switch (info.colorType()) {
112 case kRGBA_8888_SkColorType:
113 case kBGRA_8888_SkColorType:
114 break;
115 default:
116 return; // nothing to do
117 }
118
119 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
120 // and in either case, the alpha-byte is always in the same place, so we can safely call
121 // SkPreMultiplyColor()
122 //
123 SkColor* row = (SkColor*)pixels;
124 for (int y = 0; y < info.height(); ++y) {
125 for (int x = 0; x < info.width(); ++x) {
126 row[x] = SkPreMultiplyColor(row[x]);
127 }
128 }
129}
130
131bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
reed09553032015-11-23 12:32:16 -0800132 int srcX, int srcY, CachingHint) const {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500133 if (!SkImageInfoValidConversion(info, this->onImageInfo())) {
134 return false;
135 }
136
brianosmanb109b8c2016-06-16 13:03:24 -0700137 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fTexture->getContext()->caps());
reed8b26b992015-05-07 15:36:17 -0700138 uint32_t flags = 0;
139 if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) {
140 // let the GPU perform this transformation for us
141 flags = GrContext::kUnpremul_PixelOpsFlag;
142 }
Brian Osmanb62ea222016-12-22 11:12:16 -0500143 if (!fTexture->readPixels(fColorSpace.get(), srcX, srcY, info.width(), info.height(), config,
144 info.colorSpace(), pixels, rowBytes, flags)) {
reed8b26b992015-05-07 15:36:17 -0700145 return false;
146 }
147 // do we have to manually fix-up the alpha channel?
148 // src dst
149 // unpremul premul fix manually
150 // premul unpremul done by kUnpremul_PixelOpsFlag
151 // all other combos need to change.
152 //
153 // Should this be handled by Ganesh? todo:?
154 //
155 if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
156 apply_premul(info, pixels, rowBytes);
157 }
158 return true;
159}
160
reed7fb4f8b2016-03-11 04:33:52 -0800161sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700162 GrContext* ctx = fTexture->getContext();
163 GrSurfaceDesc desc = fTexture->desc();
164 desc.fWidth = subset.width();
165 desc.fHeight = subset.height();
166
Robert Phillipse2f7d182016-12-15 09:23:05 -0500167 sk_sp<GrSurfaceContext> sContext(ctx->contextPriv().makeDeferredSurfaceContext(
168 desc,
169 SkBackingFit::kExact,
170 fBudgeted));
171 if (!sContext) {
172 return nullptr;
173 }
174
175 // TODO: make gpu images be proxy-backed so we don't need to do this
176 sk_sp<GrSurfaceProxy> tmpSrc(GrSurfaceProxy::MakeWrapped(fTexture));
177 if (!tmpSrc) {
178 return nullptr;
179 }
180
181 if (!sContext->copy(tmpSrc.get(), subset, SkIPoint::Make(0, 0))) {
182 return nullptr;
183 }
184
185 // TODO: make gpu images be proxy-backed so we don't need to do this
186 GrSurface* subTx = sContext->asDeferredSurface()->instantiate(ctx->textureProvider());
reed7b6945b2015-09-24 00:50:58 -0700187 if (!subTx) {
188 return nullptr;
189 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500190
reed7fb4f8b2016-03-11 04:33:52 -0800191 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500192 fAlphaType, sk_ref_sp(subTx->asTexture()),
193 fColorSpace, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700194}
195
reed8b26b992015-05-07 15:36:17 -0700196///////////////////////////////////////////////////////////////////////////////////////////////////
197
reed7fb4f8b2016-03-11 04:33:52 -0800198static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700199 SkAlphaType at, sk_sp<SkColorSpace> colorSpace,
200 GrWrapOwnership ownership,
reed7fb4f8b2016-03-11 04:33:52 -0800201 SkImage::TextureReleaseProc releaseProc,
202 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700203 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700204 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700205 }
bungeman6bd52842016-10-27 09:30:08 -0700206 sk_sp<GrTexture> tex = ctx->textureProvider()->wrapBackendTexture(desc, ownership);
reed8b26b992015-05-07 15:36:17 -0700207 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700208 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700209 }
reedde499882015-06-18 13:41:40 -0700210 if (releaseProc) {
211 tex->setRelease(releaseProc, releaseCtx);
212 }
213
bsalomon5ec26ae2016-02-25 08:33:02 -0800214 const SkBudgeted budgeted = SkBudgeted::kNo;
reed7fb4f8b2016-03-11 04:33:52 -0800215 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700216 at, std::move(tex), std::move(colorSpace), budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700217}
218
reed7fb4f8b2016-03-11 04:33:52 -0800219sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700220 SkAlphaType at, sk_sp<SkColorSpace> cs,
221 TextureReleaseProc releaseP, ReleaseContext releaseC) {
222 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kBorrow_GrWrapOwnership,
223 releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700224}
225
reed7fb4f8b2016-03-11 04:33:52 -0800226sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700227 SkAlphaType at, sk_sp<SkColorSpace> cs) {
228 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kAdopt_GrWrapOwnership,
229 nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700230}
231
jbaumanb445a572016-06-09 13:24:48 -0700232static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace,
233 bool nv12,
234 const GrBackendObject yuvTextureHandles[],
235 const SkISize yuvSizes[],
brianosmandddbe382016-07-20 13:55:39 -0700236 GrSurfaceOrigin origin,
237 sk_sp<SkColorSpace> imageColorSpace) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800238 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700239
jbaumanb445a572016-06-09 13:24:48 -0700240 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 ||
241 yuvSizes[1].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700242 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700243 }
jbaumanb445a572016-06-09 13:24:48 -0700244 if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) {
245 return nullptr;
246 }
247
248 const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig;
249
bsalomon993a4212015-05-29 11:37:25 -0700250 GrBackendTextureDesc yDesc;
251 yDesc.fConfig = kConfig;
252 yDesc.fOrigin = origin;
253 yDesc.fSampleCnt = 0;
254 yDesc.fTextureHandle = yuvTextureHandles[0];
255 yDesc.fWidth = yuvSizes[0].fWidth;
256 yDesc.fHeight = yuvSizes[0].fHeight;
257
258 GrBackendTextureDesc uDesc;
259 uDesc.fConfig = kConfig;
260 uDesc.fOrigin = origin;
261 uDesc.fSampleCnt = 0;
262 uDesc.fTextureHandle = yuvTextureHandles[1];
263 uDesc.fWidth = yuvSizes[1].fWidth;
264 uDesc.fHeight = yuvSizes[1].fHeight;
265
jbaumanb445a572016-06-09 13:24:48 -0700266 sk_sp<GrTexture> yTex(
267 ctx->textureProvider()->wrapBackendTexture(yDesc, kBorrow_GrWrapOwnership));
268 sk_sp<GrTexture> uTex(
269 ctx->textureProvider()->wrapBackendTexture(uDesc, kBorrow_GrWrapOwnership));
270 sk_sp<GrTexture> vTex;
271 if (nv12) {
272 vTex = uTex;
273 } else {
274 GrBackendTextureDesc vDesc;
275 vDesc.fConfig = kConfig;
276 vDesc.fOrigin = origin;
277 vDesc.fSampleCnt = 0;
278 vDesc.fTextureHandle = yuvTextureHandles[2];
279 vDesc.fWidth = yuvSizes[2].fWidth;
280 vDesc.fHeight = yuvSizes[2].fHeight;
bsalomon993a4212015-05-29 11:37:25 -0700281
jbaumanb445a572016-06-09 13:24:48 -0700282 vTex = sk_sp<GrTexture>(
283 ctx->textureProvider()->wrapBackendTexture(vDesc, kBorrow_GrWrapOwnership));
284 }
bsalomon993a4212015-05-29 11:37:25 -0700285 if (!yTex || !uTex || !vTex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700286 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700287 }
288
robertphillipsd4c741e2016-04-28 09:55:15 -0700289 const int width = yuvSizes[0].fWidth;
290 const int height = yuvSizes[0].fHeight;
robertphillipsaa19a5f2016-04-28 06:21:55 -0700291
robertphillipsd4c741e2016-04-28 09:55:15 -0700292 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
Brian Osman11052242016-10-27 14:47:55 -0400293 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeRenderTargetContext(
294 SkBackingFit::kExact,
295 width, height,
296 kRGBA_8888_GrPixelConfig,
297 std::move(imageColorSpace),
298 0,
299 origin));
300 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700301 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700302 }
303
304 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400305 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
jbaumanb445a572016-06-09 13:24:48 -0700306 paint.addColorFragmentProcessor(
307 GrYUVEffect::MakeYUVToRGB(yTex.get(), uTex.get(), vTex.get(), yuvSizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700308
robertphillipsd4c741e2016-04-28 09:55:15 -0700309 const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillipsc9a37062015-09-01 08:34:28 -0700310
Brian Salomon82f44312017-01-11 13:42:54 -0500311 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Robert Phillipse60ad622016-11-17 10:22:48 -0500312
313 if (!renderTargetContext->accessRenderTarget()) {
314 return nullptr;
315 }
Brian Osman11052242016-10-27 14:47:55 -0400316 ctx->flushSurfaceWrites(renderTargetContext->accessRenderTarget());
robertphillipsd4c741e2016-04-28 09:55:15 -0700317 return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID,
Brian Osman11052242016-10-27 14:47:55 -0400318 kOpaque_SkAlphaType, renderTargetContext->asTexture(),
Robert Phillips75a475c2017-01-13 09:18:59 -0500319 renderTargetContext->refColorSpace(), budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700320}
reed56179002015-07-07 06:11:19 -0700321
jbaumanb445a572016-06-09 13:24:48 -0700322sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
323 const GrBackendObject yuvTextureHandles[3],
brianosmandddbe382016-07-20 13:55:39 -0700324 const SkISize yuvSizes[3], GrSurfaceOrigin origin,
325 sk_sp<SkColorSpace> imageColorSpace) {
326 return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin,
327 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700328}
329
330sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
331 const GrBackendObject yuvTextureHandles[2],
332 const SkISize yuvSizes[2],
brianosmandddbe382016-07-20 13:55:39 -0700333 GrSurfaceOrigin origin,
334 sk_sp<SkColorSpace> imageColorSpace) {
335 return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin,
336 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700337}
338
bsalomon634b4302016-07-12 18:11:17 -0700339sk_sp<SkImage> SkImage::makeNonTextureImage() const {
brianosman396fcdb2016-07-22 06:26:11 -0700340 if (!this->isTextureBacked()) {
bsalomon634b4302016-07-12 18:11:17 -0700341 return sk_ref_sp(const_cast<SkImage*>(this));
342 }
brianosman396fcdb2016-07-22 06:26:11 -0700343 SkImageInfo info = as_IB(this)->onImageInfo();
bsalomon634b4302016-07-12 18:11:17 -0700344 size_t rowBytes = info.minRowBytes();
345 size_t size = info.getSafeSize(rowBytes);
346 auto data = SkData::MakeUninitialized(size);
347 if (!data) {
348 return nullptr;
349 }
350 SkPixmap pm(info, data->writable_data(), rowBytes);
351 if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
352 return nullptr;
353 }
354 return MakeRasterData(info, data, rowBytes);
355}
356
reed7fb4f8b2016-03-11 04:33:52 -0800357sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap,
358 SkBudgeted budgeted) {
bsalomon0d996862016-03-09 18:44:43 -0800359 if (!ctx) {
360 return nullptr;
361 }
bungeman6bd52842016-10-27 09:30:08 -0700362 sk_sp<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted));
bsalomon0d996862016-03-09 18:44:43 -0800363 if (!texture) {
364 return nullptr;
365 }
reed7fb4f8b2016-03-11 04:33:52 -0800366 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700367 pixmap.alphaType(), std::move(texture),
brianosmandddbe382016-07-20 13:55:39 -0700368 sk_ref_sp(pixmap.info().colorSpace()), budgeted);
bsalomon0d996862016-03-09 18:44:43 -0800369}
370
reed56179002015-07-07 06:11:19 -0700371///////////////////////////////////////////////////////////////////////////////////////////////////
372
bsalomon4d516a62016-07-28 13:37:31 -0700373namespace {
374struct MipMapLevelData {
375 void* fPixelData;
376 size_t fRowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800377};
378
bsalomon4d516a62016-07-28 13:37:31 -0700379struct DeferredTextureImage {
Brian Osman7b8400d2016-11-08 17:08:54 -0500380 uint32_t fContextUniqueID;
381 // Right now, the destination color mode is only considered when generating mipmaps
382 SkDestinationSurfaceColorMode fColorMode;
bsalomon4d516a62016-07-28 13:37:31 -0700383 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace.
Brian Osman7b8400d2016-11-08 17:08:54 -0500384 int fWidth;
385 int fHeight;
386 SkColorType fColorType;
387 SkAlphaType fAlphaType;
388 void* fColorSpace;
389 size_t fColorSpaceSize;
Brian Osman7b8400d2016-11-08 17:08:54 -0500390 int fMipMapLevelCount;
bsalomon4d516a62016-07-28 13:37:31 -0700391 // The fMipMapLevelData array may contain more than 1 element.
392 // It contains fMipMapLevelCount elements.
393 // That means this struct's size is not known at compile-time.
Brian Osman7b8400d2016-11-08 17:08:54 -0500394 MipMapLevelData fMipMapLevelData[1];
bsalomon4d516a62016-07-28 13:37:31 -0700395};
396} // anonymous namespace
397
cblume33e0cb52016-08-30 12:09:23 -0700398static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
Eric Karla422d702016-11-30 11:09:29 -0800399 // There is a bug in the mipmap pre-generation logic in use in getDeferredTextureImageData.
400 // This can cause runaway memory leaks, so we are disabling this path until we can
401 // investigate further. crbug.com/669775
402 return false;
cblume33e0cb52016-08-30 12:09:23 -0700403}
404
405namespace {
406
407class DTIBufferFiller
408{
409public:
cblume921bc672016-09-22 05:25:26 -0700410 explicit DTIBufferFiller(char* bufferAsCharPtr)
411 : bufferAsCharPtr_(bufferAsCharPtr) {}
cblume33e0cb52016-08-30 12:09:23 -0700412
413 void fillMember(const void* source, size_t memberOffset, size_t size) {
cblume921bc672016-09-22 05:25:26 -0700414 memcpy(bufferAsCharPtr_ + memberOffset, source, size);
cblume33e0cb52016-08-30 12:09:23 -0700415 }
416
417private:
418
cblume921bc672016-09-22 05:25:26 -0700419 char* bufferAsCharPtr_;
cblume33e0cb52016-08-30 12:09:23 -0700420};
421}
422
423#define FILL_MEMBER(bufferFiller, member, source) \
424 bufferFiller.fillMember(source, \
425 offsetof(DeferredTextureImage, member), \
426 sizeof(DeferredTextureImage::member));
427
bsalomon41b952c2016-03-11 06:46:33 -0800428size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
ericrkb4da01d2016-06-13 11:18:14 -0700429 const DeferredTextureImageUsageParams params[],
cblume33e0cb52016-08-30 12:09:23 -0700430 int paramCnt, void* buffer,
Brian Osman6c15cc72016-10-17 09:51:37 -0400431 SkColorSpace* dstColorSpace) const {
ericrkb4da01d2016-06-13 11:18:14 -0700432 // Extract relevant min/max values from the params array.
433 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
434 SkFilterQuality highestFilterQuality = params[0].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700435 bool useMipMaps = should_use_mip_maps(params[0]);
ericrkb4da01d2016-06-13 11:18:14 -0700436 for (int i = 1; i < paramCnt; ++i) {
437 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
438 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
439 if (highestFilterQuality < params[i].fQuality)
440 highestFilterQuality = params[i].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700441 useMipMaps |= should_use_mip_maps(params[i]);
ericrkb4da01d2016-06-13 11:18:14 -0700442 }
443
bsalomon41b952c2016-03-11 06:46:33 -0800444 const bool fillMode = SkToBool(buffer);
445 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
446 return 0;
447 }
448
ericrkb4da01d2016-06-13 11:18:14 -0700449 // Calculate scaling parameters.
450 bool isScaled = lowestPreScaleMipLevel != 0;
451
452 SkISize scaledSize;
453 if (isScaled) {
454 // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the
455 // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level.
456 scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(),
457 lowestPreScaleMipLevel - 1);
458 } else {
459 scaledSize = SkISize::Make(this->width(), this->height());
460 }
461
462 // We never want to scale at higher than SW medium quality, as SW medium matches GPU high.
463 SkFilterQuality scaleFilterQuality = highestFilterQuality;
464 if (scaleFilterQuality > kMedium_SkFilterQuality) {
465 scaleFilterQuality = kMedium_SkFilterQuality;
466 }
467
ericrkc429baf2016-03-24 15:35:45 -0700468 const int maxTextureSize = proxy.fCaps->maxTextureSize();
ericrkb4da01d2016-06-13 11:18:14 -0700469 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkc429baf2016-03-24 15:35:45 -0700470 return 0;
471 }
472
bsalomon41b952c2016-03-11 06:46:33 -0800473 SkAutoPixmapStorage pixmap;
474 SkImageInfo info;
475 size_t pixelSize = 0;
Brian Osmanaaedae72017-01-20 13:21:56 -0500476 if (!isScaled && this->peekPixels(&pixmap) && !pixmap.ctable()) {
bsalomon41b952c2016-03-11 06:46:33 -0800477 info = pixmap.info();
478 pixelSize = SkAlign8(pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800479 } else {
480 // Here we're just using presence of data to know whether there is a codec behind the image.
481 // In the future we will access the cacherator and get the exact data that we want to (e.g.
482 // yuv planes) upload.
bungemanffae30d2016-08-03 13:32:32 -0700483 sk_sp<SkData> data(this->refEncoded());
ericrkb4da01d2016-06-13 11:18:14 -0700484 if (!data && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800485 return 0;
486 }
Brian Osman7992da32016-11-18 11:28:24 -0500487 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
488 // Generator backed image. Tweak info to trigger correct kind of decode.
Brian Osman7992da32016-11-18 11:28:24 -0500489 SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat(
Brian Osman61624f02016-12-09 14:51:59 -0500490 dstColorSpace, proxy.fCaps.get());
Brian Osman7992da32016-11-18 11:28:24 -0500491 info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(),
492 scaledSize.height());
Brian Osman7992da32016-11-18 11:28:24 -0500493 } else {
494 info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height());
495 }
Brian Osmanaaedae72017-01-20 13:21:56 -0500496 if (kIndex_8_SkColorType == info.colorType()) {
497 // Force Index8 to be N32 instead. Index8 is unsupported in Ganesh.
498 info = info.makeColorType(kN32_SkColorType);
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;
bsalomon4d516a62016-07-28 13:37:31 -0700546 size_t colorSpaceOffset = 0;
547 size_t colorSpaceSize = 0;
548 if (info.colorSpace()) {
549 colorSpaceOffset = size;
550 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
551 size += colorSpaceSize;
552 }
bsalomon41b952c2016-03-11 06:46:33 -0800553 if (!fillMode) {
554 return size;
555 }
cblume921bc672016-09-22 05:25:26 -0700556 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer);
557 char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset;
558 void* pixels = pixelsAsCharPtr;
bsalomon41b952c2016-03-11 06:46:33 -0800559
cblume921bc672016-09-22 05:25:26 -0700560 memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))),
561 pixmap.addr(), pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800562
Brian Osman6c15cc72016-10-17 09:51:37 -0400563 // If the context has sRGB support, and we're intending to render to a surface with an attached
564 // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB)
565 // aware mip-mapping.
Brian Osman7b8400d2016-11-08 17:08:54 -0500566 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
Brian Osman6c15cc72016-10-17 09:51:37 -0400567 if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) &&
568 info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) {
Brian Osman7b8400d2016-11-08 17:08:54 -0500569 colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware;
Brian Osman6c15cc72016-10-17 09:51:37 -0400570 }
571
bsalomon41b952c2016-03-11 06:46:33 -0800572 SkASSERT(info == pixmap.info());
573 size_t rowBytes = pixmap.rowBytes();
cblume33e0cb52016-08-30 12:09:23 -0700574 static_assert(std::is_standard_layout<DeferredTextureImage>::value,
575 "offsetof, which we use below, requires the type have standard layout");
cblume921bc672016-09-22 05:25:26 -0700576 auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr};
Brian Osman7b8400d2016-11-08 17:08:54 -0500577 FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode);
cblume33e0cb52016-08-30 12:09:23 -0700578 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID);
579 int width = info.width();
580 FILL_MEMBER(dtiBufferFiller, fWidth, &width);
581 int height = info.height();
582 FILL_MEMBER(dtiBufferFiller, fHeight, &height);
583 SkColorType colorType = info.colorType();
584 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType);
585 SkAlphaType alphaType = info.alphaType();
586 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType);
cblume33e0cb52016-08-30 12:09:23 -0700587 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount);
cblume921bc672016-09-22 05:25:26 -0700588 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData),
cblume33e0cb52016-08-30 12:09:23 -0700589 &pixels, sizeof(pixels));
cblume921bc672016-09-22 05:25:26 -0700590 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes),
cblume33e0cb52016-08-30 12:09:23 -0700591 &rowBytes, sizeof(rowBytes));
bsalomon4d516a62016-07-28 13:37:31 -0700592 if (colorSpaceSize) {
cblume921bc672016-09-22 05:25:26 -0700593 void* colorSpace = bufferAsCharPtr + colorSpaceOffset;
cblume33e0cb52016-08-30 12:09:23 -0700594 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace);
595 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize);
cblume921bc672016-09-22 05:25:26 -0700596 info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
bsalomon4d516a62016-07-28 13:37:31 -0700597 } else {
cblume921bc672016-09-22 05:25:26 -0700598 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace),
cblume33e0cb52016-08-30 12:09:23 -0700599 0, sizeof(DeferredTextureImage::fColorSpace));
cblume921bc672016-09-22 05:25:26 -0700600 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize),
cblume33e0cb52016-08-30 12:09:23 -0700601 0, sizeof(DeferredTextureImage::fColorSpaceSize));
602 }
603
604 // Fill in the mipmap levels if they exist
cblume921bc672016-09-22 05:25:26 -0700605 char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700606
607 if (useMipMaps) {
cblume94ddf542016-09-16 10:07:32 -0700608 static_assert(std::is_standard_layout<MipMapLevelData>::value,
609 "offsetof, which we use below, requires the type have a standard layout");
cblume33e0cb52016-08-30 12:09:23 -0700610
Brian Osman7b8400d2016-11-08 17:08:54 -0500611 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
cblume33e0cb52016-08-30 12:09:23 -0700612 // SkMipMap holds only the mipmap levels it generates.
613 // A programmer can use the data they provided to SkMipMap::Build as level 0.
614 // So the SkMipMap provides levels 1-x but it stores them in its own
615 // range 0-(x-1).
616 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1;
617 generatedMipLevelIndex++) {
cblume33e0cb52016-08-30 12:09:23 -0700618 SkMipMap::Level mipLevel;
619 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
620
621 // Make sure the mipmap data is after the start of the buffer
cblume921bc672016-09-22 05:25:26 -0700622 SkASSERT(mipLevelPtr > bufferAsCharPtr);
cblume33e0cb52016-08-30 12:09:23 -0700623 // Make sure the mipmap data starts before the end of the buffer
cblume921bc672016-09-22 05:25:26 -0700624 SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700625 // Make sure the mipmap data ends before the end of the buffer
cblume12f75272016-09-19 06:18:03 -0700626 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <=
cblume921bc672016-09-22 05:25:26 -0700627 bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700628
629 // getSafeSize includes rowbyte padding except for the last row,
630 // right?
631
cblume921bc672016-09-22 05:25:26 -0700632 memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700633
cblume921bc672016-09-22 05:25:26 -0700634 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
635 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
636 offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*));
cblume33e0cb52016-08-30 12:09:23 -0700637 size_t rowBytes = mipLevel.fPixmap.rowBytes();
cblume921bc672016-09-22 05:25:26 -0700638 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
639 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
640 offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes));
cblume33e0cb52016-08-30 12:09:23 -0700641
642 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize());
643 }
bsalomon4d516a62016-07-28 13:37:31 -0700644 }
bsalomon41b952c2016-03-11 06:46:33 -0800645 return size;
646}
647
648sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
649 SkBudgeted budgeted) {
650 if (!data) {
651 return nullptr;
652 }
653 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
654
655 if (!context || context->uniqueID() != dti->fContextUniqueID) {
656 return nullptr;
657 }
cblume33e0cb52016-08-30 12:09:23 -0700658 int mipLevelCount = dti->fMipMapLevelCount;
659 SkASSERT(mipLevelCount >= 1);
bsalomon4d516a62016-07-28 13:37:31 -0700660 sk_sp<SkColorSpace> colorSpace;
661 if (dti->fColorSpaceSize) {
662 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize);
663 }
664 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
665 dti->fColorType, dti->fAlphaType, colorSpace);
cblume33e0cb52016-08-30 12:09:23 -0700666 if (mipLevelCount == 1) {
667 SkPixmap pixmap;
Brian Osmanaaedae72017-01-20 13:21:56 -0500668 pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData, dti->fMipMapLevelData[0].fRowBytes);
cblume33e0cb52016-08-30 12:09:23 -0700669 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted);
670 } else {
Ben Wagner7ecc5962016-11-02 17:07:33 -0400671 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
cblume33e0cb52016-08-30 12:09:23 -0700672 for (int i = 0; i < mipLevelCount; i++) {
673 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
674 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
675 }
676
677 return SkImage::MakeTextureFromMipMap(context, info, texels.get(),
678 mipLevelCount, SkBudgeted::kYes,
Brian Osman7b8400d2016-11-08 17:08:54 -0500679 dti->fColorMode);
cblume33e0cb52016-08-30 12:09:23 -0700680 }
bsalomon41b952c2016-03-11 06:46:33 -0800681}
682
683///////////////////////////////////////////////////////////////////////////////////////////////////
684
cblume186d2d42016-06-03 11:17:42 -0700685sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
686 const GrMipLevel* texels, int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700687 SkBudgeted budgeted,
Brian Osman7b8400d2016-11-08 17:08:54 -0500688 SkDestinationSurfaceColorMode colorMode) {
cblume186d2d42016-06-03 11:17:42 -0700689 if (!ctx) {
690 return nullptr;
691 }
bungeman6bd52842016-10-27 09:30:08 -0700692 sk_sp<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount));
cblume186d2d42016-06-03 11:17:42 -0700693 if (!texture) {
694 return nullptr;
695 }
Brian Osman7b8400d2016-11-08 17:08:54 -0500696 texture->texturePriv().setMipColorMode(colorMode);
cblume186d2d42016-06-03 11:17:42 -0700697 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700698 info.alphaType(), std::move(texture),
699 sk_ref_sp(info.colorSpace()), budgeted);
cblume186d2d42016-06-03 11:17:42 -0700700}