Skip to content

Commit

Permalink
Add channel_index in ExternalImageHandler callback, ExternalImageData…
Browse files Browse the repository at this point in the history
… and UpdateForExternalBuffer.

In some video image formats(e.g. yuv or nv12), we could have more than one
channel data for that video image. Currently, we use different external image id
for each channel data. This patch add one additional channel_index data member
in ExternalImageData. Then, we could use this channel_index to select the
specific channel data with the same ExternalImageId.

Example:
Add 2 chanel image into WR.
api.add_image(...
              ImageData::External(ExternalImageData {
                                    id: ExternalImageId(external_image_id),
                                    channel_index: 0,
                                    ...
                                  }),
              ...);
api.add_image(...
              ImageData::External(ExternalImageData {
                                    id: ExternalImageId(external_image_id),
                                    channel_index: 1,
                                    ...
                                  }),
              ...);
  • Loading branch information
JerryShih committed Apr 17, 2017
1 parent 79f14c9 commit 95f0e56
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
1 change: 1 addition & 0 deletions webrender/src/internal_types.rs
Expand Up @@ -284,6 +284,7 @@ pub enum TextureUpdateOp {
UpdateForExternalBuffer {
rect: DeviceUintRect,
id: ExternalImageId,
channel_index: u8,
stride: Option<u32>,
},
Grow {
Expand Down
26 changes: 13 additions & 13 deletions webrender/src/renderer.rs
Expand Up @@ -535,7 +535,7 @@ pub struct Renderer {
external_image_handler: Option<Box<ExternalImageHandler>>,

/// Map of external image IDs to native textures.
external_images: HashMap<ExternalImageId, TextureId, BuildHasherDefault<FnvHasher>>,
external_images: HashMap<(ExternalImageId, u8), TextureId, BuildHasherDefault<FnvHasher>>,

// Optional trait object that handles WebVR commands.
// Some WebVR commands such as SubmitFrame must be synced with the WebGL render thread.
Expand Down Expand Up @@ -1068,7 +1068,7 @@ impl Renderer {
SourceTexture::WebGL(id) => TextureId::new(id, TextureTarget::Default),
SourceTexture::External(external_image) => {
*self.external_images
.get(&external_image.id)
.get(&(external_image.id, external_image.channel_index))
.expect("BUG: External image should be resolved by now!")
}
SourceTexture::TextureCache(index) => {
Expand Down Expand Up @@ -1226,7 +1226,7 @@ impl Renderer {
.as_mut()
.expect("Found external image, but no handler set!");

match handler.lock(ext_image.id).source {
match handler.lock(ext_image.id, ext_image.channel_index).source {
ExternalImageSource::RawData(raw) => {
self.device.init_texture(texture_id,
width,
Expand All @@ -1238,7 +1238,7 @@ impl Renderer {
}
_ => panic!("No external buffer found"),
};
handler.unlock(ext_image.id);
handler.unlock(ext_image.id, ext_image.channel_index);
}
_ => {
panic!("External texture handle should not use TextureUpdateOp::Create.");
Expand Down Expand Up @@ -1276,14 +1276,14 @@ impl Renderer {
width, height, stride,
&data[offset as usize..]);
}
TextureUpdateOp::UpdateForExternalBuffer { rect, id, stride } => {
TextureUpdateOp::UpdateForExternalBuffer { rect, id, channel_index, stride } => {
let handler = self.external_image_handler
.as_mut()
.expect("Found external image, but no handler set!");
let device = &mut self.device;
let cached_id = self.cache_texture_id_map[update.id.0];

match handler.lock(id).source {
match handler.lock(id, channel_index).source {
ExternalImageSource::RawData(data) => {
device.update_texture(cached_id,
rect.origin.x,
Expand All @@ -1294,7 +1294,7 @@ impl Renderer {
}
_ => panic!("No external buffer found"),
};
handler.unlock(id);
handler.unlock(id, channel_index);
}
TextureUpdateOp::Free => {
let texture_id = self.cache_texture_id_map[update.id.0];
Expand Down Expand Up @@ -1707,7 +1707,7 @@ impl Renderer {
let props = &deferred_resolve.image_properties;
let ext_image = props.external_image
.expect("BUG: Deferred resolves must be external images!");
let image = handler.lock(ext_image.id);
let image = handler.lock(ext_image.id, ext_image.channel_index);
let texture_target = match ext_image.image_type {
ExternalImageType::Texture2DHandle => TextureTarget::Default,
ExternalImageType::TextureRectHandle => TextureTarget::Rect,
Expand All @@ -1722,7 +1722,7 @@ impl Renderer {
_ => panic!("No native texture found."),
};

self.external_images.insert(ext_image.id, texture_id);
self.external_images.insert((ext_image.id, ext_image.channel_index), texture_id);
let resource_rect_index = deferred_resolve.resource_address.0 as usize;
let resource_rect = &mut frame.gpu_resource_rects[resource_rect_index];
resource_rect.uv0 = DevicePoint::new(image.u0, image.v0);
Expand All @@ -1737,8 +1737,8 @@ impl Renderer {
.as_mut()
.expect("Found external image, but no handler set!");

for (external_id, _) in self.external_images.drain() {
handler.unlock(external_id);
for (ext_data, _) in self.external_images.drain() {
handler.unlock(ext_data.0, ext_data.1);
}
}
}
Expand Down Expand Up @@ -1987,10 +1987,10 @@ pub trait ExternalImageHandler {
/// Lock the external image. Then, WR could start to read the image content.
/// The WR client should not change the image content until the unlock()
/// call.
fn lock(&mut self, key: ExternalImageId) -> ExternalImage;
fn lock(&mut self, key: ExternalImageId, channel_index: u8) -> ExternalImage;
/// Unlock the external image. The WR should not read the image content
/// after this call.
fn unlock(&mut self, key: ExternalImageId);
fn unlock(&mut self, key: ExternalImageId, channel_index: u8);
}

pub struct RendererOptions {
Expand Down
1 change: 1 addition & 0 deletions webrender/src/texture_cache.rs
Expand Up @@ -796,6 +796,7 @@ impl TextureCache {
op: TextureUpdateOp::UpdateForExternalBuffer {
rect: result.item.allocated_rect,
id: ext_image.id,
channel_index: ext_image.channel_index,
stride: stride,
},
};
Expand Down
1 change: 1 addition & 0 deletions webrender_traits/src/image.rs
Expand Up @@ -32,6 +32,7 @@ pub enum ExternalImageType {
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct ExternalImageData {
pub id: ExternalImageId,
pub channel_index: u8,
pub image_type: ExternalImageType,
}

Expand Down

0 comments on commit 95f0e56

Please sign in to comment.