Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support external image texture/buffer #547

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions webrender/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use time::precise_time_ns;
use util::TransformedRectKind;
use webrender_traits::{ColorF, Epoch, PipelineId, RenderNotifier, RenderDispatcher};
use webrender_traits::{ImageFormat, RenderApiSender, RendererKind};
use webrender_traits::{ExternalImage, ExternalImageCallback, ExternalImageKey};

pub const BLUR_INFLATION_FACTOR: u32 = 3;
pub const MAX_RASTER_OP_SIZE: u32 = 2048;
Expand Down Expand Up @@ -368,6 +369,10 @@ pub struct Renderer {
/// Used to dispatch functions to the main thread's event loop.
/// Required to allow GLContext sharing in some implementations like WGL.
main_thread_dispatcher: Arc<Mutex<Option<Box<RenderDispatcher>>>>,

// Used for accessing the external texture/buffer through FFI.
// E.g. We could use this callback function to access the texture/buffer in C++.
external_image_callback: Option<ExternalImageCallback>,
}

impl Renderer {
Expand Down Expand Up @@ -698,6 +703,7 @@ impl Renderer {
data128_texture: data128_texture,
pipeline_epoch_map: HashMap::with_hasher(Default::default()),
main_thread_dispatcher: main_thread_dispatcher,
external_image_callback: options.external_image_callback
};

renderer.update_uniform_locations();
Expand Down Expand Up @@ -1687,4 +1693,5 @@ pub struct RendererOptions {
pub precache_shaders: bool,
pub renderer_kind: RendererKind,
pub enable_subpixel_aa: bool,
pub external_image_callback: Option<ExternalImageCallback>,
}
3 changes: 1 addition & 2 deletions webrender_traits/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ impl RenderApi {
stride: Option<u32>,
format: ImageFormat,
bytes: Vec<u8>) -> ImageKey {
let new_id = self.next_unique_id();
let key = ImageKey::new(new_id.0, new_id.1);
let key = self.alloc_image();
let msg = ApiMsg::AddImage(key, width, height, stride, format, bytes);
self.api_sender.send(msg).unwrap();
key
Expand Down
52 changes: 52 additions & 0 deletions webrender_traits/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,3 +743,55 @@ pub enum WebGLShaderParameter {
Bool(bool),
Invalid,
}

#[repr(C)]
pub struct ExternalImageKey(pub u32);

pub enum ExternalImage<'i> {
TextureHandle {
handle: u32,
},
MemoryBuff {
buff: &'i [u8],
}
}

#[repr(C)]
pub enum ExternalImageType {
TEXTURE_HANDLE,
MEM_OR_SHMEM,
}

#[repr(C)]
pub struct ExternalImageStruct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

struct RgbImage {
    size: Size2D<u32>,
    stride: u32,
    format: ImageFormat,
    buffer: *const u8,
}

struct GlHandle {
     handle: u32,
}

// YuvImage, DxgiHandle, etc...

enum ExternalImage {
    RgbMemory(RgbImage),
    YuvMemory(YuvImage),
    Gl(GlHandle),
    Dxgi(DxgiHandle),
    // etc..
}

Using enums this way is a lot more idiomatic in rust, and I think that it'll make it easier to work with internally

image_type: ExternalImageType,

// external buffer handle
handle: u32,

// shmem or memory buffer
buff: *const u8,
size: usize,
}

impl ExternalImageStruct {
pub fn get_image(&self) -> ExternalImage {
match self.image_type {
ExternalImageType::TEXTURE_HANDLE =>
ExternalImage::TextureHandle { handle: self.handle },
ExternalImageType::MEM_OR_SHMEM =>
ExternalImage::MemoryBuff { buff: unsafe { std::slice::from_raw_parts(self.buff, self.size)} },
}
}
}

pub type GetExternalImageCallback = fn(ExternalImageKey) -> ExternalImageStruct;

pub type ReleaseExternalImageCallback = fn(ExternalImageKey);

#[derive(Debug, Copy, Clone)]
#[repr(C)]
pub struct ExternalImageCallback {
pub get_func: GetExternalImageCallback,
pub release_func: ReleaseExternalImageCallback,
}