• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * 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.
6  */
7 
8 #ifndef GrTypes_DEFINED
9 #define GrTypes_DEFINED
10 
11 #include "include/core/SkMath.h"
12 #include "include/core/SkTypes.h"
13 #include "include/gpu/GrConfig.h"
14 
15 class GrBackendSemaphore;
16 class SkImage;
17 class SkSurface;
18 
19 ////////////////////////////////////////////////////////////////////////////////
20 
21 /**
22  * Wraps a C++11 enum that we use as a bitfield, and enables a limited amount of
23  * masking with type safety. Instantiated with the ~ operator.
24  */
25 template<typename TFlags> class GrTFlagsMask {
26 public:
GrTFlagsMask(TFlags value)27     constexpr explicit GrTFlagsMask(TFlags value) : GrTFlagsMask(static_cast<int>(value)) {}
GrTFlagsMask(int value)28     constexpr explicit GrTFlagsMask(int value) : fValue(value) {}
value()29     constexpr int value() const { return fValue; }
30 private:
31     const int fValue;
32 };
33 
34 /**
35  * Defines bitwise operators that make it possible to use an enum class as a
36  * basic bitfield.
37  */
38 #define GR_MAKE_BITFIELD_CLASS_OPS(X) \
39     SK_MAYBE_UNUSED constexpr GrTFlagsMask<X> operator~(X a) { \
40         return GrTFlagsMask<X>(~static_cast<int>(a)); \
41     } \
42     SK_MAYBE_UNUSED constexpr X operator|(X a, X b) { \
43         return static_cast<X>(static_cast<int>(a) | static_cast<int>(b)); \
44     } \
45     SK_MAYBE_UNUSED inline X& operator|=(X& a, X b) { \
46         return (a = a | b); \
47     } \
48     SK_MAYBE_UNUSED constexpr bool operator&(X a, X b) { \
49         return SkToBool(static_cast<int>(a) & static_cast<int>(b)); \
50     } \
51     SK_MAYBE_UNUSED constexpr GrTFlagsMask<X> operator|(GrTFlagsMask<X> a, GrTFlagsMask<X> b) { \
52         return GrTFlagsMask<X>(a.value() | b.value()); \
53     } \
54     SK_MAYBE_UNUSED constexpr GrTFlagsMask<X> operator|(GrTFlagsMask<X> a, X b) { \
55         return GrTFlagsMask<X>(a.value() | static_cast<int>(b)); \
56     } \
57     SK_MAYBE_UNUSED constexpr GrTFlagsMask<X> operator|(X a, GrTFlagsMask<X> b) { \
58         return GrTFlagsMask<X>(static_cast<int>(a) | b.value()); \
59     } \
60     SK_MAYBE_UNUSED constexpr X operator&(GrTFlagsMask<X> a, GrTFlagsMask<X> b) { \
61         return static_cast<X>(a.value() & b.value()); \
62     } \
63     SK_MAYBE_UNUSED constexpr X operator&(GrTFlagsMask<X> a, X b) { \
64         return static_cast<X>(a.value() & static_cast<int>(b)); \
65     } \
66     SK_MAYBE_UNUSED constexpr X operator&(X a, GrTFlagsMask<X> b) { \
67         return static_cast<X>(static_cast<int>(a) & b.value()); \
68     } \
69     SK_MAYBE_UNUSED inline X& operator&=(X& a, GrTFlagsMask<X> b) { \
70         return (a = a & b); \
71     } \
72 
73 #define GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(X) \
74     friend constexpr GrTFlagsMask<X> operator ~(X); \
75     friend constexpr X operator |(X, X); \
76     friend X& operator |=(X&, X); \
77     friend constexpr bool operator &(X, X); \
78     friend constexpr GrTFlagsMask<X> operator|(GrTFlagsMask<X>, GrTFlagsMask<X>); \
79     friend constexpr GrTFlagsMask<X> operator|(GrTFlagsMask<X>, X); \
80     friend constexpr GrTFlagsMask<X> operator|(X, GrTFlagsMask<X>); \
81     friend constexpr X operator&(GrTFlagsMask<X>, GrTFlagsMask<X>); \
82     friend constexpr X operator&(GrTFlagsMask<X>, X); \
83     friend constexpr X operator&(X, GrTFlagsMask<X>); \
84     friend X& operator &=(X&, GrTFlagsMask<X>)
85 
86 ///////////////////////////////////////////////////////////////////////////////
87 
88 /**
89  * Possible 3D APIs that may be used by Ganesh.
90  */
91 enum class GrBackendApi : unsigned {
92     kOpenGL,
93     kVulkan,
94     kMetal,
95     kDirect3D,
96     kDawn,
97     /**
98      * Mock is a backend that does not draw anything. It is used for unit tests
99      * and to measure CPU overhead.
100      */
101     kMock,
102 
103     /**
104      * Added here to support the legacy GrBackend enum value and clients who referenced it using
105      * GrBackend::kOpenGL_GrBackend.
106      */
107     kOpenGL_GrBackend = kOpenGL,
108 };
109 
110 /**
111  * Previously the above enum was not an enum class but a normal enum. To support the legacy use of
112  * the enum values we define them below so that no clients break.
113  */
114 typedef GrBackendApi GrBackend;
115 
116 static constexpr GrBackendApi kMetal_GrBackend = GrBackendApi::kMetal;
117 static constexpr GrBackendApi kVulkan_GrBackend = GrBackendApi::kVulkan;
118 static constexpr GrBackendApi kMock_GrBackend = GrBackendApi::kMock;
119 
120 ///////////////////////////////////////////////////////////////////////////////
121 
122 /**
123  * Used to say whether a texture has mip levels allocated or not.
124  */
125 enum class GrMipmapped : bool {
126     kNo = false,
127     kYes = true
128 };
129 /** Deprecated legacy alias of GrMipmapped. */
130 using GrMipMapped = GrMipmapped;
131 
132 /*
133  * Can a GrBackendObject be rendered to?
134  */
135 enum class GrRenderable : bool {
136     kNo = false,
137     kYes = true
138 };
139 
140 /*
141  * Used to say whether texture is backed by protected memory.
142  */
143 enum class GrProtected : bool {
144     kNo = false,
145     kYes = true
146 };
147 
148 ///////////////////////////////////////////////////////////////////////////////
149 
150 /**
151  * GPU SkImage and SkSurfaces can be stored such that (0, 0) in texture space may correspond to
152  * either the top-left or bottom-left content pixel.
153  */
154 enum GrSurfaceOrigin : int {
155     kTopLeft_GrSurfaceOrigin,
156     kBottomLeft_GrSurfaceOrigin,
157 };
158 
159 /**
160  * A GrContext's cache of backend context state can be partially invalidated.
161  * These enums are specific to the GL backend and we'd add a new set for an alternative backend.
162  */
163 enum GrGLBackendState {
164     kRenderTarget_GrGLBackendState     = 1 << 0,
165     // Also includes samplers bound to texture units.
166     kTextureBinding_GrGLBackendState   = 1 << 1,
167     // View state stands for scissor and viewport
168     kView_GrGLBackendState             = 1 << 2,
169     kBlend_GrGLBackendState            = 1 << 3,
170     kMSAAEnable_GrGLBackendState       = 1 << 4,
171     kVertex_GrGLBackendState           = 1 << 5,
172     kStencil_GrGLBackendState          = 1 << 6,
173     kPixelStore_GrGLBackendState       = 1 << 7,
174     kProgram_GrGLBackendState          = 1 << 8,
175     kFixedFunction_GrGLBackendState    = 1 << 9,
176     kMisc_GrGLBackendState             = 1 << 10,
177     kALL_GrGLBackendState              = 0xffff
178 };
179 
180 /**
181  * This value translates to reseting all the context state for any backend.
182  */
183 static const uint32_t kAll_GrBackendState = 0xffffffff;
184 
185 typedef void* GrGpuFinishedContext;
186 typedef void (*GrGpuFinishedProc)(GrGpuFinishedContext finishedContext);
187 
188 typedef void* GrGpuSubmittedContext;
189 typedef void (*GrGpuSubmittedProc)(GrGpuSubmittedContext submittedContext, bool success);
190 
191 /**
192  * Struct to supply options to flush calls.
193  *
194  * After issuing all commands, fNumSemaphore semaphores will be signaled by the gpu. The client
195  * passes in an array of fNumSemaphores GrBackendSemaphores. In general these GrBackendSemaphore's
196  * can be either initialized or not. If they are initialized, the backend uses the passed in
197  * semaphore. If it is not initialized, a new semaphore is created and the GrBackendSemaphore
198  * object is initialized with that semaphore. The semaphores are not sent to the GPU until the next
199  * GrContext::submit call is made. See the GrContext::submit for more information.
200  *
201  * The client will own and be responsible for deleting the underlying semaphores that are stored
202  * and returned in initialized GrBackendSemaphore objects. The GrBackendSemaphore objects
203  * themselves can be deleted as soon as this function returns.
204  *
205  * If a finishedProc is provided, the finishedProc will be called when all work submitted to the gpu
206  * from this flush call and all previous flush calls has finished on the GPU. If the flush call
207  * fails due to an error and nothing ends up getting sent to the GPU, the finished proc is called
208  * immediately.
209  *
210  * If a submittedProc is provided, the submittedProc will be called when all work from this flush
211  * call is submitted to the GPU. If the flush call fails due to an error and nothing will get sent
212  * to the GPU, the submitted proc is called immediately. It is possibly that when work is finally
213  * submitted, that the submission actual fails. In this case we will not reattempt to do the
214  * submission. Skia notifies the client of these via the success bool passed into the submittedProc.
215  * The submittedProc is useful to the client to know when semaphores that were sent with the flush
216  * have actually been submitted to the GPU so that they can be waited on (or deleted if the submit
217  * fails).
218  * Note about GL: In GL work gets sent to the driver immediately during the flush call, but we don't
219  * really know when the driver sends the work to the GPU. Therefore, we treat the submitted proc as
220  * we do in other backends. It will be called when the next GrContext::submit is called after the
221  * flush (or possibly during the flush if there is no work to be done for the flush). The main use
222  * case for the submittedProc is to know when semaphores have been sent to the GPU and even in GL
223  * it is required to call GrContext::submit to flush them. So a client should be able to treat all
224  * backend APIs the same in terms of how the submitted procs are treated.
225  */
226 struct GrFlushInfo {
227     size_t fNumSemaphores = 0;
228     GrBackendSemaphore* fSignalSemaphores = nullptr;
229     GrGpuFinishedProc fFinishedProc = nullptr;
230     GrGpuFinishedContext fFinishedContext = nullptr;
231     GrGpuSubmittedProc fSubmittedProc = nullptr;
232     GrGpuSubmittedContext fSubmittedContext = nullptr;
233 };
234 
235 /**
236  * Enum used as return value when flush with semaphores so the client knows whether the valid
237  * semaphores will be submitted on the next GrContext::submit call.
238  */
239 enum class GrSemaphoresSubmitted : bool {
240     kNo = false,
241     kYes = true
242 };
243 
244 #endif
245