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