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 GrDrawingManager_DEFINED
9 #define GrDrawingManager_DEFINED
10 
11 #include "GrPathRenderer.h"
12 #include "GrPathRendererChain.h"
13 #include "GrRenderTargetOpList.h"
14 #include "GrResourceCache.h"
15 #include "SkTArray.h"
16 #include "text/GrAtlasTextContext.h"
17 
18 class GrContext;
19 class GrCoverageCountingPathRenderer;
20 class GrOnFlushCallbackObject;
21 class GrRenderTargetContext;
22 class GrRenderTargetProxy;
23 class GrSingleOWner;
24 class GrSoftwarePathRenderer;
25 class GrTextureContext;
26 class GrTextureOpList;
27 
28 // The GrDrawingManager allocates a new GrRenderTargetContext for each GrRenderTarget
29 // but all of them still land in the same GrOpList!
30 //
31 // In the future this class will allocate a new GrRenderTargetContext for
32 // each GrRenderTarget/GrOpList and manage the DAG.
33 class GrDrawingManager {
34 public:
35     ~GrDrawingManager();
36 
37     bool wasAbandoned() const { return fAbandoned; }
38     void freeGpuResources();
39 
40     sk_sp<GrRenderTargetContext> makeRenderTargetContext(sk_sp<GrSurfaceProxy>,
41                                                          sk_sp<SkColorSpace>,
42                                                          const SkSurfaceProps*,
43                                                          bool managedOpList = true);
44     sk_sp<GrTextureContext> makeTextureContext(sk_sp<GrSurfaceProxy>, sk_sp<SkColorSpace>);
45 
46     // The caller automatically gets a ref on the returned opList. It must
47     // be balanced by an unref call.
48     // A managed opList is controlled by the drawing manager (i.e., sorted & flushed with the
49     // other). An unmanaged one is created and used by the onFlushCallback.
50     sk_sp<GrRenderTargetOpList> newRTOpList(GrRenderTargetProxy* rtp, bool managedOpList);
51     sk_sp<GrTextureOpList> newTextureOpList(GrTextureProxy* textureProxy);
52 
53     GrContext* getContext() { return fContext; }
54 
55     GrAtlasTextContext* getAtlasTextContext();
56 
57     GrPathRenderer* getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
58                                     bool allowSW,
59                                     GrPathRendererChain::DrawType drawType,
60                                     GrPathRenderer::StencilSupport* stencilSupport = nullptr);
61 
62     // Returns a direct pointer to the coverage counting path renderer, or null if it is not
63     // supported and turned on.
64     GrCoverageCountingPathRenderer* getCoverageCountingPathRenderer();
65 
66     void flushIfNecessary() {
67         GrResourceCache* resourceCache = fContext->contextPriv().getResourceCache();
68         if (resourceCache && resourceCache->requestsFlush()) {
69             this->internalFlush(nullptr, GrResourceCache::kCacheRequested, 0, nullptr);
70         }
71     }
72 
73     static bool ProgramUnitTest(GrContext* context, int maxStages, int maxLevels);
74 
75     GrSemaphoresSubmitted prepareSurfaceForExternalIO(GrSurfaceProxy*,
76                                                       int numSemaphores,
77                                                       GrBackendSemaphore backendSemaphores[]);
78 
79     void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
80     void testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject*);
81 
82     void moveOpListsToDDL(SkDeferredDisplayList* ddl);
83     void copyOpListsFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
84 
85 private:
86     GrDrawingManager(GrContext*, const GrPathRendererChain::Options&,
87                      const GrAtlasTextContext::Options&, GrSingleOwner*,
88                      GrContextOptions::Enable sortRenderTargets);
89 
90     void abandon();
91     void cleanup();
92 
93     // return true if any opLists were actually executed; false otherwise
94     bool executeOpLists(int startIndex, int stopIndex, GrOpFlushState*);
95 
96     GrSemaphoresSubmitted flush(GrSurfaceProxy* proxy,
97                                 int numSemaphores = 0,
98                                 GrBackendSemaphore backendSemaphores[] = nullptr) {
99         return this->internalFlush(proxy, GrResourceCache::FlushType::kExternal,
100                                    numSemaphores, backendSemaphores);
101     }
102     GrSemaphoresSubmitted internalFlush(GrSurfaceProxy*,
103                                         GrResourceCache::FlushType,
104                                         int numSemaphores,
105                                         GrBackendSemaphore backendSemaphores[]);
106 
107     friend class GrContext;  // for access to: ctor, abandon, reset & flush
108     friend class GrContextPriv; // access to: flush
109     friend class GrOnFlushResourceProvider; // this is just a shallow wrapper around this class
110 
111     static const int kNumPixelGeometries = 5; // The different pixel geometries
112     static const int kNumDFTOptions = 2;      // DFT or no DFT
113 
114     GrContext*                        fContext;
115     GrPathRendererChain::Options      fOptionsForPathRendererChain;
116     GrAtlasTextContext::Options       fOptionsForAtlasTextContext;
117 
118     // In debug builds we guard against improper thread handling
119     GrSingleOwner*                    fSingleOwner;
120 
121     bool                              fAbandoned;
122     SkTArray<sk_sp<GrOpList>>         fOpLists;
123     // These are the IDs of the opLists currently being flushed (in internalFlush)
124     SkSTArray<8, uint32_t, true>      fFlushingOpListIDs;
125     // These are the new opLists generated by the onFlush CBs
126     SkSTArray<8, sk_sp<GrOpList>>     fOnFlushCBOpLists;
127 
128     std::unique_ptr<GrAtlasTextContext> fAtlasTextContext;
129 
130     GrPathRendererChain*              fPathRendererChain;
131     GrSoftwarePathRenderer*           fSoftwarePathRenderer;
132 
133     GrTokenTracker                    fTokenTracker;
134     bool                              fFlushing;
135     bool                              fSortRenderTargets;
136 
137     SkTArray<GrOnFlushCallbackObject*> fOnFlushCBObjects;
138 };
139 
140 #endif
141