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 "GrOpFlushState.h" 12 #include "GrPathRenderer.h" 13 #include "GrPathRendererChain.h" 14 #include "GrRenderTargetOpList.h" 15 #include "GrResourceCache.h" 16 #include "SkTArray.h" 17 #include "instanced/InstancedRendering.h" 18 #include "text/GrAtlasTextContext.h" 19 20 class GrContext; 21 class GrOnFlushCallbackObject; 22 class GrRenderTargetContext; 23 class GrRenderTargetProxy; 24 class GrSingleOWner; 25 class GrSoftwarePathRenderer; 26 class GrTextureContext; 27 class GrTextureOpList; 28 29 // The GrDrawingManager allocates a new GrRenderTargetContext for each GrRenderTarget 30 // but all of them still land in the same GrOpList! 31 // 32 // In the future this class will allocate a new GrRenderTargetContext for 33 // each GrRenderTarget/GrOpList and manage the DAG. 34 class GrDrawingManager { 35 public: 36 ~GrDrawingManager(); 37 wasAbandoned()38 bool wasAbandoned() const { return fAbandoned; } 39 void freeGpuResources(); 40 41 gr_instanced::OpAllocator* instancingAllocator(); 42 43 sk_sp<GrRenderTargetContext> makeRenderTargetContext(sk_sp<GrSurfaceProxy>, 44 sk_sp<SkColorSpace>, 45 const SkSurfaceProps*, 46 bool managedOpList = true); 47 sk_sp<GrTextureContext> makeTextureContext(sk_sp<GrSurfaceProxy>, sk_sp<SkColorSpace>); 48 49 // The caller automatically gets a ref on the returned opList. It must 50 // be balanced by an unref call. 51 // A managed opList is controlled by the drawing manager (i.e., sorted & flushed with the 52 // other). An unmanaged one is created and used by the onFlushCallback. 53 sk_sp<GrRenderTargetOpList> newRTOpList(GrRenderTargetProxy* rtp, bool managedOpList); 54 sk_sp<GrTextureOpList> newTextureOpList(GrTextureProxy* textureProxy); 55 getContext()56 GrContext* getContext() { return fContext; } 57 58 GrAtlasTextContext* getAtlasTextContext(); 59 60 GrPathRenderer* getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args, 61 bool allowSW, 62 GrPathRendererChain::DrawType drawType, 63 GrPathRenderer::StencilSupport* stencilSupport = NULL); 64 flushIfNecessary()65 void flushIfNecessary() { 66 if (fContext->getResourceCache()->requestsFlush()) { 67 this->internalFlush(nullptr, GrResourceCache::kCacheRequested); 68 } 69 } 70 71 static bool ProgramUnitTest(GrContext* context, int maxStages, int maxLevels); 72 73 void prepareSurfaceForExternalIO(GrSurfaceProxy*); 74 75 void addOnFlushCallbackObject(GrOnFlushCallbackObject*); 76 void testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject*); 77 78 private: GrDrawingManager(GrContext * context,const GrPathRendererChain::Options & optionsForPathRendererChain,GrSingleOwner * singleOwner)79 GrDrawingManager(GrContext* context, 80 const GrPathRendererChain::Options& optionsForPathRendererChain, 81 GrSingleOwner* singleOwner) 82 : fContext(context) 83 , fOptionsForPathRendererChain(optionsForPathRendererChain) 84 , fSingleOwner(singleOwner) 85 , fAbandoned(false) 86 , fAtlasTextContext(nullptr) 87 , fPathRendererChain(nullptr) 88 , fSoftwarePathRenderer(nullptr) 89 , fFlushState(context->getGpu(), context->resourceProvider()) 90 , fFlushing(false) { 91 } 92 93 void abandon(); 94 void cleanup(); 95 void reset(); flush(GrSurfaceProxy * proxy)96 void flush(GrSurfaceProxy* proxy) { 97 this->internalFlush(proxy, GrResourceCache::FlushType::kExternal); 98 } 99 void internalFlush(GrSurfaceProxy*, GrResourceCache::FlushType); 100 101 friend class GrContext; // for access to: ctor, abandon, reset & flush 102 friend class GrContextPriv; // access to: flush 103 friend class GrOnFlushResourceProvider; // this is just a shallow wrapper around this class 104 105 static const int kNumPixelGeometries = 5; // The different pixel geometries 106 static const int kNumDFTOptions = 2; // DFT or no DFT 107 108 GrContext* fContext; 109 GrPathRendererChain::Options fOptionsForPathRendererChain; 110 111 // In debug builds we guard against improper thread handling 112 GrSingleOwner* fSingleOwner; 113 114 bool fAbandoned; 115 SkTArray<sk_sp<GrOpList>> fOpLists; 116 117 std::unique_ptr<GrAtlasTextContext> fAtlasTextContext; 118 119 GrPathRendererChain* fPathRendererChain; 120 GrSoftwarePathRenderer* fSoftwarePathRenderer; 121 122 GrOpFlushState fFlushState; 123 bool fFlushing; 124 125 SkTArray<GrOnFlushCallbackObject*> fOnFlushCBObjects; 126 127 // Lazily allocated 128 std::unique_ptr<gr_instanced::OpAllocator> fInstancingAllocator; 129 }; 130 131 #endif 132