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