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/graphite/AttachmentTypes.h" 16 #include "src/gpu/graphite/CommandTypes.h" 17 #include "src/gpu/graphite/ComputeTypes.h" 18 #include "src/gpu/graphite/DrawTypes.h" 19 #include "src/gpu/graphite/DrawWriter.h" 20 21 namespace skgpu { 22 class RefCntedCallback; 23 } 24 25 #ifdef SK_ENABLE_PIET_GPU 26 namespace skgpu::piet { 27 class Scene; 28 } 29 #endif 30 31 namespace skgpu::graphite { 32 33 class Buffer; 34 class ComputePipeline; 35 class DrawPass; 36 class SharedContext; 37 class GraphicsPipeline; 38 class Resource; 39 class Sampler; 40 class Texture; 41 class TextureProxy; 42 43 class CommandBuffer { 44 public: 45 virtual ~CommandBuffer(); 46 47 #ifdef SK_DEBUG hasWork()48 bool hasWork() { return fHasWork; } 49 #endif 50 51 void trackResource(sk_sp<Resource> resource); 52 // Release all tracked Resources 53 void resetCommandBuffer(); 54 55 // If any work is needed to create new resources for a fresh command buffer do that here. 56 virtual bool setNewCommandBufferResources() = 0; 57 58 void addFinishedProc(sk_sp<RefCntedCallback> finishedProc); 59 void callFinishedProcs(bool success); 60 61 bool addRenderPass(const RenderPassDesc&, 62 sk_sp<Texture> colorTexture, 63 sk_sp<Texture> resolveTexture, 64 sk_sp<Texture> depthStencilTexture, 65 SkRect viewport, 66 const std::vector<std::unique_ptr<DrawPass>>& drawPasses); 67 68 bool addComputePass(const ComputePassDesc&, 69 sk_sp<ComputePipeline> pipeline, 70 const std::vector<ResourceBinding>& bindings); 71 72 //--------------------------------------------------------------- 73 // Can only be used outside renderpasses 74 //--------------------------------------------------------------- 75 bool copyBufferToBuffer(sk_sp<Buffer> srcBuffer, 76 size_t srcOffset, 77 sk_sp<Buffer> dstBuffer, 78 size_t dstOffset, 79 size_t size); 80 bool copyTextureToBuffer(sk_sp<Texture>, 81 SkIRect srcRect, 82 sk_sp<Buffer>, 83 size_t bufferOffset, 84 size_t bufferRowBytes); 85 bool copyBufferToTexture(const Buffer*, 86 sk_sp<Texture>, 87 const BufferTextureCopyData*, 88 int count); 89 bool copyTextureToTexture(sk_sp<Texture> src, 90 SkIRect srcRect, 91 sk_sp<Texture> dst, 92 SkIPoint dstPoint); 93 bool synchronizeBufferToCpu(sk_sp<Buffer>); 94 bool clearBuffer(const Buffer* buffer, size_t offset, size_t size); 95 96 #ifdef SK_ENABLE_PIET_GPU 97 void renderPietScene(const skgpu::piet::Scene& scene, sk_sp<Texture> target); 98 #endif 99 100 // This sets a translation to be applied to any subsequently added command, assuming these 101 // commands are part of a translated replay of a Graphite recording. setReplayTranslation(SkIVector translation)102 void setReplayTranslation(SkIVector translation) { fReplayTranslation = translation; } clearReplayTranslation()103 void clearReplayTranslation() { fReplayTranslation = {0, 0}; } 104 105 protected: 106 CommandBuffer(); 107 108 SkISize fRenderPassSize; 109 SkIVector fReplayTranslation; 110 111 private: 112 // Release all tracked Resources 113 void releaseResources(); 114 115 virtual void onResetCommandBuffer() = 0; 116 117 virtual bool onAddRenderPass(const RenderPassDesc&, 118 const Texture* colorTexture, 119 const Texture* resolveTexture, 120 const Texture* depthStencilTexture, 121 SkRect viewport, 122 const std::vector<std::unique_ptr<DrawPass>>& drawPasses) = 0; 123 124 virtual bool onAddComputePass(const ComputePassDesc&, 125 const ComputePipeline*, 126 const std::vector<ResourceBinding>& bindings) = 0; 127 128 virtual bool onCopyBufferToBuffer(const Buffer* srcBuffer, 129 size_t srcOffset, 130 const Buffer* dstBuffer, 131 size_t dstOffset, 132 size_t size) = 0; 133 virtual bool onCopyTextureToBuffer(const Texture*, 134 SkIRect srcRect, 135 const Buffer*, 136 size_t bufferOffset, 137 size_t bufferRowBytes) = 0; 138 virtual bool onCopyBufferToTexture(const Buffer*, 139 const Texture*, 140 const BufferTextureCopyData*, 141 int count) = 0; 142 virtual bool onCopyTextureToTexture(const Texture* src, 143 SkIRect srcRect, 144 const Texture* dst, 145 SkIPoint dstPoint) = 0; 146 virtual bool onSynchronizeBufferToCpu(const Buffer*, bool* outDidResultInWork) = 0; 147 virtual bool onClearBuffer(const Buffer*, size_t offset, size_t size) = 0; 148 149 #ifdef SK_ENABLE_PIET_GPU 150 virtual void onRenderPietScene(const skgpu::piet::Scene& scene, const Texture* target) = 0; 151 #endif 152 153 #ifdef SK_DEBUG 154 bool fHasWork = false; 155 #endif 156 157 inline static constexpr int kInitialTrackedResourcesCount = 32; 158 SkSTArray<kInitialTrackedResourcesCount, sk_sp<Resource>> fTrackedResources; 159 SkTArray<sk_sp<RefCntedCallback>> fFinishedProcs; 160 }; 161 162 } // namespace skgpu::graphite 163 164 #endif // skgpu_graphite_CommandBuffer_DEFINED 165