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 `dstCopyBounds` 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 dstCopyBounds, 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. setReplayTranslationAndClip(const SkIVector & translation,const SkIRect & clip)128 void setReplayTranslationAndClip(const SkIVector& translation, const SkIRect& clip) { 129 fReplayTranslation = translation; 130 fReplayClip = clip.makeOffset(translation); 131 } 132 isProtected()133 Protected isProtected() const { return fIsProtected; } 134 135 protected: 136 CommandBuffer(Protected); 137 138 SkISize fColorAttachmentSize; 139 // This is also the origin of the logical viewport relative to the target texture's (0,0) pixel 140 SkIVector fReplayTranslation; 141 // This is in target texture space, having been transformed by the replay translation. 142 SkIRect fReplayClip; 143 144 // The texture to use for implementing DstReadRequirement::kTextureCopy for the current render 145 // pass. This is a bare pointer since the CopyTask that initializes the texture's contents 146 // will have tracked the resource on the CommandBuffer already. 147 std::pair<const Texture*, const Sampler*> fDstCopy; 148 // Already includes replay translation and respects final color attachment bounds, but with 149 // dimensions that equal fDstCopy's width and height. 150 SkIRect fDstCopyBounds; 151 152 Protected fIsProtected; 153 154 private: 155 // Release all tracked Resources 156 void releaseResources(); 157 158 // Subclasses will hold their backend-specific ResourceProvider directly to avoid virtual calls 159 // and access backend-specific behavior, but they can reflect it back to the base CommandBuffer 160 // if it needs to make generic resources. 161 virtual ResourceProvider* resourceProvider() const = 0; 162 163 virtual void onResetCommandBuffer() = 0; 164 165 // Renderpass, viewport bounds have already been adjusted by the replay translation. The render 166 // pass bounds has been intersected with the color attachment bounds. 167 virtual bool onAddRenderPass(const RenderPassDesc&, 168 SkIRect renderPassBounds, 169 const Texture* colorTexture, 170 const Texture* resolveTexture, 171 const Texture* depthStencilTexture, 172 SkIRect viewport, 173 const DrawPassList& drawPasses) = 0; 174 175 virtual bool onAddComputePass(DispatchGroupSpan dispatchGroups) = 0; 176 177 virtual bool onCopyBufferToBuffer(const Buffer* srcBuffer, 178 size_t srcOffset, 179 const Buffer* dstBuffer, 180 size_t dstOffset, 181 size_t size) = 0; 182 virtual bool onCopyTextureToBuffer(const Texture*, 183 SkIRect srcRect, 184 const Buffer*, 185 size_t bufferOffset, 186 size_t bufferRowBytes) = 0; 187 virtual bool onCopyBufferToTexture(const Buffer*, 188 const Texture*, 189 const BufferTextureCopyData*, 190 int count) = 0; 191 virtual bool onCopyTextureToTexture(const Texture* src, 192 SkIRect srcRect, 193 const Texture* dst, 194 SkIPoint dstPoint, 195 int mipLevel) = 0; 196 virtual bool onSynchronizeBufferToCpu(const Buffer*, bool* outDidResultInWork) = 0; 197 virtual bool onClearBuffer(const Buffer*, size_t offset, size_t size) = 0; 198 199 #ifdef SK_DEBUG 200 bool fHasWork = false; 201 #endif 202 inline static constexpr int kInitialTrackedResourcesCount = 32; 203 template <typename T> 204 using TrackedResourceArray = skia_private::STArray<kInitialTrackedResourcesCount, T>; 205 TrackedResourceArray<sk_sp<Resource>> fTrackedUsageResources; 206 TrackedResourceArray<gr_cb<Resource>> fCommandBufferResources; 207 skia_private::TArray<sk_sp<RefCntedCallback>> fFinishedProcs; 208 skia_private::TArray<sk_sp<Buffer>> fBuffersToAsyncMap; 209 }; 210 211 } // namespace skgpu::graphite 212 213 #endif // skgpu_graphite_CommandBuffer_DEFINED 214