GrDrawingManager.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #ifndef GrDrawingManager_DEFINED
  8. #define GrDrawingManager_DEFINED
  9. #include <set>
  10. #include "include/core/SkSurface.h"
  11. #include "include/private/SkTArray.h"
  12. #include "src/gpu/GrBufferAllocPool.h"
  13. #include "src/gpu/GrDeferredUpload.h"
  14. #include "src/gpu/GrPathRenderer.h"
  15. #include "src/gpu/GrPathRendererChain.h"
  16. #include "src/gpu/GrResourceCache.h"
  17. #include "src/gpu/text/GrTextContext.h"
  18. class GrCoverageCountingPathRenderer;
  19. class GrOnFlushCallbackObject;
  20. class GrOpFlushState;
  21. class GrRecordingContext;
  22. class GrRenderTargetContext;
  23. class GrRenderTargetProxy;
  24. class GrRenderTargetOpList;
  25. class GrSoftwarePathRenderer;
  26. class GrTextureContext;
  27. class GrTextureOpList;
  28. class SkDeferredDisplayList;
  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. void freeGpuResources();
  38. sk_sp<GrRenderTargetContext> makeRenderTargetContext(sk_sp<GrSurfaceProxy>,
  39. GrColorType,
  40. sk_sp<SkColorSpace>,
  41. const SkSurfaceProps*,
  42. bool managedOpList = true);
  43. sk_sp<GrTextureContext> makeTextureContext(sk_sp<GrSurfaceProxy>,
  44. GrColorType,
  45. SkAlphaType,
  46. sk_sp<SkColorSpace>);
  47. // A managed opList is controlled by the drawing manager (i.e., sorted & flushed with the
  48. // others). An unmanaged one is created and used by the onFlushCallback.
  49. sk_sp<GrRenderTargetOpList> newRTOpList(sk_sp<GrRenderTargetProxy>, bool managedOpList);
  50. sk_sp<GrTextureOpList> newTextureOpList(sk_sp<GrTextureProxy>);
  51. GrRecordingContext* getContext() { return fContext; }
  52. GrTextContext* getTextContext();
  53. GrPathRenderer* getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
  54. bool allowSW,
  55. GrPathRendererChain::DrawType drawType,
  56. GrPathRenderer::StencilSupport* stencilSupport = nullptr);
  57. GrPathRenderer* getSoftwarePathRenderer();
  58. // Returns a direct pointer to the coverage counting path renderer, or null if it is not
  59. // supported and turned on.
  60. GrCoverageCountingPathRenderer* getCoverageCountingPathRenderer();
  61. void flushIfNecessary();
  62. static bool ProgramUnitTest(GrContext* context, int maxStages, int maxLevels);
  63. GrSemaphoresSubmitted flushSurfaces(GrSurfaceProxy* proxies[],
  64. int cnt,
  65. SkSurface::BackendSurfaceAccess access,
  66. const GrFlushInfo& info);
  67. GrSemaphoresSubmitted flushSurface(GrSurfaceProxy* proxy,
  68. SkSurface::BackendSurfaceAccess access,
  69. const GrFlushInfo& info) {
  70. return this->flushSurfaces(&proxy, 1, access, info);
  71. }
  72. void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
  73. #if GR_TEST_UTILS
  74. void testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject*);
  75. #endif
  76. void moveOpListsToDDL(SkDeferredDisplayList* ddl);
  77. void copyOpListsFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
  78. private:
  79. // This class encapsulates maintenance and manipulation of the drawing manager's DAG of opLists.
  80. class OpListDAG {
  81. public:
  82. OpListDAG(bool sortOpLists);
  83. ~OpListDAG();
  84. // Currently, when explicitly allocating resources, this call will topologically sort the
  85. // opLists.
  86. // MDB TODO: remove once incremental opList sorting is enabled
  87. void prepForFlush();
  88. void closeAll(const GrCaps* caps);
  89. // A yucky combination of closeAll and reset
  90. void cleanup(const GrCaps* caps);
  91. void gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const;
  92. void reset();
  93. // These calls forceably remove an opList from the DAG. They are problematic bc they just
  94. // remove the opList but don't cleanup any refering pointers (i.e., dependency pointers
  95. // in the DAG). They work right now bc they are only called at flush time, after the
  96. // topological sort is complete (so the dangling pointers aren't used).
  97. void removeOpList(int index);
  98. void removeOpLists(int startIndex, int stopIndex);
  99. bool empty() const { return fOpLists.empty(); }
  100. int numOpLists() const { return fOpLists.count(); }
  101. bool isUsed(GrSurfaceProxy*) const;
  102. GrOpList* opList(int index) { return fOpLists[index].get(); }
  103. const GrOpList* opList(int index) const { return fOpLists[index].get(); }
  104. GrOpList* back() { return fOpLists.back().get(); }
  105. const GrOpList* back() const { return fOpLists.back().get(); }
  106. void add(sk_sp<GrOpList>);
  107. void add(const SkTArray<sk_sp<GrOpList>>&);
  108. void swap(SkTArray<sk_sp<GrOpList>>* opLists);
  109. bool sortingOpLists() const { return fSortOpLists; }
  110. private:
  111. SkTArray<sk_sp<GrOpList>> fOpLists;
  112. bool fSortOpLists;
  113. };
  114. GrDrawingManager(GrRecordingContext*, const GrPathRendererChain::Options&,
  115. const GrTextContext::Options&,
  116. bool sortOpLists,
  117. bool reduceOpListSplitting);
  118. bool wasAbandoned() const;
  119. void cleanup();
  120. // return true if any opLists were actually executed; false otherwise
  121. bool executeOpLists(int startIndex, int stopIndex, GrOpFlushState*, int* numOpListsExecuted);
  122. GrSemaphoresSubmitted flush(GrSurfaceProxy* proxies[],
  123. int numProxies,
  124. SkSurface::BackendSurfaceAccess access,
  125. const GrFlushInfo&,
  126. const GrPrepareForExternalIORequests&);
  127. SkDEBUGCODE(void validate() const);
  128. friend class GrContext; // access to: flush & cleanup
  129. friend class GrContextPriv; // access to: flush
  130. friend class GrOnFlushResourceProvider; // this is just a shallow wrapper around this class
  131. friend class GrRecordingContext; // access to: ctor
  132. friend class SkImage; // for access to: flush
  133. static const int kNumPixelGeometries = 5; // The different pixel geometries
  134. static const int kNumDFTOptions = 2; // DFT or no DFT
  135. GrRecordingContext* fContext;
  136. GrPathRendererChain::Options fOptionsForPathRendererChain;
  137. GrTextContext::Options fOptionsForTextContext;
  138. // This cache is used by both the vertex and index pools. It reuses memory across multiple
  139. // flushes.
  140. sk_sp<GrBufferAllocPool::CpuBufferCache> fCpuBufferCache;
  141. OpListDAG fDAG;
  142. GrOpList* fActiveOpList = nullptr;
  143. // These are the IDs of the opLists currently being flushed (in internalFlush)
  144. SkSTArray<8, uint32_t, true> fFlushingOpListIDs;
  145. // These are the new opLists generated by the onFlush CBs
  146. SkSTArray<8, sk_sp<GrOpList>> fOnFlushCBOpLists;
  147. std::unique_ptr<GrTextContext> fTextContext;
  148. std::unique_ptr<GrPathRendererChain> fPathRendererChain;
  149. sk_sp<GrSoftwarePathRenderer> fSoftwarePathRenderer;
  150. GrTokenTracker fTokenTracker;
  151. bool fFlushing;
  152. bool fReduceOpListSplitting;
  153. SkTArray<GrOnFlushCallbackObject*> fOnFlushCBObjects;
  154. void addDDLTarget(GrSurfaceProxy* proxy) { fDDLTargets.insert(proxy); }
  155. bool isDDLTarget(GrSurfaceProxy* proxy) { return fDDLTargets.find(proxy) != fDDLTargets.end(); }
  156. void clearDDLTargets() { fDDLTargets.clear(); }
  157. // We play a trick with lazy proxies to retarget the base target of a DDL to the SkSurface
  158. // it is replayed on. Because of this remapping we need to explicitly store the targets of
  159. // DDL replaying.
  160. // Note: we do not expect a whole lot of these per flush
  161. std::set<GrSurfaceProxy*> fDDLTargets;
  162. };
  163. #endif