Skip to content

Implement WebGLContext resize #519

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

Merged
merged 1 commit into from
Nov 3, 2016
Merged
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
4 changes: 4 additions & 0 deletions webrender/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,10 @@ impl Device {
self.raw_textures.insert(texture_id, (x0, y0, width, height));
}

pub fn remove_raw_texture(&mut self, texture_id: TextureId) {
self.raw_textures.remove(&texture_id);
}

fn set_texture_parameters(&mut self, target: gl::GLuint, filter: TextureFilter) {
let filter = match filter {
TextureFilter::Nearest => {
Expand Down
12 changes: 12 additions & 0 deletions webrender/src/internal_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ impl GLContextWrapper {
}
}
}

pub fn resize(&mut self, size: &Size2D<i32>) -> Result<(), &'static str> {
match *self {
GLContextWrapper::Native(ref mut ctx) => {
ctx.resize(*size)
}
GLContextWrapper::OSMesa(ref mut ctx) => {
ctx.resize(*size)
}
}
}
}

pub type DeviceRect = TypedRect<i32, DevicePixel>;
Expand Down Expand Up @@ -338,6 +349,7 @@ pub enum TextureUpdateOp {
Create(u32, u32, ImageFormat, TextureFilter, RenderTargetMode, Option<Vec<u8>>),
Update(u32, u32, u32, u32, TextureUpdateDetails),
Grow(u32, u32, ImageFormat, TextureFilter, RenderTargetMode),
Remove
}

pub struct TextureUpdate {
Expand Down
15 changes: 15 additions & 0 deletions webrender/src/render_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,21 @@ impl RenderBackend {
tx.send(Err("Not implemented yet".to_owned())).unwrap();
}
}
ApiMsg::ResizeWebGLContext(context_id, size) => {
let ctx = self.webgl_contexts.get_mut(&context_id).unwrap();
ctx.make_current();
match ctx.resize(&size) {
Ok(_) => {
// Update webgl texture size. Texture id may change too.
let (real_size, texture_id, _) = ctx.get_info();
self.resource_cache
.update_webgl_texture(context_id, TextureId::new(texture_id), real_size);
},
Err(msg) => {
error!("Error resizing WebGLContext: {}", msg);
}
}
}
ApiMsg::WebGLCommand(context_id, command) => {
// TODO: Buffer the commands and only apply them here if they need to
// be synchronous.
Expand Down
3 changes: 3 additions & 0 deletions webrender/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,9 @@ impl Renderer {
}
}
}
TextureUpdateOp::Remove => {
self.device.remove_raw_texture(update.id);
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions webrender/src/resource_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ impl ResourceCache {
self.texture_cache.add_raw_update(texture_id, size);
}

pub fn update_webgl_texture(&mut self, id: WebGLContextId, texture_id: TextureId, size: Size2D<i32>) {
let prev_texture_id = *self.webgl_textures.get(&id).unwrap();

// Remove existing cache if texture id has changed
if prev_texture_id != texture_id {
self.texture_cache.add_raw_remove(prev_texture_id);
}
// Update new texture id and size
self.webgl_textures.insert(id, texture_id);
self.texture_cache.add_raw_update(texture_id, size);
}

pub fn add_resource_list(&mut self, resource_list: &ResourceList, frame_id: FrameId) {
// Update texture cache with any images that aren't yet uploaded to GPU.
resource_list.for_each_image(|image_key, image_rendering| {
Expand Down
7 changes: 7 additions & 0 deletions webrender/src/texture_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,13 @@ impl TextureCache {
})
}

pub fn add_raw_remove(&mut self, id: TextureId) {
Copy link
Member

Choose a reason for hiding this comment

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

add_raw_remove sounds a bit weird, though it follows the convention. I can't think of an immediately better name now, so I guess this is ok.

Copy link
Member

Choose a reason for hiding this comment

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

yeah, it does follow the convention. Perhaps, the convention should be revised? as a separate PR

self.pending_updates.push(TextureUpdate {
id: id,
op: TextureUpdateOp::Remove
});
}

pub fn update(&mut self,
image_id: TextureCacheItemId,
width: u32,
Expand Down
5 changes: 5 additions & 0 deletions webrender_traits/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ impl RenderApi {
rx.recv().unwrap()
}

pub fn resize_webgl_context(&self, context_id: WebGLContextId, size: &Size2D<i32>) {
let msg = ApiMsg::ResizeWebGLContext(context_id, *size);
self.api_sender.send(msg).unwrap();
}

pub fn send_webgl_command(&self, context_id: WebGLContextId, command: WebGLCommand) {
let msg = ApiMsg::WebGLCommand(context_id, command);
self.api_sender.send(msg).unwrap();
Expand Down
1 change: 1 addition & 0 deletions webrender_traits/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub enum ApiMsg {
TranslatePointToLayerSpace(Point2D<f32>, IpcSender<(Point2D<f32>, PipelineId)>),
GetScrollLayerState(IpcSender<Vec<ScrollLayerState>>),
RequestWebGLContext(Size2D<i32>, GLContextAttributes, IpcSender<Result<(WebGLContextId, GLLimits), String>>),
ResizeWebGLContext(WebGLContextId, Size2D<i32>),
WebGLCommand(WebGLContextId, WebGLCommand),
}

Expand Down