From 1d6cd4bc010376fb60d955d56e7f889b8cf4afbf Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Tue, 8 Sep 2015 11:09:45 -0700 Subject: [PATCH] Adds bindings to Skia for VertexMode and drawVertices --- sky/engine/bindings/scripts/dart_types.py | 2 + sky/engine/core/core.gni | 2 + sky/engine/core/painting/Canvas.cpp | 55 +++++++++++++++++++++++ sky/engine/core/painting/Canvas.h | 10 +++++ sky/engine/core/painting/Canvas.idl | 1 + sky/engine/core/painting/VertexMode.dart | 11 +++++ sky/engine/core/painting/VertexMode.h | 21 +++++++++ 7 files changed, 102 insertions(+) create mode 100644 sky/engine/core/painting/VertexMode.dart create mode 100644 sky/engine/core/painting/VertexMode.h diff --git a/sky/engine/bindings/scripts/dart_types.py b/sky/engine/bindings/scripts/dart_types.py index a47a1e29839..5651eb419f0 100644 --- a/sky/engine/bindings/scripts/dart_types.py +++ b/sky/engine/bindings/scripts/dart_types.py @@ -126,6 +126,7 @@ CPP_SPECIAL_CONVERSION_RULES = { 'MojoDataPipeConsumer': 'mojo::ScopedDataPipeConsumerHandle', 'TileMode': 'SkShader::TileMode', 'TransferMode': 'SkXfermode::Mode', + 'VertexMode': 'SkCanvas::VertexMode', 'FilterQuality': 'SkFilterQuality', 'PaintingStyle': 'SkPaint::Style', # TODO(abarth): Give these better C++ types. @@ -386,6 +387,7 @@ DART_TO_CPP_VALUE = { 'Size': pass_by_value_format('Size'), 'TileMode': pass_by_value_format('TileMode', ''), 'TransferMode': pass_by_value_format('TransferMode', ''), + 'VertexMode': pass_by_value_format('VertexMode', ''), 'FilterQuality': pass_by_value_format('FilterQuality', ''), 'PaintingStyle': pass_by_value_format('PaintingStyle', ''), 'FontStyle': pass_by_value_format('FontStyle', ''), diff --git a/sky/engine/core/core.gni b/sky/engine/core/core.gni index 4aec360a375..87601dd7cca 100644 --- a/sky/engine/core/core.gni +++ b/sky/engine/core/core.gni @@ -440,6 +440,7 @@ sky_core_files = [ "painting/Size.cpp", "painting/Size.h", "painting/TransferMode.h", + "painting/VertexMode.h", "rendering/BidiRun.h", "rendering/BidiRunForLine.cpp", "rendering/BidiRunForLine.h", @@ -687,6 +688,7 @@ core_dart_files = get_path_info([ "painting/RSTransform.dart", "painting/Size.dart", "painting/TransferMode.dart", + "painting/VertexMode.dart", "text/FontStyle.dart", "text/FontWeight.dart", "text/TextAlign.dart", diff --git a/sky/engine/core/painting/Canvas.cpp b/sky/engine/core/painting/Canvas.cpp index 6e01c9fee28..300d14f0fa7 100644 --- a/sky/engine/core/painting/Canvas.cpp +++ b/sky/engine/core/painting/Canvas.cpp @@ -231,6 +231,61 @@ void Canvas::drawDrawable(Drawable* drawable) m_canvas->drawDrawable(drawable->toSkia()); } +void Canvas::drawVertices(SkCanvas::VertexMode vmode, + const Vector& vertices, + const Vector& texs, + const Vector& colors, + SkXfermode::Mode mode, + const Vector& indices, + const Paint& paint, + ExceptionState& es) +{ + if (!m_canvas) + return; + size_t vertexCount = vertices.size(); + + if (texs.size() && texs.size() != vertexCount) + return es.ThrowRangeError("vertices and texs lengths must match"); + if (colors.size() && colors.size() != vertexCount) + return es.ThrowRangeError("vertices and colors lengths must match"); + + Vector skVertices; + for (size_t x = 0; x < vertices.size(); x++) { + const Point& point = vertices[x]; + if (point.is_null) + return es.ThrowRangeError("vertices contained a null"); + skVertices.append(point.sk_point); + } + + Vector skTexs; + for (size_t x = 0; x < texs.size(); x++) { + const Point& point = texs[x]; + if (point.is_null) + return es.ThrowRangeError("texs contained a null"); + skTexs.append(point.sk_point); + } + + Vector skIndices; + for (size_t x = 0; x < indices.size(); x++) { + uint16_t i = indices[x]; + skIndices.append(i); + } + + RefPtr modePtr = adoptRef(SkXfermode::Create(mode)); + + m_canvas->drawVertices( + vmode, + skVertices.size(), + skVertices.data(), + skTexs.isEmpty() ? nullptr : skTexs.data(), + colors.isEmpty() ? nullptr : colors.data(), + modePtr.get(), + skIndices.isEmpty() ? nullptr : skIndices.data(), + skIndices.size(), + *paint.paint() + ); +} + void Canvas::drawAtlas(CanvasImage* atlas, const Vector& transforms, const Vector& rects, const Vector& colors, SkXfermode::Mode mode, diff --git a/sky/engine/core/painting/Canvas.h b/sky/engine/core/painting/Canvas.h index 35b13534bea..022b4bb643e 100644 --- a/sky/engine/core/painting/Canvas.h +++ b/sky/engine/core/painting/Canvas.h @@ -17,6 +17,7 @@ #include "sky/engine/core/painting/Rect.h" #include "sky/engine/core/painting/Size.h" #include "sky/engine/core/painting/RSTransform.h" +#include "sky/engine/core/painting/VertexMode.h" #include "sky/engine/platform/graphics/DisplayList.h" #include "sky/engine/tonic/dart_wrappable.h" #include "sky/engine/tonic/float32_list.h" @@ -91,6 +92,15 @@ public: void drawPicture(Picture* picture); void drawDrawable(Drawable* drawable); + void drawVertices(SkCanvas::VertexMode vmode, + const Vector& vertices, + const Vector& texs, + const Vector& colors, + SkXfermode::Mode mode, + const Vector& indices, + const Paint& paint, + ExceptionState& es); + void drawAtlas(CanvasImage* atlas, const Vector& transforms, const Vector& rects, const Vector& colors, SkXfermode::Mode mode, diff --git a/sky/engine/core/painting/Canvas.idl b/sky/engine/core/painting/Canvas.idl index e790046fd8f..6a0ea3a827c 100644 --- a/sky/engine/core/painting/Canvas.idl +++ b/sky/engine/core/painting/Canvas.idl @@ -39,6 +39,7 @@ void drawImageRect(Image image, Rect src, Rect dst, Paint paint); void drawPicture(Picture picture); void drawDrawable(Drawable drawable); + [RaisesException] void drawVertices(VertexMode vmode, sequence vertices, sequence texs, sequence colors, TransferMode mode, sequence indicies, Paint paint); // TODO(eseidel): Paint should be optional, but optional doesn't work. [RaisesException] void drawAtlas(Image image, diff --git a/sky/engine/core/painting/VertexMode.dart b/sky/engine/core/painting/VertexMode.dart new file mode 100644 index 00000000000..f1c781ed297 --- /dev/null +++ b/sky/engine/core/painting/VertexMode.dart @@ -0,0 +1,11 @@ +// 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. + +part of dart.sky; + +enum VertexMode { + triangles, + triangleStrip, + triangleFan, +} diff --git a/sky/engine/core/painting/VertexMode.h b/sky/engine/core/painting/VertexMode.h new file mode 100644 index 00000000000..9863b1457f8 --- /dev/null +++ b/sky/engine/core/painting/VertexMode.h @@ -0,0 +1,21 @@ +// 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. + +#ifndef SKY_ENGINE_CORE_PAINTING_VERTEXMODE_H_ +#define SKY_ENGINE_CORE_PAINTING_VERTEXMODE_H_ + +#include "sky/engine/tonic/dart_converter.h" +#include "third_party/skia/include/core/SkCanvas.h" + +namespace blink { + +class VertexMode {}; + +template <> +struct DartConverter + : public DartConverterEnum {}; + +} // namespace blink + +#endif // SKY_ENGINE_CORE_PAINTING_VERTEXMODE_H_