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 #ifndef GrImageUtils_DEFINED 8 #define GrImageUtils_DEFINED 9 10 #include "include/core/SkRefCnt.h" 11 12 #include "include/core/SkSamplingOptions.h" 13 #include "include/core/SkYUVAPixmaps.h" 14 #include "src/gpu/ganesh/GrFragmentProcessor.h" // IWYU pragma: keep 15 #include "src/gpu/ganesh/GrSurfaceProxyView.h" // IWYU pragma: keep 16 #include "src/gpu/ganesh/SkGr.h" 17 18 #include <cstdint> 19 #include <memory> 20 #include <string_view> 21 #include <tuple> 22 23 class GrCaps; 24 class GrImageContext; 25 class GrRecordingContext; 26 class SkImage; 27 class SkImage_Lazy; 28 class SkImage_Raster; 29 class SkMatrix; 30 class SkSurfaceProps; 31 enum GrSurfaceOrigin : int; 32 enum SkAlphaType : int; 33 enum SkColorType : int; 34 enum class GrColorType; 35 enum class SkTileMode; 36 namespace skgpu { enum class Mipmapped : bool; } 37 struct SkRect; 38 39 namespace skgpu::ganesh { 40 // Returns a GrSurfaceProxyView representation of the image, if possible. This also returns 41 // a color type. This may be different than the image's color type when the image is not 42 // texture-backed and the capabilities of the GPU require a data type conversion to put 43 // the data in a texture. 44 std::tuple<GrSurfaceProxyView, GrColorType> AsView( 45 GrRecordingContext*, 46 const SkImage*, 47 skgpu::Mipmapped, 48 GrImageTexGenPolicy = GrImageTexGenPolicy::kDraw); 49 50 inline std::tuple<GrSurfaceProxyView, GrColorType> AsView( 51 GrRecordingContext* ctx, 52 const sk_sp<const SkImage>& img, 53 skgpu::Mipmapped mm, 54 GrImageTexGenPolicy policy = GrImageTexGenPolicy::kDraw) { 55 return AsView(ctx, img.get(), mm, policy); 56 } 57 58 std::tuple<GrSurfaceProxyView, GrColorType> RasterAsView( 59 GrRecordingContext*, 60 const SkImage_Raster*, 61 skgpu::Mipmapped, 62 GrImageTexGenPolicy = GrImageTexGenPolicy::kDraw); 63 64 // Utility for making a copy of an existing view when the GrImageTexGenPolicy is not kDraw. 65 GrSurfaceProxyView CopyView(GrRecordingContext*, 66 GrSurfaceProxyView src, 67 skgpu::Mipmapped, 68 GrImageTexGenPolicy, 69 std::string_view label); 70 71 // Returns the texture proxy. CachingHint refers to whether the generator's output should be 72 // cached in CPU memory. We will always cache the generated texture on success. 73 GrSurfaceProxyView LockTextureProxyView(GrRecordingContext*, 74 const SkImage_Lazy*, 75 GrImageTexGenPolicy, 76 skgpu::Mipmapped); 77 78 // Returns the GrColorType to use with the GrTextureProxy returned from lockTextureProxy. This 79 // may be different from the color type on the image in the case where we need up upload CPU 80 // data to a texture but the GPU doesn't support the format of CPU data. In this case we convert 81 // the data to RGBA_8888 unorm on the CPU then upload that. 82 GrColorType ColorTypeOfLockTextureProxy(const GrCaps*, SkColorType); 83 84 /** 85 * Returns a GrFragmentProcessor that can be used with the passed GrRecordingContext to 86 * draw the image. SkSamplingOptions indicates the filter and SkTileMode[] indicates the x and 87 * y tile modes. The passed matrix is applied to the coordinates before sampling the image. 88 * Optional 'subset' indicates whether the tile modes should be applied to a subset of the image 89 * Optional 'domain' is a bound on the coordinates of the image that will be required and can be 90 * used to optimize the shader if 'subset' is also specified. 91 */ 92 std::unique_ptr<GrFragmentProcessor> AsFragmentProcessor(GrRecordingContext*, 93 const SkImage*, 94 SkSamplingOptions, 95 const SkTileMode[2], 96 const SkMatrix&, 97 const SkRect* subset = nullptr, 98 const SkRect* domain = nullptr); 99 100 inline std::unique_ptr<GrFragmentProcessor> AsFragmentProcessor(GrRecordingContext* ctx, 101 const sk_sp<const SkImage>& img, 102 SkSamplingOptions opt, 103 const SkTileMode tm[2], 104 const SkMatrix& m, 105 const SkRect* subset = nullptr, 106 const SkRect* domain = nullptr) { 107 return AsFragmentProcessor(ctx, img.get(), opt, tm, m, subset, domain); 108 } 109 110 std::unique_ptr<GrFragmentProcessor> MakeFragmentProcessorFromView(GrRecordingContext*, 111 GrSurfaceProxyView, 112 SkAlphaType, 113 SkSamplingOptions, 114 const SkTileMode[2], 115 const SkMatrix&, 116 const SkRect* subset, 117 const SkRect* domain); 118 119 /** 120 * Returns input view if it is already mipmapped. Otherwise, attempts to make a mipmapped view 121 * with the same contents. If the mipmapped copy is successfully created it will be cached 122 * using the image unique ID. A subsequent call with the same unique ID will return the cached 123 * view if it has not been purged. The view is cached with a key domain specific to this 124 * function. 125 */ 126 GrSurfaceProxyView FindOrMakeCachedMipmappedView(GrRecordingContext*, 127 GrSurfaceProxyView, 128 uint32_t imageUniqueID); 129 130 /** Init based on texture formats supported by the context. */ 131 SkYUVAPixmapInfo::SupportedDataTypes SupportedTextureFormats(const GrImageContext&); 132 133 } // namespace skgpu::ganesh 134 135 namespace skif { 136 137 class Backend; 138 139 sk_sp<Backend> MakeGaneshBackend(sk_sp<GrRecordingContext> context, 140 GrSurfaceOrigin origin, 141 const SkSurfaceProps& surfaceProps, 142 SkColorType colorType); 143 144 } // namespace skif 145 146 #endif 147