blob: 1501f904db8c509624b03c30c7f23450c743e292 [file] [log] [blame]
reed8f343722015-08-13 13:32:39 -07001/*
Brian Osman3b655982017-03-07 16:58:08 -05002 * Copyright 2017 Google Inc.
reed8f343722015-08-13 13:32:39 -07003 *
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 Osman3b655982017-03-07 16:58:08 -05008#ifndef SkGr_DEFINED
9#define SkGr_DEFINED
reed8f343722015-08-13 13:32:39 -070010
Ethan Nicholas052fd512017-01-27 15:34:34 +000011#include "GrBlend.h"
Brian Osman3b655982017-03-07 16:58:08 -050012#include "GrColor.h"
Brian Salomon2bbdcc42017-09-07 12:36:34 -040013#include "GrSamplerState.h"
Brian Salomon587e08f2017-01-27 10:59:27 -050014#include "GrTypes.h"
Brian Salomon2bbdcc42017-09-07 12:36:34 -040015#include "SkBlendModePriv.h"
Brian Salomon199fb872017-02-06 09:41:10 -050016#include "SkCanvas.h"
Brian Osman3b655982017-03-07 16:58:08 -050017#include "SkColor.h"
Cary Clarka4083c92017-09-15 11:59:23 -040018#include "SkColorData.h"
Brian Osman3b655982017-03-07 16:58:08 -050019#include "SkFilterQuality.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070020#include "SkImageInfo.h"
bsalomonafa95e22015-10-12 10:39:46 -070021#include "SkMatrix.h"
Brian Salomon587e08f2017-01-27 10:59:27 -050022#include "SkPM4f.h"
Mike Reed887cdf12017-04-03 11:11:09 -040023#include "SkVertices.h"
reed8f343722015-08-13 13:32:39 -070024
25class GrCaps;
Brian Salomonf3569f02017-10-24 12:52:33 -040026class GrColorSpaceInfo;
Brian Osman3b655982017-03-07 16:58:08 -050027class GrColorSpaceXform;
bsalomonf1b7a1d2015-09-28 06:26:28 -070028class GrContext;
29class GrFragmentProcessor;
30class GrPaint;
Robert Phillips26c90e02017-03-14 14:39:29 -040031class GrResourceProvider;
Robert Phillipse14d3052017-02-15 13:18:21 -050032class GrTextureProxy;
reed8f343722015-08-13 13:32:39 -070033class GrUniqueKey;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050034class SkBitmap;
bsalomonafa95e22015-10-12 10:39:46 -070035class SkData;
bsalomonf1b7a1d2015-09-28 06:26:28 -070036class SkPaint;
bsalomonafa95e22015-10-12 10:39:46 -070037class SkPixelRef;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050038class SkPixmap;
bsalomonf1b7a1d2015-09-28 06:26:28 -070039struct SkIRect;
reed8f343722015-08-13 13:32:39 -070040
Brian Osman3b655982017-03-07 16:58:08 -050041////////////////////////////////////////////////////////////////////////////////
42// Color type conversions
reed856e9d92015-09-30 12:21:45 -070043
Brian Osman3b655982017-03-07 16:58:08 -050044static inline GrColor SkColorToPremulGrColor(SkColor c) {
45 SkPMColor pm = SkPreMultiplyColor(c);
46 unsigned r = SkGetPackedR32(pm);
47 unsigned g = SkGetPackedG32(pm);
48 unsigned b = SkGetPackedB32(pm);
49 unsigned a = SkGetPackedA32(pm);
50 return GrColorPackRGBA(r, g, b, a);
51}
52
53static inline GrColor SkColorToUnpremulGrColor(SkColor c) {
54 unsigned r = SkColorGetR(c);
55 unsigned g = SkColorGetG(c);
56 unsigned b = SkColorGetB(c);
57 unsigned a = SkColorGetA(c);
58 return GrColorPackRGBA(r, g, b, a);
59}
60
Brian Osmanb0f4d0a2018-09-05 14:16:27 +000061/** Transform an SkColor (sRGB bytes) or SkColor4f (sRGB floats) to GrColor4f
62 for the specified color space info. */
Brian Salomon4cbb6e62017-10-25 15:12:19 -040063GrColor4f SkColorToPremulGrColor4f(SkColor, const GrColorSpaceInfo&);
Brian Osmanb0f4d0a2018-09-05 14:16:27 +000064GrColor4f SkColor4fToPremulGrColor4fLegacy(SkColor4f);
65GrColor4f SkColor4fToUnpremulGrColor4f(SkColor4f, const GrColorSpaceInfo&);
Brian Osman3b655982017-03-07 16:58:08 -050066
67//////////////////////////////////////////////////////////////////////////////
68
69static inline SkPM4f GrColor4fToSkPM4f(const GrColor4f& c) {
70 SkPM4f pm4f;
71 pm4f.fVec[SkPM4f::R] = c.fRGBA[0];
72 pm4f.fVec[SkPM4f::G] = c.fRGBA[1];
73 pm4f.fVec[SkPM4f::B] = c.fRGBA[2];
74 pm4f.fVec[SkPM4f::A] = c.fRGBA[3];
75 return pm4f;
76}
77
78static inline GrColor4f SkPM4fToGrColor4f(const SkPM4f& c) {
79 return GrColor4f{c.r(), c.g(), c.b(), c.a()};
80}
81
82////////////////////////////////////////////////////////////////////////////////
83// Paint conversion
bsalomonc55271f2015-11-09 11:55:57 -080084
bsalomonf1b7a1d2015-09-28 06:26:28 -070085/** Converts an SkPaint to a GrPaint for a given GrContext. The matrix is required in order
bsalomonaa48d362015-10-01 08:34:17 -070086 to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070087bool SkPaintToGrPaint(GrContext*,
Brian Salomonf3569f02017-10-24 12:52:33 -040088 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -070089 const SkPaint& skPaint,
90 const SkMatrix& viewM,
91 GrPaint* grPaint);
92
bsalomonaa48d362015-10-01 08:34:17 -070093/** Same as above but ignores the SkShader (if any) on skPaint. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070094bool SkPaintToGrPaintNoShader(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -040095 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -070096 const SkPaint& skPaint,
97 GrPaint* grPaint);
98
99/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
bsalomonaa48d362015-10-01 08:34:17 -0700100 should expect an unpremul input color and produce a premultiplied output color. There is
101 no primitive color. */
bsalomonf1b7a1d2015-09-28 06:26:28 -0700102bool SkPaintToGrPaintReplaceShader(GrContext*,
Brian Salomonf3569f02017-10-24 12:52:33 -0400103 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700104 const SkPaint& skPaint,
Brian Salomonaff329b2017-08-11 09:40:37 -0400105 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700106 GrPaint* grPaint);
107
108/** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
Mike Reed185ba212017-04-28 12:31:05 -0400109 GrOp's GrPrimitiveProcesssor. */
bsalomonf1b7a1d2015-09-28 06:26:28 -0700110bool SkPaintToGrPaintWithXfermode(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400111 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700112 const SkPaint& skPaint,
113 const SkMatrix& viewM,
Mike Reed7d954ad2016-10-28 15:42:34 -0400114 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700115 GrPaint* grPaint);
116
bsalomonaa48d362015-10-01 08:34:17 -0700117/** This is used when there is a primitive color, but the shader should be ignored. Currently,
118 the expectation is that the primitive color will be premultiplied, though it really should be
119 unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
120 applied to the primitive color after interpolation. */
Brian Salomonf3569f02017-10-24 12:52:33 -0400121inline bool SkPaintToGrPaintWithPrimitiveColor(GrContext* context,
122 const GrColorSpaceInfo& dstColorSpaceInfo,
brianosman8fe485b2016-07-25 12:31:51 -0700123 const SkPaint& skPaint, GrPaint* grPaint) {
Brian Salomonf3569f02017-10-24 12:52:33 -0400124 return SkPaintToGrPaintWithXfermode(context, dstColorSpaceInfo, skPaint, SkMatrix::I(),
125 SkBlendMode::kDst, grPaint);
bsalomonaa48d362015-10-01 08:34:17 -0700126}
127
joshualitt33a5fce2015-11-18 13:28:51 -0800128/** This is used when there may or may not be a shader, and the caller wants to plugin a texture
129 lookup. If there is a shader, then its output will only be used if the texture is alpha8. */
130bool SkPaintToGrPaintWithTexture(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400131 const GrColorSpaceInfo& dstColorSpaceInfo,
joshualitt33a5fce2015-11-18 13:28:51 -0800132 const SkPaint& paint,
133 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400134 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800135 bool textureIsAlphaOnly,
136 GrPaint* grPaint);
137
Brian Osman3b655982017-03-07 16:58:08 -0500138////////////////////////////////////////////////////////////////////////////////
139// Misc Sk to Gr type conversions
140
Brian Osman2b23c4b2018-06-01 12:25:08 -0400141GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&);
142GrPixelConfig SkColorType2GrPixelConfig(const SkColorType);
143GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info);
Brian Osman3b655982017-03-07 16:58:08 -0500144
145bool GrPixelConfigToColorType(GrPixelConfig, SkColorType*);
146
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400147GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
148 const SkMatrix& viewM,
149 const SkMatrix& localM,
Brian Osmandb78cba2018-02-15 10:09:48 -0500150 bool sharpenMipmappedTextures,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400151 bool* doBicubic);
Brian Osman3b655982017-03-07 16:58:08 -0500152
bsalomonf276ac52015-10-09 13:36:42 -0700153//////////////////////////////////////////////////////////////////////////////
154
Mike Reed887cdf12017-04-03 11:11:09 -0400155static inline GrPrimitiveType SkVertexModeToGrPrimitiveType(SkVertices::VertexMode mode) {
Brian Salomon199fb872017-02-06 09:41:10 -0500156 switch (mode) {
Mike Reed887cdf12017-04-03 11:11:09 -0400157 case SkVertices::kTriangles_VertexMode:
Chris Dalton3809bab2017-06-13 10:55:06 -0600158 return GrPrimitiveType::kTriangles;
Mike Reed887cdf12017-04-03 11:11:09 -0400159 case SkVertices::kTriangleStrip_VertexMode:
Chris Dalton3809bab2017-06-13 10:55:06 -0600160 return GrPrimitiveType::kTriangleStrip;
Mike Reed887cdf12017-04-03 11:11:09 -0400161 case SkVertices::kTriangleFan_VertexMode:
Brian Salomoncccafe82018-04-28 16:13:08 -0400162 break;
Brian Salomon199fb872017-02-06 09:41:10 -0500163 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400164 SK_ABORT("Invalid mode");
Chris Dalton3809bab2017-06-13 10:55:06 -0600165 return GrPrimitiveType::kPoints;
Brian Salomon199fb872017-02-06 09:41:10 -0500166}
167
168//////////////////////////////////////////////////////////////////////////////
169
Mike Reed6b3542a2017-06-06 10:41:18 -0400170GR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkBlendModeCoeff::kZero);
171GR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkBlendModeCoeff::kOne);
172GR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkBlendModeCoeff::kSC);
173GR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkBlendModeCoeff::kISC);
174GR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkBlendModeCoeff::kDC);
175GR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkBlendModeCoeff::kIDC);
176GR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkBlendModeCoeff::kSA);
177GR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkBlendModeCoeff::kISA);
178GR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkBlendModeCoeff::kDA);
179GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkBlendModeCoeff::kIDA);
180//GR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10);
Brian Salomon587e08f2017-01-27 10:59:27 -0500181
Brian Osman3b655982017-03-07 16:58:08 -0500182#define SkXfermodeCoeffToGrBlendCoeff(X) ((GrBlendCoeff)(X))
Brian Salomon587e08f2017-01-27 10:59:27 -0500183
Brian Osman3b655982017-03-07 16:58:08 -0500184////////////////////////////////////////////////////////////////////////////////
185// Texture management
Brian Salomon587e08f2017-01-27 10:59:27 -0500186
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400187/** Returns a texture representing the bitmap that is compatible with the GrSamplerState. The
Brian Osman3b655982017-03-07 16:58:08 -0500188 * texture is inserted into the cache (unless the bitmap is marked volatile) and can be
189 * retrieved again via this function.
190 * The 'scaleAdjust' in/out parameter will be updated to hold any rescaling that needs to be
191 * performed on the absolute texture coordinates (e.g., if the texture is resized out to
192 * the next power of two). It can be null if the caller is sure the bitmap won't be resized.
bsalomon045802d2015-10-20 07:58:01 -0700193 */
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400194sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrContext*,
195 const SkBitmap&,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400196 const GrSamplerState&,
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400197 SkScalar scaleAdjust[2]);
198
bsalomon045802d2015-10-20 07:58:01 -0700199/**
200 * Creates a new texture for the bitmap. Does not concern itself with cache keys or texture params.
201 * The bitmap must have CPU-accessible pixels. Attempts to take advantage of faster paths for
Robert Phillips92de6312017-05-23 07:43:48 -0400202 * yuv planes.
bsalomon045802d2015-10-20 07:58:01 -0700203 */
Brian Osman2b23c4b2018-06-01 12:25:08 -0400204sk_sp<GrTextureProxy> GrUploadBitmapToTextureProxy(GrProxyProvider*, const SkBitmap&);
Robert Phillipsd3749482017-03-14 09:17:43 -0400205
bsalomon0d996862016-03-09 18:44:43 -0800206/**
Greg Daniel55afd6d2017-09-29 09:32:44 -0400207 * Creates a new texture with mipmap levels and copies the baseProxy into the base layer.
208 */
209sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrContext*,
Greg Daniele1da1d92017-10-06 15:59:27 -0400210 GrTextureProxy* baseProxy);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400211
Greg Daniel7e1912a2018-02-08 09:15:33 -0500212/*
213 * Create a texture proxy from the provided bitmap by wrapping it in an image and calling
214 * GrMakeCachedImageProxy.
215 */
216sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrProxyProvider*, const SkBitmap& bitmap,
217 SkBackingFit fit = SkBackingFit::kExact);
Robert Phillipse14d3052017-02-15 13:18:21 -0500218
Robert Phillips7a926392018-02-01 15:49:54 -0500219/*
220 * Create a texture proxy from the provided 'srcImage' and add it to the texture cache
221 * using the key also extracted from 'srcImage'.
222 */
Greg Daniel490695b2018-02-05 09:34:02 -0500223sk_sp<GrTextureProxy> GrMakeCachedImageProxy(GrProxyProvider*, sk_sp<SkImage> srcImage,
224 SkBackingFit fit = SkBackingFit::kExact);
Brian Osman3b655982017-03-07 16:58:08 -0500225
226/**
227 * Our key includes the offset, width, and height so that bitmaps created by extractSubset()
228 * are unique.
229 *
230 * The imageID is in the shared namespace (see SkNextID::ImageID())
231 * - SkBitmap/SkPixelRef
232 * - SkImage
233 * - SkImageGenerator
234 *
235 * Note: width/height must fit in 16bits for this impl.
236 */
237void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
238
239/** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be
240 removed should the bitmap's contents change or be destroyed. */
Brian Salomon238069b2018-07-11 15:58:57 -0400241void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID,
242 SkPixelRef* pixelRef);
Brian Osman3b655982017-03-07 16:58:08 -0500243
reed8f343722015-08-13 13:32:39 -0700244#endif