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 #ifndef GrRecordingContextPriv_DEFINED
9 #define GrRecordingContextPriv_DEFINED
10
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkTypes.h"
13 #include "include/gpu/GrRecordingContext.h"
14 #include "src/gpu/RefCntedCallback.h"
15 #include "src/gpu/SkBackingFit.h"
16 #include "src/gpu/ganesh/Device.h"
17 #include "src/gpu/ganesh/GrImageContextPriv.h"
18 #include "src/text/gpu/SDFTControl.h"
19
20 class GrImageInfo;
21 class GrDeferredDisplayList;
22 namespace skgpu {
23 class Swizzle;
24 }
25 namespace skgpu::ganesh {
26 class SurfaceContext;
27 class SurfaceFillContext;
28 } // namespace skgpu::ganesh
29
30 /** Class that exposes methods on GrRecordingContext that are only intended for use internal to
31 Skia. This class is purely a privileged window into GrRecordingContext. It should never have
32 additional data members or virtual methods. */
33 class GrRecordingContextPriv : public GrImageContextPriv {
34 public:
context()35 GrRecordingContext* context() { return static_cast<GrRecordingContext*>(fContext); }
context()36 const GrRecordingContext* context() const {
37 return static_cast<const GrRecordingContext*>(fContext);
38 }
39
proxyProvider()40 GrProxyProvider* proxyProvider() { return this->context()->proxyProvider(); }
proxyProvider()41 const GrProxyProvider* proxyProvider() const { return this->context()->proxyProvider(); }
42
drawingManager()43 GrDrawingManager* drawingManager() { return this->context()->drawingManager(); }
44
recordTimeAllocator()45 SkArenaAlloc* recordTimeAllocator() { return this->context()->arenas().recordTimeAllocator(); }
recordTimeSubRunAllocator()46 sktext::gpu::SubRunAllocator* recordTimeSubRunAllocator() {
47 return this->context()->arenas().recordTimeSubRunAllocator();
48 }
arenas()49 GrRecordingContext::Arenas arenas() { return this->context()->arenas(); }
50
detachArenas()51 GrRecordingContext::OwnedArenas&& detachArenas() { return this->context()->detachArenas(); }
52
recordProgramInfo(const GrProgramInfo * programInfo)53 void recordProgramInfo(const GrProgramInfo* programInfo) {
54 this->context()->recordProgramInfo(programInfo);
55 }
56
detachProgramData(skia_private::TArray<GrRecordingContext::ProgramData> * dst)57 void detachProgramData(skia_private::TArray<GrRecordingContext::ProgramData>* dst) {
58 this->context()->detachProgramData(dst);
59 }
60
getTextBlobCache()61 sktext::gpu::TextBlobRedrawCoordinator* getTextBlobCache() {
62 return this->context()->getTextBlobRedrawCoordinator();
63 }
64
threadSafeCache()65 GrThreadSafeCache* threadSafeCache() { return this->context()->threadSafeCache(); }
66
67 void moveRenderTasksToDDL(GrDeferredDisplayList*);
68
69 /**
70 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
71 *
72 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
73 * ensure its lifetime is tied to that of the context.
74 */
75 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
76
auditTrail()77 GrAuditTrail* auditTrail() { return this->context()->fAuditTrail.get(); }
78
79 #if defined(GR_TEST_UTILS)
80 // Used by tests that intentionally exercise codepaths that print warning messages, in order to
81 // not confuse users with output that looks like a testing failure.
82 class AutoSuppressWarningMessages {
83 public:
AutoSuppressWarningMessages(GrRecordingContext * context)84 AutoSuppressWarningMessages(GrRecordingContext* context) : fContext(context) {
85 ++fContext->fSuppressWarningMessages;
86 }
~AutoSuppressWarningMessages()87 ~AutoSuppressWarningMessages() {
88 --fContext->fSuppressWarningMessages;
89 }
90 private:
91 GrRecordingContext* fContext;
92 };
incrSuppressWarningMessages()93 void incrSuppressWarningMessages() { ++this->context()->fSuppressWarningMessages; }
decrSuppressWarningMessages()94 void decrSuppressWarningMessages() { --this->context()->fSuppressWarningMessages; }
95 #endif
96
printWarningMessage(const char * msg)97 void printWarningMessage(const char* msg) const {
98 #if defined(GR_TEST_UTILS)
99 if (this->context()->fSuppressWarningMessages > 0) {
100 return;
101 }
102 #endif
103 SkDebugf("%s", msg);
104 }
105
stats()106 GrRecordingContext::Stats* stats() {
107 return &this->context()->fStats;
108 }
109
110 #if GR_GPU_STATS && defined(GR_TEST_UTILS)
111 using DMSAAStats = GrRecordingContext::DMSAAStats;
dmsaaStats()112 DMSAAStats& dmsaaStats() { return this->context()->fDMSAAStats; }
113 #endif
114
115 sktext::gpu::SDFTControl getSDFTControl(bool useSDFTForSmallText) const;
116
117 /**
118 * Create a GrRecordingContext without a resource cache
119 */
120 static sk_sp<GrRecordingContext> MakeDDL(sk_sp<GrContextThreadSafeProxy>);
121
122 sk_sp<skgpu::ganesh::Device> createDevice(GrColorType,
123 sk_sp<GrSurfaceProxy>,
124 sk_sp<SkColorSpace>,
125 GrSurfaceOrigin,
126 const SkSurfaceProps&,
127 skgpu::ganesh::Device::InitContents);
128 sk_sp<skgpu::ganesh::Device> createDevice(skgpu::Budgeted,
129 const SkImageInfo&,
130 SkBackingFit,
131 int sampleCount,
132 skgpu::Mipmapped,
133 skgpu::Protected,
134 GrSurfaceOrigin,
135 const SkSurfaceProps&,
136 skgpu::ganesh::Device::InitContents);
137
138 // If the passed in GrSurfaceProxy is renderable this will return a SurfaceDrawContext,
139 // otherwise it will return a SurfaceContext.
140 std::unique_ptr<skgpu::ganesh::SurfaceContext> makeSC(GrSurfaceProxyView readView,
141 const GrColorInfo&);
142
143 // Makes either a SurfaceContext, SurfaceFillContext, or a SurfaceDrawContext, depending on
144 // GrRenderable and the GrImageInfo.
145 std::unique_ptr<skgpu::ganesh::SurfaceContext> makeSC(
146 const GrImageInfo&,
147 const GrBackendFormat&,
148 std::string_view label,
149 SkBackingFit = SkBackingFit::kExact,
150 GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
151 skgpu::Renderable = skgpu::Renderable::kNo,
152 int renderTargetSampleCnt = 1,
153 skgpu::Mipmapped = skgpu::Mipmapped::kNo,
154 skgpu::Protected = skgpu::Protected::kNo,
155 skgpu::Budgeted = skgpu::Budgeted::kYes);
156
157 /**
158 * Uses GrImageInfo's color type to pick the default texture format. Will return a
159 * SurfaceDrawContext if possible.
160 */
161 std::unique_ptr<skgpu::ganesh::SurfaceFillContext> makeSFC(
162 GrImageInfo,
163 std::string_view label,
164 SkBackingFit = SkBackingFit::kExact,
165 int sampleCount = 1,
166 skgpu::Mipmapped = skgpu::Mipmapped::kNo,
167 skgpu::Protected = skgpu::Protected::kNo,
168 GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
169 skgpu::Budgeted = skgpu::Budgeted::kYes);
170
171 /**
172 * Makes a custom configured SurfaceFillContext where the caller specifies the specific
173 * texture format and swizzles. The color type will be kUnknown. Returns a SurfaceDrawContext
174 * if possible.
175 */
176 std::unique_ptr<skgpu::ganesh::SurfaceFillContext> makeSFC(SkAlphaType,
177 sk_sp<SkColorSpace>,
178 SkISize dimensions,
179 SkBackingFit,
180 const GrBackendFormat&,
181 int sampleCount,
182 skgpu::Mipmapped,
183 skgpu::Protected,
184 skgpu::Swizzle readSwizzle,
185 skgpu::Swizzle writeSwizzle,
186 GrSurfaceOrigin,
187 skgpu::Budgeted,
188 std::string_view label);
189
190 /**
191 * Like the above but uses GetFallbackColorTypeAndFormat to find a fallback color type (and
192 * compatible format) if the passed GrImageInfo's color type is not renderable.
193 */
194 std::unique_ptr<skgpu::ganesh::SurfaceFillContext> makeSFCWithFallback(
195 GrImageInfo,
196 SkBackingFit,
197 int sampleCount,
198 skgpu::Mipmapped,
199 skgpu::Protected,
200 GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
201 skgpu::Budgeted = skgpu::Budgeted::kYes);
202
203 /**
204 * Creates a SurfaceFillContext from an existing GrBackendTexture. The GrColorInfo's color
205 * type must be compatible with backend texture's format or this will fail. All formats are
206 * considered compatible with kUnknown. Returns a SurfaceDrawContext if possible.
207 */
208 std::unique_ptr<skgpu::ganesh::SurfaceFillContext> makeSFCFromBackendTexture(
209 GrColorInfo,
210 const GrBackendTexture&,
211 int sampleCount,
212 GrSurfaceOrigin,
213 sk_sp<skgpu::RefCntedCallback> releaseHelper);
214
215 protected:
GrRecordingContextPriv(GrRecordingContext * rContext)216 explicit GrRecordingContextPriv(GrRecordingContext* rContext) : GrImageContextPriv(rContext) {}
217
218 private:
219 GrRecordingContextPriv(const GrRecordingContextPriv&) = delete;
220 GrRecordingContextPriv& operator=(const GrRecordingContextPriv&) = delete;
221
222 // No taking addresses of this type.
223 const GrRecordingContextPriv* operator&() const;
224 GrRecordingContextPriv* operator&();
225
226 friend class GrRecordingContext; // to construct/copy this type.
227
228 using INHERITED = GrImageContextPriv;
229 };
230
priv()231 inline GrRecordingContextPriv GrRecordingContext::priv() { return GrRecordingContextPriv(this); }
232
priv()233 inline const GrRecordingContextPriv GrRecordingContext::priv () const { // NOLINT(readability-const-return-type)
234 return GrRecordingContextPriv(const_cast<GrRecordingContext*>(this));
235 }
236
237 #endif
238