blob: 8bd1f2f8a8bdbc01164a439440a5e82190ebd0d7 [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"
16
bsalomon@google.comfea37b52011-04-25 15:51:06 +000017////////////////////////////////////////////////////////////////////////////////
18
19/**
20 * Defines overloaded bitwise operators to make it easier to use an enum as a
21 * bitfield.
22 */
23#define GR_MAKE_BITFIELD_OPS(X) \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000024 inline X operator | (X a, X b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000025 return (X) (+a | +b); \
26 } \
27 \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000028 inline X operator & (X a, X b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000029 return (X) (+a & +b); \
30 } \
31 template <typename T> \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000032 inline X operator & (T a, X b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000033 return (X) (+a & +b); \
34 } \
35 template <typename T> \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000036 inline X operator & (X a, T b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000037 return (X) (+a & +b); \
38 } \
39
bsalomon@google.com86c1f712011-10-12 14:54:26 +000040#define GR_DECL_BITFIELD_OPS_FRIENDS(X) \
41 friend X operator | (X a, X b); \
42 \
43 friend X operator & (X a, X b); \
44 \
45 template <typename T> \
46 friend X operator & (T a, X b); \
47 \
48 template <typename T> \
49 friend X operator & (X a, T b); \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000050////////////////////////////////////////////////////////////////////////////////
51
52
reed@google.comac10a2d2010-12-22 21:39:39 +000053/**
54 * Macro to round n up to the next multiple of 4, or return it unchanged if
55 * n is already a multiple of 4
56 */
reed@google.com9b24d252011-06-01 20:27:53 +000057#define GrALIGN4(n) SkAlign4(n)
tomhudson@google.com01224d52011-11-28 18:22:01 +000058#define GrIsALIGN4(n) SkIsAlign4(n)
reed@google.comac10a2d2010-12-22 21:39:39 +000059
60template <typename T> const T& GrMin(const T& a, const T& b) {
robertphillips@google.com07ef9112012-06-04 13:22:14 +000061 return (a < b) ? a : b;
reed@google.comac10a2d2010-12-22 21:39:39 +000062}
63
64template <typename T> const T& GrMax(const T& a, const T& b) {
robertphillips@google.com07ef9112012-06-04 13:22:14 +000065 return (b < a) ? a : b;
reed@google.comac10a2d2010-12-22 21:39:39 +000066}
67
68// compile time versions of min/max
69#define GR_CT_MAX(a, b) (((b) < (a)) ? (a) : (b))
70#define GR_CT_MIN(a, b) (((b) < (a)) ? (b) : (a))
71
72/**
73 * divide, rounding up
74 */
bsalomon@google.com91958362011-06-13 17:58:13 +000075static inline int32_t GrIDivRoundUp(int x, int y) {
76 GrAssert(y > 0);
77 return (x + (y-1)) / y;
78}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000079static inline uint32_t GrUIDivRoundUp(uint32_t x, uint32_t y) {
80 return (x + (y-1)) / y;
81}
bsalomon@google.com4da34e32012-06-19 15:40:27 +000082static inline size_t GrSizeDivRoundUp(size_t x, size_t y) {
reed@google.comac10a2d2010-12-22 21:39:39 +000083 return (x + (y-1)) / y;
84}
85
bsalomon@google.com4da34e32012-06-19 15:40:27 +000086// compile time, evaluates Y multiple times
87#define GR_CT_DIV_ROUND_UP(X, Y) (((X) + ((Y)-1)) / (Y))
88
reed@google.comac10a2d2010-12-22 21:39:39 +000089/**
90 * align up
91 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +000092static inline uint32_t GrUIAlignUp(uint32_t x, uint32_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +000093 return GrUIDivRoundUp(x, alignment) * alignment;
94}
bsalomon@google.com4da34e32012-06-19 15:40:27 +000095static inline size_t GrSizeAlignUp(size_t x, size_t alignment) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000096 return GrSizeDivRoundUp(x, alignment) * alignment;
97}
reed@google.comac10a2d2010-12-22 21:39:39 +000098
bsalomon@google.com4da34e32012-06-19 15:40:27 +000099// compile time, evaluates A multiple times
100#define GR_CT_ALIGN_UP(X, A) (GR_CT_DIV_ROUND_UP((X),(A)) * (A))
101
reed@google.comac10a2d2010-12-22 21:39:39 +0000102/**
103 * amount of pad needed to align up
104 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000105static inline uint32_t GrUIAlignUpPad(uint32_t x, uint32_t alignment) {
106 return (alignment - x % alignment) % alignment;
107}
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000108static inline size_t GrSizeAlignUpPad(size_t x, size_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 return (alignment - x % alignment) % alignment;
110}
111
112/**
113 * align down
114 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000115static inline uint32_t GrUIAlignDown(uint32_t x, uint32_t alignment) {
116 return (x / alignment) * alignment;
117}
bsalomon@google.com5e497322012-08-28 21:45:26 +0000118static inline size_t GrSizeAlignDown(size_t x, uint32_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000119 return (x / alignment) * alignment;
120}
121
122/**
123 * Count elements in an array
124 */
reed@google.com9b24d252011-06-01 20:27:53 +0000125#define GR_ARRAY_COUNT(array) SK_ARRAY_COUNT(array)
reed@google.comac10a2d2010-12-22 21:39:39 +0000126
127//!< allocate a block of memory, will never return NULL
128extern void* GrMalloc(size_t bytes);
129
130//!< free block allocated by GrMalloc. ptr may be NULL
131extern void GrFree(void* ptr);
132
133static inline void Gr_bzero(void* dst, size_t size) {
134 memset(dst, 0, size);
135}
136
137///////////////////////////////////////////////////////////////////////////////
138
139/**
140 * Return the number of leading zeros in n
141 */
142extern int Gr_clz(uint32_t n);
143
144/**
145 * Return true if n is a power of 2
146 */
147static inline bool GrIsPow2(unsigned n) {
148 return n && 0 == (n & (n - 1));
149}
150
151/**
152 * Return the next power of 2 >= n.
153 */
154static inline uint32_t GrNextPow2(uint32_t n) {
155 return n ? (1 << (32 - Gr_clz(n - 1))) : 1;
156}
157
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000158static inline int GrNextPow2(int n) {
159 GrAssert(n >= 0); // this impl only works for non-neg.
160 return n ? (1 << (32 - Gr_clz(n - 1))) : 1;
161}
162
reed@google.comac10a2d2010-12-22 21:39:39 +0000163///////////////////////////////////////////////////////////////////////////////
164
165/**
166 * 16.16 fixed point type
167 */
168typedef int32_t GrFixed;
169
170#if GR_DEBUG
171
172static inline int16_t GrToS16(intptr_t x) {
173 GrAssert((int16_t)x == x);
174 return (int16_t)x;
175}
176
177#else
178
179#define GrToS16(x) x
180
181#endif
182
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000183
184///////////////////////////////////////////////////////////////////////////////
185
reed@google.comac10a2d2010-12-22 21:39:39 +0000186/**
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000187 * Possible 3D APIs that may be used by Ganesh.
188 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000189enum GrBackend {
190 kOpenGL_GrBackend,
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000191};
192
193/**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000194 * Backend-specific 3D context handle
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000195 * GrGLInterface* for OpenGL. If NULL will use the default GL interface.
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000196 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000197typedef intptr_t GrBackendContext;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000198
199///////////////////////////////////////////////////////////////////////////////
200
201/**
reed@google.comac10a2d2010-12-22 21:39:39 +0000202 * Type used to describe format of vertices in arrays
203 * Values are defined in GrDrawTarget
204 */
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000205typedef int GrVertexLayout;
reed@google.comac10a2d2010-12-22 21:39:39 +0000206
bsalomon@google.comffca4002011-02-22 20:34:01 +0000207/**
bsalomon@google.comffca4002011-02-22 20:34:01 +0000208* Geometric primitives used for drawing.
209*/
210enum GrPrimitiveType {
bsalomon@google.com47059542012-06-06 20:51:20 +0000211 kTriangles_GrPrimitiveType,
212 kTriangleStrip_GrPrimitiveType,
213 kTriangleFan_GrPrimitiveType,
214 kPoints_GrPrimitiveType,
215 kLines_GrPrimitiveType, // 1 pix wide only
216 kLineStrip_GrPrimitiveType // 1 pix wide only
bsalomon@google.comffca4002011-02-22 20:34:01 +0000217};
218
bsalomon@google.com0650e812011-04-08 18:07:53 +0000219static inline bool GrIsPrimTypeLines(GrPrimitiveType type) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000220 return kLines_GrPrimitiveType == type || kLineStrip_GrPrimitiveType == type;
bsalomon@google.com0650e812011-04-08 18:07:53 +0000221}
222
223static inline bool GrIsPrimTypeTris(GrPrimitiveType type) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000224 return kTriangles_GrPrimitiveType == type ||
225 kTriangleStrip_GrPrimitiveType == type ||
226 kTriangleFan_GrPrimitiveType == type;
bsalomon@google.com0650e812011-04-08 18:07:53 +0000227}
228
bsalomon@google.comffca4002011-02-22 20:34:01 +0000229/**
230 * Coeffecients for alpha-blending.
231 */
232enum GrBlendCoeff {
bsalomon@google.com47059542012-06-06 20:51:20 +0000233 kInvalid_GrBlendCoeff = -1,
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000234
bsalomon@google.com47059542012-06-06 20:51:20 +0000235 kZero_GrBlendCoeff, //<! 0
236 kOne_GrBlendCoeff, //<! 1
237 kSC_GrBlendCoeff, //<! src color
238 kISC_GrBlendCoeff, //<! one minus src color
239 kDC_GrBlendCoeff, //<! dst color
240 kIDC_GrBlendCoeff, //<! one minus dst color
241 kSA_GrBlendCoeff, //<! src alpha
242 kISA_GrBlendCoeff, //<! one minus src alpha
243 kDA_GrBlendCoeff, //<! dst alpha
244 kIDA_GrBlendCoeff, //<! one minus dst alpha
245 kConstC_GrBlendCoeff, //<! constant color
246 kIConstC_GrBlendCoeff, //<! one minus constant color
247 kConstA_GrBlendCoeff, //<! constant color alpha
248 kIConstA_GrBlendCoeff, //<! one minus constant color alpha
bsalomon@google.com080773c2011-03-15 19:09:25 +0000249
bsalomon@google.com47059542012-06-06 20:51:20 +0000250 kPublicGrBlendCoeffCount
bsalomon@google.comffca4002011-02-22 20:34:01 +0000251};
252
bsalomon@google.comd302f142011-03-03 13:54:13 +0000253/**
reed@google.com759c16e2011-03-15 19:15:15 +0000254 * Formats for masks, used by the font cache.
255 * Important that these are 0-based.
reed@google.com98539c62011-03-15 15:40:16 +0000256 */
257enum GrMaskFormat {
caryclark@google.com1eeaf0b2011-06-22 13:19:43 +0000258 kA8_GrMaskFormat, //!< 1-byte per pixel
259 kA565_GrMaskFormat, //!< 2-bytes per pixel
260 kA888_GrMaskFormat, //!< 4-bytes per pixel
261
262 kCount_GrMaskFormats //!< used to allocate arrays sized for mask formats
reed@google.com98539c62011-03-15 15:40:16 +0000263};
264
265/**
266 * Return the number of bytes-per-pixel for the specified mask format.
267 */
268static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) {
reed@google.combbf12262011-10-04 20:14:57 +0000269 GrAssert((unsigned)format <= 2);
270 // kA8 (0) -> 1
271 // kA565 (1) -> 2
272 // kA888 (2) -> 4
273 return 1 << (int)format;
reed@google.com98539c62011-03-15 15:40:16 +0000274}
275
276/**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000277 * Pixel configurations.
278 */
279enum GrPixelConfig {
280 kUnknown_GrPixelConfig,
281 kAlpha_8_GrPixelConfig,
282 kIndex_8_GrPixelConfig,
283 kRGB_565_GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000284 /**
285 * Premultiplied
286 */
287 kRGBA_4444_GrPixelConfig,
288 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000289 * Premultiplied. Byte order is r,g,b,a.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000290 */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000291 kRGBA_8888_GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000292 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000293 * Premultiplied. Byte order is b,g,r,a.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000294 */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000295 kBGRA_8888_GrPixelConfig,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000296
297 kGrPixelConfigCount
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000298};
299
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000300// Aliases for pixel configs that match skia's byte order.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000301#ifndef SK_CPU_LENDIAN
302 #error "Skia gpu currently assumes little endian"
303#endif
304#if 24 == SK_A32_SHIFT && 16 == SK_R32_SHIFT && \
305 8 == SK_G32_SHIFT && 0 == SK_B32_SHIFT
bsalomon@google.com0342a852012-08-20 19:22:38 +0000306 static const GrPixelConfig kSkia8888_GrPixelConfig = kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000307#elif 24 == SK_A32_SHIFT && 16 == SK_B32_SHIFT && \
308 8 == SK_G32_SHIFT && 0 == SK_R32_SHIFT
bsalomon@google.com0342a852012-08-20 19:22:38 +0000309 static const GrPixelConfig kSkia8888_GrPixelConfig = kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000310#else
311 #error "SK_*32_SHIFT values must correspond to GL_BGRA or GL_RGBA format."
312#endif
313
bsalomon@google.com0342a852012-08-20 19:22:38 +0000314// This alias is deprecated and will be removed.
315static const GrPixelConfig kSkia8888_PM_GrPixelConfig = kSkia8888_GrPixelConfig;
316
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000317// Returns true if the pixel config has 8bit r,g,b,a components in that byte
318// order
319static inline bool GrPixelConfigIsRGBA8888(GrPixelConfig config) {
320 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000321 case kRGBA_8888_GrPixelConfig:
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000322 return true;
323 default:
324 return false;
325 }
326}
327
328// Returns true if the pixel config has 8bit b,g,r,a components in that byte
329// order
330static inline bool GrPixelConfigIsBGRA8888(GrPixelConfig config) {
331 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000332 case kBGRA_8888_GrPixelConfig:
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000333 return true;
334 default:
335 return false;
336 }
337}
338
339// Returns true if the pixel config is 32 bits per pixel
340static inline bool GrPixelConfigIs32Bit(GrPixelConfig config) {
341 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000342 case kRGBA_8888_GrPixelConfig:
343 case kBGRA_8888_GrPixelConfig:
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000344 return true;
345 default:
346 return false;
347 }
348}
349
350// Takes a config and returns the equivalent config with the R and B order
351// swapped if such a config exists. Otherwise, kUnknown_GrPixelConfig
352static inline GrPixelConfig GrPixelConfigSwapRAndB(GrPixelConfig config) {
353 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000354 case kBGRA_8888_GrPixelConfig:
355 return kRGBA_8888_GrPixelConfig;
356 case kRGBA_8888_GrPixelConfig:
357 return kBGRA_8888_GrPixelConfig;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000358 default:
359 return kUnknown_GrPixelConfig;
360 }
361}
362
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000363static inline size_t GrBytesPerPixel(GrPixelConfig config) {
364 switch (config) {
365 case kAlpha_8_GrPixelConfig:
366 case kIndex_8_GrPixelConfig:
367 return 1;
368 case kRGB_565_GrPixelConfig:
369 case kRGBA_4444_GrPixelConfig:
370 return 2;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000371 case kRGBA_8888_GrPixelConfig:
372 case kBGRA_8888_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000373 return 4;
374 default:
375 return 0;
376 }
377}
378
379static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
380 switch (config) {
381 case kRGB_565_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +0000382 return true;
383 default:
384 return false;
385 }
386}
387
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000388static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
389 switch (config) {
390 case kAlpha_8_GrPixelConfig:
391 return true;
392 default:
393 return false;
394 }
395}
396
397/**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000398 * Optional bitfield flags that can be passed to createTexture.
399 */
400enum GrTextureFlags {
401 kNone_GrTextureFlags = 0x0,
402 /**
403 * Creates a texture that can be rendered to as a GrRenderTarget. Use
404 * GrTexture::asRenderTarget() to access.
405 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000406 kRenderTarget_GrTextureFlagBit = 0x1,
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000407 /**
408 * By default all render targets have an associated stencil buffer that
409 * may be required for path filling. This flag overrides stencil buffer
410 * creation.
411 * MAKE THIS PRIVATE?
412 */
413 kNoStencil_GrTextureFlagBit = 0x2,
414 /**
415 * Hint that the CPU may modify this texture after creation.
416 */
417 kDynamicUpdate_GrTextureFlagBit = 0x4,
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000418
419 kDummy_GrTextureFlagBit,
420 kLastPublic_GrTextureFlagBit = kDummy_GrTextureFlagBit-1,
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000421};
422
423GR_MAKE_BITFIELD_OPS(GrTextureFlags)
424
425enum {
426 /**
427 * For Index8 pixel config, the colortable must be 256 entries
428 */
429 kGrColorTableSize = 256 * 4 //sizeof(GrColor)
430};
431
robertphillips@google.com5091b702012-08-09 10:49:39 +0000432
433/**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000434 * Describes a texture to be created.
435 */
436struct GrTextureDesc {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000437 GrTextureDesc()
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000438 : fFlags(kNone_GrTextureFlags)
439 , fWidth(0)
440 , fHeight(0)
441 , fConfig(kUnknown_GrPixelConfig)
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000442 , fSampleCnt(0) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000443 }
444
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000445 GrTextureFlags fFlags; //!< bitfield of TextureFlags
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +0000446 int fWidth; //!< Width of the texture
447 int fHeight; //!< Height of the texture
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000448
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000449 /**
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000450 * Format of source data of the texture. Not guaranteed to be the same as
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000451 * internal format used by 3D API.
452 */
bsalomon@google.com5bc34f02011-12-06 14:46:34 +0000453 GrPixelConfig fConfig;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000454
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000455 /**
456 * The number of samples per pixel or 0 to disable full scene AA. This only
457 * applies if the kRenderTarget_GrTextureFlagBit is set. The actual number
458 * of samples may not exactly match the request. The request will be rounded
459 * up to the next supported sample count, or down if it is larger than the
460 * max supportex count.
461 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000462 int fSampleCnt;
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000463};
464
465/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000466 * GrCacheData holds user-provided cache-specific data. It is used in
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000467 * combination with the GrTextureDesc to construct a cache key for texture
468 * resources.
469 */
470struct GrCacheData {
471 /*
472 * Scratch textures should all have this value as their fClientCacheID
473 */
474 static const uint64_t kScratch_CacheID = 0xBBBBBBBB;
475
476 /**
477 * Resources in the "scratch" domain can be used by any domain. All
478 * scratch textures will have this as their domain.
479 */
480 static const uint8_t kScratch_ResourceDomain = 0;
481
482
483 // No default constructor is provided since, if you are creating one
484 // of these, you should definitely have a key (or be using the scratch
485 // key).
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000486 GrCacheData(uint64_t key)
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000487 : fClientCacheID(key)
488 , fResourceDomain(kScratch_ResourceDomain) {
489 }
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000490
491 /**
492 * A user-provided texture ID. It should be unique to the texture data and
493 * does not need to take into account the width or height. Two textures
494 * with the same ID but different dimensions will not collide. This field
495 * is only relevant for textures that will be cached.
496 */
497 uint64_t fClientCacheID;
robertphillips@google.com5091b702012-08-09 10:49:39 +0000498
499 /**
500 * Allows cache clients to cluster their textures inside domains (e.g.,
501 * alpha clip masks). Only relevant for cached textures.
502 */
503 uint8_t fResourceDomain;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000504};
505
506/**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000507 * Clips are composed from these objects.
508 */
509enum GrClipType {
510 kRect_ClipType,
511 kPath_ClipType
512};
513
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000514/**
515 * Commands used to describe a path. Each command
516 * is accompanied by some number of points.
517 */
518enum GrPathCmd {
519 kMove_PathCmd, //!< Starts a new subpath at
520 // at the returned point
521 // 1 point
522 kLine_PathCmd, //!< Adds a line segment
523 // 2 points
524 kQuadratic_PathCmd, //!< Adds a quadratic segment
525 // 3 points
526 kCubic_PathCmd, //!< Adds a cubic segment
527 // 4 points
528 kClose_PathCmd, //!< Closes the current subpath
529 // by connecting a line to the
530 // starting point.
531 // 0 points
532 kEnd_PathCmd //!< Indicates the end of the last subpath
533 // when iterating
534 // 0 points.
535};
536
537/**
538 * Gets the number of points associated with a path command.
539 */
540static int inline NumPathCmdPoints(GrPathCmd cmd) {
541 static const int gNumPoints[] = {
542 1, 2, 3, 4, 0, 0
543 };
544 return gNumPoints[cmd];
545}
546
547/**
548 * Path filling rules
549 */
550enum GrPathFill {
bsalomon@google.com47059542012-06-06 20:51:20 +0000551 kWinding_GrPathFill,
552 kEvenOdd_GrPathFill,
553 kInverseWinding_GrPathFill,
554 kInverseEvenOdd_GrPathFill,
555 kHairLine_GrPathFill,
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000556
bsalomon@google.com47059542012-06-06 20:51:20 +0000557 kGrPathFillCount
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000558};
559
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000560static inline GrPathFill GrNonInvertedFill(GrPathFill fill) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000561 static const GrPathFill gNonInvertedFills[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000562 kWinding_GrPathFill, // kWinding_GrPathFill
563 kEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
564 kWinding_GrPathFill, // kInverseWinding_GrPathFill
565 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
566 kHairLine_GrPathFill,// kHairLine_GrPathFill
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000567 };
bsalomon@google.com47059542012-06-06 20:51:20 +0000568 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
569 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
570 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
571 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
572 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
573 GR_STATIC_ASSERT(5 == kGrPathFillCount);
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000574 return gNonInvertedFills[fill];
575}
576
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000577static inline bool GrIsFillInverted(GrPathFill fill) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000578 static const bool gIsFillInverted[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000579 false, // kWinding_GrPathFill
580 false, // kEvenOdd_GrPathFill
581 true, // kInverseWinding_GrPathFill
582 true, // kInverseEvenOdd_GrPathFill
583 false, // kHairLine_GrPathFill
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000584 };
bsalomon@google.com47059542012-06-06 20:51:20 +0000585 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
586 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
587 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
588 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
589 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
590 GR_STATIC_ASSERT(5 == kGrPathFillCount);
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000591 return gIsFillInverted[fill];
592}
593
reed@google.comac10a2d2010-12-22 21:39:39 +0000594///////////////////////////////////////////////////////////////////////////////
595
bsalomon@google.come269f212011-11-07 13:29:52 +0000596// opaque type for 3D API object handles
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000597typedef intptr_t GrBackendObject;
bsalomon@google.come269f212011-11-07 13:29:52 +0000598
599/**
600 * Gr can wrap an existing texture created by the client with a GrTexture
601 * object. The client is responsible for ensuring that the texture lives at
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000602 * least as long as the GrTexture object wrapping it. We require the client to
603 * explicitly provide information about the texture, such as width, height,
bsalomon@google.come269f212011-11-07 13:29:52 +0000604 * and pixel config, rather than querying the 3D APIfor these values. We expect
605 * these to be immutable even if the 3D API doesn't require this (OpenGL).
606 *
607 * Textures that are also render targets are supported as well. Gr will manage
608 * any ancillary 3D API (stencil buffer, FBO id, etc) objects necessary for
609 * Gr to draw into the render target. To access the render target object
610 * call GrTexture::asRenderTarget().
611 *
612 * If in addition to the render target flag, the caller also specifies a sample
613 * count Gr will create an MSAA buffer that resolves into the texture. Gr auto-
614 * resolves when it reads from the texture. The client can explictly resolve
615 * using the GrRenderTarget interface.
robertphillips@google.com32716282012-06-04 12:48:45 +0000616 *
617 * Note: These flags currently form a subset of GrTexture's flags.
bsalomon@google.come269f212011-11-07 13:29:52 +0000618 */
619
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000620enum GrBackendTextureFlags {
bsalomon@google.come269f212011-11-07 13:29:52 +0000621 /**
622 * No flags enabled
623 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000624 kNone_GrBackendTextureFlag = kNone_GrTextureFlags,
bsalomon@google.come269f212011-11-07 13:29:52 +0000625 /**
626 * Indicates that the texture is also a render target, and thus should have
627 * a GrRenderTarget object.
628 *
629 * D3D (future): client must have created the texture with flags that allow
630 * it to be used as a render target.
631 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000632 kRenderTarget_GrBackendTextureFlag = kRenderTarget_GrTextureFlagBit,
bsalomon@google.come269f212011-11-07 13:29:52 +0000633};
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000634GR_MAKE_BITFIELD_OPS(GrBackendTextureFlags)
bsalomon@google.come269f212011-11-07 13:29:52 +0000635
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000636struct GrBackendTextureDesc {
637 GrBackendTextureDesc() { memset(this, 0, sizeof(*this)); }
638 GrBackendTextureFlags fFlags;
bsalomon@google.come269f212011-11-07 13:29:52 +0000639 int fWidth; //<! width in pixels
640 int fHeight; //<! height in pixels
641 GrPixelConfig fConfig; //<! color format
642 /**
643 * If the render target flag is set and sample count is greater than 0
644 * then Gr will create an MSAA buffer that resolves to the texture.
645 */
646 int fSampleCnt;
647 /**
648 * Handle to the 3D API object.
649 * OpenGL: Texture ID.
650 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000651 GrBackendObject fTextureHandle;
bsalomon@google.come269f212011-11-07 13:29:52 +0000652};
653
654///////////////////////////////////////////////////////////////////////////////
655
656/**
657 * Gr can wrap an existing render target created by the client in the 3D API
658 * with a GrRenderTarget object. The client is responsible for ensuring that the
659 * underlying 3D API object lives at least as long as the GrRenderTarget object
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000660 * wrapping it. We require the client to explicitly provide information about
bsalomon@google.come269f212011-11-07 13:29:52 +0000661 * the target, such as width, height, and pixel config rather than querying the
662 * 3D API for these values. We expect these properties to be immutable even if
663 * the 3D API doesn't require this (OpenGL).
664 */
665
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000666struct GrBackendRenderTargetDesc {
667 GrBackendRenderTargetDesc() { memset(this, 0, sizeof(*this)); }
bsalomon@google.come269f212011-11-07 13:29:52 +0000668 int fWidth; //<! width in pixels
669 int fHeight; //<! height in pixels
670 GrPixelConfig fConfig; //<! color format
671 /**
672 * The number of samples per pixel. Gr uses this to influence decisions
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000673 * about applying other forms of anti-aliasing.
bsalomon@google.come269f212011-11-07 13:29:52 +0000674 */
675 int fSampleCnt;
676 /**
677 * Number of bits of stencil per-pixel.
678 */
679 int fStencilBits;
680 /**
681 * Handle to the 3D API object.
682 * OpenGL: FBO ID
683 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000684 GrBackendObject fRenderTargetHandle;
bsalomon@google.come269f212011-11-07 13:29:52 +0000685};
686
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000687///////////////////////////////////////////////////////////////////////////////
688// Legacy names that will be kept until WebKit can be updated.
689
690typedef GrBackend GrEngine;
691static const GrBackend kOpenGL_Shaders_GrEngine = kOpenGL_GrBackend;
692
693typedef GrBackendContext GrPlatform3DContext;
694
695typedef GrBackendObject GrPlatform3DObject;
696
697typedef GrBackendTextureFlags GrPlatformTextureFlags;
698static const GrBackendTextureFlags kNone_GrPlatformTextureFlag = kNone_GrBackendTextureFlag;
699static const GrBackendTextureFlags kRenderTarget_GrPlatformTextureFlag = kRenderTarget_GrBackendTextureFlag;
700
701typedef GrBackendTextureDesc GrPlatformTextureDesc;
702
703typedef GrBackendRenderTargetDesc GrPlatformRenderTargetDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000704
705///////////////////////////////////////////////////////////////////////////////
706
reed@google.comac10a2d2010-12-22 21:39:39 +0000707// this is included only to make it easy to use this debugging facility
708#include "GrInstanceCounter.h"
709
710#endif