reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 SkGrPriv_DEFINED |
| 9 | #define SkGrPriv_DEFINED |
| 10 | |
Ethan Nicholas | 052fd51 | 2017-01-27 15:34:34 +0000 | [diff] [blame] | 11 | #include "GrBlend.h" |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 12 | #include "GrTypes.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 13 | #include "SkImageInfo.h" |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 14 | #include "SkMatrix.h" |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 15 | #include "SkPM4f.h" |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 16 | #include "SkXfermodePriv.h" |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 17 | |
| 18 | class GrCaps; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 19 | class GrContext; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 20 | class GrRenderTargetContext; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 21 | class GrFragmentProcessor; |
| 22 | class GrPaint; |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 23 | class GrTexture; |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 24 | class GrUniqueKey; |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 25 | class SkBitmap; |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 26 | class SkData; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 27 | class SkPaint; |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 28 | class SkPixelRef; |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 29 | class SkPixmap; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 30 | struct SkIRect; |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Our key includes the offset, width, and height so that bitmaps created by extractSubset() |
| 34 | * are unique. |
| 35 | * |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 36 | * The imageID is in the shared namespace (see SkNextID::ImageID()) |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 37 | * - SkBitmap/SkPixelRef |
| 38 | * - SkImage |
| 39 | * - SkImageGenerator |
| 40 | * |
| 41 | * Note: width/height must fit in 16bits for this impl. |
| 42 | */ |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 43 | void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 44 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 45 | /** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be |
| 46 | removed should the bitmap's contents change or be destroyed. */ |
| 47 | void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef); |
| 48 | |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 49 | /** 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] | 50 | 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] | 51 | bool SkPaintToGrPaint(GrContext*, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 52 | GrRenderTargetContext*, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 53 | const SkPaint& skPaint, |
| 54 | const SkMatrix& viewM, |
| 55 | GrPaint* grPaint); |
| 56 | |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 57 | /** Same as above but ignores the SkShader (if any) on skPaint. */ |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 58 | bool SkPaintToGrPaintNoShader(GrContext* context, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 59 | GrRenderTargetContext* rtc, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 60 | const SkPaint& skPaint, |
| 61 | GrPaint* grPaint); |
| 62 | |
| 63 | /** 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] | 64 | should expect an unpremul input color and produce a premultiplied output color. There is |
| 65 | no primitive color. */ |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 66 | bool SkPaintToGrPaintReplaceShader(GrContext*, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 67 | GrRenderTargetContext*, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 68 | const SkPaint& skPaint, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 69 | sk_sp<GrFragmentProcessor> shaderFP, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 70 | GrPaint* grPaint); |
| 71 | |
| 72 | /** Blends the SkPaint's shader (or color if no shader) with the color which specified via a |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 73 | GrOp's GrPrimitiveProcesssor. Currently there is a bool param to indicate whether the |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 74 | primitive color is the dst or src color to the blend in order to work around differences between |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 75 | drawVertices and drawAtlas. */ |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 76 | bool SkPaintToGrPaintWithXfermode(GrContext* context, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 77 | GrRenderTargetContext* rtc, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 78 | const SkPaint& skPaint, |
| 79 | const SkMatrix& viewM, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 80 | SkBlendMode primColorMode, |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 81 | bool primitiveIsSrc, |
| 82 | GrPaint* grPaint); |
| 83 | |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 84 | /** This is used when there is a primitive color, but the shader should be ignored. Currently, |
| 85 | the expectation is that the primitive color will be premultiplied, though it really should be |
| 86 | unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be |
| 87 | applied to the primitive color after interpolation. */ |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 88 | inline bool SkPaintToGrPaintWithPrimitiveColor(GrContext* context, GrRenderTargetContext* rtc, |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 89 | const SkPaint& skPaint, GrPaint* grPaint) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 90 | return SkPaintToGrPaintWithXfermode(context, rtc, skPaint, SkMatrix::I(), SkBlendMode::kDst, |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 91 | false, grPaint); |
bsalomon | aa48d36 | 2015-10-01 08:34:17 -0700 | [diff] [blame] | 92 | } |
| 93 | |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 94 | /** This is used when there may or may not be a shader, and the caller wants to plugin a texture |
| 95 | lookup. If there is a shader, then its output will only be used if the texture is alpha8. */ |
| 96 | bool SkPaintToGrPaintWithTexture(GrContext* context, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 97 | GrRenderTargetContext* rtc, |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 98 | const SkPaint& paint, |
| 99 | const SkMatrix& viewM, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 100 | sk_sp<GrFragmentProcessor> fp, |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 101 | bool textureIsAlphaOnly, |
| 102 | GrPaint* grPaint); |
| 103 | |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 104 | ////////////////////////////////////////////////////////////////////////////// |
| 105 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 106 | static inline SkPM4f GrColor4fToSkPM4f(const GrColor4f& c) { |
| 107 | SkPM4f pm4f; |
| 108 | pm4f.fVec[SkPM4f::R] = c.fRGBA[0]; |
| 109 | pm4f.fVec[SkPM4f::G] = c.fRGBA[1]; |
| 110 | pm4f.fVec[SkPM4f::B] = c.fRGBA[2]; |
| 111 | pm4f.fVec[SkPM4f::A] = c.fRGBA[3]; |
| 112 | return pm4f; |
| 113 | } |
| 114 | |
| 115 | static inline GrColor4f SkPM4fToGrColor4f(const SkPM4f& c) { |
| 116 | return GrColor4f{c.r(), c.g(), c.b(), c.a()}; |
| 117 | } |
| 118 | |
| 119 | ////////////////////////////////////////////////////////////////////////////// |
| 120 | |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 121 | GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&, const GrCaps&); |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 122 | |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 123 | bool GrPixelConfigToColorType(GrPixelConfig, SkColorType*); |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 124 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 125 | /** When image filter code needs to construct a render target context to do intermediate rendering, |
| 126 | we need a renderable pixel config. The source (SkSpecialImage) may not be in a renderable |
| 127 | format, but we want to preserve the color space of that source. This picks an appropriate format |
| 128 | to use. */ |
brianosman | 27a3aa5 | 2016-09-23 07:11:34 -0700 | [diff] [blame] | 129 | GrPixelConfig GrRenderableConfigForColorSpace(const SkColorSpace*); |
brianosman | 2695eaa | 2016-09-21 06:45:09 -0700 | [diff] [blame] | 130 | |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 131 | /** |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 132 | * If the compressed data in the SkData is supported (as a texture format, this returns |
| 133 | * the pixel-config that should be used, and sets outStartOfDataToUpload to the ptr into |
| 134 | * the data where the actual raw data starts (skipping any header bytes). |
| 135 | * |
| 136 | * If the compressed data is not supported, this returns kUnknown_GrPixelConfig, and |
| 137 | * ignores outStartOfDataToUpload. |
| 138 | */ |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 139 | GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data, |
| 140 | int expectedW, int expectedH, |
| 141 | const void** outStartOfDataToUpload); |
| 142 | |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 143 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 144 | /** |
| 145 | * Creates a new texture for the bitmap. Does not concern itself with cache keys or texture params. |
| 146 | * The bitmap must have CPU-accessible pixels. Attempts to take advantage of faster paths for |
| 147 | * compressed textures and yuv planes. |
| 148 | */ |
| 149 | GrTexture* GrUploadBitmapToTexture(GrContext*, const SkBitmap&); |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 150 | |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 151 | GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext*, const SkBitmap&, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 152 | SkColorSpace* dstColorSpace); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 153 | |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 154 | /** |
| 155 | * Creates a new texture for the pixmap. |
| 156 | */ |
ericrk | 8bea890 | 2016-03-18 11:52:20 -0700 | [diff] [blame] | 157 | GrTexture* GrUploadPixmapToTexture(GrContext*, const SkPixmap&, SkBudgeted budgeted); |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 158 | |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 159 | /** |
| 160 | * Creates a new texture populated with the mipmap levels. |
| 161 | */ |
| 162 | GrTexture* GrUploadMipMapToTexture(GrContext*, const SkImageInfo&, const GrMipLevel* texels, |
| 163 | int mipLevelCount); |
| 164 | |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 165 | ////////////////////////////////////////////////////////////////////////////// |
| 166 | |
| 167 | GR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkXfermode::kZero_Coeff); |
| 168 | GR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkXfermode::kOne_Coeff); |
| 169 | GR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkXfermode::kSC_Coeff); |
| 170 | GR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkXfermode::kISC_Coeff); |
| 171 | GR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkXfermode::kDC_Coeff); |
| 172 | GR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkXfermode::kIDC_Coeff); |
| 173 | GR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkXfermode::kSA_Coeff); |
| 174 | GR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkXfermode::kISA_Coeff); |
| 175 | GR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkXfermode::kDA_Coeff); |
| 176 | GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkXfermode::kIDA_Coeff); |
| 177 | GR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10); |
| 178 | |
| 179 | #define SkXfermodeCoeffToGrBlendCoeff(X) ((GrBlendCoeff)(X)) |
| 180 | |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 181 | #endif |