GrBackendSurface.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright 2017 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 GrBackendSurface_DEFINED
  8. #define GrBackendSurface_DEFINED
  9. #include "GrTypes.h"
  10. #include "gl/GrGLTypes.h"
  11. #include "mock/GrMockTypes.h"
  12. #include "vk/GrVkTypes.h"
  13. #include "../private/GrVkTypesPriv.h"
  14. class GrVkImageLayout;
  15. #ifdef SK_METAL
  16. #include "mtl/GrMtlTypes.h"
  17. #endif
  18. #if !SK_SUPPORT_GPU
  19. // SkSurface and SkImage rely on a minimal version of these always being available
  20. class SK_API GrBackendTexture {
  21. public:
  22. GrBackendTexture() {}
  23. bool isValid() const { return false; }
  24. };
  25. class SK_API GrBackendRenderTarget {
  26. public:
  27. GrBackendRenderTarget() {}
  28. bool isValid() const { return false; }
  29. };
  30. #else
  31. class SK_API GrBackendFormat {
  32. public:
  33. // Creates an invalid backend format.
  34. GrBackendFormat() : fValid(false) {}
  35. static GrBackendFormat MakeGL(GrGLenum format, GrGLenum target) {
  36. return GrBackendFormat(format, target);
  37. }
  38. static GrBackendFormat MakeVk(VkFormat format) {
  39. return GrBackendFormat(format);
  40. }
  41. #ifdef SK_METAL
  42. static GrBackendFormat MakeMtl(GrMTLPixelFormat format) {
  43. return GrBackendFormat(format);
  44. }
  45. #endif
  46. static GrBackendFormat MakeMock(GrPixelConfig config) {
  47. return GrBackendFormat(config);
  48. }
  49. GrBackendApi backend() const { return fBackend; }
  50. GrTextureType textureType() const { return fTextureType; }
  51. // If the backend API is GL, these return a pointer to the format and target. Otherwise
  52. // it returns nullptr.
  53. const GrGLenum* getGLFormat() const;
  54. const GrGLenum* getGLTarget() const;
  55. // If the backend API is Vulkan, this returns a pointer to a VkFormat. Otherwise
  56. // it returns nullptr
  57. const VkFormat* getVkFormat() const;
  58. #ifdef SK_METAL
  59. // If the backend API is Metal, this returns a pointer to a GrMTLPixelFormat. Otherwise
  60. // it returns nullptr
  61. const GrMTLPixelFormat* getMtlFormat() const;
  62. #endif
  63. // If the backend API is Mock, this returns a pointer to a GrPixelConfig. Otherwise
  64. // it returns nullptr.
  65. const GrPixelConfig* getMockFormat() const;
  66. // If possible, copies the GrBackendFormat and forces the texture type to be Texture2D
  67. GrBackendFormat makeTexture2D() const;
  68. // Returns true if the backend format has been initialized.
  69. bool isValid() const { return fValid; }
  70. private:
  71. GrBackendFormat(GrGLenum format, GrGLenum target);
  72. GrBackendFormat(const VkFormat vkFormat);
  73. #ifdef SK_METAL
  74. GrBackendFormat(const GrMTLPixelFormat mtlFormat);
  75. #endif
  76. GrBackendFormat(const GrPixelConfig config);
  77. GrBackendApi fBackend;
  78. bool fValid;
  79. union {
  80. GrGLenum fGLFormat; // the sized, internal format of the GL resource
  81. VkFormat fVkFormat;
  82. #ifdef SK_METAL
  83. GrMTLPixelFormat fMtlFormat;
  84. #endif
  85. GrPixelConfig fMockFormat;
  86. };
  87. GrTextureType fTextureType;
  88. };
  89. class SK_API GrBackendTexture {
  90. public:
  91. // Creates an invalid backend texture.
  92. GrBackendTexture() : fIsValid(false) {}
  93. // The GrGLTextureInfo must have a valid fFormat.
  94. GrBackendTexture(int width,
  95. int height,
  96. GrMipMapped,
  97. const GrGLTextureInfo& glInfo);
  98. GrBackendTexture(int width,
  99. int height,
  100. const GrVkImageInfo& vkInfo);
  101. #ifdef SK_METAL
  102. GrBackendTexture(int width,
  103. int height,
  104. GrMipMapped,
  105. const GrMtlTextureInfo& mtlInfo);
  106. #endif
  107. GrBackendTexture(int width,
  108. int height,
  109. GrMipMapped,
  110. const GrMockTextureInfo& mockInfo);
  111. GrBackendTexture(const GrBackendTexture& that);
  112. ~GrBackendTexture();
  113. GrBackendTexture& operator=(const GrBackendTexture& that);
  114. int width() const { return fWidth; }
  115. int height() const { return fHeight; }
  116. bool hasMipMaps() const { return GrMipMapped::kYes == fMipMapped; }
  117. GrBackendApi backend() const {return fBackend; }
  118. // If the backend API is GL, copies a snapshot of the GrGLTextureInfo struct into the passed in
  119. // pointer and returns true. Otherwise returns false if the backend API is not GL.
  120. bool getGLTextureInfo(GrGLTextureInfo*) const;
  121. // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
  122. // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
  123. // state. Otherwise returns false if the backend API is not Vulkan.
  124. bool getVkImageInfo(GrVkImageInfo*) const;
  125. // Anytime the client changes the VkImageLayout of the VkImage captured by this
  126. // GrBackendTexture, they must call this function to notify Skia of the changed layout.
  127. void setVkImageLayout(VkImageLayout);
  128. #ifdef SK_METAL
  129. // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
  130. // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
  131. bool getMtlTextureInfo(GrMtlTextureInfo*) const;
  132. #endif
  133. // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
  134. // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
  135. bool getMockTextureInfo(GrMockTextureInfo*) const;
  136. // Returns true if the backend texture has been initialized.
  137. bool isValid() const { return fIsValid; }
  138. #if GR_TEST_UTILS
  139. // We can remove the pixelConfig getter and setter once we remove the GrPixelConfig from the
  140. // GrBackendTexture and plumb the GrPixelconfig manually throughout our code (or remove all use
  141. // of GrPixelConfig in general).
  142. GrPixelConfig pixelConfig() const { return fConfig; }
  143. void setPixelConfig(GrPixelConfig config) { fConfig = config; }
  144. static bool TestingOnly_Equals(const GrBackendTexture& , const GrBackendTexture&);
  145. #endif
  146. private:
  147. // Friending for access to the GrPixelConfig
  148. friend class SkImage;
  149. friend class SkImage_Gpu;
  150. friend class SkImage_GpuBase;
  151. friend class SkImage_GpuYUVA;
  152. friend class SkPromiseImageHelper;
  153. friend class SkSurface;
  154. friend class GrAHardwareBufferImageGenerator;
  155. friend class GrBackendTextureImageGenerator;
  156. friend class GrProxyProvider;
  157. friend class GrGpu;
  158. friend class GrGLGpu;
  159. friend class GrVkGpu;
  160. friend class GrMtlGpu;
  161. friend class PromiseImageHelper;
  162. GrPixelConfig config() const { return fConfig; }
  163. // Requires friending of GrVkGpu (done above already)
  164. sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
  165. friend class GrVkTexture;
  166. #ifdef SK_VULKAN
  167. GrBackendTexture(int width,
  168. int height,
  169. const GrVkImageInfo& vkInfo,
  170. sk_sp<GrVkImageLayout> layout);
  171. #endif
  172. // Free and release and resources being held by the GrBackendTexture.
  173. void cleanup();
  174. bool fIsValid;
  175. int fWidth; //<! width in pixels
  176. int fHeight; //<! height in pixels
  177. GrPixelConfig fConfig;
  178. GrMipMapped fMipMapped;
  179. GrBackendApi fBackend;
  180. union {
  181. GrGLTextureInfo fGLInfo;
  182. GrVkBackendSurfaceInfo fVkInfo;
  183. #ifdef SK_METAL
  184. GrMtlTextureInfo fMtlInfo;
  185. #endif
  186. GrMockTextureInfo fMockInfo;
  187. };
  188. };
  189. class SK_API GrBackendRenderTarget {
  190. public:
  191. // Creates an invalid backend texture.
  192. GrBackendRenderTarget() : fIsValid(false) {}
  193. // The GrGLTextureInfo must have a valid fFormat.
  194. GrBackendRenderTarget(int width,
  195. int height,
  196. int sampleCnt,
  197. int stencilBits,
  198. const GrGLFramebufferInfo& glInfo);
  199. /** Deprecated, use version that does not take stencil bits. */
  200. GrBackendRenderTarget(int width,
  201. int height,
  202. int sampleCnt,
  203. int stencilBits,
  204. const GrVkImageInfo& vkInfo);
  205. GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo);
  206. #ifdef SK_METAL
  207. GrBackendRenderTarget(int width,
  208. int height,
  209. int sampleCnt,
  210. const GrMtlTextureInfo& mtlInfo);
  211. #endif
  212. GrBackendRenderTarget(int width,
  213. int height,
  214. int sampleCnt,
  215. int stencilBits,
  216. const GrMockRenderTargetInfo& mockInfo);
  217. ~GrBackendRenderTarget();
  218. GrBackendRenderTarget(const GrBackendRenderTarget& that);
  219. GrBackendRenderTarget& operator=(const GrBackendRenderTarget&);
  220. int width() const { return fWidth; }
  221. int height() const { return fHeight; }
  222. int sampleCnt() const { return fSampleCnt; }
  223. int stencilBits() const { return fStencilBits; }
  224. GrBackendApi backend() const {return fBackend; }
  225. // If the backend API is GL, copies a snapshot of the GrGLFramebufferInfo struct into the passed
  226. // in pointer and returns true. Otherwise returns false if the backend API is not GL.
  227. bool getGLFramebufferInfo(GrGLFramebufferInfo*) const;
  228. // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
  229. // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
  230. // state. Otherwise returns false if the backend API is not Vulkan.
  231. bool getVkImageInfo(GrVkImageInfo*) const;
  232. // Anytime the client changes the VkImageLayout of the VkImage captured by this
  233. // GrBackendRenderTarget, they must call this function to notify Skia of the changed layout.
  234. void setVkImageLayout(VkImageLayout);
  235. #ifdef SK_METAL
  236. // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
  237. // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
  238. bool getMtlTextureInfo(GrMtlTextureInfo*) const;
  239. #endif
  240. // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
  241. // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
  242. bool getMockRenderTargetInfo(GrMockRenderTargetInfo*) const;
  243. // Returns true if the backend texture has been initialized.
  244. bool isValid() const { return fIsValid; }
  245. #if GR_TEST_UTILS
  246. // We can remove the pixelConfig getter and setter once we remove the pixel config from the
  247. // GrBackendRenderTarget and plumb the pixel config manually throughout our code (or remove all
  248. // use of GrPixelConfig in general).
  249. GrPixelConfig pixelConfig() const { return fConfig; }
  250. void setPixelConfig(GrPixelConfig config) { fConfig = config; }
  251. static bool TestingOnly_Equals(const GrBackendRenderTarget&, const GrBackendRenderTarget&);
  252. #endif
  253. private:
  254. // Friending for access to the GrPixelConfig
  255. friend class SkSurface;
  256. friend class SkSurface_Gpu;
  257. friend class SkImage_Gpu;
  258. friend class GrGpu;
  259. friend class GrGLGpu;
  260. friend class GrProxyProvider;
  261. friend class GrVkGpu;
  262. friend class GrMtlGpu;
  263. GrPixelConfig config() const { return fConfig; }
  264. // Requires friending of GrVkGpu (done above already)
  265. sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
  266. friend class GrVkRenderTarget;
  267. GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo,
  268. sk_sp<GrVkImageLayout> layout);
  269. // Free and release and resources being held by the GrBackendTexture.
  270. void cleanup();
  271. bool fIsValid;
  272. int fWidth; //<! width in pixels
  273. int fHeight; //<! height in pixels
  274. int fSampleCnt;
  275. int fStencilBits;
  276. GrPixelConfig fConfig;
  277. GrBackendApi fBackend;
  278. union {
  279. GrGLFramebufferInfo fGLInfo;
  280. GrVkBackendSurfaceInfo fVkInfo;
  281. #ifdef SK_METAL
  282. GrMtlTextureInfo fMtlInfo;
  283. #endif
  284. GrMockRenderTargetInfo fMockInfo;
  285. };
  286. };
  287. #endif
  288. #endif