blob: 767e07206e990db701c3e30c6b95330304d6af2b [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#ifndef GrTypes_DEFINED
9#define GrTypes_DEFINED
10
reed@google.com9b24d252011-06-01 20:27:53 +000011#include "SkTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000012#include "GrConfig.h"
bsalomon@google.comb9086a02012-11-01 18:02:54 +000013#include "SkMath.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
bsalomon@google.comfea37b52011-04-25 15:51:06 +000015////////////////////////////////////////////////////////////////////////////////
16
17/**
18 * Defines overloaded bitwise operators to make it easier to use an enum as a
19 * bitfield.
20 */
21#define GR_MAKE_BITFIELD_OPS(X) \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000022 inline X operator | (X a, X b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000023 return (X) (+a | +b); \
24 } \
cdalton32117702015-05-11 11:21:23 -070025 inline X& operator |= (X& a, X b) { \
26 return (a = a | b); \
27 } \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000028 \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000029 inline X operator & (X a, X b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000030 return (X) (+a & +b); \
31 } \
32 template <typename T> \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000033 inline X operator & (T a, X b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000034 return (X) (+a & +b); \
35 } \
36 template <typename T> \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000037 inline X operator & (X a, T b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000038 return (X) (+a & +b); \
39 } \
40
bsalomon@google.com86c1f712011-10-12 14:54:26 +000041#define GR_DECL_BITFIELD_OPS_FRIENDS(X) \
42 friend X operator | (X a, X b); \
cdalton32117702015-05-11 11:21:23 -070043 friend X& operator |= (X& a, X b); \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000044 \
45 friend X operator & (X a, X b); \
46 \
47 template <typename T> \
48 friend X operator & (T a, X b); \
49 \
50 template <typename T> \
51 friend X operator & (X a, T b); \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000052////////////////////////////////////////////////////////////////////////////////
53
reed@google.comac10a2d2010-12-22 21:39:39 +000054// compile time versions of min/max
55#define GR_CT_MAX(a, b) (((b) < (a)) ? (a) : (b))
56#define GR_CT_MIN(a, b) (((b) < (a)) ? (b) : (a))
57
58/**
59 * divide, rounding up
60 */
bsalomon@google.com91958362011-06-13 17:58:13 +000061static inline int32_t GrIDivRoundUp(int x, int y) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000062 SkASSERT(y > 0);
bsalomon@google.com91958362011-06-13 17:58:13 +000063 return (x + (y-1)) / y;
64}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000065static inline uint32_t GrUIDivRoundUp(uint32_t x, uint32_t y) {
66 return (x + (y-1)) / y;
67}
bsalomon@google.com4da34e32012-06-19 15:40:27 +000068static inline size_t GrSizeDivRoundUp(size_t x, size_t y) {
reed@google.comac10a2d2010-12-22 21:39:39 +000069 return (x + (y-1)) / y;
70}
71
bsalomon@google.com4da34e32012-06-19 15:40:27 +000072// compile time, evaluates Y multiple times
73#define GR_CT_DIV_ROUND_UP(X, Y) (((X) + ((Y)-1)) / (Y))
74
reed@google.comac10a2d2010-12-22 21:39:39 +000075/**
76 * align up
77 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +000078static inline uint32_t GrUIAlignUp(uint32_t x, uint32_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +000079 return GrUIDivRoundUp(x, alignment) * alignment;
80}
bsalomon@google.com4da34e32012-06-19 15:40:27 +000081static inline size_t GrSizeAlignUp(size_t x, size_t alignment) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000082 return GrSizeDivRoundUp(x, alignment) * alignment;
83}
reed@google.comac10a2d2010-12-22 21:39:39 +000084
bsalomon@google.com4da34e32012-06-19 15:40:27 +000085// compile time, evaluates A multiple times
86#define GR_CT_ALIGN_UP(X, A) (GR_CT_DIV_ROUND_UP((X),(A)) * (A))
87
reed@google.comac10a2d2010-12-22 21:39:39 +000088/**
89 * amount of pad needed to align up
90 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +000091static inline uint32_t GrUIAlignUpPad(uint32_t x, uint32_t alignment) {
92 return (alignment - x % alignment) % alignment;
93}
bsalomon@google.com4da34e32012-06-19 15:40:27 +000094static inline size_t GrSizeAlignUpPad(size_t x, size_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +000095 return (alignment - x % alignment) % alignment;
96}
97
98/**
99 * align down
100 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000101static inline uint32_t GrUIAlignDown(uint32_t x, uint32_t alignment) {
102 return (x / alignment) * alignment;
103}
bsalomon@google.com5e497322012-08-28 21:45:26 +0000104static inline size_t GrSizeAlignDown(size_t x, uint32_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000105 return (x / alignment) * alignment;
106}
107
reed@google.comac10a2d2010-12-22 21:39:39 +0000108///////////////////////////////////////////////////////////////////////////////
109
110/**
reed@google.comac10a2d2010-12-22 21:39:39 +0000111 * Return the next power of 2 >= n.
112 */
113static inline uint32_t GrNextPow2(uint32_t n) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000114 return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000115}
116
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000117static inline int GrNextPow2(int n) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000118 SkASSERT(n >= 0); // this impl only works for non-neg.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000119 return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000120}
121
reed@google.comac10a2d2010-12-22 21:39:39 +0000122///////////////////////////////////////////////////////////////////////////////
123
124/**
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000125 * Possible 3D APIs that may be used by Ganesh.
126 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000127enum GrBackend {
128 kOpenGL_GrBackend,
jvanvertha50e17a2015-08-12 12:19:36 -0700129 kVulkan_GrBackend,
130
131 kLast_GrBackend = kVulkan_GrBackend
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000132};
jvanvertha50e17a2015-08-12 12:19:36 -0700133const int kBackendCount = kLast_GrBackend + 1;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000134
135/**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000136 * Backend-specific 3D context handle
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000137 * GrGLInterface* for OpenGL. If NULL will use the default GL interface.
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000138 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000139typedef intptr_t GrBackendContext;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000140
141///////////////////////////////////////////////////////////////////////////////
142
143/**
bsalomon@google.comffca4002011-02-22 20:34:01 +0000144* Geometric primitives used for drawing.
145*/
146enum GrPrimitiveType {
bsalomon@google.com47059542012-06-06 20:51:20 +0000147 kTriangles_GrPrimitiveType,
148 kTriangleStrip_GrPrimitiveType,
149 kTriangleFan_GrPrimitiveType,
150 kPoints_GrPrimitiveType,
151 kLines_GrPrimitiveType, // 1 pix wide only
joshualitte5902662015-05-11 08:18:35 -0700152 kLineStrip_GrPrimitiveType, // 1 pix wide only
153 kLast_GrPrimitiveType = kLineStrip_GrPrimitiveType
bsalomon@google.comffca4002011-02-22 20:34:01 +0000154};
155
bsalomon@google.com0650e812011-04-08 18:07:53 +0000156static inline bool GrIsPrimTypeLines(GrPrimitiveType type) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000157 return kLines_GrPrimitiveType == type || kLineStrip_GrPrimitiveType == type;
bsalomon@google.com0650e812011-04-08 18:07:53 +0000158}
159
160static inline bool GrIsPrimTypeTris(GrPrimitiveType type) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000161 return kTriangles_GrPrimitiveType == type ||
162 kTriangleStrip_GrPrimitiveType == type ||
163 kTriangleFan_GrPrimitiveType == type;
bsalomon@google.com0650e812011-04-08 18:07:53 +0000164}
165
bsalomon@google.comffca4002011-02-22 20:34:01 +0000166/**
reed@google.com759c16e2011-03-15 19:15:15 +0000167 * Formats for masks, used by the font cache.
168 * Important that these are 0-based.
reed@google.com98539c62011-03-15 15:40:16 +0000169 */
170enum GrMaskFormat {
caryclark@google.com1eeaf0b2011-06-22 13:19:43 +0000171 kA8_GrMaskFormat, //!< 1-byte per pixel
jvanverth5eefe422014-11-26 12:07:33 -0800172 kA565_GrMaskFormat, //!< 2-bytes per pixel, RGB represent 3-channel LCD coverage
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000173 kARGB_GrMaskFormat, //!< 4-bytes per pixel, color format
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +0000174
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000175 kLast_GrMaskFormat = kARGB_GrMaskFormat
reed@google.com98539c62011-03-15 15:40:16 +0000176};
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000177static const int kMaskFormatCount = kLast_GrMaskFormat + 1;
reed@google.com98539c62011-03-15 15:40:16 +0000178
179/**
180 * Return the number of bytes-per-pixel for the specified mask format.
181 */
182static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) {
mtklein1d02a602015-11-19 09:53:21 -0800183 SkASSERT(format < kMaskFormatCount);
reed@google.combbf12262011-10-04 20:14:57 +0000184 // kA8 (0) -> 1
185 // kA565 (1) -> 2
jvanverth5eefe422014-11-26 12:07:33 -0800186 // kARGB (2) -> 4
reedd54d3fc2014-11-13 14:39:58 -0800187 static const int sBytesPerPixel[] = { 1, 2, 4 };
bungeman99fe8222015-08-20 07:57:51 -0700188 static_assert(SK_ARRAY_COUNT(sBytesPerPixel) == kMaskFormatCount, "array_size_mismatch");
189 static_assert(kA8_GrMaskFormat == 0, "enum_order_dependency");
190 static_assert(kA565_GrMaskFormat == 1, "enum_order_dependency");
191 static_assert(kARGB_GrMaskFormat == 2, "enum_order_dependency");
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000192
193 return sBytesPerPixel[(int) format];
reed@google.com98539c62011-03-15 15:40:16 +0000194}
195
196/**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000197 * Pixel configurations.
198 */
199enum GrPixelConfig {
200 kUnknown_GrPixelConfig,
201 kAlpha_8_GrPixelConfig,
202 kIndex_8_GrPixelConfig,
203 kRGB_565_GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000204 /**
205 * Premultiplied
206 */
207 kRGBA_4444_GrPixelConfig,
208 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000209 * Premultiplied. Byte order is r,g,b,a.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000210 */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000211 kRGBA_8888_GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000212 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000213 * Premultiplied. Byte order is b,g,r,a.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000214 */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000215 kBGRA_8888_GrPixelConfig,
joshualittee5da552014-07-16 13:32:56 -0700216 /**
jvanverthfa1e8a72014-12-22 08:31:49 -0800217 * Premultiplied and sRGB. Byte order is r,g,b,a.
218 */
219 kSRGBA_8888_GrPixelConfig,
220 /**
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000221 * ETC1 Compressed Data
222 */
223 kETC1_GrPixelConfig,
224 /**
225 * LATC/RGTC/3Dc/BC4 Compressed Data
226 */
227 kLATC_GrPixelConfig,
krajcevski238b4562014-06-30 09:09:22 -0700228 /**
229 * R11 EAC Compressed Data
230 * (Corresponds to section C.3.5 of the OpenGL 4.4 core profile spec)
231 */
232 kR11_EAC_GrPixelConfig,
krajcevski7ef21622014-07-16 15:21:13 -0700233
234 /**
235 * 12x12 ASTC Compressed Data
236 * ASTC stands for Adaptive Scalable Texture Compression. It is a technique
237 * that allows for a lot of customization in the compressed representataion
238 * of a block. The only thing fixed in the representation is the block size,
239 * which means that a texture that contains ASTC data must be treated as
240 * having RGBA values. However, there are single-channel encodings which set
241 * the alpha to opaque and all three RGB channels equal effectively making the
242 * compression format a single channel such as R11 EAC and LATC.
243 */
244 kASTC_12x12_GrPixelConfig,
245
joshualittee5da552014-07-16 13:32:56 -0700246 /**
247 * Byte order is r, g, b, a. This color format is 32 bits per channel
248 */
249 kRGBA_float_GrPixelConfig,
jvanverth28f9c602014-12-05 13:06:35 -0800250
251 /**
252 * This color format is a single 16 bit float channel
253 */
254 kAlpha_half_GrPixelConfig,
255
jvanverthfb5df432015-05-21 08:12:27 -0700256 /**
257 * Byte order is r, g, b, a. This color format is 16 bits per channel
258 */
259 kRGBA_half_GrPixelConfig,
260
261 kLast_GrPixelConfig = kRGBA_half_GrPixelConfig
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000262};
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +0000263static const int kGrPixelConfigCnt = kLast_GrPixelConfig + 1;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000264
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000265// Aliases for pixel configs that match skia's byte order.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000266#ifndef SK_CPU_LENDIAN
267 #error "Skia gpu currently assumes little endian"
268#endif
commit-bot@chromium.orge4657ed2013-03-19 14:16:31 +0000269#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
bsalomon@google.com0342a852012-08-20 19:22:38 +0000270 static const GrPixelConfig kSkia8888_GrPixelConfig = kBGRA_8888_GrPixelConfig;
commit-bot@chromium.orge4657ed2013-03-19 14:16:31 +0000271#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
bsalomon@google.com0342a852012-08-20 19:22:38 +0000272 static const GrPixelConfig kSkia8888_GrPixelConfig = kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000273#else
274 #error "SK_*32_SHIFT values must correspond to GL_BGRA or GL_RGBA format."
275#endif
276
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000277// Returns true if the pixel config is a GPU-specific compressed format
278// representation.
279static inline bool GrPixelConfigIsCompressed(GrPixelConfig config) {
280 switch (config) {
bsalomond4cb9222014-08-11 14:19:09 -0700281 case kIndex_8_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000282 case kETC1_GrPixelConfig:
283 case kLATC_GrPixelConfig:
krajcevski238b4562014-06-30 09:09:22 -0700284 case kR11_EAC_GrPixelConfig:
krajcevski7ef21622014-07-16 15:21:13 -0700285 case kASTC_12x12_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000286 return true;
287 default:
288 return false;
289 }
290}
291
bsalomon37f9a262015-02-02 13:00:10 -0800292/** If the pixel config is compressed, return an equivalent uncompressed format. */
293static inline GrPixelConfig GrMakePixelConfigUncompressed(GrPixelConfig config) {
294 switch (config) {
295 case kIndex_8_GrPixelConfig:
296 case kETC1_GrPixelConfig:
297 case kASTC_12x12_GrPixelConfig:
298 return kRGBA_8888_GrPixelConfig;
299 case kLATC_GrPixelConfig:
300 case kR11_EAC_GrPixelConfig:
301 return kAlpha_8_GrPixelConfig;
302 default:
303 SkASSERT(!GrPixelConfigIsCompressed(config));
304 return config;
305 }
306}
307
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000308// Returns true if the pixel config is 32 bits per pixel
bsalomon@google.com9c680582013-02-06 18:17:50 +0000309static inline bool GrPixelConfigIs8888(GrPixelConfig config) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000310 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000311 case kRGBA_8888_GrPixelConfig:
312 case kBGRA_8888_GrPixelConfig:
jvanverthfa1e8a72014-12-22 08:31:49 -0800313 case kSRGBA_8888_GrPixelConfig:
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000314 return true;
315 default:
316 return false;
317 }
318}
319
bsalomon16921ec2015-07-30 15:34:56 -0700320// Returns true if the color (non-alpha) components represent sRGB values. It does NOT indicate that
321// all three color components are present in the config or anything about their order.
322static inline bool GrPixelConfigIsSRGB(GrPixelConfig config) {
323 switch (config) {
324 case kSRGBA_8888_GrPixelConfig:
325 return true;
326 default:
327 return false;
328 }
329}
330
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000331// Takes a config and returns the equivalent config with the R and B order
332// swapped if such a config exists. Otherwise, kUnknown_GrPixelConfig
333static inline GrPixelConfig GrPixelConfigSwapRAndB(GrPixelConfig config) {
334 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000335 case kBGRA_8888_GrPixelConfig:
336 return kRGBA_8888_GrPixelConfig;
337 case kRGBA_8888_GrPixelConfig:
338 return kBGRA_8888_GrPixelConfig;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000339 default:
340 return kUnknown_GrPixelConfig;
341 }
342}
343
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000344static inline size_t GrBytesPerPixel(GrPixelConfig config) {
bsalomond4cb9222014-08-11 14:19:09 -0700345 SkASSERT(!GrPixelConfigIsCompressed(config));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000346 switch (config) {
347 case kAlpha_8_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000348 return 1;
349 case kRGB_565_GrPixelConfig:
350 case kRGBA_4444_GrPixelConfig:
jvanverth28f9c602014-12-05 13:06:35 -0800351 case kAlpha_half_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000352 return 2;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000353 case kRGBA_8888_GrPixelConfig:
354 case kBGRA_8888_GrPixelConfig:
jvanverthfa1e8a72014-12-22 08:31:49 -0800355 case kSRGBA_8888_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000356 return 4;
jvanverthfb5df432015-05-21 08:12:27 -0700357 case kRGBA_half_GrPixelConfig:
358 return 8;
joshualittee5da552014-07-16 13:32:56 -0700359 case kRGBA_float_GrPixelConfig:
360 return 16;
361 default:
362 return 0;
363 }
364}
365
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000366static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
367 switch (config) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000368 case kETC1_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000369 case kRGB_565_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +0000370 return true;
371 default:
372 return false;
373 }
374}
375
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000376static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
377 switch (config) {
krajcevski238b4562014-06-30 09:09:22 -0700378 case kR11_EAC_GrPixelConfig:
krajcevski748e9d32014-06-09 08:32:34 -0700379 case kLATC_GrPixelConfig:
krajcevski2a413df2014-07-24 08:16:00 -0700380 case kASTC_12x12_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000381 case kAlpha_8_GrPixelConfig:
jvanverth28f9c602014-12-05 13:06:35 -0800382 case kAlpha_half_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000383 return true;
384 default:
385 return false;
386 }
387}
388
389/**
bsalomonf2703d82014-10-28 14:33:06 -0700390 * Optional bitfield flags that can be set on GrSurfaceDesc (below).
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000391 */
bsalomonf2703d82014-10-28 14:33:06 -0700392enum GrSurfaceFlags {
393 kNone_GrSurfaceFlags = 0x0,
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000394 /**
395 * Creates a texture that can be rendered to as a GrRenderTarget. Use
396 * GrTexture::asRenderTarget() to access.
397 */
bsalomonf2703d82014-10-28 14:33:06 -0700398 kRenderTarget_GrSurfaceFlag = 0x1,
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000399 /**
jvanverth5a42c332015-09-02 12:26:10 -0700400 * Placeholder for managing zero-copy textures
401 */
402 kZeroCopy_GrSurfaceFlag = 0x2,
403 /**
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000404 * Indicates that all allocations (color buffer, FBO completeness, etc)
405 * should be verified.
406 */
bsalomonf2703d82014-10-28 14:33:06 -0700407 kCheckAllocation_GrSurfaceFlag = 0x4,
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000408};
409
bsalomonf2703d82014-10-28 14:33:06 -0700410GR_MAKE_BITFIELD_OPS(GrSurfaceFlags)
411
erikchen7fec91c2016-02-05 12:10:55 -0800412// opaque type for 3D API object handles
413typedef intptr_t GrBackendObject;
414
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000415/**
416 * Some textures will be stored such that the upper and left edges of the content meet at the
417 * 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 +0000418 * the origin. kDefault_GrSurfaceOrigin sets textures to TopLeft, and render targets
419 * to BottomLeft.
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000420 */
421
422enum GrSurfaceOrigin {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000423 kDefault_GrSurfaceOrigin, // DEPRECATED; to be removed
robertphillips@google.comcf9faf62013-02-05 14:05:06 +0000424 kTopLeft_GrSurfaceOrigin,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000425 kBottomLeft_GrSurfaceOrigin,
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000426};
robertphillips@google.com5091b702012-08-09 10:49:39 +0000427
428/**
erikchen7fec91c2016-02-05 12:10:55 -0800429 * An container of function pointers which consumers of Skia can fill in and
430 * pass to Skia. Skia will use these function pointers in place of its backend
431 * API texture creation function. Either all of the function pointers should be
432 * filled in, or they should all be nullptr.
433 */
434struct GrTextureStorageAllocator {
435 GrTextureStorageAllocator()
436 : fAllocateTextureStorage(nullptr)
437 , fDeallocateTextureStorage(nullptr) {
438 }
439
440 enum class Result {
441 kSucceededAndUploaded,
442 kSucceededWithoutUpload,
443 kFailed
444 };
445 typedef Result (*AllocateTextureStorageProc)(
446 void* ctx, GrBackendObject texture, unsigned width,
447 unsigned height, GrPixelConfig config, const void* srcData, GrSurfaceOrigin);
448 typedef void (*DeallocateTextureStorageProc)(void* ctx, GrBackendObject texture);
449
450 /*
451 * Generates and binds a texture to |textureStorageTarget()|. Allocates
452 * storage for the texture.
453 *
454 * In OpenGL, the MIN and MAX filters for the created texture must be
455 * GL_LINEAR. The WRAP_S and WRAP_T must be GL_CLAMP_TO_EDGE.
456 *
457 * If |srcData| is not nullptr, then the implementation of this function
458 * may attempt to upload the data into the texture. On successful upload,
459 * or if |srcData| is nullptr, returns kSucceededAndUploaded.
460 */
461 AllocateTextureStorageProc fAllocateTextureStorage;
462
463 /*
464 * Deallocate the storage for the given texture.
465 *
466 * Skia does not always destroy its outstanding textures. See
467 * GrContext::abandonContext() for more details. The consumer of Skia is
468 * responsible for making sure that all textures are destroyed, even if this
469 * callback is not invoked.
470 */
471 DeallocateTextureStorageProc fDeallocateTextureStorage;
472
473 /*
474 * The context to use when invoking fAllocateTextureStorage and
475 * fDeallocateTextureStorage.
476 */
477 void* fCtx;
478};
479
480/**
bsalomonf2703d82014-10-28 14:33:06 -0700481 * Describes a surface to be created.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000482 */
bsalomonf2703d82014-10-28 14:33:06 -0700483struct GrSurfaceDesc {
484 GrSurfaceDesc()
485 : fFlags(kNone_GrSurfaceFlags)
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000486 , fOrigin(kDefault_GrSurfaceOrigin)
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000487 , fWidth(0)
488 , fHeight(0)
489 , fConfig(kUnknown_GrPixelConfig)
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000490 , fSampleCnt(0) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000491 }
492
bsalomonf2703d82014-10-28 14:33:06 -0700493 GrSurfaceFlags fFlags; //!< bitfield of TextureFlags
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000494 GrSurfaceOrigin fOrigin; //!< origin of the texture
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +0000495 int fWidth; //!< Width of the texture
496 int fHeight; //!< Height of the texture
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000497
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000498 /**
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000499 * Format of source data of the texture. Not guaranteed to be the same as
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000500 * internal format used by 3D API.
501 */
bsalomon@google.com5bc34f02011-12-06 14:46:34 +0000502 GrPixelConfig fConfig;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000503
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000504 /**
505 * The number of samples per pixel or 0 to disable full scene AA. This only
bsalomonf2703d82014-10-28 14:33:06 -0700506 * applies if the kRenderTarget_GrSurfaceFlag is set. The actual number
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000507 * of samples may not exactly match the request. The request will be rounded
508 * up to the next supported sample count, or down if it is larger than the
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000509 * max supported count.
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000510 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000511 int fSampleCnt;
erikchen7fec91c2016-02-05 12:10:55 -0800512
513 /**
514 * A custom platform-specific allocator to use in place of the backend APIs
515 * usual texture creation method (e.g. TexImage2D in OpenGL).
516 */
517 GrTextureStorageAllocator fTextureStorageAllocator;
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000518};
519
bsalomonf2703d82014-10-28 14:33:06 -0700520// Legacy alias
521typedef GrSurfaceDesc GrTextureDesc;
522
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000523/**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000524 * Clips are composed from these objects.
525 */
526enum GrClipType {
527 kRect_ClipType,
528 kPath_ClipType
529};
530
reed@google.comac10a2d2010-12-22 21:39:39 +0000531///////////////////////////////////////////////////////////////////////////////
532
bsalomon6dc6f5f2015-06-18 09:12:16 -0700533
534/** Ownership rules for external GPU resources imported into Skia. */
535enum GrWrapOwnership {
536 /** Skia will assume the client will keep the resource alive and Skia will not free it. */
537 kBorrow_GrWrapOwnership,
538
539 /** Skia will assume ownership of the resource and free it. */
540 kAdopt_GrWrapOwnership,
541};
542
bsalomon@google.come269f212011-11-07 13:29:52 +0000543/**
544 * Gr can wrap an existing texture created by the client with a GrTexture
545 * object. The client is responsible for ensuring that the texture lives at
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000546 * least as long as the GrTexture object wrapping it. We require the client to
547 * explicitly provide information about the texture, such as width, height,
bsalomon@google.come269f212011-11-07 13:29:52 +0000548 * and pixel config, rather than querying the 3D APIfor these values. We expect
549 * these to be immutable even if the 3D API doesn't require this (OpenGL).
550 *
551 * Textures that are also render targets are supported as well. Gr will manage
552 * any ancillary 3D API (stencil buffer, FBO id, etc) objects necessary for
553 * Gr to draw into the render target. To access the render target object
554 * call GrTexture::asRenderTarget().
555 *
556 * If in addition to the render target flag, the caller also specifies a sample
557 * count Gr will create an MSAA buffer that resolves into the texture. Gr auto-
558 * resolves when it reads from the texture. The client can explictly resolve
559 * using the GrRenderTarget interface.
robertphillips@google.com32716282012-06-04 12:48:45 +0000560 *
561 * Note: These flags currently form a subset of GrTexture's flags.
bsalomon@google.come269f212011-11-07 13:29:52 +0000562 */
563
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000564enum GrBackendTextureFlags {
bsalomon@google.come269f212011-11-07 13:29:52 +0000565 /**
566 * No flags enabled
567 */
bsalomonf2703d82014-10-28 14:33:06 -0700568 kNone_GrBackendTextureFlag = 0,
bsalomon@google.come269f212011-11-07 13:29:52 +0000569 /**
570 * Indicates that the texture is also a render target, and thus should have
571 * a GrRenderTarget object.
bsalomon@google.come269f212011-11-07 13:29:52 +0000572 */
bsalomonf2703d82014-10-28 14:33:06 -0700573 kRenderTarget_GrBackendTextureFlag = kRenderTarget_GrSurfaceFlag,
bsalomon@google.come269f212011-11-07 13:29:52 +0000574};
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000575GR_MAKE_BITFIELD_OPS(GrBackendTextureFlags)
bsalomon@google.come269f212011-11-07 13:29:52 +0000576
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000577struct GrBackendTextureDesc {
578 GrBackendTextureDesc() { memset(this, 0, sizeof(*this)); }
579 GrBackendTextureFlags fFlags;
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000580 GrSurfaceOrigin fOrigin;
bsalomon@google.come269f212011-11-07 13:29:52 +0000581 int fWidth; //<! width in pixels
582 int fHeight; //<! height in pixels
583 GrPixelConfig fConfig; //<! color format
584 /**
585 * If the render target flag is set and sample count is greater than 0
586 * then Gr will create an MSAA buffer that resolves to the texture.
587 */
588 int fSampleCnt;
589 /**
590 * Handle to the 3D API object.
591 * OpenGL: Texture ID.
592 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000593 GrBackendObject fTextureHandle;
bsalomon@google.come269f212011-11-07 13:29:52 +0000594};
595
596///////////////////////////////////////////////////////////////////////////////
597
598/**
599 * Gr can wrap an existing render target created by the client in the 3D API
600 * with a GrRenderTarget object. The client is responsible for ensuring that the
601 * underlying 3D API object lives at least as long as the GrRenderTarget object
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000602 * wrapping it. We require the client to explicitly provide information about
bsalomon@google.come269f212011-11-07 13:29:52 +0000603 * the target, such as width, height, and pixel config rather than querying the
604 * 3D API for these values. We expect these properties to be immutable even if
605 * the 3D API doesn't require this (OpenGL).
606 */
607
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000608struct GrBackendRenderTargetDesc {
609 GrBackendRenderTargetDesc() { memset(this, 0, sizeof(*this)); }
bsalomon@google.come269f212011-11-07 13:29:52 +0000610 int fWidth; //<! width in pixels
611 int fHeight; //<! height in pixels
612 GrPixelConfig fConfig; //<! color format
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000613 GrSurfaceOrigin fOrigin; //<! pixel origin
bsalomon@google.come269f212011-11-07 13:29:52 +0000614 /**
615 * The number of samples per pixel. Gr uses this to influence decisions
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000616 * about applying other forms of anti-aliasing.
bsalomon@google.come269f212011-11-07 13:29:52 +0000617 */
618 int fSampleCnt;
619 /**
620 * Number of bits of stencil per-pixel.
621 */
622 int fStencilBits;
623 /**
624 * Handle to the 3D API object.
625 * OpenGL: FBO ID
626 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000627 GrBackendObject fRenderTargetHandle;
bsalomon@google.come269f212011-11-07 13:29:52 +0000628};
629
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000630/**
631 * The GrContext's cache of backend context state can be partially invalidated.
632 * These enums are specific to the GL backend and we'd add a new set for an alternative backend.
633 */
634enum GrGLBackendState {
635 kRenderTarget_GrGLBackendState = 1 << 0,
636 kTextureBinding_GrGLBackendState = 1 << 1,
637 // View state stands for scissor and viewport
638 kView_GrGLBackendState = 1 << 2,
639 kBlend_GrGLBackendState = 1 << 3,
egdanielb414f252014-07-29 13:15:47 -0700640 kMSAAEnable_GrGLBackendState = 1 << 4,
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000641 kVertex_GrGLBackendState = 1 << 5,
642 kStencil_GrGLBackendState = 1 << 6,
643 kPixelStore_GrGLBackendState = 1 << 7,
644 kProgram_GrGLBackendState = 1 << 8,
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000645 kFixedFunction_GrGLBackendState = 1 << 9,
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000646 kMisc_GrGLBackendState = 1 << 10,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000647 kPathRendering_GrGLBackendState = 1 << 11,
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000648 kALL_GrGLBackendState = 0xffff
649};
650
651/**
krajcevski9c0e6292014-06-02 07:38:14 -0700652 * Returns the data size for the given compressed pixel config
joshualittee5da552014-07-16 13:32:56 -0700653 */
krajcevski9c0e6292014-06-02 07:38:14 -0700654static inline size_t GrCompressedFormatDataSize(GrPixelConfig config,
655 int width, int height) {
656 SkASSERT(GrPixelConfigIsCompressed(config));
bsalomond4cb9222014-08-11 14:19:09 -0700657 static const int kGrIndex8TableSize = 256 * 4; // 4 == sizeof(GrColor)
krajcevski9c0e6292014-06-02 07:38:14 -0700658
659 switch (config) {
bsalomond4cb9222014-08-11 14:19:09 -0700660 case kIndex_8_GrPixelConfig:
661 return width * height + kGrIndex8TableSize;
krajcevski238b4562014-06-30 09:09:22 -0700662 case kR11_EAC_GrPixelConfig:
krajcevski9c0e6292014-06-02 07:38:14 -0700663 case kLATC_GrPixelConfig:
664 case kETC1_GrPixelConfig:
665 SkASSERT((width & 3) == 0);
666 SkASSERT((height & 3) == 0);
667 return (width >> 2) * (height >> 2) * 8;
668
krajcevski7ef21622014-07-16 15:21:13 -0700669 case kASTC_12x12_GrPixelConfig:
670 SkASSERT((width % 12) == 0);
671 SkASSERT((height % 12) == 0);
672 return (width / 12) * (height / 12) * 16;
673
krajcevski9c0e6292014-06-02 07:38:14 -0700674 default:
675 SkFAIL("Unknown compressed pixel config");
676 return 4 * width * height;
677 }
678}
679
680/**
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000681 * This value translates to reseting all the context state for any backend.
682 */
683static const uint32_t kAll_GrBackendState = 0xffffffff;
684
reed@google.comac10a2d2010-12-22 21:39:39 +0000685#endif