reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 8 | #include <cstddef> |
| 9 | #include <cstring> |
| 10 | #include <type_traits> |
| 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkCanvas.h" |
| 13 | #include "include/gpu/GrBackendSurface.h" |
| 14 | #include "include/gpu/GrContext.h" |
| 15 | #include "include/gpu/GrTexture.h" |
| 16 | #include "include/private/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/private/SkImageInfoPriv.h" |
| 18 | #include "src/core/SkAutoPixmapStorage.h" |
| 19 | #include "src/core/SkBitmapCache.h" |
| 20 | #include "src/core/SkMipMap.h" |
| 21 | #include "src/core/SkScopeExit.h" |
| 22 | #include "src/core/SkTraceEvent.h" |
| 23 | #include "src/gpu/GrAHardwareBufferImageGenerator.h" |
| 24 | #include "src/gpu/GrAHardwareBufferUtils.h" |
| 25 | #include "src/gpu/GrBackendTextureImageGenerator.h" |
| 26 | #include "src/gpu/GrBitmapTextureMaker.h" |
| 27 | #include "src/gpu/GrCaps.h" |
| 28 | #include "src/gpu/GrClip.h" |
| 29 | #include "src/gpu/GrColorSpaceXform.h" |
| 30 | #include "src/gpu/GrContextPriv.h" |
| 31 | #include "src/gpu/GrDrawingManager.h" |
| 32 | #include "src/gpu/GrGpu.h" |
| 33 | #include "src/gpu/GrImageTextureMaker.h" |
| 34 | #include "src/gpu/GrProxyProvider.h" |
| 35 | #include "src/gpu/GrRecordingContextPriv.h" |
| 36 | #include "src/gpu/GrRenderTargetContext.h" |
| 37 | #include "src/gpu/GrResourceProvider.h" |
| 38 | #include "src/gpu/GrResourceProviderPriv.h" |
| 39 | #include "src/gpu/GrSemaphore.h" |
| 40 | #include "src/gpu/GrSurfacePriv.h" |
| 41 | #include "src/gpu/GrTextureAdjuster.h" |
| 42 | #include "src/gpu/GrTextureContext.h" |
| 43 | #include "src/gpu/GrTexturePriv.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 44 | #include "src/gpu/GrTextureProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 45 | #include "src/gpu/GrTextureProxyPriv.h" |
| 46 | #include "src/gpu/SkGr.h" |
| 47 | #include "src/gpu/effects/GrYUVtoRGBEffect.h" |
| 48 | #include "src/gpu/gl/GrGLTexture.h" |
| 49 | #include "src/image/SkImage_Gpu.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 50 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 51 | static SkColorType proxy_color_type(GrTextureProxy* proxy) { |
| 52 | SkColorType colorType; |
| 53 | if (!GrPixelConfigToColorType(proxy->config(), &colorType)) { |
| 54 | colorType = kUnknown_SkColorType; |
| 55 | } |
| 56 | return colorType; |
| 57 | } |
| 58 | |
Brian Salomon | 8a8dd33 | 2018-05-24 14:08:31 -0400 | [diff] [blame] | 59 | SkImage_Gpu::SkImage_Gpu(sk_sp<GrContext> context, uint32_t uniqueID, SkAlphaType at, |
Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 60 | sk_sp<GrTextureProxy> proxy, sk_sp<SkColorSpace> colorSpace) |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 61 | : INHERITED(std::move(context), proxy->worstCaseWidth(), proxy->worstCaseHeight(), uniqueID, |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 62 | proxy_color_type(proxy.get()), at, colorSpace) |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 63 | , fProxy(std::move(proxy)) {} |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 64 | |
Mike Reed | 30301c4 | 2018-07-19 09:39:21 -0400 | [diff] [blame] | 65 | SkImage_Gpu::~SkImage_Gpu() {} |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 66 | |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 67 | GrSemaphoresSubmitted SkImage_Gpu::onFlush(GrContext* context, const GrFlushInfo& info) { |
| 68 | if (!context || !fContext->priv().matches(context) || fContext->abandoned()) { |
| 69 | return GrSemaphoresSubmitted::kNo; |
| 70 | } |
| 71 | |
| 72 | GrSurfaceProxy* p[1] = {fProxy.get()}; |
| 73 | return context->priv().flushSurfaces(p, 1, info); |
| 74 | } |
| 75 | |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 76 | sk_sp<SkImage> SkImage_Gpu::onMakeColorTypeAndColorSpace(GrRecordingContext* context, |
| 77 | SkColorType targetCT, |
Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 78 | sk_sp<SkColorSpace> targetCS) const { |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 79 | if (!context || !fContext->priv().matches(context)) { |
| 80 | return nullptr; |
| 81 | } |
| 82 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 83 | auto xform = GrColorSpaceXformEffect::Make(this->colorSpace(), this->alphaType(), |
| 84 | targetCS.get(), this->alphaType()); |
Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 85 | SkASSERT(xform || targetCT != this->colorType()); |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 86 | |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 87 | sk_sp<GrTextureProxy> proxy = this->asTextureProxyRef(context); |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 88 | |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 89 | sk_sp<GrRenderTargetContext> renderTargetContext( |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 90 | context->priv().makeDeferredRenderTargetContextWithFallback( |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 91 | SkBackingFit::kExact, this->width(), this->height(), |
| 92 | SkColorTypeToGrColorType(targetCT), nullptr)); |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 93 | if (!renderTargetContext) { |
| 94 | return nullptr; |
| 95 | } |
| 96 | |
| 97 | GrPaint paint; |
| 98 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 99 | paint.addColorTextureProcessor(std::move(proxy), SkMatrix::I()); |
Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 100 | if (xform) { |
| 101 | paint.addColorFragmentProcessor(std::move(xform)); |
| 102 | } |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 103 | |
| 104 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), |
| 105 | SkRect::MakeIWH(this->width(), this->height())); |
| 106 | if (!renderTargetContext->asTextureProxy()) { |
| 107 | return nullptr; |
| 108 | } |
| 109 | |
| 110 | // MDB: this call is okay bc we know 'renderTargetContext' was exact |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 111 | return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, this->alphaType(), |
Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 112 | renderTargetContext->asTextureProxyRef(), std::move(targetCS)); |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 113 | } |
| 114 | |
Brian Osman | d514837 | 2019-08-14 16:14:51 -0400 | [diff] [blame^] | 115 | sk_sp<SkImage> SkImage_Gpu::onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const { |
| 116 | return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID, this->alphaType(), fProxy, |
| 117 | std::move(newCS)); |
| 118 | } |
| 119 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 120 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 121 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 122 | static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, |
| 123 | const GrBackendTexture& backendTex, |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 124 | GrColorType colorType, GrSurfaceOrigin origin, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 125 | SkAlphaType at, sk_sp<SkColorSpace> colorSpace, |
| 126 | GrWrapOwnership ownership, |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 127 | SkImage::TextureReleaseProc releaseProc, |
| 128 | SkImage::ReleaseContext releaseCtx) { |
Greg Daniel | 66aebf3 | 2018-04-09 09:15:56 -0400 | [diff] [blame] | 129 | if (!backendTex.isValid() || backendTex.width() <= 0 || backendTex.height() <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 130 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 131 | } |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 132 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 133 | GrProxyProvider* proxyProvider = ctx->priv().proxyProvider(); |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 134 | sk_sp<GrTextureProxy> proxy = |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 135 | proxyProvider->wrapBackendTexture(backendTex, colorType, origin, ownership, |
| 136 | GrWrapCacheable::kNo, kRead_GrIOType, |
| 137 | releaseProc, releaseCtx); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 138 | if (!proxy) { |
Robert Phillips | e201ebc | 2018-01-17 18:12:50 +0000 | [diff] [blame] | 139 | return nullptr; |
| 140 | } |
Brian Salomon | 8a8dd33 | 2018-05-24 14:08:31 -0400 | [diff] [blame] | 141 | return sk_make_sp<SkImage_Gpu>(sk_ref_sp(ctx), kNeedNewImageUniqueID, at, std::move(proxy), |
Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 142 | std::move(colorSpace)); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 145 | sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, |
| 146 | const GrBackendTexture& tex, GrSurfaceOrigin origin, |
| 147 | SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs, |
| 148 | TextureReleaseProc releaseP, ReleaseContext releaseC) { |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 149 | if (!ctx) { |
| 150 | return nullptr; |
| 151 | } |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 152 | |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 153 | const GrCaps* caps = ctx->priv().caps(); |
| 154 | |
| 155 | GrColorType grColorType = SkColorTypeAndFormatToGrColorType(caps, ct, tex.getBackendFormat()); |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 156 | if (GrColorType::kUnknown == grColorType) { |
Robert Phillips | 9725638 | 2019-07-17 15:26:36 -0400 | [diff] [blame] | 157 | return nullptr; |
| 158 | } |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 159 | |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 160 | if (!SkImage_GpuBase::ValidateBackendTexture(caps, tex, grColorType, ct, at, cs)) { |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 161 | return nullptr; |
| 162 | } |
| 163 | |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 164 | return new_wrapped_texture_common(ctx, tex, grColorType, origin, at, std::move(cs), |
Brian Salomon | bfd2749 | 2018-03-19 14:08:51 -0400 | [diff] [blame] | 165 | kBorrow_GrWrapOwnership, releaseP, releaseC); |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 166 | } |
| 167 | |
Greg Daniel | 9440345 | 2017-04-18 15:52:36 -0400 | [diff] [blame] | 168 | sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, |
| 169 | const GrBackendTexture& tex, GrSurfaceOrigin origin, |
Brian Salomon | 42409c2 | 2018-03-20 13:48:41 -0400 | [diff] [blame] | 170 | SkColorType ct, SkAlphaType at, |
| 171 | sk_sp<SkColorSpace> cs) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 172 | if (!ctx || !ctx->priv().resourceProvider()) { |
Greg Daniel | f2336e4 | 2018-01-23 16:38:14 -0500 | [diff] [blame] | 173 | // We have a DDL context and we don't support adopted textures for them. |
| 174 | return nullptr; |
| 175 | } |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 176 | |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 177 | const GrCaps* caps = ctx->priv().caps(); |
| 178 | |
| 179 | GrColorType grColorType = SkColorTypeAndFormatToGrColorType(caps, ct, tex.getBackendFormat()); |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 180 | if (GrColorType::kUnknown == grColorType) { |
Robert Phillips | 9725638 | 2019-07-17 15:26:36 -0400 | [diff] [blame] | 181 | return nullptr; |
| 182 | } |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 183 | |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 184 | if (!SkImage_GpuBase::ValidateBackendTexture(caps, tex, grColorType, ct, at, cs)) { |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 185 | return nullptr; |
| 186 | } |
| 187 | |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 188 | return new_wrapped_texture_common(ctx, tex, grColorType, origin, at, std::move(cs), |
Brian Salomon | 42409c2 | 2018-03-20 13:48:41 -0400 | [diff] [blame] | 189 | kAdopt_GrWrapOwnership, nullptr, nullptr); |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 190 | } |
| 191 | |
Jim Van Verth | ee06b33 | 2019-01-18 10:36:32 -0500 | [diff] [blame] | 192 | sk_sp<SkImage> SkImage::MakeFromCompressed(GrContext* context, sk_sp<SkData> data, |
| 193 | int width, int height, CompressionType type) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 194 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 195 | sk_sp<GrTextureProxy> proxy = proxyProvider->createCompressedTextureProxy( |
| 196 | width, height, SkBudgeted::kYes, type, std::move(data)); |
Jim Van Verth | ee06b33 | 2019-01-18 10:36:32 -0500 | [diff] [blame] | 197 | |
| 198 | if (!proxy) { |
| 199 | return nullptr; |
| 200 | } |
| 201 | |
| 202 | return sk_make_sp<SkImage_Gpu>(sk_ref_sp(context), kNeedNewImageUniqueID, kOpaque_SkAlphaType, |
| 203 | std::move(proxy), nullptr); |
| 204 | } |
| 205 | |
Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 206 | sk_sp<SkImage> SkImage_Gpu::ConvertYUVATexturesToRGB(GrContext* ctx, SkYUVColorSpace yuvColorSpace, |
| 207 | const GrBackendTexture yuvaTextures[], |
| 208 | const SkYUVAIndex yuvaIndices[4], SkISize size, |
| 209 | GrSurfaceOrigin origin, |
| 210 | GrRenderTargetContext* renderTargetContext) { |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 211 | SkASSERT(renderTargetContext); |
| 212 | |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 213 | int numTextures; |
| 214 | if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures)) { |
Robert Phillips | e60ad62 | 2016-11-17 10:22:48 -0500 | [diff] [blame] | 215 | return nullptr; |
| 216 | } |
Robert Phillips | ba5c439 | 2018-07-25 12:37:14 -0400 | [diff] [blame] | 217 | |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 218 | sk_sp<GrTextureProxy> tempTextureProxies[4]; |
Jim Van Verth | 5327536 | 2018-11-09 15:42:35 -0500 | [diff] [blame] | 219 | if (!SkImage_GpuBase::MakeTempTextureProxies(ctx, yuvaTextures, numTextures, yuvaIndices, |
| 220 | origin, tempTextureProxies)) { |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 221 | return nullptr; |
| 222 | } |
| 223 | |
| 224 | const SkRect rect = SkRect::MakeIWH(size.width(), size.height()); |
Brian Osman | e956049 | 2019-02-05 17:00:03 -0500 | [diff] [blame] | 225 | if (!RenderYUVAToRGBA(ctx, renderTargetContext, rect, yuvColorSpace, nullptr, |
Jim Van Verth | 0e67194 | 2018-11-09 12:03:57 -0500 | [diff] [blame] | 226 | tempTextureProxies, yuvaIndices)) { |
| 227 | return nullptr; |
| 228 | } |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 229 | |
Jim Van Verth | 5327536 | 2018-11-09 15:42:35 -0500 | [diff] [blame] | 230 | SkAlphaType at = GetAlphaTypeFromYUVAIndices(yuvaIndices); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 231 | // MDB: this call is okay bc we know 'renderTargetContext' was exact |
Jim Van Verth | 5327536 | 2018-11-09 15:42:35 -0500 | [diff] [blame] | 232 | return sk_make_sp<SkImage_Gpu>(sk_ref_sp(ctx), kNeedNewImageUniqueID, at, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 233 | renderTargetContext->asTextureProxyRef(), |
Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 234 | renderTargetContext->colorSpaceInfo().refColorSpace()); |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 235 | } |
| 236 | |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 237 | sk_sp<SkImage> SkImage::MakeFromYUVATexturesCopy(GrContext* ctx, |
| 238 | SkYUVColorSpace yuvColorSpace, |
| 239 | const GrBackendTexture yuvaTextures[], |
| 240 | const SkYUVAIndex yuvaIndices[4], |
| 241 | SkISize imageSize, |
| 242 | GrSurfaceOrigin imageOrigin, |
| 243 | sk_sp<SkColorSpace> imageColorSpace) { |
| 244 | const int width = imageSize.width(); |
| 245 | const int height = imageSize.height(); |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 246 | |
| 247 | // Needs to create a render target in order to draw to it for the yuv->rgb conversion. |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 248 | sk_sp<GrRenderTargetContext> renderTargetContext(ctx->priv().makeDeferredRenderTargetContext( |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 249 | SkBackingFit::kExact, width, height, GrColorType::kRGBA_8888, |
| 250 | std::move(imageColorSpace), 1, GrMipMapped::kNo, imageOrigin)); |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 251 | if (!renderTargetContext) { |
| 252 | return nullptr; |
| 253 | } |
| 254 | |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 255 | return SkImage_Gpu::ConvertYUVATexturesToRGB(ctx, yuvColorSpace, yuvaTextures, yuvaIndices, |
Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 256 | imageSize, imageOrigin, renderTargetContext.get()); |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 257 | } |
| 258 | |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 259 | sk_sp<SkImage> SkImage::MakeFromYUVATexturesCopyWithExternalBackend( |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 260 | GrContext* ctx, |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 261 | SkYUVColorSpace yuvColorSpace, |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 262 | const GrBackendTexture yuvaTextures[], |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 263 | const SkYUVAIndex yuvaIndices[4], |
| 264 | SkISize imageSize, |
| 265 | GrSurfaceOrigin imageOrigin, |
| 266 | const GrBackendTexture& backendTexture, |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 267 | sk_sp<SkColorSpace> imageColorSpace) { |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 268 | const GrCaps* caps = ctx->priv().caps(); |
Robert Phillips | dd39980 | 2019-07-18 12:28:00 +0000 | [diff] [blame] | 269 | |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 270 | GrColorType grColorType = SkColorTypeAndFormatToGrColorType(caps, kRGBA_8888_SkColorType, |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 271 | backendTexture.getBackendFormat()); |
| 272 | if (GrColorType::kUnknown == grColorType) { |
| 273 | return nullptr; |
| 274 | } |
| 275 | |
Jim Van Verth | 5327536 | 2018-11-09 15:42:35 -0500 | [diff] [blame] | 276 | SkAlphaType at = SkImage_GpuBase::GetAlphaTypeFromYUVAIndices(yuvaIndices); |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 277 | if (!SkImage_Gpu::ValidateBackendTexture(caps, backendTexture, grColorType, |
| 278 | kRGBA_8888_SkColorType, at, nullptr)) { |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 279 | return nullptr; |
| 280 | } |
| 281 | |
| 282 | // Needs to create a render target with external texture |
| 283 | // in order to draw to it for the yuv->rgb conversion. |
| 284 | sk_sp<GrRenderTargetContext> renderTargetContext( |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 285 | ctx->priv().makeBackendTextureRenderTargetContext(backendTexture, imageOrigin, 1, |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 286 | grColorType, |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 287 | std::move(imageColorSpace))); |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 288 | |
| 289 | if (!renderTargetContext) { |
| 290 | return nullptr; |
| 291 | } |
| 292 | |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 293 | return SkImage_Gpu::ConvertYUVATexturesToRGB(ctx, yuvColorSpace, yuvaTextures, yuvaIndices, |
Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 294 | imageSize, imageOrigin, renderTargetContext.get()); |
Robert Phillips | c25db63 | 2017-12-13 09:22:45 -0500 | [diff] [blame] | 295 | } |
| 296 | |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 297 | sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace yuvColorSpace, |
Brian Salomon | 6a426c1 | 2018-03-15 12:16:02 -0400 | [diff] [blame] | 298 | const GrBackendTexture yuvTextures[3], |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 299 | GrSurfaceOrigin imageOrigin, |
Brian Salomon | 6a426c1 | 2018-03-15 12:16:02 -0400 | [diff] [blame] | 300 | sk_sp<SkColorSpace> imageColorSpace) { |
Weiliang Chen | bed9d5e | 2018-05-22 18:44:02 -0400 | [diff] [blame] | 301 | // TODO: SkImageSourceChannel input is being ingored right now. Setup correctly in the future. |
| 302 | SkYUVAIndex yuvaIndices[4] = { |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 303 | SkYUVAIndex{0, SkColorChannel::kR}, |
| 304 | SkYUVAIndex{1, SkColorChannel::kR}, |
| 305 | SkYUVAIndex{2, SkColorChannel::kR}, |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 306 | SkYUVAIndex{-1, SkColorChannel::kA}}; |
Weiliang Chen | bed9d5e | 2018-05-22 18:44:02 -0400 | [diff] [blame] | 307 | SkISize size{yuvTextures[0].width(), yuvTextures[0].height()}; |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 308 | return SkImage_Gpu::MakeFromYUVATexturesCopy(ctx, yuvColorSpace, yuvTextures, yuvaIndices, |
| 309 | size, imageOrigin, std::move(imageColorSpace)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 310 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 311 | |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 312 | sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopyWithExternalBackend( |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 313 | GrContext* ctx, SkYUVColorSpace yuvColorSpace, const GrBackendTexture yuvTextures[3], |
| 314 | GrSurfaceOrigin imageOrigin, const GrBackendTexture& backendTexture, |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 315 | sk_sp<SkColorSpace> imageColorSpace) { |
| 316 | SkYUVAIndex yuvaIndices[4] = { |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 317 | SkYUVAIndex{0, SkColorChannel::kR}, |
| 318 | SkYUVAIndex{1, SkColorChannel::kR}, |
| 319 | SkYUVAIndex{2, SkColorChannel::kR}, |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 320 | SkYUVAIndex{-1, SkColorChannel::kA}}; |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 321 | SkISize size{yuvTextures[0].width(), yuvTextures[0].height()}; |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 322 | return SkImage_Gpu::MakeFromYUVATexturesCopyWithExternalBackend( |
| 323 | ctx, yuvColorSpace, yuvTextures, yuvaIndices, size, imageOrigin, backendTexture, |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 324 | std::move(imageColorSpace)); |
| 325 | } |
| 326 | |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 327 | sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace yuvColorSpace, |
Brian Salomon | 6a426c1 | 2018-03-15 12:16:02 -0400 | [diff] [blame] | 328 | const GrBackendTexture nv12Textures[2], |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 329 | GrSurfaceOrigin imageOrigin, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 330 | sk_sp<SkColorSpace> imageColorSpace) { |
Weiliang Chen | bed9d5e | 2018-05-22 18:44:02 -0400 | [diff] [blame] | 331 | // TODO: SkImageSourceChannel input is being ingored right now. Setup correctly in the future. |
| 332 | SkYUVAIndex yuvaIndices[4] = { |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 333 | SkYUVAIndex{0, SkColorChannel::kR}, |
| 334 | SkYUVAIndex{1, SkColorChannel::kR}, |
| 335 | SkYUVAIndex{1, SkColorChannel::kG}, |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 336 | SkYUVAIndex{-1, SkColorChannel::kA}}; |
Weiliang Chen | bed9d5e | 2018-05-22 18:44:02 -0400 | [diff] [blame] | 337 | SkISize size{nv12Textures[0].width(), nv12Textures[0].height()}; |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 338 | return SkImage_Gpu::MakeFromYUVATexturesCopy(ctx, yuvColorSpace, nv12Textures, yuvaIndices, |
| 339 | size, imageOrigin, std::move(imageColorSpace)); |
Robert Phillips | c25db63 | 2017-12-13 09:22:45 -0500 | [diff] [blame] | 340 | } |
| 341 | |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 342 | sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopyWithExternalBackend( |
| 343 | GrContext* ctx, |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 344 | SkYUVColorSpace yuvColorSpace, |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 345 | const GrBackendTexture nv12Textures[2], |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 346 | GrSurfaceOrigin imageOrigin, |
| 347 | const GrBackendTexture& backendTexture, |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 348 | sk_sp<SkColorSpace> imageColorSpace) { |
| 349 | SkYUVAIndex yuvaIndices[4] = { |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 350 | SkYUVAIndex{0, SkColorChannel::kR}, |
| 351 | SkYUVAIndex{1, SkColorChannel::kR}, |
| 352 | SkYUVAIndex{1, SkColorChannel::kG}, |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 353 | SkYUVAIndex{-1, SkColorChannel::kA}}; |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 354 | SkISize size{nv12Textures[0].width(), nv12Textures[0].height()}; |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 355 | return SkImage_Gpu::MakeFromYUVATexturesCopyWithExternalBackend( |
| 356 | ctx, yuvColorSpace, nv12Textures, yuvaIndices, size, imageOrigin, backendTexture, |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 357 | std::move(imageColorSpace)); |
| 358 | } |
| 359 | |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 360 | static sk_sp<SkImage> create_image_from_producer(GrContext* context, GrTextureProducer* producer, |
| 361 | SkAlphaType at, uint32_t id, |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 362 | GrMipMapped mipMapped) { |
Brian Osman | 6064e1c | 2018-10-19 14:27:54 -0400 | [diff] [blame] | 363 | sk_sp<GrTextureProxy> proxy(producer->refTextureProxy(mipMapped)); |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 364 | if (!proxy) { |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 365 | return nullptr; |
| 366 | } |
Brian Salomon | 8a8dd33 | 2018-05-24 14:08:31 -0400 | [diff] [blame] | 367 | return sk_make_sp<SkImage_Gpu>(sk_ref_sp(context), id, at, std::move(proxy), |
Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 368 | sk_ref_sp(producer->colorSpace())); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 369 | } |
| 370 | |
Brian Osman | d566e2e | 2019-08-14 13:19:04 -0400 | [diff] [blame] | 371 | sk_sp<SkImage> SkImage::makeTextureImage(GrContext* context, GrMipMapped mipMapped) const { |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 372 | if (!context) { |
| 373 | return nullptr; |
| 374 | } |
Robert Phillips | fe0963c | 2019-02-07 13:25:07 -0500 | [diff] [blame] | 375 | |
| 376 | if (this->isTextureBacked()) { |
| 377 | if (!as_IB(this)->context()->priv().matches(context)) { |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 378 | return nullptr; |
| 379 | } |
Robert Phillips | fe0963c | 2019-02-07 13:25:07 -0500 | [diff] [blame] | 380 | |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 381 | sk_sp<GrTextureProxy> proxy = as_IB(this)->asTextureProxyRef(context); |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 382 | SkASSERT(proxy); |
| 383 | if (GrMipMapped::kNo == mipMapped || proxy->mipMapped() == mipMapped) { |
| 384 | return sk_ref_sp(const_cast<SkImage*>(this)); |
| 385 | } |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 386 | GrTextureAdjuster adjuster(context, std::move(proxy), |
| 387 | SkColorTypeToGrColorType(this->colorType()), this->alphaType(), |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 388 | this->uniqueID(), this->colorSpace()); |
| 389 | return create_image_from_producer(context, &adjuster, this->alphaType(), |
Brian Osman | e7fd8c3 | 2018-10-19 13:30:39 -0400 | [diff] [blame] | 390 | this->uniqueID(), mipMapped); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 391 | } |
| 392 | |
Brian Osman | df7e075 | 2017-04-26 16:20:28 -0400 | [diff] [blame] | 393 | if (this->isLazyGenerated()) { |
| 394 | GrImageTextureMaker maker(context, this, kDisallow_CachingHint); |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 395 | return create_image_from_producer(context, &maker, this->alphaType(), |
Brian Osman | e7fd8c3 | 2018-10-19 13:30:39 -0400 | [diff] [blame] | 396 | this->uniqueID(), mipMapped); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) { |
| 400 | GrBitmapTextureMaker maker(context, *bmp); |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 401 | return create_image_from_producer(context, &maker, this->alphaType(), |
Brian Osman | e7fd8c3 | 2018-10-19 13:30:39 -0400 | [diff] [blame] | 402 | this->uniqueID(), mipMapped); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 403 | } |
| 404 | return nullptr; |
| 405 | } |
| 406 | |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 407 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 408 | |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 409 | sk_sp<SkImage> SkImage_Gpu::MakePromiseTexture(GrContext* context, |
| 410 | const GrBackendFormat& backendFormat, |
| 411 | int width, |
| 412 | int height, |
| 413 | GrMipMapped mipMapped, |
| 414 | GrSurfaceOrigin origin, |
| 415 | SkColorType colorType, |
| 416 | SkAlphaType alphaType, |
| 417 | sk_sp<SkColorSpace> colorSpace, |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 418 | PromiseImageTextureFulfillProc textureFulfillProc, |
| 419 | PromiseImageTextureReleaseProc textureReleaseProc, |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 420 | PromiseImageTextureDoneProc textureDoneProc, |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 421 | PromiseImageTextureContext textureContext, |
| 422 | PromiseImageApiVersion version) { |
Robert Phillips | ef85d19 | 2018-10-09 11:24:09 -0400 | [diff] [blame] | 423 | // The contract here is that if 'promiseDoneProc' is passed in it should always be called, |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 424 | // even if creation of the SkImage fails. Once we call MakePromiseImageLazyProxy it takes |
| 425 | // responsibility for calling the done proc. |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 426 | if (!textureDoneProc) { |
Robert Phillips | ef85d19 | 2018-10-09 11:24:09 -0400 | [diff] [blame] | 427 | return nullptr; |
| 428 | } |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 429 | SkScopeExit callDone([textureDoneProc, textureContext]() { textureDoneProc(textureContext); }); |
Robert Phillips | ef85d19 | 2018-10-09 11:24:09 -0400 | [diff] [blame] | 430 | |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 431 | SkImageInfo info = SkImageInfo::Make(width, height, colorType, alphaType, colorSpace); |
| 432 | if (!SkImageInfoIsValid(info)) { |
| 433 | return nullptr; |
| 434 | } |
Robert Phillips | ef85d19 | 2018-10-09 11:24:09 -0400 | [diff] [blame] | 435 | |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 436 | if (!context) { |
| 437 | return nullptr; |
| 438 | } |
| 439 | |
| 440 | if (width <= 0 || height <= 0) { |
| 441 | return nullptr; |
| 442 | } |
| 443 | |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 444 | GrColorType grColorType = SkColorTypeAndFormatToGrColorType(context->priv().caps(), |
| 445 | colorType, |
| 446 | backendFormat); |
| 447 | if (GrColorType::kUnknown == grColorType) { |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 448 | return nullptr; |
| 449 | } |
| 450 | |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 451 | callDone.clear(); |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 452 | auto proxy = MakePromiseImageLazyProxy(context, width, height, origin, |
| 453 | grColorType, backendFormat, |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 454 | mipMapped, textureFulfillProc, textureReleaseProc, |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 455 | textureDoneProc, textureContext, version); |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 456 | if (!proxy) { |
| 457 | return nullptr; |
| 458 | } |
Brian Salomon | 8a8dd33 | 2018-05-24 14:08:31 -0400 | [diff] [blame] | 459 | return sk_make_sp<SkImage_Gpu>(sk_ref_sp(context), kNeedNewImageUniqueID, alphaType, |
Brian Salomon | f05e6d3 | 2018-12-20 08:41:41 -0500 | [diff] [blame] | 460 | std::move(proxy), std::move(colorSpace)); |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 461 | } |
| 462 | |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 463 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 464 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 465 | sk_sp<SkImage> SkImage::MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> encoded, |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 466 | bool buildMips, SkColorSpace* dstColorSpace, |
| 467 | bool limitToMaxTextureSize) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 468 | sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(std::move(encoded)); |
| 469 | if (!codecImage) { |
| 470 | return nullptr; |
| 471 | } |
| 472 | |
| 473 | // Some backends or drivers don't support (safely) moving resources between contexts |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 474 | if (!context || !context->priv().caps()->crossContextTextureSupport()) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 475 | return codecImage; |
| 476 | } |
| 477 | |
Brian Osman | 0471a93 | 2019-02-07 13:36:56 -0500 | [diff] [blame] | 478 | // If non-power-of-two mipmapping isn't supported, ignore the client's request |
| 479 | if (!context->priv().caps()->mipMapSupport()) { |
| 480 | buildMips = false; |
| 481 | } |
| 482 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 483 | auto maxTextureSize = context->priv().caps()->maxTextureSize(); |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 484 | if (limitToMaxTextureSize && |
| 485 | (codecImage->width() > maxTextureSize || codecImage->height() > maxTextureSize)) { |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 486 | SkAutoPixmapStorage pmap; |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 487 | SkImageInfo info = codecImage->imageInfo(); |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 488 | if (!dstColorSpace) { |
| 489 | info = info.makeColorSpace(nullptr); |
| 490 | } |
| 491 | if (!pmap.tryAlloc(info) || !codecImage->readPixels(pmap, 0, 0, kDisallow_CachingHint)) { |
| 492 | return nullptr; |
| 493 | } |
| 494 | return MakeCrossContextFromPixmap(context, pmap, buildMips, dstColorSpace, true); |
| 495 | } |
| 496 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 497 | // Turn the codec image into a GrTextureProxy |
| 498 | GrImageTextureMaker maker(context, codecImage.get(), kDisallow_CachingHint); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 499 | GrSamplerState samplerState( |
| 500 | GrSamplerState::WrapMode::kClamp, |
| 501 | buildMips ? GrSamplerState::Filter::kMipMap : GrSamplerState::Filter::kBilerp); |
Brian Osman | 0471a93 | 2019-02-07 13:36:56 -0500 | [diff] [blame] | 502 | SkScalar scaleAdjust[2] = { 1.0f, 1.0f }; |
| 503 | sk_sp<GrTextureProxy> proxy(maker.refTextureProxyForParams(samplerState, scaleAdjust)); |
| 504 | // Given that we disable mipmaps if non-power-of-two mipmapping isn't supported, we always |
| 505 | // expect the created texture to be unscaled. |
| 506 | SkASSERT(scaleAdjust[0] == 1.0f && scaleAdjust[1] == 1.0f); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 507 | if (!proxy) { |
| 508 | return codecImage; |
| 509 | } |
| 510 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 511 | // Flush any writes or uploads |
Greg Daniel | 4aa13e7 | 2019-04-15 14:42:20 -0400 | [diff] [blame] | 512 | context->priv().flushSurface(proxy.get()); |
Robert Phillips | acc10fa | 2019-04-01 09:50:20 -0400 | [diff] [blame] | 513 | if (!proxy->isInstantiated()) { |
| 514 | return codecImage; |
| 515 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 516 | |
Robert Phillips | 73cc4e8 | 2019-04-01 07:46:13 -0400 | [diff] [blame] | 517 | sk_sp<GrTexture> texture = sk_ref_sp(proxy->peekTexture()); |
| 518 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 519 | GrGpu* gpu = context->priv().getGpu(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 520 | sk_sp<GrSemaphore> sema = gpu->prepareTextureForCrossContextUsage(texture.get()); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 521 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 522 | auto gen = GrBackendTextureImageGenerator::Make( |
| 523 | std::move(texture), proxy->origin(), std::move(sema), codecImage->colorType(), |
| 524 | codecImage->alphaType(), codecImage->refColorSpace()); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 525 | return SkImage::MakeFromGenerator(std::move(gen)); |
| 526 | } |
| 527 | |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 528 | sk_sp<SkImage> SkImage::MakeCrossContextFromPixmap(GrContext* context, |
| 529 | const SkPixmap& originalPixmap, bool buildMips, |
| 530 | SkColorSpace* dstColorSpace, |
| 531 | bool limitToMaxTextureSize) { |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 532 | // Some backends or drivers don't support (safely) moving resources between contexts |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 533 | if (!context || !context->priv().caps()->crossContextTextureSupport()) { |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 534 | return SkImage::MakeRasterCopy(originalPixmap); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 535 | } |
| 536 | |
Greg Daniel | 9e78811 | 2018-02-08 14:29:37 -0500 | [diff] [blame] | 537 | // If we don't have access to the resource provider and gpu (i.e. in a DDL context) we will not |
| 538 | // be able to make everything needed for a GPU CrossContext image. Thus return a raster copy |
| 539 | // instead. |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 540 | if (!context->priv().resourceProvider()) { |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 541 | return SkImage::MakeRasterCopy(originalPixmap); |
Greg Daniel | 9e78811 | 2018-02-08 14:29:37 -0500 | [diff] [blame] | 542 | } |
| 543 | |
Brian Osman | 0471a93 | 2019-02-07 13:36:56 -0500 | [diff] [blame] | 544 | // If non-power-of-two mipmapping isn't supported, ignore the client's request |
| 545 | if (!context->priv().caps()->mipMapSupport()) { |
| 546 | buildMips = false; |
| 547 | } |
| 548 | |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 549 | const SkPixmap* pixmap = &originalPixmap; |
| 550 | SkAutoPixmapStorage resized; |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 551 | int maxTextureSize = context->priv().caps()->maxTextureSize(); |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 552 | int maxDim = SkTMax(originalPixmap.width(), originalPixmap.height()); |
| 553 | if (limitToMaxTextureSize && maxDim > maxTextureSize) { |
| 554 | float scale = static_cast<float>(maxTextureSize) / maxDim; |
| 555 | int newWidth = SkTMin(static_cast<int>(originalPixmap.width() * scale), maxTextureSize); |
| 556 | int newHeight = SkTMin(static_cast<int>(originalPixmap.height() * scale), maxTextureSize); |
| 557 | SkImageInfo info = originalPixmap.info().makeWH(newWidth, newHeight); |
| 558 | if (!resized.tryAlloc(info) || !originalPixmap.scalePixels(resized, kLow_SkFilterQuality)) { |
| 559 | return nullptr; |
| 560 | } |
| 561 | pixmap = &resized; |
| 562 | } |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 563 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 564 | // Turn the pixmap into a GrTextureProxy |
Brian Osman | 7dcc616 | 2019-03-25 10:12:57 -0400 | [diff] [blame] | 565 | SkBitmap bmp; |
| 566 | bmp.installPixels(*pixmap); |
| 567 | GrMipMapped mipMapped = buildMips ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 568 | sk_sp<GrTextureProxy> proxy = proxyProvider->createProxyFromBitmap(bmp, mipMapped); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 569 | if (!proxy) { |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 570 | return SkImage::MakeRasterCopy(*pixmap); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 571 | } |
| 572 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 573 | sk_sp<GrTexture> texture = sk_ref_sp(proxy->peekTexture()); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 574 | |
| 575 | // Flush any writes or uploads |
Greg Daniel | 4aa13e7 | 2019-04-15 14:42:20 -0400 | [diff] [blame] | 576 | context->priv().flushSurface(proxy.get()); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 577 | GrGpu* gpu = context->priv().getGpu(); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 578 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 579 | sk_sp<GrSemaphore> sema = gpu->prepareTextureForCrossContextUsage(texture.get()); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 580 | |
| 581 | auto gen = GrBackendTextureImageGenerator::Make(std::move(texture), proxy->origin(), |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 582 | std::move(sema), pixmap->colorType(), |
| 583 | pixmap->alphaType(), |
| 584 | pixmap->info().refColorSpace()); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 585 | return SkImage::MakeFromGenerator(std::move(gen)); |
| 586 | } |
| 587 | |
Derek Sollenberger | 7a86987 | 2017-06-27 15:37:25 -0400 | [diff] [blame] | 588 | #if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26 |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 589 | sk_sp<SkImage> SkImage::MakeFromAHardwareBuffer(AHardwareBuffer* graphicBuffer, SkAlphaType at, |
Stan Iliev | 505dd57 | 2018-09-13 14:20:03 -0400 | [diff] [blame] | 590 | sk_sp<SkColorSpace> cs, |
| 591 | GrSurfaceOrigin surfaceOrigin) { |
| 592 | auto gen = GrAHardwareBufferImageGenerator::Make(graphicBuffer, at, cs, surfaceOrigin); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 593 | return SkImage::MakeFromGenerator(std::move(gen)); |
| 594 | } |
Greg Daniel | b6c15ba | 2019-03-04 13:08:25 -0500 | [diff] [blame] | 595 | |
| 596 | sk_sp<SkImage> SkImage::MakeFromAHardwareBufferWithData(GrContext* context, |
| 597 | const SkPixmap& pixmap, |
| 598 | AHardwareBuffer* hardwareBuffer, |
| 599 | GrSurfaceOrigin surfaceOrigin) { |
| 600 | AHardwareBuffer_Desc bufferDesc; |
| 601 | AHardwareBuffer_describe(hardwareBuffer, &bufferDesc); |
| 602 | |
| 603 | if (!SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE)) { |
| 604 | return nullptr; |
| 605 | } |
| 606 | |
| 607 | GrBackendFormat backendFormat = GrAHardwareBufferUtils::GetBackendFormat(context, |
| 608 | hardwareBuffer, |
| 609 | bufferDesc.format, |
| 610 | true); |
| 611 | |
| 612 | if (!backendFormat.isValid()) { |
| 613 | return nullptr; |
| 614 | } |
| 615 | |
| 616 | GrAHardwareBufferUtils::DeleteImageProc deleteImageProc = nullptr; |
Stan Iliev | a56b04a | 2019-08-01 14:22:34 -0400 | [diff] [blame] | 617 | GrAHardwareBufferUtils::UpdateImageProc updateImageProc = nullptr; |
| 618 | GrAHardwareBufferUtils::TexImageCtx deleteImageCtx = nullptr; |
Greg Daniel | b6c15ba | 2019-03-04 13:08:25 -0500 | [diff] [blame] | 619 | |
| 620 | GrBackendTexture backendTexture = |
| 621 | GrAHardwareBufferUtils::MakeBackendTexture(context, hardwareBuffer, |
| 622 | bufferDesc.width, bufferDesc.height, |
Stan Iliev | a56b04a | 2019-08-01 14:22:34 -0400 | [diff] [blame] | 623 | &deleteImageProc, &updateImageProc, |
| 624 | &deleteImageCtx, false, backendFormat, true); |
Greg Daniel | b6c15ba | 2019-03-04 13:08:25 -0500 | [diff] [blame] | 625 | if (!backendTexture.isValid()) { |
| 626 | return nullptr; |
| 627 | } |
| 628 | SkASSERT(deleteImageProc); |
| 629 | |
| 630 | SkColorType colorType = |
| 631 | GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(bufferDesc.format); |
| 632 | |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 633 | GrColorType grColorType = SkColorTypeToGrColorType(colorType); |
Greg Daniel | b6c15ba | 2019-03-04 13:08:25 -0500 | [diff] [blame] | 634 | |
| 635 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 636 | if (!proxyProvider) { |
| 637 | deleteImageProc(deleteImageCtx); |
| 638 | return nullptr; |
| 639 | } |
| 640 | |
| 641 | sk_sp<GrTextureProxy> proxy = |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 642 | proxyProvider->wrapBackendTexture(backendTexture, grColorType, surfaceOrigin, |
Greg Daniel | b6c15ba | 2019-03-04 13:08:25 -0500 | [diff] [blame] | 643 | kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, |
| 644 | kRW_GrIOType, deleteImageProc, deleteImageCtx); |
| 645 | if (!proxy) { |
| 646 | deleteImageProc(deleteImageCtx); |
| 647 | return nullptr; |
| 648 | } |
| 649 | |
| 650 | sk_sp<SkColorSpace> cs = pixmap.refColorSpace(); |
| 651 | SkAlphaType at = pixmap.alphaType(); |
| 652 | |
| 653 | sk_sp<SkImage> image = sk_make_sp<SkImage_Gpu>(sk_ref_sp(context), kNeedNewImageUniqueID, at, |
| 654 | proxy, cs); |
| 655 | if (!image) { |
| 656 | return nullptr; |
| 657 | } |
| 658 | |
| 659 | GrDrawingManager* drawingManager = context->priv().drawingManager(); |
| 660 | if (!drawingManager) { |
| 661 | return nullptr; |
| 662 | } |
| 663 | |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 664 | sk_sp<GrTextureContext> texContext = |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 665 | drawingManager->makeTextureContext(proxy, SkColorTypeToGrColorType(pixmap.colorType()), |
| 666 | pixmap.alphaType(), cs); |
Greg Daniel | b6c15ba | 2019-03-04 13:08:25 -0500 | [diff] [blame] | 667 | if (!texContext) { |
| 668 | return nullptr; |
| 669 | } |
| 670 | |
| 671 | SkImageInfo srcInfo = SkImageInfo::Make(bufferDesc.width, bufferDesc.height, colorType, at, |
| 672 | std::move(cs)); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 673 | texContext->writePixels(srcInfo, pixmap.addr(0, 0), pixmap.rowBytes(), {0, 0}); |
Greg Daniel | b6c15ba | 2019-03-04 13:08:25 -0500 | [diff] [blame] | 674 | |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 675 | GrFlushInfo info; |
| 676 | info.fFlags = kSyncCpu_GrFlushFlag; |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 677 | GrSurfaceProxy* p[1] = {proxy.get()}; |
Greg Daniel | 797efca | 2019-05-09 14:04:20 -0400 | [diff] [blame] | 678 | drawingManager->flush(p, 1, SkSurface::BackendSurfaceAccess::kNoAccess, info, |
| 679 | GrPrepareForExternalIORequests()); |
Greg Daniel | b6c15ba | 2019-03-04 13:08:25 -0500 | [diff] [blame] | 680 | |
| 681 | return image; |
| 682 | } |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 683 | #endif |
| 684 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 685 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 686 | |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 687 | bool SkImage::MakeBackendTextureFromSkImage(GrContext* ctx, |
| 688 | sk_sp<SkImage> image, |
| 689 | GrBackendTexture* backendTexture, |
| 690 | BackendTextureReleaseProc* releaseProc) { |
| 691 | if (!image || !ctx || !backendTexture || !releaseProc) { |
| 692 | return false; |
| 693 | } |
| 694 | |
| 695 | // Ensure we have a texture backed image. |
| 696 | if (!image->isTextureBacked()) { |
Brian Osman | d566e2e | 2019-08-14 13:19:04 -0400 | [diff] [blame] | 697 | image = image->makeTextureImage(ctx); |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 698 | if (!image) { |
| 699 | return false; |
| 700 | } |
| 701 | } |
| 702 | GrTexture* texture = image->getTexture(); |
Eric Karl | 36591e5 | 2018-01-19 13:45:02 -0800 | [diff] [blame] | 703 | if (!texture) { |
| 704 | // In context-loss cases, we may not have a texture. |
| 705 | return false; |
| 706 | } |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 707 | |
| 708 | // If the image's context doesn't match the provided context, fail. |
| 709 | if (texture->getContext() != ctx) { |
| 710 | return false; |
| 711 | } |
| 712 | |
| 713 | // Flush any pending IO on the texture. |
Greg Daniel | 4aa13e7 | 2019-04-15 14:42:20 -0400 | [diff] [blame] | 714 | ctx->priv().flushSurface(as_IB(image)->peekProxy()); |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 715 | SkASSERT(!texture->surfacePriv().hasPendingIO()); |
| 716 | |
| 717 | // We must make a copy of the image if the image is not unique, if the GrTexture owned by the |
| 718 | // image is not unique, or if the texture wraps an external object. |
| 719 | if (!image->unique() || !texture->surfacePriv().hasUniqueRef() || |
| 720 | texture->resourcePriv().refsWrappedObjects()) { |
| 721 | // onMakeSubset will always copy the image. |
Robert Phillips | 6603a17 | 2019-03-05 12:35:44 -0500 | [diff] [blame] | 722 | image = as_IB(image)->onMakeSubset(ctx, image->bounds()); |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 723 | if (!image) { |
| 724 | return false; |
| 725 | } |
| 726 | |
| 727 | texture = image->getTexture(); |
Eric Karl | 36591e5 | 2018-01-19 13:45:02 -0800 | [diff] [blame] | 728 | if (!texture) { |
| 729 | return false; |
| 730 | } |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 731 | |
| 732 | // Flush to ensure that the copy is completed before we return the texture. |
Greg Daniel | 4aa13e7 | 2019-04-15 14:42:20 -0400 | [diff] [blame] | 733 | ctx->priv().flushSurface(as_IB(image)->peekProxy()); |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 734 | SkASSERT(!texture->surfacePriv().hasPendingIO()); |
| 735 | } |
| 736 | |
| 737 | SkASSERT(!texture->resourcePriv().refsWrappedObjects()); |
| 738 | SkASSERT(texture->surfacePriv().hasUniqueRef()); |
| 739 | SkASSERT(image->unique()); |
| 740 | |
| 741 | // Take a reference to the GrTexture and release the image. |
| 742 | sk_sp<GrTexture> textureRef(SkSafeRef(texture)); |
| 743 | image = nullptr; |
| 744 | |
| 745 | // Steal the backend texture from the GrTexture, releasing the GrTexture in the process. |
| 746 | return GrTexture::StealBackendTexture(std::move(textureRef), backendTexture, releaseProc); |
| 747 | } |