blob: 17a0a25f5ac48055d997af0b2b431700dfb7a177 [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"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050022#include "GrProxyProvider.h"
Brian Osman11052242016-10-27 14:47:55 -040023#include "GrRenderTargetContext.h"
Brian Osman32342f02017-03-04 08:12:46 -050024#include "GrResourceProvider.h"
Brian Osmanfe3b5162017-03-02 15:09:20 -050025#include "GrSemaphore.h"
Eric Karl914a36b2017-10-12 12:44:50 -070026#include "GrSurfacePriv.h"
Brian Osmane8e54582016-11-28 10:06:27 -050027#include "GrTextureAdjuster.h"
Robert Phillips646e4292017-06-13 12:44:56 -040028#include "GrTexture.h"
Eric Karl914a36b2017-10-12 12:44:50 -070029#include "GrTexturePriv.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050030#include "GrTextureProxy.h"
Greg Daniele3204862018-04-16 11:24:10 -040031#include "GrTextureProxyPriv.h"
Robert Phillipsabf7b762018-03-21 12:13:37 -040032#include "gl/GrGLDefines.h"
Brian Osman63954c92017-03-14 12:07:12 -040033#include "effects/GrNonlinearColorSpaceXformEffect.h"
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050034#include "effects/GrYUVtoRGBEffect.h"
bsalomon993a4212015-05-29 11:37:25 -070035#include "SkCanvas.h"
reed262a71b2015-12-05 13:07:27 -080036#include "SkBitmapCache.h"
Brian Osman3b655982017-03-07 16:58:08 -050037#include "SkGr.h"
reed262a71b2015-12-05 13:07:27 -080038#include "SkImage_Gpu.h"
Brian Osman7992da32016-11-18 11:28:24 -050039#include "SkImageCacherator.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050040#include "SkImageInfoPriv.h"
ericrkb4da01d2016-06-13 11:18:14 -070041#include "SkMipMap.h"
reed6f1216a2015-08-04 08:10:13 -070042#include "SkPixelRef.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050043#include "SkReadPixelsRec.h"
Greg Danielbc54fad2018-02-09 16:40:32 -050044#include "SkTraceEvent.h"
bsalomon993a4212015-05-29 11:37:25 -070045
Robert Phillipsb726d582017-03-09 16:36:32 -050046SkImage_Gpu::SkImage_Gpu(GrContext* context, uint32_t uniqueID, SkAlphaType at,
47 sk_sp<GrTextureProxy> proxy,
48 sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted)
Robert Phillipsa108c922017-10-10 10:42:19 -040049 : INHERITED(proxy->worstCaseWidth(), proxy->worstCaseHeight(), uniqueID)
Robert Phillipsb726d582017-03-09 16:36:32 -050050 , fContext(context)
51 , fProxy(std::move(proxy))
52 , fAlphaType(at)
53 , fBudgeted(budgeted)
54 , fColorSpace(std::move(colorSpace))
55 , fAddedRasterVersionToCache(false) {
reedc9b5f8b2015-10-22 13:20:20 -070056}
piotaixrcef04f82014-07-14 07:48:04 -070057
reed6f1216a2015-08-04 08:10:13 -070058SkImage_Gpu::~SkImage_Gpu() {
59 if (fAddedRasterVersionToCache.load()) {
60 SkNotifyBitmapGenIDIsStale(this->uniqueID());
61 }
62}
63
brianosman396fcdb2016-07-22 06:26:11 -070064SkImageInfo SkImage_Gpu::onImageInfo() const {
Greg Daniel56008aa2018-03-14 15:33:42 -040065 return SkImageInfo::Make(fProxy->width(), fProxy->height(), this->onColorType(), fAlphaType,
66 fColorSpace);
67}
68
69SkColorType SkImage_Gpu::onColorType() const {
brianosman396fcdb2016-07-22 06:26:11 -070070 SkColorType ct;
Robert Phillipsb726d582017-03-09 16:36:32 -050071 if (!GrPixelConfigToColorType(fProxy->config(), &ct)) {
brianosman396fcdb2016-07-22 06:26:11 -070072 ct = kUnknown_SkColorType;
73 }
Greg Daniel56008aa2018-03-14 15:33:42 -040074 return ct;
brianosman396fcdb2016-07-22 06:26:11 -070075}
76
Brian Osman62517712017-04-26 16:26:39 -040077bool SkImage_Gpu::getROPixels(SkBitmap* dst, SkColorSpace*, CachingHint chint) const {
Robert Phillipsa6aef2a2018-04-11 10:41:20 -040078 if (!fContext->contextPriv().resourceProvider()) {
79 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
80 return false;
81 }
82
Brian Osman62517712017-04-26 16:26:39 -040083 // The SkColorSpace parameter "dstColorSpace" is really just a hint about how/where the bitmap
84 // will be used. The client doesn't expect that we convert to that color space, it's intended
85 // for codec-backed images, to drive our decoding heuristic. In theory we *could* read directly
86 // into that color space (to save the client some effort in whatever they're about to do), but
87 // that would make our use of the bitmap cache incorrect (or much less efficient, assuming we
88 // rolled the dstColorSpace into the key).
Mike Reed5fa3d6d2017-03-25 09:51:00 -040089 const auto desc = SkBitmapCacheDesc::Make(this);
90 if (SkBitmapCache::Find(desc, dst)) {
reed6f1216a2015-08-04 08:10:13 -070091 SkASSERT(dst->getGenerationID() == this->uniqueID());
92 SkASSERT(dst->isImmutable());
93 SkASSERT(dst->getPixels());
94 return true;
95 }
96
Mike Reed7a542c52017-04-11 12:03:44 -040097 SkBitmapCache::RecPtr rec = nullptr;
98 SkPixmap pmap;
99 if (kAllow_CachingHint == chint) {
Brian Osman62517712017-04-26 16:26:39 -0400100 rec = SkBitmapCache::Alloc(desc, this->onImageInfo(), &pmap);
Mike Reed7a542c52017-04-11 12:03:44 -0400101 if (!rec) {
102 return false;
103 }
104 } else {
Brian Osman62517712017-04-26 16:26:39 -0400105 if (!dst->tryAllocPixels(this->onImageInfo()) || !dst->peekPixels(&pmap)) {
Mike Reed7a542c52017-04-11 12:03:44 -0400106 return false;
107 }
reed8b26b992015-05-07 15:36:17 -0700108 }
Robert Phillipsb726d582017-03-09 16:36:32 -0500109
Brian Salomon2084ffa2017-03-27 17:32:18 -0400110 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext(
111 fProxy,
112 fColorSpace);
Robert Phillipsb726d582017-03-09 16:36:32 -0500113 if (!sContext) {
114 return false;
115 }
116
Mike Reed7a542c52017-04-11 12:03:44 -0400117 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), 0, 0)) {
reed8b26b992015-05-07 15:36:17 -0700118 return false;
119 }
reed6f1216a2015-08-04 08:10:13 -0700120
Mike Reed7a542c52017-04-11 12:03:44 -0400121 if (rec) {
122 SkBitmapCache::Add(std::move(rec), dst);
reed09553032015-11-23 12:32:16 -0800123 fAddedRasterVersionToCache.store(true);
124 }
reed8b26b992015-05-07 15:36:17 -0700125 return true;
126}
127
Robert Phillipsb726d582017-03-09 16:36:32 -0500128sk_sp<GrTextureProxy> SkImage_Gpu::asTextureProxyRef(GrContext* context,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400129 const GrSamplerState& params,
Robert Phillipsb726d582017-03-09 16:36:32 -0500130 SkColorSpace* dstColorSpace,
131 sk_sp<SkColorSpace>* texColorSpace,
132 SkScalar scaleAdjust[2]) const {
Robert Phillips30a38ff2017-03-22 10:31:11 -0400133 if (context != fContext) {
Robert Phillipsb726d582017-03-09 16:36:32 -0500134 SkASSERT(0);
135 return nullptr;
136 }
137
Greg Danielc77085d2017-11-01 16:38:48 -0400138 GrTextureAdjuster adjuster(fContext, fProxy, this->alphaType(), this->uniqueID(),
139 this->fColorSpace.get());
Brian Salomon2a943df2018-05-04 13:43:19 -0400140 return adjuster.refTextureProxyForParams(params, dstColorSpace, texColorSpace, scaleAdjust);
reed85d91782015-09-10 14:33:38 -0700141}
142
reed8b26b992015-05-07 15:36:17 -0700143static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
144 switch (info.colorType()) {
145 case kRGBA_8888_SkColorType:
146 case kBGRA_8888_SkColorType:
147 break;
148 default:
149 return; // nothing to do
150 }
151
152 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
153 // and in either case, the alpha-byte is always in the same place, so we can safely call
154 // SkPreMultiplyColor()
155 //
156 SkColor* row = (SkColor*)pixels;
157 for (int y = 0; y < info.height(); ++y) {
158 for (int x = 0; x < info.width(); ++x) {
159 row[x] = SkPreMultiplyColor(row[x]);
160 }
Brian Salomon9708af82018-02-05 12:57:10 -0500161 row = (SkColor*)((char*)(row) + rowBytes);
reed8b26b992015-05-07 15:36:17 -0700162 }
163}
164
Robert Phillipsba375a82018-04-11 10:08:06 -0400165#ifdef SK_SUPPORT_LEGACY_BACKEND_OBJECTS
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400166GrBackendObject SkImage_Gpu::onGetTextureHandle(bool flushPendingGrContextIO,
167 GrSurfaceOrigin* origin) const {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400168 SkASSERT(fProxy);
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400169
Greg Danielf2336e42018-01-23 16:38:14 -0500170 if (!fContext->contextPriv().resourceProvider() && !fProxy->priv().isInstantiated()) {
171 // This image was created with a DDL context and cannot be instantiated. Thus we return 0
172 // here which is considered invalid for all backends.
173 return 0;
174 }
175
176 if (GrSurfaceProxy::LazyState::kNot != fProxy->lazyInstantiationState()) {
177 SkASSERT(fContext->contextPriv().resourceProvider());
178 fProxy->priv().doLazyInstantiation(fContext->contextPriv().resourceProvider());
179 if (!fProxy->priv().isInstantiated()) {
180 // We failed to instantiate the lazy proxy. Thus we return 0 here which is considered
181 // invalid for all backends.
182 return 0;
183 }
184 }
185
Robert Phillips6be756b2018-01-16 15:07:54 -0500186 if (!fProxy->instantiate(fContext->contextPriv().resourceProvider())) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400187 return 0;
188 }
189
190 GrTexture* texture = fProxy->priv().peekTexture();
191
192 if (texture) {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400193 if (flushPendingGrContextIO) {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400194 fContext->contextPriv().prepareSurfaceForExternalIO(fProxy.get());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400195 }
196 if (origin) {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400197 *origin = fProxy->origin();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400198 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400199 return texture->getTextureHandle();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400200 }
201 return 0;
202}
Robert Phillipsba375a82018-04-11 10:08:06 -0400203#endif
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400204
Robert Phillipsc5509952018-04-04 15:54:55 -0400205GrBackendTexture SkImage_Gpu::onGetBackendTexture(bool flushPendingGrContextIO,
206 GrSurfaceOrigin* origin) const {
207 SkASSERT(fProxy);
208
Robert Phillipsa6aef2a2018-04-11 10:41:20 -0400209 if (!fContext->contextPriv().resourceProvider() && !fProxy->priv().isInstantiated()) {
210 // This image was created with a DDL context and cannot be instantiated.
211 return GrBackendTexture();
212 }
213
Robert Phillipsc5509952018-04-04 15:54:55 -0400214 if (!fProxy->instantiate(fContext->contextPriv().resourceProvider())) {
215 return GrBackendTexture(); // invalid
216 }
217
218 GrTexture* texture = fProxy->priv().peekTexture();
219
220 if (texture) {
221 if (flushPendingGrContextIO) {
222 fContext->contextPriv().prepareSurfaceForExternalIO(fProxy.get());
223 }
224 if (origin) {
225 *origin = fProxy->origin();
226 }
227 return texture->getBackendTexture();
228 }
229 return GrBackendTexture(); // invalid
230}
231
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400232GrTexture* SkImage_Gpu::onGetTexture() const {
233 GrTextureProxy* proxy = this->peekProxy();
234 if (!proxy) {
235 return nullptr;
236 }
237
Robert Phillipsa6aef2a2018-04-11 10:41:20 -0400238 if (!fContext->contextPriv().resourceProvider() && !fProxy->priv().isInstantiated()) {
239 // This image was created with a DDL context and cannot be instantiated.
240 return nullptr;
241 }
242
Robert Phillips6be756b2018-01-16 15:07:54 -0500243 if (!proxy->instantiate(fContext->contextPriv().resourceProvider())) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400244 return nullptr;
245 }
246
247 return proxy->priv().peekTexture();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400248}
249
Matt Sarett03dd6d52017-01-23 12:15:09 -0500250bool SkImage_Gpu::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
reed09553032015-11-23 12:32:16 -0800251 int srcX, int srcY, CachingHint) const {
Robert Phillipsa6aef2a2018-04-11 10:41:20 -0400252 if (!fContext->contextPriv().resourceProvider()) {
253 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
254 return false;
255 }
256
Matt Sarett03dd6d52017-01-23 12:15:09 -0500257 if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500258 return false;
259 }
260
Matt Sarett03dd6d52017-01-23 12:15:09 -0500261 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY);
262 if (!rec.trim(this->width(), this->height())) {
263 return false;
264 }
265
Robert Phillipsb726d582017-03-09 16:36:32 -0500266 // TODO: this seems to duplicate code in GrTextureContext::onReadPixels and
267 // GrRenderTargetContext::onReadPixels
reed8b26b992015-05-07 15:36:17 -0700268 uint32_t flags = 0;
Matt Sarett03dd6d52017-01-23 12:15:09 -0500269 if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) {
reed8b26b992015-05-07 15:36:17 -0700270 // let the GPU perform this transformation for us
Robert Phillipse78b7252017-04-06 07:59:41 -0400271 flags = GrContextPriv::kUnpremul_PixelOpsFlag;
reed8b26b992015-05-07 15:36:17 -0700272 }
Robert Phillipsb726d582017-03-09 16:36:32 -0500273
Brian Osmana8ac9242017-09-07 10:19:08 -0400274 // This hack allows us to call makeNonTextureImage on images with arbitrary color spaces.
275 // Otherwise, we'll be unable to create a render target context.
276 // TODO: This shouldn't be necessary - we need more robust support for images (and surfaces)
277 // with arbitrary color spaces. Unfortunately, this is one spot where we go from image to
278 // surface (rather than the opposite), and our lenient image rules break our (currently) more
279 // strict surface rules.
Brian Salomon366093f2018-02-13 09:25:22 -0500280 // GrSurfaceContext::readPixels does not make use of the context's color space. However, we
281 // don't allow creating a surface context for a sRGB GrPixelConfig unless the color space has
282 // sRGB gamma. So we choose null for non-SRGB GrPixelConfigs and sRGB for sRGB GrPixelConfigs.
Brian Osmana8ac9242017-09-07 10:19:08 -0400283 sk_sp<SkColorSpace> surfaceColorSpace = fColorSpace;
Mike Klein98e38e22018-01-12 15:59:53 +0000284 if (!flags) {
285 if (!dstInfo.colorSpace() ||
Brian Salomon366093f2018-02-13 09:25:22 -0500286 SkColorSpace::Equals(fColorSpace.get(), dstInfo.colorSpace())) {
287 if (GrPixelConfigIsSRGB(fProxy->config())) {
288 surfaceColorSpace = SkColorSpace::MakeSRGB();
289 } else {
290 surfaceColorSpace = nullptr;
291 }
Mike Klein98e38e22018-01-12 15:59:53 +0000292 }
Brian Osmana8ac9242017-09-07 10:19:08 -0400293 }
294
Brian Salomon2084ffa2017-03-27 17:32:18 -0400295 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext(
Brian Osmana8ac9242017-09-07 10:19:08 -0400296 fProxy, surfaceColorSpace);
Robert Phillipsb726d582017-03-09 16:36:32 -0500297 if (!sContext) {
reed8b26b992015-05-07 15:36:17 -0700298 return false;
299 }
Robert Phillipsb726d582017-03-09 16:36:32 -0500300
301 if (!sContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, flags)) {
302 return false;
303 }
304
reed8b26b992015-05-07 15:36:17 -0700305 // do we have to manually fix-up the alpha channel?
306 // src dst
307 // unpremul premul fix manually
308 // premul unpremul done by kUnpremul_PixelOpsFlag
309 // all other combos need to change.
310 //
311 // Should this be handled by Ganesh? todo:?
312 //
Matt Sarett03dd6d52017-01-23 12:15:09 -0500313 if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
314 apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes);
reed8b26b992015-05-07 15:36:17 -0700315 }
316 return true;
317}
318
reed7fb4f8b2016-03-11 04:33:52 -0800319sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
Brian Salomon63e79732017-05-15 21:23:13 -0400320 GrSurfaceDesc desc;
reed7b6945b2015-09-24 00:50:58 -0700321 desc.fWidth = subset.width();
322 desc.fHeight = subset.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -0400323 desc.fConfig = fProxy->config();
reed7b6945b2015-09-24 00:50:58 -0700324
Robert Phillipsb726d582017-03-09 16:36:32 -0500325 sk_sp<GrSurfaceContext> sContext(fContext->contextPriv().makeDeferredSurfaceContext(
Brian Salomon2a4f9832018-03-03 22:43:43 -0500326 desc, fProxy->origin(), GrMipMapped::kNo, SkBackingFit::kExact, fBudgeted));
Robert Phillipse2f7d182016-12-15 09:23:05 -0500327 if (!sContext) {
328 return nullptr;
329 }
330
Robert Phillipsb726d582017-03-09 16:36:32 -0500331 if (!sContext->copy(fProxy.get(), subset, SkIPoint::Make(0, 0))) {
Robert Phillipse2f7d182016-12-15 09:23:05 -0500332 return nullptr;
333 }
334
Robert Phillipsb726d582017-03-09 16:36:32 -0500335 // MDB: this call is okay bc we know 'sContext' was kExact
336 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID,
337 fAlphaType, sContext->asTextureProxyRef(),
Robert Phillipse2f7d182016-12-15 09:23:05 -0500338 fColorSpace, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700339}
340
reed8b26b992015-05-07 15:36:17 -0700341///////////////////////////////////////////////////////////////////////////////////////////////////
342
Greg Daniel7ef28f32017-04-20 16:41:55 +0000343static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx,
344 const GrBackendTexture& backendTex,
345 GrSurfaceOrigin origin,
brianosmandddbe382016-07-20 13:55:39 -0700346 SkAlphaType at, sk_sp<SkColorSpace> colorSpace,
347 GrWrapOwnership ownership,
reed7fb4f8b2016-03-11 04:33:52 -0800348 SkImage::TextureReleaseProc releaseProc,
349 SkImage::ReleaseContext releaseCtx) {
Greg Daniel66aebf32018-04-09 09:15:56 -0400350 if (!backendTex.isValid() || backendTex.width() <= 0 || backendTex.height() <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700351 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700352 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400353
Robert Phillipsadbe1322018-01-17 13:35:46 -0500354 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
Brian Salomon7578f3e2018-03-07 14:39:54 -0500355 sk_sp<GrTextureProxy> proxy = proxyProvider->wrapBackendTexture(backendTex, origin, ownership,
356 releaseProc, releaseCtx);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500357 if (!proxy) {
Robert Phillipse201ebc2018-01-17 18:12:50 +0000358 return nullptr;
359 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500360
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400361 return sk_make_sp<SkImage_Gpu>(ctx, kNeedNewImageUniqueID,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500362 at, std::move(proxy), std::move(colorSpace), SkBudgeted::kNo);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700363}
364
Greg Danielfaa095e2017-12-19 13:15:02 -0500365bool validate_backend_texture(GrContext* ctx, const GrBackendTexture& tex, GrPixelConfig* config,
Greg Danielf5d87582017-12-18 14:48:15 -0500366 SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs) {
Greg Daniel66aebf32018-04-09 09:15:56 -0400367 if (!tex.isValid()) {
368 return false;
369 }
Greg Danielf5d87582017-12-18 14:48:15 -0500370 // TODO: Create a SkImageColorInfo struct for color, alpha, and color space so we don't need to
371 // create a fake image info here.
372 SkImageInfo info = SkImageInfo::Make(1, 1, ct, at, cs);
373 if (!SkImageInfoIsValidAllowNumericalCS(info)) {
374 return false;
375 }
376
Greg Danielfaa095e2017-12-19 13:15:02 -0500377 return ctx->caps()->validateBackendTexture(tex, ct, config);
Greg Danielf5d87582017-12-18 14:48:15 -0500378}
379
380sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx,
381 const GrBackendTexture& tex, GrSurfaceOrigin origin,
382 SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs,
383 TextureReleaseProc releaseP, ReleaseContext releaseC) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500384 if (!ctx) {
385 return nullptr;
386 }
Greg Danielf5d87582017-12-18 14:48:15 -0500387 GrBackendTexture texCopy = tex;
Greg Danielfaa095e2017-12-19 13:15:02 -0500388 if (!validate_backend_texture(ctx, texCopy, &texCopy.fConfig, ct, at, cs)) {
Greg Danielf5d87582017-12-18 14:48:15 -0500389 return nullptr;
390 }
Brian Salomonbfd27492018-03-19 14:08:51 -0400391 return new_wrapped_texture_common(ctx, texCopy, origin, at, std::move(cs),
392 kBorrow_GrWrapOwnership, releaseP, releaseC);
Greg Danielf5d87582017-12-18 14:48:15 -0500393}
394
Greg Daniel94403452017-04-18 15:52:36 -0400395sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx,
396 const GrBackendTexture& tex, GrSurfaceOrigin origin,
Brian Salomon42409c22018-03-20 13:48:41 -0400397 SkColorType ct, SkAlphaType at,
398 sk_sp<SkColorSpace> cs) {
399 if (!ctx || !ctx->contextPriv().resourceProvider()) {
Greg Danielf2336e42018-01-23 16:38:14 -0500400 // We have a DDL context and we don't support adopted textures for them.
401 return nullptr;
402 }
Greg Danielf5d87582017-12-18 14:48:15 -0500403 GrBackendTexture texCopy = tex;
Greg Danielfaa095e2017-12-19 13:15:02 -0500404 if (!validate_backend_texture(ctx, texCopy, &texCopy.fConfig, ct, at, cs)) {
Greg Danielf5d87582017-12-18 14:48:15 -0500405 return nullptr;
406 }
Brian Salomon42409c22018-03-20 13:48:41 -0400407 return new_wrapped_texture_common(ctx, texCopy, origin, at, std::move(cs),
408 kAdopt_GrWrapOwnership, nullptr, nullptr);
Greg Danielf5d87582017-12-18 14:48:15 -0500409}
410
Brian Salomon6a426c12018-03-15 12:16:02 -0400411sk_sp<SkImage> SkImage_Gpu::MakeFromYUVTexturesCopyImpl(
412 GrContext* ctx, SkYUVColorSpace colorSpace, bool nv12,
413 const GrBackendTexture yuvBackendTexturesOrig[], GrSurfaceOrigin origin,
414 sk_sp<SkColorSpace> imageColorSpace) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500415 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
416
Brian Salomon6a426c12018-03-15 12:16:02 -0400417 GrBackendTexture yuvBackendTextures[]{
418 yuvBackendTexturesOrig[0],
419 yuvBackendTexturesOrig[1],
420 yuvBackendTexturesOrig[2],
421 };
422 auto ct = nv12 ? kRGBA_8888_SkColorType : kAlpha_8_SkColorType;
423 for (int i = 0; i < (nv12 ? 2 : 3); ++i) {
Brian Salomon3afdab12018-03-19 16:29:37 -0400424 if (!validate_backend_texture(ctx, yuvBackendTextures[i], &yuvBackendTextures[i].fConfig,
425 ct, kPremul_SkAlphaType, nullptr)) {
426 return nullptr;
Brian Salomon6a426c12018-03-15 12:16:02 -0400427 }
jbaumanb445a572016-06-09 13:24:48 -0700428 }
Brian Salomon7578f3e2018-03-07 14:39:54 -0500429 sk_sp<GrTextureProxy> yProxy = proxyProvider->wrapBackendTexture(yuvBackendTextures[0], origin);
430 sk_sp<GrTextureProxy> uProxy = proxyProvider->wrapBackendTexture(yuvBackendTextures[1], origin);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000431 sk_sp<GrTextureProxy> vProxy;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500432
jbaumanb445a572016-06-09 13:24:48 -0700433 if (nv12) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500434 vProxy = uProxy;
jbaumanb445a572016-06-09 13:24:48 -0700435 } else {
Brian Salomon7578f3e2018-03-07 14:39:54 -0500436 vProxy = proxyProvider->wrapBackendTexture(yuvBackendTextures[2], origin);
jbaumanb445a572016-06-09 13:24:48 -0700437 }
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500438 if (!yProxy || !uProxy || !vProxy) {
halcanary96fcdcc2015-08-27 07:41:13 -0700439 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700440 }
441
Brian Salomon6a426c12018-03-15 12:16:02 -0400442 const int width = yuvBackendTextures[0].width();
443 const int height = yuvBackendTextures[0].height();
robertphillipsaa19a5f2016-04-28 06:21:55 -0700444
robertphillipsd4c741e2016-04-28 09:55:15 -0700445 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500446 sk_sp<GrRenderTargetContext> renderTargetContext(
447 ctx->contextPriv().makeDeferredRenderTargetContext(
448 SkBackingFit::kExact, width, height, kRGBA_8888_GrPixelConfig,
449 std::move(imageColorSpace), 1, GrMipMapped::kNo, origin));
Brian Osman11052242016-10-27 14:47:55 -0400450 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700451 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700452 }
453
454 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400455 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon6a426c12018-03-15 12:16:02 -0400456 SkISize sizes[] = {{yuvBackendTextures[0].width(), yuvBackendTextures[0].height()},
457 {yuvBackendTextures[1].width(), yuvBackendTextures[1].height()},
458 {yuvBackendTextures[2].width(), yuvBackendTextures[2].height()}};
459 paint.addColorFragmentProcessor(
460 GrYUVtoRGBEffect::Make(yProxy, uProxy, vProxy, sizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700461
Robert Phillipsb726d582017-03-09 16:36:32 -0500462 const SkRect rect = SkRect::MakeIWH(width, height);
robertphillipsc9a37062015-09-01 08:34:28 -0700463
Brian Salomon82f44312017-01-11 13:42:54 -0500464 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Robert Phillipse60ad622016-11-17 10:22:48 -0500465
Robert Phillips7ee385e2017-03-30 08:02:11 -0400466 if (!renderTargetContext->asSurfaceProxy()) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500467 return nullptr;
468 }
Robert Phillips7ee385e2017-03-30 08:02:11 -0400469 ctx->contextPriv().flushSurfaceWrites(renderTargetContext->asSurfaceProxy());
Robert Phillipsb726d582017-03-09 16:36:32 -0500470
471 // MDB: this call is okay bc we know 'renderTargetContext' was exact
Brian Salomonf3569f02017-10-24 12:52:33 -0400472 return sk_make_sp<SkImage_Gpu>(ctx, kNeedNewImageUniqueID, kOpaque_SkAlphaType,
473 renderTargetContext->asTextureProxyRef(),
Robert Phillipsc25db632017-12-13 09:22:45 -0500474 renderTargetContext->colorSpaceInfo().refColorSpace(),
475 SkBudgeted::kYes);
476}
477
Brian Salomon6a426c12018-03-15 12:16:02 -0400478sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
479 const GrBackendTexture yuvTextures[3],
480 GrSurfaceOrigin origin,
481 sk_sp<SkColorSpace> imageColorSpace) {
482 return SkImage_Gpu::MakeFromYUVTexturesCopyImpl(ctx, colorSpace, false, yuvTextures,
483 origin, std::move(imageColorSpace));
bsalomon993a4212015-05-29 11:37:25 -0700484}
reed56179002015-07-07 06:11:19 -0700485
jbaumanb445a572016-06-09 13:24:48 -0700486sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400487 const GrBackendTexture nv12Textures[2],
brianosmandddbe382016-07-20 13:55:39 -0700488 GrSurfaceOrigin origin,
489 sk_sp<SkColorSpace> imageColorSpace) {
Brian Salomon6a426c12018-03-15 12:16:02 -0400490 return SkImage_Gpu::MakeFromYUVTexturesCopyImpl(ctx, colorSpace, true, nv12Textures,
491 origin, std::move(imageColorSpace));
Robert Phillipsc25db632017-12-13 09:22:45 -0500492}
493
Robert Phillips3798c862017-03-27 11:08:16 -0400494static sk_sp<SkImage> create_image_from_maker(GrContext* context, GrTextureMaker* maker,
495 SkAlphaType at, uint32_t id,
Brian Osman041f7df2017-02-07 11:23:28 -0500496 SkColorSpace* dstColorSpace) {
497 sk_sp<SkColorSpace> texColorSpace;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400498 sk_sp<GrTextureProxy> proxy(maker->refTextureProxyForParams(
499 GrSamplerState::ClampNearest(), dstColorSpace, &texColorSpace, nullptr));
Robert Phillips3798c862017-03-27 11:08:16 -0400500 if (!proxy) {
Brian Osman041f7df2017-02-07 11:23:28 -0500501 return nullptr;
502 }
Robert Phillips3798c862017-03-27 11:08:16 -0400503 return sk_make_sp<SkImage_Gpu>(context, id, at,
504 std::move(proxy), std::move(texColorSpace), SkBudgeted::kNo);
Brian Osman041f7df2017-02-07 11:23:28 -0500505}
506
Brian Osman2c2bc112017-02-28 10:02:49 -0500507sk_sp<SkImage> SkImage::makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const {
Brian Osman041f7df2017-02-07 11:23:28 -0500508 if (!context) {
509 return nullptr;
510 }
Robert Phillips87444052017-06-23 14:09:30 -0400511 if (GrContext* incumbent = as_IB(this)->context()) {
512 return incumbent == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr;
Brian Osman041f7df2017-02-07 11:23:28 -0500513 }
514
Brian Osmandf7e0752017-04-26 16:20:28 -0400515 if (this->isLazyGenerated()) {
516 GrImageTextureMaker maker(context, this, kDisallow_CachingHint);
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400517 return create_image_from_maker(context, &maker, this->alphaType(),
518 this->uniqueID(), dstColorSpace);
Brian Osman041f7df2017-02-07 11:23:28 -0500519 }
520
521 if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) {
522 GrBitmapTextureMaker maker(context, *bmp);
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400523 return create_image_from_maker(context, &maker, this->alphaType(),
524 this->uniqueID(), dstColorSpace);
Brian Osman041f7df2017-02-07 11:23:28 -0500525 }
526 return nullptr;
527}
528
Greg Daniel7278d682018-03-16 14:57:21 -0400529///////////////////////////////////////////////////////////////////////////////////////////////////
530
531/**
532 * This helper holds the normal hard ref for the Release proc as well as a hard ref on the DoneProc.
533 * Thus when a GrTexture is being released, it will unref both the ReleaseProc and DoneProc.
534 */
535class PromiseReleaseProcHelper : public GrReleaseProcHelper {
536public:
537 PromiseReleaseProcHelper(SkImage_Gpu::TextureReleaseProc releaseProc,
538 SkImage_Gpu::TextureContext context,
539 sk_sp<GrReleaseProcHelper> doneHelper)
540 : INHERITED(releaseProc, context)
541 , fDoneProcHelper(std::move(doneHelper)) {}
542
543 void weak_dispose() const override {
544 // Call the inherited weak_dispose first so that we call the ReleaseProc before the DoneProc
545 // if we hold the last ref to the DoneProc.
546 INHERITED::weak_dispose();
547 fDoneProcHelper.reset();
548 }
549
550private:
551 mutable sk_sp<GrReleaseProcHelper> fDoneProcHelper;
552
553 typedef GrReleaseProcHelper INHERITED;
554};
555
556/**
557 * This helper class manages the ref counting for the the ReleaseProc and DoneProc for promise
558 * images. It holds a weak ref on the ReleaseProc (hard refs are owned by GrTextures). The weak ref
559 * allows us to reuse an outstanding ReleaseProc (because we dropped our GrTexture but the GrTexture
560 * isn't done on the GPU) without needing to call FulfillProc again. It also holds a hard ref on the
561 * DoneProc. The idea is that after every flush we may call the ReleaseProc so that the client can
562 * free up their GPU memory if they want to. The life time of the DoneProc matches that of any
563 * outstanding ReleaseProc as well as the PromiseImageHelper. Thus we won't call the DoneProc until
564 * all ReleaseProcs are finished and we are finished with the PromiseImageHelper (i.e. won't call
565 * FulfillProc again).
566 */
Greg Daniela8d92112018-03-09 12:05:04 -0500567class PromiseImageHelper {
568public:
569 PromiseImageHelper(SkImage_Gpu::TextureFulfillProc fulFillProc,
570 SkImage_Gpu::TextureReleaseProc releaseProc,
Greg Daniel7278d682018-03-16 14:57:21 -0400571 SkImage_Gpu::PromiseDoneProc doneProc,
Greg Daniela8d92112018-03-09 12:05:04 -0500572 SkImage_Gpu::TextureContext context)
573 : fFulfillProc(fulFillProc)
574 , fReleaseProc(releaseProc)
Greg Daniel7278d682018-03-16 14:57:21 -0400575 , fContext(context)
576 , fDoneHelper(new GrReleaseProcHelper(doneProc, context)) {}
Greg Daniela8d92112018-03-09 12:05:04 -0500577
578 void reset() {
579 this->resetReleaseHelper();
Greg Daniel7278d682018-03-16 14:57:21 -0400580 fDoneHelper.reset();
Greg Daniela8d92112018-03-09 12:05:04 -0500581 }
582
Greg Daniel057627f2018-03-14 15:51:58 -0400583 sk_sp<GrTexture> getTexture(GrResourceProvider* resourceProvider, GrPixelConfig config) {
Greg Daniela8d92112018-03-09 12:05:04 -0500584 // Releases the promise helper if there are no outstanding hard refs. This means that we
585 // don't have any ReleaseProcs waiting to be called so we will need to do a fulfill.
586 if (fReleaseHelper && fReleaseHelper->weak_expired()) {
587 this->resetReleaseHelper();
588 }
589
590 sk_sp<GrTexture> tex;
591 if (!fReleaseHelper) {
592 fFulfillProc(fContext, &fBackendTex);
Greg Daniel057627f2018-03-14 15:51:58 -0400593 fBackendTex.fConfig = config;
Greg Daniela8d92112018-03-09 12:05:04 -0500594 if (!fBackendTex.isValid()) {
595 // Even though the GrBackendTexture is not valid, we must call the release
596 // proc to keep our contract of always calling Fulfill and Release in pairs.
597 fReleaseProc(fContext);
598 return sk_sp<GrTexture>();
599 }
600
601 tex = resourceProvider->wrapBackendTexture(fBackendTex, kBorrow_GrWrapOwnership);
602 if (!tex) {
603 // Even though the GrBackendTexture is not valid, we must call the release
604 // proc to keep our contract of always calling Fulfill and Release in pairs.
605 fReleaseProc(fContext);
606 return sk_sp<GrTexture>();
607 }
Greg Daniel7278d682018-03-16 14:57:21 -0400608 fReleaseHelper = new PromiseReleaseProcHelper(fReleaseProc, fContext, fDoneHelper);
Greg Daniela8d92112018-03-09 12:05:04 -0500609 // Take a weak ref
610 fReleaseHelper->weak_ref();
611 } else {
612 SkASSERT(fBackendTex.isValid());
613 tex = resourceProvider->wrapBackendTexture(fBackendTex, kBorrow_GrWrapOwnership);
614 if (!tex) {
615 // We weren't able to make a texture here, but since we are in this branch
616 // of the calls (promiseHelper.fReleaseHelper is valid) there is already a
617 // texture out there which will call the release proc so we don't need to
618 // call it here.
619 return sk_sp<GrTexture>();
620 }
621
622 SkAssertResult(fReleaseHelper->try_ref());
623 }
624 SkASSERT(tex);
625 // Pass the hard ref off to the texture
626 tex->setRelease(sk_sp<GrReleaseProcHelper>(fReleaseHelper));
627 return tex;
628 }
629
630private:
631 // Weak unrefs fReleaseHelper and sets it to null
632 void resetReleaseHelper() {
633 if (fReleaseHelper) {
634 fReleaseHelper->weak_unref();
635 fReleaseHelper = nullptr;
636 }
637 }
638
639 SkImage_Gpu::TextureFulfillProc fFulfillProc;
640 SkImage_Gpu::TextureReleaseProc fReleaseProc;
641 SkImage_Gpu::TextureContext fContext;
642
643 // We cache the GrBackendTexture so that if we deleted the GrTexture but the the release proc
644 // has yet not been called (this can happen on Vulkan), then we can create a new texture without
645 // needing to call the fulfill proc again.
646 GrBackendTexture fBackendTex;
647 // The fReleaseHelper is used to track a weak ref on the release proc. This helps us make sure
648 // we are always pairing fulfill and release proc calls correctly.
Greg Daniel7278d682018-03-16 14:57:21 -0400649 PromiseReleaseProcHelper* fReleaseHelper = nullptr;
650 // We don't want to call the fDoneHelper until we are done with the PromiseImageHelper and all
651 // ReleaseHelpers are finished. Thus we hold a hard ref here and we will pass a hard ref to each
652 // fReleaseHelper we make.
653 sk_sp<GrReleaseProcHelper> fDoneHelper;
Greg Daniela8d92112018-03-09 12:05:04 -0500654};
655
Robert Phillipsabf7b762018-03-21 12:13:37 -0400656static GrInternalSurfaceFlags get_flags_from_format(const GrBackendFormat& backendFormat) {
657 if (const GrGLenum* target = backendFormat.getGLTarget()) {
658 if (GR_GL_TEXTURE_RECTANGLE == *target || GR_GL_TEXTURE_EXTERNAL == *target) {
659 return GrInternalSurfaceFlags::kDoesNotSupportMipMaps |
Greg Danielb73a9f82018-05-02 15:06:47 -0400660 GrInternalSurfaceFlags::kIsGLTextureRectangleOrExternal;
Robert Phillipsabf7b762018-03-21 12:13:37 -0400661 }
662 }
663
664 return GrInternalSurfaceFlags::kNone;
665}
666
Greg Daniela8d92112018-03-09 12:05:04 -0500667sk_sp<SkImage> SkImage_Gpu::MakePromiseTexture(GrContext* context,
668 const GrBackendFormat& backendFormat,
669 int width,
670 int height,
671 GrMipMapped mipMapped,
672 GrSurfaceOrigin origin,
673 SkColorType colorType,
674 SkAlphaType alphaType,
675 sk_sp<SkColorSpace> colorSpace,
676 TextureFulfillProc textureFulfillProc,
677 TextureReleaseProc textureReleaseProc,
Greg Daniel7278d682018-03-16 14:57:21 -0400678 PromiseDoneProc promiseDoneProc,
Greg Daniela8d92112018-03-09 12:05:04 -0500679 TextureContext textureContext) {
680 if (!context) {
681 return nullptr;
682 }
683
684 if (width <= 0 || height <= 0) {
685 return nullptr;
686 }
687
Greg Daniel7278d682018-03-16 14:57:21 -0400688 if (!textureFulfillProc || !textureReleaseProc || !promiseDoneProc) {
Greg Daniela8d92112018-03-09 12:05:04 -0500689 return nullptr;
690 }
691
692 SkImageInfo info = SkImageInfo::Make(width, height, colorType, alphaType, colorSpace);
693 if (!SkImageInfoIsValidAllowNumericalCS(info)) {
694 return nullptr;
695 }
696 GrPixelConfig config = kUnknown_GrPixelConfig;
697 if (!context->caps()->getConfigFromBackendFormat(backendFormat, colorType, &config)) {
698 return nullptr;
699 }
700
701 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
702
703 GrSurfaceDesc desc;
704 desc.fWidth = width;
705 desc.fHeight = height;
706 desc.fConfig = config;
707
Greg Daniel7278d682018-03-16 14:57:21 -0400708 PromiseImageHelper promiseHelper(textureFulfillProc, textureReleaseProc, promiseDoneProc,
709 textureContext);
Greg Daniela8d92112018-03-09 12:05:04 -0500710
Robert Phillipsabf7b762018-03-21 12:13:37 -0400711 GrInternalSurfaceFlags formatFlags = get_flags_from_format(backendFormat);
712
Greg Danielb73a9f82018-05-02 15:06:47 -0400713 // Promise images always wrap resources. So if the promise image doesn't have mip maps, we
714 // cannot allocate mips for them and thus will need a copy to use a mipped image. We can't pass
715 // the fact that this creates a wrapped texture into createLazyProxy so we need to manually set
716 // in in the passed in flags.
717 if (GrMipMapped::kNo == mipMapped) {
718 formatFlags |= GrInternalSurfaceFlags::kDoesNotSupportMipMaps;
719 }
720
Greg Daniela8d92112018-03-09 12:05:04 -0500721 sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy(
Greg Daniel057627f2018-03-14 15:51:58 -0400722 [promiseHelper, config] (GrResourceProvider* resourceProvider) mutable {
Greg Daniela8d92112018-03-09 12:05:04 -0500723 if (!resourceProvider) {
724 promiseHelper.reset();
725 return sk_sp<GrTexture>();
726 }
727
Greg Daniel057627f2018-03-14 15:51:58 -0400728 return promiseHelper.getTexture(resourceProvider, config);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400729 }, desc, origin, mipMapped, formatFlags, SkBackingFit::kExact,
Greg Daniela8d92112018-03-09 12:05:04 -0500730 SkBudgeted::kNo, GrSurfaceProxy::LazyInstantiationType::kUninstantiate);
731
732 if (!proxy) {
733 return nullptr;
734 }
735
736 return sk_make_sp<SkImage_Gpu>(context, kNeedNewImageUniqueID, alphaType, std::move(proxy),
737 std::move(colorSpace), SkBudgeted::kNo);
738}
739
Greg Daniel7278d682018-03-16 14:57:21 -0400740///////////////////////////////////////////////////////////////////////////////////////////////////
741
Brian Osman13dddce2017-05-09 13:19:50 -0400742sk_sp<SkImage> SkImage::MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> encoded,
Brian Osman584b5012018-04-13 15:48:26 -0400743 bool buildMips, SkColorSpace* dstColorSpace,
744 bool limitToMaxTextureSize) {
Brian Osman13dddce2017-05-09 13:19:50 -0400745 sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(std::move(encoded));
746 if (!codecImage) {
747 return nullptr;
748 }
749
750 // Some backends or drivers don't support (safely) moving resources between contexts
751 if (!context || !context->caps()->crossContextTextureSupport()) {
752 return codecImage;
753 }
754
Brian Osman584b5012018-04-13 15:48:26 -0400755 if (limitToMaxTextureSize && (codecImage->width() > context->caps()->maxTextureSize() ||
756 codecImage->height() > context->caps()->maxTextureSize())) {
757 SkAutoPixmapStorage pmap;
758 SkImageInfo info = as_IB(codecImage)->onImageInfo();
759 if (!dstColorSpace) {
760 info = info.makeColorSpace(nullptr);
761 }
762 if (!pmap.tryAlloc(info) || !codecImage->readPixels(pmap, 0, 0, kDisallow_CachingHint)) {
763 return nullptr;
764 }
765 return MakeCrossContextFromPixmap(context, pmap, buildMips, dstColorSpace, true);
766 }
767
Brian Osman13dddce2017-05-09 13:19:50 -0400768 // Turn the codec image into a GrTextureProxy
769 GrImageTextureMaker maker(context, codecImage.get(), kDisallow_CachingHint);
770 sk_sp<SkColorSpace> texColorSpace;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400771 GrSamplerState samplerState(
772 GrSamplerState::WrapMode::kClamp,
773 buildMips ? GrSamplerState::Filter::kMipMap : GrSamplerState::Filter::kBilerp);
774 sk_sp<GrTextureProxy> proxy(
775 maker.refTextureProxyForParams(samplerState, dstColorSpace, &texColorSpace, nullptr));
Brian Osman13dddce2017-05-09 13:19:50 -0400776 if (!proxy) {
777 return codecImage;
778 }
779
Robert Phillips6be756b2018-01-16 15:07:54 -0500780 if (!proxy->instantiate(context->contextPriv().resourceProvider())) {
Brian Osman13dddce2017-05-09 13:19:50 -0400781 return codecImage;
782 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400783 sk_sp<GrTexture> texture = sk_ref_sp(proxy->priv().peekTexture());
Brian Osman13dddce2017-05-09 13:19:50 -0400784
785 // Flush any writes or uploads
786 context->contextPriv().prepareSurfaceForExternalIO(proxy.get());
787
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500788 GrGpu* gpu = context->contextPriv().getGpu();
789 sk_sp<GrSemaphore> sema = gpu->prepareTextureForCrossContextUsage(texture.get());
Brian Osman13dddce2017-05-09 13:19:50 -0400790
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400791 auto gen = GrBackendTextureImageGenerator::Make(std::move(texture), proxy->origin(),
Brian Osman052ef692018-03-27 09:56:31 -0400792 std::move(sema),
793 as_IB(codecImage)->onImageInfo().colorType(),
794 codecImage->alphaType(),
Brian Osman13dddce2017-05-09 13:19:50 -0400795 std::move(texColorSpace));
796 return SkImage::MakeFromGenerator(std::move(gen));
797}
798
Brian Osman584b5012018-04-13 15:48:26 -0400799sk_sp<SkImage> SkImage::MakeCrossContextFromPixmap(GrContext* context,
800 const SkPixmap& originalPixmap, bool buildMips,
801 SkColorSpace* dstColorSpace,
802 bool limitToMaxTextureSize) {
Brian Osman63bc48d2017-11-07 10:37:00 -0500803 // Some backends or drivers don't support (safely) moving resources between contexts
804 if (!context || !context->caps()->crossContextTextureSupport()) {
Brian Osman584b5012018-04-13 15:48:26 -0400805 return SkImage::MakeRasterCopy(originalPixmap);
Brian Osman63bc48d2017-11-07 10:37:00 -0500806 }
807
Greg Daniel9e788112018-02-08 14:29:37 -0500808 // If we don't have access to the resource provider and gpu (i.e. in a DDL context) we will not
809 // be able to make everything needed for a GPU CrossContext image. Thus return a raster copy
810 // instead.
811 if (!context->contextPriv().resourceProvider()) {
Brian Osman584b5012018-04-13 15:48:26 -0400812 return SkImage::MakeRasterCopy(originalPixmap);
Greg Daniel9e788112018-02-08 14:29:37 -0500813 }
814
Brian Osman584b5012018-04-13 15:48:26 -0400815 const SkPixmap* pixmap = &originalPixmap;
816 SkAutoPixmapStorage resized;
817 int maxTextureSize = context->caps()->maxTextureSize();
818 int maxDim = SkTMax(originalPixmap.width(), originalPixmap.height());
819 if (limitToMaxTextureSize && maxDim > maxTextureSize) {
820 float scale = static_cast<float>(maxTextureSize) / maxDim;
821 int newWidth = SkTMin(static_cast<int>(originalPixmap.width() * scale), maxTextureSize);
822 int newHeight = SkTMin(static_cast<int>(originalPixmap.height() * scale), maxTextureSize);
823 SkImageInfo info = originalPixmap.info().makeWH(newWidth, newHeight);
824 if (!resized.tryAlloc(info) || !originalPixmap.scalePixels(resized, kLow_SkFilterQuality)) {
825 return nullptr;
826 }
827 pixmap = &resized;
828 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500829 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Brian Osman63bc48d2017-11-07 10:37:00 -0500830 // Turn the pixmap into a GrTextureProxy
831 sk_sp<GrTextureProxy> proxy;
832 if (buildMips) {
833 SkBitmap bmp;
Brian Osman584b5012018-04-13 15:48:26 -0400834 bmp.installPixels(*pixmap);
Greg Daniela4ead652018-02-07 10:21:48 -0500835 proxy = proxyProvider->createMipMapProxyFromBitmap(bmp, dstColorSpace);
Brian Osman63bc48d2017-11-07 10:37:00 -0500836 } else {
Greg Danielbc54fad2018-02-09 16:40:32 -0500837 SkDestinationSurfaceColorMode colorMode = dstColorSpace
838 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
839 : SkDestinationSurfaceColorMode::kLegacy;
840
Brian Osman584b5012018-04-13 15:48:26 -0400841 if (SkImageInfoIsValid(pixmap->info(), colorMode)) {
842 ATRACE_ANDROID_FRAMEWORK("Upload Texture [%ux%u]", pixmap->width(), pixmap->height());
Greg Danielabadbee2018-03-20 12:09:09 -0400843 // We don't need a release proc on the data in pixmap since we know we are in a
844 // GrContext that has a resource provider. Thus the createTextureProxy call will
845 // immediately upload the data.
Brian Osman584b5012018-04-13 15:48:26 -0400846 sk_sp<SkImage> image = SkImage::MakeFromRaster(*pixmap, nullptr, nullptr);
Greg Danielabadbee2018-03-20 12:09:09 -0400847 proxy = proxyProvider->createTextureProxy(std::move(image), kNone_GrSurfaceFlags, 1,
848 SkBudgeted::kYes, SkBackingFit::kExact);
Greg Danielbc54fad2018-02-09 16:40:32 -0500849 }
Brian Osman63bc48d2017-11-07 10:37:00 -0500850 }
851
852 if (!proxy) {
Brian Osman584b5012018-04-13 15:48:26 -0400853 return SkImage::MakeRasterCopy(*pixmap);
Brian Osman63bc48d2017-11-07 10:37:00 -0500854 }
855
856 sk_sp<GrTexture> texture = sk_ref_sp(proxy->priv().peekTexture());
857
858 // Flush any writes or uploads
859 context->contextPriv().prepareSurfaceForExternalIO(proxy.get());
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500860 GrGpu* gpu = context->contextPriv().getGpu();
Brian Osman63bc48d2017-11-07 10:37:00 -0500861
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500862 sk_sp<GrSemaphore> sema = gpu->prepareTextureForCrossContextUsage(texture.get());
Brian Osman63bc48d2017-11-07 10:37:00 -0500863
864 auto gen = GrBackendTextureImageGenerator::Make(std::move(texture), proxy->origin(),
Brian Osman584b5012018-04-13 15:48:26 -0400865 std::move(sema), pixmap->colorType(),
866 pixmap->alphaType(),
867 pixmap->info().refColorSpace());
Brian Osman63bc48d2017-11-07 10:37:00 -0500868 return SkImage::MakeFromGenerator(std::move(gen));
869}
870
Derek Sollenberger7a869872017-06-27 15:37:25 -0400871#if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26
Stan Iliev7e910df2017-06-02 10:29:21 -0400872sk_sp<SkImage> SkImage::MakeFromAHardwareBuffer(AHardwareBuffer* graphicBuffer, SkAlphaType at,
873 sk_sp<SkColorSpace> cs) {
874 auto gen = GrAHardwareBufferImageGenerator::Make(graphicBuffer, at, cs);
875 return SkImage::MakeFromGenerator(std::move(gen));
876}
877#endif
878
reed56179002015-07-07 06:11:19 -0700879///////////////////////////////////////////////////////////////////////////////////////////////////
880
Eric Karl914a36b2017-10-12 12:44:50 -0700881bool SkImage::MakeBackendTextureFromSkImage(GrContext* ctx,
882 sk_sp<SkImage> image,
883 GrBackendTexture* backendTexture,
884 BackendTextureReleaseProc* releaseProc) {
885 if (!image || !ctx || !backendTexture || !releaseProc) {
886 return false;
887 }
888
889 // Ensure we have a texture backed image.
890 if (!image->isTextureBacked()) {
891 image = image->makeTextureImage(ctx, nullptr);
892 if (!image) {
893 return false;
894 }
895 }
896 GrTexture* texture = image->getTexture();
Eric Karl36591e52018-01-19 13:45:02 -0800897 if (!texture) {
898 // In context-loss cases, we may not have a texture.
899 return false;
900 }
Eric Karl914a36b2017-10-12 12:44:50 -0700901
902 // If the image's context doesn't match the provided context, fail.
903 if (texture->getContext() != ctx) {
904 return false;
905 }
906
907 // Flush any pending IO on the texture.
908 ctx->contextPriv().prepareSurfaceForExternalIO(as_IB(image)->peekProxy());
909 SkASSERT(!texture->surfacePriv().hasPendingIO());
910
911 // We must make a copy of the image if the image is not unique, if the GrTexture owned by the
912 // image is not unique, or if the texture wraps an external object.
913 if (!image->unique() || !texture->surfacePriv().hasUniqueRef() ||
914 texture->resourcePriv().refsWrappedObjects()) {
915 // onMakeSubset will always copy the image.
916 image = as_IB(image)->onMakeSubset(image->bounds());
917 if (!image) {
918 return false;
919 }
920
921 texture = image->getTexture();
Eric Karl36591e52018-01-19 13:45:02 -0800922 if (!texture) {
923 return false;
924 }
Eric Karl914a36b2017-10-12 12:44:50 -0700925
926 // Flush to ensure that the copy is completed before we return the texture.
927 ctx->contextPriv().prepareSurfaceForExternalIO(as_IB(image)->peekProxy());
928 SkASSERT(!texture->surfacePriv().hasPendingIO());
929 }
930
931 SkASSERT(!texture->resourcePriv().refsWrappedObjects());
932 SkASSERT(texture->surfacePriv().hasUniqueRef());
933 SkASSERT(image->unique());
934
935 // Take a reference to the GrTexture and release the image.
936 sk_sp<GrTexture> textureRef(SkSafeRef(texture));
937 image = nullptr;
938
939 // Steal the backend texture from the GrTexture, releasing the GrTexture in the process.
940 return GrTexture::StealBackendTexture(std::move(textureRef), backendTexture, releaseProc);
941}
942
943///////////////////////////////////////////////////////////////////////////////////////////////////
944
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400945sk_sp<SkImage> SkImage_Gpu::onMakeColorSpace(sk_sp<SkColorSpace> target, SkColorType,
Matt Sarett9f3dcb32017-05-04 08:53:32 -0400946 SkTransferFunctionBehavior premulBehavior) const {
947 if (SkTransferFunctionBehavior::kRespect == premulBehavior) {
948 // TODO: Implement this.
949 return nullptr;
950 }
951
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400952 sk_sp<SkColorSpace> srcSpace = fColorSpace;
953 if (!fColorSpace) {
954 if (target->isSRGB()) {
955 return sk_ref_sp(const_cast<SkImage*>((SkImage*)this));
956 }
957
958 srcSpace = SkColorSpace::MakeSRGB();
959 }
960
961 auto xform = GrNonlinearColorSpaceXformEffect::Make(srcSpace.get(), target.get());
Brian Osman63954c92017-03-14 12:07:12 -0400962 if (!xform) {
963 return sk_ref_sp(const_cast<SkImage_Gpu*>(this));
964 }
965
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500966 sk_sp<GrRenderTargetContext> renderTargetContext(
967 fContext->contextPriv().makeDeferredRenderTargetContext(
968 SkBackingFit::kExact, this->width(), this->height(),
969 kRGBA_8888_GrPixelConfig, nullptr));
Brian Osman63954c92017-03-14 12:07:12 -0400970 if (!renderTargetContext) {
971 return nullptr;
972 }
973
974 GrPaint paint;
975 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Osman2240be92017-10-18 13:15:13 -0400976 paint.addColorTextureProcessor(fProxy, SkMatrix::I());
Brian Osman63954c92017-03-14 12:07:12 -0400977 paint.addColorFragmentProcessor(std::move(xform));
978
979 const SkRect rect = SkRect::MakeIWH(this->width(), this->height());
980
981 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
982
Robert Phillips7ee385e2017-03-30 08:02:11 -0400983 if (!renderTargetContext->asTextureProxy()) {
Brian Osman63954c92017-03-14 12:07:12 -0400984 return nullptr;
985 }
986
987 // MDB: this call is okay bc we know 'renderTargetContext' was exact
988 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID,
989 fAlphaType, renderTargetContext->asTextureProxyRef(),
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400990 std::move(target), fBudgeted);
Brian Osman63954c92017-03-14 12:07:12 -0400991
992}
Brian Osman5bbd0762017-05-08 11:07:42 -0400993
994bool SkImage_Gpu::onIsValid(GrContext* context) const {
995 // The base class has already checked that context isn't abandoned (if it's not nullptr)
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500996 if (fContext->contextPriv().abandoned()) {
Brian Osman5bbd0762017-05-08 11:07:42 -0400997 return false;
998 }
999
1000 if (context && context != fContext) {
1001 return false;
1002 }
1003
1004 return true;
1005}