1 /*
2  * Copyright 2015 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 SkGrPriv_DEFINED
9 #define SkGrPriv_DEFINED
10 
11 #include "GrTypes.h"
12 #include "GrBlend.h"
13 #include "SkImageInfo.h"
14 #include "SkMatrix.h"
15 #include "SkXfermode.h"
16 
17 class GrCaps;
18 class GrContext;
19 class GrDrawContext;
20 class GrFragmentProcessor;
21 class GrPaint;
22 class GrTexture;
23 class GrTextureParams;
24 class GrUniqueKey;
25 class SkData;
26 class SkPaint;
27 class SkPixelRef;
28 struct SkIRect;
29 
30 /**
31  *  Our key includes the offset, width, and height so that bitmaps created by extractSubset()
32  *  are unique.
33  *
34  *  The imageID is in the shared namespace (see SkNextID::ImageID())
35  *      - SkBitmap/SkPixelRef
36  *      - SkImage
37  *      - SkImageGenerator
38  *
39  *  Note: width/height must fit in 16bits for this impl.
40  */
41 void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
42 
43 /** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be
44     removed should the bitmap's contents change or be destroyed. */
45 void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef);
46 
47 /** Converts an SkPaint to a GrPaint for a given GrContext. The matrix is required in order
48     to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
49 bool SkPaintToGrPaint(GrContext*,
50                       GrDrawContext*,
51                       const SkPaint& skPaint,
52                       const SkMatrix& viewM,
53                       GrPaint* grPaint);
54 
55 /** Same as above but ignores the SkShader (if any) on skPaint. */
56 bool SkPaintToGrPaintNoShader(GrContext* context,
57                               GrDrawContext* dc,
58                               const SkPaint& skPaint,
59                               GrPaint* grPaint);
60 
61 /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
62     should expect an unpremul input color and produce a premultiplied output color. There is
63     no primitive color. */
64 bool SkPaintToGrPaintReplaceShader(GrContext*,
65                                    GrDrawContext*,
66                                    const SkPaint& skPaint,
67                                    sk_sp<GrFragmentProcessor> shaderFP,
68                                    GrPaint* grPaint);
69 
70 /** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
71     GrBatch's GrPrimitiveProcesssor. Currently there is a bool param to indicate whether the
72     primitive color is the dst or src color to the blend in order to work around differences between
73     drawVertices and drawAtlas. */
74 bool SkPaintToGrPaintWithXfermode(GrContext* context,
75                                   GrDrawContext* dc,
76                                   const SkPaint& skPaint,
77                                   const SkMatrix& viewM,
78                                   SkXfermode::Mode primColorMode,
79                                   bool primitiveIsSrc,
80                                   GrPaint* grPaint);
81 
82 /** This is used when there is a primitive color, but the shader should be ignored. Currently,
83     the expectation is that the primitive color will be premultiplied, though it really should be
84     unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
85     applied to the primitive color after interpolation. */
SkPaintToGrPaintWithPrimitiveColor(GrContext * context,GrDrawContext * dc,const SkPaint & skPaint,GrPaint * grPaint)86 inline bool SkPaintToGrPaintWithPrimitiveColor(GrContext* context, GrDrawContext* dc,
87                                                const SkPaint& skPaint, GrPaint* grPaint) {
88     return SkPaintToGrPaintWithXfermode(context, dc, skPaint, SkMatrix::I(), SkXfermode::kDst_Mode,
89                                         false, grPaint);
90 }
91 
92 /** This is used when there may or may not be a shader, and the caller wants to plugin a texture
93     lookup.  If there is a shader, then its output will only be used if the texture is alpha8. */
94 bool SkPaintToGrPaintWithTexture(GrContext* context,
95                                  GrDrawContext* dc,
96                                  const SkPaint& paint,
97                                  const SkMatrix& viewM,
98                                  sk_sp<GrFragmentProcessor> fp,
99                                  bool textureIsAlphaOnly,
100                                  GrPaint* grPaint);
101 
102 //////////////////////////////////////////////////////////////////////////////
103 
104 GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&, const GrCaps&);
105 
106 bool GrPixelConfigToColorType(GrPixelConfig, SkColorType*);
107 
108 /** When image filter code needs to construct a draw context to do intermediate rendering, we need
109     a renderable pixel config. The source (SkSpecialImage) may not be in a renderable format, but
110     we want to preserve the color space of that source. This picks an appropriate format to use. */
111 GrPixelConfig GrRenderableConfigForColorSpace(const SkColorSpace*);
112 
113 /**
114  *  If the compressed data in the SkData is supported (as a texture format, this returns
115  *  the pixel-config that should be used, and sets outStartOfDataToUpload to the ptr into
116  *  the data where the actual raw data starts (skipping any header bytes).
117  *
118  *  If the compressed data is not supported, this returns kUnknown_GrPixelConfig, and
119  *  ignores outStartOfDataToUpload.
120  */
121 GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
122                                                  int expectedW, int expectedH,
123                                                  const void** outStartOfDataToUpload);
124 
125 
126 /**
127  * Creates a new texture for the bitmap. Does not concern itself with cache keys or texture params.
128  * The bitmap must have CPU-accessible pixels. Attempts to take advantage of faster paths for
129  * compressed textures and yuv planes.
130  */
131 GrTexture* GrUploadBitmapToTexture(GrContext*, const SkBitmap&);
132 
133 GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext*, const SkBitmap&, SkSourceGammaTreatment);
134 
135 /**
136  * Creates a new texture for the pixmap.
137  */
138 GrTexture* GrUploadPixmapToTexture(GrContext*, const SkPixmap&, SkBudgeted budgeted);
139 
140 /**
141  * Creates a new texture populated with the mipmap levels.
142  */
143 GrTexture* GrUploadMipMapToTexture(GrContext*, const SkImageInfo&, const GrMipLevel* texels,
144                                    int mipLevelCount);
145 
146 //////////////////////////////////////////////////////////////////////////////
147 
148 GR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkXfermode::kZero_Coeff);
149 GR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkXfermode::kOne_Coeff);
150 GR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkXfermode::kSC_Coeff);
151 GR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkXfermode::kISC_Coeff);
152 GR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkXfermode::kDC_Coeff);
153 GR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkXfermode::kIDC_Coeff);
154 GR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkXfermode::kSA_Coeff);
155 GR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkXfermode::kISA_Coeff);
156 GR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkXfermode::kDA_Coeff);
157 GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkXfermode::kIDA_Coeff);
158 GR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10);
159 
160 #define SkXfermodeCoeffToGrBlendCoeff(X) ((GrBlendCoeff)(X))
161 
162 #endif
163