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/private/GrRecordingContext.h"
9
10#include "include/gpu/GrContext.h"
11#include "src/core/SkArenaAlloc.h"
12#include "src/gpu/GrAuditTrail.h"
13#include "src/gpu/GrCaps.h"
14#include "src/gpu/GrDrawingManager.h"
15#include "src/gpu/GrMemoryPool.h"
16#include "src/gpu/GrProgramDesc.h"
17#include "src/gpu/GrProxyProvider.h"
18#include "src/gpu/GrRecordingContextPriv.h"
19#include "src/gpu/GrRenderTargetContext.h"
20#include "src/gpu/GrSurfaceContext.h"
21#include "src/gpu/SkGr.h"
22#include "src/gpu/effects/GrSkSLFP.h"
23#include "src/gpu/text/GrTextBlobCache.h"
24
25#define ASSERT_SINGLE_OWNER_PRIV \
26 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
27
28GrRecordingContext::ProgramData::ProgramData(std::unique_ptr<const GrProgramDesc> desc,
29 const GrProgramInfo* info)
30 : fDesc(std::move(desc))
31 , fInfo(info) {
32}
33
34GrRecordingContext::ProgramData::ProgramData(ProgramData&& other)
35 : fDesc(std::move(other.fDesc))
36 , fInfo(other.fInfo) {
37}
38
39GrRecordingContext::ProgramData::~ProgramData() {}
40
41GrRecordingContext::GrRecordingContext(GrBackendApi backend,
42 const GrContextOptions& options,
43 uint32_t contextID)
44 : INHERITED(backend, options, contextID)
45 , fAuditTrail(new GrAuditTrail()) {
46}
47
48GrRecordingContext::~GrRecordingContext() { }
49
50/**
51 * TODO: move textblob draw calls below context (see comment below)
52 */
53static void textblobcache_overbudget_CB(void* data) {
54 SkASSERT(data);
55 GrRecordingContext* context = reinterpret_cast<GrRecordingContext*>(data);
56
57 GrContext* direct = context->priv().asDirectContext();
58 if (!direct) {
59 return;
60 }
61
62 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
63 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
64 // to below the GrContext level, but this is not trivial because they call drawPath on
65 // SkGpuDevice.
66 direct->flush();
67}
68
69bool GrRecordingContext::init(sk_sp<const GrCaps> caps) {
70
71 if (!INHERITED::init(std::move(caps))) {
72 return false;
73 }
74
75 fTextBlobCache.reset(new GrTextBlobCache(textblobcache_overbudget_CB, this,
76 this->contextID()));
77
78 return true;
79}
80
81void GrRecordingContext::setupDrawingManager(bool sortOpsTasks, bool reduceOpsTaskSplitting) {
82 GrPathRendererChain::Options prcOptions;
83 prcOptions.fAllowPathMaskCaching = this->options().fAllowPathMaskCaching;
84#if GR_TEST_UTILS
85 prcOptions.fGpuPathRenderers = this->options().fGpuPathRenderers;
86#endif
87 // FIXME: Once this is removed from Chrome and Android, rename to fEnable"".
88 if (!this->options().fDisableCoverageCountingPaths) {
89 prcOptions.fGpuPathRenderers |= GpuPathRenderers::kCoverageCounting;
90 }
91 if (this->options().fDisableDistanceFieldPaths) {
92 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
93 }
94
95 if (!this->proxyProvider()->renderingDirectly()) {
96 // DDL TODO: remove this crippling of the path renderer chain
97 // Disable the small path renderer bc of the proxies in the atlas. They need to be
98 // unified when the opsTasks are added back to the destination drawing manager.
99 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
100 }
101
102 GrTextContext::Options textContextOptions;
103 textContextOptions.fMaxDistanceFieldFontSize = this->options().fGlyphsAsPathsFontSize;
104 textContextOptions.fMinDistanceFieldFontSize = this->options().fMinDistanceFieldFontSize;
105 textContextOptions.fDistanceFieldVerticesAlwaysHaveW = false;
106#if SK_SUPPORT_ATLAS_TEXT
107 if (GrContextOptions::Enable::kYes == this->options().fDistanceFieldGlyphVerticesAlwaysHaveW) {
108 textContextOptions.fDistanceFieldVerticesAlwaysHaveW = true;
109 }
110#endif
111
112 fDrawingManager.reset(new GrDrawingManager(this,
113 prcOptions,
114 textContextOptions,
115 sortOpsTasks,
116 reduceOpsTaskSplitting));
117}
118
119void GrRecordingContext::abandonContext() {
120 INHERITED::abandonContext();
121
122 fTextBlobCache->freeAll();
123}
124
125GrDrawingManager* GrRecordingContext::drawingManager() {
126 return fDrawingManager.get();
127}
128
129GrRecordingContext::Arenas::Arenas(GrOpMemoryPool* opMemoryPool, SkArenaAlloc* recordTimeAllocator)
130 : fOpMemoryPool(opMemoryPool)
131 , fRecordTimeAllocator(recordTimeAllocator) {
132 // OwnedArenas should instantiate these before passing the bare pointer off to this struct.
133 SkASSERT(opMemoryPool);
134 SkASSERT(recordTimeAllocator);
135}
136
137// Must be defined here so that std::unique_ptr can see the sizes of the various pools, otherwise
138// it can't generate a default destructor for them.
139GrRecordingContext::OwnedArenas::OwnedArenas() {}
140GrRecordingContext::OwnedArenas::~OwnedArenas() {}
141
142GrRecordingContext::OwnedArenas& GrRecordingContext::OwnedArenas::operator=(OwnedArenas&& a) {
143 fOpMemoryPool = std::move(a.fOpMemoryPool);
144 fRecordTimeAllocator = std::move(a.fRecordTimeAllocator);
145 return *this;
146}
147
148GrRecordingContext::Arenas GrRecordingContext::OwnedArenas::get() {
149 if (!fOpMemoryPool) {
150 // DDL TODO: should the size of the memory pool be decreased in DDL mode? CPU-side memory
151 // consumed in DDL mode vs. normal mode for a single skp might be a good metric of wasted
152 // memory.
153 fOpMemoryPool = GrOpMemoryPool::Make(16384, 16384);
154 }
155
156 if (!fRecordTimeAllocator) {
157 // TODO: empirically determine a better number for SkArenaAlloc's firstHeapAllocation param
158 fRecordTimeAllocator = std::make_unique<SkArenaAlloc>(sizeof(GrPipeline) * 100);
159 }
160
161 return {fOpMemoryPool.get(), fRecordTimeAllocator.get()};
162}
163
164GrRecordingContext::OwnedArenas&& GrRecordingContext::detachArenas() {
165 return std::move(fArenas);
166}
167
168GrTextBlobCache* GrRecordingContext::getTextBlobCache() {
169 return fTextBlobCache.get();
170}
171
172const GrTextBlobCache* GrRecordingContext::getTextBlobCache() const {
173 return fTextBlobCache.get();
174}
175
176void GrRecordingContext::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
177 this->drawingManager()->addOnFlushCallbackObject(onFlushCBObject);
178}
179
180///////////////////////////////////////////////////////////////////////////////////////////////////
181sk_sp<const GrCaps> GrRecordingContextPriv::refCaps() const {
182 return fContext->refCaps();
183}
184
185void GrRecordingContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
186 fContext->addOnFlushCallbackObject(onFlushCBObject);
187}
188
189GrContext* GrRecordingContextPriv::backdoor() {
190 return (GrContext*) fContext;
191}
192
193