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 "GrBlend.h"
12 #include "GrColor.h"
13 #include "GrSamplerState.h"
14 #include "GrTypes.h"
15 #include "SkBlendModePriv.h"
16 #include "SkCanvas.h"
17 #include "SkColor.h"
18 #include "SkColorData.h"
19 #include "SkFilterQuality.h"
20 #include "SkImageInfo.h"
21 #include "SkMatrix.h"
22 #include "SkVertices.h"
23
24 class GrCaps;
25 class GrColorSpaceInfo;
26 class GrColorSpaceXform;
27 class GrContext;
28 class GrFragmentProcessor;
29 class GrPaint;
30 class GrResourceProvider;
31 class GrTextureProxy;
32 class GrUniqueKey;
33 class SkBitmap;
34 class SkData;
35 class SkPaint;
36 class SkPixelRef;
37 class SkPixmap;
38 struct SkIRect;
39
40 ////////////////////////////////////////////////////////////////////////////////
41 // Color type conversions
42
SkColorToPremulGrColor(SkColor c)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
SkColorToUnpremulGrColor(SkColor c)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
60 /** Similar, but using SkPMColor4f. */
61 SkPMColor4f SkColorToPMColor4f(SkColor, const GrColorSpaceInfo&);
62
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
67 ////////////////////////////////////////////////////////////////////////////////
68 // Paint conversion
69
70 /** Converts an SkPaint to a GrPaint for a given GrContext. The matrix is required in order
71 to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
72 bool SkPaintToGrPaint(GrContext*,
73 const GrColorSpaceInfo& dstColorSpaceInfo,
74 const SkPaint& skPaint,
75 const SkMatrix& viewM,
76 GrPaint* grPaint);
77
78 /** Same as above but ignores the SkShader (if any) on skPaint. */
79 bool SkPaintToGrPaintNoShader(GrContext* context,
80 const GrColorSpaceInfo& dstColorSpaceInfo,
81 const SkPaint& skPaint,
82 GrPaint* grPaint);
83
84 /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
85 should expect an unpremul input color and produce a premultiplied output color. There is
86 no primitive color. */
87 bool SkPaintToGrPaintReplaceShader(GrContext*,
88 const GrColorSpaceInfo& dstColorSpaceInfo,
89 const SkPaint& skPaint,
90 std::unique_ptr<GrFragmentProcessor> shaderFP,
91 GrPaint* grPaint);
92
93 /** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
94 GrOp's GrPrimitiveProcesssor. */
95 bool SkPaintToGrPaintWithXfermode(GrContext* context,
96 const GrColorSpaceInfo& dstColorSpaceInfo,
97 const SkPaint& skPaint,
98 const SkMatrix& viewM,
99 SkBlendMode primColorMode,
100 GrPaint* grPaint);
101
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. */
SkPaintToGrPaintWithPrimitiveColor(GrContext * context,const GrColorSpaceInfo & dstColorSpaceInfo,const SkPaint & skPaint,GrPaint * grPaint)106 inline bool SkPaintToGrPaintWithPrimitiveColor(GrContext* context,
107 const GrColorSpaceInfo& dstColorSpaceInfo,
108 const SkPaint& skPaint, GrPaint* grPaint) {
109 return SkPaintToGrPaintWithXfermode(context, dstColorSpaceInfo, skPaint, SkMatrix::I(),
110 SkBlendMode::kDst, grPaint);
111 }
112
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,
116 const GrColorSpaceInfo& dstColorSpaceInfo,
117 const SkPaint& paint,
118 const SkMatrix& viewM,
119 std::unique_ptr<GrFragmentProcessor> fp,
120 bool textureIsAlphaOnly,
121 GrPaint* grPaint);
122
123 ////////////////////////////////////////////////////////////////////////////////
124 // Misc Sk to Gr type conversions
125
126 GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&);
127 GrPixelConfig SkColorType2GrPixelConfig(const SkColorType);
128 GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info);
129
130 bool GrPixelConfigToColorType(GrPixelConfig, SkColorType*);
131
132 GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
133 const SkMatrix& viewM,
134 const SkMatrix& localM,
135 bool sharpenMipmappedTextures,
136 bool* doBicubic);
137
138 //////////////////////////////////////////////////////////////////////////////
139
SkVertexModeToGrPrimitiveType(SkVertices::VertexMode mode)140 static inline GrPrimitiveType SkVertexModeToGrPrimitiveType(SkVertices::VertexMode mode) {
141 switch (mode) {
142 case SkVertices::kTriangles_VertexMode:
143 return GrPrimitiveType::kTriangles;
144 case SkVertices::kTriangleStrip_VertexMode:
145 return GrPrimitiveType::kTriangleStrip;
146 case SkVertices::kTriangleFan_VertexMode:
147 break;
148 }
149 SK_ABORT("Invalid mode");
150 return GrPrimitiveType::kPoints;
151 }
152
153 //////////////////////////////////////////////////////////////////////////////
154
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);
166
167 ////////////////////////////////////////////////////////////////////////////////
168 // Texture management
169
170 /** Returns a texture representing the bitmap that is compatible with the GrSamplerState. The
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.
176 */
177 sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrContext*,
178 const SkBitmap&,
179 const GrSamplerState&,
180 SkScalar scaleAdjust[2]);
181
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
185 * yuv planes.
186 */
187 sk_sp<GrTextureProxy> GrUploadBitmapToTextureProxy(GrProxyProvider*, const SkBitmap&);
188
189 /**
190 * Creates a new texture with mipmap levels and copies the baseProxy into the base layer.
191 */
192 sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrContext*,
193 GrTextureProxy* baseProxy);
194
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);
201
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 */
206 sk_sp<GrTextureProxy> GrMakeCachedImageProxy(GrProxyProvider*, sk_sp<SkImage> srcImage,
207 SkBackingFit fit = SkBackingFit::kExact);
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. */
224 void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID,
225 SkPixelRef* pixelRef);
226
227 #endif
228