2022-08-13 23:16:17 +00:00
|
|
|
pub mod null;
|
2022-08-13 22:12:24 +00:00
|
|
|
|
2022-08-13 23:16:17 +00:00
|
|
|
use crate::bitmap::{Bitmap, BitmapHandle, BitmapInfo, BitmapSource};
|
2022-09-03 17:22:57 +00:00
|
|
|
use crate::commands::CommandList;
|
2022-08-13 23:16:17 +00:00
|
|
|
use crate::error::Error;
|
|
|
|
use crate::shape_utils::DistilledShape;
|
|
|
|
use crate::utils;
|
|
|
|
use downcast_rs::{impl_downcast, Downcast};
|
|
|
|
use swf;
|
2022-09-03 17:22:57 +00:00
|
|
|
use swf::Color;
|
2019-08-26 23:38:37 +00:00
|
|
|
|
2020-05-11 07:02:49 +00:00
|
|
|
pub trait RenderBackend: Downcast {
|
2022-08-04 05:50:18 +00:00
|
|
|
fn viewport_dimensions(&self) -> ViewportDimensions;
|
|
|
|
// Do not call this method directly - use `player.set_viewport_dimensions`,
|
|
|
|
// which will ensure that the stage is properly updated as well.
|
|
|
|
fn set_viewport_dimensions(&mut self, dimensions: ViewportDimensions);
|
2020-12-30 23:35:43 +00:00
|
|
|
fn register_shape(
|
|
|
|
&mut self,
|
|
|
|
shape: DistilledShape,
|
2021-06-13 21:44:16 +00:00
|
|
|
bitmap_source: &dyn BitmapSource,
|
2020-12-30 23:35:43 +00:00
|
|
|
) -> ShapeHandle;
|
|
|
|
fn replace_shape(
|
|
|
|
&mut self,
|
|
|
|
shape: DistilledShape,
|
2021-06-13 21:44:16 +00:00
|
|
|
bitmap_source: &dyn BitmapSource,
|
2020-12-30 23:35:43 +00:00
|
|
|
handle: ShapeHandle,
|
|
|
|
);
|
2019-08-26 23:38:37 +00:00
|
|
|
fn register_glyph_shape(&mut self, shape: &swf::Glyph) -> ShapeHandle;
|
2022-05-27 19:16:33 +00:00
|
|
|
|
2019-08-26 23:38:37 +00:00
|
|
|
fn register_bitmap_jpeg(
|
|
|
|
&mut self,
|
|
|
|
data: &[u8],
|
2019-11-11 21:37:22 +00:00
|
|
|
jpeg_tables: Option<&[u8]>,
|
2022-05-27 19:16:33 +00:00
|
|
|
) -> Result<BitmapInfo, Error> {
|
2022-08-13 22:27:02 +00:00
|
|
|
let data = utils::glue_tables_to_jpeg(data, jpeg_tables);
|
2022-05-27 19:16:33 +00:00
|
|
|
self.register_bitmap_jpeg_2(&data)
|
|
|
|
}
|
|
|
|
|
2022-09-06 21:38:48 +00:00
|
|
|
/// Creates a new `RenderBackend` which renders directly
|
|
|
|
/// to the texture specified by `BitmapHandle` with the given
|
|
|
|
/// `width` and `height`. This backend is passed to the callback
|
|
|
|
/// `f`, which performs the desired draw operations.
|
|
|
|
///
|
|
|
|
/// After the callback `f` exectures, the texture data is copied
|
|
|
|
/// from the GPU texture to an `RgbaImage`. There is no need to call
|
|
|
|
/// `update_texture` with the pixels from this image, as they
|
|
|
|
/// reflect data that is already stored on the GPU texture.
|
|
|
|
fn render_offscreen(
|
|
|
|
&mut self,
|
|
|
|
handle: BitmapHandle,
|
|
|
|
width: u32,
|
|
|
|
height: u32,
|
2022-09-03 17:22:57 +00:00
|
|
|
commands: CommandList,
|
|
|
|
clear_color: Color,
|
2022-09-06 21:38:48 +00:00
|
|
|
) -> Result<Bitmap, Error>;
|
|
|
|
|
2022-05-27 19:16:33 +00:00
|
|
|
fn register_bitmap_jpeg_2(&mut self, data: &[u8]) -> Result<BitmapInfo, Error> {
|
2022-08-13 22:27:02 +00:00
|
|
|
let bitmap = utils::decode_define_bits_jpeg(data, None)?;
|
2022-05-27 19:16:33 +00:00
|
|
|
let width = bitmap.width() as u16;
|
|
|
|
let height = bitmap.height() as u16;
|
|
|
|
let handle = self.register_bitmap(bitmap)?;
|
|
|
|
Ok(BitmapInfo {
|
|
|
|
handle,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-04 12:27:51 +00:00
|
|
|
fn register_bitmap_jpeg_3_or_4(
|
2019-08-26 23:38:37 +00:00
|
|
|
&mut self,
|
|
|
|
jpeg_data: &[u8],
|
|
|
|
alpha_data: &[u8],
|
2022-05-27 19:16:33 +00:00
|
|
|
) -> Result<BitmapInfo, Error> {
|
2022-08-13 22:27:02 +00:00
|
|
|
let bitmap = utils::decode_define_bits_jpeg(jpeg_data, Some(alpha_data))?;
|
2022-05-27 19:16:33 +00:00
|
|
|
let width = bitmap.width() as u16;
|
|
|
|
let height = bitmap.height() as u16;
|
|
|
|
let handle = self.register_bitmap(bitmap)?;
|
|
|
|
Ok(BitmapInfo {
|
|
|
|
handle,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-19 10:15:33 +00:00
|
|
|
fn register_bitmap_png(
|
|
|
|
&mut self,
|
|
|
|
swf_tag: &swf::DefineBitsLossless,
|
2022-05-27 19:16:33 +00:00
|
|
|
) -> Result<BitmapInfo, Error> {
|
2022-08-13 22:27:02 +00:00
|
|
|
let bitmap = utils::decode_define_bits_lossless(swf_tag)?;
|
2022-05-27 19:16:33 +00:00
|
|
|
let width = bitmap.width() as u16;
|
|
|
|
let height = bitmap.height() as u16;
|
|
|
|
let handle = self.register_bitmap(bitmap)?;
|
|
|
|
Ok(BitmapInfo {
|
|
|
|
handle,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
})
|
|
|
|
}
|
2019-08-26 23:38:37 +00:00
|
|
|
|
2022-09-03 17:22:57 +00:00
|
|
|
fn submit_frame(&mut self, clear: swf::Color, commands: CommandList);
|
2022-07-25 16:31:40 +00:00
|
|
|
|
2020-11-01 18:43:15 +00:00
|
|
|
fn get_bitmap_pixels(&mut self, bitmap: BitmapHandle) -> Option<Bitmap>;
|
2022-05-27 19:16:33 +00:00
|
|
|
fn register_bitmap(&mut self, bitmap: Bitmap) -> Result<BitmapHandle, Error>;
|
2022-06-29 22:16:43 +00:00
|
|
|
// Frees memory used by the bitmap. After this call, `handle` can no longer
|
|
|
|
// be used.
|
2022-08-18 23:40:21 +00:00
|
|
|
fn unregister_bitmap(&mut self, handle: BitmapHandle);
|
2020-12-09 18:38:41 +00:00
|
|
|
fn update_texture(
|
|
|
|
&mut self,
|
|
|
|
bitmap: BitmapHandle,
|
|
|
|
width: u32,
|
|
|
|
height: u32,
|
|
|
|
rgba: Vec<u8>,
|
|
|
|
) -> Result<BitmapHandle, Error>;
|
2019-08-26 23:38:37 +00:00
|
|
|
}
|
2020-05-11 07:02:49 +00:00
|
|
|
impl_downcast!(RenderBackend);
|
2019-08-26 23:38:37 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct ShapeHandle(pub usize);
|
2022-08-04 05:50:18 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct ViewportDimensions {
|
|
|
|
/// The dimensions of the stage's containing viewport.
|
|
|
|
pub width: u32,
|
|
|
|
pub height: u32,
|
|
|
|
|
|
|
|
/// The scale factor of the containing viewport from standard-size pixels
|
|
|
|
/// to device-scale pixels.
|
|
|
|
pub scale_factor: f64,
|
|
|
|
}
|