| 1 |
/* |
| 2 |
* Copyright 2017 Google Inc. |
| 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 |
#ifndef SkGr_DEFINED |
| 9 |
#define SkGr_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkBlendMode.h" |
| 12 |
#include "include/core/SkColor.h" |
| 13 |
#include "include/core/SkRefCnt.h" |
| 14 |
#include "include/core/SkSamplingOptions.h" |
| 15 |
#include "include/core/SkTileMode.h" |
| 16 |
#include "include/core/SkTypes.h" |
| 17 |
#include "include/gpu/GpuTypes.h" |
| 18 |
#include "src/core/SkColorData.h" |
| 19 |
#include "src/core/SkColorPriv.h" |
| 20 |
#include "src/gpu/Blend.h" |
| 21 |
#include "src/gpu/SkBackingFit.h" |
| 22 |
#include "src/gpu/ganesh/GrColor.h" |
| 23 |
#include "src/gpu/ganesh/GrSamplerState.h" |
| 24 |
|
| 25 |
#include <cstdint> |
| 26 |
#include <memory> |
| 27 |
#include <string_view> |
| 28 |
#include <tuple> |
| 29 |
|
| 30 |
class GrMippedBitmap; |
| 31 |
class GrColorInfo; |
| 32 |
class GrFragmentProcessor; |
| 33 |
class GrPaint; |
| 34 |
class GrRecordingContext; |
| 35 |
class GrSurfaceProxy; |
| 36 |
class GrSurfaceProxyView; |
| 37 |
class SkBlender; |
| 38 |
class SkIDChangeListener; |
| 39 |
class SkMatrix; |
| 40 |
class SkPaint; |
| 41 |
enum class GrColorType; |
| 42 |
enum GrSurfaceOrigin : int; |
| 43 |
struct SkIRect; |
| 44 |
|
| 45 |
namespace skgpu { class UniqueKey; } |
| 46 |
namespace skgpu::ganesh { |
| 47 |
class SurfaceDrawContext; |
| 48 |
} |
| 49 |
|
| 50 |
//////////////////////////////////////////////////////////////////////////////// |
| 51 |
// Color type conversions |
| 52 |
|
| 53 |
static inline GrColor SkColorToPremulGrColor(SkColor c) { |
| 54 |
SkPMColor pm = SkPreMultiplyColor(c); |
| 55 |
unsigned r = SkGetPackedR32(pm); |
| 56 |
unsigned g = SkGetPackedG32(pm); |
| 57 |
unsigned b = SkGetPackedB32(pm); |
| 58 |
unsigned a = SkGetPackedA32(pm); |
| 59 |
return GrColorPackRGBA(r, g, b, a); |
| 60 |
} |
| 61 |
|
| 62 |
static inline GrColor SkColorToUnpremulGrColor(SkColor c) { |
| 63 |
unsigned r = SkColorGetR(c); |
| 64 |
unsigned g = SkColorGetG(c); |
| 65 |
unsigned b = SkColorGetB(c); |
| 66 |
unsigned a = SkColorGetA(c); |
| 67 |
return GrColorPackRGBA(r, g, b, a); |
| 68 |
} |
| 69 |
|
| 70 |
/** Similar, but using SkPMColor4f. */ |
| 71 |
SkPMColor4f SkColorToPMColor4f(SkColor, const GrColorInfo&); |
| 72 |
|
| 73 |
/** Converts an SkColor4f to the destination color space. */ |
| 74 |
SkColor4f SkColor4fPrepForDst(SkColor4f, const GrColorInfo&); |
| 75 |
|
| 76 |
//////////////////////////////////////////////////////////////////////////////// |
| 77 |
// SkTileMode conversion |
| 78 |
|
| 79 |
static constexpr GrSamplerState::WrapMode SkTileModeToWrapMode(SkTileMode tileMode) { |
| 80 |
switch (tileMode) { |
| 81 |
case SkTileMode::kClamp: return GrSamplerState::WrapMode::kClamp; |
| 82 |
case SkTileMode::kDecal: return GrSamplerState::WrapMode::kClampToBorder; |
| 83 |
case SkTileMode::kMirror: return GrSamplerState::WrapMode::kMirrorRepeat; |
| 84 |
case SkTileMode::kRepeat: return GrSamplerState::WrapMode::kRepeat; |
| 85 |
} |
| 86 |
SkUNREACHABLE; |
| 87 |
} |
| 88 |
|
| 89 |
//////////////////////////////////////////////////////////////////////////////// |
| 90 |
// Paint conversion |
| 91 |
|
| 92 |
/** Converts an SkPaint to a GrPaint for a given GrRecordingContext. The matrix is required in order |
| 93 |
to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */ |
| 94 |
bool SkPaintToGrPaint(skgpu::ganesh::SurfaceDrawContext*, |
| 95 |
const SkPaint& skPaint, |
| 96 |
const SkMatrix& ctm, |
| 97 |
GrPaint* grPaint); |
| 98 |
|
| 99 |
/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor, if not null. |
| 100 |
If null then it is assumed that the geometry processor is implementing a shader replacement. |
| 101 |
The processor should expect an unpremul input color and produce a premultiplied output color. */ |
| 102 |
bool SkPaintToGrPaintReplaceShader(skgpu::ganesh::SurfaceDrawContext*, |
| 103 |
const SkPaint& skPaint, |
| 104 |
const SkMatrix& ctm, |
| 105 |
std::unique_ptr<GrFragmentProcessor> shaderFP, |
| 106 |
GrPaint* grPaint); |
| 107 |
|
| 108 |
/** Blends the SkPaint's shader (or color if no shader) with the color which specified via a |
| 109 |
GrOp's GrPrimitiveProcesssor. */ |
| 110 |
bool SkPaintToGrPaintWithBlend(skgpu::ganesh::SurfaceDrawContext* context, |
| 111 |
const SkPaint& skPaint, |
| 112 |
const SkMatrix& ctm, |
| 113 |
SkBlender* primColorBlender, |
| 114 |
GrPaint* grPaint); |
| 115 |
|
| 116 |
//////////////////////////////////////////////////////////////////////////////// |
| 117 |
// Misc Sk to Gr type conversions |
| 118 |
|
| 119 |
static_assert((int)skgpu::BlendCoeff::kZero == (int)SkBlendModeCoeff::kZero); |
| 120 |
static_assert((int)skgpu::BlendCoeff::kOne == (int)SkBlendModeCoeff::kOne); |
| 121 |
static_assert((int)skgpu::BlendCoeff::kSC == (int)SkBlendModeCoeff::kSC); |
| 122 |
static_assert((int)skgpu::BlendCoeff::kISC == (int)SkBlendModeCoeff::kISC); |
| 123 |
static_assert((int)skgpu::BlendCoeff::kDC == (int)SkBlendModeCoeff::kDC); |
| 124 |
static_assert((int)skgpu::BlendCoeff::kIDC == (int)SkBlendModeCoeff::kIDC); |
| 125 |
static_assert((int)skgpu::BlendCoeff::kSA == (int)SkBlendModeCoeff::kSA); |
| 126 |
static_assert((int)skgpu::BlendCoeff::kISA == (int)SkBlendModeCoeff::kISA); |
| 127 |
static_assert((int)skgpu::BlendCoeff::kDA == (int)SkBlendModeCoeff::kDA); |
| 128 |
static_assert((int)skgpu::BlendCoeff::kIDA == (int)SkBlendModeCoeff::kIDA); |
| 129 |
static_assert((int)SkBlendModeCoeff::kCoeffCount == 10); |
| 130 |
|
| 131 |
//////////////////////////////////////////////////////////////////////////////// |
| 132 |
// Texture management |
| 133 |
|
| 134 |
/** |
| 135 |
* Policies for how to create textures for SkImages. |
| 136 |
*/ |
| 137 |
enum class GrImageTexGenPolicy : int { |
| 138 |
// Choose the cheapest way to generate the texture. Use GrResourceCache if appropriate. |
| 139 |
kDraw, |
| 140 |
// Always make a new texture that is uncached and unbudgeted. |
| 141 |
kNew_Uncached_Unbudgeted, |
| 142 |
// Always make a new texture that is uncached and budgeted. |
| 143 |
kNew_Uncached_Budgeted |
| 144 |
}; |
| 145 |
|
| 146 |
/** |
| 147 |
* Creates a new texture with mipmap levels and copies the baseProxy into the base layer. |
| 148 |
*/ |
| 149 |
sk_sp<GrSurfaceProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext*, |
| 150 |
sk_sp<GrSurfaceProxy> baseProxy, |
| 151 |
GrSurfaceOrigin origin, |
| 152 |
std::string_view label, |
| 153 |
skgpu::Budgeted = skgpu::Budgeted::kYes); |
| 154 |
/** |
| 155 |
* Same as GrCopyBaseMipMapToTextureProxy but takes the src as a view and returns a view with same |
| 156 |
* origin and swizzle as the src view. |
| 157 |
*/ |
| 158 |
GrSurfaceProxyView GrCopyBaseMipMapToView(GrRecordingContext*, |
| 159 |
GrSurfaceProxyView, |
| 160 |
skgpu::Budgeted = skgpu::Budgeted::kYes); |
| 161 |
|
| 162 |
/* |
| 163 |
* Create a texture proxy from the provided bitmap and add it to the texture cache using the key |
| 164 |
* also extracted from the bitmap. If skgpu::Mipmapped is kYes a non-mipmapped result may be |
| 165 |
* returned if mipmapping isn't supported or for a 1x1 bitmap. If skgpu::Mipmapped is kNo it |
| 166 |
* indicates mipmaps aren't required but a previously created mipmapped texture may still be |
| 167 |
* returned. A color type is returned as color type conversion may be performed if there isn't a |
| 168 |
* texture format equivalent of the bitmap's color type. |
| 169 |
*/ |
| 170 |
std::tuple<GrSurfaceProxyView, GrColorType> GrMakeCachedBitmapProxyView( |
| 171 |
GrRecordingContext*, |
| 172 |
const GrMippedBitmap&, |
| 173 |
std::string_view label, |
| 174 |
skgpu::Mipmapped = skgpu::Mipmapped::kNo); |
| 175 |
|
| 176 |
/** |
| 177 |
* Like above but always uploads the bitmap and never inserts into the cache. Unlike above, the |
| 178 |
* texture may be approx or scratch and budgeted or not. |
| 179 |
*/ |
| 180 |
std::tuple<GrSurfaceProxyView, GrColorType> GrMakeUncachedBitmapProxyView( |
| 181 |
GrRecordingContext*, |
| 182 |
const GrMippedBitmap&, |
| 183 |
skgpu::Mipmapped = skgpu::Mipmapped::kNo, |
| 184 |
SkBackingFit = SkBackingFit::kExact, |
| 185 |
skgpu::Budgeted = skgpu::Budgeted::kYes); |
| 186 |
|
| 187 |
/** |
| 188 |
* Our key includes the offset, width, and height so that bitmaps created by extractSubset() |
| 189 |
* are unique. |
| 190 |
* |
| 191 |
* The imageID is in the shared namespace (see SkNextID::ImageID()) |
| 192 |
* - SkBitmap/SkPixelRef |
| 193 |
* - SkImage |
| 194 |
* - SkImageGenerator |
| 195 |
*/ |
| 196 |
void GrMakeKeyFromImageID(skgpu::UniqueKey* key, uint32_t imageID, const SkIRect& imageBounds); |
| 197 |
|
| 198 |
/** |
| 199 |
* Makes a SkIDChangeListener from a skgpu::UniqueKey. The key will be invalidated in the resource |
| 200 |
* cache if the ID becomes invalid. This also modifies the key so that it will cause the listener |
| 201 |
* to be deregistered if the key is destroyed (to prevent unbounded listener growth when resources |
| 202 |
* are purged before listeners trigger). |
| 203 |
*/ |
| 204 |
sk_sp<SkIDChangeListener> GrMakeUniqueKeyInvalidationListener(skgpu::UniqueKey*, |
| 205 |
uint32_t contextID); |
| 206 |
|
| 207 |
static inline bool GrValidCubicResampler(SkCubicResampler cubic) { |
| 208 |
return cubic.B >= 0 && cubic.C >= 0; |
| 209 |
} |
| 210 |
|
| 211 |
#endif |