blob: 3c730df41eb6b9ccd627f79a73482ce81e0e4fc2 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrTypes_DEFINED
12#define GrTypes_DEFINED
13
reed@google.com9b24d252011-06-01 20:27:53 +000014#include "SkTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015#include "GrConfig.h"
bsalomon@google.comb9086a02012-11-01 18:02:54 +000016#include "SkMath.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.comfea37b52011-04-25 15:51:06 +000018////////////////////////////////////////////////////////////////////////////////
19
20/**
21 * Defines overloaded bitwise operators to make it easier to use an enum as a
22 * bitfield.
23 */
24#define GR_MAKE_BITFIELD_OPS(X) \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000025 inline X operator | (X a, X b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000026 return (X) (+a | +b); \
27 } \
28 \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000029 inline X operator & (X a, X b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000030 return (X) (+a & +b); \
31 } \
32 template <typename T> \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000033 inline X operator & (T a, X b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000034 return (X) (+a & +b); \
35 } \
36 template <typename T> \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000037 inline X operator & (X a, T b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000038 return (X) (+a & +b); \
39 } \
40
bsalomon@google.com86c1f712011-10-12 14:54:26 +000041#define GR_DECL_BITFIELD_OPS_FRIENDS(X) \
42 friend X operator | (X a, X b); \
43 \
44 friend X operator & (X a, X b); \
45 \
46 template <typename T> \
47 friend X operator & (T a, X b); \
48 \
49 template <typename T> \
50 friend X operator & (X a, T b); \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000051////////////////////////////////////////////////////////////////////////////////
52
53
reed@google.comac10a2d2010-12-22 21:39:39 +000054/**
55 * Macro to round n up to the next multiple of 4, or return it unchanged if
56 * n is already a multiple of 4
57 */
reed@google.com9b24d252011-06-01 20:27:53 +000058#define GrALIGN4(n) SkAlign4(n)
tomhudson@google.com01224d52011-11-28 18:22:01 +000059#define GrIsALIGN4(n) SkIsAlign4(n)
reed@google.comac10a2d2010-12-22 21:39:39 +000060
61template <typename T> const T& GrMin(const T& a, const T& b) {
robertphillips@google.com07ef9112012-06-04 13:22:14 +000062 return (a < b) ? a : b;
reed@google.comac10a2d2010-12-22 21:39:39 +000063}
64
65template <typename T> const T& GrMax(const T& a, const T& b) {
robertphillips@google.com07ef9112012-06-04 13:22:14 +000066 return (b < a) ? a : b;
reed@google.comac10a2d2010-12-22 21:39:39 +000067}
68
69// compile time versions of min/max
70#define GR_CT_MAX(a, b) (((b) < (a)) ? (a) : (b))
71#define GR_CT_MIN(a, b) (((b) < (a)) ? (b) : (a))
72
73/**
74 * divide, rounding up
75 */
bsalomon@google.com91958362011-06-13 17:58:13 +000076static inline int32_t GrIDivRoundUp(int x, int y) {
77 GrAssert(y > 0);
78 return (x + (y-1)) / y;
79}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000080static inline uint32_t GrUIDivRoundUp(uint32_t x, uint32_t y) {
81 return (x + (y-1)) / y;
82}
bsalomon@google.com4da34e32012-06-19 15:40:27 +000083static inline size_t GrSizeDivRoundUp(size_t x, size_t y) {
reed@google.comac10a2d2010-12-22 21:39:39 +000084 return (x + (y-1)) / y;
85}
86
bsalomon@google.com4da34e32012-06-19 15:40:27 +000087// compile time, evaluates Y multiple times
88#define GR_CT_DIV_ROUND_UP(X, Y) (((X) + ((Y)-1)) / (Y))
89
reed@google.comac10a2d2010-12-22 21:39:39 +000090/**
91 * align up
92 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +000093static inline uint32_t GrUIAlignUp(uint32_t x, uint32_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +000094 return GrUIDivRoundUp(x, alignment) * alignment;
95}
bsalomon@google.com4da34e32012-06-19 15:40:27 +000096static inline size_t GrSizeAlignUp(size_t x, size_t alignment) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000097 return GrSizeDivRoundUp(x, alignment) * alignment;
98}
reed@google.comac10a2d2010-12-22 21:39:39 +000099
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000100// compile time, evaluates A multiple times
101#define GR_CT_ALIGN_UP(X, A) (GR_CT_DIV_ROUND_UP((X),(A)) * (A))
102
reed@google.comac10a2d2010-12-22 21:39:39 +0000103/**
104 * amount of pad needed to align up
105 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000106static inline uint32_t GrUIAlignUpPad(uint32_t x, uint32_t alignment) {
107 return (alignment - x % alignment) % alignment;
108}
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000109static inline size_t GrSizeAlignUpPad(size_t x, size_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000110 return (alignment - x % alignment) % alignment;
111}
112
113/**
114 * align down
115 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000116static inline uint32_t GrUIAlignDown(uint32_t x, uint32_t alignment) {
117 return (x / alignment) * alignment;
118}
bsalomon@google.com5e497322012-08-28 21:45:26 +0000119static inline size_t GrSizeAlignDown(size_t x, uint32_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000120 return (x / alignment) * alignment;
121}
122
123/**
124 * Count elements in an array
125 */
reed@google.com9b24d252011-06-01 20:27:53 +0000126#define GR_ARRAY_COUNT(array) SK_ARRAY_COUNT(array)
reed@google.comac10a2d2010-12-22 21:39:39 +0000127
128//!< allocate a block of memory, will never return NULL
129extern void* GrMalloc(size_t bytes);
130
131//!< free block allocated by GrMalloc. ptr may be NULL
132extern void GrFree(void* ptr);
133
134static inline void Gr_bzero(void* dst, size_t size) {
135 memset(dst, 0, size);
136}
137
138///////////////////////////////////////////////////////////////////////////////
139
140/**
reed@google.comac10a2d2010-12-22 21:39:39 +0000141 * Return true if n is a power of 2
142 */
143static inline bool GrIsPow2(unsigned n) {
144 return n && 0 == (n & (n - 1));
145}
146
147/**
148 * Return the next power of 2 >= n.
149 */
150static inline uint32_t GrNextPow2(uint32_t n) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000151 return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000152}
153
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000154static inline int GrNextPow2(int n) {
155 GrAssert(n >= 0); // this impl only works for non-neg.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000156 return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000157}
158
reed@google.comac10a2d2010-12-22 21:39:39 +0000159///////////////////////////////////////////////////////////////////////////////
160
161/**
162 * 16.16 fixed point type
163 */
164typedef int32_t GrFixed;
165
166#if GR_DEBUG
167
168static inline int16_t GrToS16(intptr_t x) {
169 GrAssert((int16_t)x == x);
170 return (int16_t)x;
171}
172
173#else
174
175#define GrToS16(x) x
176
177#endif
178
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000179
180///////////////////////////////////////////////////////////////////////////////
181
reed@google.comac10a2d2010-12-22 21:39:39 +0000182/**
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000183 * Possible 3D APIs that may be used by Ganesh.
184 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000185enum GrBackend {
186 kOpenGL_GrBackend,
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000187};
188
189/**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000190 * Backend-specific 3D context handle
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000191 * GrGLInterface* for OpenGL. If NULL will use the default GL interface.
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000192 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000193typedef intptr_t GrBackendContext;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000194
195///////////////////////////////////////////////////////////////////////////////
196
197/**
reed@google.comac10a2d2010-12-22 21:39:39 +0000198 * Type used to describe format of vertices in arrays
199 * Values are defined in GrDrawTarget
200 */
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000201typedef int GrVertexLayout;
reed@google.comac10a2d2010-12-22 21:39:39 +0000202
bsalomon@google.comffca4002011-02-22 20:34:01 +0000203/**
bsalomon@google.comffca4002011-02-22 20:34:01 +0000204* Geometric primitives used for drawing.
205*/
206enum GrPrimitiveType {
bsalomon@google.com47059542012-06-06 20:51:20 +0000207 kTriangles_GrPrimitiveType,
208 kTriangleStrip_GrPrimitiveType,
209 kTriangleFan_GrPrimitiveType,
210 kPoints_GrPrimitiveType,
211 kLines_GrPrimitiveType, // 1 pix wide only
212 kLineStrip_GrPrimitiveType // 1 pix wide only
bsalomon@google.comffca4002011-02-22 20:34:01 +0000213};
214
bsalomon@google.com0650e812011-04-08 18:07:53 +0000215static inline bool GrIsPrimTypeLines(GrPrimitiveType type) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000216 return kLines_GrPrimitiveType == type || kLineStrip_GrPrimitiveType == type;
bsalomon@google.com0650e812011-04-08 18:07:53 +0000217}
218
219static inline bool GrIsPrimTypeTris(GrPrimitiveType type) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000220 return kTriangles_GrPrimitiveType == type ||
221 kTriangleStrip_GrPrimitiveType == type ||
222 kTriangleFan_GrPrimitiveType == type;
bsalomon@google.com0650e812011-04-08 18:07:53 +0000223}
224
bsalomon@google.comffca4002011-02-22 20:34:01 +0000225/**
226 * Coeffecients for alpha-blending.
227 */
228enum GrBlendCoeff {
bsalomon@google.com47059542012-06-06 20:51:20 +0000229 kInvalid_GrBlendCoeff = -1,
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000230
bsalomon@google.com47059542012-06-06 20:51:20 +0000231 kZero_GrBlendCoeff, //<! 0
232 kOne_GrBlendCoeff, //<! 1
233 kSC_GrBlendCoeff, //<! src color
234 kISC_GrBlendCoeff, //<! one minus src color
235 kDC_GrBlendCoeff, //<! dst color
236 kIDC_GrBlendCoeff, //<! one minus dst color
237 kSA_GrBlendCoeff, //<! src alpha
238 kISA_GrBlendCoeff, //<! one minus src alpha
239 kDA_GrBlendCoeff, //<! dst alpha
240 kIDA_GrBlendCoeff, //<! one minus dst alpha
241 kConstC_GrBlendCoeff, //<! constant color
242 kIConstC_GrBlendCoeff, //<! one minus constant color
243 kConstA_GrBlendCoeff, //<! constant color alpha
244 kIConstA_GrBlendCoeff, //<! one minus constant color alpha
bsalomon@google.com080773c2011-03-15 19:09:25 +0000245
bsalomon@google.com47059542012-06-06 20:51:20 +0000246 kPublicGrBlendCoeffCount
bsalomon@google.comffca4002011-02-22 20:34:01 +0000247};
248
bsalomon@google.comd302f142011-03-03 13:54:13 +0000249/**
reed@google.com759c16e2011-03-15 19:15:15 +0000250 * Formats for masks, used by the font cache.
251 * Important that these are 0-based.
reed@google.com98539c62011-03-15 15:40:16 +0000252 */
253enum GrMaskFormat {
caryclark@google.com1eeaf0b2011-06-22 13:19:43 +0000254 kA8_GrMaskFormat, //!< 1-byte per pixel
255 kA565_GrMaskFormat, //!< 2-bytes per pixel
256 kA888_GrMaskFormat, //!< 4-bytes per pixel
257
258 kCount_GrMaskFormats //!< used to allocate arrays sized for mask formats
reed@google.com98539c62011-03-15 15:40:16 +0000259};
260
261/**
262 * Return the number of bytes-per-pixel for the specified mask format.
263 */
264static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) {
reed@google.combbf12262011-10-04 20:14:57 +0000265 GrAssert((unsigned)format <= 2);
266 // kA8 (0) -> 1
267 // kA565 (1) -> 2
268 // kA888 (2) -> 4
269 return 1 << (int)format;
reed@google.com98539c62011-03-15 15:40:16 +0000270}
271
272/**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000273 * Pixel configurations.
274 */
275enum GrPixelConfig {
276 kUnknown_GrPixelConfig,
277 kAlpha_8_GrPixelConfig,
278 kIndex_8_GrPixelConfig,
279 kRGB_565_GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000280 /**
281 * Premultiplied
282 */
283 kRGBA_4444_GrPixelConfig,
284 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000285 * Premultiplied. Byte order is r,g,b,a.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000286 */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000287 kRGBA_8888_GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000288 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000289 * Premultiplied. Byte order is b,g,r,a.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000290 */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000291 kBGRA_8888_GrPixelConfig,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000292
293 kGrPixelConfigCount
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000294};
295
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000296// Aliases for pixel configs that match skia's byte order.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000297#ifndef SK_CPU_LENDIAN
298 #error "Skia gpu currently assumes little endian"
299#endif
300#if 24 == SK_A32_SHIFT && 16 == SK_R32_SHIFT && \
301 8 == SK_G32_SHIFT && 0 == SK_B32_SHIFT
bsalomon@google.com0342a852012-08-20 19:22:38 +0000302 static const GrPixelConfig kSkia8888_GrPixelConfig = kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000303#elif 24 == SK_A32_SHIFT && 16 == SK_B32_SHIFT && \
304 8 == SK_G32_SHIFT && 0 == SK_R32_SHIFT
bsalomon@google.com0342a852012-08-20 19:22:38 +0000305 static const GrPixelConfig kSkia8888_GrPixelConfig = kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000306#else
307 #error "SK_*32_SHIFT values must correspond to GL_BGRA or GL_RGBA format."
308#endif
309
bsalomon@google.com0342a852012-08-20 19:22:38 +0000310// This alias is deprecated and will be removed.
311static const GrPixelConfig kSkia8888_PM_GrPixelConfig = kSkia8888_GrPixelConfig;
312
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000313// Returns true if the pixel config has 8bit r,g,b,a components in that byte
314// order
315static inline bool GrPixelConfigIsRGBA8888(GrPixelConfig config) {
316 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000317 case kRGBA_8888_GrPixelConfig:
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000318 return true;
319 default:
320 return false;
321 }
322}
323
324// Returns true if the pixel config has 8bit b,g,r,a components in that byte
325// order
326static inline bool GrPixelConfigIsBGRA8888(GrPixelConfig config) {
327 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000328 case kBGRA_8888_GrPixelConfig:
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000329 return true;
330 default:
331 return false;
332 }
333}
334
335// Returns true if the pixel config is 32 bits per pixel
336static inline bool GrPixelConfigIs32Bit(GrPixelConfig config) {
337 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000338 case kRGBA_8888_GrPixelConfig:
339 case kBGRA_8888_GrPixelConfig:
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000340 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
348static inline GrPixelConfig GrPixelConfigSwapRAndB(GrPixelConfig config) {
349 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000350 case kBGRA_8888_GrPixelConfig:
351 return kRGBA_8888_GrPixelConfig;
352 case kRGBA_8888_GrPixelConfig:
353 return kBGRA_8888_GrPixelConfig;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000354 default:
355 return kUnknown_GrPixelConfig;
356 }
357}
358
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000359static inline size_t GrBytesPerPixel(GrPixelConfig config) {
360 switch (config) {
361 case kAlpha_8_GrPixelConfig:
362 case kIndex_8_GrPixelConfig:
363 return 1;
364 case kRGB_565_GrPixelConfig:
365 case kRGBA_4444_GrPixelConfig:
366 return 2;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000367 case kRGBA_8888_GrPixelConfig:
368 case kBGRA_8888_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000369 return 4;
370 default:
371 return 0;
372 }
373}
374
375static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
376 switch (config) {
377 case kRGB_565_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +0000378 return true;
379 default:
380 return false;
381 }
382}
383
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000384static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
385 switch (config) {
386 case kAlpha_8_GrPixelConfig:
387 return true;
388 default:
389 return false;
390 }
391}
392
393/**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000394 * Optional bitfield flags that can be passed to createTexture.
395 */
396enum GrTextureFlags {
397 kNone_GrTextureFlags = 0x0,
398 /**
399 * Creates a texture that can be rendered to as a GrRenderTarget. Use
400 * GrTexture::asRenderTarget() to access.
401 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000402 kRenderTarget_GrTextureFlagBit = 0x1,
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000403 /**
404 * By default all render targets have an associated stencil buffer that
405 * may be required for path filling. This flag overrides stencil buffer
406 * creation.
407 * MAKE THIS PRIVATE?
408 */
409 kNoStencil_GrTextureFlagBit = 0x2,
410 /**
411 * Hint that the CPU may modify this texture after creation.
412 */
413 kDynamicUpdate_GrTextureFlagBit = 0x4,
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000414
415 kDummy_GrTextureFlagBit,
416 kLastPublic_GrTextureFlagBit = kDummy_GrTextureFlagBit-1,
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000417};
418
419GR_MAKE_BITFIELD_OPS(GrTextureFlags)
420
421enum {
422 /**
423 * For Index8 pixel config, the colortable must be 256 entries
424 */
425 kGrColorTableSize = 256 * 4 //sizeof(GrColor)
426};
427
robertphillips@google.com5091b702012-08-09 10:49:39 +0000428
429/**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000430 * Describes a texture to be created.
431 */
432struct GrTextureDesc {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000433 GrTextureDesc()
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000434 : fFlags(kNone_GrTextureFlags)
435 , fWidth(0)
436 , fHeight(0)
437 , fConfig(kUnknown_GrPixelConfig)
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000438 , fSampleCnt(0) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000439 }
440
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000441 GrTextureFlags fFlags; //!< bitfield of TextureFlags
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +0000442 int fWidth; //!< Width of the texture
443 int fHeight; //!< Height of the texture
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000444
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000445 /**
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000446 * Format of source data of the texture. Not guaranteed to be the same as
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000447 * internal format used by 3D API.
448 */
bsalomon@google.com5bc34f02011-12-06 14:46:34 +0000449 GrPixelConfig fConfig;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000450
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000451 /**
452 * The number of samples per pixel or 0 to disable full scene AA. This only
453 * applies if the kRenderTarget_GrTextureFlagBit is set. The actual number
454 * of samples may not exactly match the request. The request will be rounded
455 * up to the next supported sample count, or down if it is larger than the
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000456 * max supported count.
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000457 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000458 int fSampleCnt;
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000459};
460
461/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000462 * GrCacheData holds user-provided cache-specific data. It is used in
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000463 * combination with the GrTextureDesc to construct a cache key for texture
464 * resources.
465 */
466struct GrCacheData {
467 /*
468 * Scratch textures should all have this value as their fClientCacheID
469 */
470 static const uint64_t kScratch_CacheID = 0xBBBBBBBB;
471
472 /**
473 * Resources in the "scratch" domain can be used by any domain. All
474 * scratch textures will have this as their domain.
475 */
476 static const uint8_t kScratch_ResourceDomain = 0;
477
478
479 // No default constructor is provided since, if you are creating one
480 // of these, you should definitely have a key (or be using the scratch
481 // key).
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000482 GrCacheData(uint64_t key)
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000483 : fClientCacheID(key)
484 , fResourceDomain(kScratch_ResourceDomain) {
485 }
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000486
487 /**
488 * A user-provided texture ID. It should be unique to the texture data and
489 * does not need to take into account the width or height. Two textures
490 * with the same ID but different dimensions will not collide. This field
491 * is only relevant for textures that will be cached.
492 */
493 uint64_t fClientCacheID;
robertphillips@google.com5091b702012-08-09 10:49:39 +0000494
495 /**
496 * Allows cache clients to cluster their textures inside domains (e.g.,
497 * alpha clip masks). Only relevant for cached textures.
498 */
499 uint8_t fResourceDomain;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000500};
501
502/**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000503 * Clips are composed from these objects.
504 */
505enum GrClipType {
506 kRect_ClipType,
507 kPath_ClipType
508};
509
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000510/**
511 * Commands used to describe a path. Each command
512 * is accompanied by some number of points.
513 */
514enum GrPathCmd {
515 kMove_PathCmd, //!< Starts a new subpath at
516 // at the returned point
517 // 1 point
518 kLine_PathCmd, //!< Adds a line segment
519 // 2 points
520 kQuadratic_PathCmd, //!< Adds a quadratic segment
521 // 3 points
522 kCubic_PathCmd, //!< Adds a cubic segment
523 // 4 points
524 kClose_PathCmd, //!< Closes the current subpath
525 // by connecting a line to the
526 // starting point.
527 // 0 points
528 kEnd_PathCmd //!< Indicates the end of the last subpath
529 // when iterating
530 // 0 points.
531};
532
533/**
534 * Gets the number of points associated with a path command.
535 */
536static int inline NumPathCmdPoints(GrPathCmd cmd) {
537 static const int gNumPoints[] = {
538 1, 2, 3, 4, 0, 0
539 };
540 return gNumPoints[cmd];
541}
542
543/**
544 * Path filling rules
545 */
546enum GrPathFill {
bsalomon@google.com47059542012-06-06 20:51:20 +0000547 kWinding_GrPathFill,
548 kEvenOdd_GrPathFill,
549 kInverseWinding_GrPathFill,
550 kInverseEvenOdd_GrPathFill,
551 kHairLine_GrPathFill,
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000552
bsalomon@google.com47059542012-06-06 20:51:20 +0000553 kGrPathFillCount
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000554};
555
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000556static inline GrPathFill GrNonInvertedFill(GrPathFill fill) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000557 static const GrPathFill gNonInvertedFills[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000558 kWinding_GrPathFill, // kWinding_GrPathFill
559 kEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
560 kWinding_GrPathFill, // kInverseWinding_GrPathFill
561 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
562 kHairLine_GrPathFill,// kHairLine_GrPathFill
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000563 };
bsalomon@google.com47059542012-06-06 20:51:20 +0000564 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
565 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
566 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
567 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
568 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
569 GR_STATIC_ASSERT(5 == kGrPathFillCount);
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000570 return gNonInvertedFills[fill];
571}
572
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000573static inline bool GrIsFillInverted(GrPathFill fill) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000574 static const bool gIsFillInverted[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000575 false, // kWinding_GrPathFill
576 false, // kEvenOdd_GrPathFill
577 true, // kInverseWinding_GrPathFill
578 true, // kInverseEvenOdd_GrPathFill
579 false, // kHairLine_GrPathFill
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000580 };
bsalomon@google.com47059542012-06-06 20:51:20 +0000581 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
582 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
583 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
584 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
585 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
586 GR_STATIC_ASSERT(5 == kGrPathFillCount);
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000587 return gIsFillInverted[fill];
588}
589
reed@google.comac10a2d2010-12-22 21:39:39 +0000590///////////////////////////////////////////////////////////////////////////////
591
bsalomon@google.come269f212011-11-07 13:29:52 +0000592// opaque type for 3D API object handles
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000593typedef intptr_t GrBackendObject;
bsalomon@google.come269f212011-11-07 13:29:52 +0000594
595/**
596 * Gr can wrap an existing texture created by the client with a GrTexture
597 * object. The client is responsible for ensuring that the texture lives at
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000598 * least as long as the GrTexture object wrapping it. We require the client to
599 * explicitly provide information about the texture, such as width, height,
bsalomon@google.come269f212011-11-07 13:29:52 +0000600 * and pixel config, rather than querying the 3D APIfor these values. We expect
601 * these to be immutable even if the 3D API doesn't require this (OpenGL).
602 *
603 * Textures that are also render targets are supported as well. Gr will manage
604 * any ancillary 3D API (stencil buffer, FBO id, etc) objects necessary for
605 * Gr to draw into the render target. To access the render target object
606 * call GrTexture::asRenderTarget().
607 *
608 * If in addition to the render target flag, the caller also specifies a sample
609 * count Gr will create an MSAA buffer that resolves into the texture. Gr auto-
610 * resolves when it reads from the texture. The client can explictly resolve
611 * using the GrRenderTarget interface.
robertphillips@google.com32716282012-06-04 12:48:45 +0000612 *
613 * Note: These flags currently form a subset of GrTexture's flags.
bsalomon@google.come269f212011-11-07 13:29:52 +0000614 */
615
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000616enum GrBackendTextureFlags {
bsalomon@google.come269f212011-11-07 13:29:52 +0000617 /**
618 * No flags enabled
619 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000620 kNone_GrBackendTextureFlag = kNone_GrTextureFlags,
bsalomon@google.come269f212011-11-07 13:29:52 +0000621 /**
622 * Indicates that the texture is also a render target, and thus should have
623 * a GrRenderTarget object.
624 *
625 * D3D (future): client must have created the texture with flags that allow
626 * it to be used as a render target.
627 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000628 kRenderTarget_GrBackendTextureFlag = kRenderTarget_GrTextureFlagBit,
bsalomon@google.come269f212011-11-07 13:29:52 +0000629};
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000630GR_MAKE_BITFIELD_OPS(GrBackendTextureFlags)
bsalomon@google.come269f212011-11-07 13:29:52 +0000631
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000632struct GrBackendTextureDesc {
633 GrBackendTextureDesc() { memset(this, 0, sizeof(*this)); }
634 GrBackendTextureFlags fFlags;
bsalomon@google.come269f212011-11-07 13:29:52 +0000635 int fWidth; //<! width in pixels
636 int fHeight; //<! height in pixels
637 GrPixelConfig fConfig; //<! color format
638 /**
639 * If the render target flag is set and sample count is greater than 0
640 * then Gr will create an MSAA buffer that resolves to the texture.
641 */
642 int fSampleCnt;
643 /**
644 * Handle to the 3D API object.
645 * OpenGL: Texture ID.
646 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000647 GrBackendObject fTextureHandle;
bsalomon@google.come269f212011-11-07 13:29:52 +0000648};
649
650///////////////////////////////////////////////////////////////////////////////
651
652/**
653 * Gr can wrap an existing render target created by the client in the 3D API
654 * with a GrRenderTarget object. The client is responsible for ensuring that the
655 * underlying 3D API object lives at least as long as the GrRenderTarget object
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000656 * wrapping it. We require the client to explicitly provide information about
bsalomon@google.come269f212011-11-07 13:29:52 +0000657 * the target, such as width, height, and pixel config rather than querying the
658 * 3D API for these values. We expect these properties to be immutable even if
659 * the 3D API doesn't require this (OpenGL).
660 */
661
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000662struct GrBackendRenderTargetDesc {
663 GrBackendRenderTargetDesc() { memset(this, 0, sizeof(*this)); }
bsalomon@google.come269f212011-11-07 13:29:52 +0000664 int fWidth; //<! width in pixels
665 int fHeight; //<! height in pixels
666 GrPixelConfig fConfig; //<! color format
667 /**
668 * The number of samples per pixel. Gr uses this to influence decisions
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000669 * about applying other forms of anti-aliasing.
bsalomon@google.come269f212011-11-07 13:29:52 +0000670 */
671 int fSampleCnt;
672 /**
673 * Number of bits of stencil per-pixel.
674 */
675 int fStencilBits;
676 /**
677 * Handle to the 3D API object.
678 * OpenGL: FBO ID
679 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000680 GrBackendObject fRenderTargetHandle;
bsalomon@google.come269f212011-11-07 13:29:52 +0000681};
682
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000683///////////////////////////////////////////////////////////////////////////////
684// Legacy names that will be kept until WebKit can be updated.
685
686typedef GrBackend GrEngine;
687static const GrBackend kOpenGL_Shaders_GrEngine = kOpenGL_GrBackend;
688
689typedef GrBackendContext GrPlatform3DContext;
690
691typedef GrBackendObject GrPlatform3DObject;
692
693typedef GrBackendTextureFlags GrPlatformTextureFlags;
694static const GrBackendTextureFlags kNone_GrPlatformTextureFlag = kNone_GrBackendTextureFlag;
695static const GrBackendTextureFlags kRenderTarget_GrPlatformTextureFlag = kRenderTarget_GrBackendTextureFlag;
696
697typedef GrBackendTextureDesc GrPlatformTextureDesc;
698
699typedef GrBackendRenderTargetDesc GrPlatformRenderTargetDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000700
701///////////////////////////////////////////////////////////////////////////////
702
reed@google.comac10a2d2010-12-22 21:39:39 +0000703// this is included only to make it easy to use this debugging facility
704#include "GrInstanceCounter.h"
705
706#endif