2023-05-31 18:02:00 +00:00
|
|
|
/*
|
2024-01-02 20:37:15 +00:00
|
|
|
* Copyright (c) 2023-2024, MacDue <macdue@dueutil.tech>
|
2023-05-31 18:02:00 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Array.h>
|
2023-06-12 19:14:05 +00:00
|
|
|
#include <AK/Debug.h>
|
2023-05-31 18:02:00 +00:00
|
|
|
#include <AK/IntegralMath.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <LibGfx/AntiAliasingPainter.h>
|
2024-07-05 08:40:29 +00:00
|
|
|
#include <LibGfx/DeprecatedPainter.h>
|
2023-05-31 18:02:00 +00:00
|
|
|
#include <LibGfx/EdgeFlagPathRasterizer.h>
|
|
|
|
|
|
|
|
#if defined(AK_COMPILER_GCC)
|
|
|
|
# pragma GCC optimize("O3")
|
|
|
|
#endif
|
|
|
|
|
2024-01-02 20:42:04 +00:00
|
|
|
// This an implementation of edge-flag scanline AA, as described in:
|
2023-05-31 18:02:00 +00:00
|
|
|
// https://mlab.taik.fi/~kkallio/antialiasing/EdgeFlagAA.pdf
|
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
2023-12-23 23:58:38 +00:00
|
|
|
static Vector<Detail::Edge> prepare_edges(ReadonlySpan<FloatLine> lines, unsigned samples_per_pixel, FloatPoint origin,
|
|
|
|
int top_clip_scanline, int bottom_clip_scanline, int& min_edge_y, int& max_edge_y)
|
2023-05-31 18:02:00 +00:00
|
|
|
{
|
|
|
|
Vector<Detail::Edge> edges;
|
|
|
|
edges.ensure_capacity(lines.size());
|
2023-12-23 23:58:38 +00:00
|
|
|
// The first visible y value.
|
|
|
|
auto top_clip = top_clip_scanline * int(samples_per_pixel);
|
|
|
|
// The last visible y value.
|
2024-08-02 11:15:45 +00:00
|
|
|
auto bottom_clip = (bottom_clip_scanline + 1) * int(samples_per_pixel);
|
2023-12-23 23:58:38 +00:00
|
|
|
min_edge_y = bottom_clip;
|
|
|
|
max_edge_y = top_clip;
|
|
|
|
|
2023-05-31 18:02:00 +00:00
|
|
|
for (auto& line : lines) {
|
2023-06-03 22:40:03 +00:00
|
|
|
auto p0 = line.a() - origin;
|
|
|
|
auto p1 = line.b() - origin;
|
2023-05-31 18:02:00 +00:00
|
|
|
|
|
|
|
p0.scale_by(1, samples_per_pixel);
|
|
|
|
p1.scale_by(1, samples_per_pixel);
|
|
|
|
|
|
|
|
i8 winding = -1;
|
|
|
|
if (p0.y() > p1.y()) {
|
|
|
|
swap(p0, p1);
|
|
|
|
} else {
|
|
|
|
winding = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p0.y() == p1.y())
|
|
|
|
continue;
|
|
|
|
|
2023-06-12 19:14:05 +00:00
|
|
|
auto min_y = static_cast<int>(p0.y());
|
|
|
|
auto max_y = static_cast<int>(p1.y());
|
2023-12-23 23:58:38 +00:00
|
|
|
|
|
|
|
// Clip edges that start below the bottom clip...
|
|
|
|
if (min_y > bottom_clip)
|
|
|
|
continue;
|
|
|
|
// ...and edges that end before the top clip.
|
|
|
|
if (max_y < top_clip)
|
|
|
|
continue;
|
|
|
|
|
2024-01-02 20:33:34 +00:00
|
|
|
auto start_x = p0.x();
|
|
|
|
auto end_x = p1.x();
|
2023-06-12 19:14:05 +00:00
|
|
|
auto dx = end_x - start_x;
|
|
|
|
auto dy = max_y - min_y;
|
2024-01-02 20:33:34 +00:00
|
|
|
|
|
|
|
if (dy == 0)
|
|
|
|
continue;
|
|
|
|
|
2023-06-12 19:14:05 +00:00
|
|
|
auto dxdy = dx / dy;
|
|
|
|
|
2023-12-23 23:58:38 +00:00
|
|
|
// Trim off the non-visible portions of the edge.
|
|
|
|
if (min_y < top_clip) {
|
2024-01-02 20:33:34 +00:00
|
|
|
start_x += dxdy * (top_clip - min_y);
|
2023-12-23 23:58:38 +00:00
|
|
|
min_y = top_clip;
|
|
|
|
}
|
2024-08-02 11:15:45 +00:00
|
|
|
if (max_y > bottom_clip)
|
2023-12-23 23:58:38 +00:00
|
|
|
max_y = bottom_clip;
|
|
|
|
|
|
|
|
min_edge_y = min(min_y, min_edge_y);
|
|
|
|
max_edge_y = max(max_y, max_edge_y);
|
|
|
|
|
2023-05-31 18:02:00 +00:00
|
|
|
edges.unchecked_append(Detail::Edge {
|
2023-06-12 19:14:05 +00:00
|
|
|
start_x,
|
|
|
|
min_y,
|
|
|
|
max_y,
|
2023-05-31 18:02:00 +00:00
|
|
|
dxdy,
|
|
|
|
winding,
|
|
|
|
nullptr });
|
|
|
|
}
|
|
|
|
return edges;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<unsigned SamplesPerPixel>
|
|
|
|
EdgeFlagPathRasterizer<SamplesPerPixel>::EdgeFlagPathRasterizer(IntSize size)
|
|
|
|
: m_size(size.width() + 1, size.height() + 1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<unsigned SamplesPerPixel>
|
2024-08-08 08:22:03 +00:00
|
|
|
void EdgeFlagPathRasterizer<SamplesPerPixel>::fill(DeprecatedPainter& painter, DeprecatedPath const& path, Color color, WindingRule winding_rule, FloatPoint offset)
|
2023-05-31 18:02:00 +00:00
|
|
|
{
|
|
|
|
fill_internal(painter, path, color, winding_rule, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<unsigned SamplesPerPixel>
|
2024-08-08 08:22:03 +00:00
|
|
|
void EdgeFlagPathRasterizer<SamplesPerPixel>::fill(DeprecatedPainter& painter, DeprecatedPath const& path, PaintStyle const& style, float opacity, WindingRule winding_rule, FloatPoint offset)
|
2023-05-31 18:02:00 +00:00
|
|
|
{
|
|
|
|
style.paint(enclosing_int_rect(path.bounding_box()), [&](PaintStyle::SamplerFunction sampler) {
|
2023-06-11 12:40:42 +00:00
|
|
|
if (opacity == 0.0f)
|
|
|
|
return;
|
|
|
|
if (opacity != 1.0f) {
|
|
|
|
return fill_internal(
|
|
|
|
painter, path, [=, sampler = move(sampler)](IntPoint point) {
|
|
|
|
return sampler(point).with_opacity(opacity);
|
|
|
|
},
|
|
|
|
winding_rule, offset);
|
|
|
|
}
|
|
|
|
return fill_internal(painter, path, move(sampler), winding_rule, offset);
|
2023-05-31 18:02:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<unsigned SamplesPerPixel>
|
2024-08-08 08:22:03 +00:00
|
|
|
void EdgeFlagPathRasterizer<SamplesPerPixel>::fill_internal(DeprecatedPainter& painter, DeprecatedPath const& path, auto color_or_function, WindingRule winding_rule, FloatPoint offset)
|
2023-05-31 18:02:00 +00:00
|
|
|
{
|
|
|
|
auto bounding_box = enclosing_int_rect(path.bounding_box().translated(offset));
|
|
|
|
auto dest_rect = bounding_box.translated(painter.translation());
|
|
|
|
auto origin = bounding_box.top_left().to_type<float>() - offset;
|
|
|
|
m_blit_origin = dest_rect.top_left();
|
|
|
|
m_clip = dest_rect.intersected(painter.clip_rect());
|
|
|
|
|
2024-01-07 16:28:26 +00:00
|
|
|
// Only allocate enough to plot the parts of the scanline that could be visible.
|
|
|
|
// Note: This can't clip the LHS.
|
|
|
|
auto scanline_length = min(m_size.width(), m_clip.right() - m_blit_origin.x());
|
|
|
|
if (scanline_length <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_scanline.resize(scanline_length);
|
|
|
|
|
2023-05-31 18:02:00 +00:00
|
|
|
if (m_clip.is_empty())
|
|
|
|
return;
|
|
|
|
|
2024-03-17 20:23:17 +00:00
|
|
|
auto lines = path.split_lines();
|
2023-05-31 18:02:00 +00:00
|
|
|
if (lines.is_empty())
|
|
|
|
return;
|
|
|
|
|
2023-12-23 23:58:38 +00:00
|
|
|
int min_edge_y = 0;
|
|
|
|
int max_edge_y = 0;
|
|
|
|
auto top_clip_scanline = m_clip.top() - m_blit_origin.y();
|
|
|
|
auto bottom_clip_scanline = m_clip.bottom() - m_blit_origin.y() - 1;
|
|
|
|
auto edges = prepare_edges(lines, SamplesPerPixel, origin, top_clip_scanline, bottom_clip_scanline, min_edge_y, max_edge_y);
|
|
|
|
if (edges.is_empty())
|
|
|
|
return;
|
2023-05-31 18:02:00 +00:00
|
|
|
|
2023-12-23 23:58:38 +00:00
|
|
|
int min_scanline = min_edge_y / SamplesPerPixel;
|
|
|
|
int max_scanline = max_edge_y / SamplesPerPixel;
|
|
|
|
m_edge_table.set_scanline_range(min_scanline, max_scanline);
|
2023-05-31 18:02:00 +00:00
|
|
|
for (auto& edge : edges) {
|
|
|
|
// Create a linked-list of edges starting on this scanline:
|
2023-12-23 23:58:38 +00:00
|
|
|
int start_scanline = edge.min_y / SamplesPerPixel;
|
2023-05-31 18:02:00 +00:00
|
|
|
edge.next_edge = m_edge_table[start_scanline];
|
|
|
|
m_edge_table[start_scanline] = &edge;
|
|
|
|
}
|
|
|
|
|
2023-08-06 13:15:04 +00:00
|
|
|
auto empty_edge_extent = [&] {
|
|
|
|
return EdgeExtent { m_size.width() - 1, 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
auto for_each_sample = [&](Detail::Edge& edge, int start_subpixel_y, int end_subpixel_y, EdgeExtent& edge_extent, auto callback) {
|
2023-06-12 19:14:05 +00:00
|
|
|
for (int y = start_subpixel_y; y < end_subpixel_y; y++) {
|
2024-01-02 20:37:15 +00:00
|
|
|
auto xi = static_cast<int>(edge.x + SubpixelSample::nrooks_subpixel_offsets[y]);
|
2024-01-07 16:28:26 +00:00
|
|
|
if (xi >= 0 && size_t(xi) < m_scanline.size()) [[likely]] {
|
|
|
|
SampleType sample = 1 << y;
|
|
|
|
callback(xi, y, sample);
|
|
|
|
} else if (xi < 0) {
|
|
|
|
if (edge.dxdy <= 0)
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
xi = m_scanline.size() - 1;
|
2023-06-12 19:14:05 +00:00
|
|
|
}
|
|
|
|
edge.x += edge.dxdy;
|
2023-08-06 13:15:04 +00:00
|
|
|
edge_extent.min_x = min(edge_extent.min_x, xi);
|
|
|
|
edge_extent.max_x = max(edge_extent.max_x, xi);
|
2023-06-12 19:14:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Detail::Edge* active_edges = nullptr;
|
|
|
|
|
2024-06-05 08:21:28 +00:00
|
|
|
if (winding_rule == WindingRule::EvenOdd) {
|
2023-08-06 13:15:04 +00:00
|
|
|
auto plot_edge = [&](Detail::Edge& edge, int start_subpixel_y, int end_subpixel_y, EdgeExtent& edge_extent) {
|
|
|
|
for_each_sample(edge, start_subpixel_y, end_subpixel_y, edge_extent, [&](int xi, int, SampleType sample) {
|
2023-05-31 18:02:00 +00:00
|
|
|
m_scanline[xi] ^= sample;
|
2023-06-12 19:14:05 +00:00
|
|
|
});
|
2023-05-31 18:02:00 +00:00
|
|
|
};
|
|
|
|
for (int scanline = min_scanline; scanline <= max_scanline; scanline++) {
|
2023-08-06 13:15:04 +00:00
|
|
|
auto edge_extent = empty_edge_extent();
|
|
|
|
active_edges = plot_edges_for_scanline(scanline, plot_edge, edge_extent, active_edges);
|
2024-06-05 08:21:28 +00:00
|
|
|
write_scanline<WindingRule::EvenOdd>(painter, scanline, edge_extent, color_or_function);
|
2023-05-31 18:02:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-06-05 08:21:28 +00:00
|
|
|
VERIFY(winding_rule == WindingRule::Nonzero);
|
2023-05-31 18:02:00 +00:00
|
|
|
// Only allocate the winding buffer if needed.
|
|
|
|
// NOTE: non-zero fills are a fair bit less efficient. So if you can do an even-odd fill do that :^)
|
|
|
|
if (m_windings.is_empty())
|
2024-01-07 16:28:26 +00:00
|
|
|
m_windings.resize(m_scanline.size());
|
2023-05-31 18:02:00 +00:00
|
|
|
|
2023-08-06 13:15:04 +00:00
|
|
|
auto plot_edge = [&](Detail::Edge& edge, int start_subpixel_y, int end_subpixel_y, EdgeExtent& edge_extent) {
|
|
|
|
for_each_sample(edge, start_subpixel_y, end_subpixel_y, edge_extent, [&](int xi, int y, SampleType sample) {
|
2023-05-31 18:02:00 +00:00
|
|
|
m_scanline[xi] |= sample;
|
|
|
|
m_windings[xi].counts[y] += edge.winding;
|
2023-06-12 19:14:05 +00:00
|
|
|
});
|
2023-05-31 18:02:00 +00:00
|
|
|
};
|
|
|
|
for (int scanline = min_scanline; scanline <= max_scanline; scanline++) {
|
2023-08-06 13:15:04 +00:00
|
|
|
auto edge_extent = empty_edge_extent();
|
|
|
|
active_edges = plot_edges_for_scanline(scanline, plot_edge, edge_extent, active_edges);
|
2024-06-05 08:21:28 +00:00
|
|
|
write_scanline<WindingRule::Nonzero>(painter, scanline, edge_extent, color_or_function);
|
2023-05-31 18:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-02 20:37:15 +00:00
|
|
|
ALWAYS_INLINE static auto switch_on_color_or_function(auto& color_or_function, auto color_case, auto function_case)
|
2023-05-31 18:02:00 +00:00
|
|
|
{
|
|
|
|
using ColorOrFunction = decltype(color_or_function);
|
|
|
|
constexpr bool has_constant_color = IsSame<RemoveCVReference<ColorOrFunction>, Color>;
|
2024-01-02 20:37:15 +00:00
|
|
|
if constexpr (has_constant_color) {
|
|
|
|
return color_case(color_or_function);
|
|
|
|
} else {
|
|
|
|
return function_case(color_or_function);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<unsigned SamplesPerPixel>
|
|
|
|
Color EdgeFlagPathRasterizer<SamplesPerPixel>::scanline_color(int scanline, int offset, u8 alpha, auto& color_or_function)
|
|
|
|
{
|
|
|
|
auto color = switch_on_color_or_function(
|
|
|
|
color_or_function, [](Color color) { return color; },
|
|
|
|
[&](auto& function) {
|
|
|
|
return function({ offset, scanline });
|
|
|
|
});
|
2024-01-06 13:45:45 +00:00
|
|
|
if (color.alpha() == 255)
|
2024-08-02 11:15:45 +00:00
|
|
|
return color.with_alpha(alpha, AlphaType::Premultiplied);
|
|
|
|
return color.with_alpha(color.alpha() * alpha / 255, AlphaType::Premultiplied);
|
2023-05-31 18:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<unsigned SamplesPerPixel>
|
2024-01-06 13:52:34 +00:00
|
|
|
__attribute__((hot)) Detail::Edge* EdgeFlagPathRasterizer<SamplesPerPixel>::plot_edges_for_scanline(int scanline, auto plot_edge, EdgeExtent& edge_extent, Detail::Edge* active_edges)
|
2023-05-31 18:02:00 +00:00
|
|
|
{
|
|
|
|
auto y_subpixel = [](int y) {
|
|
|
|
return y & (SamplesPerPixel - 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto* current_edge = active_edges;
|
|
|
|
Detail::Edge* prev_edge = nullptr;
|
|
|
|
|
|
|
|
// First iterate over the edge in the active edge table, these are edges added on earlier scanlines,
|
|
|
|
// that have not yet reached their end scanline.
|
|
|
|
while (current_edge) {
|
|
|
|
int end_scanline = current_edge->max_y / SamplesPerPixel;
|
|
|
|
if (scanline == end_scanline) {
|
|
|
|
// This edge ends this scanline.
|
2023-08-06 13:15:04 +00:00
|
|
|
plot_edge(*current_edge, 0, y_subpixel(current_edge->max_y), edge_extent);
|
2023-05-31 18:02:00 +00:00
|
|
|
// Remove this edge from the AET
|
|
|
|
current_edge = current_edge->next_edge;
|
|
|
|
if (prev_edge)
|
|
|
|
prev_edge->next_edge = current_edge;
|
|
|
|
else
|
|
|
|
active_edges = current_edge;
|
|
|
|
} else {
|
2024-01-02 20:44:50 +00:00
|
|
|
// This edge sticks around for a few more scanlines.
|
2023-08-06 13:15:04 +00:00
|
|
|
plot_edge(*current_edge, 0, SamplesPerPixel, edge_extent);
|
2023-05-31 18:02:00 +00:00
|
|
|
prev_edge = current_edge;
|
|
|
|
current_edge = current_edge->next_edge;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, iterate over new edges for this line. If active_edges was null this also becomes the new
|
|
|
|
// AET. Edges new will be appended here.
|
|
|
|
current_edge = m_edge_table[scanline];
|
|
|
|
while (current_edge) {
|
|
|
|
int end_scanline = current_edge->max_y / SamplesPerPixel;
|
|
|
|
if (scanline == end_scanline) {
|
|
|
|
// This edge will end this scanlines (no need to add to AET).
|
2023-08-06 13:15:04 +00:00
|
|
|
plot_edge(*current_edge, y_subpixel(current_edge->min_y), y_subpixel(current_edge->max_y), edge_extent);
|
2023-05-31 18:02:00 +00:00
|
|
|
} else {
|
|
|
|
// This edge will live on for a few more scanlines.
|
2023-08-06 13:15:04 +00:00
|
|
|
plot_edge(*current_edge, y_subpixel(current_edge->min_y), SamplesPerPixel, edge_extent);
|
2023-05-31 18:02:00 +00:00
|
|
|
// Add this edge to the AET
|
|
|
|
if (prev_edge)
|
|
|
|
prev_edge->next_edge = current_edge;
|
|
|
|
else
|
|
|
|
active_edges = current_edge;
|
|
|
|
prev_edge = current_edge;
|
|
|
|
}
|
|
|
|
current_edge = current_edge->next_edge;
|
|
|
|
}
|
|
|
|
|
2023-06-03 22:47:31 +00:00
|
|
|
if (prev_edge)
|
|
|
|
prev_edge->next_edge = nullptr;
|
|
|
|
|
2023-05-31 18:02:00 +00:00
|
|
|
m_edge_table[scanline] = nullptr;
|
|
|
|
return active_edges;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<unsigned SamplesPerPixel>
|
2024-01-06 13:45:45 +00:00
|
|
|
auto EdgeFlagPathRasterizer<SamplesPerPixel>::accumulate_even_odd_scanline(EdgeExtent edge_extent, auto init, auto sample_callback)
|
2023-05-31 18:02:00 +00:00
|
|
|
{
|
2024-01-06 13:45:45 +00:00
|
|
|
SampleType sample = init;
|
2024-05-24 09:31:54 +00:00
|
|
|
VERIFY(edge_extent.min_x >= 0);
|
2024-05-27 10:36:43 +00:00
|
|
|
VERIFY(edge_extent.max_x < static_cast<int>(m_scanline.size()));
|
2024-08-02 11:15:45 +00:00
|
|
|
for (int x = edge_extent.min_x; x <= edge_extent.max_x; x++) {
|
2024-05-24 09:31:54 +00:00
|
|
|
sample ^= m_scanline.data()[x];
|
2024-01-02 20:37:15 +00:00
|
|
|
sample_callback(x, sample);
|
2024-05-24 09:31:54 +00:00
|
|
|
m_scanline.data()[x] = 0;
|
2023-05-31 18:02:00 +00:00
|
|
|
}
|
2024-01-06 13:45:45 +00:00
|
|
|
return sample;
|
2023-05-31 18:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<unsigned SamplesPerPixel>
|
2024-01-06 13:45:45 +00:00
|
|
|
auto EdgeFlagPathRasterizer<SamplesPerPixel>::accumulate_non_zero_scanline(EdgeExtent edge_extent, auto init, auto sample_callback)
|
2023-05-31 18:02:00 +00:00
|
|
|
{
|
2024-01-06 13:45:45 +00:00
|
|
|
NonZeroAcc acc = init;
|
2024-05-27 10:40:02 +00:00
|
|
|
VERIFY(edge_extent.min_x >= 0);
|
|
|
|
VERIFY(edge_extent.max_x < static_cast<int>(m_scanline.size()));
|
2024-08-02 11:15:45 +00:00
|
|
|
for (int x = edge_extent.min_x; x <= edge_extent.max_x; x++) {
|
2024-05-27 10:40:02 +00:00
|
|
|
if (auto edges = m_scanline.data()[x]) {
|
2023-05-31 18:02:00 +00:00
|
|
|
// We only need to process the windings when we hit some edges.
|
|
|
|
for (auto y_sub = 0u; y_sub < SamplesPerPixel; y_sub++) {
|
|
|
|
auto subpixel_bit = 1 << y_sub;
|
|
|
|
if (edges & subpixel_bit) {
|
2024-05-27 10:40:02 +00:00
|
|
|
auto winding = m_windings.data()[x].counts[y_sub];
|
2024-01-06 13:45:45 +00:00
|
|
|
auto previous_winding_count = acc.winding.counts[y_sub];
|
|
|
|
acc.winding.counts[y_sub] += winding;
|
2024-01-02 20:45:06 +00:00
|
|
|
// Toggle fill on change to/from zero.
|
2024-01-06 13:45:45 +00:00
|
|
|
if (bool(previous_winding_count) ^ bool(acc.winding.counts[y_sub]))
|
|
|
|
acc.sample ^= subpixel_bit;
|
2023-05-31 18:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-06 13:45:45 +00:00
|
|
|
sample_callback(x, acc.sample);
|
2024-05-27 10:40:02 +00:00
|
|
|
m_scanline.data()[x] = 0;
|
|
|
|
m_windings.data()[x] = {};
|
2023-05-31 18:02:00 +00:00
|
|
|
}
|
2024-01-06 13:45:45 +00:00
|
|
|
return acc;
|
2023-05-31 18:02:00 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 20:37:15 +00:00
|
|
|
template<unsigned SamplesPerPixel>
|
2024-06-05 08:21:28 +00:00
|
|
|
template<WindingRule WindingRule, typename Callback>
|
2024-01-06 13:45:45 +00:00
|
|
|
auto EdgeFlagPathRasterizer<SamplesPerPixel>::accumulate_scanline(EdgeExtent edge_extent, auto init, Callback callback)
|
2024-01-02 20:37:15 +00:00
|
|
|
{
|
2024-06-05 08:21:28 +00:00
|
|
|
if constexpr (WindingRule == WindingRule::EvenOdd)
|
2024-01-06 13:45:45 +00:00
|
|
|
return accumulate_even_odd_scanline(edge_extent, init, callback);
|
2024-01-02 20:37:15 +00:00
|
|
|
else
|
2024-01-06 13:45:45 +00:00
|
|
|
return accumulate_non_zero_scanline(edge_extent, init, callback);
|
2024-01-02 20:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<unsigned SamplesPerPixel>
|
2024-01-06 13:45:45 +00:00
|
|
|
void EdgeFlagPathRasterizer<SamplesPerPixel>::write_pixel(BitmapFormat format, ARGB32* scanline_ptr, int scanline, int offset, SampleType sample, auto& color_or_function)
|
2024-01-02 20:37:15 +00:00
|
|
|
{
|
|
|
|
if (!sample)
|
|
|
|
return;
|
2024-01-06 13:45:45 +00:00
|
|
|
auto dest_x = offset + m_blit_origin.x();
|
2024-01-02 20:37:15 +00:00
|
|
|
auto coverage = SubpixelSample::compute_coverage(sample);
|
|
|
|
auto paint_color = scanline_color(scanline, offset, coverage_to_alpha(coverage), color_or_function);
|
2024-01-06 13:45:45 +00:00
|
|
|
scanline_ptr[dest_x] = color_for_format(format, scanline_ptr[dest_x]).blend(paint_color).value();
|
2024-01-02 20:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<unsigned SamplesPerPixel>
|
2024-01-06 13:45:45 +00:00
|
|
|
void EdgeFlagPathRasterizer<SamplesPerPixel>::fast_fill_solid_color_span(ARGB32* scanline_ptr, int start, int end, Color color)
|
2024-01-02 20:37:15 +00:00
|
|
|
{
|
2024-01-06 13:45:45 +00:00
|
|
|
auto start_x = start + m_blit_origin.x();
|
|
|
|
auto end_x = end + m_blit_origin.x();
|
|
|
|
fast_u32_fill(scanline_ptr + start_x, color.value(), end_x - start_x + 1);
|
2024-01-02 20:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<unsigned SamplesPerPixel>
|
2024-06-05 08:21:28 +00:00
|
|
|
template<WindingRule WindingRule>
|
2024-07-05 08:40:29 +00:00
|
|
|
FLATTEN __attribute__((hot)) void EdgeFlagPathRasterizer<SamplesPerPixel>::write_scanline(DeprecatedPainter& painter, int scanline, EdgeExtent edge_extent, auto& color_or_function)
|
2024-01-02 20:37:15 +00:00
|
|
|
{
|
2024-01-06 13:45:45 +00:00
|
|
|
// Handle scanline clipping.
|
|
|
|
auto left_clip = m_clip.left() - m_blit_origin.x();
|
2024-01-07 16:28:26 +00:00
|
|
|
EdgeExtent clipped_extent { max(left_clip, edge_extent.min_x), edge_extent.max_x };
|
2024-01-06 13:45:45 +00:00
|
|
|
if (clipped_extent.min_x > clipped_extent.max_x) {
|
2024-01-07 16:28:26 +00:00
|
|
|
// Fully clipped. Unfortunately we still need to zero the scanline data.
|
|
|
|
edge_extent.memset_extent(m_scanline.data(), 0);
|
2024-06-05 08:21:28 +00:00
|
|
|
if constexpr (WindingRule == WindingRule::Nonzero)
|
2024-01-07 16:28:26 +00:00
|
|
|
edge_extent.memset_extent(m_windings.data(), 0);
|
2024-01-06 13:45:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate non-visible section (without plotting pixels).
|
|
|
|
auto acc = accumulate_scanline<WindingRule>(EdgeExtent { edge_extent.min_x, left_clip - 1 }, initial_acc<WindingRule>(), [](int, SampleType) {
|
|
|
|
// Do nothing!
|
|
|
|
});
|
|
|
|
|
|
|
|
// Get pointer to current scanline pixels.
|
2024-06-05 08:43:00 +00:00
|
|
|
auto dest_format = painter.target().format();
|
|
|
|
auto dest_ptr = painter.target().scanline(scanline + m_blit_origin.y());
|
2024-01-06 13:45:45 +00:00
|
|
|
|
2024-01-02 20:37:15 +00:00
|
|
|
// Simple case: Handle each pixel individually.
|
|
|
|
// Used for PaintStyle fills and semi-transparent colors.
|
|
|
|
auto write_scanline_pixelwise = [&](auto& color_or_function) {
|
2024-01-06 13:45:45 +00:00
|
|
|
accumulate_scanline<WindingRule>(clipped_extent, acc, [&](int x, SampleType sample) {
|
|
|
|
write_pixel(dest_format, dest_ptr, scanline, x, sample, color_or_function);
|
2024-01-02 20:37:15 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
// Fast fill case: Track spans of solid color and set the entire span via a fast_u32_fill().
|
|
|
|
// Used for opaque colors (i.e. alpha == 255).
|
|
|
|
auto write_scanline_with_fast_fills = [&](Color color) {
|
|
|
|
if (color.alpha() != 255)
|
|
|
|
return write_scanline_pixelwise(color);
|
2024-08-05 11:06:37 +00:00
|
|
|
constexpr SampleType full_coverage = NumericLimits<SampleType>::max();
|
|
|
|
int full_coverage_count = 0;
|
2024-01-06 13:45:45 +00:00
|
|
|
accumulate_scanline<WindingRule>(clipped_extent, acc, [&](int x, SampleType sample) {
|
2024-08-05 11:06:37 +00:00
|
|
|
if (sample == full_coverage) {
|
|
|
|
full_coverage_count++;
|
2024-01-02 20:37:15 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2024-01-06 13:45:45 +00:00
|
|
|
write_pixel(dest_format, dest_ptr, scanline, x, sample, color);
|
2024-01-02 20:37:15 +00:00
|
|
|
}
|
2024-08-05 11:06:37 +00:00
|
|
|
if (full_coverage_count > 0) {
|
|
|
|
fast_fill_solid_color_span(dest_ptr, x - full_coverage_count, x - 1, color);
|
|
|
|
full_coverage_count = 0;
|
2024-01-02 20:37:15 +00:00
|
|
|
}
|
|
|
|
});
|
2024-08-05 11:06:37 +00:00
|
|
|
if (full_coverage_count > 0)
|
|
|
|
fast_fill_solid_color_span(dest_ptr, clipped_extent.max_x - full_coverage_count + 1, clipped_extent.max_x, color);
|
2024-01-02 20:37:15 +00:00
|
|
|
};
|
|
|
|
switch_on_color_or_function(
|
|
|
|
color_or_function, write_scanline_with_fast_fills, write_scanline_pixelwise);
|
|
|
|
}
|
|
|
|
|
2024-08-08 08:22:03 +00:00
|
|
|
static IntSize path_bounds(Gfx::DeprecatedPath const& path)
|
2023-05-31 18:02:00 +00:00
|
|
|
{
|
|
|
|
return enclosing_int_rect(path.bounding_box()).size();
|
|
|
|
}
|
|
|
|
|
2024-07-05 08:40:29 +00:00
|
|
|
// Note: The AntiAliasingPainter and DeprecatedPainter now perform the same antialiasing,
|
2023-05-31 18:02:00 +00:00
|
|
|
// since it would be harder to turn it off for the standard painter.
|
2024-07-05 08:40:29 +00:00
|
|
|
// The samples are reduced to 8 for Gfx::DeprecatedPainter though as a "speedy" option.
|
2023-05-31 18:02:00 +00:00
|
|
|
|
2024-08-08 08:22:03 +00:00
|
|
|
void DeprecatedPainter::fill_path(DeprecatedPath const& path, Color color, WindingRule winding_rule)
|
2023-05-31 18:02:00 +00:00
|
|
|
{
|
|
|
|
EdgeFlagPathRasterizer<8> rasterizer(path_bounds(path));
|
|
|
|
rasterizer.fill(*this, path, color, winding_rule);
|
|
|
|
}
|
|
|
|
|
2024-08-08 08:22:03 +00:00
|
|
|
void DeprecatedPainter::fill_path(DeprecatedPath const& path, PaintStyle const& paint_style, float opacity, WindingRule winding_rule)
|
2023-05-31 18:02:00 +00:00
|
|
|
{
|
|
|
|
EdgeFlagPathRasterizer<8> rasterizer(path_bounds(path));
|
2023-06-11 12:40:42 +00:00
|
|
|
rasterizer.fill(*this, path, paint_style, opacity, winding_rule);
|
2023-05-31 18:02:00 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 08:22:03 +00:00
|
|
|
void AntiAliasingPainter::fill_path(DeprecatedPath const& path, Color color, WindingRule winding_rule)
|
2023-05-31 18:02:00 +00:00
|
|
|
{
|
|
|
|
EdgeFlagPathRasterizer<32> rasterizer(path_bounds(path));
|
2024-08-09 19:00:01 +00:00
|
|
|
rasterizer.fill(m_underlying_painter, path, color, winding_rule, {});
|
2023-05-31 18:02:00 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 08:22:03 +00:00
|
|
|
void AntiAliasingPainter::fill_path(DeprecatedPath const& path, PaintStyle const& paint_style, float opacity, WindingRule winding_rule)
|
2023-05-31 18:02:00 +00:00
|
|
|
{
|
|
|
|
EdgeFlagPathRasterizer<32> rasterizer(path_bounds(path));
|
2024-08-09 19:00:01 +00:00
|
|
|
rasterizer.fill(m_underlying_painter, path, paint_style, opacity, winding_rule, {});
|
2023-05-31 18:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template class EdgeFlagPathRasterizer<8>;
|
|
|
|
template class EdgeFlagPathRasterizer<16>;
|
|
|
|
template class EdgeFlagPathRasterizer<32>;
|
|
|
|
|
|
|
|
}
|