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