Flutter Impeller
geometry.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 
6 
7 #include <optional>
8 
14 #include "impeller/geometry/rect.h"
15 
16 namespace impeller {
17 
18 /// Given a convex polyline, create a triangle fan structure.
19 std::pair<std::vector<Point>, std::vector<uint16_t>> TessellateConvex(
20  Path::Polyline polyline) {
21  std::vector<Point> output;
22  std::vector<uint16_t> indices;
23 
24  for (auto j = 0u; j < polyline.contours.size(); j++) {
25  auto [start, end] = polyline.GetContourPointBounds(j);
26  auto center = polyline.points[start];
27 
28  // Some polygons will not self close and an additional triangle
29  // must be inserted, others will self close and we need to avoid
30  // inserting an extra triangle.
31  if (polyline.points[end - 1] == polyline.points[start]) {
32  end--;
33  }
34  output.emplace_back(center);
35  output.emplace_back(polyline.points[start + 1]);
36 
37  for (auto i = start + 2; i < end; i++) {
38  const auto& point_b = polyline.points[i];
39  output.emplace_back(point_b);
40 
41  indices.emplace_back(0);
42  indices.emplace_back(i - 1);
43  indices.emplace_back(i);
44  }
45  }
46  return std::make_pair(output, indices);
47 }
48 
49 VertexBufferBuilder<TextureFillVertexShader::PerVertexData>
52  Point texture_origin,
53  Size texture_coverage,
54  Matrix effect_transform) {
56  vertex_builder.Reserve(input.GetVertexCount());
57  input.IterateVertices(
58  [&vertex_builder, &texture_coverage, &effect_transform,
59  &texture_origin](SolidFillVertexShader::PerVertexData old_vtx) {
60  TextureFillVertexShader::PerVertexData data;
61  data.position = old_vtx.position;
62  data.texture_coords = effect_transform *
63  (old_vtx.position - texture_origin) /
64  texture_coverage;
65  vertex_builder.AppendVertex(data);
66  });
67  return vertex_builder;
68 }
69 
71  Rect texture_coverage,
72  Matrix effect_transform,
73  const ContentContext& renderer,
74  const Entity& entity,
75  RenderPass& pass) {
76  auto& host_buffer = pass.GetTransientsBuffer();
77 
78  std::vector<Point> data(8);
79  auto points = source_rect.GetPoints();
80  for (auto i = 0u, j = 0u; i < 8; i += 2, j++) {
81  data[i] = points[j];
82  data[i + 1] = effect_transform * (points[j] - texture_coverage.origin) /
83  texture_coverage.size;
84  }
85 
86  return GeometryResult{
88  .vertex_buffer =
89  {
90  .vertex_buffer = host_buffer.Emplace(
91  data.data(), 16 * sizeof(float), alignof(float)),
92  .vertex_count = 4,
93  .index_type = IndexType::kNone,
94  },
95  .transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
96  entity.GetTransformation(),
97  .prevent_overdraw = false,
98  };
99 }
100 
101 Geometry::Geometry() = default;
102 
103 Geometry::~Geometry() = default;
104 
106  Matrix effect_transform,
107  const ContentContext& renderer,
108  const Entity& entity,
109  RenderPass& pass) {
110  return {};
111 }
112 
113 std::unique_ptr<Geometry> Geometry::MakeFillPath(
114  const Path& path,
115  std::optional<Rect> inner_rect) {
116  return std::make_unique<FillPathGeometry>(path, inner_rect);
117 }
118 
119 std::unique_ptr<Geometry> Geometry::MakePointField(std::vector<Point> points,
120  Scalar radius,
121  bool round) {
122  return std::make_unique<PointFieldGeometry>(std::move(points), radius, round);
123 }
124 
125 std::unique_ptr<Geometry> Geometry::MakeStrokePath(const Path& path,
126  Scalar stroke_width,
127  Scalar miter_limit,
128  Cap stroke_cap,
129  Join stroke_join) {
130  // Skia behaves like this.
131  if (miter_limit < 0) {
132  miter_limit = 4.0;
133  }
134  return std::make_unique<StrokePathGeometry>(path, stroke_width, miter_limit,
135  stroke_cap, stroke_join);
136 }
137 
138 std::unique_ptr<Geometry> Geometry::MakeCover() {
139  return std::make_unique<CoverGeometry>();
140 }
141 
142 std::unique_ptr<Geometry> Geometry::MakeRect(Rect rect) {
143  return std::make_unique<RectGeometry>(rect);
144 }
145 
146 bool Geometry::CoversArea(const Matrix& transform, const Rect& rect) const {
147  return false;
148 }
149 
150 } // namespace impeller
impeller::TRect::size
TSize< Type > size
Definition: rect.h:24
impeller::Scalar
float Scalar
Definition: scalar.h:15
stroke_path_geometry.h
impeller::Geometry::~Geometry
virtual ~Geometry()
impeller::VertexBufferBuilder::GetVertexCount
size_t GetVertexCount() const
Definition: vertex_buffer_builder.h:53
cover_geometry.h
point_field_geometry.h
impeller::Geometry::MakeRect
static std::unique_ptr< Geometry > MakeRect(Rect rect)
Definition: geometry.cc:142
impeller::TessellateConvex
std::pair< std::vector< Point >, std::vector< uint16_t > > TessellateConvex(Path::Polyline polyline)
Given a convex polyline, create a triangle fan structure.
Definition: geometry.cc:19
rect_geometry.h
impeller::Geometry::CoversArea
virtual bool CoversArea(const Matrix &transform, const Rect &rect) const
Determines if this geometry, transformed by the given transform, will completely cover all surface ar...
Definition: geometry.cc:146
impeller::ComputeUVGeometryCPU
VertexBufferBuilder< TextureFillVertexShader::PerVertexData > ComputeUVGeometryCPU(VertexBufferBuilder< SolidFillVertexShader::PerVertexData > &input, Point texture_origin, Size texture_coverage, Matrix effect_transform)
Compute UV geometry for a VBB that contains only position geometry.
Definition: geometry.cc:50
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
impeller::TRect::GetPoints
constexpr std::array< TPoint< T >, 4 > GetPoints() const
Get the points that represent the 4 corners of this rectangle. The order is: Top left,...
Definition: rect.h:187
impeller::Entity::GetTransformation
const Matrix & GetTransformation() const
Definition: entity.cc:49
impeller::Path::Polyline
Definition: path.h:78
impeller::Entity
Definition: entity.h:21
impeller::RenderPass::GetRenderTargetSize
ISize GetRenderTargetSize() const
Definition: render_pass.cc:30
impeller::TSize
Definition: size.h:18
impeller::GeometryResult::type
PrimitiveType type
Definition: geometry.h:19
impeller::Path
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:54
impeller::Geometry::MakeStrokePath
static std::unique_ptr< Geometry > MakeStrokePath(const Path &path, Scalar stroke_width=0.0, Scalar miter_limit=4.0, Cap stroke_cap=Cap::kButt, Join stroke_join=Join::kMiter)
Definition: geometry.cc:125
impeller::Path::Polyline::points
std::vector< Point > points
Definition: path.h:81
impeller::Geometry::Geometry
Geometry()
impeller::VertexBufferBuilder
Definition: vertex_buffer_builder.h:23
impeller::Path::Polyline::GetContourPointBounds
std::tuple< size_t, size_t > GetContourPointBounds(size_t contour_index) const
Definition: path.cc:20
geometry.h
impeller::GeometryResult
Definition: geometry.h:18
impeller::IndexType::kNone
@ kNone
Does not use the index buffer.
impeller::TRect::origin
TPoint< Type > origin
Definition: rect.h:23
fill_path_geometry.h
impeller::Geometry::MakeCover
static std::unique_ptr< Geometry > MakeCover()
Definition: geometry.cc:138
impeller::Geometry::GetPositionUVBuffer
virtual GeometryResult GetPositionUVBuffer(Rect texture_coverage, Matrix effect_transform, const ContentContext &renderer, const Entity &entity, RenderPass &pass)
Definition: geometry.cc:105
impeller::VertexBufferBuilder::IterateVertices
void IterateVertices(const std::function< void(VertexType &)> &iterator)
Definition: vertex_buffer_builder.h:97
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:27
impeller::Geometry::MakePointField
static std::unique_ptr< Geometry > MakePointField(std::vector< Point > points, Scalar radius, bool round)
Definition: geometry.cc:119
impeller::Join
Join
Definition: path.h:23
rect.h
impeller::TPoint< Scalar >
impeller::Geometry::MakeFillPath
static std::unique_ptr< Geometry > MakeFillPath(const Path &path, std::optional< Rect > inner_rect=std::nullopt)
Definition: geometry.cc:113
impeller::Path::Polyline::contours
std::vector< PolylineContour > contours
Definition: path.h:82
impeller::Matrix::MakeOrthographic
static constexpr Matrix MakeOrthographic(TSize< T > size)
Definition: matrix.h:448
impeller::VertexBufferBuilder::AppendVertex
VertexBufferBuilder & AppendVertex(VertexType_ vertex)
Definition: vertex_buffer_builder.h:59
impeller::VertexBufferBuilder::Reserve
void Reserve(size_t count)
Definition: vertex_buffer_builder.h:47
impeller::ComputeUVGeometryForRect
GeometryResult ComputeUVGeometryForRect(Rect source_rect, Rect texture_coverage, Matrix effect_transform, const ContentContext &renderer, const Entity &entity, RenderPass &pass)
Definition: geometry.cc:70
impeller
Definition: aiks_context.cc:10
impeller::ContentContext
Definition: content_context.h:344
impeller::TRect
Definition: rect.h:20
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:36
impeller::RenderPass::GetTransientsBuffer
HostBuffer & GetTransientsBuffer()
Definition: render_pass.cc:34
impeller::Cap
Cap
Definition: path.h:17