blob: fcb5466e18fbb784c0bcba0c4c9e3015e030ecdc [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
Robert Phillipsd81379d2020-04-21 10:39:02 -040021// Enabling this will print out which path renderers are being chosen
22#define GR_PATH_RENDERER_SPEW 0
23
Chris Daltonfddb6c02017-11-04 15:22:22 -060024class GrCoverageCountingPathRenderer;
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040025class GrOnFlushCallbackObject;
Herb Derbydc214c22018-11-08 13:31:39 -050026class GrOpFlushState;
Greg Danielf41b2bd2019-08-22 16:19:24 -040027class GrOpsTask;
Robert Phillips69893702019-02-22 11:16:30 -050028class GrRecordingContext;
Brian Osman11052242016-10-27 14:47:55 -040029class GrRenderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -040030class GrRenderTargetProxy;
robertphillips68737822015-10-29 12:12:21 -070031class GrSoftwarePathRenderer;
Greg Daniel46e366a2019-12-16 14:38:36 -050032class GrSurfaceContext;
Greg Daniel16f5c652019-10-29 11:26:01 -040033class GrSurfaceProxyView;
Chris Daltone2a903e2019-09-18 13:41:50 -060034class GrTextureResolveRenderTask;
Brian Salomon653f42f2018-07-10 10:07:31 -040035class SkDeferredDisplayList;
robertphillips77a2e522015-10-17 07:43:27 -070036
robertphillips77a2e522015-10-17 07:43:27 -070037class GrDrawingManager {
38public:
39 ~GrDrawingManager();
40
robertphillips68737822015-10-29 12:12:21 -070041 void freeGpuResources();
robertphillips77a2e522015-10-17 07:43:27 -070042
Greg Danielf41b2bd2019-08-22 16:19:24 -040043 // A managed opsTask is controlled by the drawing manager (i.e., sorted & flushed with the
Robert Phillips831a2932019-04-12 17:18:39 -040044 // others). An unmanaged one is created and used by the onFlushCallback.
Greg Daniel16f5c652019-10-29 11:26:01 -040045 sk_sp<GrOpsTask> newOpsTask(GrSurfaceProxyView, bool managedOpsTask);
robertphillips77a2e522015-10-17 07:43:27 -070046
Chris Daltone2a903e2019-09-18 13:41:50 -060047 // Create a render task that can resolve MSAA and/or regenerate mipmap levels on proxies. This
48 // method will only add the new render task to the list. It is up to the caller to call
49 // addProxy() on the returned object.
50 GrTextureResolveRenderTask* newTextureResolveRenderTask(const GrCaps&);
Chris Dalton3d770272019-08-14 09:24:37 -060051
Greg Danielc30f1a92019-09-06 15:28:58 -040052 // Create a new render task that will cause the gpu to wait on semaphores before executing any
53 // more RenderTasks that target proxy. It is possible for this wait to also block additional
54 // work (even to other proxies) that has already been recorded or will be recorded later. The
55 // only guarantee is that future work to the passed in proxy will wait on the semaphores to be
56 // signaled.
Greg Daniel301015c2019-11-18 14:06:46 -050057 void newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
58 std::unique_ptr<std::unique_ptr<GrSemaphore>[]>,
Greg Danielc30f1a92019-09-06 15:28:58 -040059 int numSemaphores);
60
Greg Danielbbfec9d2019-08-20 10:56:51 -040061 // Create a new render task which copies the pixels from the srcProxy into the dstBuffer. This
62 // is used to support the asynchronous readback API. The srcRect is the region of the srcProxy
63 // to be copied. The surfaceColorType says how we should interpret the data when reading back
64 // from the source. DstColorType describes how the data should be stored in the dstBuffer.
65 // DstOffset is the offset into the dstBuffer where we will start writing data.
66 void newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy, const SkIRect& srcRect,
67 GrColorType surfaceColorType, GrColorType dstColorType,
68 sk_sp<GrGpuBuffer> dstBuffer, size_t dstOffset);
69
Greg Daniel16f5c652019-10-29 11:26:01 -040070 // Creates a new render task which copies a pixel rectangle from srcView into dstView. The src
Greg Daniele227fe42019-08-21 13:52:24 -040071 // pixels copied are specified by srcRect. They are copied to a rect of the same size in
72 // dstProxy with top left at dstPoint. If the src rect is clipped by the src bounds then pixel
73 // values in the dst rect corresponding to the area clipped by the src rect are not overwritten.
74 // This method is not guaranteed to succeed depending on the type of surface, formats, etc, and
75 // the backend-specific limitations.
Greg Daniel16f5c652019-10-29 11:26:01 -040076 bool newCopyRenderTask(GrSurfaceProxyView srcView, const SkIRect& srcRect,
77 GrSurfaceProxyView dstView, const SkIPoint& dstPoint);
Greg Daniele227fe42019-08-21 13:52:24 -040078
Robert Phillips69893702019-02-22 11:16:30 -050079 GrRecordingContext* getContext() { return fContext; }
robertphillips77a2e522015-10-17 07:43:27 -070080
Herb Derby26cbe512018-05-24 14:39:01 -040081 GrTextContext* getTextContext();
brianosman86e76262016-08-11 12:17:31 -070082
robertphillips68737822015-10-29 12:12:21 -070083 GrPathRenderer* getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
84 bool allowSW,
85 GrPathRendererChain::DrawType drawType,
Ben Wagnera93a14a2017-08-28 10:34:05 -040086 GrPathRenderer::StencilSupport* stencilSupport = nullptr);
robertphillips68737822015-10-29 12:12:21 -070087
Brian Salomone7df0bb2018-05-07 14:44:57 -040088 GrPathRenderer* getSoftwarePathRenderer();
89
Chris Daltonfddb6c02017-11-04 15:22:22 -060090 // Returns a direct pointer to the coverage counting path renderer, or null if it is not
91 // supported and turned on.
92 GrCoverageCountingPathRenderer* getCoverageCountingPathRenderer();
93
Brian Salomon653f42f2018-07-10 10:07:31 -040094 void flushIfNecessary();
bsalomonb77a9072016-09-07 10:02:04 -070095
Greg Daniel78325c12017-06-19 16:39:13 -040096 static bool ProgramUnitTest(GrContext* context, int maxStages, int maxLevels);
robertphillipsa13e2022015-11-11 12:01:09 -080097
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040098 GrSemaphoresSubmitted flushSurfaces(GrSurfaceProxy* proxies[],
99 int cnt,
100 SkSurface::BackendSurfaceAccess access,
101 const GrFlushInfo& info);
Greg Daniel55f040b2020-02-13 15:38:32 +0000102 GrSemaphoresSubmitted flushSurface(GrSurfaceProxy* proxy,
Greg Daniel4aa13e72019-04-15 14:42:20 -0400103 SkSurface::BackendSurfaceAccess access,
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400104 const GrFlushInfo& info) {
Greg Daniel55f040b2020-02-13 15:38:32 +0000105 return this->flushSurfaces(&proxy, 1, access, info);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400106 }
bsalomon6a2b1942016-09-08 11:28:59 -0700107
Chris Daltonfe199b72017-05-05 11:26:15 -0400108 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500109
110#if GR_TEST_UTILS
Chris Daltonfe199b72017-05-05 11:26:15 -0400111 void testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject*);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500112#endif
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400113
Chris Dalton6b498102019-08-01 14:14:52 -0600114 void moveRenderTasksToDDL(SkDeferredDisplayList* ddl);
115 void copyRenderTasksFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
Robert Phillips62000362018-02-01 09:10:04 -0500116
robertphillips77a2e522015-10-17 07:43:27 -0700117private:
Chris Dalton6b498102019-08-01 14:14:52 -0600118 // This class encapsulates maintenance and manipulation of the drawing manager's DAG of
119 // renderTasks.
120 class RenderTaskDAG {
Robert Phillips22310d62018-09-05 11:07:21 -0400121 public:
Chris Dalton6b498102019-08-01 14:14:52 -0600122 RenderTaskDAG(bool sortRenderTasks);
123 ~RenderTaskDAG();
Robert Phillips22310d62018-09-05 11:07:21 -0400124
125 // Currently, when explicitly allocating resources, this call will topologically sort the
Greg Danielf41b2bd2019-08-22 16:19:24 -0400126 // GrRenderTasks.
127 // MDB TODO: remove once incremental GrRenderTask sorting is enabled
Robert Phillips22310d62018-09-05 11:07:21 -0400128 void prepForFlush();
129
130 void closeAll(const GrCaps* caps);
131
132 // A yucky combination of closeAll and reset
133 void cleanup(const GrCaps* caps);
134
135 void gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const;
136
137 void reset();
138
Greg Danielf41b2bd2019-08-22 16:19:24 -0400139 // These calls forceably remove a GrRenderTask from the DAG. They are problematic bc they
140 // just remove the GrRenderTask but don't cleanup any refering pointers (i.e., dependency
141 // pointers in the DAG). They work right now bc they are only called at flush time, after
142 // the topological sort is complete (so the dangling pointers aren't used).
Chris Dalton6b498102019-08-01 14:14:52 -0600143 void removeRenderTask(int index);
144 void removeRenderTasks(int startIndex, int stopIndex);
Robert Phillips22310d62018-09-05 11:07:21 -0400145
Chris Dalton6b498102019-08-01 14:14:52 -0600146 bool empty() const { return fRenderTasks.empty(); }
147 int numRenderTasks() const { return fRenderTasks.count(); }
Robert Phillips22310d62018-09-05 11:07:21 -0400148
Robert Phillips9313aa72019-04-09 18:41:27 -0400149 bool isUsed(GrSurfaceProxy*) const;
150
Chris Dalton6b498102019-08-01 14:14:52 -0600151 GrRenderTask* renderTask(int index) { return fRenderTasks[index].get(); }
152 const GrRenderTask* renderTask(int index) const { return fRenderTasks[index].get(); }
Robert Phillips22310d62018-09-05 11:07:21 -0400153
Chris Dalton6b498102019-08-01 14:14:52 -0600154 GrRenderTask* back() { return fRenderTasks.back().get(); }
155 const GrRenderTask* back() const { return fRenderTasks.back().get(); }
Robert Phillips22310d62018-09-05 11:07:21 -0400156
Chris Dalton3d770272019-08-14 09:24:37 -0600157 GrRenderTask* add(sk_sp<GrRenderTask>);
158 GrRenderTask* addBeforeLast(sk_sp<GrRenderTask>);
Chris Dalton6b498102019-08-01 14:14:52 -0600159 void add(const SkTArray<sk_sp<GrRenderTask>>&);
Robert Phillips22310d62018-09-05 11:07:21 -0400160
Chris Dalton6b498102019-08-01 14:14:52 -0600161 void swap(SkTArray<sk_sp<GrRenderTask>>* renderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400162
Chris Dalton6b498102019-08-01 14:14:52 -0600163 bool sortingRenderTasks() const { return fSortRenderTasks; }
Robert Phillips46acf9d2018-10-09 09:31:40 -0400164
Robert Phillips22310d62018-09-05 11:07:21 -0400165 private:
Chris Dalton6b498102019-08-01 14:14:52 -0600166 SkTArray<sk_sp<GrRenderTask>> fRenderTasks;
167 bool fSortRenderTasks;
Robert Phillips22310d62018-09-05 11:07:21 -0400168 };
169
Robert Phillips69893702019-02-22 11:16:30 -0500170 GrDrawingManager(GrRecordingContext*, const GrPathRendererChain::Options&,
Robert Phillips0d075de2019-03-04 11:08:13 -0500171 const GrTextContext::Options&,
Chris Dalton6b498102019-08-01 14:14:52 -0600172 bool sortRenderTasks,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400173 bool reduceOpsTaskSplitting);
robertphillips77a2e522015-10-17 07:43:27 -0700174
Robert Phillipsa9162df2019-02-11 14:12:03 -0500175 bool wasAbandoned() const;
176
robertphillips77a2e522015-10-17 07:43:27 -0700177 void cleanup();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500178
Greg Danielf41b2bd2019-08-22 16:19:24 -0400179 // Closes the target's dependent render tasks (or, if not in sorting/opsTask-splitting-reduction
180 // mode, closes fActiveOpsTask) in preparation for us opening a new opsTask that will write to
Chris Dalton5fe99772019-08-06 11:57:39 -0600181 // 'target'.
Greg Danielbbfec9d2019-08-20 10:56:51 -0400182 void closeRenderTasksForNewRenderTask(GrSurfaceProxy* target);
Chris Dalton5fe99772019-08-06 11:57:39 -0600183
Greg Danielf41b2bd2019-08-22 16:19:24 -0400184 // return true if any GrRenderTasks were actually executed; false otherwise
Chris Dalton6b498102019-08-01 14:14:52 -0600185 bool executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState*,
186 int* numRenderTasksExecuted);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500187
Greg Danielfe159622020-04-10 17:43:51 +0000188 bool flush(GrSurfaceProxy* proxies[],
189 int numProxies,
190 SkSurface::BackendSurfaceAccess access,
191 const GrFlushInfo&,
192 const GrPrepareForExternalIORequests&);
193
194 bool submitToGpu(bool syncToCpu);
robertphillips77a2e522015-10-17 07:43:27 -0700195
Robert Phillips38d64b02018-09-04 13:23:26 -0400196 SkDEBUGCODE(void validate() const);
197
Robert Phillips69893702019-02-22 11:16:30 -0500198 friend class GrContext; // access to: flush & cleanup
Robert Phillips7ee385e2017-03-30 08:02:11 -0400199 friend class GrContextPriv; // access to: flush
Chris Daltonfe199b72017-05-05 11:26:15 -0400200 friend class GrOnFlushResourceProvider; // this is just a shallow wrapper around this class
Robert Phillips69893702019-02-22 11:16:30 -0500201 friend class GrRecordingContext; // access to: ctor
Greg Danielb6c15ba2019-03-04 13:08:25 -0500202 friend class SkImage; // for access to: flush
robertphillips77a2e522015-10-17 07:43:27 -0700203
204 static const int kNumPixelGeometries = 5; // The different pixel geometries
205 static const int kNumDFTOptions = 2; // DFT or no DFT
206
Robert Phillips69893702019-02-22 11:16:30 -0500207 GrRecordingContext* fContext;
bsalomon6b2552f2016-09-15 13:50:26 -0700208 GrPathRendererChain::Options fOptionsForPathRendererChain;
Herb Derby26cbe512018-05-24 14:39:01 -0400209 GrTextContext::Options fOptionsForTextContext;
Brian Salomon601ac802019-02-07 13:37:16 -0500210 // This cache is used by both the vertex and index pools. It reuses memory across multiple
211 // flushes.
212 sk_sp<GrBufferAllocPool::CpuBufferCache> fCpuBufferCache;
joshualittde8dc7e2016-01-08 10:09:13 -0800213
Chris Dalton6b498102019-08-01 14:14:52 -0600214 RenderTaskDAG fDAG;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400215 GrOpsTask* fActiveOpsTask = nullptr;
216 // These are the IDs of the opsTask currently being flushed (in internalFlush)
Chris Dalton6b498102019-08-01 14:14:52 -0600217 SkSTArray<8, uint32_t, true> fFlushingRenderTaskIDs;
Chris Daltonc4b47352019-08-23 10:10:36 -0600218 // These are the new renderTasks generated by the onFlush CBs
219 SkSTArray<4, sk_sp<GrRenderTask>> fOnFlushRenderTasks;
robertphillips77a2e522015-10-17 07:43:27 -0700220
Herb Derby26cbe512018-05-24 14:39:01 -0400221 std::unique_ptr<GrTextContext> fTextContext;
robertphillipsa13e2022015-11-11 12:01:09 -0800222
Ben Wagner9ec70c62018-07-12 13:30:47 -0400223 std::unique_ptr<GrPathRendererChain> fPathRendererChain;
224 sk_sp<GrSoftwarePathRenderer> fSoftwarePathRenderer;
brianosman86e76262016-08-11 12:17:31 -0700225
Robert Phillips40a29d72018-01-18 12:59:22 -0500226 GrTokenTracker fTokenTracker;
brianosman86e76262016-08-11 12:17:31 -0700227 bool fFlushing;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400228 bool fReduceOpsTaskSplitting;
bsalomonb77a9072016-09-07 10:02:04 -0700229
Chris Daltonfe199b72017-05-05 11:26:15 -0400230 SkTArray<GrOnFlushCallbackObject*> fOnFlushCBObjects;
Robert Phillips15c91422019-05-07 16:54:48 -0400231
232 void addDDLTarget(GrSurfaceProxy* proxy) { fDDLTargets.insert(proxy); }
233 bool isDDLTarget(GrSurfaceProxy* proxy) { return fDDLTargets.find(proxy) != fDDLTargets.end(); }
234 void clearDDLTargets() { fDDLTargets.clear(); }
235
236 // We play a trick with lazy proxies to retarget the base target of a DDL to the SkSurface
237 // it is replayed on. Because of this remapping we need to explicitly store the targets of
238 // DDL replaying.
239 // Note: we do not expect a whole lot of these per flush
240 std::set<GrSurfaceProxy*> fDDLTargets;
robertphillips77a2e522015-10-17 07:43:27 -0700241};
242
243#endif