1 /* 2 * Copyright 2016 Google Inc. 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 GrGpuCommandBuffer_DEFINED 9 #define GrGpuCommandBuffer_DEFINED 10 11 #include "include/core/SkDrawable.h" 12 #include "src/gpu/GrPipeline.h" 13 #include "src/gpu/ops/GrDrawOp.h" 14 15 class GrOpFlushState; 16 class GrFixedClip; 17 class GrGpu; 18 class GrMesh; 19 class GrPipeline; 20 class GrPrimitiveProcessor; 21 class GrRenderTarget; 22 class GrSemaphore; 23 struct SkIRect; 24 struct SkRect; 25 26 class GrGpuRTCommandBuffer; 27 28 class GrGpuCommandBuffer { 29 public: ~GrGpuCommandBuffer()30 virtual ~GrGpuCommandBuffer() {} 31 32 // Copy src into current surface owned by either a GrGpuTextureCommandBuffer or 33 // GrGpuRenderTargetCommandBuffer. The srcRect and dstPoint must be in dst coords and have 34 // already been adjusted for any origin flips. 35 virtual void copy(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) = 0; 36 // Initiates a transfer from the surface owned by the command buffer to the GrGpuBuffer. 37 virtual void transferFrom(const SkIRect& srcRect, GrColorType surfaceColorType, 38 GrColorType bufferColorType, GrGpuBuffer* transferBuffer, 39 size_t offset) = 0; 40 41 virtual void insertEventMarker(const char*) = 0; 42 asRTCommandBuffer()43 virtual GrGpuRTCommandBuffer* asRTCommandBuffer() { return nullptr; } 44 }; 45 46 class GrGpuTextureCommandBuffer : public GrGpuCommandBuffer{ 47 public: set(GrTexture * texture,GrSurfaceOrigin origin)48 void set(GrTexture* texture, GrSurfaceOrigin origin) { 49 SkASSERT(!fTexture); 50 51 fTexture = texture; 52 } 53 54 protected: GrGpuTextureCommandBuffer()55 GrGpuTextureCommandBuffer() : fTexture(nullptr) {} 56 GrGpuTextureCommandBuffer(GrTexture * texture,GrSurfaceOrigin origin)57 GrGpuTextureCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) : fTexture(texture) {} 58 59 GrTexture* fTexture; 60 61 private: 62 typedef GrGpuCommandBuffer INHERITED; 63 }; 64 65 /** 66 * The GrGpuRenderTargetCommandBuffer is a series of commands (draws, clears, and discards), which 67 * all target the same render target. It is possible that these commands execute immediately (GL), 68 * or get buffered up for later execution (Vulkan). GrOps will execute their draw commands into a 69 * GrGpuCommandBuffer. 70 */ 71 class GrGpuRTCommandBuffer : public GrGpuCommandBuffer { 72 public: 73 struct LoadAndStoreInfo { 74 GrLoadOp fLoadOp; 75 GrStoreOp fStoreOp; 76 SkPMColor4f fClearColor; 77 }; 78 79 // Load-time clears of the stencil buffer are always to 0 so we don't store 80 // an 'fStencilClearValue' 81 struct StencilLoadAndStoreInfo { 82 GrLoadOp fLoadOp; 83 GrStoreOp fStoreOp; 84 }; 85 asRTCommandBuffer()86 GrGpuRTCommandBuffer* asRTCommandBuffer() { return this; } 87 88 virtual void begin() = 0; 89 // Signals the end of recording to the command buffer and that it can now be submitted. 90 virtual void end() = 0; 91 92 // We pass in an array of meshCount GrMesh to the draw. The backend should loop over each 93 // GrMesh object and emit a draw for it. Each draw will use the same GrPipeline and 94 // GrPrimitiveProcessor. This may fail if the draw would exceed any resource limits (e.g. 95 // number of vertex attributes is too large). 96 bool draw(const GrPrimitiveProcessor&, 97 const GrPipeline&, 98 const GrPipeline::FixedDynamicState*, 99 const GrPipeline::DynamicStateArrays*, 100 const GrMesh[], 101 int meshCount, 102 const SkRect& bounds); 103 104 // Performs an upload of vertex data in the middle of a set of a set of draws 105 virtual void inlineUpload(GrOpFlushState*, GrDeferredTextureUploadFn&) = 0; 106 107 /** 108 * Clear the owned render target. Ignores the draw state and clip. 109 */ 110 void clear(const GrFixedClip&, const SkPMColor4f&); 111 112 void clearStencilClip(const GrFixedClip&, bool insideStencilMask); 113 114 /** 115 * Executes the SkDrawable object for the underlying backend. 116 */ executeDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler>)117 virtual void executeDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler>) {} 118 119 protected: GrGpuRTCommandBuffer()120 GrGpuRTCommandBuffer() : fOrigin(kTopLeft_GrSurfaceOrigin), fRenderTarget(nullptr) {} 121 GrGpuRTCommandBuffer(GrRenderTarget * rt,GrSurfaceOrigin origin)122 GrGpuRTCommandBuffer(GrRenderTarget* rt, GrSurfaceOrigin origin) 123 : fOrigin(origin) 124 , fRenderTarget(rt) { 125 } 126 set(GrRenderTarget * rt,GrSurfaceOrigin origin)127 void set(GrRenderTarget* rt, GrSurfaceOrigin origin) { 128 SkASSERT(!fRenderTarget); 129 130 fRenderTarget = rt; 131 fOrigin = origin; 132 } 133 134 GrSurfaceOrigin fOrigin; 135 GrRenderTarget* fRenderTarget; 136 137 private: 138 virtual GrGpu* gpu() = 0; 139 140 // overridden by backend-specific derived class to perform the draw call. 141 virtual void onDraw(const GrPrimitiveProcessor&, 142 const GrPipeline&, 143 const GrPipeline::FixedDynamicState*, 144 const GrPipeline::DynamicStateArrays*, 145 const GrMesh[], 146 int meshCount, 147 const SkRect& bounds) = 0; 148 149 // overridden by backend-specific derived class to perform the clear. 150 virtual void onClear(const GrFixedClip&, const SkPMColor4f&) = 0; 151 152 virtual void onClearStencilClip(const GrFixedClip&, bool insideStencilMask) = 0; 153 154 typedef GrGpuCommandBuffer INHERITED; 155 }; 156 157 #endif 158