Plumb dst color space in many places, rather than "mode"
[platform/upstream/libSkiaSharp.git] / src / gpu / SkGrPriv.h
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 "SkXfermodePriv.h"
16
17 class GrCaps;
18 class GrContext;
19 class GrRenderTargetContext;
20 class GrFragmentProcessor;
21 class GrPaint;
22 class GrTexture;
23 class GrUniqueKey;
24 class SkData;
25 class SkPaint;
26 class SkPixelRef;
27 struct SkIRect;
28
29 /**
30  *  Our key includes the offset, width, and height so that bitmaps created by extractSubset()
31  *  are unique.
32  *
33  *  The imageID is in the shared namespace (see SkNextID::ImageID())
34  *      - SkBitmap/SkPixelRef
35  *      - SkImage
36  *      - SkImageGenerator
37  *
38  *  Note: width/height must fit in 16bits for this impl.
39  */
40 void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
41
42 /** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be
43     removed should the bitmap's contents change or be destroyed. */
44 void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef);
45
46 /** Converts an SkPaint to a GrPaint for a given GrContext. The matrix is required in order
47     to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
48 bool SkPaintToGrPaint(GrContext*,
49                       GrRenderTargetContext*,
50                       const SkPaint& skPaint,
51                       const SkMatrix& viewM,
52                       GrPaint* grPaint);
53
54 /** Same as above but ignores the SkShader (if any) on skPaint. */
55 bool SkPaintToGrPaintNoShader(GrContext* context,
56                               GrRenderTargetContext* rtc,
57                               const SkPaint& skPaint,
58                               GrPaint* grPaint);
59
60 /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
61     should expect an unpremul input color and produce a premultiplied output color. There is
62     no primitive color. */
63 bool SkPaintToGrPaintReplaceShader(GrContext*,
64                                    GrRenderTargetContext*,
65                                    const SkPaint& skPaint,
66                                    sk_sp<GrFragmentProcessor> shaderFP,
67                                    GrPaint* grPaint);
68
69 /** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
70     GrOp's GrPrimitiveProcesssor. Currently there is a bool param to indicate whether the
71     primitive color is the dst or src color to the blend in order to work around differences between
72     drawVertices and drawAtlas. */
73 bool SkPaintToGrPaintWithXfermode(GrContext* context,
74                                   GrRenderTargetContext* rtc,
75                                   const SkPaint& skPaint,
76                                   const SkMatrix& viewM,
77                                   SkBlendMode primColorMode,
78                                   bool primitiveIsSrc,
79                                   GrPaint* grPaint);
80
81 /** This is used when there is a primitive color, but the shader should be ignored. Currently,
82     the expectation is that the primitive color will be premultiplied, though it really should be
83     unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
84     applied to the primitive color after interpolation. */
85 inline bool SkPaintToGrPaintWithPrimitiveColor(GrContext* context, GrRenderTargetContext* rtc,
86                                                const SkPaint& skPaint, GrPaint* grPaint) {
87     return SkPaintToGrPaintWithXfermode(context, rtc, skPaint, SkMatrix::I(), SkBlendMode::kDst,
88                                         false, grPaint);
89 }
90
91 /** This is used when there may or may not be a shader, and the caller wants to plugin a texture
92     lookup.  If there is a shader, then its output will only be used if the texture is alpha8. */
93 bool SkPaintToGrPaintWithTexture(GrContext* context,
94                                  GrRenderTargetContext* rtc,
95                                  const SkPaint& paint,
96                                  const SkMatrix& viewM,
97                                  sk_sp<GrFragmentProcessor> fp,
98                                  bool textureIsAlphaOnly,
99                                  GrPaint* grPaint);
100
101 //////////////////////////////////////////////////////////////////////////////
102
103 GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&, const GrCaps&);
104
105 bool GrPixelConfigToColorType(GrPixelConfig, SkColorType*);
106
107 /** When image filter code needs to construct a render target context to do intermediate rendering,
108     we need a renderable pixel config. The source (SkSpecialImage) may not be in a renderable
109     format, but we want to preserve the color space of that source. This picks an appropriate format
110     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&,
134                                                SkColorSpace* dstColorSpace);
135
136 /**
137  * Creates a new texture for the pixmap.
138  */
139 GrTexture* GrUploadPixmapToTexture(GrContext*, const SkPixmap&, SkBudgeted budgeted);
140
141 /**
142  * Creates a new texture populated with the mipmap levels.
143  */
144 GrTexture* GrUploadMipMapToTexture(GrContext*, const SkImageInfo&, const GrMipLevel* texels,
145                                    int mipLevelCount);
146
147 //////////////////////////////////////////////////////////////////////////////
148
149 GR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkXfermode::kZero_Coeff);
150 GR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkXfermode::kOne_Coeff);
151 GR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkXfermode::kSC_Coeff);
152 GR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkXfermode::kISC_Coeff);
153 GR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkXfermode::kDC_Coeff);
154 GR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkXfermode::kIDC_Coeff);
155 GR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkXfermode::kSA_Coeff);
156 GR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkXfermode::kISA_Coeff);
157 GR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkXfermode::kDA_Coeff);
158 GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkXfermode::kIDA_Coeff);
159 GR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10);
160
161 #define SkXfermodeCoeffToGrBlendCoeff(X) ((GrBlendCoeff)(X))
162
163 #endif