Debian Codegraph @

src/gpu/graphite/CommandBuffer.h

raw
1 /*
2 * Copyright 2021 Google LLC
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 #ifndef skgpu_graphite_CommandBuffer_DEFINED
8 #define skgpu_graphite_CommandBuffer_DEFINED
9
10 #include "include/core/SkPoint.h"
11 #include "include/core/SkRect.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkSpan.h"
14 #include "include/core/SkTypes.h"
15 #include "include/gpu/GpuTypes.h"
16 #include "include/private/base/SkTArray.h"
17 #include "src/gpu/GpuRefCnt.h"
18 #include "src/gpu/graphite/Resource.h" // IWYU pragma: keep
19
20 #include <cstddef>
21 #include <memory>
22 #include <optional>
23 #include <utility>
24
25 class SkSurface;
26 struct SkISize;
27
28 namespace skgpu {
29 class MutableTextureState;
30 class RefCntedCallback;
31 }
32
33 namespace skgpu::graphite {
34
35 class BackendSemaphore;
36 class Buffer;
37 class DispatchGroup;
38 class DrawPass;
39 class ResourceProvider;
40 class Sampler;
41 class Texture;
42 struct BufferTextureCopyData;
43 struct RenderPassDesc;
44
45 class CommandBuffer {
46 public:
47 using DrawPassList = skia_private::TArray<std::unique_ptr<DrawPass>>;
48 using DispatchGroupSpan = SkSpan<const std::unique_ptr<DispatchGroup>>;
49
50 virtual ~CommandBuffer();
51
52 #ifdef SK_DEBUG
53 bool hasWork() { return fHasWork; }
54 #endif
55
56 // Takes a Usage ref on the Resource that will be released when the command buffer has finished
57 // execution.
58 void trackResource(sk_sp<Resource> resource);
59 // Takes a CommandBuffer ref on the Resource that will be released when the command buffer has
60 // finished execution. This allows a Resource to be returned to ResourceCache for reuse while
61 // the CommandBuffer is still executing on the GPU. This is most commonly used for Textures or
62 // Buffers which are only accessed via commands on a command buffer.
63 void trackCommandBufferResource(sk_sp<Resource> resource);
64 // Release all tracked Resources
65 void resetCommandBuffer();
66
67 // If any work is needed to create new resources for a fresh command buffer do that here.
68 virtual bool setNewCommandBufferResources() = 0;
69
70 virtual bool startTimerQuery() { SK_ABORT("Timer query unsupported."); }
71 virtual void endTimerQuery() { SK_ABORT("Timer query unsupported."); }
72 virtual std::optional<GpuStats> gpuStats() { return {}; }
73
74 void addFinishedProc(sk_sp<RefCntedCallback> finishedProc);
75 void callFinishedProcs(bool success);
76
77 virtual void addWaitSemaphores(size_t numWaitSemaphores,
78 const BackendSemaphore* waitSemaphores) {}
79 virtual void addSignalSemaphores(size_t numWaitSemaphores,
80 const BackendSemaphore* signalSemaphores) {}
81 virtual void prepareSurfaceForStateUpdate(SkSurface* targetSurface,
82 const MutableTextureState* newState) {}
83
84 void addBuffersToAsyncMapOnSubmit(SkSpan<const sk_sp<Buffer>>);
85 SkSpan<const sk_sp<Buffer>> buffersToAsyncMapOnSubmit() const;
86
87 // If any recorded draw requires a dst texture copy for blending, that texture must be provided
88 // in `dstCopy`; otherwise it should be null. The `dstReadBounds` are in the same coordinate
89 // space of the logical viewport *before* any replay translation is applied.
90 //
91 // The logical viewport is always (0,0,viewportDims) and matches the "device" coordinate space
92 // of the higher-level SkDevices that recorded the rendering operations. The actual viewport
93 // is automatically adjusted by the replay translation.
94 //
95 // If the RenderPassTask allocates a smaller color texture than the resolve texture, it can pass
96 // a non-zero `resolveOffset` which is the the offset for resolving:
97 // - The color texture's (0, 0, w, h) region.
98 // - And store in the resolve texture's (resolveOffset.x, resolveOffset.y, w, h) region.
99 bool addRenderPass(const RenderPassDesc&,
100 sk_sp<Texture> colorTexture,
101 sk_sp<Texture> resolveTexture,
102 sk_sp<Texture> depthStencilTexture,
103 const Texture* dstCopy,
104 SkIRect dstReadBounds,
105 SkIPoint resolveOffset,
106 SkISize viewportDims,
107 const DrawPassList& drawPasses);
108
109 bool addComputePass(DispatchGroupSpan dispatchGroups);
110
111 //---------------------------------------------------------------
112 // Can only be used outside renderpasses
113 //---------------------------------------------------------------
114 bool copyBufferToBuffer(const Buffer* srcBuffer,
115 size_t srcOffset,
116 sk_sp<Buffer> dstBuffer,
117 size_t dstOffset,
118 size_t size);
119 bool copyTextureToBuffer(sk_sp<Texture>,
120 SkIRect srcRect,
121 sk_sp<Buffer>,
122 size_t bufferOffset,
123 size_t bufferRowBytes);
124 bool copyBufferToTexture(const Buffer*,
125 sk_sp<Texture>,
126 const BufferTextureCopyData*,
127 int count);
128 bool copyTextureToTexture(sk_sp<Texture> src,
129 SkIRect srcRect,
130 sk_sp<Texture> dst,
131 SkIPoint dstPoint,
132 int mipLevel);
133 bool synchronizeBufferToCpu(sk_sp<Buffer>);
134 bool clearBuffer(const Buffer* buffer, size_t offset, size_t size);
135
136 // This sets a translation and clip to be applied to any subsequently added command, assuming
137 // these commands are part of a transformed replay of a Graphite recording. Returns whether the
138 // clip and render target bounds have an intersection; if not, no draws need be replayed.
139 bool setReplayTranslationAndClip(const SkIVector& translation,
140 const SkIRect& clip,
141 const SkIRect& renderTargetBounds);
142
143 Protected isProtected() const { return fIsProtected; }
144
145 protected:
146 CommandBuffer(Protected);
147
148 // These are the color attachment bounds, intersected with any clip provided on replay.
149 SkIRect fRenderPassBounds;
150 // This is also the origin of the logical viewport relative to the target texture's (0,0) pixel.
151 SkIVector fReplayTranslation;
152
153 // The texture to use for implementing DstReadStrategy::kTextureCopy for the current render
154 // pass. This is a bare pointer since the CopyTask that initializes the texture's contents
155 // will have tracked the resource on the CommandBuffer already.
156 std::pair<const Texture*, const Sampler*> fDstCopy;
157 // Already includes replay translation and respects final color attachment bounds, but with
158 // dimensions that equal fDstCopy's width and height.
159 SkIRect fDstReadBounds;
160
161 Protected fIsProtected;
162
163 private:
164 // Release all tracked Resources
165 void releaseResources();
166
167 // Subclasses will hold their backend-specific ResourceProvider directly to avoid virtual calls
168 // and access backend-specific behavior, but they can reflect it back to the base CommandBuffer
169 // if it needs to make generic resources.
170 virtual ResourceProvider* resourceProvider() const = 0;
171
172 virtual void onResetCommandBuffer() = 0;
173
174 // Renderpass, viewport bounds have already been adjusted by the replay translation. The render
175 // pass bounds has been intersected with the color attachment bounds.
176 virtual bool onAddRenderPass(const RenderPassDesc&,
177 SkIRect renderPassBounds,
178 const Texture* colorTexture,
179 const Texture* resolveTexture,
180 const Texture* depthStencilTexture,
181 SkIPoint resolveOffset,
182 SkIRect viewport,
183 const DrawPassList& drawPasses) = 0;
184
185 virtual bool onAddComputePass(DispatchGroupSpan dispatchGroups) = 0;
186
187 virtual bool onCopyBufferToBuffer(const Buffer* srcBuffer,
188 size_t srcOffset,
189 const Buffer* dstBuffer,
190 size_t dstOffset,
191 size_t size) = 0;
192 virtual bool onCopyTextureToBuffer(const Texture*,
193 SkIRect srcRect,
194 const Buffer*,
195 size_t bufferOffset,
196 size_t bufferRowBytes) = 0;
197 virtual bool onCopyBufferToTexture(const Buffer*,
198 const Texture*,
199 const BufferTextureCopyData*,
200 int count) = 0;
201 virtual bool onCopyTextureToTexture(const Texture* src,
202 SkIRect srcRect,
203 const Texture* dst,
204 SkIPoint dstPoint,
205 int mipLevel) = 0;
206 virtual bool onSynchronizeBufferToCpu(const Buffer*, bool* outDidResultInWork) = 0;
207 virtual bool onClearBuffer(const Buffer*, size_t offset, size_t size) = 0;
208
209 #ifdef SK_DEBUG
210 bool fHasWork = false;
211 #endif
212 inline static constexpr int kInitialTrackedResourcesCount = 32;
213 template <typename T>
214 using TrackedResourceArray = skia_private::STArray<kInitialTrackedResourcesCount, T>;
215 TrackedResourceArray<sk_sp<Resource>> fTrackedUsageResources;
216 TrackedResourceArray<gr_cb<Resource>> fCommandBufferResources;
217 skia_private::TArray<sk_sp<RefCntedCallback>> fFinishedProcs;
218 skia_private::TArray<sk_sp<Buffer>> fBuffersToAsyncMap;
219 };
220
221 } // namespace skgpu::graphite
222
223 #endif // skgpu_graphite_CommandBuffer_DEFINED