blob: 8aa15cbff39f31298ee7ef00c8993eb06b049bf2 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/private/GrRecordingContext.h"
15#include "src/core/SkAutoPixmapStorage.h"
16#include "src/core/SkMipMap.h"
17#include "src/core/SkScopeExit.h"
Greg Daniel82c6b102020-01-21 10:33:22 -050018#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrClip.h"
20#include "src/gpu/GrContextPriv.h"
21#include "src/gpu/GrGpu.h"
22#include "src/gpu/GrRecordingContextPriv.h"
23#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000024#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#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 Salomonc505a452020-04-06 10:29:02 -040033SkImage_GpuYUVA::SkImage_GpuYUVA(sk_sp<GrContext> context,
34 SkISize size,
35 uint32_t uniqueID,
36 SkYUVColorSpace colorSpace,
37 GrSurfaceProxyView views[],
38 int numViews,
39 const SkYUVAIndex yuvaIndices[4],
40 GrSurfaceOrigin origin,
Greg Danielc594e622019-10-15 14:01:49 -040041 sk_sp<SkColorSpace> imageColorSpace)
Brian Salomonc505a452020-04-06 10:29:02 -040042 : INHERITED(std::move(context),
43 size,
44 uniqueID,
45 kAssumedColorType,
Jim Van Verth8026ccc2018-10-04 13:10:39 -040046 // If an alpha channel is present we always switch to kPremul. This is because,
47 // although the planar data is always un-premul, the final interleaved RGB image
48 // is/would-be premul.
Brian Salomonc505a452020-04-06 10:29:02 -040049 GetAlphaTypeFromYUVAIndices(yuvaIndices),
50 std::move(imageColorSpace))
Greg Danielc7672092020-02-06 14:32:54 -050051 , fNumViews(numViews)
Jim Van Verth8026ccc2018-10-04 13:10:39 -040052 , fYUVColorSpace(colorSpace)
53 , fOrigin(origin) {
Jim Van Verth0e671942018-11-09 12:03:57 -050054 // The caller should have done this work, just verifying
55 SkDEBUGCODE(int textureCount;)
56 SkASSERT(SkYUVAIndex::AreValidIndices(yuvaIndices, &textureCount));
Greg Danielc7672092020-02-06 14:32:54 -050057 SkASSERT(textureCount == fNumViews);
Jim Van Verth0e671942018-11-09 12:03:57 -050058
Greg Danielc7672092020-02-06 14:32:54 -050059 for (int i = 0; i < numViews; ++i) {
60 fViews[i] = std::move(views[i]);
Jim Van Verthf49262d2018-10-02 12:07:20 -040061 }
Greg Danielc7672092020-02-06 14:32:54 -050062 memcpy(fYUVAIndices, yuvaIndices, 4 * sizeof(SkYUVAIndex));
Jim Van Verthf49262d2018-10-02 12:07:20 -040063}
64
Jim Van Verth3e4c2f32019-01-11 13:32:45 -050065// For onMakeColorSpace()
66SkImage_GpuYUVA::SkImage_GpuYUVA(const SkImage_GpuYUVA* image, sk_sp<SkColorSpace> targetCS)
Brian Salomon9f2b86c2019-10-22 10:37:46 -040067 : INHERITED(image->fContext, image->dimensions(), kNeedNewImageUniqueID, kAssumedColorType,
Brian Salomon5ad6fd32019-03-21 15:30:08 -040068 // If an alpha channel is present we always switch to kPremul. This is because,
69 // although the planar data is always un-premul, the final interleaved RGB image
70 // is/would-be premul.
71 GetAlphaTypeFromYUVAIndices(image->fYUVAIndices), std::move(targetCS))
Greg Danielc7672092020-02-06 14:32:54 -050072 , fNumViews(image->fNumViews)
Brian Salomon5ad6fd32019-03-21 15:30:08 -040073 , fYUVColorSpace(image->fYUVColorSpace)
74 , fOrigin(image->fOrigin)
75 // Since null fFromColorSpace means no GrColorSpaceXform, we turn a null
76 // image->refColorSpace() into an explicit SRGB.
77 , fFromColorSpace(image->colorSpace() ? image->refColorSpace() : SkColorSpace::MakeSRGB()) {
78 // The caller should have done this work, just verifying
Jim Van Verth3e4c2f32019-01-11 13:32:45 -050079 SkDEBUGCODE(int textureCount;)
Greg Danielc7672092020-02-06 14:32:54 -050080 SkASSERT(SkYUVAIndex::AreValidIndices(image->fYUVAIndices, &textureCount));
81 SkASSERT(textureCount == fNumViews);
Jim Van Verth3e4c2f32019-01-11 13:32:45 -050082
Greg Daniel1a372c32019-12-17 13:53:38 -050083 if (image->fRGBView.proxy()) {
84 fRGBView = image->fRGBView; // we ref in this case, not move
Brian Salomonad8efda2019-05-10 09:22:49 -040085 } else {
Greg Danielc7672092020-02-06 14:32:54 -050086 for (int i = 0; i < fNumViews; ++i) {
87 fViews[i] = image->fViews[i]; // we ref in this case, not move
Brian Salomonad8efda2019-05-10 09:22:49 -040088 }
Jim Van Verth3e4c2f32019-01-11 13:32:45 -050089 }
90 memcpy(fYUVAIndices, image->fYUVAIndices, 4 * sizeof(SkYUVAIndex));
91}
92
Robert Phillips8defcc12019-03-05 15:58:59 -050093bool SkImage_GpuYUVA::setupMipmapsForPlanes(GrRecordingContext* context) const {
Brian Salomonad8efda2019-05-10 09:22:49 -040094 // We shouldn't get here if the planes were already flattened to RGBA.
Greg Danielc7672092020-02-06 14:32:54 -050095 SkASSERT(fViews[0].proxy() && !fRGBView.proxy());
Robert Phillips8defcc12019-03-05 15:58:59 -050096 if (!context || !fContext->priv().matches(context)) {
97 return false;
98 }
99
Greg Danielc7672092020-02-06 14:32:54 -0500100 for (int i = 0; i < fNumViews; ++i) {
Greg Danielc7672092020-02-06 14:32:54 -0500101 int mipCount = SkMipMap::ComputeLevelCount(fViews[i].proxy()->width(),
102 fViews[i].proxy()->height());
Robert Phillips9da87e02019-02-04 13:26:26 -0500103 if (mipCount && GrGpu::IsACopyNeededForMips(fContext->priv().caps(),
Greg Danielc7672092020-02-06 14:32:54 -0500104 fViews[i].asTextureProxy(),
Brian Salomonc8d092a2020-02-24 10:14:21 -0500105 GrSamplerState::Filter::kMipMap)) {
Brian Salomonc5243782020-04-02 12:50:34 -0400106 auto mippedView = GrCopyBaseMipMapToView(context, fViews[i]);
107 if (!mippedView) {
Jim Van Verthf542cab2018-11-07 12:08:21 -0500108 return false;
109 }
Greg Danielc7672092020-02-06 14:32:54 -0500110 fViews[i] = std::move(mippedView);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400111 }
112 }
113 return true;
114}
115
Jim Van Verthf49262d2018-10-02 12:07:20 -0400116//////////////////////////////////////////////////////////////////////////////////////////////////
Jim Van Verthf49262d2018-10-02 12:07:20 -0400117
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400118GrSemaphoresSubmitted SkImage_GpuYUVA::onFlush(GrContext* context, const GrFlushInfo& info) {
119 if (!context || !fContext->priv().matches(context) || fContext->abandoned()) {
120 return GrSemaphoresSubmitted::kNo;
121 }
122
Greg Danielc7672092020-02-06 14:32:54 -0500123 GrSurfaceProxy* proxies[4] = {fViews[0].proxy(), fViews[1].proxy(), fViews[2].proxy(),
124 fViews[3].proxy()};
125 int numProxies = fNumViews;
Greg Daniel1a372c32019-12-17 13:53:38 -0500126 if (fRGBView.proxy()) {
Brian Salomonad8efda2019-05-10 09:22:49 -0400127 // Either we've already flushed the flattening draw or the flattening is unflushed. In the
Greg Daniel1a372c32019-12-17 13:53:38 -0500128 // latter case it should still be ok to just pass fRGBView proxy because it in turn depends
129 // on the planar proxies and will cause all of their work to flush as well.
130 proxies[0] = fRGBView.proxy();
Brian Salomonad8efda2019-05-10 09:22:49 -0400131 numProxies = 1;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400132 }
Greg Daniel55f040b2020-02-13 15:38:32 +0000133 return context->priv().flushSurfaces(proxies, numProxies, info);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400134}
135
Greg Danielc7672092020-02-06 14:32:54 -0500136GrTextureProxy* SkImage_GpuYUVA::peekProxy() const { return fRGBView.asTextureProxy(); }
Robert Phillips193c4212019-03-04 12:18:53 -0500137
Greg Daniel1a372c32019-12-17 13:53:38 -0500138void SkImage_GpuYUVA::flattenToRGB(GrRecordingContext* context) const {
139 if (fRGBView.proxy()) {
140 return;
Jim Van Verthf49262d2018-10-02 12:07:20 -0400141 }
142
Robert Phillips6603a172019-03-05 12:35:44 -0500143 if (!context || !fContext->priv().matches(context)) {
Greg Daniel1a372c32019-12-17 13:53:38 -0500144 return;
Robert Phillips6603a172019-03-05 12:35:44 -0500145 }
146
Robert Phillips6603a172019-03-05 12:35:44 -0500147 // Needs to create a render target in order to draw to it for the yuv->rgb conversion.
Greg Daniele20fcad2020-01-08 11:52:34 -0500148 auto renderTargetContext = GrRenderTargetContext::Make(
149 context, GrColorType::kRGBA_8888, this->refColorSpace(), SkBackingFit::kExact,
150 this->dimensions(), 1, GrMipMapped::kNo, GrProtected::kNo, fOrigin);
Robert Phillips6603a172019-03-05 12:35:44 -0500151 if (!renderTargetContext) {
Greg Daniel1a372c32019-12-17 13:53:38 -0500152 return;
Robert Phillips6603a172019-03-05 12:35:44 -0500153 }
154
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400155 sk_sp<GrColorSpaceXform> colorSpaceXform;
156 if (fFromColorSpace) {
157 colorSpaceXform = GrColorSpaceXform::Make(fFromColorSpace.get(), this->alphaType(),
158 this->colorSpace(), this->alphaType());
159 }
Robert Phillips6603a172019-03-05 12:35:44 -0500160 const SkRect rect = SkRect::MakeIWH(this->width(), this->height());
161 if (!RenderYUVAToRGBA(fContext.get(), renderTargetContext.get(), rect, fYUVColorSpace,
Greg Danielc7672092020-02-06 14:32:54 -0500162 std::move(colorSpaceXform), fViews, fYUVAIndices)) {
Greg Daniel1a372c32019-12-17 13:53:38 -0500163 return;
Robert Phillips6603a172019-03-05 12:35:44 -0500164 }
165
Greg Daniel1a372c32019-12-17 13:53:38 -0500166 fRGBView = renderTargetContext->readSurfaceView();
167 SkASSERT(fRGBView.origin() == fOrigin);
168 SkASSERT(fRGBView.swizzle() == GrSwizzle());
Greg Danielc7672092020-02-06 14:32:54 -0500169 for (auto& v : fViews) {
170 v.reset();
Brian Salomonad8efda2019-05-10 09:22:49 -0400171 }
Greg Daniel1a372c32019-12-17 13:53:38 -0500172}
173
Greg Daniel37c127f2020-02-05 10:37:27 -0500174GrSurfaceProxyView SkImage_GpuYUVA::refMippedView(GrRecordingContext* context) const {
Jim Van Verth803a5022018-11-05 15:55:53 -0500175 // if invalid or already has miplevels
Greg Daniel1a372c32019-12-17 13:53:38 -0500176 this->flattenToRGB(context);
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500177 if (!fRGBView || fRGBView.asTextureProxy()->mipMapped() == GrMipMapped::kYes) {
Greg Danielcc104db2020-02-03 14:17:08 -0500178 return fRGBView;
Jim Van Verth803a5022018-11-05 15:55:53 -0500179 }
180
181 // need to generate mips for the proxy
Brian Salomonc5243782020-04-02 12:50:34 -0400182 auto mippedView = GrCopyBaseMipMapToView(context, fRGBView);
183 if (!mippedView) {
184 return {};
Jim Van Verth803a5022018-11-05 15:55:53 -0500185 }
186
Brian Salomonc5243782020-04-02 12:50:34 -0400187 fRGBView = std::move(mippedView);
188 return fRGBView;
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,
Brian Salomonc505a452020-04-06 10:29:02 -0400220 fYUVColorSpace, fViews, fNumViews, fYUVAIndices, fOrigin,
221 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 }
243
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400244 return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(ctx), imageSize, kNeedNewImageUniqueID, colorSpace,
Brian Salomonc505a452020-04-06 10:29:02 -0400245 tempViews, numTextures, yuvaIndices, imageOrigin,
246 imageColorSpace);
Jim Van Verth9bf81202018-10-30 15:53:36 -0400247}
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500248
Greg Danielc7672092020-02-06 14:32:54 -0500249sk_sp<SkImage> SkImage::MakeFromYUVAPixmaps(GrContext* context, SkYUVColorSpace yuvColorSpace,
250 const SkPixmap yuvaPixmaps[],
251 const SkYUVAIndex yuvaIndices[4], SkISize imageSize,
252 GrSurfaceOrigin imageOrigin, bool buildMips,
253 bool limitToMaxTextureSize,
254 sk_sp<SkColorSpace> imageColorSpace) {
Mike Reed6a5f7e22019-05-23 15:30:07 -0400255 if (!context) {
Greg Danielc7672092020-02-06 14:32:54 -0500256 return nullptr; // until we impl this for raster backend
Mike Reed6a5f7e22019-05-23 15:30:07 -0400257 }
258
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500259 int numPixmaps;
260 if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numPixmaps)) {
261 return nullptr;
262 }
263
Brian Osman7dcc6162019-03-25 10:12:57 -0400264 if (!context->priv().caps()->mipMapSupport()) {
265 buildMips = false;
266 }
267
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500268 // Make proxies
Greg Danielc7672092020-02-06 14:32:54 -0500269 GrSurfaceProxyView tempViews[4];
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500270 for (int i = 0; i < numPixmaps; ++i) {
271 const SkPixmap* pixmap = &yuvaPixmaps[i];
272 SkAutoPixmapStorage resized;
Robert Phillips9da87e02019-02-04 13:26:26 -0500273 int maxTextureSize = context->priv().caps()->maxTextureSize();
Brian Osman788b9162020-02-07 10:36:46 -0500274 int maxDim = std::max(yuvaPixmaps[i].width(), yuvaPixmaps[i].height());
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500275 if (limitToMaxTextureSize && maxDim > maxTextureSize) {
276 float scale = static_cast<float>(maxTextureSize) / maxDim;
Brian Osman788b9162020-02-07 10:36:46 -0500277 int newWidth = std::min(static_cast<int>(yuvaPixmaps[i].width() * scale), maxTextureSize);
Greg Danielc7672092020-02-06 14:32:54 -0500278 int newHeight =
Brian Osman788b9162020-02-07 10:36:46 -0500279 std::min(static_cast<int>(yuvaPixmaps[i].height() * scale), maxTextureSize);
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500280 SkImageInfo info = yuvaPixmaps[i].info().makeWH(newWidth, newHeight);
281 if (!resized.tryAlloc(info) ||
282 !yuvaPixmaps[i].scalePixels(resized, kLow_SkFilterQuality)) {
283 return nullptr;
284 }
285 pixmap = &resized;
286 }
287 // Turn the pixmap into a GrTextureProxy
Brian Osman7dcc6162019-03-25 10:12:57 -0400288 SkBitmap bmp;
289 bmp.installPixels(*pixmap);
Brian Salomonbc074a62020-03-18 10:06:13 -0400290 GrBitmapTextureMaker bitmapMaker(context, bmp, GrImageTexGenPolicy::kNew_Uncached_Budgeted);
Brian Osman7dcc6162019-03-25 10:12:57 -0400291 GrMipMapped mipMapped = buildMips ? GrMipMapped::kYes : GrMipMapped::kNo;
Greg Danielcc104db2020-02-03 14:17:08 -0500292 GrSurfaceProxyView view;
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500293 tempViews[i] = bitmapMaker.view(mipMapped);
Greg Danielc7672092020-02-06 14:32:54 -0500294 if (!tempViews[i]) {
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500295 return nullptr;
296 }
297 }
298
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400299 return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(context), imageSize, kNeedNewImageUniqueID,
Brian Salomonc505a452020-04-06 10:29:02 -0400300 yuvColorSpace, tempViews, numPixmaps, yuvaIndices,
301 imageOrigin, imageColorSpace);
Jim Van Verthc8429ad2018-11-20 11:12:37 -0500302}
303
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400304/////////////////////////////////////////////////////////////////////////////////////////////////
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500305sk_sp<SkImage> SkImage_GpuYUVA::MakePromiseYUVATexture(
306 GrContext* context,
307 SkYUVColorSpace yuvColorSpace,
308 const GrBackendFormat yuvaFormats[],
309 const SkISize yuvaSizes[],
310 const SkYUVAIndex yuvaIndices[4],
311 int imageWidth,
312 int imageHeight,
313 GrSurfaceOrigin imageOrigin,
314 sk_sp<SkColorSpace> imageColorSpace,
315 PromiseImageTextureFulfillProc textureFulfillProc,
316 PromiseImageTextureReleaseProc textureReleaseProc,
317 PromiseImageTextureDoneProc promiseDoneProc,
Brian Salomon0cc57542019-03-08 13:28:46 -0500318 PromiseImageTextureContext textureContexts[],
319 PromiseImageApiVersion version) {
Jim Van Verthf00b1622018-10-10 13:03:23 -0400320 int numTextures;
321 bool valid = SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures);
322
Brian Salomonbe5a0932018-12-10 10:03:26 -0500323 // The contract here is that if 'promiseDoneProc' is passed in it should always be called,
324 // even if creation of the SkImage fails. Once we call MakePromiseImageLazyProxy it takes
325 // responsibility for calling the done proc.
326 if (!promiseDoneProc) {
327 return nullptr;
Jim Van Verthf00b1622018-10-10 13:03:23 -0400328 }
Brian Salomonbe5a0932018-12-10 10:03:26 -0500329 int proxiesCreated = 0;
330 SkScopeExit callDone([promiseDoneProc, textureContexts, numTextures, &proxiesCreated]() {
331 for (int i = proxiesCreated; i < numTextures; ++i) {
332 promiseDoneProc(textureContexts[i]);
333 }
334 });
Jim Van Verthf00b1622018-10-10 13:03:23 -0400335
336 if (!valid) {
337 return nullptr;
338 }
Robert Phillipsef85d192018-10-09 11:24:09 -0400339
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400340 if (!context) {
341 return nullptr;
342 }
343
Greg Kaiser9a2169e2019-02-10 17:29:46 -0800344 if (imageWidth <= 0 || imageHeight <= 0) {
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400345 return nullptr;
346 }
347
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400348 SkAlphaType at = (-1 != yuvaIndices[SkYUVAIndex::kA_Index].fIndex) ? kPremul_SkAlphaType
349 : kOpaque_SkAlphaType;
Greg Danielc7672092020-02-06 14:32:54 -0500350 SkImageInfo info =
351 SkImageInfo::Make(imageWidth, imageHeight, kAssumedColorType, at, imageColorSpace);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400352 if (!SkImageInfoIsValid(info)) {
353 return nullptr;
354 }
355
Jim Van Verthf9f07352018-10-24 10:32:20 -0400356 // verify sizes with expected texture count
Jim Van Verth8f11e432018-10-18 14:36:59 -0400357 for (int i = 0; i < numTextures; ++i) {
Jim Van Verthf9f07352018-10-24 10:32:20 -0400358 if (yuvaSizes[i].isEmpty()) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400359 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400360 }
361 }
Jim Van Verthe24b5872018-10-29 16:26:02 -0400362 for (int i = numTextures; i < SkYUVASizeInfo::kMaxCount; ++i) {
Jim Van Verthf9f07352018-10-24 10:32:20 -0400363 if (!yuvaSizes[i].isEmpty()) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400364 return nullptr;
365 }
Jim Van Verthf99a6742018-10-18 16:13:18 +0000366 }
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400367
368 // Get lazy proxies
Greg Danielc7672092020-02-06 14:32:54 -0500369 GrSurfaceProxyView views[4];
Jim Van Verthf00b1622018-10-10 13:03:23 -0400370 for (int texIdx = 0; texIdx < numTextures; ++texIdx) {
Greg Danielc7672092020-02-06 14:32:54 -0500371 auto proxy = MakePromiseImageLazyProxy(
Brian Salomonc3ce54a2020-04-01 16:52:37 -0400372 context, yuvaSizes[texIdx].width(), yuvaSizes[texIdx].height(),
Greg Daniel3a365112020-02-14 10:47:18 -0500373 yuvaFormats[texIdx], GrMipMapped::kNo, textureFulfillProc, textureReleaseProc,
374 promiseDoneProc, textureContexts[texIdx], version);
Brian Salomonbe5a0932018-12-10 10:03:26 -0500375 ++proxiesCreated;
Greg Danielc7672092020-02-06 14:32:54 -0500376 if (!proxy) {
Jim Van Verthf00b1622018-10-10 13:03:23 -0400377 return nullptr;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400378 }
Brian Salomonc505a452020-04-06 10:29:02 -0400379 views[texIdx] = GrSurfaceProxyView(std::move(proxy), imageOrigin, GrSwizzle("rgba"));
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400380 }
381
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400382 return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(context), SkISize{imageWidth, imageHeight},
Brian Salomonc505a452020-04-06 10:29:02 -0400383 kNeedNewImageUniqueID, yuvColorSpace, views, numTextures,
384 yuvaIndices, imageOrigin, std::move(imageColorSpace));
Jim Van Verth8bbce0e2018-10-08 14:34:52 -0400385}