1 /*
2  * Copyright 2010 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 GrRenderTargetOpList_DEFINED
9 #define GrRenderTargetOpList_DEFINED
10 
11 #include "GrAppliedClip.h"
12 #include "GrOpList.h"
13 #include "GrPathRendering.h"
14 #include "GrPrimitiveProcessor.h"
15 #include "SkArenaAlloc.h"
16 #include "SkClipStack.h"
17 #include "SkMatrix.h"
18 #include "SkStringUtils.h"
19 #include "SkStrokeRec.h"
20 #include "SkTArray.h"
21 #include "SkTLazy.h"
22 #include "SkTypes.h"
23 
24 class GrAuditTrail;
25 class GrClearOp;
26 class GrCaps;
27 class GrOp;
28 class GrPipelineBuilder;
29 class GrRenderTargetProxy;
30 
31 class GrRenderTargetOpList final : public GrOpList {
32 private:
33     using DstTexture = GrXferProcessor::DstTexture;
34 
35 public:
36     /** Options for GrRenderTargetOpList behavior. */
37     struct Options {
38         int fMaxOpCombineLookback = -1;
39         int fMaxOpCombineLookahead = -1;
40     };
41 
42     GrRenderTargetOpList(GrRenderTargetProxy*, GrGpu*, GrResourceProvider*,
43                          GrAuditTrail*, const Options&);
44 
45     ~GrRenderTargetOpList() override;
46 
makeClosed()47     void makeClosed() override {
48         INHERITED::makeClosed();
49 
50         fLastFullClearOp = nullptr;
51         this->forwardCombine();
52     }
53 
54     /**
55      * Empties the draw buffer of any queued up draws.
56      */
57     void reset() override;
58 
59     void abandonGpuResources() override;
60     void freeGpuResources() override;
61 
62     /**
63      * Together these two functions flush all queued up draws to GrCommandBuffer. The return value
64      * of executeOps() indicates whether any commands were actually issued to the GPU.
65      */
66     void prepareOps(GrOpFlushState* flushState) override;
67     bool executeOps(GrOpFlushState* flushState) override;
68 
69     /**
70      * Gets the capabilities of the draw target.
71      */
caps()72     const GrCaps* caps() const { return fGpu->caps(); }
73 
addOp(std::unique_ptr<GrOp> op,GrRenderTargetContext * renderTargetContext)74     uint32_t addOp(std::unique_ptr<GrOp> op, GrRenderTargetContext* renderTargetContext) {
75         this->recordOp(std::move(op), renderTargetContext, nullptr, nullptr);
76         return this->uniqueID();
77     }
addOp(std::unique_ptr<GrOp> op,GrRenderTargetContext * renderTargetContext,GrAppliedClip && clip,const DstTexture & dstTexture)78     uint32_t addOp(std::unique_ptr<GrOp> op, GrRenderTargetContext* renderTargetContext,
79                    GrAppliedClip&& clip, const DstTexture& dstTexture) {
80         this->recordOp(std::move(op), renderTargetContext, clip.doesClip() ? &clip : nullptr,
81                        &dstTexture);
82         return this->uniqueID();
83     }
84 
85     /** Clears the entire render target */
86     void fullClear(GrRenderTargetContext*, GrColor color);
87 
88     /** Discards the contents render target. */
89     void discard(GrRenderTargetContext*);
90 
91     /**
92      * Copies a pixel rectangle from one surface to another. This call may finalize
93      * reserved vertex/index data (as though a draw call was made). The src pixels
94      * copied are specified by srcRect. They are copied to a rect of the same
95      * size in dst with top left at dstPoint. If the src rect is clipped by the
96      * src bounds then  pixel values in the dst rect corresponding to area clipped
97      * by the src rect are not overwritten. This method is not guaranteed to succeed
98      * depending on the type of surface, configs, etc, and the backend-specific
99      * limitations.
100      */
101     bool copySurface(GrSurface* dst,
102                      GrSurface* src,
103                      const SkIRect& srcRect,
104                      const SkIPoint& dstPoint);
105 
instancedRendering()106     gr_instanced::InstancedRendering* instancedRendering() const {
107         SkASSERT(fInstancedRendering);
108         return fInstancedRendering.get();
109     }
110 
asRenderTargetOpList()111     GrRenderTargetOpList* asRenderTargetOpList() override { return this; }
112 
113     SkDEBUGCODE(void dump() const override;)
114 
115     SkDEBUGCODE(void validateTargetsSingleRenderTarget() const;)
116 
117 private:
118     friend class GrRenderTargetContextPriv; // for clearStencilClip and stencil clip state.
119 
120     struct RecordedOp {
RecordedOpRecordedOp121         RecordedOp(std::unique_ptr<GrOp> op, GrRenderTarget* rt, const GrAppliedClip* appliedClip,
122                    const DstTexture* dstTexture)
123                 : fOp(std::move(op)), fRenderTarget(rt), fAppliedClip(appliedClip) {
124             if (dstTexture) {
125                 fDstTexture = *dstTexture;
126             }
127         }
128         std::unique_ptr<GrOp> fOp;
129         // TODO: These ops will all to target the same render target and this won't be needed.
130         GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
131         DstTexture fDstTexture;
132         const GrAppliedClip* fAppliedClip;
133     };
134 
135     // If the input op is combined with an earlier op, this returns the combined op. Otherwise, it
136     // returns the input op.
137     GrOp* recordOp(std::unique_ptr<GrOp>, GrRenderTargetContext*, GrAppliedClip* = nullptr,
138                    const DstTexture* = nullptr);
139 
140     void forwardCombine();
141 
142     // Used only via GrRenderTargetContextPriv.
143     void clearStencilClip(const GrFixedClip&, bool insideStencilMask, GrRenderTargetContext*);
144 
145     // If this returns true then b has been merged into a's op.
146     bool combineIfPossible(const RecordedOp& a, GrOp* b, const GrAppliedClip* bClip,
147                            const DstTexture* bDstTexture);
148 
149     GrClearOp* fLastFullClearOp = nullptr;
150     GrGpuResource::UniqueID fLastFullClearRenderTargetID = GrGpuResource::UniqueID::InvalidID();
151 
152     GrGpu* fGpu;
153     GrResourceProvider* fResourceProvider;
154 
155     int fMaxOpLookback;
156     int fMaxOpLookahead;
157 
158     std::unique_ptr<gr_instanced::InstancedRendering> fInstancedRendering;
159 
160     int32_t fLastClipStackGenID;
161     SkIRect fLastDevClipBounds;
162 
163     SkSTArray<256, RecordedOp, true> fRecordedOps;
164 
165     char fClipAllocatorStorage[4096];
166     SkArenaAlloc fClipAllocator;
167 
168     typedef GrOpList INHERITED;
169 };
170 
171 #endif
172