Debian Codegraph @

src/gpu/ganesh/GrRecordingContext.cpp

raw
1 /*
2 * Copyright 2019 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 #include "include/gpu/ganesh/GrRecordingContext.h"
9
10 #include "include/core/SkString.h"
11 #include "include/core/SkTypes.h"
12 #include "include/gpu/GpuTypes.h"
13 #include "include/gpu/ganesh/GrBackendSurface.h"
14 #include "include/gpu/ganesh/GrContextOptions.h"
15 #include "include/gpu/ganesh/GrContextThreadSafeProxy.h"
16 #include "include/gpu/ganesh/GrTypes.h"
17 #include "include/private/base/SkDebug.h"
18 #include "include/private/base/SkMacros.h"
19 #include "include/private/gpu/ganesh/GrTypesPriv.h"
20 #include "src/base/SkArenaAlloc.h"
21 #include "src/core/SkCPUContextImpl.h"
22 #include "src/core/SkCPURecorderImpl.h"
23 #include "src/gpu/ganesh/GrAuditTrail.h"
24 #include "src/gpu/ganesh/GrCaps.h"
25 #include "src/gpu/ganesh/GrContextThreadSafeProxyPriv.h"
26 #include "src/gpu/ganesh/GrDrawingManager.h"
27 #include "src/gpu/ganesh/GrProgramDesc.h"
28 #include "src/gpu/ganesh/GrProxyProvider.h"
29 #include "src/gpu/ganesh/PathRendererChain.h"
30 #include "src/gpu/ganesh/SkGaneshRecorder.h"
31 #include "src/gpu/ganesh/ops/AtlasTextOp.h"
32 #include "src/text/gpu/SubRunAllocator.h"
33 #include "src/text/gpu/TextBlobRedrawCoordinator.h"
34
35 #include <utility>
36
37 using namespace skia_private;
38
39 using TextBlobRedrawCoordinator = sktext::gpu::TextBlobRedrawCoordinator;
40
41 GrRecordingContext::ProgramData::ProgramData(std::unique_ptr<const GrProgramDesc> desc,
42 const GrProgramInfo* info)
43 : fDesc(std::move(desc))
44 , fInfo(info) {
45 }
46
47 GrRecordingContext::ProgramData::ProgramData(ProgramData&& other)
48 : fDesc(std::move(other.fDesc))
49 , fInfo(other.fInfo) {
50 }
51
52 GrRecordingContext::ProgramData::~ProgramData() = default;
53
54 GrRecordingContext::GrRecordingContext(sk_sp<GrContextThreadSafeProxy> proxy, bool ddlRecording)
55 : GrImageContext(std::move(proxy))
56 , fAuditTrail(new GrAuditTrail())
57 , fArenas(ddlRecording) {
58 fProxyProvider = std::make_unique<GrProxyProvider>(this);
59 fCPUContext = std::make_unique<skcpu::ContextImpl>();
60 fRecorder = std::make_unique<SkGaneshRecorder>(this);
61 }
62
63 GrRecordingContext::~GrRecordingContext() {
64 skgpu::ganesh::AtlasTextOp::ClearCache();
65 }
66
67 bool GrRecordingContext::init() {
68 if (!GrImageContext::init()) {
69 return false;
70 }
71
72 skgpu::ganesh::PathRendererChain::Options prcOptions;
73 prcOptions.fAllowPathMaskCaching = this->options().fAllowPathMaskCaching;
74 #if defined(GPU_TEST_UTILS)
75 prcOptions.fGpuPathRenderers = this->options().fGpuPathRenderers;
76 #endif
77 // FIXME: Once this is removed from Chrome and Android, rename to fEnable"".
78 if (this->options().fDisableDistanceFieldPaths) {
79 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
80 }
81
82 bool reduceOpsTaskSplitting = true;
83 if (this->caps()->avoidReorderingRenderTasks()) {
84 reduceOpsTaskSplitting = false;
85 } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) {
86 reduceOpsTaskSplitting = true;
87 } else if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) {
88 reduceOpsTaskSplitting = false;
89 }
90 fDrawingManager.reset(new GrDrawingManager(this,
91 prcOptions,
92 reduceOpsTaskSplitting));
93 return true;
94 }
95
96 void GrRecordingContext::abandonContext() {
97 GrImageContext::abandonContext();
98
99 this->destroyDrawingManager();
100 }
101
102 GrDrawingManager* GrRecordingContext::drawingManager() {
103 return fDrawingManager.get();
104 }
105
106 void GrRecordingContext::destroyDrawingManager() {
107 fDrawingManager.reset();
108 }
109
110 GrRecordingContext::Arenas::Arenas(SkArenaAlloc* recordTimeAllocator,
111 sktext::gpu::SubRunAllocator* subRunAllocator)
112 : fRecordTimeAllocator(recordTimeAllocator)
113 , fRecordTimeSubRunAllocator(subRunAllocator) {
114 // OwnedArenas should instantiate these before passing the bare pointer off to this struct.
115 SkASSERT(subRunAllocator);
116 }
117
118 // Must be defined here so that std::unique_ptr can see the sizes of the various pools, otherwise
119 // it can't generate a default destructor for them.
120 GrRecordingContext::OwnedArenas::OwnedArenas(bool ddlRecording) : fDDLRecording(ddlRecording) {}
121 GrRecordingContext::OwnedArenas::~OwnedArenas() {}
122
123 GrRecordingContext::OwnedArenas& GrRecordingContext::OwnedArenas::operator=(OwnedArenas&& a) {
124 fDDLRecording = a.fDDLRecording;
125 fRecordTimeAllocator = std::move(a.fRecordTimeAllocator);
126 fRecordTimeSubRunAllocator = std::move(a.fRecordTimeSubRunAllocator);
127 return *this;
128 }
129
130 GrRecordingContext::Arenas GrRecordingContext::OwnedArenas::get() {
131 if (!fRecordTimeAllocator && fDDLRecording) {
132 // TODO: empirically determine a better number for SkArenaAlloc's firstHeapAllocation param
133 fRecordTimeAllocator = std::make_unique<SkArenaAlloc>(1024);
134 }
135
136 if (!fRecordTimeSubRunAllocator) {
137 fRecordTimeSubRunAllocator = std::make_unique<sktext::gpu::SubRunAllocator>();
138 }
139
140 return {fRecordTimeAllocator.get(), fRecordTimeSubRunAllocator.get()};
141 }
142
143 GrRecordingContext::OwnedArenas&& GrRecordingContext::detachArenas() {
144 return std::move(fArenas);
145 }
146
147 TextBlobRedrawCoordinator* GrRecordingContext::getTextBlobRedrawCoordinator() {
148 return fThreadSafeProxy->priv().getTextBlobRedrawCoordinator();
149 }
150
151 const TextBlobRedrawCoordinator* GrRecordingContext::getTextBlobRedrawCoordinator() const {
152 return fThreadSafeProxy->priv().getTextBlobRedrawCoordinator();
153 }
154
155 GrThreadSafeCache* GrRecordingContext::threadSafeCache() {
156 return fThreadSafeProxy->priv().threadSafeCache();
157 }
158
159 const GrThreadSafeCache* GrRecordingContext::threadSafeCache() const {
160 return fThreadSafeProxy->priv().threadSafeCache();
161 }
162
163 void GrRecordingContext::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
164 this->drawingManager()->addOnFlushCallbackObject(onFlushCBObject);
165 }
166
167 ////////////////////////////////////////////////////////////////////////////////
168
169 sk_sp<const SkCapabilities> GrRecordingContext::skCapabilities() const {
170 return this->refCaps();
171 }
172
173 int GrRecordingContext::maxTextureSize() const { return this->caps()->maxTextureSize(); }
174
175 int GrRecordingContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); }
176
177 bool GrRecordingContext::colorTypeSupportedAsImage(SkColorType colorType) const {
178 GrBackendFormat format =
179 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
180 GrRenderable::kNo);
181 return format.isValid();
182 }
183
184 bool GrRecordingContext::supportsProtectedContent() const {
185 return this->caps()->supportsProtectedContent();
186 }
187
188 std::unique_ptr<skcpu::Recorder> GrRecordingContext::makeCPURecorder() {
189 return std::make_unique<skcpu::RecorderImpl>(fCPUContext.get());
190 }
191
192 SkRecorder* GrRecordingContext::asRecorder() {
193 return fRecorder.get();
194 }
195
196 ///////////////////////////////////////////////////////////////////////////////////////////////////
197
198 #ifdef SK_ENABLE_DUMP_GPU
199 #include "src/utils/SkJSONWriter.h"
200
201 void GrRecordingContext::dumpJSON(SkJSONWriter* writer) const {
202 writer->beginObject();
203
204 #if GR_GPU_STATS
205 writer->appendS32("path_masks_generated", this->stats()->numPathMasksGenerated());
206 writer->appendS32("path_mask_cache_hits", this->stats()->numPathMaskCacheHits());
207 #endif
208
209 writer->endObject();
210 }
211 #else
212 void GrRecordingContext::dumpJSON(SkJSONWriter*) const { }
213 #endif
214
215 #if defined(GPU_TEST_UTILS)
216
217 #if GR_GPU_STATS
218
219 void GrRecordingContext::Stats::dump(SkString* out) const {
220 out->appendf("Num Path Masks Generated: %d\n", fNumPathMasksGenerated);
221 out->appendf("Num Path Mask Cache Hits: %d\n", fNumPathMaskCacheHits);
222 }
223
224 void GrRecordingContext::Stats::dumpKeyValuePairs(TArray<SkString>* keys,
225 TArray<double>* values) const {
226 keys->push_back(SkString("path_masks_generated"));
227 values->push_back(fNumPathMasksGenerated);
228
229 keys->push_back(SkString("path_mask_cache_hits"));
230 values->push_back(fNumPathMaskCacheHits);
231 }
232
233 void GrRecordingContext::DMSAAStats::dumpKeyValuePairs(TArray<SkString>* keys,
234 TArray<double>* values) const {
235 keys->push_back(SkString("dmsaa_render_passes"));
236 values->push_back(fNumRenderPasses);
237
238 keys->push_back(SkString("dmsaa_multisample_render_passes"));
239 values->push_back(fNumMultisampleRenderPasses);
240
241 for (const auto& [name, count] : fTriggerCounts) {
242 keys->push_back(SkStringPrintf("dmsaa_trigger_%s", name.c_str()));
243 values->push_back(count);
244 }
245 }
246
247 void GrRecordingContext::DMSAAStats::dump() const {
248 SkDebugf("DMSAA Render Passes: %d\n", fNumRenderPasses);
249 SkDebugf("DMSAA Multisample Render Passes: %d\n", fNumMultisampleRenderPasses);
250 if (!fTriggerCounts.empty()) {
251 SkDebugf("DMSAA Triggers:\n");
252 for (const auto& [name, count] : fTriggerCounts) {
253 SkDebugf(" %s: %d\n", name.c_str(), count);
254 }
255 }
256 }
257
258 void GrRecordingContext::DMSAAStats::merge(const DMSAAStats& stats) {
259 fNumRenderPasses += stats.fNumRenderPasses;
260 fNumMultisampleRenderPasses += stats.fNumMultisampleRenderPasses;
261 for (const auto& [name, count] : stats.fTriggerCounts) {
262 fTriggerCounts[name] += count;
263 }
264 }
265
266 #endif // GR_GPU_STATS
267 #endif // defined(GPU_TEST_UTILS)