GrVkTypes.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright 2016 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 GrVkTypes_DEFINED
  8. #define GrVkTypes_DEFINED
  9. #include "include/core/SkTypes.h"
  10. #include "include/gpu/vk/GrVkVulkan.h"
  11. #ifndef VK_VERSION_1_1
  12. #error Skia requires the use of Vulkan 1.1 headers
  13. #endif
  14. #include <functional>
  15. #include "include/gpu/GrTypes.h"
  16. typedef intptr_t GrVkBackendMemory;
  17. /**
  18. * Types for interacting with Vulkan resources created externally to Skia. GrBackendObjects for
  19. * Vulkan textures are really const GrVkImageInfo*
  20. */
  21. struct GrVkAlloc {
  22. GrVkAlloc()
  23. : fMemory(VK_NULL_HANDLE)
  24. , fOffset(0)
  25. , fSize(0)
  26. , fFlags(0)
  27. , fBackendMemory(0)
  28. , fUsesSystemHeap(false) {}
  29. GrVkAlloc(VkDeviceMemory memory, VkDeviceSize offset, VkDeviceSize size, uint32_t flags)
  30. : fMemory(memory)
  31. , fOffset(offset)
  32. , fSize(size)
  33. , fFlags(flags)
  34. , fBackendMemory(0)
  35. , fUsesSystemHeap(false) {}
  36. VkDeviceMemory fMemory; // can be VK_NULL_HANDLE iff is an RT and is borrowed
  37. VkDeviceSize fOffset;
  38. VkDeviceSize fSize; // this can be indeterminate iff Tex uses borrow semantics
  39. uint32_t fFlags;
  40. GrVkBackendMemory fBackendMemory; // handle to memory allocated via GrVkMemoryAllocator.
  41. enum Flag {
  42. kNoncoherent_Flag = 0x1, // memory must be flushed to device after mapping
  43. kMappable_Flag = 0x2, // memory is able to be mapped.
  44. };
  45. bool operator==(const GrVkAlloc& that) const {
  46. return fMemory == that.fMemory && fOffset == that.fOffset && fSize == that.fSize &&
  47. fFlags == that.fFlags && fUsesSystemHeap == that.fUsesSystemHeap;
  48. }
  49. private:
  50. friend class GrVkHeap; // For access to usesSystemHeap
  51. bool fUsesSystemHeap;
  52. };
  53. // This struct is used to pass in the necessary information to create a VkSamplerYcbcrConversion
  54. // object for an VkExternalFormatANDROID.
  55. struct GrVkYcbcrConversionInfo {
  56. GrVkYcbcrConversionInfo()
  57. : fYcbcrModel(VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY)
  58. , fYcbcrRange(VK_SAMPLER_YCBCR_RANGE_ITU_FULL)
  59. , fXChromaOffset(VK_CHROMA_LOCATION_COSITED_EVEN)
  60. , fYChromaOffset(VK_CHROMA_LOCATION_COSITED_EVEN)
  61. , fChromaFilter(VK_FILTER_NEAREST)
  62. , fForceExplicitReconstruction(false)
  63. , fExternalFormat(0)
  64. , fExternalFormatFeatures(0) {}
  65. GrVkYcbcrConversionInfo(VkSamplerYcbcrModelConversion ycbcrModel,
  66. VkSamplerYcbcrRange ycbcrRange,
  67. VkChromaLocation xChromaOffset,
  68. VkChromaLocation yChromaOffset,
  69. VkFilter chromaFilter,
  70. VkBool32 forceExplicitReconstruction,
  71. uint64_t externalFormat,
  72. VkFormatFeatureFlags externalFormatFeatures)
  73. : fYcbcrModel(ycbcrModel)
  74. , fYcbcrRange(ycbcrRange)
  75. , fXChromaOffset(xChromaOffset)
  76. , fYChromaOffset(yChromaOffset)
  77. , fChromaFilter(chromaFilter)
  78. , fForceExplicitReconstruction(forceExplicitReconstruction)
  79. , fExternalFormat(externalFormat)
  80. , fExternalFormatFeatures(externalFormatFeatures) {
  81. SkASSERT(fExternalFormat);
  82. }
  83. bool operator==(const GrVkYcbcrConversionInfo& that) const {
  84. // Invalid objects are not required to have all other fields intialized or matching.
  85. if (!this->isValid() && !that.isValid()) {
  86. return true;
  87. }
  88. return this->fYcbcrModel == that.fYcbcrModel &&
  89. this->fYcbcrRange == that.fYcbcrRange &&
  90. this->fXChromaOffset == that.fXChromaOffset &&
  91. this->fYChromaOffset == that.fYChromaOffset &&
  92. this->fChromaFilter == that.fChromaFilter &&
  93. this->fForceExplicitReconstruction == that.fForceExplicitReconstruction &&
  94. this->fExternalFormat == that.fExternalFormat;
  95. // We don't check fExternalFormatFeatures here since all matching external formats must have
  96. // the same format features at least in terms of how they effect ycbcr sampler conversion.
  97. }
  98. bool operator!=(const GrVkYcbcrConversionInfo& that) const { return !(*this == that); }
  99. bool isValid() const { return fExternalFormat != 0; }
  100. VkSamplerYcbcrModelConversion fYcbcrModel;
  101. VkSamplerYcbcrRange fYcbcrRange;
  102. VkChromaLocation fXChromaOffset;
  103. VkChromaLocation fYChromaOffset;
  104. VkFilter fChromaFilter;
  105. VkBool32 fForceExplicitReconstruction;
  106. // The external format should be compatible to be used in a VkExternalFormatANDROID struct
  107. uint64_t fExternalFormat;
  108. // The format features here should be those returned by a call to
  109. // vkAndroidHardwareBufferFormatPropertiesANDROID
  110. VkFormatFeatureFlags fExternalFormatFeatures;
  111. };
  112. struct GrVkImageInfo {
  113. VkImage fImage;
  114. GrVkAlloc fAlloc;
  115. VkImageTiling fImageTiling;
  116. VkImageLayout fImageLayout;
  117. VkFormat fFormat;
  118. uint32_t fLevelCount;
  119. uint32_t fCurrentQueueFamily;
  120. GrProtected fProtected;
  121. GrVkYcbcrConversionInfo fYcbcrConversionInfo;
  122. GrVkImageInfo()
  123. : fImage(VK_NULL_HANDLE)
  124. , fAlloc()
  125. , fImageTiling(VK_IMAGE_TILING_OPTIMAL)
  126. , fImageLayout(VK_IMAGE_LAYOUT_UNDEFINED)
  127. , fFormat(VK_FORMAT_UNDEFINED)
  128. , fLevelCount(0)
  129. , fCurrentQueueFamily(VK_QUEUE_FAMILY_IGNORED)
  130. , fProtected(GrProtected::kNo)
  131. , fYcbcrConversionInfo() {}
  132. GrVkImageInfo(VkImage image,
  133. GrVkAlloc alloc,
  134. VkImageTiling imageTiling,
  135. VkImageLayout layout,
  136. VkFormat format,
  137. uint32_t levelCount,
  138. uint32_t currentQueueFamily,
  139. GrProtected isProtected,
  140. GrVkYcbcrConversionInfo ycbcrConversionInfo = GrVkYcbcrConversionInfo())
  141. : fImage(image)
  142. , fAlloc(alloc)
  143. , fImageTiling(imageTiling)
  144. , fImageLayout(layout)
  145. , fFormat(format)
  146. , fLevelCount(levelCount)
  147. , fCurrentQueueFamily(currentQueueFamily)
  148. , fProtected(isProtected)
  149. , fYcbcrConversionInfo(ycbcrConversionInfo) {}
  150. // Temporary until Chrome is updated to use above constructor
  151. GrVkImageInfo(VkImage image, GrVkAlloc alloc,
  152. VkImageTiling imageTiling,
  153. VkImageLayout layout,
  154. VkFormat format,
  155. uint32_t levelCount,
  156. uint32_t currentQueueFamily = VK_QUEUE_FAMILY_IGNORED,
  157. GrVkYcbcrConversionInfo ycbcrConversionInfo = GrVkYcbcrConversionInfo(),
  158. GrProtected isProtected = GrProtected::kNo)
  159. : GrVkImageInfo(image, alloc, imageTiling, layout, format, levelCount,
  160. currentQueueFamily, isProtected, ycbcrConversionInfo) {}
  161. GrVkImageInfo(const GrVkImageInfo& info, VkImageLayout layout)
  162. : fImage(info.fImage)
  163. , fAlloc(info.fAlloc)
  164. , fImageTiling(info.fImageTiling)
  165. , fImageLayout(layout)
  166. , fFormat(info.fFormat)
  167. , fLevelCount(info.fLevelCount)
  168. , fCurrentQueueFamily(info.fCurrentQueueFamily)
  169. , fProtected(info.fProtected)
  170. , fYcbcrConversionInfo(info.fYcbcrConversionInfo) {}
  171. // This gives a way for a client to update the layout of the Image if they change the layout
  172. // while we're still holding onto the wrapped texture. They will first need to get a handle
  173. // to our internal GrVkImageInfo by calling getTextureHandle on a GrVkTexture.
  174. void updateImageLayout(VkImageLayout layout) { fImageLayout = layout; }
  175. bool operator==(const GrVkImageInfo& that) const {
  176. return fImage == that.fImage && fAlloc == that.fAlloc &&
  177. fImageTiling == that.fImageTiling && fImageLayout == that.fImageLayout &&
  178. fFormat == that.fFormat && fLevelCount == that.fLevelCount &&
  179. fCurrentQueueFamily == that.fCurrentQueueFamily && fProtected == that.fProtected &&
  180. fYcbcrConversionInfo == that.fYcbcrConversionInfo;
  181. }
  182. };
  183. using GrVkGetProc = std::function<PFN_vkVoidFunction(
  184. const char*, // function name
  185. VkInstance, // instance or VK_NULL_HANDLE
  186. VkDevice // device or VK_NULL_HANDLE
  187. )>;
  188. /**
  189. * This object is wrapped in a GrBackendDrawableInfo and passed in as an argument to
  190. * drawBackendGpu() calls on an SkDrawable. The drawable will use this info to inject direct
  191. * Vulkan calls into our stream of GPU draws.
  192. *
  193. * The SkDrawable is given a secondary VkCommandBuffer in which to record draws. The GPU backend
  194. * will then execute that command buffer within a render pass it is using for its own draws. The
  195. * drawable is also given the attachment of the color index, a compatible VkRenderPass, and the
  196. * VkFormat of the color attachment so that it can make VkPipeline objects for the draws. The
  197. * SkDrawable must not alter the state of the VkRenderpass or sub pass.
  198. *
  199. * Additionally, the SkDrawable may fill in the passed in fDrawBounds with the bounds of the draws
  200. * that it submits to the command buffer. This will be used by the GPU backend for setting the
  201. * bounds in vkCmdBeginRenderPass. If fDrawBounds is not updated, we will assume that the entire
  202. * attachment may have been written to.
  203. *
  204. * The SkDrawable is always allowed to create its own command buffers and submit them to the queue
  205. * to render offscreen textures which will be sampled in draws added to the passed in
  206. * VkCommandBuffer. If this is done the SkDrawable is in charge of adding the required memory
  207. * barriers to the queue for the sampled images since the Skia backend will not do this.
  208. *
  209. * The VkImage is informational only and should not be used or modified in any ways.
  210. */
  211. struct GrVkDrawableInfo {
  212. VkCommandBuffer fSecondaryCommandBuffer;
  213. uint32_t fColorAttachmentIndex;
  214. VkRenderPass fCompatibleRenderPass;
  215. VkFormat fFormat;
  216. VkRect2D* fDrawBounds;
  217. VkImage fImage;
  218. };
  219. #endif