blob: 7247ae36fa013152e5d3cab3af203bde061f2d48 [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
reed56179002015-07-07 06:11:19 -0700357///////////////////////////////////////////////////////////////////////////////////////////////////
358
bsalomon4d516a62016-07-28 13:37:31 -0700359namespace {
360struct MipMapLevelData {
361 void* fPixelData;
362 size_t fRowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800363};
364
bsalomon4d516a62016-07-28 13:37:31 -0700365struct DeferredTextureImage {
Brian Osman7b8400d2016-11-08 17:08:54 -0500366 uint32_t fContextUniqueID;
367 // Right now, the destination color mode is only considered when generating mipmaps
368 SkDestinationSurfaceColorMode fColorMode;
bsalomon4d516a62016-07-28 13:37:31 -0700369 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace.
Brian Osman7b8400d2016-11-08 17:08:54 -0500370 int fWidth;
371 int fHeight;
372 SkColorType fColorType;
373 SkAlphaType fAlphaType;
374 void* fColorSpace;
375 size_t fColorSpaceSize;
Brian Osman7b8400d2016-11-08 17:08:54 -0500376 int fMipMapLevelCount;
bsalomon4d516a62016-07-28 13:37:31 -0700377 // The fMipMapLevelData array may contain more than 1 element.
378 // It contains fMipMapLevelCount elements.
379 // That means this struct's size is not known at compile-time.
Brian Osman7b8400d2016-11-08 17:08:54 -0500380 MipMapLevelData fMipMapLevelData[1];
bsalomon4d516a62016-07-28 13:37:31 -0700381};
382} // anonymous namespace
383
cblume33e0cb52016-08-30 12:09:23 -0700384static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
Eric Karla422d702016-11-30 11:09:29 -0800385 // There is a bug in the mipmap pre-generation logic in use in getDeferredTextureImageData.
386 // This can cause runaway memory leaks, so we are disabling this path until we can
387 // investigate further. crbug.com/669775
388 return false;
cblume33e0cb52016-08-30 12:09:23 -0700389}
390
391namespace {
392
393class DTIBufferFiller
394{
395public:
cblume921bc672016-09-22 05:25:26 -0700396 explicit DTIBufferFiller(char* bufferAsCharPtr)
397 : bufferAsCharPtr_(bufferAsCharPtr) {}
cblume33e0cb52016-08-30 12:09:23 -0700398
399 void fillMember(const void* source, size_t memberOffset, size_t size) {
cblume921bc672016-09-22 05:25:26 -0700400 memcpy(bufferAsCharPtr_ + memberOffset, source, size);
cblume33e0cb52016-08-30 12:09:23 -0700401 }
402
403private:
404
cblume921bc672016-09-22 05:25:26 -0700405 char* bufferAsCharPtr_;
cblume33e0cb52016-08-30 12:09:23 -0700406};
407}
408
409#define FILL_MEMBER(bufferFiller, member, source) \
410 bufferFiller.fillMember(source, \
411 offsetof(DeferredTextureImage, member), \
412 sizeof(DeferredTextureImage::member));
413
bsalomon41b952c2016-03-11 06:46:33 -0800414size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
ericrkb4da01d2016-06-13 11:18:14 -0700415 const DeferredTextureImageUsageParams params[],
cblume33e0cb52016-08-30 12:09:23 -0700416 int paramCnt, void* buffer,
Brian Osman6c15cc72016-10-17 09:51:37 -0400417 SkColorSpace* dstColorSpace) const {
ericrkb4da01d2016-06-13 11:18:14 -0700418 // Extract relevant min/max values from the params array.
419 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
420 SkFilterQuality highestFilterQuality = params[0].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700421 bool useMipMaps = should_use_mip_maps(params[0]);
ericrkb4da01d2016-06-13 11:18:14 -0700422 for (int i = 1; i < paramCnt; ++i) {
423 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
424 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
425 if (highestFilterQuality < params[i].fQuality)
426 highestFilterQuality = params[i].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700427 useMipMaps |= should_use_mip_maps(params[i]);
ericrkb4da01d2016-06-13 11:18:14 -0700428 }
429
bsalomon41b952c2016-03-11 06:46:33 -0800430 const bool fillMode = SkToBool(buffer);
431 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
432 return 0;
433 }
434
ericrkb4da01d2016-06-13 11:18:14 -0700435 // Calculate scaling parameters.
436 bool isScaled = lowestPreScaleMipLevel != 0;
437
438 SkISize scaledSize;
439 if (isScaled) {
440 // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the
441 // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level.
442 scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(),
443 lowestPreScaleMipLevel - 1);
444 } else {
445 scaledSize = SkISize::Make(this->width(), this->height());
446 }
447
448 // We never want to scale at higher than SW medium quality, as SW medium matches GPU high.
449 SkFilterQuality scaleFilterQuality = highestFilterQuality;
450 if (scaleFilterQuality > kMedium_SkFilterQuality) {
451 scaleFilterQuality = kMedium_SkFilterQuality;
452 }
453
ericrkc429baf2016-03-24 15:35:45 -0700454 const int maxTextureSize = proxy.fCaps->maxTextureSize();
ericrkb4da01d2016-06-13 11:18:14 -0700455 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkc429baf2016-03-24 15:35:45 -0700456 return 0;
457 }
458
bsalomon41b952c2016-03-11 06:46:33 -0800459 SkAutoPixmapStorage pixmap;
460 SkImageInfo info;
461 size_t pixelSize = 0;
Brian Osmanaaedae72017-01-20 13:21:56 -0500462 if (!isScaled && this->peekPixels(&pixmap) && !pixmap.ctable()) {
bsalomon41b952c2016-03-11 06:46:33 -0800463 info = pixmap.info();
464 pixelSize = SkAlign8(pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800465 } else {
466 // Here we're just using presence of data to know whether there is a codec behind the image.
467 // In the future we will access the cacherator and get the exact data that we want to (e.g.
468 // yuv planes) upload.
bungemanffae30d2016-08-03 13:32:32 -0700469 sk_sp<SkData> data(this->refEncoded());
ericrkb4da01d2016-06-13 11:18:14 -0700470 if (!data && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800471 return 0;
472 }
Brian Osman7992da32016-11-18 11:28:24 -0500473 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
474 // Generator backed image. Tweak info to trigger correct kind of decode.
Brian Osman7992da32016-11-18 11:28:24 -0500475 SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat(
Brian Osman61624f02016-12-09 14:51:59 -0500476 dstColorSpace, proxy.fCaps.get());
Brian Osman7992da32016-11-18 11:28:24 -0500477 info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(),
478 scaledSize.height());
Brian Osman7992da32016-11-18 11:28:24 -0500479 } else {
480 info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height());
481 }
Brian Osmanaaedae72017-01-20 13:21:56 -0500482 if (kIndex_8_SkColorType == info.colorType()) {
483 // Force Index8 to be N32 instead. Index8 is unsupported in Ganesh.
484 info = info.makeColorType(kN32_SkColorType);
485 }
bsalomon41b952c2016-03-11 06:46:33 -0800486 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
487 if (fillMode) {
488 pixmap.alloc(info);
ericrkb4da01d2016-06-13 11:18:14 -0700489 if (isScaled) {
490 if (!this->scalePixels(pixmap, scaleFilterQuality,
491 SkImage::kDisallow_CachingHint)) {
492 return 0;
493 }
494 } else {
495 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
496 return 0;
497 }
bsalomon41b952c2016-03-11 06:46:33 -0800498 }
499 SkASSERT(!pixmap.ctable());
500 }
501 }
cblume2c052802016-05-31 09:55:08 -0700502 int mipMapLevelCount = 1;
cblume33e0cb52016-08-30 12:09:23 -0700503 if (useMipMaps) {
504 // SkMipMap only deals with the mipmap levels it generates, which does
505 // not include the base level.
506 // That means it generates and holds levels 1-x instead of 0-x.
507 // So the total mipmap level count is 1 more than what
508 // SkMipMap::ComputeLevelCount returns.
509 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1;
510
511 // We already initialized pixelSize to the size of the base level.
512 // SkMipMap will generate the extra mipmap levels. Their sizes need to
513 // be added to the total.
514 // Index 0 here does not refer to the base mipmap level -- it is
515 // SkMipMap's first generated mipmap level (level 1).
516 for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0;
517 currentMipMapLevelIndex--) {
518 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(),
519 currentMipMapLevelIndex);
brianosman32b5e702016-10-13 06:44:23 -0700520 SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight);
cblume33e0cb52016-08-30 12:09:23 -0700521 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr));
522 }
523 }
bsalomon41b952c2016-03-11 06:46:33 -0800524 size_t size = 0;
525 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
526 size += dtiSize;
cblume33e0cb52016-08-30 12:09:23 -0700527 size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData);
528 // We subtract 1 because DeferredTextureImage already includes the base
529 // level in its size
bsalomon41b952c2016-03-11 06:46:33 -0800530 size_t pixelOffset = size;
531 size += pixelSize;
bsalomon4d516a62016-07-28 13:37:31 -0700532 size_t colorSpaceOffset = 0;
533 size_t colorSpaceSize = 0;
534 if (info.colorSpace()) {
535 colorSpaceOffset = size;
536 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
537 size += colorSpaceSize;
538 }
bsalomon41b952c2016-03-11 06:46:33 -0800539 if (!fillMode) {
540 return size;
541 }
cblume921bc672016-09-22 05:25:26 -0700542 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer);
543 char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset;
544 void* pixels = pixelsAsCharPtr;
bsalomon41b952c2016-03-11 06:46:33 -0800545
cblume921bc672016-09-22 05:25:26 -0700546 memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))),
547 pixmap.addr(), pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800548
Brian Osman6c15cc72016-10-17 09:51:37 -0400549 // If the context has sRGB support, and we're intending to render to a surface with an attached
550 // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB)
551 // aware mip-mapping.
Brian Osman7b8400d2016-11-08 17:08:54 -0500552 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
Brian Osman6c15cc72016-10-17 09:51:37 -0400553 if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) &&
554 info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) {
Brian Osman7b8400d2016-11-08 17:08:54 -0500555 colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware;
Brian Osman6c15cc72016-10-17 09:51:37 -0400556 }
557
bsalomon41b952c2016-03-11 06:46:33 -0800558 SkASSERT(info == pixmap.info());
559 size_t rowBytes = pixmap.rowBytes();
cblume33e0cb52016-08-30 12:09:23 -0700560 static_assert(std::is_standard_layout<DeferredTextureImage>::value,
561 "offsetof, which we use below, requires the type have standard layout");
cblume921bc672016-09-22 05:25:26 -0700562 auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr};
Brian Osman7b8400d2016-11-08 17:08:54 -0500563 FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode);
cblume33e0cb52016-08-30 12:09:23 -0700564 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID);
565 int width = info.width();
566 FILL_MEMBER(dtiBufferFiller, fWidth, &width);
567 int height = info.height();
568 FILL_MEMBER(dtiBufferFiller, fHeight, &height);
569 SkColorType colorType = info.colorType();
570 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType);
571 SkAlphaType alphaType = info.alphaType();
572 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType);
cblume33e0cb52016-08-30 12:09:23 -0700573 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount);
cblume921bc672016-09-22 05:25:26 -0700574 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData),
cblume33e0cb52016-08-30 12:09:23 -0700575 &pixels, sizeof(pixels));
cblume921bc672016-09-22 05:25:26 -0700576 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes),
cblume33e0cb52016-08-30 12:09:23 -0700577 &rowBytes, sizeof(rowBytes));
bsalomon4d516a62016-07-28 13:37:31 -0700578 if (colorSpaceSize) {
cblume921bc672016-09-22 05:25:26 -0700579 void* colorSpace = bufferAsCharPtr + colorSpaceOffset;
cblume33e0cb52016-08-30 12:09:23 -0700580 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace);
581 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize);
cblume921bc672016-09-22 05:25:26 -0700582 info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
bsalomon4d516a62016-07-28 13:37:31 -0700583 } else {
cblume921bc672016-09-22 05:25:26 -0700584 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace),
cblume33e0cb52016-08-30 12:09:23 -0700585 0, sizeof(DeferredTextureImage::fColorSpace));
cblume921bc672016-09-22 05:25:26 -0700586 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize),
cblume33e0cb52016-08-30 12:09:23 -0700587 0, sizeof(DeferredTextureImage::fColorSpaceSize));
588 }
589
590 // Fill in the mipmap levels if they exist
cblume921bc672016-09-22 05:25:26 -0700591 char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700592
593 if (useMipMaps) {
cblume94ddf542016-09-16 10:07:32 -0700594 static_assert(std::is_standard_layout<MipMapLevelData>::value,
595 "offsetof, which we use below, requires the type have a standard layout");
cblume33e0cb52016-08-30 12:09:23 -0700596
Brian Osman7b8400d2016-11-08 17:08:54 -0500597 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
cblume33e0cb52016-08-30 12:09:23 -0700598 // SkMipMap holds only the mipmap levels it generates.
599 // A programmer can use the data they provided to SkMipMap::Build as level 0.
600 // So the SkMipMap provides levels 1-x but it stores them in its own
601 // range 0-(x-1).
602 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1;
603 generatedMipLevelIndex++) {
cblume33e0cb52016-08-30 12:09:23 -0700604 SkMipMap::Level mipLevel;
605 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
606
607 // Make sure the mipmap data is after the start of the buffer
cblume921bc672016-09-22 05:25:26 -0700608 SkASSERT(mipLevelPtr > bufferAsCharPtr);
cblume33e0cb52016-08-30 12:09:23 -0700609 // Make sure the mipmap data starts before the end of the buffer
cblume921bc672016-09-22 05:25:26 -0700610 SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700611 // Make sure the mipmap data ends before the end of the buffer
cblume12f75272016-09-19 06:18:03 -0700612 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <=
cblume921bc672016-09-22 05:25:26 -0700613 bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700614
615 // getSafeSize includes rowbyte padding except for the last row,
616 // right?
617
cblume921bc672016-09-22 05:25:26 -0700618 memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700619
cblume921bc672016-09-22 05:25:26 -0700620 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
621 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
622 offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*));
cblume33e0cb52016-08-30 12:09:23 -0700623 size_t rowBytes = mipLevel.fPixmap.rowBytes();
cblume921bc672016-09-22 05:25:26 -0700624 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
625 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
626 offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes));
cblume33e0cb52016-08-30 12:09:23 -0700627
628 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize());
629 }
bsalomon4d516a62016-07-28 13:37:31 -0700630 }
bsalomon41b952c2016-03-11 06:46:33 -0800631 return size;
632}
633
634sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
635 SkBudgeted budgeted) {
636 if (!data) {
637 return nullptr;
638 }
639 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
640
641 if (!context || context->uniqueID() != dti->fContextUniqueID) {
642 return nullptr;
643 }
cblume33e0cb52016-08-30 12:09:23 -0700644 int mipLevelCount = dti->fMipMapLevelCount;
645 SkASSERT(mipLevelCount >= 1);
bsalomon4d516a62016-07-28 13:37:31 -0700646 sk_sp<SkColorSpace> colorSpace;
647 if (dti->fColorSpaceSize) {
648 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize);
649 }
650 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
651 dti->fColorType, dti->fAlphaType, colorSpace);
Brian Osman8ccbbb42017-01-20 14:08:04 -0500652 SkAutoSTArray<16, GrMipLevel> texels(mipLevelCount);
653 for (int i = 0; i < mipLevelCount; i++) {
654 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
655 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
cblume33e0cb52016-08-30 12:09:23 -0700656 }
Brian Osman8ccbbb42017-01-20 14:08:04 -0500657
658 return SkImage::MakeTextureFromMipMap(context, info, texels.get(), mipLevelCount,
659 SkBudgeted::kYes, dti->fColorMode);
bsalomon41b952c2016-03-11 06:46:33 -0800660}
661
662///////////////////////////////////////////////////////////////////////////////////////////////////
663
cblume186d2d42016-06-03 11:17:42 -0700664sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
665 const GrMipLevel* texels, int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700666 SkBudgeted budgeted,
Brian Osman7b8400d2016-11-08 17:08:54 -0500667 SkDestinationSurfaceColorMode colorMode) {
cblume186d2d42016-06-03 11:17:42 -0700668 if (!ctx) {
669 return nullptr;
670 }
bungeman6bd52842016-10-27 09:30:08 -0700671 sk_sp<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount));
cblume186d2d42016-06-03 11:17:42 -0700672 if (!texture) {
673 return nullptr;
674 }
Brian Osman7b8400d2016-11-08 17:08:54 -0500675 texture->texturePriv().setMipColorMode(colorMode);
cblume186d2d42016-06-03 11:17:42 -0700676 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700677 info.alphaType(), std::move(texture),
678 sk_ref_sp(info.colorSpace()), budgeted);
cblume186d2d42016-06-03 11:17:42 -0700679}