feat: Turn RenderingContext into a trait (#35052)

* feat: turn RenderingContext into a trait

Signed-off-by: DK Liao <dklassic@gmail.com>

Add comment and handle drop error

Signed-off-by: DK Liao <dklassic@gmail.com>

Add doc comment

Signed-off-by: DK Liao <dklassic@gmail.com>

Allow make_current to propagate Error

Signed-off-by: DK Liao <dklassic@gmail.com>

Fix all make_current occurances

Signed-off-by: DK Liao <dklassic@gmail.com>

Move back to Rc<dyn RenderingContext>

Signed-off-by: DK Liao <dklassic@gmail.com>

fix android/ohos

Signed-off-by: DK Liao <dklassic@gmail.com>

Fix build

Signed-off-by: DK Liao <dklassic@gmail.com>

fix android/ohos again

Signed-off-by: DK Liao <dklassic@gmail.com>

* Fix macOS smoke test

Signed-off-by: DK Liao <dklassic@gmail.com>

* Add comment for create_texture and destroy_texture

Signed-off-by: DK Liao <dklassic@gmail.com>

* Improve comments written

Signed-off-by: DK Liao <dklassic@gmail.com>

---------

Signed-off-by: DK Liao <dklassic@gmail.com>
This commit is contained in:
DK Liao 2025-01-24 10:39:39 +08:00 committed by GitHub
parent ac22157491
commit a0eceee2ed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 236 additions and 112 deletions

View file

@ -96,9 +96,10 @@ pub use webgpu;
use webgpu::swapchain::WGPUImageMap;
use webrender::{RenderApiSender, ShaderPrecacheFlags, UploadMethod, ONE_TIME_USAGE_HINT};
use webrender_api::{ColorF, DocumentId, FramePublishId};
use webrender_traits::rendering_context::RenderingContext;
use webrender_traits::{
CrossProcessCompositorApi, RenderingContext, WebrenderExternalImageHandlers,
WebrenderExternalImageRegistry, WebrenderImageHandlerType,
CrossProcessCompositorApi, WebrenderExternalImageHandlers, WebrenderExternalImageRegistry,
WebrenderImageHandlerType,
};
pub use {
background_hang_monitor, base, bluetooth, bluetooth_traits, canvas, canvas_traits, compositing,
@ -236,7 +237,7 @@ where
pub fn new(
opts: Opts,
preferences: Preferences,
rendering_context: RenderingContext,
rendering_context: Rc<dyn RenderingContext>,
mut embedder: Box<dyn EmbedderMethods>,
window: Rc<Window>,
user_agent: Option<String>,
@ -282,23 +283,16 @@ where
};
// Get GL bindings
let webrender_gl = match rendering_context.connection().gl_api() {
GLApi::GL => unsafe { gl::GlFns::load_with(|s| rendering_context.get_proc_address(s)) },
GLApi::GLES => unsafe {
gl::GlesFns::load_with(|s| rendering_context.get_proc_address(s))
},
};
let webrender_gl = rendering_context.gl_api();
// Make sure the gl context is made current.
rendering_context.make_gl_context_current().unwrap();
if let Err(err) = rendering_context.make_current() {
warn!("Failed to make the rendering context current: {:?}", err);
}
debug_assert_eq!(webrender_gl.get_error(), gleam::gl::NO_ERROR,);
// Bind the webrender framebuffer
let framebuffer_object = rendering_context
.context_surface_info()
.unwrap_or(None)
.map(|info| info.framebuffer_object)
.unwrap_or(0);
let framebuffer_object = rendering_context.framebuffer_object();
webrender_gl.bind_framebuffer(gleam::gl::FRAMEBUFFER, framebuffer_object);
// Reserving a namespace to create TopLevelBrowsingContextId.
@ -539,9 +533,9 @@ where
#[cfg(all(target_os = "linux", not(target_env = "ohos")))]
fn get_native_media_display_and_gl_context(
rendering_context: &RenderingContext,
rendering_context: &Rc<dyn RenderingContext>,
) -> Option<(NativeDisplay, GlContext)> {
let gl_context = match rendering_context.native_context() {
let gl_context = match rendering_context.context() {
NativeContext::Default(LinuxNativeContext::Default(native_context)) => {
GlContext::Egl(native_context.egl_context as usize)
},
@ -566,14 +560,13 @@ where
// @TODO(victor): https://github.com/servo/media/pull/315
#[cfg(target_os = "windows")]
fn get_native_media_display_and_gl_context(
rendering_context: &RenderingContext,
rendering_context: &Rc<dyn RenderingContext>,
) -> Option<(NativeDisplay, GlContext)> {
#[cfg(feature = "no-wgl")]
{
let gl_context =
GlContext::Egl(rendering_context.native_context().egl_context as usize);
let gl_context = GlContext::Egl(rendering_context.context().egl_context as usize);
let native_display =
NativeDisplay::Egl(rendering_context.native_device().egl_display as usize);
NativeDisplay::Egl(rendering_context.device().egl_display as usize);
Some((native_display, gl_context))
}
#[cfg(not(feature = "no-wgl"))]
@ -585,7 +578,7 @@ where
all(target_os = "linux", not(target_env = "ohos"))
)))]
fn get_native_media_display_and_gl_context(
_rendering_context: &RenderingContext,
_rendering_context: &Rc<dyn RenderingContext>,
) -> Option<(NativeDisplay, GlContext)> {
None
}
@ -593,7 +586,7 @@ where
fn create_media_window_gl_context(
external_image_handlers: &mut WebrenderExternalImageHandlers,
external_images: Arc<Mutex<WebrenderExternalImageRegistry>>,
rendering_context: &RenderingContext,
rendering_context: &Rc<dyn RenderingContext>,
) -> (WindowGLContext, Option<GLPlayerThreads>) {
if !pref!(media_glvideo_enabled) {
return (
@ -622,10 +615,8 @@ where
);
},
};
let api = rendering_context.connection().gl_api();
let attributes = rendering_context.context_attributes();
let GLVersion { major, minor } = attributes.version;
let GLVersion { major, minor } = rendering_context.gl_version();
let gl_api = match api {
GLApi::GL if major >= 3 && minor >= 2 => GlApi::OpenGL3,
GLApi::GL => GlApi::OpenGL,