Implement impeller::DisplayListDispatcher::drawTextBlob. (flutter/engine#94)

This commit is contained in:
Chinmay Garde
2022-03-22 09:39:28 -07:00
committed by Dan Field
parent f8ac111920
commit a6e0e51d75
20 changed files with 325 additions and 65 deletions

View File

@@ -347,19 +347,10 @@ bool RenderTextInCanvas(std::shared_ptr<Context> context,
// Create the Impeller text frame and draw it at the designated baseline.
auto frame = TextFrameFromTextBlob(blob);
TextRenderContextSkia text_context(context);
if (!text_context.IsValid()) {
return false;
}
auto atlas = text_context.CreateGlyphAtlas(frame);
if (!atlas) {
return false;
}
Paint text_paint;
text_paint.color = Color::Yellow();
canvas.DrawTextFrame(std::move(frame), std::move(atlas), text_position,
text_paint);
canvas.DrawTextFrame(std::move(frame), text_position, text_paint);
return true;
}

View File

@@ -269,17 +269,14 @@ void Canvas::Save(bool create_subpass) {
xformation_stack_.emplace_back(std::move(entry));
}
void Canvas::DrawTextFrame(TextFrame text_frame,
std::shared_ptr<GlyphAtlas> atlas,
Point position,
Paint paint) {
if (!atlas || !atlas->IsValid()) {
return;
}
void Canvas::DrawTextFrame(TextFrame text_frame, Point position, Paint paint) {
auto lazy_glyph_atlas = GetCurrentPass().GetLazyGlyphAtlas();
lazy_glyph_atlas->AddTextFrame(std::move(text_frame));
auto text_contents = std::make_shared<TextContents>();
text_contents->SetTextFrame(std::move(text_frame));
text_contents->SetGlyphAtlas(std::move(atlas));
text_contents->SetGlyphAtlas(std::move(lazy_glyph_atlas));
text_contents->SetColor(paint.color.Premultiply());
Entity entity;

View File

@@ -78,10 +78,7 @@ class Canvas {
void DrawPicture(Picture picture);
void DrawTextFrame(TextFrame text_frame,
std::shared_ptr<GlyphAtlas> atlas,
Point position,
Paint paint);
void DrawTextFrame(TextFrame text_frame, Point position, Paint paint);
Picture EndRecordingAsPicture();

View File

@@ -10,7 +10,7 @@ impeller_component("display_list") {
"display_list_dispatcher.h",
]
deps = [
public_deps = [
"../aiks",
"//flutter/display_list",
"//flutter/fml",
@@ -21,7 +21,11 @@ impeller_component("display_list") {
impeller_component("display_list_unittests") {
testonly = true
sources = [ "display_list_unittests.cc" ]
sources = [
"display_list_playground.cc",
"display_list_playground.h",
"display_list_unittests.cc",
]
deps = [
":display_list",

View File

@@ -454,7 +454,8 @@ void DisplayListDispatcher::clipPath(const SkPath& path,
}
// |flutter::Dispatcher|
void DisplayListDispatcher::drawColor(SkColor color, flutter::DlBlendMode dl_mode) {
void DisplayListDispatcher::drawColor(SkColor color,
flutter::DlBlendMode dl_mode) {
Paint paint;
paint.color = ToColor(color);
if (auto mode = ToBlendMode(dl_mode); mode.has_value()) {
@@ -608,7 +609,10 @@ void DisplayListDispatcher::drawDisplayList(
void DisplayListDispatcher::drawTextBlob(const sk_sp<SkTextBlob> blob,
SkScalar x,
SkScalar y) {
UNIMPLEMENTED;
canvas_.DrawTextFrame(TextFrameFromTextBlob(blob), //
impeller::Point{x, y}, //
paint_ //
);
}
// |flutter::Dispatcher|

View File

@@ -0,0 +1,71 @@
// 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/display_list/display_list_playground.h"
#include "flutter/testing/testing.h"
#include "impeller/aiks/aiks_context.h"
#include "impeller/display_list/display_list_dispatcher.h"
#include "third_party/skia/include/core/SkData.h"
namespace impeller {
DisplayListPlayground::DisplayListPlayground() = default;
DisplayListPlayground::~DisplayListPlayground() = default;
bool DisplayListPlayground::OpenPlaygroundHere(
flutter::DisplayListBuilder& builder) {
return OpenPlaygroundHere(builder.Build());
}
bool DisplayListPlayground::OpenPlaygroundHere(
sk_sp<flutter::DisplayList> list) {
if (!Playground::is_enabled()) {
return true;
}
if (!list) {
return false;
}
DisplayListDispatcher dispatcher;
list->Dispatch(dispatcher);
auto picture = dispatcher.EndRecordingAsPicture();
AiksContext context(GetContext());
if (!context.IsValid()) {
return false;
}
return Playground::OpenPlaygroundHere(
[&picture, &context](RenderPass& pass) -> bool {
return context.Render(picture, pass);
});
}
static sk_sp<SkData> OpenFixtureAsSkData(const char* fixture_name) {
auto mapping = flutter::testing::OpenFixtureAsMapping(fixture_name);
if (!mapping) {
return nullptr;
}
return SkData::MakeWithProc(
mapping->GetMapping(), mapping->GetSize(),
[](const void* ptr, void* context) {
delete reinterpret_cast<fml::Mapping*>(context);
},
mapping.release());
}
SkFont DisplayListPlayground::CreateTestFontOfSize(SkScalar scalar) {
static constexpr const char* kTestFontFixture = "Roboto-Regular.ttf";
auto mapping = OpenFixtureAsSkData(kTestFontFixture);
FML_CHECK(mapping);
return SkFont{SkTypeface::MakeFromData(mapping), scalar};
}
SkFont DisplayListPlayground::CreateTestFont() {
return CreateTestFontOfSize(50);
}
} // namespace impeller

View File

@@ -0,0 +1,33 @@
// 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 "flutter/display_list/display_list.h"
#include "flutter/display_list/display_list_builder.h"
#include "flutter/fml/macros.h"
#include "impeller/playground/playground.h"
#include "third_party/skia/include/core/SkFont.h"
namespace impeller {
class DisplayListPlayground : public Playground {
public:
DisplayListPlayground();
~DisplayListPlayground();
bool OpenPlaygroundHere(flutter::DisplayListBuilder& builder);
bool OpenPlaygroundHere(sk_sp<flutter::DisplayList> list);
SkFont CreateTestFontOfSize(SkScalar scalar);
SkFont CreateTestFont();
private:
FML_DISALLOW_COPY_AND_ASSIGN(DisplayListPlayground);
};
} // namespace impeller

View File

@@ -2,12 +2,29 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/display_list/display_list_builder.h"
#include "flutter/testing/testing.h"
#include "impeller/display_list/display_list_playground.h"
namespace impeller {
namespace testing {
TEST(DisplayListTest, CanCreateDisatcher) {}
using DisplayListTest = DisplayListPlayground;
TEST_F(DisplayListTest, CanDrawRect) {
flutter::DisplayListBuilder builder;
builder.setColor(SK_ColorBLUE);
builder.drawRect(SkRect::MakeXYWH(10, 10, 100, 100));
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
}
TEST_F(DisplayListTest, CanDrawTextBlob) {
flutter::DisplayListBuilder builder;
builder.setColor(SK_ColorBLUE);
builder.drawTextBlob(SkTextBlob::MakeFromString("Hello", CreateTestFont()),
100, 100);
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
}
} // namespace testing
} // namespace impeller

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "text_contents.h"
#include "impeller/entity/contents/text_contents.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/entity.h"
@@ -11,8 +11,10 @@
#include "impeller/renderer/sampler_library.h"
#include "impeller/tessellator/tessellator.h"
#include "impeller/typographer/glyph_atlas.h"
#include "impeller/typographer/lazy_glyph_atlas.h"
namespace impeller {
TextContents::TextContents() = default;
TextContents::~TextContents() = default;
@@ -25,6 +27,23 @@ void TextContents::SetGlyphAtlas(std::shared_ptr<GlyphAtlas> atlas) {
atlas_ = std::move(atlas);
}
void TextContents::SetGlyphAtlas(std::shared_ptr<LazyGlyphAtlas> atlas) {
atlas_ = std::move(atlas);
}
std::shared_ptr<GlyphAtlas> TextContents::ResolveAtlas(
std::shared_ptr<Context> context) const {
if (auto lazy_atlas = std::get_if<std::shared_ptr<LazyGlyphAtlas>>(&atlas_)) {
return lazy_atlas->get()->CreateOrGetGlyphAtlas(context);
}
if (auto atlas = std::get_if<std::shared_ptr<GlyphAtlas>>(&atlas_)) {
return *atlas;
}
return nullptr;
}
void TextContents::SetColor(Color color) {
color_ = color;
}
@@ -36,7 +55,9 @@ bool TextContents::Render(const ContentContext& renderer,
return true;
}
if (!atlas_ || !atlas_->IsValid()) {
auto atlas = ResolveAtlas(renderer.GetContext());
if (!atlas || !atlas->IsValid()) {
VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";
return false;
}
@@ -57,15 +78,15 @@ bool TextContents::Render(const ContentContext& renderer,
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
entity.GetTransformation();
frame_info.atlas_size =
Point{static_cast<Scalar>(atlas_->GetTexture()->GetSize().width),
static_cast<Scalar>(atlas_->GetTexture()->GetSize().height)};
Point{static_cast<Scalar>(atlas->GetTexture()->GetSize().width),
static_cast<Scalar>(atlas->GetTexture()->GetSize().height)};
frame_info.text_color = ToVector(color_);
VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
// Common fragment uniforms for all glyphs.
FS::BindGlyphAtlasSampler(
cmd, // command
atlas_->GetTexture(), // texture
atlas->GetTexture(), // texture
renderer.GetContext()->GetSamplerLibrary()->GetSampler({}) // sampler
);
@@ -107,7 +128,7 @@ bool TextContents::Render(const ContentContext& renderer,
// Draw each glyph individually. This should probably be batched.
for (const auto& glyph_position : run.GetGlyphPositions()) {
FontGlyphPair font_glyph_pair{font, glyph_position.glyph};
auto atlas_glyph_pos = atlas_->FindFontGlyphPosition(font_glyph_pair);
auto atlas_glyph_pos = atlas->FindFontGlyphPosition(font_glyph_pair);
if (!atlas_glyph_pos.has_value()) {
VALIDATION_LOG << "Could not find glyph position in the atlas.";
return false;

View File

@@ -6,6 +6,7 @@
#include <functional>
#include <memory>
#include <variant>
#include <vector>
#include "flutter/fml/macros.h"
@@ -16,6 +17,8 @@
namespace impeller {
class GlyphAtlas;
class LazyGlyphAtlas;
class Context;
class TextContents final : public Contents {
public:
@@ -27,6 +30,8 @@ class TextContents final : public Contents {
void SetGlyphAtlas(std::shared_ptr<GlyphAtlas> atlas);
void SetGlyphAtlas(std::shared_ptr<LazyGlyphAtlas> atlas);
void SetColor(Color color);
// |Contents|
@@ -37,7 +42,12 @@ class TextContents final : public Contents {
private:
TextFrame frame_;
Color color_;
std::shared_ptr<GlyphAtlas> atlas_;
mutable std::variant<std::shared_ptr<GlyphAtlas>,
std::shared_ptr<LazyGlyphAtlas>>
atlas_;
std::shared_ptr<GlyphAtlas> ResolveAtlas(
std::shared_ptr<Context> context) const;
FML_DISALLOW_COPY_AND_ASSIGN(TextContents);
};

View File

@@ -44,6 +44,10 @@ size_t EntityPass::GetSubpassesDepth() const {
return max_subpass_depth + 1u;
}
const std::shared_ptr<LazyGlyphAtlas>& EntityPass::GetLazyGlyphAtlas() const {
return lazy_glyph_atlas_;
}
std::optional<Rect> EntityPass::GetEntitiesCoverage() const {
std::optional<Rect> result;
for (const auto& entity : entities_) {

View File

@@ -13,6 +13,7 @@
#include "impeller/entity/entity.h"
#include "impeller/entity/entity_pass_delegate.h"
#include "impeller/renderer/render_target.h"
#include "impeller/typographer/lazy_glyph_atlas.h"
namespace impeller {
@@ -41,6 +42,8 @@ class EntityPass {
const Subpasses& GetSubpasses() const;
const std::shared_ptr<LazyGlyphAtlas>& GetLazyGlyphAtlas() const;
EntityPass* AddSubpass(std::unique_ptr<EntityPass> pass);
EntityPass* GetSuperpass() const;
@@ -61,6 +64,8 @@ class EntityPass {
size_t stencil_depth_ = 0u;
std::unique_ptr<EntityPassDelegate> delegate_ =
EntityPassDelegate::MakeDefault();
std::shared_ptr<LazyGlyphAtlas> lazy_glyph_atlas_ =
std::make_shared<LazyGlyphAtlas>();
std::optional<Rect> GetSubpassCoverage(const EntityPass& subpass) const;

View File

@@ -20,6 +20,8 @@ impeller_component("typographer") {
"glyph.h",
"glyph_atlas.cc",
"glyph_atlas.h",
"lazy_glyph_atlas.cc",
"lazy_glyph_atlas.h",
"text_frame.cc",
"text_frame.h",
"text_render_context.cc",

View File

@@ -25,22 +25,24 @@ TextRenderContextSkia::TextRenderContextSkia(std::shared_ptr<Context> context)
TextRenderContextSkia::~TextRenderContextSkia() = default;
static FontGlyphPair::Set CollectUniqueFontGlyphPairsSet(
const std::vector<TextRun>& runs) {
TextRenderContext::FrameIterator frame_iterator) {
FontGlyphPair::Set set;
for (const auto& run : runs) {
auto font = run.GetFont();
for (const auto& glyph_position : run.GetGlyphPositions()) {
set.insert({font, glyph_position.glyph});
while (auto frame = frame_iterator()) {
for (const auto& run : frame->GetRuns()) {
auto font = run.GetFont();
for (const auto& glyph_position : run.GetGlyphPositions()) {
set.insert({font, glyph_position.glyph});
}
}
}
return set;
}
static FontGlyphPair::Vector CollectUniqueFontGlyphPairs(
const std::vector<TextRun>& runs) {
TextRenderContext::FrameIterator frame_iterator) {
TRACE_EVENT0("impeller", __FUNCTION__);
FontGlyphPair::Vector vector;
auto set = CollectUniqueFontGlyphPairsSet(runs);
auto set = CollectUniqueFontGlyphPairsSet(frame_iterator);
vector.reserve(set.size());
for (const auto& item : set) {
vector.emplace_back(std::move(item));
@@ -202,7 +204,7 @@ static std::shared_ptr<Texture> UploadGlyphTextureAtlas(
}
std::shared_ptr<GlyphAtlas> TextRenderContextSkia::CreateGlyphAtlas(
const TextFrame& frame) const {
FrameIterator frame_iterator) const {
TRACE_EVENT0("impeller", __FUNCTION__);
if (!IsValid()) {
return nullptr;
@@ -213,7 +215,8 @@ std::shared_ptr<GlyphAtlas> TextRenderContextSkia::CreateGlyphAtlas(
// ---------------------------------------------------------------------------
// Step 1: Collect unique font-glyph pairs in the frame.
// ---------------------------------------------------------------------------
auto font_glyph_pairs = CollectUniqueFontGlyphPairs(frame.GetRuns());
auto font_glyph_pairs = CollectUniqueFontGlyphPairs(frame_iterator);
if (font_glyph_pairs.empty()) {
return glyph_atlas;
}

View File

@@ -17,7 +17,7 @@ class TextRenderContextSkia : public TextRenderContext {
// |TextRenderContext|
std::shared_ptr<GlyphAtlas> CreateGlyphAtlas(
const TextFrame& frame) const override;
FrameIterator iterator) const override;
private:
FML_DISALLOW_COPY_AND_ASSIGN(TextRenderContextSkia);

View File

@@ -0,0 +1,49 @@
// 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/typographer/lazy_glyph_atlas.h"
#include "impeller/base/validation.h"
#include "impeller/typographer/text_render_context.h"
namespace impeller {
LazyGlyphAtlas::LazyGlyphAtlas() = default;
LazyGlyphAtlas::~LazyGlyphAtlas() = default;
void LazyGlyphAtlas::AddTextFrame(TextFrame frame) {
FML_DCHECK(!atlas_);
frames_.emplace_back(std::move(frame));
}
std::shared_ptr<GlyphAtlas> LazyGlyphAtlas::CreateOrGetGlyphAtlas(
std::shared_ptr<Context> context) const {
if (atlas_) {
return atlas_;
}
auto text_context = TextRenderContext::Create(std::move(context));
if (!text_context || !text_context->IsValid()) {
return nullptr;
}
size_t i = 0;
TextRenderContext::FrameIterator iterator = [&]() -> const TextFrame* {
if (i >= frames_.size()) {
return nullptr;
}
const auto& result = frames_[i];
i++;
return &result;
};
auto atlas = text_context->CreateGlyphAtlas(iterator);
if (!atlas || !atlas->IsValid()) {
VALIDATION_LOG << "Could not create valid atlas.";
return nullptr;
}
atlas_ = std::move(atlas);
return atlas_;
}
} // namespace impeller

View File

@@ -0,0 +1,32 @@
// 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 "flutter/fml/macros.h"
#include "impeller/renderer/context.h"
#include "impeller/typographer/glyph_atlas.h"
#include "impeller/typographer/text_frame.h"
namespace impeller {
class LazyGlyphAtlas {
public:
LazyGlyphAtlas();
~LazyGlyphAtlas();
void AddTextFrame(TextFrame frame);
std::shared_ptr<GlyphAtlas> CreateOrGetGlyphAtlas(
std::shared_ptr<Context> context) const;
private:
std::vector<TextFrame> frames_;
mutable std::shared_ptr<GlyphAtlas> atlas_;
FML_DISALLOW_COPY_AND_ASSIGN(LazyGlyphAtlas);
};
} // namespace impeller

View File

@@ -4,8 +4,16 @@
#include "impeller/typographer/text_render_context.h"
#include "impeller/typographer/backends/skia/text_render_context_skia.h"
namespace impeller {
std::unique_ptr<TextRenderContext> TextRenderContext::Create(
std::shared_ptr<Context> context) {
// There is only one backend today.
return std::make_unique<TextRenderContextSkia>(std::move(context));
}
TextRenderContext::TextRenderContext(std::shared_ptr<Context> context)
: context_(std::move(context)) {
if (!context_ || !context_->IsValid()) {
@@ -24,4 +32,17 @@ const std::shared_ptr<Context>& TextRenderContext::GetContext() const {
return context_;
}
std::shared_ptr<GlyphAtlas> TextRenderContext::CreateGlyphAtlas(
const TextFrame& frame) const {
size_t count = 0;
FrameIterator iterator = [&]() -> const TextFrame* {
count++;
if (count == 1) {
return &frame;
}
return nullptr;
};
return CreateGlyphAtlas(iterator);
}
} // namespace impeller

View File

@@ -4,6 +4,7 @@
#pragma once
#include <functional>
#include <memory>
#include "flutter/fml/macros.h"
@@ -19,18 +20,11 @@ namespace impeller {
/// This is necessary to create and reference resources related to
/// rendering text on the GPU.
///
/// It is caller responsibility to create as few of these and keep
/// these around for as long possible.
///
class TextRenderContext {
public:
//----------------------------------------------------------------------------
/// @brief Create a new context to render text that talks to an
/// underlying graphics context.
///
/// @param[in] context The graphics context
///
TextRenderContext(std::shared_ptr<Context> context);
static std::unique_ptr<TextRenderContext> Create(
std::shared_ptr<Context> context);
virtual ~TextRenderContext();
@@ -43,15 +37,20 @@ class TextRenderContext {
///
const std::shared_ptr<Context>& GetContext() const;
//----------------------------------------------------------------------------
/// @brief Create a new glyph atlas for the specified text frame.
///
/// @param[in] frame The text frame
///
/// @return A valid glyph atlas or null.
///
using FrameIterator = std::function<const TextFrame*(void)>;
virtual std::shared_ptr<GlyphAtlas> CreateGlyphAtlas(
const TextFrame& frame) const = 0;
FrameIterator iterator) const = 0;
std::shared_ptr<GlyphAtlas> CreateGlyphAtlas(const TextFrame& frame) const;
protected:
//----------------------------------------------------------------------------
/// @brief Create a new context to render text that talks to an
/// underlying graphics context.
///
/// @param[in] context The graphics context
///
TextRenderContext(std::shared_ptr<Context> context);
private:
std::shared_ptr<Context> context_;

View File

@@ -27,13 +27,13 @@ TEST_F(TypographerTest, CanConvertTextBlob) {
}
TEST_F(TypographerTest, CanCreateRenderContext) {
auto context = std::make_shared<TextRenderContextSkia>(GetContext());
ASSERT_TRUE(context->IsValid());
auto context = TextRenderContext::Create(GetContext());
ASSERT_TRUE(context && context->IsValid());
}
TEST_F(TypographerTest, CanCreateGlyphAtlas) {
auto context = std::make_shared<TextRenderContextSkia>(GetContext());
ASSERT_TRUE(context->IsValid());
auto context = TextRenderContext::Create(GetContext());
ASSERT_TRUE(context && context->IsValid());
SkFont sk_font;
auto blob = SkTextBlob::MakeFromString("hello", sk_font);
ASSERT_TRUE(blob);