| 1 |
/* |
| 2 |
* Copyright 2018 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 GrProxyProvider_DEFINED |
| 9 |
#define GrProxyProvider_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkRefCnt.h" |
| 12 |
#include "include/gpu/ganesh/GrTypes.h" |
| 13 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 14 |
#include "src/core/SkTDynamicHash.h" |
| 15 |
#include "src/gpu/RefCntedCallback.h" |
| 16 |
#include "src/gpu/ResourceKey.h" |
| 17 |
#include "src/gpu/ganesh/GrCaps.h" |
| 18 |
#include "src/gpu/ganesh/GrSurfaceProxy.h" |
| 19 |
#include "src/gpu/ganesh/GrTextureProxy.h" |
| 20 |
|
| 21 |
#include <cstdint> |
| 22 |
#include <string_view> |
| 23 |
|
| 24 |
class GrBackendFormat; |
| 25 |
class GrBackendRenderTarget; |
| 26 |
class GrBackendTexture; |
| 27 |
class GrMippedBitmap; |
| 28 |
class GrContextThreadSafeProxy; |
| 29 |
class GrImageContext; |
| 30 |
class GrRenderTargetProxy; |
| 31 |
class GrResourceProvider; |
| 32 |
class GrSurface; |
| 33 |
class GrSurfaceProxyView; |
| 34 |
class GrTexture; |
| 35 |
class SkBitmap; |
| 36 |
class SkData; |
| 37 |
enum class SkBackingFit; |
| 38 |
enum class SkTextureCompressionType; |
| 39 |
struct SkISize; |
| 40 |
namespace skgpu { |
| 41 |
enum class Budgeted : bool; |
| 42 |
enum class Mipmapped : bool; |
| 43 |
} // namespace skgpu |
| 44 |
|
| 45 |
/* |
| 46 |
* A factory for creating GrSurfaceProxy-derived objects. |
| 47 |
*/ |
| 48 |
class GrProxyProvider { |
| 49 |
public: |
| 50 |
using UseAllocator = GrSurfaceProxy::UseAllocator; |
| 51 |
|
| 52 |
explicit GrProxyProvider(GrImageContext*); |
| 53 |
|
| 54 |
~GrProxyProvider(); |
| 55 |
|
| 56 |
/* |
| 57 |
* Assigns a unique key to a proxy. The proxy will be findable via this key using |
| 58 |
* findProxyByUniqueKey(). It is an error if an existing proxy already has a key. |
| 59 |
*/ |
| 60 |
bool assignUniqueKeyToProxy(const skgpu::UniqueKey&, GrTextureProxy*); |
| 61 |
|
| 62 |
/* |
| 63 |
* Sets the unique key of the provided proxy to the unique key of the surface. The surface must |
| 64 |
* have a valid unique key. |
| 65 |
*/ |
| 66 |
void adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface*); |
| 67 |
|
| 68 |
/* |
| 69 |
* Removes a unique key from a proxy. If the proxy has already been instantiated, it will |
| 70 |
* also remove the unique key from the target GrSurface. |
| 71 |
*/ |
| 72 |
void removeUniqueKeyFromProxy(GrTextureProxy*); |
| 73 |
|
| 74 |
/* |
| 75 |
* Finds a proxy by unique key. |
| 76 |
*/ |
| 77 |
sk_sp<GrTextureProxy> findProxyByUniqueKey(const skgpu::UniqueKey&); |
| 78 |
|
| 79 |
/* |
| 80 |
* Finds a proxy by unique key or creates a new one that wraps a resource matching the unique |
| 81 |
* key. |
| 82 |
*/ |
| 83 |
sk_sp<GrTextureProxy> findOrCreateProxyByUniqueKey(const skgpu::UniqueKey&, |
| 84 |
UseAllocator = UseAllocator::kYes); |
| 85 |
|
| 86 |
/** |
| 87 |
* A helper that uses findOrCreateProxyByUniqueKey() to find a proxy and, if found, creates |
| 88 |
* a view for the found proxy using the passed in origin and color type. It is assumed that if |
| 89 |
* the proxy is renderable then it was created via a fallback code path so the fallback |
| 90 |
* color type will be used to create the view. |
| 91 |
*/ |
| 92 |
GrSurfaceProxyView findCachedProxyWithColorTypeFallback(const skgpu::UniqueKey&, |
| 93 |
GrSurfaceOrigin, |
| 94 |
GrColorType, |
| 95 |
int sampleCnt); |
| 96 |
|
| 97 |
/* |
| 98 |
* Creates a new texture proxy for the bitmap, optionally with mip levels generated by the cpu. |
| 99 |
* The bitmap is uploaded to the texture proxy assuming a kTopLeft_GrSurfaceOrigin. |
| 100 |
*/ |
| 101 |
sk_sp<GrTextureProxy> createProxyFromBitmap(const GrMippedBitmap&, |
| 102 |
skgpu::Mipmapped, |
| 103 |
SkBackingFit, |
| 104 |
skgpu::Budgeted); |
| 105 |
|
| 106 |
/* |
| 107 |
* Create a GrSurfaceProxy without any data. |
| 108 |
*/ |
| 109 |
sk_sp<GrTextureProxy> createProxy(const GrBackendFormat&, |
| 110 |
SkISize dimensions, |
| 111 |
GrRenderable, |
| 112 |
int renderTargetSampleCnt, |
| 113 |
skgpu::Mipmapped, |
| 114 |
SkBackingFit, |
| 115 |
skgpu::Budgeted, |
| 116 |
GrProtected, |
| 117 |
std::string_view label, |
| 118 |
GrInternalSurfaceFlags = GrInternalSurfaceFlags::kNone, |
| 119 |
UseAllocator useAllocator = UseAllocator::kYes); |
| 120 |
|
| 121 |
/* |
| 122 |
* Create a texture proxy from compressed texture data. |
| 123 |
*/ |
| 124 |
sk_sp<GrTextureProxy> createCompressedTextureProxy(SkISize dimensions, |
| 125 |
skgpu::Budgeted, |
| 126 |
skgpu::Mipmapped, |
| 127 |
GrProtected, |
| 128 |
SkTextureCompressionType, |
| 129 |
sk_sp<SkData> data); |
| 130 |
|
| 131 |
// These match the definitions in SkImage & GrTexture.h, for whence they came |
| 132 |
typedef void* ReleaseContext; |
| 133 |
typedef void (*ReleaseProc)(ReleaseContext); |
| 134 |
|
| 135 |
/* |
| 136 |
* Create a texture proxy that wraps a (non-renderable) backend texture. GrIOType must be |
| 137 |
* kRead or kRW. |
| 138 |
*/ |
| 139 |
sk_sp<GrTextureProxy> wrapBackendTexture(const GrBackendTexture&, |
| 140 |
GrWrapOwnership, |
| 141 |
GrWrapCacheable, |
| 142 |
GrIOType, |
| 143 |
sk_sp<skgpu::RefCntedCallback> = nullptr); |
| 144 |
|
| 145 |
sk_sp<GrTextureProxy> wrapCompressedBackendTexture(const GrBackendTexture&, |
| 146 |
GrWrapOwnership, |
| 147 |
GrWrapCacheable, |
| 148 |
sk_sp<skgpu::RefCntedCallback>); |
| 149 |
|
| 150 |
/* |
| 151 |
* Create a texture proxy that wraps a backend texture and is both texture-able and renderable |
| 152 |
*/ |
| 153 |
sk_sp<GrTextureProxy> wrapRenderableBackendTexture(const GrBackendTexture&, |
| 154 |
int sampleCnt, |
| 155 |
GrWrapOwnership, |
| 156 |
GrWrapCacheable, |
| 157 |
sk_sp<skgpu::RefCntedCallback> releaseHelper); |
| 158 |
|
| 159 |
/* |
| 160 |
* Create a render target proxy that wraps a backend render target |
| 161 |
*/ |
| 162 |
sk_sp<GrSurfaceProxy> wrapBackendRenderTarget(const GrBackendRenderTarget&, |
| 163 |
sk_sp<skgpu::RefCntedCallback> releaseHelper); |
| 164 |
|
| 165 |
using LazyInstantiationKeyMode = GrSurfaceProxy::LazyInstantiationKeyMode; |
| 166 |
using LazySurfaceDesc = GrSurfaceProxy::LazySurfaceDesc; |
| 167 |
using LazyCallbackResult = GrSurfaceProxy::LazyCallbackResult; |
| 168 |
using LazyInstantiateCallback = GrSurfaceProxy::LazyInstantiateCallback; |
| 169 |
|
| 170 |
struct TextureInfo { |
| 171 |
skgpu::Mipmapped fMipmapped; |
| 172 |
GrTextureType fTextureType; |
| 173 |
}; |
| 174 |
|
| 175 |
/** |
| 176 |
* Similar to createLazyProxy below, except narrowed to the use case of shared promise images |
| 177 |
* i.e. static so it doesn't have access to mutable state. Used by MakePromiseImageLazyProxy(). |
| 178 |
*/ |
| 179 |
static sk_sp<GrTextureProxy> CreatePromiseProxy(GrContextThreadSafeProxy*, |
| 180 |
LazyInstantiateCallback&&, |
| 181 |
const GrBackendFormat&, |
| 182 |
SkISize dimensions, |
| 183 |
skgpu::Mipmapped); |
| 184 |
|
| 185 |
/** |
| 186 |
* Creates a texture proxy that will be instantiated by a user-supplied callback during flush. |
| 187 |
* The width and height must either both be greater than 0 or both less than or equal to zero. A |
| 188 |
* non-positive value is a signal that the width height are currently unknown. The texture will |
| 189 |
* not be renderable. |
| 190 |
* |
| 191 |
* When called, the callback must be able to cleanup any resources that it captured at creation. |
| 192 |
* It also must support being passed in a null GrResourceProvider. When this happens, the |
| 193 |
* callback should cleanup any resources it captured and return an empty sk_sp<GrTextureProxy>. |
| 194 |
*/ |
| 195 |
sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, |
| 196 |
const GrBackendFormat&, |
| 197 |
SkISize dimensions, |
| 198 |
skgpu::Mipmapped, |
| 199 |
GrMipmapStatus, |
| 200 |
GrInternalSurfaceFlags, |
| 201 |
SkBackingFit, |
| 202 |
skgpu::Budgeted, |
| 203 |
GrProtected, |
| 204 |
UseAllocator, |
| 205 |
std::string_view label); |
| 206 |
|
| 207 |
/** A null TextureInfo indicates a non-textureable render target. */ |
| 208 |
sk_sp<GrRenderTargetProxy> createLazyRenderTargetProxy(LazyInstantiateCallback&&, |
| 209 |
const GrBackendFormat&, |
| 210 |
SkISize dimensions, |
| 211 |
int renderTargetSampleCnt, |
| 212 |
GrInternalSurfaceFlags, |
| 213 |
const TextureInfo*, |
| 214 |
GrMipmapStatus, |
| 215 |
SkBackingFit, |
| 216 |
skgpu::Budgeted, |
| 217 |
GrProtected, |
| 218 |
bool wrapsVkSecondaryCB, |
| 219 |
UseAllocator useAllocator); |
| 220 |
|
| 221 |
/** |
| 222 |
* Fully lazy proxies have unspecified width and height. Methods that rely on those values |
| 223 |
* (e.g., width, height, getBoundsRect) should be avoided. |
| 224 |
*/ |
| 225 |
static sk_sp<GrTextureProxy> MakeFullyLazyProxy(LazyInstantiateCallback&&, |
| 226 |
const GrBackendFormat&, |
| 227 |
GrRenderable, |
| 228 |
int renderTargetSampleCnt, |
| 229 |
GrProtected, |
| 230 |
const GrCaps&, |
| 231 |
UseAllocator); |
| 232 |
|
| 233 |
enum class InvalidateGPUResource : bool { kNo = false, kYes = true }; |
| 234 |
|
| 235 |
/* |
| 236 |
* This method ensures that, if a proxy w/ the supplied unique key exists, it is removed from |
| 237 |
* the proxy provider's map and its unique key is removed. If 'invalidateSurface' is true, it |
| 238 |
* will independently ensure that the unique key is removed from any GrGpuResources that may |
| 239 |
* have it. |
| 240 |
* |
| 241 |
* If 'proxy' is provided (as an optimization to stop re-looking it up), its unique key must be |
| 242 |
* valid and match the provided unique key. |
| 243 |
* |
| 244 |
* This method is called if either the proxy attached to the unique key is being deleted |
| 245 |
* (in which case we don't want it cluttering up the hash table) or the client has indicated |
| 246 |
* that it will never refer to the unique key again. |
| 247 |
*/ |
| 248 |
void processInvalidUniqueKey(const skgpu::UniqueKey&, GrTextureProxy*, InvalidateGPUResource); |
| 249 |
|
| 250 |
GrDDLProvider isDDLProvider() const; |
| 251 |
|
| 252 |
// TODO: remove these entry points - it is a bit sloppy to be getting context info from here |
| 253 |
uint32_t contextID() const; |
| 254 |
const GrCaps* caps() const; |
| 255 |
sk_sp<const GrCaps> refCaps() const; |
| 256 |
|
| 257 |
GrResourceProvider* resourceProvider() const; |
| 258 |
|
| 259 |
int numUniqueKeyProxies_TestOnly() const; |
| 260 |
|
| 261 |
// This is called on a DDL's proxyprovider when the DDL is finished. The uniquely keyed |
| 262 |
// proxies need to keep their unique key but cannot hold on to the proxy provider unique |
| 263 |
// pointer. |
| 264 |
void orphanAllUniqueKeys(); |
| 265 |
// This is only used by GrContext::releaseResourcesAndAbandonContext() |
| 266 |
void removeAllUniqueKeys(); |
| 267 |
|
| 268 |
/** |
| 269 |
* Does the proxy provider have access to a GrDirectContext? If so, proxies will be |
| 270 |
* instantiated immediately. |
| 271 |
*/ |
| 272 |
bool renderingDirectly() const; |
| 273 |
bool isAbandoned() const; |
| 274 |
|
| 275 |
#if defined(GPU_TEST_UTILS) |
| 276 |
/** |
| 277 |
* Create a texture proxy that is backed by an instantiated GrSurface. |
| 278 |
*/ |
| 279 |
sk_sp<GrTextureProxy> testingOnly_createInstantiatedProxy(SkISize dimensions, |
| 280 |
const GrBackendFormat& format, |
| 281 |
GrRenderable renderable, |
| 282 |
int renderTargetSampleCnt, |
| 283 |
SkBackingFit fit, |
| 284 |
skgpu::Budgeted budgeted, |
| 285 |
GrProtected isProtected); |
| 286 |
|
| 287 |
/** Version of above that picks the default format for the color type. */ |
| 288 |
sk_sp<GrTextureProxy> testingOnly_createInstantiatedProxy(SkISize dimensions, |
| 289 |
GrColorType colorType, |
| 290 |
GrRenderable renderable, |
| 291 |
int renderTargetSampleCnt, |
| 292 |
SkBackingFit fit, |
| 293 |
skgpu::Budgeted budgeted, |
| 294 |
GrProtected isProtected); |
| 295 |
|
| 296 |
sk_sp<GrTextureProxy> testingOnly_createWrapped(sk_sp<GrTexture>); |
| 297 |
#endif |
| 298 |
|
| 299 |
private: |
| 300 |
friend class GrAHardwareBufferImageGenerator; // for createWrapped |
| 301 |
friend class GrResourceProvider; // for createWrapped |
| 302 |
|
| 303 |
// processInvalidUniqueKey() with control over removing hash table entries, |
| 304 |
// which is not safe while iterating with foreach(). |
| 305 |
enum class RemoveTableEntry { kNo, kYes }; |
| 306 |
void processInvalidUniqueKeyImpl(const skgpu::UniqueKey&, GrTextureProxy*, |
| 307 |
InvalidateGPUResource, RemoveTableEntry); |
| 308 |
|
| 309 |
/* |
| 310 |
* Create an un-mipmapped texture proxy for the bitmap. |
| 311 |
*/ |
| 312 |
sk_sp<GrTextureProxy> createNonMippedProxyFromBitmap(const SkBitmap&, |
| 313 |
SkBackingFit, |
| 314 |
skgpu::Budgeted); |
| 315 |
/* |
| 316 |
* Create an mipmapped texture proxy for the bitmap. |
| 317 |
*/ |
| 318 |
sk_sp<GrTextureProxy> createMippedProxyFromBitmap(const GrMippedBitmap&, skgpu::Budgeted); |
| 319 |
|
| 320 |
sk_sp<GrTextureProxy> createWrapped(sk_sp<GrTexture> tex, UseAllocator useAllocator); |
| 321 |
|
| 322 |
struct UniquelyKeyedProxyHashTraits { |
| 323 |
static const skgpu::UniqueKey& GetKey(const GrTextureProxy& p) { return p.getUniqueKey(); } |
| 324 |
|
| 325 |
static uint32_t Hash(const skgpu::UniqueKey& key) { return key.hash(); } |
| 326 |
}; |
| 327 |
typedef SkTDynamicHash<GrTextureProxy, skgpu::UniqueKey, UniquelyKeyedProxyHashTraits> UniquelyKeyedProxyHash; |
| 328 |
|
| 329 |
// This holds the texture proxies that have unique keys. The resourceCache does not get a ref |
| 330 |
// on these proxies but they must send a message to the resourceCache when they are deleted. |
| 331 |
UniquelyKeyedProxyHash fUniquelyKeyedProxies; |
| 332 |
|
| 333 |
GrImageContext* fImageContext; |
| 334 |
}; |
| 335 |
|
| 336 |
#endif |