blob: 7a87ffc5eb585800ee711c88a8acd46229044f9c [file] [log] [blame]
robertphillips77a2e522015-10-17 07:43:27 -07001/*
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
Robert Phillips15c91422019-05-07 16:54:48 -040011#include <set>
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkSurface.h"
13#include "include/private/SkTArray.h"
14#include "src/gpu/GrBufferAllocPool.h"
15#include "src/gpu/GrDeferredUpload.h"
16#include "src/gpu/GrPathRenderer.h"
17#include "src/gpu/GrPathRendererChain.h"
18#include "src/gpu/GrResourceCache.h"
19#include "src/gpu/text/GrTextContext.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040020
Chris Daltonfddb6c02017-11-04 15:22:22 -060021class GrCoverageCountingPathRenderer;
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040022class GrOnFlushCallbackObject;
Herb Derbydc214c22018-11-08 13:31:39 -050023class GrOpFlushState;
Robert Phillips69893702019-02-22 11:16:30 -050024class GrRecordingContext;
Brian Osman11052242016-10-27 14:47:55 -040025class GrRenderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -040026class GrRenderTargetProxy;
Brian Salomon653f42f2018-07-10 10:07:31 -040027class GrRenderTargetOpList;
robertphillips68737822015-10-29 12:12:21 -070028class GrSoftwarePathRenderer;
Brian Osman45580d32016-11-23 09:37:01 -050029class GrTextureContext;
30class GrTextureOpList;
Brian Salomon653f42f2018-07-10 10:07:31 -040031class SkDeferredDisplayList;
robertphillips77a2e522015-10-17 07:43:27 -070032
Brian Osman11052242016-10-27 14:47:55 -040033// The GrDrawingManager allocates a new GrRenderTargetContext for each GrRenderTarget
Robert Phillipsf2361d22016-10-25 14:20:06 -040034// but all of them still land in the same GrOpList!
robertphillips77a2e522015-10-17 07:43:27 -070035//
Brian Osman11052242016-10-27 14:47:55 -040036// In the future this class will allocate a new GrRenderTargetContext for
Robert Phillipsf2361d22016-10-25 14:20:06 -040037// each GrRenderTarget/GrOpList and manage the DAG.
robertphillips77a2e522015-10-17 07:43:27 -070038class GrDrawingManager {
39public:
40 ~GrDrawingManager();
41
robertphillips68737822015-10-29 12:12:21 -070042 void freeGpuResources();
robertphillips77a2e522015-10-17 07:43:27 -070043
Robert Phillips37430132016-11-09 06:50:43 -050044 sk_sp<GrRenderTargetContext> makeRenderTargetContext(sk_sp<GrSurfaceProxy>,
Brian Salomond6287472019-06-24 15:50:07 -040045 GrColorType,
Brian Osman11052242016-10-27 14:47:55 -040046 sk_sp<SkColorSpace>,
Robert Phillips941d1442017-06-14 16:37:02 -040047 const SkSurfaceProps*,
48 bool managedOpList = true);
Brian Salomone7499c72019-06-24 12:12:36 -040049 sk_sp<GrTextureContext> makeTextureContext(sk_sp<GrSurfaceProxy>,
Brian Salomond6287472019-06-24 15:50:07 -040050 GrColorType,
Brian Salomone7499c72019-06-24 12:12:36 -040051 SkAlphaType,
52 sk_sp<SkColorSpace>);
robertphillips77a2e522015-10-17 07:43:27 -070053
Robert Phillips941d1442017-06-14 16:37:02 -040054 // A managed opList is controlled by the drawing manager (i.e., sorted & flushed with the
Robert Phillips831a2932019-04-12 17:18:39 -040055 // others). An unmanaged one is created and used by the onFlushCallback.
56 sk_sp<GrRenderTargetOpList> newRTOpList(sk_sp<GrRenderTargetProxy>, bool managedOpList);
57 sk_sp<GrTextureOpList> newTextureOpList(sk_sp<GrTextureProxy>);
robertphillips77a2e522015-10-17 07:43:27 -070058
Robert Phillips69893702019-02-22 11:16:30 -050059 GrRecordingContext* getContext() { return fContext; }
robertphillips77a2e522015-10-17 07:43:27 -070060
Herb Derby26cbe512018-05-24 14:39:01 -040061 GrTextContext* getTextContext();
brianosman86e76262016-08-11 12:17:31 -070062
robertphillips68737822015-10-29 12:12:21 -070063 GrPathRenderer* getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
64 bool allowSW,
65 GrPathRendererChain::DrawType drawType,
Ben Wagnera93a14a2017-08-28 10:34:05 -040066 GrPathRenderer::StencilSupport* stencilSupport = nullptr);
robertphillips68737822015-10-29 12:12:21 -070067
Brian Salomone7df0bb2018-05-07 14:44:57 -040068 GrPathRenderer* getSoftwarePathRenderer();
69
Chris Daltonfddb6c02017-11-04 15:22:22 -060070 // Returns a direct pointer to the coverage counting path renderer, or null if it is not
71 // supported and turned on.
72 GrCoverageCountingPathRenderer* getCoverageCountingPathRenderer();
73
Brian Salomon653f42f2018-07-10 10:07:31 -040074 void flushIfNecessary();
bsalomonb77a9072016-09-07 10:02:04 -070075
Greg Daniel78325c12017-06-19 16:39:13 -040076 static bool ProgramUnitTest(GrContext* context, int maxStages, int maxLevels);
robertphillipsa13e2022015-11-11 12:01:09 -080077
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040078 GrSemaphoresSubmitted flushSurfaces(GrSurfaceProxy* proxies[],
79 int cnt,
80 SkSurface::BackendSurfaceAccess access,
81 const GrFlushInfo& info);
82 GrSemaphoresSubmitted flushSurface(GrSurfaceProxy* proxy,
Greg Daniel4aa13e72019-04-15 14:42:20 -040083 SkSurface::BackendSurfaceAccess access,
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040084 const GrFlushInfo& info) {
85 return this->flushSurfaces(&proxy, 1, access, info);
86 }
bsalomon6a2b1942016-09-08 11:28:59 -070087
Chris Daltonfe199b72017-05-05 11:26:15 -040088 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
Robert Phillipsdbaf3172019-02-06 15:12:53 -050089
90#if GR_TEST_UTILS
Chris Daltonfe199b72017-05-05 11:26:15 -040091 void testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject*);
Robert Phillipsdbaf3172019-02-06 15:12:53 -050092#endif
Robert Phillipseb35f4d2017-03-21 07:56:47 -040093
Robert Phillips62000362018-02-01 09:10:04 -050094 void moveOpListsToDDL(SkDeferredDisplayList* ddl);
95 void copyOpListsFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
96
robertphillips77a2e522015-10-17 07:43:27 -070097private:
Robert Phillips22310d62018-09-05 11:07:21 -040098 // This class encapsulates maintenance and manipulation of the drawing manager's DAG of opLists.
99 class OpListDAG {
100 public:
Robert Phillips12c46292019-04-23 07:36:17 -0400101 OpListDAG(bool sortOpLists);
Robert Phillips22310d62018-09-05 11:07:21 -0400102 ~OpListDAG();
103
104 // Currently, when explicitly allocating resources, this call will topologically sort the
105 // opLists.
106 // MDB TODO: remove once incremental opList sorting is enabled
107 void prepForFlush();
108
109 void closeAll(const GrCaps* caps);
110
111 // A yucky combination of closeAll and reset
112 void cleanup(const GrCaps* caps);
113
114 void gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const;
115
116 void reset();
117
118 // These calls forceably remove an opList from the DAG. They are problematic bc they just
119 // remove the opList but don't cleanup any refering pointers (i.e., dependency pointers
120 // in the DAG). They work right now bc they are only called at flush time, after the
121 // topological sort is complete (so the dangling pointers aren't used).
122 void removeOpList(int index);
123 void removeOpLists(int startIndex, int stopIndex);
124
125 bool empty() const { return fOpLists.empty(); }
126 int numOpLists() const { return fOpLists.count(); }
127
Robert Phillips9313aa72019-04-09 18:41:27 -0400128 bool isUsed(GrSurfaceProxy*) const;
129
Robert Phillips22310d62018-09-05 11:07:21 -0400130 GrOpList* opList(int index) { return fOpLists[index].get(); }
131 const GrOpList* opList(int index) const { return fOpLists[index].get(); }
132
133 GrOpList* back() { return fOpLists.back().get(); }
134 const GrOpList* back() const { return fOpLists.back().get(); }
135
136 void add(sk_sp<GrOpList>);
137 void add(const SkTArray<sk_sp<GrOpList>>&);
138
139 void swap(SkTArray<sk_sp<GrOpList>>* opLists);
140
Robert Phillips46acf9d2018-10-09 09:31:40 -0400141 bool sortingOpLists() const { return fSortOpLists; }
142
Robert Phillips22310d62018-09-05 11:07:21 -0400143 private:
144 SkTArray<sk_sp<GrOpList>> fOpLists;
145 bool fSortOpLists;
146 };
147
Robert Phillips69893702019-02-22 11:16:30 -0500148 GrDrawingManager(GrRecordingContext*, const GrPathRendererChain::Options&,
Robert Phillips0d075de2019-03-04 11:08:13 -0500149 const GrTextContext::Options&,
Robert Phillips56181ba2019-03-08 12:00:45 -0500150 bool sortOpLists,
Robert Phillips6db27c22019-05-01 10:43:56 -0400151 bool reduceOpListSplitting);
robertphillips77a2e522015-10-17 07:43:27 -0700152
Robert Phillipsa9162df2019-02-11 14:12:03 -0500153 bool wasAbandoned() const;
154
robertphillips77a2e522015-10-17 07:43:27 -0700155 void cleanup();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500156
157 // return true if any opLists were actually executed; false otherwise
Greg Danield2073452018-12-07 11:20:33 -0500158 bool executeOpLists(int startIndex, int stopIndex, GrOpFlushState*, int* numOpListsExecuted);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500159
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400160 GrSemaphoresSubmitted flush(GrSurfaceProxy* proxies[],
161 int numProxies,
Greg Danielbae71212019-03-01 15:24:35 -0500162 SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -0400163 const GrFlushInfo&,
164 const GrPrepareForExternalIORequests&);
robertphillips77a2e522015-10-17 07:43:27 -0700165
Robert Phillips38d64b02018-09-04 13:23:26 -0400166 SkDEBUGCODE(void validate() const);
167
Robert Phillips69893702019-02-22 11:16:30 -0500168 friend class GrContext; // access to: flush & cleanup
Robert Phillips7ee385e2017-03-30 08:02:11 -0400169 friend class GrContextPriv; // access to: flush
Chris Daltonfe199b72017-05-05 11:26:15 -0400170 friend class GrOnFlushResourceProvider; // this is just a shallow wrapper around this class
Robert Phillips69893702019-02-22 11:16:30 -0500171 friend class GrRecordingContext; // access to: ctor
Greg Danielb6c15ba2019-03-04 13:08:25 -0500172 friend class SkImage; // for access to: flush
robertphillips77a2e522015-10-17 07:43:27 -0700173
174 static const int kNumPixelGeometries = 5; // The different pixel geometries
175 static const int kNumDFTOptions = 2; // DFT or no DFT
176
Robert Phillips69893702019-02-22 11:16:30 -0500177 GrRecordingContext* fContext;
bsalomon6b2552f2016-09-15 13:50:26 -0700178 GrPathRendererChain::Options fOptionsForPathRendererChain;
Herb Derby26cbe512018-05-24 14:39:01 -0400179 GrTextContext::Options fOptionsForTextContext;
Brian Salomon601ac802019-02-07 13:37:16 -0500180 // This cache is used by both the vertex and index pools. It reuses memory across multiple
181 // flushes.
182 sk_sp<GrBufferAllocPool::CpuBufferCache> fCpuBufferCache;
joshualittde8dc7e2016-01-08 10:09:13 -0800183
Robert Phillips22310d62018-09-05 11:07:21 -0400184 OpListDAG fDAG;
Robert Phillips38d64b02018-09-04 13:23:26 -0400185 GrOpList* fActiveOpList = nullptr;
Chris Dalton3968ff92017-11-27 12:26:31 -0700186 // These are the IDs of the opLists currently being flushed (in internalFlush)
187 SkSTArray<8, uint32_t, true> fFlushingOpListIDs;
188 // These are the new opLists generated by the onFlush CBs
189 SkSTArray<8, sk_sp<GrOpList>> fOnFlushCBOpLists;
robertphillips77a2e522015-10-17 07:43:27 -0700190
Herb Derby26cbe512018-05-24 14:39:01 -0400191 std::unique_ptr<GrTextContext> fTextContext;
robertphillipsa13e2022015-11-11 12:01:09 -0800192
Ben Wagner9ec70c62018-07-12 13:30:47 -0400193 std::unique_ptr<GrPathRendererChain> fPathRendererChain;
194 sk_sp<GrSoftwarePathRenderer> fSoftwarePathRenderer;
brianosman86e76262016-08-11 12:17:31 -0700195
Robert Phillips40a29d72018-01-18 12:59:22 -0500196 GrTokenTracker fTokenTracker;
brianosman86e76262016-08-11 12:17:31 -0700197 bool fFlushing;
Robert Phillips46acf9d2018-10-09 09:31:40 -0400198 bool fReduceOpListSplitting;
bsalomonb77a9072016-09-07 10:02:04 -0700199
Chris Daltonfe199b72017-05-05 11:26:15 -0400200 SkTArray<GrOnFlushCallbackObject*> fOnFlushCBObjects;
Robert Phillips15c91422019-05-07 16:54:48 -0400201
202 void addDDLTarget(GrSurfaceProxy* proxy) { fDDLTargets.insert(proxy); }
203 bool isDDLTarget(GrSurfaceProxy* proxy) { return fDDLTargets.find(proxy) != fDDLTargets.end(); }
204 void clearDDLTargets() { fDDLTargets.clear(); }
205
206 // We play a trick with lazy proxies to retarget the base target of a DDL to the SkSurface
207 // it is replayed on. Because of this remapping we need to explicitly store the targets of
208 // DDL replaying.
209 // Note: we do not expect a whole lot of these per flush
210 std::set<GrSurfaceProxy*> fDDLTargets;
robertphillips77a2e522015-10-17 07:43:27 -0700211};
212
213#endif