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 "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkFilterQuality.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkMatrix.h"
16 #include "include/core/SkVertices.h"
17 #include "include/gpu/GrTypes.h"
18 #include "include/private/SkColorData.h"
19 #include "src/core/SkBlendModePriv.h"
20 #include "src/gpu/GrBlend.h"
21 #include "src/gpu/GrCaps.h"
22 #include "src/gpu/GrColor.h"
23 #include "src/gpu/GrSamplerState.h"
24
25 class GrCaps;
26 class GrColorSpaceInfo;
27 class GrColorSpaceXform;
28 class GrContext;
29 class GrFragmentProcessor;
30 class GrPaint;
31 class GrRecordingContext;
32 class GrResourceProvider;
33 class GrTextureProxy;
34 class GrUniqueKey;
35 class SkBitmap;
36 class SkData;
37 class SkPaint;
38 class SkPixelRef;
39 class SkPixmap;
40 struct SkIRect;
41
42 ////////////////////////////////////////////////////////////////////////////////
43 // Color type conversions
44
SkColorToPremulGrColor(SkColor c)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
SkColorToUnpremulGrColor(SkColor c)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
62 /** Similar, but using SkPMColor4f. */
63 SkPMColor4f SkColorToPMColor4f(SkColor, const GrColorSpaceInfo&);
64
65 /** Converts an SkColor4f to the destination color space. */
66 SkColor4f SkColor4fPrepForDst(SkColor4f, const GrColorSpaceInfo&);
67
68 /** Returns true if half-floats are required to store the color in a vertex (and half-floats
69 are supported). */
SkPMColor4fNeedsWideColor(SkPMColor4f color,GrClampType clampType,const GrCaps & caps)70 static inline bool SkPMColor4fNeedsWideColor(SkPMColor4f color, GrClampType clampType,
71 const GrCaps& caps) {
72 return GrClampType::kNone == clampType &&
73 caps.halfFloatVertexAttributeSupport() &&
74 !color.fitsInBytes();
75 }
76
77 ////////////////////////////////////////////////////////////////////////////////
78 // Paint conversion
79
80 /** Converts an SkPaint to a GrPaint for a given GrRecordingContext. The matrix is required in order
81 to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
82 bool SkPaintToGrPaint(GrRecordingContext*,
83 const GrColorSpaceInfo& dstColorSpaceInfo,
84 const SkPaint& skPaint,
85 const SkMatrix& viewM,
86 GrPaint* grPaint);
87
88 /** Same as above but ignores the SkShader (if any) on skPaint. */
89 bool SkPaintToGrPaintNoShader(GrRecordingContext*,
90 const GrColorSpaceInfo& dstColorSpaceInfo,
91 const SkPaint& skPaint,
92 GrPaint* grPaint);
93
94 /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
95 should expect an unpremul input color and produce a premultiplied output color. There is
96 no primitive color. */
97 bool SkPaintToGrPaintReplaceShader(GrRecordingContext*,
98 const GrColorSpaceInfo& dstColorSpaceInfo,
99 const SkPaint& skPaint,
100 std::unique_ptr<GrFragmentProcessor> shaderFP,
101 GrPaint* grPaint);
102
103 /** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
104 GrOp's GrPrimitiveProcesssor. */
105 bool SkPaintToGrPaintWithXfermode(GrRecordingContext*,
106 const GrColorSpaceInfo& dstColorSpaceInfo,
107 const SkPaint& skPaint,
108 const SkMatrix& viewM,
109 SkBlendMode primColorMode,
110 GrPaint* grPaint);
111
112 /** This is used when there is a primitive color, but the shader should be ignored. Currently,
113 the expectation is that the primitive color will be premultiplied, though it really should be
114 unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
115 applied to the primitive color after interpolation. */
SkPaintToGrPaintWithPrimitiveColor(GrRecordingContext * context,const GrColorSpaceInfo & dstColorSpaceInfo,const SkPaint & skPaint,GrPaint * grPaint)116 inline bool SkPaintToGrPaintWithPrimitiveColor(GrRecordingContext* context,
117 const GrColorSpaceInfo& dstColorSpaceInfo,
118 const SkPaint& skPaint,
119 GrPaint* grPaint) {
120 return SkPaintToGrPaintWithXfermode(context, dstColorSpaceInfo, skPaint, SkMatrix::I(),
121 SkBlendMode::kDst, grPaint);
122 }
123
124 /** This is used when there may or may not be a shader, and the caller wants to plugin a texture
125 lookup. If there is a shader, then its output will only be used if the texture is alpha8. */
126 bool SkPaintToGrPaintWithTexture(GrRecordingContext*,
127 const GrColorSpaceInfo& dstColorSpaceInfo,
128 const SkPaint& skPaint,
129 const SkMatrix& viewM,
130 std::unique_ptr<GrFragmentProcessor> fp,
131 bool textureIsAlphaOnly,
132 GrPaint* grPaint);
133
134 ////////////////////////////////////////////////////////////////////////////////
135 // Misc Sk to Gr type conversions
136
137 GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&);
138 GrPixelConfig SkColorType2GrPixelConfig(const SkColorType);
139 GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info);
140
141 bool GrPixelConfigToColorType(GrPixelConfig, SkColorType*);
142
143 GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(int imageWidth, int imageHeight,
144 SkFilterQuality paintFilterQuality,
145 const SkMatrix& viewM,
146 const SkMatrix& localM,
147 bool sharpenMipmappedTextures,
148 bool* doBicubic);
149
150 //////////////////////////////////////////////////////////////////////////////
151
SkVertexModeToGrPrimitiveType(SkVertices::VertexMode mode)152 static inline GrPrimitiveType SkVertexModeToGrPrimitiveType(SkVertices::VertexMode mode) {
153 switch (mode) {
154 case SkVertices::kTriangles_VertexMode:
155 return GrPrimitiveType::kTriangles;
156 case SkVertices::kTriangleStrip_VertexMode:
157 return GrPrimitiveType::kTriangleStrip;
158 case SkVertices::kTriangleFan_VertexMode:
159 break;
160 }
161 SK_ABORT("Invalid mode");
162 }
163
164 //////////////////////////////////////////////////////////////////////////////
165
166 GR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkBlendModeCoeff::kZero);
167 GR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkBlendModeCoeff::kOne);
168 GR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkBlendModeCoeff::kSC);
169 GR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkBlendModeCoeff::kISC);
170 GR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkBlendModeCoeff::kDC);
171 GR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkBlendModeCoeff::kIDC);
172 GR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkBlendModeCoeff::kSA);
173 GR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkBlendModeCoeff::kISA);
174 GR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkBlendModeCoeff::kDA);
175 GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkBlendModeCoeff::kIDA);
176 //GR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10);
177
178 ////////////////////////////////////////////////////////////////////////////////
179 // Texture management
180
181 /** Returns a texture representing the bitmap that is compatible with the GrSamplerState. The
182 * texture is inserted into the cache (unless the bitmap is marked volatile) and can be
183 * retrieved again via this function.
184 * The 'scaleAdjust' in/out parameter will be updated to hold any rescaling that needs to be
185 * performed on the absolute texture coordinates (e.g., if the texture is resized out to
186 * the next power of two). It can be null if the caller is sure the bitmap won't be resized.
187 */
188 sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrRecordingContext*,
189 const SkBitmap&,
190 const GrSamplerState&,
191 SkScalar scaleAdjust[2]);
192
193 /**
194 * Creates a new texture with mipmap levels and copies the baseProxy into the base layer.
195 */
196 sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext*,
197 GrTextureProxy* baseProxy);
198
199 /*
200 * Create a texture proxy from the provided bitmap by wrapping it in an image and calling
201 * GrMakeCachedImageProxy.
202 */
203 sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrProxyProvider*, const SkBitmap& bitmap,
204 SkBackingFit fit = SkBackingFit::kExact);
205
206 /*
207 * Create a texture proxy from the provided 'srcImage' and add it to the texture cache
208 * using the key also extracted from 'srcImage'.
209 */
210 sk_sp<GrTextureProxy> GrMakeCachedImageProxy(GrProxyProvider*, sk_sp<SkImage> srcImage,
211 SkBackingFit fit = SkBackingFit::kExact);
212
213 /**
214 * Our key includes the offset, width, and height so that bitmaps created by extractSubset()
215 * are unique.
216 *
217 * The imageID is in the shared namespace (see SkNextID::ImageID())
218 * - SkBitmap/SkPixelRef
219 * - SkImage
220 * - SkImageGenerator
221 *
222 * Note: width/height must fit in 16bits for this impl.
223 */
224 void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
225
226 /** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be
227 removed should the bitmap's contents change or be destroyed. */
228 void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextID,
229 SkPixelRef* pixelRef);
230
231 #endif
232