Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/SVG/SVGDecodedImageData.cpp (C++)

Code before:
        VERIFY_NOT_REACHED();
    }

    return bitmap;
}

RefPtr<Gfx::PaintingSurface> SVGDecodedImageData::render_to_surface(Gfx::IntSize size) const
{
    VERIFY(m_document->navigable());

    RefPtr<Gfx::PaintingSurface> surface;

#ifdef AK_OS_MACOS
    if (m_page_client->display_list_player_type() == DisplayListPlayerType::SkiaGPUIfAvailable) {
        VERIFY(m_document->navigable()->skia_backend_context());
        auto iosurface = Core::IOSurfaceHandle::create(size.width(), size.height());
        surface = Gfx::PaintingSurface::create_from_iosurface(move(iosurface), *m_document->navigable()->skia_backend_context(), Gfx::PaintingSurface::Origin::TopLeft);
    }
#endif

    // FIXME: Support Vulkan

    if (!surface) {
        auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, size).release_value();
        surface = Gfx::PaintingSurface::wrap_bitmap(*bitmap);
    }

    m_document->navigable()->set_viewport_size(size.to_type<CSSPixels>());
    m_document->update_layout(DOM::UpdateLayoutReason::SVGDecodedImageDataRender);

    auto display_list = m_document->record_display_list({});
    if (!display_list)
        return nullptr;

    switch (m_page_client->display_list_player_type()) {
    case DisplayListPlayerType::SkiaGPUIfAvailable:
    case DisplayListPlayerType::SkiaCPU: {
        Painting::DisplayListPlayerSkia display_list_player;
        display_list_player.execute(*display_list, {}, surface);
        break;
    }
    default:
        VERIFY_NOT_REACHED();
    }

    return surface;
}

RefPtr<Gfx::ImmutableBitmap> SVGDecodedImageData::bitmap(size_t, Gfx::IntSize size) const
{
    if (size.is_empty())

Review comment (suggestion):
since IOSurface is not shared either between processes or GPU contexts, we could simply use `PaintingSurface::create_with_size()` which will allow GPU acceleration for both Vulkan and Metal.

Code after:
        VERIFY_NOT_REACHED();
    }

    return bitmap;
}

RefPtr<Gfx::PaintingSurface> SVGDecodedImageData::render_to_surface(Gfx::IntSize size) const
{
    VERIFY(m_document->navigable());

    auto surface = Gfx::PaintingSurface::create_with_size(m_document->navigable()->skia_backend_context(), size, Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied);

    m_document->navigable()->set_viewport_size(size.to_type<CSSPixels>());
    m_document->update_layout(DOM::UpdateLayoutReason::SVGDecodedImageDataRender);

    auto display_list = m_document->record_display_list({});
    if (!display_list)
        return nullptr;

    switch (m_page_client->display_list_player_type()) {
    case DisplayListPlayerType::SkiaGPUIfAvailable:
    case DisplayListPlayerType::SkiaCPU: {
        Painting::DisplayListPlayerSkia display_list_player;
        display_list_player.execute(*display_list, {}, surface);
        break;
    }
    default:
        VERIFY_NOT_REACHED();
    }

    return surface;
}

RefPtr<Gfx::ImmutableBitmap> SVGDecodedImageData::bitmap(size_t, Gfx::IntSize size) const
{
    if (size.is_empty())
        return nullptr;

    if (auto it = m_cached_rendered_bitmaps.find(size); it != m_cached_rendered_bitmaps.end())
        return it->value;

    // Prevent the cache from growing too big.
    // FIXME: Evict least used entries.
    if (m_cached_rendered_bitmaps.size() > 10)
        m_cached_rendered_bitmaps.remove(m_cached_rendered_bitmaps.begin());

    auto immutable_bitmap = Gfx::ImmutableBitmap::create(*render(size));
    m_cached_rendered_bitmaps.set(size, immutable_bitmap);
    return immutable_bitmap;
}