blob: eedd3991c5fe80945c837489a9799cd631cf7aac [file] [log] [blame]
Jim Van Verthf49262d2018-10-02 12:07:20 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkYUVASizeInfo.h"
13#include "include/gpu/GrContext.h"
14#include "include/gpu/GrTexture.h"
15#include "include/private/GrRecordingContext.h"
16#include "src/core/SkAutoPixmapStorage.h"
17#include "src/core/SkMipMap.h"
18#include "src/core/SkScopeExit.h"
Greg Daniel82c6b102020-01-21 10:33:22 -050019#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrClip.h"
21#include "src/gpu/GrContextPriv.h"
22#include "src/gpu/GrGpu.h"
23#include "src/gpu/GrRecordingContextPriv.h"
24#include "src/gpu/GrRenderTargetContext.h"
25#include "src/gpu/GrTextureProducer.h"
26#include "src/gpu/SkGr.h"
27#include "src/gpu/effects/GrYUVtoRGBEffect.h"
28#include "src/image/SkImage_Gpu.h"
29#include "src/image/SkImage_GpuYUVA.h"
Jim Van Verthf49262d2018-10-02 12:07:20 -040030
Brian Salomon5ad6fd32019-03-21 15:30:08 -040031static constexpr auto kAssumedColorType = kRGBA_8888_SkColorType;
32
Brian Salomon9f2b86c2019-10-22 10:37:46 -040033SkImage_GpuYUVA::SkImage_GpuYUVA(sk_sp<GrContext> context, SkISize size, uint32_t uniqueID,
Greg Danielc7672092020-02-06 14:32:54 -050034 SkYUVColorSpace colorSpace, GrSurfaceProxyView views[],
35 GrColorType proxyColorTypes[], int numViews,
Greg Danielc594e622019-10-15 14:01:49 -040036 const SkYUVAIndex yuvaIndices[4], GrSurfaceOrigin origin,
37 sk_sp<SkColorSpace> imageColorSpace)
Brian Salomon9f2b86c2019-10-22 10:37:46 -040038 : INHERITED(std::move(context), size, uniqueID, kAssumedColorType,
Jim Van Verth8026ccc2018-10-04 13:10:39 -040039 // If an alpha channel is present we always switch to kPremul. This is because,
40 // although the planar data is always un-premul, the final interleaved RGB image
41 // is/would-be premul.
Brian Salomon5ad6fd32019-03-21 15:30:08 -040042 GetAlphaTypeFromYUVAIndices(yuvaIndices), std::move(imageColorSpace))
Greg Danielc7672092020-02-06 14:32:54 -050043 , fNumViews(numViews)
Jim Van Verth8026ccc2018-10-04 13:10:39 -040044 , fYUVColorSpace(colorSpace)
45 , fOrigin(origin) {
Jim Van Verth0e671942018-11-09 12:03:57 -050046 // The caller should have done this work, just verifying
47 SkDEBUGCODE(int textureCount;)
48 SkASSERT(SkYUVAIndex::AreValidIndices(yuvaIndices, &textureCount));
Greg Danielc7672092020-02-06 14:32:54 -050049 SkASSERT(textureCount == fNumViews);
Jim Van Verth0e671942018-11-09 12:03:57 -050050
Greg Danielc7672092020-02-06 14:32:54 -050051 for (int i = 0; i < numViews; ++i) {
52 fViews[i] = std::move(views[i]);
Greg Danielc594e622019-10-15 14:01:49 -040053 fProxyColorTypes[i] = proxyColorTypes[i];
Jim Van Verthf49262d2018-10-02 12:07:20 -040054 }
Greg Danielc7672092020-02-06 14:32:54 -050055 memcpy(fYUVAIndices, yuvaIndices, 4 * sizeof(SkYUVAIndex));
Jim Van Verthf49262d2018-10-02 12:07:20 -040056}
57
Jim Van Verth3e4c2f32019-01-11 13:32:45 -050058// For onMakeColorSpace()
59SkImage_GpuYUVA::SkImage_GpuYUVA(const SkImage_GpuYUVA* image, sk_sp<SkColorSpace> targetCS)
Brian Salomon9f2b86c2019-10-22 10:37:46 -040060 : INHERITED(image->fContext, image->dimensions(), kNeedNewImageUniqueID, kAssumedColorType,
Brian Salomon5ad6fd32019-03-21 15:30:08 -040061 // If an alpha channel is present we always switch to kPremul. This is because,
62 // although the planar data is always un-premul, the final interleaved RGB image
63 // is/would-be premul.
64 GetAlphaTypeFromYUVAIndices(image->fYUVAIndices), std::move(targetCS))
Greg Danielc7672092020-02-06 14:32:54 -050065 , fNumViews(image->fNumViews)
Brian Salomon5ad6fd32019-03-21 15:30:08 -040066 , fYUVColorSpace(image->fYUVColorSpace)
67 , fOrigin(image->fOrigin)
68 // Since null fFromColorSpace means no GrColorSpaceXform, we turn a null
69 // image->refColorSpace() into an explicit SRGB.
70 , fFromColorSpace(image->colorSpace() ? image->refColorSpace() : SkColorSpace::MakeSRGB()) {
71 // The caller should have done this work, just verifying
Jim Van Verth3e4c2f32019-01-11 13:32:45 -050072 SkDEBUGCODE(int textureCount;)
Greg Danielc7672092020-02-06 14:32:54 -050073 SkASSERT(SkYUVAIndex::AreValidIndices(image->fYUVAIndices, &textureCount));
74 SkASSERT(textureCount == fNumViews);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -050075
Greg Daniel1a372c32019-12-17 13:53:38 -050076 if (image->fRGBView.proxy()) {
77 fRGBView = image->fRGBView; // we ref in this case, not move
Brian Salomonad8efda2019-05-10 09:22:49 -040078 } else {
Greg Danielc7672092020-02-06 14:32:54 -050079 for (int i = 0; i < fNumViews; ++i) {
80 fViews[i] = image->fViews[i]; // we ref in this case, not move
Greg Danielc594e622019-10-15 14:01:49 -040081 fProxyColorTypes[i] = image->fProxyColorTypes[i];
Brian Salomonad8efda2019-05-10 09:22:49 -040082 }
Jim Van Verth3e4c2f32019-01-11 13:32:45 -050083 }
84 memcpy(fYUVAIndices, image->fYUVAIndices, 4 * sizeof(SkYUVAIndex));
85}
86
Robert Phillips8defcc12019-03-05 15:58:59 -050087bool SkImage_GpuYUVA::setupMipmapsForPlanes(GrRecordingContext* context) const {
Brian Salomonad8efda2019-05-10 09:22:49 -040088 // We shouldn't get here if the planes were already flattened to RGBA.
Greg Danielc7672092020-02-06 14:32:54 -050089 SkASSERT(fViews[0].proxy() && !fRGBView.proxy());
Robert Phillips8defcc12019-03-05 15:58:59 -050090 if (!context || !fContext->priv().matches(context)) {
91 return false;
92 }
93
Greg Danielc7672092020-02-06 14:32:54 -050094 for (int i = 0; i < fNumViews; ++i) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040095 GrTextureProducer::CopyParams copyParams;
Greg Danielc7672092020-02-06 14:32:54 -050096 int mipCount = SkMipMap::ComputeLevelCount(fViews[i].proxy()->width(),
97 fViews[i].proxy()->height());
Robert Phillips9da87e02019-02-04 13:26:26 -050098 if (mipCount && GrGpu::IsACopyNeededForMips(fContext->priv().caps(),
Greg Danielc7672092020-02-06 14:32:54 -050099 fViews[i].asTextureProxy(),
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400100 GrSamplerState::Filter::kMipMap,
101 &copyParams)) {
Greg Danielc7672092020-02-06 14:32:54 -0500102 auto mippedView = GrCopyBaseMipMapToTextureProxy(context, fViews[i].asTextureProxy(),
103 fOrigin, fProxyColorTypes[i]);
Greg Danielcc104db2020-02-03 14:17:08 -0500104 if (!mippedView.proxy()) {
Jim Van Verthf542cab2018-11-07 12:08:21 -0500105 return false;
106 }
Greg Danielc7672092020-02-06 14:32:54 -0500107 fViews[i] = std::move(mippedView);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400108 }
109 }
110 return true;
111}
112
Jim Van Verthf49262d2018-10-02 12:07:20 -0400113//////////////////////////////////////////////////////////////////////////////////////////////////
Jim Van Verthf49262d2018-10-02 12:07:20 -0400114
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400115GrSemaphoresSubmitted SkImage_GpuYUVA::onFlush(GrContext* context, const GrFlushInfo& info) {
116 if (!context || !fContext->priv().matches(context) || fContext->abandoned()) {
117 return GrSemaphoresSubmitted::kNo;
118 }
119
Greg Danielc7672092020-02-06 14:32:54 -0500120 GrSurfaceProxy* proxies[4] = {fViews[0].proxy(), fViews[1].proxy(), fViews[2].proxy(),
121 fViews[3].proxy()};
122 int numProxies = fNumViews;
Greg Daniel1a372c32019-12-17 13:53:38 -0500123 if (fRGBView.proxy()) {
Brian Salomonad8efda2019-05-10 09:22:49 -0400124 // Either we've already flushed the flattening draw or the flattening is unflushed. In the
Greg Daniel1a372c32019-12-17 13:53:38 -0500125 // latter case it should still be ok to just pass fRGBView proxy because it in turn depends
126 // on the planar proxies and will cause all of their work to flush as well.
127 proxies[0] = fRGBView.proxy();
Brian Salomonad8efda2019-05-10 09:22:49 -0400128 numProxies = 1;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400129 }
Greg Daniel55f040b2020-02-13 15:38:32 +0000130 return context->priv().flushSurfaces(proxies, numProxies, info);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400131}
132
Greg Danielc7672092020-02-06 14:32:54 -0500133GrTextureProxy* SkImage_GpuYUVA::peekProxy() const { return fRGBView.asTextureProxy(); }
Robert Phillips193c4212019-03-04 12:18:53 -0500134
Greg Daniel1a372c32019-12-17 13:53:38 -0500135void SkImage_GpuYUVA::flattenToRGB(GrRecordingContext* context) const {
136 if (fRGBView.proxy()) {
137 return;
Jim Van Verthf49262d2018-10-02 12:07:20 -0400138 }
139
Robert Phillips6603a172019-03-05 12:35:44 -0500140 if (!context || !fContext->priv().matches(context)) {
Greg Daniel1a372c32019-12-17 13:53:38 -0500141 return;
Robert Phillips6603a172019-03-05 12:35:44 -0500142 }
143
Robert Phillips6603a172019-03-05 12:35:44 -0500144 // Needs to create a render target in order to draw to it for the yuv->rgb conversion.
Greg Daniele20fcad2020-01-08 11:52:34 -0500145 auto renderTargetContext = GrRenderTargetContext::Make(
146 context, GrColorType::kRGBA_8888, this->refColorSpace(), SkBackingFit::kExact,
147 this->dimensions(), 1, GrMipMapped::kNo, GrProtected::kNo, fOrigin);
Robert Phillips6603a172019-03-05 12:35:44 -0500148 if (!renderTargetContext) {
Greg Daniel1a372c32019-12-17 13:53:38 -0500149 return;
Robert Phillips6603a172019-03-05 12:35:44 -0500150 }
151
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400152 sk_sp<GrColorSpaceXform> colorSpaceXform;
153 if (fFromColorSpace) {
154 colorSpaceXform = GrColorSpaceXform::Make(fFromColorSpace.get(), this->alphaType(),
155 this->colorSpace(), this->alphaType());
156 }
Robert Phillips6603a172019-03-05 12:35:44 -0500157 const SkRect rect = SkRect::MakeIWH(this->width(), this->height());
158 if (!RenderYUVAToRGBA(fContext.get(), renderTargetContext.get(), rect, fYUVColorSpace,
Greg Danielc7672092020-02-06 14:32:54 -0500159 std::move(colorSpaceXform), fViews, fYUVAIndices)) {
Greg Daniel1a372c32019-12-17 13:53:38 -0500160 return;
Robert Phillips6603a172019-03-05 12:35:44 -0500161 }
162
Greg Daniel1a372c32019-12-17 13:53:38 -0500163 fRGBView = renderTargetContext->readSurfaceView();
164 SkASSERT(fRGBView.origin() == fOrigin);
165 SkASSERT(fRGBView.swizzle() == GrSwizzle());
Greg Danielc7672092020-02-06 14:32:54 -0500166 for (auto& v : fViews) {
167 v.reset();
Brian Salomonad8efda2019-05-10 09:22:49 -0400168 }
Greg Daniel1a372c32019-12-17 13:53:38 -0500169}
170
Greg Daniel37c127f2020-02-05 10:37:27 -0500171GrSurfaceProxyView SkImage_GpuYUVA::refMippedView(GrRecordingContext* context) const {
Jim Van Verth803a5022018-11-05 15:55:53 -0500172 // if invalid or already has miplevels
Greg Daniel1a372c32019-12-17 13:53:38 -0500173 this->flattenToRGB(context);
174 if (!fRGBView.proxy() || GrMipMapped::kYes == fRGBView.asTextureProxy()->mipMapped()) {
Greg Danielcc104db2020-02-03 14:17:08 -0500175 return fRGBView;
Jim Van Verth803a5022018-11-05 15:55:53 -0500176 }
177
178 // need to generate mips for the proxy
Greg Danielc594e622019-10-15 14:01:49 -0400179 GrColorType srcColorType = SkColorTypeToGrColorType(this->colorType());
Greg Danielc7672092020-02-06 14:32:54 -0500180 GrSurfaceProxyView mippedView = GrCopyBaseMipMapToTextureProxy(context, fRGBView.proxy(),
181 fRGBView.origin(), srcColorType);
Greg Danielcc104db2020-02-03 14:17:08 -0500182 if (mippedView.proxy()) {
183 fRGBView = std::move(mippedView);
184 return fRGBView;
Jim Van Verth803a5022018-11-05 15:55:53 -0500185 }
186
187 // failed to generate mips
Greg Danielcc104db2020-02-03 14:17:08 -0500188 return {};
Jim Van Verth803a5022018-11-05 15:55:53 -0500189}
190
Greg Daniel37c127f2020-02-05 10:37:27 -0500191const GrSurfaceProxyView* SkImage_GpuYUVA::view(GrRecordingContext* context) const {
Greg Daniel1a372c32019-12-17 13:53:38 -0500192 this->flattenToRGB(context);
Greg Daniel37c127f2020-02-05 10:37:27 -0500193 if (!fRGBView.proxy()) {
194 return nullptr;
195 }
196 return &fRGBView;
Greg Daniel81b98972019-12-13 11:09:43 -0500197}
198
Jim Van Verth9bf81202018-10-30 15:53:36 -0400199//////////////////////////////////////////////////////////////////////////////////////////////////
200
Greg Danielc7672092020-02-06 14:32:54 -0500201sk_sp<SkImage> SkImage_GpuYUVA::onMakeColorTypeAndColorSpace(
202 GrRecordingContext*, SkColorType, sk_sp<SkColorSpace> targetCS) const {
Brian Osmanf48c9962019-01-14 11:15:50 -0500203 // We explicitly ignore color type changes, for now.
204
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500205 // we may need a mutex here but for now we expect usage to be in a single thread
206 if (fOnMakeColorSpaceTarget &&
Brian Osmanf48c9962019-01-14 11:15:50 -0500207 SkColorSpace::Equals(targetCS.get(), fOnMakeColorSpaceTarget.get())) {
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500208 return fOnMakeColorSpaceResult;
209 }
Brian Osmanf48c9962019-01-14 11:15:50 -0500210 sk_sp<SkImage> result = sk_sp<SkImage>(new SkImage_GpuYUVA(this, targetCS));
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500211 if (result) {
Brian Osmanf48c9962019-01-14 11:15:50 -0500212 fOnMakeColorSpaceTarget = targetCS;
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500213 fOnMakeColorSpaceResult = result;
214 }
215 return result;
216}
217
Brian Osmand5148372019-08-14 16:14:51 -0400218sk_sp<SkImage> SkImage_GpuYUVA::onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400219 return sk_make_sp<SkImage_GpuYUVA>(fContext, this->dimensions(), kNeedNewImageUniqueID,
Greg Danielc7672092020-02-06 14:32:54 -0500220 fYUVColorSpace, fViews, fProxyColorTypes, fNumViews,
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400221 fYUVAIndices, fOrigin, std::move(newCS));
Brian Osmand5148372019-08-14 16:14:51 -0400222}
223
Jim Van Verth3e4c2f32019-01-11 13:32:45 -0500224//////////////////////////////////////////////////////////////////////////////////////////////////
225
Jim Van Verth9bf81202018-10-30 15:53:36 -0400226sk_sp<SkImage> SkImage::MakeFromYUVATextures(GrContext* ctx,
227 SkYUVColorSpace colorSpace,
228 const GrBackendTexture yuvaTextures[],
229 const SkYUVAIndex yuvaIndices[4],
230 SkISize imageSize,
231 GrSurfaceOrigin imageOrigin,
232 sk_sp<SkColorSpace> imageColorSpace) {
Jim Van Verth0e671942018-11-09 12:03:57 -0500233 int numTextures;
234 if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures)) {
235 return nullptr;
Jim Van Verth9bf81202018-10-30 15:53:36 -0400236 }
237
Greg Danielc7672092020-02-06 14:32:54 -0500238 GrSurfaceProxyView tempViews[4];
Jim Van Verth53275362018-11-09 15:42:35 -0500239 if (!SkImage_GpuBase::MakeTempTextureProxies(ctx, yuvaTextures, numTextures, yuvaIndices,
Greg Danielc7672092020-02-06 14:32:54 -0500240 imageOrigin, tempViews)) {
Jim Van Verth0e671942018-11-09 12:03:57 -0500241 return nullptr;
Jim Van Verth9bf81202018-10-30 15:53:36 -0400242 }
Greg Danielc594e622019-10-15 14:01:49 -0400243 GrColorType proxyColorTypes[4];
244 for (int i = 0; i < numTextures; ++i) {
245 proxyColorTypes[i] = ctx->priv().caps()->getYUVAColorTypeFromBackendFormat(
246 yuvaTextures[i].getBackendFormat(), yuvaIndices[3].fIndex == i);
247 }
Jim Van Verth9bf81202018-10-30 15:53:36 -0400248
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400249 return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(ctx), imageSize, kNeedNewImageUniqueID, colorSpace,
Greg Danielc7672092020-02-06 14:32:54 -0500250 tempViews, proxyColorTypes, numTextures, yuvaIndices,
251 imageOrigin, imageColorSpace);
Jim Van Verth9bf81202018-10-30 15:53:36 -0400252}
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500253
Greg Danielc7672092020-02-06 14:32:54 -0500254sk_sp<SkImage> SkImage::MakeFromYUVAPixmaps(GrContext* context, SkYUVColorSpace yuvColorSpace,
255 const SkPixmap yuvaPixmaps[],
256 const SkYUVAIndex yuvaIndices[4], SkISize imageSize,
257 GrSurfaceOrigin imageOrigin, bool buildMips,
258 bool limitToMaxTextureSize,
259 sk_sp<SkColorSpace> imageColorSpace) {
Mike Reed6a5f7e22019-05-23 15:30:07 -0400260 if (!context) {
Greg Danielc7672092020-02-06 14:32:54 -0500261 return nullptr; // until we impl this for raster backend
Mike Reed6a5f7e22019-05-23 15:30:07 -0400262 }
263
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500264 int numPixmaps;
265 if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numPixmaps)) {
266 return nullptr;
267 }
268
Brian Osman7dcc6162019-03-25 10:12:57 -0400269 if (!context->priv().caps()->mipMapSupport()) {
270 buildMips = false;
271 }
272
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500273 // Make proxies
Greg Danielc7672092020-02-06 14:32:54 -0500274 GrSurfaceProxyView tempViews[4];
Greg Danielc594e622019-10-15 14:01:49 -0400275 GrColorType proxyColorTypes[4];
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500276 for (int i = 0; i < numPixmaps; ++i) {
277 const SkPixmap* pixmap = &yuvaPixmaps[i];
278 SkAutoPixmapStorage resized;
Robert Phillips9da87e02019-02-04 13:26:26 -0500279 int maxTextureSize = context->priv().caps()->maxTextureSize();
Brian Osman788b9162020-02-07 10:36:46 -0500280 int maxDim = std::max(yuvaPixmaps[i].width(), yuvaPixmaps[i].height());
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500281 if (limitToMaxTextureSize && maxDim > maxTextureSize) {
282 float scale = static_cast<float>(maxTextureSize) / maxDim;
Brian Osman788b9162020-02-07 10:36:46 -0500283 int newWidth = std::min(static_cast<int>(yuvaPixmaps[i].width() * scale), maxTextureSize);
Greg Danielc7672092020-02-06 14:32:54 -0500284 int newHeight =
Brian Osman788b9162020-02-07 10:36:46 -0500285 std::min(static_cast<int>(yuvaPixmaps[i].height() * scale), maxTextureSize);
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500286 SkImageInfo info = yuvaPixmaps[i].info().makeWH(newWidth, newHeight);
287 if (!resized.tryAlloc(info) ||
288 !yuvaPixmaps[i].scalePixels(resized, kLow_SkFilterQuality)) {
289 return nullptr;
290 }
291 pixmap = &resized;
292 }
293 // Turn the pixmap into a GrTextureProxy
Brian Osman7dcc6162019-03-25 10:12:57 -0400294 SkBitmap bmp;
295 bmp.installPixels(*pixmap);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500296 GrBitmapTextureMaker bitmapMaker(context, bmp);
Brian Osman7dcc6162019-03-25 10:12:57 -0400297 GrMipMapped mipMapped = buildMips ? GrMipMapped::kYes : GrMipMapped::kNo;
Greg Danielcc104db2020-02-03 14:17:08 -0500298 GrSurfaceProxyView view;
Greg Danielc7672092020-02-06 14:32:54 -0500299 std::tie(tempViews[i], proxyColorTypes[i]) = bitmapMaker.view(mipMapped);
300 if (!tempViews[i]) {
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500301 return nullptr;
302 }
303 }
304
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400305 return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(context), imageSize, kNeedNewImageUniqueID,
Greg Danielc7672092020-02-06 14:32:54 -0500306 yuvColorSpace, tempViews, proxyColorTypes, numPixmaps,
307 yuvaIndices, imageOrigin, imageColorSpace);
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500308}
309
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400310/////////////////////////////////////////////////////////////////////////////////////////////////
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500311sk_sp<SkImage> SkImage_GpuYUVA::MakePromiseYUVATexture(
312 GrContext* context,
313 SkYUVColorSpace yuvColorSpace,
314 const GrBackendFormat yuvaFormats[],
315 const SkISize yuvaSizes[],
316 const SkYUVAIndex yuvaIndices[4],
317 int imageWidth,
318 int imageHeight,
319 GrSurfaceOrigin imageOrigin,
320 sk_sp<SkColorSpace> imageColorSpace,
321 PromiseImageTextureFulfillProc textureFulfillProc,
322 PromiseImageTextureReleaseProc textureReleaseProc,
323 PromiseImageTextureDoneProc promiseDoneProc,
Brian Salomon0cc57542019-03-08 13:28:46 -0500324 PromiseImageTextureContext textureContexts[],
325 PromiseImageApiVersion version) {
Jim Van Verthf00b1622018-10-10 13:03:23 -0400326 int numTextures;
327 bool valid = SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures);
328
Brian Salomonbe5a0932018-12-10 10:03:26 -0500329 // The contract here is that if 'promiseDoneProc' is passed in it should always be called,
330 // even if creation of the SkImage fails. Once we call MakePromiseImageLazyProxy it takes
331 // responsibility for calling the done proc.
332 if (!promiseDoneProc) {
333 return nullptr;
Jim Van Verthf00b1622018-10-10 13:03:23 -0400334 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500335 int proxiesCreated = 0;
336 SkScopeExit callDone([promiseDoneProc, textureContexts, numTextures, &proxiesCreated]() {
337 for (int i = proxiesCreated; i < numTextures; ++i) {
338 promiseDoneProc(textureContexts[i]);
339 }
340 });
Jim Van Verthf00b1622018-10-10 13:03:23 -0400341
342 if (!valid) {
343 return nullptr;
344 }
Robert Phillipsef85d192018-10-09 11:24:09 -0400345
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400346 if (!context) {
347 return nullptr;
348 }
349
Greg Kaiser9a2169e2019-02-10 17:29:46 -0800350 if (imageWidth <= 0 || imageHeight <= 0) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400351 return nullptr;
352 }
353
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400354 SkAlphaType at = (-1 != yuvaIndices[SkYUVAIndex::kA_Index].fIndex) ? kPremul_SkAlphaType
355 : kOpaque_SkAlphaType;
Greg Danielc7672092020-02-06 14:32:54 -0500356 SkImageInfo info =
357 SkImageInfo::Make(imageWidth, imageHeight, kAssumedColorType, at, imageColorSpace);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400358 if (!SkImageInfoIsValid(info)) {
359 return nullptr;
360 }
361
Jim Van Verthf9f07352018-10-24 10:32:20 -0400362 // verify sizes with expected texture count
Jim Van Verth8f11e432018-10-18 14:36:59 -0400363 for (int i = 0; i < numTextures; ++i) {
Jim Van Verthf9f07352018-10-24 10:32:20 -0400364 if (yuvaSizes[i].isEmpty()) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400365 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400366 }
367 }
Jim Van Verthe24b5872018-10-29 16:26:02 -0400368 for (int i = numTextures; i < SkYUVASizeInfo::kMaxCount; ++i) {
Jim Van Verthf9f07352018-10-24 10:32:20 -0400369 if (!yuvaSizes[i].isEmpty()) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400370 return nullptr;
371 }
Jim Van Verthf99a6742018-10-18 16:13:18 +0000372 }
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400373
374 // Get lazy proxies
Greg Danielc7672092020-02-06 14:32:54 -0500375 GrSurfaceProxyView views[4];
Greg Danielc594e622019-10-15 14:01:49 -0400376 GrColorType proxyColorTypes[4];
Jim Van Verthf00b1622018-10-10 13:03:23 -0400377 for (int texIdx = 0; texIdx < numTextures; ++texIdx) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400378 GrColorType colorType = context->priv().caps()->getYUVAColorTypeFromBackendFormat(
Greg Danielc7672092020-02-06 14:32:54 -0500379 yuvaFormats[texIdx], yuvaIndices[3].fIndex == texIdx);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400380 if (GrColorType::kUnknown == colorType) {
Jim Van Verthf00b1622018-10-10 13:03:23 -0400381 return nullptr;
382 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400383
Greg Danielc7672092020-02-06 14:32:54 -0500384 auto proxy = MakePromiseImageLazyProxy(
Greg Daniel3a365112020-02-14 10:47:18 -0500385 context, yuvaSizes[texIdx].width(), yuvaSizes[texIdx].height(), colorType,
386 yuvaFormats[texIdx], GrMipMapped::kNo, textureFulfillProc, textureReleaseProc,
387 promiseDoneProc, textureContexts[texIdx], version);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500388 ++proxiesCreated;
Greg Danielc7672092020-02-06 14:32:54 -0500389 if (!proxy) {
Jim Van Verthf00b1622018-10-10 13:03:23 -0400390 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400391 }
Greg Danielc7672092020-02-06 14:32:54 -0500392 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(),
393 colorType);
394 views[texIdx] = GrSurfaceProxyView(std::move(proxy), imageOrigin, swizzle);
Greg Danielc594e622019-10-15 14:01:49 -0400395 proxyColorTypes[texIdx] = colorType;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400396 }
397
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400398 return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(context), SkISize{imageWidth, imageHeight},
Greg Danielc7672092020-02-06 14:32:54 -0500399 kNeedNewImageUniqueID, yuvColorSpace, views,
Greg Danielc594e622019-10-15 14:01:49 -0400400 proxyColorTypes, numTextures, yuvaIndices, imageOrigin,
401 std::move(imageColorSpace));
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400402}