Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
| 8 | #include <cstddef> |
| 9 | #include <cstring> |
| 10 | #include <type_traits> |
| 11 | |
Brian Salomon | c2a9a97 | 2020-09-15 11:24:28 -0400 | [diff] [blame] | 12 | #include "include/core/SkYUVAPixmaps.h" |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 13 | #include "include/gpu/GrDirectContext.h" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 14 | #include "include/gpu/GrRecordingContext.h" |
Brian Salomon | c1a249d | 2020-10-19 10:55:45 -0400 | [diff] [blame] | 15 | #include "include/gpu/GrYUVABackendTextures.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/core/SkAutoPixmapStorage.h" |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 17 | #include "src/core/SkMipmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/core/SkScopeExit.h" |
| 19 | #include "src/gpu/GrClip.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/gpu/GrGpu.h" |
Adlai Holler | 302e8fb | 2020-09-14 11:58:06 -0400 | [diff] [blame] | 22 | #include "src/gpu/GrImageContextPriv.h" |
Brian Salomon | 8670c98 | 2020-12-08 16:39:32 -0500 | [diff] [blame] | 23 | #include "src/gpu/GrProxyProvider.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/gpu/GrRecordingContextPriv.h" |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 25 | #include "src/gpu/GrSurfaceDrawContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "src/gpu/SkGr.h" |
Brian Salomon | d468867 | 2021-04-28 15:42:09 -0400 | [diff] [blame] | 27 | #include "src/gpu/effects/GrBicubicEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "src/gpu/effects/GrYUVtoRGBEffect.h" |
| 29 | #include "src/image/SkImage_Gpu.h" |
| 30 | #include "src/image/SkImage_GpuYUVA.h" |
Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 31 | |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 32 | static constexpr auto kAssumedColorType = kRGBA_8888_SkColorType; |
| 33 | |
Adlai Holler | 302e8fb | 2020-09-14 11:58:06 -0400 | [diff] [blame] | 34 | SkImage_GpuYUVA::SkImage_GpuYUVA(sk_sp<GrImageContext> context, |
Brian Salomon | c505a45 | 2020-04-06 10:29:02 -0400 | [diff] [blame] | 35 | uint32_t uniqueID, |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 36 | GrYUVATextureProxies proxies, |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 37 | sk_sp<SkColorSpace> imageColorSpace) |
Adlai Holler | 302e8fb | 2020-09-14 11:58:06 -0400 | [diff] [blame] | 38 | : INHERITED(std::move(context), |
Brian Salomon | 9a56eb7 | 2021-04-20 16:52:11 -0400 | [diff] [blame] | 39 | SkImageInfo::Make(proxies.yuvaInfo().dimensions(), |
| 40 | kAssumedColorType, |
| 41 | // If an alpha channel is present we always use kPremul. This |
| 42 | // is because, although the planar data is always un-premul, |
| 43 | // the final interleaved RGBA sample produced in the shader |
| 44 | // is premul (and similar if flattened via asView). |
| 45 | proxies.yuvaInfo().hasAlpha() ? kPremul_SkAlphaType |
| 46 | : kOpaque_SkAlphaType, |
| 47 | std::move(imageColorSpace)), |
| 48 | uniqueID) |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 49 | , fYUVAProxies(std::move(proxies)) { |
| 50 | // The caller should have checked this, just verifying. |
| 51 | SkASSERT(fYUVAProxies.isValid()); |
Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 52 | } |
| 53 | |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 54 | // For onMakeColorSpace() |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 55 | SkImage_GpuYUVA::SkImage_GpuYUVA(sk_sp<GrImageContext> context, |
| 56 | const SkImage_GpuYUVA* image, |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 57 | sk_sp<SkColorSpace> targetCS) |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 58 | : INHERITED(std::move(context), |
Brian Salomon | 9a56eb7 | 2021-04-20 16:52:11 -0400 | [diff] [blame] | 59 | image->imageInfo().makeColorSpace(std::move(targetCS)), |
| 60 | kNeedNewImageUniqueID) |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 61 | , fYUVAProxies(image->fYUVAProxies) |
Brian Salomon | 5ad6fd3 | 2019-03-21 15:30:08 -0400 | [diff] [blame] | 62 | // Since null fFromColorSpace means no GrColorSpaceXform, we turn a null |
| 63 | // image->refColorSpace() into an explicit SRGB. |
| 64 | , fFromColorSpace(image->colorSpace() ? image->refColorSpace() : SkColorSpace::MakeSRGB()) { |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 65 | } |
| 66 | |
Robert Phillips | 8defcc1 | 2019-03-05 15:58:59 -0500 | [diff] [blame] | 67 | bool SkImage_GpuYUVA::setupMipmapsForPlanes(GrRecordingContext* context) const { |
| 68 | if (!context || !fContext->priv().matches(context)) { |
| 69 | return false; |
| 70 | } |
Brian Salomon | 69100f0 | 2020-07-21 10:49:25 -0400 | [diff] [blame] | 71 | if (!context->priv().caps()->mipmapSupport()) { |
Brian Salomon | 4759dca | 2020-07-14 15:44:15 -0400 | [diff] [blame] | 72 | // We succeed in this case by doing nothing. |
| 73 | return true; |
| 74 | } |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 75 | int n = fYUVAProxies.yuvaInfo().numPlanes(); |
| 76 | sk_sp<GrSurfaceProxy> newProxies[4]; |
| 77 | for (int i = 0; i < n; ++i) { |
| 78 | auto* t = fYUVAProxies.proxy(i)->asTextureProxy(); |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 79 | if (t->mipmapped() == GrMipmapped::kNo && (t->width() > 1 || t->height() > 1)) { |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 80 | auto newView = GrCopyBaseMipMapToView(context, fYUVAProxies.makeView(i)); |
| 81 | if (!newView) { |
Jim Van Verth | f542cab | 2018-11-07 12:08:21 -0500 | [diff] [blame] | 82 | return false; |
| 83 | } |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 84 | SkASSERT(newView.swizzle() == fYUVAProxies.makeView(i).swizzle()); |
| 85 | newProxies[i] = newView.detachProxy(); |
Brian Salomon | 4759dca | 2020-07-14 15:44:15 -0400 | [diff] [blame] | 86 | } else { |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 87 | newProxies[i] = fYUVAProxies.refProxy(i); |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 88 | } |
| 89 | } |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 90 | fYUVAProxies = GrYUVATextureProxies(fYUVAProxies.yuvaInfo(), |
| 91 | newProxies, |
| 92 | fYUVAProxies.textureOrigin()); |
| 93 | SkASSERT(fYUVAProxies.isValid()); |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 94 | return true; |
| 95 | } |
| 96 | |
Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 97 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
Jim Van Verth | f49262d | 2018-10-02 12:07:20 -0400 | [diff] [blame] | 98 | |
Adlai Holler | 40ad5fd | 2020-07-22 10:22:12 -0400 | [diff] [blame] | 99 | GrSemaphoresSubmitted SkImage_GpuYUVA::onFlush(GrDirectContext* dContext, const GrFlushInfo& info) { |
| 100 | if (!fContext->priv().matches(dContext) || dContext->abandoned()) { |
Greg Daniel | 55822f1 | 2020-05-26 11:26:45 -0400 | [diff] [blame] | 101 | if (info.fSubmittedProc) { |
| 102 | info.fSubmittedProc(info.fSubmittedContext, false); |
| 103 | } |
| 104 | if (info.fFinishedProc) { |
| 105 | info.fFinishedProc(info.fFinishedContext); |
| 106 | } |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 107 | return GrSemaphoresSubmitted::kNo; |
| 108 | } |
| 109 | |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 110 | GrSurfaceProxy* proxies[SkYUVAInfo::kMaxPlanes] = {}; |
Brian Salomon | d0f35c2 | 2021-04-16 14:52:49 -0400 | [diff] [blame] | 111 | size_t numProxies = fYUVAProxies.numPlanes(); |
| 112 | for (size_t i = 0; i < numProxies; ++i) { |
| 113 | proxies[i] = fYUVAProxies.proxy(i); |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 114 | } |
Robert Phillips | 80bfda8 | 2020-11-12 09:23:36 -0500 | [diff] [blame] | 115 | return dContext->priv().flushSurfaces({proxies, numProxies}, |
| 116 | SkSurface::BackendSurfaceAccess::kNoAccess, |
| 117 | info); |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 118 | } |
| 119 | |
Brian Salomon | d0f35c2 | 2021-04-16 14:52:49 -0400 | [diff] [blame] | 120 | bool SkImage_GpuYUVA::onHasMipmaps() const { return fYUVAProxies.mipmapped() == GrMipmapped::kYes; } |
Brian Salomon | 7d2757f | 2021-01-26 17:46:50 -0500 | [diff] [blame] | 121 | |
Brian Salomon | 650dd08 | 2021-02-11 08:59:03 -0500 | [diff] [blame] | 122 | size_t SkImage_GpuYUVA::onTextureSize() const { |
Brian Salomon | 650dd08 | 2021-02-11 08:59:03 -0500 | [diff] [blame] | 123 | size_t size = 0; |
| 124 | for (int i = 0; i < fYUVAProxies.numPlanes(); ++i) { |
| 125 | size += fYUVAProxies.proxy(i)->gpuMemorySize(); |
| 126 | } |
| 127 | return size; |
| 128 | } |
| 129 | |
Brian Salomon | d0924f3 | 2021-02-03 10:15:31 -0500 | [diff] [blame] | 130 | sk_sp<SkImage> SkImage_GpuYUVA::onMakeColorTypeAndColorSpace(SkColorType, |
| 131 | sk_sp<SkColorSpace> targetCS, |
| 132 | GrDirectContext* direct) const { |
Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 133 | // We explicitly ignore color type changes, for now. |
| 134 | |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 135 | // we may need a mutex here but for now we expect usage to be in a single thread |
| 136 | if (fOnMakeColorSpaceTarget && |
Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 137 | SkColorSpace::Equals(targetCS.get(), fOnMakeColorSpaceTarget.get())) { |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 138 | return fOnMakeColorSpaceResult; |
| 139 | } |
Adlai Holler | 3a22017 | 2020-07-15 10:37:50 -0400 | [diff] [blame] | 140 | sk_sp<SkImage> result = sk_sp<SkImage>(new SkImage_GpuYUVA(sk_ref_sp(direct), this, targetCS)); |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 141 | if (result) { |
Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 142 | fOnMakeColorSpaceTarget = targetCS; |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 143 | fOnMakeColorSpaceResult = result; |
| 144 | } |
| 145 | return result; |
| 146 | } |
| 147 | |
Brian Osman | d514837 | 2019-08-14 16:14:51 -0400 | [diff] [blame] | 148 | sk_sp<SkImage> SkImage_GpuYUVA::onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const { |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 149 | return sk_sp<SkImage>(new SkImage_GpuYUVA(fContext, this, std::move(newCS))); |
Brian Osman | d514837 | 2019-08-14 16:14:51 -0400 | [diff] [blame] | 150 | } |
| 151 | |
Brian Salomon | d0f35c2 | 2021-04-16 14:52:49 -0400 | [diff] [blame] | 152 | std::tuple<GrSurfaceProxyView, GrColorType> SkImage_GpuYUVA::onAsView( |
| 153 | GrRecordingContext* context, |
| 154 | GrMipmapped mipmapped, |
| 155 | GrImageTexGenPolicy) const { |
| 156 | if (!fContext->priv().matches(context)) { |
| 157 | return {}; |
| 158 | } |
| 159 | auto sfc = GrSurfaceFillContext::Make(context, |
| 160 | this->imageInfo(), |
| 161 | SkBackingFit::kExact, |
| 162 | /*sample count*/ 1, |
| 163 | mipmapped, |
| 164 | GrProtected::kNo, |
| 165 | kTopLeft_GrSurfaceOrigin, |
| 166 | SkBudgeted::kYes); |
| 167 | if (!sfc) { |
Brian Salomon | d0924f3 | 2021-02-03 10:15:31 -0500 | [diff] [blame] | 168 | return {}; |
| 169 | } |
| 170 | |
| 171 | const GrCaps& caps = *context->priv().caps(); |
Brian Salomon | d0f35c2 | 2021-04-16 14:52:49 -0400 | [diff] [blame] | 172 | auto fp = GrYUVtoRGBEffect::Make(fYUVAProxies, GrSamplerState::Filter::kNearest, caps); |
| 173 | if (fFromColorSpace) { |
Brian Salomon | d0924f3 | 2021-02-03 10:15:31 -0500 | [diff] [blame] | 174 | fp = GrColorSpaceXformEffect::Make(std::move(fp), |
Brian Salomon | d0f35c2 | 2021-04-16 14:52:49 -0400 | [diff] [blame] | 175 | fFromColorSpace.get(), this->alphaType(), |
| 176 | this->colorSpace() , this->alphaType()); |
Brian Salomon | d0924f3 | 2021-02-03 10:15:31 -0500 | [diff] [blame] | 177 | } |
Brian Salomon | d0f35c2 | 2021-04-16 14:52:49 -0400 | [diff] [blame] | 178 | sfc->fillWithFP(std::move(fp)); |
Brian Salomon | d0924f3 | 2021-02-03 10:15:31 -0500 | [diff] [blame] | 179 | |
Robert Phillips | 9e42ec3 | 2021-05-17 18:01:49 -0400 | [diff] [blame] | 180 | return {sfc->readSurfaceView(), sfc->colorInfo().colorType()}; |
Brian Salomon | d0924f3 | 2021-02-03 10:15:31 -0500 | [diff] [blame] | 181 | } |
| 182 | |
Brian Salomon | 6ef10e9 | 2021-04-20 09:11:24 -0400 | [diff] [blame] | 183 | std::unique_ptr<GrFragmentProcessor> SkImage_GpuYUVA::onAsFragmentProcessor( |
| 184 | GrRecordingContext* context, |
| 185 | SkSamplingOptions sampling, |
| 186 | const SkTileMode tileModes[2], |
| 187 | const SkMatrix& m, |
| 188 | const SkRect* subset, |
| 189 | const SkRect* domain) const { |
| 190 | if (!fContext->priv().matches(context)) { |
| 191 | return {}; |
| 192 | } |
Brian Salomon | 6ef10e9 | 2021-04-20 09:11:24 -0400 | [diff] [blame] | 193 | auto wmx = SkTileModeToWrapMode(tileModes[0]); |
| 194 | auto wmy = SkTileModeToWrapMode(tileModes[1]); |
Brian Salomon | 6ef10e9 | 2021-04-20 09:11:24 -0400 | [diff] [blame] | 195 | GrSamplerState sampler(wmx, wmy, sampling.filter, sampling.mipmap); |
Brian Salomon | d468867 | 2021-04-28 15:42:09 -0400 | [diff] [blame] | 196 | if (sampler.mipmapped() == GrMipmapped::kYes && !this->setupMipmapsForPlanes(context)) { |
| 197 | sampler.setMipmapMode(GrSamplerState::MipmapMode::kNone); |
| 198 | } |
| 199 | |
| 200 | const auto& yuvM = sampling.useCubic ? SkMatrix::I() : m; |
| 201 | auto fp = GrYUVtoRGBEffect::Make(fYUVAProxies, |
| 202 | sampler, |
| 203 | *context->priv().caps(), |
| 204 | yuvM, |
| 205 | subset, |
| 206 | domain); |
| 207 | if (sampling.useCubic) { |
| 208 | fp = GrBicubicEffect::Make(std::move(fp), |
| 209 | this->alphaType(), |
| 210 | m, |
| 211 | sampling.cubic, |
| 212 | GrBicubicEffect::Direction::kXY); |
| 213 | } |
| 214 | if (fFromColorSpace) { |
| 215 | fp = GrColorSpaceXformEffect::Make(std::move(fp), |
| 216 | fFromColorSpace.get(), this->alphaType(), |
| 217 | this->colorSpace() , this->alphaType()); |
| 218 | } |
| 219 | return fp; |
Brian Salomon | 6ef10e9 | 2021-04-20 09:11:24 -0400 | [diff] [blame] | 220 | } |
| 221 | |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 222 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 223 | |
Brian Salomon | c1a249d | 2020-10-19 10:55:45 -0400 | [diff] [blame] | 224 | sk_sp<SkImage> SkImage::MakeFromYUVATextures(GrRecordingContext* context, |
| 225 | const GrYUVABackendTextures& yuvaTextures, |
| 226 | sk_sp<SkColorSpace> imageColorSpace, |
| 227 | TextureReleaseProc textureReleaseProc, |
| 228 | ReleaseContext releaseContext) { |
Brian Salomon | 694ff17 | 2020-11-04 16:54:28 -0500 | [diff] [blame] | 229 | auto releaseHelper = GrRefCntedCallback::Make(textureReleaseProc, releaseContext); |
Brian Salomon | c1a249d | 2020-10-19 10:55:45 -0400 | [diff] [blame] | 230 | |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 231 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 232 | int numPlanes = yuvaTextures.yuvaInfo().numPlanes(); |
| 233 | sk_sp<GrSurfaceProxy> proxies[SkYUVAInfo::kMaxPlanes]; |
| 234 | for (int plane = 0; plane < numPlanes; ++plane) { |
| 235 | proxies[plane] = proxyProvider->wrapBackendTexture(yuvaTextures.texture(plane), |
| 236 | kBorrow_GrWrapOwnership, |
| 237 | GrWrapCacheable::kNo, |
| 238 | kRead_GrIOType, |
| 239 | releaseHelper); |
| 240 | if (!proxies[plane]) { |
| 241 | return {}; |
| 242 | } |
| 243 | } |
| 244 | GrYUVATextureProxies yuvaProxies(yuvaTextures.yuvaInfo(), |
| 245 | proxies, |
| 246 | yuvaTextures.textureOrigin()); |
Brian Salomon | c1a249d | 2020-10-19 10:55:45 -0400 | [diff] [blame] | 247 | |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 248 | if (!yuvaProxies.isValid()) { |
Brian Salomon | c1a249d | 2020-10-19 10:55:45 -0400 | [diff] [blame] | 249 | return nullptr; |
| 250 | } |
| 251 | |
| 252 | return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(context), |
Brian Salomon | c1a249d | 2020-10-19 10:55:45 -0400 | [diff] [blame] | 253 | kNeedNewImageUniqueID, |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 254 | yuvaProxies, |
Brian Salomon | c1a249d | 2020-10-19 10:55:45 -0400 | [diff] [blame] | 255 | imageColorSpace); |
| 256 | } |
| 257 | |
Adlai Holler | 302e8fb | 2020-09-14 11:58:06 -0400 | [diff] [blame] | 258 | sk_sp<SkImage> SkImage::MakeFromYUVAPixmaps(GrRecordingContext* context, |
Brian Salomon | c2a9a97 | 2020-09-15 11:24:28 -0400 | [diff] [blame] | 259 | const SkYUVAPixmaps& pixmaps, |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 260 | GrMipmapped buildMips, |
Brian Salomon | c2a9a97 | 2020-09-15 11:24:28 -0400 | [diff] [blame] | 261 | bool limitToMaxTextureSize, |
| 262 | sk_sp<SkColorSpace> imageColorSpace) { |
| 263 | if (!context) { |
| 264 | return nullptr; // until we impl this for raster backend |
| 265 | } |
| 266 | |
| 267 | if (!pixmaps.isValid()) { |
| 268 | return nullptr; |
| 269 | } |
| 270 | |
Brian Salomon | c2a9a97 | 2020-09-15 11:24:28 -0400 | [diff] [blame] | 271 | if (!context->priv().caps()->mipmapSupport()) { |
| 272 | buildMips = GrMipMapped::kNo; |
| 273 | } |
| 274 | |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 275 | // Resize the pixmaps if necessary. |
Brian Salomon | c2a9a97 | 2020-09-15 11:24:28 -0400 | [diff] [blame] | 276 | int numPlanes = pixmaps.numPlanes(); |
| 277 | int maxTextureSize = context->priv().caps()->maxTextureSize(); |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 278 | int maxDim = std::max(pixmaps.yuvaInfo().width(), pixmaps.yuvaInfo().height()); |
| 279 | |
| 280 | SkYUVAPixmaps tempPixmaps; |
| 281 | const SkYUVAPixmaps* pixmapsToUpload = &pixmaps; |
| 282 | // We assume no plane is larger than the image size (and at least one plane is as big). |
| 283 | if (maxDim > maxTextureSize) { |
| 284 | if (!limitToMaxTextureSize) { |
Brian Salomon | b602550 | 2021-01-12 20:37:49 -0500 | [diff] [blame] | 285 | return nullptr; |
| 286 | } |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 287 | float scale = static_cast<float>(maxTextureSize)/maxDim; |
| 288 | SkISize newDimensions = { |
| 289 | std::min(static_cast<int>(pixmaps.yuvaInfo().width() *scale), maxTextureSize), |
| 290 | std::min(static_cast<int>(pixmaps.yuvaInfo().height()*scale), maxTextureSize) |
| 291 | }; |
| 292 | SkYUVAInfo newInfo = pixmaps.yuvaInfo().makeDimensions(newDimensions); |
| 293 | SkYUVAPixmapInfo newPixmapInfo(newInfo, pixmaps.dataType(), /*row bytes*/ nullptr); |
| 294 | tempPixmaps = SkYUVAPixmaps::Allocate(newPixmapInfo); |
Mike Reed | 5ec2238 | 2021-01-14 21:59:01 -0500 | [diff] [blame] | 295 | SkSamplingOptions sampling(SkFilterMode::kLinear); |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 296 | if (!tempPixmaps.isValid()) { |
| 297 | return nullptr; |
| 298 | } |
| 299 | for (int i = 0; i < numPlanes; ++i) { |
| 300 | if (!pixmaps.plane(i).scalePixels(tempPixmaps.plane(i), sampling)) { |
| 301 | return nullptr; |
| 302 | } |
| 303 | } |
| 304 | pixmapsToUpload = &tempPixmaps; |
Brian Salomon | b602550 | 2021-01-12 20:37:49 -0500 | [diff] [blame] | 305 | } |
| 306 | |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 307 | // Convert to texture proxies. |
| 308 | GrSurfaceProxyView views[SkYUVAInfo::kMaxPlanes]; |
| 309 | GrColorType pixmapColorTypes[SkYUVAInfo::kMaxPlanes]; |
| 310 | for (int i = 0; i < numPlanes; ++i) { |
| 311 | // Turn the pixmap into a GrTextureProxy |
| 312 | SkBitmap bmp; |
| 313 | bmp.installPixels(pixmapsToUpload->plane(i)); |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 314 | std::tie(views[i], std::ignore) = GrMakeUncachedBitmapProxyView(context, bmp, buildMips); |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 315 | if (!views[i]) { |
| 316 | return nullptr; |
| 317 | } |
| 318 | pixmapColorTypes[i] = SkColorTypeToGrColorType(bmp.colorType()); |
| 319 | } |
| 320 | |
| 321 | GrYUVATextureProxies yuvaProxies(pixmapsToUpload->yuvaInfo(), views, pixmapColorTypes); |
| 322 | SkASSERT(yuvaProxies.isValid()); |
Brian Salomon | c2a9a97 | 2020-09-15 11:24:28 -0400 | [diff] [blame] | 323 | return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(context), |
Brian Salomon | c2a9a97 | 2020-09-15 11:24:28 -0400 | [diff] [blame] | 324 | kNeedNewImageUniqueID, |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 325 | std::move(yuvaProxies), |
Brian Salomon | c2a9a97 | 2020-09-15 11:24:28 -0400 | [diff] [blame] | 326 | std::move(imageColorSpace)); |
Jim Van Verth | c8429ad | 2018-11-20 11:12:37 -0500 | [diff] [blame] | 327 | } |
| 328 | |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 329 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | f143274 | 2020-11-09 15:40:27 -0500 | [diff] [blame] | 330 | |
Adlai Holler | c44e21f | 2021-02-22 14:08:07 -0500 | [diff] [blame] | 331 | sk_sp<SkImage> SkImage::MakePromiseYUVATexture(sk_sp<GrContextThreadSafeProxy> threadSafeProxy, |
| 332 | const GrYUVABackendTextureInfo& backendTextureInfo, |
| 333 | sk_sp<SkColorSpace> imageColorSpace, |
| 334 | PromiseImageTextureFulfillProc textureFulfillProc, |
| 335 | PromiseImageTextureReleaseProc textureReleaseProc, |
| 336 | PromiseImageTextureContext textureContexts[]) { |
| 337 | if (!backendTextureInfo.isValid()) { |
Brian Salomon | 98e17bf | 2020-11-16 10:31:49 -0500 | [diff] [blame] | 338 | return nullptr; |
| 339 | } |
| 340 | |
| 341 | SkISize planeDimensions[SkYUVAInfo::kMaxPlanes]; |
Adlai Holler | c44e21f | 2021-02-22 14:08:07 -0500 | [diff] [blame] | 342 | int n = backendTextureInfo.yuvaInfo().planeDimensions(planeDimensions); |
Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 343 | |
Brian Salomon | f143274 | 2020-11-09 15:40:27 -0500 | [diff] [blame] | 344 | // Our contract is that we will always call the release proc even on failure. |
| 345 | // We use the helper to convey the context, so we need to ensure make doesn't fail. |
| 346 | textureReleaseProc = textureReleaseProc ? textureReleaseProc : [](void*) {}; |
| 347 | sk_sp<GrRefCntedCallback> releaseHelpers[4]; |
Brian Salomon | 98e17bf | 2020-11-16 10:31:49 -0500 | [diff] [blame] | 348 | for (int i = 0; i < n; ++i) { |
Brian Salomon | f143274 | 2020-11-09 15:40:27 -0500 | [diff] [blame] | 349 | releaseHelpers[i] = GrRefCntedCallback::Make(textureReleaseProc, textureContexts[i]); |
| 350 | } |
| 351 | |
Adlai Holler | dced31f | 2021-02-19 12:33:46 -0500 | [diff] [blame] | 352 | if (!threadSafeProxy) { |
Brian Salomon | be5a093 | 2018-12-10 10:03:26 -0500 | [diff] [blame] | 353 | return nullptr; |
Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 354 | } |
| 355 | |
Adlai Holler | c44e21f | 2021-02-22 14:08:07 -0500 | [diff] [blame] | 356 | SkAlphaType at = backendTextureInfo.yuvaInfo().hasAlpha() ? kPremul_SkAlphaType |
| 357 | : kOpaque_SkAlphaType; |
| 358 | SkImageInfo info = SkImageInfo::Make(backendTextureInfo.yuvaInfo().dimensions(), |
Brian Salomon | 98e17bf | 2020-11-16 10:31:49 -0500 | [diff] [blame] | 359 | kAssumedColorType, at, imageColorSpace); |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 360 | if (!SkImageInfoIsValid(info)) { |
| 361 | return nullptr; |
| 362 | } |
| 363 | |
Brian Salomon | 98e17bf | 2020-11-16 10:31:49 -0500 | [diff] [blame] | 364 | // Make a lazy proxy for each plane and wrap in a view. |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 365 | sk_sp<GrSurfaceProxy> proxies[4]; |
| 366 | for (int i = 0; i < n; ++i) { |
Adlai Holler | c44e21f | 2021-02-22 14:08:07 -0500 | [diff] [blame] | 367 | proxies[i] = SkImage_GpuBase::MakePromiseImageLazyProxy(threadSafeProxy.get(), |
| 368 | planeDimensions[i], |
| 369 | backendTextureInfo.planeFormat(i), |
| 370 | GrMipmapped::kNo, |
| 371 | textureFulfillProc, |
| 372 | std::move(releaseHelpers[i])); |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 373 | if (!proxies[i]) { |
Jim Van Verth | f00b162 | 2018-10-10 13:03:23 -0400 | [diff] [blame] | 374 | return nullptr; |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 375 | } |
| 376 | } |
Adlai Holler | c44e21f | 2021-02-22 14:08:07 -0500 | [diff] [blame] | 377 | GrYUVATextureProxies yuvaTextureProxies(backendTextureInfo.yuvaInfo(), |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 378 | proxies, |
Adlai Holler | c44e21f | 2021-02-22 14:08:07 -0500 | [diff] [blame] | 379 | backendTextureInfo.textureOrigin()); |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 380 | SkASSERT(yuvaTextureProxies.isValid()); |
Adlai Holler | dced31f | 2021-02-19 12:33:46 -0500 | [diff] [blame] | 381 | sk_sp<GrImageContext> ctx(GrImageContextPriv::MakeForPromiseImage(std::move(threadSafeProxy))); |
| 382 | return sk_make_sp<SkImage_GpuYUVA>(std::move(ctx), |
Brian Salomon | 0857bef | 2021-01-13 15:54:04 -0500 | [diff] [blame] | 383 | kNeedNewImageUniqueID, |
| 384 | std::move(yuvaTextureProxies), |
| 385 | std::move(imageColorSpace)); |
Jim Van Verth | 8bbce0e | 2018-10-08 14:34:52 -0400 | [diff] [blame] | 386 | } |