reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 1 | /* |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 2 | * Copyright 2017 Google Inc. |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 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 | |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 8 | #ifndef SkGr_DEFINED |
| 9 | #define SkGr_DEFINED |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkCanvas.h" |
| 12 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkImageInfo.h" |
Mike Reed | 52130b0 | 2020-12-28 15:33:13 -0500 | [diff] [blame] | 14 | #include "include/core/SkSamplingOptions.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/gpu/GrTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/private/SkColorData.h" |
| 17 | #include "src/core/SkBlendModePriv.h" |
Brian Salomon | 096b091 | 2019-08-14 16:56:13 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrBlend.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrCaps.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrColor.h" |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrSamplerState.h" |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 22 | |
| 23 | class GrCaps; |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 24 | class GrColorInfo; |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 25 | class GrColorSpaceXform; |
Adlai Holler | 872a32c | 2020-07-10 14:33:22 -0400 | [diff] [blame] | 26 | class GrDirectContext; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 27 | class GrFragmentProcessor; |
| 28 | class GrPaint; |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 29 | class GrRecordingContext; |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 30 | class GrResourceProvider; |
Robert Phillips | e14d305 | 2017-02-15 13:18:21 -0500 | [diff] [blame] | 31 | class GrTextureProxy; |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 32 | class GrUniqueKey; |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 33 | class SkBitmap; |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 34 | class SkData; |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 35 | class SkMatrix; |
| 36 | class SkMatrixProvider; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 37 | class SkPaint; |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 38 | class SkPixelRef; |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 39 | class SkPixmap; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 40 | struct SkIRect; |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 41 | |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 42 | //////////////////////////////////////////////////////////////////////////////// |
| 43 | // Color type conversions |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 44 | |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 45 | static inline GrColor SkColorToPremulGrColor(SkColor c) { |
| 46 | SkPMColor pm = SkPreMultiplyColor(c); |
| 47 | unsigned r = SkGetPackedR32(pm); |
| 48 | unsigned g = SkGetPackedG32(pm); |
| 49 | unsigned b = SkGetPackedB32(pm); |
| 50 | unsigned a = SkGetPackedA32(pm); |
| 51 | return GrColorPackRGBA(r, g, b, a); |
| 52 | } |
| 53 | |
| 54 | static inline GrColor SkColorToUnpremulGrColor(SkColor c) { |
| 55 | unsigned r = SkColorGetR(c); |
| 56 | unsigned g = SkColorGetG(c); |
| 57 | unsigned b = SkColorGetB(c); |
| 58 | unsigned a = SkColorGetA(c); |
| 59 | return GrColorPackRGBA(r, g, b, a); |
| 60 | } |
| 61 | |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 62 | /** Similar, but using SkPMColor4f. */ |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 63 | SkPMColor4f SkColorToPMColor4f(SkColor, const GrColorInfo&); |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 64 | |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 65 | /** Converts an SkColor4f to the destination color space. */ |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 66 | SkColor4f SkColor4fPrepForDst(SkColor4f, const GrColorInfo&); |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 67 | |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 68 | //////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | 694ec49 | 2020-04-14 13:39:31 -0400 | [diff] [blame] | 69 | // SkTileMode conversion |
| 70 | |
| 71 | static constexpr GrSamplerState::WrapMode SkTileModeToWrapMode(SkTileMode tileMode) { |
| 72 | switch (tileMode) { |
| 73 | case SkTileMode::kClamp: return GrSamplerState::WrapMode::kClamp; |
| 74 | case SkTileMode::kDecal: return GrSamplerState::WrapMode::kClampToBorder; |
| 75 | case SkTileMode::kMirror: return GrSamplerState::WrapMode::kMirrorRepeat; |
| 76 | case SkTileMode::kRepeat: return GrSamplerState::WrapMode::kRepeat; |
| 77 | } |
| 78 | SkUNREACHABLE; |
| 79 | } |
| 80 | |
| 81 | //////////////////////////////////////////////////////////////////////////////// |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 82 | // Paint conversion |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 83 | |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 84 | /** Converts an SkPaint to a GrPaint for a given GrRecordingContext. The matrix is required in order |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 85 | to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */ |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 86 | bool SkPaintToGrPaint(GrRecordingContext*, |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 87 | const GrColorInfo& dstColorInfo, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 88 | const SkPaint& skPaint, |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 89 | const SkMatrixProvider& matrixProvider, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 90 | GrPaint* grPaint); |
| 91 | |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 92 | /** Same as above but ignores the SkShader (if any) on skPaint. */ |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 93 | bool SkPaintToGrPaintNoShader(GrRecordingContext*, |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 94 | const GrColorInfo& dstColorInfo, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 95 | const SkPaint& skPaint, |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 96 | const SkMatrixProvider& matrixProvider, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 97 | GrPaint* grPaint); |
| 98 | |
| 99 | /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 100 | should expect an unpremul input color and produce a premultiplied output color. There is |
| 101 | no primitive color. */ |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 102 | bool SkPaintToGrPaintReplaceShader(GrRecordingContext*, |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 103 | const GrColorInfo& dstColorInfo, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 104 | const SkPaint& skPaint, |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 105 | const SkMatrixProvider& matrixProvider, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 106 | std::unique_ptr<GrFragmentProcessor> shaderFP, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 107 | GrPaint* grPaint); |
| 108 | |
| 109 | /** Blends the SkPaint's shader (or color if no shader) with the color which specified via a |
Mike Reed | 185ba21 | 2017-04-28 12:31:05 -0400 | [diff] [blame] | 110 | GrOp's GrPrimitiveProcesssor. */ |
John Stiles | f743d4e | 2020-07-23 11:35:08 -0400 | [diff] [blame] | 111 | bool SkPaintToGrPaintWithBlend(GrRecordingContext*, |
| 112 | const GrColorInfo& dstColorInfo, |
| 113 | const SkPaint& skPaint, |
| 114 | const SkMatrixProvider& matrixProvider, |
| 115 | SkBlendMode primColorMode, |
| 116 | GrPaint* grPaint); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 117 | |
John Stiles | f2f9b0d | 2020-08-25 13:33:45 -0400 | [diff] [blame] | 118 | /** Blends the passed-in shader with a per-primitive color which must be setup as a vertex attribute |
| 119 | using the specified SkBlendMode. */ |
| 120 | bool SkPaintToGrPaintWithBlendReplaceShader(GrRecordingContext* context, |
| 121 | const GrColorInfo& dstColorInfo, |
| 122 | const SkPaint& skPaint, |
| 123 | const SkMatrixProvider& matrixProvider, |
| 124 | std::unique_ptr<GrFragmentProcessor> shaderFP, |
| 125 | SkBlendMode primColorMode, |
| 126 | GrPaint* grPaint); |
| 127 | |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 128 | /** This is used when there is a primitive color, but the shader should be ignored. Currently, |
| 129 | the expectation is that the primitive color will be premultiplied, though it really should be |
| 130 | unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be |
| 131 | applied to the primitive color after interpolation. */ |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 132 | inline bool SkPaintToGrPaintWithPrimitiveColor(GrRecordingContext* context, |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 133 | const GrColorInfo& dstColorInfo, |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 134 | const SkPaint& skPaint, |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 135 | const SkMatrixProvider& matrixProvider, |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 136 | GrPaint* grPaint) { |
John Stiles | f743d4e | 2020-07-23 11:35:08 -0400 | [diff] [blame] | 137 | return SkPaintToGrPaintWithBlend(context, dstColorInfo, skPaint, matrixProvider, |
| 138 | SkBlendMode::kDst, grPaint); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 139 | } |
| 140 | |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 141 | /** This is used when there may or may not be a shader, and the caller wants to plugin a texture |
| 142 | lookup. If there is a shader, then its output will only be used if the texture is alpha8. */ |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 143 | bool SkPaintToGrPaintWithTexture(GrRecordingContext*, |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 144 | const GrColorInfo& dstColorInfo, |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 145 | const SkPaint& skPaint, |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 146 | const SkMatrixProvider& matrixProvider, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 147 | std::unique_ptr<GrFragmentProcessor> fp, |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 148 | bool textureIsAlphaOnly, |
| 149 | GrPaint* grPaint); |
| 150 | |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 151 | //////////////////////////////////////////////////////////////////////////////// |
| 152 | // Misc Sk to Gr type conversions |
| 153 | |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 154 | static_assert((int)kZero_GrBlendCoeff == (int)SkBlendModeCoeff::kZero); |
| 155 | static_assert((int)kOne_GrBlendCoeff == (int)SkBlendModeCoeff::kOne); |
| 156 | static_assert((int)kSC_GrBlendCoeff == (int)SkBlendModeCoeff::kSC); |
| 157 | static_assert((int)kISC_GrBlendCoeff == (int)SkBlendModeCoeff::kISC); |
| 158 | static_assert((int)kDC_GrBlendCoeff == (int)SkBlendModeCoeff::kDC); |
| 159 | static_assert((int)kIDC_GrBlendCoeff == (int)SkBlendModeCoeff::kIDC); |
| 160 | static_assert((int)kSA_GrBlendCoeff == (int)SkBlendModeCoeff::kSA); |
| 161 | static_assert((int)kISA_GrBlendCoeff == (int)SkBlendModeCoeff::kISA); |
| 162 | static_assert((int)kDA_GrBlendCoeff == (int)SkBlendModeCoeff::kDA); |
| 163 | static_assert((int)kIDA_GrBlendCoeff == (int)SkBlendModeCoeff::kIDA); |
| 164 | // static_assert(SkXfermode::kCoeffCount == 10); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 165 | |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 166 | //////////////////////////////////////////////////////////////////////////////// |
| 167 | // Texture management |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 168 | |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 169 | /** |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 170 | * Policies for how to create textures for SkImages (and SkBitmaps). |
| 171 | */ |
| 172 | enum class GrImageTexGenPolicy : int { |
| 173 | // Choose the cheapest way to generate the texture. Use GrResourceCache if appropriate. |
| 174 | kDraw, |
| 175 | // Always make a new texture that is uncached and unbudgeted. |
| 176 | kNew_Uncached_Unbudgeted, |
| 177 | // Always make a new texture that is uncached and budgeted. |
| 178 | kNew_Uncached_Budgeted |
| 179 | }; |
| 180 | |
| 181 | /** |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 182 | * Creates a new texture with mipmap levels and copies the baseProxy into the base layer. |
| 183 | */ |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 184 | sk_sp<GrSurfaceProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext*, |
Brian Salomon | 982127b | 2021-01-21 10:43:35 -0500 | [diff] [blame] | 185 | sk_sp<GrSurfaceProxy> baseProxy, |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 186 | GrSurfaceOrigin origin, |
| 187 | SkBudgeted = SkBudgeted::kYes); |
| 188 | /** |
| 189 | * Same as GrCopyBaseMipMapToTextureProxy but takes the src as a view and returns a view with same |
| 190 | * origin and swizzle as the src view. |
| 191 | */ |
| 192 | GrSurfaceProxyView GrCopyBaseMipMapToView(GrRecordingContext*, |
| 193 | GrSurfaceProxyView, |
| 194 | SkBudgeted = SkBudgeted::kYes); |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 195 | |
Greg Daniel | 7e1912a | 2018-02-08 09:15:33 -0500 | [diff] [blame] | 196 | /* |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 197 | * Create a texture proxy from the provided bitmap and add it to the texture cache using the key |
| 198 | * also extracted from the bitmap. If GrMipmapped is kYes a non-mipmapped result may be returned |
| 199 | * if mipmapping isn't supported or for a 1x1 bitmap. If GrMipmapped is kNo it indicates mipmaps |
| 200 | * aren't required but a previously created mipmapped texture may still be returned. A color type is |
| 201 | * returned as color type conversion may be performed if there isn't a texture format equivalent of |
Brian Salomon | 3650f6d | 2021-06-08 13:22:41 -0400 | [diff] [blame] | 202 | * the bitmap's color type. |
Greg Daniel | 7e1912a | 2018-02-08 09:15:33 -0500 | [diff] [blame] | 203 | */ |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 204 | std::tuple<GrSurfaceProxyView, GrColorType> |
| 205 | GrMakeCachedBitmapProxyView(GrRecordingContext*, |
| 206 | const SkBitmap&, |
| 207 | GrMipmapped = GrMipmapped::kNo); |
| 208 | |
| 209 | /** |
Brian Salomon | 3650f6d | 2021-06-08 13:22:41 -0400 | [diff] [blame] | 210 | * Like above but always uploads the bitmap and never inserts into the cache. Unlike above, the |
| 211 | * texture may be approx or scratch and budgeted or not. |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 212 | */ |
| 213 | std::tuple<GrSurfaceProxyView, GrColorType> |
| 214 | GrMakeUncachedBitmapProxyView(GrRecordingContext*, |
| 215 | const SkBitmap&, |
| 216 | GrMipmapped = GrMipmapped::kNo, |
| 217 | SkBackingFit = SkBackingFit::kExact, |
| 218 | SkBudgeted = SkBudgeted::kYes); |
Robert Phillips | e14d305 | 2017-02-15 13:18:21 -0500 | [diff] [blame] | 219 | |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 220 | /** |
| 221 | * Our key includes the offset, width, and height so that bitmaps created by extractSubset() |
| 222 | * are unique. |
| 223 | * |
| 224 | * The imageID is in the shared namespace (see SkNextID::ImageID()) |
| 225 | * - SkBitmap/SkPixelRef |
| 226 | * - SkImage |
| 227 | * - SkImageGenerator |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 228 | */ |
| 229 | void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds); |
| 230 | |
Brian Salomon | 99a813c | 2020-03-02 12:50:47 -0500 | [diff] [blame] | 231 | /** |
| 232 | * Makes a SkIDChangeListener from a GrUniqueKey. The key will be invalidated in the resource |
| 233 | * cache if the ID becomes invalid. This also modifies the key so that it will cause the listener |
| 234 | * to be deregistered if the key is destroyed (to prevent unbounded listener growth when resources |
| 235 | * are purged before listeners trigger). |
| 236 | */ |
| 237 | sk_sp<SkIDChangeListener> GrMakeUniqueKeyInvalidationListener(GrUniqueKey*, uint32_t contextID); |
| 238 | |
Mike Reed | 52130b0 | 2020-12-28 15:33:13 -0500 | [diff] [blame] | 239 | static inline bool GrValidCubicResampler(SkCubicResampler cubic) { |
| 240 | return cubic.B >= 0 && cubic.C >= 0; |
| 241 | } |
| 242 | |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 243 | #endif |