Skip to content

Commit d8bcc18

Browse files
committed
Implement WebGLContext resize
1 parent 72770bf commit d8bcc18

File tree

8 files changed

+59
-0
lines changed

8 files changed

+59
-0
lines changed

webrender/src/device.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,10 @@ impl Device {
10421042
self.raw_textures.insert(texture_id, (x0, y0, width, height));
10431043
}
10441044

1045+
pub fn remove_raw_texture(&mut self, texture_id: TextureId) {
1046+
self.raw_textures.remove(&texture_id);
1047+
}
1048+
10451049
fn set_texture_parameters(&mut self, target: gl::GLuint, filter: TextureFilter) {
10461050
let filter = match filter {
10471051
TextureFilter::Nearest => {

webrender/src/internal_types.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,17 @@ impl GLContextWrapper {
134134
}
135135
}
136136
}
137+
138+
pub fn resize(&mut self, size: &Size2D<i32>) -> Result<(), &'static str> {
139+
match *self {
140+
GLContextWrapper::Native(ref mut ctx) => {
141+
ctx.resize(*size)
142+
}
143+
GLContextWrapper::OSMesa(ref mut ctx) => {
144+
ctx.resize(*size)
145+
}
146+
}
147+
}
137148
}
138149

139150
pub type DeviceRect = TypedRect<i32, DevicePixel>;
@@ -338,6 +349,7 @@ pub enum TextureUpdateOp {
338349
Create(u32, u32, ImageFormat, TextureFilter, RenderTargetMode, Option<Vec<u8>>),
339350
Update(u32, u32, u32, u32, TextureUpdateDetails),
340351
Grow(u32, u32, ImageFormat, TextureFilter, RenderTargetMode),
352+
Remove
341353
}
342354

343355
pub struct TextureUpdate {

webrender/src/render_backend.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,21 @@ impl RenderBackend {
303303
tx.send(Err("Not implemented yet".to_owned())).unwrap();
304304
}
305305
}
306+
ApiMsg::ResizeWebGLContext(context_id, size) => {
307+
let ctx = self.webgl_contexts.get_mut(&context_id).unwrap();
308+
ctx.make_current();
309+
match ctx.resize(&size) {
310+
Ok(_) => {
311+
// Update webgl texture size. Texture id may change too.
312+
let (real_size, texture_id, _) = ctx.get_info();
313+
self.resource_cache
314+
.update_webgl_texture(context_id, TextureId::new(texture_id), real_size);
315+
},
316+
Err(msg) => {
317+
error!("Error resizing WebGLContext: {}", msg);
318+
}
319+
}
320+
}
306321
ApiMsg::WebGLCommand(context_id, command) => {
307322
// TODO: Buffer the commands and only apply them here if they need to
308323
// be synchronous.

webrender/src/renderer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,9 @@ impl Renderer {
10331033
}
10341034
}
10351035
}
1036+
TextureUpdateOp::Remove => {
1037+
self.device.remove_raw_texture(update.id);
1038+
}
10361039
}
10371040
}
10381041
}

webrender/src/resource_cache.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@ impl ResourceCache {
216216
self.texture_cache.add_raw_update(texture_id, size);
217217
}
218218

219+
pub fn update_webgl_texture(&mut self, id: WebGLContextId, texture_id: TextureId, size: Size2D<i32>) {
220+
let prev_texture_id = *self.webgl_textures.get(&id).unwrap();
221+
222+
// Remove existing cache if texture id has changed
223+
if prev_texture_id != texture_id {
224+
self.texture_cache.add_raw_remove(prev_texture_id);
225+
}
226+
// Update new texture id and size
227+
self.webgl_textures.insert(id, texture_id);
228+
self.texture_cache.add_raw_update(texture_id, size);
229+
}
230+
219231
pub fn add_resource_list(&mut self, resource_list: &ResourceList, frame_id: FrameId) {
220232
// Update texture cache with any images that aren't yet uploaded to GPU.
221233
resource_list.for_each_image(|image_key, image_rendering| {

webrender/src/texture_cache.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,13 @@ impl TextureCache {
779779
})
780780
}
781781

782+
pub fn add_raw_remove(&mut self, id: TextureId) {
783+
self.pending_updates.push(TextureUpdate {
784+
id: id,
785+
op: TextureUpdateOp::Remove
786+
});
787+
}
788+
782789
pub fn update(&mut self,
783790
image_id: TextureCacheItemId,
784791
width: u32,

webrender_traits/src/api.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ impl RenderApi {
229229
rx.recv().unwrap()
230230
}
231231

232+
pub fn resize_webgl_context(&self, context_id: WebGLContextId, size: &Size2D<i32>) {
233+
let msg = ApiMsg::ResizeWebGLContext(context_id, *size);
234+
self.api_sender.send(msg).unwrap();
235+
}
236+
232237
pub fn send_webgl_command(&self, context_id: WebGLContextId, command: WebGLCommand) {
233238
let msg = ApiMsg::WebGLCommand(context_id, command);
234239
self.api_sender.send(msg).unwrap();

webrender_traits/src/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub enum ApiMsg {
5555
TranslatePointToLayerSpace(Point2D<f32>, IpcSender<(Point2D<f32>, PipelineId)>),
5656
GetScrollLayerState(IpcSender<Vec<ScrollLayerState>>),
5757
RequestWebGLContext(Size2D<i32>, GLContextAttributes, IpcSender<Result<(WebGLContextId, GLLimits), String>>),
58+
ResizeWebGLContext(WebGLContextId, Size2D<i32>),
5859
WebGLCommand(WebGLContextId, WebGLCommand),
5960
}
6061

0 commit comments

Comments
 (0)