Skip to content

Commit

Permalink
[Skia] Implement outset drop shadows
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=269596

Reviewed by Nikolas Zimmermann.

Whenever the GraphicsContext has a drop shadow set, use that to create
and set a SkImageFilters::DropShadow() image effect to the fill paint.
Stroke paints are unaffected.

The SkImageFilters::DropShadow() receives a blur sigma instead of radius
and the formal definition of that [1] states that sigma is half of the
blur radius, so convert between these values.

This only implements outset shadows. Inset shadows won't be rendered
anyway because GraphicsContextSkia::fillRectWithRoundedHole() is not
implemented yet.

[1] https://html.spec.whatwg.org/multipage/canvas.html#shadows

* Source/WebCore/platform/graphics/skia/GraphicsContextSkia.cpp:
(WebCore::GraphicsContextSkia::createFillPaint const):

Canonical link: https://commits.webkit.org/274928@main
  • Loading branch information
GeorgesStavracas authored and nikolaszimmermann committed Feb 17, 2024
1 parent 16db5d8 commit f5c941f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Source/WebCore/platform/graphics/skia/GraphicsContextSkia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <skia/core/SkRegion.h>
#include <skia/core/SkTileMode.h>
#include <skia/effects/SkDashPathEffect.h>
#include <skia/effects/SkImageFilters.h>
#include <wtf/MathExtras.h>

#if USE(THEME_ADWAITA)
Expand Down Expand Up @@ -306,6 +307,21 @@ SkPaint GraphicsContextSkia::createFillPaint(std::optional<Color> fillColor) con
paint.setColor(SkColorSetARGB(a, r, g, b));
}

// Outset shadow
// FIXME: Don't add the effect if the shadow is inset
if (hasDropShadow()) {
const auto shadow = dropShadow();
ASSERT(shadow);

const auto sigma = shadow->radius / 2.0;
auto globalAlpha = state.alpha();
auto shadowColor = shadow->color;
if (globalAlpha < 1)
shadowColor = shadowColor.colorWithAlphaMultipliedBy(globalAlpha);
auto [r, g, b, a] = shadowColor.toColorTypeLossy<SRGBA<uint8_t>>().resolved();
paint.setImageFilter(SkImageFilters::DropShadow(shadow->offset.width(), shadow->offset.height(), sigma, sigma, SkColorSetARGB(a, r, g, b), nullptr));
}

return paint;
}

Expand Down

0 comments on commit f5c941f

Please sign in to comment.