• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GrColorInfo;
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 GrColorInfo&);
64 
65 /** Converts an SkColor4f to the destination color space. */
66 SkColor4f SkColor4fPrepForDst(SkColor4f, const GrColorInfo&);
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 // Paint conversion
70 
71 /** Converts an SkPaint to a GrPaint for a given GrRecordingContext. The matrix is required in order
72     to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
73 bool SkPaintToGrPaint(GrRecordingContext*,
74                       const GrColorInfo& dstColorInfo,
75                       const SkPaint& skPaint,
76                       const SkMatrix& viewM,
77                       GrPaint* grPaint);
78 
79 /** Same as above but ignores the SkShader (if any) on skPaint. */
80 bool SkPaintToGrPaintNoShader(GrRecordingContext*,
81                               const GrColorInfo& dstColorInfo,
82                               const SkPaint& skPaint,
83                               GrPaint* grPaint);
84 
85 /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
86     should expect an unpremul input color and produce a premultiplied output color. There is
87     no primitive color. */
88 bool SkPaintToGrPaintReplaceShader(GrRecordingContext*,
89                                    const GrColorInfo& dstColorInfo,
90                                    const SkPaint& skPaint,
91                                    std::unique_ptr<GrFragmentProcessor> shaderFP,
92                                    GrPaint* grPaint);
93 
94 /** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
95     GrOp's GrPrimitiveProcesssor. */
96 bool SkPaintToGrPaintWithXfermode(GrRecordingContext*,
97                                   const GrColorInfo& dstColorInfo,
98                                   const SkPaint& skPaint,
99                                   const SkMatrix& viewM,
100                                   SkBlendMode primColorMode,
101                                   GrPaint* grPaint);
102 
103 /** This is used when there is a primitive color, but the shader should be ignored. Currently,
104     the expectation is that the primitive color will be premultiplied, though it really should be
105     unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
106     applied to the primitive color after interpolation. */
SkPaintToGrPaintWithPrimitiveColor(GrRecordingContext * context,const GrColorInfo & dstColorInfo,const SkPaint & skPaint,GrPaint * grPaint)107 inline bool SkPaintToGrPaintWithPrimitiveColor(GrRecordingContext* context,
108                                                const GrColorInfo& dstColorInfo,
109                                                const SkPaint& skPaint,
110                                                GrPaint* grPaint) {
111     return SkPaintToGrPaintWithXfermode(context, dstColorInfo, skPaint, SkMatrix::I(),
112                                         SkBlendMode::kDst, grPaint);
113 }
114 
115 /** This is used when there may or may not be a shader, and the caller wants to plugin a texture
116     lookup.  If there is a shader, then its output will only be used if the texture is alpha8. */
117 bool SkPaintToGrPaintWithTexture(GrRecordingContext*,
118                                  const GrColorInfo& dstColorInfo,
119                                  const SkPaint& skPaint,
120                                  const SkMatrix& viewM,
121                                  std::unique_ptr<GrFragmentProcessor> fp,
122                                  bool textureIsAlphaOnly,
123                                  GrPaint* grPaint);
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 // Misc Sk to Gr type conversions
127 
128 GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(int imageWidth, int imageHeight,
129                                                        SkFilterQuality paintFilterQuality,
130                                                        const SkMatrix& viewM,
131                                                        const SkMatrix& localM,
132                                                        bool sharpenMipmappedTextures,
133                                                        bool* doBicubic);
134 
135 //////////////////////////////////////////////////////////////////////////////
136 
SkVertexModeToGrPrimitiveType(SkVertices::VertexMode mode)137 static inline GrPrimitiveType SkVertexModeToGrPrimitiveType(SkVertices::VertexMode mode) {
138     switch (mode) {
139         case SkVertices::kTriangles_VertexMode:
140             return GrPrimitiveType::kTriangles;
141         case SkVertices::kTriangleStrip_VertexMode:
142             return GrPrimitiveType::kTriangleStrip;
143         case SkVertices::kTriangleFan_VertexMode:
144             break;
145     }
146     SK_ABORT("Invalid mode");
147 }
148 
149 //////////////////////////////////////////////////////////////////////////////
150 
151 static_assert((int)kZero_GrBlendCoeff == (int)SkBlendModeCoeff::kZero);
152 static_assert((int)kOne_GrBlendCoeff == (int)SkBlendModeCoeff::kOne);
153 static_assert((int)kSC_GrBlendCoeff == (int)SkBlendModeCoeff::kSC);
154 static_assert((int)kISC_GrBlendCoeff == (int)SkBlendModeCoeff::kISC);
155 static_assert((int)kDC_GrBlendCoeff == (int)SkBlendModeCoeff::kDC);
156 static_assert((int)kIDC_GrBlendCoeff == (int)SkBlendModeCoeff::kIDC);
157 static_assert((int)kSA_GrBlendCoeff == (int)SkBlendModeCoeff::kSA);
158 static_assert((int)kISA_GrBlendCoeff == (int)SkBlendModeCoeff::kISA);
159 static_assert((int)kDA_GrBlendCoeff == (int)SkBlendModeCoeff::kDA);
160 static_assert((int)kIDA_GrBlendCoeff == (int)SkBlendModeCoeff::kIDA);
161 // static_assert(SkXfermode::kCoeffCount == 10);
162 
163 ////////////////////////////////////////////////////////////////////////////////
164 // Texture management
165 
166 /** Returns a view that wraps a texture representing the bitmap that is compatible with the
167  *  GrSamplerState. The texture is inserted into the cache (unless the bitmap is marked volatile)
168  *  and can be retrieved again via this function.
169  *  The 'scaleAdjust' in/out parameter will be updated to hold any rescaling that needs to be
170  *  performed on the absolute texture coordinates (e.g., if the texture is resized out to
171  *  the next power of two). It can be null if the caller is sure the bitmap won't be resized.
172  */
173 GrSurfaceProxyView GrRefCachedBitmapView(GrRecordingContext*, const SkBitmap&, GrSamplerState,
174                                          SkScalar scaleAdjust[2]);
175 
176 /**
177  * Creates a new texture with mipmap levels and copies the baseProxy into the base layer.
178  */
179 GrSurfaceProxyView GrCopyBaseMipMapToTextureProxy(GrRecordingContext*,
180                                                   GrSurfaceProxy* baseProxy,
181                                                   GrSurfaceOrigin origin,
182                                                   GrColorType srcColorType);
183 
184 /*
185  * Create a texture proxy from the provided bitmap and add it to the texture cache
186  * using the key also extracted from 'bitmp'.
187  */
188 GrSurfaceProxyView GrMakeCachedBitmapProxyView(GrRecordingContext*, const SkBitmap& bitmap,
189                                                SkBackingFit fit = SkBackingFit::kExact);
190 
191 /**
192  *  Our key includes the offset, width, and height so that bitmaps created by extractSubset()
193  *  are unique.
194  *
195  *  The imageID is in the shared namespace (see SkNextID::ImageID())
196  *      - SkBitmap/SkPixelRef
197  *      - SkImage
198  *      - SkImageGenerator
199  *
200  *  Note: width/height must fit in 16bits for this impl.
201  */
202 void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
203 
204 /** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be
205     removed should the bitmap's contents change or be destroyed. */
206 void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextID,
207                                          SkPixelRef* pixelRef);
208 
209 #endif
210