blob: 6b5fc1d2768f1d0c90fcd0a07276162e1e602ae5 [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 Danielf21bf9e2019-08-22 20:12:20 +000024class 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;
Greg Danielf21bf9e2019-08-22 20:12:20 +000028class GrRenderTargetOpList;
robertphillips68737822015-10-29 12:12:21 -070029class GrSoftwarePathRenderer;
Brian Osman45580d32016-11-23 09:37:01 -050030class GrTextureContext;
Brian Salomon653f42f2018-07-10 10:07:31 -040031class SkDeferredDisplayList;
robertphillips77a2e522015-10-17 07:43:27 -070032
robertphillips77a2e522015-10-17 07:43:27 -070033class GrDrawingManager {
34public:
35 ~GrDrawingManager();
36
robertphillips68737822015-10-29 12:12:21 -070037 void freeGpuResources();
robertphillips77a2e522015-10-17 07:43:27 -070038
Brian Salomonbf6b9792019-08-21 09:38:10 -040039 std::unique_ptr<GrRenderTargetContext> makeRenderTargetContext(sk_sp<GrSurfaceProxy>,
40 GrColorType,
41 sk_sp<SkColorSpace>,
42 const SkSurfaceProps*,
Greg Danielf21bf9e2019-08-22 20:12:20 +000043 bool managedOpList = true);
Brian Salomonbf6b9792019-08-21 09:38:10 -040044 std::unique_ptr<GrTextureContext> makeTextureContext(sk_sp<GrSurfaceProxy>,
Brian Salomond6287472019-06-24 15:50:07 -040045 GrColorType,
Brian Salomonbf6b9792019-08-21 09:38:10 -040046 SkAlphaType,
47 sk_sp<SkColorSpace>);
robertphillips77a2e522015-10-17 07:43:27 -070048
Greg Danielf21bf9e2019-08-22 20:12:20 +000049 // A managed opList is controlled by the drawing manager (i.e., sorted & flushed with the
Robert Phillips831a2932019-04-12 17:18:39 -040050 // others). An unmanaged one is created and used by the onFlushCallback.
Greg Danielf21bf9e2019-08-22 20:12:20 +000051 sk_sp<GrRenderTargetOpList> newRTOpList(sk_sp<GrRenderTargetProxy>, bool managedOpList);
robertphillips77a2e522015-10-17 07:43:27 -070052
Chris Dalton3d770272019-08-14 09:24:37 -060053 // Create a new, specialized, render task that will regenerate mipmap levels and/or resolve
54 // MSAA (depending on GrTextureResolveFlags). This method will add the new render task to the
55 // list of render tasks and make it depend on the target texture proxy. It is up to the caller
56 // to add any dependencies on the new render task.
57 GrRenderTask* newTextureResolveRenderTask(
58 sk_sp<GrTextureProxy>, GrTextureResolveFlags, const GrCaps&);
59
Greg Danielbbfec9d2019-08-20 10:56:51 -040060 // Create a new render task which copies the pixels from the srcProxy into the dstBuffer. This
61 // is used to support the asynchronous readback API. The srcRect is the region of the srcProxy
62 // to be copied. The surfaceColorType says how we should interpret the data when reading back
63 // from the source. DstColorType describes how the data should be stored in the dstBuffer.
64 // DstOffset is the offset into the dstBuffer where we will start writing data.
65 void newTransferFromRenderTask(sk_sp<GrSurfaceProxy> srcProxy, const SkIRect& srcRect,
66 GrColorType surfaceColorType, GrColorType dstColorType,
67 sk_sp<GrGpuBuffer> dstBuffer, size_t dstOffset);
68
Greg Daniele227fe42019-08-21 13:52:24 -040069 // Creates a new render task which copies a pixel rectangle from srcProxy into dstProxy. The src
70 // pixels copied are specified by srcRect. They are copied to a rect of the same size in
71 // dstProxy with top left at dstPoint. If the src rect is clipped by the src bounds then pixel
72 // values in the dst rect corresponding to the area clipped by the src rect are not overwritten.
73 // This method is not guaranteed to succeed depending on the type of surface, formats, etc, and
74 // the backend-specific limitations.
75 bool newCopyRenderTask(sk_sp<GrSurfaceProxy> srcProxy, const SkIRect& srcRect,
76 sk_sp<GrSurfaceProxy> dstProxy, const SkIPoint& dstPoint);
77
Robert Phillips69893702019-02-22 11:16:30 -050078 GrRecordingContext* getContext() { return fContext; }
robertphillips77a2e522015-10-17 07:43:27 -070079
Herb Derby26cbe512018-05-24 14:39:01 -040080 GrTextContext* getTextContext();
brianosman86e76262016-08-11 12:17:31 -070081
robertphillips68737822015-10-29 12:12:21 -070082 GrPathRenderer* getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
83 bool allowSW,
84 GrPathRendererChain::DrawType drawType,
Ben Wagnera93a14a2017-08-28 10:34:05 -040085 GrPathRenderer::StencilSupport* stencilSupport = nullptr);
robertphillips68737822015-10-29 12:12:21 -070086
Brian Salomone7df0bb2018-05-07 14:44:57 -040087 GrPathRenderer* getSoftwarePathRenderer();
88
Chris Daltonfddb6c02017-11-04 15:22:22 -060089 // Returns a direct pointer to the coverage counting path renderer, or null if it is not
90 // supported and turned on.
91 GrCoverageCountingPathRenderer* getCoverageCountingPathRenderer();
92
Brian Salomon653f42f2018-07-10 10:07:31 -040093 void flushIfNecessary();
bsalomonb77a9072016-09-07 10:02:04 -070094
Greg Daniel78325c12017-06-19 16:39:13 -040095 static bool ProgramUnitTest(GrContext* context, int maxStages, int maxLevels);
robertphillipsa13e2022015-11-11 12:01:09 -080096
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040097 GrSemaphoresSubmitted flushSurfaces(GrSurfaceProxy* proxies[],
98 int cnt,
99 SkSurface::BackendSurfaceAccess access,
100 const GrFlushInfo& info);
101 GrSemaphoresSubmitted flushSurface(GrSurfaceProxy* proxy,
Greg Daniel4aa13e72019-04-15 14:42:20 -0400102 SkSurface::BackendSurfaceAccess access,
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400103 const GrFlushInfo& info) {
104 return this->flushSurfaces(&proxy, 1, access, info);
105 }
bsalomon6a2b1942016-09-08 11:28:59 -0700106
Chris Daltonfe199b72017-05-05 11:26:15 -0400107 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500108
109#if GR_TEST_UTILS
Chris Daltonfe199b72017-05-05 11:26:15 -0400110 void testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject*);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500111#endif
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400112
Chris Dalton6b498102019-08-01 14:14:52 -0600113 void moveRenderTasksToDDL(SkDeferredDisplayList* ddl);
114 void copyRenderTasksFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
Robert Phillips62000362018-02-01 09:10:04 -0500115
robertphillips77a2e522015-10-17 07:43:27 -0700116private:
Chris Dalton6b498102019-08-01 14:14:52 -0600117 // This class encapsulates maintenance and manipulation of the drawing manager's DAG of
118 // renderTasks.
119 class RenderTaskDAG {
Robert Phillips22310d62018-09-05 11:07:21 -0400120 public:
Chris Dalton6b498102019-08-01 14:14:52 -0600121 RenderTaskDAG(bool sortRenderTasks);
122 ~RenderTaskDAG();
Robert Phillips22310d62018-09-05 11:07:21 -0400123
124 // Currently, when explicitly allocating resources, this call will topologically sort the
Greg Danielf21bf9e2019-08-22 20:12:20 +0000125 // opLists.
126 // MDB TODO: remove once incremental opList sorting is enabled
Robert Phillips22310d62018-09-05 11:07:21 -0400127 void prepForFlush();
128
129 void closeAll(const GrCaps* caps);
130
131 // A yucky combination of closeAll and reset
132 void cleanup(const GrCaps* caps);
133
134 void gatherIDs(SkSTArray<8, uint32_t, true>* idArray) const;
135
136 void reset();
137
Greg Danielf21bf9e2019-08-22 20:12:20 +0000138 // These calls forceably remove an opList from the DAG. They are problematic bc they just
139 // remove the opList but don't cleanup any refering pointers (i.e., dependency pointers
140 // in the DAG). They work right now bc they are only called at flush time, after the
141 // topological sort is complete (so the dangling pointers aren't used).
Chris Dalton6b498102019-08-01 14:14:52 -0600142 void removeRenderTask(int index);
143 void removeRenderTasks(int startIndex, int stopIndex);
Robert Phillips22310d62018-09-05 11:07:21 -0400144
Chris Dalton6b498102019-08-01 14:14:52 -0600145 bool empty() const { return fRenderTasks.empty(); }
146 int numRenderTasks() const { return fRenderTasks.count(); }
Robert Phillips22310d62018-09-05 11:07:21 -0400147
Robert Phillips9313aa72019-04-09 18:41:27 -0400148 bool isUsed(GrSurfaceProxy*) const;
149
Chris Dalton6b498102019-08-01 14:14:52 -0600150 GrRenderTask* renderTask(int index) { return fRenderTasks[index].get(); }
151 const GrRenderTask* renderTask(int index) const { return fRenderTasks[index].get(); }
Robert Phillips22310d62018-09-05 11:07:21 -0400152
Chris Dalton6b498102019-08-01 14:14:52 -0600153 GrRenderTask* back() { return fRenderTasks.back().get(); }
154 const GrRenderTask* back() const { return fRenderTasks.back().get(); }
Robert Phillips22310d62018-09-05 11:07:21 -0400155
Chris Dalton3d770272019-08-14 09:24:37 -0600156 GrRenderTask* add(sk_sp<GrRenderTask>);
157 GrRenderTask* addBeforeLast(sk_sp<GrRenderTask>);
Chris Dalton6b498102019-08-01 14:14:52 -0600158 void add(const SkTArray<sk_sp<GrRenderTask>>&);
Robert Phillips22310d62018-09-05 11:07:21 -0400159
Chris Dalton6b498102019-08-01 14:14:52 -0600160 void swap(SkTArray<sk_sp<GrRenderTask>>* renderTasks);
Robert Phillips22310d62018-09-05 11:07:21 -0400161
Chris Dalton6b498102019-08-01 14:14:52 -0600162 bool sortingRenderTasks() const { return fSortRenderTasks; }
Robert Phillips46acf9d2018-10-09 09:31:40 -0400163
Robert Phillips22310d62018-09-05 11:07:21 -0400164 private:
Chris Dalton6b498102019-08-01 14:14:52 -0600165 SkTArray<sk_sp<GrRenderTask>> fRenderTasks;
166 bool fSortRenderTasks;
Robert Phillips22310d62018-09-05 11:07:21 -0400167 };
168
Robert Phillips69893702019-02-22 11:16:30 -0500169 GrDrawingManager(GrRecordingContext*, const GrPathRendererChain::Options&,
Robert Phillips0d075de2019-03-04 11:08:13 -0500170 const GrTextContext::Options&,
Chris Dalton6b498102019-08-01 14:14:52 -0600171 bool sortRenderTasks,
Greg Danielf21bf9e2019-08-22 20:12:20 +0000172 bool reduceOpListSplitting);
robertphillips77a2e522015-10-17 07:43:27 -0700173
Robert Phillipsa9162df2019-02-11 14:12:03 -0500174 bool wasAbandoned() const;
175
robertphillips77a2e522015-10-17 07:43:27 -0700176 void cleanup();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500177
Greg Danielf21bf9e2019-08-22 20:12:20 +0000178 // Closes the target's dependent render tasks (or, if not in sorting/opList-splitting-reduction
179 // mode, closes fActiveOpList) in preparation for us opening a new opList that will write to
Chris Dalton5fe99772019-08-06 11:57:39 -0600180 // 'target'.
Greg Danielbbfec9d2019-08-20 10:56:51 -0400181 void closeRenderTasksForNewRenderTask(GrSurfaceProxy* target);
Chris Dalton5fe99772019-08-06 11:57:39 -0600182
Greg Danielf21bf9e2019-08-22 20:12:20 +0000183 // return true if any opLists were actually executed; false otherwise
Chris Dalton6b498102019-08-01 14:14:52 -0600184 bool executeRenderTasks(int startIndex, int stopIndex, GrOpFlushState*,
185 int* numRenderTasksExecuted);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500186
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400187 GrSemaphoresSubmitted flush(GrSurfaceProxy* proxies[],
188 int numProxies,
Greg Danielbae71212019-03-01 15:24:35 -0500189 SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -0400190 const GrFlushInfo&,
191 const GrPrepareForExternalIORequests&);
robertphillips77a2e522015-10-17 07:43:27 -0700192
Robert Phillips38d64b02018-09-04 13:23:26 -0400193 SkDEBUGCODE(void validate() const);
194
Robert Phillips69893702019-02-22 11:16:30 -0500195 friend class GrContext; // access to: flush & cleanup
Robert Phillips7ee385e2017-03-30 08:02:11 -0400196 friend class GrContextPriv; // access to: flush
Chris Daltonfe199b72017-05-05 11:26:15 -0400197 friend class GrOnFlushResourceProvider; // this is just a shallow wrapper around this class
Robert Phillips69893702019-02-22 11:16:30 -0500198 friend class GrRecordingContext; // access to: ctor
Greg Danielb6c15ba2019-03-04 13:08:25 -0500199 friend class SkImage; // for access to: flush
robertphillips77a2e522015-10-17 07:43:27 -0700200
201 static const int kNumPixelGeometries = 5; // The different pixel geometries
202 static const int kNumDFTOptions = 2; // DFT or no DFT
203
Robert Phillips69893702019-02-22 11:16:30 -0500204 GrRecordingContext* fContext;
bsalomon6b2552f2016-09-15 13:50:26 -0700205 GrPathRendererChain::Options fOptionsForPathRendererChain;
Herb Derby26cbe512018-05-24 14:39:01 -0400206 GrTextContext::Options fOptionsForTextContext;
Brian Salomon601ac802019-02-07 13:37:16 -0500207 // This cache is used by both the vertex and index pools. It reuses memory across multiple
208 // flushes.
209 sk_sp<GrBufferAllocPool::CpuBufferCache> fCpuBufferCache;
joshualittde8dc7e2016-01-08 10:09:13 -0800210
Chris Dalton6b498102019-08-01 14:14:52 -0600211 RenderTaskDAG fDAG;
Greg Danielf21bf9e2019-08-22 20:12:20 +0000212 GrOpList* fActiveOpList = nullptr;
213 // These are the IDs of the opLists currently being flushed (in internalFlush)
Chris Dalton6b498102019-08-01 14:14:52 -0600214 SkSTArray<8, uint32_t, true> fFlushingRenderTaskIDs;
Greg Danielf21bf9e2019-08-22 20:12:20 +0000215 // These are the new opLists generated by the onFlush CBs
216 SkSTArray<8, sk_sp<GrOpList>> fOnFlushCBOpLists;
robertphillips77a2e522015-10-17 07:43:27 -0700217
Herb Derby26cbe512018-05-24 14:39:01 -0400218 std::unique_ptr<GrTextContext> fTextContext;
robertphillipsa13e2022015-11-11 12:01:09 -0800219
Ben Wagner9ec70c62018-07-12 13:30:47 -0400220 std::unique_ptr<GrPathRendererChain> fPathRendererChain;
221 sk_sp<GrSoftwarePathRenderer> fSoftwarePathRenderer;
brianosman86e76262016-08-11 12:17:31 -0700222
Robert Phillips40a29d72018-01-18 12:59:22 -0500223 GrTokenTracker fTokenTracker;
brianosman86e76262016-08-11 12:17:31 -0700224 bool fFlushing;
Greg Danielf21bf9e2019-08-22 20:12:20 +0000225 bool fReduceOpListSplitting;
bsalomonb77a9072016-09-07 10:02:04 -0700226
Chris Daltonfe199b72017-05-05 11:26:15 -0400227 SkTArray<GrOnFlushCallbackObject*> fOnFlushCBObjects;
Robert Phillips15c91422019-05-07 16:54:48 -0400228
229 void addDDLTarget(GrSurfaceProxy* proxy) { fDDLTargets.insert(proxy); }
230 bool isDDLTarget(GrSurfaceProxy* proxy) { return fDDLTargets.find(proxy) != fDDLTargets.end(); }
231 void clearDDLTargets() { fDDLTargets.clear(); }
232
233 // We play a trick with lazy proxies to retarget the base target of a DDL to the SkSurface
234 // it is replayed on. Because of this remapping we need to explicitly store the targets of
235 // DDL replaying.
236 // Note: we do not expect a whole lot of these per flush
237 std::set<GrSurfaceProxy*> fDDLTargets;
robertphillips77a2e522015-10-17 07:43:27 -0700238};
239
240#endif