| 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_Context_DEFINED |
| 9 |
#define skgpu_graphite_Context_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkImage.h" |
| 12 |
#include "include/core/SkRefCnt.h" |
| 13 |
#include "include/core/SkSize.h" |
| 14 |
#include "include/core/SkSpan.h" |
| 15 |
#include "include/core/SkTypes.h" |
| 16 |
#include "include/gpu/graphite/GraphiteTypes.h" |
| 17 |
#include "include/gpu/graphite/Recorder.h" // IWYU pragma: keep |
| 18 |
#include "include/private/base/SingleOwner.h" |
| 19 |
#include "include/private/base/SkThreadAnnotations.h" |
| 20 |
|
| 21 |
#if defined(GPU_TEST_UTILS) |
| 22 |
#include "include/private/base/SkMutex.h" |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <chrono> |
| 26 |
#include <cstddef> |
| 27 |
#include <cstdint> |
| 28 |
#include <functional> |
| 29 |
#include <memory> |
| 30 |
#include <vector> |
| 31 |
|
| 32 |
class SkColorInfo; |
| 33 |
class SkSurface; |
| 34 |
class SkCapture; |
| 35 |
enum SkYUVColorSpace : int; |
| 36 |
class SkColorSpace; |
| 37 |
class SkTraceMemoryDump; |
| 38 |
struct SkIRect; |
| 39 |
struct SkImageInfo; |
| 40 |
|
| 41 |
namespace skcpu { |
| 42 |
class ContextImpl; |
| 43 |
class Recorder; |
| 44 |
} // namespace skcpu |
| 45 |
|
| 46 |
namespace skgpu { |
| 47 |
enum class BackendApi : unsigned int; |
| 48 |
enum class GpuStatsFlags : uint32_t; |
| 49 |
} |
| 50 |
|
| 51 |
namespace skgpu::graphite { |
| 52 |
|
| 53 |
class BackendTexture; |
| 54 |
class Buffer; |
| 55 |
class ClientMappedBufferManager; |
| 56 |
class ContextPriv; |
| 57 |
struct ContextOptions; |
| 58 |
class PersistentPipelineStorage; |
| 59 |
class PrecompileContext; |
| 60 |
class QueueManager; |
| 61 |
class ResourceProvider; |
| 62 |
class SharedContext; |
| 63 |
class TextureProxy; |
| 64 |
|
| 65 |
class SK_API Context final { |
| 66 |
public: |
| 67 |
Context(const Context&) = delete; |
| 68 |
Context(Context&&) = delete; |
| 69 |
Context& operator=(const Context&) = delete; |
| 70 |
Context& operator=(Context&&) = delete; |
| 71 |
|
| 72 |
~Context(); |
| 73 |
|
| 74 |
BackendApi backend() const; |
| 75 |
|
| 76 |
std::unique_ptr<Recorder> makeRecorder(const RecorderOptions& = {}); |
| 77 |
std::unique_ptr<skcpu::Recorder> makeCPURecorder(); |
| 78 |
|
| 79 |
/** Creates a helper object that can be moved to a different thread and used |
| 80 |
* for precompilation. |
| 81 |
*/ |
| 82 |
std::unique_ptr<PrecompileContext> makePrecompileContext(); |
| 83 |
|
| 84 |
InsertStatus insertRecording(const InsertRecordingInfo&); |
| 85 |
bool submit(SubmitInfo submitInfo = {}); |
| 86 |
|
| 87 |
/** Returns true if there is work that was submitted to the GPU that has not finished. */ |
| 88 |
bool hasUnfinishedGpuWork() const; |
| 89 |
|
| 90 |
/** Makes image pixel data available to caller, possibly asynchronously. It can also rescale |
| 91 |
the image pixels. |
| 92 |
|
| 93 |
Data is read from the source sub-rectangle, is optionally converted to a linear gamma, is |
| 94 |
rescaled to the size indicated by 'dstImageInfo', is then converted to the color space, |
| 95 |
color type, and alpha type of 'dstImageInfo'. A 'srcRect' that is not contained by the |
| 96 |
bounds of the image causes failure. |
| 97 |
|
| 98 |
When the pixel data is ready the caller's ReadPixelsCallback is called with a |
| 99 |
AsyncReadResult containing pixel data in the requested color type, alpha type, and color |
| 100 |
space. The AsyncReadResult will have count() == 1. Upon failure the callback is called with |
| 101 |
nullptr for AsyncReadResult. The callback can be triggered, for example, with a call to |
| 102 |
Context::submit(SyncToCpu::kYes). |
| 103 |
|
| 104 |
The data is valid for the lifetime of AsyncReadResult with the exception that the data is |
| 105 |
immediately invalidated if the Graphite context is abandoned or destroyed. |
| 106 |
|
| 107 |
@param src Graphite-backed image or surface to read the data from. |
| 108 |
@param dstImageInfo info of the requested pixels |
| 109 |
@param srcRect subrectangle of image to read |
| 110 |
@param rescaleGamma controls whether rescaling is done in the image's gamma or whether |
| 111 |
the source data is transformed to a linear gamma before rescaling. |
| 112 |
@param rescaleMode controls the technique (and cost) of the rescaling |
| 113 |
@param callback function to call with result of the read |
| 114 |
@param context passed to callback |
| 115 |
*/ |
| 116 |
void asyncRescaleAndReadPixels(const SkImage* src, |
| 117 |
const SkImageInfo& dstImageInfo, |
| 118 |
const SkIRect& srcRect, |
| 119 |
SkImage::RescaleGamma rescaleGamma, |
| 120 |
SkImage::RescaleMode rescaleMode, |
| 121 |
SkImage::ReadPixelsCallback callback, |
| 122 |
SkImage::ReadPixelsContext context); |
| 123 |
void asyncRescaleAndReadPixels(const SkSurface* src, |
| 124 |
const SkImageInfo& dstImageInfo, |
| 125 |
const SkIRect& srcRect, |
| 126 |
SkImage::RescaleGamma rescaleGamma, |
| 127 |
SkImage::RescaleMode rescaleMode, |
| 128 |
SkImage::ReadPixelsCallback callback, |
| 129 |
SkImage::ReadPixelsContext context); |
| 130 |
|
| 131 |
/** |
| 132 |
Similar to asyncRescaleAndReadPixels but performs an additional conversion to YUV. The |
| 133 |
RGB->YUV conversion is controlled by 'yuvColorSpace'. The YUV data is returned as three |
| 134 |
planes ordered y, u, v. The u and v planes are half the width and height of the resized |
| 135 |
rectangle. The y, u, and v values are single bytes. Currently this fails if 'dstSize' |
| 136 |
width and height are not even. A 'srcRect' that is not contained by the bounds of the |
| 137 |
surface causes failure. |
| 138 |
|
| 139 |
When the pixel data is ready the caller's ReadPixelsCallback is called with a |
| 140 |
AsyncReadResult containing the planar data. The AsyncReadResult will have count() == 3. |
| 141 |
Upon failure the callback is called with nullptr for AsyncReadResult. The callback can |
| 142 |
be triggered, for example, with a call to Context::submit(SyncToCpu::kYes). |
| 143 |
|
| 144 |
The data is valid for the lifetime of AsyncReadResult with the exception that the data |
| 145 |
is immediately invalidated if the context is abandoned or destroyed. |
| 146 |
|
| 147 |
@param src Graphite-backed image or surface to read the data from. |
| 148 |
@param yuvColorSpace The transformation from RGB to YUV. Applied to the resized image |
| 149 |
after it is converted to dstColorSpace. |
| 150 |
@param dstColorSpace The color space to convert the resized image to, after rescaling. |
| 151 |
@param srcRect The portion of the surface to rescale and convert to YUV planes. |
| 152 |
@param dstSize The size to rescale srcRect to |
| 153 |
@param rescaleGamma controls whether rescaling is done in the surface's gamma or whether |
| 154 |
the source data is transformed to a linear gamma before rescaling. |
| 155 |
@param rescaleMode controls the sampling technique of the rescaling |
| 156 |
@param callback function to call with the planar read result |
| 157 |
@param context passed to callback |
| 158 |
*/ |
| 159 |
void asyncRescaleAndReadPixelsYUV420(const SkImage* src, |
| 160 |
SkYUVColorSpace yuvColorSpace, |
| 161 |
sk_sp<SkColorSpace> dstColorSpace, |
| 162 |
const SkIRect& srcRect, |
| 163 |
const SkISize& dstSize, |
| 164 |
SkImage::RescaleGamma rescaleGamma, |
| 165 |
SkImage::RescaleMode rescaleMode, |
| 166 |
SkImage::ReadPixelsCallback callback, |
| 167 |
SkImage::ReadPixelsContext context); |
| 168 |
void asyncRescaleAndReadPixelsYUV420(const SkSurface* src, |
| 169 |
SkYUVColorSpace yuvColorSpace, |
| 170 |
sk_sp<SkColorSpace> dstColorSpace, |
| 171 |
const SkIRect& srcRect, |
| 172 |
const SkISize& dstSize, |
| 173 |
SkImage::RescaleGamma rescaleGamma, |
| 174 |
SkImage::RescaleMode rescaleMode, |
| 175 |
SkImage::ReadPixelsCallback callback, |
| 176 |
SkImage::ReadPixelsContext context); |
| 177 |
|
| 178 |
/** |
| 179 |
* Identical to asyncRescaleAndReadPixelsYUV420 but a fourth plane is returned in the |
| 180 |
* AsyncReadResult passed to 'callback'. The fourth plane contains the alpha chanel at the |
| 181 |
* same full resolution as the Y plane. |
| 182 |
*/ |
| 183 |
void asyncRescaleAndReadPixelsYUVA420(const SkImage* src, |
| 184 |
SkYUVColorSpace yuvColorSpace, |
| 185 |
sk_sp<SkColorSpace> dstColorSpace, |
| 186 |
const SkIRect& srcRect, |
| 187 |
const SkISize& dstSize, |
| 188 |
SkImage::RescaleGamma rescaleGamma, |
| 189 |
SkImage::RescaleMode rescaleMode, |
| 190 |
SkImage::ReadPixelsCallback callback, |
| 191 |
SkImage::ReadPixelsContext context); |
| 192 |
void asyncRescaleAndReadPixelsYUVA420(const SkSurface* src, |
| 193 |
SkYUVColorSpace yuvColorSpace, |
| 194 |
sk_sp<SkColorSpace> dstColorSpace, |
| 195 |
const SkIRect& srcRect, |
| 196 |
const SkISize& dstSize, |
| 197 |
SkImage::RescaleGamma rescaleGamma, |
| 198 |
SkImage::RescaleMode rescaleMode, |
| 199 |
SkImage::ReadPixelsCallback callback, |
| 200 |
SkImage::ReadPixelsContext context); |
| 201 |
|
| 202 |
/** |
| 203 |
* Checks whether any asynchronous work is complete and if so calls related callbacks. |
| 204 |
*/ |
| 205 |
void checkAsyncWorkCompletion(); |
| 206 |
|
| 207 |
/** |
| 208 |
* Called to delete the passed in BackendTexture. This should only be called if the |
| 209 |
* BackendTexture was created by calling Recorder::createBackendTexture on a Recorder created |
| 210 |
* from this Context. If the BackendTexture is not valid or does not match the BackendApi of the |
| 211 |
* Context then nothing happens. |
| 212 |
* |
| 213 |
* Otherwise this will delete/release the backend object that is wrapped in the BackendTexture. |
| 214 |
* The BackendTexture will be reset to an invalid state and should not be used again. |
| 215 |
*/ |
| 216 |
void deleteBackendTexture(const BackendTexture&); |
| 217 |
|
| 218 |
/** |
| 219 |
* Frees GPU resources created and held by the Context. Can be called to reduce GPU memory |
| 220 |
* pressure. Any resources that are still in use (e.g. being used by work submitted to the GPU) |
| 221 |
* will not be deleted by this call. If the caller wants to make sure all resources are freed, |
| 222 |
* then they should first make sure to submit and wait on any outstanding work. |
| 223 |
*/ |
| 224 |
void freeGpuResources(); |
| 225 |
|
| 226 |
/** |
| 227 |
* Purge GPU resources on the Context that haven't been used in the past 'msNotUsed' |
| 228 |
* milliseconds or are otherwise marked for deletion, regardless of whether the context is under |
| 229 |
* budget. |
| 230 |
*/ |
| 231 |
void performDeferredCleanup(std::chrono::milliseconds msNotUsed); |
| 232 |
|
| 233 |
/** |
| 234 |
* Returns the number of bytes of the Context's gpu memory cache budget that are currently in |
| 235 |
* use. |
| 236 |
*/ |
| 237 |
size_t currentBudgetedBytes() const; |
| 238 |
|
| 239 |
/** |
| 240 |
* Returns the number of bytes of the Context's resource cache that are currently purgeable. |
| 241 |
*/ |
| 242 |
size_t currentPurgeableBytes() const; |
| 243 |
|
| 244 |
/** |
| 245 |
* Returns the size of Context's gpu memory cache budget in bytes. |
| 246 |
*/ |
| 247 |
size_t maxBudgetedBytes() const; |
| 248 |
|
| 249 |
/** |
| 250 |
* Sets the size of Context's gpu memory cache budget in bytes. If the new budget is lower than |
| 251 |
* the current budget, the cache will try to free resources to get under the new budget. |
| 252 |
*/ |
| 253 |
void setMaxBudgetedBytes(size_t bytes); |
| 254 |
|
| 255 |
/** |
| 256 |
* Enumerates all cached GPU resources owned by the Context and dumps their memory to |
| 257 |
* traceMemoryDump. |
| 258 |
*/ |
| 259 |
void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const; |
| 260 |
|
| 261 |
/** |
| 262 |
* Returns true if the backend-specific context has gotten into an unrecoverarble, lost state |
| 263 |
* (e.g. if we've gotten a VK_ERROR_DEVICE_LOST in the Vulkan backend). |
| 264 |
*/ |
| 265 |
bool isDeviceLost() const; |
| 266 |
|
| 267 |
/** |
| 268 |
* Returns the maximum texture dimension supported by the underlying backend. |
| 269 |
*/ |
| 270 |
int maxTextureSize() const; |
| 271 |
|
| 272 |
/* |
| 273 |
* Does this context support protected content? |
| 274 |
*/ |
| 275 |
bool supportsProtectedContent() const; |
| 276 |
|
| 277 |
/* |
| 278 |
* Gets the types of GPU stats supported by this Context. |
| 279 |
*/ |
| 280 |
GpuStatsFlags supportedGpuStats() const; |
| 281 |
|
| 282 |
/** |
| 283 |
* If supported by the backend, stores the current pipeline cache data into the |
| 284 |
* PersistentPipelineStorage-derived object passed into Graphite via |
| 285 |
* ContextOptions::fPersistentPipelineStorage. The amount stored is limited to 'maxSize'. |
| 286 |
* |
| 287 |
* Skia attempts to only call store() on the PersistentPipelineStorage object when the data |
| 288 |
* is likely to be different from what was last sync'ed. |
| 289 |
*/ |
| 290 |
void syncPipelineData(size_t maxSize = SIZE_MAX); |
| 291 |
|
| 292 |
/* |
| 293 |
* TODO (b/412351769): Do not use startCapture() or endCapture() as the feature is still under |
| 294 |
* development. |
| 295 |
* |
| 296 |
* Starts the SkCapture. Must have set ContextOptions::fEnableCapture to start. |
| 297 |
*/ |
| 298 |
void startCapture(); |
| 299 |
|
| 300 |
/* |
| 301 |
* Ends the SkCapture and returns the collected draws and surface creation. |
| 302 |
*/ |
| 303 |
sk_sp<SkCapture> endCapture(); |
| 304 |
|
| 305 |
// Provides access to functions that aren't part of the public API. |
| 306 |
ContextPriv priv(); |
| 307 |
const ContextPriv priv() const; // NOLINT(readability-const-return-type) |
| 308 |
|
| 309 |
class ContextID { |
| 310 |
public: |
| 311 |
static Context::ContextID Next(); |
| 312 |
|
| 313 |
ContextID() : fID(SK_InvalidUniqueID) {} |
| 314 |
|
| 315 |
bool operator==(const ContextID& that) const { return fID == that.fID; } |
| 316 |
bool operator!=(const ContextID& that) const { return !(*this == that); } |
| 317 |
|
| 318 |
void makeInvalid() { fID = SK_InvalidUniqueID; } |
| 319 |
bool isValid() const { return fID != SK_InvalidUniqueID; } |
| 320 |
|
| 321 |
private: |
| 322 |
constexpr ContextID(uint32_t id) : fID(id) {} |
| 323 |
uint32_t fID; |
| 324 |
}; |
| 325 |
|
| 326 |
ContextID contextID() const { return fContextID; } |
| 327 |
|
| 328 |
protected: |
| 329 |
Context(sk_sp<SharedContext>, std::unique_ptr<QueueManager>, const ContextOptions&); |
| 330 |
|
| 331 |
private: |
| 332 |
friend class ContextPriv; |
| 333 |
friend class ContextCtorAccessor; |
| 334 |
|
| 335 |
struct PixelTransferResult { |
| 336 |
PixelTransferResult(); |
| 337 |
PixelTransferResult(const PixelTransferResult&); |
| 338 |
PixelTransferResult(PixelTransferResult&&); |
| 339 |
PixelTransferResult& operator=(const PixelTransferResult&); |
| 340 |
~PixelTransferResult(); |
| 341 |
|
| 342 |
using ConversionFn = void(void* dst, const void* mappedBuffer); |
| 343 |
// If null then the transfer could not be performed. Otherwise this buffer will contain |
| 344 |
// the pixel data when the transfer is complete. |
| 345 |
sk_sp<Buffer> fTransferBuffer; |
| 346 |
// Size of the read. |
| 347 |
SkISize fSize; |
| 348 |
// RowBytes for transfer buffer data |
| 349 |
size_t fRowBytes; |
| 350 |
// If this is null then the transfer buffer will contain the data in the requested |
| 351 |
// color type. Otherwise, when the transfer is done this must be called to convert |
| 352 |
// from the transfer buffer's color type to the requested color type. |
| 353 |
std::function<ConversionFn> fPixelConverter; |
| 354 |
}; |
| 355 |
|
| 356 |
SingleOwner* singleOwner() const { return &fSingleOwner; } |
| 357 |
|
| 358 |
// Must be called in Make() to handle one-time GPU setup operations that can possibly fail and |
| 359 |
// require Context::Make() to return a nullptr. |
| 360 |
bool finishInitialization(); |
| 361 |
|
| 362 |
void checkForFinishedWork(SyncToCpu); |
| 363 |
|
| 364 |
std::unique_ptr<Recorder> makeInternalRecorder() const; |
| 365 |
|
| 366 |
template <typename SrcPixels> struct AsyncParams; |
| 367 |
|
| 368 |
template <typename ReadFn, typename... ExtraArgs> |
| 369 |
void asyncRescaleAndReadImpl(ReadFn Context::* asyncRead, |
| 370 |
SkImage::RescaleGamma rescaleGamma, |
| 371 |
SkImage::RescaleMode rescaleMode, |
| 372 |
const AsyncParams<SkImage>&, |
| 373 |
ExtraArgs...); |
| 374 |
|
| 375 |
// Recorder is optional and will be used if drawing operations are required. If no Recorder is |
| 376 |
// provided but drawing operations are needed, a new Recorder will be created automatically. |
| 377 |
void asyncReadPixels(std::unique_ptr<Recorder>, const AsyncParams<SkImage>&); |
| 378 |
void asyncReadPixelsYUV420(std::unique_ptr<Recorder>, |
| 379 |
const AsyncParams<SkImage>&, |
| 380 |
SkYUVColorSpace); |
| 381 |
|
| 382 |
// Like asyncReadPixels() except it performs no fallbacks, and requires that the texture be |
| 383 |
// readable. However, the texture does not need to be sampleable. |
| 384 |
void asyncReadTexture(std::unique_ptr<Recorder>, |
| 385 |
const AsyncParams<TextureProxy>&, |
| 386 |
const SkColorInfo& srcColorInfo); |
| 387 |
|
| 388 |
// Inserts a texture to buffer transfer task, used by asyncReadPixels methods. If the |
| 389 |
// Recorder is non-null, tasks will be added to the Recorder's list; otherwise the transfer |
| 390 |
// tasks will be added to the queue manager directly. |
| 391 |
PixelTransferResult transferPixels(Recorder*, |
| 392 |
const TextureProxy* srcProxy, |
| 393 |
const SkColorInfo& srcColorInfo, |
| 394 |
const SkColorInfo& dstColorInfo, |
| 395 |
const SkIRect& srcRect); |
| 396 |
|
| 397 |
// If the recorder is non-null, it will be snapped and inserted with the assumption that the |
| 398 |
// copy tasks (and possibly preparatory draw tasks) have already been added to the Recording. |
| 399 |
void finalizeAsyncReadPixels(std::unique_ptr<Recorder>, |
| 400 |
SkSpan<PixelTransferResult>, |
| 401 |
SkImage::ReadPixelsCallback callback, |
| 402 |
SkImage::ReadPixelsContext callbackContext); |
| 403 |
|
| 404 |
sk_sp<SharedContext> fSharedContext; |
| 405 |
std::unique_ptr<ResourceProvider> fResourceProvider; |
| 406 |
std::unique_ptr<QueueManager> fQueueManager; |
| 407 |
std::unique_ptr<ClientMappedBufferManager> fMappedBufferManager; |
| 408 |
std::unique_ptr<const skcpu::ContextImpl> fCPUContext; |
| 409 |
|
| 410 |
PersistentPipelineStorage* fPersistentPipelineStorage; |
| 411 |
|
| 412 |
// In debug builds we guard against improper thread handling. This guard is passed to the |
| 413 |
// ResourceCache for the Context. |
| 414 |
mutable SingleOwner fSingleOwner; |
| 415 |
|
| 416 |
#if defined(GPU_TEST_UTILS) |
| 417 |
void deregisterRecorder(const Recorder*) SK_EXCLUDES(fTestingLock); |
| 418 |
|
| 419 |
// In test builds a Recorder may track the Context that was used to create it. |
| 420 |
bool fStoreContextRefInRecorder = false; |
| 421 |
// If this tracking is on, to allow the client to safely delete this Context or its Recorders |
| 422 |
// in any order we must also track the Recorders created here. |
| 423 |
SkMutex fTestingLock; |
| 424 |
std::vector<Recorder*> fTrackedRecorders SK_GUARDED_BY(fTestingLock); |
| 425 |
#endif |
| 426 |
|
| 427 |
// Needed for MessageBox handling |
| 428 |
const ContextID fContextID; |
| 429 |
}; |
| 430 |
|
| 431 |
} // namespace skgpu::graphite |
| 432 |
|
| 433 |
#endif // skgpu_graphite_Context_DEFINED |