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 34 /** 35 * Types for interacting with Vulkan resources created externally to Skia. 36 */ 37 struct VulkanAlloc { 38 // can be VK_NULL_HANDLE iff is an RT and is borrowed 39 VkDeviceMemory fMemory = VK_NULL_HANDLE; 40 VkDeviceSize fOffset = 0; 41 VkDeviceSize fSize = 0; // this can be indeterminate iff Tex uses borrow semantics 42 uint32_t fFlags = 0; 43 // handle to memory allocated via skgpu::VulkanMemoryAllocator. 44 VulkanBackendMemory fBackendMemory = 0; 45 46 enum Flag { 47 kNoncoherent_Flag = 0x1, // memory must be flushed to device after mapping 48 kMappable_Flag = 0x2, // memory is able to be mapped. 49 kLazilyAllocated_Flag = 0x4, // memory was created with lazy allocation 50 }; 51 52 bool operator==(const VulkanAlloc& that) const { 53 return fMemory == that.fMemory && fOffset == that.fOffset && fSize == that.fSize && 54 fFlags == that.fFlags && fUsesSystemHeap == that.fUsesSystemHeap; 55 } 56 57 private: 58 bool fUsesSystemHeap = false; 59 }; 60 61 // Used to pass in the necessary information to create a VkSamplerYcbcrConversion object for an 62 // VkExternalFormatANDROID. 63 struct VulkanYcbcrConversionInfo { 64 bool operator==(const VulkanYcbcrConversionInfo& that) const { 65 // Invalid objects are not required to have all other fields initialized or matching. 66 if (!this->isValid() && !that.isValid()) { 67 return true; 68 } 69 70 // Note that we do not need to check for fFormatFeatures equality. This is because the 71 // Vulkan spec dictates that Android hardware buffers with the same external format must 72 // have the same support for key features. See 73 // https://docs.vulkan.org/spec/latest/chapters/memory.html#_android_hardware_buffer_external_memory 74 // for more details. 75 return this->fFormat == that.fFormat && 76 this->fExternalFormat == that.fExternalFormat && 77 this->fYcbcrModel == that.fYcbcrModel && 78 this->fYcbcrRange == that.fYcbcrRange && 79 this->fXChromaOffset == that.fXChromaOffset && 80 this->fYChromaOffset == that.fYChromaOffset && 81 this->fChromaFilter == that.fChromaFilter && 82 this->fForceExplicitReconstruction == that.fForceExplicitReconstruction && 83 this->fComponents.r == that.fComponents.r && 84 this->fComponents.g == that.fComponents.g && 85 this->fComponents.b == that.fComponents.b && 86 this->fComponents.a == that.fComponents.a; 87 } 88 bool operator!=(const VulkanYcbcrConversionInfo& that) const { return !(*this == that); } 89 isValidVulkanYcbcrConversionInfo90 bool isValid() const { 91 return fYcbcrModel != VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY || 92 fExternalFormat != 0; 93 } 94 95 // Format of the source image. Must be set to VK_FORMAT_UNDEFINED for external images or 96 // a valid image format otherwise. 97 VkFormat fFormat = VK_FORMAT_UNDEFINED; 98 99 // The external format. Must be non-zero for external images, zero otherwise. 100 // Should be compatible to be used in a VkExternalFormatANDROID struct. 101 uint64_t fExternalFormat = 0; 102 103 VkSamplerYcbcrModelConversion fYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY; 104 VkSamplerYcbcrRange fYcbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL; 105 VkChromaLocation fXChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN; 106 VkChromaLocation fYChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN; 107 VkFilter fChromaFilter = VK_FILTER_NEAREST; 108 VkBool32 fForceExplicitReconstruction = false; 109 110 // For external images format features here should be those returned by a call to 111 // vkAndroidHardwareBufferFormatPropertiesANDROID 112 VkFormatFeatureFlags fFormatFeatures = 0; 113 114 // This is ignored when fExternalFormat is non-zero. 115 VkComponentMapping fComponents = {VK_COMPONENT_SWIZZLE_IDENTITY, 116 VK_COMPONENT_SWIZZLE_IDENTITY, 117 VK_COMPONENT_SWIZZLE_IDENTITY, 118 VK_COMPONENT_SWIZZLE_IDENTITY}; 119 }; 120 121 typedef void* VulkanDeviceLostContext; 122 typedef void (*VulkanDeviceLostProc)(VulkanDeviceLostContext faultContext, 123 const std::string& description, 124 const std::vector<VkDeviceFaultAddressInfoEXT>& addressInfos, 125 const std::vector<VkDeviceFaultVendorInfoEXT>& vendorInfos, 126 const std::vector<std::byte>& vendorBinaryData); 127 128 } // namespace skgpu 129 130 #endif // skgpu_VulkanTypes_DEFINED 131