| 1 |
/* |
| 2 |
* Copyright 2021 Google LLC |
| 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 skgpu_graphite_Recorder_DEFINED |
| 9 |
#define skgpu_graphite_Recorder_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkCPURecorder.h" |
| 12 |
#include "include/core/SkRecorder.h" |
| 13 |
#include "include/core/SkRefCnt.h" |
| 14 |
#include "include/core/SkSurface.h" |
| 15 |
#include "include/gpu/graphite/GraphiteTypes.h" |
| 16 |
#include "include/gpu/graphite/Recording.h" |
| 17 |
#include "include/private/base/SingleOwner.h" |
| 18 |
#include "include/private/base/SkAPI.h" |
| 19 |
#include "include/private/base/SkTArray.h" |
| 20 |
#include "include/private/base/SkTDArray.h" |
| 21 |
|
| 22 |
#include <chrono> |
| 23 |
#include <cstddef> |
| 24 |
#include <cstdint> |
| 25 |
#include <memory> |
| 26 |
#include <optional> |
| 27 |
|
| 28 |
class SkCanvas; |
| 29 |
class SkPixmap; |
| 30 |
class SkTraceMemoryDump; |
| 31 |
struct SkISize; |
| 32 |
struct SkImageInfo; |
| 33 |
|
| 34 |
#if defined(SK_BUILD_FOR_ANDROID) |
| 35 |
struct AHardwareBuffer; |
| 36 |
#endif |
| 37 |
|
| 38 |
namespace skgpu { |
| 39 |
enum class BackendApi : unsigned int; |
| 40 |
class RefCntedCallback; |
| 41 |
class TokenTracker; |
| 42 |
} |
| 43 |
|
| 44 |
namespace sktext::gpu { |
| 45 |
class StrikeCache; |
| 46 |
class TextBlobRedrawCoordinator; |
| 47 |
} |
| 48 |
|
| 49 |
namespace skgpu::graphite { |
| 50 |
|
| 51 |
class AtlasProvider; |
| 52 |
class BackendTexture; |
| 53 |
class Context; |
| 54 |
class Device; |
| 55 |
class DrawBufferManager; |
| 56 |
class FloatStorageManager; |
| 57 |
class ImageProvider; |
| 58 |
class PaintParamsKeyBuilder; |
| 59 |
class PipelineDataGatherer; |
| 60 |
class ProxyReadCountMap; |
| 61 |
class RecorderPriv; |
| 62 |
class ResourceProvider; |
| 63 |
class RuntimeEffectDictionary; |
| 64 |
class SharedContext; |
| 65 |
class TaskList; |
| 66 |
class TextureInfo; |
| 67 |
class UploadBufferManager; |
| 68 |
class UploadList; |
| 69 |
|
| 70 |
struct RecorderOptionsPriv; |
| 71 |
|
| 72 |
using KeyAndDataBuilder = std::pair<PipelineDataGatherer, PaintParamsKeyBuilder>; |
| 73 |
|
| 74 |
struct SK_API RecorderOptions final { |
| 75 |
RecorderOptions(); |
| 76 |
RecorderOptions(const RecorderOptions&); |
| 77 |
~RecorderOptions(); |
| 78 |
|
| 79 |
sk_sp<ImageProvider> fImageProvider; |
| 80 |
|
| 81 |
static constexpr size_t kDefaultRecorderBudget = 256 * (1 << 20); |
| 82 |
// What is the budget for GPU resources allocated and held by this Recorder. |
| 83 |
size_t fGpuBudgetInBytes = kDefaultRecorderBudget; |
| 84 |
// If Recordings are known to be played back in the order they are recorded, then Graphite |
| 85 |
// may be able to make certain assumptions that improve performance. This is often the case |
| 86 |
// if the content being drawn triggers the use of internal atlasing in Graphite (e.g. text). |
| 87 |
std::optional<bool> fRequireOrderedRecordings; |
| 88 |
|
| 89 |
// Private options that are only meant for testing within Skia's tools. |
| 90 |
RecorderOptionsPriv* fRecorderOptionsPriv = nullptr; |
| 91 |
}; |
| 92 |
|
| 93 |
class SK_API Recorder final : public SkRecorder { |
| 94 |
public: |
| 95 |
Recorder(const Recorder&) = delete; |
| 96 |
Recorder(Recorder&&) = delete; |
| 97 |
Recorder& operator=(const Recorder&) = delete; |
| 98 |
Recorder& operator=(Recorder&&) = delete; |
| 99 |
|
| 100 |
~Recorder() override; |
| 101 |
|
| 102 |
BackendApi backend() const; |
| 103 |
|
| 104 |
Type type() const override { return SkRecorder::Type::kGraphite; } |
| 105 |
skcpu::Recorder* cpuRecorder() override; |
| 106 |
|
| 107 |
std::unique_ptr<Recording> snap(); |
| 108 |
|
| 109 |
ImageProvider* clientImageProvider() { return fClientImageProvider.get(); } |
| 110 |
const ImageProvider* clientImageProvider() const { return fClientImageProvider.get(); } |
| 111 |
|
| 112 |
/** |
| 113 |
* Gets the maximum supported texture size. |
| 114 |
*/ |
| 115 |
int maxTextureSize() const; |
| 116 |
|
| 117 |
/** |
| 118 |
* Creates a new backend gpu texture matching the dimensions and TextureInfo. If an invalid |
| 119 |
* TextureInfo or a TextureInfo Skia can't support is passed in, this will return an invalid |
| 120 |
* BackendTexture. Thus the client should check isValid on the returned BackendTexture to know |
| 121 |
* if it succeeded or not. |
| 122 |
* |
| 123 |
* If this does return a valid BackendTexture, the caller is required to use |
| 124 |
* Recorder::deleteBackendTexture or Context::deleteBackendTexture to delete the texture. It is |
| 125 |
* safe to use the Context that created this Recorder or any other Recorder created from the |
| 126 |
* same Context to call deleteBackendTexture. |
| 127 |
*/ |
| 128 |
BackendTexture createBackendTexture(SkISize dimensions, const TextureInfo&); |
| 129 |
|
| 130 |
#ifdef SK_BUILD_FOR_ANDROID |
| 131 |
BackendTexture createBackendTexture(AHardwareBuffer*, |
| 132 |
bool isRenderable, |
| 133 |
bool isProtectedContent, |
| 134 |
SkISize dimensions, |
| 135 |
bool fromAndroidWindow = false) const; |
| 136 |
#endif |
| 137 |
|
| 138 |
/** |
| 139 |
* If possible, updates a backend texture with the provided pixmap data. The client |
| 140 |
* should check the return value to see if the update was successful. The client is required |
| 141 |
* to insert a Recording into the Context and call `submit` to send the upload work to the gpu. |
| 142 |
* The backend texture must be compatible with the provided pixmap(s). Compatible, in this case, |
| 143 |
* means that the backend format is compatible with the base pixmap's colortype. The src data |
| 144 |
* can be deleted when this call returns. When the BackendTexture is safe to be destroyed by the |
| 145 |
* client, Skia will call the passed in GpuFinishedProc. The BackendTexture should not be |
| 146 |
* destroyed before that. |
| 147 |
* If the backend texture is mip mapped, the data for all the mipmap levels must be provided. |
| 148 |
* In the mipmapped case all the colortypes of the provided pixmaps must be the same. |
| 149 |
* Additionally, all the miplevels must be sized correctly (please see |
| 150 |
* SkMipmap::ComputeLevelSize and ComputeLevelCount). |
| 151 |
* Note: the pixmap's alphatypes and colorspaces are ignored. |
| 152 |
* For the Vulkan backend after a successful update the layout of the created VkImage will be: |
| 153 |
* VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL |
| 154 |
*/ |
| 155 |
bool updateBackendTexture(const BackendTexture&, |
| 156 |
const SkPixmap srcData[], |
| 157 |
int numLevels, |
| 158 |
GpuFinishedProc = nullptr, |
| 159 |
GpuFinishedContext = nullptr); |
| 160 |
|
| 161 |
/** |
| 162 |
* If possible, updates a compressed backend texture filled with the provided raw data. The |
| 163 |
* client should check the return value to see if the update was successful. The client is |
| 164 |
* required to insert a Recording into the Context and call `submit` to send the upload work to |
| 165 |
* the gpu. When the BackendTexture is safe to be destroyed by the client, Skia will call the |
| 166 |
* passed in GpuFinishedProc. The BackendTexture should not be destroyed before that. |
| 167 |
* If the backend texture is mip mapped, the data for all the mipmap levels must be provided. |
| 168 |
* Additionally, all the miplevels must be sized correctly (please see |
| 169 |
* SkMipMap::ComputeLevelSize and ComputeLevelCount). |
| 170 |
* For the Vulkan backend after a successful update the layout of the created VkImage will be: |
| 171 |
* VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL |
| 172 |
*/ |
| 173 |
bool updateCompressedBackendTexture(const BackendTexture&, |
| 174 |
const void* data, |
| 175 |
size_t dataSize, |
| 176 |
GpuFinishedProc = nullptr, |
| 177 |
GpuFinishedContext = nullptr); |
| 178 |
|
| 179 |
/** |
| 180 |
* Called to delete the passed in BackendTexture. This should only be called if the |
| 181 |
* BackendTexture was created by calling Recorder::createBackendTexture on a Recorder that is |
| 182 |
* associated with the same Context. If the BackendTexture is not valid or does not match the |
| 183 |
* BackendApi of the Recorder then nothing happens. |
| 184 |
* |
| 185 |
* Otherwise this will delete/release the backend object that is wrapped in the BackendTexture. |
| 186 |
* The BackendTexture will be reset to an invalid state and should not be used again. |
| 187 |
*/ |
| 188 |
void deleteBackendTexture(const BackendTexture&); |
| 189 |
|
| 190 |
// Adds a proc that will be moved to the Recording upon snap, subsequently attached to the |
| 191 |
// CommandBuffer when the Recording is added, and called when that CommandBuffer is submitted |
| 192 |
// and finishes. If the Recorder or Recording is deleted before the proc is added to the |
| 193 |
// CommandBuffer, it will be called with result Failure. |
| 194 |
void addFinishInfo(const InsertFinishInfo&); |
| 195 |
|
| 196 |
// Returns a canvas that will record to a proxy surface, which must be instantiated on replay. |
| 197 |
// This can only be called once per Recording; subsequent calls will return null until a |
| 198 |
// Recording is snapped. Additionally, the returned SkCanvas is only valid until the next |
| 199 |
// Recording snap, at which point it is deleted. |
| 200 |
SkCanvas* makeDeferredCanvas(const SkImageInfo&, const TextureInfo&); |
| 201 |
|
| 202 |
/** |
| 203 |
* Frees GPU resources created and held by the Recorder. Can be called to reduce GPU memory |
| 204 |
* pressure. Any resources that are still in use (e.g. being used by work submitted to the GPU) |
| 205 |
* will not be deleted by this call. If the caller wants to make sure all resources are freed, |
| 206 |
* then they should first make sure to submit and wait on any outstanding work. |
| 207 |
*/ |
| 208 |
void freeGpuResources(); |
| 209 |
|
| 210 |
/** |
| 211 |
* Purge GPU resources on the Recorder that haven't been used in the past 'msNotUsed' |
| 212 |
* milliseconds or are otherwise marked for deletion, regardless of whether the context is under |
| 213 |
* budget. |
| 214 |
*/ |
| 215 |
void performDeferredCleanup(std::chrono::milliseconds msNotUsed); |
| 216 |
|
| 217 |
/** |
| 218 |
* Returns the number of bytes of the Recorder's gpu memory cache budget that are currently in |
| 219 |
* use. |
| 220 |
*/ |
| 221 |
size_t currentBudgetedBytes() const; |
| 222 |
|
| 223 |
/** |
| 224 |
* Returns the number of bytes of the Recorder's resource cache that are currently purgeable. |
| 225 |
*/ |
| 226 |
size_t currentPurgeableBytes() const; |
| 227 |
|
| 228 |
/** |
| 229 |
* Returns the size of Recorder's gpu memory cache budget in bytes. |
| 230 |
*/ |
| 231 |
size_t maxBudgetedBytes() const; |
| 232 |
|
| 233 |
/** |
| 234 |
* Sets the size of Recorders's gpu memory cache budget in bytes. If the new budget is lower |
| 235 |
* than the current budget, the cache will try to free resources to get under the new budget. |
| 236 |
*/ |
| 237 |
void setMaxBudgetedBytes(size_t bytes); |
| 238 |
|
| 239 |
/** |
| 240 |
* Enumerates all cached GPU resources owned by the Recorder and dumps their memory to |
| 241 |
* traceMemoryDump. |
| 242 |
*/ |
| 243 |
void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const; |
| 244 |
|
| 245 |
// Provides access to functions that aren't part of the public API. |
| 246 |
RecorderPriv priv(); |
| 247 |
const RecorderPriv priv() const; // NOLINT(readability-const-return-type) |
| 248 |
|
| 249 |
private: |
| 250 |
static constexpr int kMaxKeyAndDataBuilders = 2; |
| 251 |
|
| 252 |
friend class Context; // For ctor |
| 253 |
friend class Device; // For registering and deregistering Devices; |
| 254 |
friend class RecorderPriv; // for ctor and hidden methods |
| 255 |
|
| 256 |
// If Context is non-null, the Recorder will use the Context's resource provider |
| 257 |
// instead of creating its own. |
| 258 |
Recorder(sk_sp<SharedContext>, const RecorderOptions&, const Context*); |
| 259 |
|
| 260 |
SingleOwner* singleOwner() const { return &fSingleOwner; } |
| 261 |
|
| 262 |
// We keep track of all Devices that are connected to a Recorder. This allows the client to |
| 263 |
// safely delete an SkSurface or a Recorder in any order. If the client deletes the Recorder |
| 264 |
// we need to notify all Devices that the Recorder is no longer valid. If we delete the |
| 265 |
// SkSurface/Device first we will flush all the Device's into the Recorder before deregistering |
| 266 |
// it from the Recorder. |
| 267 |
// |
| 268 |
// We take a ref on the Device so that ~Device() does not have to deregister the recorder |
| 269 |
// (which can happen on any thread if the Device outlives the Surface via an Image view). |
| 270 |
// Recorder::flushTrackedDevices() cleans up uniquely held and immutable Devices on the recorder |
| 271 |
// thread so this extra ref is not significantly increasing the Device lifetime. |
| 272 |
// |
| 273 |
// Note: We could probably get by with only registering Devices directly connected to |
| 274 |
// SkSurfaces. All other one off Devices will be created in a controlled scope where the |
| 275 |
// Recorder should still be valid by the time they need to flush their work when the Device is |
| 276 |
// deleted. We would have to make sure we safely handle cases where a client calls saveLayer |
| 277 |
// then either deletes the SkSurface or Recorder before calling restore. For simplicity we just |
| 278 |
// register every device for now, but if we see extra overhead in pushing back the extra |
| 279 |
// pointers, we can look into only registering SkSurface Devices. |
| 280 |
void registerDevice(sk_sp<Device>); |
| 281 |
void deregisterDevice(const Device*); |
| 282 |
|
| 283 |
SkCanvas* makeCaptureCanvas(SkCanvas*) override; |
| 284 |
void createCaptureBreakpoint(SkSurface*) override; |
| 285 |
|
| 286 |
sk_sp<SharedContext> fSharedContext; |
| 287 |
ResourceProvider* fResourceProvider; // May point to the Context's resource provider |
| 288 |
std::unique_ptr<ResourceProvider> fOwnedResourceProvider; // May be null |
| 289 |
|
| 290 |
sk_sp<RuntimeEffectDictionary> fRuntimeEffectDict; |
| 291 |
|
| 292 |
// NOTE: These are stored by pointer to allow them to be forward declared. |
| 293 |
std::unique_ptr<TaskList> fRootTaskList; |
| 294 |
// Aggregated one-time uploads that preceed all tasks in the root task list. |
| 295 |
std::unique_ptr<UploadList> fRootUploads; |
| 296 |
|
| 297 |
std::unique_ptr<DrawBufferManager> fDrawBufferManager; |
| 298 |
std::unique_ptr<UploadBufferManager> fUploadBufferManager; |
| 299 |
sk_sp<FloatStorageManager> fFloatStorageManager; |
| 300 |
std::unique_ptr<ProxyReadCountMap> fProxyReadCounts; |
| 301 |
|
| 302 |
skia_private::STArray<kMaxKeyAndDataBuilders, std::unique_ptr<KeyAndDataBuilder>> |
| 303 |
fKeyAndDataBuilders; |
| 304 |
|
| 305 |
// Iterating over tracked devices in flushTrackedDevices() needs to be re-entrant and support |
| 306 |
// additions to fTrackedDevices if registerDevice() is triggered by a temporary device during |
| 307 |
// flushing. Removals are handled by setting elements to null; final clean up is handled at the |
| 308 |
// end of the initial call to flushTrackedDevices(). |
| 309 |
skia_private::TArray<sk_sp<Device>> fTrackedDevices; |
| 310 |
int fFlushingDevicesIndex = -1; |
| 311 |
|
| 312 |
uint32_t fUniqueID; // Needed for MessageBox handling for text |
| 313 |
uint32_t fNextRecordingID = 1; |
| 314 |
const bool fRequireOrderedRecordings; |
| 315 |
|
| 316 |
std::unique_ptr<AtlasProvider> fAtlasProvider; |
| 317 |
std::unique_ptr<TokenTracker> fTokenTracker; |
| 318 |
std::unique_ptr<sktext::gpu::StrikeCache> fStrikeCache; |
| 319 |
std::unique_ptr<sktext::gpu::TextBlobRedrawCoordinator> fTextBlobCache; |
| 320 |
sk_sp<ImageProvider> fClientImageProvider; |
| 321 |
|
| 322 |
// In debug builds we guard against improper thread handling |
| 323 |
// This guard is passed to the ResourceCache. |
| 324 |
// TODO: Should we also pass this to Device, DrawContext, and similar classes? |
| 325 |
mutable SingleOwner fSingleOwner; |
| 326 |
|
| 327 |
sk_sp<Device> fTargetProxyDevice; |
| 328 |
std::unique_ptr<SkCanvas> fTargetProxyCanvas; |
| 329 |
std::unique_ptr<Recording::LazyProxyData> fTargetProxyData; |
| 330 |
|
| 331 |
skia_private::TArray<sk_sp<RefCntedCallback>> fFinishedProcs; |
| 332 |
|
| 333 |
// Tracks the flushing state to ensure recursive flushing does not occur. |
| 334 |
SkDEBUGCODE(bool fIsFlushingTrackedDevices = false;) |
| 335 |
|
| 336 |
#if defined(GPU_TEST_UTILS) |
| 337 |
// For testing use only -- the Context used to create this Recorder |
| 338 |
Context* fContext = nullptr; |
| 339 |
#endif |
| 340 |
|
| 341 |
#if defined(SK_DUMP_TASKS) |
| 342 |
// Traverses and dumps the task list at Recorder::snap() |
| 343 |
void dumpTasks(TaskList*) const; |
| 344 |
|
| 345 |
// Log of all callers of RecorderPriv::flushTrackedDevices |
| 346 |
SkTDArray<const char*> fFlushSources; |
| 347 |
#endif |
| 348 |
}; |
| 349 |
|
| 350 |
} // namespace skgpu::graphite |
| 351 |
|
| 352 |
#endif // skgpu_graphite_Recorder_DEFINED |