Debian Codegraph @

src/gpu/ganesh/image/SkImage_GaneshFactories.cpp

raw
1 /*
2 * Copyright 2023 Google LLC
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 "include/gpu/ganesh/SkImageGanesh.h"
9
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkData.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkPixmap.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkSamplingOptions.h"
19 #include "include/core/SkSize.h"
20 #include "include/core/SkYUVAInfo.h"
21 #include "include/core/SkYUVAPixmaps.h"
22 #include "include/gpu/GpuTypes.h"
23 #include "include/gpu/ganesh/GrBackendSurface.h"
24 #include "include/gpu/ganesh/GrContextThreadSafeProxy.h"
25 #include "include/gpu/ganesh/GrDirectContext.h"
26 #include "include/gpu/ganesh/GrExternalTextureGenerator.h"
27 #include "include/gpu/ganesh/GrRecordingContext.h"
28 #include "include/gpu/ganesh/GrTypes.h"
29 #include "include/gpu/ganesh/GrYUVABackendTextures.h"
30 #include "include/private/base/SkAssert.h"
31 #include "include/private/chromium/SkImageChromium.h"
32 #include "include/private/gpu/ganesh/GrImageContext.h"
33 #include "include/private/gpu/ganesh/GrTextureGenerator.h"
34 #include "include/private/gpu/ganesh/GrTypesPriv.h"
35 #include "src/core/SkAutoPixmapStorage.h"
36 #include "src/core/SkImageInfoPriv.h"
37 #include "src/gpu/GpuTypesPriv.h"
38 #include "src/gpu/RefCntedCallback.h"
39 #include "src/gpu/Swizzle.h"
40 #include "src/gpu/ganesh/GrBackendTextureImageGenerator.h"
41 #include "src/gpu/ganesh/GrBackendUtils.h"
42 #include "src/gpu/ganesh/GrCaps.h"
43 #include "src/gpu/ganesh/GrContextThreadSafeProxyPriv.h"
44 #include "src/gpu/ganesh/GrDirectContextPriv.h"
45 #include "src/gpu/ganesh/GrGpu.h"
46 #include "src/gpu/ganesh/GrGpuResourcePriv.h"
47 #include "src/gpu/ganesh/GrImageContextPriv.h"
48 #include "src/gpu/ganesh/GrProxyProvider.h"
49 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
50 #include "src/gpu/ganesh/GrSemaphore.h"
51 #include "src/gpu/ganesh/GrSurfaceProxy.h"
52 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
53 #include "src/gpu/ganesh/GrTexture.h"
54 #include "src/gpu/ganesh/GrTextureProxy.h"
55 #include "src/gpu/ganesh/GrYUVATextureProxies.h"
56 #include "src/gpu/ganesh/SkGr.h"
57 #include "src/gpu/ganesh/image/GrImageUtils.h"
58 #include "src/gpu/ganesh/image/GrMippedBitmap.h"
59 #include "src/gpu/ganesh/image/SkImage_Ganesh.h"
60 #include "src/gpu/ganesh/image/SkImage_GaneshBase.h"
61 #include "src/gpu/ganesh/image/SkImage_GaneshYUVA.h"
62 #include "src/image/SkImage_Base.h"
63
64 #include <algorithm>
65 #include <memory>
66 #include <tuple>
67 #include <utility>
68
69 enum SkColorType : int;
70 enum class SkTextureCompressionType;
71
72 namespace SkImages {
73
74 bool MakeBackendTextureFromImage(GrDirectContext* direct,
75 sk_sp<SkImage> image,
76 GrBackendTexture* backendTexture,
77 BackendTextureReleaseProc* releaseProc) {
78 if (!image || !backendTexture || !releaseProc) {
79 return false;
80 }
81
82 auto [view, ct] = skgpu::ganesh::AsView(direct, image, skgpu::Mipmapped::kNo,
83 /*targetSurface=*/nullptr);
84 if (!view) {
85 return false;
86 }
87
88 // Flush any pending IO on the texture.
89 direct->priv().flushSurface(view.proxy());
90
91 GrTexture* texture = view.asTextureProxy()->peekTexture();
92 if (!texture) {
93 return false;
94 }
95 // We must make a copy of the image if the image is not unique, if the GrTexture owned by the
96 // image is not unique, or if the texture wraps an external object.
97 if (!image->unique() || !texture->unique() || texture->resourcePriv().refsWrappedObjects()) {
98 // onMakeSubset will always copy the image.
99 image = as_IB(image)->onMakeSubset(direct->asRecorder(), image->bounds(), {});
100 if (!image) {
101 return false;
102 }
103 return MakeBackendTextureFromImage(direct, std::move(image), backendTexture, releaseProc);
104 }
105
106 SkASSERT(!texture->resourcePriv().refsWrappedObjects());
107 SkASSERT(texture->unique());
108 SkASSERT(image->unique());
109
110 // Take a reference to the GrTexture and release the image.
111 sk_sp<GrTexture> textureRef = sk_ref_sp(texture);
112 view.reset();
113 image = nullptr;
114 SkASSERT(textureRef->unique());
115
116 // Steal the backend texture from the GrTexture, releasing the GrTexture in the process.
117 return GrTexture::StealBackendTexture(std::move(textureRef), backendTexture, releaseProc);
118 }
119
120 bool GetBackendTextureFromImage(const SkImage* img,
121 GrBackendTexture* outTexture,
122 bool flushPendingGrContextIO,
123 GrSurfaceOrigin* origin) {
124 if (!img) {
125 return false;
126 }
127 auto ib = as_IB(img);
128 if (ib->type() != SkImage_Base::Type::kGanesh) {
129 return false;
130 }
131 auto ig = static_cast<const SkImage_Ganesh*>(img);
132 return ig->getExistingBackendTexture(outTexture, flushPendingGrContextIO, origin);
133 }
134
135 sk_sp<SkImage> TextureFromCompressedTexture(GrRecordingContext* context,
136 const GrBackendTexture& backendTexture,
137 GrSurfaceOrigin origin,
138 SkAlphaType alphaType,
139 sk_sp<SkColorSpace> colorSpace,
140 TextureReleaseProc textureReleaseProc,
141 ReleaseContext releaseContext) {
142 auto releaseHelper = skgpu::RefCntedCallback::Make(textureReleaseProc, releaseContext);
143
144 if (!context) {
145 return nullptr;
146 }
147
148 const GrCaps* caps = context->priv().caps();
149
150 if (!SkImage_GaneshBase::ValidateCompressedBackendTexture(caps, backendTexture, alphaType)) {
151 return nullptr;
152 }
153
154 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
155 sk_sp<GrTextureProxy> proxy =
156 proxyProvider->wrapCompressedBackendTexture(backendTexture,
157 kBorrow_GrWrapOwnership,
158 GrWrapCacheable::kNo,
159 std::move(releaseHelper));
160 if (!proxy) {
161 return nullptr;
162 }
163
164 SkTextureCompressionType type =
165 GrBackendFormatToCompressionType(backendTexture.getBackendFormat());
166 SkColorType ct = skgpu::CompressionTypeToSkColorType(type);
167
168 GrSurfaceProxyView view(std::move(proxy), origin, skgpu::Swizzle::RGBA());
169 return sk_make_sp<SkImage_Ganesh>(sk_ref_sp(context),
170 kNeedNewImageUniqueID,
171 std::move(view),
172 SkColorInfo(ct, alphaType, std::move(colorSpace)));
173 }
174
175 static sk_sp<SkImage> new_wrapped_texture_common(GrRecordingContext* rContext,
176 const GrBackendTexture& backendTex,
177 GrColorType colorType,
178 GrSurfaceOrigin origin,
179 SkAlphaType at,
180 sk_sp<SkColorSpace> colorSpace,
181 GrWrapOwnership ownership,
182 sk_sp<skgpu::RefCntedCallback> releaseHelper) {
183 if (!backendTex.isValid() || backendTex.width() <= 0 || backendTex.height() <= 0) {
184 return nullptr;
185 }
186
187 GrProxyProvider* proxyProvider = rContext->priv().proxyProvider();
188 sk_sp<GrTextureProxy> proxy = proxyProvider->wrapBackendTexture(
189 backendTex, ownership, GrWrapCacheable::kNo, kRead_GrIOType, std::move(releaseHelper));
190 if (!proxy) {
191 return nullptr;
192 }
193
194 skgpu::Swizzle swizzle =
195 rContext->priv().caps()->getReadSwizzle(proxy->backendFormat(), colorType);
196 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
197 SkColorInfo info(GrColorTypeToSkColorType(colorType), at, std::move(colorSpace));
198 return sk_make_sp<SkImage_Ganesh>(
199 sk_ref_sp(rContext), kNeedNewImageUniqueID, std::move(view), std::move(info));
200 }
201
202 sk_sp<SkImage> BorrowTextureFrom(GrRecordingContext* context,
203 const GrBackendTexture& backendTexture,
204 GrSurfaceOrigin origin,
205 SkColorType colorType,
206 SkAlphaType alphaType,
207 sk_sp<SkColorSpace> colorSpace,
208 TextureReleaseProc textureReleaseProc,
209 ReleaseContext releaseContext) {
210 auto releaseHelper = skgpu::RefCntedCallback::Make(textureReleaseProc, releaseContext);
211
212 if (!context) {
213 return nullptr;
214 }
215
216 const GrCaps* caps = context->priv().caps();
217
218 GrColorType grColorType = SkColorTypeToGrColorType(colorType);
219 if (GrColorType::kUnknown == grColorType) {
220 return nullptr;
221 }
222
223 if (!SkImage_GaneshBase::ValidateBackendTexture(
224 caps, backendTexture, grColorType, colorType, alphaType, colorSpace)) {
225 return nullptr;
226 }
227
228 return new_wrapped_texture_common(context,
229 backendTexture,
230 grColorType,
231 origin,
232 alphaType,
233 std::move(colorSpace),
234 kBorrow_GrWrapOwnership,
235 std::move(releaseHelper));
236 }
237
238 sk_sp<SkImage> AdoptTextureFrom(GrRecordingContext* context,
239 const GrBackendTexture& backendTexture,
240 GrSurfaceOrigin textureOrigin,
241 SkColorType colorType) {
242 return AdoptTextureFrom(
243 context, backendTexture, textureOrigin, colorType, kPremul_SkAlphaType, nullptr);
244 }
245
246 sk_sp<SkImage> AdoptTextureFrom(GrRecordingContext* context,
247 const GrBackendTexture& backendTexture,
248 GrSurfaceOrigin textureOrigin,
249 SkColorType colorType,
250 SkAlphaType alphaType) {
251 return AdoptTextureFrom(context, backendTexture, textureOrigin, colorType, alphaType, nullptr);
252 }
253
254 sk_sp<SkImage> AdoptTextureFrom(GrRecordingContext* context,
255 const GrBackendTexture& backendTexture,
256 GrSurfaceOrigin origin,
257 SkColorType colorType,
258 SkAlphaType alphaType,
259 sk_sp<SkColorSpace> colorSpace) {
260 auto dContext = GrAsDirectContext(context);
261 if (!dContext) {
262 // We have a DDL context and we don't support adopted textures for them.
263 return nullptr;
264 }
265
266 const GrCaps* caps = dContext->priv().caps();
267
268 GrColorType grColorType = SkColorTypeToGrColorType(colorType);
269 if (GrColorType::kUnknown == grColorType) {
270 return nullptr;
271 }
272
273 if (!SkImage_GaneshBase::ValidateBackendTexture(
274 caps, backendTexture, grColorType, colorType, alphaType, colorSpace)) {
275 return nullptr;
276 }
277
278 return new_wrapped_texture_common(dContext,
279 backendTexture,
280 grColorType,
281 origin,
282 alphaType,
283 std::move(colorSpace),
284 kAdopt_GrWrapOwnership,
285 nullptr);
286 }
287
288 sk_sp<SkImage> TextureFromCompressedTextureData(GrDirectContext* direct,
289 sk_sp<SkData> data,
290 int width,
291 int height,
292 SkTextureCompressionType type,
293 skgpu::Mipmapped mipmapped,
294 GrProtected isProtected) {
295 if (!direct || !data) {
296 return nullptr;
297 }
298
299 GrBackendFormat beFormat = direct->compressedBackendFormat(type);
300 if (!beFormat.isValid()) {
301 sk_sp<SkImage> tmp = RasterFromCompressedTextureData(std::move(data), width, height, type);
302 if (!tmp) {
303 return nullptr;
304 }
305 return TextureFromImage(direct, tmp, mipmapped);
306 }
307
308 GrProxyProvider* proxyProvider = direct->priv().proxyProvider();
309 sk_sp<GrTextureProxy> proxy = proxyProvider->createCompressedTextureProxy(
310 {width, height}, skgpu::Budgeted::kYes, mipmapped, isProtected, type, std::move(data));
311 if (!proxy) {
312 return nullptr;
313 }
314 GrSurfaceProxyView view(std::move(proxy));
315
316 SkColorType colorType = skgpu::CompressionTypeToSkColorType(type);
317
318 return sk_make_sp<SkImage_Ganesh>(sk_ref_sp(direct),
319 kNeedNewImageUniqueID,
320 std::move(view),
321 SkColorInfo(colorType, kOpaque_SkAlphaType, nullptr));
322 }
323
324 sk_sp<SkImage> PromiseTextureFrom(sk_sp<GrContextThreadSafeProxy> threadSafeProxy,
325 const GrBackendFormat& backendFormat,
326 SkISize dimensions,
327 skgpu::Mipmapped mipmapped,
328 GrSurfaceOrigin origin,
329 SkColorType colorType,
330 SkAlphaType alphaType,
331 sk_sp<SkColorSpace> colorSpace,
332 PromiseImageTextureFulfillProc textureFulfillProc,
333 PromiseImageTextureReleaseProc textureReleaseProc,
334 PromiseImageTextureContext textureContext) {
335 // Our contract is that we will always call the release proc even on failure.
336 // We use the helper to convey the context, so we need to ensure make doesn't fail.
337 textureReleaseProc = textureReleaseProc ? textureReleaseProc : [](void*) {};
338 auto releaseHelper = skgpu::RefCntedCallback::Make(textureReleaseProc, textureContext);
339 SkImageInfo info = SkImageInfo::Make(dimensions, colorType, alphaType, colorSpace);
340 if (!SkImageInfoIsValid(info)) {
341 return nullptr;
342 }
343
344 if (!threadSafeProxy) {
345 return nullptr;
346 }
347
348 if (dimensions.isEmpty()) {
349 return nullptr;
350 }
351
352 GrColorType grColorType = SkColorTypeToGrColorType(colorType);
353 if (GrColorType::kUnknown == grColorType) {
354 return nullptr;
355 }
356
357 if (!threadSafeProxy->priv().caps()->areColorTypeAndFormatCompatible(grColorType,
358 backendFormat)) {
359 return nullptr;
360 }
361
362 auto proxy = SkImage_GaneshBase::MakePromiseImageLazyProxy(threadSafeProxy.get(),
363 dimensions,
364 backendFormat,
365 mipmapped,
366 textureFulfillProc,
367 std::move(releaseHelper));
368 if (!proxy) {
369 return nullptr;
370 }
371 skgpu::Swizzle swizzle =
372 threadSafeProxy->priv().caps()->getReadSwizzle(backendFormat, grColorType);
373 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
374 sk_sp<GrImageContext> ctx(GrImageContextPriv::MakeForPromiseImage(std::move(threadSafeProxy)));
375 return sk_make_sp<SkImage_Ganesh>(std::move(ctx),
376 kNeedNewImageUniqueID,
377 std::move(view),
378 SkColorInfo(colorType, alphaType, std::move(colorSpace)));
379 }
380
381 sk_sp<SkImage> CrossContextTextureFromPixmap(GrDirectContext* dContext,
382 const SkPixmap& originalPixmap,
383 bool buildMips,
384 bool limitToMaxTextureSize) {
385 // Some backends or drivers don't support (safely) moving resources between contexts
386 if (!dContext || !dContext->priv().caps()->crossContextTextureSupport()) {
387 return RasterFromPixmapCopy(originalPixmap);
388 }
389
390 // If non-power-of-two mipmapping isn't supported, ignore the client's request
391 if (!dContext->priv().caps()->mipmapSupport()) {
392 buildMips = false;
393 }
394
395 const SkPixmap* pixmap = &originalPixmap;
396 SkAutoPixmapStorage resized;
397 int maxTextureSize = dContext->priv().caps()->maxTextureSize();
398 int maxDim = std::max(originalPixmap.width(), originalPixmap.height());
399 if (limitToMaxTextureSize && maxDim > maxTextureSize) {
400 float scale = static_cast<float>(maxTextureSize) / maxDim;
401 int newWidth = std::min(static_cast<int>(originalPixmap.width() * scale), maxTextureSize);
402 int newHeight = std::min(static_cast<int>(originalPixmap.height() * scale), maxTextureSize);
403 SkImageInfo info = originalPixmap.info().makeWH(newWidth, newHeight);
404 SkSamplingOptions sampling(SkFilterMode::kLinear);
405 if (!resized.tryAlloc(info) || !originalPixmap.scalePixels(resized, sampling)) {
406 return nullptr;
407 }
408 pixmap = &resized;
409 }
410 // Turn the pixmap into a GrTextureProxy
411 std::optional<GrMippedBitmap> bitmap =
412 GrMippedBitmap::Make(pixmap->info(), pixmap->addr(), pixmap->rowBytes());
413 if (!bitmap) {
414 return nullptr;
415 }
416 skgpu::Mipmapped mipmapped = buildMips ? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo;
417 auto [view, ct] = GrMakeUncachedBitmapProxyView(dContext, bitmap.value(), mipmapped);
418 if (!view) {
419 return RasterFromPixmapCopy(*pixmap);
420 }
421
422 sk_sp<GrTexture> texture = sk_ref_sp(view.proxy()->peekTexture());
423
424 // Flush any writes or uploads
425 dContext->priv().flushSurface(view.proxy());
426 GrGpu* gpu = dContext->priv().getGpu();
427
428 std::unique_ptr<GrSemaphore> sema = gpu->prepareTextureForCrossContextUsage(texture.get());
429
430 SkColorType skCT = GrColorTypeToSkColorType(ct);
431 auto gen = GrBackendTextureImageGenerator::Make(std::move(texture),
432 view.origin(),
433 std::move(sema),
434 skCT,
435 pixmap->alphaType(),
436 pixmap->info().refColorSpace());
437 return DeferredFromTextureGenerator(std::move(gen));
438 }
439
440 sk_sp<SkImage> TextureFromImage(GrDirectContext* dContext,
441 const SkImage* img,
442 skgpu::Mipmapped mipmapped,
443 skgpu::Budgeted budgeted) {
444 if (!dContext || !img) {
445 return nullptr;
446 }
447 auto ib = as_IB(img);
448 if (!dContext->priv().caps()->mipmapSupport() || ib->dimensions().area() <= 1) {
449 mipmapped = skgpu::Mipmapped::kNo;
450 }
451
452 if (ib->isGaneshBacked()) {
453 if (!ib->context()->priv().matches(dContext)) {
454 return nullptr;
455 }
456
457 if (mipmapped == skgpu::Mipmapped::kNo || ib->hasMipmaps()) {
458 return sk_ref_sp(const_cast<SkImage_Base*>(ib));
459 }
460 }
461 GrImageTexGenPolicy policy = budgeted == skgpu::Budgeted::kYes
462 ? GrImageTexGenPolicy::kNew_Uncached_Budgeted
463 : GrImageTexGenPolicy::kNew_Uncached_Unbudgeted;
464 // TODO: Don't flatten YUVA images here. Add mips to the planes instead.
465 auto [view, ct] = skgpu::ganesh::AsView(dContext, ib, mipmapped, /*targetSurface=*/nullptr,
466 policy);
467 if (!view) {
468 return nullptr;
469 }
470 SkASSERT(view.asTextureProxy());
471 SkASSERT(mipmapped == skgpu::Mipmapped::kNo ||
472 view.asTextureProxy()->mipmapped() == skgpu::Mipmapped::kYes);
473 SkColorInfo colorInfo(GrColorTypeToSkColorType(ct), ib->alphaType(), ib->refColorSpace());
474 return sk_make_sp<SkImage_Ganesh>(
475 sk_ref_sp(dContext), ib->uniqueID(), std::move(view), std::move(colorInfo));
476 }
477
478 sk_sp<SkImage> TextureFromYUVATextures(GrRecordingContext* context,
479 const GrYUVABackendTextures& yuvaTextures) {
480 return TextureFromYUVATextures(context, yuvaTextures, nullptr, nullptr, nullptr);
481 }
482
483 sk_sp<SkImage> TextureFromYUVATextures(GrRecordingContext* context,
484 const GrYUVABackendTextures& yuvaTextures,
485 sk_sp<SkColorSpace> imageColorSpace,
486 TextureReleaseProc textureReleaseProc,
487 ReleaseContext releaseContext) {
488 auto releaseHelper = skgpu::RefCntedCallback::Make(textureReleaseProc, releaseContext);
489
490 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
491 int numPlanes = yuvaTextures.yuvaInfo().numPlanes();
492 sk_sp<GrSurfaceProxy> proxies[SkYUVAInfo::kMaxPlanes];
493 for (int plane = 0; plane < numPlanes; ++plane) {
494 proxies[plane] = proxyProvider->wrapBackendTexture(yuvaTextures.texture(plane),
495 kBorrow_GrWrapOwnership,
496 GrWrapCacheable::kNo,
497 kRead_GrIOType,
498 releaseHelper);
499 if (!proxies[plane]) {
500 return {};
501 }
502 }
503 GrYUVATextureProxies yuvaProxies(
504 yuvaTextures.yuvaInfo(), proxies, yuvaTextures.textureOrigin());
505
506 if (!yuvaProxies.isValid()) {
507 return nullptr;
508 }
509
510 return sk_make_sp<SkImage_GaneshYUVA>(
511 sk_ref_sp(context), kNeedNewImageUniqueID, yuvaProxies, imageColorSpace);
512 }
513
514 sk_sp<SkImage> TextureFromYUVAPixmaps(GrRecordingContext* context,
515 const SkYUVAPixmaps& pixmaps,
516 skgpu::Mipmapped buildMips,
517 bool limitToMaxTextureSize) {
518 return TextureFromYUVAPixmaps(context, pixmaps, buildMips, limitToMaxTextureSize, nullptr);
519 }
520
521 sk_sp<SkImage> TextureFromYUVAPixmaps(GrRecordingContext* context,
522 const SkYUVAPixmaps& pixmaps,
523 skgpu::Mipmapped buildMips,
524 bool limitToMaxTextureSize,
525 sk_sp<SkColorSpace> imageColorSpace) {
526 if (!context) {
527 return nullptr;
528 }
529
530 if (!pixmaps.isValid()) {
531 return nullptr;
532 }
533
534 if (!context->priv().caps()->mipmapSupport()) {
535 buildMips = skgpu::Mipmapped::kNo;
536 }
537
538 // Resize the pixmaps if necessary.
539 int numPlanes = pixmaps.numPlanes();
540 int maxTextureSize = context->priv().caps()->maxTextureSize();
541 int maxDim = std::max(pixmaps.yuvaInfo().width(), pixmaps.yuvaInfo().height());
542
543 SkYUVAPixmaps tempPixmaps;
544 const SkYUVAPixmaps* pixmapsToUpload = &pixmaps;
545 // We assume no plane is larger than the image size (and at least one plane is as big).
546 if (maxDim > maxTextureSize) {
547 if (!limitToMaxTextureSize) {
548 return nullptr;
549 }
550 float scale = static_cast<float>(maxTextureSize) / maxDim;
551 SkISize newDimensions = {
552 std::min(static_cast<int>(pixmaps.yuvaInfo().width() * scale), maxTextureSize),
553 std::min(static_cast<int>(pixmaps.yuvaInfo().height() * scale), maxTextureSize)};
554 SkYUVAInfo newInfo = pixmaps.yuvaInfo().makeDimensions(newDimensions);
555 SkYUVAPixmapInfo newPixmapInfo(newInfo, pixmaps.dataType(), /*row bytes*/ nullptr);
556 tempPixmaps = SkYUVAPixmaps::Allocate(newPixmapInfo);
557 SkSamplingOptions sampling(SkFilterMode::kLinear);
558 if (!tempPixmaps.isValid()) {
559 return nullptr;
560 }
561 for (int i = 0; i < numPlanes; ++i) {
562 if (!pixmaps.plane(i).scalePixels(tempPixmaps.plane(i), sampling)) {
563 return nullptr;
564 }
565 }
566 pixmapsToUpload = &tempPixmaps;
567 }
568
569 // Convert to texture proxies.
570 GrSurfaceProxyView views[SkYUVAInfo::kMaxPlanes];
571 GrColorType pixmapColorTypes[SkYUVAInfo::kMaxPlanes];
572 for (int i = 0; i < numPlanes; ++i) {
573 // Turn the pixmap into a GrTextureProxy
574 std::optional<GrMippedBitmap> bitmap = GrMippedBitmap::Make(pixmapsToUpload->plane(i));
575 if (!bitmap) {
576 return nullptr;
577 }
578 pixmapColorTypes[i] = SkColorTypeToGrColorType(bitmap->colorType());
579 std::tie(views[i], std::ignore) =
580 GrMakeUncachedBitmapProxyView(context, bitmap.value(), buildMips);
581 if (!views[i]) {
582 return nullptr;
583 }
584 }
585
586 GrYUVATextureProxies yuvaProxies(pixmapsToUpload->yuvaInfo(), views, pixmapColorTypes);
587 SkASSERT(yuvaProxies.isValid());
588 return sk_make_sp<SkImage_GaneshYUVA>(sk_ref_sp(context),
589 kNeedNewImageUniqueID,
590 std::move(yuvaProxies),
591 std::move(imageColorSpace));
592 }
593
594 sk_sp<SkImage> PromiseTextureFromYUVA(sk_sp<GrContextThreadSafeProxy> threadSafeProxy,
595 const GrYUVABackendTextureInfo& backendTextureInfo,
596 sk_sp<SkColorSpace> imageColorSpace,
597 PromiseImageTextureFulfillProc textureFulfillProc,
598 PromiseImageTextureReleaseProc textureReleaseProc,
599 PromiseImageTextureContext textureContexts[]) {
600 if (!backendTextureInfo.isValid()) {
601 return nullptr;
602 }
603
604 SkISize planeDimensions[SkYUVAInfo::kMaxPlanes];
605 int n = backendTextureInfo.yuvaInfo().planeDimensions(planeDimensions);
606
607 // Our contract is that we will always call the release proc even on failure.
608 // We use the helper to convey the context, so we need to ensure make doesn't fail.
609 textureReleaseProc = textureReleaseProc ? textureReleaseProc : [](void*) {};
610 sk_sp<skgpu::RefCntedCallback> releaseHelpers[4];
611 for (int i = 0; i < n; ++i) {
612 releaseHelpers[i] = skgpu::RefCntedCallback::Make(textureReleaseProc, textureContexts[i]);
613 }
614
615 if (!threadSafeProxy) {
616 return nullptr;
617 }
618
619 SkAlphaType at =
620 backendTextureInfo.yuvaInfo().hasAlpha() ? kPremul_SkAlphaType : kOpaque_SkAlphaType;
621 SkImageInfo info = SkImageInfo::Make(
622 backendTextureInfo.yuvaInfo().dimensions(), SkImage_GaneshYUVA::kAssumedColorType, at,
623 imageColorSpace);
624 if (!SkImageInfoIsValid(info)) {
625 return nullptr;
626 }
627
628 // Make a lazy proxy for each plane
629 sk_sp<GrSurfaceProxy> proxies[4];
630 for (int i = 0; i < n; ++i) {
631 proxies[i] =
632 SkImage_GaneshBase::MakePromiseImageLazyProxy(threadSafeProxy.get(),
633 planeDimensions[i],
634 backendTextureInfo.planeFormat(i),
635 skgpu::Mipmapped::kNo,
636 textureFulfillProc,
637 std::move(releaseHelpers[i]));
638 if (!proxies[i]) {
639 return nullptr;
640 }
641 }
642 GrYUVATextureProxies yuvaTextureProxies(
643 backendTextureInfo.yuvaInfo(), proxies, backendTextureInfo.textureOrigin());
644 SkASSERT(yuvaTextureProxies.isValid());
645 sk_sp<GrImageContext> ctx(GrImageContextPriv::MakeForPromiseImage(std::move(threadSafeProxy)));
646 return sk_make_sp<SkImage_GaneshYUVA>(std::move(ctx),
647 kNeedNewImageUniqueID,
648 std::move(yuvaTextureProxies),
649 std::move(imageColorSpace));
650 }
651
652 } // namespace SkImages