| 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 | |
| 11 | #include "GrResourceKey.h" |
| 12 | #include "GrTextureProxy.h" |
| 13 | #include "GrTypes.h" |
| 14 | #include "SkRefCnt.h" |
| 15 | #include "SkTDynamicHash.h" |
| 16 | |
| 17 | class GrCaps; |
| 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*); |
| 30 | |
| 31 | ~GrProxyProvider(); |
| 32 | |
| 33 | /* |
| 34 | * Assigns a unique key to a proxy. The proxy will be findable via this key using |
| 35 | * findProxyByUniqueKey(). It is an error if an existing proxy already has a key. |
| 36 | */ |
| Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 37 | bool assignUniqueKeyToProxy(const GrUniqueKey&, GrTextureProxy*); |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * Sets the unique key of the provided proxy to the unique key of the surface. The surface must |
| 41 | * have a valid unique key. |
| 42 | */ |
| 43 | void adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface*); |
| 44 | |
| 45 | /* |
| 46 | * Removes a unique key from a proxy. If the proxy has already been instantiated, it will |
| 47 | * also remove the unique key from the target GrSurface. |
| 48 | */ |
| 49 | void removeUniqueKeyFromProxy(const GrUniqueKey&, GrTextureProxy*); |
| 50 | |
| 51 | /* |
| 52 | * Finds a proxy by unique key. |
| 53 | */ |
| 54 | sk_sp<GrTextureProxy> findProxyByUniqueKey(const GrUniqueKey&, GrSurfaceOrigin); |
| 55 | |
| 56 | /* |
| 57 | * Finds a proxy by unique key or creates a new one that wraps a resource matching the unique |
| 58 | * key. |
| 59 | */ |
| 60 | sk_sp<GrTextureProxy> findOrCreateProxyByUniqueKey(const GrUniqueKey&, GrSurfaceOrigin); |
| 61 | |
| 62 | /* |
| Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 63 | * Create a texture proxy that is backed by an instantiated GrSurface. This is almost entirely |
| 64 | * used by Skia's testing code. |
| 65 | * DDL TODO: remove the remaining Skia-internal use of this method and make it truly |
| 66 | * testing-only. |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 67 | */ |
| Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 68 | sk_sp<GrTextureProxy> createInstantiatedProxy(const GrSurfaceDesc&, SkBackingFit, SkBudgeted, |
| 69 | uint32_t flags = 0); |
| 70 | |
| 71 | /* |
| 72 | * Create an un-mipmapped texture proxy with data. |
| 73 | * DDL TODO: need to refine ownership semantics of 'srcData' if we're in completely |
| 74 | * deferred mode |
| 75 | */ |
| 76 | sk_sp<GrTextureProxy> createTextureProxy(const GrSurfaceDesc&, SkBudgeted, |
| 77 | const void* srcData, size_t rowBytes); |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 78 | |
| 79 | /* |
| Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 80 | * Create an un-mipmapped texture proxy with data. The SkImage must be a raster backend image. |
| 81 | * Since the SkImage is ref counted, we simply take a ref on it to keep the data alive until we |
| 82 | * actually upload the data to the gpu. |
| 83 | */ |
| 84 | sk_sp<GrTextureProxy> createTextureProxy(sk_sp<SkImage> srcImage, |
| 85 | GrSurfaceFlags flags, |
| 86 | GrSurfaceOrigin origin, |
| 87 | int sampleCnt, |
| Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 88 | SkBudgeted budgeted, |
| 89 | SkBackingFit fit); |
| Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 90 | |
| 91 | /* |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 92 | * Create a mipmapped texture proxy with data. |
| Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 93 | * |
| 94 | * @param desc Description of the texture properties. |
| 95 | * @param budgeted Does the texture count against the resource cache budget? |
| 96 | * @param texels A contiguous array of mipmap levels |
| 97 | * @param mipLevelCount The amount of elements in the texels array |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 98 | */ |
| Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 99 | sk_sp<GrTextureProxy> createMipMapProxy(const GrSurfaceDesc&, SkBudgeted, |
| 100 | const GrMipLevel texels[], int mipLevelCount, |
| 101 | SkDestinationSurfaceColorMode mipColorMode = |
| 102 | SkDestinationSurfaceColorMode::kLegacy); |
| 103 | |
| Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 104 | /* |
| 105 | * Create a mipmapped texture proxy without any data. |
| 106 | * |
| 107 | * Like the call above but there are no texels to upload. A texture proxy is returned that |
| 108 | * simply has space allocated for the mips. We will allocated the full amount of mip levels |
| 109 | * based on the width and height in the GrSurfaceDesc. |
| 110 | */ |
| 111 | sk_sp<GrTextureProxy> createMipMapProxy(const GrSurfaceDesc&, SkBudgeted); |
| 112 | |
| 113 | /* |
| Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame^] | 114 | * Creates a new mipmapped texture proxy for the bitmap with mip levels generated by the cpu. |
| 115 | */ |
| 116 | sk_sp<GrTextureProxy> createMipMapProxyFromBitmap(const SkBitmap& bitmap, |
| 117 | SkColorSpace* dstColorSpace); |
| 118 | |
| 119 | /* |
| Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 120 | * Create a GrSurfaceProxy without any data. |
| 121 | */ |
| 122 | sk_sp<GrTextureProxy> createProxy(const GrSurfaceDesc&, SkBackingFit, SkBudgeted, |
| 123 | uint32_t flags = 0); |
| 124 | |
| Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 125 | // These match the definitions in SkImage & GrTexture.h, for whence they came |
| 126 | typedef void* ReleaseContext; |
| 127 | typedef void (*ReleaseProc)(ReleaseContext); |
| 128 | |
| Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 129 | /* |
| 130 | * Create a texture proxy that wraps a (non-renderable) backend texture. |
| 131 | */ |
| Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 132 | sk_sp<GrTextureProxy> createWrappedTextureProxy(const GrBackendTexture&, GrSurfaceOrigin, |
| 133 | GrWrapOwnership = kBorrow_GrWrapOwnership, |
| 134 | ReleaseProc = nullptr, |
| 135 | ReleaseContext = nullptr); |
| Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 136 | |
| 137 | /* |
| 138 | * Create a texture proxy that wraps a backend texture and is both texture-able and renderable |
| 139 | */ |
| 140 | sk_sp<GrTextureProxy> createWrappedTextureProxy(const GrBackendTexture&, |
| 141 | GrSurfaceOrigin, |
| 142 | int sampleCnt); |
| 143 | |
| 144 | /* |
| 145 | * Create a render target proxy that wraps a backend rendertarget |
| 146 | */ |
| 147 | sk_sp<GrSurfaceProxy> createWrappedRenderTargetProxy(const GrBackendRenderTarget&, |
| 148 | GrSurfaceOrigin); |
| 149 | |
| 150 | /* |
| 151 | * Create a render target proxy that wraps a backend texture? |
| 152 | */ |
| 153 | sk_sp<GrSurfaceProxy> createWrappedRenderTargetProxy(const GrBackendTexture& tex, |
| 154 | GrSurfaceOrigin origin, |
| 155 | int sampleCnt); |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 156 | |
| Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 157 | using LazyInstantiateCallback = std::function<sk_sp<GrSurface>(GrResourceProvider*, |
| Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 158 | GrSurfaceOrigin* outOrigin)>; |
| 159 | |
| Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 160 | enum class Textureable : bool { |
| 161 | kNo = false, |
| 162 | kYes = true |
| 163 | }; |
| 164 | |
| Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 165 | enum class Renderable : bool { |
| 166 | kNo = false, |
| 167 | kYes = true |
| 168 | }; |
| 169 | |
| 170 | /** |
| 171 | * Creates a texture proxy that will be instantiated by a user-supplied callback during flush. |
| 172 | * (Stencil is not supported by this method.) The width and height must either both be greater |
| 173 | * than 0 or both less than or equal to zero. A non-positive value is a signal that the width |
| 174 | * and height are currently unknown. |
| 175 | * |
| 176 | * When called, the callback must be able to cleanup any resources that it captured at creation. |
| 177 | * It also must support being passed in a null GrResourceProvider. When this happens, the |
| 178 | * callback should cleanup any resources it captured and return an empty sk_sp<GrTextureProxy>. |
| 179 | */ |
| 180 | sk_sp<GrTextureProxy> createLazyProxy(LazyInstantiateCallback&&, const GrSurfaceDesc&, |
| 181 | GrMipMapped, SkBackingFit, SkBudgeted); |
| 182 | |
| 183 | sk_sp<GrTextureProxy> createFullyLazyProxy(LazyInstantiateCallback&&, |
| 184 | Renderable, GrPixelConfig); |
| 185 | |
| Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 186 | sk_sp<GrRenderTargetProxy> createLazyRenderTargetProxy(LazyInstantiateCallback&&, |
| 187 | const GrSurfaceDesc&, Textureable, |
| 188 | GrMipMapped, SkBackingFit, SkBudgeted); |
| 189 | |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 190 | // 'proxy' is about to be used as a texture src or drawn to. This query can be used to |
| 191 | // determine if it is going to need a texture domain or a full clear. |
| 192 | static bool IsFunctionallyExact(GrSurfaceProxy* proxy); |
| 193 | |
| 194 | /** |
| 195 | * Either the proxy attached to the unique key is being deleted (in which case we |
| 196 | * don't want it cluttering up the hash table) or the client has indicated that |
| 197 | * it will never refer to the unique key again. In either case, remove the key |
| 198 | * from the hash table. |
| 199 | * Note: this does not, by itself, alter unique key attached to the underlying GrTexture. |
| 200 | */ |
| 201 | void processInvalidProxyUniqueKey(const GrUniqueKey&); |
| 202 | |
| 203 | /** |
| 204 | * Same as above, but you must pass in a GrTextureProxy to save having to search for it. The |
| 205 | * GrUniqueKey of the proxy must be valid and it must match the passed in key. This function |
| 206 | * also gives the option to invalidate the GrUniqueKey on the underlying GrTexture. |
| 207 | */ |
| 208 | void processInvalidProxyUniqueKey(const GrUniqueKey&, GrTextureProxy*, bool invalidateSurface); |
| 209 | |
| 210 | const GrCaps* caps() const { return fCaps.get(); } |
| 211 | |
| 212 | void abandon() { |
| 213 | fResourceCache = nullptr; |
| 214 | fResourceProvider = nullptr; |
| Robert Phillips | 4d12051 | 2018-01-19 13:22:07 -0500 | [diff] [blame] | 215 | fAbandoned = true; |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | bool isAbandoned() const { |
| Robert Phillips | 4d12051 | 2018-01-19 13:22:07 -0500 | [diff] [blame] | 219 | #ifdef SK_DEBUG |
| 220 | if (fAbandoned) { |
| 221 | SkASSERT(!fResourceCache && !fResourceProvider); |
| 222 | } |
| 223 | #endif |
| 224 | return fAbandoned; |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | int numUniqueKeyProxies_TestOnly() const; |
| 228 | |
| 229 | void removeAllUniqueKeys(); |
| 230 | |
| 231 | private: |
| Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 232 | friend class GrAHardwareBufferImageGenerator; // for createWrapped |
| 233 | |
| 234 | sk_sp<GrTextureProxy> createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin); |
| 235 | |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 236 | struct UniquelyKeyedProxyHashTraits { |
| 237 | static const GrUniqueKey& GetKey(const GrTextureProxy& p) { return p.getUniqueKey(); } |
| 238 | |
| 239 | static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); } |
| 240 | }; |
| 241 | typedef SkTDynamicHash<GrTextureProxy, GrUniqueKey, UniquelyKeyedProxyHashTraits> UniquelyKeyedProxyHash; |
| 242 | |
| 243 | // This holds the texture proxies that have unique keys. The resourceCache does not get a ref |
| 244 | // on these proxies but they must send a message to the resourceCache when they are deleted. |
| 245 | UniquelyKeyedProxyHash fUniquelyKeyedProxies; |
| 246 | |
| 247 | GrResourceProvider* fResourceProvider; |
| 248 | GrResourceCache* fResourceCache; |
| Robert Phillips | 4d12051 | 2018-01-19 13:22:07 -0500 | [diff] [blame] | 249 | bool fAbandoned; |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 250 | sk_sp<const GrCaps> fCaps; |
| 251 | |
| 252 | // In debug builds we guard against improper thread handling |
| 253 | SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;) |
| 254 | }; |
| 255 | |
| 256 | #endif |