blob: 752973afc9cb8cc70b76112329fcd5a32ce2aa3b [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;
Greg Danielf41b2bd2019-08-22 16:19:24 -040024class GrOpsTask;
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;
robertphillips68737822015-10-29 12:12:21 -070028class GrSoftwarePathRenderer;
Greg Daniel46e366a2019-12-16 14:38:36 -050029class GrSurfaceContext;
Greg Daniel16f5c652019-10-29 11:26:01 -040030class GrSurfaceProxyView;
Chris Daltone2a903e2019-09-18 13:41:50 -060031class GrTextureResolveRenderTask;
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
Greg Danielf41b2bd2019-08-22 16:19:24 -040040 // A managed opsTask is controlled by the drawing manager (i.e., sorted & flushed with the
Robert Phillips831a2932019-04-12 17:18:39 -040041 // others). An unmanaged one is created and used by the onFlushCallback.
Greg Daniel16f5c652019-10-29 11:26:01 -040042 sk_sp<GrOpsTask> newOpsTask(GrSurfaceProxyView, bool managedOpsTask);
robertphillips77a2e522015-10-17 07:43:27 -070043
Chris Daltone2a903e2019-09-18 13:41:50 -060044 // Create a render task that can resolve MSAA and/or regenerate mipmap levels on proxies. This
45 // method will only add the new render task to the list. It is up to the caller to call
46 // addProxy() on the returned object.
47 GrTextureResolveRenderTask* newTextureResolveRenderTask(const GrCaps&);
Chris Dalton3d770272019-08-14 09:24:37 -060048
Greg Danielc30f1a92019-09-06 15:28:58 -040049 // Create a new render task that will cause the gpu to wait on semaphores before executing any
50 // more RenderTasks that target proxy. It is possible for this wait to also block additional
51 // work (even to other proxies) that has already been recorded or will be recorded later. The
52 // only guarantee is that future work to the passed in proxy will wait on the semaphores to be
53 // signaled.
Greg Daniel301015c2019-11-18 14:06:46 -050054 void newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
55 std::unique_ptr<std::unique_ptr<GrSemaphore>[]>,
Greg Danielc30f1a92019-09-06 15:28:58 -040056 int numSemaphores);
57
Greg Danielbbfec9d2019-08-20 10:56:51 -040058 // Create a new render task which copies the pixels from the srcProxy into the dstBuffer. This
59 // is used to support the asynchronous readback API. The srcRect is the region of the srcProxy
60 // to be copied. The surfaceColorType says how we should interpret the data when reading back
61 // from the source. DstColorType describes how the data should be stored in the dstBuffer.
62 // DstOffset is the offset into the dstBuffer where we will start writing data.
63 void newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy, const SkIRect& srcRect,
64 GrColorType surfaceColorType, GrColorType dstColorType,
65 sk_sp<GrGpuBuffer> dstBuffer, size_t dstOffset);
66
Greg Daniel16f5c652019-10-29 11:26:01 -040067 // Creates a new render task which copies a pixel rectangle from srcView into dstView. The src
Greg Daniele227fe42019-08-21 13:52:24 -040068 // pixels copied are specified by srcRect. They are copied to a rect of the same size in
69 // dstProxy with top left at dstPoint. If the src rect is clipped by the src bounds then pixel
70 // values in the dst rect corresponding to the area clipped by the src rect are not overwritten.
71 // This method is not guaranteed to succeed depending on the type of surface, formats, etc, and
72 // the backend-specific limitations.
Greg Daniel16f5c652019-10-29 11:26:01 -040073 bool newCopyRenderTask(GrSurfaceProxyView srcView, const SkIRect& srcRect,
74 GrSurfaceProxyView dstView, const SkIPoint& dstPoint);
Greg Daniele227fe42019-08-21 13:52:24 -040075
Robert Phillips69893702019-02-22 11:16:30 -050076 GrRecordingContext* getContext() { return fContext; }
robertphillips77a2e522015-10-17 07:43:27 -070077
Herb Derby26cbe512018-05-24 14:39:01 -040078 GrTextContext* getTextContext();
brianosman86e76262016-08-11 12:17:31 -070079
robertphillips68737822015-10-29 12:12:21 -070080 GrPathRenderer* getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
81 bool allowSW,
82 GrPathRendererChain::DrawType drawType,
Ben Wagnera93a14a2017-08-28 10:34:05 -040083 GrPathRenderer::StencilSupport* stencilSupport = nullptr);
robertphillips68737822015-10-29 12:12:21 -070084
Brian Salomone7df0bb2018-05-07 14:44:57 -040085 GrPathRenderer* getSoftwarePathRenderer();
86
Chris Daltonfddb6c02017-11-04 15:22:22 -060087 // Returns a direct pointer to the coverage counting path renderer, or null if it is not
88 // supported and turned on.
89 GrCoverageCountingPathRenderer* getCoverageCountingPathRenderer();
90
Brian Salomon653f42f2018-07-10 10:07:31 -040091 void flushIfNecessary();
bsalomonb77a9072016-09-07 10:02:04 -070092
Greg Daniel78325c12017-06-19 16:39:13 -040093 static bool ProgramUnitTest(GrContext* context, int maxStages, int maxLevels);
robertphillipsa13e2022015-11-11 12:01:09 -080094
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040095 GrSemaphoresSubmitted flushSurfaces(GrSurfaceProxy* proxies[],
Greg Danielec340812020-02-12 15:27:49 -050096 GrSurfaceOrigin origins[],
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040097 int cnt,
98 SkSurface::BackendSurfaceAccess access,
99 const GrFlushInfo& info);
Greg Danielec340812020-02-12 15:27:49 -0500100 GrSemaphoresSubmitted flushSurface(GrSurfaceProxy* proxy, GrSurfaceOrigin origin,
Greg Daniel4aa13e72019-04-15 14:42:20 -0400101 SkSurface::BackendSurfaceAccess access,
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400102 const GrFlushInfo& info) {
Greg Danielec340812020-02-12 15:27:49 -0500103 return this->flushSurfaces(&proxy, &origin, 1, access, info);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400104 }
bsalomon6a2b1942016-09-08 11:28:59 -0700105
Chris Daltonfe199b72017-05-05 11:26:15 -0400106 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500107
108#if GR_TEST_UTILS
Chris Daltonfe199b72017-05-05 11:26:15 -0400109 void testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject*);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500110#endif
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400111
Chris Dalton6b498102019-08-01 14:14:52 -0600112 void moveRenderTasksToDDL(SkDeferredDisplayList* ddl);
113 void copyRenderTasksFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
Robert Phillips62000362018-02-01 09:10:04 -0500114
robertphillips77a2e522015-10-17 07:43:27 -0700115private:
Chris Dalton6b498102019-08-01 14:14:52 -0600116 // This class encapsulates maintenance and manipulation of the drawing manager's DAG of
117 // renderTasks.
118 class RenderTaskDAG {
Robert Phillips22310d62018-09-05 11:07:21 -0400119 public:
Chris Dalton6b498102019-08-01 14:14:52 -0600120 RenderTaskDAG(bool sortRenderTasks);
121 ~RenderTaskDAG();
Robert Phillips22310d62018-09-05 11:07:21 -0400122
123 // Currently, when explicitly allocating resources, this call will topologically sort the
Greg Danielf41b2bd2019-08-22 16:19:24 -0400124 // GrRenderTasks.
125 // MDB TODO: remove once incremental GrRenderTask sorting is enabled
Robert Phillips22310d62018-09-05 11:07:21 -0400126 void prepForFlush();
127
128 void closeAll(const GrCaps* caps);
129
130 // A yucky combination of closeAll and reset
131 void cleanup(const GrCaps* caps);
132
133 void gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const;
134
135 void reset();
136
Greg Danielf41b2bd2019-08-22 16:19:24 -0400137 // These calls forceably remove a GrRenderTask from the DAG. They are problematic bc they
138 // just remove the GrRenderTask but don't cleanup any refering pointers (i.e., dependency
139 // pointers in the DAG). They work right now bc they are only called at flush time, after
140 // the topological sort is complete (so the dangling pointers aren't used).
Chris Dalton6b498102019-08-01 14:14:52 -0600141 void removeRenderTask(int index);
142 void removeRenderTasks(int startIndex, int stopIndex);
Robert Phillips22310d62018-09-05 11:07:21 -0400143
Chris Dalton6b498102019-08-01 14:14:52 -0600144 bool empty() const { return fRenderTasks.empty(); }
145 int numRenderTasks() const { return fRenderTasks.count(); }
Robert Phillips22310d62018-09-05 11:07:21 -0400146
Robert Phillips9313aa72019-04-09 18:41:27 -0400147 bool isUsed(GrSurfaceProxy*) const;
148
Chris Dalton6b498102019-08-01 14:14:52 -0600149 GrRenderTask* renderTask(int index) { return fRenderTasks[index].get(); }
150 const GrRenderTask* renderTask(int index) const { return fRenderTasks[index].get(); }
Robert Phillips22310d62018-09-05 11:07:21 -0400151
Chris Dalton6b498102019-08-01 14:14:52 -0600152 GrRenderTask* back() { return fRenderTasks.back().get(); }
153 const GrRenderTask* back() const { return fRenderTasks.back().get(); }
Robert Phillips22310d62018-09-05 11:07:21 -0400154
Chris Dalton3d770272019-08-14 09:24:37 -0600155 GrRenderTask* add(sk_sp<GrRenderTask>);
156 GrRenderTask* addBeforeLast(sk_sp<GrRenderTask>);
Chris Dalton6b498102019-08-01 14:14:52 -0600157 void add(const SkTArray<sk_sp<GrRenderTask>>&);
Robert Phillips22310d62018-09-05 11:07:21 -0400158
Chris Dalton6b498102019-08-01 14:14:52 -0600159 void swap(SkTArray<sk_sp<GrRenderTask>>* renderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400160
Chris Dalton6b498102019-08-01 14:14:52 -0600161 bool sortingRenderTasks() const { return fSortRenderTasks; }
Robert Phillips46acf9d2018-10-09 09:31:40 -0400162
Robert Phillips22310d62018-09-05 11:07:21 -0400163 private:
Chris Dalton6b498102019-08-01 14:14:52 -0600164 SkTArray<sk_sp<GrRenderTask>> fRenderTasks;
165 bool fSortRenderTasks;
Robert Phillips22310d62018-09-05 11:07:21 -0400166 };
167
Robert Phillips69893702019-02-22 11:16:30 -0500168 GrDrawingManager(GrRecordingContext*, const GrPathRendererChain::Options&,
Robert Phillips0d075de2019-03-04 11:08:13 -0500169 const GrTextContext::Options&,
Chris Dalton6b498102019-08-01 14:14:52 -0600170 bool sortRenderTasks,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400171 bool reduceOpsTaskSplitting);
robertphillips77a2e522015-10-17 07:43:27 -0700172
Robert Phillipsa9162df2019-02-11 14:12:03 -0500173 bool wasAbandoned() const;
174
robertphillips77a2e522015-10-17 07:43:27 -0700175 void cleanup();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500176
Greg Danielf41b2bd2019-08-22 16:19:24 -0400177 // Closes the target's dependent render tasks (or, if not in sorting/opsTask-splitting-reduction
178 // mode, closes fActiveOpsTask) in preparation for us opening a new opsTask that will write to
Chris Dalton5fe99772019-08-06 11:57:39 -0600179 // 'target'.
Greg Danielbbfec9d2019-08-20 10:56:51 -0400180 void closeRenderTasksForNewRenderTask(GrSurfaceProxy* target);
Chris Dalton5fe99772019-08-06 11:57:39 -0600181
Greg Danielf41b2bd2019-08-22 16:19:24 -0400182 // return true if any GrRenderTasks were actually executed; false otherwise
Chris Dalton6b498102019-08-01 14:14:52 -0600183 bool executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState*,
184 int* numRenderTasksExecuted);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500185
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400186 GrSemaphoresSubmitted flush(GrSurfaceProxy* proxies[],
187 int numProxies,
Greg Danielbae71212019-03-01 15:24:35 -0500188 SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -0400189 const GrFlushInfo&,
190 const GrPrepareForExternalIORequests&);
robertphillips77a2e522015-10-17 07:43:27 -0700191
Robert Phillips38d64b02018-09-04 13:23:26 -0400192 SkDEBUGCODE(void validate() const);
193
Robert Phillips69893702019-02-22 11:16:30 -0500194 friend class GrContext; // access to: flush & cleanup
Robert Phillips7ee385e2017-03-30 08:02:11 -0400195 friend class GrContextPriv; // access to: flush
Chris Daltonfe199b72017-05-05 11:26:15 -0400196 friend class GrOnFlushResourceProvider; // this is just a shallow wrapper around this class
Robert Phillips69893702019-02-22 11:16:30 -0500197 friend class GrRecordingContext; // access to: ctor
Greg Danielb6c15ba2019-03-04 13:08:25 -0500198 friend class SkImage; // for access to: flush
robertphillips77a2e522015-10-17 07:43:27 -0700199
200 static const int kNumPixelGeometries = 5; // The different pixel geometries
201 static const int kNumDFTOptions = 2; // DFT or no DFT
202
Robert Phillips69893702019-02-22 11:16:30 -0500203 GrRecordingContext* fContext;
bsalomon6b2552f2016-09-15 13:50:26 -0700204 GrPathRendererChain::Options fOptionsForPathRendererChain;
Herb Derby26cbe512018-05-24 14:39:01 -0400205 GrTextContext::Options fOptionsForTextContext;
Brian Salomon601ac802019-02-07 13:37:16 -0500206 // This cache is used by both the vertex and index pools. It reuses memory across multiple
207 // flushes.
208 sk_sp<GrBufferAllocPool::CpuBufferCache> fCpuBufferCache;
joshualittde8dc7e2016-01-08 10:09:13 -0800209
Chris Dalton6b498102019-08-01 14:14:52 -0600210 RenderTaskDAG fDAG;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400211 GrOpsTask* fActiveOpsTask = nullptr;
212 // These are the IDs of the opsTask currently being flushed (in internalFlush)
Chris Dalton6b498102019-08-01 14:14:52 -0600213 SkSTArray<8, uint32_t, true> fFlushingRenderTaskIDs;
Chris Daltonc4b47352019-08-23 10:10:36 -0600214 // These are the new renderTasks generated by the onFlush CBs
215 SkSTArray<4, sk_sp<GrRenderTask>> fOnFlushRenderTasks;
robertphillips77a2e522015-10-17 07:43:27 -0700216
Herb Derby26cbe512018-05-24 14:39:01 -0400217 std::unique_ptr<GrTextContext> fTextContext;
robertphillipsa13e2022015-11-11 12:01:09 -0800218
Ben Wagner9ec70c62018-07-12 13:30:47 -0400219 std::unique_ptr<GrPathRendererChain> fPathRendererChain;
220 sk_sp<GrSoftwarePathRenderer> fSoftwarePathRenderer;
brianosman86e76262016-08-11 12:17:31 -0700221
Robert Phillips40a29d72018-01-18 12:59:22 -0500222 GrTokenTracker fTokenTracker;
brianosman86e76262016-08-11 12:17:31 -0700223 bool fFlushing;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400224 bool fReduceOpsTaskSplitting;
bsalomonb77a9072016-09-07 10:02:04 -0700225
Chris Daltonfe199b72017-05-05 11:26:15 -0400226 SkTArray<GrOnFlushCallbackObject*> fOnFlushCBObjects;
Robert Phillips15c91422019-05-07 16:54:48 -0400227
228 void addDDLTarget(GrSurfaceProxy* proxy) { fDDLTargets.insert(proxy); }
229 bool isDDLTarget(GrSurfaceProxy* proxy) { return fDDLTargets.find(proxy) != fDDLTargets.end(); }
230 void clearDDLTargets() { fDDLTargets.clear(); }
231
232 // We play a trick with lazy proxies to retarget the base target of a DDL to the SkSurface
233 // it is replayed on. Because of this remapping we need to explicitly store the targets of
234 // DDL replaying.
235 // Note: we do not expect a whole lot of these per flush
236 std::set<GrSurfaceProxy*> fDDLTargets;
robertphillips77a2e522015-10-17 07:43:27 -0700237};
238
239#endif