diff --git a/engine/core/painting/Canvas.cpp b/engine/core/painting/Canvas.cpp index 5e25baf4893..c3bde989568 100644 --- a/engine/core/painting/Canvas.cpp +++ b/engine/core/painting/Canvas.cpp @@ -13,6 +13,14 @@ namespace blink { +SkRect toSkRect(const Vector& rect) +{ + ASSERT(rect.size() == 4); + SkRect sk_rect; + sk_rect.set(rect[0], rect[1], rect[2], rect[3]); + return sk_rect; +} + Canvas::Canvas(const FloatSize& size) : m_size(size) { @@ -24,7 +32,113 @@ Canvas::~Canvas() { } -void Canvas::drawCircle(double x, double y, double radius, Paint* paint) +void Canvas::save() +{ + if (!m_canvas) + return; + ASSERT(m_displayList->isRecording()); + m_canvas->save(); +} + +void Canvas::saveLayer(const Vector& bounds, const Paint* paint) +{ + if (!m_canvas) + return; + ASSERT(m_displayList->isRecording()); + SkRect sk_bounds; + if (!bounds.isEmpty()) + sk_bounds = toSkRect(bounds); + m_canvas->saveLayer(!bounds.isEmpty() ? &sk_bounds : nullptr, + paint ? &paint->paint() : nullptr); +} + +void Canvas::restore() +{ + if (!m_canvas) + return; + ASSERT(m_displayList->isRecording()); + m_canvas->restore(); +} + +void Canvas::translate(float dx, float dy) +{ + if (!m_canvas) + return; + ASSERT(m_displayList->isRecording()); + m_canvas->translate(dx, dy); +} + +void Canvas::scale(float sx, float sy) +{ + if (!m_canvas) + return; + ASSERT(m_displayList->isRecording()); + m_canvas->scale(sx, sy); +} + +void Canvas::rotateDegrees(float degrees) +{ + if (!m_canvas) + return; + ASSERT(m_displayList->isRecording()); + m_canvas->rotate(degrees); +} + +void Canvas::skew(float sx, float sy) +{ + if (!m_canvas) + return; + ASSERT(m_displayList->isRecording()); + m_canvas->skew(sx, sy); +} + +void Canvas::concat(const Vector& matrix) +{ + if (!m_canvas) + return; + ASSERT(m_displayList->isRecording()); + ASSERT(matrix.size() == 9); + SkMatrix sk_matrix; + sk_matrix.set9(matrix.data()); + m_canvas->concat(sk_matrix); +} + +void Canvas::clipRect(const Vector& rect) +{ + if (!m_canvas) + return; + ASSERT(m_displayList->isRecording()); + m_canvas->clipRect(toSkRect(rect)); +} + +void Canvas::drawPaint(Paint* paint) +{ + if (!m_canvas) + return; + ASSERT(paint); + ASSERT(m_displayList->isRecording()); + m_canvas->drawPaint(paint->paint()); +} + +void Canvas::drawRect(const Vector& rect, const Paint* paint) +{ + if (!m_canvas) + return; + ASSERT(paint); + ASSERT(m_displayList->isRecording()); + m_canvas->drawRect(toSkRect(rect), paint->paint()); +} + +void Canvas::drawOval(const Vector& rect, const Paint* paint) +{ + if (!m_canvas) + return; + ASSERT(paint); + ASSERT(m_displayList->isRecording()); + m_canvas->drawOval(toSkRect(rect), paint->paint()); +} + +void Canvas::drawCircle(float x, float y, float radius, Paint* paint) { if (!m_canvas) return; diff --git a/engine/core/painting/Canvas.h b/engine/core/painting/Canvas.h index 8cb0b21ba53..3775597ef87 100644 --- a/engine/core/painting/Canvas.h +++ b/engine/core/painting/Canvas.h @@ -22,10 +22,25 @@ public: // Width/Height define a culling rect which Skia may use for optimizing // out draw calls issued outside the rect. - double width() const { return m_size.width(); } - double height() const { return m_size.height(); } + float width() const { return m_size.width(); } + float height() const { return m_size.height(); } - void drawCircle(double x, double y, double radius, Paint* paint); + void save(); + void saveLayer(const Vector& bounds, const Paint* paint); + void restore(); + + void translate(float dx, float dy); + void scale(float sx, float sy); + void rotateDegrees(float degrees); + void skew(float sx, float sy); + void concat(const Vector& matrix); + + void clipRect(const Vector& rect); + + void drawPaint(Paint* paint); + void drawRect(const Vector& rect, const Paint* paint); + void drawOval(const Vector& rect, const Paint* paint); + void drawCircle(float x, float y, float radius, Paint* paint); protected: PassRefPtr finishRecording(); diff --git a/engine/core/painting/Canvas.idl b/engine/core/painting/Canvas.idl index 36fc06318df..92302ce3665 100644 --- a/engine/core/painting/Canvas.idl +++ b/engine/core/painting/Canvas.idl @@ -1,11 +1,28 @@ // Copyright 2015 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. + +// TODO(mpcomplete): Figure out a better SkMatrix/SkRect representation. interface Canvas { // Height and width are used for culling optimizations and do not necessarily // imply that the Canvas is backed by a buffer with any specific bounds. - readonly attribute double height; - readonly attribute double width; + readonly attribute float height; + readonly attribute float width; - void drawCircle(double x, double y, double radius, Paint paint); + void save(); + void saveLayer(float[] bounds4 /* optional */, Paint paint /* optional */); + void restore(); + + void translate(float dx, float dy); + void scale(float sx, float sy); + void rotateDegrees(float degrees); + void skew(float sx, float sy); + void concat(float[] matrix9); + + void clipRect(float[] rect4); + + void drawPaint(Paint paint); + void drawRect(float[] rect4, Paint paint); + void drawOval(float[] rect4, Paint paint); + void drawCircle(float x, float y, float radius, Paint paint); }; diff --git a/engine/core/painting/PaintingContext.h b/engine/core/painting/PaintingContext.h index 8d51be47c88..aab0a1dc605 100644 --- a/engine/core/painting/PaintingContext.h +++ b/engine/core/painting/PaintingContext.h @@ -6,6 +6,7 @@ #define SKY_ENGINE_CORE_PAINTING_PAINTINGCONTEXT_H_ #include "sky/engine/core/painting/Canvas.h" +#include "sky/engine/wtf/Vector.h" namespace blink { class Element; diff --git a/examples/raw/painting.sky b/examples/raw/painting.sky new file mode 100644 index 00000000000..6ff4002c642 --- /dev/null +++ b/examples/raw/painting.sky @@ -0,0 +1,46 @@ + + +
+ +