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