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 "GrSamplerParams.h"
14 #include "GrTypes.h"
15 #include "SkCanvas.h"
16 #include "SkColor.h"
17 #include "SkColorPriv.h"
18 #include "SkFilterQuality.h"
19 #include "SkImageInfo.h"
20 #include "SkMatrix.h"
21 #include "SkPM4f.h"
22 #include "SkVertices.h"
23 #include "SkBlendModePriv.h"
24
25 class GrCaps;
26 class GrColorSpaceXform;
27 class GrContext;
28 class GrRenderTargetContext;
29 class GrFragmentProcessor;
30 class GrPaint;
31 class GrResourceProvider;
32 class GrTextureProxy;
33 class GrUniqueKey;
34 class SkBitmap;
35 class SkData;
36 class SkPaint;
37 class SkPixelRef;
38 class SkPixmap;
39 struct SkIRect;
40
41 ////////////////////////////////////////////////////////////////////////////////
42 // Color type conversions
43
SkColorToPremulGrColor(SkColor c)44 static 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
SkColorToUnpremulGrColor(SkColor c)53 static 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
61 /** Transform an SkColor (sRGB bytes) to GrColor4f for the specified color space. */
62 GrColor4f SkColorToPremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace);
63 GrColor4f SkColorToUnpremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace);
64
65 /**
66 * As above, but with a caller-supplied color space xform object. Faster for the cases where we
67 * have that cached.
68 */
69 GrColor4f SkColorToPremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace,
70 GrColorSpaceXform* gamutXform);
71 GrColor4f SkColorToUnpremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace,
72 GrColorSpaceXform* gamutXform);
73
74 /** Replicates the SkColor's alpha to all four channels of the GrColor. */
SkColorAlphaToGrColor(SkColor c)75 static inline GrColor SkColorAlphaToGrColor(SkColor c) {
76 U8CPU a = SkColorGetA(c);
77 return GrColorPackRGBA(a, a, a, a);
78 }
79
80 //////////////////////////////////////////////////////////////////////////////
81
GrColor4fToSkPM4f(const GrColor4f & c)82 static inline SkPM4f GrColor4fToSkPM4f(const GrColor4f& c) {
83 SkPM4f pm4f;
84 pm4f.fVec[SkPM4f::R] = c.fRGBA[0];
85 pm4f.fVec[SkPM4f::G] = c.fRGBA[1];
86 pm4f.fVec[SkPM4f::B] = c.fRGBA[2];
87 pm4f.fVec[SkPM4f::A] = c.fRGBA[3];
88 return pm4f;
89 }
90
SkPM4fToGrColor4f(const SkPM4f & c)91 static inline GrColor4f SkPM4fToGrColor4f(const SkPM4f& c) {
92 return GrColor4f{c.r(), c.g(), c.b(), c.a()};
93 }
94
95 ////////////////////////////////////////////////////////////////////////////////
96 // Paint conversion
97
98 /** Converts an SkPaint to a GrPaint for a given GrContext. The matrix is required in order
99 to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
100 bool SkPaintToGrPaint(GrContext*,
101 GrRenderTargetContext*,
102 const SkPaint& skPaint,
103 const SkMatrix& viewM,
104 GrPaint* grPaint);
105
106 /** Same as above but ignores the SkShader (if any) on skPaint. */
107 bool SkPaintToGrPaintNoShader(GrContext* context,
108 GrRenderTargetContext* rtc,
109 const SkPaint& skPaint,
110 GrPaint* grPaint);
111
112 /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
113 should expect an unpremul input color and produce a premultiplied output color. There is
114 no primitive color. */
115 bool SkPaintToGrPaintReplaceShader(GrContext*,
116 GrRenderTargetContext*,
117 const SkPaint& skPaint,
118 sk_sp<GrFragmentProcessor> shaderFP,
119 GrPaint* grPaint);
120
121 /** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
122 GrOp's GrPrimitiveProcesssor. */
123 bool SkPaintToGrPaintWithXfermode(GrContext* context,
124 GrRenderTargetContext* rtc,
125 const SkPaint& skPaint,
126 const SkMatrix& viewM,
127 SkBlendMode primColorMode,
128 GrPaint* grPaint);
129
130 /** This is used when there is a primitive color, but the shader should be ignored. Currently,
131 the expectation is that the primitive color will be premultiplied, though it really should be
132 unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
133 applied to the primitive color after interpolation. */
SkPaintToGrPaintWithPrimitiveColor(GrContext * context,GrRenderTargetContext * rtc,const SkPaint & skPaint,GrPaint * grPaint)134 inline bool SkPaintToGrPaintWithPrimitiveColor(GrContext* context, GrRenderTargetContext* rtc,
135 const SkPaint& skPaint, GrPaint* grPaint) {
136 return SkPaintToGrPaintWithXfermode(context, rtc, skPaint, SkMatrix::I(), SkBlendMode::kDst,
137 grPaint);
138 }
139
140 /** This is used when there may or may not be a shader, and the caller wants to plugin a texture
141 lookup. If there is a shader, then its output will only be used if the texture is alpha8. */
142 bool SkPaintToGrPaintWithTexture(GrContext* context,
143 GrRenderTargetContext* rtc,
144 const SkPaint& paint,
145 const SkMatrix& viewM,
146 sk_sp<GrFragmentProcessor> fp,
147 bool textureIsAlphaOnly,
148 GrPaint* grPaint);
149
150 ////////////////////////////////////////////////////////////////////////////////
151 // Misc Sk to Gr type conversions
152
153 GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&, const GrCaps&);
154 GrPixelConfig SkImageInfo2GrPixelConfig(const SkColorType, SkColorSpace*, const GrCaps& caps);
155 GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info, const GrCaps& caps);
156
157 bool GrPixelConfigToColorType(GrPixelConfig, SkColorType*);
158
159 GrSamplerParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
160 const SkMatrix& viewM,
161 const SkMatrix& localM,
162 bool* doBicubic);
163
164 //////////////////////////////////////////////////////////////////////////////
165
SkVertexModeToGrPrimitiveType(SkVertices::VertexMode mode)166 static inline GrPrimitiveType SkVertexModeToGrPrimitiveType(SkVertices::VertexMode mode) {
167 switch (mode) {
168 case SkVertices::kTriangles_VertexMode:
169 return GrPrimitiveType::kTriangles;
170 case SkVertices::kTriangleStrip_VertexMode:
171 return GrPrimitiveType::kTriangleStrip;
172 case SkVertices::kTriangleFan_VertexMode:
173 return GrPrimitiveType::kTriangleFan;
174 }
175 SkFAIL("Invalid mode");
176 return GrPrimitiveType::kPoints;
177 }
178
179 //////////////////////////////////////////////////////////////////////////////
180
181 GR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkBlendModeCoeff::kZero);
182 GR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkBlendModeCoeff::kOne);
183 GR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkBlendModeCoeff::kSC);
184 GR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkBlendModeCoeff::kISC);
185 GR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkBlendModeCoeff::kDC);
186 GR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkBlendModeCoeff::kIDC);
187 GR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkBlendModeCoeff::kSA);
188 GR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkBlendModeCoeff::kISA);
189 GR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkBlendModeCoeff::kDA);
190 GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkBlendModeCoeff::kIDA);
191 //GR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10);
192
193 #define SkXfermodeCoeffToGrBlendCoeff(X) ((GrBlendCoeff)(X))
194
195 ////////////////////////////////////////////////////////////////////////////////
196 // Texture management
197
198 /** Returns a texture representing the bitmap that is compatible with the GrSamplerParams. The
199 * texture is inserted into the cache (unless the bitmap is marked volatile) and can be
200 * retrieved again via this function.
201 * The 'scaleAdjust' in/out parameter will be updated to hold any rescaling that needs to be
202 * performed on the absolute texture coordinates (e.g., if the texture is resized out to
203 * the next power of two). It can be null if the caller is sure the bitmap won't be resized.
204 */
205 sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrContext*,
206 const SkBitmap&,
207 const GrSamplerParams&,
208 SkScalar scaleAdjust[2]);
209
210 /**
211 * Creates a new texture for the bitmap. Does not concern itself with cache keys or texture params.
212 * The bitmap must have CPU-accessible pixels. Attempts to take advantage of faster paths for
213 * yuv planes.
214 */
215 sk_sp<GrTextureProxy> GrUploadBitmapToTextureProxy(GrResourceProvider*, const SkBitmap&,
216 SkColorSpace* dstColorSpace);
217
218 sk_sp<GrTextureProxy> GrGenerateMipMapsAndUploadToTextureProxy(GrContext*, const SkBitmap&,
219 SkColorSpace* dstColorSpace);
220
221 /**
222 * Creates a new texture for the pixmap.
223 */
224 sk_sp<GrTextureProxy> GrUploadPixmapToTextureProxy(GrResourceProvider*,
225 const SkPixmap&, SkBudgeted, SkColorSpace*);
226
227 /**
228 * Creates a new texture populated with the mipmap levels.
229 */
230 sk_sp<GrTextureProxy> GrUploadMipMapToTextureProxy(GrContext*, const SkImageInfo&,
231 const GrMipLevel texels[],
232 int mipLevelCount,
233 SkDestinationSurfaceColorMode colorMode);
234
235 // This is intended to replace:
236 // SkAutoLockPixels alp(bitmap, true);
237 // if (!bitmap.readyToDraw()) {
238 // return nullptr;
239 // }
240 // sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(fContext.get(), bitmap,
241 // GrSamplerParams::ClampNoFilter(),
242 // nullptr);
243 // if (!texture) {
244 // return nullptr;
245 // }
246 sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrResourceProvider*, const SkBitmap& bitmap);
247
248
249 /**
250 * Our key includes the offset, width, and height so that bitmaps created by extractSubset()
251 * are unique.
252 *
253 * The imageID is in the shared namespace (see SkNextID::ImageID())
254 * - SkBitmap/SkPixelRef
255 * - SkImage
256 * - SkImageGenerator
257 *
258 * Note: width/height must fit in 16bits for this impl.
259 */
260 void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
261
262 /** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be
263 removed should the bitmap's contents change or be destroyed. */
264 void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef);
265
266 //////////////////////////////////////////////////////////////////////////////
267
268 /** When image filter code needs to construct a render target context to do intermediate rendering,
269 we need a renderable pixel config. The source (SkSpecialImage) may not be in a renderable
270 format, but we want to preserve the color space of that source. This picks an appropriate format
271 to use. */
272 GrPixelConfig GrRenderableConfigForColorSpace(const SkColorSpace*);
273
274 #endif
275