blob: b8856ac45a45059b2c5f2a8dc6c90d4aa5c0fc8a [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkSurface.h"
12#include "include/private/SkTArray.h"
Adlai Hollerd71b7b02020-06-08 15:55:00 -040013#include "include/private/SkTHash.h"
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050014#include "src/core/SkSpan.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrBufferAllocPool.h"
16#include "src/gpu/GrDeferredUpload.h"
Robert Phillipsc7ed7e62020-06-29 20:04:57 +000017#include "src/gpu/GrHashMapWithCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrPathRenderer.h"
19#include "src/gpu/GrPathRendererChain.h"
20#include "src/gpu/GrResourceCache.h"
Herb Derby082232b2020-06-10 15:08:18 -040021#include "src/gpu/GrSurfaceProxy.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040022
Robert Phillipsd81379d2020-04-21 10:39:02 -040023// Enabling this will print out which path renderers are being chosen
24#define GR_PATH_RENDERER_SPEW 0
25
Chris Daltonfddb6c02017-11-04 15:22:22 -060026class GrCoverageCountingPathRenderer;
Herb Derby082232b2020-06-10 15:08:18 -040027class GrGpuBuffer;
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040028class GrOnFlushCallbackObject;
Herb Derbydc214c22018-11-08 13:31:39 -050029class GrOpFlushState;
Greg Danielf41b2bd2019-08-22 16:19:24 -040030class GrOpsTask;
Robert Phillips69893702019-02-22 11:16:30 -050031class GrRecordingContext;
Brian Osman11052242016-10-27 14:47:55 -040032class GrRenderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -040033class GrRenderTargetProxy;
Herb Derby082232b2020-06-10 15:08:18 -040034class GrRenderTask;
35class GrSemaphore;
robertphillips68737822015-10-29 12:12:21 -070036class GrSoftwarePathRenderer;
Greg Daniel46e366a2019-12-16 14:38:36 -050037class GrSurfaceContext;
Greg Daniel16f5c652019-10-29 11:26:01 -040038class GrSurfaceProxyView;
Chris Daltone2a903e2019-09-18 13:41:50 -060039class GrTextureResolveRenderTask;
Brian Salomon653f42f2018-07-10 10:07:31 -040040class SkDeferredDisplayList;
robertphillips77a2e522015-10-17 07:43:27 -070041
robertphillips77a2e522015-10-17 07:43:27 -070042class GrDrawingManager {
43public:
44 ~GrDrawingManager();
45
robertphillips68737822015-10-29 12:12:21 -070046 void freeGpuResources();
robertphillips77a2e522015-10-17 07:43:27 -070047
Greg Danielf41b2bd2019-08-22 16:19:24 -040048 // A managed opsTask is controlled by the drawing manager (i.e., sorted & flushed with the
Robert Phillips831a2932019-04-12 17:18:39 -040049 // others). An unmanaged one is created and used by the onFlushCallback.
Greg Daniel16f5c652019-10-29 11:26:01 -040050 sk_sp<GrOpsTask> newOpsTask(GrSurfaceProxyView, bool managedOpsTask);
robertphillips77a2e522015-10-17 07:43:27 -070051
Chris Daltone2a903e2019-09-18 13:41:50 -060052 // Create a render task that can resolve MSAA and/or regenerate mipmap levels on proxies. This
53 // method will only add the new render task to the list. It is up to the caller to call
54 // addProxy() on the returned object.
55 GrTextureResolveRenderTask* newTextureResolveRenderTask(const GrCaps&);
Chris Dalton3d770272019-08-14 09:24:37 -060056
Greg Danielc30f1a92019-09-06 15:28:58 -040057 // Create a new render task that will cause the gpu to wait on semaphores before executing any
58 // more RenderTasks that target proxy. It is possible for this wait to also block additional
59 // work (even to other proxies) that has already been recorded or will be recorded later. The
60 // only guarantee is that future work to the passed in proxy will wait on the semaphores to be
61 // signaled.
Greg Daniel301015c2019-11-18 14:06:46 -050062 void newWaitRenderTask(sk_sp<GrSurfaceProxy> proxy,
63 std::unique_ptr<std::unique_ptr<GrSemaphore>[]>,
Greg Danielc30f1a92019-09-06 15:28:58 -040064 int numSemaphores);
65
Greg Danielbbfec9d2019-08-20 10:56:51 -040066 // Create a new render task which copies the pixels from the srcProxy into the dstBuffer. This
67 // is used to support the asynchronous readback API. The srcRect is the region of the srcProxy
68 // to be copied. The surfaceColorType says how we should interpret the data when reading back
69 // from the source. DstColorType describes how the data should be stored in the dstBuffer.
70 // DstOffset is the offset into the dstBuffer where we will start writing data.
71 void newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy, const SkIRect& srcRect,
72 GrColorType surfaceColorType, GrColorType dstColorType,
73 sk_sp<GrGpuBuffer> dstBuffer, size_t dstOffset);
74
Greg Daniel16f5c652019-10-29 11:26:01 -040075 // Creates a new render task which copies a pixel rectangle from srcView into dstView. The src
Greg Daniele227fe42019-08-21 13:52:24 -040076 // pixels copied are specified by srcRect. They are copied to a rect of the same size in
77 // dstProxy with top left at dstPoint. If the src rect is clipped by the src bounds then pixel
78 // values in the dst rect corresponding to the area clipped by the src rect are not overwritten.
79 // This method is not guaranteed to succeed depending on the type of surface, formats, etc, and
80 // the backend-specific limitations.
Greg Daniel16f5c652019-10-29 11:26:01 -040081 bool newCopyRenderTask(GrSurfaceProxyView srcView, const SkIRect& srcRect,
82 GrSurfaceProxyView dstView, const SkIPoint& dstPoint);
Greg Daniele227fe42019-08-21 13:52:24 -040083
Robert Phillips69893702019-02-22 11:16:30 -050084 GrRecordingContext* getContext() { return fContext; }
robertphillips77a2e522015-10-17 07:43:27 -070085
robertphillips68737822015-10-29 12:12:21 -070086 GrPathRenderer* getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
87 bool allowSW,
88 GrPathRendererChain::DrawType drawType,
Ben Wagnera93a14a2017-08-28 10:34:05 -040089 GrPathRenderer::StencilSupport* stencilSupport = nullptr);
robertphillips68737822015-10-29 12:12:21 -070090
Brian Salomone7df0bb2018-05-07 14:44:57 -040091 GrPathRenderer* getSoftwarePathRenderer();
92
Chris Daltonfddb6c02017-11-04 15:22:22 -060093 // Returns a direct pointer to the coverage counting path renderer, or null if it is not
94 // supported and turned on.
95 GrCoverageCountingPathRenderer* getCoverageCountingPathRenderer();
96
Brian Salomon653f42f2018-07-10 10:07:31 -040097 void flushIfNecessary();
bsalomonb77a9072016-09-07 10:02:04 -070098
Robert Phillips4e105e22020-07-16 09:18:50 -040099 static bool ProgramUnitTest(GrDirectContext*, int maxStages, int maxLevels);
robertphillipsa13e2022015-11-11 12:01:09 -0800100
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500101 GrSemaphoresSubmitted flushSurfaces(SkSpan<GrSurfaceProxy*>,
102 SkSurface::BackendSurfaceAccess,
103 const GrFlushInfo&,
Greg Daniel9efe3862020-06-11 11:51:06 -0400104 const GrBackendSurfaceMutableState* newState);
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*);
Chris Dalton31634282020-09-17 12:16:54 -0600110 GrPathRendererChain::Options testingOnly_getOptionsForPathRendererChain() {
111 return fOptionsForPathRendererChain;
112 }
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500113#endif
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400114
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400115 GrRenderTask* getLastRenderTask(const GrSurfaceProxy*) const;
116 GrOpsTask* getLastOpsTask(const GrSurfaceProxy*) const;
117 void setLastRenderTask(const GrSurfaceProxy*, GrRenderTask*);
118
Chris Dalton6b498102019-08-01 14:14:52 -0600119 void moveRenderTasksToDDL(SkDeferredDisplayList* ddl);
Robert Phillips88b29612020-11-16 15:15:08 -0500120 void createDDLTask(sk_sp<const SkDeferredDisplayList>, GrRenderTargetProxy* newDest,
121 SkIPoint offset);
Robert Phillips62000362018-02-01 09:10:04 -0500122
robertphillips77a2e522015-10-17 07:43:27 -0700123private:
Herb Derby082232b2020-06-10 15:08:18 -0400124 GrDrawingManager(GrRecordingContext*,
125 const GrPathRendererChain::Options&,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400126 bool reduceOpsTaskSplitting);
robertphillips77a2e522015-10-17 07:43:27 -0700127
Robert Phillipsa9162df2019-02-11 14:12:03 -0500128 bool wasAbandoned() const;
129
Greg Danielf41b2bd2019-08-22 16:19:24 -0400130 // Closes the target's dependent render tasks (or, if not in sorting/opsTask-splitting-reduction
131 // mode, closes fActiveOpsTask) in preparation for us opening a new opsTask that will write to
Chris Dalton5fe99772019-08-06 11:57:39 -0600132 // 'target'.
Greg Danielbbfec9d2019-08-20 10:56:51 -0400133 void closeRenderTasksForNewRenderTask(GrSurfaceProxy* target);
Chris Dalton5fe99772019-08-06 11:57:39 -0600134
Greg Danielf41b2bd2019-08-22 16:19:24 -0400135 // return true if any GrRenderTasks were actually executed; false otherwise
Chris Dalton6b498102019-08-01 14:14:52 -0600136 bool executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState*,
137 int* numRenderTasksExecuted);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500138
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400139 void removeRenderTasks(int startIndex, int stopIndex);
140
Adlai Holler33432272020-11-11 13:54:37 -0500141 void sortTasks();
142
143 void closeAllTasks();
144
145 GrRenderTask* appendTask(sk_sp<GrRenderTask>);
146 void appendTasks(SkSpan<const sk_sp<GrRenderTask>>);
147 GrRenderTask* insertTaskBeforeLast(sk_sp<GrRenderTask>);
148
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500149 bool flush(SkSpan<GrSurfaceProxy*> proxies,
Greg Danielfe159622020-04-10 17:43:51 +0000150 SkSurface::BackendSurfaceAccess access,
Greg Daniel9efe3862020-06-11 11:51:06 -0400151 const GrFlushInfo&,
152 const GrBackendSurfaceMutableState* newState);
Greg Danielfe159622020-04-10 17:43:51 +0000153
154 bool submitToGpu(bool syncToCpu);
robertphillips77a2e522015-10-17 07:43:27 -0700155
Robert Phillips38d64b02018-09-04 13:23:26 -0400156 SkDEBUGCODE(void validate() const);
157
Adlai Holler3acc69a2020-10-13 08:20:51 -0400158 friend class GrDirectContext; // access to: flush & cleanup
Adlai Hollera0693042020-10-14 11:23:11 -0400159 friend class GrDirectContextPriv; // access to: flush
Chris Daltonfe199b72017-05-05 11:26:15 -0400160 friend class GrOnFlushResourceProvider; // this is just a shallow wrapper around this class
Robert Phillips69893702019-02-22 11:16:30 -0500161 friend class GrRecordingContext; // access to: ctor
Greg Danielb6c15ba2019-03-04 13:08:25 -0500162 friend class SkImage; // for access to: flush
robertphillips77a2e522015-10-17 07:43:27 -0700163
164 static const int kNumPixelGeometries = 5; // The different pixel geometries
165 static const int kNumDFTOptions = 2; // DFT or no DFT
166
Robert Phillips69893702019-02-22 11:16:30 -0500167 GrRecordingContext* fContext;
bsalomon6b2552f2016-09-15 13:50:26 -0700168 GrPathRendererChain::Options fOptionsForPathRendererChain;
Herb Derby082232b2020-06-10 15:08:18 -0400169
Brian Salomon601ac802019-02-07 13:37:16 -0500170 // This cache is used by both the vertex and index pools. It reuses memory across multiple
171 // flushes.
172 sk_sp<GrBufferAllocPool::CpuBufferCache> fCpuBufferCache;
joshualittde8dc7e2016-01-08 10:09:13 -0800173
Adlai Holler33432272020-11-11 13:54:37 -0500174 SkTArray<sk_sp<GrRenderTask>> fDAG;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400175 GrOpsTask* fActiveOpsTask = nullptr;
Robert Phillips07f675d2020-11-16 13:44:01 -0500176 // These are the IDs of the opsTask currently being flushed (in internalFlush). They are
177 // only stored here to prevent memory thrashing.
Chris Dalton6b498102019-08-01 14:14:52 -0600178 SkSTArray<8, uint32_t, true> fFlushingRenderTaskIDs;
Chris Daltonc4b47352019-08-23 10:10:36 -0600179 // These are the new renderTasks generated by the onFlush CBs
180 SkSTArray<4, sk_sp<GrRenderTask>> fOnFlushRenderTasks;
robertphillips77a2e522015-10-17 07:43:27 -0700181
Ben Wagner9ec70c62018-07-12 13:30:47 -0400182 std::unique_ptr<GrPathRendererChain> fPathRendererChain;
183 sk_sp<GrSoftwarePathRenderer> fSoftwarePathRenderer;
brianosman86e76262016-08-11 12:17:31 -0700184
Robert Phillips40a29d72018-01-18 12:59:22 -0500185 GrTokenTracker fTokenTracker;
brianosman86e76262016-08-11 12:17:31 -0700186 bool fFlushing;
Adlai Hollerd7b59c02020-11-06 13:45:50 -0500187 const bool fReduceOpsTaskSplitting;
bsalomonb77a9072016-09-07 10:02:04 -0700188
Chris Daltonfe199b72017-05-05 11:26:15 -0400189 SkTArray<GrOnFlushCallbackObject*> fOnFlushCBObjects;
Robert Phillips15c91422019-05-07 16:54:48 -0400190
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400191 void addDDLTarget(GrSurfaceProxy* newTarget, GrRenderTargetProxy* ddlTarget) {
192 fDDLTargets.set(newTarget->uniqueID().asUInt(), ddlTarget);
193 }
194 bool isDDLTarget(GrSurfaceProxy* newTarget) {
195 return SkToBool(fDDLTargets.find(newTarget->uniqueID().asUInt()));
196 }
197 GrRenderTargetProxy* getDDLTarget(GrSurfaceProxy* newTarget) {
198 auto entry = fDDLTargets.find(newTarget->uniqueID().asUInt());
199 return entry ? *entry : nullptr;
200 }
201 void clearDDLTargets() { fDDLTargets.reset(); }
Robert Phillips15c91422019-05-07 16:54:48 -0400202
203 // We play a trick with lazy proxies to retarget the base target of a DDL to the SkSurface
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400204 // it is replayed on. 'fDDLTargets' stores this mapping from SkSurface unique proxy ID
205 // to the DDL's lazy proxy.
Robert Phillips15c91422019-05-07 16:54:48 -0400206 // Note: we do not expect a whole lot of these per flush
Robert Phillips7eb0a5f2020-06-09 14:15:09 -0400207 SkTHashMap<uint32_t, GrRenderTargetProxy*> fDDLTargets;
Adlai Hollerd71b7b02020-06-08 15:55:00 -0400208
Robert Phillipsc7ed7e62020-06-29 20:04:57 +0000209 struct SurfaceIDKeyTraits {
210 static uint32_t GetInvalidKey() {
211 return GrSurfaceProxy::UniqueID::InvalidID().asUInt();
Adlai Holler1bdbe552020-06-12 11:28:15 -0400212 }
213 };
214
Robert Phillipsc7ed7e62020-06-29 20:04:57 +0000215 GrHashMapWithCache<uint32_t, GrRenderTask*, SurfaceIDKeyTraits, GrCheapHash> fLastRenderTasks;
robertphillips77a2e522015-10-17 07:43:27 -0700216};
217
218#endif