• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 Google LLC
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 GrRenderTask_DEFINED
9 #define GrRenderTask_DEFINED
10 
11 #include "include/core/SkRefCnt.h"
12 #include "include/private/SkColorData.h"
13 #include "include/private/SkTDArray.h"
14 #include "src/gpu/GrTextureProxy.h"
15 #include "src/gpu/GrTextureResolveManager.h"
16 
17 class GrOpFlushState;
18 class GrOpList;
19 class GrRenderTargetOpList;
20 class GrResourceAllocator;
21 class GrTextureOpList;
22 
23 // This class abstracts a task that targets a single GrSurfaceProxy, participates in the
24 // GrDrawingManager's DAG, and implements the onExecute method to modify its target proxy's
25 // contents. (e.g., an opList that executes a command buffer, a task to regenerate mipmaps, etc.)
26 class GrRenderTask : public SkRefCnt {
27 public:
28     GrRenderTask(sk_sp<GrSurfaceProxy> target);
29     ~GrRenderTask() override;
30 
31     // These two methods are only invoked at flush time
32     void prepare(GrOpFlushState* flushState);
execute(GrOpFlushState * flushState)33     bool execute(GrOpFlushState* flushState) { return this->onExecute(flushState); }
34 
makeClosed(const GrCaps &)35     virtual void makeClosed(const GrCaps&) {
36         if (!this->isClosed()) {
37             this->setFlag(kClosed_Flag);
38         }
39     }
40 
41     // Called when this class will survive a flush and needs to truncate its ops and start over.
42     // TODO: ultimately it should be invalid for an op list to survive a flush.
43     // https://bugs.chromium.org/p/skia/issues/detail?id=7111
endFlush()44     virtual void endFlush() {}
45 
isClosed()46     bool isClosed() const { return this->isSetFlag(kClosed_Flag); }
47 
48     /*
49      * Notify this GrRenderTask that it relies on the contents of 'dependedOn'
50      */
51     void addDependency(GrSurfaceProxy* dependedOn, GrMipMapped, GrTextureResolveManager,
52                        const GrCaps& caps);
53 
54     /*
55      * Does this renderTask depend on 'dependedOn'?
56      */
57     bool dependsOn(const GrRenderTask* dependedOn) const;
58 
uniqueID()59     uint32_t uniqueID() const { return fUniqueID; }
60 
61     /*
62      * Safely cast this GrRenderTask to a GrTextureOpList (if possible).
63      */
asTextureOpList()64     virtual GrTextureOpList* asTextureOpList() { return nullptr; }
65 
66     /*
67      * Safely cast this GrRenderTask to a GrRenderTargetOpList (if possible).
68      */
asRenderTargetOpList()69     virtual GrRenderTargetOpList* asRenderTargetOpList() { return nullptr; }
70 
71     /*
72      * Dump out the GrRenderTask dependency DAG
73      */
74     SkDEBUGCODE(virtual void dump(bool printDependencies) const;)
75 
76     SkDEBUGCODE(virtual int numClips() const { return 0; })
77 
78 protected:
79     // In addition to just the GrSurface being allocated, has the stencil buffer been allocated (if
80     // it is required)?
81     bool isInstantiated() const;
82 
83     SkDEBUGCODE(bool deferredProxiesAreInstantiated() const;)
84 
85     sk_sp<GrSurfaceProxy> fTarget;
86 
87     // List of texture proxies whose contents are being prepared on a worker thread
88     // TODO: this list exists so we can fire off the proper upload when an renderTask begins
89     // executing. Can this be replaced?
90     SkTArray<GrTextureProxy*, true> fDeferredProxies;
91 
92 private:
93     // for resetFlag, TopoSortTraits, gatherProxyIntervals, handleInternalAllocationFailure
94     friend class GrDrawingManager;
95 
96     // Drops any pending operations that reference proxies that are not instantiated.
97     // NOTE: Derived classes don't need to check fTarget. That is handled when the drawingManager
98     // calls isInstantiated.
99     virtual void handleInternalAllocationFailure() = 0;
100 
101     virtual bool onIsUsed(GrSurfaceProxy*) const = 0;
102 
isUsed(GrSurfaceProxy * proxy)103     bool isUsed(GrSurfaceProxy* proxy) const {
104         if (proxy == fTarget.get()) {
105             return true;
106         }
107 
108         return this->onIsUsed(proxy);
109     }
110 
111     void addDependency(GrRenderTask* dependedOn);
112     void addDependent(GrRenderTask* dependent);
113     SkDEBUGCODE(bool isDependedent(const GrRenderTask* dependent) const;)
114     SkDEBUGCODE(void validate() const;)
115     void closeThoseWhoDependOnMe(const GrCaps&);
116 
117     // Feed proxy usage intervals to the GrResourceAllocator class
118     virtual void gatherProxyIntervals(GrResourceAllocator*) const = 0;
119 
120     static uint32_t CreateUniqueID();
121 
122     enum Flags {
123         kClosed_Flag    = 0x01,   //!< This GrRenderTask can't accept any more dependencies.
124 
125         kWasOutput_Flag = 0x02,   //!< Flag for topological sorting
126         kTempMark_Flag  = 0x04,   //!< Flag for topological sorting
127     };
128 
setFlag(uint32_t flag)129     void setFlag(uint32_t flag) {
130         fFlags |= flag;
131     }
132 
resetFlag(uint32_t flag)133     void resetFlag(uint32_t flag) {
134         fFlags &= ~flag;
135     }
136 
isSetFlag(uint32_t flag)137     bool isSetFlag(uint32_t flag) const {
138         return SkToBool(fFlags & flag);
139     }
140 
141     struct TopoSortTraits {
OutputTopoSortTraits142         static void Output(GrRenderTask* renderTask, int /* index */) {
143             renderTask->setFlag(kWasOutput_Flag);
144         }
WasOutputTopoSortTraits145         static bool WasOutput(const GrRenderTask* renderTask) {
146             return renderTask->isSetFlag(kWasOutput_Flag);
147         }
SetTempMarkTopoSortTraits148         static void SetTempMark(GrRenderTask* renderTask) {
149             renderTask->setFlag(kTempMark_Flag);
150         }
ResetTempMarkTopoSortTraits151         static void ResetTempMark(GrRenderTask* renderTask) {
152             renderTask->resetFlag(kTempMark_Flag);
153         }
IsTempMarkedTopoSortTraits154         static bool IsTempMarked(const GrRenderTask* renderTask) {
155             return renderTask->isSetFlag(kTempMark_Flag);
156         }
NumDependenciesTopoSortTraits157         static int NumDependencies(const GrRenderTask* renderTask) {
158             return renderTask->fDependencies.count();
159         }
DependencyTopoSortTraits160         static GrRenderTask* Dependency(GrRenderTask* renderTask, int index) {
161             return renderTask->fDependencies[index];
162         }
163     };
164 
165     virtual void onPrepare(GrOpFlushState* flushState) = 0;
166     virtual bool onExecute(GrOpFlushState* flushState) = 0;
167 
168     const uint32_t         fUniqueID;
169     uint32_t               fFlags;
170 
171     // 'this' GrOpList relies on the output of the GrOpLists in 'fDependencies'
172     SkSTArray<1, GrRenderTask*, true> fDependencies;
173     // 'this' GrOpList's output is relied on by the GrOpLists in 'fDependents'
174     SkSTArray<1, GrRenderTask*, true> fDependents;
175 };
176 
177 #endif
178