123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867 |
- /*
- * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include <LibGfx/Filters/StackBlurFilter.h>
- #include <LibGfx/StylePainter.h>
- #include <LibWeb/Painting/BorderRadiusCornerClipper.h>
- #include <LibWeb/Painting/FilterPainting.h>
- #include <LibWeb/Painting/RecordingPainter.h>
- #include <LibWeb/Painting/ShadowPainting.h>
- namespace Web::Painting {
- struct CommandExecutionState {
- struct StackingContext {
- Gfx::Painter painter;
- Gfx::IntRect destination;
- float opacity;
- };
- [[nodiscard]] Gfx::Painter const& painter() const { return stacking_contexts.last().painter; }
- [[nodiscard]] Gfx::Painter& painter() { return stacking_contexts.last().painter; }
- [[nodiscard]] bool would_be_fully_clipped_by_painter(Gfx::IntRect rect) const
- {
- return !painter().clip_rect().intersects(rect.translated(painter().translation()));
- }
- Vector<StackingContext> stacking_contexts;
- };
- CommandResult FillRectWithRoundedCorners::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- Gfx::AntiAliasingPainter aa_painter(painter);
- if (aa_translation.has_value())
- aa_painter.translate(*aa_translation);
- aa_painter.fill_rect_with_rounded_corners(
- rect,
- color,
- top_left_radius,
- top_right_radius,
- bottom_right_radius,
- bottom_left_radius);
- return CommandResult::Continue;
- }
- CommandResult DrawText::execute(CommandExecutionState& state) const
- {
- if (state.would_be_fully_clipped_by_painter(rect))
- return CommandResult::Continue;
- auto& painter = state.painter();
- if (font.has_value()) {
- painter.draw_text(rect, raw_text, *font, alignment, color, elision, wrapping);
- } else {
- painter.draw_text(rect, raw_text, alignment, color, elision, wrapping);
- }
- return CommandResult::Continue;
- }
- CommandResult DrawTextRun::execute(CommandExecutionState& state) const
- {
- if (state.would_be_fully_clipped_by_painter(rect))
- return CommandResult::Continue;
- auto& painter = state.painter();
- painter.draw_text_run(baseline_start, Utf8View(string), font, color);
- return CommandResult::Continue;
- }
- CommandResult FillPathUsingColor::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- Gfx::AntiAliasingPainter aa_painter(painter);
- if (aa_translation.has_value())
- aa_painter.translate(*aa_translation);
- aa_painter.fill_path(path, color, winding_rule);
- return CommandResult::Continue;
- }
- CommandResult FillPathUsingPaintStyle::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- Gfx::AntiAliasingPainter aa_painter(painter);
- if (aa_translation.has_value())
- aa_painter.translate(*aa_translation);
- aa_painter.fill_path(path, paint_style, opacity, winding_rule);
- return CommandResult::Continue;
- }
- CommandResult StrokePathUsingColor::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- Gfx::AntiAliasingPainter aa_painter(painter);
- if (aa_translation.has_value())
- aa_painter.translate(*aa_translation);
- aa_painter.stroke_path(path, color, thickness);
- return CommandResult::Continue;
- }
- CommandResult StrokePathUsingPaintStyle::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- Gfx::AntiAliasingPainter aa_painter(painter);
- if (aa_translation.has_value())
- aa_painter.translate(*aa_translation);
- aa_painter.stroke_path(path, paint_style, thickness, opacity);
- return CommandResult::Continue;
- }
- CommandResult FillRect::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- painter.fill_rect(rect, color);
- return CommandResult::Continue;
- }
- CommandResult DrawScaledBitmap::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- painter.draw_scaled_bitmap(dst_rect, bitmap, src_rect, opacity, scaling_mode);
- return CommandResult::Continue;
- }
- CommandResult SetClipRect::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- painter.clear_clip_rect();
- painter.add_clip_rect(rect);
- return CommandResult::Continue;
- }
- CommandResult ClearClipRect::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- painter.clear_clip_rect();
- return CommandResult::Continue;
- }
- CommandResult SetFont::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- painter.set_font(font);
- return CommandResult::Continue;
- }
- CommandResult PushStackingContext::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- if (semitransparent_or_has_non_identity_transform) {
- auto destination_rect = transformed_destination_rect.to_rounded<int>();
- // FIXME: We should find a way to scale the paintable, rather than paint into a separate bitmap,
- // then scale it. This snippet now copies the background at the destination, then scales it down/up
- // to the size of the source (which could add some artefacts, though just scaling the bitmap already does that).
- // We need to copy the background at the destination because a bunch of our rendering effects now rely on
- // being able to sample the painter (see border radii, shadows, filters, etc).
- Gfx::FloatPoint destination_clipped_fixup {};
- auto try_get_scaled_destination_bitmap = [&]() -> ErrorOr<NonnullRefPtr<Gfx::Bitmap>> {
- Gfx::IntRect actual_destination_rect;
- auto bitmap = TRY(painter.get_region_bitmap(destination_rect, Gfx::BitmapFormat::BGRA8888, actual_destination_rect));
- // get_region_bitmap() may clip to a smaller region if the requested rect goes outside the painter, so we need to account for that.
- destination_clipped_fixup = Gfx::FloatPoint { destination_rect.location() - actual_destination_rect.location() };
- destination_rect = actual_destination_rect;
- if (source_rect.size() != transformed_destination_rect.size()) {
- auto sx = static_cast<float>(source_rect.width()) / transformed_destination_rect.width();
- auto sy = static_cast<float>(source_rect.height()) / transformed_destination_rect.height();
- bitmap = TRY(bitmap->scaled(sx, sy));
- destination_clipped_fixup.scale_by(sx, sy);
- }
- return bitmap;
- };
- auto bitmap_or_error = try_get_scaled_destination_bitmap();
- if (bitmap_or_error.is_error()) {
- // NOTE: If the creation of the bitmap fails, we need to skip all painting commands that belong to this stacking context.
- // We don't interrupt the execution of painting commands because get_region_bitmap() returns an error if the requested
- // region is outside of the viewport (mmap fails to allocate a zero-size region), which means we can safely proceed
- // with execution of commands outside of this stacking context.
- // FIXME: Change the get_region_bitmap() API to return ErrorOr<Optional<Bitmap>> and exit the execution of commands here
- // if we run out of memory.
- return CommandResult::SkipStackingContext;
- }
- auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
- Gfx::Painter stacking_context_painter(bitmap);
- stacking_context_painter.translate(painter_location + destination_clipped_fixup.to_type<int>());
- state.stacking_contexts.append(CommandExecutionState::StackingContext {
- .painter = stacking_context_painter,
- .destination = destination_rect,
- .opacity = opacity,
- });
- } else {
- state.painter().save();
- }
- return CommandResult::Continue;
- }
- CommandResult PopStackingContext::execute(CommandExecutionState& state) const
- {
- if (semitransparent_or_has_non_identity_transform) {
- auto stacking_context = state.stacking_contexts.take_last();
- auto bitmap = stacking_context.painter.target();
- auto destination_rect = stacking_context.destination;
- if (destination_rect.size() == bitmap->size()) {
- state.painter().blit(destination_rect.location(), *bitmap, bitmap->rect(), stacking_context.opacity);
- } else {
- state.painter().draw_scaled_bitmap(destination_rect, *bitmap, bitmap->rect(), stacking_context.opacity, scaling_mode);
- }
- } else {
- state.painter().restore();
- }
- return CommandResult::Continue;
- }
- CommandResult PushStackingContextWithMask::execute(CommandExecutionState& state) const
- {
- auto bitmap_or_error = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, paint_rect.size());
- if (bitmap_or_error.is_error())
- return CommandResult::Continue;
- auto bitmap = bitmap_or_error.release_value();
- Gfx::Painter stacking_context_painter(bitmap);
- stacking_context_painter.translate(-paint_rect.location());
- state.stacking_contexts.append(CommandExecutionState::StackingContext {
- .painter = stacking_context_painter,
- .destination = {},
- .opacity = 1,
- });
- return CommandResult::Continue;
- }
- CommandResult PopStackingContextWithMask::execute(CommandExecutionState& state) const
- {
- auto stacking_context = state.stacking_contexts.take_last();
- auto bitmap = stacking_context.painter.target();
- if (mask_bitmap)
- bitmap->apply_mask(*mask_bitmap, mask_kind);
- state.painter().blit(paint_rect.location(), *bitmap, bitmap->rect(), opacity);
- return CommandResult::Continue;
- }
- CommandResult PaintLinearGradient::execute(CommandExecutionState& state) const
- {
- auto const& data = linear_gradient_data;
- state.painter().fill_rect_with_linear_gradient(
- gradient_rect, data.color_stops.list,
- data.gradient_angle, data.color_stops.repeat_length);
- return CommandResult::Continue;
- }
- CommandResult PaintRadialGradient::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- painter.fill_rect_with_radial_gradient(rect, radial_gradient_data.color_stops.list, center, size, radial_gradient_data.color_stops.repeat_length);
- return CommandResult::Continue;
- }
- CommandResult PaintConicGradient::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- painter.fill_rect_with_conic_gradient(rect, conic_gradient_data.color_stops.list, position, conic_gradient_data.start_angle, conic_gradient_data.color_stops.repeat_length);
- return CommandResult::Continue;
- }
- Gfx::IntRect PaintOuterBoxShadow::bounding_rect() const
- {
- return get_outer_box_shadow_bounding_rect(outer_box_shadow_params);
- }
- CommandResult PaintOuterBoxShadow::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- paint_outer_box_shadow(painter, outer_box_shadow_params);
- return CommandResult::Continue;
- }
- CommandResult PaintInnerBoxShadow::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- paint_inner_box_shadow(painter, outer_box_shadow_params);
- return CommandResult::Continue;
- }
- CommandResult PaintTextShadow::execute(CommandExecutionState& state) const
- {
- if (state.would_be_fully_clipped_by_painter(text_rect))
- return CommandResult::Continue;
- // FIXME: Figure out the maximum bitmap size for all shadows and then allocate it once and reuse it?
- auto maybe_shadow_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, shadow_bounding_rect.size());
- if (maybe_shadow_bitmap.is_error()) {
- dbgln("Unable to allocate temporary bitmap {} for text-shadow rendering: {}", shadow_bounding_rect.size(), maybe_shadow_bitmap.error());
- return CommandResult::Continue;
- }
- auto shadow_bitmap = maybe_shadow_bitmap.release_value();
- Gfx::Painter shadow_painter { *shadow_bitmap };
- // FIXME: "Spread" the shadow somehow.
- Gfx::IntPoint baseline_start(text_rect.x(), text_rect.y() + fragment_baseline);
- shadow_painter.draw_text_run(baseline_start, Utf8View(text), font, color);
- // Blur
- Gfx::StackBlurFilter filter(*shadow_bitmap);
- filter.process_rgba(blur_radius, color);
- auto& painter = state.painter();
- painter.blit(draw_location, *shadow_bitmap, shadow_bounding_rect);
- return CommandResult::Continue;
- }
- CommandResult DrawEllipse::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- Gfx::AntiAliasingPainter aa_painter(painter);
- aa_painter.draw_ellipse(rect, color, thickness);
- return CommandResult::Continue;
- }
- CommandResult FillElipse::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- Gfx::AntiAliasingPainter aa_painter(painter);
- aa_painter.fill_ellipse(rect, color, blend_mode);
- return CommandResult::Continue;
- }
- CommandResult DrawLine::execute(CommandExecutionState& state) const
- {
- if (style == Gfx::Painter::LineStyle::Dotted) {
- Gfx::AntiAliasingPainter aa_painter(state.painter());
- aa_painter.draw_line(from, to, color, thickness, style, alternate_color);
- } else {
- state.painter().draw_line(from, to, color, thickness, style, alternate_color);
- }
- return CommandResult::Continue;
- }
- CommandResult DrawSignedDistanceField::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- painter.draw_signed_distance_field(rect, color, sdf, smoothing);
- return CommandResult::Continue;
- }
- CommandResult PaintProgressbar::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- Gfx::StylePainter::paint_progressbar(painter, progress_rect, palette, min, max, value, text);
- Gfx::StylePainter::paint_frame(painter, frame_rect, palette, Gfx::FrameStyle::RaisedBox);
- return CommandResult::Continue;
- }
- CommandResult PaintFrame::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- Gfx::StylePainter::paint_frame(painter, rect, palette, style);
- return CommandResult::Continue;
- }
- CommandResult ApplyBackdropFilter::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- // This performs the backdrop filter operation: https://drafts.fxtf.org/filter-effects-2/#backdrop-filter-operation
- // Note: The region bitmap can be smaller than the backdrop_region if it's at the edge of canvas.
- // Note: This is in DevicePixels, but we use an IntRect because `get_region_bitmap()` below writes to it.
- // FIXME: Go through the steps to find the "Backdrop Root Image"
- // https://drafts.fxtf.org/filter-effects-2/#BackdropRoot
- // 1. Copy the Backdrop Root Image into a temporary buffer, such as a raster image. Call this buffer T’.
- Gfx::IntRect actual_region {};
- auto maybe_backdrop_bitmap = painter.get_region_bitmap(backdrop_region, Gfx::BitmapFormat::BGRA8888, actual_region);
- if (actual_region.is_empty())
- return CommandResult::Continue;
- if (maybe_backdrop_bitmap.is_error()) {
- dbgln("Failed get region bitmap for backdrop-filter");
- return CommandResult::Continue;
- }
- auto backdrop_bitmap = maybe_backdrop_bitmap.release_value();
- // 2. Apply the backdrop-filter’s filter operations to the entire contents of T'.
- apply_filter_list(*backdrop_bitmap, backdrop_filter.filters);
- // FIXME: 3. If element B has any transforms (between B and the Backdrop Root), apply the inverse of those transforms to the contents of T’.
- // 4. Apply a clip to the contents of T’, using the border box of element B, including border-radius if specified. Note that the children of B are not considered for the sizing or location of this clip.
- // FIXME: 5. Draw all of element B, including its background, border, and any children elements, into T’.
- // FXIME: 6. If element B has any transforms, effects, or clips, apply those to T’.
- // 7. Composite the contents of T’ into element B’s parent, using source-over compositing.
- painter.blit(actual_region.location(), *backdrop_bitmap, backdrop_bitmap->rect());
- return CommandResult::Continue;
- }
- CommandResult DrawRect::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- painter.draw_rect(rect, color, rough);
- return CommandResult::Continue;
- }
- CommandResult DrawTriangleWave::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- painter.draw_triangle_wave(p1, p2, color, amplitude, thickness);
- return CommandResult::Continue;
- }
- Gfx::IntRect SampleUnderCorners::bounding_rect() const
- {
- return corner_clipper->border_rect().to_type<int>();
- }
- CommandResult SampleUnderCorners::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- corner_clipper->sample_under_corners(painter);
- return CommandResult::Continue;
- }
- Gfx::IntRect BlitCornerClipping::bounding_rect() const
- {
- return corner_clipper->border_rect().to_type<int>();
- }
- CommandResult BlitCornerClipping::execute(CommandExecutionState& state) const
- {
- auto& painter = state.painter();
- corner_clipper->blit_corner_clipping(painter);
- return CommandResult::Continue;
- }
- void RecordingPainter::sample_under_corners(NonnullRefPtr<BorderRadiusCornerClipper> corner_clipper)
- {
- push_command(SampleUnderCorners { corner_clipper });
- }
- void RecordingPainter::blit_corner_clipping(NonnullRefPtr<BorderRadiusCornerClipper> corner_clipper)
- {
- push_command(BlitCornerClipping { corner_clipper });
- }
- void RecordingPainter::fill_rect(Gfx::IntRect const& rect, Color color)
- {
- push_command(FillRect {
- .rect = rect,
- .color = color,
- });
- }
- void RecordingPainter::fill_path(FillPathUsingColorParams params)
- {
- auto aa_translation = state().translation.map(params.translation.value_or(Gfx::FloatPoint {}));
- auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
- push_command(FillPathUsingColor {
- .path_bounding_rect = path_bounding_rect,
- .path = params.path,
- .color = params.color,
- .winding_rule = params.winding_rule,
- .aa_translation = aa_translation,
- });
- }
- void RecordingPainter::fill_path(FillPathUsingPaintStyleParams params)
- {
- auto aa_translation = state().translation.map(params.translation.value_or(Gfx::FloatPoint {}));
- auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
- push_command(FillPathUsingPaintStyle {
- .path_bounding_rect = path_bounding_rect,
- .path = params.path,
- .paint_style = params.paint_style,
- .winding_rule = params.winding_rule,
- .opacity = params.opacity,
- .aa_translation = aa_translation,
- });
- }
- void RecordingPainter::stroke_path(StrokePathUsingColorParams params)
- {
- auto aa_translation = state().translation.map(params.translation.value_or(Gfx::FloatPoint {}));
- auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
- push_command(StrokePathUsingColor {
- .path_bounding_rect = path_bounding_rect,
- .path = params.path,
- .color = params.color,
- .thickness = params.thickness,
- .aa_translation = aa_translation,
- });
- }
- void RecordingPainter::stroke_path(StrokePathUsingPaintStyleParams params)
- {
- auto aa_translation = state().translation.map(params.translation.value_or(Gfx::FloatPoint {}));
- auto path_bounding_rect = params.path.bounding_box().translated(aa_translation).to_type<int>();
- push_command(StrokePathUsingPaintStyle {
- .path_bounding_rect = path_bounding_rect,
- .path = params.path,
- .paint_style = params.paint_style,
- .thickness = params.thickness,
- .aa_translation = aa_translation,
- });
- }
- void RecordingPainter::draw_ellipse(Gfx::IntRect const& a_rect, Color color, int thickness)
- {
- push_command(DrawEllipse {
- .rect = state().translation.map(a_rect),
- .color = color,
- .thickness = thickness,
- });
- }
- void RecordingPainter::fill_ellipse(Gfx::IntRect const& a_rect, Color color, Gfx::AntiAliasingPainter::BlendMode blend_mode)
- {
- push_command(FillElipse {
- .rect = state().translation.map(a_rect),
- .color = color,
- .blend_mode = blend_mode,
- });
- }
- void RecordingPainter::fill_rect_with_linear_gradient(Gfx::IntRect const& gradient_rect, LinearGradientData const& data)
- {
- push_command(PaintLinearGradient {
- .gradient_rect = state().translation.map(gradient_rect),
- .linear_gradient_data = data,
- });
- }
- void RecordingPainter::fill_rect_with_conic_gradient(Gfx::IntRect const& rect, ConicGradientData const& data, Gfx::IntPoint const& position)
- {
- push_command(PaintConicGradient {
- .rect = state().translation.map(rect),
- .conic_gradient_data = data,
- .position = position });
- }
- void RecordingPainter::fill_rect_with_radial_gradient(Gfx::IntRect const& rect, RadialGradientData const& data, Gfx::IntPoint center, Gfx::IntSize size)
- {
- push_command(PaintRadialGradient {
- .rect = state().translation.map(rect),
- .radial_gradient_data = data,
- .center = center,
- .size = size });
- }
- void RecordingPainter::draw_rect(Gfx::IntRect const& rect, Color color, bool rough)
- {
- push_command(DrawRect {
- .rect = state().translation.map(rect),
- .color = color,
- .rough = rough });
- }
- void RecordingPainter::draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, float opacity, Gfx::Painter::ScalingMode scaling_mode)
- {
- push_command(DrawScaledBitmap {
- .dst_rect = state().translation.map(dst_rect),
- .bitmap = bitmap,
- .src_rect = src_rect,
- .opacity = opacity,
- .scaling_mode = scaling_mode,
- });
- }
- void RecordingPainter::draw_line(Gfx::IntPoint from, Gfx::IntPoint to, Color color, int thickness, Gfx::Painter::LineStyle style, Color alternate_color)
- {
- push_command(DrawLine {
- .color = color,
- .from = state().translation.map(from),
- .to = state().translation.map(to),
- .thickness = thickness,
- .style = style,
- .alternate_color = alternate_color,
- });
- }
- void RecordingPainter::draw_text(Gfx::IntRect const& rect, StringView raw_text, Gfx::TextAlignment alignment, Color color, Gfx::TextElision elision, Gfx::TextWrapping wrapping)
- {
- push_command(DrawText {
- .rect = state().translation.map(rect),
- .raw_text = String::from_utf8(raw_text).release_value_but_fixme_should_propagate_errors(),
- .alignment = alignment,
- .color = color,
- .elision = elision,
- .wrapping = wrapping,
- });
- }
- void RecordingPainter::draw_text(Gfx::IntRect const& rect, StringView raw_text, Gfx::Font const& font, Gfx::TextAlignment alignment, Color color, Gfx::TextElision elision, Gfx::TextWrapping wrapping)
- {
- push_command(DrawText {
- .rect = state().translation.map(rect),
- .raw_text = String::from_utf8(raw_text).release_value_but_fixme_should_propagate_errors(),
- .alignment = alignment,
- .color = color,
- .elision = elision,
- .wrapping = wrapping,
- .font = font,
- });
- }
- void RecordingPainter::draw_signed_distance_field(Gfx::IntRect const& dst_rect, Color color, Gfx::GrayscaleBitmap const& sdf, float smoothing)
- {
- push_command(DrawSignedDistanceField {
- .rect = state().translation.map(dst_rect),
- .color = color,
- .sdf = sdf,
- .smoothing = smoothing,
- });
- }
- void RecordingPainter::draw_text_run(Gfx::IntPoint baseline_start, Utf8View string, Gfx::Font const& font, Color color, Gfx::IntRect const& rect)
- {
- push_command(DrawTextRun {
- .color = color,
- .baseline_start = state().translation.map(baseline_start),
- .string = String::from_utf8(string.as_string()).release_value_but_fixme_should_propagate_errors(),
- .font = font,
- .rect = state().translation.map(rect),
- });
- }
- void RecordingPainter::add_clip_rect(Gfx::IntRect const& rect)
- {
- auto prev_clip_rect = state().clip_rect;
- if (!state().clip_rect.has_value()) {
- state().clip_rect = state().translation.map(rect);
- } else {
- state().clip_rect->intersect(state().translation.map(rect));
- }
- if (prev_clip_rect != state().clip_rect)
- push_command(SetClipRect { .rect = *state().clip_rect });
- }
- void RecordingPainter::translate(int dx, int dy)
- {
- m_state_stack.last().translation.translate(dx, dy);
- }
- void RecordingPainter::translate(Gfx::IntPoint delta)
- {
- m_state_stack.last().translation.translate(delta.to_type<float>());
- }
- void RecordingPainter::set_font(Gfx::Font const& font)
- {
- push_command(SetFont { .font = font });
- }
- void RecordingPainter::save()
- {
- m_state_stack.append(m_state_stack.last());
- }
- void RecordingPainter::restore()
- {
- auto prev_clip_rect = state().clip_rect;
- VERIFY(m_state_stack.size() > 1);
- m_state_stack.take_last();
- if (state().clip_rect != prev_clip_rect) {
- if (state().clip_rect.has_value())
- push_command(SetClipRect { .rect = *state().clip_rect });
- else
- push_command(ClearClipRect {});
- }
- }
- void RecordingPainter::push_stacking_context(PushStackingContextParams params)
- {
- push_command(PushStackingContext {
- .semitransparent_or_has_non_identity_transform = params.semitransparent_or_has_non_identity_transform,
- .has_fixed_position = params.has_fixed_position,
- .opacity = params.opacity,
- .source_rect = state().translation.map(params.source_rect),
- .transformed_destination_rect = state().translation.map(params.transformed_destination_rect),
- .painter_location = state().translation.map(params.painter_location),
- });
- if (params.has_fixed_position) {
- state().translation.set_translation(0, 0);
- }
- if (params.semitransparent_or_has_non_identity_transform) {
- m_state_stack.append(State());
- }
- }
- void RecordingPainter::pop_stacking_context(PopStackingContextParams params)
- {
- push_command(PopStackingContext {
- .semitransparent_or_has_non_identity_transform = params.semitransparent_or_has_non_identity_transform,
- .scaling_mode = params.scaling_mode,
- });
- if (params.semitransparent_or_has_non_identity_transform) {
- m_state_stack.take_last();
- }
- }
- void RecordingPainter::paint_progressbar(Gfx::IntRect frame_rect, Gfx::IntRect progress_rect, Palette palette, int min, int max, int value, StringView text)
- {
- push_command(PaintProgressbar {
- .frame_rect = state().translation.map(frame_rect),
- .progress_rect = state().translation.map(progress_rect),
- .palette = palette,
- .min = min,
- .max = max,
- .value = value,
- .text = text,
- });
- }
- void RecordingPainter::paint_frame(Gfx::IntRect rect, Palette palette, Gfx::FrameStyle style)
- {
- push_command(PaintFrame { state().translation.map(rect), palette, style });
- }
- void RecordingPainter::apply_backdrop_filter(Gfx::IntRect const& backdrop_region, BorderRadiiData const& border_radii_data, CSS::ResolvedBackdropFilter const& backdrop_filter)
- {
- push_command(ApplyBackdropFilter {
- .backdrop_region = state().translation.map(backdrop_region),
- .border_radii_data = border_radii_data,
- .backdrop_filter = backdrop_filter,
- });
- }
- void RecordingPainter::paint_outer_box_shadow_params(PaintOuterBoxShadowParams params)
- {
- push_command(PaintOuterBoxShadow {
- .outer_box_shadow_params = params,
- });
- }
- void RecordingPainter::paint_inner_box_shadow_params(PaintOuterBoxShadowParams params)
- {
- push_command(PaintInnerBoxShadow {
- .outer_box_shadow_params = params,
- });
- }
- void RecordingPainter::paint_text_shadow(int blur_radius, Gfx::IntRect bounding_rect, Gfx::IntRect text_rect, Utf8View text, Gfx::Font const& font, Color color, int fragment_baseline, Gfx::IntPoint draw_location)
- {
- push_command(PaintTextShadow {
- .blur_radius = blur_radius,
- .shadow_bounding_rect = state().translation.map(bounding_rect),
- .text_rect = state().translation.map(text_rect),
- .text = String::from_utf8(text.as_string()).release_value_but_fixme_should_propagate_errors(),
- .font = font,
- .color = color,
- .fragment_baseline = fragment_baseline,
- .draw_location = state().translation.map(draw_location) });
- }
- void RecordingPainter::fill_rect_with_rounded_corners(Gfx::IntRect const& rect, Color color, Gfx::AntiAliasingPainter::CornerRadius top_left_radius, Gfx::AntiAliasingPainter::CornerRadius top_right_radius, Gfx::AntiAliasingPainter::CornerRadius bottom_right_radius, Gfx::AntiAliasingPainter::CornerRadius bottom_left_radius)
- {
- push_command(FillRectWithRoundedCorners {
- .rect = state().translation.map(rect),
- .color = color,
- .top_left_radius = top_left_radius,
- .top_right_radius = top_right_radius,
- .bottom_left_radius = bottom_left_radius,
- .bottom_right_radius = bottom_right_radius,
- });
- }
- void RecordingPainter::fill_rect_with_rounded_corners(Gfx::IntRect const& a_rect, Color color, int radius)
- {
- fill_rect_with_rounded_corners(a_rect, color, radius, radius, radius, radius);
- }
- void RecordingPainter::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)
- {
- fill_rect_with_rounded_corners(a_rect, color,
- { top_left_radius, top_left_radius },
- { top_right_radius, top_right_radius },
- { bottom_right_radius, bottom_right_radius },
- { bottom_left_radius, bottom_left_radius });
- }
- void RecordingPainter::push_stacking_context_with_mask(Gfx::IntRect paint_rect)
- {
- push_command(PushStackingContextWithMask { .paint_rect = state().translation.map(paint_rect) });
- }
- void RecordingPainter::pop_stacking_context_with_mask(RefPtr<Gfx::Bitmap> mask_bitmap, Gfx::Bitmap::MaskKind mask_kind, Gfx::IntRect paint_rect, float opacity)
- {
- push_command(PopStackingContextWithMask {
- .paint_rect = state().translation.map(paint_rect),
- .mask_bitmap = mask_bitmap,
- .mask_kind = mask_kind,
- .opacity = opacity });
- }
- void RecordingPainter::draw_triangle_wave(Gfx::IntPoint a_p1, Gfx::IntPoint a_p2, Color color, int amplitude, int thickness = 1)
- {
- push_command(DrawTriangleWave {
- .p1 = state().translation.map(a_p1),
- .p2 = state().translation.map(a_p2),
- .color = color,
- .amplitude = amplitude,
- .thickness = thickness });
- }
- static Optional<Gfx::IntRect> command_bounding_rectangle(PaintingCommand const& command)
- {
- return command.visit(
- [&](auto const& command) -> Optional<Gfx::IntRect> {
- if constexpr (requires { command.bounding_rect(); })
- return command.bounding_rect();
- else
- return {};
- });
- }
- void RecordingPainter::execute(Gfx::Bitmap& bitmap)
- {
- CommandExecutionState state;
- state.stacking_contexts.append(CommandExecutionState::StackingContext {
- .painter = Gfx::Painter(bitmap),
- .destination = Gfx::IntRect { 0, 0, 0, 0 },
- .opacity = 1,
- });
- size_t next_command_index = 0;
- while (next_command_index < m_painting_commands.size()) {
- auto& command = m_painting_commands[next_command_index++];
- auto bounding_rect = command_bounding_rectangle(command);
- if (bounding_rect.has_value() && state.would_be_fully_clipped_by_painter(*bounding_rect))
- continue;
- auto result = command.visit([&](auto const& command) { return command.execute(state); });
- if (result == CommandResult::SkipStackingContext) {
- auto stacking_context_nesting_level = 1;
- while (next_command_index < m_painting_commands.size()) {
- if (m_painting_commands[next_command_index].has<PushStackingContext>()) {
- stacking_context_nesting_level++;
- } else if (m_painting_commands[next_command_index].has<PopStackingContext>()) {
- stacking_context_nesting_level--;
- }
- next_command_index++;
- if (stacking_context_nesting_level == 0)
- break;
- }
- }
- }
- VERIFY(state.stacking_contexts.size() == 1);
- }
- }
|