Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / include / gpu / GrTypesPriv.h
1 /*
2  * Copyright 2013 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 GrTypesPriv_DEFINED
9 #define GrTypesPriv_DEFINED
10
11 #include "GrTypes.h"
12 #include "SkTArray.h"
13
14 /**
15  * Types of shader-language-specific boxed variables we can create. (Currently only GrGLShaderVars,
16  * but should be applicable to other shader languages.)
17  */
18 enum GrSLType {
19     kVoid_GrSLType,
20     kFloat_GrSLType,
21     kVec2f_GrSLType,
22     kVec3f_GrSLType,
23     kVec4f_GrSLType,
24     kMat33f_GrSLType,
25     kMat44f_GrSLType,
26     kSampler2D_GrSLType,
27
28     kLast_GrSLType = kSampler2D_GrSLType
29 };
30 static const int kGrSLTypeCount = kLast_GrSLType + 1;
31
32 /**
33  * Gets the vector size of the SLType. Returns -1 for void, matrices, and samplers.
34  */
35 static inline int GrSLTypeVectorCount(GrSLType type) {
36     SkASSERT(type >= 0 && type < static_cast<GrSLType>(kGrSLTypeCount));
37     static const int kCounts[] = { -1, 1, 2, 3, 4, -1, -1, -1 };
38     return kCounts[type];
39
40     GR_STATIC_ASSERT(0 == kVoid_GrSLType);
41     GR_STATIC_ASSERT(1 == kFloat_GrSLType);
42     GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
43     GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
44     GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
45     GR_STATIC_ASSERT(5 == kMat33f_GrSLType);
46     GR_STATIC_ASSERT(6 == kMat44f_GrSLType);
47     GR_STATIC_ASSERT(7 == kSampler2D_GrSLType);
48     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kCounts) == kGrSLTypeCount);
49 }
50
51 /** Return the type enum for a vector of floats of length n (1..4),
52  e.g. 1 -> kFloat_GrSLType, 2 -> kVec2_GrSLType, ... */
53 static inline GrSLType GrSLFloatVectorType(int count) {
54     SkASSERT(count > 0 && count <= 4);
55     return (GrSLType)(count);
56
57     GR_STATIC_ASSERT(kFloat_GrSLType == 1);
58     GR_STATIC_ASSERT(kVec2f_GrSLType == 2);
59     GR_STATIC_ASSERT(kVec3f_GrSLType == 3);
60     GR_STATIC_ASSERT(kVec4f_GrSLType == 4);
61 }
62
63 //////////////////////////////////////////////////////////////////////////////
64
65 /**
66  * Types used to describe format of vertices in arrays.
67   */
68 enum GrVertexAttribType {
69     kFloat_GrVertexAttribType = 0,
70     kVec2f_GrVertexAttribType,
71     kVec3f_GrVertexAttribType,
72     kVec4f_GrVertexAttribType,
73
74     kUByte_GrVertexAttribType,   // unsigned byte, e.g. coverage
75     kVec4ub_GrVertexAttribType,   // vector of 4 unsigned bytes, e.g. colors
76
77     kLast_GrVertexAttribType = kVec4ub_GrVertexAttribType
78 };
79 static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1;
80
81 /**
82  * Returns the vector size of the type.
83  */
84 static inline int GrVertexAttribTypeVectorCount(GrVertexAttribType type) {
85     SkASSERT(type >= 0 && type < kGrVertexAttribTypeCount);
86     static const int kCounts[] = { 1, 2, 3, 4, 1, 4 };
87     return kCounts[type];
88
89     GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType);
90     GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType);
91     GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType);
92     GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType);
93     GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
94     GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
95     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kCounts) == kGrVertexAttribTypeCount);
96 }
97
98 /**
99  * Returns the size of the attrib type in bytes.
100  */
101 static inline size_t GrVertexAttribTypeSize(GrVertexAttribType type) {
102     SkASSERT(type >= 0 && type < kGrVertexAttribTypeCount);
103     static const size_t kSizes[] = {
104         sizeof(float),          // kFloat_GrVertexAttribType
105         2*sizeof(float),        // kVec2f_GrVertexAttribType
106         3*sizeof(float),        // kVec3f_GrVertexAttribType
107         4*sizeof(float),        // kVec4f_GrVertexAttribType
108         1*sizeof(char),         // kUByte_GrVertexAttribType
109         4*sizeof(char)          // kVec4ub_GrVertexAttribType
110     };
111     return kSizes[type];
112
113     GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType);
114     GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType);
115     GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType);
116     GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType);
117     GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
118     GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
119     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kSizes) == kGrVertexAttribTypeCount);
120 }
121
122 /**
123  * Semantic bindings for vertex attributes. kEffect means that the attribute is input to a
124  * GrProcessor. Each binding other than kEffect may not appear more than once in the current set of
125  * attributes. kPosition must be appear for exactly one attribute.
126  */
127 enum GrVertexAttribBinding {
128     kPosition_GrVertexAttribBinding,    // required, must have vector count of 2
129     kLocalCoord_GrVertexAttribBinding,  // must have vector count of 2
130     kColor_GrVertexAttribBinding,       // must have vector count of 4
131     kCoverage_GrVertexAttribBinding,    // must have a single byte
132
133     kLastFixedFunction_GrVertexAttribBinding = kCoverage_GrVertexAttribBinding,
134
135     kGeometryProcessor_GrVertexAttribBinding,      // vector length must agree with
136                                         // GrProcessor::vertexAttribType() for each effect input to
137                                         // which the attribute is mapped by GrDrawState::setEffect()
138     kLast_GrVertexAttribBinding = kGeometryProcessor_GrVertexAttribBinding
139 };
140
141 static const int kGrVertexAttribBindingCnt = kLast_GrVertexAttribBinding + 1;
142 static const int kGrFixedFunctionVertexAttribBindingCnt =
143     kLastFixedFunction_GrVertexAttribBinding + 1;
144
145 static inline int GrFixedFunctionVertexAttribVectorCount(GrVertexAttribBinding binding) {
146     SkASSERT(binding >= 0 && binding < kGrFixedFunctionVertexAttribBindingCnt);
147     static const int kVecCounts[] = { 2, 2, 4, 1 };
148
149     return kVecCounts[binding];
150
151     GR_STATIC_ASSERT(0 == kPosition_GrVertexAttribBinding);
152     GR_STATIC_ASSERT(1 == kLocalCoord_GrVertexAttribBinding);
153     GR_STATIC_ASSERT(2 == kColor_GrVertexAttribBinding);
154     GR_STATIC_ASSERT(3 == kCoverage_GrVertexAttribBinding);
155     GR_STATIC_ASSERT(kGrFixedFunctionVertexAttribBindingCnt == SK_ARRAY_COUNT(kVecCounts));
156 }
157
158 struct GrVertexAttrib {
159     inline void set(GrVertexAttribType type, size_t offset, GrVertexAttribBinding binding) {
160         fType = type;
161         fOffset = offset;
162         fBinding = binding;
163     }
164     bool operator==(const GrVertexAttrib& other) const {
165         return fType == other.fType && fOffset == other.fOffset && fBinding == other.fBinding;
166     };
167     bool operator!=(const GrVertexAttrib& other) const { return !(*this == other); }
168
169     GrVertexAttribType      fType;
170     size_t                  fOffset;
171     GrVertexAttribBinding   fBinding;
172 };
173
174 template <int N> class GrVertexAttribArray : public SkSTArray<N, GrVertexAttrib, true> {};
175
176 //////////////////////////////////////////////////////////////////////////////
177
178 /**
179 * We have coverage effects that clip rendering to the edge of some geometric primitive.
180 * This enum specifies how that clipping is performed. Not all factories that take a
181 * GrProcessorEdgeType will succeed with all values and it is up to the caller to check for
182 * a NULL return.
183 */
184 enum GrPrimitiveEdgeType {
185     kFillBW_GrProcessorEdgeType,
186     kFillAA_GrProcessorEdgeType,
187     kInverseFillBW_GrProcessorEdgeType,
188     kInverseFillAA_GrProcessorEdgeType,
189     kHairlineAA_GrProcessorEdgeType,
190
191     kLast_GrProcessorEdgeType = kHairlineAA_GrProcessorEdgeType
192 };
193
194 static const int kGrProcessorEdgeTypeCnt = kLast_GrProcessorEdgeType + 1;
195
196 static inline bool GrProcessorEdgeTypeIsFill(const GrPrimitiveEdgeType edgeType) {
197     return (kFillAA_GrProcessorEdgeType == edgeType || kFillBW_GrProcessorEdgeType == edgeType);
198 }
199
200 static inline bool GrProcessorEdgeTypeIsInverseFill(const GrPrimitiveEdgeType edgeType) {
201     return (kInverseFillAA_GrProcessorEdgeType == edgeType ||
202             kInverseFillBW_GrProcessorEdgeType == edgeType);
203 }
204
205 static inline bool GrProcessorEdgeTypeIsAA(const GrPrimitiveEdgeType edgeType) {
206     return (kFillBW_GrProcessorEdgeType != edgeType && kInverseFillBW_GrProcessorEdgeType != edgeType);
207 }
208
209 static inline GrPrimitiveEdgeType GrInvertProcessorEdgeType(const GrPrimitiveEdgeType edgeType) {
210     switch (edgeType) {
211         case kFillBW_GrProcessorEdgeType:
212             return kInverseFillBW_GrProcessorEdgeType;
213         case kFillAA_GrProcessorEdgeType:
214             return kInverseFillAA_GrProcessorEdgeType;
215         case kInverseFillBW_GrProcessorEdgeType:
216             return kFillBW_GrProcessorEdgeType;
217         case kInverseFillAA_GrProcessorEdgeType:
218             return kFillAA_GrProcessorEdgeType;
219         case kHairlineAA_GrProcessorEdgeType:
220             SkFAIL("Hairline fill isn't invertible.");
221     }
222     return kFillAA_GrProcessorEdgeType; // suppress warning.
223 }
224
225 /**
226  * Indicates the type of pending IO operations that can be recorded for gpu resources.
227  */
228 enum GrIOType {
229     kRead_GrIOType,
230     kWrite_GrIOType,
231     kRW_GrIOType
232 };
233
234 #endif