|
| 1 | +#pragma once |
| 2 | +#include "owned_image_pool.h" |
| 3 | +#include "posix_fd.h" |
| 4 | +#include <array> |
| 5 | +#include <memory> |
| 6 | +#include <vector> |
| 7 | + |
| 8 | +namespace webscene::graphics { |
| 9 | +enum class linux_memory_handle : uint32_t { opaque_fd=1, dma_buf=2 }; |
| 10 | +enum class linux_sync_handle : uint32_t { sync_fd=1, vk_semaphore_opaque_fd=2 }; |
| 11 | +enum class linux_queue_sharing : uint32_t { exclusive=1, concurrent=2 }; |
| 12 | + |
| 13 | +struct linux_external_plane { |
| 14 | + int borrowed_fd{-1}; |
| 15 | + uint64_t offset{}; |
| 16 | + uint32_t stride{}; |
| 17 | +}; |
| 18 | +struct linux_external_wait { |
| 19 | + linux_sync_handle handle{}; |
| 20 | + int borrowed_fd{-1}; |
| 21 | + uint64_t ordering_domain{}; |
| 22 | + uint64_t signaled_value{}; |
| 23 | + bool timeline{}; |
| 24 | +}; |
| 25 | +struct linux_external_image_snapshot { |
| 26 | + image_metadata metadata{}; |
| 27 | + linux_memory_handle memory_handle{}; |
| 28 | + linux_queue_sharing queue_sharing{}; |
| 29 | + uint64_t allocation_size{}; |
| 30 | + uint64_t drm_modifier{}; |
| 31 | + uint32_t drm_format{}; |
| 32 | + uint32_t memory_type_index{UINT32_MAX}; |
| 33 | + uint32_t vk_format{}; |
| 34 | + uint32_t vk_image_type{}; |
| 35 | + uint32_t vk_tiling{}; |
| 36 | + uint32_t vk_usage{}; |
| 37 | + uint32_t vk_create_flags{}; |
| 38 | + uint32_t vk_sharing_mode{}; |
| 39 | + int32_t vk_initial_layout{}; |
| 40 | + uint32_t vk_queue_family_index_count{}; |
| 41 | + uint32_t sample_count{}; |
| 42 | + uint32_t mip_level_count{}; |
| 43 | + uint32_t array_layer_count{}; |
| 44 | + int32_t producer_layout{}; |
| 45 | + int32_t consumer_layout{}; |
| 46 | + uint32_t producer_queue_family{UINT32_MAX}; |
| 47 | + uint32_t consumer_queue_family{UINT32_MAX}; |
| 48 | + std::array<uint32_t,4> vk_queue_family_indices{}; |
| 49 | + std::array<uint8_t,16> device_uuid{}; |
| 50 | + std::array<uint8_t,16> driver_uuid{}; |
| 51 | + bool dedicated_allocation{}; |
| 52 | + bool producer_complete{}; |
| 53 | + std::vector<linux_external_plane> planes; |
| 54 | + std::vector<linux_external_wait> waits; |
| 55 | +}; |
| 56 | + |
| 57 | +// Implemented only by a storage allocator that owns Linux external memory. |
| 58 | +// export_image borrows its FDs for the duration of the call. The ABI adapter |
| 59 | +// duplicates every accepted FD before returning to the host. |
| 60 | +struct linux_external_image_provider : image_provider_lifetime { |
| 61 | + image_provider_kind kind()const noexcept final{return image_provider_kind::linux_external;} |
| 62 | + virtual bool export_image(const image_metadata&,linux_external_image_snapshot&)const=0; |
| 63 | +}; |
| 64 | + |
| 65 | +inline bool same_image_metadata(const image_metadata& a,const image_metadata& b)noexcept{ |
| 66 | + return a.canvas==b.canvas&&a.allocation==b.allocation |
| 67 | + &&a.allocation_generation==b.allocation_generation&&a.content_serial==b.content_serial |
| 68 | + &&a.producer_timeline==b.producer_timeline&&a.producer_value==b.producer_value |
| 69 | + &&a.width==b.width&&a.height==b.height&&a.format==b.format&&a.alpha==b.alpha |
| 70 | + &&a.color_space==b.color_space&&a.orientation==b.orientation; |
| 71 | +} |
| 72 | +inline bool nonzero_uuid(const std::array<uint8_t,16>& value)noexcept{ |
| 73 | + for(const auto byte:value)if(byte)return true; |
| 74 | + return false; |
| 75 | +} |
| 76 | +inline bool valid_linux_external_snapshot(const linux_external_image_snapshot& value, |
| 77 | + const image_metadata& expected)noexcept{ |
| 78 | + if(!same_image_metadata(value.metadata,expected)||!nonzero_uuid(value.device_uuid) |
| 79 | + ||value.planes.empty()||value.planes.size()>4||value.waits.size()>UINT32_MAX |
| 80 | + ||value.sample_count!=1||value.mip_level_count!=1||value.array_layer_count!=1 |
| 81 | + ||!value.vk_format||value.vk_image_type!=1||!value.vk_usage |
| 82 | + ||(value.vk_initial_layout!=0&&value.vk_initial_layout!=8) |
| 83 | + ||value.producer_layout<=0||value.consumer_layout<=0)return false; |
| 84 | + if(value.queue_sharing==linux_queue_sharing::exclusive){ |
| 85 | + if(value.vk_sharing_mode!=0||value.vk_queue_family_index_count |
| 86 | + ||value.producer_queue_family==UINT32_MAX||value.consumer_queue_family==UINT32_MAX)return false; |
| 87 | + }else if(value.queue_sharing==linux_queue_sharing::concurrent){ |
| 88 | + if(value.vk_sharing_mode!=1||value.vk_queue_family_index_count<2 |
| 89 | + ||value.vk_queue_family_index_count>value.vk_queue_family_indices.size() |
| 90 | + ||value.producer_queue_family!=UINT32_MAX||value.consumer_queue_family!=UINT32_MAX)return false; |
| 91 | + for(uint32_t i=0;i<value.vk_queue_family_index_count;++i){ |
| 92 | + if(value.vk_queue_family_indices[i]==UINT32_MAX)return false; |
| 93 | + for(uint32_t j=0;j<i;++j) |
| 94 | + if(value.vk_queue_family_indices[i]==value.vk_queue_family_indices[j])return false; |
| 95 | + } |
| 96 | + }else return false; |
| 97 | + if(!value.allocation_size)return false; |
| 98 | + if(value.memory_handle==linux_memory_handle::opaque_fd){ |
| 99 | + if(value.planes.size()!=1||value.memory_type_index>=32 |
| 100 | + ||value.drm_format||value.drm_modifier)return false; |
| 101 | + }else if(value.memory_handle==linux_memory_handle::dma_buf){ |
| 102 | + if(!value.drm_format||value.memory_type_index!=UINT32_MAX)return false; |
| 103 | + }else return false; |
| 104 | + for(const auto& plane:value.planes){ |
| 105 | + if(plane.borrowed_fd<0)return false; |
| 106 | + if(value.memory_handle==linux_memory_handle::dma_buf&&!plane.stride)return false; |
| 107 | + } |
| 108 | + if(value.producer_complete!=value.waits.empty())return false; |
| 109 | + for(size_t index=0;index<value.waits.size();++index){ |
| 110 | + const auto& wait=value.waits[index]; |
| 111 | + if(wait.borrowed_fd<0||!wait.ordering_domain)return false; |
| 112 | + if(wait.handle==linux_sync_handle::sync_fd){ |
| 113 | + if(wait.timeline||wait.signaled_value!=1)return false; |
| 114 | + }else if(wait.handle==linux_sync_handle::vk_semaphore_opaque_fd){ |
| 115 | + if(wait.timeline?!wait.signaled_value:wait.signaled_value!=1)return false; |
| 116 | + }else return false; |
| 117 | + for(size_t prior=0;prior<index;++prior) |
| 118 | + if(value.waits[prior].ordering_domain==wait.ordering_domain)return false; |
| 119 | + } |
| 120 | + return true; |
| 121 | +} |
| 122 | + |
| 123 | +template<class FdOps> class basic_linux_shared_image_owner { |
| 124 | +public: |
| 125 | + using owned_fd=unique_posix_fd<FdOps>; |
| 126 | + struct plane { owned_fd fd; uint64_t offset{}; uint32_t stride{}; }; |
| 127 | + struct wait { linux_sync_handle handle{}; owned_fd fd; uint64_t ordering_domain{}; uint64_t value{}; bool timeline{}; }; |
| 128 | +private: |
| 129 | + linux_external_image_snapshot snapshot_; |
| 130 | + std::vector<plane> planes_; |
| 131 | + std::vector<wait> waits_; |
| 132 | +public: |
| 133 | + const linux_external_image_snapshot& snapshot()const noexcept{return snapshot_;} |
| 134 | + const std::vector<plane>& planes()const noexcept{return planes_;} |
| 135 | + const std::vector<wait>& waits()const noexcept{return waits_;} |
| 136 | + static std::unique_ptr<basic_linux_shared_image_owner> create( |
| 137 | + const owned_image_pool::consumer& consumer){ |
| 138 | + const auto metadata=consumer.describe(); |
| 139 | + const auto anchor=consumer.provider(); |
| 140 | + if(!anchor||anchor->kind()!=image_provider_kind::linux_external)return {}; |
| 141 | + const auto provider=std::static_pointer_cast<linux_external_image_provider>(anchor); |
| 142 | + linux_external_image_snapshot snapshot; |
| 143 | + if(!provider->export_image(metadata,snapshot)||!valid_linux_external_snapshot(snapshot,metadata)) |
| 144 | + throw std::invalid_argument("invalid Linux external image export"); |
| 145 | + auto result=std::make_unique<basic_linux_shared_image_owner>(); |
| 146 | + result->planes_.reserve(snapshot.planes.size()); |
| 147 | + result->waits_.reserve(snapshot.waits.size()); |
| 148 | + for(const auto& source:snapshot.planes) |
| 149 | + result->planes_.push_back({owned_fd::duplicate(source.borrowed_fd),source.offset,source.stride}); |
| 150 | + for(const auto& source:snapshot.waits) |
| 151 | + result->waits_.push_back({source.handle,owned_fd::duplicate(source.borrowed_fd), |
| 152 | + source.ordering_domain,source.signaled_value,source.timeline}); |
| 153 | + // Never retain the provider's borrowed descriptor integers in the |
| 154 | + // immutable metadata copy exposed by owner inspection. |
| 155 | + snapshot.planes.clear();snapshot.waits.clear(); |
| 156 | + result->snapshot_=std::move(snapshot); |
| 157 | + return result; |
| 158 | + } |
| 159 | +}; |
| 160 | +#if defined(__linux__) |
| 161 | +using linux_shared_image_owner=basic_linux_shared_image_owner<linux_fd_ops>; |
| 162 | +#endif |
| 163 | +} // namespace webscene::graphics |
0 commit comments