51acee0c46b35539dd07ddc56640ba3b930a0c22
[platform/upstream/libSkiaSharp.git] / src / gpu / GrTargetCommands.h
1 /*
2  * Copyright 2015 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 GrTargetCommands_DEFINED
9 #define GrTargetCommands_DEFINED
10
11 #include "GrBatch.h"
12 #include "GrBatchTarget.h"
13 #include "GrDrawTarget.h"
14 #include "GrGpu.h"
15 #include "GrPath.h"
16 #include "GrPendingProgramElement.h"
17 #include "GrRenderTarget.h"
18 #include "GrTRecorder.h"
19 #include "SkRect.h"
20 #include "SkTypes.h"
21
22 class GrInOrderDrawBuffer;
23 class GrVertexBufferAllocPool;
24 class GrIndexBufferAllocPool;
25
26 class GrTargetCommands : ::SkNoncopyable {
27     struct SetState;
28
29 public:
30     GrTargetCommands(GrGpu* gpu,
31                      GrVertexBufferAllocPool* vertexPool,
32                      GrIndexBufferAllocPool* indexPool)
33         : fCmdBuffer(kCmdBufferInitialSizeInBytes)
34         , fPrevState(NULL)
35         , fBatchTarget(gpu, vertexPool, indexPool)
36         , fDrawBatch(NULL) {
37     }
38
39     class Cmd : ::SkNoncopyable {
40     public:
41         enum CmdType {
42             kDraw_CmdType              = 1,
43             kStencilPath_CmdType       = 2,
44             kSetState_CmdType          = 3,
45             kClear_CmdType             = 4,
46             kCopySurface_CmdType       = 5,
47             kDrawPath_CmdType          = 6,
48             kDrawPaths_CmdType         = 7,
49             kDrawBatch_CmdType         = 8,
50         };
51
52         Cmd(CmdType type) : fMarkerID(-1), fType(type) {}
53         virtual ~Cmd() {}
54
55         virtual void execute(GrGpu*, const SetState*) = 0;
56
57         CmdType type() const { return fType; }
58
59         // trace markers
60         bool isTraced() const { return -1 != fMarkerID; }
61         void setMarkerID(int markerID) { SkASSERT(-1 == fMarkerID); fMarkerID = markerID; }
62         int markerID() const { return fMarkerID; }
63
64     private:
65         int              fMarkerID;
66         CmdType          fType;
67     };
68
69     void reset();
70     void flush(GrInOrderDrawBuffer*);
71
72     Cmd* recordClearStencilClip(GrInOrderDrawBuffer*,
73                                 const SkIRect& rect,
74                                 bool insideClip,
75                                 GrRenderTarget* renderTarget);
76
77     Cmd* recordDiscard(GrInOrderDrawBuffer*, GrRenderTarget*);
78
79     Cmd* recordDraw(GrInOrderDrawBuffer*,
80                     const GrGeometryProcessor*,
81                     const GrDrawTarget::DrawInfo&,
82                     const GrDrawTarget::PipelineInfo&);
83     Cmd* recordDrawBatch(GrInOrderDrawBuffer*,
84                          GrBatch*,
85                          const GrDrawTarget::PipelineInfo&);
86     void recordDrawRect(GrInOrderDrawBuffer*,
87                         GrPipelineBuilder*,
88                         GrColor,
89                         const SkMatrix& viewMatrix,
90                         const SkRect& rect,
91                         const SkRect* localRect,
92                         const SkMatrix* localMatrix);
93     Cmd* recordStencilPath(GrInOrderDrawBuffer*,
94                            const GrPipelineBuilder&,
95                            const GrPathProcessor*,
96                            const GrPath*,
97                            const GrScissorState&,
98                            const GrStencilSettings&);
99     Cmd* recordDrawPath(GrInOrderDrawBuffer*,
100                         const GrPathProcessor*,
101                         const GrPath*,
102                         const GrStencilSettings&,
103                         const GrDrawTarget::PipelineInfo&);
104     Cmd* recordDrawPaths(GrInOrderDrawBuffer*,
105                          const GrPathProcessor*,
106                          const GrPathRange*,
107                          const void*,
108                          GrDrawTarget::PathIndexType,
109                          const float transformValues[],
110                          GrDrawTarget::PathTransformType ,
111                          int,
112                          const GrStencilSettings&,
113                          const GrDrawTarget::PipelineInfo&);
114     Cmd* recordClear(GrInOrderDrawBuffer*,
115                      const SkIRect* rect,
116                      GrColor,
117                      bool canIgnoreRect,
118                      GrRenderTarget*);
119     Cmd* recordCopySurface(GrInOrderDrawBuffer*,
120                            GrSurface* dst,
121                            GrSurface* src,
122                            const SkIRect& srcRect,
123                            const SkIPoint& dstPoint);
124
125 protected:
126     void willReserveVertexAndIndexSpace(int vertexCount,
127                                         size_t vertexStride,
128                                         int indexCount);
129
130 private:
131     friend class GrInOrderDrawBuffer;
132
133     typedef GrGpu::DrawArgs DrawArgs;
134
135     // Attempts to concat instances from info onto the previous draw. info must represent an
136     // instanced draw. The caller must have already recorded a new draw state and clip if necessary.
137     int concatInstancedDraw(GrInOrderDrawBuffer*, const GrDrawTarget::DrawInfo&);
138
139     bool SK_WARN_UNUSED_RESULT setupPipelineAndShouldDraw(GrInOrderDrawBuffer*,
140                                                           const GrPrimitiveProcessor*,
141                                                           const GrDrawTarget::PipelineInfo&);
142     bool SK_WARN_UNUSED_RESULT setupPipelineAndShouldDraw(GrInOrderDrawBuffer*,
143                                                           GrBatch*,
144                                                           const GrDrawTarget::PipelineInfo&);
145
146     struct Draw : public Cmd {
147         Draw(const GrDrawTarget::DrawInfo& info) : Cmd(kDraw_CmdType), fInfo(info) {}
148
149         void execute(GrGpu*, const SetState*) SK_OVERRIDE;
150
151         GrDrawTarget::DrawInfo     fInfo;
152     };
153
154     struct StencilPath : public Cmd {
155         StencilPath(const GrPath* path, GrRenderTarget* rt)
156             : Cmd(kStencilPath_CmdType)
157             , fRenderTarget(rt)
158             , fPath(path) {}
159
160         const GrPath* path() const { return fPath.get(); }
161
162         void execute(GrGpu*, const SetState*) SK_OVERRIDE;
163
164         SkMatrix                                                fViewMatrix;
165         bool                                                    fUseHWAA;
166         GrStencilSettings                                       fStencil;
167         GrScissorState                                          fScissor;
168     private:
169         GrPendingIOResource<GrRenderTarget, kWrite_GrIOType>    fRenderTarget;
170         GrPendingIOResource<const GrPath, kRead_GrIOType>       fPath;
171     };
172
173     struct DrawPath : public Cmd {
174         DrawPath(const GrPath* path) : Cmd(kDrawPath_CmdType), fPath(path) {}
175
176         const GrPath* path() const { return fPath.get(); }
177
178         void execute(GrGpu*, const SetState*) SK_OVERRIDE;
179
180         GrStencilSettings       fStencilSettings;
181
182     private:
183         GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
184     };
185
186     struct DrawPaths : public Cmd {
187         DrawPaths(const GrPathRange* pathRange) : Cmd(kDrawPaths_CmdType), fPathRange(pathRange) {}
188
189         const GrPathRange* pathRange() const { return fPathRange.get();  }
190
191         void execute(GrGpu*, const SetState*) SK_OVERRIDE;
192
193         char*                           fIndices;
194         GrDrawTarget::PathIndexType     fIndexType;
195         float*                          fTransforms;
196         GrDrawTarget::PathTransformType fTransformType;
197         int                             fCount;
198         GrStencilSettings               fStencilSettings;
199
200     private:
201         GrPendingIOResource<const GrPathRange, kRead_GrIOType> fPathRange;
202     };
203
204     // This is also used to record a discard by setting the color to GrColor_ILLEGAL
205     struct Clear : public Cmd {
206         Clear(GrRenderTarget* rt) : Cmd(kClear_CmdType), fRenderTarget(rt) {}
207
208         GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
209
210         void execute(GrGpu*, const SetState*) SK_OVERRIDE;
211
212         SkIRect fRect;
213         GrColor fColor;
214         bool    fCanIgnoreRect;
215
216     private:
217         GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
218     };
219
220     // This command is ONLY used by the clip mask manager to clear the stencil clip bits
221     struct ClearStencilClip : public Cmd {
222         ClearStencilClip(GrRenderTarget* rt) : Cmd(kClear_CmdType), fRenderTarget(rt) {}
223
224         GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
225
226         void execute(GrGpu*, const SetState*) SK_OVERRIDE;
227
228         SkIRect fRect;
229         bool    fInsideClip;
230
231     private:
232         GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
233     };
234
235     struct CopySurface : public Cmd {
236         CopySurface(GrSurface* dst, GrSurface* src)
237             : Cmd(kCopySurface_CmdType)
238             , fDst(dst)
239             , fSrc(src) {
240         }
241
242         GrSurface* dst() const { return fDst.get(); }
243         GrSurface* src() const { return fSrc.get(); }
244
245         void execute(GrGpu*, const SetState*) SK_OVERRIDE;
246
247         SkIPoint    fDstPoint;
248         SkIRect     fSrcRect;
249
250     private:
251         GrPendingIOResource<GrSurface, kWrite_GrIOType> fDst;
252         GrPendingIOResource<GrSurface, kRead_GrIOType> fSrc;
253     };
254
255     // TODO: rename to SetPipeline once pp, batch tracker, and desc are removed
256     struct SetState : public Cmd {
257         // TODO get rid of the prim proc parameter when we use batch everywhere
258         SetState(const GrPrimitiveProcessor* primProc = NULL)
259         : Cmd(kSetState_CmdType)
260         , fPrimitiveProcessor(primProc) {}
261
262         ~SetState() { reinterpret_cast<GrPipeline*>(fPipeline.get())->~GrPipeline(); }
263
264         // This function is only for getting the location in memory where we will create our
265         // pipeline object.
266         GrPipeline* pipelineLocation() { return reinterpret_cast<GrPipeline*>(fPipeline.get()); }
267
268         const GrPipeline* getPipeline() const {
269             return reinterpret_cast<const GrPipeline*>(fPipeline.get());
270         }
271
272         void execute(GrGpu*, const SetState*) SK_OVERRIDE;
273
274         typedef GrPendingProgramElement<const GrPrimitiveProcessor> ProgramPrimitiveProcessor;
275         ProgramPrimitiveProcessor               fPrimitiveProcessor;
276         SkAlignedSStorage<sizeof(GrPipeline)>   fPipeline;
277         GrProgramDesc                           fDesc;
278         GrBatchTracker                          fBatchTracker;
279     };
280
281     struct DrawBatch : public Cmd {
282         DrawBatch(GrBatch* batch, GrBatchTarget* batchTarget) 
283             : Cmd(kDrawBatch_CmdType)
284             , fBatch(SkRef(batch))
285             , fBatchTarget(batchTarget) {
286             SkASSERT(!batch->isUsed());
287         }
288
289         void execute(GrGpu*, const SetState*) SK_OVERRIDE;
290
291         // TODO it wouldn't be too hard to let batches allocate in the cmd buffer
292         SkAutoTUnref<GrBatch>  fBatch;
293
294     private:
295         GrBatchTarget*         fBatchTarget;
296     };
297
298      static const int kCmdBufferInitialSizeInBytes = 8 * 1024;
299
300      typedef void* TCmdAlign; // This wouldn't be enough align if a command used long double.
301      typedef GrTRecorder<Cmd, TCmdAlign> CmdBuffer;
302
303      CmdBuffer                           fCmdBuffer;
304      SetState*                           fPrevState;
305      GrBatchTarget                       fBatchTarget;
306      // TODO hack until batch is everywhere
307      GrTargetCommands::DrawBatch*        fDrawBatch;
308
309      // This will go away when everything uses batch.  However, in the short term anything which
310      // might be put into the GrInOrderDrawBuffer needs to make sure it closes the last batch
311      void closeBatch();
312 };
313
314 #endif
315