Skip to content

Commit e41c85e

Browse files
kalenikaliaksandrgmta
authored andcommitted
LibWeb: Replace DrawTriangleWave with StrokePathUsingColor
There's no need to have separate display list item for drawing triangle wave when we could simply use StrokePathUsingColor. By switching to StrokePathUsingColor we could also reduce painting because it supports filtering out by bounding box.
1 parent 3dd8b32 commit e41c85e

File tree

9 files changed

+72
-117
lines changed

9 files changed

+72
-117
lines changed

Libraries/LibWeb/Painting/DisplayList.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ void DisplayListPlayer::execute_impl(DisplayList& display_list, ScrollStateSnaps
223223
else HANDLE_COMMAND(DrawLine, draw_line)
224224
else HANDLE_COMMAND(ApplyBackdropFilter, apply_backdrop_filter)
225225
else HANDLE_COMMAND(DrawRect, draw_rect)
226-
else HANDLE_COMMAND(DrawTriangleWave, draw_triangle_wave)
227226
else HANDLE_COMMAND(AddRoundedRectClip, add_rounded_rect_clip)
228227
else HANDLE_COMMAND(AddMask, add_mask)
229228
else HANDLE_COMMAND(PaintScrollBar, paint_scrollbar)

Libraries/LibWeb/Painting/DisplayList.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ class DisplayListPlayer {
6464
virtual void draw_line(DrawLine const&) = 0;
6565
virtual void apply_backdrop_filter(ApplyBackdropFilter const&) = 0;
6666
virtual void draw_rect(DrawRect const&) = 0;
67-
virtual void draw_triangle_wave(DrawTriangleWave const&) = 0;
6867
virtual void add_rounded_rect_clip(AddRoundedRectClip const&) = 0;
6968
virtual void add_mask(AddMask const&) = 0;
7069
virtual void paint_nested_display_list(PaintNestedDisplayList const&) = 0;

Libraries/LibWeb/Painting/DisplayListCommand.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,6 @@ void DrawRect::dump(StringBuilder& builder) const
182182
builder.appendff("DrawRect rect={} color={} rough={}", rect, color, rough);
183183
}
184184

185-
void DrawTriangleWave::dump(StringBuilder& builder) const
186-
{
187-
builder.appendff("DrawTriangleWave p1={} p2={} color={} amplitude={} thickness={}", p1, p2, color, amplitude, thickness);
188-
}
189-
190185
void AddRoundedRectClip::dump(StringBuilder& builder) const
191186
{
192187
builder.appendff("AddRoundedRectClip rect={}", border_rect);

Libraries/LibWeb/Painting/DisplayListCommand.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -382,21 +382,6 @@ struct PaintConicGradient {
382382
void dump(StringBuilder&) const;
383383
};
384384

385-
struct DrawTriangleWave {
386-
Gfx::IntPoint p1;
387-
Gfx::IntPoint p2;
388-
Color color;
389-
int amplitude;
390-
int thickness;
391-
392-
void translate_by(Gfx::IntPoint const& offset)
393-
{
394-
p1.translate_by(offset);
395-
p2.translate_by(offset);
396-
}
397-
void dump(StringBuilder&) const;
398-
};
399-
400385
struct AddRoundedRectClip {
401386
CornerRadii corner_radii;
402387
Gfx::IntRect border_rect;
@@ -530,7 +515,6 @@ using DisplayListCommand = Variant<
530515
DrawLine,
531516
ApplyBackdropFilter,
532517
DrawRect,
533-
DrawTriangleWave,
534518
AddRoundedRectClip,
535519
AddMask,
536520
PaintNestedDisplayList,

Libraries/LibWeb/Painting/DisplayListPlayerSkia.cpp

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -872,75 +872,6 @@ void DisplayListPlayerSkia::paint_conic_gradient(PaintConicGradient const& comma
872872
surface().canvas().drawRect(to_skia_rect(rect), paint);
873873
}
874874

875-
void DisplayListPlayerSkia::draw_triangle_wave(DrawTriangleWave const& command)
876-
{
877-
// FIXME: Support more than horizontal waves
878-
if (command.p1.y() != command.p2.y()) {
879-
dbgln("FIXME: Support more than horizontal waves");
880-
return;
881-
}
882-
883-
auto& canvas = surface().canvas();
884-
auto from = to_skia_point(command.p1);
885-
auto to = to_skia_point(command.p2);
886-
887-
SkPaint paint;
888-
paint.setAntiAlias(true);
889-
paint.setStyle(SkPaint::kStroke_Style);
890-
paint.setStrokeWidth(command.thickness);
891-
paint.setStrokeJoin(SkPaint::kRound_Join);
892-
paint.setStrokeCap(SkPaint::kRound_Cap);
893-
paint.setColor(to_skia_color(command.color));
894-
895-
SkPath path;
896-
path.moveTo(from);
897-
898-
float const wavelength = command.amplitude * 2.0f;
899-
float const half_wavelength = command.amplitude;
900-
float const quarter_wavelength = command.amplitude / 2.0f;
901-
902-
auto position = from;
903-
auto remaining = abs(to.x() - position.x());
904-
while (remaining > wavelength) {
905-
// Draw a whole wave
906-
path.lineTo(position.x() + quarter_wavelength, position.y() - quarter_wavelength);
907-
path.lineTo(position.x() + quarter_wavelength + half_wavelength, position.y() + quarter_wavelength);
908-
path.lineTo(position.x() + wavelength, position.y());
909-
position.offset(wavelength, 0);
910-
remaining = abs(to.x() - position.x());
911-
}
912-
913-
// Up
914-
if (remaining > quarter_wavelength) {
915-
path.lineTo(position.x() + quarter_wavelength, position.y() - quarter_wavelength);
916-
position.offset(quarter_wavelength, 0);
917-
remaining = abs(to.x() - position.x());
918-
} else if (remaining >= 1) {
919-
auto fraction = remaining / quarter_wavelength;
920-
path.lineTo(position.x() + (fraction * quarter_wavelength), position.y() - (fraction * quarter_wavelength));
921-
remaining = 0;
922-
}
923-
924-
// Down
925-
if (remaining > half_wavelength) {
926-
path.lineTo(position.x() + half_wavelength, position.y() + quarter_wavelength);
927-
position.offset(half_wavelength, 0);
928-
remaining = abs(to.x() - position.x());
929-
} else if (remaining >= 1) {
930-
auto fraction = remaining / half_wavelength;
931-
path.lineTo(position.x() + (fraction * half_wavelength), position.y() - quarter_wavelength + (fraction * half_wavelength));
932-
remaining = 0;
933-
}
934-
935-
// Back to middle
936-
if (remaining >= 1) {
937-
auto fraction = remaining / quarter_wavelength;
938-
path.lineTo(position.x() + (fraction * quarter_wavelength), position.y() + ((1 - fraction) * quarter_wavelength));
939-
}
940-
941-
canvas.drawPath(path, paint);
942-
}
943-
944875
void DisplayListPlayerSkia::add_rounded_rect_clip(AddRoundedRectClip const& command)
945876
{
946877
auto rounded_rect = to_skia_rrect(command.border_rect, command.corner_radii);

Libraries/LibWeb/Painting/DisplayListPlayerSkia.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class DisplayListPlayerSkia final : public DisplayListPlayer {
5151
void draw_rect(DrawRect const&) override;
5252
void paint_radial_gradient(PaintRadialGradient const&) override;
5353
void paint_conic_gradient(PaintConicGradient const&) override;
54-
void draw_triangle_wave(DrawTriangleWave const&) override;
5554
void add_rounded_rect_clip(AddRoundedRectClip const&) override;
5655
void add_mask(AddMask const&) override;
5756
void paint_scrollbar(PaintScrollBar const&) override;

Libraries/LibWeb/Painting/DisplayListRecorder.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -433,21 +433,6 @@ void DisplayListRecorder::fill_rect_with_rounded_corners(Gfx::IntRect const& a_r
433433
{ bottom_left_radius, bottom_left_radius });
434434
}
435435

436-
void DisplayListRecorder::draw_triangle_wave(Gfx::IntPoint a_p1, Gfx::IntPoint a_p2, Color color, int amplitude, int thickness = 1)
437-
{
438-
// Skia treats zero thickness as a special case and will draw a hairline, while we want to draw nothing.
439-
if (!thickness)
440-
return;
441-
if (color.alpha() == 0)
442-
return;
443-
APPEND(DrawTriangleWave {
444-
.p1 = a_p1,
445-
.p2 = a_p2,
446-
.color = color,
447-
.amplitude = amplitude,
448-
.thickness = thickness });
449-
}
450-
451436
void DisplayListRecorder::paint_scrollbar(int scroll_frame_id, Gfx::IntRect gutter_rect, Gfx::IntRect thumb_rect, CSSPixelFraction scroll_size, Color thumb_color, Color track_color, bool vertical)
452437
{
453438
APPEND(PaintScrollBar {

Libraries/LibWeb/Painting/DisplayListRecorder.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,6 @@ class DisplayListRecorder {
144144
void fill_rect_with_rounded_corners(Gfx::IntRect const& a_rect, Color color, int radius);
145145
void fill_rect_with_rounded_corners(Gfx::IntRect const& a_rect, Color color, int top_left_radius, int top_right_radius, int bottom_right_radius, int bottom_left_radius);
146146

147-
void draw_triangle_wave(Gfx::IntPoint a_p1, Gfx::IntPoint a_p2, Color color, int amplitude, int thickness);
148-
149147
void paint_scrollbar(int scroll_frame_id, Gfx::IntRect gutter_rect, Gfx::IntRect thumb_rect, CSSPixelFraction scroll_size, Color thumb_color, Color track_color, bool vertical);
150148

151149
void apply_opacity(float opacity);

Libraries/LibWeb/Painting/PaintableBox.cpp

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,65 @@ void paint_cursor_if_needed(DisplayListRecordingContext& context, TextPaintable
734734
context.display_list_recorder().fill_rect(cursor_device_rect, caret_color);
735735
}
736736

737+
static Gfx::Path build_triangle_wave_path(Gfx::IntPoint from, Gfx::IntPoint to, float amplitude)
738+
{
739+
Gfx::Path path;
740+
if (from.y() != to.y()) {
741+
dbgln("FIXME: Support more than horizontal waves");
742+
return path;
743+
}
744+
745+
path.move_to(from.to_type<float>());
746+
747+
float const wavelength = amplitude * 2.0f;
748+
float const half_wavelength = amplitude;
749+
float const quarter_wavelength = amplitude / 2.0f;
750+
751+
auto position = from.to_type<float>();
752+
auto remaining = abs(to.x() - position.x());
753+
while (remaining > wavelength) {
754+
// Draw a whole wave
755+
path.line_to({ position.x() + quarter_wavelength, position.y() - quarter_wavelength });
756+
path.line_to({ position.x() + quarter_wavelength + half_wavelength, position.y() + quarter_wavelength });
757+
path.line_to({ position.x() + wavelength, (float)position.y() });
758+
position.translate_by({ wavelength, 0 });
759+
remaining = abs(to.x() - position.x());
760+
}
761+
762+
// Up
763+
if (remaining > quarter_wavelength) {
764+
path.line_to({ position.x() + quarter_wavelength, position.y() - quarter_wavelength });
765+
position.translate_by({ quarter_wavelength, 0 });
766+
remaining = abs(to.x() - position.x());
767+
} else if (remaining >= 1) {
768+
auto fraction = remaining / quarter_wavelength;
769+
path.line_to({ position.x() + (fraction * quarter_wavelength), position.y() - (fraction * quarter_wavelength) });
770+
remaining = 0;
771+
}
772+
773+
// Down
774+
if (remaining > half_wavelength) {
775+
path.line_to({ position.x() + half_wavelength, position.y() + quarter_wavelength });
776+
position.translate_by(half_wavelength, 0);
777+
remaining = abs(to.x() - position.x());
778+
} else if (remaining >= 1) {
779+
auto fraction = remaining / half_wavelength;
780+
path.line_to({ position.x() + (fraction * half_wavelength), position.y() - quarter_wavelength + (fraction * half_wavelength) });
781+
remaining = 0;
782+
}
783+
784+
// Back to middle
785+
if (remaining >= 1) {
786+
auto fraction = remaining / quarter_wavelength;
787+
path.line_to({ position.x() + (fraction * quarter_wavelength), position.y() + ((1 - fraction) * quarter_wavelength) });
788+
}
789+
790+
return path;
791+
}
792+
737793
void paint_text_decoration(DisplayListRecordingContext& context, TextPaintable const& paintable, PaintableFragment const& fragment)
738794
{
739-
auto& painter = context.display_list_recorder();
795+
auto& recorder = context.display_list_recorder();
740796
auto& font = fragment.layout_node().first_available_font();
741797
auto fragment_box = fragment.absolute_rect();
742798
CSSPixels glyph_height = CSSPixels::nearest_value_for(font.pixel_size());
@@ -796,7 +852,7 @@ void paint_text_decoration(DisplayListRecordingContext& context, TextPaintable c
796852

797853
switch (line_style) {
798854
case CSS::TextDecorationStyle::Solid:
799-
painter.draw_line(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value(), Gfx::LineStyle::Solid);
855+
recorder.draw_line(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value(), Gfx::LineStyle::Solid);
800856
break;
801857
case CSS::TextDecorationStyle::Double:
802858
switch (line) {
@@ -814,14 +870,14 @@ void paint_text_decoration(DisplayListRecordingContext& context, TextPaintable c
814870
VERIFY_NOT_REACHED();
815871
}
816872

817-
painter.draw_line(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value());
818-
painter.draw_line(line_start_point.translated(0, device_line_thickness + 1).to_type<int>(), line_end_point.translated(0, device_line_thickness + 1).to_type<int>(), line_color, device_line_thickness.value());
873+
recorder.draw_line(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value());
874+
recorder.draw_line(line_start_point.translated(0, device_line_thickness + 1).to_type<int>(), line_end_point.translated(0, device_line_thickness + 1).to_type<int>(), line_color, device_line_thickness.value());
819875
break;
820876
case CSS::TextDecorationStyle::Dashed:
821-
painter.draw_line(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value(), Gfx::LineStyle::Dashed);
877+
recorder.draw_line(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value(), Gfx::LineStyle::Dashed);
822878
break;
823879
case CSS::TextDecorationStyle::Dotted:
824-
painter.draw_line(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value(), Gfx::LineStyle::Dotted);
880+
recorder.draw_line(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value(), Gfx::LineStyle::Dotted);
825881
break;
826882
case CSS::TextDecorationStyle::Wavy:
827883
auto amplitude = device_line_thickness.value() * 3;
@@ -841,7 +897,16 @@ void paint_text_decoration(DisplayListRecordingContext& context, TextPaintable c
841897
default:
842898
VERIFY_NOT_REACHED();
843899
}
844-
painter.draw_triangle_wave(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, amplitude, device_line_thickness.value());
900+
recorder.stroke_path({
901+
.cap_style = Gfx::Path::CapStyle::Round,
902+
.join_style = Gfx::Path::JoinStyle::Round,
903+
.miter_limit = 0,
904+
.dash_array = {},
905+
.dash_offset = 0,
906+
.path = build_triangle_wave_path(line_start_point.to_type<int>(), line_end_point.to_type<int>(), amplitude),
907+
.color = line_color,
908+
.thickness = static_cast<float>(device_line_thickness.value()),
909+
});
845910
break;
846911
}
847912
}

0 commit comments

Comments
 (0)