| 1 | use crate::Error; |
| 2 | use crate::device::Shared; |
| 3 | use crate::dmabuf::{self, DmabufCaps, RENDER_USAGE, SAMPLE_USAGE, VulkanAllocator}; |
| 4 | use crate::format::{BYTES_PER_PIXEL, SUPPORTED}; |
| 5 | use crate::frame::{Op, VulkanFrame}; |
| 6 | use crate::image::{ |
| 7 | ImageInner, Staging, VulkanImage, VulkanMapping, VulkanTarget, VulkanTexture, color_range, |
| 8 | }; |
| 9 | use crate::pipeline::Pipelines; |
| 10 | use crate::sync::{FenceInner, VkFence}; |
| 11 | use ash::vk; |
| 12 | use gpu_allocator::MemoryLocation; |
| 13 | use smithay::backend::allocator::Fourcc; |
| 14 | use smithay::backend::allocator::dmabuf::{Dmabuf, WeakDmabuf}; |
| 15 | use smithay::backend::allocator::format::FormatSet; |
| 16 | use smithay::backend::renderer::ImportMemWl; |
| 17 | use smithay::backend::renderer::sync::SyncPoint; |
| 18 | use smithay::backend::renderer::{ |
| 19 | Bind, ContextId, DebugFlags, ExportMem, ImportDma, ImportDmaWl, ImportMem, Offscreen, Renderer, |
| 20 | RendererSuper, Texture, TextureFilter, |
| 21 | }; |
| 22 | use smithay::reexports::wayland_server::protocol::wl_buffer::WlBuffer; |
| 23 | use smithay::utils::{Buffer, Physical, Rectangle, Size, Transform}; |
| 24 | use smithay::wayland::compositor::SurfaceData; |
| 25 | use smithay::wayland::shm::{shm_format_to_fourcc, with_buffer_contents}; |
| 26 | use std::collections::HashMap; |
| 27 | use std::fmt; |
| 28 | use std::marker::PhantomData; |
| 29 | use std::sync::{Arc, Mutex}; |
| 30 | |
| 31 | struct Pending { |
| 32 | fence: Arc<FenceInner>, |
| 33 | cmd: vk::CommandBuffer, |
| 34 | _keep: Vec<Arc<ImageInner>>, |
| 35 | _staging: Option<Staging>, |
| 36 | } |
| 37 | |
| 38 | pub struct VulkanRenderer { |
| 39 | pending: Vec<Pending>, |
| 40 | dmabuf_textures: HashMap<WeakDmabuf, VulkanTexture>, |
| 41 | dmabuf_targets: HashMap<WeakDmabuf, Arc<ImageInner>>, |
| 42 | pipelines: Pipelines, |
| 43 | pool: vk::CommandPool, |
| 44 | samplers: [vk::Sampler; 4], |
| 45 | min_filter: TextureFilter, |
| 46 | mag_filter: TextureFilter, |
| 47 | debug: DebugFlags, |
| 48 | context: ContextId<VulkanTexture>, |
| 49 | caps: Arc<DmabufCaps>, |
| 50 | shared: Arc<Shared>, |
| 51 | } |
| 52 | |
| 53 | impl fmt::Debug for VulkanRenderer { |
| 54 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 55 | f.debug_struct("VulkanRenderer") |
| 56 | .field("device", &self.shared.device.name) |
| 57 | .finish() |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | impl VulkanRenderer { |
| 62 | pub fn new() -> Result<Self, Error> { |
| 63 | Self::with_node(None) |
| 64 | } |
| 65 | |
| 66 | /// A renderer on the GPU behind DRM device number `node`, so it can |
| 67 | /// render into buffers that GPU scans out. |
| 68 | pub fn for_node(node: u64) -> Result<Self, Error> { |
| 69 | Self::with_node(Some(node)) |
| 70 | } |
| 71 | |
| 72 | fn with_node(node: Option<u64>) -> Result<Self, Error> { |
| 73 | let shared = Arc::new(Shared::new(node)?); |
| 74 | let d = &shared.device.device; |
| 75 | let pool = unsafe { |
| 76 | d.create_command_pool( |
| 77 | &vk::CommandPoolCreateInfo::default() |
| 78 | .queue_family_index(shared.device.queue_family) |
| 79 | .flags(vk::CommandPoolCreateFlags::TRANSIENT), |
| 80 | None, |
| 81 | )? |
| 82 | }; |
| 83 | let filter = |f: TextureFilter| match f { |
| 84 | TextureFilter::Linear => vk::Filter::LINEAR, |
| 85 | TextureFilter::Nearest => vk::Filter::NEAREST, |
| 86 | }; |
| 87 | let mut samplers = [vk::Sampler::null(); 4]; |
| 88 | for (i, s) in samplers.iter_mut().enumerate() { |
| 89 | let (min, mag) = (sampler_filter(i >> 1), sampler_filter(i & 1)); |
| 90 | *s = unsafe { |
| 91 | d.create_sampler( |
| 92 | &vk::SamplerCreateInfo::default() |
| 93 | .min_filter(filter(min)) |
| 94 | .mag_filter(filter(mag)) |
| 95 | .address_mode_u(vk::SamplerAddressMode::CLAMP_TO_EDGE) |
| 96 | .address_mode_v(vk::SamplerAddressMode::CLAMP_TO_EDGE) |
| 97 | .address_mode_w(vk::SamplerAddressMode::CLAMP_TO_EDGE), |
| 98 | None, |
| 99 | )? |
| 100 | }; |
| 101 | } |
| 102 | Ok(Self { |
| 103 | pending: Vec::new(), |
| 104 | dmabuf_textures: HashMap::new(), |
| 105 | dmabuf_targets: HashMap::new(), |
| 106 | pipelines: Pipelines::new(&shared)?, |
| 107 | pool, |
| 108 | samplers, |
| 109 | min_filter: TextureFilter::Linear, |
| 110 | mag_filter: TextureFilter::Linear, |
| 111 | debug: DebugFlags::empty(), |
| 112 | context: ContextId::new(), |
| 113 | caps: Arc::new(DmabufCaps::query(&shared)), |
| 114 | shared, |
| 115 | }) |
| 116 | } |
| 117 | |
| 118 | /// Name of the physical device in use. |
| 119 | pub fn device_name(&self) -> &str { |
| 120 | &self.shared.device.name |
| 121 | } |
| 122 | |
| 123 | /// Whether dmabufs can be imported, rendered into and allocated. |
| 124 | pub fn supports_dmabuf(&self) -> bool { |
| 125 | self.caps.render.iter().next().is_some() |
| 126 | } |
| 127 | |
| 128 | /// Formats and modifiers this renderer can render into as dmabufs. |
| 129 | pub fn render_formats(&self) -> FormatSet { |
| 130 | self.caps.render.clone() |
| 131 | } |
| 132 | |
| 133 | /// An allocator for render-capable dmabufs on this device. |
| 134 | pub fn allocator(&self) -> VulkanAllocator { |
| 135 | VulkanAllocator { |
| 136 | shared: self.shared.clone(), |
| 137 | caps: self.caps.clone(), |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | pub(crate) fn shared(&self) -> &Arc<Shared> { |
| 142 | &self.shared |
| 143 | } |
| 144 | |
| 145 | pub(crate) fn context_id_ref(&self) -> &ContextId<VulkanTexture> { |
| 146 | &self.context |
| 147 | } |
| 148 | |
| 149 | fn sampler(&self) -> vk::Sampler { |
| 150 | let idx = |f: TextureFilter| usize::from(f == TextureFilter::Nearest); |
| 151 | self.samplers[(idx(self.min_filter) << 1) | idx(self.mag_filter)] |
| 152 | } |
| 153 | |
| 154 | fn reap(&mut self) { |
| 155 | let d = &self.shared.device.device; |
| 156 | let pool = self.pool; |
| 157 | self.pending.retain(|p| { |
| 158 | if !p.fence.signaled() { |
| 159 | return true; |
| 160 | } |
| 161 | unsafe { d.free_command_buffers(pool, &[p.cmd]) }; |
| 162 | false |
| 163 | }); |
| 164 | self.dmabuf_textures.retain(|k, _| !k.is_gone()); |
| 165 | self.dmabuf_targets.retain(|k, _| !k.is_gone()); |
| 166 | } |
| 167 | |
| 168 | fn begin(&self) -> Result<vk::CommandBuffer, Error> { |
| 169 | let d = &self.shared.device.device; |
| 170 | let cmd = unsafe { |
| 171 | d.allocate_command_buffers( |
| 172 | &vk::CommandBufferAllocateInfo::default() |
| 173 | .command_pool(self.pool) |
| 174 | .level(vk::CommandBufferLevel::PRIMARY) |
| 175 | .command_buffer_count(1), |
| 176 | )?[0] |
| 177 | }; |
| 178 | unsafe { |
| 179 | d.begin_command_buffer( |
| 180 | cmd, |
| 181 | &vk::CommandBufferBeginInfo::default() |
| 182 | .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT), |
| 183 | )? |
| 184 | }; |
| 185 | Ok(cmd) |
| 186 | } |
| 187 | |
| 188 | fn end_and_submit(&self, cmd: vk::CommandBuffer) -> Result<Arc<FenceInner>, Error> { |
| 189 | let d = &self.shared.device.device; |
| 190 | unsafe { d.end_command_buffer(cmd)? }; |
| 191 | let fence = FenceInner::new(&self.shared)?; |
| 192 | let cmds = [vk::CommandBufferSubmitInfo::default().command_buffer(cmd)]; |
| 193 | let submit = vk::SubmitInfo2::default().command_buffer_infos(&cmds); |
| 194 | let queue = self |
| 195 | .shared |
| 196 | .device |
| 197 | .queue |
| 198 | .lock() |
| 199 | .unwrap_or_else(|e| e.into_inner()); |
| 200 | unsafe { d.queue_submit2(*queue, &[submit], fence.fence())? }; |
| 201 | Ok(fence) |
| 202 | } |
| 203 | |
| 204 | /// Records, submits and waits for a short command buffer. |
| 205 | fn one_shot( |
| 206 | &mut self, |
| 207 | record: impl FnOnce(&ash::Device, vk::CommandBuffer, u32), |
| 208 | ) -> Result<(), Error> { |
| 209 | let cmd = self.begin()?; |
| 210 | record( |
| 211 | &self.shared.device.device, |
| 212 | cmd, |
| 213 | self.shared.device.queue_family, |
| 214 | ); |
| 215 | let fence = self.end_and_submit(cmd)?; |
| 216 | let res = fence.wait(); |
| 217 | unsafe { |
| 218 | self.shared |
| 219 | .device |
| 220 | .device |
| 221 | .free_command_buffers(self.pool, &[cmd]) |
| 222 | }; |
| 223 | Ok(res?) |
| 224 | } |
| 225 | |
| 226 | pub(crate) fn submit( |
| 227 | &mut self, |
| 228 | target: &mut VulkanTarget<'_>, |
| 229 | ops: Vec<Op>, |
| 230 | ) -> Result<SyncPoint, Error> { |
| 231 | self.reap(); |
| 232 | let image = target.0.clone(); |
| 233 | let mut sampled: Vec<Arc<ImageInner>> = Vec::new(); |
| 234 | for op in &ops { |
| 235 | if let Op::Draw { |
| 236 | texture: Some(t), .. |
| 237 | } = op |
| 238 | && !sampled.iter().any(|s| Arc::ptr_eq(s, &t.0)) |
| 239 | { |
| 240 | sampled.push(t.0.clone()); |
| 241 | } |
| 242 | } |
| 243 | let mut pipelines = Vec::with_capacity(ops.len()); |
| 244 | for op in &ops { |
| 245 | pipelines.push(match op { |
| 246 | Op::Draw { kind, .. } => Some(self.pipelines.get(image.format, *kind)?), |
| 247 | Op::Clear { .. } => None, |
| 248 | }); |
| 249 | } |
| 250 | let cmd = self.begin()?; |
| 251 | let d = &self.shared.device.device; |
| 252 | let family = self.shared.device.queue_family; |
| 253 | let push = &self.shared.device.push_descriptor; |
| 254 | let (w, h) = (image.width, image.height); |
| 255 | let sampler = self.sampler(); |
| 256 | unsafe { |
| 257 | for t in &sampled { |
| 258 | acquire(d, cmd, family, t, vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL); |
| 259 | } |
| 260 | acquire( |
| 261 | d, |
| 262 | cmd, |
| 263 | family, |
| 264 | &image, |
| 265 | vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL, |
| 266 | ); |
| 267 | let attachments = [vk::RenderingAttachmentInfo::default() |
| 268 | .image_view(image.view) |
| 269 | .image_layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL) |
| 270 | .load_op(vk::AttachmentLoadOp::LOAD) |
| 271 | .store_op(vk::AttachmentStoreOp::STORE)]; |
| 272 | let area = vk::Rect2D { |
| 273 | offset: vk::Offset2D::default(), |
| 274 | extent: vk::Extent2D { |
| 275 | width: w, |
| 276 | height: h, |
| 277 | }, |
| 278 | }; |
| 279 | d.cmd_begin_rendering( |
| 280 | cmd, |
| 281 | &vk::RenderingInfo::default() |
| 282 | .render_area(area) |
| 283 | .layer_count(1) |
| 284 | .color_attachments(&attachments), |
| 285 | ); |
| 286 | d.cmd_set_viewport( |
| 287 | cmd, |
| 288 | 0, |
| 289 | &[vk::Viewport { |
| 290 | x: 0.0, |
| 291 | y: 0.0, |
| 292 | width: w as f32, |
| 293 | height: h as f32, |
| 294 | min_depth: 0.0, |
| 295 | max_depth: 1.0, |
| 296 | }], |
| 297 | ); |
| 298 | let mut bound = vk::Pipeline::null(); |
| 299 | for (op, pipeline) in ops.iter().zip(pipelines) { |
| 300 | match op { |
| 301 | Op::Clear { color, rects } => { |
| 302 | let clear = [vk::ClearAttachment { |
| 303 | aspect_mask: vk::ImageAspectFlags::COLOR, |
| 304 | color_attachment: 0, |
| 305 | clear_value: vk::ClearValue { |
| 306 | color: vk::ClearColorValue { float32: *color }, |
| 307 | }, |
| 308 | }]; |
| 309 | let rects: Vec<_> = rects |
| 310 | .iter() |
| 311 | .map(|&rect| vk::ClearRect { |
| 312 | rect, |
| 313 | base_array_layer: 0, |
| 314 | layer_count: 1, |
| 315 | }) |
| 316 | .collect(); |
| 317 | d.cmd_clear_attachments(cmd, &clear, &rects); |
| 318 | } |
| 319 | Op::Draw { |
| 320 | texture, |
| 321 | push: constants, |
| 322 | scissors, |
| 323 | .. |
| 324 | } => { |
| 325 | let pipeline = pipeline.unwrap_or_default(); |
| 326 | if pipeline != bound { |
| 327 | d.cmd_bind_pipeline(cmd, vk::PipelineBindPoint::GRAPHICS, pipeline); |
| 328 | bound = pipeline; |
| 329 | } |
| 330 | if let Some(t) = texture { |
| 331 | let info = [vk::DescriptorImageInfo::default() |
| 332 | .image_view(t.0.view) |
| 333 | .image_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL) |
| 334 | .sampler(sampler)]; |
| 335 | let write = vk::WriteDescriptorSet::default() |
| 336 | .dst_binding(0) |
| 337 | .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER) |
| 338 | .image_info(&info); |
| 339 | push.cmd_push_descriptor_set( |
| 340 | cmd, |
| 341 | vk::PipelineBindPoint::GRAPHICS, |
| 342 | self.pipelines.layout, |
| 343 | 0, |
| 344 | &[write], |
| 345 | ); |
| 346 | } |
| 347 | d.cmd_push_constants( |
| 348 | cmd, |
| 349 | self.pipelines.layout, |
| 350 | vk::ShaderStageFlags::VERTEX | vk::ShaderStageFlags::FRAGMENT, |
| 351 | 0, |
| 352 | constants.bytes(), |
| 353 | ); |
| 354 | for s in scissors { |
| 355 | d.cmd_set_scissor(cmd, 0, &[*s]); |
| 356 | d.cmd_draw(cmd, 4, 1, 0, 0); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | d.cmd_end_rendering(cmd); |
| 362 | release( |
| 363 | d, |
| 364 | cmd, |
| 365 | family, |
| 366 | &image, |
| 367 | vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL, |
| 368 | ); |
| 369 | for t in &sampled { |
| 370 | release(d, cmd, family, t, vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL); |
| 371 | } |
| 372 | } |
| 373 | let fence = self.end_and_submit(cmd)?; |
| 374 | sampled.push(image); |
| 375 | self.pending.push(Pending { |
| 376 | fence: fence.clone(), |
| 377 | cmd, |
| 378 | _keep: sampled, |
| 379 | _staging: None, |
| 380 | }); |
| 381 | Ok(SyncPoint::from(VkFence(fence))) |
| 382 | } |
| 383 | |
| 384 | /// Copies `region` of a full-image `data` into `image`, rows top-down or |
| 385 | /// flipped. |
| 386 | fn upload( |
| 387 | &mut self, |
| 388 | image: &Arc<ImageInner>, |
| 389 | data: &[u8], |
| 390 | region: Rectangle<i32, Buffer>, |
| 391 | flipped: bool, |
| 392 | ) -> Result<(), Error> { |
| 393 | let width = image.width as usize; |
| 394 | let rows = image.height as usize; |
| 395 | let needed = width * rows * BYTES_PER_PIXEL; |
| 396 | if data.len() < needed { |
| 397 | return Err(Error::BufferTooSmall { |
| 398 | needed, |
| 399 | got: data.len(), |
| 400 | }); |
| 401 | } |
| 402 | let stride = width * BYTES_PER_PIXEL; |
| 403 | self.upload_with(image, region, |y| { |
| 404 | let from = if flipped { rows - 1 - y } else { y }; |
| 405 | let start = from * stride + region.loc.x as usize * BYTES_PER_PIXEL; |
| 406 | &data[start..start + region.size.w as usize * BYTES_PER_PIXEL] |
| 407 | }) |
| 408 | } |
| 409 | |
| 410 | /// Uploads `region` of `image`, taking each image row `y` in the region |
| 411 | /// from `row(y)`. The CPU does not wait: the copy is ordered before later |
| 412 | /// frames by its barrier, and its staging memory lives until its fence. |
| 413 | fn upload_with<'a>( |
| 414 | &mut self, |
| 415 | image: &Arc<ImageInner>, |
| 416 | region: Rectangle<i32, Buffer>, |
| 417 | row: impl Fn(usize) -> &'a [u8], |
| 418 | ) -> Result<(), Error> { |
| 419 | if image.external { |
| 420 | return Err(Error::ForeignTexture); |
| 421 | } |
| 422 | self.reap(); |
| 423 | let (w, h) = (region.size.w as usize, region.size.h as usize); |
| 424 | if w == 0 || h == 0 { |
| 425 | return Ok(()); |
| 426 | } |
| 427 | let line = w * BYTES_PER_PIXEL; |
| 428 | let mut staging = Staging::new( |
| 429 | &self.shared, |
| 430 | line * h, |
| 431 | vk::BufferUsageFlags::TRANSFER_SRC, |
| 432 | MemoryLocation::CpuToGpu, |
| 433 | )?; |
| 434 | let dst = staging.bytes_mut(); |
| 435 | for i in 0..h { |
| 436 | dst[i * line..(i + 1) * line].copy_from_slice(row(region.loc.y as usize + i)); |
| 437 | } |
| 438 | let copy = vk::BufferImageCopy::default() |
| 439 | .buffer_row_length(w as u32) |
| 440 | .image_subresource( |
| 441 | vk::ImageSubresourceLayers::default() |
| 442 | .aspect_mask(vk::ImageAspectFlags::COLOR) |
| 443 | .layer_count(1), |
| 444 | ) |
| 445 | .image_offset(vk::Offset3D { |
| 446 | x: region.loc.x, |
| 447 | y: region.loc.y, |
| 448 | z: 0, |
| 449 | }) |
| 450 | .image_extent(vk::Extent3D { |
| 451 | width: w as u32, |
| 452 | height: h as u32, |
| 453 | depth: 1, |
| 454 | }); |
| 455 | let d = &self.shared.device.device; |
| 456 | let family = self.shared.device.queue_family; |
| 457 | let cmd = self.begin()?; |
| 458 | unsafe { |
| 459 | acquire(d, cmd, family, image, vk::ImageLayout::TRANSFER_DST_OPTIMAL); |
| 460 | d.cmd_copy_buffer_to_image( |
| 461 | cmd, |
| 462 | staging.buffer, |
| 463 | image.image, |
| 464 | vk::ImageLayout::TRANSFER_DST_OPTIMAL, |
| 465 | &[copy], |
| 466 | ); |
| 467 | acquire( |
| 468 | d, |
| 469 | cmd, |
| 470 | family, |
| 471 | image, |
| 472 | vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL, |
| 473 | ); |
| 474 | } |
| 475 | let fence = self.end_and_submit(cmd)?; |
| 476 | self.pending.push(Pending { |
| 477 | fence, |
| 478 | cmd, |
| 479 | _keep: vec![image.clone()], |
| 480 | _staging: Some(staging), |
| 481 | }); |
| 482 | Ok(()) |
| 483 | } |
| 484 | |
| 485 | fn read_back( |
| 486 | &mut self, |
| 487 | image: &Arc<ImageInner>, |
| 488 | region: Rectangle<i32, Buffer>, |
| 489 | fourcc: Fourcc, |
| 490 | ) -> Result<VulkanMapping, Error> { |
| 491 | let region = region |
| 492 | .intersection(Rectangle::from_size( |
| 493 | (image.width as i32, image.height as i32).into(), |
| 494 | )) |
| 495 | .unwrap_or_default(); |
| 496 | let (w, h) = (region.size.w.max(0) as u32, region.size.h.max(0) as u32); |
| 497 | let swap = needs_swizzle(image.fourcc, fourcc)?; |
| 498 | let len = (w as usize * h as usize * BYTES_PER_PIXEL).max(BYTES_PER_PIXEL); |
| 499 | let staging = Staging::new( |
| 500 | &self.shared, |
| 501 | len, |
| 502 | vk::BufferUsageFlags::TRANSFER_DST, |
| 503 | MemoryLocation::GpuToCpu, |
| 504 | )?; |
| 505 | if w > 0 && h > 0 { |
| 506 | let copy = vk::BufferImageCopy::default() |
| 507 | .image_subresource( |
| 508 | vk::ImageSubresourceLayers::default() |
| 509 | .aspect_mask(vk::ImageAspectFlags::COLOR) |
| 510 | .layer_count(1), |
| 511 | ) |
| 512 | .image_offset(vk::Offset3D { |
| 513 | x: region.loc.x, |
| 514 | y: region.loc.y, |
| 515 | z: 0, |
| 516 | }) |
| 517 | .image_extent(vk::Extent3D { |
| 518 | width: w, |
| 519 | height: h, |
| 520 | depth: 1, |
| 521 | }); |
| 522 | let buffer = staging.buffer; |
| 523 | let source = image.clone(); |
| 524 | self.one_shot(|d, cmd, family| unsafe { |
| 525 | acquire( |
| 526 | d, |
| 527 | cmd, |
| 528 | family, |
| 529 | &source, |
| 530 | vk::ImageLayout::TRANSFER_SRC_OPTIMAL, |
| 531 | ); |
| 532 | d.cmd_copy_image_to_buffer( |
| 533 | cmd, |
| 534 | source.image, |
| 535 | vk::ImageLayout::TRANSFER_SRC_OPTIMAL, |
| 536 | buffer, |
| 537 | &[copy], |
| 538 | ); |
| 539 | release( |
| 540 | d, |
| 541 | cmd, |
| 542 | family, |
| 543 | &source, |
| 544 | vk::ImageLayout::TRANSFER_SRC_OPTIMAL, |
| 545 | ); |
| 546 | })?; |
| 547 | } |
| 548 | let mut data = staging.bytes()[..w as usize * h as usize * BYTES_PER_PIXEL].to_vec(); |
| 549 | if swap { |
| 550 | for px in data.as_chunks_mut::<BYTES_PER_PIXEL>().0 { |
| 551 | px.swap(0, 2); |
| 552 | } |
| 553 | } |
| 554 | Ok(VulkanMapping { |
| 555 | data, |
| 556 | width: w, |
| 557 | height: h, |
| 558 | fourcc, |
| 559 | }) |
| 560 | } |
| 561 | |
| 562 | fn own(&self, image: &Arc<ImageInner>) -> Result<(), Error> { |
| 563 | if Arc::ptr_eq(&image.shared, &self.shared) { |
| 564 | Ok(()) |
| 565 | } else { |
| 566 | Err(Error::ForeignTexture) |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | impl Drop for VulkanRenderer { |
| 572 | fn drop(&mut self) { |
| 573 | let d = &self.shared.device.device; |
| 574 | unsafe { |
| 575 | let _ = d.device_wait_idle(); |
| 576 | for p in self.pending.drain(..) { |
| 577 | d.free_command_buffers(self.pool, &[p.cmd]); |
| 578 | } |
| 579 | for s in self.samplers { |
| 580 | d.destroy_sampler(s, None); |
| 581 | } |
| 582 | d.destroy_command_pool(self.pool, None); |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | fn sampler_filter(bit: usize) -> TextureFilter { |
| 588 | if bit == 0 { |
| 589 | TextureFilter::Linear |
| 590 | } else { |
| 591 | TextureFilter::Nearest |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /// A full barrier between all earlier and later work on the image, optionally |
| 596 | /// transferring ownership between queue families. Coarse, but correct for |
| 597 | /// every transition the renderer makes. |
| 598 | unsafe fn barrier( |
| 599 | d: &ash::Device, |
| 600 | cmd: vk::CommandBuffer, |
| 601 | image: vk::Image, |
| 602 | old: vk::ImageLayout, |
| 603 | new: vk::ImageLayout, |
| 604 | src_family: u32, |
| 605 | dst_family: u32, |
| 606 | ) { |
| 607 | let b = [vk::ImageMemoryBarrier2::default() |
| 608 | .src_stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS) |
| 609 | .src_access_mask(vk::AccessFlags2::MEMORY_WRITE) |
| 610 | .dst_stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS) |
| 611 | .dst_access_mask(vk::AccessFlags2::MEMORY_READ | vk::AccessFlags2::MEMORY_WRITE) |
| 612 | .old_layout(old) |
| 613 | .new_layout(new) |
| 614 | .src_queue_family_index(src_family) |
| 615 | .dst_queue_family_index(dst_family) |
| 616 | .image(image) |
| 617 | .subresource_range(color_range())]; |
| 618 | unsafe { |
| 619 | d.cmd_pipeline_barrier2( |
| 620 | cmd, |
| 621 | &vk::DependencyInfo::default().image_memory_barriers(&b), |
| 622 | ) |
| 623 | }; |
| 624 | } |
| 625 | |
| 626 | /// Moves an image into `layout` for use on this queue. Dmabuf images come |
| 627 | /// from the foreign queue family in the general layout, others continue from |
| 628 | /// their tracked layout. |
| 629 | unsafe fn acquire( |
| 630 | d: &ash::Device, |
| 631 | cmd: vk::CommandBuffer, |
| 632 | family: u32, |
| 633 | image: &ImageInner, |
| 634 | layout: vk::ImageLayout, |
| 635 | ) { |
| 636 | if image.external { |
| 637 | unsafe { |
| 638 | barrier( |
| 639 | d, |
| 640 | cmd, |
| 641 | image.image, |
| 642 | vk::ImageLayout::GENERAL, |
| 643 | layout, |
| 644 | vk::QUEUE_FAMILY_FOREIGN_EXT, |
| 645 | family, |
| 646 | ) |
| 647 | }; |
| 648 | } else { |
| 649 | let old = image.layout(); |
| 650 | if old != layout { |
| 651 | unsafe { |
| 652 | barrier( |
| 653 | d, |
| 654 | cmd, |
| 655 | image.image, |
| 656 | old, |
| 657 | layout, |
| 658 | vk::QUEUE_FAMILY_IGNORED, |
| 659 | vk::QUEUE_FAMILY_IGNORED, |
| 660 | ) |
| 661 | }; |
| 662 | image.set_layout(layout); |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | /// Hands a dmabuf image back to the foreign queue family after use. |
| 668 | unsafe fn release( |
| 669 | d: &ash::Device, |
| 670 | cmd: vk::CommandBuffer, |
| 671 | family: u32, |
| 672 | image: &ImageInner, |
| 673 | layout: vk::ImageLayout, |
| 674 | ) { |
| 675 | if image.external { |
| 676 | unsafe { |
| 677 | barrier( |
| 678 | d, |
| 679 | cmd, |
| 680 | image.image, |
| 681 | layout, |
| 682 | vk::ImageLayout::GENERAL, |
| 683 | family, |
| 684 | vk::QUEUE_FAMILY_FOREIGN_EXT, |
| 685 | ) |
| 686 | }; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | /// Whether reading `from` as `to` needs red and blue swapped. |
| 691 | fn needs_swizzle(from: Fourcc, to: Fourcc) -> Result<bool, Error> { |
| 692 | let bgr = |f: Fourcc| match f { |
| 693 | Fourcc::Argb8888 | Fourcc::Xrgb8888 => Ok(true), |
| 694 | Fourcc::Abgr8888 | Fourcc::Xbgr8888 => Ok(false), |
| 695 | f => Err(Error::UnsupportedFormat(f)), |
| 696 | }; |
| 697 | Ok(bgr(from)? != bgr(to)?) |
| 698 | } |
| 699 | |
| 700 | impl RendererSuper for VulkanRenderer { |
| 701 | type Error = Error; |
| 702 | type TextureId = VulkanTexture; |
| 703 | type Framebuffer<'buffer> = VulkanTarget<'buffer>; |
| 704 | type Frame<'frame, 'buffer> |
| 705 | = VulkanFrame<'frame, 'buffer> |
| 706 | where |
| 707 | 'buffer: 'frame, |
| 708 | Self: 'frame; |
| 709 | } |
| 710 | |
| 711 | impl Renderer for VulkanRenderer { |
| 712 | fn context_id(&self) -> ContextId<VulkanTexture> { |
| 713 | self.context.clone() |
| 714 | } |
| 715 | |
| 716 | fn downscale_filter(&mut self, filter: TextureFilter) -> Result<(), Error> { |
| 717 | self.min_filter = filter; |
| 718 | Ok(()) |
| 719 | } |
| 720 | |
| 721 | fn upscale_filter(&mut self, filter: TextureFilter) -> Result<(), Error> { |
| 722 | self.mag_filter = filter; |
| 723 | Ok(()) |
| 724 | } |
| 725 | |
| 726 | fn set_debug_flags(&mut self, flags: DebugFlags) { |
| 727 | self.debug = flags; |
| 728 | } |
| 729 | |
| 730 | fn debug_flags(&self) -> DebugFlags { |
| 731 | self.debug |
| 732 | } |
| 733 | |
| 734 | fn render<'frame, 'buffer>( |
| 735 | &'frame mut self, |
| 736 | framebuffer: &'frame mut VulkanTarget<'buffer>, |
| 737 | output_size: Size<i32, Physical>, |
| 738 | dst_transform: Transform, |
| 739 | ) -> Result<VulkanFrame<'frame, 'buffer>, Error> |
| 740 | where |
| 741 | 'buffer: 'frame, |
| 742 | { |
| 743 | self.own(&framebuffer.0)?; |
| 744 | self.reap(); |
| 745 | Ok(VulkanFrame::new( |
| 746 | self, |
| 747 | framebuffer, |
| 748 | output_size, |
| 749 | dst_transform, |
| 750 | )) |
| 751 | } |
| 752 | |
| 753 | fn wait(&mut self, sync: &SyncPoint) -> Result<(), Error> { |
| 754 | sync.wait().map_err(|_| Error::Interrupted) |
| 755 | } |
| 756 | |
| 757 | fn cleanup_texture_cache(&mut self) -> Result<(), Error> { |
| 758 | self.reap(); |
| 759 | Ok(()) |
| 760 | } |
| 761 | |
| 762 | fn invalidate_caches(&mut self) -> Result<(), Error> { |
| 763 | self.reap(); |
| 764 | self.dmabuf_textures.clear(); |
| 765 | self.dmabuf_targets.clear(); |
| 766 | Ok(()) |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | impl Bind<VulkanImage> for VulkanRenderer { |
| 771 | fn bind<'a>(&mut self, target: &'a mut VulkanImage) -> Result<VulkanTarget<'a>, Error> { |
| 772 | self.own(&target.0)?; |
| 773 | Ok(VulkanTarget(target.0.clone(), PhantomData)) |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | impl Bind<Dmabuf> for VulkanRenderer { |
| 778 | fn bind<'a>(&mut self, target: &'a mut Dmabuf) -> Result<VulkanTarget<'a>, Error> { |
| 779 | let key = target.weak(); |
| 780 | let image = match self.dmabuf_targets.get(&key) { |
| 781 | Some(image) => image.clone(), |
| 782 | None => { |
| 783 | let image = dmabuf::import(&self.shared, &self.caps, target, RENDER_USAGE)?; |
| 784 | self.dmabuf_targets.insert(key, image.clone()); |
| 785 | image |
| 786 | } |
| 787 | }; |
| 788 | Ok(VulkanTarget(image, PhantomData)) |
| 789 | } |
| 790 | |
| 791 | fn supported_formats(&self) -> Option<FormatSet> { |
| 792 | Some(self.caps.render.clone()) |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | impl Offscreen<VulkanImage> for VulkanRenderer { |
| 797 | fn create_buffer( |
| 798 | &mut self, |
| 799 | format: Fourcc, |
| 800 | size: Size<i32, Buffer>, |
| 801 | ) -> Result<VulkanImage, Error> { |
| 802 | if size.w <= 0 || size.h <= 0 { |
| 803 | return Err(Error::InvalidSize(size.w, size.h)); |
| 804 | } |
| 805 | let usage = vk::ImageUsageFlags::COLOR_ATTACHMENT |
| 806 | | vk::ImageUsageFlags::SAMPLED |
| 807 | | vk::ImageUsageFlags::TRANSFER_SRC |
| 808 | | vk::ImageUsageFlags::TRANSFER_DST; |
| 809 | Ok(VulkanImage(ImageInner::create( |
| 810 | &self.shared, |
| 811 | format, |
| 812 | size.w as u32, |
| 813 | size.h as u32, |
| 814 | usage, |
| 815 | )?)) |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | impl ImportMem for VulkanRenderer { |
| 820 | fn import_memory( |
| 821 | &mut self, |
| 822 | data: &[u8], |
| 823 | format: Fourcc, |
| 824 | size: Size<i32, Buffer>, |
| 825 | flipped: bool, |
| 826 | ) -> Result<VulkanTexture, Error> { |
| 827 | if size.w <= 0 || size.h <= 0 { |
| 828 | return Err(Error::InvalidSize(size.w, size.h)); |
| 829 | } |
| 830 | let usage = vk::ImageUsageFlags::SAMPLED |
| 831 | | vk::ImageUsageFlags::TRANSFER_DST |
| 832 | | vk::ImageUsageFlags::TRANSFER_SRC; |
| 833 | let image = ImageInner::create(&self.shared, format, size.w as u32, size.h as u32, usage)?; |
| 834 | self.upload(&image, data, Rectangle::from_size(size), flipped)?; |
| 835 | Ok(VulkanTexture(image)) |
| 836 | } |
| 837 | |
| 838 | fn update_memory( |
| 839 | &mut self, |
| 840 | texture: &VulkanTexture, |
| 841 | data: &[u8], |
| 842 | region: Rectangle<i32, Buffer>, |
| 843 | ) -> Result<(), Error> { |
| 844 | self.own(&texture.0)?; |
| 845 | let Some(region) = region.intersection(Rectangle::from_size(texture.size())) else { |
| 846 | return Ok(()); |
| 847 | }; |
| 848 | self.upload(&texture.0, data, region, false) |
| 849 | } |
| 850 | |
| 851 | fn mem_formats(&self) -> Box<dyn Iterator<Item = Fourcc>> { |
| 852 | Box::new(SUPPORTED.into_iter()) |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | impl ImportDma for VulkanRenderer { |
| 857 | fn dmabuf_formats(&self) -> FormatSet { |
| 858 | self.caps.sample.clone() |
| 859 | } |
| 860 | |
| 861 | fn import_dmabuf( |
| 862 | &mut self, |
| 863 | buffer: &Dmabuf, |
| 864 | _damage: Option<&[Rectangle<i32, Buffer>]>, |
| 865 | ) -> Result<VulkanTexture, Error> { |
| 866 | let key = buffer.weak(); |
| 867 | if let Some(t) = self.dmabuf_textures.get(&key) { |
| 868 | return Ok(t.clone()); |
| 869 | } |
| 870 | let texture = VulkanTexture(dmabuf::import( |
| 871 | &self.shared, |
| 872 | &self.caps, |
| 873 | buffer, |
| 874 | SAMPLE_USAGE, |
| 875 | )?); |
| 876 | self.dmabuf_textures.insert(key, texture.clone()); |
| 877 | Ok(texture) |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | impl ImportDmaWl for VulkanRenderer {} |
| 882 | |
| 883 | impl ExportMem for VulkanRenderer { |
| 884 | type TextureMapping = VulkanMapping; |
| 885 | |
| 886 | fn copy_framebuffer( |
| 887 | &mut self, |
| 888 | target: &VulkanTarget<'_>, |
| 889 | region: Rectangle<i32, Buffer>, |
| 890 | format: Fourcc, |
| 891 | ) -> Result<VulkanMapping, Error> { |
| 892 | self.own(&target.0)?; |
| 893 | let image = target.0.clone(); |
| 894 | self.read_back(&image, region, format) |
| 895 | } |
| 896 | |
| 897 | fn copy_texture( |
| 898 | &mut self, |
| 899 | texture: &VulkanTexture, |
| 900 | region: Rectangle<i32, Buffer>, |
| 901 | format: Fourcc, |
| 902 | ) -> Result<VulkanMapping, Error> { |
| 903 | self.own(&texture.0)?; |
| 904 | self.read_back(&texture.0, region, format) |
| 905 | } |
| 906 | |
| 907 | fn can_read_texture(&mut self, texture: &VulkanTexture) -> Result<bool, Error> { |
| 908 | Ok(texture.0.external || texture.0.layout() != vk::ImageLayout::UNDEFINED) |
| 909 | } |
| 910 | |
| 911 | fn map_texture<'a>(&mut self, mapping: &'a VulkanMapping) -> Result<&'a [u8], Error> { |
| 912 | Ok(&mapping.data) |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | impl ImportMemWl for VulkanRenderer { |
| 917 | /// Keeps one texture per surface and uploads only the damaged rows into |
| 918 | /// it, straight from the client's shm pool. |
| 919 | fn import_shm_buffer( |
| 920 | &mut self, |
| 921 | buffer: &WlBuffer, |
| 922 | surface: Option<&SurfaceData>, |
| 923 | damage: &[Rectangle<i32, Buffer>], |
| 924 | ) -> Result<VulkanTexture, Error> { |
| 925 | let cache = surface.map(|s| { |
| 926 | s.data_map |
| 927 | .insert_if_missing_threadsafe(|| ShmCache(Mutex::new(None))); |
| 928 | s.data_map.get::<ShmCache>().expect("just inserted") |
| 929 | }); |
| 930 | with_buffer_contents(buffer, |ptr, len, data| { |
| 931 | let fourcc = shm_format_to_fourcc(data.format).ok_or(Error::ShmAccess)?; |
| 932 | let (w, h, stride, offset) = ( |
| 933 | data.width as usize, |
| 934 | data.height as usize, |
| 935 | data.stride as usize, |
| 936 | data.offset as usize, |
| 937 | ); |
| 938 | let line = w * BYTES_PER_PIXEL; |
| 939 | if w == 0 || h == 0 || stride < line || offset + stride * (h - 1) + line > len { |
| 940 | return Err(Error::ShmAccess); |
| 941 | } |
| 942 | let size = Size::from((data.width, data.height)); |
| 943 | let full = Rectangle::from_size(size); |
| 944 | let reused = cache |
| 945 | .and_then(|c| c.0.lock().unwrap_or_else(|e| e.into_inner()).clone()) |
| 946 | .filter(|t| { |
| 947 | t.0.fourcc == fourcc |
| 948 | && t.size() == size |
| 949 | && Arc::ptr_eq(&t.0.shared, &self.shared) |
| 950 | }); |
| 951 | let (texture, region) = match reused { |
| 952 | Some(t) => { |
| 953 | let dirty = damage |
| 954 | .iter() |
| 955 | .filter_map(|d| d.intersection(full)) |
| 956 | .reduce(|a, b| a.merge(b)); |
| 957 | (t, if damage.is_empty() { Some(full) } else { dirty }) |
| 958 | } |
| 959 | None => { |
| 960 | let usage = vk::ImageUsageFlags::SAMPLED |
| 961 | | vk::ImageUsageFlags::TRANSFER_DST |
| 962 | | vk::ImageUsageFlags::TRANSFER_SRC; |
| 963 | let image = |
| 964 | ImageInner::create(&self.shared, fourcc, w as u32, h as u32, usage)?; |
| 965 | (VulkanTexture(image), Some(full)) |
| 966 | } |
| 967 | }; |
| 968 | if let Some(region) = region { |
| 969 | let x = region.loc.x as usize * BYTES_PER_PIXEL; |
| 970 | let rw = region.size.w as usize * BYTES_PER_PIXEL; |
| 971 | self.upload_with(&texture.0, region, |y| unsafe { |
| 972 | std::slice::from_raw_parts(ptr.add(offset + y * stride + x), rw) |
| 973 | })?; |
| 974 | } |
| 975 | if let Some(c) = cache { |
| 976 | *c.0.lock().unwrap_or_else(|e| e.into_inner()) = Some(texture.clone()); |
| 977 | } |
| 978 | Ok(texture) |
| 979 | }) |
| 980 | .map_err(|_| Error::ShmAccess)? |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | /// The texture last imported for a surface's shm buffers. |
| 985 | struct ShmCache(Mutex<Option<VulkanTexture>>); |