blob: 434d929f1f56552ccf9b4cece032d3732fc8b245 [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
reed@google.comac10a2d2010-12-22 21:39:39 +00009#ifndef GrTypes_DEFINED
10#define GrTypes_DEFINED
11
reed@google.com9b24d252011-06-01 20:27:53 +000012#include "SkTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrConfig.h"
bsalomon@google.comb9086a02012-11-01 18:02:54 +000014#include "SkMath.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015
bsalomon@google.comfea37b52011-04-25 15:51:06 +000016////////////////////////////////////////////////////////////////////////////////
17
18/**
19 * Defines overloaded bitwise operators to make it easier to use an enum as a
20 * bitfield.
21 */
22#define GR_MAKE_BITFIELD_OPS(X) \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000023 inline X operator | (X a, X b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000024 return (X) (+a | +b); \
25 } \
26 \
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 template <typename T> \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000031 inline X operator & (T 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 & (X a, T b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000036 return (X) (+a & +b); \
37 } \
38
bsalomon@google.com86c1f712011-10-12 14:54:26 +000039#define GR_DECL_BITFIELD_OPS_FRIENDS(X) \
40 friend X operator | (X a, X b); \
41 \
42 friend X operator & (X a, X b); \
43 \
44 template <typename T> \
45 friend X operator & (T a, X b); \
46 \
47 template <typename T> \
48 friend X operator & (X a, T b); \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000049////////////////////////////////////////////////////////////////////////////////
50
reed@google.comac10a2d2010-12-22 21:39:39 +000051// compile time versions of min/max
52#define GR_CT_MAX(a, b) (((b) < (a)) ? (a) : (b))
53#define GR_CT_MIN(a, b) (((b) < (a)) ? (b) : (a))
54
55/**
56 * divide, rounding up
57 */
bsalomon@google.com91958362011-06-13 17:58:13 +000058static inline int32_t GrIDivRoundUp(int x, int y) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000059 SkASSERT(y > 0);
bsalomon@google.com91958362011-06-13 17:58:13 +000060 return (x + (y-1)) / y;
61}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000062static inline uint32_t GrUIDivRoundUp(uint32_t x, uint32_t y) {
63 return (x + (y-1)) / y;
64}
bsalomon@google.com4da34e32012-06-19 15:40:27 +000065static inline size_t GrSizeDivRoundUp(size_t x, size_t y) {
reed@google.comac10a2d2010-12-22 21:39:39 +000066 return (x + (y-1)) / y;
67}
68
bsalomon@google.com4da34e32012-06-19 15:40:27 +000069// compile time, evaluates Y multiple times
70#define GR_CT_DIV_ROUND_UP(X, Y) (((X) + ((Y)-1)) / (Y))
71
reed@google.comac10a2d2010-12-22 21:39:39 +000072/**
73 * align up
74 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +000075static inline uint32_t GrUIAlignUp(uint32_t x, uint32_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +000076 return GrUIDivRoundUp(x, alignment) * alignment;
77}
bsalomon@google.com4da34e32012-06-19 15:40:27 +000078static inline size_t GrSizeAlignUp(size_t x, size_t alignment) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000079 return GrSizeDivRoundUp(x, alignment) * alignment;
80}
reed@google.comac10a2d2010-12-22 21:39:39 +000081
bsalomon@google.com4da34e32012-06-19 15:40:27 +000082// compile time, evaluates A multiple times
83#define GR_CT_ALIGN_UP(X, A) (GR_CT_DIV_ROUND_UP((X),(A)) * (A))
84
reed@google.comac10a2d2010-12-22 21:39:39 +000085/**
86 * amount of pad needed to align up
87 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +000088static inline uint32_t GrUIAlignUpPad(uint32_t x, uint32_t alignment) {
89 return (alignment - x % alignment) % alignment;
90}
bsalomon@google.com4da34e32012-06-19 15:40:27 +000091static inline size_t GrSizeAlignUpPad(size_t x, size_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +000092 return (alignment - x % alignment) % alignment;
93}
94
95/**
96 * align down
97 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +000098static inline uint32_t GrUIAlignDown(uint32_t x, uint32_t alignment) {
99 return (x / alignment) * alignment;
100}
bsalomon@google.com5e497322012-08-28 21:45:26 +0000101static inline size_t GrSizeAlignDown(size_t x, uint32_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 return (x / alignment) * alignment;
103}
104
reed@google.comac10a2d2010-12-22 21:39:39 +0000105///////////////////////////////////////////////////////////////////////////////
106
107/**
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 * Return the next power of 2 >= n.
109 */
110static inline uint32_t GrNextPow2(uint32_t n) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000111 return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000112}
113
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000114static inline int GrNextPow2(int n) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000115 SkASSERT(n >= 0); // this impl only works for non-neg.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000116 return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000117}
118
reed@google.comac10a2d2010-12-22 21:39:39 +0000119///////////////////////////////////////////////////////////////////////////////
120
121/**
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000122 * Possible 3D APIs that may be used by Ganesh.
123 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000124enum GrBackend {
125 kOpenGL_GrBackend,
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000126};
127
128/**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000129 * Backend-specific 3D context handle
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000130 * GrGLInterface* for OpenGL. If NULL will use the default GL interface.
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000131 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000132typedef intptr_t GrBackendContext;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000133
134///////////////////////////////////////////////////////////////////////////////
135
136/**
bsalomon@google.comffca4002011-02-22 20:34:01 +0000137* Geometric primitives used for drawing.
138*/
139enum GrPrimitiveType {
bsalomon@google.com47059542012-06-06 20:51:20 +0000140 kTriangles_GrPrimitiveType,
141 kTriangleStrip_GrPrimitiveType,
142 kTriangleFan_GrPrimitiveType,
143 kPoints_GrPrimitiveType,
144 kLines_GrPrimitiveType, // 1 pix wide only
145 kLineStrip_GrPrimitiveType // 1 pix wide only
bsalomon@google.comffca4002011-02-22 20:34:01 +0000146};
147
bsalomon@google.com0650e812011-04-08 18:07:53 +0000148static inline bool GrIsPrimTypeLines(GrPrimitiveType type) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000149 return kLines_GrPrimitiveType == type || kLineStrip_GrPrimitiveType == type;
bsalomon@google.com0650e812011-04-08 18:07:53 +0000150}
151
152static inline bool GrIsPrimTypeTris(GrPrimitiveType type) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000153 return kTriangles_GrPrimitiveType == type ||
154 kTriangleStrip_GrPrimitiveType == type ||
155 kTriangleFan_GrPrimitiveType == type;
bsalomon@google.com0650e812011-04-08 18:07:53 +0000156}
157
bsalomon@google.comffca4002011-02-22 20:34:01 +0000158/**
159 * Coeffecients for alpha-blending.
160 */
161enum GrBlendCoeff {
bsalomon@google.com47059542012-06-06 20:51:20 +0000162 kInvalid_GrBlendCoeff = -1,
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000163
bsalomon@google.com47059542012-06-06 20:51:20 +0000164 kZero_GrBlendCoeff, //<! 0
165 kOne_GrBlendCoeff, //<! 1
166 kSC_GrBlendCoeff, //<! src color
167 kISC_GrBlendCoeff, //<! one minus src color
168 kDC_GrBlendCoeff, //<! dst color
169 kIDC_GrBlendCoeff, //<! one minus dst color
170 kSA_GrBlendCoeff, //<! src alpha
171 kISA_GrBlendCoeff, //<! one minus src alpha
172 kDA_GrBlendCoeff, //<! dst alpha
173 kIDA_GrBlendCoeff, //<! one minus dst alpha
174 kConstC_GrBlendCoeff, //<! constant color
175 kIConstC_GrBlendCoeff, //<! one minus constant color
176 kConstA_GrBlendCoeff, //<! constant color alpha
177 kIConstA_GrBlendCoeff, //<! one minus constant color alpha
bsalomon@google.com080773c2011-03-15 19:09:25 +0000178
joshualitt65171342014-10-09 07:25:36 -0700179 kFirstPublicGrBlendCoeff = kZero_GrBlendCoeff,
180 kLastPublicGrBlendCoeff = kIConstA_GrBlendCoeff,
bsalomon@google.comffca4002011-02-22 20:34:01 +0000181};
joshualitt65171342014-10-09 07:25:36 -0700182static const int kPublicGrBlendCoeffCount = kLastPublicGrBlendCoeff + 1;
bsalomon@google.comffca4002011-02-22 20:34:01 +0000183
bsalomon@google.comd302f142011-03-03 13:54:13 +0000184/**
reed@google.com759c16e2011-03-15 19:15:15 +0000185 * Formats for masks, used by the font cache.
186 * Important that these are 0-based.
reed@google.com98539c62011-03-15 15:40:16 +0000187 */
188enum GrMaskFormat {
caryclark@google.com1eeaf0b2011-06-22 13:19:43 +0000189 kA8_GrMaskFormat, //!< 1-byte per pixel
jvanverth5eefe422014-11-26 12:07:33 -0800190 kA565_GrMaskFormat, //!< 2-bytes per pixel, RGB represent 3-channel LCD coverage
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000191 kARGB_GrMaskFormat, //!< 4-bytes per pixel, color format
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +0000192
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000193 kLast_GrMaskFormat = kARGB_GrMaskFormat
reed@google.com98539c62011-03-15 15:40:16 +0000194};
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000195static const int kMaskFormatCount = kLast_GrMaskFormat + 1;
reed@google.com98539c62011-03-15 15:40:16 +0000196
197/**
198 * Return the number of bytes-per-pixel for the specified mask format.
199 */
200static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) {
jvanverth5eefe422014-11-26 12:07:33 -0800201 SkASSERT((unsigned)format < kMaskFormatCount);
reed@google.combbf12262011-10-04 20:14:57 +0000202 // kA8 (0) -> 1
203 // kA565 (1) -> 2
jvanverth5eefe422014-11-26 12:07:33 -0800204 // kARGB (2) -> 4
reedd54d3fc2014-11-13 14:39:58 -0800205 static const int sBytesPerPixel[] = { 1, 2, 4 };
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000206 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sBytesPerPixel) == kMaskFormatCount, array_size_mismatch);
jvanverth5eefe422014-11-26 12:07:33 -0800207 SK_COMPILE_ASSERT(kA8_GrMaskFormat == 0, enum_order_dependency);
208 SK_COMPILE_ASSERT(kA565_GrMaskFormat == 1, enum_order_dependency);
209 SK_COMPILE_ASSERT(kARGB_GrMaskFormat == 2, enum_order_dependency);
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000210
211 return sBytesPerPixel[(int) format];
reed@google.com98539c62011-03-15 15:40:16 +0000212}
213
214/**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000215 * Pixel configurations.
216 */
217enum GrPixelConfig {
218 kUnknown_GrPixelConfig,
219 kAlpha_8_GrPixelConfig,
220 kIndex_8_GrPixelConfig,
221 kRGB_565_GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000222 /**
223 * Premultiplied
224 */
225 kRGBA_4444_GrPixelConfig,
226 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000227 * Premultiplied. Byte order is r,g,b,a.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000228 */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000229 kRGBA_8888_GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000230 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000231 * Premultiplied. Byte order is b,g,r,a.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000232 */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000233 kBGRA_8888_GrPixelConfig,
joshualittee5da552014-07-16 13:32:56 -0700234 /**
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000235 * ETC1 Compressed Data
236 */
237 kETC1_GrPixelConfig,
238 /**
239 * LATC/RGTC/3Dc/BC4 Compressed Data
240 */
241 kLATC_GrPixelConfig,
krajcevski238b4562014-06-30 09:09:22 -0700242 /**
243 * R11 EAC Compressed Data
244 * (Corresponds to section C.3.5 of the OpenGL 4.4 core profile spec)
245 */
246 kR11_EAC_GrPixelConfig,
krajcevski7ef21622014-07-16 15:21:13 -0700247
248 /**
249 * 12x12 ASTC Compressed Data
250 * ASTC stands for Adaptive Scalable Texture Compression. It is a technique
251 * that allows for a lot of customization in the compressed representataion
252 * of a block. The only thing fixed in the representation is the block size,
253 * which means that a texture that contains ASTC data must be treated as
254 * having RGBA values. However, there are single-channel encodings which set
255 * the alpha to opaque and all three RGB channels equal effectively making the
256 * compression format a single channel such as R11 EAC and LATC.
257 */
258 kASTC_12x12_GrPixelConfig,
259
joshualittee5da552014-07-16 13:32:56 -0700260 /**
261 * Byte order is r, g, b, a. This color format is 32 bits per channel
262 */
263 kRGBA_float_GrPixelConfig,
jvanverth28f9c602014-12-05 13:06:35 -0800264
265 /**
266 * This color format is a single 16 bit float channel
267 */
268 kAlpha_half_GrPixelConfig,
269
270 kLast_GrPixelConfig = kAlpha_half_GrPixelConfig
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000271};
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +0000272static const int kGrPixelConfigCnt = kLast_GrPixelConfig + 1;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000273
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000274// Aliases for pixel configs that match skia's byte order.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000275#ifndef SK_CPU_LENDIAN
276 #error "Skia gpu currently assumes little endian"
277#endif
commit-bot@chromium.orge4657ed2013-03-19 14:16:31 +0000278#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
bsalomon@google.com0342a852012-08-20 19:22:38 +0000279 static const GrPixelConfig kSkia8888_GrPixelConfig = kBGRA_8888_GrPixelConfig;
commit-bot@chromium.orge4657ed2013-03-19 14:16:31 +0000280#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
bsalomon@google.com0342a852012-08-20 19:22:38 +0000281 static const GrPixelConfig kSkia8888_GrPixelConfig = kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000282#else
283 #error "SK_*32_SHIFT values must correspond to GL_BGRA or GL_RGBA format."
284#endif
285
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000286// Returns true if the pixel config is a GPU-specific compressed format
287// representation.
288static inline bool GrPixelConfigIsCompressed(GrPixelConfig config) {
289 switch (config) {
bsalomond4cb9222014-08-11 14:19:09 -0700290 case kIndex_8_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000291 case kETC1_GrPixelConfig:
292 case kLATC_GrPixelConfig:
krajcevski238b4562014-06-30 09:09:22 -0700293 case kR11_EAC_GrPixelConfig:
krajcevski7ef21622014-07-16 15:21:13 -0700294 case kASTC_12x12_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000295 return true;
296 default:
297 return false;
298 }
299}
300
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000301// Returns true if the pixel config is 32 bits per pixel
bsalomon@google.com9c680582013-02-06 18:17:50 +0000302static inline bool GrPixelConfigIs8888(GrPixelConfig config) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000303 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000304 case kRGBA_8888_GrPixelConfig:
305 case kBGRA_8888_GrPixelConfig:
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000306 return true;
307 default:
308 return false;
309 }
310}
311
312// Takes a config and returns the equivalent config with the R and B order
313// swapped if such a config exists. Otherwise, kUnknown_GrPixelConfig
314static inline GrPixelConfig GrPixelConfigSwapRAndB(GrPixelConfig config) {
315 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000316 case kBGRA_8888_GrPixelConfig:
317 return kRGBA_8888_GrPixelConfig;
318 case kRGBA_8888_GrPixelConfig:
319 return kBGRA_8888_GrPixelConfig;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000320 default:
321 return kUnknown_GrPixelConfig;
322 }
323}
324
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000325static inline size_t GrBytesPerPixel(GrPixelConfig config) {
bsalomond4cb9222014-08-11 14:19:09 -0700326 SkASSERT(!GrPixelConfigIsCompressed(config));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000327 switch (config) {
328 case kAlpha_8_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000329 return 1;
330 case kRGB_565_GrPixelConfig:
331 case kRGBA_4444_GrPixelConfig:
jvanverth28f9c602014-12-05 13:06:35 -0800332 case kAlpha_half_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000333 return 2;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000334 case kRGBA_8888_GrPixelConfig:
335 case kBGRA_8888_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000336 return 4;
joshualittee5da552014-07-16 13:32:56 -0700337 case kRGBA_float_GrPixelConfig:
338 return 16;
339 default:
340 return 0;
341 }
342}
343
344static inline size_t GrUnpackAlignment(GrPixelConfig config) {
bsalomond4cb9222014-08-11 14:19:09 -0700345 SkASSERT(!GrPixelConfigIsCompressed(config));
joshualittee5da552014-07-16 13:32:56 -0700346 switch (config) {
347 case kAlpha_8_GrPixelConfig:
joshualittee5da552014-07-16 13:32:56 -0700348 return 1;
349 case kRGB_565_GrPixelConfig:
350 case kRGBA_4444_GrPixelConfig:
jvanverth28f9c602014-12-05 13:06:35 -0800351 case kAlpha_half_GrPixelConfig:
joshualittee5da552014-07-16 13:32:56 -0700352 return 2;
353 case kRGBA_8888_GrPixelConfig:
354 case kBGRA_8888_GrPixelConfig:
355 case kRGBA_float_GrPixelConfig:
356 return 4;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000357 default:
358 return 0;
359 }
360}
361
362static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
363 switch (config) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000364 case kETC1_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000365 case kRGB_565_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +0000366 return true;
367 default:
368 return false;
369 }
370}
371
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000372static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
373 switch (config) {
krajcevski238b4562014-06-30 09:09:22 -0700374 case kR11_EAC_GrPixelConfig:
krajcevski748e9d32014-06-09 08:32:34 -0700375 case kLATC_GrPixelConfig:
krajcevski2a413df2014-07-24 08:16:00 -0700376 case kASTC_12x12_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000377 case kAlpha_8_GrPixelConfig:
jvanverth28f9c602014-12-05 13:06:35 -0800378 case kAlpha_half_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000379 return true;
380 default:
381 return false;
382 }
383}
384
385/**
bsalomonf2703d82014-10-28 14:33:06 -0700386 * Optional bitfield flags that can be set on GrSurfaceDesc (below).
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000387 */
bsalomonf2703d82014-10-28 14:33:06 -0700388enum GrSurfaceFlags {
389 kNone_GrSurfaceFlags = 0x0,
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000390 /**
391 * Creates a texture that can be rendered to as a GrRenderTarget. Use
392 * GrTexture::asRenderTarget() to access.
393 */
bsalomonf2703d82014-10-28 14:33:06 -0700394 kRenderTarget_GrSurfaceFlag = 0x1,
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000395 /**
396 * By default all render targets have an associated stencil buffer that
397 * may be required for path filling. This flag overrides stencil buffer
398 * creation.
399 * MAKE THIS PRIVATE?
400 */
bsalomonf2703d82014-10-28 14:33:06 -0700401 kNoStencil_GrSurfaceFlag = 0x2,
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000402 /**
403 * Indicates that all allocations (color buffer, FBO completeness, etc)
404 * should be verified.
405 */
bsalomonf2703d82014-10-28 14:33:06 -0700406 kCheckAllocation_GrSurfaceFlag = 0x4,
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000407};
408
bsalomonf2703d82014-10-28 14:33:06 -0700409GR_MAKE_BITFIELD_OPS(GrSurfaceFlags)
410
411// Legacy aliases
412typedef GrSurfaceFlags GrTextureFlags;
413static const GrSurfaceFlags kNone_GrTextureFlags = kNone_GrSurfaceFlags;
bsalomon81b09282014-10-28 15:17:38 -0700414static const GrSurfaceFlags kRenderTarget_GrTextureFlagBit = kRenderTarget_GrSurfaceFlag;
bsalomonf2703d82014-10-28 14:33:06 -0700415static const GrSurfaceFlags kNoStencil_GrTextureFlagBit = kNoStencil_GrSurfaceFlag;
416static const GrSurfaceFlags kCheckAllocation_GrTextureFlagBit = kCheckAllocation_GrSurfaceFlag;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000417
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000418/**
419 * Some textures will be stored such that the upper and left edges of the content meet at the
420 * 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 +0000421 * the origin. kDefault_GrSurfaceOrigin sets textures to TopLeft, and render targets
422 * to BottomLeft.
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000423 */
424
425enum GrSurfaceOrigin {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000426 kDefault_GrSurfaceOrigin, // DEPRECATED; to be removed
robertphillips@google.comcf9faf62013-02-05 14:05:06 +0000427 kTopLeft_GrSurfaceOrigin,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000428 kBottomLeft_GrSurfaceOrigin,
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000429};
robertphillips@google.com5091b702012-08-09 10:49:39 +0000430
431/**
bsalomonf2703d82014-10-28 14:33:06 -0700432 * Describes a surface to be created.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000433 */
bsalomonf2703d82014-10-28 14:33:06 -0700434struct GrSurfaceDesc {
435 GrSurfaceDesc()
436 : fFlags(kNone_GrSurfaceFlags)
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000437 , fOrigin(kDefault_GrSurfaceOrigin)
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000438 , fWidth(0)
439 , fHeight(0)
440 , fConfig(kUnknown_GrPixelConfig)
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000441 , fSampleCnt(0) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000442 }
443
bsalomonf2703d82014-10-28 14:33:06 -0700444 GrSurfaceFlags fFlags; //!< bitfield of TextureFlags
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000445 GrSurfaceOrigin fOrigin; //!< origin of the texture
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
bsalomonf2703d82014-10-28 14:33:06 -0700457 * applies if the kRenderTarget_GrSurfaceFlag is set. The actual number
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000458 * 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
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000460 * max supported count.
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000461 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000462 int fSampleCnt;
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000463};
464
bsalomonf2703d82014-10-28 14:33:06 -0700465// Legacy alias
466typedef GrSurfaceDesc GrTextureDesc;
467
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000468/**
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000469 * GrCacheID is used create and find cached GrResources (e.g. GrTextures). The ID has two parts:
470 * the domain and the key. Domains simply allow multiple clients to use 0-based indices as their
471 * cache key without colliding. The key uniquely identifies a GrResource within the domain.
472 * Users of the cache must obtain a domain via GenerateDomain().
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000473 */
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000474struct GrCacheID {
475public:
476 typedef uint8_t Domain;
477
478 struct Key {
479 union {
480 uint8_t fData8[16];
481 uint32_t fData32[4];
482 uint64_t fData64[2];
483 };
484 };
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000485
486 /**
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000487 * A default cache ID is invalid; a set method must be called before the object is used.
488 */
489 GrCacheID() { fDomain = kInvalid_Domain; }
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000490
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000491 /**
492 * Initialize the cache ID to a domain and key.
493 */
494 GrCacheID(Domain domain, const Key& key) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000495 SkASSERT(kInvalid_Domain != domain);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000496 this->reset(domain, key);
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000497 }
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000498
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000499 void reset(Domain domain, const Key& key) {
500 fDomain = domain;
501 memcpy(&fKey, &key, sizeof(Key));
502 }
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000503
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000504 /** Has this been initialized to a valid domain */
505 bool isValid() const { return kInvalid_Domain != fDomain; }
506
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000507 const Key& getKey() const { SkASSERT(this->isValid()); return fKey; }
508 Domain getDomain() const { SkASSERT(this->isValid()); return fDomain; }
skia.committer@gmail.com2859eb72012-12-21 02:01:28 +0000509
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000510 /** Creates a new unique ID domain. */
511 static Domain GenerateDomain();
512
513private:
514 Key fKey;
515 Domain fDomain;
516
517 static const Domain kInvalid_Domain = 0;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000518};
519
520/**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000521 * Clips are composed from these objects.
522 */
523enum GrClipType {
524 kRect_ClipType,
525 kPath_ClipType
526};
527
reed@google.comac10a2d2010-12-22 21:39:39 +0000528///////////////////////////////////////////////////////////////////////////////
529
bsalomon@google.come269f212011-11-07 13:29:52 +0000530// opaque type for 3D API object handles
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000531typedef intptr_t GrBackendObject;
bsalomon@google.come269f212011-11-07 13:29:52 +0000532
533/**
534 * Gr can wrap an existing texture created by the client with a GrTexture
535 * object. The client is responsible for ensuring that the texture lives at
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000536 * least as long as the GrTexture object wrapping it. We require the client to
537 * explicitly provide information about the texture, such as width, height,
bsalomon@google.come269f212011-11-07 13:29:52 +0000538 * and pixel config, rather than querying the 3D APIfor these values. We expect
539 * these to be immutable even if the 3D API doesn't require this (OpenGL).
540 *
541 * Textures that are also render targets are supported as well. Gr will manage
542 * any ancillary 3D API (stencil buffer, FBO id, etc) objects necessary for
543 * Gr to draw into the render target. To access the render target object
544 * call GrTexture::asRenderTarget().
545 *
546 * If in addition to the render target flag, the caller also specifies a sample
547 * count Gr will create an MSAA buffer that resolves into the texture. Gr auto-
548 * resolves when it reads from the texture. The client can explictly resolve
549 * using the GrRenderTarget interface.
robertphillips@google.com32716282012-06-04 12:48:45 +0000550 *
551 * Note: These flags currently form a subset of GrTexture's flags.
bsalomon@google.come269f212011-11-07 13:29:52 +0000552 */
553
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000554enum GrBackendTextureFlags {
bsalomon@google.come269f212011-11-07 13:29:52 +0000555 /**
556 * No flags enabled
557 */
bsalomonf2703d82014-10-28 14:33:06 -0700558 kNone_GrBackendTextureFlag = 0,
bsalomon@google.come269f212011-11-07 13:29:52 +0000559 /**
560 * Indicates that the texture is also a render target, and thus should have
561 * a GrRenderTarget object.
bsalomon@google.come269f212011-11-07 13:29:52 +0000562 */
bsalomonf2703d82014-10-28 14:33:06 -0700563 kRenderTarget_GrBackendTextureFlag = kRenderTarget_GrSurfaceFlag,
bsalomon@google.come269f212011-11-07 13:29:52 +0000564};
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000565GR_MAKE_BITFIELD_OPS(GrBackendTextureFlags)
bsalomon@google.come269f212011-11-07 13:29:52 +0000566
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000567struct GrBackendTextureDesc {
568 GrBackendTextureDesc() { memset(this, 0, sizeof(*this)); }
569 GrBackendTextureFlags fFlags;
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000570 GrSurfaceOrigin fOrigin;
bsalomon@google.come269f212011-11-07 13:29:52 +0000571 int fWidth; //<! width in pixels
572 int fHeight; //<! height in pixels
573 GrPixelConfig fConfig; //<! color format
574 /**
575 * If the render target flag is set and sample count is greater than 0
576 * then Gr will create an MSAA buffer that resolves to the texture.
577 */
578 int fSampleCnt;
579 /**
580 * Handle to the 3D API object.
581 * OpenGL: Texture ID.
582 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000583 GrBackendObject fTextureHandle;
bsalomon@google.come269f212011-11-07 13:29:52 +0000584};
585
586///////////////////////////////////////////////////////////////////////////////
587
588/**
589 * Gr can wrap an existing render target created by the client in the 3D API
590 * with a GrRenderTarget object. The client is responsible for ensuring that the
591 * underlying 3D API object lives at least as long as the GrRenderTarget object
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000592 * wrapping it. We require the client to explicitly provide information about
bsalomon@google.come269f212011-11-07 13:29:52 +0000593 * the target, such as width, height, and pixel config rather than querying the
594 * 3D API for these values. We expect these properties to be immutable even if
595 * the 3D API doesn't require this (OpenGL).
596 */
597
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000598struct GrBackendRenderTargetDesc {
599 GrBackendRenderTargetDesc() { memset(this, 0, sizeof(*this)); }
bsalomon@google.come269f212011-11-07 13:29:52 +0000600 int fWidth; //<! width in pixels
601 int fHeight; //<! height in pixels
602 GrPixelConfig fConfig; //<! color format
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000603 GrSurfaceOrigin fOrigin; //<! pixel origin
bsalomon@google.come269f212011-11-07 13:29:52 +0000604 /**
605 * The number of samples per pixel. Gr uses this to influence decisions
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000606 * about applying other forms of anti-aliasing.
bsalomon@google.come269f212011-11-07 13:29:52 +0000607 */
608 int fSampleCnt;
609 /**
610 * Number of bits of stencil per-pixel.
611 */
612 int fStencilBits;
613 /**
614 * Handle to the 3D API object.
615 * OpenGL: FBO ID
616 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000617 GrBackendObject fRenderTargetHandle;
bsalomon@google.come269f212011-11-07 13:29:52 +0000618};
619
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000620/**
621 * The GrContext's cache of backend context state can be partially invalidated.
622 * These enums are specific to the GL backend and we'd add a new set for an alternative backend.
623 */
624enum GrGLBackendState {
625 kRenderTarget_GrGLBackendState = 1 << 0,
626 kTextureBinding_GrGLBackendState = 1 << 1,
627 // View state stands for scissor and viewport
628 kView_GrGLBackendState = 1 << 2,
629 kBlend_GrGLBackendState = 1 << 3,
egdanielb414f252014-07-29 13:15:47 -0700630 kMSAAEnable_GrGLBackendState = 1 << 4,
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000631 kVertex_GrGLBackendState = 1 << 5,
632 kStencil_GrGLBackendState = 1 << 6,
633 kPixelStore_GrGLBackendState = 1 << 7,
634 kProgram_GrGLBackendState = 1 << 8,
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000635 kFixedFunction_GrGLBackendState = 1 << 9,
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000636 kMisc_GrGLBackendState = 1 << 10,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000637 kPathRendering_GrGLBackendState = 1 << 11,
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000638 kALL_GrGLBackendState = 0xffff
639};
640
641/**
krajcevski9c0e6292014-06-02 07:38:14 -0700642 * Returns the data size for the given compressed pixel config
joshualittee5da552014-07-16 13:32:56 -0700643 */
krajcevski9c0e6292014-06-02 07:38:14 -0700644static inline size_t GrCompressedFormatDataSize(GrPixelConfig config,
645 int width, int height) {
646 SkASSERT(GrPixelConfigIsCompressed(config));
bsalomond4cb9222014-08-11 14:19:09 -0700647 static const int kGrIndex8TableSize = 256 * 4; // 4 == sizeof(GrColor)
krajcevski9c0e6292014-06-02 07:38:14 -0700648
649 switch (config) {
bsalomond4cb9222014-08-11 14:19:09 -0700650 case kIndex_8_GrPixelConfig:
651 return width * height + kGrIndex8TableSize;
krajcevski238b4562014-06-30 09:09:22 -0700652 case kR11_EAC_GrPixelConfig:
krajcevski9c0e6292014-06-02 07:38:14 -0700653 case kLATC_GrPixelConfig:
654 case kETC1_GrPixelConfig:
655 SkASSERT((width & 3) == 0);
656 SkASSERT((height & 3) == 0);
657 return (width >> 2) * (height >> 2) * 8;
658
krajcevski7ef21622014-07-16 15:21:13 -0700659 case kASTC_12x12_GrPixelConfig:
660 SkASSERT((width % 12) == 0);
661 SkASSERT((height % 12) == 0);
662 return (width / 12) * (height / 12) * 16;
663
krajcevski9c0e6292014-06-02 07:38:14 -0700664 default:
665 SkFAIL("Unknown compressed pixel config");
666 return 4 * width * height;
667 }
668}
669
670/**
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000671 * This value translates to reseting all the context state for any backend.
672 */
673static const uint32_t kAll_GrBackendState = 0xffffffff;
674
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000675///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000676
georgeb62508b2014-08-12 18:00:47 -0700677#if GR_ALWAYS_ALLOCATE_ON_HEAP
678 #define GrAutoMallocBaseType SkAutoMalloc
679#else
680 #define GrAutoMallocBaseType SkAutoSMalloc<S>
681#endif
682
683template <size_t S> class GrAutoMalloc : public GrAutoMallocBaseType {
684public:
685 GrAutoMalloc() : INHERITED() {}
686 explicit GrAutoMalloc(size_t size) : INHERITED(size) {}
687 virtual ~GrAutoMalloc() {}
688private:
689 typedef GrAutoMallocBaseType INHERITED;
690};
691
692#undef GrAutoMallocBaseType
reed@google.comac10a2d2010-12-22 21:39:39 +0000693#endif