1 /* 2 * Copyright 2022 Google LLC. 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 8 #ifndef skgpu_VulkanTypes_DEFINED 9 #define skgpu_VulkanTypes_DEFINED 10 11 #include "include/core/SkTypes.h" 12 #include "include/private/gpu/vk/SkiaVulkan.h" 13 14 #include <cstddef> 15 #include <cstdint> 16 #include <functional> 17 #include <string> 18 #include <vector> 19 20 #ifndef VK_VERSION_1_1 21 #error Skia requires the use of Vulkan 1.1 headers 22 #endif 23 24 namespace skgpu { 25 26 using VulkanGetProc = std::function<PFN_vkVoidFunction( 27 const char*, // function name 28 VkInstance, // instance or VK_NULL_HANDLE 29 VkDevice // device or VK_NULL_HANDLE 30 )>; 31 32 typedef intptr_t VulkanBackendMemory; 33 class VulkanMemoryAllocator; 34 35 /** 36 * Types for interacting with Vulkan resources created externally to Skia. 37 */ 38 struct VulkanAlloc { 39 VulkanMemoryAllocator* fAllocator = nullptr; 40 #ifdef SKIA_DFX_FOR_OHOS 41 VkDeviceSize fBytes = 0; 42 #endif 43 // can be VK_NULL_HANDLE iff is an RT and is borrowed 44 VkDeviceMemory fMemory = VK_NULL_HANDLE; 45 VkDeviceSize fOffset = 0; 46 VkDeviceSize fSize = 0; // this can be indeterminate iff Tex uses borrow semantics 47 uint32_t fFlags = 0; 48 // handle to memory allocated via skgpu::VulkanMemoryAllocator. 49 VulkanBackendMemory fBackendMemory = 0; 50 bool fIsExternalMemory = false; // whether imported from external memory. 51 52 enum Flag { 53 kNoncoherent_Flag = 0x1, // memory must be flushed to device after mapping 54 kMappable_Flag = 0x2, // memory is able to be mapped. 55 kLazilyAllocated_Flag = 0x4, // memory was created with lazy allocation 56 }; 57 58 bool operator==(const VulkanAlloc& that) const { 59 return fMemory == that.fMemory && fOffset == that.fOffset && fSize == that.fSize && 60 fAllocator == that.fAllocator && 61 fFlags == that.fFlags && fUsesSystemHeap == that.fUsesSystemHeap; 62 } 63 64 private: 65 bool fUsesSystemHeap = false; 66 }; 67 68 // Used to pass in the necessary information to create a VkSamplerYcbcrConversion object for an 69 // VkExternalFormatANDROID. 70 struct VulkanYcbcrConversionInfo { 71 bool operator==(const VulkanYcbcrConversionInfo& that) const { 72 // Invalid objects are not required to have all other fields initialized or matching. 73 if (!this->isValid() && !that.isValid()) { 74 return true; 75 } 76 77 // Note that we do not need to check for fFormatFeatures equality. This is because the 78 // Vulkan spec dictates that Android hardware buffers with the same external format must 79 // have the same support for key features. See 80 // https://docs.vulkan.org/spec/latest/chapters/memory.html#_android_hardware_buffer_external_memory 81 // for more details. 82 return this->fFormat == that.fFormat && 83 this->fExternalFormat == that.fExternalFormat && 84 this->fYcbcrModel == that.fYcbcrModel && 85 this->fYcbcrRange == that.fYcbcrRange && 86 this->fXChromaOffset == that.fXChromaOffset && 87 this->fYChromaOffset == that.fYChromaOffset && 88 this->fChromaFilter == that.fChromaFilter && 89 this->fForceExplicitReconstruction == that.fForceExplicitReconstruction && 90 this->fComponents.r == that.fComponents.r && 91 this->fComponents.g == that.fComponents.g && 92 this->fComponents.b == that.fComponents.b && 93 this->fComponents.a == that.fComponents.a; 94 } 95 bool operator!=(const VulkanYcbcrConversionInfo& that) const { return !(*this == that); } 96 isValidVulkanYcbcrConversionInfo97 bool isValid() const { 98 #ifdef TODO_M133_SKIA 99 return fYcbcrModel != VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY; 100 #else 101 return fYcbcrModel != VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY || 102 fExternalFormat != 0; 103 #endif 104 } 105 106 // Format of the source image. Must be set to VK_FORMAT_UNDEFINED for external images or 107 // a valid image format otherwise. 108 VkFormat fFormat = VK_FORMAT_UNDEFINED; 109 110 // The external format. Must be non-zero for external images, zero otherwise. 111 // Should be compatible to be used in a VkExternalFormatANDROID struct. 112 uint64_t fExternalFormat = 0; 113 114 VkSamplerYcbcrModelConversion fYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY; 115 VkSamplerYcbcrRange fYcbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL; 116 VkChromaLocation fXChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN; 117 VkChromaLocation fYChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN; 118 VkFilter fChromaFilter = VK_FILTER_NEAREST; 119 VkBool32 fForceExplicitReconstruction = false; 120 121 // For external images format features here should be those returned by a call to 122 // vkAndroidHardwareBufferFormatPropertiesANDROID 123 VkFormatFeatureFlags fFormatFeatures = 0; 124 125 // This is ignored when fExternalFormat is non-zero. 126 VkComponentMapping fComponents = {VK_COMPONENT_SWIZZLE_IDENTITY, 127 VK_COMPONENT_SWIZZLE_IDENTITY, 128 VK_COMPONENT_SWIZZLE_IDENTITY, 129 VK_COMPONENT_SWIZZLE_IDENTITY}; 130 }; 131 132 typedef void* VulkanDeviceLostContext; 133 typedef void (*VulkanDeviceLostProc)(VulkanDeviceLostContext faultContext, 134 const std::string& description, 135 const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos, 136 const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos, 137 const std::vector<std::byte>& vendorBinaryData); 138 139 } // namespace skgpu 140 141 #endif // skgpu_VulkanTypes_DEFINED 142