blob: e477a5e564dac5e31c0131507dfad7dbe9f33cb3 [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,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400113 const GrSamplerState& params,
Robert Phillipsb726d582017-03-09 16:36:32 -0500114 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
Brian Salomon4df00922017-09-07 16:34:11 +0000126 GrTextureAdjuster adjuster(fContext, fProxy, this->alphaType(), this->bounds(),
127 this->uniqueID(), this->fColorSpace.get());
Greg Daniel4faa0402017-09-29 15:21:28 -0400128 return adjuster.refTextureProxySafeForParams(params, 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 Osmana8ac9242017-09-07 10:19:08 -0400206 // This hack allows us to call makeNonTextureImage on images with arbitrary color spaces.
207 // Otherwise, we'll be unable to create a render target context.
208 // TODO: This shouldn't be necessary - we need more robust support for images (and surfaces)
209 // with arbitrary color spaces. Unfortunately, this is one spot where we go from image to
210 // surface (rather than the opposite), and our lenient image rules break our (currently) more
211 // strict surface rules.
212 sk_sp<SkColorSpace> surfaceColorSpace = fColorSpace;
213 if (!flags && SkColorSpace::Equals(fColorSpace.get(), dstInfo.colorSpace())) {
214 surfaceColorSpace = nullptr;
215 }
216
Brian Salomon2084ffa2017-03-27 17:32:18 -0400217 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext(
Brian Osmana8ac9242017-09-07 10:19:08 -0400218 fProxy, surfaceColorSpace);
Robert Phillipsb726d582017-03-09 16:36:32 -0500219 if (!sContext) {
reed8b26b992015-05-07 15:36:17 -0700220 return false;
221 }
Robert Phillipsb726d582017-03-09 16:36:32 -0500222
223 if (!sContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, flags)) {
224 return false;
225 }
226
reed8b26b992015-05-07 15:36:17 -0700227 // do we have to manually fix-up the alpha channel?
228 // src dst
229 // unpremul premul fix manually
230 // premul unpremul done by kUnpremul_PixelOpsFlag
231 // all other combos need to change.
232 //
233 // Should this be handled by Ganesh? todo:?
234 //
Matt Sarett03dd6d52017-01-23 12:15:09 -0500235 if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
236 apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes);
reed8b26b992015-05-07 15:36:17 -0700237 }
238 return true;
239}
240
reed7fb4f8b2016-03-11 04:33:52 -0800241sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
Brian Salomon63e79732017-05-15 21:23:13 -0400242 GrSurfaceDesc desc;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400243 desc.fOrigin = fProxy->origin();
reed7b6945b2015-09-24 00:50:58 -0700244 desc.fWidth = subset.width();
245 desc.fHeight = subset.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -0400246 desc.fConfig = fProxy->config();
reed7b6945b2015-09-24 00:50:58 -0700247
Robert Phillipsb726d582017-03-09 16:36:32 -0500248 sk_sp<GrSurfaceContext> sContext(fContext->contextPriv().makeDeferredSurfaceContext(
Robert Phillipse2f7d182016-12-15 09:23:05 -0500249 desc,
250 SkBackingFit::kExact,
251 fBudgeted));
252 if (!sContext) {
253 return nullptr;
254 }
255
Robert Phillipsb726d582017-03-09 16:36:32 -0500256 if (!sContext->copy(fProxy.get(), subset, SkIPoint::Make(0, 0))) {
Robert Phillipse2f7d182016-12-15 09:23:05 -0500257 return nullptr;
258 }
259
Robert Phillipsb726d582017-03-09 16:36:32 -0500260 // MDB: this call is okay bc we know 'sContext' was kExact
261 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID,
262 fAlphaType, sContext->asTextureProxyRef(),
Robert Phillipse2f7d182016-12-15 09:23:05 -0500263 fColorSpace, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700264}
265
reed8b26b992015-05-07 15:36:17 -0700266///////////////////////////////////////////////////////////////////////////////////////////////////
267
Greg Daniel7ef28f32017-04-20 16:41:55 +0000268static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx,
269 const GrBackendTexture& backendTex,
270 GrSurfaceOrigin origin,
brianosmandddbe382016-07-20 13:55:39 -0700271 SkAlphaType at, sk_sp<SkColorSpace> colorSpace,
272 GrWrapOwnership ownership,
reed7fb4f8b2016-03-11 04:33:52 -0800273 SkImage::TextureReleaseProc releaseProc,
274 SkImage::ReleaseContext releaseCtx) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000275 if (backendTex.width() <= 0 || backendTex.height() <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700276 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700277 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400278
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400279 sk_sp<GrTexture> tex = ctx->resourceProvider()->wrapBackendTexture(backendTex, ownership);
reed8b26b992015-05-07 15:36:17 -0700280 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700281 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700282 }
reedde499882015-06-18 13:41:40 -0700283 if (releaseProc) {
284 tex->setRelease(releaseProc, releaseCtx);
285 }
286
Brian Osman85d34b22017-05-10 12:06:26 -0400287 const SkBudgeted budgeted = SkBudgeted::kNo;
Robert Phillips066f0202017-07-25 10:16:35 -0400288 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(tex), origin));
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400289 return sk_make_sp<SkImage_Gpu>(ctx, kNeedNewImageUniqueID,
290 at, std::move(proxy), std::move(colorSpace), budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700291}
292
Greg Daniel94403452017-04-18 15:52:36 -0400293sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx,
294 const GrBackendTexture& tex, GrSurfaceOrigin origin,
295 SkAlphaType at, sk_sp<SkColorSpace> cs,
296 TextureReleaseProc releaseP, ReleaseContext releaseC) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000297 return new_wrapped_texture_common(ctx, tex, origin, at, std::move(cs), kBorrow_GrWrapOwnership,
298 releaseP, releaseC);
Greg Daniel94403452017-04-18 15:52:36 -0400299}
300
301sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx,
302 const GrBackendTexture& tex, GrSurfaceOrigin origin,
303 SkAlphaType at, sk_sp<SkColorSpace> cs) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000304 return new_wrapped_texture_common(ctx, tex, origin, at, std::move(cs), kAdopt_GrWrapOwnership,
305 nullptr, nullptr);
306}
307
308static GrBackendTexture make_backend_texture_from_handle(GrBackend backend,
309 int width, int height,
310 GrPixelConfig config,
311 GrBackendObject handle) {
Brian Salomon8fe24272017-07-07 12:56:11 -0400312 switch (backend) {
313 case kOpenGL_GrBackend: {
314 const GrGLTextureInfo* glInfo = (const GrGLTextureInfo*)(handle);
315 return GrBackendTexture(width, height, config, *glInfo);
316 }
Mike Reedd20b5c42017-06-14 06:03:10 -0400317#ifdef SK_VULKAN
Brian Salomon8fe24272017-07-07 12:56:11 -0400318 case kVulkan_GrBackend: {
319 const GrVkImageInfo* vkInfo = (const GrVkImageInfo*)(handle);
320 return GrBackendTexture(width, height, *vkInfo);
321 }
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000322#endif
Brian Salomon8fe24272017-07-07 12:56:11 -0400323 case kMock_GrBackend: {
324 const GrMockTextureInfo* mockInfo = (const GrMockTextureInfo*)(handle);
325 return GrBackendTexture(width, height, config, *mockInfo);
326 }
327 default:
328 return GrBackendTexture();
329 }
Greg Daniel94403452017-04-18 15:52:36 -0400330}
331
jbaumanb445a572016-06-09 13:24:48 -0700332static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace,
333 bool nv12,
334 const GrBackendObject yuvTextureHandles[],
335 const SkISize yuvSizes[],
brianosmandddbe382016-07-20 13:55:39 -0700336 GrSurfaceOrigin origin,
337 sk_sp<SkColorSpace> imageColorSpace) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800338 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700339
jbaumanb445a572016-06-09 13:24:48 -0700340 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 ||
341 yuvSizes[1].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700342 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700343 }
jbaumanb445a572016-06-09 13:24:48 -0700344 if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) {
345 return nullptr;
346 }
347
348 const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig;
349
Greg Daniel7ef28f32017-04-20 16:41:55 +0000350 GrBackend backend = ctx->contextPriv().getBackend();
351 GrBackendTexture yTex = make_backend_texture_from_handle(backend,
352 yuvSizes[0].fWidth,
353 yuvSizes[0].fHeight,
354 kConfig,
355 yuvTextureHandles[0]);
356 GrBackendTexture uTex = make_backend_texture_from_handle(backend,
357 yuvSizes[1].fWidth,
358 yuvSizes[1].fHeight,
359 kConfig,
360 yuvTextureHandles[1]);
bsalomon993a4212015-05-29 11:37:25 -0700361
Greg Daniel7ef28f32017-04-20 16:41:55 +0000362 sk_sp<GrTextureProxy> yProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, yTex, origin);
363 sk_sp<GrTextureProxy> uProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, uTex, origin);
364 sk_sp<GrTextureProxy> vProxy;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500365
jbaumanb445a572016-06-09 13:24:48 -0700366 if (nv12) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500367 vProxy = uProxy;
jbaumanb445a572016-06-09 13:24:48 -0700368 } else {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000369 GrBackendTexture vTex = make_backend_texture_from_handle(backend,
370 yuvSizes[2].fWidth,
371 yuvSizes[2].fHeight,
372 kConfig,
373 yuvTextureHandles[2]);
374 vProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, vTex, origin);
jbaumanb445a572016-06-09 13:24:48 -0700375 }
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500376 if (!yProxy || !uProxy || !vProxy) {
halcanary96fcdcc2015-08-27 07:41:13 -0700377 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700378 }
379
robertphillipsd4c741e2016-04-28 09:55:15 -0700380 const int width = yuvSizes[0].fWidth;
381 const int height = yuvSizes[0].fHeight;
robertphillipsaa19a5f2016-04-28 06:21:55 -0700382
robertphillipsd4c741e2016-04-28 09:55:15 -0700383 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400384 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400385 SkBackingFit::kExact,
386 width, height,
387 kRGBA_8888_GrPixelConfig,
388 std::move(imageColorSpace),
389 0,
390 origin));
391 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700392 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700393 }
394
395 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400396 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400397 paint.addColorFragmentProcessor(GrYUVEffect::MakeYUVToRGB(yProxy, uProxy, vProxy,
398 yuvSizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700399
Robert Phillipsb726d582017-03-09 16:36:32 -0500400 const SkRect rect = SkRect::MakeIWH(width, height);
robertphillipsc9a37062015-09-01 08:34:28 -0700401
Brian Salomon82f44312017-01-11 13:42:54 -0500402 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Robert Phillipse60ad622016-11-17 10:22:48 -0500403
Robert Phillips7ee385e2017-03-30 08:02:11 -0400404 if (!renderTargetContext->asSurfaceProxy()) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500405 return nullptr;
406 }
Robert Phillips7ee385e2017-03-30 08:02:11 -0400407 ctx->contextPriv().flushSurfaceWrites(renderTargetContext->asSurfaceProxy());
Robert Phillipsb726d582017-03-09 16:36:32 -0500408
409 // MDB: this call is okay bc we know 'renderTargetContext' was exact
410 return sk_make_sp<SkImage_Gpu>(ctx, kNeedNewImageUniqueID,
411 kOpaque_SkAlphaType, renderTargetContext->asTextureProxyRef(),
Robert Phillips75a475c2017-01-13 09:18:59 -0500412 renderTargetContext->refColorSpace(), budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700413}
reed56179002015-07-07 06:11:19 -0700414
jbaumanb445a572016-06-09 13:24:48 -0700415sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
416 const GrBackendObject yuvTextureHandles[3],
brianosmandddbe382016-07-20 13:55:39 -0700417 const SkISize yuvSizes[3], GrSurfaceOrigin origin,
418 sk_sp<SkColorSpace> imageColorSpace) {
419 return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin,
420 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700421}
422
423sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
424 const GrBackendObject yuvTextureHandles[2],
425 const SkISize yuvSizes[2],
brianosmandddbe382016-07-20 13:55:39 -0700426 GrSurfaceOrigin origin,
427 sk_sp<SkColorSpace> imageColorSpace) {
428 return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin,
429 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700430}
431
Robert Phillips3798c862017-03-27 11:08:16 -0400432static sk_sp<SkImage> create_image_from_maker(GrContext* context, GrTextureMaker* maker,
433 SkAlphaType at, uint32_t id,
Brian Osman041f7df2017-02-07 11:23:28 -0500434 SkColorSpace* dstColorSpace) {
435 sk_sp<SkColorSpace> texColorSpace;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400436 sk_sp<GrTextureProxy> proxy(maker->refTextureProxyForParams(
437 GrSamplerState::ClampNearest(), dstColorSpace, &texColorSpace, nullptr));
Robert Phillips3798c862017-03-27 11:08:16 -0400438 if (!proxy) {
Brian Osman041f7df2017-02-07 11:23:28 -0500439 return nullptr;
440 }
Robert Phillips3798c862017-03-27 11:08:16 -0400441 return sk_make_sp<SkImage_Gpu>(context, id, at,
442 std::move(proxy), std::move(texColorSpace), SkBudgeted::kNo);
Brian Osman041f7df2017-02-07 11:23:28 -0500443}
444
Brian Osman2c2bc112017-02-28 10:02:49 -0500445sk_sp<SkImage> SkImage::makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const {
Brian Osman041f7df2017-02-07 11:23:28 -0500446 if (!context) {
447 return nullptr;
448 }
Robert Phillips87444052017-06-23 14:09:30 -0400449 if (GrContext* incumbent = as_IB(this)->context()) {
450 return incumbent == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr;
Brian Osman041f7df2017-02-07 11:23:28 -0500451 }
452
Brian Osmandf7e0752017-04-26 16:20:28 -0400453 if (this->isLazyGenerated()) {
454 GrImageTextureMaker maker(context, this, kDisallow_CachingHint);
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400455 return create_image_from_maker(context, &maker, this->alphaType(),
456 this->uniqueID(), dstColorSpace);
Brian Osman041f7df2017-02-07 11:23:28 -0500457 }
458
459 if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) {
460 GrBitmapTextureMaker maker(context, *bmp);
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400461 return create_image_from_maker(context, &maker, this->alphaType(),
462 this->uniqueID(), dstColorSpace);
Brian Osman041f7df2017-02-07 11:23:28 -0500463 }
464 return nullptr;
465}
466
Brian Osman13dddce2017-05-09 13:19:50 -0400467sk_sp<SkImage> SkImage::MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> encoded,
468 bool buildMips, SkColorSpace* dstColorSpace) {
469 sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(std::move(encoded));
470 if (!codecImage) {
471 return nullptr;
472 }
473
474 // Some backends or drivers don't support (safely) moving resources between contexts
475 if (!context || !context->caps()->crossContextTextureSupport()) {
476 return codecImage;
477 }
478
479 // Turn the codec image into a GrTextureProxy
480 GrImageTextureMaker maker(context, codecImage.get(), kDisallow_CachingHint);
481 sk_sp<SkColorSpace> texColorSpace;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400482 GrSamplerState samplerState(
483 GrSamplerState::WrapMode::kClamp,
484 buildMips ? GrSamplerState::Filter::kMipMap : GrSamplerState::Filter::kBilerp);
485 sk_sp<GrTextureProxy> proxy(
486 maker.refTextureProxyForParams(samplerState, dstColorSpace, &texColorSpace, nullptr));
Brian Osman13dddce2017-05-09 13:19:50 -0400487 if (!proxy) {
488 return codecImage;
489 }
490
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400491 if (!proxy->instantiate(context->resourceProvider())) {
Brian Osman13dddce2017-05-09 13:19:50 -0400492 return codecImage;
493 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400494 sk_sp<GrTexture> texture = sk_ref_sp(proxy->priv().peekTexture());
Brian Osman13dddce2017-05-09 13:19:50 -0400495
496 // Flush any writes or uploads
497 context->contextPriv().prepareSurfaceForExternalIO(proxy.get());
498
499 sk_sp<GrSemaphore> sema = context->getGpu()->prepareTextureForCrossContextUsage(texture.get());
500
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400501 auto gen = GrBackendTextureImageGenerator::Make(std::move(texture), proxy->origin(),
Robert Phillips16d8ec62017-07-27 16:16:25 -0400502 std::move(sema), codecImage->alphaType(),
Brian Osman13dddce2017-05-09 13:19:50 -0400503 std::move(texColorSpace));
504 return SkImage::MakeFromGenerator(std::move(gen));
505}
506
Derek Sollenberger7a869872017-06-27 15:37:25 -0400507#if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26
Stan Iliev7e910df2017-06-02 10:29:21 -0400508sk_sp<SkImage> SkImage::MakeFromAHardwareBuffer(AHardwareBuffer* graphicBuffer, SkAlphaType at,
509 sk_sp<SkColorSpace> cs) {
510 auto gen = GrAHardwareBufferImageGenerator::Make(graphicBuffer, at, cs);
511 return SkImage::MakeFromGenerator(std::move(gen));
512}
513#endif
514
bsalomon634b4302016-07-12 18:11:17 -0700515sk_sp<SkImage> SkImage::makeNonTextureImage() const {
brianosman396fcdb2016-07-22 06:26:11 -0700516 if (!this->isTextureBacked()) {
bsalomon634b4302016-07-12 18:11:17 -0700517 return sk_ref_sp(const_cast<SkImage*>(this));
518 }
brianosman396fcdb2016-07-22 06:26:11 -0700519 SkImageInfo info = as_IB(this)->onImageInfo();
bsalomon634b4302016-07-12 18:11:17 -0700520 size_t rowBytes = info.minRowBytes();
Mike Reedcd284c52017-09-29 15:22:56 -0400521 size_t size = info.computeByteSize(rowBytes);
bsalomon634b4302016-07-12 18:11:17 -0700522 auto data = SkData::MakeUninitialized(size);
523 if (!data) {
524 return nullptr;
525 }
526 SkPixmap pm(info, data->writable_data(), rowBytes);
527 if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
528 return nullptr;
529 }
530 return MakeRasterData(info, data, rowBytes);
531}
532
reed56179002015-07-07 06:11:19 -0700533///////////////////////////////////////////////////////////////////////////////////////////////////
534
bsalomon4d516a62016-07-28 13:37:31 -0700535namespace {
536struct MipMapLevelData {
537 void* fPixelData;
538 size_t fRowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800539};
540
bsalomon4d516a62016-07-28 13:37:31 -0700541struct DeferredTextureImage {
Brian Osman7b8400d2016-11-08 17:08:54 -0500542 uint32_t fContextUniqueID;
543 // Right now, the destination color mode is only considered when generating mipmaps
544 SkDestinationSurfaceColorMode fColorMode;
bsalomon4d516a62016-07-28 13:37:31 -0700545 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace.
Brian Osman7b8400d2016-11-08 17:08:54 -0500546 int fWidth;
547 int fHeight;
548 SkColorType fColorType;
549 SkAlphaType fAlphaType;
550 void* fColorSpace;
551 size_t fColorSpaceSize;
Brian Osman7b8400d2016-11-08 17:08:54 -0500552 int fMipMapLevelCount;
bsalomon4d516a62016-07-28 13:37:31 -0700553 // The fMipMapLevelData array may contain more than 1 element.
554 // It contains fMipMapLevelCount elements.
555 // That means this struct's size is not known at compile-time.
Brian Osman7b8400d2016-11-08 17:08:54 -0500556 MipMapLevelData fMipMapLevelData[1];
bsalomon4d516a62016-07-28 13:37:31 -0700557};
558} // anonymous namespace
559
cblume33e0cb52016-08-30 12:09:23 -0700560static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
Eric Karla422d702016-11-30 11:09:29 -0800561 // There is a bug in the mipmap pre-generation logic in use in getDeferredTextureImageData.
562 // This can cause runaway memory leaks, so we are disabling this path until we can
563 // investigate further. crbug.com/669775
564 return false;
cblume33e0cb52016-08-30 12:09:23 -0700565}
566
567namespace {
568
569class DTIBufferFiller
570{
571public:
cblume921bc672016-09-22 05:25:26 -0700572 explicit DTIBufferFiller(char* bufferAsCharPtr)
573 : bufferAsCharPtr_(bufferAsCharPtr) {}
cblume33e0cb52016-08-30 12:09:23 -0700574
575 void fillMember(const void* source, size_t memberOffset, size_t size) {
cblume921bc672016-09-22 05:25:26 -0700576 memcpy(bufferAsCharPtr_ + memberOffset, source, size);
cblume33e0cb52016-08-30 12:09:23 -0700577 }
578
579private:
580
cblume921bc672016-09-22 05:25:26 -0700581 char* bufferAsCharPtr_;
cblume33e0cb52016-08-30 12:09:23 -0700582};
583}
584
585#define FILL_MEMBER(bufferFiller, member, source) \
586 bufferFiller.fillMember(source, \
587 offsetof(DeferredTextureImage, member), \
588 sizeof(DeferredTextureImage::member));
589
Eric Karl7a8c84c2017-06-12 10:05:49 -0700590static bool SupportsColorSpace(SkColorType colorType) {
591 switch (colorType) {
592 case kRGBA_8888_SkColorType:
593 case kBGRA_8888_SkColorType:
594 case kRGBA_F16_SkColorType:
595 return true;
596 default:
597 return false;
598 }
599}
600
bsalomon41b952c2016-03-11 06:46:33 -0800601size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
ericrkb4da01d2016-06-13 11:18:14 -0700602 const DeferredTextureImageUsageParams params[],
cblume33e0cb52016-08-30 12:09:23 -0700603 int paramCnt, void* buffer,
Eric Karl7a8c84c2017-06-12 10:05:49 -0700604 SkColorSpace* dstColorSpace,
605 SkColorType dstColorType) const {
Mike Reed7f1d0202017-05-08 16:13:39 -0400606 // Some quick-rejects where is makes no sense to return CPU data
607 // e.g.
608 // - texture backed
609 // - picture backed
610 //
611 if (this->isTextureBacked()) {
612 return 0;
613 }
614 if (as_IB(this)->onCanLazyGenerateOnGPU()) {
615 return 0;
616 }
617
Eric Karl7a8c84c2017-06-12 10:05:49 -0700618 bool supportsColorSpace = SupportsColorSpace(dstColorType);
619 // Quick reject if the caller requests a color space with an unsupported color type.
620 if (SkToBool(dstColorSpace) && !supportsColorSpace) {
621 return 0;
622 }
623
ericrkb4da01d2016-06-13 11:18:14 -0700624 // Extract relevant min/max values from the params array.
625 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
626 SkFilterQuality highestFilterQuality = params[0].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700627 bool useMipMaps = should_use_mip_maps(params[0]);
ericrkb4da01d2016-06-13 11:18:14 -0700628 for (int i = 1; i < paramCnt; ++i) {
629 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
630 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
631 if (highestFilterQuality < params[i].fQuality)
632 highestFilterQuality = params[i].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700633 useMipMaps |= should_use_mip_maps(params[i]);
ericrkb4da01d2016-06-13 11:18:14 -0700634 }
635
bsalomon41b952c2016-03-11 06:46:33 -0800636 const bool fillMode = SkToBool(buffer);
637 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
638 return 0;
639 }
640
ericrkb4da01d2016-06-13 11:18:14 -0700641 // Calculate scaling parameters.
642 bool isScaled = lowestPreScaleMipLevel != 0;
643
644 SkISize scaledSize;
645 if (isScaled) {
646 // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the
647 // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level.
648 scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(),
649 lowestPreScaleMipLevel - 1);
650 } else {
651 scaledSize = SkISize::Make(this->width(), this->height());
652 }
653
654 // We never want to scale at higher than SW medium quality, as SW medium matches GPU high.
655 SkFilterQuality scaleFilterQuality = highestFilterQuality;
656 if (scaleFilterQuality > kMedium_SkFilterQuality) {
657 scaleFilterQuality = kMedium_SkFilterQuality;
658 }
659
ericrkc429baf2016-03-24 15:35:45 -0700660 const int maxTextureSize = proxy.fCaps->maxTextureSize();
ericrkb4da01d2016-06-13 11:18:14 -0700661 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkc429baf2016-03-24 15:35:45 -0700662 return 0;
663 }
664
bsalomon41b952c2016-03-11 06:46:33 -0800665 SkAutoPixmapStorage pixmap;
666 SkImageInfo info;
667 size_t pixelSize = 0;
Mike Reed086a4272017-07-18 10:53:11 -0400668 if (!isScaled && this->peekPixels(&pixmap) && pixmap.info().colorType() == dstColorType) {
bsalomon41b952c2016-03-11 06:46:33 -0800669 info = pixmap.info();
Mike Reedcd284c52017-09-29 15:22:56 -0400670 pixelSize = SkAlign8(pixmap.computeByteSize());
Matt Sarettade76e92017-04-14 12:41:55 -0400671 if (!dstColorSpace) {
672 pixmap.setColorSpace(nullptr);
673 info = info.makeColorSpace(nullptr);
674 }
bsalomon41b952c2016-03-11 06:46:33 -0800675 } else {
Mike Reed7f1d0202017-05-08 16:13:39 -0400676 if (!this->isLazyGenerated() && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800677 return 0;
678 }
Brian Osman7992da32016-11-18 11:28:24 -0500679 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
680 // Generator backed image. Tweak info to trigger correct kind of decode.
Brian Osman7992da32016-11-18 11:28:24 -0500681 SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat(
Brian Osman61624f02016-12-09 14:51:59 -0500682 dstColorSpace, proxy.fCaps.get());
Brian Osman7992da32016-11-18 11:28:24 -0500683 info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(),
684 scaledSize.height());
Brian Osman7992da32016-11-18 11:28:24 -0500685 } else {
686 info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height());
Matt Sarettade76e92017-04-14 12:41:55 -0400687 if (!dstColorSpace) {
688 info = info.makeColorSpace(nullptr);
689 }
Brian Osman7992da32016-11-18 11:28:24 -0500690 }
Eric Karl7a8c84c2017-06-12 10:05:49 -0700691 // Force color type to be the requested type.
692 info = info.makeColorType(dstColorType);
bsalomon41b952c2016-03-11 06:46:33 -0800693 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
694 if (fillMode) {
Eric Karl7a8c84c2017-06-12 10:05:49 -0700695 // Always decode to N32 and convert to the requested type if necessary.
696 SkImageInfo decodeInfo = info.makeColorType(kN32_SkColorType);
697 SkAutoPixmapStorage decodePixmap;
698 decodePixmap.alloc(decodeInfo);
699
ericrkb4da01d2016-06-13 11:18:14 -0700700 if (isScaled) {
Eric Karl7a8c84c2017-06-12 10:05:49 -0700701 if (!this->scalePixels(decodePixmap, scaleFilterQuality,
ericrkb4da01d2016-06-13 11:18:14 -0700702 SkImage::kDisallow_CachingHint)) {
703 return 0;
704 }
705 } else {
Eric Karl7a8c84c2017-06-12 10:05:49 -0700706 if (!this->readPixels(decodePixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
ericrkb4da01d2016-06-13 11:18:14 -0700707 return 0;
708 }
bsalomon41b952c2016-03-11 06:46:33 -0800709 }
Eric Karl7a8c84c2017-06-12 10:05:49 -0700710
711 if (decodeInfo.colorType() != info.colorType()) {
712 pixmap.alloc(info);
713 // Convert and copy the decoded pixmap to the target pixmap.
714 decodePixmap.readPixels(pixmap.info(), pixmap.writable_addr(), pixmap.rowBytes(), 0,
715 0);
716 } else {
717 pixmap = std::move(decodePixmap);
718 }
bsalomon41b952c2016-03-11 06:46:33 -0800719 }
720 }
cblume2c052802016-05-31 09:55:08 -0700721 int mipMapLevelCount = 1;
cblume33e0cb52016-08-30 12:09:23 -0700722 if (useMipMaps) {
723 // SkMipMap only deals with the mipmap levels it generates, which does
724 // not include the base level.
725 // That means it generates and holds levels 1-x instead of 0-x.
726 // So the total mipmap level count is 1 more than what
727 // SkMipMap::ComputeLevelCount returns.
728 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1;
729
730 // We already initialized pixelSize to the size of the base level.
731 // SkMipMap will generate the extra mipmap levels. Their sizes need to
732 // be added to the total.
733 // Index 0 here does not refer to the base mipmap level -- it is
734 // SkMipMap's first generated mipmap level (level 1).
735 for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0;
736 currentMipMapLevelIndex--) {
737 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(),
738 currentMipMapLevelIndex);
brianosman32b5e702016-10-13 06:44:23 -0700739 SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight);
cblume33e0cb52016-08-30 12:09:23 -0700740 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr));
741 }
742 }
bsalomon41b952c2016-03-11 06:46:33 -0800743 size_t size = 0;
744 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
745 size += dtiSize;
cblume33e0cb52016-08-30 12:09:23 -0700746 size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData);
747 // We subtract 1 because DeferredTextureImage already includes the base
748 // level in its size
bsalomon41b952c2016-03-11 06:46:33 -0800749 size_t pixelOffset = size;
750 size += pixelSize;
bsalomon4d516a62016-07-28 13:37:31 -0700751 size_t colorSpaceOffset = 0;
752 size_t colorSpaceSize = 0;
Matt Sarett84c9cb72017-04-10 11:03:27 -0400753 SkColorSpaceTransferFn fn;
bsalomon4d516a62016-07-28 13:37:31 -0700754 if (info.colorSpace()) {
Matt Sarettade76e92017-04-14 12:41:55 -0400755 SkASSERT(dstColorSpace);
Eric Karl7a8c84c2017-06-12 10:05:49 -0700756 SkASSERT(supportsColorSpace);
bsalomon4d516a62016-07-28 13:37:31 -0700757 colorSpaceOffset = size;
758 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
759 size += colorSpaceSize;
Eric Karl7a8c84c2017-06-12 10:05:49 -0700760 } else if (supportsColorSpace && this->colorSpace() && this->colorSpace()->isNumericalTransferFn(&fn)) {
Matt Sarett84c9cb72017-04-10 11:03:27 -0400761 // In legacy mode, preserve the color space tag on the SkImage. This is only
762 // supported if the color space has a parametric transfer function.
763 SkASSERT(!dstColorSpace);
764 colorSpaceOffset = size;
765 colorSpaceSize = this->colorSpace()->writeToMemory(nullptr);
766 size += colorSpaceSize;
bsalomon4d516a62016-07-28 13:37:31 -0700767 }
bsalomon41b952c2016-03-11 06:46:33 -0800768 if (!fillMode) {
769 return size;
770 }
cblume921bc672016-09-22 05:25:26 -0700771 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer);
772 char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset;
773 void* pixels = pixelsAsCharPtr;
bsalomon41b952c2016-03-11 06:46:33 -0800774
cblume921bc672016-09-22 05:25:26 -0700775 memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))),
Mike Reedcd284c52017-09-29 15:22:56 -0400776 pixmap.addr(), pixmap.computeByteSize());
bsalomon41b952c2016-03-11 06:46:33 -0800777
Brian Osman6c15cc72016-10-17 09:51:37 -0400778 // If the context has sRGB support, and we're intending to render to a surface with an attached
779 // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB)
780 // aware mip-mapping.
Brian Osman7b8400d2016-11-08 17:08:54 -0500781 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
Brian Osman6c15cc72016-10-17 09:51:37 -0400782 if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) &&
783 info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) {
Eric Karl7a8c84c2017-06-12 10:05:49 -0700784 SkASSERT(supportsColorSpace);
Brian Osman7b8400d2016-11-08 17:08:54 -0500785 colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware;
Brian Osman6c15cc72016-10-17 09:51:37 -0400786 }
787
bsalomon41b952c2016-03-11 06:46:33 -0800788 SkASSERT(info == pixmap.info());
789 size_t rowBytes = pixmap.rowBytes();
cblume33e0cb52016-08-30 12:09:23 -0700790 static_assert(std::is_standard_layout<DeferredTextureImage>::value,
791 "offsetof, which we use below, requires the type have standard layout");
cblume921bc672016-09-22 05:25:26 -0700792 auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr};
Brian Osman7b8400d2016-11-08 17:08:54 -0500793 FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode);
cblume33e0cb52016-08-30 12:09:23 -0700794 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID);
795 int width = info.width();
796 FILL_MEMBER(dtiBufferFiller, fWidth, &width);
797 int height = info.height();
798 FILL_MEMBER(dtiBufferFiller, fHeight, &height);
799 SkColorType colorType = info.colorType();
800 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType);
801 SkAlphaType alphaType = info.alphaType();
802 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType);
cblume33e0cb52016-08-30 12:09:23 -0700803 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount);
cblume921bc672016-09-22 05:25:26 -0700804 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData),
cblume33e0cb52016-08-30 12:09:23 -0700805 &pixels, sizeof(pixels));
cblume921bc672016-09-22 05:25:26 -0700806 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes),
cblume33e0cb52016-08-30 12:09:23 -0700807 &rowBytes, sizeof(rowBytes));
bsalomon4d516a62016-07-28 13:37:31 -0700808 if (colorSpaceSize) {
cblume921bc672016-09-22 05:25:26 -0700809 void* colorSpace = bufferAsCharPtr + colorSpaceOffset;
cblume33e0cb52016-08-30 12:09:23 -0700810 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace);
811 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize);
Matt Sarett84c9cb72017-04-10 11:03:27 -0400812 if (info.colorSpace()) {
813 info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
814 } else {
815 SkASSERT(this->colorSpace() && this->colorSpace()->isNumericalTransferFn(&fn));
816 SkASSERT(!dstColorSpace);
817 this->colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
818 }
bsalomon4d516a62016-07-28 13:37:31 -0700819 } else {
cblume921bc672016-09-22 05:25:26 -0700820 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace),
cblume33e0cb52016-08-30 12:09:23 -0700821 0, sizeof(DeferredTextureImage::fColorSpace));
cblume921bc672016-09-22 05:25:26 -0700822 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize),
cblume33e0cb52016-08-30 12:09:23 -0700823 0, sizeof(DeferredTextureImage::fColorSpaceSize));
824 }
825
826 // Fill in the mipmap levels if they exist
Mike Reedcd284c52017-09-29 15:22:56 -0400827 char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.computeByteSize());
cblume33e0cb52016-08-30 12:09:23 -0700828
829 if (useMipMaps) {
cblume94ddf542016-09-16 10:07:32 -0700830 static_assert(std::is_standard_layout<MipMapLevelData>::value,
831 "offsetof, which we use below, requires the type have a standard layout");
cblume33e0cb52016-08-30 12:09:23 -0700832
Brian Osman7b8400d2016-11-08 17:08:54 -0500833 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
cblume33e0cb52016-08-30 12:09:23 -0700834 // SkMipMap holds only the mipmap levels it generates.
835 // A programmer can use the data they provided to SkMipMap::Build as level 0.
836 // So the SkMipMap provides levels 1-x but it stores them in its own
837 // range 0-(x-1).
838 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1;
839 generatedMipLevelIndex++) {
cblume33e0cb52016-08-30 12:09:23 -0700840 SkMipMap::Level mipLevel;
841 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
842
843 // Make sure the mipmap data is after the start of the buffer
cblume921bc672016-09-22 05:25:26 -0700844 SkASSERT(mipLevelPtr > bufferAsCharPtr);
cblume33e0cb52016-08-30 12:09:23 -0700845 // Make sure the mipmap data starts before the end of the buffer
cblume921bc672016-09-22 05:25:26 -0700846 SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700847 // Make sure the mipmap data ends before the end of the buffer
Mike Reedcd284c52017-09-29 15:22:56 -0400848 SkASSERT(mipLevelPtr + mipLevel.fPixmap.computeByteSize() <=
cblume921bc672016-09-22 05:25:26 -0700849 bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700850
Mike Reedcd284c52017-09-29 15:22:56 -0400851 memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.computeByteSize());
cblume33e0cb52016-08-30 12:09:23 -0700852
cblume921bc672016-09-22 05:25:26 -0700853 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
854 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
855 offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*));
cblume33e0cb52016-08-30 12:09:23 -0700856 size_t rowBytes = mipLevel.fPixmap.rowBytes();
cblume921bc672016-09-22 05:25:26 -0700857 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
858 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
859 offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes));
cblume33e0cb52016-08-30 12:09:23 -0700860
Mike Reedcd284c52017-09-29 15:22:56 -0400861 mipLevelPtr += SkAlign8(mipLevel.fPixmap.computeByteSize());
cblume33e0cb52016-08-30 12:09:23 -0700862 }
bsalomon4d516a62016-07-28 13:37:31 -0700863 }
bsalomon41b952c2016-03-11 06:46:33 -0800864 return size;
865}
866
867sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
868 SkBudgeted budgeted) {
869 if (!data) {
870 return nullptr;
871 }
872 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
873
Brian Salomon228482a2017-07-13 09:23:21 -0400874 if (!context || context->uniqueID() != dti->fContextUniqueID || context->abandoned()) {
bsalomon41b952c2016-03-11 06:46:33 -0800875 return nullptr;
876 }
cblume33e0cb52016-08-30 12:09:23 -0700877 int mipLevelCount = dti->fMipMapLevelCount;
878 SkASSERT(mipLevelCount >= 1);
bsalomon4d516a62016-07-28 13:37:31 -0700879 sk_sp<SkColorSpace> colorSpace;
880 if (dti->fColorSpaceSize) {
881 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize);
882 }
883 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
884 dti->fColorType, dti->fAlphaType, colorSpace);
Brian Osmanb92234a2017-01-25 14:13:00 +0000885 if (mipLevelCount == 1) {
886 SkPixmap pixmap;
887 pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData, dti->fMipMapLevelData[0].fRowBytes);
Matt Sarett84c9cb72017-04-10 11:03:27 -0400888
Matt Sarettdedac852017-05-12 10:56:49 -0400889 // Pass nullptr for the |dstColorSpace|. This opts in to more lenient color space
890 // verification. This is ok because we've already verified the color space in
891 // getDeferredTextureImageData().
892 sk_sp<GrTextureProxy> proxy(GrUploadPixmapToTextureProxy(
893 context->resourceProvider(), pixmap, budgeted, nullptr));
Brian Osmaned182d72017-03-20 11:03:55 -0400894 if (!proxy) {
895 return nullptr;
896 }
897 return sk_make_sp<SkImage_Gpu>(context, kNeedNewImageUniqueID, pixmap.alphaType(),
898 std::move(proxy), std::move(colorSpace), budgeted);
Brian Osmanb92234a2017-01-25 14:13:00 +0000899 } else {
900 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
901 for (int i = 0; i < mipLevelCount; i++) {
902 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
903 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
904 }
Brian Osman8ccbbb42017-01-20 14:08:04 -0500905
Brian Osmanb92234a2017-01-25 14:13:00 +0000906 return SkImage::MakeTextureFromMipMap(context, info, texels.get(),
907 mipLevelCount, SkBudgeted::kYes,
908 dti->fColorMode);
909 }
bsalomon41b952c2016-03-11 06:46:33 -0800910}
911
912///////////////////////////////////////////////////////////////////////////////////////////////////
913
cblume186d2d42016-06-03 11:17:42 -0700914sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
Robert Phillips590533f2017-07-11 14:22:35 -0400915 const GrMipLevel texels[], int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700916 SkBudgeted budgeted,
Brian Osman7b8400d2016-11-08 17:08:54 -0500917 SkDestinationSurfaceColorMode colorMode) {
Robert Phillips1119dc32017-04-11 12:54:57 -0400918 SkASSERT(mipLevelCount >= 1);
cblume186d2d42016-06-03 11:17:42 -0700919 if (!ctx) {
920 return nullptr;
921 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400922 // For images where the client is passing the mip data we require that all the mip levels have
923 // valid data.
924 for (int i = 0; i < mipLevelCount; ++i) {
925 if (!texels[i].fPixels) {
926 return nullptr;
927 }
928 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400929 sk_sp<GrTextureProxy> proxy(GrUploadMipMapToTextureProxy(ctx, info, texels, mipLevelCount,
930 colorMode));
931 if (!proxy) {
cblume186d2d42016-06-03 11:17:42 -0700932 return nullptr;
933 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400934
935 SkASSERT(proxy->priv().isExact());
936 return sk_make_sp<SkImage_Gpu>(ctx, kNeedNewImageUniqueID,
937 info.alphaType(), std::move(proxy),
938 info.refColorSpace(), budgeted);
cblume186d2d42016-06-03 11:17:42 -0700939}
Brian Osman63954c92017-03-14 12:07:12 -0400940
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400941sk_sp<SkImage> SkImage_Gpu::onMakeColorSpace(sk_sp<SkColorSpace> target, SkColorType,
Matt Sarett9f3dcb32017-05-04 08:53:32 -0400942 SkTransferFunctionBehavior premulBehavior) const {
943 if (SkTransferFunctionBehavior::kRespect == premulBehavior) {
944 // TODO: Implement this.
945 return nullptr;
946 }
947
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400948 sk_sp<SkColorSpace> srcSpace = fColorSpace;
949 if (!fColorSpace) {
950 if (target->isSRGB()) {
951 return sk_ref_sp(const_cast<SkImage*>((SkImage*)this));
952 }
953
954 srcSpace = SkColorSpace::MakeSRGB();
955 }
956
957 auto xform = GrNonlinearColorSpaceXformEffect::Make(srcSpace.get(), target.get());
Brian Osman63954c92017-03-14 12:07:12 -0400958 if (!xform) {
959 return sk_ref_sp(const_cast<SkImage_Gpu*>(this));
960 }
961
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400962 sk_sp<GrRenderTargetContext> renderTargetContext(fContext->makeDeferredRenderTargetContext(
Brian Osman63954c92017-03-14 12:07:12 -0400963 SkBackingFit::kExact, this->width(), this->height(), kRGBA_8888_GrPixelConfig, nullptr));
964 if (!renderTargetContext) {
965 return nullptr;
966 }
967
968 GrPaint paint;
969 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400970 paint.addColorTextureProcessor(fProxy, nullptr, SkMatrix::I());
Brian Osman63954c92017-03-14 12:07:12 -0400971 paint.addColorFragmentProcessor(std::move(xform));
972
973 const SkRect rect = SkRect::MakeIWH(this->width(), this->height());
974
975 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
976
Robert Phillips7ee385e2017-03-30 08:02:11 -0400977 if (!renderTargetContext->asTextureProxy()) {
Brian Osman63954c92017-03-14 12:07:12 -0400978 return nullptr;
979 }
980
981 // MDB: this call is okay bc we know 'renderTargetContext' was exact
982 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID,
983 fAlphaType, renderTargetContext->asTextureProxyRef(),
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400984 std::move(target), fBudgeted);
Brian Osman63954c92017-03-14 12:07:12 -0400985
986}
Brian Osman5bbd0762017-05-08 11:07:42 -0400987
988bool SkImage_Gpu::onIsValid(GrContext* context) const {
989 // The base class has already checked that context isn't abandoned (if it's not nullptr)
990 if (fContext->abandoned()) {
991 return false;
992 }
993
994 if (context && context != fContext) {
995 return false;
996 }
997
998 return true;
999}