blob: 8d76364e2016e74d2bc357ee886f3011c5b1b9a2 [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) \
Brian Salomon587e08f2017-01-27 10:59:27 -050022 inline X operator |(X a, X b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000023 return (X) (+a | +b); \
24 } \
Brian Salomon587e08f2017-01-27 10:59:27 -050025 inline X& operator |=(X& a, X b) { \
cdalton32117702015-05-11 11:21:23 -070026 return (a = a | b); \
27 } \
Brian Salomon587e08f2017-01-27 10:59:27 -050028 inline X operator &(X a, X b) { \
29 return (X) (+a & +b); \
30 } \
31 inline X& operator &=(X& a, X b) { \
32 return (a = a & b); \
33 } \
34 template <typename T> \
35 inline X operator &(T a, X b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000036 return (X) (+a & +b); \
37 } \
38 template <typename T> \
Brian Salomon587e08f2017-01-27 10:59:27 -050039 inline X operator &(X a, T b) { \
bsalomon@google.comfea37b52011-04-25 15:51:06 +000040 return (X) (+a & +b); \
41 } \
42
bsalomon@google.com86c1f712011-10-12 14:54:26 +000043#define GR_DECL_BITFIELD_OPS_FRIENDS(X) \
Brian Salomon587e08f2017-01-27 10:59:27 -050044 friend X operator |(X a, X b); \
45 friend X& operator |=(X& a, X b); \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000046 \
Brian Salomon587e08f2017-01-27 10:59:27 -050047 friend X operator &(X a, X b); \
48 friend X& operator &=(X& a, X b); \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000049 \
50 template <typename T> \
Brian Salomon587e08f2017-01-27 10:59:27 -050051 friend X operator &(T a, X b); \
bsalomon@google.com86c1f712011-10-12 14:54:26 +000052 \
53 template <typename T> \
Brian Salomon587e08f2017-01-27 10:59:27 -050054 friend X operator &(X a, T b); \
csmartdaltonf9635992016-08-10 11:09:07 -070055
56/**
csmartdaltonad2a2be2017-02-19 23:51:45 -070057 * Wraps a C++11 enum that we use as a bitfield, and enables a limited amount of
58 * masking with type safety. Instantiated with the ~ operator.
59 */
60template<typename TFlags> class GrTFlagsMask {
61public:
62 constexpr explicit GrTFlagsMask(TFlags value) : GrTFlagsMask(static_cast<int>(value)) {}
63 constexpr explicit GrTFlagsMask(int value) : fValue(value) {}
64 constexpr int value() const { return fValue; }
65private:
66 const int fValue;
67};
68
69// Or-ing a mask always returns another mask.
70template<typename TFlags> constexpr GrTFlagsMask<TFlags> operator|(GrTFlagsMask<TFlags> a,
71 GrTFlagsMask<TFlags> b) {
72 return GrTFlagsMask<TFlags>(a.value() | b.value());
73}
74template<typename TFlags> constexpr GrTFlagsMask<TFlags> operator|(GrTFlagsMask<TFlags> a,
75 TFlags b) {
76 return GrTFlagsMask<TFlags>(a.value() | static_cast<int>(b));
77}
78template<typename TFlags> constexpr GrTFlagsMask<TFlags> operator|(TFlags a,
79 GrTFlagsMask<TFlags> b) {
80 return GrTFlagsMask<TFlags>(static_cast<int>(a) | b.value());
81}
82template<typename TFlags> inline GrTFlagsMask<TFlags>& operator|=(GrTFlagsMask<TFlags>& a,
83 GrTFlagsMask<TFlags> b) {
84 return (a = a | b);
85}
86
87// And-ing two masks returns another mask; and-ing one with regular flags returns flags.
88template<typename TFlags> constexpr GrTFlagsMask<TFlags> operator&(GrTFlagsMask<TFlags> a,
89 GrTFlagsMask<TFlags> b) {
90 return GrTFlagsMask<TFlags>(a.value() & b.value());
91}
92template<typename TFlags> constexpr TFlags operator&(GrTFlagsMask<TFlags> a, TFlags b) {
93 return static_cast<TFlags>(a.value() & static_cast<int>(b));
94}
95template<typename TFlags> constexpr TFlags operator&(TFlags a, GrTFlagsMask<TFlags> b) {
96 return static_cast<TFlags>(static_cast<int>(a) & b.value());
97}
98template<typename TFlags> inline TFlags& operator&=(TFlags& a, GrTFlagsMask<TFlags> b) {
99 return (a = a & b);
100}
101
102/**
csmartdaltonf9635992016-08-10 11:09:07 -0700103 * Defines bitwise operators that make it possible to use an enum class as a
csmartdaltonad2a2be2017-02-19 23:51:45 -0700104 * basic bitfield.
csmartdaltonf9635992016-08-10 11:09:07 -0700105 */
106#define GR_MAKE_BITFIELD_CLASS_OPS(X) \
csmartdaltonad2a2be2017-02-19 23:51:45 -0700107 constexpr GrTFlagsMask<X> operator~(X a) { \
108 return GrTFlagsMask<X>(~static_cast<int>(a)); \
csmartdaltonf9635992016-08-10 11:09:07 -0700109 } \
csmartdaltonad2a2be2017-02-19 23:51:45 -0700110 constexpr X operator|(X a, X b) { \
111 return static_cast<X>(static_cast<int>(a) | static_cast<int>(b)); \
112 } \
113 inline X& operator|=(X& a, X b) { \
csmartdaltonf9635992016-08-10 11:09:07 -0700114 return (a = a | b); \
115 } \
csmartdaltonad2a2be2017-02-19 23:51:45 -0700116 constexpr bool operator&(X a, X b) { \
117 return SkToBool(static_cast<int>(a) & static_cast<int>(b)); \
118 } \
csmartdaltonf9635992016-08-10 11:09:07 -0700119
120#define GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(X) \
csmartdaltonad2a2be2017-02-19 23:51:45 -0700121 friend constexpr GrTFlagsMask<X> operator ~(X); \
122 friend constexpr X operator |(X, X); \
123 friend X& operator |=(X&, X); \
124 friend constexpr bool operator &(X, X);
csmartdaltonf9635992016-08-10 11:09:07 -0700125
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000126////////////////////////////////////////////////////////////////////////////////
127
reed@google.comac10a2d2010-12-22 21:39:39 +0000128// compile time versions of min/max
129#define GR_CT_MAX(a, b) (((b) < (a)) ? (a) : (b))
130#define GR_CT_MIN(a, b) (((b) < (a)) ? (b) : (a))
131
132/**
133 * divide, rounding up
134 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000135static inline int32_t GrIDivRoundUp(int x, int y) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000136 SkASSERT(y > 0);
bsalomon@google.com91958362011-06-13 17:58:13 +0000137 return (x + (y-1)) / y;
138}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000139static inline uint32_t GrUIDivRoundUp(uint32_t x, uint32_t y) {
140 return (x + (y-1)) / y;
141}
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000142static inline size_t GrSizeDivRoundUp(size_t x, size_t y) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000143 return (x + (y-1)) / y;
144}
145
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000146// compile time, evaluates Y multiple times
147#define GR_CT_DIV_ROUND_UP(X, Y) (((X) + ((Y)-1)) / (Y))
148
reed@google.comac10a2d2010-12-22 21:39:39 +0000149/**
150 * align up
151 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000152static inline uint32_t GrUIAlignUp(uint32_t x, uint32_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000153 return GrUIDivRoundUp(x, alignment) * alignment;
154}
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000155static inline size_t GrSizeAlignUp(size_t x, size_t alignment) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000156 return GrSizeDivRoundUp(x, alignment) * alignment;
157}
reed@google.comac10a2d2010-12-22 21:39:39 +0000158
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000159// compile time, evaluates A multiple times
160#define GR_CT_ALIGN_UP(X, A) (GR_CT_DIV_ROUND_UP((X),(A)) * (A))
161
reed@google.comac10a2d2010-12-22 21:39:39 +0000162/**
163 * amount of pad needed to align up
164 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000165static inline uint32_t GrUIAlignUpPad(uint32_t x, uint32_t alignment) {
166 return (alignment - x % alignment) % alignment;
167}
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000168static inline size_t GrSizeAlignUpPad(size_t x, size_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000169 return (alignment - x % alignment) % alignment;
170}
171
172/**
173 * align down
174 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000175static inline uint32_t GrUIAlignDown(uint32_t x, uint32_t alignment) {
176 return (x / alignment) * alignment;
177}
bsalomon@google.com5e497322012-08-28 21:45:26 +0000178static inline size_t GrSizeAlignDown(size_t x, uint32_t alignment) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000179 return (x / alignment) * alignment;
180}
181
reed@google.comac10a2d2010-12-22 21:39:39 +0000182///////////////////////////////////////////////////////////////////////////////
183
184/**
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000185 * Possible 3D APIs that may be used by Ganesh.
186 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000187enum GrBackend {
Greg Daniele5ddff52017-07-05 16:49:36 -0400188 kMetal_GrBackend,
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000189 kOpenGL_GrBackend,
jvanvertha50e17a2015-08-12 12:19:36 -0700190 kVulkan_GrBackend,
Brian Salomon8fe24272017-07-07 12:56:11 -0400191 /**
192 * Mock is a backend that does not draw anything. It is used for unit tests
193 * and to measure CPU overhead.
194 */
Brian Salomon91a3e522017-06-23 10:58:19 -0400195 kMock_GrBackend,
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000196};
197
198/**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000199 * Backend-specific 3D context handle
Brian Salomoncfe910d2017-07-06 16:40:18 -0400200 * OpenGL: const GrGLInterface*. If null will use the result of GrGLCreateNativeInterface().
201 * Vulkan: GrVkBackendContext*.
202 * Mock: const GrMockOptions* or null for default constructed GrMockContextOptions.
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000203 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000204typedef intptr_t GrBackendContext;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000205
206///////////////////////////////////////////////////////////////////////////////
207
208/**
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500209 * Used to control antialiasing in draw calls.
210 */
211enum class GrAA {
212 kYes,
213 kNo
214};
215
216static inline GrAA GrBoolToAA(bool aa) { return aa ? GrAA::kYes : GrAA::kNo; }
217
218///////////////////////////////////////////////////////////////////////////////
219
220/**
bsalomon@google.comffca4002011-02-22 20:34:01 +0000221* Geometric primitives used for drawing.
222*/
Chris Dalton3809bab2017-06-13 10:55:06 -0600223enum class GrPrimitiveType {
224 kTriangles,
225 kTriangleStrip,
226 kTriangleFan,
227 kPoints,
228 kLines, // 1 pix wide only
229 kLineStrip, // 1 pix wide only
230 kLinesAdjacency // requires geometry shader support.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000231};
Chris Dalton3809bab2017-06-13 10:55:06 -0600232static constexpr int kNumGrPrimitiveTypes = (int) GrPrimitiveType::kLinesAdjacency + 1;
bsalomon@google.comffca4002011-02-22 20:34:01 +0000233
Chris Daltonb894c2b2017-06-14 12:39:19 -0600234static constexpr bool GrIsPrimTypeLines(GrPrimitiveType type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600235 return GrPrimitiveType::kLines == type ||
236 GrPrimitiveType::kLineStrip == type ||
237 GrPrimitiveType::kLinesAdjacency == type;
bsalomon@google.com0650e812011-04-08 18:07:53 +0000238}
239
Chris Daltonb894c2b2017-06-14 12:39:19 -0600240static constexpr bool GrIsPrimTypeTris(GrPrimitiveType type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600241 return GrPrimitiveType::kTriangles == type ||
242 GrPrimitiveType::kTriangleStrip == type ||
243 GrPrimitiveType::kTriangleFan == type;
bsalomon@google.com0650e812011-04-08 18:07:53 +0000244}
245
Chris Daltonb894c2b2017-06-14 12:39:19 -0600246static constexpr bool GrPrimTypeRequiresGeometryShaderSupport(GrPrimitiveType type) {
247 return GrPrimitiveType::kLinesAdjacency == type;
248}
249
bsalomon@google.comffca4002011-02-22 20:34:01 +0000250/**
reed@google.com759c16e2011-03-15 19:15:15 +0000251 * Formats for masks, used by the font cache.
252 * Important that these are 0-based.
reed@google.com98539c62011-03-15 15:40:16 +0000253 */
254enum GrMaskFormat {
caryclark@google.com1eeaf0b2011-06-22 13:19:43 +0000255 kA8_GrMaskFormat, //!< 1-byte per pixel
jvanverth5eefe422014-11-26 12:07:33 -0800256 kA565_GrMaskFormat, //!< 2-bytes per pixel, RGB represent 3-channel LCD coverage
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000257 kARGB_GrMaskFormat, //!< 4-bytes per pixel, color format
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +0000258
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000259 kLast_GrMaskFormat = kARGB_GrMaskFormat
reed@google.com98539c62011-03-15 15:40:16 +0000260};
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000261static const int kMaskFormatCount = kLast_GrMaskFormat + 1;
reed@google.com98539c62011-03-15 15:40:16 +0000262
263/**
264 * Return the number of bytes-per-pixel for the specified mask format.
265 */
266static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) {
mtklein1d02a602015-11-19 09:53:21 -0800267 SkASSERT(format < kMaskFormatCount);
reed@google.combbf12262011-10-04 20:14:57 +0000268 // kA8 (0) -> 1
269 // kA565 (1) -> 2
jvanverth5eefe422014-11-26 12:07:33 -0800270 // kARGB (2) -> 4
reedd54d3fc2014-11-13 14:39:58 -0800271 static const int sBytesPerPixel[] = { 1, 2, 4 };
bungeman99fe8222015-08-20 07:57:51 -0700272 static_assert(SK_ARRAY_COUNT(sBytesPerPixel) == kMaskFormatCount, "array_size_mismatch");
273 static_assert(kA8_GrMaskFormat == 0, "enum_order_dependency");
274 static_assert(kA565_GrMaskFormat == 1, "enum_order_dependency");
275 static_assert(kARGB_GrMaskFormat == 2, "enum_order_dependency");
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000276
277 return sBytesPerPixel[(int) format];
reed@google.com98539c62011-03-15 15:40:16 +0000278}
279
280/**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000281 * Pixel configurations.
282 */
283enum GrPixelConfig {
284 kUnknown_GrPixelConfig,
285 kAlpha_8_GrPixelConfig,
Brian Osman986563b2017-01-10 14:20:02 -0500286 kGray_8_GrPixelConfig,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000287 kRGB_565_GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000288 /**
289 * Premultiplied
290 */
291 kRGBA_4444_GrPixelConfig,
292 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000293 * Premultiplied. Byte order is r,g,b,a.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000294 */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000295 kRGBA_8888_GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000296 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000297 * Premultiplied. Byte order is b,g,r,a.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000298 */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000299 kBGRA_8888_GrPixelConfig,
joshualittee5da552014-07-16 13:32:56 -0700300 /**
jvanverthfa1e8a72014-12-22 08:31:49 -0800301 * Premultiplied and sRGB. Byte order is r,g,b,a.
302 */
303 kSRGBA_8888_GrPixelConfig,
304 /**
brianosmana6359362016-03-21 06:55:37 -0700305 * Premultiplied and sRGB. Byte order is b,g,r,a.
306 */
307 kSBGRA_8888_GrPixelConfig,
308 /**
Brian Salomonbf7b6202016-11-11 16:08:03 -0500309 * 8 bit signed integers per-channel. Byte order is b,g,r,a.
310 */
311 kRGBA_8888_sint_GrPixelConfig,
Robert Phillips92de6312017-05-23 07:43:48 -0400312
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000313 /**
joshualittee5da552014-07-16 13:32:56 -0700314 * Byte order is r, g, b, a. This color format is 32 bits per channel
315 */
316 kRGBA_float_GrPixelConfig,
csmartdalton6aa0e112017-02-08 16:14:11 -0500317 /**
318 * Byte order is r, g. This color format is 32 bits per channel
319 */
320 kRG_float_GrPixelConfig,
jvanverth28f9c602014-12-05 13:06:35 -0800321
322 /**
323 * This color format is a single 16 bit float channel
324 */
325 kAlpha_half_GrPixelConfig,
326
jvanverthfb5df432015-05-21 08:12:27 -0700327 /**
328 * Byte order is r, g, b, a. This color format is 16 bits per channel
329 */
330 kRGBA_half_GrPixelConfig,
331
332 kLast_GrPixelConfig = kRGBA_half_GrPixelConfig
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000333};
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +0000334static const int kGrPixelConfigCnt = kLast_GrPixelConfig + 1;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000335
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000336// Aliases for pixel configs that match skia's byte order.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000337#ifndef SK_CPU_LENDIAN
338 #error "Skia gpu currently assumes little endian"
339#endif
commit-bot@chromium.orge4657ed2013-03-19 14:16:31 +0000340#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
bsalomon@google.com0342a852012-08-20 19:22:38 +0000341 static const GrPixelConfig kSkia8888_GrPixelConfig = kBGRA_8888_GrPixelConfig;
commit-bot@chromium.orge4657ed2013-03-19 14:16:31 +0000342#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
bsalomon@google.com0342a852012-08-20 19:22:38 +0000343 static const GrPixelConfig kSkia8888_GrPixelConfig = kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000344#else
345 #error "SK_*32_SHIFT values must correspond to GL_BGRA or GL_RGBA format."
346#endif
347
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000348// Returns true if the pixel config is 32 bits per pixel
Brian Salomonbf7b6202016-11-11 16:08:03 -0500349static inline bool GrPixelConfigIs8888Unorm(GrPixelConfig config) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000350 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000351 case kRGBA_8888_GrPixelConfig:
352 case kBGRA_8888_GrPixelConfig:
jvanverthfa1e8a72014-12-22 08:31:49 -0800353 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700354 case kSBGRA_8888_GrPixelConfig:
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000355 return true;
csmartdalton6aa0e112017-02-08 16:14:11 -0500356 case kUnknown_GrPixelConfig:
357 case kAlpha_8_GrPixelConfig:
358 case kGray_8_GrPixelConfig:
359 case kRGB_565_GrPixelConfig:
360 case kRGBA_4444_GrPixelConfig:
361 case kRGBA_8888_sint_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500362 case kRGBA_float_GrPixelConfig:
363 case kRG_float_GrPixelConfig:
364 case kAlpha_half_GrPixelConfig:
365 case kRGBA_half_GrPixelConfig:
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000366 return false;
367 }
csmartdalton6aa0e112017-02-08 16:14:11 -0500368 SkFAIL("Invalid pixel config");
369 return false;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000370}
371
bsalomon16921ec2015-07-30 15:34:56 -0700372// Returns true if the color (non-alpha) components represent sRGB values. It does NOT indicate that
373// all three color components are present in the config or anything about their order.
374static inline bool GrPixelConfigIsSRGB(GrPixelConfig config) {
375 switch (config) {
376 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700377 case kSBGRA_8888_GrPixelConfig:
bsalomon16921ec2015-07-30 15:34:56 -0700378 return true;
csmartdalton6aa0e112017-02-08 16:14:11 -0500379 case kUnknown_GrPixelConfig:
380 case kAlpha_8_GrPixelConfig:
381 case kGray_8_GrPixelConfig:
382 case kRGB_565_GrPixelConfig:
383 case kRGBA_4444_GrPixelConfig:
384 case kRGBA_8888_GrPixelConfig:
385 case kBGRA_8888_GrPixelConfig:
386 case kRGBA_8888_sint_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500387 case kRGBA_float_GrPixelConfig:
388 case kRG_float_GrPixelConfig:
389 case kAlpha_half_GrPixelConfig:
390 case kRGBA_half_GrPixelConfig:
bsalomon16921ec2015-07-30 15:34:56 -0700391 return false;
392 }
csmartdalton6aa0e112017-02-08 16:14:11 -0500393 SkFAIL("Invalid pixel config");
394 return false;
bsalomon16921ec2015-07-30 15:34:56 -0700395}
396
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000397// Takes a config and returns the equivalent config with the R and B order
398// swapped if such a config exists. Otherwise, kUnknown_GrPixelConfig
399static inline GrPixelConfig GrPixelConfigSwapRAndB(GrPixelConfig config) {
400 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000401 case kBGRA_8888_GrPixelConfig:
402 return kRGBA_8888_GrPixelConfig;
403 case kRGBA_8888_GrPixelConfig:
404 return kBGRA_8888_GrPixelConfig;
brianosmana6359362016-03-21 06:55:37 -0700405 case kSBGRA_8888_GrPixelConfig:
406 return kSRGBA_8888_GrPixelConfig;
407 case kSRGBA_8888_GrPixelConfig:
408 return kSBGRA_8888_GrPixelConfig;
csmartdalton6aa0e112017-02-08 16:14:11 -0500409 case kUnknown_GrPixelConfig:
410 case kAlpha_8_GrPixelConfig:
411 case kGray_8_GrPixelConfig:
412 case kRGB_565_GrPixelConfig:
413 case kRGBA_4444_GrPixelConfig:
414 case kRGBA_8888_sint_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500415 case kRGBA_float_GrPixelConfig:
416 case kRG_float_GrPixelConfig:
417 case kAlpha_half_GrPixelConfig:
418 case kRGBA_half_GrPixelConfig:
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000419 return kUnknown_GrPixelConfig;
420 }
csmartdalton6aa0e112017-02-08 16:14:11 -0500421 SkFAIL("Invalid pixel config");
422 return kUnknown_GrPixelConfig;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000423}
424
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000425static inline size_t GrBytesPerPixel(GrPixelConfig config) {
426 switch (config) {
427 case kAlpha_8_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500428 case kGray_8_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000429 return 1;
430 case kRGB_565_GrPixelConfig:
431 case kRGBA_4444_GrPixelConfig:
jvanverth28f9c602014-12-05 13:06:35 -0800432 case kAlpha_half_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000433 return 2;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000434 case kRGBA_8888_GrPixelConfig:
435 case kBGRA_8888_GrPixelConfig:
jvanverthfa1e8a72014-12-22 08:31:49 -0800436 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700437 case kSBGRA_8888_GrPixelConfig:
Brian Salomonbf7b6202016-11-11 16:08:03 -0500438 case kRGBA_8888_sint_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000439 return 4;
jvanverthfb5df432015-05-21 08:12:27 -0700440 case kRGBA_half_GrPixelConfig:
441 return 8;
joshualittee5da552014-07-16 13:32:56 -0700442 case kRGBA_float_GrPixelConfig:
443 return 16;
csmartdalton6aa0e112017-02-08 16:14:11 -0500444 case kRG_float_GrPixelConfig:
445 return 8;
446 case kUnknown_GrPixelConfig:
joshualittee5da552014-07-16 13:32:56 -0700447 return 0;
448 }
csmartdalton6aa0e112017-02-08 16:14:11 -0500449 SkFAIL("Invalid pixel config");
450 return 0;
joshualittee5da552014-07-16 13:32:56 -0700451}
452
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000453static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
454 switch (config) {
455 case kRGB_565_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500456 case kGray_8_GrPixelConfig:
Brian Salomond17b4a62017-05-23 16:53:47 -0400457 case kRG_float_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +0000458 return true;
csmartdalton6aa0e112017-02-08 16:14:11 -0500459 case kAlpha_8_GrPixelConfig:
460 case kRGBA_4444_GrPixelConfig:
461 case kAlpha_half_GrPixelConfig:
462 case kRGBA_8888_GrPixelConfig:
463 case kBGRA_8888_GrPixelConfig:
464 case kSRGBA_8888_GrPixelConfig:
465 case kSBGRA_8888_GrPixelConfig:
466 case kRGBA_8888_sint_GrPixelConfig:
467 case kRGBA_half_GrPixelConfig:
468 case kRGBA_float_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500469 case kUnknown_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +0000470 return false;
471 }
csmartdalton6aa0e112017-02-08 16:14:11 -0500472 SkFAIL("Invalid pixel config");
473 return false;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000474}
475
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000476static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
477 switch (config) {
478 case kAlpha_8_GrPixelConfig:
jvanverth28f9c602014-12-05 13:06:35 -0800479 case kAlpha_half_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000480 return true;
csmartdalton6aa0e112017-02-08 16:14:11 -0500481 case kUnknown_GrPixelConfig:
482 case kGray_8_GrPixelConfig:
483 case kRGB_565_GrPixelConfig:
484 case kRGBA_4444_GrPixelConfig:
485 case kRGBA_8888_GrPixelConfig:
486 case kBGRA_8888_GrPixelConfig:
487 case kSRGBA_8888_GrPixelConfig:
488 case kSBGRA_8888_GrPixelConfig:
489 case kRGBA_8888_sint_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500490 case kRGBA_float_GrPixelConfig:
491 case kRG_float_GrPixelConfig:
492 case kRGBA_half_GrPixelConfig:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000493 return false;
494 }
csmartdalton6aa0e112017-02-08 16:14:11 -0500495 SkFAIL("Invalid pixel config.");
496 return false;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000497}
498
csmartdaltonc411f2d2016-06-27 09:29:07 -0700499static inline bool GrPixelConfigIsFloatingPoint(GrPixelConfig config) {
500 switch (config) {
501 case kRGBA_float_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500502 case kRG_float_GrPixelConfig:
csmartdaltonc411f2d2016-06-27 09:29:07 -0700503 case kAlpha_half_GrPixelConfig:
504 case kRGBA_half_GrPixelConfig:
505 return true;
csmartdalton6aa0e112017-02-08 16:14:11 -0500506 case kUnknown_GrPixelConfig:
507 case kAlpha_8_GrPixelConfig:
508 case kGray_8_GrPixelConfig:
509 case kRGB_565_GrPixelConfig:
510 case kRGBA_4444_GrPixelConfig:
511 case kRGBA_8888_GrPixelConfig:
512 case kBGRA_8888_GrPixelConfig:
513 case kSRGBA_8888_GrPixelConfig:
514 case kSBGRA_8888_GrPixelConfig:
515 case kRGBA_8888_sint_GrPixelConfig:
csmartdaltonc411f2d2016-06-27 09:29:07 -0700516 return false;
517 }
csmartdalton6aa0e112017-02-08 16:14:11 -0500518 SkFAIL("Invalid pixel config");
519 return false;
csmartdaltonc411f2d2016-06-27 09:29:07 -0700520}
521
Brian Salomonbf7b6202016-11-11 16:08:03 -0500522static inline bool GrPixelConfigIsSint(GrPixelConfig config) {
523 return config == kRGBA_8888_sint_GrPixelConfig;
524}
525
Brian Salomon9bada542017-06-12 12:09:30 -0400526static inline bool GrPixelConfigIsUnorm(GrPixelConfig config) {
527 switch (config) {
528 case kAlpha_8_GrPixelConfig:
529 case kGray_8_GrPixelConfig:
530 case kRGB_565_GrPixelConfig:
531 case kRGBA_4444_GrPixelConfig:
532 case kRGBA_8888_GrPixelConfig:
533 case kBGRA_8888_GrPixelConfig:
534 case kSRGBA_8888_GrPixelConfig:
535 case kSBGRA_8888_GrPixelConfig:
536 return true;
537 case kUnknown_GrPixelConfig:
538 case kAlpha_half_GrPixelConfig:
539 case kRGBA_8888_sint_GrPixelConfig:
540 case kRGBA_float_GrPixelConfig:
541 case kRG_float_GrPixelConfig:
542 case kRGBA_half_GrPixelConfig:
543 return false;
544 }
545 SkFAIL("Invalid pixel config.");
546 return false;
547}
548
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000549/**
bsalomonf2703d82014-10-28 14:33:06 -0700550 * Optional bitfield flags that can be set on GrSurfaceDesc (below).
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000551 */
bsalomonf2703d82014-10-28 14:33:06 -0700552enum GrSurfaceFlags {
Brian Salomond17b4a62017-05-23 16:53:47 -0400553 kNone_GrSurfaceFlags = 0x0,
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000554 /**
555 * Creates a texture that can be rendered to as a GrRenderTarget. Use
556 * GrTexture::asRenderTarget() to access.
557 */
Brian Salomond17b4a62017-05-23 16:53:47 -0400558 kRenderTarget_GrSurfaceFlag = 0x1,
559 /**
560 * Clears to zero on creation. It will cause creation failure if initial data is supplied to the
561 * texture. This only affects the base level if the texture is created with MIP levels.
562 */
563 kPerformInitialClear_GrSurfaceFlag = 0x2
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000564};
565
bsalomonf2703d82014-10-28 14:33:06 -0700566GR_MAKE_BITFIELD_OPS(GrSurfaceFlags)
567
erikchen9a1ed5d2016-02-10 16:32:34 -0800568// opaque type for 3D API object handles
569typedef intptr_t GrBackendObject;
570
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000571/**
572 * Some textures will be stored such that the upper and left edges of the content meet at the
573 * 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 +0000574 * the origin. kDefault_GrSurfaceOrigin sets textures to TopLeft, and render targets
575 * to BottomLeft.
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000576 */
577
578enum GrSurfaceOrigin {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000579 kDefault_GrSurfaceOrigin, // DEPRECATED; to be removed
robertphillips@google.comcf9faf62013-02-05 14:05:06 +0000580 kTopLeft_GrSurfaceOrigin,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000581 kBottomLeft_GrSurfaceOrigin,
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000582};
robertphillips@google.com5091b702012-08-09 10:49:39 +0000583
cblume55f2d2d2016-02-26 13:20:48 -0800584struct GrMipLevel {
585 const void* fPixels;
586 size_t fRowBytes;
587};
588
robertphillips@google.com5091b702012-08-09 10:49:39 +0000589/**
bsalomonf2703d82014-10-28 14:33:06 -0700590 * Describes a surface to be created.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000591 */
bsalomonf2703d82014-10-28 14:33:06 -0700592struct GrSurfaceDesc {
593 GrSurfaceDesc()
594 : fFlags(kNone_GrSurfaceFlags)
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000595 , fOrigin(kDefault_GrSurfaceOrigin)
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000596 , fWidth(0)
597 , fHeight(0)
598 , fConfig(kUnknown_GrPixelConfig)
cblume55f2d2d2016-02-26 13:20:48 -0800599 , fSampleCnt(0)
600 , fIsMipMapped(false) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000601 }
602
bsalomonf2703d82014-10-28 14:33:06 -0700603 GrSurfaceFlags fFlags; //!< bitfield of TextureFlags
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000604 GrSurfaceOrigin fOrigin; //!< origin of the texture
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +0000605 int fWidth; //!< Width of the texture
606 int fHeight; //!< Height of the texture
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000607
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000608 /**
robertphillips@google.com07ef9112012-06-04 13:22:14 +0000609 * Format of source data of the texture. Not guaranteed to be the same as
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000610 * internal format used by 3D API.
611 */
bsalomon@google.com5bc34f02011-12-06 14:46:34 +0000612 GrPixelConfig fConfig;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000613
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000614 /**
615 * The number of samples per pixel or 0 to disable full scene AA. This only
bsalomonf2703d82014-10-28 14:33:06 -0700616 * applies if the kRenderTarget_GrSurfaceFlag is set. The actual number
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000617 * of samples may not exactly match the request. The request will be rounded
618 * up to the next supported sample count, or down if it is larger than the
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000619 * max supported count.
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000620 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000621 int fSampleCnt;
cblume55f2d2d2016-02-26 13:20:48 -0800622 bool fIsMipMapped; //!< Indicates if the texture has mipmaps
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000623};
624
bsalomonf2703d82014-10-28 14:33:06 -0700625// Legacy alias
626typedef GrSurfaceDesc GrTextureDesc;
627
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000628/**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000629 * Clips are composed from these objects.
630 */
631enum GrClipType {
632 kRect_ClipType,
633 kPath_ClipType
634};
635
reed@google.comac10a2d2010-12-22 21:39:39 +0000636///////////////////////////////////////////////////////////////////////////////
637
bsalomon6dc6f5f2015-06-18 09:12:16 -0700638/** Ownership rules for external GPU resources imported into Skia. */
639enum GrWrapOwnership {
640 /** Skia will assume the client will keep the resource alive and Skia will not free it. */
641 kBorrow_GrWrapOwnership,
642
643 /** Skia will assume ownership of the resource and free it. */
644 kAdopt_GrWrapOwnership,
645};
646
bsalomon@google.come269f212011-11-07 13:29:52 +0000647///////////////////////////////////////////////////////////////////////////////
648
649/**
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000650 * The GrContext's cache of backend context state can be partially invalidated.
651 * These enums are specific to the GL backend and we'd add a new set for an alternative backend.
652 */
653enum GrGLBackendState {
654 kRenderTarget_GrGLBackendState = 1 << 0,
655 kTextureBinding_GrGLBackendState = 1 << 1,
656 // View state stands for scissor and viewport
657 kView_GrGLBackendState = 1 << 2,
658 kBlend_GrGLBackendState = 1 << 3,
egdanielb414f252014-07-29 13:15:47 -0700659 kMSAAEnable_GrGLBackendState = 1 << 4,
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000660 kVertex_GrGLBackendState = 1 << 5,
661 kStencil_GrGLBackendState = 1 << 6,
662 kPixelStore_GrGLBackendState = 1 << 7,
663 kProgram_GrGLBackendState = 1 << 8,
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000664 kFixedFunction_GrGLBackendState = 1 << 9,
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000665 kMisc_GrGLBackendState = 1 << 10,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000666 kPathRendering_GrGLBackendState = 1 << 11,
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000667 kALL_GrGLBackendState = 0xffff
668};
669
670/**
671 * This value translates to reseting all the context state for any backend.
672 */
673static const uint32_t kAll_GrBackendState = 0xffffffff;
674
reed@google.comac10a2d2010-12-22 21:39:39 +0000675#endif