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