blob: 3458075321465ad2fc52698c8b6dda8ad0bf9929 [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
Brian Salomon4df00922017-09-07 16:34:11 +0000126 GrTextureAdjuster adjuster(fContext, fProxy, this->alphaType(), this->bounds(),
127 this->uniqueID(), this->fColorSpace.get());
128 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 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;
Robert Phillips3798c862017-03-27 11:08:16 -0400436 sk_sp<GrTextureProxy> proxy(maker->refTextureProxyForParams(GrSamplerParams::ClampNoFilter(),
437 dstColorSpace,
438 &texColorSpace, nullptr));
439 if (!proxy) {
Brian Osman041f7df2017-02-07 11:23:28 -0500440 return nullptr;
441 }
Robert Phillips3798c862017-03-27 11:08:16 -0400442 return sk_make_sp<SkImage_Gpu>(context, id, at,
443 std::move(proxy), std::move(texColorSpace), SkBudgeted::kNo);
Brian Osman041f7df2017-02-07 11:23:28 -0500444}
445
Brian Osman2c2bc112017-02-28 10:02:49 -0500446sk_sp<SkImage> SkImage::makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const {
Brian Osman041f7df2017-02-07 11:23:28 -0500447 if (!context) {
448 return nullptr;
449 }
Robert Phillips87444052017-06-23 14:09:30 -0400450 if (GrContext* incumbent = as_IB(this)->context()) {
451 return incumbent == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr;
Brian Osman041f7df2017-02-07 11:23:28 -0500452 }
453
Brian Osmandf7e0752017-04-26 16:20:28 -0400454 if (this->isLazyGenerated()) {
455 GrImageTextureMaker maker(context, this, kDisallow_CachingHint);
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400456 return create_image_from_maker(context, &maker, this->alphaType(),
457 this->uniqueID(), dstColorSpace);
Brian Osman041f7df2017-02-07 11:23:28 -0500458 }
459
460 if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) {
461 GrBitmapTextureMaker maker(context, *bmp);
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400462 return create_image_from_maker(context, &maker, this->alphaType(),
463 this->uniqueID(), dstColorSpace);
Brian Osman041f7df2017-02-07 11:23:28 -0500464 }
465 return nullptr;
466}
467
Brian Osman13dddce2017-05-09 13:19:50 -0400468sk_sp<SkImage> SkImage::MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> encoded,
469 bool buildMips, SkColorSpace* dstColorSpace) {
470 sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(std::move(encoded));
471 if (!codecImage) {
472 return nullptr;
473 }
474
475 // Some backends or drivers don't support (safely) moving resources between contexts
476 if (!context || !context->caps()->crossContextTextureSupport()) {
477 return codecImage;
478 }
479
480 // Turn the codec image into a GrTextureProxy
481 GrImageTextureMaker maker(context, codecImage.get(), kDisallow_CachingHint);
482 sk_sp<SkColorSpace> texColorSpace;
483 GrSamplerParams params(SkShader::kClamp_TileMode,
484 buildMips ? GrSamplerParams::kMipMap_FilterMode
485 : GrSamplerParams::kBilerp_FilterMode);
486 sk_sp<GrTextureProxy> proxy(maker.refTextureProxyForParams(params, dstColorSpace,
487 &texColorSpace, nullptr));
488 if (!proxy) {
489 return codecImage;
490 }
491
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400492 if (!proxy->instantiate(context->resourceProvider())) {
Brian Osman13dddce2017-05-09 13:19:50 -0400493 return codecImage;
494 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400495 sk_sp<GrTexture> texture = sk_ref_sp(proxy->priv().peekTexture());
Brian Osman13dddce2017-05-09 13:19:50 -0400496
497 // Flush any writes or uploads
498 context->contextPriv().prepareSurfaceForExternalIO(proxy.get());
499
500 sk_sp<GrSemaphore> sema = context->getGpu()->prepareTextureForCrossContextUsage(texture.get());
501
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400502 auto gen = GrBackendTextureImageGenerator::Make(std::move(texture), proxy->origin(),
Robert Phillips16d8ec62017-07-27 16:16:25 -0400503 std::move(sema), codecImage->alphaType(),
Brian Osman13dddce2017-05-09 13:19:50 -0400504 std::move(texColorSpace));
505 return SkImage::MakeFromGenerator(std::move(gen));
506}
507
Derek Sollenberger7a869872017-06-27 15:37:25 -0400508#if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26
Stan Iliev7e910df2017-06-02 10:29:21 -0400509sk_sp<SkImage> SkImage::MakeFromAHardwareBuffer(AHardwareBuffer* graphicBuffer, SkAlphaType at,
510 sk_sp<SkColorSpace> cs) {
511 auto gen = GrAHardwareBufferImageGenerator::Make(graphicBuffer, at, cs);
512 return SkImage::MakeFromGenerator(std::move(gen));
513}
514#endif
515
bsalomon634b4302016-07-12 18:11:17 -0700516sk_sp<SkImage> SkImage::makeNonTextureImage() const {
brianosman396fcdb2016-07-22 06:26:11 -0700517 if (!this->isTextureBacked()) {
bsalomon634b4302016-07-12 18:11:17 -0700518 return sk_ref_sp(const_cast<SkImage*>(this));
519 }
brianosman396fcdb2016-07-22 06:26:11 -0700520 SkImageInfo info = as_IB(this)->onImageInfo();
bsalomon634b4302016-07-12 18:11:17 -0700521 size_t rowBytes = info.minRowBytes();
522 size_t size = info.getSafeSize(rowBytes);
523 auto data = SkData::MakeUninitialized(size);
524 if (!data) {
525 return nullptr;
526 }
527 SkPixmap pm(info, data->writable_data(), rowBytes);
528 if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
529 return nullptr;
530 }
531 return MakeRasterData(info, data, rowBytes);
532}
533
reed56179002015-07-07 06:11:19 -0700534///////////////////////////////////////////////////////////////////////////////////////////////////
535
bsalomon4d516a62016-07-28 13:37:31 -0700536namespace {
537struct MipMapLevelData {
538 void* fPixelData;
539 size_t fRowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800540};
541
bsalomon4d516a62016-07-28 13:37:31 -0700542struct DeferredTextureImage {
Brian Osman7b8400d2016-11-08 17:08:54 -0500543 uint32_t fContextUniqueID;
544 // Right now, the destination color mode is only considered when generating mipmaps
545 SkDestinationSurfaceColorMode fColorMode;
bsalomon4d516a62016-07-28 13:37:31 -0700546 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace.
Brian Osman7b8400d2016-11-08 17:08:54 -0500547 int fWidth;
548 int fHeight;
549 SkColorType fColorType;
550 SkAlphaType fAlphaType;
551 void* fColorSpace;
552 size_t fColorSpaceSize;
Brian Osman7b8400d2016-11-08 17:08:54 -0500553 int fMipMapLevelCount;
bsalomon4d516a62016-07-28 13:37:31 -0700554 // The fMipMapLevelData array may contain more than 1 element.
555 // It contains fMipMapLevelCount elements.
556 // That means this struct's size is not known at compile-time.
Brian Osman7b8400d2016-11-08 17:08:54 -0500557 MipMapLevelData fMipMapLevelData[1];
bsalomon4d516a62016-07-28 13:37:31 -0700558};
559} // anonymous namespace
560
cblume33e0cb52016-08-30 12:09:23 -0700561static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
Eric Karla422d702016-11-30 11:09:29 -0800562 // There is a bug in the mipmap pre-generation logic in use in getDeferredTextureImageData.
563 // This can cause runaway memory leaks, so we are disabling this path until we can
564 // investigate further. crbug.com/669775
565 return false;
cblume33e0cb52016-08-30 12:09:23 -0700566}
567
568namespace {
569
570class DTIBufferFiller
571{
572public:
cblume921bc672016-09-22 05:25:26 -0700573 explicit DTIBufferFiller(char* bufferAsCharPtr)
574 : bufferAsCharPtr_(bufferAsCharPtr) {}
cblume33e0cb52016-08-30 12:09:23 -0700575
576 void fillMember(const void* source, size_t memberOffset, size_t size) {
cblume921bc672016-09-22 05:25:26 -0700577 memcpy(bufferAsCharPtr_ + memberOffset, source, size);
cblume33e0cb52016-08-30 12:09:23 -0700578 }
579
580private:
581
cblume921bc672016-09-22 05:25:26 -0700582 char* bufferAsCharPtr_;
cblume33e0cb52016-08-30 12:09:23 -0700583};
584}
585
586#define FILL_MEMBER(bufferFiller, member, source) \
587 bufferFiller.fillMember(source, \
588 offsetof(DeferredTextureImage, member), \
589 sizeof(DeferredTextureImage::member));
590
Eric Karl7a8c84c2017-06-12 10:05:49 -0700591static bool SupportsColorSpace(SkColorType colorType) {
592 switch (colorType) {
593 case kRGBA_8888_SkColorType:
594 case kBGRA_8888_SkColorType:
595 case kRGBA_F16_SkColorType:
596 return true;
597 default:
598 return false;
599 }
600}
601
bsalomon41b952c2016-03-11 06:46:33 -0800602size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
ericrkb4da01d2016-06-13 11:18:14 -0700603 const DeferredTextureImageUsageParams params[],
cblume33e0cb52016-08-30 12:09:23 -0700604 int paramCnt, void* buffer,
Eric Karl7a8c84c2017-06-12 10:05:49 -0700605 SkColorSpace* dstColorSpace,
606 SkColorType dstColorType) const {
Mike Reed7f1d0202017-05-08 16:13:39 -0400607 // Some quick-rejects where is makes no sense to return CPU data
608 // e.g.
609 // - texture backed
610 // - picture backed
611 //
612 if (this->isTextureBacked()) {
613 return 0;
614 }
615 if (as_IB(this)->onCanLazyGenerateOnGPU()) {
616 return 0;
617 }
618
Eric Karl7a8c84c2017-06-12 10:05:49 -0700619 bool supportsColorSpace = SupportsColorSpace(dstColorType);
620 // Quick reject if the caller requests a color space with an unsupported color type.
621 if (SkToBool(dstColorSpace) && !supportsColorSpace) {
622 return 0;
623 }
624
ericrkb4da01d2016-06-13 11:18:14 -0700625 // Extract relevant min/max values from the params array.
626 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
627 SkFilterQuality highestFilterQuality = params[0].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700628 bool useMipMaps = should_use_mip_maps(params[0]);
ericrkb4da01d2016-06-13 11:18:14 -0700629 for (int i = 1; i < paramCnt; ++i) {
630 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
631 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
632 if (highestFilterQuality < params[i].fQuality)
633 highestFilterQuality = params[i].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700634 useMipMaps |= should_use_mip_maps(params[i]);
ericrkb4da01d2016-06-13 11:18:14 -0700635 }
636
bsalomon41b952c2016-03-11 06:46:33 -0800637 const bool fillMode = SkToBool(buffer);
638 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
639 return 0;
640 }
641
ericrkb4da01d2016-06-13 11:18:14 -0700642 // Calculate scaling parameters.
643 bool isScaled = lowestPreScaleMipLevel != 0;
644
645 SkISize scaledSize;
646 if (isScaled) {
647 // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the
648 // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level.
649 scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(),
650 lowestPreScaleMipLevel - 1);
651 } else {
652 scaledSize = SkISize::Make(this->width(), this->height());
653 }
654
655 // We never want to scale at higher than SW medium quality, as SW medium matches GPU high.
656 SkFilterQuality scaleFilterQuality = highestFilterQuality;
657 if (scaleFilterQuality > kMedium_SkFilterQuality) {
658 scaleFilterQuality = kMedium_SkFilterQuality;
659 }
660
ericrkc429baf2016-03-24 15:35:45 -0700661 const int maxTextureSize = proxy.fCaps->maxTextureSize();
ericrkb4da01d2016-06-13 11:18:14 -0700662 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkc429baf2016-03-24 15:35:45 -0700663 return 0;
664 }
665
bsalomon41b952c2016-03-11 06:46:33 -0800666 SkAutoPixmapStorage pixmap;
667 SkImageInfo info;
668 size_t pixelSize = 0;
Mike Reed086a4272017-07-18 10:53:11 -0400669 if (!isScaled && this->peekPixels(&pixmap) && pixmap.info().colorType() == dstColorType) {
bsalomon41b952c2016-03-11 06:46:33 -0800670 info = pixmap.info();
671 pixelSize = SkAlign8(pixmap.getSafeSize());
Matt Sarettade76e92017-04-14 12:41:55 -0400672 if (!dstColorSpace) {
673 pixmap.setColorSpace(nullptr);
674 info = info.makeColorSpace(nullptr);
675 }
bsalomon41b952c2016-03-11 06:46:33 -0800676 } else {
Mike Reed7f1d0202017-05-08 16:13:39 -0400677 if (!this->isLazyGenerated() && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800678 return 0;
679 }
Brian Osman7992da32016-11-18 11:28:24 -0500680 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
681 // Generator backed image. Tweak info to trigger correct kind of decode.
Brian Osman7992da32016-11-18 11:28:24 -0500682 SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat(
Brian Osman61624f02016-12-09 14:51:59 -0500683 dstColorSpace, proxy.fCaps.get());
Brian Osman7992da32016-11-18 11:28:24 -0500684 info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(),
685 scaledSize.height());
Brian Osman7992da32016-11-18 11:28:24 -0500686 } else {
687 info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height());
Matt Sarettade76e92017-04-14 12:41:55 -0400688 if (!dstColorSpace) {
689 info = info.makeColorSpace(nullptr);
690 }
Brian Osman7992da32016-11-18 11:28:24 -0500691 }
Eric Karl7a8c84c2017-06-12 10:05:49 -0700692 // Force color type to be the requested type.
693 info = info.makeColorType(dstColorType);
bsalomon41b952c2016-03-11 06:46:33 -0800694 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
695 if (fillMode) {
Eric Karl7a8c84c2017-06-12 10:05:49 -0700696 // Always decode to N32 and convert to the requested type if necessary.
697 SkImageInfo decodeInfo = info.makeColorType(kN32_SkColorType);
698 SkAutoPixmapStorage decodePixmap;
699 decodePixmap.alloc(decodeInfo);
700
ericrkb4da01d2016-06-13 11:18:14 -0700701 if (isScaled) {
Eric Karl7a8c84c2017-06-12 10:05:49 -0700702 if (!this->scalePixels(decodePixmap, scaleFilterQuality,
ericrkb4da01d2016-06-13 11:18:14 -0700703 SkImage::kDisallow_CachingHint)) {
704 return 0;
705 }
706 } else {
Eric Karl7a8c84c2017-06-12 10:05:49 -0700707 if (!this->readPixels(decodePixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
ericrkb4da01d2016-06-13 11:18:14 -0700708 return 0;
709 }
bsalomon41b952c2016-03-11 06:46:33 -0800710 }
Eric Karl7a8c84c2017-06-12 10:05:49 -0700711
712 if (decodeInfo.colorType() != info.colorType()) {
713 pixmap.alloc(info);
714 // Convert and copy the decoded pixmap to the target pixmap.
715 decodePixmap.readPixels(pixmap.info(), pixmap.writable_addr(), pixmap.rowBytes(), 0,
716 0);
717 } else {
718 pixmap = std::move(decodePixmap);
719 }
bsalomon41b952c2016-03-11 06:46:33 -0800720 }
721 }
cblume2c052802016-05-31 09:55:08 -0700722 int mipMapLevelCount = 1;
cblume33e0cb52016-08-30 12:09:23 -0700723 if (useMipMaps) {
724 // SkMipMap only deals with the mipmap levels it generates, which does
725 // not include the base level.
726 // That means it generates and holds levels 1-x instead of 0-x.
727 // So the total mipmap level count is 1 more than what
728 // SkMipMap::ComputeLevelCount returns.
729 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1;
730
731 // We already initialized pixelSize to the size of the base level.
732 // SkMipMap will generate the extra mipmap levels. Their sizes need to
733 // be added to the total.
734 // Index 0 here does not refer to the base mipmap level -- it is
735 // SkMipMap's first generated mipmap level (level 1).
736 for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0;
737 currentMipMapLevelIndex--) {
738 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(),
739 currentMipMapLevelIndex);
brianosman32b5e702016-10-13 06:44:23 -0700740 SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight);
cblume33e0cb52016-08-30 12:09:23 -0700741 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr));
742 }
743 }
bsalomon41b952c2016-03-11 06:46:33 -0800744 size_t size = 0;
745 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
746 size += dtiSize;
cblume33e0cb52016-08-30 12:09:23 -0700747 size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData);
748 // We subtract 1 because DeferredTextureImage already includes the base
749 // level in its size
bsalomon41b952c2016-03-11 06:46:33 -0800750 size_t pixelOffset = size;
751 size += pixelSize;
bsalomon4d516a62016-07-28 13:37:31 -0700752 size_t colorSpaceOffset = 0;
753 size_t colorSpaceSize = 0;
Matt Sarett84c9cb72017-04-10 11:03:27 -0400754 SkColorSpaceTransferFn fn;
bsalomon4d516a62016-07-28 13:37:31 -0700755 if (info.colorSpace()) {
Matt Sarettade76e92017-04-14 12:41:55 -0400756 SkASSERT(dstColorSpace);
Eric Karl7a8c84c2017-06-12 10:05:49 -0700757 SkASSERT(supportsColorSpace);
bsalomon4d516a62016-07-28 13:37:31 -0700758 colorSpaceOffset = size;
759 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
760 size += colorSpaceSize;
Eric Karl7a8c84c2017-06-12 10:05:49 -0700761 } else if (supportsColorSpace && this->colorSpace() && this->colorSpace()->isNumericalTransferFn(&fn)) {
Matt Sarett84c9cb72017-04-10 11:03:27 -0400762 // In legacy mode, preserve the color space tag on the SkImage. This is only
763 // supported if the color space has a parametric transfer function.
764 SkASSERT(!dstColorSpace);
765 colorSpaceOffset = size;
766 colorSpaceSize = this->colorSpace()->writeToMemory(nullptr);
767 size += colorSpaceSize;
bsalomon4d516a62016-07-28 13:37:31 -0700768 }
bsalomon41b952c2016-03-11 06:46:33 -0800769 if (!fillMode) {
770 return size;
771 }
cblume921bc672016-09-22 05:25:26 -0700772 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer);
773 char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset;
774 void* pixels = pixelsAsCharPtr;
bsalomon41b952c2016-03-11 06:46:33 -0800775
cblume921bc672016-09-22 05:25:26 -0700776 memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))),
777 pixmap.addr(), pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800778
Brian Osman6c15cc72016-10-17 09:51:37 -0400779 // If the context has sRGB support, and we're intending to render to a surface with an attached
780 // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB)
781 // aware mip-mapping.
Brian Osman7b8400d2016-11-08 17:08:54 -0500782 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
Brian Osman6c15cc72016-10-17 09:51:37 -0400783 if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) &&
784 info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) {
Eric Karl7a8c84c2017-06-12 10:05:49 -0700785 SkASSERT(supportsColorSpace);
Brian Osman7b8400d2016-11-08 17:08:54 -0500786 colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware;
Brian Osman6c15cc72016-10-17 09:51:37 -0400787 }
788
bsalomon41b952c2016-03-11 06:46:33 -0800789 SkASSERT(info == pixmap.info());
790 size_t rowBytes = pixmap.rowBytes();
cblume33e0cb52016-08-30 12:09:23 -0700791 static_assert(std::is_standard_layout<DeferredTextureImage>::value,
792 "offsetof, which we use below, requires the type have standard layout");
cblume921bc672016-09-22 05:25:26 -0700793 auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr};
Brian Osman7b8400d2016-11-08 17:08:54 -0500794 FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode);
cblume33e0cb52016-08-30 12:09:23 -0700795 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID);
796 int width = info.width();
797 FILL_MEMBER(dtiBufferFiller, fWidth, &width);
798 int height = info.height();
799 FILL_MEMBER(dtiBufferFiller, fHeight, &height);
800 SkColorType colorType = info.colorType();
801 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType);
802 SkAlphaType alphaType = info.alphaType();
803 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType);
cblume33e0cb52016-08-30 12:09:23 -0700804 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount);
cblume921bc672016-09-22 05:25:26 -0700805 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData),
cblume33e0cb52016-08-30 12:09:23 -0700806 &pixels, sizeof(pixels));
cblume921bc672016-09-22 05:25:26 -0700807 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes),
cblume33e0cb52016-08-30 12:09:23 -0700808 &rowBytes, sizeof(rowBytes));
bsalomon4d516a62016-07-28 13:37:31 -0700809 if (colorSpaceSize) {
cblume921bc672016-09-22 05:25:26 -0700810 void* colorSpace = bufferAsCharPtr + colorSpaceOffset;
cblume33e0cb52016-08-30 12:09:23 -0700811 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace);
812 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize);
Matt Sarett84c9cb72017-04-10 11:03:27 -0400813 if (info.colorSpace()) {
814 info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
815 } else {
816 SkASSERT(this->colorSpace() && this->colorSpace()->isNumericalTransferFn(&fn));
817 SkASSERT(!dstColorSpace);
818 this->colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
819 }
bsalomon4d516a62016-07-28 13:37:31 -0700820 } else {
cblume921bc672016-09-22 05:25:26 -0700821 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace),
cblume33e0cb52016-08-30 12:09:23 -0700822 0, sizeof(DeferredTextureImage::fColorSpace));
cblume921bc672016-09-22 05:25:26 -0700823 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize),
cblume33e0cb52016-08-30 12:09:23 -0700824 0, sizeof(DeferredTextureImage::fColorSpaceSize));
825 }
826
827 // Fill in the mipmap levels if they exist
cblume921bc672016-09-22 05:25:26 -0700828 char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700829
830 if (useMipMaps) {
cblume94ddf542016-09-16 10:07:32 -0700831 static_assert(std::is_standard_layout<MipMapLevelData>::value,
832 "offsetof, which we use below, requires the type have a standard layout");
cblume33e0cb52016-08-30 12:09:23 -0700833
Brian Osman7b8400d2016-11-08 17:08:54 -0500834 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
cblume33e0cb52016-08-30 12:09:23 -0700835 // SkMipMap holds only the mipmap levels it generates.
836 // A programmer can use the data they provided to SkMipMap::Build as level 0.
837 // So the SkMipMap provides levels 1-x but it stores them in its own
838 // range 0-(x-1).
839 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1;
840 generatedMipLevelIndex++) {
cblume33e0cb52016-08-30 12:09:23 -0700841 SkMipMap::Level mipLevel;
842 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
843
844 // Make sure the mipmap data is after the start of the buffer
cblume921bc672016-09-22 05:25:26 -0700845 SkASSERT(mipLevelPtr > bufferAsCharPtr);
cblume33e0cb52016-08-30 12:09:23 -0700846 // Make sure the mipmap data starts before the end of the buffer
cblume921bc672016-09-22 05:25:26 -0700847 SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700848 // Make sure the mipmap data ends before the end of the buffer
cblume12f75272016-09-19 06:18:03 -0700849 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <=
cblume921bc672016-09-22 05:25:26 -0700850 bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700851
852 // getSafeSize includes rowbyte padding except for the last row,
853 // right?
854
cblume921bc672016-09-22 05:25:26 -0700855 memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700856
cblume921bc672016-09-22 05:25:26 -0700857 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
858 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
859 offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*));
cblume33e0cb52016-08-30 12:09:23 -0700860 size_t rowBytes = mipLevel.fPixmap.rowBytes();
cblume921bc672016-09-22 05:25:26 -0700861 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
862 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
863 offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes));
cblume33e0cb52016-08-30 12:09:23 -0700864
865 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize());
866 }
bsalomon4d516a62016-07-28 13:37:31 -0700867 }
bsalomon41b952c2016-03-11 06:46:33 -0800868 return size;
869}
870
871sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
872 SkBudgeted budgeted) {
873 if (!data) {
874 return nullptr;
875 }
876 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
877
Brian Salomon228482a2017-07-13 09:23:21 -0400878 if (!context || context->uniqueID() != dti->fContextUniqueID || context->abandoned()) {
bsalomon41b952c2016-03-11 06:46:33 -0800879 return nullptr;
880 }
cblume33e0cb52016-08-30 12:09:23 -0700881 int mipLevelCount = dti->fMipMapLevelCount;
882 SkASSERT(mipLevelCount >= 1);
bsalomon4d516a62016-07-28 13:37:31 -0700883 sk_sp<SkColorSpace> colorSpace;
884 if (dti->fColorSpaceSize) {
885 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize);
886 }
887 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
888 dti->fColorType, dti->fAlphaType, colorSpace);
Brian Osmanb92234a2017-01-25 14:13:00 +0000889 if (mipLevelCount == 1) {
890 SkPixmap pixmap;
891 pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData, dti->fMipMapLevelData[0].fRowBytes);
Matt Sarett84c9cb72017-04-10 11:03:27 -0400892
Matt Sarettdedac852017-05-12 10:56:49 -0400893 // Pass nullptr for the |dstColorSpace|. This opts in to more lenient color space
894 // verification. This is ok because we've already verified the color space in
895 // getDeferredTextureImageData().
896 sk_sp<GrTextureProxy> proxy(GrUploadPixmapToTextureProxy(
897 context->resourceProvider(), pixmap, budgeted, nullptr));
Brian Osmaned182d72017-03-20 11:03:55 -0400898 if (!proxy) {
899 return nullptr;
900 }
901 return sk_make_sp<SkImage_Gpu>(context, kNeedNewImageUniqueID, pixmap.alphaType(),
902 std::move(proxy), std::move(colorSpace), budgeted);
Brian Osmanb92234a2017-01-25 14:13:00 +0000903 } else {
904 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
905 for (int i = 0; i < mipLevelCount; i++) {
906 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
907 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
908 }
Brian Osman8ccbbb42017-01-20 14:08:04 -0500909
Brian Osmanb92234a2017-01-25 14:13:00 +0000910 return SkImage::MakeTextureFromMipMap(context, info, texels.get(),
911 mipLevelCount, SkBudgeted::kYes,
912 dti->fColorMode);
913 }
bsalomon41b952c2016-03-11 06:46:33 -0800914}
915
916///////////////////////////////////////////////////////////////////////////////////////////////////
917
cblume186d2d42016-06-03 11:17:42 -0700918sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
Robert Phillips590533f2017-07-11 14:22:35 -0400919 const GrMipLevel texels[], int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700920 SkBudgeted budgeted,
Brian Osman7b8400d2016-11-08 17:08:54 -0500921 SkDestinationSurfaceColorMode colorMode) {
Robert Phillips1119dc32017-04-11 12:54:57 -0400922 SkASSERT(mipLevelCount >= 1);
cblume186d2d42016-06-03 11:17:42 -0700923 if (!ctx) {
924 return nullptr;
925 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400926 sk_sp<GrTextureProxy> proxy(GrUploadMipMapToTextureProxy(ctx, info, texels, mipLevelCount,
927 colorMode));
928 if (!proxy) {
cblume186d2d42016-06-03 11:17:42 -0700929 return nullptr;
930 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400931
932 SkASSERT(proxy->priv().isExact());
933 return sk_make_sp<SkImage_Gpu>(ctx, kNeedNewImageUniqueID,
934 info.alphaType(), std::move(proxy),
935 info.refColorSpace(), budgeted);
cblume186d2d42016-06-03 11:17:42 -0700936}
Brian Osman63954c92017-03-14 12:07:12 -0400937
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400938sk_sp<SkImage> SkImage_Gpu::onMakeColorSpace(sk_sp<SkColorSpace> target, SkColorType,
Matt Sarett9f3dcb32017-05-04 08:53:32 -0400939 SkTransferFunctionBehavior premulBehavior) const {
940 if (SkTransferFunctionBehavior::kRespect == premulBehavior) {
941 // TODO: Implement this.
942 return nullptr;
943 }
944
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400945 sk_sp<SkColorSpace> srcSpace = fColorSpace;
946 if (!fColorSpace) {
947 if (target->isSRGB()) {
948 return sk_ref_sp(const_cast<SkImage*>((SkImage*)this));
949 }
950
951 srcSpace = SkColorSpace::MakeSRGB();
952 }
953
954 auto xform = GrNonlinearColorSpaceXformEffect::Make(srcSpace.get(), target.get());
Brian Osman63954c92017-03-14 12:07:12 -0400955 if (!xform) {
956 return sk_ref_sp(const_cast<SkImage_Gpu*>(this));
957 }
958
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400959 sk_sp<GrRenderTargetContext> renderTargetContext(fContext->makeDeferredRenderTargetContext(
Brian Osman63954c92017-03-14 12:07:12 -0400960 SkBackingFit::kExact, this->width(), this->height(), kRGBA_8888_GrPixelConfig, nullptr));
961 if (!renderTargetContext) {
962 return nullptr;
963 }
964
965 GrPaint paint;
966 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400967 paint.addColorTextureProcessor(fProxy, nullptr, SkMatrix::I());
Brian Osman63954c92017-03-14 12:07:12 -0400968 paint.addColorFragmentProcessor(std::move(xform));
969
970 const SkRect rect = SkRect::MakeIWH(this->width(), this->height());
971
972 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
973
Robert Phillips7ee385e2017-03-30 08:02:11 -0400974 if (!renderTargetContext->asTextureProxy()) {
Brian Osman63954c92017-03-14 12:07:12 -0400975 return nullptr;
976 }
977
978 // MDB: this call is okay bc we know 'renderTargetContext' was exact
979 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID,
980 fAlphaType, renderTargetContext->asTextureProxyRef(),
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400981 std::move(target), fBudgeted);
Brian Osman63954c92017-03-14 12:07:12 -0400982
983}
Brian Osman5bbd0762017-05-08 11:07:42 -0400984
985bool SkImage_Gpu::onIsValid(GrContext* context) const {
986 // The base class has already checked that context isn't abandoned (if it's not nullptr)
987 if (fContext->abandoned()) {
988 return false;
989 }
990
991 if (context && context != fContext) {
992 return false;
993 }
994
995 return true;
996}