| 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/SkColorType.h" |
| 12 |
#include "include/core/SkRefCnt.h" |
| 13 |
#include "include/core/SkString.h" // IWYU pragma: keep |
| 14 |
#include "include/core/SkTypes.h" |
| 15 |
#include "include/private/base/SkTArray.h" |
| 16 |
#include "include/private/gpu/ganesh/GrContext_Base.h" |
| 17 |
#include "include/private/gpu/ganesh/GrImageContext.h" |
| 18 |
|
| 19 |
#include <map> |
| 20 |
#include <memory> |
| 21 |
#include <string> |
| 22 |
|
| 23 |
class GrAuditTrail; |
| 24 |
class GrContextThreadSafeProxy; |
| 25 |
class GrDirectContext; |
| 26 |
class GrDrawingManager; |
| 27 |
class GrOnFlushCallbackObject; |
| 28 |
class GrProgramDesc; |
| 29 |
class GrProgramInfo; |
| 30 |
class GrProxyProvider; |
| 31 |
class GrRecordingContextPriv; |
| 32 |
class GrThreadSafeCache; |
| 33 |
class SkArenaAlloc; |
| 34 |
class SkCapabilities; |
| 35 |
class SkJSONWriter; |
| 36 |
class SkGaneshRecorder; |
| 37 |
class SkRecorder; |
| 38 |
|
| 39 |
namespace skcpu { |
| 40 |
class ContextImpl; |
| 41 |
class Recorder; |
| 42 |
} // namespace skcpu |
| 43 |
|
| 44 |
namespace sktext::gpu { |
| 45 |
class SubRunAllocator; |
| 46 |
class TextBlobRedrawCoordinator; |
| 47 |
} |
| 48 |
|
| 49 |
class GrRecordingContext : public GrImageContext { |
| 50 |
public: |
| 51 |
~GrRecordingContext() override; |
| 52 |
|
| 53 |
/** |
| 54 |
* Reports whether the GrDirectContext associated with this GrRecordingContext is abandoned. |
| 55 |
* When called on a GrDirectContext it may actively check whether the underlying 3D API |
| 56 |
* device/context has been disconnected before reporting the status. If so, calling this |
| 57 |
* method will transition the GrDirectContext to the abandoned state. |
| 58 |
*/ |
| 59 |
bool abandoned() override { return GrImageContext::abandoned(); } |
| 60 |
|
| 61 |
/* |
| 62 |
* Can a SkSurface be created with the given color type. To check whether MSAA is supported |
| 63 |
* use maxSurfaceSampleCountForColorType(). |
| 64 |
*/ |
| 65 |
SK_API bool colorTypeSupportedAsSurface(SkColorType colorType) const { |
| 66 |
if (colorType == kR16G16_unorm_SkColorType || |
| 67 |
colorType == kA16_unorm_SkColorType || |
| 68 |
colorType == kA16_float_SkColorType || |
| 69 |
colorType == kR16_unorm_SkColorType || |
| 70 |
colorType == kR16G16_float_SkColorType || |
| 71 |
colorType == kR16G16B16A16_unorm_SkColorType || |
| 72 |
colorType == kGray_8_SkColorType) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
return this->maxSurfaceSampleCountForColorType(colorType) > 0; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Gets the maximum supported texture size. |
| 81 |
*/ |
| 82 |
SK_API int maxTextureSize() const; |
| 83 |
|
| 84 |
/** |
| 85 |
* Gets the maximum supported render target size. |
| 86 |
*/ |
| 87 |
SK_API int maxRenderTargetSize() const; |
| 88 |
|
| 89 |
/** |
| 90 |
* Can a SkImage be created with the given color type. |
| 91 |
*/ |
| 92 |
SK_API bool colorTypeSupportedAsImage(SkColorType) const; |
| 93 |
|
| 94 |
/** |
| 95 |
* Does this context support protected content? |
| 96 |
*/ |
| 97 |
SK_API bool supportsProtectedContent() const; |
| 98 |
|
| 99 |
/** |
| 100 |
* Gets the maximum supported sample count for a color type. 1 is returned if only non-MSAA |
| 101 |
* rendering is supported for the color type. 0 is returned if rendering to this color type |
| 102 |
* is not supported at all. |
| 103 |
*/ |
| 104 |
SK_API int maxSurfaceSampleCountForColorType(SkColorType colorType) const { |
| 105 |
return GrImageContext::maxSurfaceSampleCountForColorType(colorType); |
| 106 |
} |
| 107 |
|
| 108 |
SK_API sk_sp<const SkCapabilities> skCapabilities() const; |
| 109 |
|
| 110 |
SK_API SkRecorder* asRecorder(); |
| 111 |
SK_API std::unique_ptr<skcpu::Recorder> makeCPURecorder(); |
| 112 |
|
| 113 |
// Provides access to functions that aren't part of the public API. |
| 114 |
GrRecordingContextPriv priv(); |
| 115 |
const GrRecordingContextPriv priv() const; // NOLINT(readability-const-return-type) |
| 116 |
|
| 117 |
// The collection of specialized memory arenas for different types of data recorded by a |
| 118 |
// GrRecordingContext. Arenas does not maintain ownership of the pools it groups together. |
| 119 |
class Arenas { |
| 120 |
public: |
| 121 |
Arenas(SkArenaAlloc*, sktext::gpu::SubRunAllocator*); |
| 122 |
|
| 123 |
// For storing pipelines and other complex data as-needed by ops |
| 124 |
SkArenaAlloc* recordTimeAllocator() { return fRecordTimeAllocator; } |
| 125 |
|
| 126 |
// For storing GrTextBlob SubRuns |
| 127 |
sktext::gpu::SubRunAllocator* recordTimeSubRunAllocator() { |
| 128 |
return fRecordTimeSubRunAllocator; |
| 129 |
} |
| 130 |
|
| 131 |
private: |
| 132 |
SkArenaAlloc* fRecordTimeAllocator; |
| 133 |
sktext::gpu::SubRunAllocator* fRecordTimeSubRunAllocator; |
| 134 |
}; |
| 135 |
|
| 136 |
protected: |
| 137 |
friend class GrRecordingContextPriv; // for hidden functions |
| 138 |
friend class GrDeferredDisplayList; // for OwnedArenas |
| 139 |
friend class GrDeferredDisplayListPriv; // for ProgramData |
| 140 |
|
| 141 |
// Like Arenas, but preserves ownership of the underlying pools. |
| 142 |
class OwnedArenas { |
| 143 |
public: |
| 144 |
OwnedArenas(bool ddlRecording); |
| 145 |
~OwnedArenas(); |
| 146 |
|
| 147 |
Arenas get(); |
| 148 |
|
| 149 |
OwnedArenas& operator=(OwnedArenas&&); |
| 150 |
|
| 151 |
private: |
| 152 |
bool fDDLRecording; |
| 153 |
std::unique_ptr<SkArenaAlloc> fRecordTimeAllocator; |
| 154 |
std::unique_ptr<sktext::gpu::SubRunAllocator> fRecordTimeSubRunAllocator; |
| 155 |
}; |
| 156 |
|
| 157 |
GrRecordingContext(sk_sp<GrContextThreadSafeProxy>, bool ddlRecording); |
| 158 |
|
| 159 |
bool init() override; |
| 160 |
|
| 161 |
void abandonContext() override; |
| 162 |
|
| 163 |
GrDrawingManager* drawingManager(); |
| 164 |
|
| 165 |
// There is no going back from this method. It should only be called to control the timing |
| 166 |
// during abandon or destruction of the context. |
| 167 |
void destroyDrawingManager(); |
| 168 |
|
| 169 |
Arenas arenas() { return fArenas.get(); } |
| 170 |
// This entry point should only be used for DDL creation where we want the ops' lifetime to |
| 171 |
// match that of the DDL. |
| 172 |
OwnedArenas&& detachArenas(); |
| 173 |
|
| 174 |
GrProxyProvider* proxyProvider() { return fProxyProvider.get(); } |
| 175 |
const GrProxyProvider* proxyProvider() const { return fProxyProvider.get(); } |
| 176 |
|
| 177 |
struct ProgramData { |
| 178 |
ProgramData(std::unique_ptr<const GrProgramDesc>, const GrProgramInfo*); |
| 179 |
ProgramData(ProgramData&&); // for SkTArray |
| 180 |
ProgramData(const ProgramData&) = delete; |
| 181 |
~ProgramData(); |
| 182 |
|
| 183 |
const GrProgramDesc& desc() const { return *fDesc; } |
| 184 |
const GrProgramInfo& info() const { return *fInfo; } |
| 185 |
|
| 186 |
private: |
| 187 |
// TODO: store the GrProgramDescs in the 'fRecordTimeData' arena |
| 188 |
std::unique_ptr<const GrProgramDesc> fDesc; |
| 189 |
// The program infos should be stored in 'fRecordTimeData' so do not need to be ref |
| 190 |
// counted or deleted in the destructor. |
| 191 |
const GrProgramInfo* fInfo = nullptr; |
| 192 |
}; |
| 193 |
|
| 194 |
// This entry point gives the recording context a chance to cache the provided |
| 195 |
// programInfo. The DDL context takes this opportunity to store programInfos as a sidecar |
| 196 |
// to the DDL. |
| 197 |
virtual void recordProgramInfo(const GrProgramInfo*) {} |
| 198 |
// This asks the recording context to return any programInfos it may have collected |
| 199 |
// via the 'recordProgramInfo' call. It is up to the caller to ensure that the lifetime |
| 200 |
// of the programInfos matches the intended use. For example, in DDL-record mode it |
| 201 |
// is known that all the programInfos will have been allocated in an arena with the |
| 202 |
// same lifetime at the DDL itself. |
| 203 |
virtual void detachProgramData(skia_private::TArray<ProgramData>*) {} |
| 204 |
|
| 205 |
sktext::gpu::TextBlobRedrawCoordinator* getTextBlobRedrawCoordinator(); |
| 206 |
const sktext::gpu::TextBlobRedrawCoordinator* getTextBlobRedrawCoordinator() const; |
| 207 |
|
| 208 |
GrThreadSafeCache* threadSafeCache(); |
| 209 |
const GrThreadSafeCache* threadSafeCache() const; |
| 210 |
|
| 211 |
/** |
| 212 |
* Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.) |
| 213 |
* |
| 214 |
* NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to |
| 215 |
* ensure its lifetime is tied to that of the context. |
| 216 |
*/ |
| 217 |
void addOnFlushCallbackObject(GrOnFlushCallbackObject*); |
| 218 |
|
| 219 |
GrRecordingContext* asRecordingContext() override { return this; } |
| 220 |
|
| 221 |
class Stats { |
| 222 |
public: |
| 223 |
Stats() = default; |
| 224 |
|
| 225 |
#if GR_GPU_STATS |
| 226 |
void reset() { *this = {}; } |
| 227 |
|
| 228 |
int numPathMasksGenerated() const { return fNumPathMasksGenerated; } |
| 229 |
void incNumPathMasksGenerated() { fNumPathMasksGenerated++; } |
| 230 |
|
| 231 |
int numPathMaskCacheHits() const { return fNumPathMaskCacheHits; } |
| 232 |
void incNumPathMasksCacheHits() { fNumPathMaskCacheHits++; } |
| 233 |
|
| 234 |
#if defined(GPU_TEST_UTILS) |
| 235 |
void dump(SkString* out) const; |
| 236 |
void dumpKeyValuePairs(skia_private::TArray<SkString>* keys, |
| 237 |
skia_private::TArray<double>* values) const; |
| 238 |
#endif |
| 239 |
|
| 240 |
private: |
| 241 |
int fNumPathMasksGenerated{0}; |
| 242 |
int fNumPathMaskCacheHits{0}; |
| 243 |
|
| 244 |
#else // GR_GPU_STATS |
| 245 |
void incNumPathMasksGenerated() {} |
| 246 |
void incNumPathMasksCacheHits() {} |
| 247 |
|
| 248 |
#if defined(GPU_TEST_UTILS) |
| 249 |
void dump(SkString*) const {} |
| 250 |
void dumpKeyValuePairs(skia_private::TArray<SkString>* keys, |
| 251 |
skia_private::TArray<double>* values) const {} |
| 252 |
#endif |
| 253 |
#endif // GR_GPU_STATS |
| 254 |
} fStats; |
| 255 |
|
| 256 |
#if GR_GPU_STATS && defined(GPU_TEST_UTILS) |
| 257 |
struct DMSAAStats { |
| 258 |
void dumpKeyValuePairs(skia_private::TArray<SkString>* keys, |
| 259 |
skia_private::TArray<double>* values) const; |
| 260 |
void dump() const; |
| 261 |
void merge(const DMSAAStats&); |
| 262 |
int fNumRenderPasses = 0; |
| 263 |
int fNumMultisampleRenderPasses = 0; |
| 264 |
std::map<std::string, int> fTriggerCounts; |
| 265 |
}; |
| 266 |
|
| 267 |
DMSAAStats fDMSAAStats; |
| 268 |
#endif |
| 269 |
|
| 270 |
Stats* stats() { return &fStats; } |
| 271 |
const Stats* stats() const { return &fStats; } |
| 272 |
void dumpJSON(SkJSONWriter*) const; |
| 273 |
|
| 274 |
protected: |
| 275 |
// Delete last in case other objects call it during destruction. |
| 276 |
std::unique_ptr<GrAuditTrail> fAuditTrail; |
| 277 |
|
| 278 |
private: |
| 279 |
OwnedArenas fArenas; |
| 280 |
|
| 281 |
std::unique_ptr<GrDrawingManager> fDrawingManager; |
| 282 |
std::unique_ptr<GrProxyProvider> fProxyProvider; |
| 283 |
std::unique_ptr<const skcpu::ContextImpl> fCPUContext; |
| 284 |
std::unique_ptr<SkGaneshRecorder> fRecorder; |
| 285 |
|
| 286 |
#if defined(GPU_TEST_UTILS) |
| 287 |
int fSuppressWarningMessages = 0; |
| 288 |
#endif |
| 289 |
}; |
| 290 |
|
| 291 |
/** |
| 292 |
* Safely cast a possibly-null base context to direct context. |
| 293 |
*/ |
| 294 |
static inline GrDirectContext* GrAsDirectContext(GrContext_Base* base) { |
| 295 |
return base ? base->asDirectContext() : nullptr; |
| 296 |
} |
| 297 |
|
| 298 |
#endif |