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 "GrCaps.h" 12 #include "GrResourceKey.h" 13 #include "GrTextureProxy.h" 14 #include "GrTypes.h" 15 #include "SkRefCnt.h" 16 #include "SkTDynamicHash.h" 17 18 class GrResourceProvider; 19 class GrSingleOwner; 20 class GrBackendRenderTarget; 21 class SkBitmap; 22 class SkImage; 23 24 /* 25 * A factory for creating GrSurfaceProxy-derived objects. 26 */ 27 class GrProxyProvider { 28 public: 29 GrProxyProvider(GrResourceProvider*, GrResourceCache*, sk_sp<const GrCaps>, GrSingleOwner*); 30 GrProxyProvider(uint32_t contextUniqueID, sk_sp<const GrCaps>, GrSingleOwner*); 31 32 ~GrProxyProvider(); 33 34 /* 35 * Assigns a unique key to a proxy. The proxy will be findable via this key using 36 * findProxyByUniqueKey(). It is an error if an existing proxy already has a key. 37 */ 38 bool assignUniqueKeyToProxy(const GrUniqueKey&, GrTextureProxy*); 39 40 /* 41 * Sets the unique key of the provided proxy to the unique key of the surface. The surface must 42 * have a valid unique key. 43 */ 44 void adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface*); 45 46 /* 47 * Removes a unique key from a proxy. If the proxy has already been instantiated, it will 48 * also remove the unique key from the target GrSurface. 49 */ 50 void removeUniqueKeyFromProxy(GrTextureProxy*); 51 52 /* 53 * Finds a proxy by unique key. 54 */ 55 sk_sp<GrTextureProxy> findProxyByUniqueKey(const GrUniqueKey&, GrSurfaceOrigin); 56 57 /* 58 * Finds a proxy by unique key or creates a new one that wraps a resource matching the unique 59 * key. 60 */ 61 sk_sp<GrTextureProxy> findOrCreateProxyByUniqueKey(const GrUniqueKey&, GrSurfaceOrigin); 62 63 /* 64 * Create an un-mipmapped texture proxy with data. The SkImage must be a raster backend image. 65 * Since the SkImage is ref counted, we simply take a ref on it to keep the data alive until we 66 * actually upload the data to the gpu. 67 */ 68 sk_sp<GrTextureProxy> createTextureProxy( 69 sk_sp<SkImage> srcImage, GrSurfaceDescFlags, int sampleCnt, SkBudgeted, SkBackingFit, 70 GrInternalSurfaceFlags = GrInternalSurfaceFlags::kNone); 71 72 /* 73 * Create a mipmapped texture proxy without any data. 74 * 75 * Like the call above but there are no texels to upload. A texture proxy is returned that 76 * simply has space allocated for the mips. We will allocated the full amount of mip levels 77 * based on the width and height in the GrSurfaceDesc. 78 */ 79 sk_sp<GrTextureProxy> createMipMapProxy(const GrBackendFormat&, const GrSurfaceDesc&, 80 GrSurfaceOrigin, SkBudgeted); 81 82 /* 83 * Creates a new mipmapped texture proxy for the bitmap with mip levels generated by the cpu. 84 */ 85 sk_sp<GrTextureProxy> createMipMapProxyFromBitmap(const SkBitmap& bitmap); 86 87 /* 88 * Create a GrSurfaceProxy without any data. 89 */ 90 sk_sp<GrTextureProxy> createProxy(const GrBackendFormat&, const GrSurfaceDesc&, GrSurfaceOrigin, 91 GrMipMapped, SkBackingFit, SkBudgeted, 92 GrInternalSurfaceFlags); 93 94 sk_sp<GrTextureProxy> createProxy( 95 const GrBackendFormat& format, const GrSurfaceDesc& desc, 96 GrSurfaceOrigin origin, SkBackingFit fit, SkBudgeted budgeted, 97 GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone) { 98 return this->createProxy(format, desc, origin, GrMipMapped::kNo, fit, budgeted, 99 surfaceFlags); 100 } 101 102 /* 103 * Create a texture proxy with data. It's assumed that the data is packed tightly. 104 */ 105 sk_sp<GrTextureProxy> createProxy(sk_sp<SkData>, const GrSurfaceDesc& desc); 106 107 // These match the definitions in SkImage & GrTexture.h, for whence they came 108 typedef void* ReleaseContext; 109 typedef void (*ReleaseProc)(ReleaseContext); 110 111 /* 112 * Create a texture proxy that wraps a (non-renderable) backend texture. GrIOType must be 113 * kRead or kRW. 114 */ 115 sk_sp<GrTextureProxy> wrapBackendTexture(const GrBackendTexture&, GrSurfaceOrigin, 116 GrWrapOwnership, GrWrapCacheable, GrIOType, 117 ReleaseProc = nullptr, ReleaseContext = nullptr); 118 119 /* 120 * Create a texture proxy that wraps a backend texture and is both texture-able and renderable 121 */ 122 sk_sp<GrTextureProxy> wrapRenderableBackendTexture(const GrBackendTexture&, GrSurfaceOrigin, 123 int sampleCnt, GrWrapOwnership, 124 GrWrapCacheable); 125 126 /* 127 * Create a render target proxy that wraps a backend render target 128 */ 129 sk_sp<GrSurfaceProxy> wrapBackendRenderTarget(const GrBackendRenderTarget&, GrSurfaceOrigin); 130 131 /* 132 * Create a render target proxy that wraps a backend texture 133 */ 134 sk_sp<GrSurfaceProxy> wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex, 135 GrSurfaceOrigin origin, 136 int sampleCnt); 137 138 sk_sp<GrRenderTargetProxy> wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&, 139 const GrVkDrawableInfo&); 140 141 using LazyInstantiateCallback = std::function<sk_sp<GrSurface>(GrResourceProvider*)>; 142 143 enum class Renderable : bool { 144 kNo = false, 145 kYes = true 146 }; 147 148 struct TextureInfo { 149 GrMipMapped fMipMapped; 150 GrTextureType fTextureType; 151 }; 152 153 using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType; 154 /** 155 * Creates a texture proxy that will be instantiated by a user-supplied callback during flush. 156 * (Stencil is not supported by this method.) The width and height must either both be greater 157 * than 0 or both less than or equal to zero. A non-positive value is a signal that the width 158 * and height are currently unknown. 159 * 160 * When called, the callback must be able to cleanup any resources that it captured at creation. 161 * It also must support being passed in a null GrResourceProvider. When this happens, the 162 * callback should cleanup any resources it captured and return an empty sk_sp<GrTextureProxy>. 163 */ 164 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrBackendFormat&, 165 const GrSurfaceDesc&, GrSurfaceOrigin, GrMipMapped, 166 GrInternalSurfaceFlags, SkBackingFit, SkBudgeted, 167 LazyInstantiationType); 168 169 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrBackendFormat&, 170 const GrSurfaceDesc&, GrSurfaceOrigin, GrMipMapped, 171 GrInternalSurfaceFlags, SkBackingFit, SkBudgeted); 172 173 sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrBackendFormat&, 174 const GrSurfaceDesc&, GrSurfaceOrigin, GrMipMapped, 175 SkBackingFit, SkBudgeted); 176 177 /** A null TextureInfo indicates a non-textureable render target. */ 178 sk_sp<GrRenderTargetProxy> createLazyRenderTargetProxy(LazyInstantiateCallback&&, 179 const GrBackendFormat&, 180 const GrSurfaceDesc&, 181 GrSurfaceOrigin origin, 182 GrInternalSurfaceFlags, 183 const TextureInfo*, 184 SkBackingFit, 185 SkBudgeted); 186 187 /** 188 * Fully lazy proxies have unspecified width and height. Methods that rely on those values 189 * (e.g., width, height, getBoundsRect) should be avoided. 190 */ 191 static sk_sp<GrTextureProxy> MakeFullyLazyProxy(LazyInstantiateCallback&&, 192 const GrBackendFormat&, Renderable, 193 GrSurfaceOrigin, GrPixelConfig, const GrCaps&); 194 195 // 'proxy' is about to be used as a texture src or drawn to. This query can be used to 196 // determine if it is going to need a texture domain or a full clear. 197 static bool IsFunctionallyExact(GrSurfaceProxy* proxy); 198 199 enum class InvalidateGPUResource : bool { kNo = false, kYes = true }; 200 201 /* 202 * This method ensures that, if a proxy w/ the supplied unique key exists, it is removed from 203 * the proxy provider's map and its unique key is removed. If 'invalidateSurface' is true, it 204 * will independently ensure that the unique key is removed from any GrGpuResources that may 205 * have it. 206 * 207 * If 'proxy' is provided (as an optimization to stop re-looking it up), its unique key must be 208 * valid and match the provided unique key. 209 * 210 * This method is called if either the proxy attached to the unique key is being deleted 211 * (in which case we don't want it cluttering up the hash table) or the client has indicated 212 * that it will never refer to the unique key again. 213 */ 214 void processInvalidUniqueKey(const GrUniqueKey&, GrTextureProxy*, InvalidateGPUResource); 215 contextUniqueID()216 uint32_t contextUniqueID() const { return fContextUniqueID; } caps()217 const GrCaps* caps() const { return fCaps.get(); } refCaps()218 sk_sp<const GrCaps> refCaps() const { return fCaps; } 219 abandon()220 void abandon() { 221 fResourceCache = nullptr; 222 fResourceProvider = nullptr; 223 fAbandoned = true; 224 } 225 isAbandoned()226 bool isAbandoned() const { 227 #ifdef SK_DEBUG 228 if (fAbandoned) { 229 SkASSERT(!fResourceCache && !fResourceProvider); 230 } 231 #endif 232 return fAbandoned; 233 } 234 235 int numUniqueKeyProxies_TestOnly() const; 236 237 // This is called on a DDL's proxyprovider when the DDL is finished. The uniquely keyed 238 // proxies need to keep their unique key but cannot hold on to the proxy provider unique 239 // pointer. 240 void orphanAllUniqueKeys(); 241 // This is only used by GrContext::releaseResourcesAndAbandonContext() 242 void removeAllUniqueKeys(); 243 244 /** 245 * Are we currently recording a DDL? 246 */ recordingDDL()247 bool recordingDDL() const { return !SkToBool(fResourceProvider); } 248 249 /* 250 * Create a texture proxy that is backed by an instantiated GrSurface. 251 */ 252 sk_sp<GrTextureProxy> testingOnly_createInstantiatedProxy(const GrSurfaceDesc&, GrSurfaceOrigin, 253 SkBackingFit, SkBudgeted); 254 sk_sp<GrTextureProxy> testingOnly_createWrapped(sk_sp<GrTexture>, GrSurfaceOrigin); 255 256 private: 257 friend class GrAHardwareBufferImageGenerator; // for createWrapped 258 friend class GrResourceProvider; // for createWrapped 259 260 sk_sp<GrTextureProxy> createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin); 261 262 struct UniquelyKeyedProxyHashTraits { GetKeyUniquelyKeyedProxyHashTraits263 static const GrUniqueKey& GetKey(const GrTextureProxy& p) { return p.getUniqueKey(); } 264 HashUniquelyKeyedProxyHashTraits265 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); } 266 }; 267 typedef SkTDynamicHash<GrTextureProxy, GrUniqueKey, UniquelyKeyedProxyHashTraits> UniquelyKeyedProxyHash; 268 269 // This holds the texture proxies that have unique keys. The resourceCache does not get a ref 270 // on these proxies but they must send a message to the resourceCache when they are deleted. 271 UniquelyKeyedProxyHash fUniquelyKeyedProxies; 272 273 GrResourceProvider* fResourceProvider; 274 GrResourceCache* fResourceCache; 275 bool fAbandoned; 276 sk_sp<const GrCaps> fCaps; 277 // If this provider is owned by a DDLContext then this is the DirectContext's ID. 278 uint32_t fContextUniqueID; 279 280 // In debug builds we guard against improper thread handling 281 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;) 282 }; 283 284 #endif 285