blob: 850d200886893bff530b1e7c05dec77a41aa2aa2 [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
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000018//#define SK_SUPPORT_LEGACY_GRTYPES
19
bsalomon@google.comfea37b52011-04-25 15:51:06 +000020////////////////////////////////////////////////////////////////////////////////
21
22/**
23 * Defines overloaded bitwise operators to make it easier to use an enum as a
24 * bitfield.
25 */
26#define GR_MAKE_BITFIELD_OPS(X) \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000027 inline X operator | (X a, X b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000028 return (X) (+a | +b); \
29 } \
30 \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000031 inline X operator & (X a, X b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000032 return (X) (+a & +b); \
33 } \
34 template <typename T> \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000035 inline X operator & (T a, X b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000036 return (X) (+a & +b); \
37 } \
38 template <typename T> \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000039 inline X operator & (X a, T b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000040 return (X) (+a & +b); \
41 } \
42
bsalomon@google.com86c1f712011-10-12 14:54:26 +000043#define GR_DECL_BITFIELD_OPS_FRIENDS(X) \
44 friend X operator | (X a, X b); \
45 \
46 friend X operator & (X a, X b); \
47 \
48 template <typename T> \
49 friend X operator & (T a, X b); \
50 \
51 template <typename T> \
52 friend X operator & (X a, T b); \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000053////////////////////////////////////////////////////////////////////////////////
54
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000055#ifdef SK_SUPPORT_LEGACY_GRTYPES
bsalomon@google.comfea37b52011-04-25 15:51:06 +000056
reed@google.comac10a2d2010-12-22 21:39:39 +000057/**
58 * Macro to round n up to the next multiple of 4, or return it unchanged if
59 * n is already a multiple of 4
60 */
reed@google.com9b24d252011-06-01 20:27:53 +000061#define GrALIGN4(n) SkAlign4(n)
tomhudson@google.com01224d52011-11-28 18:22:01 +000062#define GrIsALIGN4(n) SkIsAlign4(n)
reed@google.comac10a2d2010-12-22 21:39:39 +000063
64template <typename T> const T& GrMin(const T& a, const T& b) {
robertphillips@google.com07ef9112012-06-04 13:22:14 +000065 return (a < b) ? a : b;
reed@google.comac10a2d2010-12-22 21:39:39 +000066}
67
68template <typename T> const T& GrMax(const T& a, const T& b) {
robertphillips@google.com07ef9112012-06-04 13:22:14 +000069 return (b < a) ? a : b;
reed@google.comac10a2d2010-12-22 21:39:39 +000070}
71
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000072/**
73 * Count elements in an array
74 */
75#define GR_ARRAY_COUNT(array) SK_ARRAY_COUNT(array)
76
77/**
78 * 16.16 fixed point type
79 */
80typedef int32_t GrFixed;
81
82#ifdef SK_DEBUG
83
84static inline int16_t GrToS16(intptr_t x) {
85 SkASSERT((int16_t)x == x);
86 return (int16_t)x;
87}
88
89#else
90
91#define GrToS16(x) x
92
93#endif
94
95#endif
96
reed@google.comac10a2d2010-12-22 21:39:39 +000097// compile time versions of min/max
98#define GR_CT_MAX(a, b) (((b) < (a)) ? (a) : (b))
99#define GR_CT_MIN(a, b) (((b) < (a)) ? (b) : (a))
100
101/**
102 * divide, rounding up
103 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000104static inline int32_t GrIDivRoundUp(int x, int y) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000105 SkASSERT(y > 0);
bsalomon@google.com91958362011-06-13 17:58:13 +0000106 return (x + (y-1)) / y;
107}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000108static inline uint32_t GrUIDivRoundUp(uint32_t x, uint32_t y) {
109 return (x + (y-1)) / y;
110}
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000111static inline size_t GrSizeDivRoundUp(size_t x, size_t y) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000112 return (x + (y-1)) / y;
113}
114
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000115// compile time, evaluates Y multiple times
116#define GR_CT_DIV_ROUND_UP(X, Y) (((X) + ((Y)-1)) / (Y))
117
reed@google.comac10a2d2010-12-22 21:39:39 +0000118/**
119 * align up
120 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000121static inline uint32_t GrUIAlignUp(uint32_t x, uint32_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000122 return GrUIDivRoundUp(x, alignment) * alignment;
123}
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000124static inline size_t GrSizeAlignUp(size_t x, size_t alignment) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000125 return GrSizeDivRoundUp(x, alignment) * alignment;
126}
reed@google.comac10a2d2010-12-22 21:39:39 +0000127
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000128// compile time, evaluates A multiple times
129#define GR_CT_ALIGN_UP(X, A) (GR_CT_DIV_ROUND_UP((X),(A)) * (A))
130
reed@google.comac10a2d2010-12-22 21:39:39 +0000131/**
132 * amount of pad needed to align up
133 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000134static inline uint32_t GrUIAlignUpPad(uint32_t x, uint32_t alignment) {
135 return (alignment - x % alignment) % alignment;
136}
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000137static inline size_t GrSizeAlignUpPad(size_t x, size_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000138 return (alignment - x % alignment) % alignment;
139}
140
141/**
142 * align down
143 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000144static inline uint32_t GrUIAlignDown(uint32_t x, uint32_t alignment) {
145 return (x / alignment) * alignment;
146}
bsalomon@google.com5e497322012-08-28 21:45:26 +0000147static inline size_t GrSizeAlignDown(size_t x, uint32_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000148 return (x / alignment) * alignment;
149}
150
reed@google.comac10a2d2010-12-22 21:39:39 +0000151///////////////////////////////////////////////////////////////////////////////
152
153/**
reed@google.comac10a2d2010-12-22 21:39:39 +0000154 * Return the next power of 2 >= n.
155 */
156static inline uint32_t GrNextPow2(uint32_t n) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000157 return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000158}
159
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000160static inline int GrNextPow2(int n) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000161 SkASSERT(n >= 0); // this impl only works for non-neg.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000162 return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000163}
164
reed@google.comac10a2d2010-12-22 21:39:39 +0000165///////////////////////////////////////////////////////////////////////////////
166
167/**
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000168 * Possible 3D APIs that may be used by Ganesh.
169 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000170enum GrBackend {
171 kOpenGL_GrBackend,
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000172};
173
174/**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000175 * Backend-specific 3D context handle
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000176 * GrGLInterface* for OpenGL. If NULL will use the default GL interface.
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000177 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000178typedef intptr_t GrBackendContext;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000179
180///////////////////////////////////////////////////////////////////////////////
181
182/**
bsalomon@google.comffca4002011-02-22 20:34:01 +0000183* Geometric primitives used for drawing.
184*/
185enum GrPrimitiveType {
bsalomon@google.com47059542012-06-06 20:51:20 +0000186 kTriangles_GrPrimitiveType,
187 kTriangleStrip_GrPrimitiveType,
188 kTriangleFan_GrPrimitiveType,
189 kPoints_GrPrimitiveType,
190 kLines_GrPrimitiveType, // 1 pix wide only
191 kLineStrip_GrPrimitiveType // 1 pix wide only
bsalomon@google.comffca4002011-02-22 20:34:01 +0000192};
193
bsalomon@google.com0650e812011-04-08 18:07:53 +0000194static inline bool GrIsPrimTypeLines(GrPrimitiveType type) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000195 return kLines_GrPrimitiveType == type || kLineStrip_GrPrimitiveType == type;
bsalomon@google.com0650e812011-04-08 18:07:53 +0000196}
197
198static inline bool GrIsPrimTypeTris(GrPrimitiveType type) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000199 return kTriangles_GrPrimitiveType == type ||
200 kTriangleStrip_GrPrimitiveType == type ||
201 kTriangleFan_GrPrimitiveType == type;
bsalomon@google.com0650e812011-04-08 18:07:53 +0000202}
203
bsalomon@google.comffca4002011-02-22 20:34:01 +0000204/**
205 * Coeffecients for alpha-blending.
206 */
207enum GrBlendCoeff {
bsalomon@google.com47059542012-06-06 20:51:20 +0000208 kInvalid_GrBlendCoeff = -1,
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000209
bsalomon@google.com47059542012-06-06 20:51:20 +0000210 kZero_GrBlendCoeff, //<! 0
211 kOne_GrBlendCoeff, //<! 1
212 kSC_GrBlendCoeff, //<! src color
213 kISC_GrBlendCoeff, //<! one minus src color
214 kDC_GrBlendCoeff, //<! dst color
215 kIDC_GrBlendCoeff, //<! one minus dst color
216 kSA_GrBlendCoeff, //<! src alpha
217 kISA_GrBlendCoeff, //<! one minus src alpha
218 kDA_GrBlendCoeff, //<! dst alpha
219 kIDA_GrBlendCoeff, //<! one minus dst alpha
220 kConstC_GrBlendCoeff, //<! constant color
221 kIConstC_GrBlendCoeff, //<! one minus constant color
222 kConstA_GrBlendCoeff, //<! constant color alpha
223 kIConstA_GrBlendCoeff, //<! one minus constant color alpha
bsalomon@google.com080773c2011-03-15 19:09:25 +0000224
joshualitt65171342014-10-09 07:25:36 -0700225 kFirstPublicGrBlendCoeff = kZero_GrBlendCoeff,
226 kLastPublicGrBlendCoeff = kIConstA_GrBlendCoeff,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000227};
joshualitt65171342014-10-09 07:25:36 -0700228static const int kPublicGrBlendCoeffCount = kLastPublicGrBlendCoeff + 1;
bsalomon@google.comffca4002011-02-22 20:34:01 +0000229
bsalomon@google.comd302f142011-03-03 13:54:13 +0000230/**
reed@google.com759c16e2011-03-15 19:15:15 +0000231 * Formats for masks, used by the font cache.
232 * Important that these are 0-based.
reed@google.com98539c62011-03-15 15:40:16 +0000233 */
234enum GrMaskFormat {
caryclark@google.com1eeaf0b2011-06-22 13:19:43 +0000235 kA8_GrMaskFormat, //!< 1-byte per pixel
236 kA565_GrMaskFormat, //!< 2-bytes per pixel
237 kA888_GrMaskFormat, //!< 4-bytes per pixel
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000238 kARGB_GrMaskFormat, //!< 4-bytes per pixel, color format
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +0000239
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000240 kLast_GrMaskFormat = kARGB_GrMaskFormat
reed@google.com98539c62011-03-15 15:40:16 +0000241};
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000242static const int kMaskFormatCount = kLast_GrMaskFormat + 1;
reed@google.com98539c62011-03-15 15:40:16 +0000243
244/**
245 * Return the number of bytes-per-pixel for the specified mask format.
246 */
247static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) {
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000248 SkASSERT((unsigned)format <= 3);
reed@google.combbf12262011-10-04 20:14:57 +0000249 // kA8 (0) -> 1
250 // kA565 (1) -> 2
251 // kA888 (2) -> 4
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +0000252 // kARGB (3) -> 4
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000253 static const int sBytesPerPixel[] = { 1, 2, 4, 4 };
254 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sBytesPerPixel) == kMaskFormatCount, array_size_mismatch);
255
256 return sBytesPerPixel[(int) format];
reed@google.com98539c62011-03-15 15:40:16 +0000257}
258
259/**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000260 * Pixel configurations.
261 */
262enum GrPixelConfig {
263 kUnknown_GrPixelConfig,
264 kAlpha_8_GrPixelConfig,
265 kIndex_8_GrPixelConfig,
266 kRGB_565_GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000267 /**
268 * Premultiplied
269 */
270 kRGBA_4444_GrPixelConfig,
271 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000272 * Premultiplied. Byte order is r,g,b,a.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000273 */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000274 kRGBA_8888_GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000275 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000276 * Premultiplied. Byte order is b,g,r,a.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000277 */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000278 kBGRA_8888_GrPixelConfig,
joshualittee5da552014-07-16 13:32:56 -0700279 /**
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000280 * ETC1 Compressed Data
281 */
282 kETC1_GrPixelConfig,
283 /**
284 * LATC/RGTC/3Dc/BC4 Compressed Data
285 */
286 kLATC_GrPixelConfig,
krajcevski238b4562014-06-30 09:09:22 -0700287 /**
288 * R11 EAC Compressed Data
289 * (Corresponds to section C.3.5 of the OpenGL 4.4 core profile spec)
290 */
291 kR11_EAC_GrPixelConfig,
krajcevski7ef21622014-07-16 15:21:13 -0700292
293 /**
294 * 12x12 ASTC Compressed Data
295 * ASTC stands for Adaptive Scalable Texture Compression. It is a technique
296 * that allows for a lot of customization in the compressed representataion
297 * of a block. The only thing fixed in the representation is the block size,
298 * which means that a texture that contains ASTC data must be treated as
299 * having RGBA values. However, there are single-channel encodings which set
300 * the alpha to opaque and all three RGB channels equal effectively making the
301 * compression format a single channel such as R11 EAC and LATC.
302 */
303 kASTC_12x12_GrPixelConfig,
304
joshualittee5da552014-07-16 13:32:56 -0700305 /**
306 * Byte order is r, g, b, a. This color format is 32 bits per channel
307 */
308 kRGBA_float_GrPixelConfig,
309 kLast_GrPixelConfig = kRGBA_float_GrPixelConfig
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000310};
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +0000311static const int kGrPixelConfigCnt = kLast_GrPixelConfig + 1;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000312
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000313// Aliases for pixel configs that match skia's byte order.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000314#ifndef SK_CPU_LENDIAN
315 #error "Skia gpu currently assumes little endian"
316#endif
commit-bot@chromium.orge4657ed2013-03-19 14:16:31 +0000317#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
bsalomon@google.com0342a852012-08-20 19:22:38 +0000318 static const GrPixelConfig kSkia8888_GrPixelConfig = kBGRA_8888_GrPixelConfig;
commit-bot@chromium.orge4657ed2013-03-19 14:16:31 +0000319#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
bsalomon@google.com0342a852012-08-20 19:22:38 +0000320 static const GrPixelConfig kSkia8888_GrPixelConfig = kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000321#else
322 #error "SK_*32_SHIFT values must correspond to GL_BGRA or GL_RGBA format."
323#endif
324
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000325// Returns true if the pixel config is a GPU-specific compressed format
326// representation.
327static inline bool GrPixelConfigIsCompressed(GrPixelConfig config) {
328 switch (config) {
bsalomond4cb9222014-08-11 14:19:09 -0700329 case kIndex_8_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000330 case kETC1_GrPixelConfig:
331 case kLATC_GrPixelConfig:
krajcevski238b4562014-06-30 09:09:22 -0700332 case kR11_EAC_GrPixelConfig:
krajcevski7ef21622014-07-16 15:21:13 -0700333 case kASTC_12x12_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000334 return true;
335 default:
336 return false;
337 }
338}
339
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000340// Returns true if the pixel config is 32 bits per pixel
bsalomon@google.com9c680582013-02-06 18:17:50 +0000341static inline bool GrPixelConfigIs8888(GrPixelConfig config) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000342 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000343 case kRGBA_8888_GrPixelConfig:
344 case kBGRA_8888_GrPixelConfig:
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000345 return true;
346 default:
347 return false;
348 }
349}
350
351// Takes a config and returns the equivalent config with the R and B order
352// swapped if such a config exists. Otherwise, kUnknown_GrPixelConfig
353static inline GrPixelConfig GrPixelConfigSwapRAndB(GrPixelConfig config) {
354 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000355 case kBGRA_8888_GrPixelConfig:
356 return kRGBA_8888_GrPixelConfig;
357 case kRGBA_8888_GrPixelConfig:
358 return kBGRA_8888_GrPixelConfig;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000359 default:
360 return kUnknown_GrPixelConfig;
361 }
362}
363
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000364static inline size_t GrBytesPerPixel(GrPixelConfig config) {
bsalomond4cb9222014-08-11 14:19:09 -0700365 SkASSERT(!GrPixelConfigIsCompressed(config));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000366 switch (config) {
367 case kAlpha_8_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000368 return 1;
369 case kRGB_565_GrPixelConfig:
370 case kRGBA_4444_GrPixelConfig:
371 return 2;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000372 case kRGBA_8888_GrPixelConfig:
373 case kBGRA_8888_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000374 return 4;
joshualittee5da552014-07-16 13:32:56 -0700375 case kRGBA_float_GrPixelConfig:
376 return 16;
377 default:
378 return 0;
379 }
380}
381
382static inline size_t GrUnpackAlignment(GrPixelConfig config) {
bsalomond4cb9222014-08-11 14:19:09 -0700383 SkASSERT(!GrPixelConfigIsCompressed(config));
joshualittee5da552014-07-16 13:32:56 -0700384 switch (config) {
385 case kAlpha_8_GrPixelConfig:
joshualittee5da552014-07-16 13:32:56 -0700386 return 1;
387 case kRGB_565_GrPixelConfig:
388 case kRGBA_4444_GrPixelConfig:
389 return 2;
390 case kRGBA_8888_GrPixelConfig:
391 case kBGRA_8888_GrPixelConfig:
392 case kRGBA_float_GrPixelConfig:
393 return 4;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000394 default:
395 return 0;
396 }
397}
398
399static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
400 switch (config) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000401 case kETC1_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000402 case kRGB_565_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +0000403 return true;
404 default:
405 return false;
406 }
407}
408
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000409static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
410 switch (config) {
krajcevski238b4562014-06-30 09:09:22 -0700411 case kR11_EAC_GrPixelConfig:
krajcevski748e9d32014-06-09 08:32:34 -0700412 case kLATC_GrPixelConfig:
krajcevski2a413df2014-07-24 08:16:00 -0700413 case kASTC_12x12_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000414 case kAlpha_8_GrPixelConfig:
415 return true;
416 default:
417 return false;
418 }
419}
420
421/**
bsalomonf2703d82014-10-28 14:33:06 -0700422 * Optional bitfield flags that can be set on GrSurfaceDesc (below).
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000423 */
bsalomonf2703d82014-10-28 14:33:06 -0700424enum GrSurfaceFlags {
425 kNone_GrSurfaceFlags = 0x0,
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000426 /**
427 * Creates a texture that can be rendered to as a GrRenderTarget. Use
428 * GrTexture::asRenderTarget() to access.
429 */
bsalomonf2703d82014-10-28 14:33:06 -0700430 kRenderTarget_GrSurfaceFlag = 0x1,
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000431 /**
432 * By default all render targets have an associated stencil buffer that
433 * may be required for path filling. This flag overrides stencil buffer
434 * creation.
435 * MAKE THIS PRIVATE?
436 */
bsalomonf2703d82014-10-28 14:33:06 -0700437 kNoStencil_GrSurfaceFlag = 0x2,
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000438 /**
439 * Indicates that all allocations (color buffer, FBO completeness, etc)
440 * should be verified.
441 */
bsalomonf2703d82014-10-28 14:33:06 -0700442 kCheckAllocation_GrSurfaceFlag = 0x4,
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000443};
444
bsalomonf2703d82014-10-28 14:33:06 -0700445GR_MAKE_BITFIELD_OPS(GrSurfaceFlags)
446
447// Legacy aliases
448typedef GrSurfaceFlags GrTextureFlags;
449static const GrSurfaceFlags kNone_GrTextureFlags = kNone_GrSurfaceFlags;
450static const GrSurfaceFlags kRenderTarget_GrTExtureFlagBit = kRenderTarget_GrSurfaceFlag;
451static const GrSurfaceFlags kNoStencil_GrTextureFlagBit = kNoStencil_GrSurfaceFlag;
452static const GrSurfaceFlags kCheckAllocation_GrTextureFlagBit = kCheckAllocation_GrSurfaceFlag;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000453
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000454/**
455 * Some textures will be stored such that the upper and left edges of the content meet at the
456 * the origin (in texture coord space) and for other textures the lower and left edges meet at
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000457 * the origin. kDefault_GrSurfaceOrigin sets textures to TopLeft, and render targets
458 * to BottomLeft.
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000459 */
460
461enum GrSurfaceOrigin {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000462 kDefault_GrSurfaceOrigin, // DEPRECATED; to be removed
robertphillips@google.comcf9faf62013-02-05 14:05:06 +0000463 kTopLeft_GrSurfaceOrigin,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000464 kBottomLeft_GrSurfaceOrigin,
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000465};
robertphillips@google.com5091b702012-08-09 10:49:39 +0000466
467/**
bsalomonf2703d82014-10-28 14:33:06 -0700468 * Describes a surface to be created.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000469 */
bsalomonf2703d82014-10-28 14:33:06 -0700470struct GrSurfaceDesc {
471 GrSurfaceDesc()
472 : fFlags(kNone_GrSurfaceFlags)
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000473 , fOrigin(kDefault_GrSurfaceOrigin)
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000474 , fWidth(0)
475 , fHeight(0)
476 , fConfig(kUnknown_GrPixelConfig)
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000477 , fSampleCnt(0) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000478 }
479
bsalomonf2703d82014-10-28 14:33:06 -0700480 GrSurfaceFlags fFlags; //!< bitfield of TextureFlags
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000481 GrSurfaceOrigin fOrigin; //!< origin of the texture
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +0000482 int fWidth; //!< Width of the texture
483 int fHeight; //!< Height of the texture
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000484
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000485 /**
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000486 * Format of source data of the texture. Not guaranteed to be the same as
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000487 * internal format used by 3D API.
488 */
bsalomon@google.com5bc34f02011-12-06 14:46:34 +0000489 GrPixelConfig fConfig;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000490
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000491 /**
492 * The number of samples per pixel or 0 to disable full scene AA. This only
bsalomonf2703d82014-10-28 14:33:06 -0700493 * applies if the kRenderTarget_GrSurfaceFlag is set. The actual number
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000494 * of samples may not exactly match the request. The request will be rounded
495 * up to the next supported sample count, or down if it is larger than the
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000496 * max supported count.
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000497 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000498 int fSampleCnt;
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000499};
500
bsalomonf2703d82014-10-28 14:33:06 -0700501// Legacy alias
502typedef GrSurfaceDesc GrTextureDesc;
503
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000504/**
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000505 * GrCacheID is used create and find cached GrResources (e.g. GrTextures). The ID has two parts:
506 * the domain and the key. Domains simply allow multiple clients to use 0-based indices as their
507 * cache key without colliding. The key uniquely identifies a GrResource within the domain.
508 * Users of the cache must obtain a domain via GenerateDomain().
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000509 */
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000510struct GrCacheID {
511public:
512 typedef uint8_t Domain;
513
514 struct Key {
515 union {
516 uint8_t fData8[16];
517 uint32_t fData32[4];
518 uint64_t fData64[2];
519 };
520 };
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000521
522 /**
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000523 * A default cache ID is invalid; a set method must be called before the object is used.
524 */
525 GrCacheID() { fDomain = kInvalid_Domain; }
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000526
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000527 /**
528 * Initialize the cache ID to a domain and key.
529 */
530 GrCacheID(Domain domain, const Key& key) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000531 SkASSERT(kInvalid_Domain != domain);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000532 this->reset(domain, key);
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000533 }
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000534
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000535 void reset(Domain domain, const Key& key) {
536 fDomain = domain;
537 memcpy(&fKey, &key, sizeof(Key));
538 }
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000539
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000540 /** Has this been initialized to a valid domain */
541 bool isValid() const { return kInvalid_Domain != fDomain; }
542
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000543 const Key& getKey() const { SkASSERT(this->isValid()); return fKey; }
544 Domain getDomain() const { SkASSERT(this->isValid()); return fDomain; }
skia.committer@gmail.com2859eb72012-12-21 02:01:28 +0000545
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000546 /** Creates a new unique ID domain. */
547 static Domain GenerateDomain();
548
549private:
550 Key fKey;
551 Domain fDomain;
552
553 static const Domain kInvalid_Domain = 0;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000554};
555
556/**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000557 * Clips are composed from these objects.
558 */
559enum GrClipType {
560 kRect_ClipType,
561 kPath_ClipType
562};
563
reed@google.comac10a2d2010-12-22 21:39:39 +0000564///////////////////////////////////////////////////////////////////////////////
565
bsalomon@google.come269f212011-11-07 13:29:52 +0000566// opaque type for 3D API object handles
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000567typedef intptr_t GrBackendObject;
bsalomon@google.come269f212011-11-07 13:29:52 +0000568
569/**
570 * Gr can wrap an existing texture created by the client with a GrTexture
571 * object. The client is responsible for ensuring that the texture lives at
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000572 * least as long as the GrTexture object wrapping it. We require the client to
573 * explicitly provide information about the texture, such as width, height,
bsalomon@google.come269f212011-11-07 13:29:52 +0000574 * and pixel config, rather than querying the 3D APIfor these values. We expect
575 * these to be immutable even if the 3D API doesn't require this (OpenGL).
576 *
577 * Textures that are also render targets are supported as well. Gr will manage
578 * any ancillary 3D API (stencil buffer, FBO id, etc) objects necessary for
579 * Gr to draw into the render target. To access the render target object
580 * call GrTexture::asRenderTarget().
581 *
582 * If in addition to the render target flag, the caller also specifies a sample
583 * count Gr will create an MSAA buffer that resolves into the texture. Gr auto-
584 * resolves when it reads from the texture. The client can explictly resolve
585 * using the GrRenderTarget interface.
robertphillips@google.com32716282012-06-04 12:48:45 +0000586 *
587 * Note: These flags currently form a subset of GrTexture's flags.
bsalomon@google.come269f212011-11-07 13:29:52 +0000588 */
589
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000590enum GrBackendTextureFlags {
bsalomon@google.come269f212011-11-07 13:29:52 +0000591 /**
592 * No flags enabled
593 */
bsalomonf2703d82014-10-28 14:33:06 -0700594 kNone_GrBackendTextureFlag = 0,
bsalomon@google.come269f212011-11-07 13:29:52 +0000595 /**
596 * Indicates that the texture is also a render target, and thus should have
597 * a GrRenderTarget object.
bsalomon@google.come269f212011-11-07 13:29:52 +0000598 */
bsalomonf2703d82014-10-28 14:33:06 -0700599 kRenderTarget_GrBackendTextureFlag = kRenderTarget_GrSurfaceFlag,
bsalomon@google.come269f212011-11-07 13:29:52 +0000600};
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000601GR_MAKE_BITFIELD_OPS(GrBackendTextureFlags)
bsalomon@google.come269f212011-11-07 13:29:52 +0000602
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000603struct GrBackendTextureDesc {
604 GrBackendTextureDesc() { memset(this, 0, sizeof(*this)); }
605 GrBackendTextureFlags fFlags;
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000606 GrSurfaceOrigin fOrigin;
bsalomon@google.come269f212011-11-07 13:29:52 +0000607 int fWidth; //<! width in pixels
608 int fHeight; //<! height in pixels
609 GrPixelConfig fConfig; //<! color format
610 /**
611 * If the render target flag is set and sample count is greater than 0
612 * then Gr will create an MSAA buffer that resolves to the texture.
613 */
614 int fSampleCnt;
615 /**
616 * Handle to the 3D API object.
617 * OpenGL: Texture ID.
618 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000619 GrBackendObject fTextureHandle;
bsalomon@google.come269f212011-11-07 13:29:52 +0000620};
621
622///////////////////////////////////////////////////////////////////////////////
623
624/**
625 * Gr can wrap an existing render target created by the client in the 3D API
626 * with a GrRenderTarget object. The client is responsible for ensuring that the
627 * underlying 3D API object lives at least as long as the GrRenderTarget object
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000628 * wrapping it. We require the client to explicitly provide information about
bsalomon@google.come269f212011-11-07 13:29:52 +0000629 * the target, such as width, height, and pixel config rather than querying the
630 * 3D API for these values. We expect these properties to be immutable even if
631 * the 3D API doesn't require this (OpenGL).
632 */
633
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000634struct GrBackendRenderTargetDesc {
635 GrBackendRenderTargetDesc() { memset(this, 0, sizeof(*this)); }
bsalomon@google.come269f212011-11-07 13:29:52 +0000636 int fWidth; //<! width in pixels
637 int fHeight; //<! height in pixels
638 GrPixelConfig fConfig; //<! color format
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000639 GrSurfaceOrigin fOrigin; //<! pixel origin
bsalomon@google.come269f212011-11-07 13:29:52 +0000640 /**
641 * The number of samples per pixel. Gr uses this to influence decisions
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000642 * about applying other forms of anti-aliasing.
bsalomon@google.come269f212011-11-07 13:29:52 +0000643 */
644 int fSampleCnt;
645 /**
646 * Number of bits of stencil per-pixel.
647 */
648 int fStencilBits;
649 /**
650 * Handle to the 3D API object.
651 * OpenGL: FBO ID
652 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000653 GrBackendObject fRenderTargetHandle;
bsalomon@google.come269f212011-11-07 13:29:52 +0000654};
655
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000656/**
657 * The GrContext's cache of backend context state can be partially invalidated.
658 * These enums are specific to the GL backend and we'd add a new set for an alternative backend.
659 */
660enum GrGLBackendState {
661 kRenderTarget_GrGLBackendState = 1 << 0,
662 kTextureBinding_GrGLBackendState = 1 << 1,
663 // View state stands for scissor and viewport
664 kView_GrGLBackendState = 1 << 2,
665 kBlend_GrGLBackendState = 1 << 3,
egdanielb414f252014-07-29 13:15:47 -0700666 kMSAAEnable_GrGLBackendState = 1 << 4,
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000667 kVertex_GrGLBackendState = 1 << 5,
668 kStencil_GrGLBackendState = 1 << 6,
669 kPixelStore_GrGLBackendState = 1 << 7,
670 kProgram_GrGLBackendState = 1 << 8,
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000671 kFixedFunction_GrGLBackendState = 1 << 9,
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000672 kMisc_GrGLBackendState = 1 << 10,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000673 kPathRendering_GrGLBackendState = 1 << 11,
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000674 kALL_GrGLBackendState = 0xffff
675};
676
677/**
krajcevski9c0e6292014-06-02 07:38:14 -0700678 * Returns the data size for the given compressed pixel config
joshualittee5da552014-07-16 13:32:56 -0700679 */
krajcevski9c0e6292014-06-02 07:38:14 -0700680static inline size_t GrCompressedFormatDataSize(GrPixelConfig config,
681 int width, int height) {
682 SkASSERT(GrPixelConfigIsCompressed(config));
bsalomond4cb9222014-08-11 14:19:09 -0700683 static const int kGrIndex8TableSize = 256 * 4; // 4 == sizeof(GrColor)
krajcevski9c0e6292014-06-02 07:38:14 -0700684
685 switch (config) {
bsalomond4cb9222014-08-11 14:19:09 -0700686 case kIndex_8_GrPixelConfig:
687 return width * height + kGrIndex8TableSize;
krajcevski238b4562014-06-30 09:09:22 -0700688 case kR11_EAC_GrPixelConfig:
krajcevski9c0e6292014-06-02 07:38:14 -0700689 case kLATC_GrPixelConfig:
690 case kETC1_GrPixelConfig:
691 SkASSERT((width & 3) == 0);
692 SkASSERT((height & 3) == 0);
693 return (width >> 2) * (height >> 2) * 8;
694
krajcevski7ef21622014-07-16 15:21:13 -0700695 case kASTC_12x12_GrPixelConfig:
696 SkASSERT((width % 12) == 0);
697 SkASSERT((height % 12) == 0);
698 return (width / 12) * (height / 12) * 16;
699
krajcevski9c0e6292014-06-02 07:38:14 -0700700 default:
701 SkFAIL("Unknown compressed pixel config");
702 return 4 * width * height;
703 }
704}
705
706/**
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000707 * This value translates to reseting all the context state for any backend.
708 */
709static const uint32_t kAll_GrBackendState = 0xffffffff;
710
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000711///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000712
georgeb62508b2014-08-12 18:00:47 -0700713#if GR_ALWAYS_ALLOCATE_ON_HEAP
714 #define GrAutoMallocBaseType SkAutoMalloc
715#else
716 #define GrAutoMallocBaseType SkAutoSMalloc<S>
717#endif
718
719template <size_t S> class GrAutoMalloc : public GrAutoMallocBaseType {
720public:
721 GrAutoMalloc() : INHERITED() {}
722 explicit GrAutoMalloc(size_t size) : INHERITED(size) {}
723 virtual ~GrAutoMalloc() {}
724private:
725 typedef GrAutoMallocBaseType INHERITED;
726};
727
728#undef GrAutoMallocBaseType
reed@google.comac10a2d2010-12-22 21:39:39 +0000729#endif