Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly clean up the final URL creation in canvas.toDataURL #21850

Merged
merged 7 commits into from Oct 3, 2018
@@ -35,7 +35,7 @@ pub struct CanvasData<'a> {

impl<'a> CanvasData<'a> {
pub fn new(
size: Size2D<i32>,
size: Size2D<u32>,
webrender_api_sender: webrender_api::RenderApiSender,
antialias: AntialiasMode,
canvas_id: CanvasId
@@ -369,11 +369,12 @@ impl<'a> CanvasData<'a> {
self.state.draw_options.set_composition_op(op.to_azure_style());
}

pub fn create(size: Size2D<i32>) -> DrawTarget {
DrawTarget::new(BackendType::Skia, size, SurfaceFormat::B8G8R8A8)
pub fn create(size: Size2D<u32>) -> DrawTarget {
// FIXME(nox): Why is the size made of i32 values?
DrawTarget::new(BackendType::Skia, size.to_i32(), SurfaceFormat::B8G8R8A8)
}

pub fn recreate(&mut self, size: Size2D<i32>) {
pub fn recreate(&mut self, size: Size2D<u32>) {
self.drawtarget = CanvasData::create(size);
self.state = CanvasPaintState::new(self.state.draw_options.antialias);
self.saved_states.clear();
@@ -901,9 +902,9 @@ pub trait ToAzurePattern {

impl ToAzurePattern for FillOrStrokeStyle {
fn to_azure_pattern(&self, drawtarget: &DrawTarget) -> Option<Pattern> {
match *self {
Some(match *self {
FillOrStrokeStyle::Color(ref color) => {
Some(Pattern::Color(ColorPattern::new(color.to_azure_style())))
Pattern::Color(ColorPattern::new(color.to_azure_style()))
},
FillOrStrokeStyle::LinearGradient(ref linear_gradient_style) => {
let gradient_stops: Vec<GradientStop> = linear_gradient_style.stops.iter().map(|s| {
@@ -913,11 +914,12 @@ impl ToAzurePattern for FillOrStrokeStyle {
}
}).collect();

Some(Pattern::LinearGradient(LinearGradientPattern::new(
Pattern::LinearGradient(LinearGradientPattern::new(
&Point2D::new(linear_gradient_style.x0 as AzFloat, linear_gradient_style.y0 as AzFloat),
&Point2D::new(linear_gradient_style.x1 as AzFloat, linear_gradient_style.y1 as AzFloat),
drawtarget.create_gradient_stops(&gradient_stops, ExtendMode::Clamp),
&Transform2D::identity())))
&Transform2D::identity(),
))
},
FillOrStrokeStyle::RadialGradient(ref radial_gradient_style) => {
let gradient_stops: Vec<GradientStop> = radial_gradient_style.stops.iter().map(|s| {
@@ -927,27 +929,30 @@ impl ToAzurePattern for FillOrStrokeStyle {
}
}).collect();

Some(Pattern::RadialGradient(RadialGradientPattern::new(
Pattern::RadialGradient(RadialGradientPattern::new(
&Point2D::new(radial_gradient_style.x0 as AzFloat, radial_gradient_style.y0 as AzFloat),
&Point2D::new(radial_gradient_style.x1 as AzFloat, radial_gradient_style.y1 as AzFloat),
radial_gradient_style.r0 as AzFloat, radial_gradient_style.r1 as AzFloat,
drawtarget.create_gradient_stops(&gradient_stops, ExtendMode::Clamp),
&Transform2D::identity())))
&Transform2D::identity(),
))
},
FillOrStrokeStyle::Surface(ref surface_style) => {
drawtarget.create_source_surface_from_data(&surface_style.surface_data,
surface_style.surface_size,
surface_style.surface_size.width * 4,
SurfaceFormat::B8G8R8A8)
.map(|source_surface| {
Pattern::Surface(SurfacePattern::new(
source_surface.azure_source_surface,
surface_style.repeat_x,
surface_style.repeat_y,
&Transform2D::identity()))
})
let source_surface = drawtarget.create_source_surface_from_data(
&surface_style.surface_data,
// FIXME(nox): Why are those i32 values?
surface_style.surface_size.to_i32(),
surface_style.surface_size.width as i32 * 4,
SurfaceFormat::B8G8R8A8,
)?;
Pattern::Surface(SurfacePattern::new(
source_surface.azure_source_surface,
surface_style.repeat_x,
surface_style.repeat_y,
&Transform2D::identity(),
))
}
}
})
}
}

@@ -80,7 +80,7 @@ impl<'a> CanvasPaintThread <'a> {

pub fn create_canvas(
&mut self,
size: Size2D<i32>,
size: Size2D<u32>,
webrender_api_sender: webrender_api::RenderApiSender,
antialias: bool
) -> CanvasId {
@@ -47,63 +47,71 @@ impl GLContextFactory {
pub fn new_shared_context(
&self,
webgl_version: WebGLVersion,
size: Size2D<i32>,
size: Size2D<u32>,
attributes: GLContextAttributes
) -> Result<GLContextWrapper, &'static str> {
match *self {
Ok(match *self {
GLContextFactory::Native(ref handle, ref dispatcher) => {
let dispatcher = dispatcher.as_ref().map(|d| Box::new(d.clone()) as Box<_>);
let ctx = GLContext::<NativeGLContext>::new_shared_with_dispatcher(size,
attributes,
ColorAttachmentType::Texture,
gl::GlType::default(),
Self::gl_version(webgl_version),
Some(handle),
dispatcher);
ctx.map(GLContextWrapper::Native)
GLContextWrapper::Native(GLContext::new_shared_with_dispatcher(
// FIXME(nox): Why are those i32 values?
size.to_i32(),
attributes,
ColorAttachmentType::Texture,
gl::GlType::default(),
Self::gl_version(webgl_version),
Some(handle),
dispatcher,
)?)
}
GLContextFactory::OSMesa(ref handle) => {
let ctx = GLContext::<OSMesaContext>::new_shared_with_dispatcher(size.to_untyped(),
attributes,
ColorAttachmentType::Texture,
gl::GlType::default(),
Self::gl_version(webgl_version),
Some(handle),
None);
ctx.map(GLContextWrapper::OSMesa)
GLContextWrapper::OSMesa(GLContext::new_shared_with_dispatcher(
// FIXME(nox): Why are those i32 values?
size.to_i32(),
attributes,
ColorAttachmentType::Texture,
gl::GlType::default(),
Self::gl_version(webgl_version),
Some(handle),
None,
)?)
}
}
})
}

/// Creates a new non-shared GLContext
pub fn new_context(
&self,
webgl_version: WebGLVersion,
size: Size2D<i32>,
size: Size2D<u32>,
attributes: GLContextAttributes
) -> Result<GLContextWrapper, &'static str> {
match *self {
Ok(match *self {
GLContextFactory::Native(..) => {
let ctx = GLContext::<NativeGLContext>::new_shared_with_dispatcher(size,
attributes,
ColorAttachmentType::Texture,
gl::GlType::default(),
Self::gl_version(webgl_version),
None,
None);
ctx.map(GLContextWrapper::Native)
GLContextWrapper::Native(GLContext::new_shared_with_dispatcher(
// FIXME(nox): Why are those i32 values?
size.to_i32(),
attributes,
ColorAttachmentType::Texture,
gl::GlType::default(),
Self::gl_version(webgl_version),
None,
None,
)?)
}
GLContextFactory::OSMesa(_) => {
let ctx = GLContext::<OSMesaContext>::new_shared_with_dispatcher(size.to_untyped(),
attributes,
ColorAttachmentType::Texture,
gl::GlType::default(),
Self::gl_version(webgl_version),
None,
None);
ctx.map(GLContextWrapper::OSMesa)
GLContextWrapper::OSMesa(GLContext::new_shared_with_dispatcher(
// FIXME(nox): Why are those i32 values?
size.to_i32(),
attributes,
ColorAttachmentType::Texture,
gl::GlType::default(),
Self::gl_version(webgl_version),
None,
None,
)?)
}
}
})
}

fn gl_version(webgl_version: WebGLVersion) -> GLVersion {
@@ -196,13 +204,15 @@ impl GLContextWrapper {
}
}

pub fn resize(&mut self, size: Size2D<i32>) -> Result<(), &'static str> {
pub fn resize(&mut self, size: Size2D<u32>) -> Result<(), &'static str> {
match *self {
GLContextWrapper::Native(ref mut ctx) => {
ctx.resize(size)
// FIXME(nox): Why are those i32 values?
ctx.resize(size.to_i32())
}
GLContextWrapper::OSMesa(ref mut ctx) => {
ctx.resize(size)
// FIXME(nox): Why are those i32 values?
ctx.resize(size.to_i32())
}
}
}
@@ -216,57 +216,57 @@ impl<VR: WebVRRenderHandler + 'static> WebGLThread<VR> {
}

/// Creates a new WebGLContext
fn create_webgl_context(&mut self,
version: WebGLVersion,
size: Size2D<i32>,
attributes: GLContextAttributes)
-> Result<(WebGLContextId, GLLimits, WebGLContextShareMode), String> {
// First try to create a shared context for the best performance.
// Fallback to readback mode if the shared context creation fails.
let result = self.gl_factory.new_shared_context(version, size, attributes)
.map(|r| (r, WebGLContextShareMode::SharedTexture))
.or_else(|err| {
warn!("Couldn't create shared GL context ({}), using slow \
readback context instead.", err);
let ctx = self.gl_factory.new_context(version, size, attributes);
ctx.map(|r| (r, WebGLContextShareMode::Readback))
});

fn create_webgl_context(
&mut self,
version: WebGLVersion,
size: Size2D<u32>,
attributes: GLContextAttributes,
) -> Result<(WebGLContextId, GLLimits, WebGLContextShareMode), String> {
// Creating a new GLContext may make the current bound context_id dirty.
// Clear it to ensure that make_current() is called in subsequent commands.
self.bound_context_id = None;

match result {
Ok((ctx, share_mode)) => {
let id = WebGLContextId(self.next_webgl_id);
let (size, texture_id, limits) = ctx.get_info();
self.next_webgl_id += 1;
self.contexts.insert(id, GLContextData {
ctx,
state: Default::default(),
});
self.cached_context_info.insert(id, WebGLContextInfo {
texture_id,
size,
alpha: attributes.alpha,
image_key: None,
share_mode,
gl_sync: None,
});

Ok((id, limits, share_mode))
},
Err(msg) => {
Err(msg.to_owned())
}
}
// First try to create a shared context for the best performance.
// Fallback to readback mode if the shared context creation fails.
let (ctx, share_mode) = self.gl_factory
.new_shared_context(version, size, attributes)
.map(|r| (r, WebGLContextShareMode::SharedTexture))
.or_else(|err| {
warn!(
"Couldn't create shared GL context ({}), using slow readback context instead.",
err
);
let ctx = self.gl_factory.new_context(version, size, attributes)?;
Ok((ctx, WebGLContextShareMode::Readback))
})
.map_err(|msg: &str| msg.to_owned())?;

let id = WebGLContextId(self.next_webgl_id);
let (size, texture_id, limits) = ctx.get_info();
self.next_webgl_id += 1;
self.contexts.insert(id, GLContextData {
ctx,
state: Default::default(),
});
self.cached_context_info.insert(id, WebGLContextInfo {
texture_id,
size,
alpha: attributes.alpha,
image_key: None,
share_mode,
gl_sync: None,
});

Ok((id, limits, share_mode))
}

/// Resizes a WebGLContext
fn resize_webgl_context(&mut self,
context_id: WebGLContextId,
size: Size2D<i32>,
sender: WebGLSender<Result<(), String>>) {
fn resize_webgl_context(
&mut self,
context_id: WebGLContextId,
size: Size2D<u32>,
sender: WebGLSender<Result<(), String>>,
) {
let data = Self::make_current_if_needed_mut(
context_id,
&mut self.contexts,
@@ -798,8 +798,12 @@ impl WebGLImpl {
ctx.gl().renderbuffer_storage(target, format, width, height),
WebGLCommand::SampleCoverage(value, invert) =>
ctx.gl().sample_coverage(value, invert),
WebGLCommand::Scissor(x, y, width, height) =>
ctx.gl().scissor(x, y, width, height),
WebGLCommand::Scissor(x, y, width, height) => {
// FIXME(nox): Kinda unfortunate that some u32 values could
// end up as negative numbers here, but I don't even think
// that can happen in the real world.
ctx.gl().scissor(x, y, width as i32, height as i32);
},
WebGLCommand::StencilFunc(func, ref_, mask) =>
ctx.gl().stencil_func(func, ref_, mask),
WebGLCommand::StencilFuncSeparate(face, func, ref_, mask) =>
@@ -22,10 +22,10 @@ pub struct CanvasId(pub u64);
#[derive(Clone, Deserialize, Serialize)]
pub enum CanvasMsg {
Canvas2d(Canvas2dMsg, CanvasId),
Create(IpcSender<CanvasId>, Size2D<i32>, webrender_api::RenderApiSender, bool),
Create(IpcSender<CanvasId>, Size2D<u32>, webrender_api::RenderApiSender, bool),
FromLayout(FromLayoutMsg, CanvasId),
FromScript(FromScriptMsg, CanvasId),
Recreate(Size2D<i32>, CanvasId),
Recreate(Size2D<u32>, CanvasId),
Close(CanvasId),
}

@@ -143,15 +143,15 @@ impl RadialGradientStyle {
#[derive(Clone, Deserialize, Serialize)]
pub struct SurfaceStyle {
pub surface_data: ByteBuf,
pub surface_size: Size2D<i32>,
pub surface_size: Size2D<u32>,
pub repeat_x: bool,
pub repeat_y: bool,
}

impl SurfaceStyle {
pub fn new(
surface_data: Vec<u8>,
surface_size: Size2D<i32>,
surface_size: Size2D<u32>,
repeat_x: bool,
repeat_y: bool,
) -> Self {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.