1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
#![allow(clippy::new_without_default)]
#![allow(clippy::type_complexity)]
use std::sync::{Arc, Mutex};
use irondash_run_loop::{util::Capsule, RunLoop, RunLoopSender};
use platform::PlatformTexture;
mod error;
mod log;
mod platform;
pub use error::*;
pub type Result<T> = std::result::Result<T, Error>;
/// Native texture.
///
/// `Type` parameters specifies the payload type of the texture.
/// It can be [`BoxedPixelData`], which is supported on all platforms, or
/// one of the platform specific types such as `BoxedIOSurface`,
/// `BoxedGLTexture` or `BoxedTextureDescriptor`.
pub struct Texture<Type> {
platform_texture: PlatformTexture<Type>,
}
impl<Type> Texture<Type> {
/// Returns identifier of the texture. This needs to be passed to
/// Dart and used to create a Flutter Texture widget.
pub fn id(&self) -> i64 {
self.platform_texture.id()
}
/// Informs Flutter that new texture frame is available.
/// This will make Flutter request new texture payload from provider
/// during next frame rasterization.
pub fn mark_frame_available(&self) -> Result<()> {
self.platform_texture.mark_frame_available()
}
/// Converts Texture to a SendableTexture. SendableTexture can be
/// sent between threads and update the content on any thread.
pub fn into_sendable_texture(self) -> Arc<SendableTexture<Type>> {
Arc::new(SendableTexture {
sender: RunLoop::current().new_sender(),
texture: Mutex::new(Capsule::new(self)),
})
}
}
///
/// Trait that implemented by objects that provide texture contents.
pub trait PayloadProvider<Type>: Send + Sync {
/// Called by the engine to get the latest texture payload. This will
/// most likely be called on raster thread. Hence PayloadProvider must
/// be thread safe.
///
/// Boxed payload is used to allow custom payload objects, which might
/// be useful in situation where the provider needs to know when Flutter
/// is done with the payload (i.e. by implementing Drop trait on the payload
/// object).
fn get_payload(&self) -> Type;
}
impl<Type: PlatformTextureWithProvider> Texture<Type> {
/// Creates new texture for given engine with specified payload provider.
///
/// Creating PixelData backed texture is supported on all platforms:
///
/// ```ignore
/// // Assume MyPixelDataProvider implements PayloadProvider<BoxedPixelData>
/// let provider = Arc::new(MyPixelDataProvider::new());
///
/// let texture = Texture::new_with_provider(engine_handle, provider)?;
///
/// // This will cause flutter to request a PixelData during next
/// // frame rasterization.
/// texture.mark_frame_available()?;
/// ```
pub fn new_with_provider(
engine_handle: i64,
payload_provider: Arc<dyn PayloadProvider<Type>>,
) -> Result<Self> {
Ok(Self {
platform_texture: Type::create_texture(engine_handle, payload_provider)?,
})
}
}
impl<Type: PlatformTextureWithoutProvider> Texture<Type> {
/// Creates new texture for given engine without payload. This is used on
/// Android where instead of providing payload to the texture,
/// you work directly with underlying surface or native window.
///
/// ```ignore
/// let texture = Texture::<NativeWindow>::new(engine_handle)?;
/// let native_window = texture.get();
/// ```
pub fn new(engine_handle: i64) -> Result<Self> {
Ok(Self {
platform_texture: Type::create_texture(engine_handle)?,
})
}
pub fn get(&self) -> Type {
Type::get(&self.platform_texture)
}
}
pub enum PixelFormat {
BGRA,
RGBA,
}
/// Pixel data is supported payload type on every platform, but the expected
/// PixelFormat may differ. You can [`PixelData::FORMAT`] to query expected
/// pixel format.
pub struct PixelData<'a> {
pub width: i32,
pub height: i32,
pub data: &'a [u8],
}
impl<'a> PixelData<'a> {
pub const FORMAT: PixelFormat = platform::PIXEL_DATA_FORMAT;
}
pub trait PixelDataProvider {
fn get(&self) -> PixelData;
}
/// Actual type for pixel buffer payload.
pub type BoxedPixelData = Box<dyn PixelDataProvider>;
/// Convenience implementation for pixel data texture.
pub struct SimplePixelData {
width: i32,
height: i32,
data: Vec<u8>,
}
impl SimplePixelData {
pub fn new_boxed(width: i32, height: i32, data: Vec<u8>) -> Box<Self> {
Box::new(Self {
width,
height,
data,
})
}
}
impl PixelDataProvider for SimplePixelData {
fn get(&self) -> PixelData {
PixelData {
width: self.width,
height: self.height,
data: &self.data,
}
}
}
//
// Platform specific payloads.
//
#[cfg(target_os = "android")]
mod android {
// These can be obtained from texture using Texture::get(&self).
pub type NativeWindow = super::platform::NativeWindow;
pub type Surface = super::platform::Surface;
}
#[cfg(target_os = "android")]
pub use android::*;
#[cfg(any(target_os = "ios", target_os = "macos"))]
mod darwin {
pub mod io_surface {
pub use crate::platform::io_surface::*;
}
pub trait IOSurfaceProvider {
fn get(&self) -> &io_surface::IOSurface;
}
/// Payload type for IOSurface backed texture.
pub type BoxedIOSurface = Box<dyn IOSurfaceProvider>;
}
#[cfg(any(target_os = "ios", target_os = "macos"))]
pub use darwin::*;
#[cfg(target_os = "linux")]
mod linux {
pub struct GLTexture<'a> {
pub target: u32, // texture target (i.e. GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE)
pub name: &'a u32, // OpenGL texture name
pub width: i32,
pub height: i32,
}
pub trait GLTextureProvider {
fn get(&self) -> GLTexture;
}
/// Payload type for IOSurface backed texture.
pub type BoxedGLTexture = Box<dyn GLTextureProvider>;
}
#[cfg(target_os = "linux")]
pub use linux::*;
#[cfg(target_os = "windows")]
mod windows {
use std::ffi::c_void;
/// Texture descriptor for native texture.
pub struct TextureDescriptor<'a, HandleType> {
pub handle: &'a HandleType,
pub width: i32,
pub height: i32,
pub visible_width: i32,
pub visible_height: i32,
pub pixel_format: super::PixelFormat,
}
pub trait TextureDescriptorProvider<HandleType> {
fn get(&self) -> TextureDescriptor<HandleType>;
}
pub type BoxedTextureDescriptor<HandleType> = Box<dyn TextureDescriptorProvider<HandleType>>;
/// Wrapper around `ID3D11Texture2D`, can be used as `TextureHandle` in
/// `TextureDescriptor`.
pub struct ID3D11Texture2D(pub *mut c_void);
/// Wrapper around DXGI shared handle (*mut HANDLE), can be used as
// `TextureHandle` in `TextureDescriptor`.
pub struct DxgiSharedHandle(pub *mut c_void);
}
#[cfg(target_os = "windows")]
pub use windows::*;
use crate::log::OkLog;
/// SendableTexture is Send and Sync so it can be sent between threads, but it
/// can only update the texture, it can not retrieve payload (such as Surface
/// or NativeWindow on Android).
pub struct SendableTexture<T: 'static> {
sender: RunLoopSender,
texture: Mutex<Capsule<Texture<T>>>,
}
impl<T> SendableTexture<T> {
pub fn mark_frame_available(self: &Arc<Self>) {
if self.sender.is_same_thread() {
let texture = self.texture.lock().unwrap();
let texture = texture.get_ref().unwrap();
texture.mark_frame_available().ok_log();
} else {
let texture_clone = self.clone();
self.sender.send(move || {
let texture = texture_clone.texture.lock().unwrap();
let texture = texture.get_ref().unwrap();
texture.mark_frame_available().ok_log();
});
}
}
}
// Helper traits
pub trait PlatformTextureWithProvider: Sized {
fn create_texture(
engine_handle: i64,
payload_provider: Arc<dyn PayloadProvider<Self>>,
) -> Result<PlatformTexture<Self>>;
}
pub trait PlatformTextureWithoutProvider: Sized {
fn create_texture(engine_handle: i64) -> Result<PlatformTexture<Self>>;
fn get(texture: &PlatformTexture<Self>) -> Self;
}