xref: /aosp_15_r20/external/skia/src/gpu/ganesh/image/GrImageUtils.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/SurfaceFillContext.h"
57 #include "src/gpu/ganesh/effects/GrBicubicEffect.h"
58 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
59 #include "src/gpu/ganesh/effects/GrYUVtoRGBEffect.h"
60 #include "src/gpu/ganesh/image/SkImage_Ganesh.h"
61 #include "src/gpu/ganesh/image/SkImage_GaneshBase.h"
62 #include "src/gpu/ganesh/image/SkImage_RasterPinnable.h"
63 #include "src/gpu/ganesh/image/SkSpecialImage_Ganesh.h"
64 #include "src/image/SkImage_Base.h"
65 #include "src/image/SkImage_Lazy.h"
66 #include "src/image/SkImage_Picture.h"
67 #include "src/image/SkImage_Raster.h"
68 
69 #include <string_view>
70 #include <utility>
71 
72 class SkDevice;
73 class SkMatrix;
74 class SkSurfaceProps;
75 enum SkColorType : int;
76 
77 #if defined(SK_USE_LEGACY_BLUR_GANESH)
78 #include "include/core/SkPoint.h"
79 #include "include/core/SkScalar.h"
80 #include "src/core/SkSpecialImage.h"
81 #include "src/gpu/ganesh/GrBlurUtils.h"
82 #include "src/gpu/ganesh/SurfaceDrawContext.h"
83 #else
84 class SkSpecialImage;
85 #endif
86 
87 namespace skgpu::ganesh {
88 
CopyView(GrRecordingContext * context,GrSurfaceProxyView src,skgpu::Mipmapped mipmapped,GrImageTexGenPolicy policy,std::string_view label)89 GrSurfaceProxyView CopyView(GrRecordingContext* context,
90                             GrSurfaceProxyView src,
91                             skgpu::Mipmapped mipmapped,
92                             GrImageTexGenPolicy policy,
93                             std::string_view label) {
94     skgpu::Budgeted budgeted = policy == GrImageTexGenPolicy::kNew_Uncached_Budgeted
95                                        ? skgpu::Budgeted::kYes
96                                        : skgpu::Budgeted::kNo;
97     return GrSurfaceProxyView::Copy(context,
98                                     std::move(src),
99                                     mipmapped,
100                                     SkBackingFit::kExact,
101                                     budgeted,
102                                     /*label=*/label);
103 }
104 
RasterAsView(GrRecordingContext * rContext,const SkImage_Raster * raster,skgpu::Mipmapped mipmapped,GrImageTexGenPolicy policy)105 std::tuple<GrSurfaceProxyView, GrColorType> RasterAsView(GrRecordingContext* rContext,
106                                                          const SkImage_Raster* raster,
107                                                          skgpu::Mipmapped mipmapped,
108                                                          GrImageTexGenPolicy policy) {
109     if (policy == GrImageTexGenPolicy::kDraw) {
110         // If the draw doesn't require mipmaps but this SkImage has them go ahead and make a
111         // mipmapped texture. There are three reasons for this:
112         // 1) Avoiding another texture creation if a later draw requires mipmaps.
113         // 2) Ensuring we upload the bitmap's levels instead of generating on the GPU from the base.
114         if (raster->hasMipmaps()) {
115             mipmapped = skgpu::Mipmapped::kYes;
116         }
117         return GrMakeCachedBitmapProxyView(rContext,
118                                            raster->bitmap(),
119                                            /*label=*/"TextureForImageRasterWithPolicyEqualKDraw",
120                                            mipmapped);
121     }
122     auto budgeted = (policy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted)
123                             ? skgpu::Budgeted::kNo
124                             : skgpu::Budgeted::kYes;
125     return GrMakeUncachedBitmapProxyView(
126             rContext, raster->bitmap(), mipmapped, SkBackingFit::kExact, budgeted);
127 }
128 
129 // Returns the GrColorType to use with the GrTextureProxy returned from lockTextureProxy. This
130 // may be different from the color type on the image in the case where we need up upload CPU
131 // data to a texture but the GPU doesn't support the format of CPU data. In this case we convert
132 // the data to RGBA_8888 unorm on the CPU then upload that.
ColorTypeOfLockTextureProxy(const GrCaps * caps,SkColorType sct)133 GrColorType ColorTypeOfLockTextureProxy(const GrCaps* caps, SkColorType sct) {
134     GrColorType ct = SkColorTypeToGrColorType(sct);
135     GrBackendFormat format = caps->getDefaultBackendFormat(ct, GrRenderable::kNo);
136     if (!format.isValid()) {
137         ct = GrColorType::kRGBA_8888;
138     }
139     return ct;
140 }
141 
get_origin(const SkImage_Lazy * img)142 static GrSurfaceOrigin get_origin(const SkImage_Lazy* img) {
143     SkASSERT(img->generator());
144     if (!img->generator()->isTextureGenerator()) {
145         return kTopLeft_GrSurfaceOrigin;
146     }
147     // origin should be thread safe
148     return static_cast<const GrTextureGenerator*>(img->generator()->fGenerator.get())->origin();
149 }
150 
151 
texture_proxy_view_from_planes(GrRecordingContext * ctx,const SkImage_Lazy * img,skgpu::Budgeted budgeted)152 static GrSurfaceProxyView texture_proxy_view_from_planes(GrRecordingContext* ctx,
153                                                          const SkImage_Lazy* img,
154                                                          skgpu::Budgeted budgeted) {
155     auto supportedDataTypes = SupportedTextureFormats(*ctx);
156     SkYUVAPixmaps yuvaPixmaps;
157     sk_sp<SkCachedData> dataStorage = img->getPlanes(supportedDataTypes, &yuvaPixmaps);
158     if (!dataStorage) {
159         return {};
160     }
161 
162     GrSurfaceProxyView views[SkYUVAInfo::kMaxPlanes];
163     GrColorType pixmapColorTypes[SkYUVAInfo::kMaxPlanes];
164     for (int i = 0; i < yuvaPixmaps.numPlanes(); ++i) {
165         // If the sizes of the components are not all the same we choose to create exact-match
166         // textures for the smaller ones rather than add a texture domain to the draw.
167         // TODO: revisit this decision to improve texture reuse?
168         SkBackingFit fit = yuvaPixmaps.plane(i).dimensions() == img->dimensions()
169                                    ? SkBackingFit::kApprox
170                                    : SkBackingFit::kExact;
171 
172         // We grab a ref to cached yuv data. When the SkBitmap we create below goes away it will
173         // call releaseProc which will release this ref.
174         // DDL TODO: Currently we end up creating a lazy proxy that will hold onto a ref to the
175         // SkImage in its lambda. This means that we'll keep the ref on the YUV data around for the
176         // life time of the proxy and not just upload. For non-DDL draws we should look into
177         // releasing this SkImage after uploads (by deleting the lambda after instantiation).
178         auto releaseProc = [](void*, void* data) {
179             auto cachedData = static_cast<SkCachedData*>(data);
180             SkASSERT(cachedData);
181             cachedData->unref();
182         };
183         SkBitmap bitmap;
184         bitmap.installPixels(yuvaPixmaps.plane(i).info(),
185                              yuvaPixmaps.plane(i).writable_addr(),
186                              yuvaPixmaps.plane(i).rowBytes(),
187                              releaseProc,
188                              SkRef(dataStorage.get()));
189         bitmap.setImmutable();
190 
191         std::tie(views[i], std::ignore) =
192                 GrMakeUncachedBitmapProxyView(ctx, bitmap, skgpu::Mipmapped::kNo, fit);
193         if (!views[i]) {
194             return {};
195         }
196         pixmapColorTypes[i] = SkColorTypeToGrColorType(bitmap.colorType());
197     }
198 
199     // TODO: investigate preallocating mip maps here
200     GrImageInfo info(SkColorTypeToGrColorType(img->colorType()),
201                      kPremul_SkAlphaType,
202                      /*color space*/ nullptr,
203                      img->dimensions());
204 
205     auto sfc = ctx->priv().makeSFC(info,
206                                    "ImageLazy_TextureProxyViewFromPlanes",
207                                    SkBackingFit::kExact,
208                                    1,
209                                    skgpu::Mipmapped::kNo,
210                                    GrProtected::kNo,
211                                    kTopLeft_GrSurfaceOrigin,
212                                    budgeted);
213     if (!sfc) {
214         return {};
215     }
216 
217     GrYUVATextureProxies yuvaProxies(yuvaPixmaps.yuvaInfo(), views, pixmapColorTypes);
218     SkAssertResult(yuvaProxies.isValid());
219 
220     std::unique_ptr<GrFragmentProcessor> fp = GrYUVtoRGBEffect::Make(
221             yuvaProxies,
222             GrSamplerState::Filter::kNearest,
223             *ctx->priv().caps());
224 
225     // The pixels after yuv->rgb will be in the generator's color space.
226     // If onMakeColorTypeAndColorSpace has been called then this will not match this image's
227     // color space. To correct this, apply a color space conversion from the generator's color
228     // space to this image's color space.
229     SkColorSpace* srcColorSpace = img->generator()->getInfo().colorSpace();
230     SkColorSpace* dstColorSpace = img->colorSpace();
231 
232     // If the caller expects the pixels in a different color space than the one from the image,
233     // apply a color conversion to do this.
234     fp = GrColorSpaceXformEffect::Make(std::move(fp),
235                                        srcColorSpace, kOpaque_SkAlphaType,
236                                        dstColorSpace, kOpaque_SkAlphaType);
237     sfc->fillWithFP(std::move(fp));
238 
239     return sfc->readSurfaceView();
240 }
241 
generate_picture_texture(GrRecordingContext * ctx,const SkImage_Picture * img,skgpu::Mipmapped mipmapped,GrImageTexGenPolicy texGenPolicy)242 static GrSurfaceProxyView generate_picture_texture(GrRecordingContext* ctx,
243                                                    const SkImage_Picture* img,
244                                                    skgpu::Mipmapped mipmapped,
245                                                    GrImageTexGenPolicy texGenPolicy) {
246     SkASSERT(ctx);
247     SkASSERT(img);
248 
249     skgpu::Budgeted budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted
250                                        ? skgpu::Budgeted::kNo
251                                        : skgpu::Budgeted::kYes;
252     auto surface = SkSurfaces::RenderTarget(ctx,
253                                             budgeted,
254                                             img->imageInfo(),
255                                             0,
256                                             kTopLeft_GrSurfaceOrigin,
257                                             img->props(),
258                                             mipmapped == skgpu::Mipmapped::kYes);
259     if (!surface) {
260         return {};
261     }
262 
263     img->replay(surface->getCanvas());
264 
265     sk_sp<SkImage> image(surface->makeImageSnapshot());
266     if (!image) {
267         return {};
268     }
269 
270     auto [view, ct] = AsView(ctx, image, mipmapped);
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
LockTextureProxyView(GrRecordingContext * rContext,const SkImage_Lazy * img,GrImageTexGenPolicy texGenPolicy,skgpu::Mipmapped mipmapped)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/7094.
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         } else if (img->generator()->isTextureGenerator()) {
346             auto sharedGenerator = img->generator();
347             SkAutoMutexExclusive mutex(sharedGenerator->fMutex);
348             auto textureGen = static_cast<GrTextureGenerator*>(sharedGenerator->fGenerator.get());
349             if (auto view = textureGen->generateTexture(rContext,
350                                                         img->imageInfo(),
351                                                         mipmapped,
352                                                         texGenPolicy)) {
353                 installKey(view);
354                 return view;
355             }
356         }
357     }
358 
359     // 3. Ask the generator to return YUV planes, which the GPU can convert. If we will be mipping
360     //    the texture we skip this step so the CPU generate non-planar MIP maps for us.
361     if (mipmapped == skgpu::Mipmapped::kNo &&
362         !rContext->priv().options().fDisableGpuYUVConversion) {
363         // TODO: Update to create the mipped surface in the textureProxyViewFromPlanes generator and
364         //  draw the base layer directly into the mipped surface.
365         skgpu::Budgeted budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted
366                                            ? skgpu::Budgeted::kNo
367                                            : skgpu::Budgeted::kYes;
368         auto view = texture_proxy_view_from_planes(rContext, img, budgeted);
369         if (view) {
370             installKey(view);
371             return view;
372         }
373     }
374 
375     // 4. Ask the generator to return a bitmap, which the GPU can convert.
376     auto hint = texGenPolicy == GrImageTexGenPolicy::kDraw ? SkImage::CachingHint::kAllow_CachingHint
377                                                            : SkImage::CachingHint::kDisallow_CachingHint;
378     if (SkBitmap bitmap; img->getROPixels(nullptr, &bitmap, hint)) {
379         // We always make an uncached bitmap here because we will cache it based on passed in policy
380         // with *our* key, not a key derived from bitmap. We're just making the proxy here.
381         auto budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted
382                                 ? skgpu::Budgeted::kNo
383                                 : skgpu::Budgeted::kYes;
384         auto view = std::get<0>(GrMakeUncachedBitmapProxyView(rContext,
385                                                               bitmap,
386                                                               mipmapped,
387                                                               SkBackingFit::kExact,
388                                                               budgeted));
389         if (view) {
390             installKey(view);
391             return view;
392         }
393     }
394 
395     return {};
396 }
397 
lazy_as_view(GrRecordingContext * context,const SkImage_Lazy * img,skgpu::Mipmapped mipmapped,GrImageTexGenPolicy policy)398 static std::tuple<GrSurfaceProxyView, GrColorType> lazy_as_view(GrRecordingContext* context,
399                                                                 const SkImage_Lazy* img,
400                                                                 skgpu::Mipmapped mipmapped,
401                                                                 GrImageTexGenPolicy policy) {
402     GrColorType ct = ColorTypeOfLockTextureProxy(context->priv().caps(), img->colorType());
403     return {LockTextureProxyView(context, img, policy, mipmapped), ct};
404 }
405 
AsView(GrRecordingContext * rContext,const SkImage * img,skgpu::Mipmapped mipmapped,GrImageTexGenPolicy policy)406 std::tuple<GrSurfaceProxyView, GrColorType> AsView(GrRecordingContext* rContext,
407                                                    const SkImage* img,
408                                                    skgpu::Mipmapped mipmapped,
409                                                    GrImageTexGenPolicy policy) {
410     SkASSERT(img);
411     if (!rContext) {
412         return {};
413     }
414     if (!rContext->priv().caps()->mipmapSupport() || img->dimensions().area() <= 1) {
415         mipmapped = skgpu::Mipmapped::kNo;
416     }
417 
418     auto ib = static_cast<const SkImage_Base*>(img);
419     if (ib->type() == SkImage_Base::Type::kRaster) {
420         return skgpu::ganesh::RasterAsView(
421                     rContext, static_cast<const SkImage_Raster*>(ib), mipmapped, policy);
422     } else if (ib->type() == SkImage_Base::Type::kRasterPinnable) {
423         auto rp = static_cast<const SkImage_RasterPinnable*>(img);
424         return rp->asView(rContext, mipmapped, policy);
425     } else if (ib->isGaneshBacked()) {
426         auto gb = static_cast<const SkImage_GaneshBase*>(img);
427         return gb->asView(rContext, mipmapped, policy);
428     } else if (ib->isLazyGenerated()) {
429         return lazy_as_view(rContext, static_cast<const SkImage_Lazy*>(ib), mipmapped, policy);
430     }
431 
432     SkDEBUGFAIL("Unsupported image type to make a View");
433     return {};
434 }
435 
make_fp_from_view(GrRecordingContext * rContext,GrSurfaceProxyView view,SkAlphaType at,SkSamplingOptions sampling,const SkTileMode tileModes[2],const SkMatrix & m,const SkRect * subset,const SkRect * domain)436 static std::unique_ptr<GrFragmentProcessor> make_fp_from_view(GrRecordingContext* rContext,
437                                                               GrSurfaceProxyView view,
438                                                               SkAlphaType at,
439                                                               SkSamplingOptions sampling,
440                                                               const SkTileMode tileModes[2],
441                                                               const SkMatrix& m,
442                                                               const SkRect* subset,
443                                                               const SkRect* domain) {
444     if (!view) {
445         return nullptr;
446     }
447     const GrCaps& caps = *rContext->priv().caps();
448     auto wmx = SkTileModeToWrapMode(tileModes[0]);
449     auto wmy = SkTileModeToWrapMode(tileModes[1]);
450     if (sampling.useCubic) {
451         if (subset) {
452             if (domain) {
453                 return GrBicubicEffect::MakeSubset(std::move(view),
454                                                    at,
455                                                    m,
456                                                    wmx,
457                                                    wmy,
458                                                    *subset,
459                                                    *domain,
460                                                    sampling.cubic,
461                                                    GrBicubicEffect::Direction::kXY,
462                                                    *rContext->priv().caps());
463             }
464             return GrBicubicEffect::MakeSubset(std::move(view),
465                                                at,
466                                                m,
467                                                wmx,
468                                                wmy,
469                                                *subset,
470                                                sampling.cubic,
471                                                GrBicubicEffect::Direction::kXY,
472                                                *rContext->priv().caps());
473         }
474         return GrBicubicEffect::Make(std::move(view),
475                                      at,
476                                      m,
477                                      wmx,
478                                      wmy,
479                                      sampling.cubic,
480                                      GrBicubicEffect::Direction::kXY,
481                                      *rContext->priv().caps());
482     }
483     if (sampling.isAniso()) {
484         if (!rContext->priv().caps()->anisoSupport()) {
485             // Fallback to linear
486             sampling = SkSamplingPriv::AnisoFallback(view.mipmapped() == skgpu::Mipmapped::kYes);
487         }
488     } else if (view.mipmapped() == skgpu::Mipmapped::kNo) {
489         sampling = SkSamplingOptions(sampling.filter);
490     }
491     GrSamplerState sampler;
492     if (sampling.isAniso()) {
493         sampler = GrSamplerState::Aniso(wmx, wmy, sampling.maxAniso, view.mipmapped());
494     } else {
495         sampler = GrSamplerState(wmx, wmy, sampling.filter, sampling.mipmap);
496     }
497     if (subset) {
498         if (domain) {
499             return GrTextureEffect::MakeSubset(
500                     std::move(view), at, m, sampler, *subset, *domain, caps);
501         }
502         return GrTextureEffect::MakeSubset(std::move(view), at, m, sampler, *subset, caps);
503     } else {
504         return GrTextureEffect::Make(std::move(view), at, m, sampler, caps);
505     }
506 }
507 
raster_as_fp(GrRecordingContext * rContext,const SkImage_Raster * img,SkSamplingOptions sampling,const SkTileMode tileModes[2],const SkMatrix & m,const SkRect * subset,const SkRect * domain)508 std::unique_ptr<GrFragmentProcessor> raster_as_fp(GrRecordingContext* rContext,
509                                                   const SkImage_Raster* img,
510                                                   SkSamplingOptions sampling,
511                                                   const SkTileMode tileModes[2],
512                                                   const SkMatrix& m,
513                                                   const SkRect* subset,
514                                                   const SkRect* domain) {
515     auto mm =
516             sampling.mipmap == SkMipmapMode::kNone ? skgpu::Mipmapped::kNo : skgpu::Mipmapped::kYes;
517     return make_fp_from_view(rContext,
518                              std::get<0>(AsView(rContext, img, mm)),
519                              img->alphaType(),
520                              sampling,
521                              tileModes,
522                              m,
523                              subset,
524                              domain);
525 }
526 
AsFragmentProcessor(GrRecordingContext * rContext,const SkImage * img,SkSamplingOptions sampling,const SkTileMode tileModes[2],const SkMatrix & m,const SkRect * subset,const SkRect * domain)527 std::unique_ptr<GrFragmentProcessor> AsFragmentProcessor(GrRecordingContext* rContext,
528                                                          const SkImage* img,
529                                                          SkSamplingOptions sampling,
530                                                          const SkTileMode tileModes[2],
531                                                          const SkMatrix& m,
532                                                          const SkRect* subset,
533                                                          const SkRect* domain) {
534     if (!rContext) {
535         return {};
536     }
537     if (sampling.useCubic && !GrValidCubicResampler(sampling.cubic)) {
538         return {};
539     }
540     if (sampling.mipmap != SkMipmapMode::kNone &&
541         (!rContext->priv().caps()->mipmapSupport() || img->dimensions().area() <= 1)) {
542         sampling = SkSamplingOptions(sampling.filter);
543     }
544 
545     auto ib = static_cast<const SkImage_Base*>(img);
546     if (ib->isRasterBacked()) {
547         return raster_as_fp(rContext,
548                             static_cast<const SkImage_Raster*>(ib),
549                             sampling,
550                             tileModes,
551                             m,
552                             subset,
553                             domain);
554     } else if (ib->isGaneshBacked()) {
555         auto gb = static_cast<const SkImage_GaneshBase*>(img);
556         return gb->asFragmentProcessor(rContext, sampling, tileModes, m, subset, domain);
557     } else if (ib->isLazyGenerated()) {
558         // TODO: If the CPU data is extracted as planes return a FP that reconstructs the image from
559         // the planes.
560         auto mm = sampling.mipmap == SkMipmapMode::kNone ? skgpu::Mipmapped::kNo : skgpu::Mipmapped::kYes;
561         return MakeFragmentProcessorFromView(rContext,
562                                              std::get<0>(AsView(rContext, img, mm)),
563                                              img->alphaType(),
564                                              sampling,
565                                              tileModes,
566                                              m,
567                                              subset,
568                                              domain);
569     }
570 
571     SkDEBUGFAIL("Unsupported image type to make a FragmentProcessor");
572     return {};
573 }
574 
MakeFragmentProcessorFromView(GrRecordingContext * rContext,GrSurfaceProxyView view,SkAlphaType at,SkSamplingOptions sampling,const SkTileMode tileModes[2],const SkMatrix & m,const SkRect * subset,const SkRect * domain)575 std::unique_ptr<GrFragmentProcessor> MakeFragmentProcessorFromView(
576         GrRecordingContext* rContext,
577         GrSurfaceProxyView view,
578         SkAlphaType at,
579         SkSamplingOptions sampling,
580         const SkTileMode tileModes[2],
581         const SkMatrix& m,
582         const SkRect* subset,
583         const SkRect* domain) {
584     if (!view) {
585         return nullptr;
586     }
587     const GrCaps& caps = *rContext->priv().caps();
588     auto wmx = SkTileModeToWrapMode(tileModes[0]);
589     auto wmy = SkTileModeToWrapMode(tileModes[1]);
590     if (sampling.useCubic) {
591         if (subset) {
592             if (domain) {
593                 return GrBicubicEffect::MakeSubset(std::move(view),
594                                                    at,
595                                                    m,
596                                                    wmx,
597                                                    wmy,
598                                                    *subset,
599                                                    *domain,
600                                                    sampling.cubic,
601                                                    GrBicubicEffect::Direction::kXY,
602                                                    *rContext->priv().caps());
603             }
604             return GrBicubicEffect::MakeSubset(std::move(view),
605                                                at,
606                                                m,
607                                                wmx,
608                                                wmy,
609                                                *subset,
610                                                sampling.cubic,
611                                                GrBicubicEffect::Direction::kXY,
612                                                *rContext->priv().caps());
613         }
614         return GrBicubicEffect::Make(std::move(view),
615                                      at,
616                                      m,
617                                      wmx,
618                                      wmy,
619                                      sampling.cubic,
620                                      GrBicubicEffect::Direction::kXY,
621                                      *rContext->priv().caps());
622     }
623     if (sampling.isAniso()) {
624         if (!rContext->priv().caps()->anisoSupport()) {
625             // Fallback to linear
626             sampling = SkSamplingPriv::AnisoFallback(view.mipmapped() == skgpu::Mipmapped::kYes);
627         }
628     } else if (view.mipmapped() == skgpu::Mipmapped::kNo) {
629         sampling = SkSamplingOptions(sampling.filter);
630     }
631     GrSamplerState sampler;
632     if (sampling.isAniso()) {
633         sampler = GrSamplerState::Aniso(wmx, wmy, sampling.maxAniso, view.mipmapped());
634     } else {
635         sampler = GrSamplerState(wmx, wmy, sampling.filter, sampling.mipmap);
636     }
637     if (subset) {
638         if (domain) {
639             return GrTextureEffect::MakeSubset(std::move(view),
640                                                at,
641                                                m,
642                                                sampler,
643                                                *subset,
644                                                *domain,
645                                                caps);
646         }
647         return GrTextureEffect::MakeSubset(std::move(view),
648                                            at,
649                                            m,
650                                            sampler,
651                                            *subset,
652                                            caps);
653     } else {
654         return GrTextureEffect::Make(std::move(view), at, m, sampler, caps);
655     }
656 }
657 
FindOrMakeCachedMipmappedView(GrRecordingContext * rContext,GrSurfaceProxyView view,uint32_t imageUniqueID)658 GrSurfaceProxyView FindOrMakeCachedMipmappedView(GrRecordingContext* rContext,
659                                                  GrSurfaceProxyView view,
660                                                  uint32_t imageUniqueID) {
661     SkASSERT(rContext);
662     SkASSERT(imageUniqueID != SK_InvalidUniqueID);
663 
664     if (!view || view.proxy()->asTextureProxy()->mipmapped() == skgpu::Mipmapped::kYes) {
665         return view;
666     }
667     GrProxyProvider* proxyProvider = rContext->priv().proxyProvider();
668 
669     skgpu::UniqueKey baseKey;
670     GrMakeKeyFromImageID(&baseKey, imageUniqueID, SkIRect::MakeSize(view.dimensions()));
671     SkASSERT(baseKey.isValid());
672     skgpu::UniqueKey mipmappedKey;
673     static const skgpu::UniqueKey::Domain kMipmappedDomain = skgpu::UniqueKey::GenerateDomain();
674     {  // No extra values beyond the domain are required. Must name the var to please
675        // clang-tidy.
676         skgpu::UniqueKey::Builder b(&mipmappedKey, baseKey, kMipmappedDomain, 0);
677     }
678     SkASSERT(mipmappedKey.isValid());
679     if (sk_sp<GrTextureProxy> cachedMippedView =
680                 proxyProvider->findOrCreateProxyByUniqueKey(mipmappedKey)) {
681         return {std::move(cachedMippedView), view.origin(), view.swizzle()};
682     }
683 
684     auto copy = GrCopyBaseMipMapToView(rContext, view);
685     if (!copy) {
686         return view;
687     }
688     // TODO: If we move listeners up from SkImage_Lazy to SkImage_Base then add one here.
689     proxyProvider->assignUniqueKeyToProxy(mipmappedKey, copy.asTextureProxy());
690     return copy;
691 }
692 
693 using DataType = SkYUVAPixmapInfo::DataType;
694 
SupportedTextureFormats(const GrImageContext & context)695 SkYUVAPixmapInfo::SupportedDataTypes SupportedTextureFormats(const GrImageContext& context) {
696     SkYUVAPixmapInfo::SupportedDataTypes dataTypes;
697     const auto isValid = [&context](DataType dt, int n) {
698         return context.defaultBackendFormat(SkYUVAPixmapInfo::DefaultColorTypeForDataType(dt, n),
699                                             GrRenderable::kNo).isValid();
700     };
701      for (int n = 1; n <= 4; ++n) {
702         if (isValid(DataType::kUnorm8, n)) {
703             dataTypes.enableDataType(DataType::kUnorm8, n);
704         }
705         if (isValid(DataType::kUnorm16, n)) {
706             dataTypes.enableDataType(DataType::kUnorm16, n);
707         }
708         if (isValid(DataType::kFloat16, n)) {
709             dataTypes.enableDataType(DataType::kFloat16, n);
710         }
711         if (isValid(DataType::kUnorm10_Unorm2, n)) {
712             dataTypes.enableDataType(DataType::kUnorm10_Unorm2, n);
713         }
714     }
715      return dataTypes;
716 }
717 
718 }  // namespace skgpu::ganesh
719 
720 namespace skif {
721 
722 namespace {
723 
724 class GaneshBackend :
725         public Backend,
726 #if defined(SK_USE_LEGACY_BLUR_GANESH)
727         private SkBlurEngine::Algorithm,
728 #else
729         private SkShaderBlurAlgorithm,
730 #endif
731         private SkBlurEngine {
732 public:
733 
GaneshBackend(sk_sp<GrRecordingContext> context,GrSurfaceOrigin origin,const SkSurfaceProps & surfaceProps,SkColorType colorType)734     GaneshBackend(sk_sp<GrRecordingContext> context,
735                   GrSurfaceOrigin origin,
736                   const SkSurfaceProps& surfaceProps,
737                   SkColorType colorType)
738             : Backend(SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize),
739                       surfaceProps, colorType)
740             , fContext(std::move(context))
741             , fOrigin(origin) {}
742 
743     // Backend
makeDevice(SkISize size,sk_sp<SkColorSpace> colorSpace,const SkSurfaceProps * props) const744     sk_sp<SkDevice> makeDevice(SkISize size,
745                                sk_sp<SkColorSpace> colorSpace,
746                                const SkSurfaceProps* props) const override {
747         SkImageInfo imageInfo = SkImageInfo::Make(size,
748                                                   this->colorType(),
749                                                   kPremul_SkAlphaType,
750                                                   std::move(colorSpace));
751 
752         return fContext->priv().createDevice(skgpu::Budgeted::kYes,
753                                              imageInfo,
754                                              SkBackingFit::kApprox,
755                                              1,
756                                              skgpu::Mipmapped::kNo,
757                                              GrProtected::kNo,
758                                              fOrigin,
759                                              props ? *props : this->surfaceProps(),
760                                              skgpu::ganesh::Device::InitContents::kUninit);
761     }
762 
makeImage(const SkIRect & subset,sk_sp<SkImage> image) const763     sk_sp<SkSpecialImage> makeImage(const SkIRect& subset, sk_sp<SkImage> image) const override {
764         return SkSpecialImages::MakeFromTextureImage(
765                 fContext.get(), subset, image, this->surfaceProps());
766     }
767 
getCachedBitmap(const SkBitmap & data) const768     sk_sp<SkImage> getCachedBitmap(const SkBitmap& data) const override {
769         // This uses the thread safe cache (instead of GrMakeCachedBitmapProxyView) so that image
770         // filters can be evaluated on other threads with DDLs.
771         auto threadSafeCache = fContext->priv().threadSafeCache();
772 
773         skgpu::UniqueKey key;
774         SkIRect subset = SkIRect::MakePtSize(data.pixelRefOrigin(), data.dimensions());
775         GrMakeKeyFromImageID(&key, data.getGenerationID(), subset);
776 
777         auto view = threadSafeCache->find(key);
778         if (!view) {
779             view = std::get<0>(GrMakeUncachedBitmapProxyView(fContext.get(), data));
780             if (!view) {
781                 return nullptr;
782             }
783             threadSafeCache->add(key, view);
784         }
785 
786         return sk_make_sp<SkImage_Ganesh>(fContext,
787                                           data.getGenerationID(),
788                                           std::move(view),
789                                           data.info().colorInfo());
790     }
791 
getBlurEngine() const792     const SkBlurEngine* getBlurEngine() const override { return this; }
793 
794     // SkBlurEngine
findAlgorithm(SkSize sigma,SkColorType colorType) const795     const SkBlurEngine::Algorithm* findAlgorithm(SkSize sigma,
796                                                  SkColorType colorType) const override {
797         // GrBlurUtils supports all tile modes and color types
798         return this;
799     }
800 
801 #if defined(SK_USE_LEGACY_BLUR_GANESH)
802     // NOTE: When SK_USE_LEGACY_BLUR_GANESH is defined, `useLegacyFilterResultBlur()` returns true,
803     // so FilterResult::blur() will resolve all tiling in the original image space before calling
804     // into this function that routes to GrBlurUtils::GaussianBlur to perform the rescaling and
805     // blurring. It is possible ot restore original GrBlurUtils performance by just having
806     // `useLegacyFilterResultBlur()` return false but still reporting a max sigma of infinity and
807     // advertising support for all tile modes.
808     //
809     // Since all clients are currently rebased on the intermediate "legacy" blur approach, the ideal
810     // step would be to just migrate them to the SkShaderBlurAlgorithm variant instead of first
811     // going back to pure GrBlurUtils. But if needed for cherry-picking to old releases, the
812     // original GrBlurUtils behavior can be achieved quikly.
813 
814     // SkBlurEngine::Algorithm
maxSigma() const815     float maxSigma() const override {
816         // GrBlurUtils handles resizing at the moment
817         return SK_ScalarInfinity;
818     }
819 
supportsOnlyDecalTiling() const820     bool supportsOnlyDecalTiling() const override { return false; }
821 
blur(SkSize sigma,sk_sp<SkSpecialImage> input,const SkIRect & srcRect,SkTileMode tileMode,const SkIRect & dstRect) const822     sk_sp<SkSpecialImage> blur(SkSize sigma,
823                                sk_sp<SkSpecialImage> input,
824                                const SkIRect& srcRect,
825                                SkTileMode tileMode,
826                                const SkIRect& dstRect) const override {
827         GrSurfaceProxyView inputView = SkSpecialImages::AsView(fContext.get(), input);
828         if (!inputView.proxy()) {
829             return nullptr;
830         }
831         SkASSERT(inputView.asTextureProxy());
832 
833         // Update srcRect and dstRect to be relative to the underlying texture proxy of 'input'.
834         auto proxyOffset = input->subset().topLeft() - srcRect.topLeft();
835         auto sdc = GrBlurUtils::GaussianBlur(
836                 fContext.get(),
837                 std::move(inputView),
838                 SkColorTypeToGrColorType(input->colorType()),
839                 input->alphaType(),
840                 sk_ref_sp(input->getColorSpace()),
841                 dstRect.makeOffset(proxyOffset),
842                 srcRect.makeOffset(proxyOffset),
843                 sigma.width(),
844                 sigma.height(),
845                 tileMode);
846         if (!sdc) {
847             return nullptr;
848         }
849 
850         return SkSpecialImages::MakeDeferredFromGpu(fContext.get(),
851                                                     SkIRect::MakeSize(dstRect.size()),
852                                                     kNeedNewImageUniqueID_SpecialImage,
853                                                     sdc->readSurfaceView(),
854                                                     sdc->colorInfo(),
855                                                     this->surfaceProps());
856     }
857 #else
useLegacyFilterResultBlur() const858     bool useLegacyFilterResultBlur() const override { return false; }
859 
860     // SkShaderBlurAlgorithm
makeDevice(const SkImageInfo & imageInfo) const861     sk_sp<SkDevice> makeDevice(const SkImageInfo& imageInfo) const override {
862         return fContext->priv().createDevice(skgpu::Budgeted::kYes,
863                                              imageInfo,
864                                              SkBackingFit::kApprox,
865                                              1,
866                                              skgpu::Mipmapped::kNo,
867                                              GrProtected::kNo,
868                                              fOrigin,
869                                              this->surfaceProps(),
870                                              skgpu::ganesh::Device::InitContents::kUninit);
871     }
872 
873 #endif
874 
875 private:
876     sk_sp<GrRecordingContext> fContext;
877     GrSurfaceOrigin fOrigin;
878 };
879 
880 } // anonymous namespace
881 
MakeGaneshBackend(sk_sp<GrRecordingContext> context,GrSurfaceOrigin origin,const SkSurfaceProps & surfaceProps,SkColorType colorType)882 sk_sp<Backend> MakeGaneshBackend(sk_sp<GrRecordingContext> context,
883                                  GrSurfaceOrigin origin,
884                                  const SkSurfaceProps& surfaceProps,
885                                  SkColorType colorType) {
886     SkASSERT(context);
887     return sk_make_sp<GaneshBackend>(std::move(context), origin, surfaceProps, colorType);
888 }
889 
890 }  // namespace skif
891