Line data Source code
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 "SkXfermodePriv.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 :
44 0 : static inline GrColor SkColorToPremulGrColor(SkColor c) {
45 0 : SkPMColor pm = SkPreMultiplyColor(c);
46 0 : unsigned r = SkGetPackedR32(pm);
47 0 : unsigned g = SkGetPackedG32(pm);
48 0 : unsigned b = SkGetPackedB32(pm);
49 0 : unsigned a = SkGetPackedA32(pm);
50 0 : return GrColorPackRGBA(r, g, b, a);
51 : }
52 :
53 0 : static inline GrColor SkColorToUnpremulGrColor(SkColor c) {
54 0 : unsigned r = SkColorGetR(c);
55 0 : unsigned g = SkColorGetG(c);
56 0 : unsigned b = SkColorGetB(c);
57 0 : unsigned a = SkColorGetA(c);
58 0 : 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. */
75 0 : static inline GrColor SkColorAlphaToGrColor(SkColor c) {
76 0 : U8CPU a = SkColorGetA(c);
77 0 : return GrColorPackRGBA(a, a, a, a);
78 : }
79 :
80 : //////////////////////////////////////////////////////////////////////////////
81 :
82 0 : static inline SkPM4f GrColor4fToSkPM4f(const GrColor4f& c) {
83 : SkPM4f pm4f;
84 0 : pm4f.fVec[SkPM4f::R] = c.fRGBA[0];
85 0 : pm4f.fVec[SkPM4f::G] = c.fRGBA[1];
86 0 : pm4f.fVec[SkPM4f::B] = c.fRGBA[2];
87 0 : pm4f.fVec[SkPM4f::A] = c.fRGBA[3];
88 0 : return pm4f;
89 : }
90 :
91 0 : static inline GrColor4f SkPM4fToGrColor4f(const SkPM4f& c) {
92 0 : 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. Currently there is a bool param to indicate whether the
123 : primitive color is the dst or src color to the blend in order to work around differences between
124 : drawVertices and drawAtlas. */
125 : bool SkPaintToGrPaintWithXfermode(GrContext* context,
126 : GrRenderTargetContext* rtc,
127 : const SkPaint& skPaint,
128 : const SkMatrix& viewM,
129 : SkBlendMode primColorMode,
130 : bool primitiveIsSrc,
131 : GrPaint* grPaint);
132 :
133 : /** This is used when there is a primitive color, but the shader should be ignored. Currently,
134 : the expectation is that the primitive color will be premultiplied, though it really should be
135 : unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
136 : applied to the primitive color after interpolation. */
137 0 : inline bool SkPaintToGrPaintWithPrimitiveColor(GrContext* context, GrRenderTargetContext* rtc,
138 : const SkPaint& skPaint, GrPaint* grPaint) {
139 0 : return SkPaintToGrPaintWithXfermode(context, rtc, skPaint, SkMatrix::I(), SkBlendMode::kDst,
140 0 : false, grPaint);
141 : }
142 :
143 : /** This is used when there may or may not be a shader, and the caller wants to plugin a texture
144 : lookup. If there is a shader, then its output will only be used if the texture is alpha8. */
145 : bool SkPaintToGrPaintWithTexture(GrContext* context,
146 : GrRenderTargetContext* rtc,
147 : const SkPaint& paint,
148 : const SkMatrix& viewM,
149 : sk_sp<GrFragmentProcessor> fp,
150 : bool textureIsAlphaOnly,
151 : GrPaint* grPaint);
152 :
153 : ////////////////////////////////////////////////////////////////////////////////
154 : // Misc Sk to Gr type conversions
155 :
156 : GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&, const GrCaps&);
157 : GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info, const GrCaps& caps);
158 :
159 : bool GrPixelConfigToColorType(GrPixelConfig, SkColorType*);
160 :
161 : GrSamplerParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
162 : const SkMatrix& viewM,
163 : const SkMatrix& localM,
164 : bool* doBicubic);
165 :
166 : //////////////////////////////////////////////////////////////////////////////
167 :
168 0 : static inline GrPrimitiveType SkVertexModeToGrPrimitiveType(SkVertices::VertexMode mode) {
169 0 : switch (mode) {
170 : case SkVertices::kTriangles_VertexMode:
171 0 : return kTriangles_GrPrimitiveType;
172 : case SkVertices::kTriangleStrip_VertexMode:
173 0 : return kTriangleStrip_GrPrimitiveType;
174 : case SkVertices::kTriangleFan_VertexMode:
175 0 : return kTriangleFan_GrPrimitiveType;
176 : }
177 0 : SkFAIL("Invalid mode");
178 0 : return kPoints_GrPrimitiveType;
179 : }
180 :
181 : //////////////////////////////////////////////////////////////////////////////
182 :
183 : GR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkXfermode::kZero_Coeff);
184 : GR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkXfermode::kOne_Coeff);
185 : GR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkXfermode::kSC_Coeff);
186 : GR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkXfermode::kISC_Coeff);
187 : GR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkXfermode::kDC_Coeff);
188 : GR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkXfermode::kIDC_Coeff);
189 : GR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkXfermode::kSA_Coeff);
190 : GR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkXfermode::kISA_Coeff);
191 : GR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkXfermode::kDA_Coeff);
192 : GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkXfermode::kIDA_Coeff);
193 : GR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10);
194 :
195 : #define SkXfermodeCoeffToGrBlendCoeff(X) ((GrBlendCoeff)(X))
196 :
197 : ////////////////////////////////////////////////////////////////////////////////
198 : // Texture management
199 :
200 : /** Returns a texture representing the bitmap that is compatible with the GrSamplerParams. The
201 : * texture is inserted into the cache (unless the bitmap is marked volatile) and can be
202 : * retrieved again via this function.
203 : * The 'scaleAdjust' in/out parameter will be updated to hold any rescaling that needs to be
204 : * performed on the absolute texture coordinates (e.g., if the texture is resized out to
205 : * the next power of two). It can be null if the caller is sure the bitmap won't be resized.
206 : */
207 : sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrContext*,
208 : const SkBitmap&,
209 : const GrSamplerParams&,
210 : SkScalar scaleAdjust[2]);
211 :
212 : /**
213 : * Creates a new texture for the bitmap. Does not concern itself with cache keys or texture params.
214 : * The bitmap must have CPU-accessible pixels. Attempts to take advantage of faster paths for
215 : * compressed textures and yuv planes.
216 : */
217 : sk_sp<GrTextureProxy> GrUploadBitmapToTextureProxy(GrResourceProvider*, const SkBitmap&);
218 :
219 : sk_sp<GrTextureProxy> GrGenerateMipMapsAndUploadToTextureProxy(GrContext*, const SkBitmap&,
220 : SkColorSpace* dstColorSpace);
221 :
222 : /**
223 : * Creates a new texture for the pixmap.
224 : */
225 : sk_sp<GrTextureProxy> GrUploadPixmapToTextureProxy(GrResourceProvider*,
226 : const SkPixmap&, SkBudgeted);
227 : sk_sp<GrTextureProxy> GrUploadPixmapToTextureProxyNoCheck(GrResourceProvider*,
228 : const SkPixmap&, SkBudgeted);
229 :
230 : /**
231 : * Creates a new texture populated with the mipmap levels.
232 : */
233 : sk_sp<GrTextureProxy> GrUploadMipMapToTextureProxy(GrContext*, const SkImageInfo&,
234 : const GrMipLevel* texels,
235 : int mipLevelCount,
236 : SkDestinationSurfaceColorMode colorMode);
237 :
238 : // This is intended to replace:
239 : // SkAutoLockPixels alp(bitmap, true);
240 : // if (!bitmap.readyToDraw()) {
241 : // return nullptr;
242 : // }
243 : // sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(fContext.get(), bitmap,
244 : // GrSamplerParams::ClampNoFilter(),
245 : // nullptr);
246 : // if (!texture) {
247 : // return nullptr;
248 : // }
249 : sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrResourceProvider*, const SkBitmap& bitmap);
250 :
251 :
252 : /**
253 : * Our key includes the offset, width, and height so that bitmaps created by extractSubset()
254 : * are unique.
255 : *
256 : * The imageID is in the shared namespace (see SkNextID::ImageID())
257 : * - SkBitmap/SkPixelRef
258 : * - SkImage
259 : * - SkImageGenerator
260 : *
261 : * Note: width/height must fit in 16bits for this impl.
262 : */
263 : void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
264 :
265 : /** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be
266 : removed should the bitmap's contents change or be destroyed. */
267 : void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef);
268 :
269 : //////////////////////////////////////////////////////////////////////////////
270 :
271 : /** When image filter code needs to construct a render target context to do intermediate rendering,
272 : we need a renderable pixel config. The source (SkSpecialImage) may not be in a renderable
273 : format, but we want to preserve the color space of that source. This picks an appropriate format
274 : to use. */
275 : GrPixelConfig GrRenderableConfigForColorSpace(const SkColorSpace*);
276 :
277 : /**
278 : * If the compressed data in the SkData is supported (as a texture format, this returns
279 : * the pixel-config that should be used, and sets outStartOfDataToUpload to the ptr into
280 : * the data where the actual raw data starts (skipping any header bytes).
281 : *
282 : * If the compressed data is not supported, this returns kUnknown_GrPixelConfig, and
283 : * ignores outStartOfDataToUpload.
284 : */
285 : GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
286 : int expectedW, int expectedH,
287 : const void** outStartOfDataToUpload);
288 :
289 :
290 :
291 : #endif
|