| 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/core/SkBitmap.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkColorSpace.h" |
| 11 |
#include "include/core/SkImage.h" |
| 12 |
#include "include/core/SkSurface.h" |
| 13 |
#include "include/core/SkYUVAInfo.h" |
| 14 |
#include "include/core/SkYUVAPixmaps.h" |
| 15 |
#include "include/gpu/GpuTypes.h" |
| 16 |
#include "include/gpu/graphite/BackendTexture.h" |
| 17 |
#include "include/gpu/graphite/Image.h" |
| 18 |
#include "include/gpu/graphite/Recorder.h" |
| 19 |
#include "include/gpu/graphite/Surface.h" |
| 20 |
#include "include/gpu/graphite/YUVABackendTextures.h" |
| 21 |
#include "include/private/base/SkMutex.h" |
| 22 |
#include "src/core/SkImageFilterTypes.h" |
| 23 |
#include "src/core/SkImageFilter_Base.h" |
| 24 |
#include "src/gpu/RefCntedCallback.h" |
| 25 |
#include "src/gpu/graphite/Caps.h" |
| 26 |
#include "src/gpu/graphite/Image_Base_Graphite.h" |
| 27 |
#include "src/gpu/graphite/Image_Graphite.h" |
| 28 |
#include "src/gpu/graphite/Image_YUVA_Graphite.h" |
| 29 |
#include "src/gpu/graphite/Log.h" |
| 30 |
#include "src/gpu/graphite/RecorderPriv.h" |
| 31 |
#include "src/gpu/graphite/ResourceProvider.h" |
| 32 |
#include "src/gpu/graphite/Surface_Graphite.h" |
| 33 |
#include "src/gpu/graphite/Texture.h" |
| 34 |
#include "src/gpu/graphite/TextureProxy.h" |
| 35 |
#include "src/gpu/graphite/TextureProxyView.h" |
| 36 |
#include "src/gpu/graphite/TextureUtils.h" |
| 37 |
#include "src/image/SkImage_Base.h" |
| 38 |
#include "src/image/SkImage_Lazy.h" |
| 39 |
#include "src/image/SkImage_Picture.h" |
| 40 |
#include "src/image/SkImage_Raster.h" |
| 41 |
|
| 42 |
#include <string> |
| 43 |
|
| 44 |
namespace SkImages { |
| 45 |
|
| 46 |
using namespace skgpu::graphite; |
| 47 |
|
| 48 |
static bool validate_backend_texture(const skgpu::graphite::Caps* caps, |
| 49 |
const skgpu::graphite::BackendTexture& texture, |
| 50 |
const SkColorInfo& info) { |
| 51 |
if (!texture.isValid() || texture.dimensions().width() <= 0 || |
| 52 |
texture.dimensions().height() <= 0) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
if (!SkColorInfoIsValid(info)) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
if (!caps->isTexturable(texture.info())) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
return caps->areColorTypeAndTextureInfoCompatible(info.colorType(), texture.info()); |
| 65 |
} |
| 66 |
|
| 67 |
sk_sp<SkImage> WrapTexture(Recorder* recorder, |
| 68 |
const BackendTexture& backendTex, |
| 69 |
SkColorType ct, |
| 70 |
SkAlphaType at, |
| 71 |
sk_sp<SkColorSpace> cs, |
| 72 |
skgpu::Origin origin, |
| 73 |
GenerateMipmapsFromBase genMipmaps, |
| 74 |
TextureReleaseProc releaseP, |
| 75 |
ReleaseContext releaseC, |
| 76 |
std::string_view label) { |
| 77 |
auto releaseHelper = skgpu::RefCntedCallback::Make(releaseP, releaseC); |
| 78 |
|
| 79 |
if (!recorder) { |
| 80 |
return nullptr; |
| 81 |
} |
| 82 |
|
| 83 |
const Caps* caps = recorder->priv().caps(); |
| 84 |
|
| 85 |
SkColorInfo info(ct, at, std::move(cs)); |
| 86 |
|
| 87 |
if (!validate_backend_texture(caps, backendTex, info)) { |
| 88 |
return nullptr; |
| 89 |
} |
| 90 |
|
| 91 |
if (label.empty()) { |
| 92 |
label = "WrappedImage"; |
| 93 |
} |
| 94 |
|
| 95 |
sk_sp<Texture> texture = |
| 96 |
recorder->priv().resourceProvider()->createWrappedTexture(backendTex, label); |
| 97 |
if (!texture) { |
| 98 |
SKGPU_LOG_W("Texture creation failed"); |
| 99 |
return nullptr; |
| 100 |
} |
| 101 |
texture->setReleaseCallback(std::move(releaseHelper)); |
| 102 |
|
| 103 |
sk_sp<TextureProxy> proxy = TextureProxy::Wrap(std::move(texture)); |
| 104 |
SkASSERT(proxy); |
| 105 |
|
| 106 |
skgpu::Swizzle swizzle = caps->getReadSwizzle(ct, backendTex.info()); |
| 107 |
TextureProxyView view(std::move(proxy), swizzle, origin); |
| 108 |
|
| 109 |
if (genMipmaps == GenerateMipmapsFromBase::kYes) { |
| 110 |
if (view.proxy()->mipmapped() == skgpu::Mipmapped::kNo) { |
| 111 |
SKGPU_LOG_W("Failed SkImage:::WrapTexture because asked to generate mipmaps for " |
| 112 |
"nonmipmapped texture"); |
| 113 |
return nullptr; |
| 114 |
} |
| 115 |
if (!GenerateMipmaps(recorder, /*drawContext=*/nullptr, view.refProxy(), info)) { |
| 116 |
SKGPU_LOG_W("Failed SkImage::WrapTexture. Could not generate mipmaps."); |
| 117 |
return nullptr; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return sk_make_sp<skgpu::graphite::Image>(view, info); |
| 122 |
} |
| 123 |
|
| 124 |
sk_sp<SkImage> WrapTexture(Recorder* recorder, |
| 125 |
const BackendTexture& backendTex, |
| 126 |
SkColorType ct, |
| 127 |
SkAlphaType at, |
| 128 |
sk_sp<SkColorSpace> cs, |
| 129 |
skgpu::Origin origin, |
| 130 |
TextureReleaseProc releaseP, |
| 131 |
ReleaseContext releaseC, |
| 132 |
std::string_view label) { |
| 133 |
return WrapTexture(recorder, |
| 134 |
backendTex, |
| 135 |
ct, |
| 136 |
at, |
| 137 |
std::move(cs), |
| 138 |
origin, |
| 139 |
SkImages::GenerateMipmapsFromBase::kNo, |
| 140 |
releaseP, |
| 141 |
releaseC, |
| 142 |
label); |
| 143 |
} |
| 144 |
|
| 145 |
sk_sp<SkImage> WrapTexture(Recorder* recorder, |
| 146 |
const BackendTexture& backendTex, |
| 147 |
SkColorType ct, |
| 148 |
SkAlphaType at, |
| 149 |
sk_sp<SkColorSpace> cs, |
| 150 |
TextureReleaseProc releaseP, |
| 151 |
ReleaseContext releaseC, |
| 152 |
std::string_view label) { |
| 153 |
return WrapTexture(recorder, |
| 154 |
backendTex, |
| 155 |
ct, |
| 156 |
at, |
| 157 |
std::move(cs), |
| 158 |
skgpu::Origin::kTopLeft, |
| 159 |
SkImages::GenerateMipmapsFromBase::kNo, |
| 160 |
releaseP, |
| 161 |
releaseC, |
| 162 |
label); |
| 163 |
} |
| 164 |
|
| 165 |
sk_sp<SkImage> PromiseTextureFrom(Recorder* recorder, |
| 166 |
SkISize dimensions, |
| 167 |
const TextureInfo& textureInfo, |
| 168 |
const SkColorInfo& colorInfo, |
| 169 |
skgpu::Origin origin, |
| 170 |
Volatile isVolatile, |
| 171 |
GraphitePromiseTextureFulfillProc fulfillProc, |
| 172 |
GraphitePromiseImageReleaseProc imageReleaseProc, |
| 173 |
GraphitePromiseTextureReleaseProc textureReleaseProc, |
| 174 |
GraphitePromiseImageContext imageContext, |
| 175 |
std::string_view label) { |
| 176 |
// Our contract is that we will always call the _image_ release proc even on failure. |
| 177 |
// We use the helper to convey the imageContext, so we need to ensure Make doesn't fail. |
| 178 |
imageReleaseProc = imageReleaseProc ? imageReleaseProc : [](void*) {}; |
| 179 |
auto releaseHelper = skgpu::RefCntedCallback::Make(imageReleaseProc, imageContext); |
| 180 |
|
| 181 |
if (!recorder) { |
| 182 |
SKGPU_LOG_W("Null Recorder"); |
| 183 |
return nullptr; |
| 184 |
} |
| 185 |
|
| 186 |
const Caps* caps = recorder->priv().caps(); |
| 187 |
|
| 188 |
SkImageInfo info = SkImageInfo::Make(dimensions, colorInfo); |
| 189 |
if (!SkImageInfoIsValid(info)) { |
| 190 |
SKGPU_LOG_W("Invalid SkImageInfo"); |
| 191 |
return nullptr; |
| 192 |
} |
| 193 |
|
| 194 |
if (!caps->areColorTypeAndTextureInfoCompatible(colorInfo.colorType(), textureInfo)) { |
| 195 |
SKGPU_LOG_W("Incompatible SkColorType and TextureInfo"); |
| 196 |
return nullptr; |
| 197 |
} |
| 198 |
|
| 199 |
// Non-YUVA promise images use the 'imageContext' for both the release proc and fulfill proc. |
| 200 |
sk_sp<TextureProxy> proxy = MakePromiseImageLazyProxy(caps, |
| 201 |
dimensions, |
| 202 |
textureInfo, |
| 203 |
isVolatile, |
| 204 |
std::move(releaseHelper), |
| 205 |
fulfillProc, |
| 206 |
imageContext, |
| 207 |
textureReleaseProc, |
| 208 |
label); |
| 209 |
if (!proxy) { |
| 210 |
return nullptr; |
| 211 |
} |
| 212 |
|
| 213 |
skgpu::Swizzle swizzle = caps->getReadSwizzle(colorInfo.colorType(), textureInfo); |
| 214 |
TextureProxyView view(std::move(proxy), swizzle, origin); |
| 215 |
return sk_make_sp<Image>(view, colorInfo); |
| 216 |
} |
| 217 |
|
| 218 |
sk_sp<SkImage> PromiseTextureFrom(Recorder* recorder, |
| 219 |
SkISize dimensions, |
| 220 |
const TextureInfo& textureInfo, |
| 221 |
const SkColorInfo& colorInfo, |
| 222 |
Volatile isVolatile, |
| 223 |
GraphitePromiseTextureFulfillProc fulfillProc, |
| 224 |
GraphitePromiseImageReleaseProc imageReleaseProc, |
| 225 |
GraphitePromiseTextureReleaseProc textureReleaseProc, |
| 226 |
GraphitePromiseImageContext imageContext) { |
| 227 |
return PromiseTextureFrom(recorder, |
| 228 |
dimensions, |
| 229 |
textureInfo, |
| 230 |
colorInfo, |
| 231 |
skgpu::Origin::kTopLeft, |
| 232 |
isVolatile, |
| 233 |
fulfillProc, |
| 234 |
imageReleaseProc, |
| 235 |
textureReleaseProc, |
| 236 |
imageContext); |
| 237 |
} |
| 238 |
|
| 239 |
sk_sp<SkImage> PromiseTextureFromYUVA(skgpu::graphite::Recorder* recorder, |
| 240 |
const YUVABackendTextureInfo& backendTextureInfo, |
| 241 |
sk_sp<SkColorSpace> imageColorSpace, |
| 242 |
skgpu::graphite::Volatile isVolatile, |
| 243 |
GraphitePromiseTextureFulfillProc fulfillProc, |
| 244 |
GraphitePromiseImageReleaseProc imageReleaseProc, |
| 245 |
GraphitePromiseTextureReleaseProc textureReleaseProc, |
| 246 |
GraphitePromiseImageContext imageContext, |
| 247 |
GraphitePromiseTextureFulfillContext planeContexts[], |
| 248 |
std::string_view label) { |
| 249 |
// Our contract is that we will always call the _image_ release proc even on failure. |
| 250 |
// We use the helper to convey the imageContext, so we need to ensure Make doesn't fail. |
| 251 |
auto releaseHelper = skgpu::RefCntedCallback::Make(imageReleaseProc, imageContext); |
| 252 |
if (!recorder) { |
| 253 |
return nullptr; |
| 254 |
} |
| 255 |
// Precompute the dimensions for all promise texture planes |
| 256 |
SkISize planeDimensions[SkYUVAInfo::kMaxPlanes]; |
| 257 |
if (!backendTextureInfo.yuvaInfo().planeDimensions(planeDimensions)) { |
| 258 |
return nullptr; |
| 259 |
} |
| 260 |
|
| 261 |
std::string labelStr(label); |
| 262 |
if (labelStr.empty()) { |
| 263 |
labelStr = "Wrapped_PromiseYUVPlane"; |
| 264 |
} else { |
| 265 |
labelStr += "_PromiseYUVPlane"; |
| 266 |
} |
| 267 |
|
| 268 |
TextureProxyView planes[SkYUVAInfo::kMaxPlanes]; |
| 269 |
for (int i = 0; i < backendTextureInfo.numPlanes(); ++i) { |
| 270 |
sk_sp<TextureProxy> lazyProxy = MakePromiseImageLazyProxy( |
| 271 |
recorder->priv().caps(), |
| 272 |
planeDimensions[i], |
| 273 |
backendTextureInfo.planeTextureInfo(i), |
| 274 |
isVolatile, |
| 275 |
releaseHelper, |
| 276 |
fulfillProc, |
| 277 |
planeContexts[i], |
| 278 |
textureReleaseProc, |
| 279 |
labelStr); |
| 280 |
// Promise YUVA images assume the default rgba swizzle. |
| 281 |
planes[i] = TextureProxyView(std::move(lazyProxy)); |
| 282 |
} |
| 283 |
return Image_YUVA::Make(recorder->priv().caps(), backendTextureInfo.yuvaInfo(), |
| 284 |
SkSpan(planes), std::move(imageColorSpace)); |
| 285 |
} |
| 286 |
|
| 287 |
sk_sp<SkImage> SubsetTextureFrom(skgpu::graphite::Recorder* recorder, |
| 288 |
const SkImage* img, |
| 289 |
const SkIRect& subset, |
| 290 |
SkImage::RequiredProperties props) { |
| 291 |
if (!recorder || !img) { |
| 292 |
return nullptr; |
| 293 |
} |
| 294 |
auto subsetImg = img->makeSubset(recorder, subset, props); |
| 295 |
return SkImages::TextureFromImage(recorder, subsetImg, props); |
| 296 |
} |
| 297 |
|
| 298 |
sk_sp<SkImage> MakeWithFilter(skgpu::graphite::Recorder* recorder, |
| 299 |
sk_sp<SkImage> src, |
| 300 |
const SkImageFilter* filter, |
| 301 |
const SkIRect& subset, |
| 302 |
const SkIRect& clipBounds, |
| 303 |
SkIRect* outSubset, |
| 304 |
SkIPoint* offset) { |
| 305 |
if (!recorder || !src || !filter) { |
| 306 |
return nullptr; |
| 307 |
} |
| 308 |
|
| 309 |
sk_sp<skif::Backend> backend = skif::MakeGraphiteBackend(recorder, {}, src->colorType()); |
| 310 |
return as_IFB(filter)->makeImageWithFilter(std::move(backend), |
| 311 |
std::move(src), |
| 312 |
subset, |
| 313 |
clipBounds, |
| 314 |
outSubset, |
| 315 |
offset); |
| 316 |
} |
| 317 |
|
| 318 |
static sk_sp<SkImage> generate_picture_texture(skgpu::graphite::Recorder* recorder, |
| 319 |
const SkImage_Picture* img, |
| 320 |
const SkImageInfo& info, |
| 321 |
SkImage::RequiredProperties requiredProps) { |
| 322 |
auto mm = requiredProps.fMipmapped ? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo; |
| 323 |
// Use a non-budgeted surface since the image wrapping the surface's texture will be owned by |
| 324 |
// the client. |
| 325 |
sk_sp<Surface> surface = Surface::Make(recorder, |
| 326 |
info, |
| 327 |
"LazySkImagePictureTexture", |
| 328 |
skgpu::Budgeted::kNo, |
| 329 |
mm, |
| 330 |
SkBackingFit::kExact, |
| 331 |
img->props()); |
| 332 |
|
| 333 |
if (!surface) { |
| 334 |
SKGPU_LOG_E("Failed to create Surface"); |
| 335 |
return nullptr; |
| 336 |
} |
| 337 |
|
| 338 |
img->replay(surface->getCanvas()); |
| 339 |
// If the surface was created with mipmaps, they will be automatically generated when flushing |
| 340 |
// the tasks when 'surface' goes out of scope. |
| 341 |
return surface->asImage(); |
| 342 |
} |
| 343 |
|
| 344 |
/* |
| 345 |
* We only have 2 ways to create a Graphite-backed image. |
| 346 |
* |
| 347 |
* 1. Ask the generator to natively create one |
| 348 |
* 2. Ask the generator to return RGB(A) data, which the GPU can convert |
| 349 |
*/ |
| 350 |
static sk_sp<SkImage> make_texture_image_from_lazy(skgpu::graphite::Recorder* recorder, |
| 351 |
const SkImage_Lazy* img, |
| 352 |
SkImage::RequiredProperties requiredProps) { |
| 353 |
// 1. Ask the generator to natively create one. |
| 354 |
{ |
| 355 |
if (img->type() == SkImage_Base::Type::kLazyPicture) { |
| 356 |
sk_sp<SkImage> newImage = |
| 357 |
generate_picture_texture(recorder, |
| 358 |
static_cast<const SkImage_Picture*>(img), |
| 359 |
img->imageInfo(), |
| 360 |
requiredProps); |
| 361 |
if (newImage) { |
| 362 |
SkASSERT(as_IB(newImage)->isGraphiteBacked()); |
| 363 |
return newImage; |
| 364 |
} |
| 365 |
// The fallback for this would be to generate a bitmap, but some picture-backed |
| 366 |
// images can only be played back on the GPU. |
| 367 |
return nullptr; |
| 368 |
} |
| 369 |
// There is not an analog to GrTextureGenerator for Graphite yet, but if there was, |
| 370 |
// we would want to call it here. |
| 371 |
} |
| 372 |
|
| 373 |
// 2. Ask the generator to return a bitmap, which the GPU can convert. |
| 374 |
{ |
| 375 |
SkBitmap bitmap; |
| 376 |
if (img->getROPixels(nullptr, &bitmap, SkImage_Lazy::CachingHint::kDisallow_CachingHint)) { |
| 377 |
return skgpu::graphite::MakeFromBitmap(recorder, |
| 378 |
img->imageInfo().colorInfo(), |
| 379 |
bitmap, |
| 380 |
nullptr, |
| 381 |
skgpu::Budgeted::kNo, |
| 382 |
requiredProps, |
| 383 |
"LazySkImageBitmapTexture"); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
return nullptr; |
| 388 |
} |
| 389 |
|
| 390 |
sk_sp<SkImage> TextureFromImage(skgpu::graphite::Recorder* recorder, |
| 391 |
const SkImage* image, |
| 392 |
SkImage::RequiredProperties requiredProps) { |
| 393 |
if (!recorder || !image) { |
| 394 |
return nullptr; |
| 395 |
} |
| 396 |
if (image->dimensions().area() <= 1) { |
| 397 |
requiredProps.fMipmapped = false; |
| 398 |
} |
| 399 |
|
| 400 |
auto ib = as_IB(image); |
| 401 |
SkASSERT(!ib->isGaneshBacked()); |
| 402 |
|
| 403 |
if (ib->isRasterBacked()) { |
| 404 |
auto raster = static_cast<const SkImage_Raster*>(ib); |
| 405 |
return skgpu::graphite::MakeFromBitmap(recorder, |
| 406 |
raster->imageInfo().colorInfo(), |
| 407 |
raster->bitmap(), |
| 408 |
raster->refMips(), |
| 409 |
skgpu::Budgeted::kNo, |
| 410 |
requiredProps, |
| 411 |
"RasterBitmapTexture"); |
| 412 |
} |
| 413 |
if (ib->isLazyGenerated()) { |
| 414 |
return make_texture_image_from_lazy( |
| 415 |
recorder, static_cast<const SkImage_Lazy*>(ib), requiredProps); |
| 416 |
} |
| 417 |
SkASSERT(ib->isGraphiteBacked()); |
| 418 |
return ib->makeSubset(recorder, ib->bounds(), requiredProps); |
| 419 |
} |
| 420 |
|
| 421 |
sk_sp<SkImage> TextureFromYUVAPixmaps(Recorder* recorder, |
| 422 |
const SkYUVAPixmaps& pixmaps, |
| 423 |
SkImage::RequiredProperties requiredProps, |
| 424 |
bool limitToMaxTextureSize, |
| 425 |
sk_sp<SkColorSpace> imageColorSpace, |
| 426 |
std::string_view label) { |
| 427 |
if (!recorder) { |
| 428 |
return nullptr; |
| 429 |
} |
| 430 |
|
| 431 |
// Determine if we have to resize the pixmaps |
| 432 |
const int maxTextureSize = recorder->priv().caps()->maxTextureSize(); |
| 433 |
const int maxDim = std::max(pixmaps.yuvaInfo().width(), pixmaps.yuvaInfo().height()); |
| 434 |
|
| 435 |
SkYUVAPixmapInfo finalInfo = pixmaps.pixmapsInfo(); |
| 436 |
if (maxDim > maxTextureSize) { |
| 437 |
if (!limitToMaxTextureSize) { |
| 438 |
return nullptr; |
| 439 |
} |
| 440 |
float scale = static_cast<float>(maxTextureSize) / maxDim; |
| 441 |
SkISize newDimensions = { |
| 442 |
std::min(static_cast<int>(pixmaps.yuvaInfo().width() * scale), maxTextureSize), |
| 443 |
std::min(static_cast<int>(pixmaps.yuvaInfo().height() * scale), maxTextureSize)}; |
| 444 |
finalInfo = SkYUVAPixmapInfo(pixmaps.yuvaInfo().makeDimensions(newDimensions), |
| 445 |
pixmaps.dataType(), |
| 446 |
/*rowBytes=*/nullptr); |
| 447 |
} |
| 448 |
|
| 449 |
std::string labelStr(label); |
| 450 |
if (labelStr.empty()) { |
| 451 |
labelStr = "YUVRasterBitmapPlane"; |
| 452 |
} else { |
| 453 |
labelStr += "_YUVBitmapPlane"; |
| 454 |
} |
| 455 |
|
| 456 |
auto mipmapped = requiredProps.fMipmapped ? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo; |
| 457 |
TextureProxyView planes[SkYUVAInfo::kMaxPlanes]; |
| 458 |
for (int i = 0; i < finalInfo.yuvaInfo().numPlanes(); ++i) { |
| 459 |
SkBitmap bmp; |
| 460 |
if (maxDim > maxTextureSize) { |
| 461 |
// Rescale the data before uploading |
| 462 |
if (!bmp.tryAllocPixels(finalInfo.planeInfo(i)) || |
| 463 |
!pixmaps.plane(i).scalePixels(bmp.pixmap(), SkFilterMode::kLinear)) { |
| 464 |
return nullptr; |
| 465 |
} |
| 466 |
} else { |
| 467 |
// Use original data to upload |
| 468 |
if (!bmp.installPixels(pixmaps.plane(i))) { |
| 469 |
return nullptr; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
auto [view, _] = MakeBitmapProxyView(recorder, bmp, /*mipmapsIn=*/nullptr, |
| 474 |
mipmapped, skgpu::Budgeted::kNo, |
| 475 |
labelStr); |
| 476 |
planes[i] = std::move(view); |
| 477 |
} |
| 478 |
return Image_YUVA::Make(recorder->priv().caps(), finalInfo.yuvaInfo(), |
| 479 |
SkSpan(planes), std::move(imageColorSpace)); |
| 480 |
} |
| 481 |
|
| 482 |
sk_sp<SkImage> TextureFromYUVATextures(Recorder* recorder, |
| 483 |
const YUVABackendTextures& yuvaTextures, |
| 484 |
sk_sp<SkColorSpace> imageColorSpace, |
| 485 |
TextureReleaseProc releaseP, |
| 486 |
ReleaseContext releaseC, |
| 487 |
std::string_view label) { |
| 488 |
auto releaseHelper = skgpu::RefCntedCallback::Make(releaseP, releaseC); |
| 489 |
if (!recorder) { |
| 490 |
return nullptr; |
| 491 |
} |
| 492 |
|
| 493 |
std::string labelStr(label); |
| 494 |
if (labelStr.empty()) { |
| 495 |
labelStr = "Wrapped_YUVPlane"; |
| 496 |
} else { |
| 497 |
labelStr += "_YUVPlane"; |
| 498 |
} |
| 499 |
|
| 500 |
TextureProxyView planes[SkYUVAInfo::kMaxPlanes]; |
| 501 |
for (int i = 0; i < yuvaTextures.yuvaInfo().numPlanes(); ++i) { |
| 502 |
sk_sp<Texture> texture = recorder->priv().resourceProvider()->createWrappedTexture( |
| 503 |
yuvaTextures.planeTexture(i), labelStr); |
| 504 |
if (!texture) { |
| 505 |
SKGPU_LOG_W("Failed to wrap backend texture for YUVA plane %d", i); |
| 506 |
return nullptr; |
| 507 |
} |
| 508 |
texture->setReleaseCallback(releaseHelper); |
| 509 |
planes[i] = TextureProxyView(TextureProxy::Wrap(std::move(texture))); |
| 510 |
} |
| 511 |
|
| 512 |
return Image_YUVA::Make(recorder->priv().caps(), yuvaTextures.yuvaInfo(), |
| 513 |
SkSpan(planes), std::move(imageColorSpace)); |
| 514 |
} |
| 515 |
|
| 516 |
sk_sp<SkImage> TextureFromYUVAImages(Recorder* recorder, |
| 517 |
const SkYUVAInfo& yuvaInfo, |
| 518 |
SkSpan<const sk_sp<SkImage>> images, |
| 519 |
sk_sp<SkColorSpace> imageColorSpace) { |
| 520 |
// This factory is just a view of the images, so does not actually trigger any work on the |
| 521 |
// recorder. It is just used to provide the Caps. |
| 522 |
return Image_YUVA::WrapImages(recorder->priv().caps(), yuvaInfo, images, imageColorSpace); |
| 523 |
} |
| 524 |
|
| 525 |
} // namespace SkImages |