GrBackendSurface.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 "include/gpu/GrTypes.h"
  10. #include "include/gpu/gl/GrGLTypes.h"
  11. #include "include/gpu/mock/GrMockTypes.h"
  12. #include "include/gpu/vk/GrVkTypes.h"
  13. #include "include/private/GrGLTypesPriv.h"
  14. #include "include/private/GrVkTypesPriv.h"
  15. #ifdef SK_DAWN
  16. #include "include/gpu/dawn/GrDawnTypes.h"
  17. #endif
  18. class GrVkImageLayout;
  19. class GrGLTextureParameters;
  20. #ifdef SK_DAWN
  21. #include "dawn/dawncpp.h"
  22. #endif
  23. #ifdef SK_METAL
  24. #include "include/gpu/mtl/GrMtlTypes.h"
  25. #endif
  26. #if !SK_SUPPORT_GPU
  27. // SkSurface and SkImage rely on a minimal version of these always being available
  28. class SK_API GrBackendTexture {
  29. public:
  30. GrBackendTexture() {}
  31. bool isValid() const { return false; }
  32. };
  33. class SK_API GrBackendRenderTarget {
  34. public:
  35. GrBackendRenderTarget() {}
  36. bool isValid() const { return false; }
  37. };
  38. #else
  39. class SK_API GrBackendFormat {
  40. public:
  41. // Creates an invalid backend format.
  42. GrBackendFormat() {}
  43. GrBackendFormat(const GrBackendFormat& src);
  44. static GrBackendFormat MakeGL(GrGLenum format, GrGLenum target) {
  45. return GrBackendFormat(format, target);
  46. }
  47. static GrBackendFormat MakeVk(VkFormat format) {
  48. return GrBackendFormat(format, GrVkYcbcrConversionInfo());
  49. }
  50. // This is used for external textures and the VkFormat is assumed to be VK_FORMAT_UNDEFINED.
  51. // This call is only supported on Android since the GrVkYcbcrConvesionInfo contains an android
  52. // external format.
  53. static GrBackendFormat MakeVk(const GrVkYcbcrConversionInfo& ycbcrInfo);
  54. #ifdef SK_DAWN
  55. static GrBackendFormat MakeDawn(dawn::TextureFormat format) {
  56. return GrBackendFormat(format);
  57. }
  58. #endif
  59. #ifdef SK_METAL
  60. static GrBackendFormat MakeMtl(GrMTLPixelFormat format) {
  61. return GrBackendFormat(format);
  62. }
  63. #endif
  64. static GrBackendFormat MakeMock(GrColorType colorType) {
  65. return GrBackendFormat(colorType);
  66. }
  67. bool operator==(const GrBackendFormat& that) const;
  68. bool operator!=(const GrBackendFormat& that) const { return !(*this == that); }
  69. GrBackendApi backend() const { return fBackend; }
  70. GrTextureType textureType() const { return fTextureType; }
  71. // If the backend API is GL, these return a pointer to the format and target. Otherwise
  72. // they return nullptr.
  73. const GrGLenum* getGLFormat() const;
  74. const GrGLenum* getGLTarget() const;
  75. // If the backend API is Vulkan, this returns a pointer to a VkFormat. Otherwise
  76. // it returns nullptr
  77. const VkFormat* getVkFormat() const;
  78. const GrVkYcbcrConversionInfo* getVkYcbcrConversionInfo() const;
  79. #ifdef SK_DAWN
  80. const dawn::TextureFormat* getDawnFormat() const;
  81. #endif
  82. #ifdef SK_METAL
  83. // If the backend API is Metal, this returns a pointer to a GrMTLPixelFormat. Otherwise
  84. // it returns nullptr
  85. const GrMTLPixelFormat* getMtlFormat() const;
  86. #endif
  87. // If the backend API is Mock, this returns a pointer to the colorType.
  88. // Otherwise it returns nullptr.
  89. const GrColorType* getMockColorType() const;
  90. // If possible, copies the GrBackendFormat and forces the texture type to be Texture2D. If the
  91. // GrBackendFormat was for Vulkan and it originally had a GrVkYcbcrConversionInfo, we will
  92. // remove the conversion and set the format to be VK_FORMAT_R8G8B8A8_UNORM.
  93. GrBackendFormat makeTexture2D() const;
  94. // Returns true if the backend format has been initialized.
  95. bool isValid() const { return fValid; }
  96. private:
  97. GrBackendFormat(GrGLenum format, GrGLenum target);
  98. GrBackendFormat(const VkFormat vkFormat, const GrVkYcbcrConversionInfo&);
  99. #ifdef SK_DAWN
  100. GrBackendFormat(dawn::TextureFormat format);
  101. #endif
  102. #ifdef SK_METAL
  103. GrBackendFormat(const GrMTLPixelFormat mtlFormat);
  104. #endif
  105. GrBackendFormat(GrColorType colorType);
  106. GrBackendApi fBackend = GrBackendApi::kMock;
  107. bool fValid = false;
  108. union {
  109. GrGLenum fGLFormat; // the sized, internal format of the GL resource
  110. struct {
  111. VkFormat fFormat;
  112. GrVkYcbcrConversionInfo fYcbcrConversionInfo;
  113. } fVk;
  114. #ifdef SK_DAWN
  115. dawn::TextureFormat fDawnFormat;
  116. #endif
  117. #ifdef SK_METAL
  118. GrMTLPixelFormat fMtlFormat;
  119. #endif
  120. GrColorType fMockColorType;
  121. };
  122. GrTextureType fTextureType = GrTextureType::kNone;
  123. };
  124. class SK_API GrBackendTexture {
  125. public:
  126. // Creates an invalid backend texture.
  127. GrBackendTexture() : fIsValid(false) {}
  128. // The GrGLTextureInfo must have a valid fFormat.
  129. GrBackendTexture(int width,
  130. int height,
  131. GrMipMapped,
  132. const GrGLTextureInfo& glInfo);
  133. GrBackendTexture(int width,
  134. int height,
  135. const GrVkImageInfo& vkInfo);
  136. #ifdef SK_METAL
  137. GrBackendTexture(int width,
  138. int height,
  139. GrMipMapped,
  140. const GrMtlTextureInfo& mtlInfo);
  141. #endif
  142. #ifdef SK_DAWN
  143. GrBackendTexture(int width,
  144. int height,
  145. const GrDawnImageInfo& dawnInfo);
  146. #endif
  147. GrBackendTexture(int width,
  148. int height,
  149. GrMipMapped,
  150. const GrMockTextureInfo& mockInfo);
  151. GrBackendTexture(const GrBackendTexture& that);
  152. ~GrBackendTexture();
  153. GrBackendTexture& operator=(const GrBackendTexture& that);
  154. int width() const { return fWidth; }
  155. int height() const { return fHeight; }
  156. bool hasMipMaps() const { return GrMipMapped::kYes == fMipMapped; }
  157. GrBackendApi backend() const {return fBackend; }
  158. // If the backend API is GL, copies a snapshot of the GrGLTextureInfo struct into the passed in
  159. // pointer and returns true. Otherwise returns false if the backend API is not GL.
  160. bool getGLTextureInfo(GrGLTextureInfo*) const;
  161. // Call this to indicate that the texture parameters have been modified in the GL context
  162. // externally to GrContext.
  163. void glTextureParametersModified();
  164. #ifdef SK_DAWN
  165. // If the backend API is Dawn, copies a snapshot of the GrDawnImageInfo struct into the passed
  166. // in pointer and returns true. Otherwise returns false if the backend API is not Dawn.
  167. bool getDawnImageInfo(GrDawnImageInfo*) const;
  168. #endif
  169. // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
  170. // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
  171. // state. Otherwise returns false if the backend API is not Vulkan.
  172. bool getVkImageInfo(GrVkImageInfo*) const;
  173. // Anytime the client changes the VkImageLayout of the VkImage captured by this
  174. // GrBackendTexture, they must call this function to notify Skia of the changed layout.
  175. void setVkImageLayout(VkImageLayout);
  176. #ifdef SK_METAL
  177. // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
  178. // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
  179. bool getMtlTextureInfo(GrMtlTextureInfo*) const;
  180. #endif
  181. // Get the GrBackendFormat for this texture (or an invalid format if this is not valid).
  182. GrBackendFormat getBackendFormat() const;
  183. // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
  184. // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
  185. bool getMockTextureInfo(GrMockTextureInfo*) const;
  186. // Returns true if we are working with protected content.
  187. bool isProtected() const;
  188. // Returns true if the backend texture has been initialized.
  189. bool isValid() const { return fIsValid; }
  190. // Returns true if both textures are valid and refer to the same API texture.
  191. bool isSameTexture(const GrBackendTexture&);
  192. #if GR_TEST_UTILS
  193. // We can remove the pixelConfig setter once we remove the GrPixelConfig from the
  194. // GrBackendTexture and plumb the GrPixelConfig manually throughout our code (or remove all use
  195. // of GrPixelConfig in general).
  196. void setPixelConfig(GrPixelConfig config) { fConfig = config; }
  197. static bool TestingOnly_Equals(const GrBackendTexture& , const GrBackendTexture&);
  198. #endif
  199. private:
  200. // Friending for access to the GrPixelConfig
  201. friend class SkImage;
  202. friend class SkImage_Gpu;
  203. friend class SkImage_GpuBase;
  204. friend class SkImage_GpuYUVA;
  205. friend class SkPromiseImageHelper;
  206. friend class SkSurface;
  207. friend class SkSurface_Gpu;
  208. friend class GrAHardwareBufferImageGenerator;
  209. friend class GrBackendTextureImageGenerator;
  210. friend class GrProxyProvider;
  211. friend class GrGpu;
  212. friend class GrGLGpu;
  213. friend class GrDawnGpu;
  214. friend class GrVkGpu;
  215. friend class GrMtlGpu;
  216. friend class PromiseImageHelper;
  217. GrPixelConfig config() const { return fConfig; }
  218. #ifdef SK_GL
  219. friend class GrGLTexture;
  220. GrBackendTexture(int width,
  221. int height,
  222. GrMipMapped,
  223. const GrGLTextureInfo,
  224. sk_sp<GrGLTextureParameters>);
  225. sk_sp<GrGLTextureParameters> getGLTextureParams() const;
  226. #endif
  227. #ifdef SK_VULKAN
  228. friend class GrVkTexture;
  229. GrBackendTexture(int width,
  230. int height,
  231. const GrVkImageInfo& vkInfo,
  232. sk_sp<GrVkImageLayout> layout);
  233. sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
  234. #endif
  235. // Free and release and resources being held by the GrBackendTexture.
  236. void cleanup();
  237. bool fIsValid;
  238. int fWidth; //<! width in pixels
  239. int fHeight; //<! height in pixels
  240. GrPixelConfig fConfig;
  241. GrMipMapped fMipMapped;
  242. GrBackendApi fBackend;
  243. union {
  244. #ifdef SK_GL
  245. GrGLBackendTextureInfo fGLInfo;
  246. #endif
  247. GrVkBackendSurfaceInfo fVkInfo;
  248. GrMockTextureInfo fMockInfo;
  249. };
  250. #ifdef SK_METAL
  251. GrMtlTextureInfo fMtlInfo;
  252. #endif
  253. #ifdef SK_DAWN
  254. GrDawnImageInfo fDawnInfo;
  255. #endif
  256. };
  257. class SK_API GrBackendRenderTarget {
  258. public:
  259. // Creates an invalid backend texture.
  260. GrBackendRenderTarget() : fIsValid(false) {}
  261. // The GrGLTextureInfo must have a valid fFormat.
  262. GrBackendRenderTarget(int width,
  263. int height,
  264. int sampleCnt,
  265. int stencilBits,
  266. const GrGLFramebufferInfo& glInfo);
  267. #ifdef SK_DAWN
  268. GrBackendRenderTarget(int width,
  269. int height,
  270. int sampleCnt,
  271. int stencilBits,
  272. const GrDawnImageInfo& dawnInfo);
  273. #endif
  274. /** Deprecated, use version that does not take stencil bits. */
  275. GrBackendRenderTarget(int width,
  276. int height,
  277. int sampleCnt,
  278. int stencilBits,
  279. const GrVkImageInfo& vkInfo);
  280. GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo);
  281. #ifdef SK_METAL
  282. GrBackendRenderTarget(int width,
  283. int height,
  284. int sampleCnt,
  285. const GrMtlTextureInfo& mtlInfo);
  286. #endif
  287. GrBackendRenderTarget(int width,
  288. int height,
  289. int sampleCnt,
  290. int stencilBits,
  291. const GrMockRenderTargetInfo& mockInfo);
  292. ~GrBackendRenderTarget();
  293. GrBackendRenderTarget(const GrBackendRenderTarget& that);
  294. GrBackendRenderTarget& operator=(const GrBackendRenderTarget&);
  295. int width() const { return fWidth; }
  296. int height() const { return fHeight; }
  297. int sampleCnt() const { return fSampleCnt; }
  298. int stencilBits() const { return fStencilBits; }
  299. GrBackendApi backend() const {return fBackend; }
  300. // If the backend API is GL, copies a snapshot of the GrGLFramebufferInfo struct into the passed
  301. // in pointer and returns true. Otherwise returns false if the backend API is not GL.
  302. bool getGLFramebufferInfo(GrGLFramebufferInfo*) const;
  303. #ifdef SK_DAWN
  304. // If the backend API is Dawn, copies a snapshot of the GrDawnImageInfo struct into the passed
  305. // in pointer and returns true. Otherwise returns false if the backend API is not Dawn.
  306. bool getDawnImageInfo(GrDawnImageInfo*) const;
  307. #endif
  308. // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
  309. // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
  310. // state. Otherwise returns false if the backend API is not Vulkan.
  311. bool getVkImageInfo(GrVkImageInfo*) const;
  312. // Anytime the client changes the VkImageLayout of the VkImage captured by this
  313. // GrBackendRenderTarget, they must call this function to notify Skia of the changed layout.
  314. void setVkImageLayout(VkImageLayout);
  315. #ifdef SK_METAL
  316. // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
  317. // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
  318. bool getMtlTextureInfo(GrMtlTextureInfo*) const;
  319. #endif
  320. // Get the GrBackendFormat for this render target (or an invalid format if this is not valid).
  321. GrBackendFormat getBackendFormat() const;
  322. // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
  323. // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
  324. bool getMockRenderTargetInfo(GrMockRenderTargetInfo*) const;
  325. // Returns true if we are working with protected content.
  326. bool isProtected() const;
  327. // Returns true if the backend texture has been initialized.
  328. bool isValid() const { return fIsValid; }
  329. #if GR_TEST_UTILS
  330. // We can remove the pixelConfig setter once we remove the pixel config from the
  331. // GrBackendRenderTarget and plumb the pixel config manually throughout our code (or remove all
  332. // use of GrPixelConfig in general).
  333. void setPixelConfig(GrPixelConfig config) { fConfig = config; }
  334. static bool TestingOnly_Equals(const GrBackendRenderTarget&, const GrBackendRenderTarget&);
  335. #endif
  336. private:
  337. // Friending for access to the GrPixelConfig
  338. friend class SkSurface;
  339. friend class SkSurface_Gpu;
  340. friend class SkImage_Gpu;
  341. friend class GrGpu;
  342. friend class GrGLGpu;
  343. friend class GrDawnGpu;
  344. friend class GrProxyProvider;
  345. friend class GrVkGpu;
  346. friend class GrMtlGpu;
  347. GrPixelConfig config() const { return fConfig; }
  348. // Requires friending of GrVkGpu (done above already)
  349. sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
  350. friend class GrVkRenderTarget;
  351. GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo,
  352. sk_sp<GrVkImageLayout> layout);
  353. // Free and release and resources being held by the GrBackendTexture.
  354. void cleanup();
  355. bool fIsValid;
  356. int fWidth; //<! width in pixels
  357. int fHeight; //<! height in pixels
  358. int fSampleCnt;
  359. int fStencilBits;
  360. GrPixelConfig fConfig;
  361. GrBackendApi fBackend;
  362. union {
  363. #ifdef SK_GL
  364. GrGLFramebufferInfo fGLInfo;
  365. #endif
  366. GrVkBackendSurfaceInfo fVkInfo;
  367. GrMockRenderTargetInfo fMockInfo;
  368. };
  369. #ifdef SK_METAL
  370. GrMtlTextureInfo fMtlInfo;
  371. #endif
  372. #ifdef SK_DAWN
  373. GrDawnImageInfo fDawnInfo;
  374. #endif
  375. };
  376. #endif
  377. #endif