libservo: Move size handling to RenderContext from WindowMethods (#35621)

This is the first step toward removing `WindowMethods`, which will
gradually be integrated into the `WebView` and `WebViewDelegate`. Sizing
of the `WebView` is now handled by the a size associated with a
`RenderingContext`. `WebView`s will eventually just paint the entire
size of their `RenderingContext`. Notes:

- This is transitionary step so now there is a `WebView::resize` and a
  `WebView::move_resize`. The first is the future which will resize the
  `WebView` and its associated `RenderingContext`. The second is a
  function that the virtual `WebView`s that will soon be replaced by a
  the one-`WebView` per `WebView` model.
- We do not need to call `WebView::move_resize` at as much any longer
  because the default size of the `WebView` is to take up the whole
  `RenderingContext`.
- `SurfmanRenderingContext` is no longer exposed in the API, as a
  surfman context doesn't naturally have a size unless a surface is
  bound to it.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-02-25 16:03:53 +01:00 committed by GitHub
parent ebb19bcd60
commit 23524a5413
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 200 additions and 312 deletions

View file

@ -7,14 +7,14 @@ use std::collections::HashMap;
use std::path::PathBuf;
use std::rc::Rc;
use euclid::Vector2D;
use euclid::{Point2D, Vector2D};
use image::{DynamicImage, ImageFormat};
use keyboard_types::{Key, KeyboardEvent, Modifiers, ShortcutMatcher};
use log::{error, info};
use servo::base::id::WebViewId;
use servo::config::pref;
use servo::ipc_channel::ipc::IpcSender;
use servo::webrender_api::units::{DeviceIntPoint, DeviceIntSize};
use servo::webrender_api::units::{DeviceIntPoint, DeviceIntRect, DeviceIntSize};
use servo::webrender_api::ScrollLocation;
use servo::{
AllowOrDenyRequest, AuthenticationRequest, FilterPattern, GamepadHapticEffectType, LoadStatus,
@ -130,13 +130,8 @@ impl RunningAppState {
};
let inner = self.inner();
let viewport_rect = inner
.window
.get_coordinates()
.viewport
.to_rect()
.to_untyped()
.to_u32();
let size = inner.window.rendering_context().size2d().to_i32();
let viewport_rect = DeviceIntRect::from_origin_and_size(Point2D::origin(), size);
let Some(image) = inner
.window
.rendering_context()
@ -467,17 +462,8 @@ impl WebViewDelegate for RunningAppState {
}
fn notify_ready_to_show(&self, webview: servo::WebView) {
let rect = self
.inner()
.window
.get_coordinates()
.get_viewport()
.to_f32();
webview.focus();
webview.move_resize(rect);
webview.raise_to_top(true);
webview.notify_rendering_context_resized();
}
fn notify_closed(&self, webview: servo::WebView) {

View file

@ -121,7 +121,7 @@ impl Window {
.window_handle()
.expect("could not get window handle from window");
let window_rendering_context = Rc::new(
WindowRenderingContext::new(display_handle, window_handle, &inner_size)
WindowRenderingContext::new(display_handle, window_handle, inner_size)
.expect("Could not create RenderingContext for Window"),
);
@ -135,9 +135,7 @@ impl Window {
// Make sure the gl context is made current.
window_rendering_context.make_current().unwrap();
let rendering_context_size = Size2D::new(inner_size.width, inner_size.height);
let rendering_context =
Rc::new(window_rendering_context.offscreen_context(rendering_context_size));
let rendering_context = Rc::new(window_rendering_context.offscreen_context(inner_size));
debug!("Created window {:?}", winit_window.id());
Window {
@ -614,11 +612,8 @@ impl WindowPortsMethods for Window {
},
WindowEvent::Resized(new_size) => {
if self.inner_size.get() != new_size {
let rendering_context_size = Size2D::new(new_size.width, new_size.height);
self.window_rendering_context
.resize(rendering_context_size.to_i32());
self.window_rendering_context.resize(new_size);
self.inner_size.set(new_size);
webview.notify_rendering_context_resized();
}
},
WindowEvent::ThemeChanged(theme) => {
@ -736,17 +731,9 @@ impl WindowMethods for Window {
let window_scale: Scale<f64, DeviceIndependentPixel, DevicePixel> =
Scale::new(self.winit_window.scale_factor());
let window_rect = (window_rect.to_f64() / window_scale).to_i32();
let viewport_origin = DeviceIntPoint::zero(); // bottom left
let mut viewport_size = winit_size_to_euclid_size(self.winit_window.inner_size()).to_f32();
viewport_size.height -= (self.toolbar_height() * self.hidpi_factor()).0;
let viewport = DeviceIntRect::from_origin_and_size(viewport_origin, viewport_size.to_i32());
let screen_size = self.screen_size.to_i32();
EmbedderCoordinates {
viewport,
framebuffer: viewport.size(),
window_rect,
screen_size,
// FIXME: Winit doesn't have API for available size. Fallback to screen size

View file

@ -86,7 +86,7 @@ impl WindowPortsMethods for Window {
// Because we are managing the rendering surface ourselves, there will be no other
// notification (such as from the display manager) that it has changed size, so we
// must notify the compositor here.
webview.notify_rendering_context_resized();
webview.resize(PhysicalSize::new(size.width as u32, size.height as u32));
Some(new_size)
}
@ -149,10 +149,7 @@ impl WindowPortsMethods for Window {
impl WindowMethods for Window {
fn get_coordinates(&self) -> EmbedderCoordinates {
let inner_size = self.inner_size.get();
EmbedderCoordinates {
viewport: Box2D::from_origin_and_size(Point2D::zero(), inner_size),
framebuffer: inner_size,
window_rect: self.window_rect,
screen_size: self.screen_size,
available_screen_size: self.screen_size,

View file

@ -7,6 +7,7 @@ use std::rc::Rc;
use std::sync::Arc;
use std::time::Instant;
use dpi::PhysicalSize;
use egui::text::{CCursor, CCursorRange};
use egui::text_edit::TextEditState;
use egui::{
@ -379,13 +380,11 @@ impl Minibrowser {
// If the top parts of the GUI changed size, then update the size of the WebView and also
// the size of its RenderingContext.
let available_size = ui.available_size();
let rect = Box2D::from_origin_and_size(
Point2D::origin(),
Size2D::new(available_size.x, available_size.y),
) * scale;
let size = Size2D::new(available_size.x, available_size.y) * scale;
let rect = Box2D::from_origin_and_size(Point2D::origin(), size);
if rect != webview.rect() {
webview.move_resize(rect);
rendering_context.resize(rect.size().to_i32().to_untyped());
webview.resize(PhysicalSize::new(size.width as u32, size.height as u32))
}
let min = ui.cursor().min;

View file

@ -713,13 +713,7 @@ fn jni_coords_to_rust_coords<'local>(
let height = get_non_null_field(env, obj, "height", "I")?
.i()
.map_err(|_| "height not an int")? as i32;
let fb_width = get_non_null_field(env, obj, "fb_width", "I")?
.i()
.map_err(|_| "fb_width not an int")? as i32;
let fb_height = get_non_null_field(env, obj, "fb_height", "I")?
.i()
.map_err(|_| "fb_height not an int")? as i32;
Ok(Coordinates::new(x, y, width, height, fb_width, fb_height))
Ok(Coordinates::new(x, y, width, height))
}
fn get_field<'local>(

View file

@ -6,6 +6,7 @@ use std::cell::RefCell;
use std::mem;
use std::rc::Rc;
use dpi::PhysicalSize;
use raw_window_handle::{DisplayHandle, RawDisplayHandle, RawWindowHandle, WindowHandle};
pub use servo::webrender_api::units::DeviceIntRect;
/// The EventLoopWaker::wake function will be called from any thread.
@ -68,11 +69,13 @@ pub fn init(
WindowHandle::borrow_raw(init_opts.window_handle),
)
};
let size = init_opts.coordinates.viewport.size;
let rendering_context = Rc::new(
WindowRenderingContext::new(
display_handle,
window_handle,
&init_opts.coordinates.framebuffer_size(),
PhysicalSize::new(size.width as u32, size.height as u32),
)
.expect("Could not create RenderingContext"),
);

View file

@ -16,7 +16,7 @@ use servo::compositing::windowing::{
};
use servo::euclid::{Box2D, Point2D, Rect, Scale, Size2D, Vector2D};
use servo::servo_geometry::DeviceIndependentPixel;
use servo::webrender_api::units::{DeviceIntRect, DeviceIntSize, DevicePixel, DeviceRect};
use servo::webrender_api::units::{DeviceIntRect, DeviceIntSize, DevicePixel};
use servo::webrender_api::ScrollLocation;
use servo::{
AllowOrDenyRequest, ContextMenuResult, EmbedderProxy, EventLoopWaker, ImeEvent, InputEvent,
@ -34,30 +34,14 @@ use crate::prefs::ServoShellPreferences;
#[derive(Clone, Debug)]
pub struct Coordinates {
pub viewport: Rect<i32, DevicePixel>,
pub framebuffer: Size2D<i32, DevicePixel>,
}
impl Coordinates {
pub fn new(
x: i32,
y: i32,
width: i32,
height: i32,
fb_width: i32,
fb_height: i32,
) -> Coordinates {
pub fn new(x: i32, y: i32, width: i32, height: i32) -> Coordinates {
Coordinates {
viewport: Rect::new(Point2D::new(x, y), Size2D::new(width, height)),
framebuffer: Size2D::new(fb_width, fb_height),
}
}
pub(crate) fn framebuffer_size(&self) -> PhysicalSize<u32> {
PhysicalSize::new(
self.framebuffer.width as u32,
self.framebuffer.height as u32,
)
}
}
pub(super) struct ServoWindowCallbacks {
@ -426,14 +410,12 @@ impl RunningAppState {
/// Let Servo know that the window has been resized.
pub fn resize(&self, coordinates: Coordinates) {
info!("resize to {:?}", coordinates);
let size = coordinates.viewport.size;
self.rendering_context
.resize(Size2D::new(size.width, size.height));
info!("resize to {:?}", coordinates,);
self.active_webview().resize(PhysicalSize::new(
coordinates.viewport.width() as u32,
coordinates.viewport.height() as u32,
));
*self.callbacks.coordinates.borrow_mut() = coordinates;
self.active_webview().notify_rendering_context_resized();
self.active_webview()
.move_resize(DeviceRect::from_size(size.to_f32()));
self.perform_updates();
}
@ -633,9 +615,10 @@ impl RunningAppState {
pub fn resume_compositor(&self, window_handle: RawWindowHandle, coords: Coordinates) {
let window_handle = unsafe { WindowHandle::borrow_raw(window_handle) };
let size = coords.viewport.size.to_u32();
if let Err(e) = self
.rendering_context
.set_window(window_handle, &coords.framebuffer_size())
.set_window(window_handle, PhysicalSize::new(size.width, size.height))
{
warn!("Binding native surface to context failed ({:?})", e);
}
@ -723,8 +706,6 @@ impl WindowMethods for ServoWindowCallbacks {
let coords = self.coordinates.borrow();
let screen_size = (coords.viewport.size.to_f32() / self.hidpi_factor).to_i32();
EmbedderCoordinates {
viewport: coords.viewport.to_box2d(),
framebuffer: coords.framebuffer,
window_rect: Box2D::from_origin_and_size(Point2D::zero(), screen_size),
screen_size,
available_screen_size: screen_size,

View file

@ -184,9 +184,7 @@ impl ServoAction {
servo.notify_vsync();
servo.present_if_needed();
},
Resize { width, height } => {
servo.resize(Coordinates::new(0, 0, *width, *height, *width, *height))
},
Resize { width, height } => servo.resize(Coordinates::new(0, 0, *width, *height)),
};
}
}

View file

@ -7,6 +7,7 @@ use std::path::PathBuf;
use std::ptr::NonNull;
use std::rc::Rc;
use dpi::PhysicalSize;
use log::{debug, info};
use raw_window_handle::{
DisplayHandle, OhosDisplayHandle, OhosNdkWindowHandle, RawDisplayHandle, RawWindowHandle,
@ -66,14 +67,7 @@ pub fn init(
let Ok(window_size) = (unsafe { super::get_xcomponent_size(xcomponent, native_window) }) else {
return Err("Failed to get xcomponent size");
};
let coordinates = Coordinates::new(
0,
0,
window_size.width,
window_size.height,
window_size.width,
window_size.height,
);
let coordinates = Coordinates::new(0, 0, window_size.width, window_size.height);
let display_handle = RawDisplayHandle::Ohos(OhosDisplayHandle::new());
let display_handle = unsafe { DisplayHandle::borrow_raw(display_handle) };
@ -86,7 +80,7 @@ pub fn init(
WindowRenderingContext::new(
display_handle,
window_handle,
&coordinates.framebuffer_size(),
PhysicalSize::new(window_size.width as u32, window_size.height as u32),
)
.expect("Could not create RenderingContext"),
);