GrProxyProvider.h 13 KB

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