• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             // 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         auto view = std::get<0>(GrMakeUncachedBitmapProxyView(rContext,
388                                                               bitmap,
389                                                               mipmapped,
390                                                               SkBackingFit::kExact,
391                                                               budgeted));
392         if (view) {
393             installKey(view);
394             return view;
395         }
396     }
397 
398     return {};
399 }
400 
lazy_as_view(GrRecordingContext * context,const SkImage_Lazy * img,skgpu::Mipmapped mipmapped,GrImageTexGenPolicy policy)401 static std::tuple<GrSurfaceProxyView, GrColorType> lazy_as_view(GrRecordingContext* context,
402                                                                 const SkImage_Lazy* img,
403                                                                 skgpu::Mipmapped mipmapped,
404                                                                 GrImageTexGenPolicy policy) {
405     GrColorType ct = ColorTypeOfLockTextureProxy(context->priv().caps(), img->colorType());
406     return {LockTextureProxyView(context, img, policy, mipmapped), ct};
407 }
408 
AsView(GrRecordingContext * rContext,const SkImage * img,skgpu::Mipmapped mipmapped,GrImageTexGenPolicy policy)409 std::tuple<GrSurfaceProxyView, GrColorType> AsView(GrRecordingContext* rContext,
410                                                    const SkImage* img,
411                                                    skgpu::Mipmapped mipmapped,
412                                                    GrImageTexGenPolicy policy) {
413     SkASSERT(img);
414     if (!rContext) {
415         return {};
416     }
417     if (!rContext->priv().caps()->mipmapSupport() || img->dimensions().area() <= 1) {
418         mipmapped = skgpu::Mipmapped::kNo;
419     }
420 
421     auto ib = static_cast<const SkImage_Base*>(img);
422     if (ib->type() == SkImage_Base::Type::kRaster) {
423         return skgpu::ganesh::RasterAsView(
424                     rContext, static_cast<const SkImage_Raster*>(ib), mipmapped, policy);
425     } else if (ib->type() == SkImage_Base::Type::kRasterPinnable) {
426         auto rp = static_cast<const SkImage_RasterPinnable*>(img);
427         return rp->asView(rContext, mipmapped, policy);
428     } else if (ib->isGaneshBacked()) {
429         auto gb = static_cast<const SkImage_GaneshBase*>(img);
430         return gb->asView(rContext, mipmapped, policy);
431     } else if (ib->isLazyGenerated()) {
432         return lazy_as_view(rContext, static_cast<const SkImage_Lazy*>(ib), mipmapped, policy);
433     }
434 
435     SkDEBUGFAIL("Unsupported image type to make a View");
436     return {};
437 }
438 
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)439 static std::unique_ptr<GrFragmentProcessor> make_fp_from_view(GrRecordingContext* rContext,
440                                                               GrSurfaceProxyView view,
441                                                               SkAlphaType at,
442                                                               SkSamplingOptions sampling,
443                                                               const SkTileMode tileModes[2],
444                                                               const SkMatrix& m,
445                                                               const SkRect* subset,
446                                                               const SkRect* domain) {
447     if (!view) {
448         return nullptr;
449     }
450     const GrCaps& caps = *rContext->priv().caps();
451     auto wmx = SkTileModeToWrapMode(tileModes[0]);
452     auto wmy = SkTileModeToWrapMode(tileModes[1]);
453     if (sampling.useCubic) {
454         if (subset) {
455             if (domain) {
456                 return GrBicubicEffect::MakeSubset(std::move(view),
457                                                    at,
458                                                    m,
459                                                    wmx,
460                                                    wmy,
461                                                    *subset,
462                                                    *domain,
463                                                    sampling.cubic,
464                                                    GrBicubicEffect::Direction::kXY,
465                                                    *rContext->priv().caps());
466             }
467             return GrBicubicEffect::MakeSubset(std::move(view),
468                                                at,
469                                                m,
470                                                wmx,
471                                                wmy,
472                                                *subset,
473                                                sampling.cubic,
474                                                GrBicubicEffect::Direction::kXY,
475                                                *rContext->priv().caps());
476         }
477         return GrBicubicEffect::Make(std::move(view),
478                                      at,
479                                      m,
480                                      wmx,
481                                      wmy,
482                                      sampling.cubic,
483                                      GrBicubicEffect::Direction::kXY,
484                                      *rContext->priv().caps());
485     }
486     if (sampling.isAniso()) {
487         if (!rContext->priv().caps()->anisoSupport()) {
488             // Fallback to linear
489             sampling = SkSamplingPriv::AnisoFallback(view.mipmapped() == skgpu::Mipmapped::kYes);
490         }
491     } else if (view.mipmapped() == skgpu::Mipmapped::kNo) {
492         sampling = SkSamplingOptions(sampling.filter);
493     }
494     GrSamplerState sampler;
495     if (sampling.isAniso()) {
496         sampler = GrSamplerState::Aniso(wmx, wmy, sampling.maxAniso, view.mipmapped());
497     } else {
498         sampler = GrSamplerState(wmx, wmy, sampling.filter, sampling.mipmap);
499     }
500     if (subset) {
501         if (domain) {
502             return GrTextureEffect::MakeSubset(
503                     std::move(view), at, m, sampler, *subset, *domain, caps);
504         }
505         return GrTextureEffect::MakeSubset(std::move(view), at, m, sampler, *subset, caps);
506     } else {
507         return GrTextureEffect::Make(std::move(view), at, m, sampler, caps);
508     }
509 }
510 
raster_as_fp(GrRecordingContext * rContext,const SkImage_Raster * img,SkSamplingOptions sampling,const SkTileMode tileModes[2],const SkMatrix & m,const SkRect * subset,const SkRect * domain)511 std::unique_ptr<GrFragmentProcessor> raster_as_fp(GrRecordingContext* rContext,
512                                                   const SkImage_Raster* img,
513                                                   SkSamplingOptions sampling,
514                                                   const SkTileMode tileModes[2],
515                                                   const SkMatrix& m,
516                                                   const SkRect* subset,
517                                                   const SkRect* domain) {
518     auto mm =
519             sampling.mipmap == SkMipmapMode::kNone ? skgpu::Mipmapped::kNo : skgpu::Mipmapped::kYes;
520     return make_fp_from_view(rContext,
521                              std::get<0>(AsView(rContext, img, mm)),
522                              img->alphaType(),
523                              sampling,
524                              tileModes,
525                              m,
526                              subset,
527                              domain);
528 }
529 
AsFragmentProcessor(GrRecordingContext * rContext,const SkImage * img,SkSamplingOptions sampling,const SkTileMode tileModes[2],const SkMatrix & m,const SkRect * subset,const SkRect * domain)530 std::unique_ptr<GrFragmentProcessor> AsFragmentProcessor(GrRecordingContext* rContext,
531                                                          const SkImage* img,
532                                                          SkSamplingOptions sampling,
533                                                          const SkTileMode tileModes[2],
534                                                          const SkMatrix& m,
535                                                          const SkRect* subset,
536                                                          const SkRect* domain) {
537     if (!rContext) {
538         return {};
539     }
540     if (sampling.useCubic && !GrValidCubicResampler(sampling.cubic)) {
541         return {};
542     }
543     if (sampling.mipmap != SkMipmapMode::kNone &&
544         (!rContext->priv().caps()->mipmapSupport() || img->dimensions().area() <= 1)) {
545         sampling = SkSamplingOptions(sampling.filter);
546     }
547 
548     auto ib = static_cast<const SkImage_Base*>(img);
549     if (ib->isRasterBacked()) {
550         return raster_as_fp(rContext,
551                             static_cast<const SkImage_Raster*>(ib),
552                             sampling,
553                             tileModes,
554                             m,
555                             subset,
556                             domain);
557     } else if (ib->isGaneshBacked()) {
558         auto gb = static_cast<const SkImage_GaneshBase*>(img);
559         return gb->asFragmentProcessor(rContext, sampling, tileModes, m, subset, domain);
560     } else if (ib->isLazyGenerated()) {
561         // TODO: If the CPU data is extracted as planes return a FP that reconstructs the image from
562         // the planes.
563         auto mm = sampling.mipmap == SkMipmapMode::kNone ? skgpu::Mipmapped::kNo : skgpu::Mipmapped::kYes;
564         return MakeFragmentProcessorFromView(rContext,
565                                              std::get<0>(AsView(rContext, img, mm)),
566                                              img->alphaType(),
567                                              sampling,
568                                              tileModes,
569                                              m,
570                                              subset,
571                                              domain);
572     }
573 
574     SkDEBUGFAIL("Unsupported image type to make a FragmentProcessor");
575     return {};
576 }
577 
MakeFragmentProcessorFromView(GrRecordingContext * rContext,GrSurfaceProxyView view,SkAlphaType at,SkSamplingOptions sampling,const SkTileMode tileModes[2],const SkMatrix & m,const SkRect * subset,const SkRect * domain)578 std::unique_ptr<GrFragmentProcessor> MakeFragmentProcessorFromView(
579         GrRecordingContext* rContext,
580         GrSurfaceProxyView view,
581         SkAlphaType at,
582         SkSamplingOptions sampling,
583         const SkTileMode tileModes[2],
584         const SkMatrix& m,
585         const SkRect* subset,
586         const SkRect* domain) {
587     if (!view) {
588         return nullptr;
589     }
590     const GrCaps& caps = *rContext->priv().caps();
591     auto wmx = SkTileModeToWrapMode(tileModes[0]);
592     auto wmy = SkTileModeToWrapMode(tileModes[1]);
593     if (sampling.useCubic) {
594         if (subset) {
595             if (domain) {
596                 return GrBicubicEffect::MakeSubset(std::move(view),
597                                                    at,
598                                                    m,
599                                                    wmx,
600                                                    wmy,
601                                                    *subset,
602                                                    *domain,
603                                                    sampling.cubic,
604                                                    GrBicubicEffect::Direction::kXY,
605                                                    *rContext->priv().caps());
606             }
607             return GrBicubicEffect::MakeSubset(std::move(view),
608                                                at,
609                                                m,
610                                                wmx,
611                                                wmy,
612                                                *subset,
613                                                sampling.cubic,
614                                                GrBicubicEffect::Direction::kXY,
615                                                *rContext->priv().caps());
616         }
617         return GrBicubicEffect::Make(std::move(view),
618                                      at,
619                                      m,
620                                      wmx,
621                                      wmy,
622                                      sampling.cubic,
623                                      GrBicubicEffect::Direction::kXY,
624                                      *rContext->priv().caps());
625     }
626     if (sampling.isAniso()) {
627         if (!rContext->priv().caps()->anisoSupport()) {
628             // Fallback to linear
629             sampling = SkSamplingPriv::AnisoFallback(view.mipmapped() == skgpu::Mipmapped::kYes);
630         }
631     } else if (view.mipmapped() == skgpu::Mipmapped::kNo) {
632         sampling = SkSamplingOptions(sampling.filter);
633     }
634     GrSamplerState sampler;
635     if (sampling.isAniso()) {
636         sampler = GrSamplerState::Aniso(wmx, wmy, sampling.maxAniso, view.mipmapped());
637     } else {
638         sampler = GrSamplerState(wmx, wmy, sampling.filter, sampling.mipmap);
639     }
640     if (subset) {
641         if (domain) {
642             return GrTextureEffect::MakeSubset(std::move(view),
643                                                at,
644                                                m,
645                                                sampler,
646                                                *subset,
647                                                *domain,
648                                                caps);
649         }
650         return GrTextureEffect::MakeSubset(std::move(view),
651                                            at,
652                                            m,
653                                            sampler,
654                                            *subset,
655                                            caps);
656     } else {
657         return GrTextureEffect::Make(std::move(view), at, m, sampler, caps);
658     }
659 }
660 
FindOrMakeCachedMipmappedView(GrRecordingContext * rContext,GrSurfaceProxyView view,uint32_t imageUniqueID)661 GrSurfaceProxyView FindOrMakeCachedMipmappedView(GrRecordingContext* rContext,
662                                                  GrSurfaceProxyView view,
663                                                  uint32_t imageUniqueID) {
664     SkASSERT(rContext);
665     SkASSERT(imageUniqueID != SK_InvalidUniqueID);
666 
667     if (!view || view.proxy()->asTextureProxy()->mipmapped() == skgpu::Mipmapped::kYes) {
668         return view;
669     }
670     GrProxyProvider* proxyProvider = rContext->priv().proxyProvider();
671 
672     skgpu::UniqueKey baseKey;
673     GrMakeKeyFromImageID(&baseKey, imageUniqueID, SkIRect::MakeSize(view.dimensions()));
674     SkASSERT(baseKey.isValid());
675     skgpu::UniqueKey mipmappedKey;
676     static const skgpu::UniqueKey::Domain kMipmappedDomain = skgpu::UniqueKey::GenerateDomain();
677     {  // No extra values beyond the domain are required. Must name the var to please
678        // clang-tidy.
679         skgpu::UniqueKey::Builder b(&mipmappedKey, baseKey, kMipmappedDomain, 0);
680     }
681     SkASSERT(mipmappedKey.isValid());
682     if (sk_sp<GrTextureProxy> cachedMippedView =
683                 proxyProvider->findOrCreateProxyByUniqueKey(mipmappedKey)) {
684         return {std::move(cachedMippedView), view.origin(), view.swizzle()};
685     }
686 
687     auto copy = GrCopyBaseMipMapToView(rContext, view);
688     if (!copy) {
689         return view;
690     }
691     // TODO: If we move listeners up from SkImage_Lazy to SkImage_Base then add one here.
692     proxyProvider->assignUniqueKeyToProxy(mipmappedKey, copy.asTextureProxy());
693     return copy;
694 }
695 
696 using DataType = SkYUVAPixmapInfo::DataType;
697 
SupportedTextureFormats(const GrImageContext & context)698 SkYUVAPixmapInfo::SupportedDataTypes SupportedTextureFormats(const GrImageContext& context) {
699     SkYUVAPixmapInfo::SupportedDataTypes dataTypes;
700     const auto isValid = [&context](DataType dt, int n) {
701         return context.defaultBackendFormat(SkYUVAPixmapInfo::DefaultColorTypeForDataType(dt, n),
702                                             GrRenderable::kNo).isValid();
703     };
704      for (int n = 1; n <= 4; ++n) {
705         if (isValid(DataType::kUnorm8, n)) {
706             dataTypes.enableDataType(DataType::kUnorm8, n);
707         }
708         if (isValid(DataType::kUnorm16, n)) {
709             dataTypes.enableDataType(DataType::kUnorm16, n);
710         }
711         if (isValid(DataType::kFloat16, n)) {
712             dataTypes.enableDataType(DataType::kFloat16, n);
713         }
714         if (isValid(DataType::kUnorm10_Unorm2, n)) {
715             dataTypes.enableDataType(DataType::kUnorm10_Unorm2, n);
716         }
717     }
718      return dataTypes;
719 }
720 
721 }  // namespace skgpu::ganesh
722 
723 namespace skif {
724 
725 namespace {
726 
727 class GaneshBackend :
728         public Backend,
729 #if defined(SK_USE_LEGACY_BLUR_GANESH)
730         private SkBlurEngine::Algorithm,
731 #else
732         private SkShaderBlurAlgorithm,
733 #endif
734         private SkBlurEngine {
735 public:
736 
GaneshBackend(sk_sp<GrRecordingContext> context,GrSurfaceOrigin origin,const SkSurfaceProps & surfaceProps,SkColorType colorType)737     GaneshBackend(sk_sp<GrRecordingContext> context,
738                   GrSurfaceOrigin origin,
739                   const SkSurfaceProps& surfaceProps,
740                   SkColorType colorType)
741             : Backend(SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize),
742                       surfaceProps, colorType)
743             , fContext(std::move(context))
744             , fOrigin(origin) {}
745 
746     // Backend
makeDevice(SkISize size,sk_sp<SkColorSpace> colorSpace,const SkSurfaceProps * props) const747     sk_sp<SkDevice> makeDevice(SkISize size,
748                                sk_sp<SkColorSpace> colorSpace,
749                                const SkSurfaceProps* props) const override {
750         SkImageInfo imageInfo = SkImageInfo::Make(size,
751                                                   this->colorType(),
752                                                   kPremul_SkAlphaType,
753                                                   std::move(colorSpace));
754 
755         return fContext->priv().createDevice(skgpu::Budgeted::kYes,
756                                              imageInfo,
757                                              SkBackingFit::kApprox,
758                                              1,
759                                              skgpu::Mipmapped::kNo,
760                                              GrProtected::kNo,
761                                              fOrigin,
762                                              props ? *props : this->surfaceProps(),
763                                              skgpu::ganesh::Device::InitContents::kUninit);
764     }
765 
makeImage(const SkIRect & subset,sk_sp<SkImage> image) const766     sk_sp<SkSpecialImage> makeImage(const SkIRect& subset, sk_sp<SkImage> image) const override {
767         return SkSpecialImages::MakeFromTextureImage(
768                 fContext.get(), subset, image, this->surfaceProps());
769     }
770 
getCachedBitmap(const SkBitmap & data) const771     sk_sp<SkImage> getCachedBitmap(const SkBitmap& data) const override {
772         // This uses the thread safe cache (instead of GrMakeCachedBitmapProxyView) so that image
773         // filters can be evaluated on other threads with DDLs.
774         auto threadSafeCache = fContext->priv().threadSafeCache();
775 
776         skgpu::UniqueKey key;
777         SkIRect subset = SkIRect::MakePtSize(data.pixelRefOrigin(), data.dimensions());
778         GrMakeKeyFromImageID(&key, data.getGenerationID(), subset);
779 
780         auto view = threadSafeCache->find(key);
781         if (!view) {
782             view = std::get<0>(GrMakeUncachedBitmapProxyView(fContext.get(), data));
783             if (!view) {
784                 return nullptr;
785             }
786             threadSafeCache->add(key, view);
787         }
788 
789         return sk_make_sp<SkImage_Ganesh>(fContext,
790                                           data.getGenerationID(),
791                                           std::move(view),
792                                           data.info().colorInfo());
793     }
794 
getBlurEngine() const795     const SkBlurEngine* getBlurEngine() const override { return this; }
796 
797     // SkBlurEngine
findAlgorithm(SkSize sigma,SkColorType colorType) const798     const SkBlurEngine::Algorithm* findAlgorithm(SkSize sigma,
799                                                  SkColorType colorType) const override {
800         // GrBlurUtils supports all tile modes and color types
801         return this;
802     }
803 
804 #if defined(SK_USE_LEGACY_BLUR_GANESH)
805     // NOTE: When SK_USE_LEGACY_BLUR_GANESH is defined, `useLegacyFilterResultBlur()` returns true,
806     // so FilterResult::blur() will resolve all tiling in the original image space before calling
807     // into this function that routes to GrBlurUtils::GaussianBlur to perform the rescaling and
808     // blurring. It is possible ot restore original GrBlurUtils performance by just having
809     // `useLegacyFilterResultBlur()` return false but still reporting a max sigma of infinity and
810     // advertising support for all tile modes.
811     //
812     // Since all clients are currently rebased on the intermediate "legacy" blur approach, the ideal
813     // step would be to just migrate them to the SkShaderBlurAlgorithm variant instead of first
814     // going back to pure GrBlurUtils. But if needed for cherry-picking to old releases, the
815     // original GrBlurUtils behavior can be achieved quikly.
816 
817     // SkBlurEngine::Algorithm
maxSigma() const818     float maxSigma() const override {
819         // GrBlurUtils handles resizing at the moment
820         return SK_ScalarInfinity;
821     }
822 
supportsOnlyDecalTiling() const823     bool supportsOnlyDecalTiling() const override { return false; }
824 
blur(SkSize sigma,sk_sp<SkSpecialImage> input,const SkIRect & srcRect,SkTileMode tileMode,const SkIRect & dstRect) const825     sk_sp<SkSpecialImage> blur(SkSize sigma,
826                                sk_sp<SkSpecialImage> input,
827                                const SkIRect& srcRect,
828                                SkTileMode tileMode,
829                                const SkIRect& dstRect) const override {
830         GrSurfaceProxyView inputView = SkSpecialImages::AsView(fContext.get(), input);
831         if (!inputView.proxy()) {
832             return nullptr;
833         }
834         SkASSERT(inputView.asTextureProxy());
835 
836         // Update srcRect and dstRect to be relative to the underlying texture proxy of 'input'.
837         auto proxyOffset = input->subset().topLeft() - srcRect.topLeft();
838         auto sdc = GrBlurUtils::GaussianBlur(
839                 fContext.get(),
840                 std::move(inputView),
841                 SkColorTypeToGrColorType(input->colorType()),
842                 input->alphaType(),
843                 sk_ref_sp(input->getColorSpace()),
844                 dstRect.makeOffset(proxyOffset),
845                 srcRect.makeOffset(proxyOffset),
846                 sigma.width(),
847                 sigma.height(),
848                 tileMode);
849         if (!sdc) {
850             return nullptr;
851         }
852 
853         return SkSpecialImages::MakeDeferredFromGpu(fContext.get(),
854                                                     SkIRect::MakeSize(dstRect.size()),
855                                                     kNeedNewImageUniqueID_SpecialImage,
856                                                     sdc->readSurfaceView(),
857                                                     sdc->colorInfo(),
858                                                     this->surfaceProps());
859     }
860 #else
useLegacyFilterResultBlur() const861     bool useLegacyFilterResultBlur() const override { return false; }
862 
863     // SkShaderBlurAlgorithm
makeDevice(const SkImageInfo & imageInfo) const864     sk_sp<SkDevice> makeDevice(const SkImageInfo& imageInfo) const override {
865         return fContext->priv().createDevice(skgpu::Budgeted::kYes,
866                                              imageInfo,
867                                              SkBackingFit::kApprox,
868                                              1,
869                                              skgpu::Mipmapped::kNo,
870                                              GrProtected::kNo,
871                                              fOrigin,
872                                              this->surfaceProps(),
873                                              skgpu::ganesh::Device::InitContents::kUninit);
874     }
875 
876 #endif
877 
878 private:
879     sk_sp<GrRecordingContext> fContext;
880     GrSurfaceOrigin fOrigin;
881 };
882 
883 } // anonymous namespace
884 
MakeGaneshBackend(sk_sp<GrRecordingContext> context,GrSurfaceOrigin origin,const SkSurfaceProps & surfaceProps,SkColorType colorType)885 sk_sp<Backend> MakeGaneshBackend(sk_sp<GrRecordingContext> context,
886                                  GrSurfaceOrigin origin,
887                                  const SkSurfaceProps& surfaceProps,
888                                  SkColorType colorType) {
889     SkASSERT(context);
890     return sk_make_sp<GaneshBackend>(std::move(context), origin, surfaceProps, colorType);
891 }
892 
893 }  // namespace skif
894