Flutter Engine
vertices.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/lib/ui/painting/vertices.h"
6 
7 #include <algorithm>
8 
9 #include "flutter/lib/ui/ui_dart_state.h"
12 
13 namespace flutter {
14 
15 namespace {
16 
17 void DecodePoints(const tonic::Float32List& coords, SkPoint* points) {
18  for (int i = 0; i < coords.num_elements(); i += 2) {
19  points[i / 2] = SkPoint::Make(coords[i], coords[i + 1]);
20  }
21 }
22 
23 template <typename T>
24 void DecodeInts(const tonic::Int32List& ints, T* out) {
25  for (int i = 0; i < ints.num_elements(); i++) {
26  out[i] = ints[i];
27  }
28 }
29 
30 } // namespace
31 
32 IMPLEMENT_WRAPPERTYPEINFO(ui, Vertices);
33 
34 #define FOR_EACH_BINDING(V) V(Vertices, init)
35 
37 
38 Vertices::Vertices() {}
39 
41 
44 }
45 
46 bool Vertices::init(Dart_Handle vertices_handle,
47  SkVertices::VertexMode vertex_mode,
48  const tonic::Float32List& positions,
49  const tonic::Float32List& texture_coordinates,
50  const tonic::Int32List& colors,
51  const tonic::Uint16List& indices) {
53  uint32_t builderFlags = 0;
54  if (texture_coordinates.data()) {
55  builderFlags |= SkVertices::kHasTexCoords_BuilderFlag;
56  }
57  if (colors.data()) {
58  builderFlags |= SkVertices::kHasColors_BuilderFlag;
59  }
60 
61  SkVertices::Builder builder(vertex_mode, positions.num_elements() / 2,
62  indices.num_elements(), builderFlags);
63 
64  if (!builder.isValid()) {
65  return false;
66  }
67 
68  // positions are required for SkVertices::Builder
69  FML_DCHECK(positions.data());
70  if (positions.data()) {
71  DecodePoints(positions, builder.positions());
72  }
73 
74  if (texture_coordinates.data()) {
75  // SkVertices::Builder assumes equal numbers of elements
76  FML_DCHECK(positions.num_elements() == texture_coordinates.num_elements());
77  DecodePoints(texture_coordinates, builder.texCoords());
78  }
79  if (colors.data()) {
80  // SkVertices::Builder assumes equal numbers of elements
81  FML_DCHECK(positions.num_elements() / 2 == colors.num_elements());
82  DecodeInts<SkColor>(colors, builder.colors());
83  }
84 
85  if (indices.data()) {
86  std::copy(indices.data(), indices.data() + indices.num_elements(),
87  builder.indices());
88  }
89 
90  auto vertices = fml::MakeRefCounted<Vertices>();
91  vertices->vertices_ = builder.detach();
92  vertices->AssociateWithDartWrapper(vertices_handle);
93 
94  return true;
95 }
96 
98  return sizeof(Vertices) + vertices_->approximateSize();
99 }
100 
101 } // namespace flutter
#define FOR_EACH_BINDING(V)
Definition: vertices.cc:34
static void RegisterNatives(tonic::DartLibraryNatives *natives)
Definition: vertices.cc:42
#define FML_DCHECK(condition)
Definition: logging.h:86
const sk_sp< SkVertices > & vertices() const
Definition: vertices.h:34
#define DART_NATIVE_CALLBACK(CLASS, METHOD)
IMPLEMENT_WRAPPERTYPEINFO(ui, Scene)
void Register(std::initializer_list< Entry > entries)
static void ThrowIfUIOperationsProhibited()
~Vertices() override
Definition: vertices.cc:40
#define DART_REGISTER_NATIVE(CLASS, METHOD)
size_t GetAllocationSize() const override
Definition: vertices.cc:97
static bool init(Dart_Handle vertices_handle, SkVertices::VertexMode vertex_mode, const tonic::Float32List &positions, const tonic::Float32List &texture_coordinates, const tonic::Int32List &colors, const tonic::Uint16List &indices)
Definition: vertices.cc:46