1 // Copyright 2020 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 use crate::{ 6 DisplayT, GpuDisplayError, GpuDisplayFramebuffer, GpuDisplayResult, GpuDisplaySurface, 7 SurfaceType, 8 }; 9 10 use base::{AsRawDescriptor, Event, RawDescriptor}; 11 use data_model::VolatileSlice; 12 13 #[allow(dead_code)] 14 struct Buffer { 15 width: u32, 16 height: u32, 17 bytes_per_pixel: u32, 18 bytes: Vec<u8>, 19 } 20 21 impl Drop for Buffer { drop(&mut self)22 fn drop(&mut self) {} 23 } 24 25 impl Buffer { as_volatile_slice(&mut self) -> VolatileSlice26 fn as_volatile_slice(&mut self) -> VolatileSlice { 27 VolatileSlice::new(self.bytes.as_mut_slice()) 28 } 29 stride(&self) -> usize30 fn stride(&self) -> usize { 31 (self.bytes_per_pixel as usize) * (self.width as usize) 32 } 33 bytes_per_pixel(&self) -> usize34 fn bytes_per_pixel(&self) -> usize { 35 self.bytes_per_pixel as usize 36 } 37 } 38 39 struct StubSurface { 40 width: u32, 41 height: u32, 42 buffer: Option<Buffer>, 43 } 44 45 impl StubSurface { 46 /// Gets the buffer at buffer_index, allocating it if necessary. lazily_allocate_buffer(&mut self) -> Option<&mut Buffer>47 fn lazily_allocate_buffer(&mut self) -> Option<&mut Buffer> { 48 if self.buffer.is_none() { 49 // XRGB8888 50 let bytes_per_pixel = 4; 51 let bytes_total = (self.width as u64) * (self.height as u64) * (bytes_per_pixel as u64); 52 53 self.buffer = Some(Buffer { 54 width: self.width, 55 height: self.height, 56 bytes_per_pixel, 57 bytes: vec![0; bytes_total as usize], 58 }); 59 } 60 61 self.buffer.as_mut() 62 } 63 } 64 65 impl GpuDisplaySurface for StubSurface { framebuffer(&mut self) -> Option<GpuDisplayFramebuffer>66 fn framebuffer(&mut self) -> Option<GpuDisplayFramebuffer> { 67 let framebuffer = self.lazily_allocate_buffer()?; 68 let framebuffer_stride = framebuffer.stride() as u32; 69 let framebuffer_bytes_per_pixel = framebuffer.bytes_per_pixel() as u32; 70 Some(GpuDisplayFramebuffer::new( 71 framebuffer.as_volatile_slice(), 72 framebuffer_stride, 73 framebuffer_bytes_per_pixel, 74 )) 75 } 76 } 77 78 impl Drop for StubSurface { drop(&mut self)79 fn drop(&mut self) {} 80 } 81 82 pub struct DisplayStub { 83 /// This event is never triggered and is used solely to fulfill AsRawDescriptor. 84 event: Event, 85 } 86 87 impl DisplayStub { new() -> GpuDisplayResult<DisplayStub>88 pub fn new() -> GpuDisplayResult<DisplayStub> { 89 let event = Event::new().map_err(|_| GpuDisplayError::CreateEvent)?; 90 91 Ok(DisplayStub { event }) 92 } 93 } 94 95 impl DisplayT for DisplayStub { create_surface( &mut self, parent_surface_id: Option<u32>, _surface_id: u32, width: u32, height: u32, _surf_type: SurfaceType, ) -> GpuDisplayResult<Box<dyn GpuDisplaySurface>>96 fn create_surface( 97 &mut self, 98 parent_surface_id: Option<u32>, 99 _surface_id: u32, 100 width: u32, 101 height: u32, 102 _surf_type: SurfaceType, 103 ) -> GpuDisplayResult<Box<dyn GpuDisplaySurface>> { 104 if parent_surface_id.is_some() { 105 return Err(GpuDisplayError::Unsupported); 106 } 107 108 Ok(Box::new(StubSurface { 109 width, 110 height, 111 buffer: None, 112 })) 113 } 114 } 115 116 impl AsRawDescriptor for DisplayStub { as_raw_descriptor(&self) -> RawDescriptor117 fn as_raw_descriptor(&self) -> RawDescriptor { 118 self.event.as_raw_descriptor() 119 } 120 } 121