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 namespace skgpu { 22 class RefCntedCallback; 23 class MutableTextureState; 24 } 25 26 namespace skgpu::graphite { 27 28 class Buffer; 29 class DispatchGroup; 30 class DrawPass; 31 class SharedContext; 32 class GraphicsPipeline; 33 struct RenderPassDesc; 34 class Sampler; 35 class Texture; 36 class TextureProxy; 37 38 class CommandBuffer { 39 public: 40 using DrawPassList = skia_private::TArray<std::unique_ptr<DrawPass>>; 41 using DispatchGroupSpan = SkSpan<const std::unique_ptr<DispatchGroup>>; 42 43 virtual ~CommandBuffer(); 44 45 #ifdef SK_DEBUG hasWork()46 bool hasWork() { return fHasWork; } 47 #endif 48 49 // Takes a Usage ref on the Resource that will be released when the command buffer has finished 50 // execution. 51 void trackResource(sk_sp<Resource> resource); 52 // Takes a CommandBuffer ref on the Resource that will be released when the command buffer has 53 // finished execution. This allows a Resource to be returned to ResourceCache for reuse while 54 // the CommandBuffer is still executing on the GPU. This is most commonly used for Textures or 55 // Buffers which are only accessed via commands on a command buffer. 56 void trackCommandBufferResource(sk_sp<Resource> resource); 57 // Release all tracked Resources 58 void resetCommandBuffer(); 59 60 // If any work is needed to create new resources for a fresh command buffer do that here. 61 virtual bool setNewCommandBufferResources() = 0; 62 63 void addFinishedProc(sk_sp<RefCntedCallback> finishedProc); 64 void callFinishedProcs(bool success); 65 addWaitSemaphores(size_t numWaitSemaphores,const BackendSemaphore * waitSemaphores)66 virtual void addWaitSemaphores(size_t numWaitSemaphores, 67 const BackendSemaphore* waitSemaphores) {} addSignalSemaphores(size_t numWaitSemaphores,const BackendSemaphore * signalSemaphores)68 virtual void addSignalSemaphores(size_t numWaitSemaphores, 69 const BackendSemaphore* signalSemaphores) {} prepareSurfaceForStateUpdate(SkSurface * targetSurface,const MutableTextureState * newState)70 virtual void prepareSurfaceForStateUpdate(SkSurface* targetSurface, 71 const MutableTextureState* newState) {} 72 73 void addBuffersToAsyncMapOnSubmit(SkSpan<const sk_sp<Buffer>>); 74 SkSpan<const sk_sp<Buffer>> buffersToAsyncMapOnSubmit() const; 75 76 bool addRenderPass(const RenderPassDesc&, 77 sk_sp<Texture> colorTexture, 78 sk_sp<Texture> resolveTexture, 79 sk_sp<Texture> depthStencilTexture, 80 SkRect viewport, 81 const DrawPassList& drawPasses); 82 83 bool addComputePass(DispatchGroupSpan dispatchGroups); 84 85 //--------------------------------------------------------------- 86 // Can only be used outside renderpasses 87 //--------------------------------------------------------------- 88 bool copyBufferToBuffer(const Buffer* srcBuffer, 89 size_t srcOffset, 90 sk_sp<Buffer> dstBuffer, 91 size_t dstOffset, 92 size_t size); 93 bool copyTextureToBuffer(sk_sp<Texture>, 94 SkIRect srcRect, 95 sk_sp<Buffer>, 96 size_t bufferOffset, 97 size_t bufferRowBytes); 98 bool copyBufferToTexture(const Buffer*, 99 sk_sp<Texture>, 100 const BufferTextureCopyData*, 101 int count); 102 bool copyTextureToTexture(sk_sp<Texture> src, 103 SkIRect srcRect, 104 sk_sp<Texture> dst, 105 SkIPoint dstPoint, 106 int mipLevel); 107 bool synchronizeBufferToCpu(sk_sp<Buffer>); 108 bool clearBuffer(const Buffer* buffer, size_t offset, size_t size); 109 110 // This sets a translation to be applied to any subsequently added command, assuming these 111 // commands are part of a translated replay of a Graphite recording. setReplayTranslation(SkIVector translation)112 void setReplayTranslation(SkIVector translation) { fReplayTranslation = translation; } clearReplayTranslation()113 void clearReplayTranslation() { fReplayTranslation = {0, 0}; } 114 115 protected: 116 CommandBuffer(); 117 118 SkISize fRenderPassSize; 119 SkIVector fReplayTranslation; 120 121 private: 122 // Release all tracked Resources 123 void releaseResources(); 124 125 virtual void onResetCommandBuffer() = 0; 126 127 virtual bool onAddRenderPass(const RenderPassDesc&, 128 const Texture* colorTexture, 129 const Texture* resolveTexture, 130 const Texture* depthStencilTexture, 131 SkRect viewport, 132 const DrawPassList& drawPasses) = 0; 133 134 virtual bool onAddComputePass(DispatchGroupSpan dispatchGroups) = 0; 135 136 virtual bool onCopyBufferToBuffer(const Buffer* srcBuffer, 137 size_t srcOffset, 138 const Buffer* dstBuffer, 139 size_t dstOffset, 140 size_t size) = 0; 141 virtual bool onCopyTextureToBuffer(const Texture*, 142 SkIRect srcRect, 143 const Buffer*, 144 size_t bufferOffset, 145 size_t bufferRowBytes) = 0; 146 virtual bool onCopyBufferToTexture(const Buffer*, 147 const Texture*, 148 const BufferTextureCopyData*, 149 int count) = 0; 150 virtual bool onCopyTextureToTexture(const Texture* src, 151 SkIRect srcRect, 152 const Texture* dst, 153 SkIPoint dstPoint, 154 int mipLevel) = 0; 155 virtual bool onSynchronizeBufferToCpu(const Buffer*, bool* outDidResultInWork) = 0; 156 virtual bool onClearBuffer(const Buffer*, size_t offset, size_t size) = 0; 157 158 #ifdef SK_DEBUG 159 bool fHasWork = false; 160 #endif 161 inline static constexpr int kInitialTrackedResourcesCount = 32; 162 template <typename T> 163 using TrackedResourceArray = skia_private::STArray<kInitialTrackedResourcesCount, T>; 164 TrackedResourceArray<sk_sp<Resource>> fTrackedUsageResources; 165 TrackedResourceArray<gr_cb<Resource>> fCommandBufferResources; 166 skia_private::TArray<sk_sp<RefCntedCallback>> fFinishedProcs; 167 skia_private::TArray<sk_sp<Buffer>> fBuffersToAsyncMap; 168 }; 169 170 } // namespace skgpu::graphite 171 172 #endif // skgpu_graphite_CommandBuffer_DEFINED 173