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 "text/GrAtlasTextContext.h"
12 #include "GrDrawTarget.h"
13 #include "GrBatchFlushState.h"
14 #include "GrPathRendererChain.h"
15 #include "GrPathRenderer.h"
16 #include "GrResourceCache.h"
17 #include "SkTDArray.h"
18 
19 class GrContext;
20 class GrDrawContext;
21 class GrSingleOWner;
22 class GrSoftwarePathRenderer;
23 
24 // The GrDrawingManager allocates a new GrDrawContext for each GrRenderTarget
25 // but all of them still land in the same GrDrawTarget!
26 //
27 // In the future this class will allocate a new GrDrawContext for
28 // each GrRenderTarget/GrDrawTarget and manage the DAG.
29 class GrDrawingManager {
30 public:
31     ~GrDrawingManager();
32 
wasAbandoned()33     bool wasAbandoned() const { return fAbandoned; }
34     void freeGpuResources();
35 
36     sk_sp<GrDrawContext> makeDrawContext(sk_sp<GrRenderTarget> rt,
37                                          sk_sp<SkColorSpace>,
38                                          const SkSurfaceProps*);
39 
40     // The caller automatically gets a ref on the returned drawTarget. It must
41     // be balanced by an unref call.
42     GrDrawTarget* newDrawTarget(GrRenderTarget* rt);
43 
getContext()44     GrContext* getContext() { return fContext; }
45 
46     GrAtlasTextContext* getAtlasTextContext();
47 
48     GrPathRenderer* getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
49                                     bool allowSW,
50                                     GrPathRendererChain::DrawType drawType,
51                                     GrPathRenderer::StencilSupport* stencilSupport = NULL);
52 
flushIfNecessary()53     void flushIfNecessary() {
54         if (fContext->getResourceCache()->requestsFlush()) {
55             this->internalFlush(GrResourceCache::kCacheRequested);
56         } else if (fIsImmediateMode) {
57             this->internalFlush(GrResourceCache::kImmediateMode);
58         }
59     }
60 
61     static bool ProgramUnitTest(GrContext* context, int maxStages);
62 
63     void prepareSurfaceForExternalIO(GrSurface*);
64 
65 private:
GrDrawingManager(GrContext * context,const GrDrawTarget::Options & optionsForDrawTargets,const GrPathRendererChain::Options & optionsForPathRendererChain,bool isImmediateMode,GrSingleOwner * singleOwner)66     GrDrawingManager(GrContext* context, const GrDrawTarget::Options& optionsForDrawTargets,
67                      const GrPathRendererChain::Options& optionsForPathRendererChain,
68                      bool isImmediateMode, GrSingleOwner* singleOwner)
69         : fContext(context)
70         , fOptionsForDrawTargets(optionsForDrawTargets)
71         , fOptionsForPathRendererChain(optionsForPathRendererChain)
72         , fSingleOwner(singleOwner)
73         , fAbandoned(false)
74         , fAtlasTextContext(nullptr)
75         , fPathRendererChain(nullptr)
76         , fSoftwarePathRenderer(nullptr)
77         , fFlushState(context->getGpu(), context->resourceProvider())
78         , fFlushing(false)
79         , fIsImmediateMode(isImmediateMode) {
80     }
81 
82     void abandon();
83     void cleanup();
84     void reset();
flush()85     void flush() { this->internalFlush(GrResourceCache::FlushType::kExternal); }
86     void internalFlush(GrResourceCache::FlushType);
87 
88     friend class GrContext;  // for access to: ctor, abandon, reset & flush
89 
90     static const int kNumPixelGeometries = 5; // The different pixel geometries
91     static const int kNumDFTOptions = 2;      // DFT or no DFT
92 
93     GrContext*                        fContext;
94     GrDrawTarget::Options             fOptionsForDrawTargets;
95     GrPathRendererChain::Options      fOptionsForPathRendererChain;
96 
97     // In debug builds we guard against improper thread handling
98     GrSingleOwner*                    fSingleOwner;
99 
100     bool                              fAbandoned;
101     SkTDArray<GrDrawTarget*>          fDrawTargets;
102 
103     SkAutoTDelete<GrAtlasTextContext> fAtlasTextContext;
104 
105     GrPathRendererChain*              fPathRendererChain;
106     GrSoftwarePathRenderer*           fSoftwarePathRenderer;
107 
108     GrBatchFlushState                 fFlushState;
109     bool                              fFlushing;
110 
111     bool                              fIsImmediateMode;
112 };
113 
114 #endif
115