Auto merge of #26191 - jdm:webgl2-origin-trial, r=Manishearth

Automatically enable WebGL 2 for specific domains.

Our WebGL 2 implementation has progressed to the point where some but not all content works well. One particular example of non-working content is Babylon.js; if the WebGL2RenderingContext interface is present, it automatically tries to use it and causes panics in Servo. However, there are demos like http://www.servoexperiments.com/webxr-particles/ that work fine today and would be great to feature on our HoloLens homepage.

This change adds what is effectively an origin trial, where we can identify content that works well, ensure it's running in a list of blessed hosts, and reap the benefits.
This commit is contained in:
bors-servo 2020-04-15 20:25:43 -04:00 committed by GitHub
commit f8db6666e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 4 deletions

View file

@ -41,7 +41,6 @@ use js::error::throw_type_error;
use js::rust::HandleValue;
use profile_traits::ipc;
use script_layout_interface::{HTMLCanvasData, HTMLCanvasDataSource};
use servo_config::pref;
use style::attr::{AttrValue, LengthOrPercentageOrAuto};
const DEFAULT_WIDTH: u32 = 300;
@ -222,7 +221,8 @@ impl HTMLCanvasElement {
cx: JSContext,
options: HandleValue,
) -> Option<DomRoot<WebGL2RenderingContext>> {
if !pref!(dom.webgl2.enabled) {
if !WebGL2RenderingContext::is_webgl2_enabled(cx, self.global().reflector().get_jsobject())
{
return None;
}
if let Some(ctx) = self.context() {

View file

@ -16,6 +16,7 @@ use crate::dom::bindings::reflector::DomObject;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot, LayoutDom, MutNullableDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::globalscope::GlobalScope;
use crate::dom::htmlcanvaselement::HTMLCanvasElement;
use crate::dom::htmliframeelement::HTMLIFrameElement;
use crate::dom::webglactiveinfo::WebGLActiveInfo;
@ -50,12 +51,14 @@ use ipc_channel::ipc;
use js::jsapi::{JSObject, Type};
use js::jsval::{BooleanValue, DoubleValue, Int32Value, UInt32Value};
use js::jsval::{JSVal, NullValue, ObjectValue, UndefinedValue};
use js::rust::CustomAutoRooterGuard;
use js::rust::{CustomAutoRooterGuard, HandleObject};
use js::typedarray::{ArrayBufferView, CreateWith, Float32, Int32Array, Uint32, Uint32Array};
use script_layout_interface::HTMLCanvasDataSource;
use servo_config::pref;
use std::cell::Cell;
use std::cmp;
use std::ptr::{self, NonNull};
use url::Host;
#[unrooted_must_root_lint::must_root]
#[derive(JSTraceable, MallocSizeOf)]
@ -178,8 +181,26 @@ impl WebGL2RenderingContext {
WebGL2RenderingContext::new_inherited(window, canvas, size, attrs)
.map(|ctx| reflect_dom_object(Box::new(ctx), window))
}
#[allow(unsafe_code)]
pub fn is_webgl2_enabled(_cx: JSContext, global: HandleObject) -> bool {
if pref!(dom.webgl2.enabled) {
return true;
}
let global = unsafe { GlobalScope::from_object(global.get()) };
let origin = global.origin();
let host = origin.host();
WEBGL2_ORIGINS
.iter()
.any(|origin| host == Host::parse(origin).ok().as_ref())
}
}
/// List of domains for which WebGL 2 is enabled automatically, regardless
/// of the status of the dom.webgl2.enabled preference.
static WEBGL2_ORIGINS: &[&str] = &["www.servoexperiments.com"];
impl WebGL2RenderingContext {
pub fn recreate(&self, size: Size2D<u32>) {
self.base.recreate(size)

View file

@ -542,7 +542,7 @@ interface mixin WebGL2RenderingContextBase
void bindVertexArray(WebGLVertexArrayObject? array);
};
[Exposed=Window, Pref="dom.webgl2.enabled"]
[Exposed=Window, Func="WebGL2RenderingContext::is_webgl2_enabled"]
interface WebGL2RenderingContext
{
};