blob: 3e8e4149aecd8a56aa26889d64d341cea482f2c0 [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
Brian Salomon7226c232018-07-30 13:13:17 -040012#include "GrAHardwareBufferImageGenerator.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000013#include "GrBackendSurface.h"
Brian Osman13dddce2017-05-09 13:19:50 -040014#include "GrBackendTextureImageGenerator.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050015#include "GrBitmapTextureMaker.h"
reed856e9d92015-09-30 12:21:45 -070016#include "GrCaps.h"
Robert Phillips79730ac2018-09-25 10:40:04 -040017#include "GrClip.h"
Brian Osman47c27512018-06-18 10:58:51 -040018#include "GrColorSpaceXform.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000019#include "GrContext.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050020#include "GrContextPriv.h"
Brian Osman2c2bc112017-02-28 10:02:49 -050021#include "GrGpu.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050022#include "GrImageTextureMaker.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050023#include "GrProxyProvider.h"
Brian Osman11052242016-10-27 14:47:55 -040024#include "GrRenderTargetContext.h"
Brian Osman32342f02017-03-04 08:12:46 -050025#include "GrResourceProvider.h"
Robert Phillips79730ac2018-09-25 10:40:04 -040026#include "GrResourceProviderPriv.h"
Brian Osmanfe3b5162017-03-02 15:09:20 -050027#include "GrSemaphore.h"
Eric Karl914a36b2017-10-12 12:44:50 -070028#include "GrSurfacePriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040029#include "GrTexture.h"
Brian Salomon7226c232018-07-30 13:13:17 -040030#include "GrTextureAdjuster.h"
Eric Karl914a36b2017-10-12 12:44:50 -070031#include "GrTexturePriv.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050032#include "GrTextureProxy.h"
Greg Daniele3204862018-04-16 11:24:10 -040033#include "GrTextureProxyPriv.h"
Brian Salomon7226c232018-07-30 13:13:17 -040034#include "SkAutoPixmapStorage.h"
reed262a71b2015-12-05 13:07:27 -080035#include "SkBitmapCache.h"
Brian Salomon7226c232018-07-30 13:13:17 -040036#include "SkCanvas.h"
Brian Osman3b655982017-03-07 16:58:08 -050037#include "SkGr.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050038#include "SkImageInfoPriv.h"
Brian Salomon7226c232018-07-30 13:13:17 -040039#include "SkImage_Gpu.h"
ericrkb4da01d2016-06-13 11:18:14 -070040#include "SkMipMap.h"
Brian Salomonbe5a0932018-12-10 10:03:26 -050041#include "SkScopeExit.h"
Greg Danielbc54fad2018-02-09 16:40:32 -050042#include "SkTraceEvent.h"
Brian Salomon7226c232018-07-30 13:13:17 -040043#include "effects/GrYUVtoRGBEffect.h"
44#include "gl/GrGLTexture.h"
bsalomon993a4212015-05-29 11:37:25 -070045
Brian Salomon8a8dd332018-05-24 14:08:31 -040046SkImage_Gpu::SkImage_Gpu(sk_sp<GrContext> context, uint32_t uniqueID, SkAlphaType at,
Brian Salomonf05e6d32018-12-20 08:41:41 -050047 sk_sp<GrTextureProxy> proxy, sk_sp<SkColorSpace> colorSpace)
Jim Van Verth8026ccc2018-10-04 13:10:39 -040048 : INHERITED(std::move(context), proxy->worstCaseWidth(), proxy->worstCaseHeight(), uniqueID,
Brian Salomonf05e6d32018-12-20 08:41:41 -050049 at, colorSpace)
Jim Van Verth8026ccc2018-10-04 13:10:39 -040050 , fProxy(std::move(proxy)) {}
piotaixrcef04f82014-07-14 07:48:04 -070051
Mike Reed30301c42018-07-19 09:39:21 -040052SkImage_Gpu::~SkImage_Gpu() {}
reed6f1216a2015-08-04 08:10:13 -070053
brianosman396fcdb2016-07-22 06:26:11 -070054SkImageInfo SkImage_Gpu::onImageInfo() const {
Brian Osman3a5ff102018-09-27 12:19:23 -040055 SkColorType colorType;
56 if (!GrPixelConfigToColorType(fProxy->config(), &colorType)) {
57 colorType = kUnknown_SkColorType;
brianosman396fcdb2016-07-22 06:26:11 -070058 }
Brian Osman3a5ff102018-09-27 12:19:23 -040059
60 return SkImageInfo::Make(fProxy->width(), fProxy->height(), colorType, fAlphaType, fColorSpace);
brianosman396fcdb2016-07-22 06:26:11 -070061}
62
Brian Osmanf48c9962019-01-14 11:15:50 -050063sk_sp<SkImage> SkImage_Gpu::onMakeColorTypeAndColorSpace(SkColorType targetCT,
64 sk_sp<SkColorSpace> targetCS) const {
Jim Van Verth3e4c2f32019-01-11 13:32:45 -050065 auto xform = GrColorSpaceXformEffect::Make(fColorSpace.get(), fAlphaType,
Brian Osmanf48c9962019-01-14 11:15:50 -050066 targetCS.get(), fAlphaType);
67 SkASSERT(xform || targetCT != this->colorType());
Jim Van Verth3e4c2f32019-01-11 13:32:45 -050068
69 sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef();
70
71 GrBackendFormat format = proxy->backendFormat().makeTexture2D();
72 if (!format.isValid()) {
73 return nullptr;
74 }
75
76 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips9da87e02019-02-04 13:26:26 -050077 fContext->priv().makeDeferredRenderTargetContextWithFallback(
Jim Van Verth3e4c2f32019-01-11 13:32:45 -050078 format, SkBackingFit::kExact, this->width(), this->height(),
Brian Osmanf48c9962019-01-14 11:15:50 -050079 SkColorType2GrPixelConfig(targetCT), nullptr));
Jim Van Verth3e4c2f32019-01-11 13:32:45 -050080 if (!renderTargetContext) {
81 return nullptr;
82 }
83
84 GrPaint paint;
85 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
86 paint.addColorTextureProcessor(std::move(proxy), SkMatrix::I());
Brian Osmanf48c9962019-01-14 11:15:50 -050087 if (xform) {
88 paint.addColorFragmentProcessor(std::move(xform));
89 }
Jim Van Verth3e4c2f32019-01-11 13:32:45 -050090
91 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
92 SkRect::MakeIWH(this->width(), this->height()));
93 if (!renderTargetContext->asTextureProxy()) {
94 return nullptr;
95 }
96
97 // MDB: this call is okay bc we know 'renderTargetContext' was exact
98 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, fAlphaType,
Brian Osmanf48c9962019-01-14 11:15:50 -050099 renderTargetContext->asTextureProxyRef(), std::move(targetCS));
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500100}
101
reed8b26b992015-05-07 15:36:17 -0700102///////////////////////////////////////////////////////////////////////////////////////////////////
103
Greg Daniel7ef28f32017-04-20 16:41:55 +0000104static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx,
105 const GrBackendTexture& backendTex,
106 GrSurfaceOrigin origin,
brianosmandddbe382016-07-20 13:55:39 -0700107 SkAlphaType at, sk_sp<SkColorSpace> colorSpace,
108 GrWrapOwnership ownership,
reed7fb4f8b2016-03-11 04:33:52 -0800109 SkImage::TextureReleaseProc releaseProc,
110 SkImage::ReleaseContext releaseCtx) {
Greg Daniel66aebf32018-04-09 09:15:56 -0400111 if (!backendTex.isValid() || backendTex.width() <= 0 || backendTex.height() <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700112 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700113 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400114
Robert Phillips9da87e02019-02-04 13:26:26 -0500115 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500116 sk_sp<GrTextureProxy> proxy =
117 proxyProvider->wrapBackendTexture(backendTex, origin, ownership, GrWrapCacheable::kNo,
118 kRead_GrIOType, releaseProc, releaseCtx);
Robert Phillipsadbe1322018-01-17 13:35:46 -0500119 if (!proxy) {
Robert Phillipse201ebc2018-01-17 18:12:50 +0000120 return nullptr;
121 }
Brian Salomon8a8dd332018-05-24 14:08:31 -0400122 return sk_make_sp<SkImage_Gpu>(sk_ref_sp(ctx), kNeedNewImageUniqueID, at, std::move(proxy),
Brian Salomonf05e6d32018-12-20 08:41:41 -0500123 std::move(colorSpace));
bsalomon6dc6f5f2015-06-18 09:12:16 -0700124}
125
Greg Danielf5d87582017-12-18 14:48:15 -0500126sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx,
127 const GrBackendTexture& tex, GrSurfaceOrigin origin,
128 SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs,
129 TextureReleaseProc releaseP, ReleaseContext releaseC) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500130 if (!ctx) {
131 return nullptr;
132 }
Greg Danielf5d87582017-12-18 14:48:15 -0500133 GrBackendTexture texCopy = tex;
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400134 if (!SkImage_GpuBase::ValidateBackendTexture(ctx, texCopy, &texCopy.fConfig, ct, at, cs)) {
Greg Danielf5d87582017-12-18 14:48:15 -0500135 return nullptr;
136 }
Brian Salomonbfd27492018-03-19 14:08:51 -0400137 return new_wrapped_texture_common(ctx, texCopy, origin, at, std::move(cs),
138 kBorrow_GrWrapOwnership, releaseP, releaseC);
Greg Danielf5d87582017-12-18 14:48:15 -0500139}
140
Greg Daniel94403452017-04-18 15:52:36 -0400141sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx,
142 const GrBackendTexture& tex, GrSurfaceOrigin origin,
Brian Salomon42409c22018-03-20 13:48:41 -0400143 SkColorType ct, SkAlphaType at,
144 sk_sp<SkColorSpace> cs) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500145 if (!ctx || !ctx->priv().resourceProvider()) {
Greg Danielf2336e42018-01-23 16:38:14 -0500146 // We have a DDL context and we don't support adopted textures for them.
147 return nullptr;
148 }
Greg Danielf5d87582017-12-18 14:48:15 -0500149 GrBackendTexture texCopy = tex;
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400150 if (!SkImage_GpuBase::ValidateBackendTexture(ctx, texCopy, &texCopy.fConfig, ct, at, cs)) {
Greg Danielf5d87582017-12-18 14:48:15 -0500151 return nullptr;
152 }
Brian Salomon42409c22018-03-20 13:48:41 -0400153 return new_wrapped_texture_common(ctx, texCopy, origin, at, std::move(cs),
154 kAdopt_GrWrapOwnership, nullptr, nullptr);
Greg Danielf5d87582017-12-18 14:48:15 -0500155}
156
Jim Van Verthee06b332019-01-18 10:36:32 -0500157sk_sp<SkImage> SkImage::MakeFromCompressed(GrContext* context, sk_sp<SkData> data,
158 int width, int height, CompressionType type) {
159 // create the backing texture
160 GrSurfaceDesc desc;
161 desc.fFlags = kNone_GrSurfaceFlags;
162 desc.fWidth = width;
163 desc.fHeight = height;
164 switch (type) {
165 case kETC1_CompressionType:
166 desc.fConfig = kRGB_ETC1_GrPixelConfig;
167 break;
168 default:
169 desc.fConfig = kUnknown_GrPixelConfig;
170 break;
171 }
172 desc.fSampleCnt = 1;
173
Robert Phillips9da87e02019-02-04 13:26:26 -0500174 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Jim Van Verthee06b332019-01-18 10:36:32 -0500175 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(std::move(data), desc);
176
177 if (!proxy) {
178 return nullptr;
179 }
180
181 return sk_make_sp<SkImage_Gpu>(sk_ref_sp(context), kNeedNewImageUniqueID, kOpaque_SkAlphaType,
182 std::move(proxy), nullptr);
183}
184
Brian Salomonf05e6d32018-12-20 08:41:41 -0500185sk_sp<SkImage> SkImage_Gpu::ConvertYUVATexturesToRGB(GrContext* ctx, SkYUVColorSpace yuvColorSpace,
186 const GrBackendTexture yuvaTextures[],
187 const SkYUVAIndex yuvaIndices[4], SkISize size,
188 GrSurfaceOrigin origin,
189 GrRenderTargetContext* renderTargetContext) {
Brian Salomond2fcfb52018-09-17 21:57:11 -0400190 SkASSERT(renderTargetContext);
191
Jim Van Verth0e671942018-11-09 12:03:57 -0500192 int numTextures;
193 if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures)) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500194 return nullptr;
195 }
Robert Phillipsba5c4392018-07-25 12:37:14 -0400196
Jim Van Verth0e671942018-11-09 12:03:57 -0500197 sk_sp<GrTextureProxy> tempTextureProxies[4];
Jim Van Verth53275362018-11-09 15:42:35 -0500198 if (!SkImage_GpuBase::MakeTempTextureProxies(ctx, yuvaTextures, numTextures, yuvaIndices,
199 origin, tempTextureProxies)) {
Jim Van Verth0e671942018-11-09 12:03:57 -0500200 return nullptr;
201 }
202
203 const SkRect rect = SkRect::MakeIWH(size.width(), size.height());
Brian Osmane9560492019-02-05 17:00:03 -0500204 if (!RenderYUVAToRGBA(ctx, renderTargetContext, rect, yuvColorSpace, nullptr,
Jim Van Verth0e671942018-11-09 12:03:57 -0500205 tempTextureProxies, yuvaIndices)) {
206 return nullptr;
207 }
Robert Phillipsb726d582017-03-09 16:36:32 -0500208
Jim Van Verth53275362018-11-09 15:42:35 -0500209 SkAlphaType at = GetAlphaTypeFromYUVAIndices(yuvaIndices);
Robert Phillipsb726d582017-03-09 16:36:32 -0500210 // MDB: this call is okay bc we know 'renderTargetContext' was exact
Jim Van Verth53275362018-11-09 15:42:35 -0500211 return sk_make_sp<SkImage_Gpu>(sk_ref_sp(ctx), kNeedNewImageUniqueID, at,
Brian Salomonf3569f02017-10-24 12:52:33 -0400212 renderTargetContext->asTextureProxyRef(),
Brian Salomonf05e6d32018-12-20 08:41:41 -0500213 renderTargetContext->colorSpaceInfo().refColorSpace());
Brian Salomond2fcfb52018-09-17 21:57:11 -0400214}
215
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400216sk_sp<SkImage> SkImage::MakeFromYUVATexturesCopy(GrContext* ctx,
217 SkYUVColorSpace yuvColorSpace,
218 const GrBackendTexture yuvaTextures[],
219 const SkYUVAIndex yuvaIndices[4],
220 SkISize imageSize,
221 GrSurfaceOrigin imageOrigin,
222 sk_sp<SkColorSpace> imageColorSpace) {
223 const int width = imageSize.width();
224 const int height = imageSize.height();
Brian Salomond2fcfb52018-09-17 21:57:11 -0400225
Greg Daniel4065d452018-11-16 15:43:41 -0500226 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500227 ctx->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Greg Daniel4065d452018-11-16 15:43:41 -0500228
Brian Salomond2fcfb52018-09-17 21:57:11 -0400229 // Needs to create a render target in order to draw to it for the yuv->rgb conversion.
230 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips9da87e02019-02-04 13:26:26 -0500231 ctx->priv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500232 format, SkBackingFit::kExact, width, height, kRGBA_8888_GrPixelConfig,
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400233 std::move(imageColorSpace), 1, GrMipMapped::kNo, imageOrigin));
Brian Salomond2fcfb52018-09-17 21:57:11 -0400234 if (!renderTargetContext) {
235 return nullptr;
236 }
237
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400238 return SkImage_Gpu::ConvertYUVATexturesToRGB(ctx, yuvColorSpace, yuvaTextures, yuvaIndices,
Brian Salomonf05e6d32018-12-20 08:41:41 -0500239 imageSize, imageOrigin, renderTargetContext.get());
Brian Salomond2fcfb52018-09-17 21:57:11 -0400240}
241
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400242sk_sp<SkImage> SkImage::MakeFromYUVATexturesCopyWithExternalBackend(
Brian Salomond2fcfb52018-09-17 21:57:11 -0400243 GrContext* ctx,
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400244 SkYUVColorSpace yuvColorSpace,
Brian Salomond2fcfb52018-09-17 21:57:11 -0400245 const GrBackendTexture yuvaTextures[],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400246 const SkYUVAIndex yuvaIndices[4],
247 SkISize imageSize,
248 GrSurfaceOrigin imageOrigin,
249 const GrBackendTexture& backendTexture,
Brian Salomond2fcfb52018-09-17 21:57:11 -0400250 sk_sp<SkColorSpace> imageColorSpace) {
251 GrBackendTexture backendTextureCopy = backendTexture;
252
Jim Van Verth53275362018-11-09 15:42:35 -0500253 SkAlphaType at = SkImage_GpuBase::GetAlphaTypeFromYUVAIndices(yuvaIndices);
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400254 if (!SkImage_Gpu::ValidateBackendTexture(ctx, backendTextureCopy, &backendTextureCopy.fConfig,
Jim Van Verth53275362018-11-09 15:42:35 -0500255 kRGBA_8888_SkColorType, at, nullptr)) {
Brian Salomond2fcfb52018-09-17 21:57:11 -0400256 return nullptr;
257 }
258
259 // Needs to create a render target with external texture
260 // in order to draw to it for the yuv->rgb conversion.
261 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips9da87e02019-02-04 13:26:26 -0500262 ctx->priv().makeBackendTextureRenderTargetContext(backendTextureCopy,
263 imageOrigin, 1,
264 std::move(imageColorSpace)));
Brian Salomond2fcfb52018-09-17 21:57:11 -0400265
266 if (!renderTargetContext) {
267 return nullptr;
268 }
269
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400270 return SkImage_Gpu::ConvertYUVATexturesToRGB(ctx, yuvColorSpace, yuvaTextures, yuvaIndices,
Brian Salomonf05e6d32018-12-20 08:41:41 -0500271 imageSize, imageOrigin, renderTargetContext.get());
Robert Phillipsc25db632017-12-13 09:22:45 -0500272}
273
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400274sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400275 const GrBackendTexture yuvTextures[3],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400276 GrSurfaceOrigin imageOrigin,
Brian Salomon6a426c12018-03-15 12:16:02 -0400277 sk_sp<SkColorSpace> imageColorSpace) {
Weiliang Chenbed9d5e2018-05-22 18:44:02 -0400278 // TODO: SkImageSourceChannel input is being ingored right now. Setup correctly in the future.
279 SkYUVAIndex yuvaIndices[4] = {
Robert Phillips94ade752018-10-09 12:32:31 -0400280 SkYUVAIndex{0, SkColorChannel::kR},
281 SkYUVAIndex{1, SkColorChannel::kR},
282 SkYUVAIndex{2, SkColorChannel::kR},
Robert Phillips66a97342018-10-04 09:10:29 -0400283 SkYUVAIndex{-1, SkColorChannel::kA}};
Weiliang Chenbed9d5e2018-05-22 18:44:02 -0400284 SkISize size{yuvTextures[0].width(), yuvTextures[0].height()};
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400285 return SkImage_Gpu::MakeFromYUVATexturesCopy(ctx, yuvColorSpace, yuvTextures, yuvaIndices,
286 size, imageOrigin, std::move(imageColorSpace));
bsalomon993a4212015-05-29 11:37:25 -0700287}
reed56179002015-07-07 06:11:19 -0700288
Brian Salomond2fcfb52018-09-17 21:57:11 -0400289sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopyWithExternalBackend(
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400290 GrContext* ctx, SkYUVColorSpace yuvColorSpace, const GrBackendTexture yuvTextures[3],
291 GrSurfaceOrigin imageOrigin, const GrBackendTexture& backendTexture,
Brian Salomond2fcfb52018-09-17 21:57:11 -0400292 sk_sp<SkColorSpace> imageColorSpace) {
293 SkYUVAIndex yuvaIndices[4] = {
Robert Phillips94ade752018-10-09 12:32:31 -0400294 SkYUVAIndex{0, SkColorChannel::kR},
295 SkYUVAIndex{1, SkColorChannel::kR},
296 SkYUVAIndex{2, SkColorChannel::kR},
Robert Phillips66a97342018-10-04 09:10:29 -0400297 SkYUVAIndex{-1, SkColorChannel::kA}};
Brian Salomond2fcfb52018-09-17 21:57:11 -0400298 SkISize size{yuvTextures[0].width(), yuvTextures[0].height()};
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400299 return SkImage_Gpu::MakeFromYUVATexturesCopyWithExternalBackend(
300 ctx, yuvColorSpace, yuvTextures, yuvaIndices, size, imageOrigin, backendTexture,
Brian Salomond2fcfb52018-09-17 21:57:11 -0400301 std::move(imageColorSpace));
302}
303
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400304sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400305 const GrBackendTexture nv12Textures[2],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400306 GrSurfaceOrigin imageOrigin,
brianosmandddbe382016-07-20 13:55:39 -0700307 sk_sp<SkColorSpace> imageColorSpace) {
Weiliang Chenbed9d5e2018-05-22 18:44:02 -0400308 // TODO: SkImageSourceChannel input is being ingored right now. Setup correctly in the future.
309 SkYUVAIndex yuvaIndices[4] = {
Robert Phillips94ade752018-10-09 12:32:31 -0400310 SkYUVAIndex{0, SkColorChannel::kR},
311 SkYUVAIndex{1, SkColorChannel::kR},
312 SkYUVAIndex{1, SkColorChannel::kG},
Robert Phillips66a97342018-10-04 09:10:29 -0400313 SkYUVAIndex{-1, SkColorChannel::kA}};
Weiliang Chenbed9d5e2018-05-22 18:44:02 -0400314 SkISize size{nv12Textures[0].width(), nv12Textures[0].height()};
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400315 return SkImage_Gpu::MakeFromYUVATexturesCopy(ctx, yuvColorSpace, nv12Textures, yuvaIndices,
316 size, imageOrigin, std::move(imageColorSpace));
Robert Phillipsc25db632017-12-13 09:22:45 -0500317}
318
Brian Salomond2fcfb52018-09-17 21:57:11 -0400319sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopyWithExternalBackend(
320 GrContext* ctx,
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400321 SkYUVColorSpace yuvColorSpace,
Brian Salomond2fcfb52018-09-17 21:57:11 -0400322 const GrBackendTexture nv12Textures[2],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400323 GrSurfaceOrigin imageOrigin,
324 const GrBackendTexture& backendTexture,
Brian Salomond2fcfb52018-09-17 21:57:11 -0400325 sk_sp<SkColorSpace> imageColorSpace) {
326 SkYUVAIndex yuvaIndices[4] = {
Robert Phillips94ade752018-10-09 12:32:31 -0400327 SkYUVAIndex{0, SkColorChannel::kR},
328 SkYUVAIndex{1, SkColorChannel::kR},
329 SkYUVAIndex{1, SkColorChannel::kG},
Robert Phillips66a97342018-10-04 09:10:29 -0400330 SkYUVAIndex{-1, SkColorChannel::kA}};
Brian Salomond2fcfb52018-09-17 21:57:11 -0400331 SkISize size{nv12Textures[0].width(), nv12Textures[0].height()};
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400332 return SkImage_Gpu::MakeFromYUVATexturesCopyWithExternalBackend(
333 ctx, yuvColorSpace, nv12Textures, yuvaIndices, size, imageOrigin, backendTexture,
Brian Salomond2fcfb52018-09-17 21:57:11 -0400334 std::move(imageColorSpace));
335}
336
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400337static sk_sp<SkImage> create_image_from_producer(GrContext* context, GrTextureProducer* producer,
338 SkAlphaType at, uint32_t id,
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400339 GrMipMapped mipMapped) {
Brian Osman6064e1c2018-10-19 14:27:54 -0400340 sk_sp<GrTextureProxy> proxy(producer->refTextureProxy(mipMapped));
Robert Phillips3798c862017-03-27 11:08:16 -0400341 if (!proxy) {
Brian Osman041f7df2017-02-07 11:23:28 -0500342 return nullptr;
343 }
Brian Salomon8a8dd332018-05-24 14:08:31 -0400344 return sk_make_sp<SkImage_Gpu>(sk_ref_sp(context), id, at, std::move(proxy),
Brian Salomonf05e6d32018-12-20 08:41:41 -0500345 sk_ref_sp(producer->colorSpace()));
Brian Osman041f7df2017-02-07 11:23:28 -0500346}
347
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400348sk_sp<SkImage> SkImage::makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace,
349 GrMipMapped mipMapped) const {
Brian Osman041f7df2017-02-07 11:23:28 -0500350 if (!context) {
351 return nullptr;
352 }
Robert Phillips27467652019-01-10 16:34:22 -0500353 if (uint32_t incumbentID = as_IB(this)->contextID()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500354 if (incumbentID != context->priv().contextID()) {
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400355 return nullptr;
356 }
357 sk_sp<GrTextureProxy> proxy = as_IB(this)->asTextureProxyRef();
358 SkASSERT(proxy);
359 if (GrMipMapped::kNo == mipMapped || proxy->mipMapped() == mipMapped) {
360 return sk_ref_sp(const_cast<SkImage*>(this));
361 }
362 GrTextureAdjuster adjuster(context, std::move(proxy), this->alphaType(),
363 this->uniqueID(), this->colorSpace());
364 return create_image_from_producer(context, &adjuster, this->alphaType(),
Brian Osmane7fd8c32018-10-19 13:30:39 -0400365 this->uniqueID(), mipMapped);
Brian Osman041f7df2017-02-07 11:23:28 -0500366 }
367
Brian Osmandf7e0752017-04-26 16:20:28 -0400368 if (this->isLazyGenerated()) {
369 GrImageTextureMaker maker(context, this, kDisallow_CachingHint);
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400370 return create_image_from_producer(context, &maker, this->alphaType(),
Brian Osmane7fd8c32018-10-19 13:30:39 -0400371 this->uniqueID(), mipMapped);
Brian Osman041f7df2017-02-07 11:23:28 -0500372 }
373
374 if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) {
375 GrBitmapTextureMaker maker(context, *bmp);
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400376 return create_image_from_producer(context, &maker, this->alphaType(),
Brian Osmane7fd8c32018-10-19 13:30:39 -0400377 this->uniqueID(), mipMapped);
Brian Osman041f7df2017-02-07 11:23:28 -0500378 }
379 return nullptr;
380}
381
Greg Daniel7278d682018-03-16 14:57:21 -0400382///////////////////////////////////////////////////////////////////////////////////////////////////
383
Greg Daniela8d92112018-03-09 12:05:04 -0500384sk_sp<SkImage> SkImage_Gpu::MakePromiseTexture(GrContext* context,
385 const GrBackendFormat& backendFormat,
386 int width,
387 int height,
388 GrMipMapped mipMapped,
389 GrSurfaceOrigin origin,
390 SkColorType colorType,
391 SkAlphaType alphaType,
392 sk_sp<SkColorSpace> colorSpace,
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500393 PromiseImageTextureFulfillProc textureFulfillProc,
394 PromiseImageTextureReleaseProc textureReleaseProc,
Brian Salomonf55e8d52019-01-30 17:28:20 -0500395 PromiseImageTextureDoneProc textureDoneProc,
396 PromiseImageTextureContext textureContext,
397 DelayReleaseCallback delayReleaseCallback) {
Robert Phillipsef85d192018-10-09 11:24:09 -0400398 // The contract here is that if 'promiseDoneProc' is passed in it should always be called,
Brian Salomonbe5a0932018-12-10 10:03:26 -0500399 // even if creation of the SkImage fails. Once we call MakePromiseImageLazyProxy it takes
400 // responsibility for calling the done proc.
Brian Salomonf55e8d52019-01-30 17:28:20 -0500401 if (!textureDoneProc) {
Robert Phillipsef85d192018-10-09 11:24:09 -0400402 return nullptr;
403 }
Brian Salomonf55e8d52019-01-30 17:28:20 -0500404 SkScopeExit callDone([textureDoneProc, textureContext]() { textureDoneProc(textureContext); });
Robert Phillipsef85d192018-10-09 11:24:09 -0400405
Brian Salomonbe5a0932018-12-10 10:03:26 -0500406 SkImageInfo info = SkImageInfo::Make(width, height, colorType, alphaType, colorSpace);
407 if (!SkImageInfoIsValid(info)) {
408 return nullptr;
409 }
Robert Phillipsef85d192018-10-09 11:24:09 -0400410
Greg Daniela8d92112018-03-09 12:05:04 -0500411 if (!context) {
412 return nullptr;
413 }
414
415 if (width <= 0 || height <= 0) {
416 return nullptr;
417 }
418
Brian Salomonf391d0f2018-12-14 09:18:50 -0500419 GrPixelConfig config =
Robert Phillips9da87e02019-02-04 13:26:26 -0500420 context->priv().caps()->getConfigFromBackendFormat(backendFormat, colorType);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500421 if (config == kUnknown_GrPixelConfig) {
Greg Daniela8d92112018-03-09 12:05:04 -0500422 return nullptr;
423 }
424
Brian Salomonbe5a0932018-12-10 10:03:26 -0500425 callDone.clear();
426 auto proxy = MakePromiseImageLazyProxy(context, width, height, origin, config, backendFormat,
427 mipMapped, textureFulfillProc, textureReleaseProc,
Brian Salomonf55e8d52019-01-30 17:28:20 -0500428 textureDoneProc, textureContext, delayReleaseCallback);
Greg Daniela8d92112018-03-09 12:05:04 -0500429 if (!proxy) {
430 return nullptr;
431 }
Brian Salomon8a8dd332018-05-24 14:08:31 -0400432 return sk_make_sp<SkImage_Gpu>(sk_ref_sp(context), kNeedNewImageUniqueID, alphaType,
Brian Salomonf05e6d32018-12-20 08:41:41 -0500433 std::move(proxy), std::move(colorSpace));
Greg Daniela8d92112018-03-09 12:05:04 -0500434}
435
Greg Daniel7278d682018-03-16 14:57:21 -0400436///////////////////////////////////////////////////////////////////////////////////////////////////
437
Brian Osman13dddce2017-05-09 13:19:50 -0400438sk_sp<SkImage> SkImage::MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> encoded,
Brian Osman584b5012018-04-13 15:48:26 -0400439 bool buildMips, SkColorSpace* dstColorSpace,
440 bool limitToMaxTextureSize) {
Brian Osman13dddce2017-05-09 13:19:50 -0400441 sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(std::move(encoded));
442 if (!codecImage) {
443 return nullptr;
444 }
445
446 // Some backends or drivers don't support (safely) moving resources between contexts
Robert Phillips9da87e02019-02-04 13:26:26 -0500447 if (!context || !context->priv().caps()->crossContextTextureSupport()) {
Brian Osman13dddce2017-05-09 13:19:50 -0400448 return codecImage;
449 }
450
Brian Osman0471a932019-02-07 13:36:56 -0500451 // If non-power-of-two mipmapping isn't supported, ignore the client's request
452 if (!context->priv().caps()->mipMapSupport()) {
453 buildMips = false;
454 }
455
Robert Phillips9da87e02019-02-04 13:26:26 -0500456 auto maxTextureSize = context->priv().caps()->maxTextureSize();
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400457 if (limitToMaxTextureSize &&
458 (codecImage->width() > maxTextureSize || codecImage->height() > maxTextureSize)) {
Brian Osman584b5012018-04-13 15:48:26 -0400459 SkAutoPixmapStorage pmap;
460 SkImageInfo info = as_IB(codecImage)->onImageInfo();
461 if (!dstColorSpace) {
462 info = info.makeColorSpace(nullptr);
463 }
464 if (!pmap.tryAlloc(info) || !codecImage->readPixels(pmap, 0, 0, kDisallow_CachingHint)) {
465 return nullptr;
466 }
467 return MakeCrossContextFromPixmap(context, pmap, buildMips, dstColorSpace, true);
468 }
469
Brian Osman13dddce2017-05-09 13:19:50 -0400470 // Turn the codec image into a GrTextureProxy
471 GrImageTextureMaker maker(context, codecImage.get(), kDisallow_CachingHint);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400472 GrSamplerState samplerState(
473 GrSamplerState::WrapMode::kClamp,
474 buildMips ? GrSamplerState::Filter::kMipMap : GrSamplerState::Filter::kBilerp);
Brian Osman0471a932019-02-07 13:36:56 -0500475 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
476 sk_sp<GrTextureProxy> proxy(maker.refTextureProxyForParams(samplerState, scaleAdjust));
477 // Given that we disable mipmaps if non-power-of-two mipmapping isn't supported, we always
478 // expect the created texture to be unscaled.
479 SkASSERT(scaleAdjust[0] == 1.0f && scaleAdjust[1] == 1.0f);
Brian Osman13dddce2017-05-09 13:19:50 -0400480 if (!proxy) {
481 return codecImage;
482 }
483
Robert Phillips9da87e02019-02-04 13:26:26 -0500484 if (!proxy->instantiate(context->priv().resourceProvider())) {
Brian Osman13dddce2017-05-09 13:19:50 -0400485 return codecImage;
486 }
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400487 sk_sp<GrTexture> texture = sk_ref_sp(proxy->peekTexture());
Brian Osman13dddce2017-05-09 13:19:50 -0400488
489 // Flush any writes or uploads
Robert Phillips9da87e02019-02-04 13:26:26 -0500490 context->priv().prepareSurfaceForExternalIO(proxy.get());
Brian Osman13dddce2017-05-09 13:19:50 -0400491
Robert Phillips9da87e02019-02-04 13:26:26 -0500492 GrGpu* gpu = context->priv().getGpu();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500493 sk_sp<GrSemaphore> sema = gpu->prepareTextureForCrossContextUsage(texture.get());
Brian Osman13dddce2017-05-09 13:19:50 -0400494
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400495 auto gen = GrBackendTextureImageGenerator::Make(std::move(texture), proxy->origin(),
Brian Osman052ef692018-03-27 09:56:31 -0400496 std::move(sema),
497 as_IB(codecImage)->onImageInfo().colorType(),
498 codecImage->alphaType(),
Brian Osman6064e1c2018-10-19 14:27:54 -0400499 codecImage->refColorSpace());
Brian Osman13dddce2017-05-09 13:19:50 -0400500 return SkImage::MakeFromGenerator(std::move(gen));
501}
502
Brian Osman584b5012018-04-13 15:48:26 -0400503sk_sp<SkImage> SkImage::MakeCrossContextFromPixmap(GrContext* context,
504 const SkPixmap& originalPixmap, bool buildMips,
505 SkColorSpace* dstColorSpace,
506 bool limitToMaxTextureSize) {
Brian Osman63bc48d2017-11-07 10:37:00 -0500507 // Some backends or drivers don't support (safely) moving resources between contexts
Robert Phillips9da87e02019-02-04 13:26:26 -0500508 if (!context || !context->priv().caps()->crossContextTextureSupport()) {
Brian Osman584b5012018-04-13 15:48:26 -0400509 return SkImage::MakeRasterCopy(originalPixmap);
Brian Osman63bc48d2017-11-07 10:37:00 -0500510 }
511
Greg Daniel9e788112018-02-08 14:29:37 -0500512 // If we don't have access to the resource provider and gpu (i.e. in a DDL context) we will not
513 // be able to make everything needed for a GPU CrossContext image. Thus return a raster copy
514 // instead.
Robert Phillips9da87e02019-02-04 13:26:26 -0500515 if (!context->priv().resourceProvider()) {
Brian Osman584b5012018-04-13 15:48:26 -0400516 return SkImage::MakeRasterCopy(originalPixmap);
Greg Daniel9e788112018-02-08 14:29:37 -0500517 }
518
Brian Osman0471a932019-02-07 13:36:56 -0500519 // If non-power-of-two mipmapping isn't supported, ignore the client's request
520 if (!context->priv().caps()->mipMapSupport()) {
521 buildMips = false;
522 }
523
Brian Osman584b5012018-04-13 15:48:26 -0400524 const SkPixmap* pixmap = &originalPixmap;
525 SkAutoPixmapStorage resized;
Robert Phillips9da87e02019-02-04 13:26:26 -0500526 int maxTextureSize = context->priv().caps()->maxTextureSize();
Brian Osman584b5012018-04-13 15:48:26 -0400527 int maxDim = SkTMax(originalPixmap.width(), originalPixmap.height());
528 if (limitToMaxTextureSize && maxDim > maxTextureSize) {
529 float scale = static_cast<float>(maxTextureSize) / maxDim;
530 int newWidth = SkTMin(static_cast<int>(originalPixmap.width() * scale), maxTextureSize);
531 int newHeight = SkTMin(static_cast<int>(originalPixmap.height() * scale), maxTextureSize);
532 SkImageInfo info = originalPixmap.info().makeWH(newWidth, newHeight);
533 if (!resized.tryAlloc(info) || !originalPixmap.scalePixels(resized, kLow_SkFilterQuality)) {
534 return nullptr;
535 }
536 pixmap = &resized;
537 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500538 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Osman63bc48d2017-11-07 10:37:00 -0500539 // Turn the pixmap into a GrTextureProxy
540 sk_sp<GrTextureProxy> proxy;
541 if (buildMips) {
542 SkBitmap bmp;
Brian Osman584b5012018-04-13 15:48:26 -0400543 bmp.installPixels(*pixmap);
Brian Osman2b23c4b2018-06-01 12:25:08 -0400544 proxy = proxyProvider->createMipMapProxyFromBitmap(bmp);
Brian Osman63bc48d2017-11-07 10:37:00 -0500545 } else {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400546 if (SkImageInfoIsValid(pixmap->info())) {
Brian Osman584b5012018-04-13 15:48:26 -0400547 ATRACE_ANDROID_FRAMEWORK("Upload Texture [%ux%u]", pixmap->width(), pixmap->height());
Greg Danielabadbee2018-03-20 12:09:09 -0400548 // We don't need a release proc on the data in pixmap since we know we are in a
549 // GrContext that has a resource provider. Thus the createTextureProxy call will
550 // immediately upload the data.
Brian Osman584b5012018-04-13 15:48:26 -0400551 sk_sp<SkImage> image = SkImage::MakeFromRaster(*pixmap, nullptr, nullptr);
Greg Danielabadbee2018-03-20 12:09:09 -0400552 proxy = proxyProvider->createTextureProxy(std::move(image), kNone_GrSurfaceFlags, 1,
553 SkBudgeted::kYes, SkBackingFit::kExact);
Greg Danielbc54fad2018-02-09 16:40:32 -0500554 }
Brian Osman63bc48d2017-11-07 10:37:00 -0500555 }
556
557 if (!proxy) {
Brian Osman584b5012018-04-13 15:48:26 -0400558 return SkImage::MakeRasterCopy(*pixmap);
Brian Osman63bc48d2017-11-07 10:37:00 -0500559 }
560
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400561 sk_sp<GrTexture> texture = sk_ref_sp(proxy->peekTexture());
Brian Osman63bc48d2017-11-07 10:37:00 -0500562
563 // Flush any writes or uploads
Robert Phillips9da87e02019-02-04 13:26:26 -0500564 context->priv().prepareSurfaceForExternalIO(proxy.get());
565 GrGpu* gpu = context->priv().getGpu();
Brian Osman63bc48d2017-11-07 10:37:00 -0500566
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500567 sk_sp<GrSemaphore> sema = gpu->prepareTextureForCrossContextUsage(texture.get());
Brian Osman63bc48d2017-11-07 10:37:00 -0500568
569 auto gen = GrBackendTextureImageGenerator::Make(std::move(texture), proxy->origin(),
Brian Osman584b5012018-04-13 15:48:26 -0400570 std::move(sema), pixmap->colorType(),
571 pixmap->alphaType(),
572 pixmap->info().refColorSpace());
Brian Osman63bc48d2017-11-07 10:37:00 -0500573 return SkImage::MakeFromGenerator(std::move(gen));
574}
575
Derek Sollenberger7a869872017-06-27 15:37:25 -0400576#if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26
Stan Iliev7e910df2017-06-02 10:29:21 -0400577sk_sp<SkImage> SkImage::MakeFromAHardwareBuffer(AHardwareBuffer* graphicBuffer, SkAlphaType at,
Stan Iliev505dd572018-09-13 14:20:03 -0400578 sk_sp<SkColorSpace> cs,
579 GrSurfaceOrigin surfaceOrigin) {
580 auto gen = GrAHardwareBufferImageGenerator::Make(graphicBuffer, at, cs, surfaceOrigin);
Stan Iliev7e910df2017-06-02 10:29:21 -0400581 return SkImage::MakeFromGenerator(std::move(gen));
582}
583#endif
584
reed56179002015-07-07 06:11:19 -0700585///////////////////////////////////////////////////////////////////////////////////////////////////
586
Eric Karl914a36b2017-10-12 12:44:50 -0700587bool SkImage::MakeBackendTextureFromSkImage(GrContext* ctx,
588 sk_sp<SkImage> image,
589 GrBackendTexture* backendTexture,
590 BackendTextureReleaseProc* releaseProc) {
591 if (!image || !ctx || !backendTexture || !releaseProc) {
592 return false;
593 }
594
595 // Ensure we have a texture backed image.
596 if (!image->isTextureBacked()) {
597 image = image->makeTextureImage(ctx, nullptr);
598 if (!image) {
599 return false;
600 }
601 }
602 GrTexture* texture = image->getTexture();
Eric Karl36591e52018-01-19 13:45:02 -0800603 if (!texture) {
604 // In context-loss cases, we may not have a texture.
605 return false;
606 }
Eric Karl914a36b2017-10-12 12:44:50 -0700607
608 // If the image's context doesn't match the provided context, fail.
609 if (texture->getContext() != ctx) {
610 return false;
611 }
612
613 // Flush any pending IO on the texture.
Robert Phillips9da87e02019-02-04 13:26:26 -0500614 ctx->priv().prepareSurfaceForExternalIO(as_IB(image)->peekProxy());
Eric Karl914a36b2017-10-12 12:44:50 -0700615 SkASSERT(!texture->surfacePriv().hasPendingIO());
616
617 // We must make a copy of the image if the image is not unique, if the GrTexture owned by the
618 // image is not unique, or if the texture wraps an external object.
619 if (!image->unique() || !texture->surfacePriv().hasUniqueRef() ||
620 texture->resourcePriv().refsWrappedObjects()) {
621 // onMakeSubset will always copy the image.
622 image = as_IB(image)->onMakeSubset(image->bounds());
623 if (!image) {
624 return false;
625 }
626
627 texture = image->getTexture();
Eric Karl36591e52018-01-19 13:45:02 -0800628 if (!texture) {
629 return false;
630 }
Eric Karl914a36b2017-10-12 12:44:50 -0700631
632 // Flush to ensure that the copy is completed before we return the texture.
Robert Phillips9da87e02019-02-04 13:26:26 -0500633 ctx->priv().prepareSurfaceForExternalIO(as_IB(image)->peekProxy());
Eric Karl914a36b2017-10-12 12:44:50 -0700634 SkASSERT(!texture->surfacePriv().hasPendingIO());
635 }
636
637 SkASSERT(!texture->resourcePriv().refsWrappedObjects());
638 SkASSERT(texture->surfacePriv().hasUniqueRef());
639 SkASSERT(image->unique());
640
641 // Take a reference to the GrTexture and release the image.
642 sk_sp<GrTexture> textureRef(SkSafeRef(texture));
643 image = nullptr;
644
645 // Steal the backend texture from the GrTexture, releasing the GrTexture in the process.
646 return GrTexture::StealBackendTexture(std::move(textureRef), backendTexture, releaseProc);
647}