From b7bbd71f79f98417508629100b1873586cec2782 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Fri, 24 Mar 2017 10:40:19 -0700 Subject: [PATCH] Update Canvas.drawVertices to match Skia's latest interface (flutter/engine#3505) Fixes https://github.com/flutter/flutter/issues/8809 --- engine/src/flutter/lib/ui/BUILD.gn | 2 + engine/src/flutter/lib/ui/dart_ui.cc | 2 + engine/src/flutter/lib/ui/painting.dart | 74 ++++++++------- engine/src/flutter/lib/ui/painting/canvas.cc | 31 ++----- engine/src/flutter/lib/ui/painting/canvas.h | 11 +-- .../src/flutter/lib/ui/painting/vertices.cc | 91 +++++++++++++++++++ engine/src/flutter/lib/ui/painting/vertices.h | 49 ++++++++++ 7 files changed, 197 insertions(+), 63 deletions(-) create mode 100644 engine/src/flutter/lib/ui/painting/vertices.cc create mode 100644 engine/src/flutter/lib/ui/painting/vertices.h diff --git a/engine/src/flutter/lib/ui/BUILD.gn b/engine/src/flutter/lib/ui/BUILD.gn index 8d13499ed4..72f1299419 100644 --- a/engine/src/flutter/lib/ui/BUILD.gn +++ b/engine/src/flutter/lib/ui/BUILD.gn @@ -43,6 +43,8 @@ source_set("ui") { "painting/shader.cc", "painting/shader.h", "painting/utils.h", + "painting/vertices.cc", + "painting/vertices.h", "semantics/semantics_node.cc", "semantics/semantics_node.h", "semantics/semantics_update.cc", diff --git a/engine/src/flutter/lib/ui/dart_ui.cc b/engine/src/flutter/lib/ui/dart_ui.cc index 02ec5ca496..f394412c69 100644 --- a/engine/src/flutter/lib/ui/dart_ui.cc +++ b/engine/src/flutter/lib/ui/dart_ui.cc @@ -17,6 +17,7 @@ #include "flutter/lib/ui/painting/path.h" #include "flutter/lib/ui/painting/picture.h" #include "flutter/lib/ui/painting/picture_recorder.h" +#include "flutter/lib/ui/painting/vertices.h" #include "flutter/lib/ui/semantics/semantics_update.h" #include "flutter/lib/ui/semantics/semantics_update_builder.h" #include "flutter/lib/ui/text/paragraph.h" @@ -65,6 +66,7 @@ void DartUI::InitForGlobal() { SceneBuilder::RegisterNatives(g_natives); SemanticsUpdate::RegisterNatives(g_natives); SemanticsUpdateBuilder::RegisterNatives(g_natives); + Vertices::RegisterNatives(g_natives); Window::RegisterNatives(g_natives); } } diff --git a/engine/src/flutter/lib/ui/painting.dart b/engine/src/flutter/lib/ui/painting.dart index f024779948..0a1382cd32 100644 --- a/engine/src/flutter/lib/ui/painting.dart +++ b/engine/src/flutter/lib/ui/painting.dart @@ -1074,6 +1074,43 @@ enum VertexMode { triangleFan, } +/// A set of vertex data used by [Canvas.drawVertices]. +class Vertices extends NativeFieldWrapperClass2 { + Vertices( + VertexMode mode, + List positions, + { + List textureCoordinates, + List colors, + List indices, + } + ) { + if (textureCoordinates != null && textureCoordinates.length != positions.length) + throw new ArgumentError("[positions] and [textureCoordinates] lengths must match"); + if (colors != null && colors.length != positions.length) + throw new ArgumentError("[positions] and [colors] lengths must match"); + if (indices != null && indices.any((int i) => i < 0 || i >= positions.length)) + throw new ArgumentError("[indices] values must be valid indices in the positions list"); + + Float32List encodedPositions = _encodePointList(positions); + Float32List encodedTexs = (textureCoordinates != null) ? + _encodePointList(textureCoordinates) : null; + Int32List encodedColors = (colors != null) ? _encodeColorList(colors) : null; + Int32List encodedIndices = (indices != null) ? new Int32List.fromList(indices) : null; + + _constructor(); + _init(mode.index, encodedPositions, encodedTexs, encodedColors, encodedIndices); + } + + void _constructor() native "Vertices_constructor"; + + void _init(int mode, + Float32List positions, + Float32List textureCoordinates, + Int32List colors, + Int32List indices) native "Vertices_init"; +} + /// Defines how a list of points is interpreted when drawing a set of points. /// /// Used by [Canvas.drawPoints]. @@ -1471,38 +1508,13 @@ class Canvas extends NativeFieldWrapperClass2 { int pointMode, Float32List points) native "Canvas_drawPoints"; - void drawVertices(VertexMode vertexMode, - List vertices, - List textureCoordinates, - List colors, - BlendMode blendMode, - List indicies, - Paint paint) { - final int vertexCount = vertices.length; - - if (textureCoordinates.isNotEmpty && textureCoordinates.length != vertexCount) - throw new ArgumentError("[vertices] and [textureCoordinates] lengths must match"); - if (colors.isNotEmpty && colors.length != vertexCount) - throw new ArgumentError("[vertices] and [colors] lengths must match"); - - final Float32List vertexBuffer = _encodePointList(vertices); - final Float32List textureCoordinateBuffer = textureCoordinates.isEmpty ? null : _encodePointList(textureCoordinates); - final Int32List colorBuffer = colors.isEmpty ? null : _encodeColorList(colors); - final Int32List indexBuffer = new Int32List.fromList(indicies); - - _drawVertices( - paint._objects, paint._data, vertexMode.index, vertexBuffer, - textureCoordinateBuffer, colorBuffer, blendMode.index, indexBuffer - ); + void drawVertices(Vertices vertices, BlendMode blendMode, Paint paint) { + _drawVertices(vertices, blendMode, paint._objects, paint._data); } - void _drawVertices(List paintObjects, - ByteData paintData, - int vertexMode, - Float32List vertices, - Float32List textureCoordinates, - Int32List colors, - int blendMode, - Int32List indicies) native "Canvas_drawVertices"; + void _drawVertices(Vertices vertices, + BlendMode blendMode, + List paintObjects, + ByteData paintData) native "Canvas_drawVertices"; // TODO(eseidel): Paint should be optional, but optional doesn't work. void drawAtlas(Image atlas, diff --git a/engine/src/flutter/lib/ui/painting/canvas.cc b/engine/src/flutter/lib/ui/painting/canvas.cc index c63f23e2b2..34d48d0c35 100644 --- a/engine/src/flutter/lib/ui/painting/canvas.cc +++ b/engine/src/flutter/lib/ui/painting/canvas.cc @@ -339,35 +339,16 @@ void Canvas::drawPoints(const Paint& paint, *paint.paint()); } -void Canvas::drawVertices(const Paint& paint, - const PaintData& paint_data, - SkCanvas::VertexMode vertex_mode, - const tonic::Float32List& vertices, - const tonic::Float32List& texture_coordinates, - const tonic::Int32List& colors, +void Canvas::drawVertices(const Vertices* vertices, SkBlendMode blend_mode, - const tonic::Int32List& indices) { + const Paint& paint, + const PaintData& paint_data) { if (!canvas_) return; - std::vector indices16; - indices16.reserve(indices.num_elements()); - for (int i = 0; i < indices.num_elements(); ++i) - indices16.push_back(indices.data()[i]); - - static_assert(sizeof(SkPoint) == sizeof(float) * 2, - "SkPoint doesn't use floats."); - static_assert(sizeof(SkColor) == sizeof(int32_t), - "SkColor doesn't use int32_t."); - - canvas_->drawVertices( - vertex_mode, - vertices.num_elements() / 2, // SkPoints have two floats. - reinterpret_cast(vertices.data()), - reinterpret_cast(texture_coordinates.data()), - reinterpret_cast(colors.data()), blend_mode, - indices16.empty() ? nullptr : indices16.data(), indices16.size(), - *paint.paint()); + canvas_->drawVertices(vertices->vertices(), + blend_mode, + *paint.paint()); } void Canvas::drawAtlas(const Paint& paint, diff --git a/engine/src/flutter/lib/ui/painting/canvas.h b/engine/src/flutter/lib/ui/painting/canvas.h index 1eeffb5e33..a47b28d8dd 100644 --- a/engine/src/flutter/lib/ui/painting/canvas.h +++ b/engine/src/flutter/lib/ui/painting/canvas.h @@ -10,6 +10,7 @@ #include "flutter/lib/ui/painting/picture.h" #include "flutter/lib/ui/painting/picture_recorder.h" #include "flutter/lib/ui/painting/rrect.h" +#include "flutter/lib/ui/painting/vertices.h" #include "lib/tonic/dart_wrappable.h" #include "lib/tonic/typed_data/float32_list.h" #include "lib/tonic/typed_data/float64_list.h" @@ -142,14 +143,10 @@ class Canvas : public ftl::RefCountedThreadSafe, SkCanvas::PointMode point_mode, const tonic::Float32List& points); - void drawVertices(const Paint& paint, - const PaintData& paint_data, - SkCanvas::VertexMode vertex_mode, - const tonic::Float32List& vertices, - const tonic::Float32List& texture_coordinates, - const tonic::Int32List& colors, + void drawVertices(const Vertices* vertices, SkBlendMode blend_mode, - const tonic::Int32List& indices); + const Paint& paint, + const PaintData& paint_data); void drawAtlas(const Paint& paint, const PaintData& paint_data, diff --git a/engine/src/flutter/lib/ui/painting/vertices.cc b/engine/src/flutter/lib/ui/painting/vertices.cc new file mode 100644 index 0000000000..7429359f9d --- /dev/null +++ b/engine/src/flutter/lib/ui/painting/vertices.cc @@ -0,0 +1,91 @@ +// Copyright 2017 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. + +#include "flutter/lib/ui/painting/vertices.h" + +#include "lib/tonic/dart_binding_macros.h" +#include "lib/tonic/dart_library_natives.h" + +namespace blink { + +namespace { + +std::unique_ptr DecodePoints( + const tonic::Float32List& coords) { + std::unique_ptr result; + if (coords.data()) { + result.reset(new SkPoint[coords.num_elements() / 2]); + for (int i = 0; i < coords.num_elements(); i += 2) + result[i / 2] = SkPoint::Make(coords[i], coords[i + 1]); + } + return result; +} + +template std::unique_ptr DecodeInts( + const tonic::Int32List& ints) { + std::unique_ptr result; + if (ints.data()) { + result.reset(new T[ints.num_elements()]); + for (int i = 0; i < ints.num_elements(); i++) + result[i] = ints[i]; + } + return result; +} + +} // namespace + +static void Vertices_constructor(Dart_NativeArguments args) { + DartCallConstructor(&Vertices::Create, args); +} + +IMPLEMENT_WRAPPERTYPEINFO(ui, Vertices); + +#define FOR_EACH_BINDING(V) \ + V(Vertices, init) + +FOR_EACH_BINDING(DART_NATIVE_CALLBACK) + +Vertices::Vertices() {} + +Vertices::~Vertices() {} + +void Vertices::RegisterNatives(tonic::DartLibraryNatives* natives) { + natives->Register({{"Vertices_constructor", Vertices_constructor, 1, true}, + FOR_EACH_BINDING(DART_REGISTER_NATIVE)}); +} + +ftl::RefPtr Vertices::Create() { + return ftl::MakeRefCounted(); +} + +void Vertices::init(SkCanvas::VertexMode vertex_mode, + const tonic::Float32List& positions, + const tonic::Float32List& texture_coordinates, + const tonic::Int32List& colors, + const tonic::Int32List& indices) { + std::unique_ptr sk_positions(DecodePoints(positions)); + std::unique_ptr sk_texs(DecodePoints(texture_coordinates)); + + std::unique_ptr sk_colors(DecodeInts(colors)); + std::unique_ptr sk_indices(DecodeInts(indices)); + + if (sk_indices) { + vertices_ = SkVertices::MakeIndexed(vertex_mode, + std::move(sk_positions), + std::move(sk_colors), + std::move(sk_texs), + positions.num_elements() / 2, + std::move(sk_indices), + indices.num_elements()); + + } else { + vertices_ = SkVertices::Make(vertex_mode, + std::move(sk_positions), + std::move(sk_colors), + std::move(sk_texs), + positions.num_elements() / 2); + } +} + +} // namespace blink diff --git a/engine/src/flutter/lib/ui/painting/vertices.h b/engine/src/flutter/lib/ui/painting/vertices.h new file mode 100644 index 0000000000..4dcef21550 --- /dev/null +++ b/engine/src/flutter/lib/ui/painting/vertices.h @@ -0,0 +1,49 @@ +// Copyright 2017 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 FLUTTER_LIB_UI_PAINTING_VERTICES_H_ +#define FLUTTER_LIB_UI_PAINTING_VERTICES_H_ + +#include "lib/tonic/dart_wrappable.h" +#include "lib/tonic/typed_data/float32_list.h" +#include "lib/tonic/typed_data/int32_list.h" +#include "third_party/skia/include/core/SkCanvas.h" +#include "third_party/skia/include/core/SkVertices.h" + +namespace tonic { +class DartLibraryNatives; +} // namespace tonic + +namespace blink { + +class Vertices : public ftl::RefCountedThreadSafe, + public tonic::DartWrappable { + DEFINE_WRAPPERTYPEINFO(); + FRIEND_MAKE_REF_COUNTED(Vertices); + + public: + ~Vertices() override; + + static void RegisterNatives(tonic::DartLibraryNatives* natives); + + static ftl::RefPtr Create(); + + void init( + SkCanvas::VertexMode vertex_mode, + const tonic::Float32List& positions, + const tonic::Float32List& texture_coordinates, + const tonic::Int32List& colors, + const tonic::Int32List& indices); + + const sk_sp& vertices() const { return vertices_; } + + private: + Vertices(); + + sk_sp vertices_; +}; + +} // namespace blink + +#endif // FLUTTER_LIB_UI_PAINTING_VERTICES_H_