Update Canvas.drawVertices to match Skia's latest interface (flutter/engine#3505)

Fixes https://github.com/flutter/flutter/issues/8809
This commit is contained in:
Jason Simmons
2017-03-24 10:40:19 -07:00
committed by GitHub
parent 929be47132
commit b7bbd71f79
7 changed files with 197 additions and 63 deletions

View File

@@ -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",

View File

@@ -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);
}
}

View File

@@ -1074,6 +1074,43 @@ enum VertexMode {
triangleFan,
}
/// A set of vertex data used by [Canvas.drawVertices].
class Vertices extends NativeFieldWrapperClass2 {
Vertices(
VertexMode mode,
List<Point> positions,
{
List<Point> textureCoordinates,
List<Color> colors,
List<int> 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<Point> vertices,
List<Point> textureCoordinates,
List<Color> colors,
BlendMode blendMode,
List<int> 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<dynamic> 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<dynamic> paintObjects,
ByteData paintData) native "Canvas_drawVertices";
// TODO(eseidel): Paint should be optional, but optional doesn't work.
void drawAtlas(Image atlas,

View File

@@ -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<uint16_t> 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<const SkPoint*>(vertices.data()),
reinterpret_cast<const SkPoint*>(texture_coordinates.data()),
reinterpret_cast<const SkColor*>(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,

View File

@@ -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<Canvas>,
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,

View File

@@ -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<SkPoint[]> DecodePoints(
const tonic::Float32List& coords) {
std::unique_ptr<SkPoint[]> 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 <typename T> std::unique_ptr<T[]> DecodeInts(
const tonic::Int32List& ints) {
std::unique_ptr<T[]> 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> Vertices::Create() {
return ftl::MakeRefCounted<Vertices>();
}
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<const SkPoint[]> sk_positions(DecodePoints(positions));
std::unique_ptr<const SkPoint[]> sk_texs(DecodePoints(texture_coordinates));
std::unique_ptr<const SkColor[]> sk_colors(DecodeInts<SkColor>(colors));
std::unique_ptr<const uint16_t[]> sk_indices(DecodeInts<uint16_t>(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

View File

@@ -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<Vertices>,
public tonic::DartWrappable {
DEFINE_WRAPPERTYPEINFO();
FRIEND_MAKE_REF_COUNTED(Vertices);
public:
~Vertices() override;
static void RegisterNatives(tonic::DartLibraryNatives* natives);
static ftl::RefPtr<Vertices> 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<SkVertices>& vertices() const { return vertices_; }
private:
Vertices();
sk_sp<SkVertices> vertices_;
};
} // namespace blink
#endif // FLUTTER_LIB_UI_PAINTING_VERTICES_H_