|
| 1 | +use crate::api::osmesa; |
| 2 | +use crate::{ |
| 3 | + Api, ContextBuilderWrapper, ContextError, CreationError, PixelFormat, |
| 4 | +}; |
| 5 | +use winit::dpi; |
| 6 | + |
| 7 | +#[derive(Debug)] |
| 8 | +pub struct OsMesaContext { |
| 9 | + pub(crate) context: osmesa::OsMesaContext, |
| 10 | +} |
| 11 | + |
| 12 | +pub trait OsMesaContextExt { |
| 13 | + fn build_osmesa(self) -> Result<OsMesaContext, CreationError> |
| 14 | + where |
| 15 | + Self: Sized; |
| 16 | +} |
| 17 | + |
| 18 | +impl<'a> OsMesaContextExt for OsMesaContextBuilder<'a> { |
| 19 | + #[inline] |
| 20 | + fn build_osmesa(self) -> Result<OsMesaContext, CreationError> |
| 21 | + where |
| 22 | + Self: Sized, |
| 23 | + { |
| 24 | + let cb = self.map_sharing(|ctx| &ctx.context); |
| 25 | + osmesa::OsMesaContext::new(cb).map(|context| OsMesaContext { context }) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +pub type OsMesaContextBuilder<'a> = ContextBuilderWrapper<&'a OsMesaContext>; |
| 30 | + |
| 31 | +#[derive(Debug)] |
| 32 | +pub struct OsMesaBuffer { |
| 33 | + pub(crate) buffer: osmesa::OsMesaBuffer, |
| 34 | +} |
| 35 | + |
| 36 | +impl OsMesaContext { |
| 37 | + /// Returns the address of an OpenGL function. |
| 38 | + pub fn get_proc_address(&self, addr: &str) -> *const () { |
| 39 | + self.context.get_proc_address(addr) |
| 40 | + } |
| 41 | + |
| 42 | + pub fn is_current(&self) -> bool { |
| 43 | + self.context.is_current() |
| 44 | + } |
| 45 | + |
| 46 | + pub fn get_api(&self) -> Api { |
| 47 | + self.context.get_api() |
| 48 | + } |
| 49 | + |
| 50 | + pub unsafe fn make_current_osmesa_buffer( |
| 51 | + &self, |
| 52 | + buffer: &OsMesaBuffer, |
| 53 | + ) -> Result<(), ContextError> { |
| 54 | + self.context.make_current_osmesa_buffer(buffer.inner()) |
| 55 | + } |
| 56 | + |
| 57 | + pub unsafe fn make_not_current(&self) -> Result<(), ContextError> { |
| 58 | + self.context.make_not_current() |
| 59 | + } |
| 60 | + |
| 61 | + pub(crate) fn inner(&self) -> &osmesa::OsMesaContext { |
| 62 | + &self.context |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl OsMesaBuffer { |
| 67 | + pub(crate) fn inner(&self) -> &osmesa::OsMesaBuffer { |
| 68 | + &self.buffer |
| 69 | + } |
| 70 | + |
| 71 | + pub fn get_pixel_format(&self) -> PixelFormat { |
| 72 | + self.buffer.get_pixel_format() |
| 73 | + } |
| 74 | + |
| 75 | + pub fn new( |
| 76 | + ctx: &OsMesaContext, |
| 77 | + size: dpi::PhysicalSize, |
| 78 | + ) -> Result<OsMesaBuffer, CreationError> { |
| 79 | + let ctx = ctx.inner(); |
| 80 | + osmesa::OsMesaBuffer::new(ctx, size) |
| 81 | + .map(|buffer| OsMesaBuffer { buffer }) |
| 82 | + } |
| 83 | +} |
0 commit comments