mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-24 21:45:20 +00:00
When decoding data into bitmaps, we end up with different alpha types (premultiplied vs. unpremultiplied color data). Unfortunately, Skia only seems to handle premultiplied color data well when scaling bitmaps with an alpha channel. This might be due to Skia historically only supporting premultiplied color blending, with unpremultiplied support having been added more recently. When using Skia to blend bitmaps, we need the color data to be premultiplied. ImmutableBitmap gains a new method to enforce the alpha type to be used, which is now used by SharedResourceRequest and CanvasRenderingContext2D to enforce the right alpha type. Our LibWeb tests actually had a couple of screenshot tests that exposed the graphical glitches caused by Skia; see the big smiley faces in the CSS backgrounds tests for example. The failing tests are now updated to accommodate the new behavior. Chromium and Firefox both seem to apply the same behavior; e.g. they actively decode PNGs (which are unpremultiplied in nature) to a premultiplied bitmap. Fixes #3691.
118 lines
3.5 KiB
C++
118 lines
3.5 KiB
C++
/*
|
|
* Copyright (c) 2023-2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibGfx/ImmutableBitmap.h>
|
|
#include <LibGfx/PaintingSurface.h>
|
|
#include <LibGfx/SkiaUtils.h>
|
|
|
|
#include <core/SkBitmap.h>
|
|
#include <core/SkColorSpace.h>
|
|
#include <core/SkImage.h>
|
|
|
|
namespace Gfx {
|
|
|
|
struct ImmutableBitmapImpl {
|
|
sk_sp<SkImage> sk_image;
|
|
SkBitmap sk_bitmap;
|
|
Variant<NonnullRefPtr<Gfx::Bitmap>, NonnullRefPtr<Gfx::PaintingSurface>, Empty> source;
|
|
ColorSpace color_space;
|
|
};
|
|
|
|
int ImmutableBitmap::width() const
|
|
{
|
|
return m_impl->sk_image->width();
|
|
}
|
|
|
|
int ImmutableBitmap::height() const
|
|
{
|
|
return m_impl->sk_image->height();
|
|
}
|
|
|
|
IntRect ImmutableBitmap::rect() const
|
|
{
|
|
return { {}, size() };
|
|
}
|
|
|
|
IntSize ImmutableBitmap::size() const
|
|
{
|
|
return { width(), height() };
|
|
}
|
|
|
|
Gfx::AlphaType ImmutableBitmap::alpha_type() const
|
|
{
|
|
return m_impl->sk_image->alphaType() == kPremul_SkAlphaType ? Gfx::AlphaType::Premultiplied : Gfx::AlphaType::Unpremultiplied;
|
|
}
|
|
|
|
SkImage const* ImmutableBitmap::sk_image() const
|
|
{
|
|
return m_impl->sk_image.get();
|
|
}
|
|
|
|
RefPtr<Gfx::Bitmap const> ImmutableBitmap::bitmap() const
|
|
{
|
|
// FIXME: Implement for PaintingSurface
|
|
return m_impl->source.get<NonnullRefPtr<Gfx::Bitmap>>();
|
|
}
|
|
|
|
Color ImmutableBitmap::get_pixel(int x, int y) const
|
|
{
|
|
// FIXME: Implement for PaintingSurface
|
|
return m_impl->source.get<NonnullRefPtr<Gfx::Bitmap>>()->get_pixel(x, y);
|
|
}
|
|
|
|
static SkAlphaType to_skia_alpha_type(Gfx::AlphaType alpha_type)
|
|
{
|
|
switch (alpha_type) {
|
|
case AlphaType::Premultiplied:
|
|
return kPremul_SkAlphaType;
|
|
case AlphaType::Unpremultiplied:
|
|
return kUnpremul_SkAlphaType;
|
|
default:
|
|
VERIFY_NOT_REACHED();
|
|
}
|
|
}
|
|
|
|
NonnullRefPtr<ImmutableBitmap> ImmutableBitmap::create(NonnullRefPtr<Bitmap> bitmap, ColorSpace color_space)
|
|
{
|
|
ImmutableBitmapImpl impl;
|
|
auto info = SkImageInfo::Make(bitmap->width(), bitmap->height(), to_skia_color_type(bitmap->format()), to_skia_alpha_type(bitmap->alpha_type()), color_space.color_space<sk_sp<SkColorSpace>>());
|
|
impl.sk_bitmap.installPixels(info, const_cast<void*>(static_cast<void const*>(bitmap->scanline(0))), bitmap->pitch());
|
|
impl.sk_bitmap.setImmutable();
|
|
impl.sk_image = impl.sk_bitmap.asImage();
|
|
impl.source = bitmap;
|
|
impl.color_space = move(color_space);
|
|
return adopt_ref(*new ImmutableBitmap(make<ImmutableBitmapImpl>(impl)));
|
|
}
|
|
|
|
NonnullRefPtr<ImmutableBitmap> ImmutableBitmap::create(NonnullRefPtr<Bitmap> bitmap, AlphaType alpha_type, ColorSpace color_space)
|
|
{
|
|
// Convert the source bitmap to the right alpha type on a mismatch. We want to do this when converting from a
|
|
// Bitmap to an ImmutableBitmap, since at that point we usually know the right alpha type to use in context.
|
|
auto source_bitmap = bitmap;
|
|
if (source_bitmap->alpha_type() != alpha_type) {
|
|
source_bitmap = MUST(bitmap->clone());
|
|
source_bitmap->set_alpha_type_destructive(alpha_type);
|
|
}
|
|
|
|
return create(source_bitmap, move(color_space));
|
|
}
|
|
|
|
NonnullRefPtr<ImmutableBitmap> ImmutableBitmap::create_snapshot_from_painting_surface(NonnullRefPtr<PaintingSurface> painting_surface)
|
|
{
|
|
ImmutableBitmapImpl impl;
|
|
impl.sk_image = painting_surface->sk_image_snapshot<sk_sp<SkImage>>();
|
|
impl.source = painting_surface;
|
|
return adopt_ref(*new ImmutableBitmap(make<ImmutableBitmapImpl>(impl)));
|
|
}
|
|
|
|
ImmutableBitmap::ImmutableBitmap(NonnullOwnPtr<ImmutableBitmapImpl> impl)
|
|
: m_impl(move(impl))
|
|
{
|
|
}
|
|
|
|
ImmutableBitmap::~ImmutableBitmap() = default;
|
|
|
|
}
|