blob: 80f006e4cd3ab237d5fd18c68535f2271f3011f0 [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;
Chris Dalton6b498102019-08-01 14:14:52 -060024class GrOpList;
Robert Phillips69893702019-02-22 11:16:30 -050025class GrRecordingContext;
Brian Osman11052242016-10-27 14:47:55 -040026class GrRenderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -040027class GrRenderTargetProxy;
Brian Salomon653f42f2018-07-10 10:07:31 -040028class GrRenderTargetOpList;
robertphillips68737822015-10-29 12:12:21 -070029class GrSoftwarePathRenderer;
Brian Osman45580d32016-11-23 09:37:01 -050030class GrTextureContext;
31class GrTextureOpList;
Brian Salomon653f42f2018-07-10 10:07:31 -040032class SkDeferredDisplayList;
robertphillips77a2e522015-10-17 07:43:27 -070033
robertphillips77a2e522015-10-17 07:43:27 -070034class GrDrawingManager {
35public:
36 ~GrDrawingManager();
37
robertphillips68737822015-10-29 12:12:21 -070038 void freeGpuResources();
robertphillips77a2e522015-10-17 07:43:27 -070039
Robert Phillips37430132016-11-09 06:50:43 -050040 sk_sp<GrRenderTargetContext> makeRenderTargetContext(sk_sp<GrSurfaceProxy>,
Brian Salomond6287472019-06-24 15:50:07 -040041 GrColorType,
Brian Osman11052242016-10-27 14:47:55 -040042 sk_sp<SkColorSpace>,
Robert Phillips941d1442017-06-14 16:37:02 -040043 const SkSurfaceProps*,
44 bool managedOpList = true);
Brian Salomone7499c72019-06-24 12:12:36 -040045 sk_sp<GrTextureContext> makeTextureContext(sk_sp<GrSurfaceProxy>,
Brian Salomond6287472019-06-24 15:50:07 -040046 GrColorType,
Brian Salomone7499c72019-06-24 12:12:36 -040047 SkAlphaType,
48 sk_sp<SkColorSpace>);
robertphillips77a2e522015-10-17 07:43:27 -070049
Robert Phillips941d1442017-06-14 16:37:02 -040050 // A managed opList is controlled by the drawing manager (i.e., sorted & flushed with the
Robert Phillips831a2932019-04-12 17:18:39 -040051 // others). An unmanaged one is created and used by the onFlushCallback.
52 sk_sp<GrRenderTargetOpList> newRTOpList(sk_sp<GrRenderTargetProxy>, bool managedOpList);
53 sk_sp<GrTextureOpList> newTextureOpList(sk_sp<GrTextureProxy>);
robertphillips77a2e522015-10-17 07:43:27 -070054
Chris Dalton3d770272019-08-14 09:24:37 -060055 // Create a new, specialized, render task that will regenerate mipmap levels and/or resolve
56 // MSAA (depending on GrTextureResolveFlags). This method will add the new render task to the
57 // list of render tasks and make it depend on the target texture proxy. It is up to the caller
58 // to add any dependencies on the new render task.
59 GrRenderTask* newTextureResolveRenderTask(
60 sk_sp<GrTextureProxy>, GrTextureResolveFlags, const GrCaps&);
61
Robert Phillips69893702019-02-22 11:16:30 -050062 GrRecordingContext* getContext() { return fContext; }
robertphillips77a2e522015-10-17 07:43:27 -070063
Herb Derby26cbe512018-05-24 14:39:01 -040064 GrTextContext* getTextContext();
brianosman86e76262016-08-11 12:17:31 -070065
robertphillips68737822015-10-29 12:12:21 -070066 GrPathRenderer* getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
67 bool allowSW,
68 GrPathRendererChain::DrawType drawType,
Ben Wagnera93a14a2017-08-28 10:34:05 -040069 GrPathRenderer::StencilSupport* stencilSupport = nullptr);
robertphillips68737822015-10-29 12:12:21 -070070
Brian Salomone7df0bb2018-05-07 14:44:57 -040071 GrPathRenderer* getSoftwarePathRenderer();
72
Chris Daltonfddb6c02017-11-04 15:22:22 -060073 // Returns a direct pointer to the coverage counting path renderer, or null if it is not
74 // supported and turned on.
75 GrCoverageCountingPathRenderer* getCoverageCountingPathRenderer();
76
Brian Salomon653f42f2018-07-10 10:07:31 -040077 void flushIfNecessary();
bsalomonb77a9072016-09-07 10:02:04 -070078
Greg Daniel78325c12017-06-19 16:39:13 -040079 static bool ProgramUnitTest(GrContext* context, int maxStages, int maxLevels);
robertphillipsa13e2022015-11-11 12:01:09 -080080
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040081 GrSemaphoresSubmitted flushSurfaces(GrSurfaceProxy* proxies[],
82 int cnt,
83 SkSurface::BackendSurfaceAccess access,
84 const GrFlushInfo& info);
85 GrSemaphoresSubmitted flushSurface(GrSurfaceProxy* proxy,
Greg Daniel4aa13e72019-04-15 14:42:20 -040086 SkSurface::BackendSurfaceAccess access,
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040087 const GrFlushInfo& info) {
88 return this->flushSurfaces(&proxy, 1, access, info);
89 }
bsalomon6a2b1942016-09-08 11:28:59 -070090
Chris Daltonfe199b72017-05-05 11:26:15 -040091 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
Robert Phillipsdbaf3172019-02-06 15:12:53 -050092
93#if GR_TEST_UTILS
Chris Daltonfe199b72017-05-05 11:26:15 -040094 void testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject*);
Robert Phillipsdbaf3172019-02-06 15:12:53 -050095#endif
Robert Phillipseb35f4d2017-03-21 07:56:47 -040096
Chris Dalton6b498102019-08-01 14:14:52 -060097 void moveRenderTasksToDDL(SkDeferredDisplayList* ddl);
98 void copyRenderTasksFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
Robert Phillips62000362018-02-01 09:10:04 -050099
robertphillips77a2e522015-10-17 07:43:27 -0700100private:
Chris Dalton6b498102019-08-01 14:14:52 -0600101 // This class encapsulates maintenance and manipulation of the drawing manager's DAG of
102 // renderTasks.
103 class RenderTaskDAG {
Robert Phillips22310d62018-09-05 11:07:21 -0400104 public:
Chris Dalton6b498102019-08-01 14:14:52 -0600105 RenderTaskDAG(bool sortRenderTasks);
106 ~RenderTaskDAG();
Robert Phillips22310d62018-09-05 11:07:21 -0400107
108 // Currently, when explicitly allocating resources, this call will topologically sort the
109 // opLists.
110 // MDB TODO: remove once incremental opList sorting is enabled
111 void prepForFlush();
112
113 void closeAll(const GrCaps* caps);
114
115 // A yucky combination of closeAll and reset
116 void cleanup(const GrCaps* caps);
117
118 void gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const;
119
120 void reset();
121
122 // These calls forceably remove an opList from the DAG. They are problematic bc they just
123 // remove the opList but don't cleanup any refering pointers (i.e., dependency pointers
124 // in the DAG). They work right now bc they are only called at flush time, after the
125 // topological sort is complete (so the dangling pointers aren't used).
Chris Dalton6b498102019-08-01 14:14:52 -0600126 void removeRenderTask(int index);
127 void removeRenderTasks(int startIndex, int stopIndex);
Robert Phillips22310d62018-09-05 11:07:21 -0400128
Chris Dalton6b498102019-08-01 14:14:52 -0600129 bool empty() const { return fRenderTasks.empty(); }
130 int numRenderTasks() const { return fRenderTasks.count(); }
Robert Phillips22310d62018-09-05 11:07:21 -0400131
Robert Phillips9313aa72019-04-09 18:41:27 -0400132 bool isUsed(GrSurfaceProxy*) const;
133
Chris Dalton6b498102019-08-01 14:14:52 -0600134 GrRenderTask* renderTask(int index) { return fRenderTasks[index].get(); }
135 const GrRenderTask* renderTask(int index) const { return fRenderTasks[index].get(); }
Robert Phillips22310d62018-09-05 11:07:21 -0400136
Chris Dalton6b498102019-08-01 14:14:52 -0600137 GrRenderTask* back() { return fRenderTasks.back().get(); }
138 const GrRenderTask* back() const { return fRenderTasks.back().get(); }
Robert Phillips22310d62018-09-05 11:07:21 -0400139
Chris Dalton3d770272019-08-14 09:24:37 -0600140 GrRenderTask* add(sk_sp<GrRenderTask>);
141 GrRenderTask* addBeforeLast(sk_sp<GrRenderTask>);
Chris Dalton6b498102019-08-01 14:14:52 -0600142 void add(const SkTArray<sk_sp<GrRenderTask>>&);
Robert Phillips22310d62018-09-05 11:07:21 -0400143
Chris Dalton6b498102019-08-01 14:14:52 -0600144 void swap(SkTArray<sk_sp<GrRenderTask>>* renderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400145
Chris Dalton6b498102019-08-01 14:14:52 -0600146 bool sortingRenderTasks() const { return fSortRenderTasks; }
Robert Phillips46acf9d2018-10-09 09:31:40 -0400147
Robert Phillips22310d62018-09-05 11:07:21 -0400148 private:
Chris Dalton6b498102019-08-01 14:14:52 -0600149 SkTArray<sk_sp<GrRenderTask>> fRenderTasks;
150 bool fSortRenderTasks;
Robert Phillips22310d62018-09-05 11:07:21 -0400151 };
152
Robert Phillips69893702019-02-22 11:16:30 -0500153 GrDrawingManager(GrRecordingContext*, const GrPathRendererChain::Options&,
Robert Phillips0d075de2019-03-04 11:08:13 -0500154 const GrTextContext::Options&,
Chris Dalton6b498102019-08-01 14:14:52 -0600155 bool sortRenderTasks,
Robert Phillips6db27c22019-05-01 10:43:56 -0400156 bool reduceOpListSplitting);
robertphillips77a2e522015-10-17 07:43:27 -0700157
Robert Phillipsa9162df2019-02-11 14:12:03 -0500158 bool wasAbandoned() const;
159
robertphillips77a2e522015-10-17 07:43:27 -0700160 void cleanup();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500161
Chris Dalton5fe99772019-08-06 11:57:39 -0600162 // Closes the target's dependent render tasks (or, if not in sorting/opList-splitting-reduction
163 // mode, closes fActiveOpList) in preparation for us opening a new opList that will write to
164 // 'target'.
165 void closeRenderTasksForNewOpList(GrSurfaceProxy* target);
166
Robert Phillipseafd48a2017-11-16 07:52:08 -0500167 // return true if any opLists were actually executed; false otherwise
Chris Dalton6b498102019-08-01 14:14:52 -0600168 bool executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState*,
169 int* numRenderTasksExecuted);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500170
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400171 GrSemaphoresSubmitted flush(GrSurfaceProxy* proxies[],
172 int numProxies,
Greg Danielbae71212019-03-01 15:24:35 -0500173 SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -0400174 const GrFlushInfo&,
175 const GrPrepareForExternalIORequests&);
robertphillips77a2e522015-10-17 07:43:27 -0700176
Robert Phillips38d64b02018-09-04 13:23:26 -0400177 SkDEBUGCODE(void validate() const);
178
Robert Phillips69893702019-02-22 11:16:30 -0500179 friend class GrContext; // access to: flush & cleanup
Robert Phillips7ee385e2017-03-30 08:02:11 -0400180 friend class GrContextPriv; // access to: flush
Chris Daltonfe199b72017-05-05 11:26:15 -0400181 friend class GrOnFlushResourceProvider; // this is just a shallow wrapper around this class
Robert Phillips69893702019-02-22 11:16:30 -0500182 friend class GrRecordingContext; // access to: ctor
Greg Danielb6c15ba2019-03-04 13:08:25 -0500183 friend class SkImage; // for access to: flush
robertphillips77a2e522015-10-17 07:43:27 -0700184
185 static const int kNumPixelGeometries = 5; // The different pixel geometries
186 static const int kNumDFTOptions = 2; // DFT or no DFT
187
Robert Phillips69893702019-02-22 11:16:30 -0500188 GrRecordingContext* fContext;
bsalomon6b2552f2016-09-15 13:50:26 -0700189 GrPathRendererChain::Options fOptionsForPathRendererChain;
Herb Derby26cbe512018-05-24 14:39:01 -0400190 GrTextContext::Options fOptionsForTextContext;
Brian Salomon601ac802019-02-07 13:37:16 -0500191 // This cache is used by both the vertex and index pools. It reuses memory across multiple
192 // flushes.
193 sk_sp<GrBufferAllocPool::CpuBufferCache> fCpuBufferCache;
joshualittde8dc7e2016-01-08 10:09:13 -0800194
Chris Dalton6b498102019-08-01 14:14:52 -0600195 RenderTaskDAG fDAG;
Robert Phillips38d64b02018-09-04 13:23:26 -0400196 GrOpList* fActiveOpList = nullptr;
Chris Dalton3968ff92017-11-27 12:26:31 -0700197 // These are the IDs of the opLists currently being flushed (in internalFlush)
Chris Dalton6b498102019-08-01 14:14:52 -0600198 SkSTArray<8, uint32_t, true> fFlushingRenderTaskIDs;
Chris Dalton3968ff92017-11-27 12:26:31 -0700199 // These are the new opLists generated by the onFlush CBs
200 SkSTArray<8, sk_sp<GrOpList>> fOnFlushCBOpLists;
robertphillips77a2e522015-10-17 07:43:27 -0700201
Herb Derby26cbe512018-05-24 14:39:01 -0400202 std::unique_ptr<GrTextContext> fTextContext;
robertphillipsa13e2022015-11-11 12:01:09 -0800203
Ben Wagner9ec70c62018-07-12 13:30:47 -0400204 std::unique_ptr<GrPathRendererChain> fPathRendererChain;
205 sk_sp<GrSoftwarePathRenderer> fSoftwarePathRenderer;
brianosman86e76262016-08-11 12:17:31 -0700206
Robert Phillips40a29d72018-01-18 12:59:22 -0500207 GrTokenTracker fTokenTracker;
brianosman86e76262016-08-11 12:17:31 -0700208 bool fFlushing;
Robert Phillips46acf9d2018-10-09 09:31:40 -0400209 bool fReduceOpListSplitting;
bsalomonb77a9072016-09-07 10:02:04 -0700210
Chris Daltonfe199b72017-05-05 11:26:15 -0400211 SkTArray<GrOnFlushCallbackObject*> fOnFlushCBObjects;
Robert Phillips15c91422019-05-07 16:54:48 -0400212
213 void addDDLTarget(GrSurfaceProxy* proxy) { fDDLTargets.insert(proxy); }
214 bool isDDLTarget(GrSurfaceProxy* proxy) { return fDDLTargets.find(proxy) != fDDLTargets.end(); }
215 void clearDDLTargets() { fDDLTargets.clear(); }
216
217 // We play a trick with lazy proxies to retarget the base target of a DDL to the SkSurface
218 // it is replayed on. Because of this remapping we need to explicitly store the targets of
219 // DDL replaying.
220 // Note: we do not expect a whole lot of these per flush
221 std::set<GrSurfaceProxy*> fDDLTargets;
robertphillips77a2e522015-10-17 07:43:27 -0700222};
223
224#endif