Skia 2DGraphicsLibrary
GrTypes.h
1 /*
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 GrTypes_DEFINED
9 #define GrTypes_DEFINED
10
11 #include "SkMath.h"
12 #include "SkTypes.h"
13 #include "GrConfig.h"
14
16
21 #define GR_MAKE_BITFIELD_OPS(X) \
22  inline X operator |(X a, X b) { \
23  return (X) (+a | +b); \
24  } \
25  inline X& operator |=(X& a, X b) { \
26  return (a = a | b); \
27  } \
28  inline X operator &(X a, X b) { \
29  return (X) (+a & +b); \
30  } \
31  inline X& operator &=(X& a, X b) { \
32  return (a = a & b); \
33  } \
34  template <typename T> \
35  inline X operator &(T a, X b) { \
36  return (X) (+a & +b); \
37  } \
38  template <typename T> \
39  inline X operator &(X a, T b) { \
40  return (X) (+a & +b); \
41  } \
42
43 #define GR_DECL_BITFIELD_OPS_FRIENDS(X) \
44  friend X operator |(X a, X b); \
45  friend X& operator |=(X& a, X b); \
46  \
47  friend X operator &(X a, X b); \
48  friend X& operator &=(X& a, X b); \
49  \
50  template <typename T> \
51  friend X operator &(T a, X b); \
52  \
53  template <typename T> \
54  friend X operator &(X a, T b); \
55
56
60 template<typename TFlags> class GrTFlagsMask {
61 public:
63  constexpr explicit GrTFlagsMask(int value) : fValue(value) {}
64  constexpr int value() const { return fValue; }
65 private:
66  const int fValue;
67 };
68
73 }
75  TFlags b) {
77 }
78 template<typename TFlags> constexpr GrTFlagsMask<TFlags> operator|(TFlags a,
81 }
84  return (a = a | b);
85 }
86
87 // And-ing two masks returns another mask; and-ing one with regular flags returns flags.
91 }
92 template<typename TFlags> constexpr TFlags operator&(GrTFlagsMask<TFlags> a, TFlags b) {
93  return static_cast<TFlags>(a.value() & static_cast<int>(b));
94 }
95 template<typename TFlags> constexpr TFlags operator&(TFlags a, GrTFlagsMask<TFlags> b) {
96  return static_cast<TFlags>(static_cast<int>(a) & b.value());
97 }
98 template<typename TFlags> inline TFlags& operator&=(TFlags& a, GrTFlagsMask<TFlags> b) {
99  return (a = a & b);
100 }
101
106 #define GR_MAKE_BITFIELD_CLASS_OPS(X) \
107  constexpr GrTFlagsMask<X> operator~(X a) { \
109  } \
110  constexpr X operator|(X a, X b) { \
111  return static_cast<X>(static_cast<int>(a) | static_cast<int>(b)); \
112  } \
113  inline X& operator|=(X& a, X b) { \
114  return (a = a | b); \
115  } \
116  constexpr bool operator&(X a, X b) { \
117  return SkToBool(static_cast<int>(a) & static_cast<int>(b)); \
118  } \
119
120 #define GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(X) \
121  friend constexpr GrTFlagsMask<X> operator ~(X); \
122  friend constexpr X operator |(X, X); \
123  friend X& operator |=(X&, X); \
124  friend constexpr bool operator &(X, X);
125
127
128 // compile time versions of min/max
129 #define GR_CT_MAX(a, b) (((b) < (a)) ? (a) : (b))
130 #define GR_CT_MIN(a, b) (((b) < (a)) ? (b) : (a))
131
135 static inline int32_t GrIDivRoundUp(int x, int y) {
136  SkASSERT(y > 0);
137  return (x + (y-1)) / y;
138 }
139 static inline uint32_t GrUIDivRoundUp(uint32_t x, uint32_t y) {
140  return (x + (y-1)) / y;
141 }
142 static inline size_t GrSizeDivRoundUp(size_t x, size_t y) {
143  return (x + (y-1)) / y;
144 }
145
146 // compile time, evaluates Y multiple times
147 #define GR_CT_DIV_ROUND_UP(X, Y) (((X) + ((Y)-1)) / (Y))
148
152 static inline uint32_t GrUIAlignUp(uint32_t x, uint32_t alignment) {
153  return GrUIDivRoundUp(x, alignment) * alignment;
154 }
155 static inline size_t GrSizeAlignUp(size_t x, size_t alignment) {
156  return GrSizeDivRoundUp(x, alignment) * alignment;
157 }
158
159 // compile time, evaluates A multiple times
160 #define GR_CT_ALIGN_UP(X, A) (GR_CT_DIV_ROUND_UP((X),(A)) * (A))
161
165 static inline uint32_t GrUIAlignUpPad(uint32_t x, uint32_t alignment) {
166  return (alignment - x % alignment) % alignment;
167 }
168 static inline size_t GrSizeAlignUpPad(size_t x, size_t alignment) {
169  return (alignment - x % alignment) % alignment;
170 }
171
175 static inline uint32_t GrUIAlignDown(uint32_t x, uint32_t alignment) {
176  return (x / alignment) * alignment;
177 }
178 static inline size_t GrSizeAlignDown(size_t x, uint32_t alignment) {
179  return (x / alignment) * alignment;
180 }
181
183
187 enum GrBackend {
188  kOpenGL_GrBackend,
189  kVulkan_GrBackend,
190  kMock_GrBackend,
191
192  kLast_GrBackend = kVulkan_GrBackend
193 };
194 const int kBackendCount = kLast_GrBackend + 1;
195
201 typedef intptr_t GrBackendContext;
202
204
208 enum class GrAA {
209  kYes,
210  kNo
211 };
212
213 static inline GrAA GrBoolToAA(bool aa) { return aa ? GrAA::kYes : GrAA::kNo; }
214
216
220 enum class GrPrimitiveType {
221  kTriangles,
222  kTriangleStrip,
223  kTriangleFan,
224  kPoints,
225  kLines, // 1 pix wide only
226  kLineStrip, // 1 pix wide only
228 };
229 static constexpr int kNumGrPrimitiveTypes = (int) GrPrimitiveType::kLinesAdjacency + 1;
230
231 static constexpr bool GrIsPrimTypeLines(GrPrimitiveType type) {
232  return GrPrimitiveType::kLines == type ||
233  GrPrimitiveType::kLineStrip == type ||
235 }
236
237 static constexpr bool GrIsPrimTypeTris(GrPrimitiveType type) {
238  return GrPrimitiveType::kTriangles == type ||
239  GrPrimitiveType::kTriangleStrip == type ||
240  GrPrimitiveType::kTriangleFan == type;
241 }
242
243 static constexpr bool GrPrimTypeRequiresGeometryShaderSupport(GrPrimitiveType type) {
245 }
246
255
257 };
259
265  // kA8 (0) -> 1
266  // kA565 (1) -> 2
267  // kARGB (2) -> 4
268  static const int sBytesPerPixel[] = { 1, 2, 4 };
273
274  return sBytesPerPixel[(int) format];
275 }
276
280 enum GrPixelConfig {
281  kUnknown_GrPixelConfig,
282  kAlpha_8_GrPixelConfig,
283  kGray_8_GrPixelConfig,
284  kRGB_565_GrPixelConfig,
288  kRGBA_4444_GrPixelConfig,
292  kRGBA_8888_GrPixelConfig,
296  kBGRA_8888_GrPixelConfig,
300  kSRGBA_8888_GrPixelConfig,
304  kSBGRA_8888_GrPixelConfig,
308  kRGBA_8888_sint_GrPixelConfig,
309
313  kRGBA_float_GrPixelConfig,
317  kRG_float_GrPixelConfig,
318
322  kAlpha_half_GrPixelConfig,
323
327  kRGBA_half_GrPixelConfig,
328
329  kLast_GrPixelConfig = kRGBA_half_GrPixelConfig
330 };
331 static const int kGrPixelConfigCnt = kLast_GrPixelConfig + 1;
332
333 // Aliases for pixel configs that match skia's byte order.
334 #ifndef SK_CPU_LENDIAN
335  #error "Skia gpu currently assumes little endian"
336 #endif
337 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
338  static const GrPixelConfig kSkia8888_GrPixelConfig = kBGRA_8888_GrPixelConfig;
339 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
340  static const GrPixelConfig kSkia8888_GrPixelConfig = kRGBA_8888_GrPixelConfig;
341 #else
342  #error "SK_*32_SHIFT values must correspond to GL_BGRA or GL_RGBA format."
343 #endif
344
345 // Returns true if the pixel config is 32 bits per pixel
346 static inline bool GrPixelConfigIs8888Unorm(GrPixelConfig config) {
347  switch (config) {
348  case kRGBA_8888_GrPixelConfig:
349  case kBGRA_8888_GrPixelConfig:
350  case kSRGBA_8888_GrPixelConfig:
351  case kSBGRA_8888_GrPixelConfig:
352  return true;
353  case kUnknown_GrPixelConfig:
354  case kAlpha_8_GrPixelConfig:
355  case kGray_8_GrPixelConfig:
356  case kRGB_565_GrPixelConfig:
357  case kRGBA_4444_GrPixelConfig:
358  case kRGBA_8888_sint_GrPixelConfig:
359  case kRGBA_float_GrPixelConfig:
360  case kRG_float_GrPixelConfig:
361  case kAlpha_half_GrPixelConfig:
362  case kRGBA_half_GrPixelConfig:
363  return false;
364  }
365  SkFAIL("Invalid pixel config");
366  return false;
367 }
368
369 // Returns true if the color (non-alpha) components represent sRGB values. It does NOT indicate that
370 // all three color components are present in the config or anything about their order.
371 static inline bool GrPixelConfigIsSRGB(GrPixelConfig config) {
372  switch (config) {
373  case kSRGBA_8888_GrPixelConfig:
374  case kSBGRA_8888_GrPixelConfig:
375  return true;
376  case kUnknown_GrPixelConfig:
377  case kAlpha_8_GrPixelConfig:
378  case kGray_8_GrPixelConfig:
379  case kRGB_565_GrPixelConfig:
380  case kRGBA_4444_GrPixelConfig:
381  case kRGBA_8888_GrPixelConfig:
382  case kBGRA_8888_GrPixelConfig:
383  case kRGBA_8888_sint_GrPixelConfig:
384  case kRGBA_float_GrPixelConfig:
385  case kRG_float_GrPixelConfig:
386  case kAlpha_half_GrPixelConfig:
387  case kRGBA_half_GrPixelConfig:
388  return false;
389  }
390  SkFAIL("Invalid pixel config");
391  return false;
392 }
393
394 // Takes a config and returns the equivalent config with the R and B order
395 // swapped if such a config exists. Otherwise, kUnknown_GrPixelConfig
396 static inline GrPixelConfig GrPixelConfigSwapRAndB(GrPixelConfig config) {
397  switch (config) {
398  case kBGRA_8888_GrPixelConfig:
399  return kRGBA_8888_GrPixelConfig;
400  case kRGBA_8888_GrPixelConfig:
401  return kBGRA_8888_GrPixelConfig;
402  case kSBGRA_8888_GrPixelConfig:
403  return kSRGBA_8888_GrPixelConfig;
404  case kSRGBA_8888_GrPixelConfig:
405  return kSBGRA_8888_GrPixelConfig;
406  case kUnknown_GrPixelConfig:
407  case kAlpha_8_GrPixelConfig:
408  case kGray_8_GrPixelConfig:
409  case kRGB_565_GrPixelConfig:
410  case kRGBA_4444_GrPixelConfig:
411  case kRGBA_8888_sint_GrPixelConfig:
412  case kRGBA_float_GrPixelConfig:
413  case kRG_float_GrPixelConfig:
414  case kAlpha_half_GrPixelConfig:
415  case kRGBA_half_GrPixelConfig:
416  return kUnknown_GrPixelConfig;
417  }
418  SkFAIL("Invalid pixel config");
419  return kUnknown_GrPixelConfig;
420 }
421
422 static inline size_t GrBytesPerPixel(GrPixelConfig config) {
423  switch (config) {
424  case kAlpha_8_GrPixelConfig:
425  case kGray_8_GrPixelConfig:
426  return 1;
427  case kRGB_565_GrPixelConfig:
428  case kRGBA_4444_GrPixelConfig:
429  case kAlpha_half_GrPixelConfig:
430  return 2;
431  case kRGBA_8888_GrPixelConfig:
432  case kBGRA_8888_GrPixelConfig:
433  case kSRGBA_8888_GrPixelConfig:
434  case kSBGRA_8888_GrPixelConfig:
435  case kRGBA_8888_sint_GrPixelConfig:
436  return 4;
437  case kRGBA_half_GrPixelConfig:
438  return 8;
439  case kRGBA_float_GrPixelConfig:
440  return 16;
441  case kRG_float_GrPixelConfig:
442  return 8;
443  case kUnknown_GrPixelConfig:
444  return 0;
445  }
446  SkFAIL("Invalid pixel config");
447  return 0;
448 }
449
450 static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
451  switch (config) {
452  case kRGB_565_GrPixelConfig:
453  case kGray_8_GrPixelConfig:
454  case kRG_float_GrPixelConfig:
455  return true;
456  case kAlpha_8_GrPixelConfig:
457  case kRGBA_4444_GrPixelConfig:
458  case kAlpha_half_GrPixelConfig:
459  case kRGBA_8888_GrPixelConfig:
460  case kBGRA_8888_GrPixelConfig:
461  case kSRGBA_8888_GrPixelConfig:
462  case kSBGRA_8888_GrPixelConfig:
463  case kRGBA_8888_sint_GrPixelConfig:
464  case kRGBA_half_GrPixelConfig:
465  case kRGBA_float_GrPixelConfig:
466  case kUnknown_GrPixelConfig:
467  return false;
468  }
469  SkFAIL("Invalid pixel config");
470  return false;
471 }
472
473 static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
474  switch (config) {
475  case kAlpha_8_GrPixelConfig:
476  case kAlpha_half_GrPixelConfig:
477  return true;
478  case kUnknown_GrPixelConfig:
479  case kGray_8_GrPixelConfig:
480  case kRGB_565_GrPixelConfig:
481  case kRGBA_4444_GrPixelConfig:
482  case kRGBA_8888_GrPixelConfig:
483  case kBGRA_8888_GrPixelConfig:
484  case kSRGBA_8888_GrPixelConfig:
485  case kSBGRA_8888_GrPixelConfig:
486  case kRGBA_8888_sint_GrPixelConfig:
487  case kRGBA_float_GrPixelConfig:
488  case kRG_float_GrPixelConfig:
489  case kRGBA_half_GrPixelConfig:
490  return false;
491  }
492  SkFAIL("Invalid pixel config.");
493  return false;
494 }
495
496 static inline bool GrPixelConfigIsFloatingPoint(GrPixelConfig config) {
497  switch (config) {
498  case kRGBA_float_GrPixelConfig:
499  case kRG_float_GrPixelConfig:
500  case kAlpha_half_GrPixelConfig:
501  case kRGBA_half_GrPixelConfig:
502  return true;
503  case kUnknown_GrPixelConfig:
504  case kAlpha_8_GrPixelConfig:
505  case kGray_8_GrPixelConfig:
506  case kRGB_565_GrPixelConfig:
507  case kRGBA_4444_GrPixelConfig:
508  case kRGBA_8888_GrPixelConfig:
509  case kBGRA_8888_GrPixelConfig:
510  case kSRGBA_8888_GrPixelConfig:
511  case kSBGRA_8888_GrPixelConfig:
512  case kRGBA_8888_sint_GrPixelConfig:
513  return false;
514  }
515  SkFAIL("Invalid pixel config");
516  return false;
517 }
518
519 static inline bool GrPixelConfigIsSint(GrPixelConfig config) {
520  return config == kRGBA_8888_sint_GrPixelConfig;
521 }
522
523 static inline bool GrPixelConfigIsUnorm(GrPixelConfig config) {
524  switch (config) {
525  case kAlpha_8_GrPixelConfig:
526  case kGray_8_GrPixelConfig:
527  case kRGB_565_GrPixelConfig:
528  case kRGBA_4444_GrPixelConfig:
529  case kRGBA_8888_GrPixelConfig:
530  case kBGRA_8888_GrPixelConfig:
531  case kSRGBA_8888_GrPixelConfig:
532  case kSBGRA_8888_GrPixelConfig:
533  return true;
534  case kUnknown_GrPixelConfig:
535  case kAlpha_half_GrPixelConfig:
536  case kRGBA_8888_sint_GrPixelConfig:
537  case kRGBA_float_GrPixelConfig:
538  case kRG_float_GrPixelConfig:
539  case kRGBA_half_GrPixelConfig:
540  return false;
541  }
542  SkFAIL("Invalid pixel config.");
543  return false;
544 }
545
549 enum GrSurfaceFlags {
550  kNone_GrSurfaceFlags = 0x0,
555  kRenderTarget_GrSurfaceFlag = 0x1,
560  kPerformInitialClear_GrSurfaceFlag = 0x2
561 };
562
563 GR_MAKE_BITFIELD_OPS(GrSurfaceFlags)
564
565 // opaque type for 3D API object handles
566 typedef intptr_t GrBackendObject;
567
575 enum GrSurfaceOrigin {
576  kDefault_GrSurfaceOrigin, // DEPRECATED; to be removed
577  kTopLeft_GrSurfaceOrigin,
578  kBottomLeft_GrSurfaceOrigin,
579 };
580
581 struct GrMipLevel {
582  const void* fPixels;
583  size_t fRowBytes;
584 };
585
590  GrSurfaceDesc()
591  : fFlags(kNone_GrSurfaceFlags)
592  , fOrigin(kDefault_GrSurfaceOrigin)
593  , fWidth(0)
594  , fHeight(0)
595  , fConfig(kUnknown_GrPixelConfig)
596  , fSampleCnt(0)
597  , fIsMipMapped(false) {
598  }
599
600  GrSurfaceFlags fFlags;
601  GrSurfaceOrigin fOrigin;
602  int fWidth;
603  int fHeight;
604
609  GrPixelConfig fConfig;
610
620 };
621
622 // Legacy alias
624
628 enum GrClipType {
629  kRect_ClipType,
630  kPath_ClipType
631 };
632
634
635
637 enum GrWrapOwnership {
639  kBorrow_GrWrapOwnership,
640
643 };
644
666 enum GrBackendTextureFlags {
670  kNone_GrBackendTextureFlag = 0,
675  kRenderTarget_GrBackendTextureFlag = kRenderTarget_GrSurfaceFlag,
676 };
677 GR_MAKE_BITFIELD_OPS(GrBackendTextureFlags)
678
680  GrBackendTextureDesc() { memset(this, 0, sizeof(*this)); }
681  GrBackendTextureFlags fFlags;
682  GrSurfaceOrigin fOrigin;
683  int fWidth; //<! width in pixels
684  int fHeight; //<! height in pixels
685  GrPixelConfig fConfig; //<! color format
696  GrBackendObject fTextureHandle;
697 };
698
700
712  GrBackendRenderTargetDesc() { memset(this, 0, sizeof(*this)); }
713  int fWidth; //<! width in pixels
714  int fHeight; //<! height in pixels
715  GrPixelConfig fConfig; //<! color format
716  GrSurfaceOrigin fOrigin; //<! pixel origin
731  GrBackendObject fRenderTargetHandle;
732 };
733
738 enum GrGLBackendState {
739  kRenderTarget_GrGLBackendState = 1 << 0,
740  kTextureBinding_GrGLBackendState = 1 << 1,
741  // View state stands for scissor and viewport
742  kView_GrGLBackendState = 1 << 2,
743  kBlend_GrGLBackendState = 1 << 3,
744  kMSAAEnable_GrGLBackendState = 1 << 4,
745  kVertex_GrGLBackendState = 1 << 5,
746  kStencil_GrGLBackendState = 1 << 6,
747  kPixelStore_GrGLBackendState = 1 << 7,
748  kProgram_GrGLBackendState = 1 << 8,
749  kFixedFunction_GrGLBackendState = 1 << 9,
750  kMisc_GrGLBackendState = 1 << 10,
751  kPathRendering_GrGLBackendState = 1 << 11,
752  kALL_GrGLBackendState = 0xffff
753 };
754
758 static const uint32_t kAll_GrBackendState = 0xffffffff;
759
760 #endif
int fWidth
Width of the texture.
Definition: GrTypes.h:602
Definition: GrTypes.h:679
int fSampleCnt
The number of samples per pixel.
Definition: GrTypes.h:721
GrBackendObject fTextureHandle
Handle to the 3D API object.
Definition: GrTypes.h:696
Describes a surface to be created.
Definition: GrTypes.h:589
int fHeight
Height of the texture.
Definition: GrTypes.h:603
Wraps a C++11 enum that we use as a bitfield, and enables a limited amount of masking with type safet...
Definition: GrTypes.h:60
Gr can wrap an existing render target created by the client in the 3D API with a GrRenderTarget objec...
Definition: GrTypes.h:711
int fStencilBits
Number of bits of stencil per-pixel.
Definition: GrTypes.h:725
GrPixelConfig fConfig
Format of source data of the texture.
Definition: GrTypes.h:609
Definition: GrTypes.h:581
bool fIsMipMapped
Indicates if the texture has mipmaps.
Definition: GrTypes.h:619
int fSampleCnt
The number of samples per pixel or 0 to disable full scene AA.
Definition: GrTypes.h:618
int fSampleCnt
If the render target flag is set and sample count is greater than 0 then Gr will create an MSAA buffe...
Definition: GrTypes.h:690
GrBackendObject fRenderTargetHandle
Handle to the 3D API object.
Definition: GrTypes.h:731
GrSurfaceOrigin fOrigin
origin of the texture
Definition: GrTypes.h:601
GrSurfaceFlags fFlags
bitfield of TextureFlags
Definition: GrTypes.h:600