Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions impeller/aiks/aiks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ TEST_P(AiksTest, ColorWheel) {
ImGui::End();

Canvas canvas;
canvas.Scale(GetContentScale());
Paint paint;
// Default blend is kSourceOver.
paint.color = Color::White();
Expand Down Expand Up @@ -653,6 +654,7 @@ TEST_P(AiksTest, SolidStrokesRenderCorrectly) {
ImGui::End();

Canvas canvas;
canvas.Scale(GetContentScale());
Paint paint;

paint.color = Color::White();
Expand Down
3 changes: 3 additions & 0 deletions impeller/display_list/display_list_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ TEST_P(DisplayListTest, CanDrawArc) {
Point(200, 200), Point(400, 400), 20, Color::White(), Color::White());

flutter::DisplayListBuilder builder;

Vector2 scale = GetContentScale();
builder.scale(scale.x, scale.y);
builder.setStyle(flutter::DlDrawStyle::kStroke);
builder.setStrokeCap(flutter::DlStrokeCap::kRound);
builder.setStrokeJoin(flutter::DlStrokeJoin::kMiter);
Expand Down
26 changes: 20 additions & 6 deletions impeller/entity/entity_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ TEST_P(EntityTest, EntityPassSubpassCoverageIsCorrect) {

TEST_P(EntityTest, CanDrawRect) {
Entity entity;
entity.SetTransformation(Matrix::MakeScale(GetContentScale()));
entity.SetContents(SolidColorContents::Make(
PathBuilder{}.AddRect({100, 100, 100, 100}).TakePath(), Color::Red()));
ASSERT_TRUE(OpenPlaygroundHere(entity));
Expand All @@ -121,6 +122,7 @@ TEST_P(EntityTest, ThreeStrokesInOnePath) {
.TakePath();

Entity entity;
entity.SetTransformation(Matrix::MakeScale(GetContentScale()));
auto contents = std::make_unique<SolidStrokeContents>();
contents->SetPath(std::move(path));
contents->SetColor(Color::Red());
Expand Down Expand Up @@ -151,6 +153,7 @@ TEST_P(EntityTest, TriangleInsideASquare) {
.TakePath();

Entity entity;
entity.SetTransformation(Matrix::MakeScale(GetContentScale()));
auto contents = std::make_unique<SolidStrokeContents>();
contents->SetPath(std::move(path));
contents->SetColor(Color::Red());
Expand Down Expand Up @@ -192,7 +195,8 @@ TEST_P(EntityTest, StrokeCapAndJoinTest) {
}
ImGui::End();

auto render_path = [width = width, &context, &pass](
auto world_matrix = Matrix::MakeScale(GetContentScale());
auto render_path = [width = width, &context, &pass, &world_matrix](
Path path, SolidStrokeContents::Cap cap,
SolidStrokeContents::Join join) {
auto contents = std::make_unique<SolidStrokeContents>();
Expand All @@ -204,6 +208,7 @@ TEST_P(EntityTest, StrokeCapAndJoinTest) {
contents->SetStrokeMiter(miter_limit);

Entity entity;
entity.SetTransformation(world_matrix);
entity.SetContents(std::move(contents));

auto coverage = entity.GetCoverage();
Expand Down Expand Up @@ -314,6 +319,7 @@ TEST_P(EntityTest, CubicCurveTest) {
.Close()
.TakePath();
Entity entity;
entity.SetTransformation(Matrix::MakeScale(GetContentScale()));
entity.SetContents(SolidColorContents::Make(path, Color::Red()));
ASSERT_TRUE(OpenPlaygroundHere(entity));
}
Expand Down Expand Up @@ -541,6 +547,7 @@ TEST_P(EntityTest, CubicCurveAndOverlapTest) {
.Close()
.TakePath();
Entity entity;
entity.SetTransformation(Matrix::MakeScale(GetContentScale()));
entity.SetContents(SolidColorContents::Make(path, Color::Red()));
ASSERT_TRUE(OpenPlaygroundHere(entity));
}
Expand Down Expand Up @@ -643,8 +650,10 @@ TEST_P(EntityTest, BlendingModeOptions) {
ImGui::SetNextWindowPos({200, 450});
}

auto draw_rect = [&context, &pass](Rect rect, Color color,
Entity::BlendMode blend_mode) -> bool {
auto world_matrix = Matrix::MakeScale(GetContentScale());
auto draw_rect = [&context, &pass, &world_matrix](
Rect rect, Color color,
Entity::BlendMode blend_mode) -> bool {
using VS = SolidFillPipeline::VertexShader;
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
{
Expand All @@ -668,7 +677,8 @@ TEST_P(EntityTest, BlendingModeOptions) {
vtx_builder.CreateVertexBuffer(pass.GetTransientsBuffer()));

VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize());
frame_info.mvp =
Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * world_matrix;
frame_info.color = color.Premultiply();
VS::BindFrameInfo(cmd,
pass.GetTransientsBuffer().EmplaceUniform(frame_info));
Expand Down Expand Up @@ -710,6 +720,7 @@ TEST_P(EntityTest, BlendingModeOptions) {

TEST_P(EntityTest, BezierCircleScaled) {
Entity entity;
entity.SetTransformation(Matrix::MakeScale(GetContentScale()));
auto path = PathBuilder{}
.MoveTo({97.325, 34.818})
.CubicCurveTo({98.50862885295136, 34.81812293973836},
Expand Down Expand Up @@ -751,7 +762,8 @@ TEST_P(EntityTest, Filters) {
{fi_bridge, FilterInput::Make(blend0), fi_bridge, fi_bridge});

Entity entity;
entity.SetTransformation(Matrix::MakeTranslation({500, 300}) *
entity.SetTransformation(Matrix::MakeScale(GetContentScale()) *
Matrix::MakeTranslation({500, 300}) *
Matrix::MakeScale(Vector2{0.5, 0.5}));
entity.SetContents(blend1);
return entity.Render(context, pass);
Expand Down Expand Up @@ -823,7 +835,8 @@ TEST_P(EntityTest, GaussianBlurFilter) {
blur_styles[selected_blur_style]);

auto input_size = bridge->GetSize();
auto ctm = Matrix::MakeTranslation(Vector3(offset[0], offset[1])) *
auto ctm = Matrix::MakeScale(GetContentScale()) *
Matrix::MakeTranslation(Vector3(offset[0], offset[1])) *
Matrix::MakeRotationZ(Radians(rotation)) *
Matrix::MakeScale(Vector2(scale[0], scale[1])) *
Matrix::MakeSkew(skew[0], skew[1]) *
Expand Down Expand Up @@ -964,6 +977,7 @@ TEST_P(EntityTest, DrawVerticesSolidColorTrianglesWithoutIndex) {
std::make_shared<VerticesContents>(vertices);
contents->SetColor(Color::White());
Entity e;
e.SetTransformation(Matrix::MakeScale(GetContentScale()));
e.SetContents(contents);

ASSERT_TRUE(OpenPlaygroundHere(e));
Expand Down
2 changes: 1 addition & 1 deletion impeller/playground/backend/gles/playground_impl_gles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ std::shared_ptr<Context> PlaygroundImplGLES::GetContext() const {
}

// |PlaygroundImpl|
PlaygroundImpl::WindowHandle PlaygroundImplGLES::GetWindowHandle() {
PlaygroundImpl::WindowHandle PlaygroundImplGLES::GetWindowHandle() const {
return handle_.get();
}

Expand Down
2 changes: 1 addition & 1 deletion impeller/playground/backend/gles/playground_impl_gles.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PlaygroundImplGLES final : public PlaygroundImpl {
std::shared_ptr<Context> GetContext() const override;

// |PlaygroundImpl|
WindowHandle GetWindowHandle() override;
WindowHandle GetWindowHandle() const override;

// |PlaygroundImpl|
std::unique_ptr<Surface> AcquireSurfaceFrame(
Expand Down
2 changes: 1 addition & 1 deletion impeller/playground/backend/metal/playground_impl_mtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PlaygroundImplMTL final : public PlaygroundImpl {
std::shared_ptr<Context> GetContext() const override;

// |PlaygroundImpl|
WindowHandle GetWindowHandle() override;
WindowHandle GetWindowHandle() const override;

// |PlaygroundImpl|
std::unique_ptr<Surface> AcquireSurfaceFrame(
Expand Down
9 changes: 5 additions & 4 deletions impeller/playground/backend/metal/playground_impl_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
}

// |PlaygroundImpl|
PlaygroundImpl::WindowHandle PlaygroundImplMTL::GetWindowHandle() {
PlaygroundImpl::WindowHandle PlaygroundImplMTL::GetWindowHandle() const {
return handle_.get();
}

Expand All @@ -96,9 +96,10 @@
}

const auto layer_size = data_->metal_layer.bounds.size;
const auto layer_scale = data_->metal_layer.contentsScale;
data_->metal_layer.drawableSize = CGSizeMake(layer_size.width * layer_scale,
layer_size.height * layer_scale);
const auto scale = GetContentScale();
data_->metal_layer.drawableSize =
CGSizeMake(layer_size.width * scale.x, layer_size.height * scale.y);

return SurfaceMTL::WrapCurrentMetalLayerDrawable(context, data_->metal_layer);
}

Expand Down
76 changes: 45 additions & 31 deletions impeller/playground/imgui/imgui_impl_impeller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,23 @@ void ImGui_ImplImpeller_RenderDrawData(ImDrawData* draw_data,
impeller::StorageMode::kHostVisible, total_vtx_bytes + total_idx_bytes);
buffer->SetLabel(impeller::SPrintF("ImGui vertex+index buffer"));

auto display_rect =
impeller::Rect(draw_data->DisplayPos.x, draw_data->DisplayPos.y,
draw_data->DisplaySize.x, draw_data->DisplaySize.y);

auto viewport = impeller::Viewport{
.rect = impeller::Rect(
display_rect.origin.x * draw_data->FramebufferScale.x,
display_rect.origin.y * draw_data->FramebufferScale.y,
display_rect.size.width * draw_data->FramebufferScale.x,
display_rect.size.height * draw_data->FramebufferScale.y)};

// Allocate vertex shader uniform buffer.
VS::UniformBuffer uniforms;
uniforms.mvp = impeller::Matrix::MakeOrthographic(
impeller::Size(draw_data->DisplaySize.x, draw_data->DisplaySize.y));
uniforms.mvp = uniforms.mvp.Translate(
-impeller::Vector3(draw_data->DisplayPos.x, draw_data->DisplayPos.y));
uniforms.mvp = impeller::Matrix::MakeOrthographic(display_rect.size)
.Translate(-display_rect.origin);
auto vtx_uniforms =
render_pass.GetTransientsBuffer().EmplaceUniform(uniforms);

size_t vertex_buffer_offset = 0;
size_t index_buffer_offset = total_vtx_bytes;
Expand Down Expand Up @@ -185,49 +197,51 @@ void ImGui_ImplImpeller_RenderDrawData(ImDrawData* draw_data,
IM_ASSERT(false && "Could not copy indices to buffer.");
}

auto viewport = impeller::Viewport{
.rect =
impeller::Rect(draw_data->DisplayPos.x, draw_data->DisplayPos.y,
draw_data->DisplaySize.x, draw_data->DisplaySize.y)};

for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) {
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];

if (pcmd->UserCallback) {
pcmd->UserCallback(cmd_list, pcmd);
} else {
// Project scissor/clipping rectangles into framebuffer space.
impeller::IPoint clip_min(pcmd->ClipRect.x - draw_data->DisplayPos.x,
pcmd->ClipRect.y - draw_data->DisplayPos.y);
impeller::IPoint clip_max(pcmd->ClipRect.z - draw_data->DisplayPos.x,
pcmd->ClipRect.w - draw_data->DisplayPos.y);
// Ensure the scissor never goes out of bounds.
clip_min.x = std::clamp<impeller::IPoint::Type>(
clip_min.x, 0ll, draw_data->DisplaySize.x);
clip_min.y = std::clamp<impeller::IPoint::Type>(
clip_min.y, 0ll, draw_data->DisplaySize.y);
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y) {
continue; // Nothing to render.
// Make the clip rect relative to the viewport.
auto clip_rect = impeller::Rect::MakeLTRB(
(pcmd->ClipRect.x - draw_data->DisplayPos.x) *
draw_data->FramebufferScale.x,
(pcmd->ClipRect.y - draw_data->DisplayPos.y) *
draw_data->FramebufferScale.y,
(pcmd->ClipRect.z - draw_data->DisplayPos.x) *
draw_data->FramebufferScale.x,
(pcmd->ClipRect.w - draw_data->DisplayPos.y) *
draw_data->FramebufferScale.y);
{
// Clamp the clip to the viewport bounds.
auto visible_clip = clip_rect.Intersection(viewport.rect);
if (!visible_clip.has_value()) {
continue; // Nothing to render.
}
clip_rect = visible_clip.value();
}
{
// Clamp the clip to ensure it never goes outside of the render
// target.
auto visible_clip = clip_rect.Intersection(impeller::Rect::MakeSize(
impeller::Size(render_pass.GetRenderTargetSize())));
if (!visible_clip.has_value()) {
continue; // Nothing to render.
}
clip_rect = visible_clip.value();
}

impeller::Command cmd;
cmd.label = impeller::SPrintF("ImGui draw list %d (command %d)",
draw_list_i, cmd_i);

cmd.viewport = viewport;
cmd.scissor = impeller::IRect::MakeLTRB(
std::max<impeller::IRect::Type>(0ll, clip_min.x), //
std::max<impeller::IRect::Type>(0ll, clip_min.y), //
std::min<impeller::IRect::Type>(
render_pass.GetRenderTargetSize().width, clip_max.x), //
std::min<impeller::IRect::Type>(
render_pass.GetRenderTargetSize().height, clip_max.y) //
);
cmd.scissor = impeller::IRect(clip_rect);

cmd.winding = impeller::WindingOrder::kClockwise;
cmd.pipeline = bd->pipeline;
VS::BindUniformBuffer(
cmd, render_pass.GetTransientsBuffer().EmplaceUniform(uniforms));
VS::BindUniformBuffer(cmd, vtx_uniforms);
FS::BindTex(cmd, bd->font_texture, bd->sampler);

size_t vb_start =
Expand Down
4 changes: 4 additions & 0 deletions impeller/playground/playground.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ ISize Playground::GetWindowSize() const {
return window_size_;
}

Point Playground::GetContentScale() const {
return impl_->GetContentScale();
}

void Playground::SetCursorPosition(Point pos) {
cursor_position_ = pos;
}
Expand Down
2 changes: 2 additions & 0 deletions impeller/playground/playground.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class Playground : public ::testing::TestWithParam<PlaygroundBackend> {

ISize GetWindowSize() const;

Point GetContentScale() const;

std::shared_ptr<Context> GetContext() const;

bool OpenPlaygroundHere(Renderer::RenderCallback render_callback);
Expand Down
12 changes: 12 additions & 0 deletions impeller/playground/playground_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

#include "impeller/playground/playground_impl.h"

#define GLFW_INCLUDE_NONE
#include "third_party/glfw/include/GLFW/glfw3.h"

#if IMPELLER_ENABLE_METAL
#include "impeller/playground/backend/metal/playground_impl_mtl.h"
#endif // IMPELLER_ENABLE_METAL
Expand Down Expand Up @@ -37,4 +40,13 @@ PlaygroundImpl::PlaygroundImpl() = default;

PlaygroundImpl::~PlaygroundImpl() = default;

Vector2 PlaygroundImpl::GetContentScale() const {
auto window = reinterpret_cast<GLFWwindow*>(GetWindowHandle());

Vector2 scale(1, 1);
::glfwGetWindowContentScale(window, &scale.x, &scale.y);

return scale;
}

} // namespace impeller
4 changes: 3 additions & 1 deletion impeller/playground/playground_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ class PlaygroundImpl {

using WindowHandle = void*;

virtual WindowHandle GetWindowHandle() = 0;
virtual WindowHandle GetWindowHandle() const = 0;

virtual std::shared_ptr<Context> GetContext() const = 0;

virtual std::unique_ptr<Surface> AcquireSurfaceFrame(
std::shared_ptr<Context> context) = 0;

Vector2 GetContentScale() const;

protected:
PlaygroundImpl();

Expand Down
7 changes: 5 additions & 2 deletions impeller/renderer/renderer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ TEST_P(RendererTest, CanCreateBoxPrimitive) {
cmd.BindVertices(vertex_buffer);

VS::UniformBuffer uniforms;
uniforms.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize());
uniforms.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
Matrix::MakeScale(GetContentScale());
VS::BindUniformBuffer(cmd,
pass.GetTransientsBuffer().EmplaceUniform(uniforms));

Expand Down Expand Up @@ -158,6 +159,7 @@ TEST_P(RendererTest, CanRenderMultiplePrimitives) {
for (size_t j = 0; j < 1; j++) {
VS::UniformBuffer uniforms;
uniforms.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
Matrix::MakeScale(GetContentScale()) *
Matrix::MakeTranslation({i * 50.0f, j * 50.0f, 0.0f});
VS::BindUniformBuffer(
cmd, pass.GetTransientsBuffer().EmplaceUniform(uniforms));
Expand Down Expand Up @@ -324,7 +326,8 @@ TEST_P(RendererTest, CanRenderInstanced) {

ASSERT_TRUE(OpenPlaygroundHere([&](RenderPass& pass) -> bool {
VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize());
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
Matrix::MakeScale(GetContentScale());
VS::BindFrameInfo(cmd,
pass.GetTransientsBuffer().EmplaceUniform(frame_info));
VS::BindInstanceInfo(
Expand Down