-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Impeller] Add direct tesselation of circles for DrawCircle and Round…
… end caps (#48103) Note: The GPU tesselation for drawPoints is disabled in this test PR, but will be re-enabled when I can run some benchmarks to see where a good cutoff exists. Tessellation for circular or quarter circular paths is now handled by a single tessellator class that provides services for DrawCircle, DrawPoints (round caps), and DrawLines (round caps). Creating a general path is avoided along with the associated overhead of recognizing and using a generic tessellation step.
- Loading branch information
Showing
22 changed files
with
1,018 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Copyright 2013 The Flutter 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 "impeller/entity/geometry/ellipse_geometry.h" | ||
|
||
#include "flutter/impeller/tessellator/circle_tessellator.h" | ||
|
||
namespace impeller { | ||
|
||
EllipseGeometry::EllipseGeometry(Point center, Scalar radius) | ||
: center_(center), radius_(radius) { | ||
FML_DCHECK(radius >= 0); | ||
} | ||
|
||
GeometryResult EllipseGeometry::GetPositionBuffer( | ||
const ContentContext& renderer, | ||
const Entity& entity, | ||
RenderPass& pass) const { | ||
auto& host_buffer = pass.GetTransientsBuffer(); | ||
using VT = SolidFillVertexShader::PerVertexData; | ||
|
||
Scalar radius = radius_; | ||
const Point& center = center_; | ||
std::shared_ptr<Tessellator> tessellator = renderer.GetTessellator(); | ||
CircleTessellator circle_tessellator(tessellator, entity.GetTransform(), | ||
radius_); | ||
size_t count = circle_tessellator.GetCircleVertexCount(); | ||
auto vertex_buffer = host_buffer.Emplace( | ||
count * sizeof(VT), alignof(VT), | ||
[&circle_tessellator, ¢er, radius](uint8_t* buffer) { | ||
auto vertices = reinterpret_cast<VT*>(buffer); | ||
circle_tessellator.GenerateCircleTriangleStrip( | ||
[&vertices](const Point& p) { // | ||
*vertices++ = { | ||
.position = p, | ||
}; | ||
}, | ||
center, radius); | ||
}); | ||
|
||
return GeometryResult{ | ||
.type = PrimitiveType::kTriangleStrip, | ||
.vertex_buffer = | ||
{ | ||
.vertex_buffer = vertex_buffer, | ||
.vertex_count = count, | ||
.index_type = IndexType::kNone, | ||
}, | ||
.transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * | ||
entity.GetTransform(), | ||
.prevent_overdraw = false, | ||
}; | ||
} | ||
|
||
// |Geometry| | ||
GeometryResult EllipseGeometry::GetPositionUVBuffer( | ||
Rect texture_coverage, | ||
Matrix effect_transform, | ||
const ContentContext& renderer, | ||
const Entity& entity, | ||
RenderPass& pass) const { | ||
auto& host_buffer = pass.GetTransientsBuffer(); | ||
using VT = TextureFillVertexShader::PerVertexData; | ||
auto uv_transform = | ||
texture_coverage.GetNormalizingTransform() * effect_transform; | ||
|
||
Scalar radius = radius_; | ||
const Point& center = center_; | ||
std::shared_ptr<Tessellator> tessellator = renderer.GetTessellator(); | ||
CircleTessellator circle_tessellator(tessellator, entity.GetTransform(), | ||
radius_); | ||
size_t count = circle_tessellator.GetCircleVertexCount(); | ||
auto vertex_buffer = host_buffer.Emplace( | ||
count * sizeof(VT), alignof(VT), | ||
[&circle_tessellator, &uv_transform, ¢er, radius](uint8_t* buffer) { | ||
auto vertices = reinterpret_cast<VT*>(buffer); | ||
circle_tessellator.GenerateCircleTriangleStrip( | ||
[&vertices, &uv_transform](const Point& p) { // | ||
*vertices++ = { | ||
.position = p, | ||
.texture_coords = uv_transform * p, | ||
}; | ||
}, | ||
center, radius); | ||
}); | ||
|
||
return GeometryResult{ | ||
.type = PrimitiveType::kTriangleStrip, | ||
.vertex_buffer = | ||
{ | ||
.vertex_buffer = vertex_buffer, | ||
.vertex_count = count, | ||
.index_type = IndexType::kNone, | ||
}, | ||
.transform = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * | ||
entity.GetTransform(), | ||
.prevent_overdraw = false, | ||
}; | ||
} | ||
|
||
GeometryVertexType EllipseGeometry::GetVertexType() const { | ||
return GeometryVertexType::kPosition; | ||
} | ||
|
||
std::optional<Rect> EllipseGeometry::GetCoverage( | ||
const Matrix& transform) const { | ||
Point corners[4]{ | ||
{center_.x, center_.y - radius_}, | ||
{center_.x + radius_, center_.y}, | ||
{center_.x, center_.y + radius_}, | ||
{center_.x - radius_, center_.y}, | ||
}; | ||
|
||
for (int i = 0; i < 4; i++) { | ||
corners[i] = transform * corners[i]; | ||
} | ||
return Rect::MakePointBounds(std::begin(corners), std::end(corners)); | ||
} | ||
|
||
bool EllipseGeometry::CoversArea(const Matrix& transform, | ||
const Rect& rect) const { | ||
return false; | ||
} | ||
|
||
bool EllipseGeometry::IsAxisAlignedRect() const { | ||
return false; | ||
} | ||
|
||
} // namespace impeller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#pragma once | ||
|
||
#include "impeller/entity/geometry/geometry.h" | ||
|
||
namespace impeller { | ||
|
||
class EllipseGeometry final : public Geometry { | ||
public: | ||
explicit EllipseGeometry(Point center, Scalar radius); | ||
|
||
~EllipseGeometry() = default; | ||
|
||
// |Geometry| | ||
bool CoversArea(const Matrix& transform, const Rect& rect) const override; | ||
|
||
// |Geometry| | ||
bool IsAxisAlignedRect() const override; | ||
|
||
private: | ||
// Computes the 4 corners of a rectangle that defines the line and | ||
// possibly extended endpoints which will be rendered under the given | ||
// transform, and returns true if such a rectangle is defined. | ||
// | ||
// The coordinates will be generated in the original coordinate system | ||
// of the line end points and the transform will only be used to determine | ||
// the minimum line width. | ||
// | ||
// For kButt and kSquare end caps the ends should always be exteded as | ||
// per that decoration, but for kRound caps the ends might be extended | ||
// if the goal is to get a conservative bounds and might not be extended | ||
// if the calling code is planning to draw the round caps on the ends. | ||
// | ||
// @return true if the transform and width were not degenerate | ||
bool ComputeCorners(Point corners[4], | ||
const Matrix& transform, | ||
bool extend_endpoints) const; | ||
|
||
// |Geometry| | ||
GeometryResult GetPositionBuffer(const ContentContext& renderer, | ||
const Entity& entity, | ||
RenderPass& pass) const override; | ||
|
||
// |Geometry| | ||
GeometryVertexType GetVertexType() const override; | ||
|
||
// |Geometry| | ||
std::optional<Rect> GetCoverage(const Matrix& transform) const override; | ||
|
||
// |Geometry| | ||
GeometryResult GetPositionUVBuffer(Rect texture_coverage, | ||
Matrix effect_transform, | ||
const ContentContext& renderer, | ||
const Entity& entity, | ||
RenderPass& pass) const override; | ||
|
||
Point center_; | ||
Scalar radius_; | ||
|
||
EllipseGeometry(const EllipseGeometry&) = delete; | ||
|
||
EllipseGeometry& operator=(const EllipseGeometry&) = delete; | ||
}; | ||
|
||
} // namespace impeller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.