mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Auto merge of #23135 - maharsh312:master, r=jdm
Create CanvasRect for OffscreenCanvas <!-- Please describe your changes on the following line: --> Created CanvasRect fot OffscreenCanvas and Updated Testcases --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [ ] `./mach test-tidy` does not report any errors - [ ] These changes fix #___ (GitHub issue number if applicable) <!-- Either: --> - [X] There are tests for these changes OR - [ ] These changes do not require tests because ___ <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/23135) <!-- Reviewable:end -->
This commit is contained in:
commit
fdafc833ae
131 changed files with 145 additions and 551 deletions
|
@ -62,8 +62,6 @@ enum CanvasFillOrStrokeStyle {
|
|||
#[dom_struct]
|
||||
pub struct CanvasRenderingContext2D {
|
||||
reflector_: Reflector,
|
||||
#[ignore_malloc_size_of = "Defined in ipc-channel"]
|
||||
ipc_renderer: IpcSender<CanvasMsg>,
|
||||
/// For rendering contexts created by an HTML canvas element, this is Some,
|
||||
/// for ones created by a paint worklet, this is None.
|
||||
canvas: Option<Dom<HTMLCanvasElement>>,
|
||||
|
@ -77,7 +75,7 @@ pub struct CanvasRenderingContext2D {
|
|||
state: DomRefCell<CanvasContextState>,
|
||||
saved_states: DomRefCell<Vec<CanvasContextState>>,
|
||||
origin_clean: Cell<bool>,
|
||||
canvas_id: CanvasId,
|
||||
canvas_state: DomRefCell<CanvasState>,
|
||||
}
|
||||
|
||||
#[must_root]
|
||||
|
@ -121,14 +119,15 @@ impl CanvasContextState {
|
|||
}
|
||||
}
|
||||
|
||||
impl CanvasRenderingContext2D {
|
||||
pub fn new_inherited(
|
||||
global: &GlobalScope,
|
||||
canvas: Option<&HTMLCanvasElement>,
|
||||
image_cache: Arc<dyn ImageCache>,
|
||||
base_url: ServoUrl,
|
||||
size: Size2D<u32>,
|
||||
) -> CanvasRenderingContext2D {
|
||||
#[derive(JSTraceable, MallocSizeOf)]
|
||||
pub struct CanvasState {
|
||||
#[ignore_malloc_size_of = "Defined in ipc-channel"]
|
||||
ipc_renderer: IpcSender<CanvasMsg>,
|
||||
canvas_id: CanvasId,
|
||||
}
|
||||
|
||||
impl CanvasState {
|
||||
pub fn new(global: &GlobalScope, size: Size2D<u64>) -> CanvasState {
|
||||
debug!("Creating new canvas rendering context.");
|
||||
let (sender, receiver) =
|
||||
profiled_ipc::channel(global.time_profiler_chan().clone()).unwrap();
|
||||
|
@ -139,9 +138,69 @@ impl CanvasRenderingContext2D {
|
|||
.unwrap();
|
||||
let (ipc_renderer, canvas_id) = receiver.recv().unwrap();
|
||||
debug!("Done.");
|
||||
CanvasState {
|
||||
ipc_renderer: ipc_renderer,
|
||||
canvas_id: canvas_id,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_canvas_id(&self) -> CanvasId {
|
||||
self.canvas_id.clone()
|
||||
}
|
||||
|
||||
pub fn send_canvas_2d_msg(&self, msg: Canvas2dMsg) {
|
||||
self.ipc_renderer
|
||||
.send(CanvasMsg::Canvas2d(msg, self.get_canvas_id()))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn create_drawable_rect(&self, x: f64, y: f64, w: f64, h: f64) -> Option<Rect<f32>> {
|
||||
if !([x, y, w, h].iter().all(|val| val.is_finite())) {
|
||||
return None;
|
||||
}
|
||||
|
||||
if w == 0.0 && h == 0.0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Rect::new(
|
||||
Point2D::new(x as f32, y as f32),
|
||||
Size2D::new(w as f32, h as f32),
|
||||
))
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-context-2d-fillrect
|
||||
pub fn FillRect(&self, x: f64, y: f64, width: f64, height: f64) {
|
||||
if let Some(rect) = self.create_drawable_rect(x, y, width, height) {
|
||||
self.send_canvas_2d_msg(Canvas2dMsg::FillRect(rect));
|
||||
}
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-context-2d-clearrect
|
||||
pub fn ClearRect(&self, x: f64, y: f64, width: f64, height: f64) {
|
||||
if let Some(rect) = self.create_drawable_rect(x, y, width, height) {
|
||||
self.send_canvas_2d_msg(Canvas2dMsg::ClearRect(rect));
|
||||
}
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-context-2d-strokerect
|
||||
pub fn StrokeRect(&self, x: f64, y: f64, width: f64, height: f64) {
|
||||
if let Some(rect) = self.create_drawable_rect(x, y, width, height) {
|
||||
self.send_canvas_2d_msg(Canvas2dMsg::StrokeRect(rect));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CanvasRenderingContext2D {
|
||||
pub fn new_inherited(
|
||||
global: &GlobalScope,
|
||||
canvas: Option<&HTMLCanvasElement>,
|
||||
image_cache: Arc<dyn ImageCache>,
|
||||
base_url: ServoUrl,
|
||||
size: Size2D<u32>,
|
||||
) -> CanvasRenderingContext2D {
|
||||
CanvasRenderingContext2D {
|
||||
reflector_: Reflector::new(),
|
||||
ipc_renderer: ipc_renderer,
|
||||
canvas: canvas.map(Dom::from_ref),
|
||||
image_cache: image_cache,
|
||||
missing_image_urls: DomRefCell::new(Vec::new()),
|
||||
|
@ -149,7 +208,10 @@ impl CanvasRenderingContext2D {
|
|||
state: DomRefCell::new(CanvasContextState::new()),
|
||||
saved_states: DomRefCell::new(Vec::new()),
|
||||
origin_clean: Cell::new(true),
|
||||
canvas_id: canvas_id,
|
||||
canvas_state: DomRefCell::new(CanvasState::new(
|
||||
global,
|
||||
Size2D::new(size.width as u64, size.height as u64),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -174,8 +236,13 @@ impl CanvasRenderingContext2D {
|
|||
// https://html.spec.whatwg.org/multipage/#concept-canvas-set-bitmap-dimensions
|
||||
pub fn set_bitmap_dimensions(&self, size: Size2D<u32>) {
|
||||
self.reset_to_initial_state();
|
||||
self.ipc_renderer
|
||||
.send(CanvasMsg::Recreate(size, self.get_canvas_id()))
|
||||
self.canvas_state
|
||||
.borrow()
|
||||
.ipc_renderer
|
||||
.send(CanvasMsg::Recreate(
|
||||
size,
|
||||
self.canvas_state.borrow().get_canvas_id(),
|
||||
))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
|
@ -366,7 +433,7 @@ impl CanvasRenderingContext2D {
|
|||
match *context {
|
||||
CanvasContext::Context2d(ref context) => {
|
||||
context.send_canvas_2d_msg(Canvas2dMsg::DrawImageInOther(
|
||||
self.get_canvas_id(),
|
||||
self.canvas_state.borrow().get_canvas_id(),
|
||||
image_size,
|
||||
dest_rect,
|
||||
source_rect,
|
||||
|
@ -475,21 +542,6 @@ impl CanvasRenderingContext2D {
|
|||
mem::replace(&mut self.missing_image_urls.borrow_mut(), vec![])
|
||||
}
|
||||
|
||||
fn create_drawable_rect(&self, x: f64, y: f64, w: f64, h: f64) -> Option<Rect<f32>> {
|
||||
if !([x, y, w, h].iter().all(|val| val.is_finite())) {
|
||||
return None;
|
||||
}
|
||||
|
||||
if w == 0.0 && h == 0.0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Rect::new(
|
||||
Point2D::new(x as f32, y as f32),
|
||||
Size2D::new(w as f32, h as f32),
|
||||
))
|
||||
}
|
||||
|
||||
fn parse_color(&self, string: &str) -> Result<RGBA, ()> {
|
||||
let mut input = ParserInput::new(string);
|
||||
let mut parser = Parser::new(&mut input);
|
||||
|
@ -528,17 +580,15 @@ impl CanvasRenderingContext2D {
|
|||
}
|
||||
|
||||
pub fn get_canvas_id(&self) -> CanvasId {
|
||||
self.canvas_id.clone()
|
||||
self.canvas_state.borrow().get_canvas_id()
|
||||
}
|
||||
|
||||
pub fn send_canvas_2d_msg(&self, msg: Canvas2dMsg) {
|
||||
self.ipc_renderer
|
||||
.send(CanvasMsg::Canvas2d(msg, self.get_canvas_id()))
|
||||
.unwrap()
|
||||
self.canvas_state.borrow().send_canvas_2d_msg(msg)
|
||||
}
|
||||
|
||||
pub fn get_ipc_renderer(&self) -> IpcSender<CanvasMsg> {
|
||||
self.ipc_renderer.clone()
|
||||
self.canvas_state.borrow().ipc_renderer.clone()
|
||||
}
|
||||
|
||||
pub fn origin_is_clean(&self) -> bool {
|
||||
|
@ -585,12 +635,19 @@ pub trait LayoutCanvasRenderingContext2DHelpers {
|
|||
impl LayoutCanvasRenderingContext2DHelpers for LayoutDom<CanvasRenderingContext2D> {
|
||||
#[allow(unsafe_code)]
|
||||
unsafe fn get_ipc_renderer(&self) -> IpcSender<CanvasMsg> {
|
||||
(*self.unsafe_get()).ipc_renderer.clone()
|
||||
(*self.unsafe_get())
|
||||
.canvas_state
|
||||
.borrow_for_layout()
|
||||
.ipc_renderer
|
||||
.clone()
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
unsafe fn get_canvas_id(&self) -> CanvasId {
|
||||
(*self.unsafe_get()).canvas_id.clone()
|
||||
(*self.unsafe_get())
|
||||
.canvas_state
|
||||
.borrow_for_layout()
|
||||
.get_canvas_id()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -747,26 +804,20 @@ impl CanvasRenderingContext2DMethods for CanvasRenderingContext2D {
|
|||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-context-2d-fillrect
|
||||
fn FillRect(&self, x: f64, y: f64, width: f64, height: f64) {
|
||||
if let Some(rect) = self.create_drawable_rect(x, y, width, height) {
|
||||
self.send_canvas_2d_msg(Canvas2dMsg::FillRect(rect));
|
||||
self.mark_as_dirty();
|
||||
}
|
||||
self.canvas_state.borrow().FillRect(x, y, width, height);
|
||||
self.mark_as_dirty();
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-context-2d-clearrect
|
||||
fn ClearRect(&self, x: f64, y: f64, width: f64, height: f64) {
|
||||
if let Some(rect) = self.create_drawable_rect(x, y, width, height) {
|
||||
self.send_canvas_2d_msg(Canvas2dMsg::ClearRect(rect));
|
||||
self.mark_as_dirty();
|
||||
}
|
||||
self.canvas_state.borrow().ClearRect(x, y, width, height);
|
||||
self.mark_as_dirty();
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-context-2d-strokerect
|
||||
fn StrokeRect(&self, x: f64, y: f64, width: f64, height: f64) {
|
||||
if let Some(rect) = self.create_drawable_rect(x, y, width, height) {
|
||||
self.send_canvas_2d_msg(Canvas2dMsg::StrokeRect(rect));
|
||||
self.mark_as_dirty();
|
||||
}
|
||||
self.canvas_state.borrow().StrokeRect(x, y, width, height);
|
||||
self.mark_as_dirty();
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-context-2d-beginpath
|
||||
|
@ -1458,8 +1509,10 @@ impl CanvasRenderingContext2DMethods for CanvasRenderingContext2D {
|
|||
impl Drop for CanvasRenderingContext2D {
|
||||
fn drop(&mut self) {
|
||||
if let Err(err) = self
|
||||
.canvas_state
|
||||
.borrow()
|
||||
.ipc_renderer
|
||||
.send(CanvasMsg::Close(self.get_canvas_id()))
|
||||
.send(CanvasMsg::Close(self.canvas_state.borrow().get_canvas_id()))
|
||||
{
|
||||
warn!("Could not close canvas: {}", err)
|
||||
}
|
||||
|
|
|
@ -2,10 +2,12 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use crate::dom::bindings::cell::DomRefCell;
|
||||
use crate::dom::bindings::codegen::Bindings::OffscreenCanvasRenderingContext2DBinding;
|
||||
use crate::dom::bindings::codegen::Bindings::OffscreenCanvasRenderingContext2DBinding::OffscreenCanvasRenderingContext2DMethods;
|
||||
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
|
||||
use crate::dom::bindings::root::{Dom, DomRoot};
|
||||
use crate::dom::canvasrenderingcontext2d::CanvasState;
|
||||
use crate::dom::globalscope::GlobalScope;
|
||||
use crate::dom::offscreencanvas::OffscreenCanvas;
|
||||
use dom_struct::dom_struct;
|
||||
|
@ -15,33 +17,35 @@ use euclid::Size2D;
|
|||
pub struct OffscreenCanvasRenderingContext2D {
|
||||
reflector_: Reflector,
|
||||
canvas: Option<Dom<OffscreenCanvas>>,
|
||||
canvas_state: DomRefCell<CanvasState>,
|
||||
}
|
||||
|
||||
impl OffscreenCanvasRenderingContext2D {
|
||||
pub fn new_inherited(
|
||||
_global: &GlobalScope,
|
||||
global: &GlobalScope,
|
||||
canvas: Option<&OffscreenCanvas>,
|
||||
_size: Size2D<u64>,
|
||||
size: Size2D<u64>,
|
||||
) -> OffscreenCanvasRenderingContext2D {
|
||||
OffscreenCanvasRenderingContext2D {
|
||||
reflector_: Reflector::new(),
|
||||
canvas: canvas.map(Dom::from_ref),
|
||||
canvas_state: DomRefCell::new(CanvasState::new(global, size)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(
|
||||
_global: &GlobalScope,
|
||||
global: &GlobalScope,
|
||||
canvas: &OffscreenCanvas,
|
||||
_size: Size2D<u64>,
|
||||
size: Size2D<u64>,
|
||||
) -> DomRoot<OffscreenCanvasRenderingContext2D> {
|
||||
let boxed = Box::new(OffscreenCanvasRenderingContext2D::new_inherited(
|
||||
_global,
|
||||
global,
|
||||
Some(canvas),
|
||||
_size,
|
||||
size,
|
||||
));
|
||||
reflect_dom_object(
|
||||
boxed,
|
||||
_global,
|
||||
global,
|
||||
OffscreenCanvasRenderingContext2DBinding::Wrap,
|
||||
)
|
||||
}
|
||||
|
@ -52,4 +56,19 @@ impl OffscreenCanvasRenderingContext2DMethods for OffscreenCanvasRenderingContex
|
|||
fn Canvas(&self) -> DomRoot<OffscreenCanvas> {
|
||||
DomRoot::from_ref(self.canvas.as_ref().expect("No canvas."))
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-context-2d-fillrect
|
||||
fn FillRect(&self, x: f64, y: f64, width: f64, height: f64) {
|
||||
self.canvas_state.borrow().FillRect(x, y, width, height);
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-context-2d-clearrect
|
||||
fn ClearRect(&self, x: f64, y: f64, width: f64, height: f64) {
|
||||
self.canvas_state.borrow().ClearRect(x, y, width, height);
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-context-2d-strokerect
|
||||
fn StrokeRect(&self, x: f64, y: f64, width: f64, height: f64) {
|
||||
self.canvas_state.borrow().StrokeRect(x, y, width, height);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -111,7 +111,7 @@ interface CanvasFilters {
|
|||
//attribute DOMString filter; // (default "none")
|
||||
};
|
||||
|
||||
[Exposed=(PaintWorklet, Window), NoInterfaceObject]
|
||||
[Exposed=(PaintWorklet, Window, Worker), NoInterfaceObject]
|
||||
interface CanvasRect {
|
||||
// rects
|
||||
void clearRect(unrestricted double x, unrestricted double y, unrestricted double w, unrestricted double h);
|
||||
|
|
|
@ -8,6 +8,8 @@ interface OffscreenCanvasRenderingContext2D {
|
|||
//void commit();
|
||||
readonly attribute OffscreenCanvas canvas;
|
||||
};
|
||||
OffscreenCanvasRenderingContext2D implements CanvasRect;
|
||||
|
||||
|
||||
//OffscreenCanvasRenderingContext2D includes CanvasState;
|
||||
//OffscreenCanvasRenderingContext2D includes CanvasTransform;
|
||||
|
@ -16,7 +18,6 @@ interface OffscreenCanvasRenderingContext2D {
|
|||
//OffscreenCanvasRenderingContext2D includes CanvasFillStrokeStyles;
|
||||
//OffscreenCanvasRenderingContext2D includes CanvasShadowStyles;
|
||||
//OffscreenCanvasRenderingContext2D includes CanvasFilters;
|
||||
//OffscreenCanvasRenderingContext2D includes CanvasRect;
|
||||
//OffscreenCanvasRenderingContext2D includes CanvasDrawPath;
|
||||
//OffscreenCanvasRenderingContext2D includes CanvasText;
|
||||
//OffscreenCanvasRenderingContext2D includes CanvasDrawImage;
|
||||
|
@ -24,3 +25,7 @@ interface OffscreenCanvasRenderingContext2D {
|
|||
//OffscreenCanvasRenderingContext2D includes CanvasPathDrawingStyles;
|
||||
//OffscreenCanvasRenderingContext2D includes CanvasTextDrawingStyles;
|
||||
//OffscreenCanvasRenderingContext2D includes CanvasPath;
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue