// Copyright 2020 The Chromium OS Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. use crate::{ DisplayT, GpuDisplayError, GpuDisplayFramebuffer, GpuDisplayResult, GpuDisplaySurface, SurfaceType, }; use base::{AsRawDescriptor, Event, RawDescriptor}; use data_model::VolatileSlice; #[allow(dead_code)] struct Buffer { width: u32, height: u32, bytes_per_pixel: u32, bytes: Vec, } impl Drop for Buffer { fn drop(&mut self) {} } impl Buffer { fn as_volatile_slice(&mut self) -> VolatileSlice { VolatileSlice::new(self.bytes.as_mut_slice()) } fn stride(&self) -> usize { (self.bytes_per_pixel as usize) * (self.width as usize) } fn bytes_per_pixel(&self) -> usize { self.bytes_per_pixel as usize } } struct StubSurface { width: u32, height: u32, buffer: Option, } impl StubSurface { /// Gets the buffer at buffer_index, allocating it if necessary. fn lazily_allocate_buffer(&mut self) -> Option<&mut Buffer> { if self.buffer.is_none() { // XRGB8888 let bytes_per_pixel = 4; let bytes_total = (self.width as u64) * (self.height as u64) * (bytes_per_pixel as u64); self.buffer = Some(Buffer { width: self.width, height: self.height, bytes_per_pixel, bytes: vec![0; bytes_total as usize], }); } self.buffer.as_mut() } } impl GpuDisplaySurface for StubSurface { fn framebuffer(&mut self) -> Option { let framebuffer = self.lazily_allocate_buffer()?; let framebuffer_stride = framebuffer.stride() as u32; let framebuffer_bytes_per_pixel = framebuffer.bytes_per_pixel() as u32; Some(GpuDisplayFramebuffer::new( framebuffer.as_volatile_slice(), framebuffer_stride, framebuffer_bytes_per_pixel, )) } } impl Drop for StubSurface { fn drop(&mut self) {} } pub struct DisplayStub { /// This event is never triggered and is used solely to fulfill AsRawDescriptor. event: Event, } impl DisplayStub { pub fn new() -> GpuDisplayResult { let event = Event::new().map_err(|_| GpuDisplayError::CreateEvent)?; Ok(DisplayStub { event }) } } impl DisplayT for DisplayStub { fn create_surface( &mut self, parent_surface_id: Option, _surface_id: u32, width: u32, height: u32, _surf_type: SurfaceType, ) -> GpuDisplayResult> { if parent_surface_id.is_some() { return Err(GpuDisplayError::Unsupported); } Ok(Box::new(StubSurface { width, height, buffer: None, })) } } impl AsRawDescriptor for DisplayStub { fn as_raw_descriptor(&self) -> RawDescriptor { self.event.as_raw_descriptor() } }