blob: a19556b6539df2a703c93071cebc54200a528706 [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"
Greg Daniel7ef28f32017-04-20 16:41:55 +000013#include "GrBackendSurface.h"
Brian Osman13dddce2017-05-09 13:19:50 -040014#include "GrBackendTextureImageGenerator.h"
Stan Iliev7e910df2017-06-02 10:29:21 -040015#include "GrAHardwareBufferImageGenerator.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050016#include "GrBitmapTextureMaker.h"
reed856e9d92015-09-30 12:21:45 -070017#include "GrCaps.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000018#include "GrContext.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050019#include "GrContextPriv.h"
Brian Osman2c2bc112017-02-28 10:02:49 -050020#include "GrGpu.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050021#include "GrImageTextureMaker.h"
Brian Osman11052242016-10-27 14:47:55 -040022#include "GrRenderTargetContext.h"
Brian Osman32342f02017-03-04 08:12:46 -050023#include "GrResourceProvider.h"
Brian Osmanfe3b5162017-03-02 15:09:20 -050024#include "GrSemaphore.h"
Brian Osmane8e54582016-11-28 10:06:27 -050025#include "GrTextureAdjuster.h"
Robert Phillips646e4292017-06-13 12:44:56 -040026#include "GrTexture.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050027#include "GrTextureProxy.h"
Brian Osman63954c92017-03-14 12:07:12 -040028#include "effects/GrNonlinearColorSpaceXformEffect.h"
bsalomonf267c1e2016-02-01 13:16:14 -080029#include "effects/GrYUVEffect.h"
bsalomon993a4212015-05-29 11:37:25 -070030#include "SkCanvas.h"
reed262a71b2015-12-05 13:07:27 -080031#include "SkBitmapCache.h"
Brian Osman3b655982017-03-07 16:58:08 -050032#include "SkGr.h"
reed262a71b2015-12-05 13:07:27 -080033#include "SkImage_Gpu.h"
Brian Osman7992da32016-11-18 11:28:24 -050034#include "SkImageCacherator.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050035#include "SkImageInfoPriv.h"
ericrkb4da01d2016-06-13 11:18:14 -070036#include "SkMipMap.h"
reed6f1216a2015-08-04 08:10:13 -070037#include "SkPixelRef.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050038#include "SkReadPixelsRec.h"
bsalomon993a4212015-05-29 11:37:25 -070039
Robert Phillipsb726d582017-03-09 16:36:32 -050040SkImage_Gpu::SkImage_Gpu(GrContext* context, uint32_t uniqueID, SkAlphaType at,
41 sk_sp<GrTextureProxy> proxy,
42 sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted)
43 : INHERITED(proxy->width(), proxy->height(), uniqueID)
44 , fContext(context)
45 , fProxy(std::move(proxy))
46 , fAlphaType(at)
47 , fBudgeted(budgeted)
48 , fColorSpace(std::move(colorSpace))
49 , fAddedRasterVersionToCache(false) {
reedc9b5f8b2015-10-22 13:20:20 -070050}
piotaixrcef04f82014-07-14 07:48:04 -070051
reed6f1216a2015-08-04 08:10:13 -070052SkImage_Gpu::~SkImage_Gpu() {
53 if (fAddedRasterVersionToCache.load()) {
54 SkNotifyBitmapGenIDIsStale(this->uniqueID());
55 }
56}
57
brianosman396fcdb2016-07-22 06:26:11 -070058SkImageInfo SkImage_Gpu::onImageInfo() const {
59 SkColorType ct;
Robert Phillipsb726d582017-03-09 16:36:32 -050060 if (!GrPixelConfigToColorType(fProxy->config(), &ct)) {
brianosman396fcdb2016-07-22 06:26:11 -070061 ct = kUnknown_SkColorType;
62 }
Robert Phillipsb726d582017-03-09 16:36:32 -050063 return SkImageInfo::Make(fProxy->width(), fProxy->height(), ct, fAlphaType, fColorSpace);
brianosman396fcdb2016-07-22 06:26:11 -070064}
65
Brian Osman62517712017-04-26 16:26:39 -040066bool SkImage_Gpu::getROPixels(SkBitmap* dst, SkColorSpace*, CachingHint chint) const {
67 // The SkColorSpace parameter "dstColorSpace" is really just a hint about how/where the bitmap
68 // will be used. The client doesn't expect that we convert to that color space, it's intended
69 // for codec-backed images, to drive our decoding heuristic. In theory we *could* read directly
70 // into that color space (to save the client some effort in whatever they're about to do), but
71 // that would make our use of the bitmap cache incorrect (or much less efficient, assuming we
72 // rolled the dstColorSpace into the key).
Mike Reed5fa3d6d2017-03-25 09:51:00 -040073 const auto desc = SkBitmapCacheDesc::Make(this);
74 if (SkBitmapCache::Find(desc, dst)) {
reed6f1216a2015-08-04 08:10:13 -070075 SkASSERT(dst->getGenerationID() == this->uniqueID());
76 SkASSERT(dst->isImmutable());
77 SkASSERT(dst->getPixels());
78 return true;
79 }
80
Mike Reed7a542c52017-04-11 12:03:44 -040081 SkBitmapCache::RecPtr rec = nullptr;
82 SkPixmap pmap;
83 if (kAllow_CachingHint == chint) {
Brian Osman62517712017-04-26 16:26:39 -040084 rec = SkBitmapCache::Alloc(desc, this->onImageInfo(), &pmap);
Mike Reed7a542c52017-04-11 12:03:44 -040085 if (!rec) {
86 return false;
87 }
88 } else {
Brian Osman62517712017-04-26 16:26:39 -040089 if (!dst->tryAllocPixels(this->onImageInfo()) || !dst->peekPixels(&pmap)) {
Mike Reed7a542c52017-04-11 12:03:44 -040090 return false;
91 }
reed8b26b992015-05-07 15:36:17 -070092 }
Robert Phillipsb726d582017-03-09 16:36:32 -050093
Brian Salomon2084ffa2017-03-27 17:32:18 -040094 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext(
95 fProxy,
96 fColorSpace);
Robert Phillipsb726d582017-03-09 16:36:32 -050097 if (!sContext) {
98 return false;
99 }
100
Mike Reed7a542c52017-04-11 12:03:44 -0400101 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), 0, 0)) {
reed8b26b992015-05-07 15:36:17 -0700102 return false;
103 }
reed6f1216a2015-08-04 08:10:13 -0700104
Mike Reed7a542c52017-04-11 12:03:44 -0400105 if (rec) {
106 SkBitmapCache::Add(std::move(rec), dst);
reed09553032015-11-23 12:32:16 -0800107 fAddedRasterVersionToCache.store(true);
108 }
reed8b26b992015-05-07 15:36:17 -0700109 return true;
110}
111
Robert Phillipsb726d582017-03-09 16:36:32 -0500112sk_sp<GrTextureProxy> SkImage_Gpu::asTextureProxyRef(GrContext* context,
113 const GrSamplerParams& params,
114 SkColorSpace* dstColorSpace,
115 sk_sp<SkColorSpace>* texColorSpace,
116 SkScalar scaleAdjust[2]) const {
Robert Phillips30a38ff2017-03-22 10:31:11 -0400117 if (context != fContext) {
Robert Phillipsb726d582017-03-09 16:36:32 -0500118 SkASSERT(0);
119 return nullptr;
120 }
121
Brian Osman7992da32016-11-18 11:28:24 -0500122 if (texColorSpace) {
123 *texColorSpace = this->fColorSpace;
124 }
Robert Phillipsb726d582017-03-09 16:36:32 -0500125
Robert Phillips3798c862017-03-27 11:08:16 -0400126 GrTextureAdjuster adjuster(fContext, fProxy, this->alphaType(), this->bounds(),
Brian Osman7992da32016-11-18 11:28:24 -0500127 this->uniqueID(), this->fColorSpace.get());
Robert Phillips3798c862017-03-27 11:08:16 -0400128 return adjuster.refTextureProxySafeForParams(params, nullptr, scaleAdjust);
reed85d91782015-09-10 14:33:38 -0700129}
130
reed8b26b992015-05-07 15:36:17 -0700131static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
132 switch (info.colorType()) {
133 case kRGBA_8888_SkColorType:
134 case kBGRA_8888_SkColorType:
135 break;
136 default:
137 return; // nothing to do
138 }
139
140 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
141 // and in either case, the alpha-byte is always in the same place, so we can safely call
142 // SkPreMultiplyColor()
143 //
144 SkColor* row = (SkColor*)pixels;
145 for (int y = 0; y < info.height(); ++y) {
146 for (int x = 0; x < info.width(); ++x) {
147 row[x] = SkPreMultiplyColor(row[x]);
148 }
149 }
150}
151
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400152GrBackendObject SkImage_Gpu::onGetTextureHandle(bool flushPendingGrContextIO,
153 GrSurfaceOrigin* origin) const {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400154 SkASSERT(fProxy);
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400155
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400156 if (!fProxy->instantiate(fContext->resourceProvider())) {
157 return 0;
158 }
159
160 GrTexture* texture = fProxy->priv().peekTexture();
161
162 if (texture) {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400163 if (flushPendingGrContextIO) {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400164 fContext->contextPriv().prepareSurfaceForExternalIO(fProxy.get());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400165 }
166 if (origin) {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400167 *origin = fProxy->origin();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400168 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400169 return texture->getTextureHandle();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400170 }
171 return 0;
172}
173
174GrTexture* SkImage_Gpu::onGetTexture() const {
175 GrTextureProxy* proxy = this->peekProxy();
176 if (!proxy) {
177 return nullptr;
178 }
179
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400180 if (!proxy->instantiate(fContext->resourceProvider())) {
181 return nullptr;
182 }
183
184 return proxy->priv().peekTexture();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400185}
186
Matt Sarett03dd6d52017-01-23 12:15:09 -0500187bool SkImage_Gpu::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
reed09553032015-11-23 12:32:16 -0800188 int srcX, int srcY, CachingHint) const {
Matt Sarett03dd6d52017-01-23 12:15:09 -0500189 if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500190 return false;
191 }
192
Matt Sarett03dd6d52017-01-23 12:15:09 -0500193 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY);
194 if (!rec.trim(this->width(), this->height())) {
195 return false;
196 }
197
Robert Phillipsb726d582017-03-09 16:36:32 -0500198 // TODO: this seems to duplicate code in GrTextureContext::onReadPixels and
199 // GrRenderTargetContext::onReadPixels
reed8b26b992015-05-07 15:36:17 -0700200 uint32_t flags = 0;
Matt Sarett03dd6d52017-01-23 12:15:09 -0500201 if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) {
reed8b26b992015-05-07 15:36:17 -0700202 // let the GPU perform this transformation for us
Robert Phillipse78b7252017-04-06 07:59:41 -0400203 flags = GrContextPriv::kUnpremul_PixelOpsFlag;
reed8b26b992015-05-07 15:36:17 -0700204 }
Robert Phillipsb726d582017-03-09 16:36:32 -0500205
Brian Salomon2084ffa2017-03-27 17:32:18 -0400206 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext(
207 fProxy,
208 fColorSpace);
Robert Phillipsb726d582017-03-09 16:36:32 -0500209 if (!sContext) {
reed8b26b992015-05-07 15:36:17 -0700210 return false;
211 }
Robert Phillipsb726d582017-03-09 16:36:32 -0500212
213 if (!sContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, flags)) {
214 return false;
215 }
216
reed8b26b992015-05-07 15:36:17 -0700217 // do we have to manually fix-up the alpha channel?
218 // src dst
219 // unpremul premul fix manually
220 // premul unpremul done by kUnpremul_PixelOpsFlag
221 // all other combos need to change.
222 //
223 // Should this be handled by Ganesh? todo:?
224 //
Matt Sarett03dd6d52017-01-23 12:15:09 -0500225 if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
226 apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes);
reed8b26b992015-05-07 15:36:17 -0700227 }
228 return true;
229}
230
reed7fb4f8b2016-03-11 04:33:52 -0800231sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
Brian Salomon63e79732017-05-15 21:23:13 -0400232 GrSurfaceDesc desc;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400233 desc.fOrigin = fProxy->origin();
reed7b6945b2015-09-24 00:50:58 -0700234 desc.fWidth = subset.width();
235 desc.fHeight = subset.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -0400236 desc.fConfig = fProxy->config();
reed7b6945b2015-09-24 00:50:58 -0700237
Robert Phillipsb726d582017-03-09 16:36:32 -0500238 sk_sp<GrSurfaceContext> sContext(fContext->contextPriv().makeDeferredSurfaceContext(
Robert Phillipse2f7d182016-12-15 09:23:05 -0500239 desc,
240 SkBackingFit::kExact,
241 fBudgeted));
242 if (!sContext) {
243 return nullptr;
244 }
245
Robert Phillipsb726d582017-03-09 16:36:32 -0500246 if (!sContext->copy(fProxy.get(), subset, SkIPoint::Make(0, 0))) {
Robert Phillipse2f7d182016-12-15 09:23:05 -0500247 return nullptr;
248 }
249
Robert Phillipsb726d582017-03-09 16:36:32 -0500250 // MDB: this call is okay bc we know 'sContext' was kExact
251 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID,
252 fAlphaType, sContext->asTextureProxyRef(),
Robert Phillipse2f7d182016-12-15 09:23:05 -0500253 fColorSpace, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700254}
255
reed8b26b992015-05-07 15:36:17 -0700256///////////////////////////////////////////////////////////////////////////////////////////////////
257
Greg Daniel7ef28f32017-04-20 16:41:55 +0000258static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx,
259 const GrBackendTexture& backendTex,
260 GrSurfaceOrigin origin,
brianosmandddbe382016-07-20 13:55:39 -0700261 SkAlphaType at, sk_sp<SkColorSpace> colorSpace,
262 GrWrapOwnership ownership,
reed7fb4f8b2016-03-11 04:33:52 -0800263 SkImage::TextureReleaseProc releaseProc,
264 SkImage::ReleaseContext releaseCtx) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000265 if (backendTex.width() <= 0 || backendTex.height() <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700266 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700267 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400268
Robert Phillips7294b852017-08-01 13:51:44 +0000269 sk_sp<GrTexture> tex = ctx->resourceProvider()->wrapBackendTexture(backendTex,
270 origin,
271 ownership);
reed8b26b992015-05-07 15:36:17 -0700272 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700273 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700274 }
reedde499882015-06-18 13:41:40 -0700275 if (releaseProc) {
276 tex->setRelease(releaseProc, releaseCtx);
277 }
278
Brian Osman85d34b22017-05-10 12:06:26 -0400279 const SkBudgeted budgeted = SkBudgeted::kNo;
Robert Phillips066f0202017-07-25 10:16:35 -0400280 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(tex), origin));
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400281 return sk_make_sp<SkImage_Gpu>(ctx, kNeedNewImageUniqueID,
282 at, std::move(proxy), std::move(colorSpace), budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700283}
284
Greg Daniel94403452017-04-18 15:52:36 -0400285sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx,
286 const GrBackendTexture& tex, GrSurfaceOrigin origin,
287 SkAlphaType at, sk_sp<SkColorSpace> cs,
288 TextureReleaseProc releaseP, ReleaseContext releaseC) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000289 return new_wrapped_texture_common(ctx, tex, origin, at, std::move(cs), kBorrow_GrWrapOwnership,
290 releaseP, releaseC);
Greg Daniel94403452017-04-18 15:52:36 -0400291}
292
293sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx,
294 const GrBackendTexture& tex, GrSurfaceOrigin origin,
295 SkAlphaType at, sk_sp<SkColorSpace> cs) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000296 return new_wrapped_texture_common(ctx, tex, origin, at, std::move(cs), kAdopt_GrWrapOwnership,
297 nullptr, nullptr);
298}
299
300static GrBackendTexture make_backend_texture_from_handle(GrBackend backend,
301 int width, int height,
302 GrPixelConfig config,
303 GrBackendObject handle) {
Brian Salomon8fe24272017-07-07 12:56:11 -0400304 switch (backend) {
305 case kOpenGL_GrBackend: {
306 const GrGLTextureInfo* glInfo = (const GrGLTextureInfo*)(handle);
307 return GrBackendTexture(width, height, config, *glInfo);
308 }
Mike Reedd20b5c42017-06-14 06:03:10 -0400309#ifdef SK_VULKAN
Brian Salomon8fe24272017-07-07 12:56:11 -0400310 case kVulkan_GrBackend: {
311 const GrVkImageInfo* vkInfo = (const GrVkImageInfo*)(handle);
312 return GrBackendTexture(width, height, *vkInfo);
313 }
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000314#endif
Brian Salomon8fe24272017-07-07 12:56:11 -0400315 case kMock_GrBackend: {
316 const GrMockTextureInfo* mockInfo = (const GrMockTextureInfo*)(handle);
317 return GrBackendTexture(width, height, config, *mockInfo);
318 }
319 default:
320 return GrBackendTexture();
321 }
Greg Daniel94403452017-04-18 15:52:36 -0400322}
323
jbaumanb445a572016-06-09 13:24:48 -0700324static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace,
325 bool nv12,
326 const GrBackendObject yuvTextureHandles[],
327 const SkISize yuvSizes[],
brianosmandddbe382016-07-20 13:55:39 -0700328 GrSurfaceOrigin origin,
329 sk_sp<SkColorSpace> imageColorSpace) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800330 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700331
jbaumanb445a572016-06-09 13:24:48 -0700332 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 ||
333 yuvSizes[1].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700334 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700335 }
jbaumanb445a572016-06-09 13:24:48 -0700336 if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) {
337 return nullptr;
338 }
339
340 const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig;
341
Greg Daniel7ef28f32017-04-20 16:41:55 +0000342 GrBackend backend = ctx->contextPriv().getBackend();
343 GrBackendTexture yTex = make_backend_texture_from_handle(backend,
344 yuvSizes[0].fWidth,
345 yuvSizes[0].fHeight,
346 kConfig,
347 yuvTextureHandles[0]);
348 GrBackendTexture uTex = make_backend_texture_from_handle(backend,
349 yuvSizes[1].fWidth,
350 yuvSizes[1].fHeight,
351 kConfig,
352 yuvTextureHandles[1]);
bsalomon993a4212015-05-29 11:37:25 -0700353
Greg Daniel7ef28f32017-04-20 16:41:55 +0000354 sk_sp<GrTextureProxy> yProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, yTex, origin);
355 sk_sp<GrTextureProxy> uProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, uTex, origin);
356 sk_sp<GrTextureProxy> vProxy;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500357
jbaumanb445a572016-06-09 13:24:48 -0700358 if (nv12) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500359 vProxy = uProxy;
jbaumanb445a572016-06-09 13:24:48 -0700360 } else {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000361 GrBackendTexture vTex = make_backend_texture_from_handle(backend,
362 yuvSizes[2].fWidth,
363 yuvSizes[2].fHeight,
364 kConfig,
365 yuvTextureHandles[2]);
366 vProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, vTex, origin);
jbaumanb445a572016-06-09 13:24:48 -0700367 }
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500368 if (!yProxy || !uProxy || !vProxy) {
halcanary96fcdcc2015-08-27 07:41:13 -0700369 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700370 }
371
robertphillipsd4c741e2016-04-28 09:55:15 -0700372 const int width = yuvSizes[0].fWidth;
373 const int height = yuvSizes[0].fHeight;
robertphillipsaa19a5f2016-04-28 06:21:55 -0700374
robertphillipsd4c741e2016-04-28 09:55:15 -0700375 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400376 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400377 SkBackingFit::kExact,
378 width, height,
379 kRGBA_8888_GrPixelConfig,
380 std::move(imageColorSpace),
381 0,
382 origin));
383 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700384 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700385 }
386
387 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400388 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400389 paint.addColorFragmentProcessor(GrYUVEffect::MakeYUVToRGB(yProxy, uProxy, vProxy,
390 yuvSizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700391
Robert Phillipsb726d582017-03-09 16:36:32 -0500392 const SkRect rect = SkRect::MakeIWH(width, height);
robertphillipsc9a37062015-09-01 08:34:28 -0700393
Brian Salomon82f44312017-01-11 13:42:54 -0500394 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Robert Phillipse60ad622016-11-17 10:22:48 -0500395
Robert Phillips7ee385e2017-03-30 08:02:11 -0400396 if (!renderTargetContext->asSurfaceProxy()) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500397 return nullptr;
398 }
Robert Phillips7ee385e2017-03-30 08:02:11 -0400399 ctx->contextPriv().flushSurfaceWrites(renderTargetContext->asSurfaceProxy());
Robert Phillipsb726d582017-03-09 16:36:32 -0500400
401 // MDB: this call is okay bc we know 'renderTargetContext' was exact
402 return sk_make_sp<SkImage_Gpu>(ctx, kNeedNewImageUniqueID,
403 kOpaque_SkAlphaType, renderTargetContext->asTextureProxyRef(),
Robert Phillips75a475c2017-01-13 09:18:59 -0500404 renderTargetContext->refColorSpace(), budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700405}
reed56179002015-07-07 06:11:19 -0700406
jbaumanb445a572016-06-09 13:24:48 -0700407sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
408 const GrBackendObject yuvTextureHandles[3],
brianosmandddbe382016-07-20 13:55:39 -0700409 const SkISize yuvSizes[3], GrSurfaceOrigin origin,
410 sk_sp<SkColorSpace> imageColorSpace) {
411 return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin,
412 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700413}
414
415sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
416 const GrBackendObject yuvTextureHandles[2],
417 const SkISize yuvSizes[2],
brianosmandddbe382016-07-20 13:55:39 -0700418 GrSurfaceOrigin origin,
419 sk_sp<SkColorSpace> imageColorSpace) {
420 return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin,
421 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700422}
423
Robert Phillips3798c862017-03-27 11:08:16 -0400424static sk_sp<SkImage> create_image_from_maker(GrContext* context, GrTextureMaker* maker,
425 SkAlphaType at, uint32_t id,
Brian Osman041f7df2017-02-07 11:23:28 -0500426 SkColorSpace* dstColorSpace) {
427 sk_sp<SkColorSpace> texColorSpace;
Robert Phillips3798c862017-03-27 11:08:16 -0400428 sk_sp<GrTextureProxy> proxy(maker->refTextureProxyForParams(GrSamplerParams::ClampNoFilter(),
429 dstColorSpace,
430 &texColorSpace, nullptr));
431 if (!proxy) {
Brian Osman041f7df2017-02-07 11:23:28 -0500432 return nullptr;
433 }
Robert Phillips3798c862017-03-27 11:08:16 -0400434 return sk_make_sp<SkImage_Gpu>(context, id, at,
435 std::move(proxy), std::move(texColorSpace), SkBudgeted::kNo);
Brian Osman041f7df2017-02-07 11:23:28 -0500436}
437
Brian Osman2c2bc112017-02-28 10:02:49 -0500438sk_sp<SkImage> SkImage::makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const {
Brian Osman041f7df2017-02-07 11:23:28 -0500439 if (!context) {
440 return nullptr;
441 }
Robert Phillips87444052017-06-23 14:09:30 -0400442 if (GrContext* incumbent = as_IB(this)->context()) {
443 return incumbent == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr;
Brian Osman041f7df2017-02-07 11:23:28 -0500444 }
445
Brian Osmandf7e0752017-04-26 16:20:28 -0400446 if (this->isLazyGenerated()) {
447 GrImageTextureMaker maker(context, this, kDisallow_CachingHint);
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400448 return create_image_from_maker(context, &maker, this->alphaType(),
449 this->uniqueID(), dstColorSpace);
Brian Osman041f7df2017-02-07 11:23:28 -0500450 }
451
452 if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) {
453 GrBitmapTextureMaker maker(context, *bmp);
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400454 return create_image_from_maker(context, &maker, this->alphaType(),
455 this->uniqueID(), dstColorSpace);
Brian Osman041f7df2017-02-07 11:23:28 -0500456 }
457 return nullptr;
458}
459
Brian Osman13dddce2017-05-09 13:19:50 -0400460sk_sp<SkImage> SkImage::MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> encoded,
461 bool buildMips, SkColorSpace* dstColorSpace) {
462 sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(std::move(encoded));
463 if (!codecImage) {
464 return nullptr;
465 }
466
467 // Some backends or drivers don't support (safely) moving resources between contexts
468 if (!context || !context->caps()->crossContextTextureSupport()) {
469 return codecImage;
470 }
471
472 // Turn the codec image into a GrTextureProxy
473 GrImageTextureMaker maker(context, codecImage.get(), kDisallow_CachingHint);
474 sk_sp<SkColorSpace> texColorSpace;
475 GrSamplerParams params(SkShader::kClamp_TileMode,
476 buildMips ? GrSamplerParams::kMipMap_FilterMode
477 : GrSamplerParams::kBilerp_FilterMode);
478 sk_sp<GrTextureProxy> proxy(maker.refTextureProxyForParams(params, dstColorSpace,
479 &texColorSpace, nullptr));
480 if (!proxy) {
481 return codecImage;
482 }
483
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400484 if (!proxy->instantiate(context->resourceProvider())) {
Brian Osman13dddce2017-05-09 13:19:50 -0400485 return codecImage;
486 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400487 sk_sp<GrTexture> texture = sk_ref_sp(proxy->priv().peekTexture());
Brian Osman13dddce2017-05-09 13:19:50 -0400488
489 // Flush any writes or uploads
490 context->contextPriv().prepareSurfaceForExternalIO(proxy.get());
491
492 sk_sp<GrSemaphore> sema = context->getGpu()->prepareTextureForCrossContextUsage(texture.get());
493
Robert Phillips7294b852017-08-01 13:51:44 +0000494 auto gen = GrBackendTextureImageGenerator::Make(std::move(texture),
Robert Phillips16d8ec62017-07-27 16:16:25 -0400495 std::move(sema), codecImage->alphaType(),
Brian Osman13dddce2017-05-09 13:19:50 -0400496 std::move(texColorSpace));
497 return SkImage::MakeFromGenerator(std::move(gen));
498}
499
Derek Sollenberger7a869872017-06-27 15:37:25 -0400500#if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26
Stan Iliev7e910df2017-06-02 10:29:21 -0400501sk_sp<SkImage> SkImage::MakeFromAHardwareBuffer(AHardwareBuffer* graphicBuffer, SkAlphaType at,
502 sk_sp<SkColorSpace> cs) {
503 auto gen = GrAHardwareBufferImageGenerator::Make(graphicBuffer, at, cs);
504 return SkImage::MakeFromGenerator(std::move(gen));
505}
506#endif
507
bsalomon634b4302016-07-12 18:11:17 -0700508sk_sp<SkImage> SkImage::makeNonTextureImage() const {
brianosman396fcdb2016-07-22 06:26:11 -0700509 if (!this->isTextureBacked()) {
bsalomon634b4302016-07-12 18:11:17 -0700510 return sk_ref_sp(const_cast<SkImage*>(this));
511 }
brianosman396fcdb2016-07-22 06:26:11 -0700512 SkImageInfo info = as_IB(this)->onImageInfo();
bsalomon634b4302016-07-12 18:11:17 -0700513 size_t rowBytes = info.minRowBytes();
514 size_t size = info.getSafeSize(rowBytes);
515 auto data = SkData::MakeUninitialized(size);
516 if (!data) {
517 return nullptr;
518 }
519 SkPixmap pm(info, data->writable_data(), rowBytes);
520 if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
521 return nullptr;
522 }
523 return MakeRasterData(info, data, rowBytes);
524}
525
reed56179002015-07-07 06:11:19 -0700526///////////////////////////////////////////////////////////////////////////////////////////////////
527
bsalomon4d516a62016-07-28 13:37:31 -0700528namespace {
529struct MipMapLevelData {
530 void* fPixelData;
531 size_t fRowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800532};
533
bsalomon4d516a62016-07-28 13:37:31 -0700534struct DeferredTextureImage {
Brian Osman7b8400d2016-11-08 17:08:54 -0500535 uint32_t fContextUniqueID;
536 // Right now, the destination color mode is only considered when generating mipmaps
537 SkDestinationSurfaceColorMode fColorMode;
bsalomon4d516a62016-07-28 13:37:31 -0700538 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace.
Brian Osman7b8400d2016-11-08 17:08:54 -0500539 int fWidth;
540 int fHeight;
541 SkColorType fColorType;
542 SkAlphaType fAlphaType;
543 void* fColorSpace;
544 size_t fColorSpaceSize;
Brian Osman7b8400d2016-11-08 17:08:54 -0500545 int fMipMapLevelCount;
bsalomon4d516a62016-07-28 13:37:31 -0700546 // The fMipMapLevelData array may contain more than 1 element.
547 // It contains fMipMapLevelCount elements.
548 // That means this struct's size is not known at compile-time.
Brian Osman7b8400d2016-11-08 17:08:54 -0500549 MipMapLevelData fMipMapLevelData[1];
bsalomon4d516a62016-07-28 13:37:31 -0700550};
551} // anonymous namespace
552
cblume33e0cb52016-08-30 12:09:23 -0700553static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
Eric Karla422d702016-11-30 11:09:29 -0800554 // There is a bug in the mipmap pre-generation logic in use in getDeferredTextureImageData.
555 // This can cause runaway memory leaks, so we are disabling this path until we can
556 // investigate further. crbug.com/669775
557 return false;
cblume33e0cb52016-08-30 12:09:23 -0700558}
559
560namespace {
561
562class DTIBufferFiller
563{
564public:
cblume921bc672016-09-22 05:25:26 -0700565 explicit DTIBufferFiller(char* bufferAsCharPtr)
566 : bufferAsCharPtr_(bufferAsCharPtr) {}
cblume33e0cb52016-08-30 12:09:23 -0700567
568 void fillMember(const void* source, size_t memberOffset, size_t size) {
cblume921bc672016-09-22 05:25:26 -0700569 memcpy(bufferAsCharPtr_ + memberOffset, source, size);
cblume33e0cb52016-08-30 12:09:23 -0700570 }
571
572private:
573
cblume921bc672016-09-22 05:25:26 -0700574 char* bufferAsCharPtr_;
cblume33e0cb52016-08-30 12:09:23 -0700575};
576}
577
578#define FILL_MEMBER(bufferFiller, member, source) \
579 bufferFiller.fillMember(source, \
580 offsetof(DeferredTextureImage, member), \
581 sizeof(DeferredTextureImage::member));
582
Eric Karl7a8c84c2017-06-12 10:05:49 -0700583static bool SupportsColorSpace(SkColorType colorType) {
584 switch (colorType) {
585 case kRGBA_8888_SkColorType:
586 case kBGRA_8888_SkColorType:
587 case kRGBA_F16_SkColorType:
588 return true;
589 default:
590 return false;
591 }
592}
593
bsalomon41b952c2016-03-11 06:46:33 -0800594size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
ericrkb4da01d2016-06-13 11:18:14 -0700595 const DeferredTextureImageUsageParams params[],
cblume33e0cb52016-08-30 12:09:23 -0700596 int paramCnt, void* buffer,
Eric Karl7a8c84c2017-06-12 10:05:49 -0700597 SkColorSpace* dstColorSpace,
598 SkColorType dstColorType) const {
Mike Reed7f1d0202017-05-08 16:13:39 -0400599 // Some quick-rejects where is makes no sense to return CPU data
600 // e.g.
601 // - texture backed
602 // - picture backed
603 //
604 if (this->isTextureBacked()) {
605 return 0;
606 }
607 if (as_IB(this)->onCanLazyGenerateOnGPU()) {
608 return 0;
609 }
610
Eric Karl7a8c84c2017-06-12 10:05:49 -0700611 bool supportsColorSpace = SupportsColorSpace(dstColorType);
612 // Quick reject if the caller requests a color space with an unsupported color type.
613 if (SkToBool(dstColorSpace) && !supportsColorSpace) {
614 return 0;
615 }
616
ericrkb4da01d2016-06-13 11:18:14 -0700617 // Extract relevant min/max values from the params array.
618 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
619 SkFilterQuality highestFilterQuality = params[0].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700620 bool useMipMaps = should_use_mip_maps(params[0]);
ericrkb4da01d2016-06-13 11:18:14 -0700621 for (int i = 1; i < paramCnt; ++i) {
622 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
623 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
624 if (highestFilterQuality < params[i].fQuality)
625 highestFilterQuality = params[i].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700626 useMipMaps |= should_use_mip_maps(params[i]);
ericrkb4da01d2016-06-13 11:18:14 -0700627 }
628
bsalomon41b952c2016-03-11 06:46:33 -0800629 const bool fillMode = SkToBool(buffer);
630 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
631 return 0;
632 }
633
ericrkb4da01d2016-06-13 11:18:14 -0700634 // Calculate scaling parameters.
635 bool isScaled = lowestPreScaleMipLevel != 0;
636
637 SkISize scaledSize;
638 if (isScaled) {
639 // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the
640 // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level.
641 scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(),
642 lowestPreScaleMipLevel - 1);
643 } else {
644 scaledSize = SkISize::Make(this->width(), this->height());
645 }
646
647 // We never want to scale at higher than SW medium quality, as SW medium matches GPU high.
648 SkFilterQuality scaleFilterQuality = highestFilterQuality;
649 if (scaleFilterQuality > kMedium_SkFilterQuality) {
650 scaleFilterQuality = kMedium_SkFilterQuality;
651 }
652
ericrkc429baf2016-03-24 15:35:45 -0700653 const int maxTextureSize = proxy.fCaps->maxTextureSize();
ericrkb4da01d2016-06-13 11:18:14 -0700654 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkc429baf2016-03-24 15:35:45 -0700655 return 0;
656 }
657
bsalomon41b952c2016-03-11 06:46:33 -0800658 SkAutoPixmapStorage pixmap;
659 SkImageInfo info;
660 size_t pixelSize = 0;
Mike Reed086a4272017-07-18 10:53:11 -0400661 if (!isScaled && this->peekPixels(&pixmap) && pixmap.info().colorType() == dstColorType) {
bsalomon41b952c2016-03-11 06:46:33 -0800662 info = pixmap.info();
663 pixelSize = SkAlign8(pixmap.getSafeSize());
Matt Sarettade76e92017-04-14 12:41:55 -0400664 if (!dstColorSpace) {
665 pixmap.setColorSpace(nullptr);
666 info = info.makeColorSpace(nullptr);
667 }
bsalomon41b952c2016-03-11 06:46:33 -0800668 } else {
Mike Reed7f1d0202017-05-08 16:13:39 -0400669 if (!this->isLazyGenerated() && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800670 return 0;
671 }
Brian Osman7992da32016-11-18 11:28:24 -0500672 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
673 // Generator backed image. Tweak info to trigger correct kind of decode.
Brian Osman7992da32016-11-18 11:28:24 -0500674 SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat(
Brian Osman61624f02016-12-09 14:51:59 -0500675 dstColorSpace, proxy.fCaps.get());
Brian Osman7992da32016-11-18 11:28:24 -0500676 info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(),
677 scaledSize.height());
Brian Osman7992da32016-11-18 11:28:24 -0500678 } else {
679 info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height());
Matt Sarettade76e92017-04-14 12:41:55 -0400680 if (!dstColorSpace) {
681 info = info.makeColorSpace(nullptr);
682 }
Brian Osman7992da32016-11-18 11:28:24 -0500683 }
Eric Karl7a8c84c2017-06-12 10:05:49 -0700684 // Force color type to be the requested type.
685 info = info.makeColorType(dstColorType);
bsalomon41b952c2016-03-11 06:46:33 -0800686 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
687 if (fillMode) {
Eric Karl7a8c84c2017-06-12 10:05:49 -0700688 // Always decode to N32 and convert to the requested type if necessary.
689 SkImageInfo decodeInfo = info.makeColorType(kN32_SkColorType);
690 SkAutoPixmapStorage decodePixmap;
691 decodePixmap.alloc(decodeInfo);
692
ericrkb4da01d2016-06-13 11:18:14 -0700693 if (isScaled) {
Eric Karl7a8c84c2017-06-12 10:05:49 -0700694 if (!this->scalePixels(decodePixmap, scaleFilterQuality,
ericrkb4da01d2016-06-13 11:18:14 -0700695 SkImage::kDisallow_CachingHint)) {
696 return 0;
697 }
698 } else {
Eric Karl7a8c84c2017-06-12 10:05:49 -0700699 if (!this->readPixels(decodePixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
ericrkb4da01d2016-06-13 11:18:14 -0700700 return 0;
701 }
bsalomon41b952c2016-03-11 06:46:33 -0800702 }
Eric Karl7a8c84c2017-06-12 10:05:49 -0700703
704 if (decodeInfo.colorType() != info.colorType()) {
705 pixmap.alloc(info);
706 // Convert and copy the decoded pixmap to the target pixmap.
707 decodePixmap.readPixels(pixmap.info(), pixmap.writable_addr(), pixmap.rowBytes(), 0,
708 0);
709 } else {
710 pixmap = std::move(decodePixmap);
711 }
bsalomon41b952c2016-03-11 06:46:33 -0800712 }
713 }
cblume2c052802016-05-31 09:55:08 -0700714 int mipMapLevelCount = 1;
cblume33e0cb52016-08-30 12:09:23 -0700715 if (useMipMaps) {
716 // SkMipMap only deals with the mipmap levels it generates, which does
717 // not include the base level.
718 // That means it generates and holds levels 1-x instead of 0-x.
719 // So the total mipmap level count is 1 more than what
720 // SkMipMap::ComputeLevelCount returns.
721 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1;
722
723 // We already initialized pixelSize to the size of the base level.
724 // SkMipMap will generate the extra mipmap levels. Their sizes need to
725 // be added to the total.
726 // Index 0 here does not refer to the base mipmap level -- it is
727 // SkMipMap's first generated mipmap level (level 1).
728 for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0;
729 currentMipMapLevelIndex--) {
730 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(),
731 currentMipMapLevelIndex);
brianosman32b5e702016-10-13 06:44:23 -0700732 SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight);
cblume33e0cb52016-08-30 12:09:23 -0700733 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr));
734 }
735 }
bsalomon41b952c2016-03-11 06:46:33 -0800736 size_t size = 0;
737 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
738 size += dtiSize;
cblume33e0cb52016-08-30 12:09:23 -0700739 size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData);
740 // We subtract 1 because DeferredTextureImage already includes the base
741 // level in its size
bsalomon41b952c2016-03-11 06:46:33 -0800742 size_t pixelOffset = size;
743 size += pixelSize;
bsalomon4d516a62016-07-28 13:37:31 -0700744 size_t colorSpaceOffset = 0;
745 size_t colorSpaceSize = 0;
Matt Sarett84c9cb72017-04-10 11:03:27 -0400746 SkColorSpaceTransferFn fn;
bsalomon4d516a62016-07-28 13:37:31 -0700747 if (info.colorSpace()) {
Matt Sarettade76e92017-04-14 12:41:55 -0400748 SkASSERT(dstColorSpace);
Eric Karl7a8c84c2017-06-12 10:05:49 -0700749 SkASSERT(supportsColorSpace);
bsalomon4d516a62016-07-28 13:37:31 -0700750 colorSpaceOffset = size;
751 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
752 size += colorSpaceSize;
Eric Karl7a8c84c2017-06-12 10:05:49 -0700753 } else if (supportsColorSpace && this->colorSpace() && this->colorSpace()->isNumericalTransferFn(&fn)) {
Matt Sarett84c9cb72017-04-10 11:03:27 -0400754 // In legacy mode, preserve the color space tag on the SkImage. This is only
755 // supported if the color space has a parametric transfer function.
756 SkASSERT(!dstColorSpace);
757 colorSpaceOffset = size;
758 colorSpaceSize = this->colorSpace()->writeToMemory(nullptr);
759 size += colorSpaceSize;
bsalomon4d516a62016-07-28 13:37:31 -0700760 }
bsalomon41b952c2016-03-11 06:46:33 -0800761 if (!fillMode) {
762 return size;
763 }
cblume921bc672016-09-22 05:25:26 -0700764 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer);
765 char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset;
766 void* pixels = pixelsAsCharPtr;
bsalomon41b952c2016-03-11 06:46:33 -0800767
cblume921bc672016-09-22 05:25:26 -0700768 memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))),
769 pixmap.addr(), pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800770
Brian Osman6c15cc72016-10-17 09:51:37 -0400771 // If the context has sRGB support, and we're intending to render to a surface with an attached
772 // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB)
773 // aware mip-mapping.
Brian Osman7b8400d2016-11-08 17:08:54 -0500774 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
Brian Osman6c15cc72016-10-17 09:51:37 -0400775 if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) &&
776 info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) {
Eric Karl7a8c84c2017-06-12 10:05:49 -0700777 SkASSERT(supportsColorSpace);
Brian Osman7b8400d2016-11-08 17:08:54 -0500778 colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware;
Brian Osman6c15cc72016-10-17 09:51:37 -0400779 }
780
bsalomon41b952c2016-03-11 06:46:33 -0800781 SkASSERT(info == pixmap.info());
782 size_t rowBytes = pixmap.rowBytes();
cblume33e0cb52016-08-30 12:09:23 -0700783 static_assert(std::is_standard_layout<DeferredTextureImage>::value,
784 "offsetof, which we use below, requires the type have standard layout");
cblume921bc672016-09-22 05:25:26 -0700785 auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr};
Brian Osman7b8400d2016-11-08 17:08:54 -0500786 FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode);
cblume33e0cb52016-08-30 12:09:23 -0700787 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID);
788 int width = info.width();
789 FILL_MEMBER(dtiBufferFiller, fWidth, &width);
790 int height = info.height();
791 FILL_MEMBER(dtiBufferFiller, fHeight, &height);
792 SkColorType colorType = info.colorType();
793 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType);
794 SkAlphaType alphaType = info.alphaType();
795 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType);
cblume33e0cb52016-08-30 12:09:23 -0700796 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount);
cblume921bc672016-09-22 05:25:26 -0700797 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData),
cblume33e0cb52016-08-30 12:09:23 -0700798 &pixels, sizeof(pixels));
cblume921bc672016-09-22 05:25:26 -0700799 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes),
cblume33e0cb52016-08-30 12:09:23 -0700800 &rowBytes, sizeof(rowBytes));
bsalomon4d516a62016-07-28 13:37:31 -0700801 if (colorSpaceSize) {
cblume921bc672016-09-22 05:25:26 -0700802 void* colorSpace = bufferAsCharPtr + colorSpaceOffset;
cblume33e0cb52016-08-30 12:09:23 -0700803 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace);
804 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize);
Matt Sarett84c9cb72017-04-10 11:03:27 -0400805 if (info.colorSpace()) {
806 info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
807 } else {
808 SkASSERT(this->colorSpace() && this->colorSpace()->isNumericalTransferFn(&fn));
809 SkASSERT(!dstColorSpace);
810 this->colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
811 }
bsalomon4d516a62016-07-28 13:37:31 -0700812 } else {
cblume921bc672016-09-22 05:25:26 -0700813 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace),
cblume33e0cb52016-08-30 12:09:23 -0700814 0, sizeof(DeferredTextureImage::fColorSpace));
cblume921bc672016-09-22 05:25:26 -0700815 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize),
cblume33e0cb52016-08-30 12:09:23 -0700816 0, sizeof(DeferredTextureImage::fColorSpaceSize));
817 }
818
819 // Fill in the mipmap levels if they exist
cblume921bc672016-09-22 05:25:26 -0700820 char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700821
822 if (useMipMaps) {
cblume94ddf542016-09-16 10:07:32 -0700823 static_assert(std::is_standard_layout<MipMapLevelData>::value,
824 "offsetof, which we use below, requires the type have a standard layout");
cblume33e0cb52016-08-30 12:09:23 -0700825
Brian Osman7b8400d2016-11-08 17:08:54 -0500826 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
cblume33e0cb52016-08-30 12:09:23 -0700827 // SkMipMap holds only the mipmap levels it generates.
828 // A programmer can use the data they provided to SkMipMap::Build as level 0.
829 // So the SkMipMap provides levels 1-x but it stores them in its own
830 // range 0-(x-1).
831 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1;
832 generatedMipLevelIndex++) {
cblume33e0cb52016-08-30 12:09:23 -0700833 SkMipMap::Level mipLevel;
834 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
835
836 // Make sure the mipmap data is after the start of the buffer
cblume921bc672016-09-22 05:25:26 -0700837 SkASSERT(mipLevelPtr > bufferAsCharPtr);
cblume33e0cb52016-08-30 12:09:23 -0700838 // Make sure the mipmap data starts before the end of the buffer
cblume921bc672016-09-22 05:25:26 -0700839 SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700840 // Make sure the mipmap data ends before the end of the buffer
cblume12f75272016-09-19 06:18:03 -0700841 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <=
cblume921bc672016-09-22 05:25:26 -0700842 bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700843
844 // getSafeSize includes rowbyte padding except for the last row,
845 // right?
846
cblume921bc672016-09-22 05:25:26 -0700847 memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700848
cblume921bc672016-09-22 05:25:26 -0700849 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
850 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
851 offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*));
cblume33e0cb52016-08-30 12:09:23 -0700852 size_t rowBytes = mipLevel.fPixmap.rowBytes();
cblume921bc672016-09-22 05:25:26 -0700853 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
854 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
855 offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes));
cblume33e0cb52016-08-30 12:09:23 -0700856
857 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize());
858 }
bsalomon4d516a62016-07-28 13:37:31 -0700859 }
bsalomon41b952c2016-03-11 06:46:33 -0800860 return size;
861}
862
863sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
864 SkBudgeted budgeted) {
865 if (!data) {
866 return nullptr;
867 }
868 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
869
Brian Salomon228482a2017-07-13 09:23:21 -0400870 if (!context || context->uniqueID() != dti->fContextUniqueID || context->abandoned()) {
bsalomon41b952c2016-03-11 06:46:33 -0800871 return nullptr;
872 }
cblume33e0cb52016-08-30 12:09:23 -0700873 int mipLevelCount = dti->fMipMapLevelCount;
874 SkASSERT(mipLevelCount >= 1);
bsalomon4d516a62016-07-28 13:37:31 -0700875 sk_sp<SkColorSpace> colorSpace;
876 if (dti->fColorSpaceSize) {
877 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize);
878 }
879 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
880 dti->fColorType, dti->fAlphaType, colorSpace);
Brian Osmanb92234a2017-01-25 14:13:00 +0000881 if (mipLevelCount == 1) {
882 SkPixmap pixmap;
883 pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData, dti->fMipMapLevelData[0].fRowBytes);
Matt Sarett84c9cb72017-04-10 11:03:27 -0400884
Matt Sarettdedac852017-05-12 10:56:49 -0400885 // Pass nullptr for the |dstColorSpace|. This opts in to more lenient color space
886 // verification. This is ok because we've already verified the color space in
887 // getDeferredTextureImageData().
888 sk_sp<GrTextureProxy> proxy(GrUploadPixmapToTextureProxy(
889 context->resourceProvider(), pixmap, budgeted, nullptr));
Brian Osmaned182d72017-03-20 11:03:55 -0400890 if (!proxy) {
891 return nullptr;
892 }
893 return sk_make_sp<SkImage_Gpu>(context, kNeedNewImageUniqueID, pixmap.alphaType(),
894 std::move(proxy), std::move(colorSpace), budgeted);
Brian Osmanb92234a2017-01-25 14:13:00 +0000895 } else {
896 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
897 for (int i = 0; i < mipLevelCount; i++) {
898 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
899 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
900 }
Brian Osman8ccbbb42017-01-20 14:08:04 -0500901
Brian Osmanb92234a2017-01-25 14:13:00 +0000902 return SkImage::MakeTextureFromMipMap(context, info, texels.get(),
903 mipLevelCount, SkBudgeted::kYes,
904 dti->fColorMode);
905 }
bsalomon41b952c2016-03-11 06:46:33 -0800906}
907
908///////////////////////////////////////////////////////////////////////////////////////////////////
909
cblume186d2d42016-06-03 11:17:42 -0700910sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
Robert Phillips590533f2017-07-11 14:22:35 -0400911 const GrMipLevel texels[], int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700912 SkBudgeted budgeted,
Brian Osman7b8400d2016-11-08 17:08:54 -0500913 SkDestinationSurfaceColorMode colorMode) {
Robert Phillips1119dc32017-04-11 12:54:57 -0400914 SkASSERT(mipLevelCount >= 1);
cblume186d2d42016-06-03 11:17:42 -0700915 if (!ctx) {
916 return nullptr;
917 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400918 sk_sp<GrTextureProxy> proxy(GrUploadMipMapToTextureProxy(ctx, info, texels, mipLevelCount,
919 colorMode));
920 if (!proxy) {
cblume186d2d42016-06-03 11:17:42 -0700921 return nullptr;
922 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400923
924 SkASSERT(proxy->priv().isExact());
925 return sk_make_sp<SkImage_Gpu>(ctx, kNeedNewImageUniqueID,
926 info.alphaType(), std::move(proxy),
927 info.refColorSpace(), budgeted);
cblume186d2d42016-06-03 11:17:42 -0700928}
Brian Osman63954c92017-03-14 12:07:12 -0400929
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400930sk_sp<SkImage> SkImage_Gpu::onMakeColorSpace(sk_sp<SkColorSpace> target, SkColorType,
Matt Sarett9f3dcb32017-05-04 08:53:32 -0400931 SkTransferFunctionBehavior premulBehavior) const {
932 if (SkTransferFunctionBehavior::kRespect == premulBehavior) {
933 // TODO: Implement this.
934 return nullptr;
935 }
936
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400937 sk_sp<SkColorSpace> srcSpace = fColorSpace;
938 if (!fColorSpace) {
939 if (target->isSRGB()) {
940 return sk_ref_sp(const_cast<SkImage*>((SkImage*)this));
941 }
942
943 srcSpace = SkColorSpace::MakeSRGB();
944 }
945
946 auto xform = GrNonlinearColorSpaceXformEffect::Make(srcSpace.get(), target.get());
Brian Osman63954c92017-03-14 12:07:12 -0400947 if (!xform) {
948 return sk_ref_sp(const_cast<SkImage_Gpu*>(this));
949 }
950
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400951 sk_sp<GrRenderTargetContext> renderTargetContext(fContext->makeDeferredRenderTargetContext(
Brian Osman63954c92017-03-14 12:07:12 -0400952 SkBackingFit::kExact, this->width(), this->height(), kRGBA_8888_GrPixelConfig, nullptr));
953 if (!renderTargetContext) {
954 return nullptr;
955 }
956
957 GrPaint paint;
958 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400959 paint.addColorTextureProcessor(fProxy, nullptr, SkMatrix::I());
Brian Osman63954c92017-03-14 12:07:12 -0400960 paint.addColorFragmentProcessor(std::move(xform));
961
962 const SkRect rect = SkRect::MakeIWH(this->width(), this->height());
963
964 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
965
Robert Phillips7ee385e2017-03-30 08:02:11 -0400966 if (!renderTargetContext->asTextureProxy()) {
Brian Osman63954c92017-03-14 12:07:12 -0400967 return nullptr;
968 }
969
970 // MDB: this call is okay bc we know 'renderTargetContext' was exact
971 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID,
972 fAlphaType, renderTargetContext->asTextureProxyRef(),
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400973 std::move(target), fBudgeted);
Brian Osman63954c92017-03-14 12:07:12 -0400974
975}
Brian Osman5bbd0762017-05-08 11:07:42 -0400976
977bool SkImage_Gpu::onIsValid(GrContext* context) const {
978 // The base class has already checked that context isn't abandoned (if it's not nullptr)
979 if (fContext->abandoned()) {
980 return false;
981 }
982
983 if (context && context != fContext) {
984 return false;
985 }
986
987 return true;
988}