Skip to content

Commit

Permalink
Implement Canvas.drawPoints
Browse files Browse the repository at this point in the history
This enables drawing polygons faster than using Paths.

Fixes flutter/flutter#92
  • Loading branch information
abarth committed Jun 12, 2016
1 parent f7405a3 commit d0730da
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
24 changes: 23 additions & 1 deletion sky/engine/core/dart/painting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -433,13 +433,29 @@ class ImageShader extends Shader {
}

/// Defines how a list of points is interpreted when drawing a set of triangles.
/// See Skia or OpenGL documentation for more details.
enum VertexMode {
/// Draw each sequence of three points as the vertices of a triangle.
triangles,

/// Draw each sliding window of three points as the vertices of a triangle.
triangleStrip,

/// Draw the first point and each sliding window of two points as the vertices of a triangle.
triangleFan,
}

/// Defines how a list of points is interpreted when drawing a set of points.
enum PointMode {
// Draw each point separately.
points,

// Draw each sequence of two points as a line segment.
lines,

// Draw the entire sequence of point as the vetices of a polygon.
polygon,
}

/// An interface for recording graphical operations.
///
/// [Canvas] objects are used in creating [Picture] objects, which can
Expand Down Expand Up @@ -742,6 +758,12 @@ class Canvas extends NativeFieldWrapperClass2 {
}
void _drawParagraph(Paragraph paragraph, double x, double y) native "Canvas_drawParagraph";

/// Draws a sequence of points according to the given [PointMode].
void drawPoints(PointMode pointMode, List<Point> points, Paint paint) {
_drawPoints(pointMode.index, _encodePointList(points), paint);
}
void _drawPoints(int pointMode, Float32List points, Paint paint) native "Canvas_drawPoints";

void drawVertices(VertexMode vertexMode,
List<Point> vertices,
List<Point> textureCoordinates,
Expand Down
17 changes: 17 additions & 0 deletions sky/engine/core/painting/Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ IMPLEMENT_WRAPPERTYPEINFO(ui, Canvas);
V(Canvas, drawImageNine) \
V(Canvas, drawPicture) \
V(Canvas, drawParagraph) \
V(Canvas, drawPoints) \
V(Canvas, drawVertices) \
V(Canvas, drawAtlas)

Expand Down Expand Up @@ -329,6 +330,22 @@ void Canvas::drawParagraph(Paragraph* paragraph, double x, double y) {
paragraph->paint(this, x, y);
}

void Canvas::drawPoints(SkCanvas::PointMode pointMode,
const Float32List& points,
const Paint& paint) {
if (!m_canvas)
return;

static_assert(sizeof(SkPoint) == sizeof(float) * 2, "SkPoint doesn't use floats.");

m_canvas->drawPoints(
pointMode,
points.num_elements(),
reinterpret_cast<const SkPoint*>(points.data()),
*paint.paint()
);
}

void Canvas::drawVertices(
SkCanvas::VertexMode vertexMode,
const Float32List& vertices,
Expand Down
7 changes: 7 additions & 0 deletions sky/engine/core/painting/Canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class CanvasImage;
class DartLibraryNatives;
class Paragraph;

template <>
struct DartConverter<SkCanvas::PointMode> : public DartConverterInteger<SkCanvas::PointMode> {};

template <>
struct DartConverter<SkCanvas::VertexMode> : public DartConverterInteger<SkCanvas::VertexMode> {};

Expand Down Expand Up @@ -101,6 +104,10 @@ class Canvas : public base::RefCountedThreadSafe<Canvas>, public DartWrappable {
void drawPicture(Picture* picture);
void drawParagraph(Paragraph* paragraph, double x, double y);

void drawPoints(SkCanvas::PointMode pointMode,
const Float32List& points,
const Paint& paint);

void drawVertices(SkCanvas::VertexMode vertexMode,
const Float32List& vertices,
const Float32List& textureCoordinates,
Expand Down

0 comments on commit d0730da

Please sign in to comment.