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 "GrColor.h"
12 
13 class GrFixedClip;
14 class GrGpu;
15 class GrMesh;
16 class GrPipeline;
17 class GrPrimitiveProcessor;
18 class GrRenderTarget;
19 struct SkIRect;
20 
21 /**
22  * The GrGpuCommandBuffer is a series of commands (draws, clears, and discards), which all target
23  * the same render target. It is possible that these commands execute immediately (GL), or get
24  * buffered up for later execution (Vulkan). GrBatches will execute their draw commands into a
25  * GrGpuCommandBuffer.
26  */
27 class GrGpuCommandBuffer {
28 public:
29     enum class LoadOp {
30         kLoad,
31         kClear,
32         kDiscard,
33     };
34 
35     enum class StoreOp {
36         kStore,
37         kDiscard,
38     };
39 
40     struct LoadAndStoreInfo {
41         LoadOp  fLoadOp;
42         StoreOp fStoreOp;
43         GrColor fClearColor;
44     };
45 
46     GrGpuCommandBuffer() {}
47     virtual ~GrGpuCommandBuffer() {}
48 
49     // Signals the end of recording to the command buffer and that it can now be submitted.
50     virtual void end() = 0;
51 
52     // Sends the command buffer off to the GPU object to execute the commands built up in the
53     // buffer. The gpu object is allowed to defer execution of the commands until it is flushed.
54     // The bounds should represent the bounds of all the draws put into the command buffer.
55     void submit(const SkIRect& bounds);
56 
57     // We pass in an array of meshCount GrMesh to the draw. The backend should loop over each
58     // GrMesh object and emit a draw for it. Each draw will use the same GrPipeline and
59     // GrPrimitiveProcessor. This may fail if the draw would exceed any resource limits (e.g.
60     // number of vertex attributes is too large).
61     bool draw(const GrPipeline&,
62               const GrPrimitiveProcessor&,
63               const GrMesh*,
64               int meshCount);
65 
66     /**
67     * Clear the passed in render target. Ignores the draw state and clip.
68     */
69     void clear(const GrFixedClip&, GrColor, GrRenderTarget*);
70 
71     void clearStencilClip(const GrFixedClip&, bool insideStencilMask, GrRenderTarget*);
72     /**
73     * Discards the contents render target. nullptr indicates that the current render target should
74     * be discarded.
75     **/
76     // TODO: This should be removed in the future to favor using the load and store ops for discard
77     virtual void discard(GrRenderTarget* = nullptr) = 0;
78 
79 private:
80     virtual GrGpu* gpu() = 0;
81     virtual void onSubmit(const SkIRect& bounds) = 0;
82 
83     // overridden by backend-specific derived class to perform the draw call.
84     virtual void onDraw(const GrPipeline&,
85                         const GrPrimitiveProcessor&,
86                         const GrMesh*,
87                         int meshCount) = 0;
88 
89     // overridden by backend-specific derived class to perform the clear.
90     virtual void onClear(GrRenderTarget*, const GrFixedClip&, GrColor) = 0;
91 
92     virtual void onClearStencilClip(GrRenderTarget*,
93                                     const GrFixedClip&,
94                                     bool insideStencilMask) = 0;
95 
96 };
97 
98 #endif
99