Skip to content

Commit 4e323c6

Browse files
chinmaygardednfield
authored andcommitted
Relay text color from Paint. (#43)
1 parent 9f9c557 commit 4e323c6

File tree

8 files changed

+66
-69
lines changed

8 files changed

+66
-69
lines changed

impeller/aiks/aiks_unittests.cc

Lines changed: 40 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,11 @@ static sk_sp<SkData> OpenFixtureAsSkData(const char* fixture_name) {
299299
mapping.release());
300300
}
301301

302-
TEST_F(AiksTest, CanRenderTextFrame) {
303-
Canvas canvas;
304-
302+
bool RenderTextInCanvas(std::shared_ptr<Context> context,
303+
Canvas& canvas,
304+
const std::string& text,
305+
const std::string& font_fixture,
306+
Scalar font_size = 50.0) {
305307
Scalar baseline = 200.0;
306308
Point text_position = {100, baseline};
307309

@@ -314,84 +316,56 @@ TEST_F(AiksTest, CanRenderTextFrame) {
314316
Paint{.color = Color::Red().WithAlpha(0.25)});
315317

316318
// Construct the text blob.
317-
auto mapping = OpenFixtureAsSkData("Roboto-Regular.ttf");
318-
ASSERT_TRUE(mapping);
319+
auto mapping = OpenFixtureAsSkData(font_fixture.c_str());
320+
if (!mapping) {
321+
return false;
322+
}
319323
SkFont sk_font(SkTypeface::MakeFromData(mapping), 50.0);
320-
auto blob = SkTextBlob::MakeFromString(
321-
"the quick brown fox jumped over the lazy dog!.?", sk_font);
322-
ASSERT_TRUE(blob);
324+
auto blob = SkTextBlob::MakeFromString(text.c_str(), sk_font);
325+
if (!blob) {
326+
return false;
327+
}
323328

324329
// Create the Impeller text frame and draw it at the designated baseline.
325330
auto frame = TextFrameFromTextBlob(blob);
326-
TextRenderContextSkia text_context(GetContext());
327-
ASSERT_TRUE(text_context.IsValid());
331+
TextRenderContextSkia text_context(context);
332+
if (!text_context.IsValid()) {
333+
return false;
334+
}
328335
auto atlas = text_context.CreateGlyphAtlas(frame);
329-
ASSERT_NE(atlas, nullptr);
330-
canvas.DrawTextFrame(std::move(frame), std::move(atlas), text_position);
336+
if (!atlas) {
337+
return false;
338+
}
339+
340+
Paint text_paint;
341+
text_paint.color = Color::Yellow();
342+
canvas.DrawTextFrame(std::move(frame), std::move(atlas), text_position,
343+
text_paint);
344+
return true;
345+
}
346+
347+
TEST_F(AiksTest, CanRenderTextFrame) {
348+
Canvas canvas;
349+
ASSERT_TRUE(RenderTextInCanvas(
350+
GetContext(), canvas, "the quick brown fox jumped over the lazy dog!.?",
351+
"Roboto-Regular.ttf"));
331352
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
332353
}
333354

334355
TEST_F(AiksTest, CanRenderItalicizedText) {
335356
Canvas canvas;
336-
337-
Scalar baseline = 200.0;
338-
Point text_position = {100, baseline};
339-
340-
// Draw the baseline.
341-
canvas.DrawRect({50, baseline, 900, 10},
342-
Paint{.color = Color::Aqua().WithAlpha(0.25)});
343-
344-
// Mark the point at which the text is drawn.
345-
canvas.DrawCircle(text_position, 5.0,
346-
Paint{.color = Color::Red().WithAlpha(0.25)});
347-
348-
// Construct the text blob.
349-
auto mapping = OpenFixtureAsSkData("HomemadeApple.ttf");
350-
ASSERT_TRUE(mapping);
351-
SkFont sk_font(SkTypeface::MakeFromData(mapping), 50.0);
352-
auto blob = SkTextBlob::MakeFromString(
353-
"the quick brown fox jumped over the lazy dog!.?", sk_font);
354-
ASSERT_TRUE(blob);
355-
356-
// Create the Impeller text frame and draw it at the designated baseline.
357-
auto frame = TextFrameFromTextBlob(blob);
358-
TextRenderContextSkia text_context(GetContext());
359-
ASSERT_TRUE(text_context.IsValid());
360-
auto atlas = text_context.CreateGlyphAtlas(frame);
361-
ASSERT_NE(atlas, nullptr);
362-
canvas.DrawTextFrame(std::move(frame), std::move(atlas), text_position);
357+
ASSERT_TRUE(RenderTextInCanvas(
358+
GetContext(), canvas, "the quick brown fox jumped over the lazy dog!.?",
359+
"HomemadeApple.ttf"));
363360
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
364361
}
365362

366363
TEST_F(AiksTest, CanRenderEmojiTextFrame) {
367364
Canvas canvas;
368-
369-
Scalar baseline = 200.0;
370-
Point text_position = {100, baseline};
371-
372-
// Draw the baseline.
373-
canvas.DrawRect({50, baseline, 900, 10},
374-
Paint{.color = Color::Aqua().WithAlpha(0.25)});
375-
376-
// Mark the point at which the text is drawn.
377-
canvas.DrawCircle(text_position, 5.0,
378-
Paint{.color = Color::Red().WithAlpha(0.25)});
379-
380-
// Construct the text blob.
381-
auto mapping = OpenFixtureAsSkData("NotoColorEmoji.ttf");
382-
ASSERT_TRUE(mapping);
383-
SkFont sk_font(SkTypeface::MakeFromData(mapping), 50.0);
384-
auto blob = SkTextBlob::MakeFromString(
385-
"😀 😃 😄 😁 😆 😅 😂 🤣 🥲 ☺️ 😊", sk_font);
386-
ASSERT_TRUE(blob);
387-
388-
// Create the Impeller text frame and draw it at the designated baseline.
389-
auto frame = TextFrameFromTextBlob(blob);
390-
TextRenderContextSkia text_context(GetContext());
391-
ASSERT_TRUE(text_context.IsValid());
392-
auto atlas = text_context.CreateGlyphAtlas(frame);
393-
ASSERT_NE(atlas, nullptr);
394-
canvas.DrawTextFrame(std::move(frame), std::move(atlas), text_position);
365+
ASSERT_TRUE(RenderTextInCanvas(
366+
GetContext(), canvas,
367+
"😀 😃 😄 😁 😆 😅 😂 🤣 🥲 ☺️ 😊",
368+
"NotoColorEmoji.ttf"));
395369
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
396370
}
397371

impeller/aiks/canvas.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,16 @@ void Canvas::Save(bool create_subpass) {
254254

255255
void Canvas::DrawTextFrame(TextFrame text_frame,
256256
std::shared_ptr<GlyphAtlas> atlas,
257-
Point position) {
257+
Point position,
258+
Paint paint) {
258259
if (!atlas || !atlas->IsValid()) {
259260
return;
260261
}
261262

262263
auto text_contents = std::make_shared<TextContents>();
263264
text_contents->SetTextFrame(std::move(text_frame));
264265
text_contents->SetGlyphAtlas(std::move(atlas));
266+
text_contents->SetColor(paint.color);
265267

266268
Entity entity;
267269
entity.SetTransformation(GetCurrentTransformation() *

impeller/aiks/canvas.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ class Canvas {
7777

7878
void DrawTextFrame(TextFrame text_frame,
7979
std::shared_ptr<GlyphAtlas> atlas,
80-
Point position);
80+
Point position,
81+
Paint paint);
8182

8283
Picture EndRecordingAsPicture();
8384

impeller/entity/contents.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,17 @@ void TextContents::SetGlyphAtlas(std::shared_ptr<GlyphAtlas> atlas) {
568568
atlas_ = std::move(atlas);
569569
}
570570

571+
void TextContents::SetColor(Color color) {
572+
color_ = color;
573+
}
574+
571575
bool TextContents::Render(const ContentContext& renderer,
572576
const Entity& entity,
573577
RenderPass& pass) const {
578+
if (color_.IsTransparent()) {
579+
return true;
580+
}
581+
574582
if (!atlas_ || !atlas_->IsValid()) {
575583
VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";
576584
return false;
@@ -593,6 +601,7 @@ bool TextContents::Render(const ContentContext& renderer,
593601
frame_info.atlas_size =
594602
Point{static_cast<Scalar>(atlas_->GetTexture()->GetSize().width),
595603
static_cast<Scalar>(atlas_->GetTexture()->GetSize().height)};
604+
frame_info.text_color = ToVector(color_);
596605
VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
597606

598607
// Common fragment uniforms for all glyphs.

impeller/entity/contents.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,16 @@ class TextContents final : public Contents {
210210

211211
void SetGlyphAtlas(std::shared_ptr<GlyphAtlas> atlas);
212212

213+
void SetColor(Color color);
214+
213215
// |Contents|
214216
bool Render(const ContentContext& renderer,
215217
const Entity& entity,
216218
RenderPass& pass) const override;
217219

218220
private:
219221
TextFrame frame_;
222+
Color color_;
220223
std::shared_ptr<GlyphAtlas> atlas_;
221224

222225
FML_DISALLOW_COPY_AND_ASSIGN(TextContents);

impeller/entity/shaders/glyph_atlas.frag

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ in vec2 v_unit_vertex;
88
in vec2 v_atlas_position;
99
in vec2 v_atlas_glyph_size;
1010
in vec2 v_atlas_size;
11+
in vec4 v_text_color;
1112

1213
out vec4 frag_color;
1314

@@ -18,5 +19,5 @@ void main() {
1819
frag_color = texture(
1920
glyph_atlas_sampler,
2021
v_unit_vertex * scale_perspective + offset
21-
);
22+
) * v_text_color;
2223
}

impeller/entity/shaders/glyph_atlas.vert

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
uniform FrameInfo {
66
mat4 mvp;
77
vec2 atlas_size;
8+
vec4 text_color;
89
} frame_info;
910

1011
uniform GlyphInfo {
@@ -20,6 +21,7 @@ out vec2 v_unit_vertex;
2021
out vec2 v_atlas_position;
2122
out vec2 v_atlas_glyph_size;
2223
out vec2 v_atlas_size;
24+
out vec4 v_text_color;
2325

2426
void main() {
2527
mat4 scale = mat4(
@@ -37,4 +39,5 @@ void main() {
3739
v_atlas_position = glyph_info.atlas_position;
3840
v_atlas_glyph_size = glyph_info.atlas_glyph_size;
3941
v_atlas_size = frame_info.atlas_size;
42+
v_text_color = frame_info.text_color;
4043
}

impeller/renderer/shader_types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,8 @@ struct Padding {
9494
uint8_t pad_[Size];
9595
};
9696

97+
inline constexpr Vector4 ToVector(Color color) {
98+
return {color.red, color.green, color.blue, color.alpha};
99+
}
100+
97101
} // namespace impeller

0 commit comments

Comments
 (0)