xref: /aosp_15_r20/external/skia/include/gpu/ganesh/GrTypes.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/SkTypes.h"
12 #include "include/gpu/GpuTypes.h"
13 #include "include/private/base/SkTo.h" // IWYU pragma: keep
14 
15 #include <cstddef>
16 #include <cstdint>
17 class GrBackendSemaphore;
18 
19 ///////////////////////////////////////////////////////////////////////////////
20 
21 /**
22  * Possible 3D APIs that may be used by Ganesh.
23  */
24 enum class GrBackendApi : unsigned {
25     kOpenGL,
26     kVulkan,
27     kMetal,
28     kDirect3D,
29 
30     /**
31      * Mock is a backend that does not draw anything. It is used for unit tests
32      * and to measure CPU overhead.
33      */
34     kMock,
35 
36     /**
37      * Ganesh doesn't support some context types (e.g. Dawn) and will return Unsupported.
38      */
39     kUnsupported,
40 
41     /**
42      * Added here to support the legacy GrBackend enum value and clients who referenced it using
43      * GrBackend::kOpenGL_GrBackend.
44      */
45     kOpenGL_GrBackend = kOpenGL,
46 };
47 
48 /**
49  * Previously the above enum was not an enum class but a normal enum. To support the legacy use of
50  * the enum values we define them below so that no clients break.
51  */
52 typedef GrBackendApi GrBackend;
53 
54 static constexpr GrBackendApi kMetal_GrBackend = GrBackendApi::kMetal;
55 static constexpr GrBackendApi kVulkan_GrBackend = GrBackendApi::kVulkan;
56 static constexpr GrBackendApi kMock_GrBackend = GrBackendApi::kMock;
57 
58 ///////////////////////////////////////////////////////////////////////////////
59 
60 /*
61  * Can a GrBackendObject be rendered to?
62  */
63 using GrRenderable = skgpu::Renderable;
64 
65 /*
66  * Used to say whether texture is backed by protected memory.
67  */
68 using GrProtected = skgpu::Protected;
69 
70 ///////////////////////////////////////////////////////////////////////////////
71 
72 /**
73  * GPU SkImage and SkSurfaces can be stored such that (0, 0) in texture space may correspond to
74  * either the top-left or bottom-left content pixel.
75  */
76 enum GrSurfaceOrigin : int {
77     kTopLeft_GrSurfaceOrigin,
78     kBottomLeft_GrSurfaceOrigin,
79 };
80 
81 /**
82  * A GrContext's cache of backend context state can be partially invalidated.
83  * These enums are specific to the GL backend and we'd add a new set for an alternative backend.
84  */
85 enum GrGLBackendState {
86     kRenderTarget_GrGLBackendState     = 1 << 0,
87     // Also includes samplers bound to texture units.
88     kTextureBinding_GrGLBackendState   = 1 << 1,
89     // View state stands for scissor and viewport
90     kView_GrGLBackendState             = 1 << 2,
91     kBlend_GrGLBackendState            = 1 << 3,
92     kMSAAEnable_GrGLBackendState       = 1 << 4,
93     kVertex_GrGLBackendState           = 1 << 5,
94     kStencil_GrGLBackendState          = 1 << 6,
95     kPixelStore_GrGLBackendState       = 1 << 7,
96     kProgram_GrGLBackendState          = 1 << 8,
97     kFixedFunction_GrGLBackendState    = 1 << 9,
98     kMisc_GrGLBackendState             = 1 << 10,
99     kALL_GrGLBackendState              = 0xffff
100 };
101 
102 /**
103  * This value translates to reseting all the context state for any backend.
104  */
105 static const uint32_t kAll_GrBackendState = 0xffffffff;
106 
107 typedef void* GrGpuFinishedContext;
108 typedef void (*GrGpuFinishedProc)(GrGpuFinishedContext finishedContext);
109 typedef void (*GrGpuFinishedWithStatsProc)(GrGpuFinishedContext finishedContext,
110                                            const skgpu::GpuStats&);
111 
112 typedef void* GrGpuSubmittedContext;
113 typedef void (*GrGpuSubmittedProc)(GrGpuSubmittedContext submittedContext, bool success);
114 
115 typedef void* GrDirectContextDestroyedContext;
116 typedef void (*GrDirectContextDestroyedProc)(GrDirectContextDestroyedContext destroyedContext);
117 
118 /**
119  * Struct to supply options to flush calls.
120  *
121  * After issuing all commands, fNumSemaphore semaphores will be signaled by the gpu. The client
122  * passes in an array of fNumSemaphores GrBackendSemaphores. In general these GrBackendSemaphore's
123  * can be either initialized or not. If they are initialized, the backend uses the passed in
124  * semaphore. If it is not initialized, a new semaphore is created and the GrBackendSemaphore
125  * object is initialized with that semaphore. The semaphores are not sent to the GPU until the next
126  * GrContext::submit call is made. See the GrContext::submit for more information.
127  *
128  * The client will own and be responsible for deleting the underlying semaphores that are stored
129  * and returned in initialized GrBackendSemaphore objects. The GrBackendSemaphore objects
130  * themselves can be deleted as soon as this function returns.
131  *
132  * If a finishedProc or finishedWithStatsProc is provided, the proc will be called when all work
133  * submitted to the gpu from this flush call and all previous flush calls has finished on the GPU.
134  * If the flush call fails due to an error and nothing ends up getting sent to the GPU, the finished
135  * proc is called immediately. If both types of proc are provided then finishedWithStatsProc is
136  * preferred.
137  *
138  * When finishedWithStatsProc is called the GpuStats passed will contain valid values for stats
139  * by requested by gpuStatsFlags, assuming the stats are supported by the underlying backend GPU
140  * context and the GPU work completed successfully.
141  *
142  * If a submittedProc is provided, the submittedProc will be called when all work from this flush
143  * call is submitted to the GPU. If the flush call fails due to an error and nothing will get sent
144  * to the GPU, the submitted proc is called immediately. It is possibly that when work is finally
145  * submitted, that the submission actual fails. In this case we will not reattempt to do the
146  * submission. Skia notifies the client of these via the success bool passed into the submittedProc.
147  * The submittedProc is useful to the client to know when semaphores that were sent with the flush
148  * have actually been submitted to the GPU so that they can be waited on (or deleted if the submit
149  * fails).
150  * GrBackendSemaphores are not supported for the GL backend and will be ignored if set.
151  */
152 struct GrFlushInfo {
153     size_t fNumSemaphores = 0;
154     skgpu::GpuStatsFlags fGpuStatsFlags = skgpu::GpuStatsFlags::kNone;
155     GrBackendSemaphore* fSignalSemaphores = nullptr;
156     GrGpuFinishedProc fFinishedProc = nullptr;
157     GrGpuFinishedWithStatsProc fFinishedWithStatsProc = nullptr;
158     GrGpuFinishedContext fFinishedContext = nullptr;
159     GrGpuSubmittedProc fSubmittedProc = nullptr;
160     GrGpuSubmittedContext fSubmittedContext = nullptr;
161 };
162 
163 /**
164  * Enum used as return value when flush with semaphores so the client knows whether the valid
165  * semaphores will be submitted on the next GrContext::submit call.
166  */
167 enum class GrSemaphoresSubmitted : bool {
168     kNo = false,
169     kYes = true
170 };
171 
172 enum class GrPurgeResourceOptions {
173     kAllResources,
174     kScratchResourcesOnly,
175 };
176 
177 enum class GrSyncCpu : bool {
178     kNo = false,
179     kYes = true,
180 };
181 
182 enum class GrMarkFrameBoundary : bool {
183     kNo = false,
184     kYes = true,
185 };
186 
187 struct GrSubmitInfo {
188     GrSyncCpu fSync = GrSyncCpu::kNo;
189     GrMarkFrameBoundary fMarkBoundary = GrMarkFrameBoundary::kNo;
190     uint64_t fFrameID = 0;
191 };
192 
193 #endif
194