GrResourceProvider.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright 2015 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 GrResourceProvider_DEFINED
  8. #define GrResourceProvider_DEFINED
  9. #include "include/gpu/GrContextOptions.h"
  10. #include "include/private/SkImageInfoPriv.h"
  11. #include "src/core/SkScalerContext.h"
  12. #include "src/gpu/GrGpuBuffer.h"
  13. #include "src/gpu/GrResourceCache.h"
  14. class GrBackendRenderTarget;
  15. class GrBackendSemaphore;
  16. class GrBackendTexture;
  17. class GrGpu;
  18. class GrPath;
  19. class GrRenderTarget;
  20. class GrResourceProviderPriv;
  21. class GrSemaphore;
  22. class GrSingleOwner;
  23. class GrStencilAttachment;
  24. class GrTexture;
  25. struct GrVkDrawableInfo;
  26. class GrStyle;
  27. class SkDescriptor;
  28. class SkPath;
  29. class SkTypeface;
  30. /**
  31. * A factory for arbitrary resource types. This class is intended for use within the Gr code base.
  32. *
  33. * Some members force callers to make a flags (pendingIO) decision. This can be relaxed once
  34. * https://bug.skia.org/4156 is fixed.
  35. */
  36. class GrResourceProvider {
  37. public:
  38. /** These flags govern which scratch resources we are allowed to return */
  39. enum class Flags {
  40. kNone = 0x0,
  41. /** If the caller intends to do direct reads/writes to/from the CPU then this flag must be
  42. * set when accessing resources during a GrOpList flush. This includes the execution of
  43. * GrOp objects. The reason is that these memory operations are done immediately and
  44. * will occur out of order WRT the operations being flushed.
  45. * Make this automatic: https://bug.skia.org/4156
  46. */
  47. kNoPendingIO = 0x1,
  48. };
  49. GrResourceProvider(GrGpu*, GrResourceCache*, GrSingleOwner*);
  50. /**
  51. * Finds a resource in the cache, based on the specified key. Prior to calling this, the caller
  52. * must be sure that if a resource of exists in the cache with the given unique key then it is
  53. * of type T.
  54. */
  55. template <typename T = GrGpuResource>
  56. typename std::enable_if<std::is_base_of<GrGpuResource, T>::value, sk_sp<T>>::type
  57. findByUniqueKey(const GrUniqueKey& key) {
  58. return sk_sp<T>(static_cast<T*>(this->findResourceByUniqueKey(key).release()));
  59. }
  60. ///////////////////////////////////////////////////////////////////////////
  61. // Textures
  62. /**
  63. * Finds a texture that approximately matches the descriptor. Will be at least as large in width
  64. * and height as desc specifies. If renderable is kYes then the GrTexture will also be a
  65. * GrRenderTarget. The texture's format and sample count will always match the request.
  66. * The contents of the texture are undefined.
  67. */
  68. sk_sp<GrTexture> createApproxTexture(const GrSurfaceDesc&, GrRenderable,
  69. int renderTargetSampleCnt, GrProtected, Flags);
  70. /** Create an exact fit texture with no initial data to upload. */
  71. sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, GrRenderable, int renderTargetSampleCnt,
  72. SkBudgeted, GrProtected, Flags = Flags::kNone);
  73. sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, GrRenderable, int renderTargetSampleCnt,
  74. SkBudgeted, GrProtected, const GrMipLevel texels[],
  75. int mipLevelCount);
  76. /** Create a potentially loose fit texture with the provided data */
  77. sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, GrRenderable, int renderTargetSampleCnt,
  78. SkBudgeted, SkBackingFit, GrProtected, const GrMipLevel&, Flags);
  79. /**
  80. * Creates a compressed texture. The GrGpu must support the SkImageImage::Compression type.
  81. * This does not currently support MIP maps. It will not be renderable.
  82. */
  83. sk_sp<GrTexture> createCompressedTexture(int width, int height, SkImage::CompressionType,
  84. SkBudgeted, SkData* data);
  85. ///////////////////////////////////////////////////////////////////////////
  86. // Wrapped Backend Surfaces
  87. /**
  88. * Wraps an existing texture with a GrTexture object.
  89. *
  90. * GrIOType must either be kRead or kRW. kRead blocks any operations that would modify the
  91. * pixels (e.g. dst for a copy, regenerating MIP levels, write pixels).
  92. *
  93. * OpenGL: if the object is a texture Gr may change its GL texture params
  94. * when it is drawn.
  95. *
  96. * @return GrTexture object or NULL on failure.
  97. */
  98. sk_sp<GrTexture> wrapBackendTexture(const GrBackendTexture& tex, GrColorType, GrWrapOwnership,
  99. GrWrapCacheable, GrIOType);
  100. /**
  101. * This makes the backend texture be renderable. If sampleCnt is > 1 and the underlying API
  102. * uses separate MSAA render buffers then a MSAA render buffer is created that resolves
  103. * to the texture.
  104. */
  105. sk_sp<GrTexture> wrapRenderableBackendTexture(const GrBackendTexture& tex,
  106. int sampleCnt,
  107. GrColorType,
  108. GrWrapOwnership,
  109. GrWrapCacheable);
  110. /**
  111. * Wraps an existing render target with a GrRenderTarget object. It is
  112. * similar to wrapBackendTexture but can be used to draw into surfaces
  113. * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
  114. * the client will resolve to a texture). Currently wrapped render targets
  115. * always use the kBorrow_GrWrapOwnership and GrWrapCacheable::kNo semantics.
  116. *
  117. * @return GrRenderTarget object or NULL on failure.
  118. */
  119. sk_sp<GrRenderTarget> wrapBackendRenderTarget(const GrBackendRenderTarget&,
  120. GrColorType colorType);
  121. sk_sp<GrRenderTarget> wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&,
  122. const GrVkDrawableInfo&);
  123. static const uint32_t kMinScratchTextureSize;
  124. /**
  125. * Either finds and refs, or creates a static buffer with the given parameters and contents.
  126. *
  127. * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
  128. * @param size minimum size of buffer to return.
  129. * @param data optional data with which to initialize the buffer.
  130. * @param key Key to be assigned to the buffer.
  131. *
  132. * @return The buffer if successful, otherwise nullptr.
  133. */
  134. sk_sp<const GrGpuBuffer> findOrMakeStaticBuffer(GrGpuBufferType intendedType, size_t size,
  135. const void* data, const GrUniqueKey& key);
  136. /**
  137. * Either finds and refs, or creates an index buffer with a repeating pattern for drawing
  138. * contiguous vertices of a repeated mesh. If the return is non-null, the caller owns a ref on
  139. * the returned GrBuffer.
  140. *
  141. * @param pattern the pattern of indices to repeat
  142. * @param patternSize size in bytes of the pattern
  143. * @param reps number of times to repeat the pattern
  144. * @param vertCount number of vertices the pattern references
  145. * @param key Key to be assigned to the index buffer.
  146. *
  147. * @return The index buffer if successful, otherwise nullptr.
  148. */
  149. sk_sp<const GrGpuBuffer> findOrCreatePatternedIndexBuffer(const uint16_t* pattern,
  150. int patternSize,
  151. int reps,
  152. int vertCount,
  153. const GrUniqueKey& key) {
  154. if (auto buffer = this->findByUniqueKey<const GrGpuBuffer>(key)) {
  155. return buffer;
  156. }
  157. return this->createPatternedIndexBuffer(pattern, patternSize, reps, vertCount, &key);
  158. }
  159. /**
  160. * Returns an index buffer that can be used to render quads.
  161. * Six indices per quad: 0, 1, 2, 2, 1, 3, etc.
  162. * The max number of quads is the buffer's index capacity divided by 6.
  163. * Draw with GrPrimitiveType::kTriangles
  164. * @ return the quad index buffer
  165. */
  166. sk_sp<const GrGpuBuffer> refQuadIndexBuffer() {
  167. if (!fQuadIndexBuffer) {
  168. fQuadIndexBuffer = this->createQuadIndexBuffer();
  169. }
  170. return fQuadIndexBuffer;
  171. }
  172. static int QuadCountOfQuadBuffer();
  173. /**
  174. * Factories for GrPath objects. It's an error to call these if path rendering
  175. * is not supported.
  176. */
  177. sk_sp<GrPath> createPath(const SkPath&, const GrStyle&);
  178. /**
  179. * Returns a buffer.
  180. *
  181. * @param size minimum size of buffer to return.
  182. * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
  183. * @param GrAccessPattern hint to the graphics subsystem about how the data will be accessed.
  184. * @param flags see Flags enum.
  185. * @param data optional data with which to initialize the buffer.
  186. *
  187. * @return the buffer if successful, otherwise nullptr.
  188. */
  189. sk_sp<GrGpuBuffer> createBuffer(size_t size, GrGpuBufferType intendedType, GrAccessPattern,
  190. const void* data = nullptr);
  191. /**
  192. * If passed in render target already has a stencil buffer with at least "numSamples" samples,
  193. * return true. Otherwise attempt to attach one and return true on success.
  194. */
  195. bool attachStencilAttachment(GrRenderTarget* rt, int numStencilSamples);
  196. /**
  197. * Wraps an existing texture with a GrRenderTarget object. This is useful when the provided
  198. * texture has a format that cannot be textured from by Skia, but we want to raster to it.
  199. *
  200. * The texture is wrapped as borrowed. The texture object will not be freed once the
  201. * render target is destroyed.
  202. *
  203. * @return GrRenderTarget object or NULL on failure.
  204. */
  205. sk_sp<GrRenderTarget> wrapBackendTextureAsRenderTarget(const GrBackendTexture&,
  206. int sampleCnt,
  207. GrColorType);
  208. /**
  209. * Assigns a unique key to a resource. If the key is associated with another resource that
  210. * association is removed and replaced by this resource.
  211. */
  212. void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*);
  213. sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned = true);
  214. enum class SemaphoreWrapType {
  215. kWillSignal,
  216. kWillWait,
  217. };
  218. sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore&,
  219. SemaphoreWrapType wrapType,
  220. GrWrapOwnership = kBorrow_GrWrapOwnership);
  221. void abandon() {
  222. fCache = nullptr;
  223. fGpu = nullptr;
  224. }
  225. uint32_t contextUniqueID() const { return fCache->contextUniqueID(); }
  226. const GrCaps* caps() const { return fCaps.get(); }
  227. bool overBudget() const { return fCache->overBudget(); }
  228. static uint32_t MakeApprox(uint32_t value);
  229. inline GrResourceProviderPriv priv();
  230. inline const GrResourceProviderPriv priv() const;
  231. private:
  232. sk_sp<GrGpuResource> findResourceByUniqueKey(const GrUniqueKey&);
  233. // Attempts to find a resource in the cache that exactly matches the GrSurfaceDesc. Failing that
  234. // it returns null. If non-null, the resulting texture is always budgeted.
  235. sk_sp<GrTexture> refScratchTexture(const GrSurfaceDesc&, GrRenderable,
  236. int renderTargetSampleCnt, GrProtected, Flags);
  237. /*
  238. * Try to find an existing scratch texture that exactly matches 'desc'. If successful
  239. * update the budgeting accordingly.
  240. */
  241. sk_sp<GrTexture> getExactScratch(const GrSurfaceDesc&, GrRenderable, int renderTargetSampleCnt,
  242. SkBudgeted, GrProtected, Flags);
  243. GrResourceCache* cache() { return fCache; }
  244. const GrResourceCache* cache() const { return fCache; }
  245. friend class GrResourceProviderPriv;
  246. // Method made available via GrResourceProviderPriv
  247. GrGpu* gpu() { return fGpu; }
  248. const GrGpu* gpu() const { return fGpu; }
  249. bool isAbandoned() const {
  250. SkASSERT(SkToBool(fGpu) == SkToBool(fCache));
  251. return !SkToBool(fCache);
  252. }
  253. sk_sp<const GrGpuBuffer> createPatternedIndexBuffer(const uint16_t* pattern,
  254. int patternSize,
  255. int reps,
  256. int vertCount,
  257. const GrUniqueKey* key);
  258. sk_sp<const GrGpuBuffer> createQuadIndexBuffer();
  259. GrResourceCache* fCache;
  260. GrGpu* fGpu;
  261. sk_sp<const GrCaps> fCaps;
  262. sk_sp<const GrGpuBuffer> fQuadIndexBuffer;
  263. // In debug builds we guard against improper thread handling
  264. SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
  265. };
  266. GR_MAKE_BITFIELD_CLASS_OPS(GrResourceProvider::Flags);
  267. #endif