Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GrTypes.h
1 /*
2  * Copyright 2010 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 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  \
29  inline X operator & (X a, X b) { \
30  return (X) (+a & +b); \
31  } \
32  template <typename T> \
33  inline X operator & (T a, X b) { \
34  return (X) (+a & +b); \
35  } \
36  template <typename T> \
37  inline X operator & (X a, T b) { \
38  return (X) (+a & +b); \
39  } \
40 
41 #define GR_DECL_BITFIELD_OPS_FRIENDS(X) \
42  friend X operator | (X a, X b); \
43  friend X& operator |= (X& a, X b); \
44  \
45  friend X operator & (X a, X b); \
46  \
47  template <typename T> \
48  friend X operator & (T a, X b); \
49  \
50  template <typename T> \
51  friend X operator & (X a, T b); \
52 
53 
57 #define GR_MAKE_BITFIELD_CLASS_OPS(X) \
58  inline X operator | (X a, X b) { \
59  return (X) ((int)a | (int)b); \
60  } \
61  inline X& operator |= (X& a, X b) { \
62  return (a = a | b); \
63  } \
64  inline bool operator & (X a, X b) { \
65  return SkToBool((int)a & (int)b); \
66  }
67 
68 #define GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(X) \
69  friend X operator | (X a, X b); \
70  friend X& operator |= (X& a, X b); \
71  friend bool operator & (X a, X b);
72 
74 
75 // compile time versions of min/max
76 #define GR_CT_MAX(a, b) (((b) < (a)) ? (a) : (b))
77 #define GR_CT_MIN(a, b) (((b) < (a)) ? (b) : (a))
78 
82 static inline int32_t GrIDivRoundUp(int x, int y) {
83  SkASSERT(y > 0);
84  return (x + (y-1)) / y;
85 }
86 static inline uint32_t GrUIDivRoundUp(uint32_t x, uint32_t y) {
87  return (x + (y-1)) / y;
88 }
89 static inline size_t GrSizeDivRoundUp(size_t x, size_t y) {
90  return (x + (y-1)) / y;
91 }
92 
93 // compile time, evaluates Y multiple times
94 #define GR_CT_DIV_ROUND_UP(X, Y) (((X) + ((Y)-1)) / (Y))
95 
99 static inline uint32_t GrUIAlignUp(uint32_t x, uint32_t alignment) {
100  return GrUIDivRoundUp(x, alignment) * alignment;
101 }
102 static inline size_t GrSizeAlignUp(size_t x, size_t alignment) {
103  return GrSizeDivRoundUp(x, alignment) * alignment;
104 }
105 
106 // compile time, evaluates A multiple times
107 #define GR_CT_ALIGN_UP(X, A) (GR_CT_DIV_ROUND_UP((X),(A)) * (A))
108 
112 static inline uint32_t GrUIAlignUpPad(uint32_t x, uint32_t alignment) {
113  return (alignment - x % alignment) % alignment;
114 }
115 static inline size_t GrSizeAlignUpPad(size_t x, size_t alignment) {
116  return (alignment - x % alignment) % alignment;
117 }
118 
122 static inline uint32_t GrUIAlignDown(uint32_t x, uint32_t alignment) {
123  return (x / alignment) * alignment;
124 }
125 static inline size_t GrSizeAlignDown(size_t x, uint32_t alignment) {
126  return (x / alignment) * alignment;
127 }
128 
130 
134 enum GrBackend {
135  kOpenGL_GrBackend,
136  kVulkan_GrBackend,
137 
138  kLast_GrBackend = kVulkan_GrBackend
139 };
140 const int kBackendCount = kLast_GrBackend + 1;
141 
146 typedef intptr_t GrBackendContext;
147 
149 
153 enum GrPrimitiveType {
154  kTriangles_GrPrimitiveType,
155  kTriangleStrip_GrPrimitiveType,
156  kTriangleFan_GrPrimitiveType,
157  kPoints_GrPrimitiveType,
158  kLines_GrPrimitiveType, // 1 pix wide only
159  kLineStrip_GrPrimitiveType, // 1 pix wide only
160  kLast_GrPrimitiveType = kLineStrip_GrPrimitiveType
161 };
162 
163 static inline bool GrIsPrimTypeLines(GrPrimitiveType type) {
164  return kLines_GrPrimitiveType == type || kLineStrip_GrPrimitiveType == type;
165 }
166 
167 static inline bool GrIsPrimTypeTris(GrPrimitiveType type) {
168  return kTriangles_GrPrimitiveType == type ||
169  kTriangleStrip_GrPrimitiveType == type ||
170  kTriangleFan_GrPrimitiveType == type;
171 }
172 
177 enum GrMaskFormat {
178  kA8_GrMaskFormat,
179  kA565_GrMaskFormat,
180  kARGB_GrMaskFormat,
181 
182  kLast_GrMaskFormat = kARGB_GrMaskFormat
183 };
184 static const int kMaskFormatCount = kLast_GrMaskFormat + 1;
185 
189 static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) {
190  SkASSERT(format < kMaskFormatCount);
191  // kA8 (0) -> 1
192  // kA565 (1) -> 2
193  // kARGB (2) -> 4
194  static const int sBytesPerPixel[] = { 1, 2, 4 };
195  static_assert(SK_ARRAY_COUNT(sBytesPerPixel) == kMaskFormatCount, "array_size_mismatch");
196  static_assert(kA8_GrMaskFormat == 0, "enum_order_dependency");
197  static_assert(kA565_GrMaskFormat == 1, "enum_order_dependency");
198  static_assert(kARGB_GrMaskFormat == 2, "enum_order_dependency");
199 
200  return sBytesPerPixel[(int) format];
201 }
202 
206 enum GrPixelConfig {
207  kUnknown_GrPixelConfig,
208  kAlpha_8_GrPixelConfig,
209  kIndex_8_GrPixelConfig,
210  kRGB_565_GrPixelConfig,
214  kRGBA_4444_GrPixelConfig,
218  kRGBA_8888_GrPixelConfig,
222  kBGRA_8888_GrPixelConfig,
226  kSRGBA_8888_GrPixelConfig,
230  kSBGRA_8888_GrPixelConfig,
234  kETC1_GrPixelConfig,
238  kLATC_GrPixelConfig,
243  kR11_EAC_GrPixelConfig,
244 
255  kASTC_12x12_GrPixelConfig,
256 
260  kRGBA_float_GrPixelConfig,
261 
265  kAlpha_half_GrPixelConfig,
266 
270  kRGBA_half_GrPixelConfig,
271 
272  kLast_GrPixelConfig = kRGBA_half_GrPixelConfig
273 };
274 static const int kGrPixelConfigCnt = kLast_GrPixelConfig + 1;
275 
276 // Aliases for pixel configs that match skia's byte order.
277 #ifndef SK_CPU_LENDIAN
278  #error "Skia gpu currently assumes little endian"
279 #endif
280 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
281  static const GrPixelConfig kSkia8888_GrPixelConfig = kBGRA_8888_GrPixelConfig;
282  static const GrPixelConfig kSkiaGamma8888_GrPixelConfig = kSBGRA_8888_GrPixelConfig;
283 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
284  static const GrPixelConfig kSkia8888_GrPixelConfig = kRGBA_8888_GrPixelConfig;
285  static const GrPixelConfig kSkiaGamma8888_GrPixelConfig = kSRGBA_8888_GrPixelConfig;
286 #else
287  #error "SK_*32_SHIFT values must correspond to GL_BGRA or GL_RGBA format."
288 #endif
289 
290 // Returns true if the pixel config is a GPU-specific compressed format
291 // representation.
292 static inline bool GrPixelConfigIsCompressed(GrPixelConfig config) {
293  switch (config) {
294  case kIndex_8_GrPixelConfig:
295  case kETC1_GrPixelConfig:
296  case kLATC_GrPixelConfig:
297  case kR11_EAC_GrPixelConfig:
298  case kASTC_12x12_GrPixelConfig:
299  return true;
300  default:
301  return false;
302  }
303 }
304 
306 static inline GrPixelConfig GrMakePixelConfigUncompressed(GrPixelConfig config) {
307  switch (config) {
308  case kIndex_8_GrPixelConfig:
309  case kETC1_GrPixelConfig:
310  case kASTC_12x12_GrPixelConfig:
311  return kRGBA_8888_GrPixelConfig;
312  case kLATC_GrPixelConfig:
313  case kR11_EAC_GrPixelConfig:
314  return kAlpha_8_GrPixelConfig;
315  default:
316  SkASSERT(!GrPixelConfigIsCompressed(config));
317  return config;
318  }
319 }
320 
321 // Returns true if the pixel config is 32 bits per pixel
322 static inline bool GrPixelConfigIs8888(GrPixelConfig config) {
323  switch (config) {
324  case kRGBA_8888_GrPixelConfig:
325  case kBGRA_8888_GrPixelConfig:
326  case kSRGBA_8888_GrPixelConfig:
327  case kSBGRA_8888_GrPixelConfig:
328  return true;
329  default:
330  return false;
331  }
332 }
333 
334 // Returns true if the color (non-alpha) components represent sRGB values. It does NOT indicate that
335 // all three color components are present in the config or anything about their order.
336 static inline bool GrPixelConfigIsSRGB(GrPixelConfig config) {
337  switch (config) {
338  case kSRGBA_8888_GrPixelConfig:
339  case kSBGRA_8888_GrPixelConfig:
340  return true;
341  default:
342  return false;
343  }
344 }
345 
346 // Takes a config and returns the equivalent config with the R and B order
347 // swapped if such a config exists. Otherwise, kUnknown_GrPixelConfig
348 static inline GrPixelConfig GrPixelConfigSwapRAndB(GrPixelConfig config) {
349  switch (config) {
350  case kBGRA_8888_GrPixelConfig:
351  return kRGBA_8888_GrPixelConfig;
352  case kRGBA_8888_GrPixelConfig:
353  return kBGRA_8888_GrPixelConfig;
354  case kSBGRA_8888_GrPixelConfig:
355  return kSRGBA_8888_GrPixelConfig;
356  case kSRGBA_8888_GrPixelConfig:
357  return kSBGRA_8888_GrPixelConfig;
358  default:
359  return kUnknown_GrPixelConfig;
360  }
361 }
362 
363 static inline size_t GrBytesPerPixel(GrPixelConfig config) {
364  SkASSERT(!GrPixelConfigIsCompressed(config));
365  switch (config) {
366  case kAlpha_8_GrPixelConfig:
367  return 1;
368  case kRGB_565_GrPixelConfig:
369  case kRGBA_4444_GrPixelConfig:
370  case kAlpha_half_GrPixelConfig:
371  return 2;
372  case kRGBA_8888_GrPixelConfig:
373  case kBGRA_8888_GrPixelConfig:
374  case kSRGBA_8888_GrPixelConfig:
375  case kSBGRA_8888_GrPixelConfig:
376  return 4;
377  case kRGBA_half_GrPixelConfig:
378  return 8;
379  case kRGBA_float_GrPixelConfig:
380  return 16;
381  default:
382  return 0;
383  }
384 }
385 
386 static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
387  switch (config) {
388  case kETC1_GrPixelConfig:
389  case kRGB_565_GrPixelConfig:
390  return true;
391  default:
392  return false;
393  }
394 }
395 
396 static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
397  switch (config) {
398  case kR11_EAC_GrPixelConfig:
399  case kLATC_GrPixelConfig:
400  case kASTC_12x12_GrPixelConfig:
401  case kAlpha_8_GrPixelConfig:
402  case kAlpha_half_GrPixelConfig:
403  return true;
404  default:
405  return false;
406  }
407 }
408 
409 static inline bool GrPixelConfigIsFloatingPoint(GrPixelConfig config) {
410  switch (config) {
411  case kRGBA_float_GrPixelConfig:
412  case kAlpha_half_GrPixelConfig:
413  case kRGBA_half_GrPixelConfig:
414  return true;
415  default:
416  return false;
417  }
418 }
419 
423 enum GrSurfaceFlags {
424  kNone_GrSurfaceFlags = 0x0,
429  kRenderTarget_GrSurfaceFlag = 0x1,
433  kZeroCopy_GrSurfaceFlag = 0x2,
438  kCheckAllocation_GrSurfaceFlag = 0x4,
439 };
440 
441 GR_MAKE_BITFIELD_OPS(GrSurfaceFlags)
442 
443 // opaque type for 3D API object handles
444 typedef intptr_t GrBackendObject;
445 
453 enum GrSurfaceOrigin {
454  kDefault_GrSurfaceOrigin, // DEPRECATED; to be removed
455  kTopLeft_GrSurfaceOrigin,
456  kBottomLeft_GrSurfaceOrigin,
457 };
458 
459 struct GrMipLevel {
460  const void* fPixels;
461  size_t fRowBytes;
462 };
463 
468  GrSurfaceDesc()
469  : fFlags(kNone_GrSurfaceFlags)
470  , fOrigin(kDefault_GrSurfaceOrigin)
471  , fWidth(0)
472  , fHeight(0)
473  , fConfig(kUnknown_GrPixelConfig)
474  , fSampleCnt(0)
475  , fIsMipMapped(false) {
476  }
477 
478  GrSurfaceFlags fFlags;
479  GrSurfaceOrigin fOrigin;
480  int fWidth;
481  int fHeight;
482 
487  GrPixelConfig fConfig;
488 
498 };
499 
500 // Legacy alias
502 
506 enum GrClipType {
507  kRect_ClipType,
508  kPath_ClipType
509 };
510 
512 
513 
515 enum GrWrapOwnership {
517  kBorrow_GrWrapOwnership,
518 
520  kAdopt_GrWrapOwnership,
521 };
522 
544 enum GrBackendTextureFlags {
548  kNone_GrBackendTextureFlag = 0,
553  kRenderTarget_GrBackendTextureFlag = kRenderTarget_GrSurfaceFlag,
554 };
555 GR_MAKE_BITFIELD_OPS(GrBackendTextureFlags)
556 
558  GrBackendTextureDesc() { memset(this, 0, sizeof(*this)); }
559  GrBackendTextureFlags fFlags;
560  GrSurfaceOrigin fOrigin;
561  int fWidth; //<! width in pixels
562  int fHeight; //<! height in pixels
563  GrPixelConfig fConfig; //<! color format
573  GrBackendObject fTextureHandle;
574 };
575 
577 
589  GrBackendRenderTargetDesc() { memset(this, 0, sizeof(*this)); }
590  int fWidth; //<! width in pixels
591  int fHeight; //<! height in pixels
592  GrPixelConfig fConfig; //<! color format
593  GrSurfaceOrigin fOrigin; //<! pixel origin
607  GrBackendObject fRenderTargetHandle;
608 };
609 
614 enum GrGLBackendState {
615  kRenderTarget_GrGLBackendState = 1 << 0,
616  kTextureBinding_GrGLBackendState = 1 << 1,
617  // View state stands for scissor and viewport
618  kView_GrGLBackendState = 1 << 2,
619  kBlend_GrGLBackendState = 1 << 3,
620  kMSAAEnable_GrGLBackendState = 1 << 4,
621  kVertex_GrGLBackendState = 1 << 5,
622  kStencil_GrGLBackendState = 1 << 6,
623  kPixelStore_GrGLBackendState = 1 << 7,
624  kProgram_GrGLBackendState = 1 << 8,
625  kFixedFunction_GrGLBackendState = 1 << 9,
626  kMisc_GrGLBackendState = 1 << 10,
627  kPathRendering_GrGLBackendState = 1 << 11,
628  kALL_GrGLBackendState = 0xffff
629 };
630 
634 static inline size_t GrCompressedFormatDataSize(GrPixelConfig config,
635  int width, int height) {
636  SkASSERT(GrPixelConfigIsCompressed(config));
637  static const int kGrIndex8TableSize = 256 * 4; // 4 == sizeof(GrColor)
638 
639  switch (config) {
640  case kIndex_8_GrPixelConfig:
641  return width * height + kGrIndex8TableSize;
642  case kR11_EAC_GrPixelConfig:
643  case kLATC_GrPixelConfig:
644  case kETC1_GrPixelConfig:
645  SkASSERT((width & 3) == 0);
646  SkASSERT((height & 3) == 0);
647  return (width >> 2) * (height >> 2) * 8;
648 
649  case kASTC_12x12_GrPixelConfig:
650  SkASSERT((width % 12) == 0);
651  SkASSERT((height % 12) == 0);
652  return (width / 12) * (height / 12) * 16;
653 
654  default:
655  SkFAIL("Unknown compressed pixel config");
656  return 4 * width * height;
657  }
658 }
659 
663 static const uint32_t kAll_GrBackendState = 0xffffffff;
664 
665 #endif
int fWidth
Width of the texture.
Definition: GrTypes.h:480
Definition: GrTypes.h:557
int fSampleCnt
The number of samples per pixel.
Definition: GrTypes.h:598
GrBackendObject fTextureHandle
Handle to the 3D API object.
Definition: GrTypes.h:573
Describes a surface to be created.
Definition: GrTypes.h:467
int fHeight
Height of the texture.
Definition: GrTypes.h:481
Gr can wrap an existing render target created by the client in the 3D API with a GrRenderTarget objec...
Definition: GrTypes.h:588
int fStencilBits
Number of bits of stencil per-pixel.
Definition: GrTypes.h:602
GrPixelConfig fConfig
Format of source data of the texture.
Definition: GrTypes.h:487
Definition: GrTypes.h:459
bool fIsMipMapped
Indicates if the texture has mipmaps.
Definition: GrTypes.h:497
int fSampleCnt
The number of samples per pixel or 0 to disable full scene AA.
Definition: GrTypes.h:496
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:568
GrBackendObject fRenderTargetHandle
Handle to the 3D API object.
Definition: GrTypes.h:607
GrSurfaceOrigin fOrigin
origin of the texture
Definition: GrTypes.h:479
GrSurfaceFlags fFlags
bitfield of TextureFlags
Definition: GrTypes.h:478