mirror of
https://github.com/servo/servo.git
synced 2025-07-22 23:03:42 +01:00
Auto merge of #6183 - ecoal95:webglcontextattributes, r=nox
r? @jdm I couldn't add the `getContextAttributes` method since `CodegenRust` doesn't know how to return a dictionary value, I'll take a look at it ASAP. I think the helper functions can return directly the renderer, since they're used just for that, but I wanted to hear your opinions about this. By the way I'm interested in adding more serious tests for WebGL, and I think the [khronos conformance suit](https://github.com/KhronosGroup/WebGL/tree/master/conformance-suites/1.0.3) should be the best option. Should I try to integrate it in wpt, or making a `tests/webgl` directory (or similar) inside the servo tree? (Maybe this question should be for @Ms2ger) <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/6183) <!-- Reviewable:end -->
This commit is contained in:
commit
0de09b936e
16 changed files with 254 additions and 80 deletions
|
@ -28,11 +28,10 @@ pub struct WebGLPaintTask {
|
|||
unsafe impl Send for WebGLPaintTask {}
|
||||
|
||||
impl WebGLPaintTask {
|
||||
fn new(size: Size2D<i32>) -> Result<WebGLPaintTask, &'static str> {
|
||||
// TODO(ecoal95): Get the GLContextAttributes from the `GetContext` call
|
||||
fn new(size: Size2D<i32>, attrs: GLContextAttributes) -> Result<WebGLPaintTask, &'static str> {
|
||||
let context = try!(
|
||||
GLContext::create_offscreen_with_color_attachment(
|
||||
size, GLContextAttributes::default(), ColorAttachmentType::TextureWithSurface));
|
||||
size, attrs, ColorAttachmentType::TextureWithSurface));
|
||||
Ok(WebGLPaintTask {
|
||||
size: size,
|
||||
original_context_size: size,
|
||||
|
@ -42,6 +41,7 @@ impl WebGLPaintTask {
|
|||
|
||||
pub fn handle_webgl_message(&self, message: CanvasWebGLMsg) {
|
||||
match message {
|
||||
CanvasWebGLMsg::GetContextAttributes(sender) => self.get_context_attributes(sender),
|
||||
CanvasWebGLMsg::AttachShader(program_id, shader_id) => self.attach_shader(program_id, shader_id),
|
||||
CanvasWebGLMsg::BindBuffer(buffer_type, buffer_id) => self.bind_buffer(buffer_type, buffer_id),
|
||||
CanvasWebGLMsg::BufferData(buffer_type, data, usage) => self.buffer_data(buffer_type, data, usage),
|
||||
|
@ -71,9 +71,9 @@ impl WebGLPaintTask {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn start(size: Size2D<i32>) -> Result<Sender<CanvasMsg>, &'static str> {
|
||||
pub fn start(size: Size2D<i32>, attrs: GLContextAttributes) -> Result<Sender<CanvasMsg>, &'static str> {
|
||||
let (chan, port) = channel::<CanvasMsg>();
|
||||
let mut painter = try!(WebGLPaintTask::new(size));
|
||||
let mut painter = try!(WebGLPaintTask::new(size, attrs));
|
||||
spawn_named("WebGLTask".to_owned(), move || {
|
||||
painter.init();
|
||||
loop {
|
||||
|
@ -98,6 +98,10 @@ impl WebGLPaintTask {
|
|||
Ok(chan)
|
||||
}
|
||||
|
||||
fn get_context_attributes(&self, sender: Sender<GLContextAttributes>) {
|
||||
sender.send(*self.gl_context.borrow_attributes()).unwrap()
|
||||
}
|
||||
|
||||
fn attach_shader(&self, program_id: u32, shader_id: u32) {
|
||||
gl::attach_shader(program_id, shader_id);
|
||||
}
|
||||
|
|
|
@ -19,5 +19,8 @@ git = "https://github.com/servo/rust-azure"
|
|||
[dependencies.layers]
|
||||
git = "https://github.com/servo/rust-layers"
|
||||
|
||||
[dependencies.offscreen_gl_context]
|
||||
git = "https://github.com/ecoal95/rust-offscreen-rendering-context"
|
||||
|
||||
[dependencies]
|
||||
cssparser = "0.3.1"
|
||||
|
|
|
@ -9,6 +9,7 @@ extern crate geom;
|
|||
extern crate cssparser;
|
||||
extern crate gfx_traits;
|
||||
extern crate layers;
|
||||
extern crate offscreen_gl_context;
|
||||
|
||||
use azure::azure::AzFloat;
|
||||
use azure::azure_hl::{DrawTarget, Pattern, ColorPattern};
|
||||
|
@ -22,6 +23,7 @@ use geom::size::Size2D;
|
|||
use gfx_traits::color;
|
||||
use std::sync::mpsc::{Sender};
|
||||
use layers::platform::surface::NativeSurface;
|
||||
use offscreen_gl_context::GLContextAttributes;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum CanvasMsg {
|
||||
|
@ -66,6 +68,7 @@ pub enum Canvas2dMsg {
|
|||
|
||||
#[derive(Clone)]
|
||||
pub enum CanvasWebGLMsg {
|
||||
GetContextAttributes(Sender<GLContextAttributes>),
|
||||
AttachShader(u32, u32),
|
||||
BindBuffer(u32, u32),
|
||||
BufferData(u32, Vec<f32>, u32),
|
||||
|
|
|
@ -73,6 +73,9 @@ git = "https://github.com/servo/string-cache"
|
|||
version = "0.2.33"
|
||||
features = ["query_encoding"]
|
||||
|
||||
[dependencies.offscreen_gl_context]
|
||||
git = "https://github.com/ecoal95/rust-offscreen-rendering-context"
|
||||
|
||||
[dependencies]
|
||||
encoding = "0.2"
|
||||
time = "0.1.12"
|
||||
|
|
|
@ -1165,6 +1165,15 @@ def getRetvalDeclarationForType(returnType, descriptorProvider):
|
|||
return CGGeneric("*mut JSObject")
|
||||
if returnType.isSequence():
|
||||
raise TypeError("We don't support sequence return values")
|
||||
if returnType.isDictionary():
|
||||
nullable = returnType.nullable()
|
||||
dictName = returnType.inner.name if nullable else returnType.name
|
||||
result = CGGeneric(dictName)
|
||||
if typeNeedsRooting(returnType, descriptorProvider):
|
||||
raise TypeError("We don't support rootable dictionaries return values")
|
||||
if nullable:
|
||||
result = CGWrapper(result, pre="Option<", post=">")
|
||||
return result
|
||||
|
||||
raise TypeError("Don't know how to declare return value for %s" %
|
||||
returnType)
|
||||
|
@ -4517,7 +4526,14 @@ class CGDictionary(CGThing):
|
|||
conversion = self.getMemberConversion(memberInfo)
|
||||
return CGGeneric("%s: %s,\n" % (name, conversion.define()))
|
||||
|
||||
def memberInsert(memberInfo):
|
||||
member, _ = memberInfo
|
||||
name = self.makeMemberName(member.identifier.name)
|
||||
insertion = ("set_dictionary_property(cx, obj, \"%s\", &mut self.%s.to_jsval(cx)).unwrap();" % (name, name))
|
||||
return CGGeneric("%s\n" % insertion)
|
||||
|
||||
memberInits = CGList([memberInit(m) for m in self.memberInfo])
|
||||
memberInserts = CGList([memberInsert(m) for m in self.memberInfo])
|
||||
|
||||
return string.Template(
|
||||
"impl ${selfName} {\n"
|
||||
|
@ -4538,10 +4554,19 @@ class CGDictionary(CGThing):
|
|||
"${initMembers}"
|
||||
" })\n"
|
||||
" }\n"
|
||||
"}").substitute({
|
||||
"}\n"
|
||||
"\n"
|
||||
"impl ToJSValConvertible for ${selfName} {\n"
|
||||
" fn to_jsval(&self, cx: *mut JSContext) -> JSVal {\n"
|
||||
" let obj = unsafe { JS_NewObject(cx, 0 as *const JSClass, 0 as *const JSObject, 0 as *const JSObject) };\n"
|
||||
"${insertMembers}"
|
||||
" ObjectOrNullValue(obj)\n"
|
||||
" }\n"
|
||||
"}\n").substitute({
|
||||
"selfName": self.makeClassName(d),
|
||||
"initParent": CGIndenter(CGGeneric(initParent), indentLevel=12).define(),
|
||||
"initMembers": CGIndenter(memberInits, indentLevel=12).define(),
|
||||
"insertMembers": CGIndenter(memberInserts, indentLevel=8).define(),
|
||||
})
|
||||
|
||||
@staticmethod
|
||||
|
@ -4590,6 +4615,7 @@ class CGDictionary(CGThing):
|
|||
|
||||
return CGGeneric(conversion)
|
||||
|
||||
|
||||
@staticmethod
|
||||
def makeIdName(name):
|
||||
return name + "_id"
|
||||
|
@ -4750,6 +4776,7 @@ class CGBindingRoot(CGThing):
|
|||
'dom::bindings::utils::{Reflectable}',
|
||||
'dom::bindings::utils::throwing_constructor',
|
||||
'dom::bindings::utils::get_dictionary_property',
|
||||
'dom::bindings::utils::set_dictionary_property',
|
||||
'dom::bindings::utils::{NativeProperties, NativePropertyHooks}',
|
||||
'dom::bindings::utils::ConstantVal::{IntVal, UintVal}',
|
||||
'dom::bindings::utils::NonNullJSNative',
|
||||
|
|
|
@ -29,7 +29,7 @@ use js::jsapi::{JS_GetClass, JS_LinkConstructorAndPrototype, JS_GetStringCharsAn
|
|||
use js::jsapi::JSHandleObject;
|
||||
use js::jsapi::JS_GetFunctionObject;
|
||||
use js::jsapi::{JS_HasPropertyById, JS_GetPrototype};
|
||||
use js::jsapi::{JS_GetProperty, JS_HasProperty};
|
||||
use js::jsapi::{JS_GetProperty, JS_HasProperty, JS_SetProperty};
|
||||
use js::jsapi::{JS_DefineFunctions, JS_DefineProperty};
|
||||
use js::jsapi::{JS_ValueToString, JS_GetReservedSlot, JS_SetReservedSlot};
|
||||
use js::jsapi::{JSContext, JSObject, JSBool, jsid, JSClass};
|
||||
|
@ -510,7 +510,6 @@ pub fn is_platform_object(obj: *mut JSObject) -> bool {
|
|||
pub fn get_dictionary_property(cx: *mut JSContext,
|
||||
object: *mut JSObject,
|
||||
property: &str) -> Result<Option<JSVal>, ()> {
|
||||
use std::ffi::CString;
|
||||
fn has_property(cx: *mut JSContext, object: *mut JSObject, property: &CString,
|
||||
found: &mut JSBool) -> bool {
|
||||
unsafe {
|
||||
|
@ -546,6 +545,27 @@ pub fn get_dictionary_property(cx: *mut JSContext,
|
|||
Ok(Some(value))
|
||||
}
|
||||
|
||||
/// Set the property with name `property` from `object`.
|
||||
/// Returns `Err(())` on JSAPI failure, or null object,
|
||||
/// and Ok(()) otherwise
|
||||
pub fn set_dictionary_property(cx: *mut JSContext,
|
||||
object: *mut JSObject,
|
||||
property: &str,
|
||||
value: &mut JSVal) -> Result<(), ()> {
|
||||
if object.is_null() {
|
||||
return Err(());
|
||||
}
|
||||
|
||||
let property = CString::new(property).unwrap();
|
||||
unsafe {
|
||||
if JS_SetProperty(cx, object, property.as_ptr(), value) == 0 {
|
||||
return Err(());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns whether `proxy` has a property `id` on its prototype.
|
||||
pub fn has_property_on_prototype(cx: *mut JSContext, proxy: *mut JSObject,
|
||||
id: jsid) -> bool {
|
||||
|
|
|
@ -214,7 +214,11 @@ impl CanvasRenderingContext2D {
|
|||
let msg = if self.canvas.root().r() == canvas {
|
||||
CanvasMsg::Canvas2d(Canvas2dMsg::DrawImageSelf(image_size, dest_rect, source_rect, smoothing_enabled))
|
||||
} else { // Source and target canvases are different
|
||||
let context = canvas.get_2d_context().root();
|
||||
let context = match canvas.get_or_init_2d_context() {
|
||||
Some(context) => context.root(),
|
||||
None => return Err(InvalidState),
|
||||
};
|
||||
|
||||
let renderer = context.r().get_renderer();
|
||||
let (sender, receiver) = channel::<Vec<u8>>();
|
||||
// Reads pixels from source image
|
||||
|
|
|
@ -10,8 +10,9 @@ use dom::bindings::codegen::Bindings::HTMLCanvasElementBinding::HTMLCanvasElemen
|
|||
use dom::bindings::codegen::InheritTypes::HTMLCanvasElementDerived;
|
||||
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLElementCast};
|
||||
use dom::bindings::codegen::UnionTypes::CanvasRenderingContext2DOrWebGLRenderingContext;
|
||||
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLContextAttributes;
|
||||
use dom::bindings::global::GlobalRef;
|
||||
use dom::bindings::js::{JS, JSRef, LayoutJS, MutNullableHeap, Rootable};
|
||||
use dom::bindings::js::{JS, JSRef, LayoutJS, MutNullableHeap, HeapGCValue, Rootable};
|
||||
use dom::bindings::js::Temporary;
|
||||
use dom::bindings::js::Unrooted;
|
||||
use dom::bindings::utils::{Reflectable};
|
||||
|
@ -26,6 +27,9 @@ use dom::virtualmethods::VirtualMethods;
|
|||
use dom::webglrenderingcontext::{WebGLRenderingContext, LayoutCanvasWebGLRenderingContextHelpers};
|
||||
|
||||
use util::str::{DOMString, parse_unsigned_integer};
|
||||
use js::jsapi::{JSContext};
|
||||
use js::jsval::JSVal;
|
||||
use offscreen_gl_context::GLContextAttributes;
|
||||
|
||||
use geom::size::Size2D;
|
||||
|
||||
|
@ -36,11 +40,20 @@ use std::sync::mpsc::Sender;
|
|||
const DEFAULT_WIDTH: u32 = 300;
|
||||
const DEFAULT_HEIGHT: u32 = 150;
|
||||
|
||||
#[jstraceable]
|
||||
#[must_root]
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum CanvasContext {
|
||||
Context2d(JS<CanvasRenderingContext2D>),
|
||||
WebGL(JS<WebGLRenderingContext>),
|
||||
}
|
||||
|
||||
impl HeapGCValue for CanvasContext {}
|
||||
|
||||
#[dom_struct]
|
||||
pub struct HTMLCanvasElement {
|
||||
htmlelement: HTMLElement,
|
||||
context_2d: MutNullableHeap<JS<CanvasRenderingContext2D>>,
|
||||
context_webgl: MutNullableHeap<JS<WebGLRenderingContext>>,
|
||||
context: MutNullableHeap<CanvasContext>,
|
||||
width: Cell<u32>,
|
||||
height: Cell<u32>,
|
||||
}
|
||||
|
@ -60,8 +73,7 @@ impl HTMLCanvasElement {
|
|||
HTMLCanvasElement {
|
||||
htmlelement:
|
||||
HTMLElement::new_inherited(HTMLElementTypeId::HTMLCanvasElement, localName, prefix, document),
|
||||
context_2d: Default::default(),
|
||||
context_webgl: Default::default(),
|
||||
context: Default::default(),
|
||||
width: Cell::new(DEFAULT_WIDTH),
|
||||
height: Cell::new(DEFAULT_HEIGHT),
|
||||
}
|
||||
|
@ -77,11 +89,11 @@ impl HTMLCanvasElement {
|
|||
|
||||
fn recreate_contexts(&self) {
|
||||
let size = self.get_size();
|
||||
if let Some(context) = self.context_2d.get() {
|
||||
context.root().r().recreate(size)
|
||||
}
|
||||
if let Some(context) = self.context_webgl.get() {
|
||||
context.root().r().recreate(size)
|
||||
if let Some(context) = self.context.get() {
|
||||
match context {
|
||||
CanvasContext::Context2d(context) => context.root().r().recreate(size),
|
||||
CanvasContext::WebGL(context) => context.root().r().recreate(size),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -103,12 +115,13 @@ impl LayoutHTMLCanvasElementHelpers for LayoutJS<HTMLCanvasElement> {
|
|||
#[allow(unsafe_code)]
|
||||
unsafe fn get_renderer(&self) -> Option<Sender<CanvasMsg>> {
|
||||
let ref canvas = *self.unsafe_get();
|
||||
if canvas.context_2d.get().is_some() {
|
||||
let context = canvas.context_2d.get_inner_as_layout();
|
||||
context.map(|cx| cx.get_renderer())
|
||||
} else if canvas.context_webgl.get().is_some() {
|
||||
let context = canvas.context_webgl.get_inner_as_layout();
|
||||
context.map(|cx| cx.get_renderer())
|
||||
if let Some(context) = canvas.context.get() {
|
||||
match context {
|
||||
CanvasContext::Context2d(context)
|
||||
=> Some(context.to_layout().get_renderer()),
|
||||
CanvasContext::WebGL(context)
|
||||
=> Some(context.to_layout().get_renderer()),
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -126,29 +139,58 @@ impl LayoutHTMLCanvasElementHelpers for LayoutJS<HTMLCanvasElement> {
|
|||
}
|
||||
|
||||
pub trait HTMLCanvasElementHelpers {
|
||||
fn get_2d_context(self) -> Temporary<CanvasRenderingContext2D>;
|
||||
fn get_webgl_context(self) -> Temporary<WebGLRenderingContext>;
|
||||
fn get_or_init_2d_context(self) -> Option<Temporary<CanvasRenderingContext2D>>;
|
||||
fn get_or_init_webgl_context(self,
|
||||
cx: *mut JSContext,
|
||||
attrs: Option<&JSVal>) -> Option<Temporary<WebGLRenderingContext>>;
|
||||
fn is_valid(self) -> bool;
|
||||
}
|
||||
|
||||
impl<'a> HTMLCanvasElementHelpers for JSRef<'a, HTMLCanvasElement> {
|
||||
fn get_2d_context(self) -> Temporary<CanvasRenderingContext2D> {
|
||||
let context = self.GetContext(String::from_str("2d"));
|
||||
match context.unwrap() {
|
||||
CanvasRenderingContext2DOrWebGLRenderingContext::eCanvasRenderingContext2D(context) => {
|
||||
Temporary::from_unrooted(context)
|
||||
}
|
||||
_ => panic!("Wrong Context Type: Expected 2d context"),
|
||||
fn get_or_init_2d_context(self) -> Option<Temporary<CanvasRenderingContext2D>> {
|
||||
if self.context.get().is_none() {
|
||||
let window = window_from_node(self).root();
|
||||
let size = self.get_size();
|
||||
let context = CanvasRenderingContext2D::new(GlobalRef::Window(window.r()), self, size);
|
||||
self.context.set(Some(CanvasContext::Context2d(JS::from_rooted(context))));
|
||||
}
|
||||
|
||||
match self.context.get().unwrap() {
|
||||
CanvasContext::Context2d(context) => Some(Temporary::from_rooted(context)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_webgl_context(self) -> Temporary<WebGLRenderingContext> {
|
||||
let context = self.GetContext(String::from_str("webgl"));
|
||||
match context.unwrap() {
|
||||
CanvasRenderingContext2DOrWebGLRenderingContext::eWebGLRenderingContext(context) => {
|
||||
Temporary::from_unrooted(context)
|
||||
},
|
||||
_ => panic!("Wrong Context Type: Expected webgl context"),
|
||||
fn get_or_init_webgl_context(self,
|
||||
cx: *mut JSContext,
|
||||
attrs: Option<&JSVal>) -> Option<Temporary<WebGLRenderingContext>> {
|
||||
if self.context.get().is_none() {
|
||||
let window = window_from_node(self).root();
|
||||
let size = self.get_size();
|
||||
|
||||
let attrs = if let Some(webgl_attributes) = attrs {
|
||||
if let Ok(ref attrs) = WebGLContextAttributes::new(cx, *webgl_attributes) {
|
||||
From::from(attrs)
|
||||
} else {
|
||||
debug!("Unexpected error on conversion of WebGLContextAttributes");
|
||||
return None;
|
||||
}
|
||||
} else {
|
||||
GLContextAttributes::default()
|
||||
};
|
||||
|
||||
let maybe_ctx = WebGLRenderingContext::new(GlobalRef::Window(window.r()), self, size, attrs);
|
||||
|
||||
self.context.set(maybe_ctx.map( |ctx| CanvasContext::WebGL(JS::from_rooted(ctx))));
|
||||
}
|
||||
|
||||
if let Some(context) = self.context.get() {
|
||||
match context {
|
||||
CanvasContext::WebGL(context) => Some(Temporary::from_rooted(context)),
|
||||
_ => None,
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -176,39 +218,21 @@ impl<'a> HTMLCanvasElementMethods for JSRef<'a, HTMLCanvasElement> {
|
|||
elem.set_uint_attribute(&atom!("height"), height)
|
||||
}
|
||||
|
||||
fn GetContext(self, id: DOMString) -> Option<CanvasRenderingContext2DOrWebGLRenderingContext> {
|
||||
fn GetContext(self,
|
||||
cx: *mut JSContext,
|
||||
id: DOMString,
|
||||
attributes: Vec<JSVal>)
|
||||
-> Option<CanvasRenderingContext2DOrWebGLRenderingContext> {
|
||||
match &*id {
|
||||
"2d" => {
|
||||
if self.context_webgl.get().is_some() {
|
||||
debug!("Trying to get a 2d context for a canvas with an already initialized WebGL context");
|
||||
return None;
|
||||
}
|
||||
|
||||
let context_2d = self.context_2d.or_init(|| {
|
||||
let window = window_from_node(self).root();
|
||||
let size = self.get_size();
|
||||
CanvasRenderingContext2D::new(GlobalRef::Window(window.r()), self, size)
|
||||
});
|
||||
Some(
|
||||
CanvasRenderingContext2DOrWebGLRenderingContext::eCanvasRenderingContext2D(
|
||||
Unrooted::from_temporary(context_2d)))
|
||||
self.get_or_init_2d_context()
|
||||
.map(|ctx| CanvasRenderingContext2DOrWebGLRenderingContext::eCanvasRenderingContext2D(
|
||||
Unrooted::from_temporary(ctx)))
|
||||
}
|
||||
"webgl" | "experimental-webgl" => {
|
||||
if self.context_2d.get().is_some() {
|
||||
debug!("Trying to get a WebGL context for a canvas with an already initialized 2d context");
|
||||
return None;
|
||||
}
|
||||
|
||||
if !self.context_webgl.get().is_some() {
|
||||
let window = window_from_node(self).root();
|
||||
let size = self.get_size();
|
||||
|
||||
self.context_webgl.set(
|
||||
WebGLRenderingContext::new(GlobalRef::Window(window.r()), self, size).map(JS::from_rooted))
|
||||
}
|
||||
|
||||
self.context_webgl.get().map( |ctx|
|
||||
CanvasRenderingContext2DOrWebGLRenderingContext::eWebGLRenderingContext(Unrooted::from_js(ctx)))
|
||||
self.get_or_init_webgl_context(cx, attributes.get(0))
|
||||
.map(|ctx| CanvasRenderingContext2DOrWebGLRenderingContext::eWebGLRenderingContext(
|
||||
Unrooted::from_temporary(ctx)))
|
||||
}
|
||||
_ => None
|
||||
}
|
||||
|
@ -266,3 +290,17 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLCanvasElement> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a WebGLContextAttributes> for GLContextAttributes {
|
||||
fn from(attrs: &'a WebGLContextAttributes) -> GLContextAttributes {
|
||||
GLContextAttributes {
|
||||
alpha: attrs.alpha,
|
||||
depth: attrs.depth,
|
||||
stencil: attrs.stencil,
|
||||
antialias: attrs.antialias,
|
||||
premultiplied_alpha: attrs.premultipliedAlpha,
|
||||
preserve_drawing_buffer: attrs.preserveDrawingBuffer,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ use canvas::webgl_paint_task::WebGLPaintTask;
|
|||
use canvas_traits::{CanvasMsg, CanvasWebGLMsg, CanvasCommonMsg};
|
||||
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding;
|
||||
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::{
|
||||
WebGLRenderingContextMethods, WebGLRenderingContextConstants};
|
||||
WebGLContextAttributes, WebGLRenderingContextMethods, WebGLRenderingContextConstants};
|
||||
use dom::bindings::global::{GlobalRef, GlobalField};
|
||||
use dom::bindings::js::{JS, JSRef, LayoutJS, Temporary};
|
||||
use dom::bindings::utils::{Reflector, reflect_dom_object};
|
||||
|
@ -23,6 +23,7 @@ use std::mem;
|
|||
use std::ptr;
|
||||
use std::sync::mpsc::{channel, Sender};
|
||||
use util::str::DOMString;
|
||||
use offscreen_gl_context::GLContextAttributes;
|
||||
|
||||
#[dom_struct]
|
||||
pub struct WebGLRenderingContext {
|
||||
|
@ -33,9 +34,12 @@ pub struct WebGLRenderingContext {
|
|||
}
|
||||
|
||||
impl WebGLRenderingContext {
|
||||
fn new_inherited(global: GlobalRef, canvas: JSRef<HTMLCanvasElement>, size: Size2D<i32>)
|
||||
fn new_inherited(global: GlobalRef,
|
||||
canvas: JSRef<HTMLCanvasElement>,
|
||||
size: Size2D<i32>,
|
||||
attrs: GLContextAttributes)
|
||||
-> Result<WebGLRenderingContext, &'static str> {
|
||||
let chan = try!(WebGLPaintTask::start(size));
|
||||
let chan = try!(WebGLPaintTask::start(size, attrs));
|
||||
|
||||
Ok(WebGLRenderingContext {
|
||||
reflector_: Reflector::new(),
|
||||
|
@ -45,9 +49,9 @@ impl WebGLRenderingContext {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn new(global: GlobalRef, canvas: JSRef<HTMLCanvasElement>, size: Size2D<i32>)
|
||||
pub fn new(global: GlobalRef, canvas: JSRef<HTMLCanvasElement>, size: Size2D<i32>, attrs: GLContextAttributes)
|
||||
-> Option<Temporary<WebGLRenderingContext>> {
|
||||
match WebGLRenderingContext::new_inherited(global, canvas, size) {
|
||||
match WebGLRenderingContext::new_inherited(global, canvas, size, attrs) {
|
||||
Ok(ctx) => Some(reflect_dom_object(box ctx, global,
|
||||
WebGLRenderingContextBinding::Wrap)),
|
||||
Err(msg) => {
|
||||
|
@ -69,6 +73,28 @@ impl Drop for WebGLRenderingContext {
|
|||
}
|
||||
|
||||
impl<'a> WebGLRenderingContextMethods for JSRef<'a, WebGLRenderingContext> {
|
||||
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.2
|
||||
fn GetContextAttributes(self) -> Option<WebGLContextAttributes> {
|
||||
let (sender, receiver) = channel();
|
||||
|
||||
// If the send does not succeed, assume context lost
|
||||
if let Err(_) = self.renderer.send(CanvasMsg::WebGL(CanvasWebGLMsg::GetContextAttributes(sender))) {
|
||||
return None;
|
||||
}
|
||||
let attrs = receiver.recv().unwrap();
|
||||
|
||||
Some(WebGLContextAttributes {
|
||||
alpha: attrs.alpha,
|
||||
antialias: attrs.antialias,
|
||||
depth: attrs.depth,
|
||||
failIfMajorPerformanceCaveat: false,
|
||||
preferLowPowerToHighPerformance: false,
|
||||
premultipliedAlpha: attrs.premultiplied_alpha,
|
||||
preserveDrawingBuffer: attrs.preserve_drawing_buffer,
|
||||
stencil: attrs.stencil
|
||||
})
|
||||
}
|
||||
|
||||
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.9
|
||||
fn AttachShader(self, program: Option<JSRef<WebGLProgram>>, shader: Option<JSRef<WebGLShader>>) {
|
||||
let program_id = match program {
|
||||
|
|
|
@ -12,7 +12,7 @@ interface HTMLCanvasElement : HTMLElement {
|
|||
[Pure]
|
||||
attribute unsigned long height;
|
||||
|
||||
RenderingContext? getContext(DOMString contextId);
|
||||
RenderingContext? getContext(DOMString contextId, any... arguments);
|
||||
//boolean probablySupportsContext(DOMString contextId, any... arguments);
|
||||
|
||||
//void setContext(RenderingContext context);
|
||||
|
|
|
@ -481,7 +481,7 @@ interface WebGLRenderingContextBase
|
|||
//readonly attribute GLsizei drawingBufferWidth;
|
||||
//readonly attribute GLsizei drawingBufferHeight;
|
||||
|
||||
//[WebGLHandlesContextLoss] WebGLContextAttributes? getContextAttributes();
|
||||
[WebGLHandlesContextLoss] WebGLContextAttributes? getContextAttributes();
|
||||
//[WebGLHandlesContextLoss] boolean isContextLost();
|
||||
|
||||
//sequence<DOMString>? getSupportedExtensions();
|
||||
|
|
|
@ -53,6 +53,7 @@ extern crate url;
|
|||
extern crate uuid;
|
||||
extern crate string_cache;
|
||||
extern crate webdriver_traits;
|
||||
extern crate offscreen_gl_context;
|
||||
|
||||
pub mod cors;
|
||||
pub mod document_loader;
|
||||
|
|
4
components/servo/Cargo.lock
generated
4
components/servo/Cargo.lock
generated
|
@ -90,6 +90,7 @@ dependencies = [
|
|||
"geom 0.1.0 (git+https://github.com/servo/rust-geom)",
|
||||
"gfx_traits 0.0.1",
|
||||
"layers 0.1.0 (git+https://github.com/servo/rust-layers)",
|
||||
"offscreen_gl_context 0.0.1 (git+https://github.com/ecoal95/rust-offscreen-rendering-context)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -847,7 +848,7 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "offscreen_gl_context"
|
||||
version = "0.0.1"
|
||||
source = "git+https://github.com/ecoal95/rust-offscreen-rendering-context#99cee719a811f28e70fe33fa71137663ac210487"
|
||||
source = "git+https://github.com/ecoal95/rust-offscreen-rendering-context#13c9f786c55266fa1594cb520fdbc74f45fda809"
|
||||
dependencies = [
|
||||
"cgl 0.0.1 (git+https://github.com/servo/rust-cgl)",
|
||||
"core_foundation 0.1.0 (git+https://github.com/servo/rust-core-foundation)",
|
||||
|
@ -1028,6 +1029,7 @@ dependencies = [
|
|||
"msg 0.0.1",
|
||||
"net_traits 0.0.1",
|
||||
"num 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"offscreen_gl_context 0.0.1 (git+https://github.com/ecoal95/rust-offscreen-rendering-context)",
|
||||
"plugins 0.0.1",
|
||||
"png 0.1.0 (git+https://github.com/servo/rust-png)",
|
||||
"profile_traits 0.0.1",
|
||||
|
|
4
ports/cef/Cargo.lock
generated
4
ports/cef/Cargo.lock
generated
|
@ -89,6 +89,7 @@ dependencies = [
|
|||
"geom 0.1.0 (git+https://github.com/servo/rust-geom)",
|
||||
"gfx_traits 0.0.1",
|
||||
"layers 0.1.0 (git+https://github.com/servo/rust-layers)",
|
||||
"offscreen_gl_context 0.0.1 (git+https://github.com/ecoal95/rust-offscreen-rendering-context)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -837,7 +838,7 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "offscreen_gl_context"
|
||||
version = "0.0.1"
|
||||
source = "git+https://github.com/ecoal95/rust-offscreen-rendering-context#99cee719a811f28e70fe33fa71137663ac210487"
|
||||
source = "git+https://github.com/ecoal95/rust-offscreen-rendering-context#13c9f786c55266fa1594cb520fdbc74f45fda809"
|
||||
dependencies = [
|
||||
"cgl 0.0.1 (git+https://github.com/servo/rust-cgl)",
|
||||
"core_foundation 0.1.0 (git+https://github.com/servo/rust-core-foundation)",
|
||||
|
@ -1018,6 +1019,7 @@ dependencies = [
|
|||
"msg 0.0.1",
|
||||
"net_traits 0.0.1",
|
||||
"num 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"offscreen_gl_context 0.0.1 (git+https://github.com/ecoal95/rust-offscreen-rendering-context)",
|
||||
"plugins 0.0.1",
|
||||
"png 0.1.0 (git+https://github.com/servo/rust-png)",
|
||||
"profile_traits 0.0.1",
|
||||
|
|
4
ports/gonk/Cargo.lock
generated
4
ports/gonk/Cargo.lock
generated
|
@ -76,6 +76,7 @@ dependencies = [
|
|||
"geom 0.1.0 (git+https://github.com/servo/rust-geom)",
|
||||
"gfx_traits 0.0.1",
|
||||
"layers 0.1.0 (git+https://github.com/servo/rust-layers)",
|
||||
"offscreen_gl_context 0.0.1 (git+https://github.com/ecoal95/rust-offscreen-rendering-context)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -727,7 +728,7 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "offscreen_gl_context"
|
||||
version = "0.0.1"
|
||||
source = "git+https://github.com/ecoal95/rust-offscreen-rendering-context#99cee719a811f28e70fe33fa71137663ac210487"
|
||||
source = "git+https://github.com/ecoal95/rust-offscreen-rendering-context#13c9f786c55266fa1594cb520fdbc74f45fda809"
|
||||
dependencies = [
|
||||
"cgl 0.0.1 (git+https://github.com/servo/rust-cgl)",
|
||||
"core_foundation 0.1.0 (git+https://github.com/servo/rust-core-foundation)",
|
||||
|
@ -899,6 +900,7 @@ dependencies = [
|
|||
"msg 0.0.1",
|
||||
"net_traits 0.0.1",
|
||||
"num 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"offscreen_gl_context 0.0.1 (git+https://github.com/ecoal95/rust-offscreen-rendering-context)",
|
||||
"plugins 0.0.1",
|
||||
"png 0.1.0 (git+https://github.com/servo/rust-png)",
|
||||
"profile_traits 0.0.1",
|
||||
|
|
39
tests/html/test_webgl_context_attributes.html
Normal file
39
tests/html/test_webgl_context_attributes.html
Normal file
|
@ -0,0 +1,39 @@
|
|||
<meta charset="utf-8">
|
||||
<title>WebGL Context Attributes test</title>
|
||||
<script>
|
||||
(function () {
|
||||
var canvas = document.createElement('canvas'),
|
||||
closure = function() {},
|
||||
gl, attributes;
|
||||
|
||||
closure.alpha = false;
|
||||
closure.antialias = "";
|
||||
|
||||
gl = canvas.getContext("webgl", closure);
|
||||
|
||||
if ( ! gl ) {
|
||||
console.log("Passing a closure to `getContext` didn't generate a context");
|
||||
gl = canvas.getContext("webgl", { alpha: false, antialias: "" });
|
||||
}
|
||||
|
||||
if ( ! gl ) {
|
||||
console.error("Unable to generate a WebGL context");
|
||||
return;
|
||||
}
|
||||
|
||||
attributes = gl.getContextAttributes();
|
||||
console.log("Got context attributes: ", JSON.stringify(attributes));
|
||||
|
||||
if ( attributes.alpha )
|
||||
console.error("Alpha not expected");
|
||||
|
||||
if ( attributes.antialias )
|
||||
console.error("Antialias not expected, should be casted to false");
|
||||
|
||||
gl = canvas.getContext("webgl", { alpha: true });
|
||||
attributes = gl.getContextAttributes();
|
||||
|
||||
if ( attributes.alpha )
|
||||
console.error("Should have returned the previous context attributes");
|
||||
})();
|
||||
</script>
|
Loading…
Add table
Add a link
Reference in a new issue