| 1 | use std::{fmt, ptr}; |
| 2 | |
| 3 | use skia_bindings::{self as sb, GrMtlSurfaceInfo, GrMtlTextureInfo}; |
| 4 | |
| 5 | use crate::{ |
| 6 | gpu, |
| 7 | prelude::{self, NativeAccess, NativeDrop, NativePartialEq}, |
| 8 | }; |
| 9 | |
| 10 | pub use skia_bindings::GrMTLHandle as Handle; |
| 11 | pub use skia_bindings::GrMTLPixelFormat as PixelFormat; |
| 12 | pub use skia_bindings::GrMTLStorageMode as StorageMode; |
| 13 | pub use skia_bindings::GrMTLTextureUsage as TextureUsage; |
| 14 | |
| 15 | pub type TextureInfo = prelude::Handle<GrMtlTextureInfo>; |
| 16 | unsafe_send_sync!(TextureInfo); |
| 17 | |
| 18 | impl NativeDrop for GrMtlTextureInfo { |
| 19 | fn drop(&mut self) { |
| 20 | unsafe { sb::C_GrMtlTextureInfo_Destruct(self) } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | impl NativePartialEq for GrMtlTextureInfo { |
| 25 | fn eq(&self, other: &Self) -> bool { |
| 26 | unsafe { sb::C_GrMtlTextureInfo_Equals(self, other) } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | impl Default for TextureInfo { |
| 31 | fn default() -> Self { |
| 32 | unsafe { Self::new(ptr::null()) } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | impl fmt::Debug for TextureInfo { |
| 37 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 38 | f&mut DebugStruct<'_, '_>.debug_struct("TextureInfo" ) |
| 39 | .field(name:"texture" , &self.texture()) |
| 40 | .finish() |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | impl TextureInfo { |
| 45 | /// # Safety |
| 46 | /// |
| 47 | /// Unsafe because the texture provided must either be `null` or pointing to a Metal texture by |
| 48 | /// providing a raw pointer. |
| 49 | /// |
| 50 | /// This function retains the texture and releases it as soon TextureInfo is dropped. |
| 51 | pub unsafe fn new(texture: Handle) -> Self { |
| 52 | Self::construct(|ti: *mut {unknown}| sb::C_GrMtlTextureInfo_Construct(ti, texture)) |
| 53 | } |
| 54 | |
| 55 | pub fn texture(&self) -> Handle { |
| 56 | self.native().fTexture.fObject |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #[derive (Copy, Clone, PartialEq, Eq, Debug)] |
| 61 | #[repr (C)] |
| 62 | pub struct SurfaceInfo { |
| 63 | pub sample_count: u32, |
| 64 | pub level_count: u32, |
| 65 | pub protected: gpu::Protected, |
| 66 | |
| 67 | pub format: PixelFormat, |
| 68 | pub usage: TextureUsage, |
| 69 | pub storage_mode: StorageMode, |
| 70 | } |
| 71 | |
| 72 | native_transmutable!(GrMtlSurfaceInfo, SurfaceInfo, surface_info_layout); |
| 73 | |
| 74 | impl Default for SurfaceInfo { |
| 75 | fn default() -> Self { |
| 76 | Self { |
| 77 | sample_count: 1, |
| 78 | level_count: 0, |
| 79 | protected: gpu::Protected::No, |
| 80 | format: 0, |
| 81 | usage: 0, |
| 82 | storage_mode: 0, |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | #[cfg (test)] |
| 88 | mod tests { |
| 89 | use super::TextureInfo; |
| 90 | |
| 91 | #[test ] |
| 92 | fn default_texture_info() { |
| 93 | drop(TextureInfo::default()); |
| 94 | } |
| 95 | } |
| 96 | |