diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index 5531beee47..e8ab4e8b8d 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -299,9 +299,11 @@ static sk_sp OpenFixtureAsSkData(const char* fixture_name) { mapping.release()); } -TEST_F(AiksTest, CanRenderTextFrame) { - Canvas canvas; - +bool RenderTextInCanvas(std::shared_ptr context, + Canvas& canvas, + const std::string& text, + const std::string& font_fixture, + Scalar font_size = 50.0) { Scalar baseline = 200.0; Point text_position = {100, baseline}; @@ -314,84 +316,56 @@ TEST_F(AiksTest, CanRenderTextFrame) { Paint{.color = Color::Red().WithAlpha(0.25)}); // Construct the text blob. - auto mapping = OpenFixtureAsSkData("Roboto-Regular.ttf"); - ASSERT_TRUE(mapping); + auto mapping = OpenFixtureAsSkData(font_fixture.c_str()); + if (!mapping) { + return false; + } SkFont sk_font(SkTypeface::MakeFromData(mapping), 50.0); - auto blob = SkTextBlob::MakeFromString( - "the quick brown fox jumped over the lazy dog!.?", sk_font); - ASSERT_TRUE(blob); + auto blob = SkTextBlob::MakeFromString(text.c_str(), sk_font); + if (!blob) { + return false; + } // Create the Impeller text frame and draw it at the designated baseline. auto frame = TextFrameFromTextBlob(blob); - TextRenderContextSkia text_context(GetContext()); - ASSERT_TRUE(text_context.IsValid()); + TextRenderContextSkia text_context(context); + if (!text_context.IsValid()) { + return false; + } auto atlas = text_context.CreateGlyphAtlas(frame); - ASSERT_NE(atlas, nullptr); - canvas.DrawTextFrame(std::move(frame), std::move(atlas), text_position); + if (!atlas) { + return false; + } + + Paint text_paint; + text_paint.color = Color::Yellow(); + canvas.DrawTextFrame(std::move(frame), std::move(atlas), text_position, + text_paint); + return true; +} + +TEST_F(AiksTest, CanRenderTextFrame) { + Canvas canvas; + ASSERT_TRUE(RenderTextInCanvas( + GetContext(), canvas, "the quick brown fox jumped over the lazy dog!.?", + "Roboto-Regular.ttf")); ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } TEST_F(AiksTest, CanRenderItalicizedText) { Canvas canvas; - - Scalar baseline = 200.0; - Point text_position = {100, baseline}; - - // Draw the baseline. - canvas.DrawRect({50, baseline, 900, 10}, - Paint{.color = Color::Aqua().WithAlpha(0.25)}); - - // Mark the point at which the text is drawn. - canvas.DrawCircle(text_position, 5.0, - Paint{.color = Color::Red().WithAlpha(0.25)}); - - // Construct the text blob. - auto mapping = OpenFixtureAsSkData("HomemadeApple.ttf"); - ASSERT_TRUE(mapping); - SkFont sk_font(SkTypeface::MakeFromData(mapping), 50.0); - auto blob = SkTextBlob::MakeFromString( - "the quick brown fox jumped over the lazy dog!.?", sk_font); - ASSERT_TRUE(blob); - - // Create the Impeller text frame and draw it at the designated baseline. - auto frame = TextFrameFromTextBlob(blob); - TextRenderContextSkia text_context(GetContext()); - ASSERT_TRUE(text_context.IsValid()); - auto atlas = text_context.CreateGlyphAtlas(frame); - ASSERT_NE(atlas, nullptr); - canvas.DrawTextFrame(std::move(frame), std::move(atlas), text_position); + ASSERT_TRUE(RenderTextInCanvas( + GetContext(), canvas, "the quick brown fox jumped over the lazy dog!.?", + "HomemadeApple.ttf")); ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } TEST_F(AiksTest, CanRenderEmojiTextFrame) { Canvas canvas; - - Scalar baseline = 200.0; - Point text_position = {100, baseline}; - - // Draw the baseline. - canvas.DrawRect({50, baseline, 900, 10}, - Paint{.color = Color::Aqua().WithAlpha(0.25)}); - - // Mark the point at which the text is drawn. - canvas.DrawCircle(text_position, 5.0, - Paint{.color = Color::Red().WithAlpha(0.25)}); - - // Construct the text blob. - auto mapping = OpenFixtureAsSkData("NotoColorEmoji.ttf"); - ASSERT_TRUE(mapping); - SkFont sk_font(SkTypeface::MakeFromData(mapping), 50.0); - auto blob = SkTextBlob::MakeFromString( - "πŸ˜€ πŸ˜ƒ πŸ˜„ 😁 πŸ˜† πŸ˜… πŸ˜‚ 🀣 πŸ₯² ☺️ 😊", sk_font); - ASSERT_TRUE(blob); - - // Create the Impeller text frame and draw it at the designated baseline. - auto frame = TextFrameFromTextBlob(blob); - TextRenderContextSkia text_context(GetContext()); - ASSERT_TRUE(text_context.IsValid()); - auto atlas = text_context.CreateGlyphAtlas(frame); - ASSERT_NE(atlas, nullptr); - canvas.DrawTextFrame(std::move(frame), std::move(atlas), text_position); + ASSERT_TRUE(RenderTextInCanvas( + GetContext(), canvas, + "πŸ˜€ πŸ˜ƒ πŸ˜„ 😁 πŸ˜† πŸ˜… πŸ˜‚ 🀣 πŸ₯² ☺️ 😊", + "NotoColorEmoji.ttf")); ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } diff --git a/engine/src/flutter/impeller/aiks/canvas.cc b/engine/src/flutter/impeller/aiks/canvas.cc index 0a6e214de9..f9c19fe3cc 100644 --- a/engine/src/flutter/impeller/aiks/canvas.cc +++ b/engine/src/flutter/impeller/aiks/canvas.cc @@ -254,7 +254,8 @@ void Canvas::Save(bool create_subpass) { void Canvas::DrawTextFrame(TextFrame text_frame, std::shared_ptr atlas, - Point position) { + Point position, + Paint paint) { if (!atlas || !atlas->IsValid()) { return; } @@ -262,6 +263,7 @@ void Canvas::DrawTextFrame(TextFrame text_frame, auto text_contents = std::make_shared(); text_contents->SetTextFrame(std::move(text_frame)); text_contents->SetGlyphAtlas(std::move(atlas)); + text_contents->SetColor(paint.color); Entity entity; entity.SetTransformation(GetCurrentTransformation() * diff --git a/engine/src/flutter/impeller/aiks/canvas.h b/engine/src/flutter/impeller/aiks/canvas.h index 88db60911e..56a23aaa94 100644 --- a/engine/src/flutter/impeller/aiks/canvas.h +++ b/engine/src/flutter/impeller/aiks/canvas.h @@ -77,7 +77,8 @@ class Canvas { void DrawTextFrame(TextFrame text_frame, std::shared_ptr atlas, - Point position); + Point position, + Paint paint); Picture EndRecordingAsPicture(); diff --git a/engine/src/flutter/impeller/entity/contents.cc b/engine/src/flutter/impeller/entity/contents.cc index 07ec6db4eb..0cb59466a7 100644 --- a/engine/src/flutter/impeller/entity/contents.cc +++ b/engine/src/flutter/impeller/entity/contents.cc @@ -568,9 +568,17 @@ void TextContents::SetGlyphAtlas(std::shared_ptr atlas) { atlas_ = std::move(atlas); } +void TextContents::SetColor(Color color) { + color_ = color; +} + bool TextContents::Render(const ContentContext& renderer, const Entity& entity, RenderPass& pass) const { + if (color_.IsTransparent()) { + return true; + } + if (!atlas_ || !atlas_->IsValid()) { VALIDATION_LOG << "Cannot render glyphs without prepared atlas."; return false; @@ -593,6 +601,7 @@ bool TextContents::Render(const ContentContext& renderer, frame_info.atlas_size = Point{static_cast(atlas_->GetTexture()->GetSize().width), static_cast(atlas_->GetTexture()->GetSize().height)}; + frame_info.text_color = ToVector(color_); VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info)); // Common fragment uniforms for all glyphs. diff --git a/engine/src/flutter/impeller/entity/contents.h b/engine/src/flutter/impeller/entity/contents.h index 5488f6d3a8..3b1bec7509 100644 --- a/engine/src/flutter/impeller/entity/contents.h +++ b/engine/src/flutter/impeller/entity/contents.h @@ -210,6 +210,8 @@ class TextContents final : public Contents { void SetGlyphAtlas(std::shared_ptr atlas); + void SetColor(Color color); + // |Contents| bool Render(const ContentContext& renderer, const Entity& entity, @@ -217,6 +219,7 @@ class TextContents final : public Contents { private: TextFrame frame_; + Color color_; std::shared_ptr atlas_; FML_DISALLOW_COPY_AND_ASSIGN(TextContents); diff --git a/engine/src/flutter/impeller/entity/shaders/glyph_atlas.frag b/engine/src/flutter/impeller/entity/shaders/glyph_atlas.frag index 0fedba1c14..f07e591f19 100644 --- a/engine/src/flutter/impeller/entity/shaders/glyph_atlas.frag +++ b/engine/src/flutter/impeller/entity/shaders/glyph_atlas.frag @@ -8,6 +8,7 @@ in vec2 v_unit_vertex; in vec2 v_atlas_position; in vec2 v_atlas_glyph_size; in vec2 v_atlas_size; +in vec4 v_text_color; out vec4 frag_color; @@ -18,5 +19,5 @@ void main() { frag_color = texture( glyph_atlas_sampler, v_unit_vertex * scale_perspective + offset - ); + ) * v_text_color; } diff --git a/engine/src/flutter/impeller/entity/shaders/glyph_atlas.vert b/engine/src/flutter/impeller/entity/shaders/glyph_atlas.vert index 7cff4886b7..aff822fdd4 100644 --- a/engine/src/flutter/impeller/entity/shaders/glyph_atlas.vert +++ b/engine/src/flutter/impeller/entity/shaders/glyph_atlas.vert @@ -5,6 +5,7 @@ uniform FrameInfo { mat4 mvp; vec2 atlas_size; + vec4 text_color; } frame_info; uniform GlyphInfo { @@ -20,6 +21,7 @@ out vec2 v_unit_vertex; out vec2 v_atlas_position; out vec2 v_atlas_glyph_size; out vec2 v_atlas_size; +out vec4 v_text_color; void main() { mat4 scale = mat4( @@ -37,4 +39,5 @@ void main() { v_atlas_position = glyph_info.atlas_position; v_atlas_glyph_size = glyph_info.atlas_glyph_size; v_atlas_size = frame_info.atlas_size; + v_text_color = frame_info.text_color; } diff --git a/engine/src/flutter/impeller/renderer/shader_types.h b/engine/src/flutter/impeller/renderer/shader_types.h index fb6e51b65e..0f0f9919f0 100644 --- a/engine/src/flutter/impeller/renderer/shader_types.h +++ b/engine/src/flutter/impeller/renderer/shader_types.h @@ -94,4 +94,8 @@ struct Padding { uint8_t pad_[Size]; }; +inline constexpr Vector4 ToVector(Color color) { + return {color.red, color.green, color.blue, color.alpha}; +} + } // namespace impeller