diff --git a/CMakeLists.txt b/CMakeLists.txt index 05b9ecf7..b50c2438 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -650,6 +650,9 @@ if(EUI_BUILD_TEST_FIXTURES) target_compile_definitions(${probe_name} PRIVATE EUI_TEST_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}") add_test(NAME ${probe_name} COMMAND ${probe_name} --frames 3) set_tests_properties(${probe_name} PROPERTIES LABELS "probe") + if(probe_name STREQUAL "markdown_viewport_probe") + set_tests_properties(${probe_name} PROPERTIES SKIP_RETURN_CODE 77) + endif() if(probe_name STREQUAL "gpu_image_probe") set_tests_properties(${probe_name} PROPERTIES FAIL_REGULAR_EXPRESSION "Validation Error;VUID-") endif() diff --git a/components/markdown.h b/components/markdown.h index 9e9e7cd8..4e6e578a 100644 --- a/components/markdown.h +++ b/components/markdown.h @@ -817,7 +817,9 @@ inline std::shared_ptr cachedMarkdownDocument(const std: } // namespace detail class MarkdownBuilder { -public: + struct ViewportLayout; + + public: MarkdownBuilder(core::dsl::Ui& ui, std::string id) : ui_(ui), id_(std::move(id)) {} @@ -882,7 +884,21 @@ class MarkdownBuilder { MarkdownBuilder& theme(const theme::ThemeColorTokens& tokens) { style_ = MarkdownStyle(tokens); return *this; } MarkdownBuilder& zIndex(int value) { zIndex_ = value; return *this; } + // Document-local logical coordinates. This only limits construction; the caller + // clips its scroll viewport. Omit this option for the original complete tree. + MarkdownBuilder& viewport(float top, float height, float overscan = 32.0f) { + viewportEnabled_ = std::isfinite(top) && std::isfinite(height) && std::isfinite(overscan); + viewportTop_ = top; + viewportHeight_ = std::max(0.0f, height); + viewportOverscan_ = std::max(0.0f, overscan); + return *this; + } + void build() { + if (viewportEnabled_) { + buildViewport(); + return; + } const std::shared_ptr document = parsedDocument(); const std::vector& blocks = document->blocks; auto root = ui_.column(id_) @@ -1004,6 +1020,9 @@ class MarkdownBuilder { const MarkdownStyle& style) { std::string key; key.reserve(32 + style.fontFamily.size() + style.codeFontFamily.size()); + const auto revision = core::TextPrimitive::fontConfigurationRevision(); + appendInlineLayoutKey(key, static_cast(revision)); + appendInlineLayoutKey(key, static_cast(revision >> 32)); appendInlineLayoutKey(key, width); appendInlineLayoutKey(key, fontSize); appendInlineLayoutKey(key, lineHeight); @@ -1069,7 +1088,7 @@ class MarkdownBuilder { return detail::measureMarkdownTextWidth(text, fontFamily, runFontSize, weight) + padding; }; - auto pushFittingSegment = [&](detail::MarkdownRun run, std::string text) { + auto pushFittingSegment = [&](const detail::MarkdownRun& run, std::string text) { if (text.empty()) { return; } @@ -1089,7 +1108,10 @@ class MarkdownBuilder { if (hasLineItem) { cursorX += gap; } - segments.push_back({std::move(run), std::move(text), cursorX, cursorY, segmentWidth, chip}); + // A segment needs run metadata, not a copy of the entire source run. + // Keeping run.text here made one long paragraph quadratic in memory. + detail::MarkdownRun metadata{run.kind, run.style, {}, {}, {}}; + segments.push_back({std::move(metadata), std::move(text), cursorX, cursorY, segmentWidth, chip}); cursorX += segmentWidth; hasLineItem = true; }; @@ -1182,31 +1204,153 @@ class MarkdownBuilder { return inserted.first->second.layout; } - static float estimateInlineRunsHeight(const std::vector& runs, - float width, - float fontSize, - float lineHeight, - bool heading, - const MarkdownStyle& style) { - return cachedInlineLayout(runs, width, fontSize, lineHeight, heading, - core::HorizontalAlign::Left, style)->measuredHeight; + struct PositionedBlock { + std::size_t begin = 0, end = 0; + float top = 0.0f, height = 0.0f; + std::vector rows; + }; + struct ViewportLayout { + std::shared_ptr document; + std::vector blocks; + std::unordered_map*, std::shared_ptr> inlines; + float height = 0.0f; + bool collecting = true; + }; + static std::shared_ptr + plannedInlineLayout(const std::vector& runs, float width, float fontSize, float lineHeight, + bool heading, core::HorizontalAlign align, const MarkdownStyle& style, ViewportLayout* plan) { + if (plan) { + const auto found = plan->inlines.find(&runs); + if (found != plan->inlines.end()) return found->second; + } + auto layout = cachedInlineLayout(runs, width, fontSize, lineHeight, heading, align, style); + if (plan && plan->collecting) plan->inlines.emplace(&runs, layout); + return layout; + } + std::shared_ptr viewportLayout() { + struct Entry { + std::shared_ptr plan; + std::size_t tick, bytes; + }; + struct Cache { + std::unordered_map entries; + std::size_t tick = 0, bytes = 0; + }; + static thread_local Cache cache; + std::string key = inlineLayoutKey({}, width_, style_.bodySize, style_.bodyLineHeight, false, + core::HorizontalAlign::Left, style_); + for (float value : {style_.h1Size, style_.h2Size, style_.h3Size, style_.codeSize, style_.blockGap, + style_.listIndent, style_.codePadding, style_.quotePadding, style_.tableCellPadding}) + appendInlineLayoutKey(key, value); + key += markdown_; + const auto found = cache.entries.find(key); + if (found != cache.entries.end()) { + found->second.tick = ++cache.tick; + return found->second.plan; + } + auto plan = std::make_shared(); + plan->document = parsedDocument(); + const auto& blocks = plan->document->blocks; + for (std::size_t i = 0; i < blocks.size();) { + PositionedBlock item{i, i + 1, plan->height, 0.0f, {}}; + if (blocks[i].kind == detail::MarkdownBlockKind::TableRow) { + const float width = std::max(0.0f, width_ - std::max(0, blocks[i].quoteDepth) * 8.0f - + std::max(0, blocks[i].listDepth - 1) * style_.listIndent); + item.rows.push_back(0.0f); + while (item.end < blocks.size() && blocks[item.end].kind == detail::MarkdownBlockKind::TableRow) + ++item.end; + for (std::size_t row = i; row < item.end; ++row) { + item.height += estimateTableRowHeight(blocks[row], width, style_, plan.get()); + item.rows.push_back(item.height); + } + } else + item.height = estimateBlockHeight(blocks[i], width_, style_, plan.get()); + plan->height += item.height + (item.end < blocks.size() ? style_.blockGap : 0.0f); + i = item.end; + plan->blocks.push_back(std::move(item)); + } + plan->collecting = false; + std::size_t bytes = + sizeof(ViewportLayout) + key.capacity() + detail::markdownDocumentCacheBytes(markdown_, *plan->document) + + plan->blocks.capacity() * sizeof(PositionedBlock) + plan->inlines.bucket_count() * sizeof(void*); + for (const auto& block : plan->blocks) + detail::addMarkdownCacheBytes(bytes, block.rows.capacity() * sizeof(float)); + for (const auto& entry : plan->inlines) + detail::addMarkdownCacheBytes(bytes, + inlineLayoutCacheBytes(*entry.second, 0) + sizeof(entry) + 3 * sizeof(void*)); + constexpr std::size_t budget = 8 * 1024 * 1024; + if (bytes > budget / 2) return plan; + while (!cache.entries.empty() && (cache.entries.size() >= 8 || bytes > budget - cache.bytes)) { + const auto oldest = + std::min_element(cache.entries.begin(), cache.entries.end(), + [](const auto& a, const auto& b) { return a.second.tick < b.second.tick; }); + cache.bytes -= oldest->second.bytes; + cache.entries.erase(oldest); + } + cache.entries.emplace(std::move(key), Entry{plan, ++cache.tick, bytes}); + cache.bytes += bytes; + return plan; + } + bool viewportIntersects(float top, float height) const { + return viewportHeight_ > 0 && top + height >= viewportTop_ - viewportOverscan_ && + top <= viewportTop_ + viewportHeight_ + viewportOverscan_; + } + void buildViewport() { + auto plan = viewportLayout(); + activePlan_ = plan.get(); + auto root = + ui_.stack(id_) + .width(width_) + .height(height_.mode == core::SizeMode::WrapContent ? core::SizeValue::fixed(plan->height) : height_) + .margin(margin_.left, margin_.top, margin_.right, margin_.bottom) + .zIndex(zIndex_); + if (hasX_) root.x(x_); + if (hasY_) root.y(y_); + root.content([&] { + const auto first = + std::lower_bound(plan->blocks.begin(), plan->blocks.end(), viewportTop_ - viewportOverscan_, + [](const auto& block, float top) { return block.top + block.height < top; }); + for (auto it = first; it != plan->blocks.end() && viewportIntersects(it->top, it->height); ++it) { + activeBlockTop_ = it->top; + activeBlock_ = &*it; + ui_.stack(id_ + ".viewport." + std::to_string(it->begin)) + .position(0, it->top) + .size(width_, it->height) + .content([&] { + if (plan->document->blocks[it->begin].kind == detail::MarkdownBlockKind::TableRow) + renderTable(plan->document->blocks, it->begin, it->end); + else + renderBlock(plan->document->blocks[it->begin], it->begin); + }) + .build(); + } + }) + .build(); + activeBlock_ = nullptr; + activePlan_ = nullptr; + } + + static float estimateInlineRunsHeight(const std::vector& runs, float width, float fontSize, + float lineHeight, bool heading, const MarkdownStyle& style, + ViewportLayout* plan = nullptr) { + return plannedInlineLayout(runs, width, fontSize, lineHeight, heading, core::HorizontalAlign::Left, style, plan) + ->measuredHeight; } - static float estimateTableRowHeight(const detail::MarkdownBlock& block, - float contentWidth, - const MarkdownStyle& style) { + static float estimateTableRowHeight(const detail::MarkdownBlock& block, float contentWidth, + const MarkdownStyle& style, ViewportLayout* plan = nullptr) { const std::size_t cellCount = std::max(1, block.cells.size()); const float cellWidth = contentWidth / static_cast(cellCount); const float textWidth = std::max(0.0f, cellWidth - style.tableCellPadding * 2.0f); float height = 0.0f; + static const std::vector empty; for (std::size_t cell = 0; cell < cellCount; ++cell) { - height = std::max(height, estimateInlineRunsHeight( - cell < block.cells.size() ? block.cells[cell].runs : std::vector{}, - textWidth, - style.bodySize, - style.bodyLineHeight, - false, - style)); + const auto& runs = cell < block.cells.size() ? block.cells[cell].runs : empty; + const auto align = plan && cell < block.cells.size() ? detail::horizontalAlign(block.cells[cell].align) + : core::HorizontalAlign::Left; + height = std::max(height, plannedInlineLayout(runs, textWidth, style.bodySize, style.bodyLineHeight, + plan && block.tableHeader, align, style, plan) + ->measuredHeight); } return height + style.tableCellPadding * 2.0f; } @@ -1230,9 +1374,8 @@ class MarkdownBuilder { return height; } - static float estimateBlockHeight(const detail::MarkdownBlock& block, - float width, - const MarkdownStyle& style) { + static float estimateBlockHeight(const detail::MarkdownBlock& block, float width, const MarkdownStyle& style, + ViewportLayout* plan = nullptr) { const float quoteInset = static_cast(std::max(0, block.quoteDepth)) * 8.0f; const float listInset = static_cast(std::max(0, block.listDepth - 1)) * style.listIndent; const float contentWidth = std::max(0.0f, width - quoteInset - listInset); @@ -1250,7 +1393,8 @@ class MarkdownBuilder { if (block.kind == detail::MarkdownBlockKind::ListItem) { const float markerWidth = block.task ? 34.0f : 24.0f; const float textWidth = std::max(0.0f, contentWidth - markerWidth - 8.0f); - return std::max(style.bodyLineHeight, estimateInlineRunsHeight(block.runs, textWidth, style.bodySize, style.bodyLineHeight, false, style)); + return std::max(style.bodyLineHeight, estimateInlineRunsHeight(block.runs, textWidth, style.bodySize, + style.bodyLineHeight, false, style, plan)); } const bool heading = block.kind == detail::MarkdownBlockKind::Heading; @@ -1260,9 +1404,11 @@ class MarkdownBuilder { const float barWidth = 3.0f; const float barGap = 8.0f; const float textWidth = std::max(0.0f, contentWidth - barWidth - barGap - style.quotePadding * 2.0f); - return estimateInlineRunsHeight(block.runs, textWidth, fontSize, lineHeight, heading, style) + style.quotePadding * 2.0f; + return estimateInlineRunsHeight(block.runs, textWidth, fontSize, lineHeight, heading, style, plan) + + style.quotePadding * 2.0f; } - const float textHeight = estimateInlineRunsHeight(block.runs, contentWidth, fontSize, lineHeight, heading, style); + const float textHeight = + estimateInlineRunsHeight(block.runs, contentWidth, fontSize, lineHeight, heading, style, plan); return textHeight; } @@ -1412,7 +1558,8 @@ class MarkdownBuilder { float lineHeight, bool heading, core::HorizontalAlign align = core::HorizontalAlign::Left) { - return cachedInlineLayout(runs, width, fontSize, lineHeight, heading, align, style_)->measuredHeight; + return plannedInlineLayout(runs, width, fontSize, lineHeight, heading, align, style_, activePlan_) + ->measuredHeight; } void renderInlineRuns(const std::string& partId, @@ -1425,13 +1572,20 @@ class MarkdownBuilder { float x = 0.0f, float y = 0.0f, core::HorizontalAlign align = core::HorizontalAlign::Left) { - const std::shared_ptr layout = cachedInlineLayout( - runs, width, fontSize, lineHeight, heading, align, style_); + const std::shared_ptr layout = + plannedInlineLayout(runs, width, fontSize, lineHeight, heading, align, style_, activePlan_); ui_.stack(partId) .position(x, y) .size(width, height) .content([&] { - for (std::size_t index = 0; index < layout->segments.size(); ++index) { + auto first = layout->segments.begin(); + if (activePlan_) + first = std::lower_bound(first, layout->segments.end(), + viewportTop_ - viewportOverscan_ - activeBlockTop_ - y - lineHeight, + [](const auto& segment, float top) { return segment.y < top; }); + for (auto it = first; it != layout->segments.end(); ++it) { + if (activePlan_ && !viewportIntersects(activeBlockTop_ + y + it->y, lineHeight)) break; + const auto index = static_cast(it - layout->segments.begin()); renderInlineSegment(partId + ".seg." + std::to_string(index), layout->segments[index], fontSize, lineHeight, heading); } }) @@ -1591,6 +1745,34 @@ class MarkdownBuilder { const float quoteInset = static_cast(std::max(0, first.quoteDepth)) * 8.0f; const float listInset = static_cast(std::max(0, first.listDepth - 1)) * style_.listIndent; const float contentWidth = std::max(0.0f, width_ - quoteInset - listInset); + if (activePlan_ && activeBlock_) { + const auto& item = *activeBlock_; + ui_.stack(tableId) + .size(contentWidth, item.height) + .margin(quoteInset + listInset, 0, 0, 0) + .content([&] { + const auto first = std::lower_bound(item.rows.begin() + 1, item.rows.end(), + viewportTop_ - viewportOverscan_ - item.top); + for (std::size_t row = begin + (first - item.rows.begin() - 1); row < end; ++row) { + const auto index = row - begin; + const float top = item.top + item.rows[index]; + if (top > viewportTop_ + viewportHeight_ + viewportOverscan_) break; + const float height = item.rows[index + 1] - item.rows[index]; + if (!viewportIntersects(top, height)) continue; + activeBlockTop_ = top; + ui_.stack(tableId + ".viewport.row." + std::to_string(index)) + .position(0, item.rows[index]) + .size(contentWidth, height) + .content([&] { + renderTableRow(tableId + ".row." + std::to_string(index), blocks[row], contentWidth); + }) + .build(); + } + }) + .build(); + activeBlockTop_ = item.top; + return; + } float tableHeight = 0.0f; for (std::size_t row = begin; row < end; ++row) { @@ -1698,6 +1880,10 @@ class MarkdownBuilder { align); } + bool viewportEnabled_ = false; + float viewportTop_ = 0, viewportHeight_ = 0, viewportOverscan_ = 32, activeBlockTop_ = 0; + ViewportLayout* activePlan_ = nullptr; + const PositionedBlock* activeBlock_ = nullptr; core::dsl::Ui& ui_; std::string id_; std::string markdown_; diff --git a/core/render/text.cpp b/core/render/text.cpp index aba92f45..ffbf08b8 100644 --- a/core/render/text.cpp +++ b/core/render/text.cpp @@ -295,6 +295,11 @@ std::string firstExistingPath(std::initializer_list paths) { return {}; } +std::uint64_t& defaultFontConfigurationRevision() { + static std::uint64_t value = 0; + return value; +} + std::string& defaultUiFontFileOverride() { static std::string value; return value; @@ -1450,6 +1455,7 @@ void TextPrimitive::Impl::setDefaultFontFiles(const std::string& textFontFile, c defaultIconFontFileOverride() == iconFontFile) { return; } + ++defaultFontConfigurationRevision(); defaultUiFontFileOverride() = textFontFile; defaultIconFontFileOverride() = iconFontFile; clearSharedFontStackCache(); @@ -2013,6 +2019,8 @@ TextPrimitive::TextMetrics TextPrimitive::measureTextMetrics(const std::string& Vec2 TextPrimitive::measureTextSize(const TextStyle& style) { return Impl::measureTextSize(style); } +std::uint64_t TextPrimitive::fontConfigurationRevision() { return core::defaultFontConfigurationRevision(); } + void TextPrimitive::setDefaultFontFiles(const std::string& textFontFile, const std::string& iconFontFile) { Impl::setDefaultFontFiles(textFontFile, iconFontFile); } diff --git a/core/render/text.h b/core/render/text.h index 34b01e74..77134965 100644 --- a/core/render/text.h +++ b/core/render/text.h @@ -81,6 +81,8 @@ class TextPrimitive { float fontSize = 16.0f, int fontWeight = 400); static Vec2 measureTextSize(const TextStyle& style); + // Changes when the default font configuration changes; metric caches may key on it. + static std::uint64_t fontConfigurationRevision(); static void setDefaultFontFiles(const std::string& textFontFile, const std::string& iconFontFile); void prepare(); diff --git a/docs/benchmarks/markdown-viewport-20261023.json b/docs/benchmarks/markdown-viewport-20261023.json new file mode 100644 index 00000000..11a759b9 --- /dev/null +++ b/docs/benchmarks/markdown-viewport-20261023.json @@ -0,0 +1 @@ +{"metadata":{"baseline":"22f0ca35d19c06e989d64998ce18a27101ffd55c markdown.h + identical current core library","warmups":5,"samples":60,"width":420,"viewport":[500,180,32],"font_sha256":{"ttf":"fb54f2910cd8fdbc13ae3058ad6311019ac5ee25deb5bf3623a01ff0c9c680ff","cff":"faa6c9df652116dde789d351359f3d7e5d2285a2b2a1f04a2d7244df706d5ea9"},"binaries":{"baseline":"9d42d2754cc8b402b234a39addce8dfe0a3907ed71c424ca4ebbef1055b404ad","current":"eb1457adf3d3f012dcb20f3d27bb7fd02f5031595ccae3e314a3735cf23b51a0"},"load_start":[0.763671875,0.98046875,1.73388671875]},"runs":[{"font":"ttf","mode":"full","round":0,"version":"baseline","rss_kib":112168,"loadavg":[0.78271484375,0.98095703125,1.7294921875],"nodes":9003,"p50_ms":88.9564,"p95_ms":90.2075,"samples_ms":[73.2178,87.949,88.4651,88.3878,89.1271,89.3839,88.1337,87.8453,89.0669,88.3343,87.9957,88.9693,88.5939,88.4567,89.325,89.6637,88.9564,89.9743,88.0435,88.5985,88.7925,88.3943,88.3096,88.854,88.4581,89.2713,90.3042,89.7162,88.8443,90.2075,88.4913,88.6677,88.5242,88.492,88.0195,89.215,89.8833,89.098,90.7113,89.4813,88.7797,88.6673,89.2238,88.8761,89.0637,89.0563,88.7125,89.8956,89.2909,88.5262,89.1217,89.1527,89.0526,89.1847,89.9996,89.1502,88.9921,89.8849,88.5603,88.6845],"workload":"steady"},{"font":"ttf","mode":"viewport","round":0,"version":"current","rss_kib":14988,"loadavg":[0.78271484375,0.98095703125,1.7294921875],"nodes":400,"p50_ms":0.698435,"p95_ms":0.841795,"samples_ms":[0.914697,0.920468,0.841795,0.792285,0.811931,0.828107,0.790409,0.798622,0.79555,0.789163,0.736983,0.729761,0.731922,0.73778,0.714928,0.693158,0.707214,0.70982,0.705408,0.696793,0.698435,0.700493,0.69747,0.696762,0.696324,0.697023,0.688235,0.691193,0.688608,0.693776,0.693634,0.691579,0.692135,0.691354,0.697791,0.694002,0.694313,0.693535,0.699031,0.688424,0.692321,0.69918,0.689112,0.702969,0.693636,0.692855,0.691438,0.689775,0.702333,0.698624,0.724821,0.697481,0.716439,0.698444,0.709723,0.694458,0.751535,0.694597,0.689577,0.694511],"workload":"steady"},{"font":"ttf","mode":"viewport","round":1,"version":"current","rss_kib":14980,"loadavg":[0.78271484375,0.98095703125,1.7294921875],"nodes":400,"p50_ms":0.687291,"p95_ms":0.891833,"samples_ms":[0.903299,0.891833,0.836913,0.832036,0.842153,0.783607,0.776273,0.788232,0.776899,0.75165,0.719504,0.726126,0.701589,0.690748,0.683648,0.690094,0.69507,0.709964,0.690409,0.700788,0.695543,0.684903,0.696988,0.689808,0.684306,0.693845,0.685329,0.678098,0.676315,0.686713,0.68299,0.682822,0.693738,0.693058,0.684945,0.68983,0.690578,0.684796,0.683414,0.680583,0.687291,0.684793,0.694415,0.910878,0.631445,0.580165,0.580085,0.571378,0.578105,0.585893,0.571136,0.578234,0.5694,0.569601,0.571315,0.561838,0.574836,0.560578,0.571173,0.586756],"workload":"steady"},{"font":"ttf","mode":"full","round":1,"version":"baseline","rss_kib":112060,"loadavg":[0.80029296875,0.9814453125,1.72509765625],"nodes":9003,"p50_ms":88.246,"p95_ms":90.2823,"samples_ms":[88.2318,88.2859,89.636,88.5339,87.4742,87.3488,86.6422,87.045,87.4098,86.8126,88.4862,89.649,87.4039,88.246,88.704,88.6244,88.0594,88.4673,88.0675,88.1703,88.7263,87.4359,87.9364,89.0304,89.2439,90.4059,88.3434,89.7222,88.5079,87.9844,87.7638,88.3555,88.1443,88.0531,87.6458,87.7321,90.3383,89.4679,87.5288,88.2215,89.9073,89.7905,90.2823,78.7569,49.2106,51.921,46.1205,46.1756,48.1141,55.8473,89.3401,89.594,89.257,89.1223,88.5543,89.2522,88.7799,88.8612,87.7211,87.7013],"workload":"steady"},{"font":"ttf","mode":"full","round":2,"version":"baseline","rss_kib":111952,"loadavg":[0.896484375,0.99853515625,1.72705078125],"nodes":9003,"p50_ms":87.8107,"p95_ms":89.3136,"samples_ms":[87.0551,86.6524,87.0897,87.0682,87.3502,87.2919,87.3522,86.5922,87.3037,87.299,87.2924,87.5034,86.7202,87.5847,87.5407,86.8298,87.5899,89.3375,87.1095,86.5533,87.049,87.1593,86.4216,88.4288,88.6331,87.8107,88.3072,89.3136,88.4341,88.9356,88.9821,88.7139,88.5857,88.2123,88.4584,89.4711,88.7975,87.6679,87.8725,88.0462,87.5378,88.8972,88.0798,87.4041,88.1461,87.5645,87.4493,88.8278,87.9094,87.5491,88.854,88.9951,87.8361,87.9551,87.8208,87.5995,87.8606,87.303,88.5819,88.3601],"workload":"steady"},{"font":"ttf","mode":"viewport","round":2,"version":"current","rss_kib":14912,"loadavg":[0.896484375,0.99853515625,1.72705078125],"nodes":400,"p50_ms":0.692823,"p95_ms":0.830153,"samples_ms":[0.784205,0.791873,0.829956,0.830153,0.8401,0.801843,0.780261,0.807192,0.8269,0.800138,0.771702,0.774758,0.770011,0.748367,0.729946,0.740168,0.740954,0.723809,0.737449,0.700865,0.699713,0.701795,0.692418,0.698431,0.689,0.689817,0.694567,0.693739,0.680125,0.693331,0.69649,0.682216,0.690962,0.689207,0.692823,0.68135,0.695217,0.691109,0.685759,0.688221,0.967233,0.624876,0.582413,0.58105,0.575174,0.569244,0.571974,0.56617,0.56983,0.56017,0.567348,0.564412,0.566442,0.569069,0.571588,0.577822,0.571748,0.573905,0.594489,0.5827],"workload":"steady"},{"font":"ttf","mode":"append","round":0,"version":"baseline","rss_kib":112480,"loadavg":[1.30029296875,1.08154296875,1.74658203125],"nodes":9005,"p50_ms":88.6252,"p95_ms":91.3658,"samples_ms":[87.7047,88.3353,88.5249,89.0106,89.8215,89.2057,88.8232,89.2793,89.9887,89.2857,89.7399,88.9076,88.9683,87.9969,89.0872,88.5957,88.4562,88.7459,89.081,88.6169,88.138,89.0176,88.5766,87.848,88.7148,89.2932,88.6144,89.4497,88.9188,88.4896,88.566,88.1883,88.1193,88.5641,88.1001,87.5955,88.4062,87.5923,88.2908,89.4861,89.0467,88.8975,88.0609,88.092,88.0538,88.7201,87.7183,87.7587,88.6252,89.6241,90.1754,89.733,91.3658,91.3833,91.5477,53.3048,52.607,52.1377,54.8848,88.9159],"workload":"append"},{"font":"ttf","mode":"append","round":0,"version":"current","rss_kib":21664,"loadavg":[1.30029296875,1.08154296875,1.74658203125],"nodes":400,"p50_ms":22.2397,"p95_ms":22.6797,"samples_ms":[22.5443,22.6352,23.0251,22.4284,22.5064,22.6905,22.4262,22.1476,22.6797,22.254,22.0608,22.1245,22.3071,22.2528,22.4943,22.0993,22.0859,22.038,22.0817,22.0539,22.0585,22.2813,22.4446,22.462,22.1999,22.1957,22.1813,22.2085,22.1864,22.343,22.2252,22.2882,22.1396,22.2438,22.1896,22.3091,22.2192,22.209,22.1557,22.5851,22.2127,22.1084,22.2097,22.2657,22.2816,22.4329,22.278,22.3385,22.2397,22.1422,22.3721,22.2918,22.1963,22.1946,22.1952,22.1915,22.336,22.1027,22.0845,22.2398],"workload":"append"},{"font":"ttf","mode":"append","round":1,"version":"current","rss_kib":21556,"loadavg":[1.30029296875,1.08154296875,1.74658203125],"nodes":400,"p50_ms":22.6878,"p95_ms":24.1313,"samples_ms":[23.6488,23.7461,23.7574,23.0448,23.0911,22.8391,23.0789,22.8643,23.3773,22.6805,22.8287,22.8581,22.6878,22.8681,23.2569,22.5188,23.4288,22.6422,22.8988,22.7006,22.887,22.5519,23.1331,22.9171,22.8421,22.5727,22.9397,23.2648,22.8341,22.845,23.2366,28.7363,28.5011,24.1313,22.2406,22.4714,22.4833,22.3124,22.4646,22.303,22.418,22.5277,22.4156,22.1911,22.3208,22.3987,22.295,22.2858,22.3005,22.3604,22.4193,22.241,22.2542,22.302,22.3719,22.2919,22.2544,22.3032,22.8759,22.6323],"workload":"append"},{"font":"ttf","mode":"append","round":1,"version":"baseline","rss_kib":112324,"loadavg":[1.3564453125,1.09716796875,1.748046875],"nodes":9005,"p50_ms":88.9393,"p95_ms":91.1299,"samples_ms":[87.1088,87.3185,87.681,86.9172,87.7088,87.4192,87.6869,88.0078,87.7152,87.1847,87.9497,87.7409,87.9337,87.8108,87.3001,88.9956,89.3545,88.656,88.9085,88.4165,88.8757,89.1232,88.9393,87.9325,88.6328,89.6689,88.5881,89.2591,89.4241,89.6436,88.8464,88.2146,88.4625,88.9819,88.6801,87.767,88.6767,87.623,89.5835,90.7777,90.1899,90.1884,90.6102,90.5677,90.5492,90.527,90.6907,89.1341,87.9816,89.0254,90.4359,91.6549,91.0121,90.3555,90.964,89.8618,90.2614,91.1299,89.9044,92.1596],"workload":"append"},{"font":"ttf","mode":"append","round":2,"version":"baseline","rss_kib":112352,"loadavg":[1.669921875,1.17529296875,1.7666015625],"nodes":9005,"p50_ms":88.1638,"p95_ms":90.2496,"samples_ms":[88.7843,87.8892,87.5058,89.2873,88.8013,88.0974,88.1638,87.2705,86.778,87.3722,86.7114,87.2572,86.7081,87.1483,88.1576,87.7233,87.6983,88.9368,87.1697,87.7713,87.0619,87.0259,87.5504,86.53,87.1128,87.6163,87.7213,87.7151,89.2708,89.9595,88.9473,89.7844,88.8549,88.8794,88.2442,88.3297,88.938,88.8656,88.0978,88.8683,88.08,88.5811,88.86,88.3321,88.0416,88.7713,89.0327,87.2971,87.879,88.6934,88.4929,88.9098,88.9663,88.0193,90.1434,90.5688,90.5309,90.2496,88.1301,88.4691],"workload":"append"},{"font":"ttf","mode":"append","round":2,"version":"current","rss_kib":21600,"loadavg":[1.669921875,1.17529296875,1.7666015625],"nodes":400,"p50_ms":22.2013,"p95_ms":22.6249,"samples_ms":[22.6221,22.5376,22.9588,22.5797,22.4654,22.436,22.4523,22.0207,22.9003,22.1151,21.9802,22.0525,22.1448,22.246,22.4734,22.0521,22.0367,21.9716,21.9661,22.0113,21.9715,21.9459,22.018,22.0318,22.0095,22.0908,22.164,21.9731,22.2124,22.1205,22.1875,22.0436,22.0013,22.2213,22.0357,22.0062,22.3012,22.1956,22.0873,22.1482,22.0344,22.6249,22.2238,22.2713,22.2543,22.1534,22.2669,22.2961,22.4274,22.3024,22.3151,22.2013,22.1683,22.5023,22.3414,22.2159,22.3382,22.3087,22.2789,22.308],"workload":"append"},{"font":"cff","mode":"full","round":0,"version":"baseline","rss_kib":111592,"loadavg":[1.77685546875,1.2060546875,1.7734375],"nodes":9003,"p50_ms":95.153,"p95_ms":97.2303,"samples_ms":[94.232,94.7188,94.2604,94.6533,94.8363,94.7604,94.4412,94.2276,94.8479,95.0888,97.439,95.0134,95.414,94.8165,95.153,94.3966,94.488,95.0607,95.3293,95.1093,95.1606,95.7421,95.0708,96.3331,96.4192,97.2303,97.2956,96.1689,95.8805,96.559,96.1981,94.7477,95.873,96.1687,95.3207,95.2482,95.0336,95.616,94.3851,95.1773,95.2558,95.2622,94.4528,95.4617,94.9468,94.8424,95.5852,95.0845,95.0638,95.0096,95.3723,94.0432,94.5392,95.9274,96.9303,95.2561,95.647,94.9953,95.1576,94.2976],"workload":"steady"},{"font":"cff","mode":"viewport","round":0,"version":"current","rss_kib":14684,"loadavg":[1.77685546875,1.2060546875,1.7734375],"nodes":432,"p50_ms":0.740157,"p95_ms":0.839767,"samples_ms":[0.842744,0.839767,0.819517,0.793147,0.79439,0.758238,0.742085,0.742589,0.74735,0.740084,0.738215,0.760876,0.760242,0.755645,0.747083,0.754741,0.743665,0.748255,0.742144,0.745796,0.748566,0.743377,0.740157,0.739581,0.74086,0.74022,0.748299,0.721374,0.729984,0.738676,0.743886,0.73794,0.744151,0.75409,0.751311,0.746056,0.968715,0.685088,0.624318,0.606875,0.603226,0.625312,0.59534,0.602389,0.601572,0.612117,0.614613,0.612899,0.610582,0.611317,0.615978,0.614245,0.61243,0.60677,0.61249,0.611748,0.61385,0.609191,0.61215,0.607528],"workload":"steady"},{"font":"cff","mode":"viewport","round":1,"version":"current","rss_kib":14664,"loadavg":[1.77685546875,1.2060546875,1.7734375],"nodes":432,"p50_ms":0.735061,"p95_ms":0.759611,"samples_ms":[0.760021,0.743619,0.739728,0.746022,0.742124,0.742111,0.736017,0.738169,0.741,0.759611,0.730846,0.729948,0.738105,0.736016,0.740231,0.737391,0.743629,0.749784,0.755868,0.739656,0.740046,0.738452,0.735061,0.740648,0.750078,0.732965,0.737587,0.717108,0.719292,0.735485,0.74118,0.730492,0.73892,0.74,0.743397,0.734545,0.87621,0.653069,0.609964,0.602352,0.598805,0.618952,0.596768,0.604955,0.602685,0.612773,0.614839,0.6066,0.61909,0.609228,0.608304,0.613296,0.61038,0.608151,0.636406,0.606142,0.606358,0.613657,0.603653,0.61413],"workload":"steady"},{"font":"cff","mode":"full","round":1,"version":"baseline","rss_kib":111296,"loadavg":[1.8916015625,1.2490234375,1.78173828125],"nodes":9003,"p50_ms":95.0723,"p95_ms":96.614,"samples_ms":[53.9159,79.1736,95.9773,95.1496,94.5269,94.9973,95.7925,95.4098,95.4583,94.9083,94.5143,94.9273,94.9369,93.8345,94.7562,95.6808,94.4188,95.3052,94.5511,94.8843,94.2155,95.1659,95.3126,95.0723,94.7513,94.5513,93.9262,94.9893,95.3699,95.2955,94.7787,94.6509,94.2024,95.2593,95.0739,94.7912,94.764,95.4,94.5068,93.9712,96.614,96.248,95.8072,95.1579,96.3365,95.494,95.4131,95.5973,95.5947,95.9946,95.5929,94.9285,96.2674,96.1442,97.3567,94.7779,94.0034,94.6123,94.5915,100.091],"workload":"steady"},{"font":"cff","mode":"full","round":2,"version":"baseline","rss_kib":111524,"loadavg":[1.900390625,1.26171875,1.783203125],"nodes":9003,"p50_ms":95.1548,"p95_ms":96.5206,"samples_ms":[94.6171,95.0368,95.3958,96.1206,107.348,95.0713,94.0164,94.87,94.2489,96.4089,94.6023,95.2691,95.1548,94.7584,94.0937,96.4092,95.4306,95.4112,95.9037,95.266,95.4401,95.7758,95.6989,95.017,94.6669,97.3059,96.1613,94.7108,95.2115,95.2585,95.0478,93.7477,95.7544,96.5206,96.1697,96.265,95.4651,96.053,95.3172,96.1812,95.6799,95.3664,94.6714,95.286,94.6023,94.9269,93.8997,94.3477,93.7163,93.8409,94.0649,93.6315,94.265,95.1377,94.1027,94.3693,93.9629,95.698,94.8044,94.1691],"workload":"steady"},{"font":"cff","mode":"viewport","round":2,"version":"current","rss_kib":14668,"loadavg":[1.900390625,1.26171875,1.783203125],"nodes":432,"p50_ms":0.738874,"p95_ms":0.792851,"samples_ms":[0.792851,0.748948,0.738197,0.736727,0.737127,0.740096,0.741669,0.732948,0.733942,0.738874,0.739166,0.739542,0.746445,0.748457,0.744886,0.747881,0.862787,0.758133,0.761539,0.763099,0.756405,0.744858,0.765886,0.759598,0.758464,0.733603,0.746606,0.727263,0.727745,0.739588,0.736952,0.749197,0.741453,0.740799,0.742938,0.741765,0.745413,0.750223,0.719686,0.876597,0.640247,0.628772,0.603322,0.614193,0.612251,0.610812,0.618084,0.606579,0.609402,0.614231,0.607084,0.612411,0.608194,0.607324,0.613944,0.607954,0.610138,0.614583,0.609036,0.609898],"workload":"steady"},{"font":"cff","mode":"append","round":0,"version":"baseline","rss_kib":111688,"loadavg":[1.828125,1.25732421875,1.77880859375],"nodes":9005,"p50_ms":95.7566,"p95_ms":97.4684,"samples_ms":[94.2028,94.3822,93.9243,94.3712,94.88,94.8698,96.4361,96.7461,96.2242,95.8883,95.2147,96.0903,96.529,96.0167,96.0001,95.1934,95.7869,97.741,99.2839,95.691,95.7847,95.6826,95.8903,95.7478,94.907,95.1725,95.9379,96.0308,94.5126,95.2042,94.9189,94.8101,95.7325,95.3599,95.4345,95.291,95.1568,95.396,94.0382,96.1992,95.0053,95.7578,95.4547,95.7566,94.4317,95.2645,95.1303,96.0501,97.4684,96.0643,96.2962,97.1734,97.0529,96.9375,96.6412,96.2648,95.4017,96.0599,96.1357,95.9604],"workload":"append"},{"font":"cff","mode":"append","round":0,"version":"current","rss_kib":20840,"loadavg":[1.828125,1.25732421875,1.77880859375],"nodes":432,"p50_ms":25.205,"p95_ms":25.6127,"samples_ms":[25.9068,25.5988,26.1086,25.5509,25.5268,25.5556,25.4068,25.2709,25.6127,25.1826,25.1454,25.2169,25.0975,25.283,25.4423,25.0976,25.1587,25.15,25.1823,25.2116,25.3574,25.2417,25.1272,25.496,25.1534,25.201,25.205,25.1305,25.3625,25.264,25.1542,25.5569,25.0567,25.2126,25.4785,25.1002,25.0944,25.094,25.4631,25.1091,25.0832,25.1075,25.1084,25.0663,25.1599,25.1248,25.089,25.0469,25.1581,25.0975,25.096,25.1629,25.1031,25.2416,25.5642,25.2463,25.2498,25.2471,25.3059,25.2673],"workload":"append"},{"font":"cff","mode":"append","round":1,"version":"current","rss_kib":20940,"loadavg":[1.84228515625,1.27001953125,1.7802734375],"nodes":432,"p50_ms":25.2528,"p95_ms":25.8593,"samples_ms":[25.877,25.8234,26.5951,25.8593,25.4323,25.6137,25.5649,25.2189,25.7726,25.1283,25.0296,25.0522,25.0486,25.1616,25.4177,24.9831,25.0019,25.0314,25.0255,25.1127,25.0958,25.1587,25.0954,25.0549,25.0612,25.0927,25.1167,25.1864,25.0491,25.1072,25.0483,25.071,25.0055,25.2528,25.4913,25.2419,25.251,25.3837,25.6125,25.3672,25.3258,25.4077,25.2801,25.3769,25.2736,25.4097,25.2453,25.324,25.3221,25.251,25.599,25.572,25.3675,25.2808,25.2249,25.2227,25.3382,25.4205,25.4676,25.3108],"workload":"append"},{"font":"cff","mode":"append","round":1,"version":"baseline","rss_kib":111788,"loadavg":[1.7744140625,1.26513671875,1.77587890625],"nodes":9005,"p50_ms":94.6837,"p95_ms":96.4936,"samples_ms":[94.101,93.7521,93.5059,94.1811,93.8359,94.1675,94.4695,94.2285,93.93,94.5106,94.523,94.106,94.4819,94.3624,93.8429,94.1844,93.9919,93.7268,93.376,94.5798,94.2021,94.0569,95.22,94.4569,93.7847,94.4234,95.1252,94.7366,95.2181,94.637,95.4727,96.6606,96.4826,97.0911,95.5626,95.9756,96.1465,95.0703,95.2882,96.4936,94.7344,95.2263,95.4002,94.9887,95.5805,95.0761,94.6837,95.5012,94.9763,95.3394,95.0441,94.5728,95.63,95.0239,95.3142,94.6281,95.572,94.455,94.6439,95.367],"workload":"append"},{"font":"cff","mode":"append","round":2,"version":"baseline","rss_kib":111876,"loadavg":[1.7119140625,1.26025390625,1.771484375],"nodes":9005,"p50_ms":94.7914,"p95_ms":96.6083,"samples_ms":[94.1946,94.4463,93.0128,94.3308,95.1695,94.4246,95.2541,96.6083,107.581,106.817,94.9108,94.731,93.4662,95.0172,95.2767,94.6199,94.6328,94.1282,94.1042,93.7809,94.2086,94.149,94.1909,95.13,95.2656,93.6628,94.8176,94.3401,95.3047,94.909,94.7835,95.8021,94.3522,94.7914,94.7575,94.687,95.2275,95.5448,95.3345,94.3104,94.4169,93.8673,94.727,94.4743,94.0829,94.8286,96.4907,96.2252,96.4585,95.4358,94.931,94.9766,95.4881,95.0622,95.3181,94.3466,94.8974,93.9722,94.2844,95.2416],"workload":"append"},{"font":"cff","mode":"append","round":2,"version":"current","rss_kib":20832,"loadavg":[1.65478515625,1.255859375,1.76708984375],"nodes":432,"p50_ms":25.282,"p95_ms":25.6995,"samples_ms":[12.4202,12.2423,12.5534,12.2083,12.0624,12.1177,12.0987,12.0596,12.4164,12.1811,12.0827,12.252,12.0628,12.0243,12.2119,11.8281,11.9136,11.84,11.8978,13.1972,25.2187,25.1197,25.3705,25.2843,25.282,25.3122,25.2188,25.3648,25.2499,25.311,25.263,25.4055,25.2782,25.1392,25.4847,25.3326,25.2441,25.2766,25.7788,25.4261,25.166,25.3107,25.469,25.4486,25.3857,25.516,25.6995,25.6549,25.4588,25.7348,25.5786,25.4949,25.5487,25.627,25.5948,25.5917,25.5439,25.6376,25.5768,25.5315],"workload":"append"}]} diff --git a/docs/benchmarks/markdown-viewport-20261023.md b/docs/benchmarks/markdown-viewport-20261023.md new file mode 100644 index 00000000..1b8c49bd --- /dev/null +++ b/docs/benchmarks/markdown-viewport-20261023.md @@ -0,0 +1,68 @@ +# Markdown viewport paired measurement (2026-10-23) + +Ubuntu 24.04, Linux 7.0.0-38-generic, Intel Core Ultra 5 225H (14 CPUs), +30 GiB RAM, GCC 13.3.0, CMake 3.28.3, Ninja, GLFW/OpenGL Release +(`-O3 -DNDEBUG`, EUI's target `-Os -fno-exceptions -fno-rtti`). The CPU-only +component benchmark creates no window/GPU; actual viewer and pixel checks used +Xvfb/Mesa llvmpipe. Host desktop processes remained active; no frequency pinning. + +The baseline is upstream dev **22f0ca35d19c06e989d64998ce18a27101ffd55c**'s +`markdown.h`; both benchmark executables link the identical current core library. +The core changes only add font-configuration invalidation and do not change +measurement metrics. This isolates the Markdown header algorithm. It is not a +comparison against another OS, renderer, font, or Release configuration. + +Input: repeat `中文 English 😀 e\xCC\x81 ` until at least 18,000 UTF-8 bytes, +width 420, default MarkdownStyle, position (11,7), margin (3,5,7,9). Viewport: +top 500, height 180, overscan 32. Each process: five warmups, 60 timed builds, +layout, node counting and teardown. The geometry oracle's map-copying is excluded. +Steady uses the same paragraph; append adds the changing decimal sample index. +Three paired AB/BA/AB rounds per font/workload; table values are the median of +three per-run p50/p95 values, not a pooled percentile. RSS is per-process maximum +from `/usr/bin/time`, including startup, warmups and font loading. + +| Font/workload | Baseline p50/p95 ms | Viewport p50/p95 ms | Nodes old/new | RSS old/new KiB | +| --- | --- | --- | --- | --- | +| Bundled TTF, steady | 88.246 / 90.208 | 0.693 / 0.842 | 9003 / 400 | 112060 / 14980 | +| Bundled TTF, append | 88.625 / 91.130 | 22.240 / 22.680 | 9005 / 400 | 112352 / 21600 | +| Noto Sans SC CFF, steady | 95.153 / 96.614 | 0.739 / 0.793 | 9003 / 432 | 111524 / 14668 | +| Noto Sans SC CFF, append | 94.791 / 96.608 | 25.253 / 25.700 | 9005 / 432 | 111788 / 20840 | + +[Raw chronological samples and font/binary SHA256](markdown-viewport-20261023.json). +The TTF is the existing bundled JingNanJunJunTi asset; CFF is Noto Sans SC 2.004 +regular OTF, SHA256 `faa6c9df652116dde789d351359f3d7e5d2285a2b2a1f04a2d7244df706d5ea9`. +No font/resource is added. Reproduce from a Release Ninja fixture build configured +with `CMAKE_EXPORT_COMPILE_COMMANDS=ON` (build `markdown_viewport` first): + +```sh +python3 docs/benchmarks/markdown-viewport-pair.py --build /absolute/build/glfw-opengl \ + --font assets/JingNanJunJunTi-JinNanJunJunTi-Bold-2.ttf > paired-ttf.json +python3 docs/benchmarks/markdown-viewport-pair.py --build /absolute/build/glfw-opengl \ + --font /absolute/path/NotoSansSC-Regular.otf > paired-cff.json +``` + +The script uses that build's exact compiler/link argv, compiles baseline/current +headers against one core, then alternates processes without overlapping builds. +It only uses the Python standard library, Ninja and `/usr/bin/time`; these are +measurement tools, not new product dependencies. Appending still reparses and +lays out the changed paragraph: these results do not establish 60 FPS or GPU frame +time, and do not cover arbitrary documents, hardware GPUs, Windows or compositor input. + +Independent local Release builds with fixtures enabled, apps/user apps/tray disabled: + +| Backend | Full CTest | Installed SDK consumer | Viewer | +| --- | --- | --- | --- | +| GLFW/OpenGL | 38 passed | 1 passed | mapped, wheel-scrolled, closed | +| SDL2/OpenGL | 38 passed | 1 passed | mapped, wheel-scrolled, closed | +| GLFW/Vulkan | 37 passed, 1 explicit skip | 1 passed | mapped, wheel-scrolled, closed | +| SDL2/Vulkan | 37 passed, 1 explicit skip | 1 passed | mapped, wheel-scrolled, closed | + +OpenGL pixel probes compare exact RGBA bytes for twelve light/dark × 1/1.5 DPI × +scroll combinations. The OpenGL readback probe returns skip 77 on Vulkan; +geometry/ID tests and viewer interaction still run there. No Vulkan pixel-equality +claim. SDL was rerun with `WAYLAND_DISPLAY` unset and `SDL_VIDEODRIVER=x11` so the +final checks used a private X server. All four final viewport unit reruns passed. +The unit was also independently compiled with ASAN (leak detection enabled) and +UBSAN, both passed without a report; these two instrument the changed header, +while linking the same uninstrumented Release core. Full sanitizer library coverage +is left to the consuming application's instrumented build. diff --git a/docs/benchmarks/markdown-viewport-pair.py b/docs/benchmarks/markdown-viewport-pair.py new file mode 100644 index 00000000..3bf8a5b3 --- /dev/null +++ b/docs/benchmarks/markdown-viewport-pair.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +"""Build paired header benchmarks against one Release core; Ninja/compile_commands required.""" +import argparse, json, os, shlex, statistics, subprocess, tempfile +from pathlib import Path +p = argparse.ArgumentParser() +p.add_argument('--build', type=Path, required=True) +p.add_argument('--font', type=Path, required=True) +p.add_argument('--rounds', type=int, default=3) +a = p.parse_args() +root = Path(__file__).resolve().parents[2] +build = a.build.resolve() +font = a.font.resolve() +unit = root / 'tests/unit/markdown_viewport.cpp' +helper = unit.read_text().split('int main(', 1)[0] +main = 'int main() {\n const auto* font=std::getenv("EUI_TEXT_TEST_FONT");\n core::TextPrimitive::setDefaultFontFiles(font ? font : "/tmp/mirage-eui-markdown-20261023/assets/JingNanJunJunTi-JinNanJunJunTi-Bold-2.ttf",{});\n const std::string mode=std::getenv("EUI_VIEWPORT_MODE") ? std::getenv("EUI_VIEWPORT_MODE") : "viewport";\n components::MarkdownStyle style;\n std::string paragraph;\n while(paragraph.size()<18000) paragraph += "中文 English 😀 e\\xcc\\x81 ";\n std::vector values;\n for(int i=0;i<65;++i){\n const auto source=mode=="append" ? paragraph+std::to_string(i) : paragraph;\n const auto start=std::chrono::steady_clock::now();\n const auto result=build(source,420,style,mode!="full",500,180,false);\n if(i>=5)values.push_back(std::chrono::duration(std::chrono::steady_clock::now()-start).count());\n if(i==64)std::cout<<"{\\"mode\\":\\""< 0.5f; + eui::Ui ui; + ui.begin("installed-viewport"); + components::markdown(ui, "document").width(200).markdown("Installed **viewport**") + .viewport(0, 80).build(); + ui.end(); + ui.layout(200, 100); + const auto revision = core::TextPrimitive::fontConfigurationRevision(); + return color.g > 0.5f && ui.find("document") && ui.find("document")->frame.height > 0 && + revision == core::TextPrimitive::fontConfigurationRevision(); } diff --git a/tests/fixtures/markdown_viewer.cpp b/tests/fixtures/markdown_viewer.cpp index 73186f3c..f7a2de22 100644 --- a/tests/fixtures/markdown_viewer.cpp +++ b/tests/fixtures/markdown_viewer.cpp @@ -173,15 +173,18 @@ void compose(eui::Ui& ui, const eui::Screen& screen) { .scrollbarGap(20.0f) .onChange([&viewer](float value) { viewer.scrollOffset = value; + app::requestUpdate(); }) .content([&](eui::Ui& contentUi, float contentWidth, float) { - components::markdown(contentUi, "markdown") + auto markdown = components::markdown(contentUi, "markdown") .theme(tokens) .width(contentWidth) .wrapContentHeight() .markdown(markdownSample()) - .zIndex(1) - .build(); + .zIndex(1); + if (const auto* viewport = contentUi.find("markdown.scroll")) + markdown.viewport(viewport->scrollOffset, viewportHeight); + markdown.build(); }) .build(); }) diff --git a/tests/probes/markdown_viewport_probe.cpp b/tests/probes/markdown_viewport_probe.cpp new file mode 100644 index 00000000..ea3f1917 --- /dev/null +++ b/tests/probes/markdown_viewport_probe.cpp @@ -0,0 +1,74 @@ +#include "components/markdown.h" +#include "core/dsl_runtime.h" +#include "core/render/render_backend.h" +#include "core/window/window_backend.h" +#if defined(EUI_RENDER_BACKEND_OPENGL) +#include +#if defined(EUI_WINDOW_BACKEND_SDL2) +#define SDL_MAIN_HANDLED +#include +#else +#define GLFW_INCLUDE_NONE +#include +#endif +#include +#include +int main() { +#if defined(EUI_WINDOW_BACKEND_SDL2) + SDL_SetMainReady(); + if (SDL_Init(SDL_INIT_VIDEO) != 0) return 1; +#else + if (!glfwInit()) return 1; +#endif + core::window::WindowCreateRequest request; + request.width = 480; request.height = 300; request.title = "Markdown viewport pixel oracle"; + auto window = core::window::createWindow(request); + core::render::initializeRenderBackendLoader(); + auto backend = core::render::createRenderBackend(window); + if (!window || !backend || !backend->initialize()) return 1; + backend->makeCurrent(); + core::render::ScopedRenderBackend scope(*backend); + core::TextPrimitive::setDefaultFontFiles(std::string(EUI_TEST_SOURCE_DIR) + "/assets/JingNanJunJunTi-JinNanJunJunTi-Bold-2.ttf", {}); + core::dsl::Runtime runtime; + if (!runtime.initialize(window)) return 1; + std::string source; + for (int i = 0; i < 12; ++i) + source += "# Heading 中文\n\nLong **bold** paragraph, 中文 `inline` [link](https://example.com) ~~delete~~.\n\n" + "> Quoted text wraps across the viewport.\n\n- item with 中文 and English\n\n" + "| Left | Right |\n| :- | -: |\n| a long value wraps into rows | 中文字符 |\n\n" + "```cpp\nint x = 123;\n```\n\n---\n\n"; + int failures = 0; + for (bool dark : {false, true}) for (float dpi : {1.0f, 1.5f}) for (float top : {0.0f, 113.5f, 430.0f}) { + std::vector original; + for (bool sliced : {false, true}) { + runtime.compose("pixels", 480 / dpi, 300 / dpi, [&](auto& ui, const auto&) { + ui.stack("clip").position(8, 8).size(480 / dpi - 16, 300 / dpi - 16).clip().content([&] { + auto md = components::markdown(ui, "md").position(0, -top).width(480 / dpi - 16) + .markdown(source).theme(dark ? components::theme::dark() : components::theme::light()); + if (sliced) md.viewport(top, 300 / dpi - 16); + md.build(); + }).build(); + }); + runtime.update(window, 0, dpi, 1); + runtime.requestFullPaint(); + runtime.render(480, 300, dpi, dark ? core::Color{0, 0, 0, 1} : core::Color{1, 1, 1, 1}); + glFinish(); + std::vector pixels(480 * 300 * 4); + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glReadPixels(0, 0, 480, 300, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data()); + if (!sliced) original = std::move(pixels); + else if (pixels != original) { std::cerr << "viewport pixels changed at " << top << " dpi " << dpi << '\n'; ++failures; } + } + } + runtime.shutdown(); backend.reset(); core::window::destroyWindow(window); +#if defined(EUI_WINDOW_BACKEND_SDL2) + SDL_Quit(); +#else + glfwTerminate(); +#endif + return failures ? 1 : 0; +} +#else +// GPU readback oracle is OpenGL-specific; geometry/ID coverage runs on all backends. +int main() { return 77; } +#endif diff --git a/tests/unit/markdown_viewport.cpp b/tests/unit/markdown_viewport.cpp new file mode 100644 index 00000000..e2e9174d --- /dev/null +++ b/tests/unit/markdown_viewport.cpp @@ -0,0 +1,140 @@ +#include "components/markdown.h" +#include +#include +#include +#include +#include + +namespace { +using Element = core::dsl::Element; +struct Snapshot { + float height = 0, originY = 0; + std::size_t nodes = 0; + std::map leaves; +}; +void visit(const Element& element, Snapshot& out, bool oracle) { + ++out.nodes; + if (oracle && (element.kind == core::dsl::ElementKind::Text || element.kind == core::dsl::ElementKind::Rect)) { + // Element owns unique children; only copy the fields relevant to a leaf oracle. + Element leaf; + leaf.kind = element.kind; + leaf.text = element.text; + leaf.frame = element.frame; + leaf.fontSize = element.fontSize; + leaf.fontFamily = element.fontFamily; + leaf.fontWeight = element.fontWeight; + leaf.lineHeight = element.lineHeight; + out.leaves.emplace(element.id, std::move(leaf)); + } + for (const auto& child : element.children) visit(*child, out, oracle); +} +Snapshot build(const std::string& text, float width, const components::MarkdownStyle& style, bool sliced, float top, + float height, bool oracle = true) { + core::dsl::Ui ui; + ui.begin("viewport-oracle"); + auto md = components::markdown(ui, "md").position(11, 7).margin(3, 5, 7, 9).width(width).style(style).markdown(text); + if (sliced) md.viewport(top, height); + md.build(); + ui.end(); + ui.layout(width + 22, 0); + Snapshot result; + result.height = ui.find("md")->frame.height; + result.originY = ui.find("md")->frame.y; + visit(*ui.find("md"), result, oracle); + return result; +} +bool same(float a, float b) { return std::fabs(a - b) < 0.02f; } +bool compare(const Snapshot& full, const Snapshot& slice, float top, float height) { + if (!same(full.height, slice.height)) { + std::cerr << "full height changed\n"; + return false; + } + for (const auto& pair : full.leaves) { + const auto& a = pair.second; + if (a.frame.y + a.frame.height < top + full.originY || a.frame.y > top + height + full.originY) continue; + const auto found = slice.leaves.find(pair.first); + if (found == slice.leaves.end()) { + std::cerr << "missing visible leaf " << pair.first << '\n'; + return false; + } + const auto& b = found->second; + if (a.text != b.text || a.fontFamily != b.fontFamily || a.fontWeight != b.fontWeight || + !same(a.fontSize, b.fontSize) || !same(a.lineHeight, b.lineHeight) || !same(a.frame.x, b.frame.x) || + !same(a.frame.y, b.frame.y) || !same(a.frame.width, b.frame.width) || + !same(a.frame.height, b.frame.height)) { + std::cerr << "visible geometry changed " << pair.first << '\n'; + return false; + } + } + return true; +} +} // namespace +int main(int argc, char** argv) { + const auto source = std::filesystem::path(__FILE__).parent_path().parent_path().parent_path(); + const char* font = std::getenv("EUI_TEXT_TEST_FONT"); + core::TextPrimitive::setDefaultFontFiles( + font ? font : (source / "assets/JingNanJunJunTi-JinNanJunJunTi-Bold-2.ttf").string(), {}); + const std::string mixed = + "# 标题 Latin 😀\n\n普通 **bold** _emphasis_ `code` [链接](https://example.com) ~~delete~~\n\n" + "> 引用中文和 English,第二行内容继续。\n\n- 列表\n- [x] task\n\n---\n\n```cpp\nint main() {\n return " + "0;\n}\n```\n\n" + "| A | 中文标题 |\n| :- | -: |\n| first | 正文 |\n| long long long value | 另一个值 |\n\n尾段。"; + for (const bool dark : {false, true}) + for (const float width : {190.25f, 520.0f}) { + components::MarkdownStyle style(dark ? components::theme::dark() : components::theme::light()); + style.bodySize = dark ? 21.25f : 16.0f; + style.bodyLineHeight = dark ? 29.0f : 24.0f; + const auto full = build(mixed, width, style, false, 0, 0); + for (float top = 0; top < full.height; top += 57.5f) + if (!compare(full, build(mixed, width, style, true, top, 93.25f), top, 93.25f)) return 1; + if (build(mixed, width, style, true, 0, 0).nodes != 1) return 1; + if (build(mixed, width, style, true, full.height + 100, 60).nodes != 1) return 1; + } + components::MarkdownStyle style; + std::string paragraph; + while (paragraph.size() < 18000) paragraph += "中文 English 😀 e\xcc\x81 "; + const auto full = build(paragraph, 420, style, false, 0, 0); + for (float top : {0.0f, full.height / 2, full.height - 200}) { + const auto slice = build(paragraph, 420, style, true, top, 180); + if (!compare(full, slice, top, 180) || slice.nodes >= full.nodes / 4) return 1; + } + std::string table = "| left | right |\n| :- | -: |\n"; + for (int i = 0; i < 100; ++i) table += "| 中文 " + std::to_string(i) + " | **value** |\n"; + const auto fullTable = build(table, 420, style, false, 0, 0); + const auto tableSlice = build(table, 420, style, true, fullTable.height / 2, 180); + if (!compare(fullTable, tableSlice, fullTable.height / 2, 180) || tableSlice.nodes >= fullTable.nodes / 4) return 1; + // Source edits/replacements, style/width and font configuration must invalidate. + for (int i = 0; i < 12; ++i) { + auto changed = mixed + std::to_string(i) + "\n\n" + paragraph.substr(0, 200 + i * 10); + style.bodySize += 0.25f; + style.codeSize += 0.5f; + style.blockGap += 0.25f; + const auto expected = build(changed, 230 + i, style, false, 0, 0); + if (!compare(expected, build(changed, 230 + i, style, true, 80, 170), 80, 170)) return 1; + } + const auto revision = core::TextPrimitive::fontConfigurationRevision(); + core::TextPrimitive::setDefaultFontFiles({}, {}); + if (core::TextPrimitive::fontConfigurationRevision() == revision) return 1; + const auto switched = build(mixed, 300, style, false, 0, 0); + if (!compare(switched, build(mixed, 300, style, true, 0, 250), 0, 250)) return 1; + if (argc > 1 && std::string(argv[1]) == "--benchmark") { + core::TextPrimitive::setDefaultFontFiles( + font ? font : (source / "assets/JingNanJunJunTi-JinNanJunJunTi-Bold-2.ttf").string(), {}); + for (int scenario = 0; scenario < 3; ++scenario) { + const bool slice = scenario != 0; + std::vector times; + for (int i = 0; i < 65; ++i) { + const auto text = scenario == 2 ? paragraph + std::to_string(i) : paragraph; + const auto start = std::chrono::steady_clock::now(); + const auto result = build(text, 420, style, slice, 500, 180, false); + if (i >= 5) + times.push_back( + std::chrono::duration(std::chrono::steady_clock::now() - start).count()); + if (i == 64) std::cout << "{\"viewport\":" << slice << ",\"append\":" << (scenario == 2) << ",\"nodes\":" << result.nodes << ','; + } + std::sort(times.begin(), times.end()); + std::cout << "\"p50_ms\":" << times[30] << ",\"p95_ms\":" << times[57] << "}\n"; + } + } + std::cout << "Markdown viewport geometry, stable IDs and bounded construction passed\n"; +}