Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 "SkRect.h"
13 #include "SkRefCnt.h"
14 
19 enum GrSLType {
20  kVoid_GrSLType,
21  kFloat_GrSLType,
22  kVec2f_GrSLType,
23  kVec3f_GrSLType,
24  kVec4f_GrSLType,
25  kMat22f_GrSLType,
26  kMat33f_GrSLType,
27  kMat44f_GrSLType,
28  kTexture2DSampler_GrSLType,
29  kTextureExternalSampler_GrSLType,
30  kTexture2DRectSampler_GrSLType,
31  kTextureBufferSampler_GrSLType,
32  kBool_GrSLType,
33  kInt_GrSLType,
34  kUint_GrSLType,
35  kTexture2D_GrSLType,
36  kSampler_GrSLType,
37 
38  kLast_GrSLType = kSampler_GrSLType
39 };
40 static const int kGrSLTypeCount = kLast_GrSLType + 1;
41 
42 enum GrShaderType {
43  kVertex_GrShaderType,
44  kGeometry_GrShaderType,
45  kFragment_GrShaderType,
46 
47  kLastkFragment_GrShaderType = kFragment_GrShaderType
48 };
49 static const int kGrShaderTypeCount = kLastkFragment_GrShaderType + 1;
50 
51 enum GrShaderFlags {
52  kNone_GrShaderFlags = 0,
53  kVertex_GrShaderFlag = 1 << kVertex_GrShaderType,
54  kGeometry_GrShaderFlag = 1 << kGeometry_GrShaderType,
55  kFragment_GrShaderFlag = 1 << kFragment_GrShaderType
56 };
57 GR_MAKE_BITFIELD_OPS(GrShaderFlags);
58 
59 enum class GrDrawFace {
60  kInvalid = -1,
61 
62  kBoth,
63  kCCW,
64  kCW,
65 };
66 
72 enum GrSLPrecision {
73  kLow_GrSLPrecision,
74  kMedium_GrSLPrecision,
75  kHigh_GrSLPrecision,
76 
77  // Default precision is medium. This is because on OpenGL ES 2 highp support is not
78  // guaranteed. On (non-ES) OpenGL the specifiers have no effect on precision.
79  kDefault_GrSLPrecision = kMedium_GrSLPrecision,
80 
81  kLast_GrSLPrecision = kHigh_GrSLPrecision
82 };
83 
84 static const int kGrSLPrecisionCount = kLast_GrSLPrecision + 1;
85 
89 static inline int GrSLTypeVectorCount(GrSLType type) {
90  SkASSERT(type >= 0 && type < static_cast<GrSLType>(kGrSLTypeCount));
91  static const int kCounts[] = { -1, 1, 2, 3, 4, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1 };
92  return kCounts[type];
93 
94  GR_STATIC_ASSERT(0 == kVoid_GrSLType);
95  GR_STATIC_ASSERT(1 == kFloat_GrSLType);
96  GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
97  GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
98  GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
99  GR_STATIC_ASSERT(5 == kMat22f_GrSLType);
100  GR_STATIC_ASSERT(6 == kMat33f_GrSLType);
101  GR_STATIC_ASSERT(7 == kMat44f_GrSLType);
102  GR_STATIC_ASSERT(8 == kTexture2DSampler_GrSLType);
103  GR_STATIC_ASSERT(9 == kTextureExternalSampler_GrSLType);
104  GR_STATIC_ASSERT(10 == kTexture2DRectSampler_GrSLType);
105  GR_STATIC_ASSERT(11 == kTextureBufferSampler_GrSLType);
106  GR_STATIC_ASSERT(12 == kBool_GrSLType);
107  GR_STATIC_ASSERT(13 == kInt_GrSLType);
108  GR_STATIC_ASSERT(14 == kUint_GrSLType);
109  GR_STATIC_ASSERT(15 == kTexture2D_GrSLType);
110  GR_STATIC_ASSERT(16 == kSampler_GrSLType);
111  GR_STATIC_ASSERT(SK_ARRAY_COUNT(kCounts) == kGrSLTypeCount);
112 }
113 
116 static inline GrSLType GrSLFloatVectorType(int count) {
117  SkASSERT(count > 0 && count <= 4);
118  return (GrSLType)(count);
119 
120  GR_STATIC_ASSERT(kFloat_GrSLType == 1);
121  GR_STATIC_ASSERT(kVec2f_GrSLType == 2);
122  GR_STATIC_ASSERT(kVec3f_GrSLType == 3);
123  GR_STATIC_ASSERT(kVec4f_GrSLType == 4);
124 }
125 
127 static inline bool GrSLTypeIsFloatType(GrSLType type) {
128  SkASSERT(type >= 0 && type < static_cast<GrSLType>(kGrSLTypeCount));
129  return type >= kFloat_GrSLType && type <= kMat44f_GrSLType;
130 
131  GR_STATIC_ASSERT(0 == kVoid_GrSLType);
132  GR_STATIC_ASSERT(1 == kFloat_GrSLType);
133  GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
134  GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
135  GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
136  GR_STATIC_ASSERT(5 == kMat22f_GrSLType);
137  GR_STATIC_ASSERT(6 == kMat33f_GrSLType);
138  GR_STATIC_ASSERT(7 == kMat44f_GrSLType);
139  GR_STATIC_ASSERT(8 == kTexture2DSampler_GrSLType);
140  GR_STATIC_ASSERT(9 == kTextureExternalSampler_GrSLType);
141  GR_STATIC_ASSERT(10 == kTexture2DRectSampler_GrSLType);
142  GR_STATIC_ASSERT(11 == kTextureBufferSampler_GrSLType);
143  GR_STATIC_ASSERT(12 == kBool_GrSLType);
144  GR_STATIC_ASSERT(13 == kInt_GrSLType);
145  GR_STATIC_ASSERT(14 == kUint_GrSLType);
146  GR_STATIC_ASSERT(15 == kTexture2D_GrSLType);
147  GR_STATIC_ASSERT(16 == kSampler_GrSLType);
148  GR_STATIC_ASSERT(17 == kGrSLTypeCount);
149 }
150 
152 static inline bool GrSLTypeIsIntType(GrSLType type) {
153  SkASSERT(type >= 0 && type < static_cast<GrSLType>(kGrSLTypeCount));
154  return type >= kInt_GrSLType && type <= kUint_GrSLType;
155 
156  GR_STATIC_ASSERT(0 == kVoid_GrSLType);
157  GR_STATIC_ASSERT(1 == kFloat_GrSLType);
158  GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
159  GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
160  GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
161  GR_STATIC_ASSERT(5 == kMat22f_GrSLType);
162  GR_STATIC_ASSERT(6 == kMat33f_GrSLType);
163  GR_STATIC_ASSERT(7 == kMat44f_GrSLType);
164  GR_STATIC_ASSERT(8 == kTexture2DSampler_GrSLType);
165  GR_STATIC_ASSERT(9 == kTextureExternalSampler_GrSLType);
166  GR_STATIC_ASSERT(10 == kTexture2DRectSampler_GrSLType);
167  GR_STATIC_ASSERT(11 == kTextureBufferSampler_GrSLType);
168  GR_STATIC_ASSERT(12 == kBool_GrSLType);
169  GR_STATIC_ASSERT(13 == kInt_GrSLType);
170  GR_STATIC_ASSERT(14 == kUint_GrSLType);
171  GR_STATIC_ASSERT(15 == kTexture2D_GrSLType);
172  GR_STATIC_ASSERT(16 == kSampler_GrSLType);
173  GR_STATIC_ASSERT(17 == kGrSLTypeCount);
174 }
175 
177 static inline bool GrSLTypeIsNumeric(GrSLType type) {
178  return GrSLTypeIsFloatType(type) || GrSLTypeIsIntType(type);
179 }
180 
182 static inline size_t GrSLTypeSize(GrSLType type) {
183  SkASSERT(GrSLTypeIsFloatType(type));
184  static const size_t kSizes[] = {
185  0, // kVoid_GrSLType
186  sizeof(float), // kFloat_GrSLType
187  2 * sizeof(float), // kVec2f_GrSLType
188  3 * sizeof(float), // kVec3f_GrSLType
189  4 * sizeof(float), // kVec4f_GrSLType
190  2 * 2 * sizeof(float), // kMat22f_GrSLType
191  3 * 3 * sizeof(float), // kMat33f_GrSLType
192  4 * 4 * sizeof(float), // kMat44f_GrSLType
193  0, // kTexture2DSampler_GrSLType
194  0, // kTextureExternalSampler_GrSLType
195  0, // kTexture2DRectSampler_GrSLType
196  0, // kTextureBufferSampler_GrSLType
197  0, // kBool_GrSLType
198  0, // kInt_GrSLType
199  0, // kUint_GrSLType
200  0, // kTexture2D_GrSLType
201  0, // kSampler_GrSLType
202  };
203  return kSizes[type];
204 
205  GR_STATIC_ASSERT(0 == kVoid_GrSLType);
206  GR_STATIC_ASSERT(1 == kFloat_GrSLType);
207  GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
208  GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
209  GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
210  GR_STATIC_ASSERT(5 == kMat22f_GrSLType);
211  GR_STATIC_ASSERT(6 == kMat33f_GrSLType);
212  GR_STATIC_ASSERT(7 == kMat44f_GrSLType);
213  GR_STATIC_ASSERT(8 == kTexture2DSampler_GrSLType);
214  GR_STATIC_ASSERT(9 == kTextureExternalSampler_GrSLType);
215  GR_STATIC_ASSERT(10 == kTexture2DRectSampler_GrSLType);
216  GR_STATIC_ASSERT(11 == kTextureBufferSampler_GrSLType);
217  GR_STATIC_ASSERT(12 == kBool_GrSLType);
218  GR_STATIC_ASSERT(13 == kInt_GrSLType);
219  GR_STATIC_ASSERT(14 == kUint_GrSLType);
220  GR_STATIC_ASSERT(15 == kTexture2D_GrSLType);
221  GR_STATIC_ASSERT(16 == kSampler_GrSLType);
222  GR_STATIC_ASSERT(17 == kGrSLTypeCount);
223 }
224 
225 static inline bool GrSLTypeIs2DCombinedSamplerType(GrSLType type) {
226  SkASSERT(type >= 0 && type < static_cast<GrSLType>(kGrSLTypeCount));
227  return type >= kTexture2DSampler_GrSLType && type <= kTexture2DRectSampler_GrSLType;
228 
229  GR_STATIC_ASSERT(8 == kTexture2DSampler_GrSLType);
230  GR_STATIC_ASSERT(9 == kTextureExternalSampler_GrSLType);
231  GR_STATIC_ASSERT(10 == kTexture2DRectSampler_GrSLType);
232 }
233 
234 static inline bool GrSLTypeIsCombinedSamplerType(GrSLType type) {
235  SkASSERT(type >= 0 && type < static_cast<GrSLType>(kGrSLTypeCount));
236  return type >= kTexture2DSampler_GrSLType && type <= kTextureBufferSampler_GrSLType;
237 
238  GR_STATIC_ASSERT(8 == kTexture2DSampler_GrSLType);
239  GR_STATIC_ASSERT(9 == kTextureExternalSampler_GrSLType);
240  GR_STATIC_ASSERT(10 == kTexture2DRectSampler_GrSLType);
241  GR_STATIC_ASSERT(11 == kTextureBufferSampler_GrSLType);
242 }
243 
244 static inline bool GrSLTypeAcceptsPrecision(GrSLType type) {
245  return type != kVoid_GrSLType && type != kBool_GrSLType;
246 }
247 
249 
253 enum GrVertexAttribType {
254  kFloat_GrVertexAttribType = 0,
255  kVec2f_GrVertexAttribType,
256  kVec3f_GrVertexAttribType,
257  kVec4f_GrVertexAttribType,
258 
259  kUByte_GrVertexAttribType, // unsigned byte, e.g. coverage
260  kVec4ub_GrVertexAttribType, // vector of 4 unsigned bytes, e.g. colors
261 
262  kVec2us_GrVertexAttribType, // vector of 2 shorts, e.g. texture coordinates
263 
264  kInt_GrVertexAttribType,
265  kUint_GrVertexAttribType,
266 
267  kLast_GrVertexAttribType = kUint_GrVertexAttribType
268 };
269 static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1;
270 
274 static inline int GrVertexAttribTypeVectorCount(GrVertexAttribType type) {
275  SkASSERT(type >= 0 && type < kGrVertexAttribTypeCount);
276  static const int kCounts[] = { 1, 2, 3, 4, 1, 4, 2, 1, 1 };
277  return kCounts[type];
278 
279  GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType);
280  GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType);
281  GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType);
282  GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType);
283  GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
284  GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
285  GR_STATIC_ASSERT(6 == kVec2us_GrVertexAttribType);
286  GR_STATIC_ASSERT(7 == kInt_GrVertexAttribType);
287  GR_STATIC_ASSERT(8 == kUint_GrVertexAttribType);
288  GR_STATIC_ASSERT(SK_ARRAY_COUNT(kCounts) == kGrVertexAttribTypeCount);
289 }
290 
294 static inline size_t GrVertexAttribTypeSize(GrVertexAttribType type) {
295  static const size_t kSizes[] = {
296  sizeof(float), // kFloat_GrVertexAttribType
297  2*sizeof(float), // kVec2f_GrVertexAttribType
298  3*sizeof(float), // kVec3f_GrVertexAttribType
299  4*sizeof(float), // kVec4f_GrVertexAttribType
300  1*sizeof(char), // kUByte_GrVertexAttribType
301  4*sizeof(char), // kVec4ub_GrVertexAttribType
302  2*sizeof(int16_t), // kVec2us_GrVertexAttribType
303  sizeof(int32_t), // kInt_GrVertexAttribType
304  sizeof(uint32_t) // kUint_GrVertexAttribType
305  };
306  return kSizes[type];
307 
308  GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType);
309  GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType);
310  GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType);
311  GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType);
312  GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
313  GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
314  GR_STATIC_ASSERT(6 == kVec2us_GrVertexAttribType);
315  GR_STATIC_ASSERT(7 == kInt_GrVertexAttribType);
316  GR_STATIC_ASSERT(8 == kUint_GrVertexAttribType);
317  GR_STATIC_ASSERT(SK_ARRAY_COUNT(kSizes) == kGrVertexAttribTypeCount);
318 }
319 
323 static inline bool GrVertexAttribTypeIsIntType(GrVertexAttribType type) {
324  SkASSERT(type >= 0 && type < static_cast<GrVertexAttribType>(kGrVertexAttribTypeCount));
325  return type >= kInt_GrVertexAttribType;
326 
327  GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType);
328  GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType);
329  GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType);
330  GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType);
331  GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
332  GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
333  GR_STATIC_ASSERT(6 == kVec2us_GrVertexAttribType);
334  GR_STATIC_ASSERT(7 == kInt_GrVertexAttribType);
335  GR_STATIC_ASSERT(8 == kUint_GrVertexAttribType);
336  GR_STATIC_ASSERT(9 == kGrVertexAttribTypeCount);
337 }
338 
342 static inline GrSLType GrVertexAttribTypeToSLType(GrVertexAttribType type) {
343  switch (type) {
344  default:
345  SkFAIL("Unsupported type conversion");
346  return kVoid_GrSLType;
347  case kUByte_GrVertexAttribType:
348  case kFloat_GrVertexAttribType:
349  return kFloat_GrSLType;
350  case kVec2us_GrVertexAttribType:
351  case kVec2f_GrVertexAttribType:
352  return kVec2f_GrSLType;
353  case kVec3f_GrVertexAttribType:
354  return kVec3f_GrSLType;
355  case kVec4ub_GrVertexAttribType:
356  case kVec4f_GrVertexAttribType:
357  return kVec4f_GrSLType;
358  case kInt_GrVertexAttribType:
359  return kInt_GrSLType;
360  case kUint_GrVertexAttribType:
361  return kUint_GrSLType;
362  }
363 }
364 
366 
373 enum GrPrimitiveEdgeType {
374  kFillBW_GrProcessorEdgeType,
375  kFillAA_GrProcessorEdgeType,
376  kInverseFillBW_GrProcessorEdgeType,
377  kInverseFillAA_GrProcessorEdgeType,
378  kHairlineAA_GrProcessorEdgeType,
379 
380  kLast_GrProcessorEdgeType = kHairlineAA_GrProcessorEdgeType
381 };
382 
383 static const int kGrProcessorEdgeTypeCnt = kLast_GrProcessorEdgeType + 1;
384 
385 static inline bool GrProcessorEdgeTypeIsFill(const GrPrimitiveEdgeType edgeType) {
386  return (kFillAA_GrProcessorEdgeType == edgeType || kFillBW_GrProcessorEdgeType == edgeType);
387 }
388 
389 static inline bool GrProcessorEdgeTypeIsInverseFill(const GrPrimitiveEdgeType edgeType) {
390  return (kInverseFillAA_GrProcessorEdgeType == edgeType ||
391  kInverseFillBW_GrProcessorEdgeType == edgeType);
392 }
393 
394 static inline bool GrProcessorEdgeTypeIsAA(const GrPrimitiveEdgeType edgeType) {
395  return (kFillBW_GrProcessorEdgeType != edgeType && kInverseFillBW_GrProcessorEdgeType != edgeType);
396 }
397 
398 static inline GrPrimitiveEdgeType GrInvertProcessorEdgeType(const GrPrimitiveEdgeType edgeType) {
399  switch (edgeType) {
400  case kFillBW_GrProcessorEdgeType:
401  return kInverseFillBW_GrProcessorEdgeType;
402  case kFillAA_GrProcessorEdgeType:
403  return kInverseFillAA_GrProcessorEdgeType;
404  case kInverseFillBW_GrProcessorEdgeType:
405  return kFillBW_GrProcessorEdgeType;
406  case kInverseFillAA_GrProcessorEdgeType:
407  return kFillAA_GrProcessorEdgeType;
408  case kHairlineAA_GrProcessorEdgeType:
409  SkFAIL("Hairline fill isn't invertible.");
410  }
411  return kFillAA_GrProcessorEdgeType; // suppress warning.
412 }
413 
417 enum GrIOType {
418  kRead_GrIOType,
419  kWrite_GrIOType,
420  kRW_GrIOType
421 };
422 
424  GrScissorState() : fEnabled(false) {}
425  GrScissorState(const SkIRect& rect) : fEnabled(true), fRect(rect) {}
426  void setDisabled() { fEnabled = false; }
427  void set(const SkIRect& rect) { fRect = rect; fEnabled = true; }
428  bool SK_WARN_UNUSED_RESULT intersect(const SkIRect& rect) {
429  if (!fEnabled) {
430  this->set(rect);
431  return true;
432  }
433  return fRect.intersect(rect);
434  }
435  bool operator==(const GrScissorState& other) const {
436  return fEnabled == other.fEnabled &&
437  (false == fEnabled || fRect == other.fRect);
438  }
439  bool operator!=(const GrScissorState& other) const { return !(*this == other); }
440 
441  bool enabled() const { return fEnabled; }
442  const SkIRect& rect() const { return fRect; }
443 
444 private:
445  bool fEnabled;
446  SkIRect fRect;
447 };
448 
452 enum GrBufferType {
453  kVertex_GrBufferType,
454  kIndex_GrBufferType,
455  kTexel_GrBufferType,
456  kDrawIndirect_GrBufferType,
457  kXferCpuToGpu_GrBufferType,
458  kXferGpuToCpu_GrBufferType,
459 
460  kLast_GrBufferType = kXferGpuToCpu_GrBufferType
461 };
462 static const int kGrBufferTypeCount = kLast_GrBufferType + 1;
463 
464 static inline bool GrBufferTypeIsVertexOrIndex(GrBufferType type) {
465  SkASSERT(type >= 0 && type < kGrBufferTypeCount);
466  return type <= kIndex_GrBufferType;
467 
468  GR_STATIC_ASSERT(0 == kVertex_GrBufferType);
469  GR_STATIC_ASSERT(1 == kIndex_GrBufferType);
470 }
471 
475 enum GrAccessPattern {
477  kDynamic_GrAccessPattern,
479  kStatic_GrAccessPattern,
481  kStream_GrAccessPattern,
482 
483  kLast_GrAccessPattern = kStream_GrAccessPattern
484 };
485 
486 
487 #ifdef SK_DEBUG
488 // Takes a pointer to a GrCaps, and will suppress prints if required
489 #define GrCapsDebugf(caps, ...) \
490  if (!caps->suppressPrints()) { \
491  SkDebugf(__VA_ARGS__); \
492  }
493 #else
494 #define GrCapsDebugf(caps, ...)
495 #endif
496 
500 enum class GrBackendObjectOwnership : bool {
502  kBorrowed = false,
504  kOwned = true
505 };
506 
507 template <typename T> T * const * sk_sp_address_as_pointer_address(sk_sp<T> const * sp) {
508  static_assert(sizeof(T*) == sizeof(sk_sp<T>), "sk_sp not expected size.");
509  return reinterpret_cast<T * const *>(sp);
510 }
511 
512 #endif
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:258
Definition: GrTypesPriv.h:423
bool SK_WARN_UNUSED_RESULT intersect(const SkIRect &r)
If r intersects this rectangle, return true and set this rectangle to that intersection, otherwise return false and do not change this rectangle.
Definition: SkRect.h:283
SkIRect holds four 32 bit integer coordinates for a rectangle.
Definition: SkRect.h:20