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 | |
Ethan Nicholas | 052fd51 | 2017-01-27 15:34:34 +0000 | [diff] [blame] | 11 | #include "GrBlend.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 12 | #include "GrColor.h" |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 13 | #include "GrSamplerState.h" |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 14 | #include "GrTypes.h" |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 15 | #include "SkBlendModePriv.h" |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 16 | #include "SkCanvas.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 17 | #include "SkColor.h" |
Cary Clark | a4083c9 | 2017-09-15 11:59:23 -0400 | [diff] [blame] | 18 | #include "SkColorData.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 19 | #include "SkFilterQuality.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 20 | #include "SkImageInfo.h" |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 21 | #include "SkMatrix.h" |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 22 | #include "SkVertices.h" |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 23 | |
| 24 | class GrCaps; |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 25 | class GrColorSpaceInfo; |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 26 | class GrColorSpaceXform; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 27 | class GrContext; |
| 28 | class GrFragmentProcessor; |
| 29 | class GrPaint; |
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; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 35 | class SkPaint; |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 36 | class SkPixelRef; |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 37 | class SkPixmap; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 38 | struct SkIRect; |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 39 | |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 40 | //////////////////////////////////////////////////////////////////////////////// |
| 41 | // Color type conversions |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 42 | |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 43 | static inline GrColor SkColorToPremulGrColor(SkColor c) { |
| 44 | SkPMColor pm = SkPreMultiplyColor(c); |
| 45 | unsigned r = SkGetPackedR32(pm); |
| 46 | unsigned g = SkGetPackedG32(pm); |
| 47 | unsigned b = SkGetPackedB32(pm); |
| 48 | unsigned a = SkGetPackedA32(pm); |
| 49 | return GrColorPackRGBA(r, g, b, a); |
| 50 | } |
| 51 | |
| 52 | static inline GrColor SkColorToUnpremulGrColor(SkColor c) { |
| 53 | unsigned r = SkColorGetR(c); |
| 54 | unsigned g = SkColorGetG(c); |
| 55 | unsigned b = SkColorGetB(c); |
| 56 | unsigned a = SkColorGetA(c); |
| 57 | return GrColorPackRGBA(r, g, b, a); |
| 58 | } |
| 59 | |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 60 | /** Similar, but using SkPMColor4f. */ |
| 61 | SkPMColor4f SkColorToPMColor4f(SkColor, const GrColorSpaceInfo&); |
| 62 | |
Brian Osman | 5c8a6b3 | 2018-11-19 11:56:57 -0500 | [diff] [blame^] | 63 | /** Converts an SkColor4f to the destination color space. Pins the color if the destination is |
| 64 | normalized, or the device does not support half-float vertex attributes. */ |
| 65 | SkColor4f SkColor4fPrepForDst(SkColor4f, const GrColorSpaceInfo&, const GrCaps&); |
| 66 | |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 67 | //////////////////////////////////////////////////////////////////////////////// |
| 68 | // Paint conversion |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 69 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 70 | /** Converts an SkPaint to a GrPaint for a given GrContext. The matrix is required in order |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 71 | to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */ |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 72 | bool SkPaintToGrPaint(GrContext*, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 73 | const GrColorSpaceInfo& dstColorSpaceInfo, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 74 | const SkPaint& skPaint, |
| 75 | const SkMatrix& viewM, |
| 76 | GrPaint* grPaint); |
| 77 | |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 78 | /** Same as above but ignores the SkShader (if any) on skPaint. */ |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 79 | bool SkPaintToGrPaintNoShader(GrContext* context, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 80 | const GrColorSpaceInfo& dstColorSpaceInfo, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 81 | const SkPaint& skPaint, |
| 82 | GrPaint* grPaint); |
| 83 | |
| 84 | /** 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] | 85 | should expect an unpremul input color and produce a premultiplied output color. There is |
| 86 | no primitive color. */ |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 87 | bool SkPaintToGrPaintReplaceShader(GrContext*, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 88 | const GrColorSpaceInfo& dstColorSpaceInfo, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 89 | const SkPaint& skPaint, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 90 | std::unique_ptr<GrFragmentProcessor> shaderFP, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 91 | GrPaint* grPaint); |
| 92 | |
| 93 | /** 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] | 94 | GrOp's GrPrimitiveProcesssor. */ |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 95 | bool SkPaintToGrPaintWithXfermode(GrContext* context, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 96 | const GrColorSpaceInfo& dstColorSpaceInfo, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 97 | const SkPaint& skPaint, |
| 98 | const SkMatrix& viewM, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 99 | SkBlendMode primColorMode, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 100 | GrPaint* grPaint); |
| 101 | |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 102 | /** This is used when there is a primitive color, but the shader should be ignored. Currently, |
| 103 | the expectation is that the primitive color will be premultiplied, though it really should be |
| 104 | unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be |
| 105 | applied to the primitive color after interpolation. */ |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 106 | inline bool SkPaintToGrPaintWithPrimitiveColor(GrContext* context, |
| 107 | const GrColorSpaceInfo& dstColorSpaceInfo, |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 108 | const SkPaint& skPaint, GrPaint* grPaint) { |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 109 | return SkPaintToGrPaintWithXfermode(context, dstColorSpaceInfo, skPaint, SkMatrix::I(), |
| 110 | SkBlendMode::kDst, grPaint); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 111 | } |
| 112 | |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 113 | /** This is used when there may or may not be a shader, and the caller wants to plugin a texture |
| 114 | lookup. If there is a shader, then its output will only be used if the texture is alpha8. */ |
| 115 | bool SkPaintToGrPaintWithTexture(GrContext* context, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 116 | const GrColorSpaceInfo& dstColorSpaceInfo, |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 117 | const SkPaint& paint, |
| 118 | const SkMatrix& viewM, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 119 | std::unique_ptr<GrFragmentProcessor> fp, |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 120 | bool textureIsAlphaOnly, |
| 121 | GrPaint* grPaint); |
| 122 | |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 123 | //////////////////////////////////////////////////////////////////////////////// |
| 124 | // Misc Sk to Gr type conversions |
| 125 | |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 126 | GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&); |
| 127 | GrPixelConfig SkColorType2GrPixelConfig(const SkColorType); |
| 128 | GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info); |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 129 | |
| 130 | bool GrPixelConfigToColorType(GrPixelConfig, SkColorType*); |
| 131 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 132 | GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality, |
| 133 | const SkMatrix& viewM, |
| 134 | const SkMatrix& localM, |
Brian Osman | db78cba | 2018-02-15 10:09:48 -0500 | [diff] [blame] | 135 | bool sharpenMipmappedTextures, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 136 | bool* doBicubic); |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 137 | |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 138 | ////////////////////////////////////////////////////////////////////////////// |
| 139 | |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 140 | static inline GrPrimitiveType SkVertexModeToGrPrimitiveType(SkVertices::VertexMode mode) { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 141 | switch (mode) { |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 142 | case SkVertices::kTriangles_VertexMode: |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 143 | return GrPrimitiveType::kTriangles; |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 144 | case SkVertices::kTriangleStrip_VertexMode: |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 145 | return GrPrimitiveType::kTriangleStrip; |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 146 | case SkVertices::kTriangleFan_VertexMode: |
Brian Salomon | cccafe8 | 2018-04-28 16:13:08 -0400 | [diff] [blame] | 147 | break; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 148 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 149 | SK_ABORT("Invalid mode"); |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 150 | return GrPrimitiveType::kPoints; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | ////////////////////////////////////////////////////////////////////////////// |
| 154 | |
Mike Reed | 6b3542a | 2017-06-06 10:41:18 -0400 | [diff] [blame] | 155 | GR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkBlendModeCoeff::kZero); |
| 156 | GR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkBlendModeCoeff::kOne); |
| 157 | GR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkBlendModeCoeff::kSC); |
| 158 | GR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkBlendModeCoeff::kISC); |
| 159 | GR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkBlendModeCoeff::kDC); |
| 160 | GR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkBlendModeCoeff::kIDC); |
| 161 | GR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkBlendModeCoeff::kSA); |
| 162 | GR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkBlendModeCoeff::kISA); |
| 163 | GR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkBlendModeCoeff::kDA); |
| 164 | GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkBlendModeCoeff::kIDA); |
| 165 | //GR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 166 | |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 167 | //////////////////////////////////////////////////////////////////////////////// |
| 168 | // Texture management |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 169 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 170 | /** Returns a texture representing the bitmap that is compatible with the GrSamplerState. The |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 171 | * texture is inserted into the cache (unless the bitmap is marked volatile) and can be |
| 172 | * retrieved again via this function. |
| 173 | * The 'scaleAdjust' in/out parameter will be updated to hold any rescaling that needs to be |
| 174 | * performed on the absolute texture coordinates (e.g., if the texture is resized out to |
| 175 | * the next power of two). It can be null if the caller is sure the bitmap won't be resized. |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 176 | */ |
Robert Phillips | bbd7a3b | 2017-03-21 08:48:40 -0400 | [diff] [blame] | 177 | sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrContext*, |
| 178 | const SkBitmap&, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 179 | const GrSamplerState&, |
Robert Phillips | bbd7a3b | 2017-03-21 08:48:40 -0400 | [diff] [blame] | 180 | SkScalar scaleAdjust[2]); |
| 181 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Creates a new texture for the bitmap. Does not concern itself with cache keys or texture params. |
| 184 | * The bitmap must have CPU-accessible pixels. Attempts to take advantage of faster paths for |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 185 | * yuv planes. |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 186 | */ |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 187 | sk_sp<GrTextureProxy> GrUploadBitmapToTextureProxy(GrProxyProvider*, const SkBitmap&); |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 188 | |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 189 | /** |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 190 | * Creates a new texture with mipmap levels and copies the baseProxy into the base layer. |
| 191 | */ |
| 192 | sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrContext*, |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 193 | GrTextureProxy* baseProxy); |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 194 | |
Greg Daniel | 7e1912a | 2018-02-08 09:15:33 -0500 | [diff] [blame] | 195 | /* |
| 196 | * Create a texture proxy from the provided bitmap by wrapping it in an image and calling |
| 197 | * GrMakeCachedImageProxy. |
| 198 | */ |
| 199 | sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrProxyProvider*, const SkBitmap& bitmap, |
| 200 | SkBackingFit fit = SkBackingFit::kExact); |
Robert Phillips | e14d305 | 2017-02-15 13:18:21 -0500 | [diff] [blame] | 201 | |
Robert Phillips | 7a92639 | 2018-02-01 15:49:54 -0500 | [diff] [blame] | 202 | /* |
| 203 | * Create a texture proxy from the provided 'srcImage' and add it to the texture cache |
| 204 | * using the key also extracted from 'srcImage'. |
| 205 | */ |
Greg Daniel | 490695b | 2018-02-05 09:34:02 -0500 | [diff] [blame] | 206 | sk_sp<GrTextureProxy> GrMakeCachedImageProxy(GrProxyProvider*, sk_sp<SkImage> srcImage, |
| 207 | SkBackingFit fit = SkBackingFit::kExact); |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 208 | |
| 209 | /** |
| 210 | * Our key includes the offset, width, and height so that bitmaps created by extractSubset() |
| 211 | * are unique. |
| 212 | * |
| 213 | * The imageID is in the shared namespace (see SkNextID::ImageID()) |
| 214 | * - SkBitmap/SkPixelRef |
| 215 | * - SkImage |
| 216 | * - SkImageGenerator |
| 217 | * |
| 218 | * Note: width/height must fit in 16bits for this impl. |
| 219 | */ |
| 220 | void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds); |
| 221 | |
| 222 | /** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be |
| 223 | removed should the bitmap's contents change or be destroyed. */ |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 224 | void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID, |
| 225 | SkPixelRef* pixelRef); |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 226 | |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 227 | #endif |