| 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 "src/gpu/ganesh/image/GrImageUtils.h" |
| 9 |
|
| 10 |
#include "include/core/SkAlphaType.h" |
| 11 |
#include "include/core/SkBitmap.h" |
| 12 |
#include "include/core/SkColorSpace.h" |
| 13 |
#include "include/core/SkImage.h" |
| 14 |
#include "include/core/SkImageInfo.h" |
| 15 |
#include "include/core/SkPixmap.h" |
| 16 |
#include "include/core/SkRect.h" |
| 17 |
#include "include/core/SkSamplingOptions.h" |
| 18 |
#include "include/core/SkSize.h" |
| 19 |
#include "include/core/SkSurface.h" |
| 20 |
#include "include/core/SkTypes.h" |
| 21 |
#include "include/core/SkYUVAInfo.h" |
| 22 |
#include "include/core/SkYUVAPixmaps.h" |
| 23 |
#include "include/gpu/GpuTypes.h" |
| 24 |
#include "include/gpu/ganesh/GrBackendSurface.h" |
| 25 |
#include "include/gpu/ganesh/GrContextOptions.h" |
| 26 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 27 |
#include "include/gpu/ganesh/GrTypes.h" |
| 28 |
#include "include/gpu/ganesh/SkSurfaceGanesh.h" |
| 29 |
#include "include/private/SkIDChangeListener.h" |
| 30 |
#include "include/private/base/SkMutex.h" |
| 31 |
#include "include/private/gpu/ganesh/GrImageContext.h" |
| 32 |
#include "include/private/gpu/ganesh/GrTextureGenerator.h" |
| 33 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 34 |
#include "src/core/SkBlurEngine.h" |
| 35 |
#include "src/core/SkCachedData.h" |
| 36 |
#include "src/core/SkImageFilterCache.h" |
| 37 |
#include "src/core/SkImageFilterTypes.h" |
| 38 |
#include "src/core/SkSamplingPriv.h" |
| 39 |
#include "src/gpu/ResourceKey.h" |
| 40 |
#include "src/gpu/SkBackingFit.h" |
| 41 |
#include "src/gpu/Swizzle.h" |
| 42 |
#include "src/gpu/ganesh/Device.h" |
| 43 |
#include "src/gpu/ganesh/GrCaps.h" |
| 44 |
#include "src/gpu/ganesh/GrColorSpaceXform.h" |
| 45 |
#include "src/gpu/ganesh/GrFragmentProcessor.h" |
| 46 |
#include "src/gpu/ganesh/GrImageInfo.h" |
| 47 |
#include "src/gpu/ganesh/GrProxyProvider.h" |
| 48 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 49 |
#include "src/gpu/ganesh/GrSamplerState.h" |
| 50 |
#include "src/gpu/ganesh/GrSurfaceProxy.h" |
| 51 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 52 |
#include "src/gpu/ganesh/GrTextureProxy.h" |
| 53 |
#include "src/gpu/ganesh/GrThreadSafeCache.h" |
| 54 |
#include "src/gpu/ganesh/GrYUVATextureProxies.h" |
| 55 |
#include "src/gpu/ganesh/SkGr.h" |
| 56 |
#include "src/gpu/ganesh/SurfaceDrawContext.h" |
| 57 |
#include "src/gpu/ganesh/SurfaceFillContext.h" |
| 58 |
#include "src/gpu/ganesh/effects/GrBicubicEffect.h" |
| 59 |
#include "src/gpu/ganesh/effects/GrTextureEffect.h" |
| 60 |
#include "src/gpu/ganesh/effects/GrYUVtoRGBEffect.h" |
| 61 |
#include "src/gpu/ganesh/image/GrMippedBitmap.h" |
| 62 |
#include "src/gpu/ganesh/image/SkImage_Ganesh.h" |
| 63 |
#include "src/gpu/ganesh/image/SkImage_GaneshBase.h" |
| 64 |
#include "src/gpu/ganesh/image/SkImage_RasterPinnable.h" |
| 65 |
#include "src/gpu/ganesh/image/SkSpecialImage_Ganesh.h" |
| 66 |
#include "src/image/SkImage_Base.h" |
| 67 |
#include "src/image/SkImage_Lazy.h" |
| 68 |
#include "src/image/SkImage_Picture.h" |
| 69 |
#include "src/image/SkImage_Raster.h" |
| 70 |
|
| 71 |
#include <string_view> |
| 72 |
#include <utility> |
| 73 |
|
| 74 |
class SkDevice; |
| 75 |
class SkMatrix; |
| 76 |
class SkSurfaceProps; |
| 77 |
enum SkColorType : int; |
| 78 |
|
| 79 |
class SkSpecialImage; |
| 80 |
|
| 81 |
namespace skgpu::ganesh { |
| 82 |
|
| 83 |
GrSurfaceProxyView CopyView(GrRecordingContext* context, |
| 84 |
GrSurfaceProxyView src, |
| 85 |
skgpu::Mipmapped mipmapped, |
| 86 |
GrImageTexGenPolicy policy, |
| 87 |
std::string_view label) { |
| 88 |
skgpu::Budgeted budgeted = policy == GrImageTexGenPolicy::kNew_Uncached_Budgeted |
| 89 |
? skgpu::Budgeted::kYes |
| 90 |
: skgpu::Budgeted::kNo; |
| 91 |
return GrSurfaceProxyView::Copy(context, |
| 92 |
std::move(src), |
| 93 |
mipmapped, |
| 94 |
SkBackingFit::kExact, |
| 95 |
budgeted, |
| 96 |
/*label=*/label); |
| 97 |
} |
| 98 |
|
| 99 |
std::tuple<GrSurfaceProxyView, GrColorType> RasterAsView(GrRecordingContext* rContext, |
| 100 |
const SkImage_Raster* raster, |
| 101 |
skgpu::Mipmapped mipmapped, |
| 102 |
GrImageTexGenPolicy policy) { |
| 103 |
if (policy == GrImageTexGenPolicy::kDraw) { |
| 104 |
// If the draw doesn't require mipmaps but this SkImage has them go ahead and make a |
| 105 |
// mipmapped texture. There are three reasons for this: |
| 106 |
// 1) Avoiding another texture creation if a later draw requires mipmaps. |
| 107 |
// 2) Ensuring we upload the bitmap's levels instead of generating on the GPU from the base. |
| 108 |
if (raster->hasMipmaps()) { |
| 109 |
mipmapped = skgpu::Mipmapped::kYes; |
| 110 |
} |
| 111 |
return GrMakeCachedBitmapProxyView(rContext, |
| 112 |
GrMippedBitmap(raster->bitmap(), raster->refMips()), |
| 113 |
/*label=*/"TextureForImageRasterWithPolicyEqualKDraw", |
| 114 |
mipmapped); |
| 115 |
} |
| 116 |
auto budgeted = (policy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted) |
| 117 |
? skgpu::Budgeted::kNo |
| 118 |
: skgpu::Budgeted::kYes; |
| 119 |
return GrMakeUncachedBitmapProxyView(rContext, |
| 120 |
GrMippedBitmap(raster->bitmap(), raster->refMips()), |
| 121 |
mipmapped, |
| 122 |
SkBackingFit::kExact, |
| 123 |
budgeted); |
| 124 |
} |
| 125 |
|
| 126 |
// Returns the GrColorType to use with the GrTextureProxy returned from lockTextureProxy. This |
| 127 |
// may be different from the color type on the image in the case where we need up upload CPU |
| 128 |
// data to a texture but the GPU doesn't support the format of CPU data. In this case we convert |
| 129 |
// the data to RGBA_8888 unorm on the CPU then upload that. |
| 130 |
GrColorType ColorTypeOfLockTextureProxy(const GrCaps* caps, SkColorType sct) { |
| 131 |
GrColorType ct = SkColorTypeToGrColorType(sct); |
| 132 |
GrBackendFormat format = caps->getDefaultBackendFormat(ct, GrRenderable::kNo); |
| 133 |
if (!format.isValid()) { |
| 134 |
ct = GrColorType::kRGBA_8888; |
| 135 |
} |
| 136 |
return ct; |
| 137 |
} |
| 138 |
|
| 139 |
static GrSurfaceOrigin get_origin(const SkImage_Lazy* img) { |
| 140 |
SkASSERT(img->generator()); |
| 141 |
if (!img->generator()->isTextureGenerator()) { |
| 142 |
return kTopLeft_GrSurfaceOrigin; |
| 143 |
} |
| 144 |
// origin should be thread safe |
| 145 |
return static_cast<const GrTextureGenerator*>(img->generator()->fGenerator.get())->origin(); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
static GrSurfaceProxyView texture_proxy_view_from_planes(GrRecordingContext* ctx, |
| 150 |
const SkImage_Lazy* img, |
| 151 |
skgpu::Budgeted budgeted) { |
| 152 |
auto supportedDataTypes = SupportedTextureFormats(*ctx); |
| 153 |
SkYUVAPixmaps yuvaPixmaps; |
| 154 |
sk_sp<SkCachedData> dataStorage = img->getPlanes(supportedDataTypes, &yuvaPixmaps); |
| 155 |
if (!dataStorage) { |
| 156 |
return {}; |
| 157 |
} |
| 158 |
|
| 159 |
GrSurfaceProxyView views[SkYUVAInfo::kMaxPlanes]; |
| 160 |
GrColorType pixmapColorTypes[SkYUVAInfo::kMaxPlanes]; |
| 161 |
for (int i = 0; i < yuvaPixmaps.numPlanes(); ++i) { |
| 162 |
// If the sizes of the components are not all the same we choose to create exact-match |
| 163 |
// textures for the smaller ones rather than add a texture domain to the draw. |
| 164 |
// TODO: revisit this decision to improve texture reuse? |
| 165 |
SkBackingFit fit = yuvaPixmaps.plane(i).dimensions() == img->dimensions() |
| 166 |
? SkBackingFit::kApprox |
| 167 |
: SkBackingFit::kExact; |
| 168 |
|
| 169 |
// We grab a ref to cached yuv data. When the SkBitmap we create below goes away it will |
| 170 |
// call releaseProc which will release this ref. |
| 171 |
// DDL TODO: Currently we end up creating a lazy proxy that will hold onto a ref to the |
| 172 |
// SkImage in its lambda. This means that we'll keep the ref on the YUV data around for the |
| 173 |
// life time of the proxy and not just upload. For non-DDL draws we should look into |
| 174 |
// releasing this SkImage after uploads (by deleting the lambda after instantiation). |
| 175 |
auto releaseProc = [](void*, void* data) { |
| 176 |
auto cachedData = static_cast<SkCachedData*>(data); |
| 177 |
SkASSERT(cachedData); |
| 178 |
cachedData->unref(); |
| 179 |
}; |
| 180 |
std::optional<GrMippedBitmap> bitmap = |
| 181 |
GrMippedBitmap::Make(yuvaPixmaps.plane(i).info(), |
| 182 |
yuvaPixmaps.plane(i).writable_addr(), |
| 183 |
yuvaPixmaps.plane(i).rowBytes(), |
| 184 |
releaseProc, |
| 185 |
SkRef(dataStorage.get())); |
| 186 |
if (!bitmap) { |
| 187 |
return {}; |
| 188 |
} |
| 189 |
pixmapColorTypes[i] = SkColorTypeToGrColorType(bitmap->colorType()); |
| 190 |
std::tie(views[i], std::ignore) = |
| 191 |
GrMakeUncachedBitmapProxyView(ctx, bitmap.value(), skgpu::Mipmapped::kNo, fit); |
| 192 |
if (!views[i]) { |
| 193 |
return {}; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
// TODO: investigate preallocating mip maps here |
| 198 |
GrImageInfo info(SkColorTypeToGrColorType(img->colorType()), |
| 199 |
kPremul_SkAlphaType, |
| 200 |
/*color space*/ nullptr, |
| 201 |
img->dimensions()); |
| 202 |
|
| 203 |
auto sfc = ctx->priv().makeSFC(info, |
| 204 |
"ImageLazy_TextureProxyViewFromPlanes", |
| 205 |
SkBackingFit::kExact, |
| 206 |
1, |
| 207 |
skgpu::Mipmapped::kNo, |
| 208 |
GrProtected::kNo, |
| 209 |
kTopLeft_GrSurfaceOrigin, |
| 210 |
budgeted); |
| 211 |
if (!sfc) { |
| 212 |
return {}; |
| 213 |
} |
| 214 |
|
| 215 |
GrYUVATextureProxies yuvaProxies(yuvaPixmaps.yuvaInfo(), views, pixmapColorTypes); |
| 216 |
SkAssertResult(yuvaProxies.isValid()); |
| 217 |
|
| 218 |
std::unique_ptr<GrFragmentProcessor> fp = GrYUVtoRGBEffect::Make( |
| 219 |
yuvaProxies, |
| 220 |
GrSamplerState::Filter::kNearest, |
| 221 |
*ctx->priv().caps()); |
| 222 |
|
| 223 |
// The pixels after yuv->rgb will be in the generator's color space. |
| 224 |
// If onMakeColorTypeAndColorSpace has been called then this will not match this image's |
| 225 |
// color space. To correct this, apply a color space conversion from the generator's color |
| 226 |
// space to this image's color space. |
| 227 |
SkColorSpace* srcColorSpace = img->generator()->getInfo().colorSpace(); |
| 228 |
SkColorSpace* dstColorSpace = img->colorSpace(); |
| 229 |
|
| 230 |
// If the caller expects the pixels in a different color space than the one from the image, |
| 231 |
// apply a color conversion to do this. |
| 232 |
fp = GrColorSpaceXformEffect::Make(std::move(fp), |
| 233 |
srcColorSpace, kOpaque_SkAlphaType, |
| 234 |
dstColorSpace, kOpaque_SkAlphaType); |
| 235 |
sfc->fillWithFP(std::move(fp)); |
| 236 |
|
| 237 |
return sfc->readSurfaceView(); |
| 238 |
} |
| 239 |
|
| 240 |
static GrSurfaceProxyView generate_picture_texture(GrRecordingContext* ctx, |
| 241 |
const SkImage_Picture* img, |
| 242 |
skgpu::Mipmapped mipmapped, |
| 243 |
GrImageTexGenPolicy texGenPolicy) { |
| 244 |
SkASSERT(ctx); |
| 245 |
SkASSERT(img); |
| 246 |
|
| 247 |
skgpu::Budgeted budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted |
| 248 |
? skgpu::Budgeted::kNo |
| 249 |
: skgpu::Budgeted::kYes; |
| 250 |
auto surface = SkSurfaces::RenderTarget(ctx, |
| 251 |
budgeted, |
| 252 |
img->imageInfo(), |
| 253 |
0, |
| 254 |
kTopLeft_GrSurfaceOrigin, |
| 255 |
img->props(), |
| 256 |
mipmapped == skgpu::Mipmapped::kYes); |
| 257 |
if (!surface) { |
| 258 |
return {}; |
| 259 |
} |
| 260 |
|
| 261 |
img->replay(surface->getCanvas()); |
| 262 |
|
| 263 |
sk_sp<SkImage> image(surface->makeImageSnapshot()); |
| 264 |
if (!image) { |
| 265 |
return {}; |
| 266 |
} |
| 267 |
|
| 268 |
// The surface the image was created from is only local to this function so there will not be a |
| 269 |
// need for a copy when getting the view of the image for a future draw. |
| 270 |
auto [view, ct] = AsView(ctx, image, mipmapped, /*targetSurface=*/nullptr); |
| 271 |
SkASSERT(view); |
| 272 |
SkASSERT(mipmapped == skgpu::Mipmapped::kNo || |
| 273 |
view.asTextureProxy()->mipmapped() == skgpu::Mipmapped::kYes); |
| 274 |
return view; |
| 275 |
} |
| 276 |
|
| 277 |
// Returns the texture proxy. We will always cache the generated texture on success. |
| 278 |
// We have 4 ways to try to return a texture (in sorted order) |
| 279 |
// |
| 280 |
// 1. Check the cache for a pre-existing one |
| 281 |
// 2. Ask the generator to natively create one |
| 282 |
// 3. Ask the generator to return YUV planes, which the GPU can convert |
| 283 |
// 4. Ask the generator to return RGB(A) data, which the GPU can convert |
| 284 |
GrSurfaceProxyView LockTextureProxyView(GrRecordingContext* rContext, |
| 285 |
const SkImage_Lazy* img, |
| 286 |
GrImageTexGenPolicy texGenPolicy, |
| 287 |
skgpu::Mipmapped mipmapped) { |
| 288 |
skgpu::UniqueKey key; |
| 289 |
if (texGenPolicy == GrImageTexGenPolicy::kDraw) { |
| 290 |
GrMakeKeyFromImageID(&key, img->uniqueID(), SkIRect::MakeSize(img->dimensions())); |
| 291 |
} |
| 292 |
|
| 293 |
const GrCaps* caps = rContext->priv().caps(); |
| 294 |
GrProxyProvider* proxyProvider = rContext->priv().proxyProvider(); |
| 295 |
|
| 296 |
auto installKey = [&](const GrSurfaceProxyView& view) { |
| 297 |
SkASSERT(view && view.asTextureProxy()); |
| 298 |
if (key.isValid()) { |
| 299 |
auto listener = GrMakeUniqueKeyInvalidationListener(&key, rContext->priv().contextID()); |
| 300 |
img->addUniqueIDListener(std::move(listener)); |
| 301 |
proxyProvider->assignUniqueKeyToProxy(key, view.asTextureProxy()); |
| 302 |
} |
| 303 |
}; |
| 304 |
|
| 305 |
auto ct = ColorTypeOfLockTextureProxy(caps, img->colorType()); |
| 306 |
|
| 307 |
// 1. Check the cache for a pre-existing one. |
| 308 |
if (key.isValid()) { |
| 309 |
auto proxy = proxyProvider->findOrCreateProxyByUniqueKey(key); |
| 310 |
if (proxy) { |
| 311 |
skgpu::Swizzle swizzle = caps->getReadSwizzle(proxy->backendFormat(), ct); |
| 312 |
GrSurfaceOrigin origin = get_origin(img); |
| 313 |
GrSurfaceProxyView view(std::move(proxy), origin, swizzle); |
| 314 |
if (mipmapped == skgpu::Mipmapped::kNo || |
| 315 |
view.asTextureProxy()->mipmapped() == skgpu::Mipmapped::kYes) { |
| 316 |
return view; |
| 317 |
} else { |
| 318 |
// We need a mipped proxy, but we found a cached proxy that wasn't mipped. Thus we |
| 319 |
// generate a new mipped surface and copy the original proxy into the base layer. We |
| 320 |
// will then let the gpu generate the rest of the mips. |
| 321 |
auto mippedView = GrCopyBaseMipMapToView(rContext, view); |
| 322 |
if (!mippedView) { |
| 323 |
// We failed to make a mipped proxy with the base copied into it. This could |
| 324 |
// have been from failure to make the proxy or failure to do the copy. Thus we |
| 325 |
// will fall back to just using the non mipped proxy; See skbug.com/40038328. |
| 326 |
return view; |
| 327 |
} |
| 328 |
proxyProvider->removeUniqueKeyFromProxy(view.asTextureProxy()); |
| 329 |
installKey(mippedView); |
| 330 |
return mippedView; |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
// 2. Ask the generator to natively create one (if it knows how) |
| 336 |
{ |
| 337 |
if (img->type() == SkImage_Base::Type::kLazyPicture) { |
| 338 |
if (auto view = generate_picture_texture(rContext, |
| 339 |
static_cast<const SkImage_Picture*>(img), |
| 340 |
mipmapped, |
| 341 |
texGenPolicy)) { |
| 342 |
installKey(view); |
| 343 |
return view; |
| 344 |
} |
| 345 |
// The fallback for this would be to generate a bitmap, but some picture-backed |
| 346 |
// images can only be played back on the GPU. |
| 347 |
return {}; |
| 348 |
} else if (img->generator()->isTextureGenerator()) { |
| 349 |
auto sharedGenerator = img->generator(); |
| 350 |
SkAutoMutexExclusive mutex(sharedGenerator->fMutex); |
| 351 |
auto textureGen = static_cast<GrTextureGenerator*>(sharedGenerator->fGenerator.get()); |
| 352 |
if (auto view = textureGen->generateTexture(rContext, |
| 353 |
img->imageInfo(), |
| 354 |
mipmapped, |
| 355 |
texGenPolicy)) { |
| 356 |
installKey(view); |
| 357 |
return view; |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
// 3. Ask the generator to return YUV planes, which the GPU can convert. If we will be mipping |
| 363 |
// the texture we skip this step so the CPU generate non-planar MIP maps for us. |
| 364 |
if (mipmapped == skgpu::Mipmapped::kNo && |
| 365 |
!rContext->priv().options().fDisableGpuYUVConversion) { |
| 366 |
// TODO: Update to create the mipped surface in the textureProxyViewFromPlanes generator and |
| 367 |
// draw the base layer directly into the mipped surface. |
| 368 |
skgpu::Budgeted budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted |
| 369 |
? skgpu::Budgeted::kNo |
| 370 |
: skgpu::Budgeted::kYes; |
| 371 |
auto view = texture_proxy_view_from_planes(rContext, img, budgeted); |
| 372 |
if (view) { |
| 373 |
installKey(view); |
| 374 |
return view; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
// 4. Ask the generator to return a bitmap, which the GPU can convert. |
| 379 |
auto hint = texGenPolicy == GrImageTexGenPolicy::kDraw ? SkImage::CachingHint::kAllow_CachingHint |
| 380 |
: SkImage::CachingHint::kDisallow_CachingHint; |
| 381 |
if (SkBitmap bitmap; img->getROPixels(nullptr, &bitmap, hint)) { |
| 382 |
// We always make an uncached bitmap here because we will cache it based on passed in policy |
| 383 |
// with *our* key, not a key derived from bitmap. We're just making the proxy here. |
| 384 |
auto budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted |
| 385 |
? skgpu::Budgeted::kNo |
| 386 |
: skgpu::Budgeted::kYes; |
| 387 |
|
| 388 |
auto view = std::get<0>( |
| 389 |
GrMakeUncachedBitmapProxyView(rContext, |
| 390 |
GrMippedBitmap(bitmap, as_IB(img)->refMips()), |
| 391 |
mipmapped, |
| 392 |
SkBackingFit::kExact, |
| 393 |
budgeted)); |
| 394 |
if (view) { |
| 395 |
installKey(view); |
| 396 |
return view; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
return {}; |
| 401 |
} |
| 402 |
|
| 403 |
static std::tuple<GrSurfaceProxyView, GrColorType> lazy_as_view(GrRecordingContext* context, |
| 404 |
const SkImage_Lazy* img, |
| 405 |
skgpu::Mipmapped mipmapped, |
| 406 |
GrImageTexGenPolicy policy) { |
| 407 |
GrColorType ct = ColorTypeOfLockTextureProxy(context->priv().caps(), img->colorType()); |
| 408 |
return {LockTextureProxyView(context, img, policy, mipmapped), ct}; |
| 409 |
} |
| 410 |
|
| 411 |
std::tuple<GrSurfaceProxyView, GrColorType> AsView(GrRecordingContext* rContext, |
| 412 |
const SkImage* img, |
| 413 |
skgpu::Mipmapped mipmapped, |
| 414 |
GrRenderTargetProxy* targetSurface, |
| 415 |
GrImageTexGenPolicy policy) { |
| 416 |
SkASSERT(img); |
| 417 |
if (!rContext) { |
| 418 |
return {}; |
| 419 |
} |
| 420 |
if (!rContext->priv().caps()->mipmapSupport() || img->dimensions().area() <= 1) { |
| 421 |
mipmapped = skgpu::Mipmapped::kNo; |
| 422 |
} |
| 423 |
|
| 424 |
auto ib = static_cast<const SkImage_Base*>(img); |
| 425 |
if (ib->type() == SkImage_Base::Type::kRaster) { |
| 426 |
return skgpu::ganesh::RasterAsView( |
| 427 |
rContext, static_cast<const SkImage_Raster*>(ib), mipmapped, policy); |
| 428 |
} else if (ib->type() == SkImage_Base::Type::kRasterPinnable) { |
| 429 |
auto rp = static_cast<const SkImage_RasterPinnable*>(img); |
| 430 |
return rp->asView(rContext, mipmapped, policy); |
| 431 |
} else if (ib->isGaneshBacked()) { |
| 432 |
auto gb = static_cast<const SkImage_GaneshBase*>(img); |
| 433 |
return gb->asView(rContext, mipmapped, policy, targetSurface); |
| 434 |
} else if (ib->isLazyGenerated()) { |
| 435 |
return lazy_as_view(rContext, static_cast<const SkImage_Lazy*>(ib), mipmapped, policy); |
| 436 |
} |
| 437 |
|
| 438 |
SkDEBUGFAIL("Unsupported image type to make a View"); |
| 439 |
return {}; |
| 440 |
} |
| 441 |
|
| 442 |
static std::unique_ptr<GrFragmentProcessor> make_fp_from_view(GrRecordingContext* rContext, |
| 443 |
GrSurfaceProxyView view, |
| 444 |
SkAlphaType at, |
| 445 |
SkSamplingOptions sampling, |
| 446 |
const SkTileMode tileModes[2], |
| 447 |
const SkMatrix& m, |
| 448 |
const SkRect* subset, |
| 449 |
const SkRect* domain) { |
| 450 |
if (!view) { |
| 451 |
return nullptr; |
| 452 |
} |
| 453 |
const GrCaps& caps = *rContext->priv().caps(); |
| 454 |
auto wmx = SkTileModeToWrapMode(tileModes[0]); |
| 455 |
auto wmy = SkTileModeToWrapMode(tileModes[1]); |
| 456 |
if (sampling.useCubic) { |
| 457 |
if (subset) { |
| 458 |
if (domain) { |
| 459 |
return GrBicubicEffect::MakeSubset(std::move(view), |
| 460 |
at, |
| 461 |
m, |
| 462 |
wmx, |
| 463 |
wmy, |
| 464 |
*subset, |
| 465 |
*domain, |
| 466 |
sampling.cubic, |
| 467 |
GrBicubicEffect::Direction::kXY, |
| 468 |
*rContext->priv().caps()); |
| 469 |
} |
| 470 |
return GrBicubicEffect::MakeSubset(std::move(view), |
| 471 |
at, |
| 472 |
m, |
| 473 |
wmx, |
| 474 |
wmy, |
| 475 |
*subset, |
| 476 |
sampling.cubic, |
| 477 |
GrBicubicEffect::Direction::kXY, |
| 478 |
*rContext->priv().caps()); |
| 479 |
} |
| 480 |
return GrBicubicEffect::Make(std::move(view), |
| 481 |
at, |
| 482 |
m, |
| 483 |
wmx, |
| 484 |
wmy, |
| 485 |
sampling.cubic, |
| 486 |
GrBicubicEffect::Direction::kXY, |
| 487 |
*rContext->priv().caps()); |
| 488 |
} |
| 489 |
if (sampling.isAniso()) { |
| 490 |
if (!rContext->priv().caps()->anisoSupport()) { |
| 491 |
// Fallback to linear |
| 492 |
sampling = SkSamplingPriv::AnisoFallback(view.mipmapped() == skgpu::Mipmapped::kYes); |
| 493 |
} |
| 494 |
} else if (view.mipmapped() == skgpu::Mipmapped::kNo) { |
| 495 |
sampling = SkSamplingOptions(sampling.filter); |
| 496 |
} |
| 497 |
GrSamplerState sampler; |
| 498 |
if (sampling.isAniso()) { |
| 499 |
sampler = GrSamplerState::Aniso(wmx, wmy, sampling.maxAniso, view.mipmapped()); |
| 500 |
} else { |
| 501 |
sampler = GrSamplerState(wmx, wmy, sampling.filter, sampling.mipmap); |
| 502 |
} |
| 503 |
if (subset) { |
| 504 |
if (domain) { |
| 505 |
return GrTextureEffect::MakeSubset( |
| 506 |
std::move(view), at, m, sampler, *subset, *domain, caps); |
| 507 |
} |
| 508 |
return GrTextureEffect::MakeSubset(std::move(view), at, m, sampler, *subset, caps); |
| 509 |
} else { |
| 510 |
return GrTextureEffect::Make(std::move(view), at, m, sampler, caps); |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
std::unique_ptr<GrFragmentProcessor> raster_as_fp(GrRecordingContext* rContext, |
| 515 |
const SkImage_Raster* img, |
| 516 |
SkSamplingOptions sampling, |
| 517 |
const SkTileMode tileModes[2], |
| 518 |
const SkMatrix& m, |
| 519 |
const SkRect* subset, |
| 520 |
const SkRect* domain) { |
| 521 |
auto mm = |
| 522 |
sampling.mipmap == SkMipmapMode::kNone ? skgpu::Mipmapped::kNo : skgpu::Mipmapped::kYes; |
| 523 |
return make_fp_from_view(rContext, |
| 524 |
std::get<0>(AsView(rContext, img, mm, /*targetSurface=*/nullptr)), |
| 525 |
img->alphaType(), |
| 526 |
sampling, |
| 527 |
tileModes, |
| 528 |
m, |
| 529 |
subset, |
| 530 |
domain); |
| 531 |
} |
| 532 |
|
| 533 |
std::unique_ptr<GrFragmentProcessor> AsFragmentProcessor(SurfaceDrawContext* sdc, |
| 534 |
const SkImage* img, |
| 535 |
SkSamplingOptions sampling, |
| 536 |
const SkTileMode tileModes[2], |
| 537 |
const SkMatrix& m, |
| 538 |
const SkRect* subset, |
| 539 |
const SkRect* domain) { |
| 540 |
if (!sdc) { |
| 541 |
return {}; |
| 542 |
} |
| 543 |
GrRecordingContext* rContext = sdc->recordingContext(); |
| 544 |
if (!rContext) { |
| 545 |
return {}; |
| 546 |
} |
| 547 |
if (sampling.useCubic && !GrValidCubicResampler(sampling.cubic)) { |
| 548 |
return {}; |
| 549 |
} |
| 550 |
if (sampling.mipmap != SkMipmapMode::kNone && |
| 551 |
(!rContext->priv().caps()->mipmapSupport() || img->dimensions().area() <= 1)) { |
| 552 |
sampling = SkSamplingOptions(sampling.filter); |
| 553 |
} |
| 554 |
|
| 555 |
auto ib = static_cast<const SkImage_Base*>(img); |
| 556 |
if (ib->isRasterBacked()) { |
| 557 |
return raster_as_fp(rContext, |
| 558 |
static_cast<const SkImage_Raster*>(ib), |
| 559 |
sampling, |
| 560 |
tileModes, |
| 561 |
m, |
| 562 |
subset, |
| 563 |
domain); |
| 564 |
} else if (ib->isGaneshBacked()) { |
| 565 |
auto gb = static_cast<const SkImage_GaneshBase*>(img); |
| 566 |
return gb->asFragmentProcessor(sdc, sampling, tileModes, m, subset, domain); |
| 567 |
} else if (ib->isLazyGenerated()) { |
| 568 |
// TODO: If the CPU data is extracted as planes return a FP that reconstructs the image from |
| 569 |
// the planes. |
| 570 |
auto mm = sampling.mipmap == SkMipmapMode::kNone ? skgpu::Mipmapped::kNo |
| 571 |
: skgpu::Mipmapped::kYes; |
| 572 |
auto rtp = sdc->asRenderTargetProxy(); |
| 573 |
return MakeFragmentProcessorFromView(rContext, |
| 574 |
std::get<0>(AsView(rContext, img, mm, rtp)), |
| 575 |
img->alphaType(), |
| 576 |
sampling, |
| 577 |
tileModes, |
| 578 |
m, |
| 579 |
subset, |
| 580 |
domain); |
| 581 |
} |
| 582 |
|
| 583 |
SkDEBUGFAIL("Unsupported image type to make a FragmentProcessor"); |
| 584 |
return {}; |
| 585 |
} |
| 586 |
|
| 587 |
std::unique_ptr<GrFragmentProcessor> MakeFragmentProcessorFromView( |
| 588 |
GrRecordingContext* rContext, |
| 589 |
GrSurfaceProxyView view, |
| 590 |
SkAlphaType at, |
| 591 |
SkSamplingOptions sampling, |
| 592 |
const SkTileMode tileModes[2], |
| 593 |
const SkMatrix& m, |
| 594 |
const SkRect* subset, |
| 595 |
const SkRect* domain) { |
| 596 |
if (!view) { |
| 597 |
return nullptr; |
| 598 |
} |
| 599 |
const GrCaps& caps = *rContext->priv().caps(); |
| 600 |
auto wmx = SkTileModeToWrapMode(tileModes[0]); |
| 601 |
auto wmy = SkTileModeToWrapMode(tileModes[1]); |
| 602 |
if (sampling.useCubic) { |
| 603 |
if (subset) { |
| 604 |
if (domain) { |
| 605 |
return GrBicubicEffect::MakeSubset(std::move(view), |
| 606 |
at, |
| 607 |
m, |
| 608 |
wmx, |
| 609 |
wmy, |
| 610 |
*subset, |
| 611 |
*domain, |
| 612 |
sampling.cubic, |
| 613 |
GrBicubicEffect::Direction::kXY, |
| 614 |
*rContext->priv().caps()); |
| 615 |
} |
| 616 |
return GrBicubicEffect::MakeSubset(std::move(view), |
| 617 |
at, |
| 618 |
m, |
| 619 |
wmx, |
| 620 |
wmy, |
| 621 |
*subset, |
| 622 |
sampling.cubic, |
| 623 |
GrBicubicEffect::Direction::kXY, |
| 624 |
*rContext->priv().caps()); |
| 625 |
} |
| 626 |
return GrBicubicEffect::Make(std::move(view), |
| 627 |
at, |
| 628 |
m, |
| 629 |
wmx, |
| 630 |
wmy, |
| 631 |
sampling.cubic, |
| 632 |
GrBicubicEffect::Direction::kXY, |
| 633 |
*rContext->priv().caps()); |
| 634 |
} |
| 635 |
if (sampling.isAniso()) { |
| 636 |
if (!rContext->priv().caps()->anisoSupport()) { |
| 637 |
// Fallback to linear |
| 638 |
sampling = SkSamplingPriv::AnisoFallback(view.mipmapped() == skgpu::Mipmapped::kYes); |
| 639 |
} |
| 640 |
} else if (view.mipmapped() == skgpu::Mipmapped::kNo) { |
| 641 |
sampling = SkSamplingOptions(sampling.filter); |
| 642 |
} |
| 643 |
GrSamplerState sampler; |
| 644 |
if (sampling.isAniso()) { |
| 645 |
sampler = GrSamplerState::Aniso(wmx, wmy, sampling.maxAniso, view.mipmapped()); |
| 646 |
} else { |
| 647 |
sampler = GrSamplerState(wmx, wmy, sampling.filter, sampling.mipmap); |
| 648 |
} |
| 649 |
if (subset) { |
| 650 |
if (domain) { |
| 651 |
return GrTextureEffect::MakeSubset(std::move(view), |
| 652 |
at, |
| 653 |
m, |
| 654 |
sampler, |
| 655 |
*subset, |
| 656 |
*domain, |
| 657 |
caps); |
| 658 |
} |
| 659 |
return GrTextureEffect::MakeSubset(std::move(view), |
| 660 |
at, |
| 661 |
m, |
| 662 |
sampler, |
| 663 |
*subset, |
| 664 |
caps); |
| 665 |
} else { |
| 666 |
return GrTextureEffect::Make(std::move(view), at, m, sampler, caps); |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
GrSurfaceProxyView FindOrMakeCachedMipmappedView(GrRecordingContext* rContext, |
| 671 |
GrSurfaceProxyView view, |
| 672 |
uint32_t imageUniqueID) { |
| 673 |
SkASSERT(rContext); |
| 674 |
SkASSERT(imageUniqueID != SK_InvalidUniqueID); |
| 675 |
|
| 676 |
if (!view || view.proxy()->asTextureProxy()->mipmapped() == skgpu::Mipmapped::kYes) { |
| 677 |
return view; |
| 678 |
} |
| 679 |
GrProxyProvider* proxyProvider = rContext->priv().proxyProvider(); |
| 680 |
|
| 681 |
skgpu::UniqueKey baseKey; |
| 682 |
GrMakeKeyFromImageID(&baseKey, imageUniqueID, SkIRect::MakeSize(view.dimensions())); |
| 683 |
SkASSERT(baseKey.isValid()); |
| 684 |
skgpu::UniqueKey mipmappedKey; |
| 685 |
static const skgpu::UniqueKey::Domain kMipmappedDomain = skgpu::UniqueKey::GenerateDomain(); |
| 686 |
{ // No extra values beyond the domain are required. Must name the var to please clang-tidy. |
| 687 |
skgpu::UniqueKey::Builder b(&mipmappedKey, baseKey, kMipmappedDomain, 0); |
| 688 |
} |
| 689 |
SkASSERT(mipmappedKey.isValid()); |
| 690 |
if (sk_sp<GrTextureProxy> cachedMippedView = |
| 691 |
proxyProvider->findOrCreateProxyByUniqueKey(mipmappedKey)) { |
| 692 |
return {std::move(cachedMippedView), view.origin(), view.swizzle()}; |
| 693 |
} |
| 694 |
|
| 695 |
auto copy = GrCopyBaseMipMapToView(rContext, view); |
| 696 |
if (!copy) { |
| 697 |
return view; |
| 698 |
} |
| 699 |
// TODO: If we move listeners up from SkImage_Lazy to SkImage_Base then add one here. |
| 700 |
proxyProvider->assignUniqueKeyToProxy(mipmappedKey, copy.asTextureProxy()); |
| 701 |
return copy; |
| 702 |
} |
| 703 |
|
| 704 |
using DataType = SkYUVAPixmapInfo::DataType; |
| 705 |
|
| 706 |
SkYUVAPixmapInfo::SupportedDataTypes SupportedTextureFormats(const GrImageContext& context) { |
| 707 |
SkYUVAPixmapInfo::SupportedDataTypes dataTypes; |
| 708 |
const auto isValid = [&context](DataType dt, int n) { |
| 709 |
return context.defaultBackendFormat(SkYUVAPixmapInfo::DefaultColorTypeForDataType(dt, n), |
| 710 |
GrRenderable::kNo).isValid(); |
| 711 |
}; |
| 712 |
for (int n = 1; n <= 4; ++n) { |
| 713 |
if (isValid(DataType::kUnorm8, n)) { |
| 714 |
dataTypes.enableDataType(DataType::kUnorm8, n); |
| 715 |
} |
| 716 |
if (isValid(DataType::kUnorm16, n)) { |
| 717 |
dataTypes.enableDataType(DataType::kUnorm16, n); |
| 718 |
} |
| 719 |
if (isValid(DataType::kFloat16, n)) { |
| 720 |
dataTypes.enableDataType(DataType::kFloat16, n); |
| 721 |
} |
| 722 |
if (isValid(DataType::kUnorm10_Unorm2, n)) { |
| 723 |
dataTypes.enableDataType(DataType::kUnorm10_Unorm2, n); |
| 724 |
} |
| 725 |
} |
| 726 |
return dataTypes; |
| 727 |
} |
| 728 |
|
| 729 |
} // namespace skgpu::ganesh |
| 730 |
|
| 731 |
namespace skif { |
| 732 |
|
| 733 |
namespace { |
| 734 |
|
| 735 |
class GaneshBackend : |
| 736 |
public Backend, |
| 737 |
private SkShaderBlurAlgorithm, |
| 738 |
private SkBlurEngine { |
| 739 |
public: |
| 740 |
|
| 741 |
GaneshBackend(sk_sp<GrRecordingContext> context, |
| 742 |
GrSurfaceOrigin origin, |
| 743 |
const SkSurfaceProps& surfaceProps, |
| 744 |
SkColorType colorType) |
| 745 |
: Backend(SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize), |
| 746 |
surfaceProps, colorType) |
| 747 |
, fContext(std::move(context)) |
| 748 |
, fOrigin(origin) {} |
| 749 |
|
| 750 |
// Backend |
| 751 |
sk_sp<SkDevice> makeDevice(SkISize size, |
| 752 |
sk_sp<SkColorSpace> colorSpace, |
| 753 |
const SkSurfaceProps* props) const override { |
| 754 |
SkImageInfo imageInfo = SkImageInfo::Make(size, |
| 755 |
this->colorType(), |
| 756 |
kPremul_SkAlphaType, |
| 757 |
std::move(colorSpace)); |
| 758 |
|
| 759 |
return fContext->priv().createDevice(skgpu::Budgeted::kYes, |
| 760 |
imageInfo, |
| 761 |
SkBackingFit::kApprox, |
| 762 |
1, |
| 763 |
skgpu::Mipmapped::kNo, |
| 764 |
GrProtected::kNo, |
| 765 |
fOrigin, |
| 766 |
props ? *props : this->surfaceProps(), |
| 767 |
skgpu::ganesh::Device::InitContents::kUninit); |
| 768 |
} |
| 769 |
|
| 770 |
sk_sp<SkSpecialImage> makeImage(const SkIRect& subset, sk_sp<SkImage> image) const override { |
| 771 |
return SkSpecialImages::MakeFromTextureImage( |
| 772 |
fContext.get(), subset, image, this->surfaceProps()); |
| 773 |
} |
| 774 |
|
| 775 |
sk_sp<SkImage> getCachedBitmap(const SkBitmap& data) const override { |
| 776 |
// This uses the thread safe cache (instead of GrMakeCachedBitmapProxyView) so that image |
| 777 |
// filters can be evaluated on other threads with DDLs. |
| 778 |
auto threadSafeCache = fContext->priv().threadSafeCache(); |
| 779 |
|
| 780 |
skgpu::UniqueKey key; |
| 781 |
SkIRect subset = SkIRect::MakePtSize(data.pixelRefOrigin(), data.dimensions()); |
| 782 |
GrMakeKeyFromImageID(&key, data.getGenerationID(), subset); |
| 783 |
|
| 784 |
auto view = threadSafeCache->find(key); |
| 785 |
if (!view) { |
| 786 |
view = std::get<0>(GrMakeUncachedBitmapProxyView( |
| 787 |
fContext.get(), GrMippedBitmap(data))); |
| 788 |
if (!view) { |
| 789 |
return nullptr; |
| 790 |
} |
| 791 |
threadSafeCache->add(key, view); |
| 792 |
} |
| 793 |
|
| 794 |
return sk_make_sp<SkImage_Ganesh>(fContext, |
| 795 |
data.getGenerationID(), |
| 796 |
std::move(view), |
| 797 |
data.info().colorInfo()); |
| 798 |
} |
| 799 |
|
| 800 |
const SkBlurEngine* getBlurEngine() const override { return this; } |
| 801 |
|
| 802 |
// SkBlurEngine |
| 803 |
const SkBlurEngine::Algorithm* findAlgorithm(SkSize sigma, |
| 804 |
SkColorType colorType) const override { |
| 805 |
// GrBlurUtils supports all tile modes and color types |
| 806 |
return this; |
| 807 |
} |
| 808 |
|
| 809 |
// SkShaderBlurAlgorithm |
| 810 |
sk_sp<SkDevice> makeDevice(const SkImageInfo& imageInfo) const override { |
| 811 |
return fContext->priv().createDevice(skgpu::Budgeted::kYes, |
| 812 |
imageInfo, |
| 813 |
SkBackingFit::kApprox, |
| 814 |
1, |
| 815 |
skgpu::Mipmapped::kNo, |
| 816 |
GrProtected::kNo, |
| 817 |
fOrigin, |
| 818 |
this->surfaceProps(), |
| 819 |
skgpu::ganesh::Device::InitContents::kUninit); |
| 820 |
} |
| 821 |
|
| 822 |
private: |
| 823 |
sk_sp<GrRecordingContext> fContext; |
| 824 |
GrSurfaceOrigin fOrigin; |
| 825 |
}; |
| 826 |
|
| 827 |
} // anonymous namespace |
| 828 |
|
| 829 |
sk_sp<Backend> MakeGaneshBackend(sk_sp<GrRecordingContext> context, |
| 830 |
GrSurfaceOrigin origin, |
| 831 |
const SkSurfaceProps& surfaceProps, |
| 832 |
SkColorType colorType) { |
| 833 |
SkASSERT(context); |
| 834 |
return sk_make_sp<GaneshBackend>(std::move(context), origin, surfaceProps, colorType); |
| 835 |
} |
| 836 |
|
| 837 |
} // namespace skif |