blob: b3f1c19ebb7bec70ff734cb484c4f53bcb2612ab [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
cblume55f2d2d2016-02-26 13:20:48 -080011#include "SkMath.h"
reed@google.com9b24d252011-06-01 20:27:53 +000012#include "SkTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrConfig.h"
14
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); \
csmartdaltonf9635992016-08-10 11:09:07 -070052
53/**
54 * Defines bitwise operators that make it possible to use an enum class as a
55 * very basic bitfield.
56 */
57#define GR_MAKE_BITFIELD_CLASS_OPS(X) \
58 inline X operator | (X a, X b) { \
59 return (X) ((int)a | (int)b); \
60 } \
61 inline X& operator |= (X& a, X b) { \
62 return (a = a | b); \
63 } \
64 inline bool operator & (X a, X b) { \
65 return SkToBool((int)a & (int)b); \
66 }
67
68#define GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(X) \
69 friend X operator | (X a, X b); \
70 friend X& operator |= (X& a, X b); \
71 friend bool operator & (X a, X b);
72
bsalomon@google.comfea37b52011-04-25 15:51:06 +000073////////////////////////////////////////////////////////////////////////////////
74
reed@google.comac10a2d2010-12-22 21:39:39 +000075// compile time versions of min/max
76#define GR_CT_MAX(a, b) (((b) < (a)) ? (a) : (b))
77#define GR_CT_MIN(a, b) (((b) < (a)) ? (b) : (a))
78
79/**
80 * divide, rounding up
81 */
bsalomon@google.com91958362011-06-13 17:58:13 +000082static inline int32_t GrIDivRoundUp(int x, int y) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000083 SkASSERT(y > 0);
bsalomon@google.com91958362011-06-13 17:58:13 +000084 return (x + (y-1)) / y;
85}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000086static inline uint32_t GrUIDivRoundUp(uint32_t x, uint32_t y) {
87 return (x + (y-1)) / y;
88}
bsalomon@google.com4da34e32012-06-19 15:40:27 +000089static inline size_t GrSizeDivRoundUp(size_t x, size_t y) {
reed@google.comac10a2d2010-12-22 21:39:39 +000090 return (x + (y-1)) / y;
91}
92
bsalomon@google.com4da34e32012-06-19 15:40:27 +000093// compile time, evaluates Y multiple times
94#define GR_CT_DIV_ROUND_UP(X, Y) (((X) + ((Y)-1)) / (Y))
95
reed@google.comac10a2d2010-12-22 21:39:39 +000096/**
97 * align up
98 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +000099static inline uint32_t GrUIAlignUp(uint32_t x, uint32_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000100 return GrUIDivRoundUp(x, alignment) * alignment;
101}
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000102static inline size_t GrSizeAlignUp(size_t x, size_t alignment) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000103 return GrSizeDivRoundUp(x, alignment) * alignment;
104}
reed@google.comac10a2d2010-12-22 21:39:39 +0000105
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000106// compile time, evaluates A multiple times
107#define GR_CT_ALIGN_UP(X, A) (GR_CT_DIV_ROUND_UP((X),(A)) * (A))
108
reed@google.comac10a2d2010-12-22 21:39:39 +0000109/**
110 * amount of pad needed to align up
111 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000112static inline uint32_t GrUIAlignUpPad(uint32_t x, uint32_t alignment) {
113 return (alignment - x % alignment) % alignment;
114}
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000115static inline size_t GrSizeAlignUpPad(size_t x, size_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000116 return (alignment - x % alignment) % alignment;
117}
118
119/**
120 * align down
121 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000122static inline uint32_t GrUIAlignDown(uint32_t x, uint32_t alignment) {
123 return (x / alignment) * alignment;
124}
bsalomon@google.com5e497322012-08-28 21:45:26 +0000125static inline size_t GrSizeAlignDown(size_t x, uint32_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000126 return (x / alignment) * alignment;
127}
128
reed@google.comac10a2d2010-12-22 21:39:39 +0000129///////////////////////////////////////////////////////////////////////////////
130
131/**
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000132 * Possible 3D APIs that may be used by Ganesh.
133 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000134enum GrBackend {
135 kOpenGL_GrBackend,
jvanvertha50e17a2015-08-12 12:19:36 -0700136 kVulkan_GrBackend,
137
138 kLast_GrBackend = kVulkan_GrBackend
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000139};
jvanvertha50e17a2015-08-12 12:19:36 -0700140const int kBackendCount = kLast_GrBackend + 1;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000141
142/**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000143 * Backend-specific 3D context handle
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000144 * GrGLInterface* for OpenGL. If NULL will use the default GL interface.
bsalomon54dd2e22016-09-06 14:18:13 -0700145 * GrVkBackendContext* for Vulkan.
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000146 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000147typedef intptr_t GrBackendContext;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000148
149///////////////////////////////////////////////////////////////////////////////
150
151/**
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500152 * Used to control antialiasing in draw calls.
153 */
154enum class GrAA {
155 kYes,
156 kNo
157};
158
159static inline GrAA GrBoolToAA(bool aa) { return aa ? GrAA::kYes : GrAA::kNo; }
160
161///////////////////////////////////////////////////////////////////////////////
162
163/**
bsalomon@google.comffca4002011-02-22 20:34:01 +0000164* Geometric primitives used for drawing.
165*/
166enum GrPrimitiveType {
bsalomon@google.com47059542012-06-06 20:51:20 +0000167 kTriangles_GrPrimitiveType,
168 kTriangleStrip_GrPrimitiveType,
169 kTriangleFan_GrPrimitiveType,
170 kPoints_GrPrimitiveType,
171 kLines_GrPrimitiveType, // 1 pix wide only
joshualitte5902662015-05-11 08:18:35 -0700172 kLineStrip_GrPrimitiveType, // 1 pix wide only
173 kLast_GrPrimitiveType = kLineStrip_GrPrimitiveType
bsalomon@google.comffca4002011-02-22 20:34:01 +0000174};
175
bsalomon@google.com0650e812011-04-08 18:07:53 +0000176static inline bool GrIsPrimTypeLines(GrPrimitiveType type) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000177 return kLines_GrPrimitiveType == type || kLineStrip_GrPrimitiveType == type;
bsalomon@google.com0650e812011-04-08 18:07:53 +0000178}
179
180static inline bool GrIsPrimTypeTris(GrPrimitiveType type) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000181 return kTriangles_GrPrimitiveType == type ||
182 kTriangleStrip_GrPrimitiveType == type ||
183 kTriangleFan_GrPrimitiveType == type;
bsalomon@google.com0650e812011-04-08 18:07:53 +0000184}
185
bsalomon@google.comffca4002011-02-22 20:34:01 +0000186/**
reed@google.com759c16e2011-03-15 19:15:15 +0000187 * Formats for masks, used by the font cache.
188 * Important that these are 0-based.
reed@google.com98539c62011-03-15 15:40:16 +0000189 */
190enum GrMaskFormat {
caryclark@google.com1eeaf0b2011-06-22 13:19:43 +0000191 kA8_GrMaskFormat, //!< 1-byte per pixel
jvanverth5eefe422014-11-26 12:07:33 -0800192 kA565_GrMaskFormat, //!< 2-bytes per pixel, RGB represent 3-channel LCD coverage
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000193 kARGB_GrMaskFormat, //!< 4-bytes per pixel, color format
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +0000194
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000195 kLast_GrMaskFormat = kARGB_GrMaskFormat
reed@google.com98539c62011-03-15 15:40:16 +0000196};
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000197static const int kMaskFormatCount = kLast_GrMaskFormat + 1;
reed@google.com98539c62011-03-15 15:40:16 +0000198
199/**
200 * Return the number of bytes-per-pixel for the specified mask format.
201 */
202static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) {
mtklein1d02a602015-11-19 09:53:21 -0800203 SkASSERT(format < kMaskFormatCount);
reed@google.combbf12262011-10-04 20:14:57 +0000204 // kA8 (0) -> 1
205 // kA565 (1) -> 2
jvanverth5eefe422014-11-26 12:07:33 -0800206 // kARGB (2) -> 4
reedd54d3fc2014-11-13 14:39:58 -0800207 static const int sBytesPerPixel[] = { 1, 2, 4 };
bungeman99fe8222015-08-20 07:57:51 -0700208 static_assert(SK_ARRAY_COUNT(sBytesPerPixel) == kMaskFormatCount, "array_size_mismatch");
209 static_assert(kA8_GrMaskFormat == 0, "enum_order_dependency");
210 static_assert(kA565_GrMaskFormat == 1, "enum_order_dependency");
211 static_assert(kARGB_GrMaskFormat == 2, "enum_order_dependency");
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000212
213 return sBytesPerPixel[(int) format];
reed@google.com98539c62011-03-15 15:40:16 +0000214}
215
216/**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000217 * Pixel configurations.
218 */
219enum GrPixelConfig {
220 kUnknown_GrPixelConfig,
221 kAlpha_8_GrPixelConfig,
Brian Osman986563b2017-01-10 14:20:02 -0500222 kGray_8_GrPixelConfig,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000223 kRGB_565_GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000224 /**
225 * Premultiplied
226 */
227 kRGBA_4444_GrPixelConfig,
228 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000229 * Premultiplied. Byte order is r,g,b,a.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000230 */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000231 kRGBA_8888_GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000232 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000233 * Premultiplied. Byte order is b,g,r,a.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000234 */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000235 kBGRA_8888_GrPixelConfig,
joshualittee5da552014-07-16 13:32:56 -0700236 /**
jvanverthfa1e8a72014-12-22 08:31:49 -0800237 * Premultiplied and sRGB. Byte order is r,g,b,a.
238 */
239 kSRGBA_8888_GrPixelConfig,
240 /**
brianosmana6359362016-03-21 06:55:37 -0700241 * Premultiplied and sRGB. Byte order is b,g,r,a.
242 */
243 kSBGRA_8888_GrPixelConfig,
244 /**
Brian Salomonbf7b6202016-11-11 16:08:03 -0500245 * 8 bit signed integers per-channel. Byte order is b,g,r,a.
246 */
247 kRGBA_8888_sint_GrPixelConfig,
248 /**
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000249 * ETC1 Compressed Data
250 */
251 kETC1_GrPixelConfig,
252 /**
253 * LATC/RGTC/3Dc/BC4 Compressed Data
254 */
255 kLATC_GrPixelConfig,
krajcevski238b4562014-06-30 09:09:22 -0700256 /**
257 * R11 EAC Compressed Data
258 * (Corresponds to section C.3.5 of the OpenGL 4.4 core profile spec)
259 */
260 kR11_EAC_GrPixelConfig,
krajcevski7ef21622014-07-16 15:21:13 -0700261
262 /**
263 * 12x12 ASTC Compressed Data
264 * ASTC stands for Adaptive Scalable Texture Compression. It is a technique
265 * that allows for a lot of customization in the compressed representataion
266 * of a block. The only thing fixed in the representation is the block size,
267 * which means that a texture that contains ASTC data must be treated as
268 * having RGBA values. However, there are single-channel encodings which set
269 * the alpha to opaque and all three RGB channels equal effectively making the
270 * compression format a single channel such as R11 EAC and LATC.
271 */
272 kASTC_12x12_GrPixelConfig,
273
joshualittee5da552014-07-16 13:32:56 -0700274 /**
275 * Byte order is r, g, b, a. This color format is 32 bits per channel
276 */
277 kRGBA_float_GrPixelConfig,
jvanverth28f9c602014-12-05 13:06:35 -0800278
279 /**
280 * This color format is a single 16 bit float channel
281 */
282 kAlpha_half_GrPixelConfig,
283
jvanverthfb5df432015-05-21 08:12:27 -0700284 /**
285 * Byte order is r, g, b, a. This color format is 16 bits per channel
286 */
287 kRGBA_half_GrPixelConfig,
288
289 kLast_GrPixelConfig = kRGBA_half_GrPixelConfig
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000290};
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +0000291static const int kGrPixelConfigCnt = kLast_GrPixelConfig + 1;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000292
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000293// Aliases for pixel configs that match skia's byte order.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000294#ifndef SK_CPU_LENDIAN
295 #error "Skia gpu currently assumes little endian"
296#endif
commit-bot@chromium.orge4657ed2013-03-19 14:16:31 +0000297#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
bsalomon@google.com0342a852012-08-20 19:22:38 +0000298 static const GrPixelConfig kSkia8888_GrPixelConfig = kBGRA_8888_GrPixelConfig;
commit-bot@chromium.orge4657ed2013-03-19 14:16:31 +0000299#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
bsalomon@google.com0342a852012-08-20 19:22:38 +0000300 static const GrPixelConfig kSkia8888_GrPixelConfig = kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000301#else
302 #error "SK_*32_SHIFT values must correspond to GL_BGRA or GL_RGBA format."
303#endif
304
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000305// Returns true if the pixel config is a GPU-specific compressed format
306// representation.
307static inline bool GrPixelConfigIsCompressed(GrPixelConfig config) {
308 switch (config) {
309 case kETC1_GrPixelConfig:
310 case kLATC_GrPixelConfig:
krajcevski238b4562014-06-30 09:09:22 -0700311 case kR11_EAC_GrPixelConfig:
krajcevski7ef21622014-07-16 15:21:13 -0700312 case kASTC_12x12_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000313 return true;
314 default:
315 return false;
316 }
317}
318
bsalomon37f9a262015-02-02 13:00:10 -0800319/** If the pixel config is compressed, return an equivalent uncompressed format. */
320static inline GrPixelConfig GrMakePixelConfigUncompressed(GrPixelConfig config) {
321 switch (config) {
bsalomon37f9a262015-02-02 13:00:10 -0800322 case kETC1_GrPixelConfig:
323 case kASTC_12x12_GrPixelConfig:
324 return kRGBA_8888_GrPixelConfig;
325 case kLATC_GrPixelConfig:
326 case kR11_EAC_GrPixelConfig:
327 return kAlpha_8_GrPixelConfig;
328 default:
329 SkASSERT(!GrPixelConfigIsCompressed(config));
330 return config;
331 }
332}
333
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000334// Returns true if the pixel config is 32 bits per pixel
Brian Salomonbf7b6202016-11-11 16:08:03 -0500335static inline bool GrPixelConfigIs8888Unorm(GrPixelConfig config) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000336 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000337 case kRGBA_8888_GrPixelConfig:
338 case kBGRA_8888_GrPixelConfig:
jvanverthfa1e8a72014-12-22 08:31:49 -0800339 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700340 case kSBGRA_8888_GrPixelConfig:
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000341 return true;
342 default:
343 return false;
344 }
345}
346
bsalomon16921ec2015-07-30 15:34:56 -0700347// Returns true if the color (non-alpha) components represent sRGB values. It does NOT indicate that
348// all three color components are present in the config or anything about their order.
349static inline bool GrPixelConfigIsSRGB(GrPixelConfig config) {
350 switch (config) {
351 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700352 case kSBGRA_8888_GrPixelConfig:
bsalomon16921ec2015-07-30 15:34:56 -0700353 return true;
354 default:
355 return false;
356 }
357}
358
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000359// Takes a config and returns the equivalent config with the R and B order
360// swapped if such a config exists. Otherwise, kUnknown_GrPixelConfig
361static inline GrPixelConfig GrPixelConfigSwapRAndB(GrPixelConfig config) {
362 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000363 case kBGRA_8888_GrPixelConfig:
364 return kRGBA_8888_GrPixelConfig;
365 case kRGBA_8888_GrPixelConfig:
366 return kBGRA_8888_GrPixelConfig;
brianosmana6359362016-03-21 06:55:37 -0700367 case kSBGRA_8888_GrPixelConfig:
368 return kSRGBA_8888_GrPixelConfig;
369 case kSRGBA_8888_GrPixelConfig:
370 return kSBGRA_8888_GrPixelConfig;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000371 default:
372 return kUnknown_GrPixelConfig;
373 }
374}
375
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000376static inline size_t GrBytesPerPixel(GrPixelConfig config) {
bsalomond4cb9222014-08-11 14:19:09 -0700377 SkASSERT(!GrPixelConfigIsCompressed(config));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000378 switch (config) {
379 case kAlpha_8_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500380 case kGray_8_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000381 return 1;
382 case kRGB_565_GrPixelConfig:
383 case kRGBA_4444_GrPixelConfig:
jvanverth28f9c602014-12-05 13:06:35 -0800384 case kAlpha_half_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000385 return 2;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000386 case kRGBA_8888_GrPixelConfig:
387 case kBGRA_8888_GrPixelConfig:
jvanverthfa1e8a72014-12-22 08:31:49 -0800388 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700389 case kSBGRA_8888_GrPixelConfig:
Brian Salomonbf7b6202016-11-11 16:08:03 -0500390 case kRGBA_8888_sint_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000391 return 4;
jvanverthfb5df432015-05-21 08:12:27 -0700392 case kRGBA_half_GrPixelConfig:
393 return 8;
joshualittee5da552014-07-16 13:32:56 -0700394 case kRGBA_float_GrPixelConfig:
395 return 16;
396 default:
397 return 0;
398 }
399}
400
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000401static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
402 switch (config) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000403 case kETC1_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000404 case kRGB_565_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500405 case kGray_8_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +0000406 return true;
407 default:
408 return false;
409 }
410}
411
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000412static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
413 switch (config) {
krajcevski238b4562014-06-30 09:09:22 -0700414 case kR11_EAC_GrPixelConfig:
krajcevski748e9d32014-06-09 08:32:34 -0700415 case kLATC_GrPixelConfig:
krajcevski2a413df2014-07-24 08:16:00 -0700416 case kASTC_12x12_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000417 case kAlpha_8_GrPixelConfig:
jvanverth28f9c602014-12-05 13:06:35 -0800418 case kAlpha_half_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000419 return true;
420 default:
421 return false;
422 }
423}
424
csmartdaltonc411f2d2016-06-27 09:29:07 -0700425static inline bool GrPixelConfigIsFloatingPoint(GrPixelConfig config) {
426 switch (config) {
427 case kRGBA_float_GrPixelConfig:
428 case kAlpha_half_GrPixelConfig:
429 case kRGBA_half_GrPixelConfig:
430 return true;
431 default:
432 return false;
433 }
434}
435
Brian Salomonbf7b6202016-11-11 16:08:03 -0500436static inline bool GrPixelConfigIsSint(GrPixelConfig config) {
437 return config == kRGBA_8888_sint_GrPixelConfig;
438}
439
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000440/**
bsalomonf2703d82014-10-28 14:33:06 -0700441 * Optional bitfield flags that can be set on GrSurfaceDesc (below).
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000442 */
bsalomonf2703d82014-10-28 14:33:06 -0700443enum GrSurfaceFlags {
444 kNone_GrSurfaceFlags = 0x0,
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000445 /**
446 * Creates a texture that can be rendered to as a GrRenderTarget. Use
447 * GrTexture::asRenderTarget() to access.
448 */
bsalomonf2703d82014-10-28 14:33:06 -0700449 kRenderTarget_GrSurfaceFlag = 0x1,
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000450 /**
jvanverth5a42c332015-09-02 12:26:10 -0700451 * Placeholder for managing zero-copy textures
452 */
453 kZeroCopy_GrSurfaceFlag = 0x2,
454 /**
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000455 * Indicates that all allocations (color buffer, FBO completeness, etc)
456 * should be verified.
457 */
bsalomonf2703d82014-10-28 14:33:06 -0700458 kCheckAllocation_GrSurfaceFlag = 0x4,
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000459};
460
bsalomonf2703d82014-10-28 14:33:06 -0700461GR_MAKE_BITFIELD_OPS(GrSurfaceFlags)
462
erikchen9a1ed5d2016-02-10 16:32:34 -0800463// opaque type for 3D API object handles
464typedef intptr_t GrBackendObject;
465
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000466/**
467 * Some textures will be stored such that the upper and left edges of the content meet at the
468 * 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 +0000469 * the origin. kDefault_GrSurfaceOrigin sets textures to TopLeft, and render targets
470 * to BottomLeft.
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000471 */
472
473enum GrSurfaceOrigin {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000474 kDefault_GrSurfaceOrigin, // DEPRECATED; to be removed
robertphillips@google.comcf9faf62013-02-05 14:05:06 +0000475 kTopLeft_GrSurfaceOrigin,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000476 kBottomLeft_GrSurfaceOrigin,
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000477};
robertphillips@google.com5091b702012-08-09 10:49:39 +0000478
cblume55f2d2d2016-02-26 13:20:48 -0800479struct GrMipLevel {
480 const void* fPixels;
481 size_t fRowBytes;
482};
483
robertphillips@google.com5091b702012-08-09 10:49:39 +0000484/**
bsalomonf2703d82014-10-28 14:33:06 -0700485 * Describes a surface to be created.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000486 */
bsalomonf2703d82014-10-28 14:33:06 -0700487struct GrSurfaceDesc {
488 GrSurfaceDesc()
489 : fFlags(kNone_GrSurfaceFlags)
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000490 , fOrigin(kDefault_GrSurfaceOrigin)
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000491 , fWidth(0)
492 , fHeight(0)
493 , fConfig(kUnknown_GrPixelConfig)
cblume55f2d2d2016-02-26 13:20:48 -0800494 , fSampleCnt(0)
495 , fIsMipMapped(false) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000496 }
497
bsalomonf2703d82014-10-28 14:33:06 -0700498 GrSurfaceFlags fFlags; //!< bitfield of TextureFlags
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000499 GrSurfaceOrigin fOrigin; //!< origin of the texture
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +0000500 int fWidth; //!< Width of the texture
501 int fHeight; //!< Height of the texture
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000502
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000503 /**
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000504 * Format of source data of the texture. Not guaranteed to be the same as
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000505 * internal format used by 3D API.
506 */
bsalomon@google.com5bc34f02011-12-06 14:46:34 +0000507 GrPixelConfig fConfig;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000508
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000509 /**
510 * The number of samples per pixel or 0 to disable full scene AA. This only
bsalomonf2703d82014-10-28 14:33:06 -0700511 * applies if the kRenderTarget_GrSurfaceFlag is set. The actual number
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000512 * of samples may not exactly match the request. The request will be rounded
513 * up to the next supported sample count, or down if it is larger than the
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000514 * max supported count.
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000515 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000516 int fSampleCnt;
cblume55f2d2d2016-02-26 13:20:48 -0800517 bool fIsMipMapped; //!< Indicates if the texture has mipmaps
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.
bsalomon54dd2e22016-09-06 14:18:13 -0700592 * Vulkan: GrVkImageInfo*
bsalomon@google.come269f212011-11-07 13:29:52 +0000593 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000594 GrBackendObject fTextureHandle;
bsalomon@google.come269f212011-11-07 13:29:52 +0000595};
596
597///////////////////////////////////////////////////////////////////////////////
598
599/**
600 * Gr can wrap an existing render target created by the client in the 3D API
601 * with a GrRenderTarget object. The client is responsible for ensuring that the
602 * underlying 3D API object lives at least as long as the GrRenderTarget object
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000603 * wrapping it. We require the client to explicitly provide information about
bsalomon@google.come269f212011-11-07 13:29:52 +0000604 * the target, such as width, height, and pixel config rather than querying the
605 * 3D API for these values. We expect these properties to be immutable even if
606 * the 3D API doesn't require this (OpenGL).
607 */
608
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000609struct GrBackendRenderTargetDesc {
610 GrBackendRenderTargetDesc() { memset(this, 0, sizeof(*this)); }
bsalomon@google.come269f212011-11-07 13:29:52 +0000611 int fWidth; //<! width in pixels
612 int fHeight; //<! height in pixels
613 GrPixelConfig fConfig; //<! color format
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000614 GrSurfaceOrigin fOrigin; //<! pixel origin
bsalomon@google.come269f212011-11-07 13:29:52 +0000615 /**
616 * The number of samples per pixel. Gr uses this to influence decisions
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000617 * about applying other forms of anti-aliasing.
bsalomon@google.come269f212011-11-07 13:29:52 +0000618 */
619 int fSampleCnt;
620 /**
621 * Number of bits of stencil per-pixel.
622 */
623 int fStencilBits;
624 /**
625 * Handle to the 3D API object.
626 * OpenGL: FBO ID
bsalomon54dd2e22016-09-06 14:18:13 -0700627 * Vulkan: GrVkImageInfo*
bsalomon@google.come269f212011-11-07 13:29:52 +0000628 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000629 GrBackendObject fRenderTargetHandle;
bsalomon@google.come269f212011-11-07 13:29:52 +0000630};
631
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000632/**
633 * The GrContext's cache of backend context state can be partially invalidated.
634 * These enums are specific to the GL backend and we'd add a new set for an alternative backend.
635 */
636enum GrGLBackendState {
637 kRenderTarget_GrGLBackendState = 1 << 0,
638 kTextureBinding_GrGLBackendState = 1 << 1,
639 // View state stands for scissor and viewport
640 kView_GrGLBackendState = 1 << 2,
641 kBlend_GrGLBackendState = 1 << 3,
egdanielb414f252014-07-29 13:15:47 -0700642 kMSAAEnable_GrGLBackendState = 1 << 4,
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000643 kVertex_GrGLBackendState = 1 << 5,
644 kStencil_GrGLBackendState = 1 << 6,
645 kPixelStore_GrGLBackendState = 1 << 7,
646 kProgram_GrGLBackendState = 1 << 8,
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000647 kFixedFunction_GrGLBackendState = 1 << 9,
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000648 kMisc_GrGLBackendState = 1 << 10,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000649 kPathRendering_GrGLBackendState = 1 << 11,
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000650 kALL_GrGLBackendState = 0xffff
651};
652
653/**
krajcevski9c0e6292014-06-02 07:38:14 -0700654 * Returns the data size for the given compressed pixel config
joshualittee5da552014-07-16 13:32:56 -0700655 */
krajcevski9c0e6292014-06-02 07:38:14 -0700656static inline size_t GrCompressedFormatDataSize(GrPixelConfig config,
657 int width, int height) {
658 SkASSERT(GrPixelConfigIsCompressed(config));
659
660 switch (config) {
krajcevski238b4562014-06-30 09:09:22 -0700661 case kR11_EAC_GrPixelConfig:
krajcevski9c0e6292014-06-02 07:38:14 -0700662 case kLATC_GrPixelConfig:
663 case kETC1_GrPixelConfig:
664 SkASSERT((width & 3) == 0);
665 SkASSERT((height & 3) == 0);
666 return (width >> 2) * (height >> 2) * 8;
667
krajcevski7ef21622014-07-16 15:21:13 -0700668 case kASTC_12x12_GrPixelConfig:
669 SkASSERT((width % 12) == 0);
670 SkASSERT((height % 12) == 0);
671 return (width / 12) * (height / 12) * 16;
672
krajcevski9c0e6292014-06-02 07:38:14 -0700673 default:
674 SkFAIL("Unknown compressed pixel config");
675 return 4 * width * height;
676 }
677}
678
679/**
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000680 * This value translates to reseting all the context state for any backend.
681 */
682static const uint32_t kAll_GrBackendState = 0xffffffff;
683
reed@google.comac10a2d2010-12-22 21:39:39 +0000684#endif