6 #include "flutter/impeller/renderer/testing/mocks.h"
7 #include "flutter/testing/testing.h"
12 #include "third_party/googletest/googletest/include/gtest/gtest.h"
13 #include "txt/platform.h"
15 #pragma GCC diagnostic ignored "-Wunreachable-code"
23 using ::testing::Return;
31 std::shared_ptr<TextFrame> MakeTextFrame(
const std::string& text,
32 const std::string_view& font_fixture,
33 const TextOptions& options) {
34 auto c_font_fixture = std::string(font_fixture);
35 auto mapping = flutter::testing::OpenFixtureAsSkData(c_font_fixture.c_str());
39 sk_sp<SkFontMgr> font_mgr = txt::GetDefaultFontManager();
40 SkFont sk_font(font_mgr->makeFromData(mapping), options.font_size);
41 if (options.is_subpixel) {
42 sk_font.setSubpixel(
true);
44 auto blob = SkTextBlob::MakeFromString(text.c_str(), sk_font);
54 const TypographerContext* typographer_context,
55 HostBuffer& data_host_buffer,
58 const std::shared_ptr<GlyphAtlasContext>& atlas_context,
59 const std::shared_ptr<TextFrame>& frame,
61 RenderableText render_frame{
65 return typographer_context->CreateGlyphAtlas(context, type, data_host_buffer,
66 atlas_context, {render_frame});
69 Rect PerVertexDataPositionToRect(
70 std::vector<GlyphAtlasPipeline::VertexShader::PerVertexData>::iterator
76 for (
int i = 0; i < 4; ++i) {
77 right = std::max(right, data[i].position.x);
78 left = std::min(left, data[i].position.x);
79 top = std::min(top, data[i].position.y);
80 bottom = std::max(bottom, data[i].position.y);
86 Rect PerVertexDataUVToRect(
87 std::vector<GlyphAtlasPipeline::VertexShader::PerVertexData>::iterator data,
93 for (
int i = 0; i < 4; ++i) {
94 right = std::max(right, data[i].uv.x * texture_size.width);
95 left = std::min(left, data[i].uv.x * texture_size.width);
96 top = std::min(top, data[i].uv.y * texture_size.height);
97 bottom = std::max(bottom, data[i].uv.y * texture_size.height);
103 double GetAspectRatio(
Rect rect) {
104 return static_cast<double>(rect.GetWidth()) / rect.GetHeight();
109 #ifndef FML_OS_MACOSX
110 GTEST_SKIP() <<
"Results aren't stable across linux and macos.";
113 std::vector<GlyphAtlasPipeline::VertexShader::PerVertexData> data(4);
115 std::shared_ptr<TextFrame> text_frame =
116 MakeTextFrame(
"1",
"ahem.ttf", TextOptions{.font_size = 50});
119 std::shared_ptr<GlyphAtlasContext> atlas_context =
122 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter(),
123 GetContext()->GetCapabilities()->GetMinimumUniformAlignment());
124 ASSERT_TRUE(context && context->IsValid());
125 std::shared_ptr<GlyphAtlas> atlas =
130 ISize texture_size = atlas->GetTexture()->GetSize();
139 Rect position_rect = PerVertexDataPositionToRect(data.begin());
140 Rect uv_rect = PerVertexDataUVToRect(data.begin(), texture_size);
149 #ifndef FML_OS_MACOSX
150 GTEST_SKIP() <<
"Results aren't stable across linux and macos.";
153 std::vector<GlyphAtlasPipeline::VertexShader::PerVertexData> data(4);
154 std::shared_ptr<TextFrame> text_frame =
155 MakeTextFrame(
"1",
"ahem.ttf", TextOptions{.font_size = 50});
158 std::shared_ptr<GlyphAtlasContext> atlas_context =
161 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter(),
162 GetContext()->GetCapabilities()->GetMinimumUniformAlignment());
163 ASSERT_TRUE(context && context->IsValid());
165 std::shared_ptr<GlyphAtlas> atlas =
168 atlas_context, text_frame, {0, 0});
170 ISize texture_size = atlas->GetTexture()->GetSize();
180 Rect position_rect = PerVertexDataPositionToRect(data.begin());
181 Rect uv_rect = PerVertexDataUVToRect(data.begin(), texture_size);
187 std::shared_ptr<TextFrame> text_frame =
188 MakeTextFrame(
"th",
"ahem.ttf", TextOptions{.font_size = 50});
191 std::shared_ptr<GlyphAtlasContext> atlas_context =
194 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter(),
195 GetContext()->GetCapabilities()->GetMinimumUniformAlignment());
196 ASSERT_TRUE(context && context->IsValid());
198 for (
int i = 0; i <= 1000; ++i) {
199 Scalar scale = (440.0f + i) / 1000.0f;
201 Rect position_rect[2];
205 std::vector<GlyphAtlasPipeline::VertexShader::PerVertexData> data(12);
207 std::shared_ptr<GlyphAtlas> atlas =
210 atlas_context, text_frame, {0, 0});
211 ISize texture_size = atlas->GetTexture()->GetSize();
221 position_rect[0] = PerVertexDataPositionToRect(data.begin());
222 uv_rect[0] = PerVertexDataUVToRect(data.begin(), texture_size);
223 position_rect[1] = PerVertexDataPositionToRect(data.begin() + 4);
224 uv_rect[1] = PerVertexDataUVToRect(data.begin() + 4, texture_size);
226 EXPECT_NEAR(GetAspectRatio(position_rect[1]), GetAspectRatio(uv_rect[1]),
233 #ifndef FML_OS_MACOSX
234 GTEST_SKIP() <<
"Results aren't stable across linux and macos.";
237 std::vector<GlyphAtlasPipeline::VertexShader::PerVertexData> data(4);
239 std::shared_ptr<TextFrame> text_frame = MakeTextFrame(
240 "1",
"ahem.ttf", TextOptions{.font_size = 50, .is_subpixel =
true});
243 std::shared_ptr<GlyphAtlasContext> atlas_context =
246 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter(),
247 GetContext()->GetCapabilities()->GetMinimumUniformAlignment());
248 ASSERT_TRUE(context && context->IsValid());
250 std::shared_ptr<GlyphAtlas> atlas =
255 ISize texture_size = atlas->GetTexture()->GetSize();
265 Rect position_rect = PerVertexDataPositionToRect(data.begin());
266 Rect uv_rect = PerVertexDataUVToRect(data.begin(), texture_size);
275 #ifndef FML_OS_MACOSX
276 GTEST_SKIP() <<
"Results aren't stable across linux and macos.";
279 std::vector<GlyphAtlasPipeline::VertexShader::PerVertexData> data(4);
281 std::shared_ptr<TextFrame> text_frame = MakeTextFrame(
282 "1",
"ahem.ttf", TextOptions{.font_size = 50, .is_subpixel =
true});
285 std::shared_ptr<GlyphAtlasContext> atlas_context =
288 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter(),
289 GetContext()->GetCapabilities()->GetMinimumUniformAlignment());
290 ASSERT_TRUE(context && context->IsValid());
292 Point offset = {0.16667, 0};
293 std::shared_ptr<GlyphAtlas> atlas =
298 ISize texture_size = atlas->GetTexture()->GetSize();
308 Rect position_rect = PerVertexDataPositionToRect(data.begin());
309 Rect uv_rect = PerVertexDataUVToRect(data.begin(), texture_size);
314 <<
"position size:" << position_rect.
GetSize();
316 <<
"position size:" << position_rect.
GetSize();
320 #ifndef FML_OS_MACOSX
321 GTEST_SKIP() <<
"Results aren't stable across linux and macos.";
324 std::vector<GlyphAtlasPipeline::VertexShader::PerVertexData> data(4);
326 std::shared_ptr<TextFrame> text_frame = MakeTextFrame(
327 "1",
"ahem.ttf", TextOptions{.font_size = 50, .is_subpixel =
true});
330 std::shared_ptr<GlyphAtlasContext> atlas_context =
333 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter(),
334 GetContext()->GetCapabilities()->GetMinimumUniformAlignment());
335 ASSERT_TRUE(context && context->IsValid());
337 std::shared_ptr<GlyphAtlas> atlas =
342 ISize texture_size = atlas->GetTexture()->GetSize();
352 Rect position_rect = PerVertexDataPositionToRect(data.begin());
353 Rect uv_rect = PerVertexDataUVToRect(data.begin(), texture_size);
362 #ifndef FML_OS_MACOSX
363 GTEST_SKIP() <<
"Results aren't stable across linux and macos.";
366 std::vector<GlyphAtlasPipeline::VertexShader::PerVertexData> data(4);
368 std::shared_ptr<TextFrame> text_frame = MakeTextFrame(
369 "1",
"ahem.ttf", TextOptions{.font_size = 50, .is_subpixel =
true});
372 std::shared_ptr<GlyphAtlasContext> atlas_context =
375 GetContext()->GetResourceAllocator(), GetContext()->GetIdleWaiter(),
376 GetContext()->GetCapabilities()->GetMinimumUniformAlignment());
377 ASSERT_TRUE(context && context->IsValid());
379 std::shared_ptr<GlyphAtlas> atlas =
384 ISize texture_size = atlas->GetTexture()->GetSize();
394 Rect position_rect = PerVertexDataPositionToRect(data.begin());
395 Rect uv_rect = PerVertexDataUVToRect(data.begin(), texture_size);
Type
Describes how the glyphs are represented in the texture.
static std::shared_ptr< HostBuffer > Create(const std::shared_ptr< Allocator > &allocator, const std::shared_ptr< const IdleWaiter > &idle_waiter, size_t minimum_uniform_alignment)
static void ComputeVertexData(GlyphAtlasPipeline::VertexShader::PerVertexData *vtx_contents, const Matrix &entity_transform, const std::shared_ptr< TextFrame > &frame, Point position, const Matrix &screen_transform, std::optional< GlyphProperties > glyph_properties, const std::shared_ptr< GlyphAtlas > &atlas)
Computes the vertex data for the render operation from a collection of data drawn from the DrawTextFr...
static std::shared_ptr< TypographerContext > Make()
#define EXPECT_RECT_NEAR(a, b)
TEST_P(AiksTest, DrawAtlasNoColor)
static std::shared_ptr< GlyphAtlas > CreateGlyphAtlas(Context &context, const TypographerContext *typographer_context, HostBuffer &data_host_buffer, GlyphAtlas::Type type, const Matrix &transform, const std::shared_ptr< GlyphAtlasContext > &atlas_context, const std::shared_ptr< TextFrame > &frame)
INSTANTIATE_PLAYGROUND_SUITE(AiksTest)
std::shared_ptr< TextFrame > MakeTextFrameFromTextBlobSkia(const sk_sp< SkTextBlob > &blob)
A 4x4 matrix using column-major storage.
static constexpr Matrix MakeTranslation(const Vector3 &t)
static constexpr Matrix MakeScale(const Vector3 &s)
constexpr TSize< Type > GetSize() const
Returns the size of the rectangle which may be negative in either width or height and may have been c...
constexpr static TRect MakeXYWH(Type x, Type y, Type width, Type height)
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)