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