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