Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 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 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 11 | #include "GrCaps.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 12 | #include "GrResourceKey.h" |
| 13 | #include "GrTextureProxy.h" |
| 14 | #include "GrTypes.h" |
| 15 | #include "SkRefCnt.h" |
| 16 | #include "SkTDynamicHash.h" |
| 17 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 18 | class GrResourceProvider; |
| 19 | class GrSingleOwner; |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 20 | class GrBackendRenderTarget; |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 21 | class SkBitmap; |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 22 | class SkImage; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 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*); |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 30 | GrProxyProvider(uint32_t contextUniqueID, sk_sp<const GrCaps>, GrSingleOwner*); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 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 | */ |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 38 | bool assignUniqueKeyToProxy(const GrUniqueKey&, GrTextureProxy*); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 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(const GrUniqueKey&, 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 | /* |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 64 | * Create a texture proxy that is backed by an instantiated GrSurface. This is almost entirely |
| 65 | * used by Skia's testing code. |
| 66 | * DDL TODO: remove the remaining Skia-internal use of this method and make it truly |
| 67 | * testing-only. |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 68 | */ |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 69 | sk_sp<GrTextureProxy> createInstantiatedProxy(const GrSurfaceDesc&, GrSurfaceOrigin, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 70 | SkBackingFit, SkBudgeted, |
| 71 | GrSurfaceDescFlags = kNone_GrSurfaceFlags); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * Create an un-mipmapped texture proxy with data. |
| 75 | * DDL TODO: need to refine ownership semantics of 'srcData' if we're in completely |
| 76 | * deferred mode |
| 77 | */ |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 78 | sk_sp<GrTextureProxy> createTextureProxy(const GrSurfaceDesc&, SkBudgeted, const void* srcData, |
| 79 | size_t rowBytes); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 80 | |
| 81 | /* |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 82 | * Create an un-mipmapped texture proxy with data. The SkImage must be a raster backend image. |
| 83 | * Since the SkImage is ref counted, we simply take a ref on it to keep the data alive until we |
| 84 | * actually upload the data to the gpu. |
| 85 | */ |
| 86 | sk_sp<GrTextureProxy> createTextureProxy(sk_sp<SkImage> srcImage, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 87 | GrSurfaceDescFlags descFlags, |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 88 | int sampleCnt, |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 89 | SkBudgeted budgeted, |
| 90 | SkBackingFit fit); |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 91 | |
| 92 | /* |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 93 | * Create a mipmapped texture proxy without any data. |
| 94 | * |
| 95 | * Like the call above but there are no texels to upload. A texture proxy is returned that |
| 96 | * simply has space allocated for the mips. We will allocated the full amount of mip levels |
| 97 | * based on the width and height in the GrSurfaceDesc. |
| 98 | */ |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 99 | sk_sp<GrTextureProxy> createMipMapProxy(const GrSurfaceDesc&, GrSurfaceOrigin, SkBudgeted); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 100 | |
| 101 | /* |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 102 | * Creates a new mipmapped texture proxy for the bitmap with mip levels generated by the cpu. |
| 103 | */ |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 104 | sk_sp<GrTextureProxy> createMipMapProxyFromBitmap(const SkBitmap& bitmap); |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 105 | |
| 106 | /* |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 107 | * Create a GrSurfaceProxy without any data. |
| 108 | */ |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 109 | sk_sp<GrTextureProxy> createProxy(const GrSurfaceDesc&, GrSurfaceOrigin, GrMipMapped, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 110 | SkBackingFit, SkBudgeted, GrInternalSurfaceFlags); |
Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 111 | |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 112 | sk_sp<GrTextureProxy> createProxy( |
| 113 | const GrSurfaceDesc& desc, GrSurfaceOrigin origin, |
| 114 | SkBackingFit fit, SkBudgeted budgeted, |
| 115 | GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone) { |
| 116 | return this->createProxy(desc, origin, GrMipMapped::kNo, fit, budgeted, surfaceFlags); |
Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 117 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 118 | |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 119 | // These match the definitions in SkImage & GrTexture.h, for whence they came |
| 120 | typedef void* ReleaseContext; |
| 121 | typedef void (*ReleaseProc)(ReleaseContext); |
| 122 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 123 | /* |
| 124 | * Create a texture proxy that wraps a (non-renderable) backend texture. |
| 125 | */ |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 126 | sk_sp<GrTextureProxy> wrapBackendTexture(const GrBackendTexture&, GrSurfaceOrigin, |
| 127 | GrWrapOwnership = kBorrow_GrWrapOwnership, |
| 128 | ReleaseProc = nullptr, ReleaseContext = nullptr); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * Create a texture proxy that wraps a backend texture and is both texture-able and renderable |
| 132 | */ |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 133 | sk_sp<GrTextureProxy> wrapRenderableBackendTexture(const GrBackendTexture&, |
| 134 | GrSurfaceOrigin, |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 135 | int sampleCnt, |
| 136 | GrWrapOwnership = kBorrow_GrWrapOwnership); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 137 | |
| 138 | /* |
| 139 | * Create a render target proxy that wraps a backend rendertarget |
| 140 | */ |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 141 | sk_sp<GrSurfaceProxy> wrapBackendRenderTarget(const GrBackendRenderTarget&, GrSurfaceOrigin); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 142 | |
| 143 | /* |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 144 | * Create a render target proxy that wraps a backend texture |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 145 | */ |
Kevin Lubick | b5502b2 | 2018-03-12 10:17:06 -0400 | [diff] [blame] | 146 | sk_sp<GrSurfaceProxy> wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex, |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 147 | GrSurfaceOrigin origin, |
| 148 | int sampleCnt); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 149 | |
Robert Phillips | ce5209a | 2018-02-13 11:13:51 -0500 | [diff] [blame] | 150 | using LazyInstantiateCallback = std::function<sk_sp<GrSurface>(GrResourceProvider*)>; |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 151 | |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 152 | enum class Renderable : bool { |
| 153 | kNo = false, |
| 154 | kYes = true |
| 155 | }; |
| 156 | |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 157 | struct TextureInfo { |
| 158 | GrMipMapped fMipMapped; |
| 159 | GrTextureType fTextureType; |
| 160 | }; |
| 161 | |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 162 | using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType; |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 163 | /** |
| 164 | * Creates a texture proxy that will be instantiated by a user-supplied callback during flush. |
| 165 | * (Stencil is not supported by this method.) The width and height must either both be greater |
| 166 | * than 0 or both less than or equal to zero. A non-positive value is a signal that the width |
| 167 | * and height are currently unknown. |
| 168 | * |
| 169 | * When called, the callback must be able to cleanup any resources that it captured at creation. |
| 170 | * It also must support being passed in a null GrResourceProvider. When this happens, the |
| 171 | * callback should cleanup any resources it captured and return an empty sk_sp<GrTextureProxy>. |
| 172 | */ |
| 173 | sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrSurfaceDesc&, |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 174 | GrSurfaceOrigin, GrMipMapped, GrTextureType, |
| 175 | GrInternalSurfaceFlags, SkBackingFit, SkBudgeted, |
| 176 | LazyInstantiationType); |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 177 | |
| 178 | sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrSurfaceDesc&, |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 179 | GrSurfaceOrigin, GrMipMapped, GrTextureType, |
| 180 | GrInternalSurfaceFlags, SkBackingFit, SkBudgeted); |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 181 | |
| 182 | sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrSurfaceDesc&, |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 183 | GrSurfaceOrigin, GrMipMapped, GrTextureType, SkBackingFit, |
| 184 | SkBudgeted); |
| 185 | |
| 186 | /** A null TextureInfo indicates a non-textureable render target. */ |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 187 | sk_sp<GrRenderTargetProxy> createLazyRenderTargetProxy(LazyInstantiateCallback&&, |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 188 | const GrSurfaceDesc&, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 189 | GrSurfaceOrigin origin, |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 190 | GrInternalSurfaceFlags, |
| 191 | const TextureInfo*, |
| 192 | SkBackingFit, |
| 193 | SkBudgeted); |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 194 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 195 | /** |
| 196 | * Fully lazy proxies have unspecified width and height. Methods that rely on those values |
| 197 | * (e.g., width, height, getBoundsRect) should be avoided. |
| 198 | */ |
| 199 | static sk_sp<GrTextureProxy> MakeFullyLazyProxy(LazyInstantiateCallback&&, Renderable, |
| 200 | GrSurfaceOrigin, GrPixelConfig, const GrCaps&); |
| 201 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 202 | // 'proxy' is about to be used as a texture src or drawn to. This query can be used to |
| 203 | // determine if it is going to need a texture domain or a full clear. |
| 204 | static bool IsFunctionallyExact(GrSurfaceProxy* proxy); |
| 205 | |
| 206 | /** |
| 207 | * Either the proxy attached to the unique key is being deleted (in which case we |
| 208 | * don't want it cluttering up the hash table) or the client has indicated that |
| 209 | * it will never refer to the unique key again. In either case, remove the key |
| 210 | * from the hash table. |
| 211 | * Note: this does not, by itself, alter unique key attached to the underlying GrTexture. |
| 212 | */ |
| 213 | void processInvalidProxyUniqueKey(const GrUniqueKey&); |
| 214 | |
| 215 | /** |
| 216 | * Same as above, but you must pass in a GrTextureProxy to save having to search for it. The |
| 217 | * GrUniqueKey of the proxy must be valid and it must match the passed in key. This function |
| 218 | * also gives the option to invalidate the GrUniqueKey on the underlying GrTexture. |
| 219 | */ |
| 220 | void processInvalidProxyUniqueKey(const GrUniqueKey&, GrTextureProxy*, bool invalidateSurface); |
| 221 | |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 222 | uint32_t contextUniqueID() const { return fContextUniqueID; } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 223 | const GrCaps* caps() const { return fCaps.get(); } |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 224 | sk_sp<const GrCaps> refCaps() const { return fCaps; } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 225 | |
| 226 | void abandon() { |
| 227 | fResourceCache = nullptr; |
| 228 | fResourceProvider = nullptr; |
Robert Phillips | 4d12051 | 2018-01-19 13:22:07 -0500 | [diff] [blame] | 229 | fAbandoned = true; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | bool isAbandoned() const { |
Robert Phillips | 4d12051 | 2018-01-19 13:22:07 -0500 | [diff] [blame] | 233 | #ifdef SK_DEBUG |
| 234 | if (fAbandoned) { |
| 235 | SkASSERT(!fResourceCache && !fResourceProvider); |
| 236 | } |
| 237 | #endif |
| 238 | return fAbandoned; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | int numUniqueKeyProxies_TestOnly() const; |
| 242 | |
| 243 | void removeAllUniqueKeys(); |
| 244 | |
Greg Daniel | 7e1912a | 2018-02-08 09:15:33 -0500 | [diff] [blame] | 245 | /** |
Robert Phillips | 5c4b33b | 2018-03-20 16:23:08 -0400 | [diff] [blame] | 246 | * Are we currently recording a DDL? |
Greg Daniel | 7e1912a | 2018-02-08 09:15:33 -0500 | [diff] [blame] | 247 | */ |
Robert Phillips | 5c4b33b | 2018-03-20 16:23:08 -0400 | [diff] [blame] | 248 | bool recordingDDL() const { return !SkToBool(fResourceProvider); } |
Greg Daniel | 7e1912a | 2018-02-08 09:15:33 -0500 | [diff] [blame] | 249 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 250 | private: |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 251 | friend class GrAHardwareBufferImageGenerator; // for createWrapped |
| 252 | |
| 253 | sk_sp<GrTextureProxy> createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin); |
| 254 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 255 | struct UniquelyKeyedProxyHashTraits { |
| 256 | static const GrUniqueKey& GetKey(const GrTextureProxy& p) { return p.getUniqueKey(); } |
| 257 | |
| 258 | static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); } |
| 259 | }; |
| 260 | typedef SkTDynamicHash<GrTextureProxy, GrUniqueKey, UniquelyKeyedProxyHashTraits> UniquelyKeyedProxyHash; |
| 261 | |
| 262 | // This holds the texture proxies that have unique keys. The resourceCache does not get a ref |
| 263 | // on these proxies but they must send a message to the resourceCache when they are deleted. |
| 264 | UniquelyKeyedProxyHash fUniquelyKeyedProxies; |
| 265 | |
| 266 | GrResourceProvider* fResourceProvider; |
| 267 | GrResourceCache* fResourceCache; |
Robert Phillips | 4d12051 | 2018-01-19 13:22:07 -0500 | [diff] [blame] | 268 | bool fAbandoned; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 269 | sk_sp<const GrCaps> fCaps; |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 270 | // If this provider is owned by a DDLContext then this is the DirectContext's ID. |
| 271 | uint32_t fContextUniqueID; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 272 | |
| 273 | // In debug builds we guard against improper thread handling |
| 274 | SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;) |
| 275 | }; |
| 276 | |
| 277 | #endif |