Skia
2D Graphics Library
GrRecordingContext.h
Go to the documentation of this file.
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 GrRecordingContext_DEFINED
9 #define GrRecordingContext_DEFINED
10 
11 #include "include/core/SkRefCnt.h"
12 #include "include/private/GrImageContext.h"
13 #include "include/private/SkTArray.h"
14 
15 class GrAuditTrail;
16 class GrBackendFormat;
17 class GrDrawingManager;
18 class GrOnFlushCallbackObject;
19 class GrMemoryPool;
20 class GrProgramDesc;
21 class GrProgramInfo;
22 class GrProxyProvider;
23 class GrRecordingContextPriv;
24 class GrSurfaceContext;
25 class GrSurfaceProxy;
26 class GrTextBlobCache;
27 class GrThreadSafeCache;
28 class SkArenaAlloc;
29 class SkJSONWriter;
30 
31 #if GR_TEST_UTILS
32 class SkString;
33 #endif
34 
35 class GrRecordingContext : public GrImageContext {
36 public:
37  ~GrRecordingContext() override;
38 
40  return INHERITED::defaultBackendFormat(ct, renderable);
41  }
42 
49  bool abandoned() override { return INHERITED::abandoned(); }
50 
51  /*
52  * Can a SkSurface be created with the given color type. To check whether MSAA is supported
53  * use maxSurfaceSampleCountForColorType().
54  */
56  if (kR16G16_unorm_SkColorType == colorType ||
57  kA16_unorm_SkColorType == colorType ||
58  kA16_float_SkColorType == colorType ||
59  kR16G16_float_SkColorType == colorType ||
60  kR16G16B16A16_unorm_SkColorType == colorType ||
61  kGray_8_SkColorType == colorType) {
62  return false;
63  }
64 
65  return this->maxSurfaceSampleCountForColorType(colorType) > 0;
66  }
67 
71  SK_API int maxTextureSize() const;
72 
77 
82 
89 
90  // Provides access to functions that aren't part of the public API.
92  const GrRecordingContextPriv priv() const; // NOLINT(readability-const-return-type)
93 
94  // The collection of specialized memory arenas for different types of data recorded by a
95  // GrRecordingContext. Arenas does not maintain ownership of the pools it groups together.
96  class Arenas {
97  public:
98  Arenas(GrMemoryPool*, SkArenaAlloc*);
99 
100  // For storing GrOp-derived classes recorded by a GrRecordingContext
101  GrMemoryPool* opMemoryPool() { return fOpMemoryPool; }
102 
103  // For storing pipelines and other complex data as-needed by ops
104  SkArenaAlloc* recordTimeAllocator() { return fRecordTimeAllocator; }
105 
106  private:
107  GrMemoryPool* fOpMemoryPool;
108  SkArenaAlloc* fRecordTimeAllocator;
109  };
110 
111 protected:
112  friend class GrRecordingContextPriv; // for hidden functions
113  friend class SkDeferredDisplayList; // for OwnedArenas
114  friend class SkDeferredDisplayListPriv; // for ProgramData
115 
116  // Like Arenas, but preserves ownership of the underlying pools.
117  class OwnedArenas {
118  public:
121 
123 
125 
126  private:
127  std::unique_ptr<GrMemoryPool> fOpMemoryPool;
128  std::unique_ptr<SkArenaAlloc> fRecordTimeAllocator;
129  };
130 
132 
133  bool init() override;
134 
135  void abandonContext() override;
136 
137  GrDrawingManager* drawingManager();
138 
139  // There is no going back from this method. It should only be called to control the timing
140  // during abandon or destruction of the context.
142 
143  Arenas arenas() { return fArenas.get(); }
144  // This entry point should only be used for DDL creation where we want the ops' lifetime to
145  // match that of the DDL.
147 
148  GrProxyProvider* proxyProvider() { return fProxyProvider.get(); }
149  const GrProxyProvider* proxyProvider() const { return fProxyProvider.get(); }
150 
151  struct ProgramData {
152  ProgramData(std::unique_ptr<const GrProgramDesc>, const GrProgramInfo*);
153  ProgramData(ProgramData&&); // for SkTArray
154  ProgramData(const ProgramData&) = delete;
156 
157  const GrProgramDesc& desc() const { return *fDesc; }
158  const GrProgramInfo& info() const { return *fInfo; }
159 
160  private:
161  // TODO: store the GrProgramDescs in the 'fRecordTimeData' arena
162  std::unique_ptr<const GrProgramDesc> fDesc;
163  // The program infos should be stored in 'fRecordTimeData' so do not need to be ref
164  // counted or deleted in the destructor.
165  const GrProgramInfo* fInfo = nullptr;
166  };
167 
168  // This entry point gives the recording context a chance to cache the provided
169  // programInfo. The DDL context takes this opportunity to store programInfos as a sidecar
170  // to the DDL.
171  virtual void recordProgramInfo(const GrProgramInfo*) {}
172  // This asks the recording context to return any programInfos it may have collected
173  // via the 'recordProgramInfo' call. It is up to the caller to ensure that the lifetime
174  // of the programInfos matches the intended use. For example, in DDL-record mode it
175  // is known that all the programInfos will have been allocated in an arena with the
176  // same lifetime at the DDL itself.
177  virtual void detachProgramData(SkTArray<ProgramData>*) {}
178 
179  GrTextBlobCache* getTextBlobCache();
180  const GrTextBlobCache* getTextBlobCache() const;
181 
182  GrThreadSafeCache* threadSafeCache();
183  const GrThreadSafeCache* threadSafeCache() const;
184 
191  void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
192 
193  GrAuditTrail* auditTrail() { return fAuditTrail.get(); }
194 
195  GrRecordingContext* asRecordingContext() override { return this; }
196 
197  class Stats {
198  public:
199  Stats() = default;
200 
201 #if GR_GPU_STATS
202  void reset() { *this = {}; }
203 
204  int numPathMasksGenerated() const { return fNumPathMasksGenerated; }
205  void incNumPathMasksGenerated() { fNumPathMasksGenerated++; }
206 
207  int numPathMaskCacheHits() const { return fNumPathMaskCacheHits; }
208  void incNumPathMasksCacheHits() { fNumPathMaskCacheHits++; }
209 
210 #if GR_TEST_UTILS
211  void dump(SkString* out);
212  void dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values);
213 #endif
214 
215  private:
216  int fNumPathMasksGenerated{0};
217  int fNumPathMaskCacheHits{0};
218 
219 #else // GR_GPU_STATS
222 
223 #if GR_TEST_UTILS
224  void dump(SkString*) {}
225  void dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {}
226 #endif
227 #endif // GR_GPU_STATS
229 
230  Stats* stats() { return &fStats; }
231  const Stats* stats() const { return &fStats; }
232  void dumpJSON(SkJSONWriter*) const;
233 
234 private:
235  // Delete last in case other objects call it during destruction.
236  std::unique_ptr<GrAuditTrail> fAuditTrail;
237 
238  OwnedArenas fArenas;
239 
240  std::unique_ptr<GrDrawingManager> fDrawingManager;
241  std::unique_ptr<GrProxyProvider> fProxyProvider;
242 
243 #if GR_TEST_UTILS
244  int fSuppressWarningMessages = 0;
245 #endif
246 
247  using INHERITED = GrImageContext;
248 };
249 
253 static inline GrDirectContext* GrAsDirectContext(GrContext_Base* base) {
254  return base ? base->asDirectContext() : nullptr;
255 }
256 
257 #endif
GrRecordingContext
Definition: GrRecordingContext.h:35
GrRecordingContext::OwnedArenas::get
Arenas get()
GrRecordingContext::threadSafeCache
const GrThreadSafeCache * threadSafeCache() const
kR16G16_unorm_SkColorType
@ kR16G16_unorm_SkColorType
pixel with a little endian uint16_t for red and green
Definition: SkImageInfo.h:90
GrRecordingContext::OwnedArenas::~OwnedArenas
~OwnedArenas()
kGray_8_SkColorType
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition: SkImageInfo.h:76
GrRecordingContext::maxSurfaceSampleCountForColorType
SK_API int maxSurfaceSampleCountForColorType(SkColorType) const
Gets the maximum supported sample count for a color type.
GrRecordingContext::defaultBackendFormat
SK_API GrBackendFormat defaultBackendFormat(SkColorType ct, GrRenderable renderable) const
Definition: GrRecordingContext.h:39
GrRecordingContext::getTextBlobCache
const GrTextBlobCache * getTextBlobCache() const
GrRecordingContext::SkDeferredDisplayListPriv
friend class SkDeferredDisplayListPriv
Definition: GrRecordingContext.h:114
GrRecordingContext::Arenas
Definition: GrRecordingContext.h:96
GrRecordingContext::asRecordingContext
GrRecordingContext * asRecordingContext() override
Definition: GrRecordingContext.h:195
GrRecordingContext::ProgramData::ProgramData
ProgramData(std::unique_ptr< const GrProgramDesc >, const GrProgramInfo *)
GrRecordingContext::addOnFlushCallbackObject
void addOnFlushCallbackObject(GrOnFlushCallbackObject *)
Registers an object for flush-related callbacks.
GrRecordingContext::destroyDrawingManager
void destroyDrawingManager()
GrRecordingContext::ProgramData::desc
const GrProgramDesc & desc() const
Definition: GrRecordingContext.h:157
GrRecordingContext::stats
Stats * stats()
Definition: GrRecordingContext.h:230
kR16G16B16A16_unorm_SkColorType
@ kR16G16B16A16_unorm_SkColorType
pixel with a little endian uint16_t for red, green, blue
Definition: SkImageInfo.h:91
GrRecordingContext::ProgramData::info
const GrProgramInfo & info() const
Definition: GrRecordingContext.h:158
GrRecordingContext::colorTypeSupportedAsImage
SK_API bool colorTypeSupportedAsImage(SkColorType) const
Can a SkImage be created with the given color type.
GrRecordingContext::proxyProvider
const GrProxyProvider * proxyProvider() const
Definition: GrRecordingContext.h:149
GrRecordingContext::OwnedArenas::operator=
OwnedArenas & operator=(OwnedArenas &&)
GrRecordingContext::GrRecordingContextPriv
friend class GrRecordingContextPriv
Definition: GrRecordingContext.h:112
GrRecordingContext::getTextBlobCache
GrTextBlobCache * getTextBlobCache()
SkRefCnt.h
GrRecordingContext::colorTypeSupportedAsSurface
SK_API bool colorTypeSupportedAsSurface(SkColorType colorType) const
Definition: GrRecordingContext.h:55
GrRecordingContext::abandonContext
void abandonContext() override
GrRecordingContext::auditTrail
GrAuditTrail * auditTrail()
Definition: GrRecordingContext.h:193
sk_sp
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:215
kR16G16_float_SkColorType
@ kR16G16_float_SkColorType
pixel with a half float for red and green
Definition: SkImageInfo.h:87
GrRecordingContext::Arenas::recordTimeAllocator
SkArenaAlloc * recordTimeAllocator()
Definition: GrRecordingContext.h:104
GrRecordingContext::GrRecordingContext
GrRecordingContext(sk_sp< GrContextThreadSafeProxy >)
GrBackendFormat
Definition: GrBackendSurface.h:48
GrRecordingContext::abandoned
bool abandoned() override
Reports whether the GrDirectContext associated with this GrRecordingContext is abandoned.
Definition: GrRecordingContext.h:49
SK_API
#define SK_API
Definition: SkTypes.h:181
GrRecordingContext::priv
GrRecordingContextPriv priv()
GrRecordingContext::recordProgramInfo
virtual void recordProgramInfo(const GrProgramInfo *)
Definition: GrRecordingContext.h:171
GrRenderable
GrRenderable
Definition: GrTypes.h:179
GrRecordingContext::init
bool init() override
GrRecordingContext::ProgramData::ProgramData
ProgramData(ProgramData &&)
GrRecordingContext::OwnedArenas::OwnedArenas
OwnedArenas()
GrRecordingContext::ProgramData
Definition: GrRecordingContext.h:151
GrRecordingContext::Stats::Stats
Stats()=default
GrRecordingContext::Arenas::opMemoryPool
GrMemoryPool * opMemoryPool()
Definition: GrRecordingContext.h:101
GrRecordingContext::proxyProvider
GrProxyProvider * proxyProvider()
Definition: GrRecordingContext.h:148
GrRecordingContext::Stats::incNumPathMasksCacheHits
void incNumPathMasksCacheHits()
Definition: GrRecordingContext.h:221
GrRecordingContext::arenas
Arenas arenas()
Definition: GrRecordingContext.h:143
GrRecordingContext::detachProgramData
virtual void detachProgramData(SkTArray< ProgramData > *)
Definition: GrRecordingContext.h:177
kA16_unorm_SkColorType
@ kA16_unorm_SkColorType
pixel with a little endian uint16_t for alpha
Definition: SkImageInfo.h:89
GrRecordingContext::threadSafeCache
GrThreadSafeCache * threadSafeCache()
SkDeferredDisplayList
Definition: SkDeferredDisplayList.h:32
GrDirectContext
Definition: GrDirectContext.h:47
kA16_float_SkColorType
@ kA16_float_SkColorType
pixel with a half float for alpha
Definition: SkImageInfo.h:86
GrRecordingContext::Stats::incNumPathMasksGenerated
void incNumPathMasksGenerated()
Definition: GrRecordingContext.h:220
GrAsDirectContext
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
Safely cast a possibly-null base context to direct context.
Definition: GrRecordingContext.h:253
GrRecordingContext::~GrRecordingContext
~GrRecordingContext() override
GrRecordingContext::dumpJSON
void dumpJSON(SkJSONWriter *) const
SkString
Definition: SkString.h:116
GrRecordingContext::priv
const GrRecordingContextPriv priv() const
GrRecordingContext::Stats
Definition: GrRecordingContext.h:197
GrDirectContext::asDirectContext
GrDirectContext * asDirectContext() override
Definition: GrDirectContext.h:806
GrRecordingContext::maxRenderTargetSize
SK_API int maxRenderTargetSize() const
Gets the maximum supported render target size.
GrRecordingContext::maxTextureSize
SK_API int maxTextureSize() const
Gets the maximum supported texture size.
GrRecordingContext::ProgramData::ProgramData
ProgramData(const ProgramData &)=delete
SkColorType
SkColorType
Definition: SkImageInfo.h:64
GrRecordingContext::ProgramData::~ProgramData
~ProgramData()
GrRecordingContext::OwnedArenas
Definition: GrRecordingContext.h:117
GrRecordingContext::fStats
class GrRecordingContext::Stats fStats
GrRecordingContext::drawingManager
GrDrawingManager * drawingManager()
GrRecordingContext::stats
const Stats * stats() const
Definition: GrRecordingContext.h:231
GrRecordingContext::Arenas::Arenas
Arenas(GrMemoryPool *, SkArenaAlloc *)
GrRecordingContext::detachArenas
OwnedArenas && detachArenas()