Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibWeb+LibGfx: Use GPU backend for <canvas> #1532

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
10 changes: 8 additions & 2 deletions Meta/CMake/lagom_install_options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ set(CMAKE_Swift_MODULE_DIRECTORY "${CMAKE_BINARY_DIR}/${IN_BUILD_PREFIX}swift")

set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)

set(VCPKG_LIB_DIR "")
if (NOT "${VCPKG_INSTALLED_DIR}" STREQUAL "")
set(VCPKG_LIB_DIR ";${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib")
endif()

# See slide 100 of the following ppt :^)
# https://crascit.com/wp-content/uploads/2019/09/Deep-CMake-For-Library-Authors-Craig-Scott-CppCon-2019.pdf
if (APPLE)
set(CMAKE_MACOSX_RPATH TRUE)
set(CMAKE_INSTALL_NAME_DIR "@rpath")
set(CMAKE_INSTALL_RPATH "@executable_path/../lib")
set(CMAKE_INSTALL_RPATH "@executable_path/../lib${VCPKG_LIB_DIR}")
else()
set(CMAKE_INSTALL_RPATH "$ORIGIN:$ORIGIN/../${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_INSTALL_RPATH "$ORIGIN:$ORIGIN/../${CMAKE_INSTALL_LIBDIR}${VCPKG_LIB_DIR}")
endif()
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

Expand Down
2 changes: 1 addition & 1 deletion Meta/CMake/vcpkg/release-triplets/release.cmake
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
set(VCPKG_BUILD_TYPE release)
set(VCPKG_LIBRARY_LINKAGE static)
set(VCPKG_LIBRARY_LINKAGE dynamic)
12 changes: 0 additions & 12 deletions Userland/Libraries/LibCore/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
include(vulkan)

# These are the minimal set of sources needed to build the code generators. We separate them to allow
# LibCore to depend on generated sources.
set(SOURCES
Expand Down Expand Up @@ -83,17 +81,12 @@ else()
)
endif()

if (HAS_VULKAN)
list(APPEND SOURCES VulkanContext.cpp)
endif()

if (APPLE OR CMAKE_SYSTEM_NAME STREQUAL "GNU")
list(APPEND SOURCES MachPort.cpp)
endif()

if (APPLE)
list(APPEND SOURCES IOSurface.cpp)
list(APPEND SOURCES MetalContext.mm)
endif()

serenity_lib(LibCore core)
Expand All @@ -105,13 +98,8 @@ if (APPLE)
target_link_libraries(LibCore PUBLIC "-framework CoreServices")
target_link_libraries(LibCore PUBLIC "-framework Foundation")
target_link_libraries(LibCore PUBLIC "-framework IOSurface")
target_link_libraries(LibCore PUBLIC "-framework Metal")
endif()

if (ANDROID)
target_link_libraries(LibCore PRIVATE log)
endif()

if (HAS_VULKAN)
target_link_libraries(LibCore PUBLIC Vulkan::Vulkan Vulkan::Headers)
endif()
128 changes: 0 additions & 128 deletions Userland/Libraries/LibCore/VulkanContext.cpp

This file was deleted.

29 changes: 0 additions & 29 deletions Userland/Libraries/LibCore/VulkanContext.h

This file was deleted.

19 changes: 19 additions & 0 deletions Userland/Libraries/LibGfx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include(skia)
include(vulkan)

set(SOURCES
AffineTransform.cpp
Expand Down Expand Up @@ -50,6 +51,7 @@ set(SOURCES
ImageFormats/AVIFLoader.cpp
ImmutableBitmap.cpp
MedianCut.cpp
PaintingSurface.cpp
Palette.cpp
Path.cpp
PathSkia.cpp
Expand All @@ -63,8 +65,17 @@ set(SOURCES
TextLayout.cpp
Triangle.cpp
VectorGraphic.cpp
SkiaBackendContext.cpp
)

if (APPLE)
list(APPEND SOURCES MetalContext.mm)
endif()

if (HAS_VULKAN)
list(APPEND SOURCES VulkanContext.cpp)
endif()

serenity_lib(LibGfx gfx)

target_link_libraries(LibGfx PRIVATE LibCompress LibCore LibCrypto LibFileSystem LibRIFF LibTextCodec LibIPC LibUnicode LibURL)
Expand Down Expand Up @@ -124,3 +135,11 @@ if (ENABLE_SWIFT)
target_link_libraries(LibGfx PRIVATE AK)
add_swift_target_properties(LibGfx LAGOM_LIBRARIES AK)
endif()

if (APPLE)
target_link_libraries(LibCore PUBLIC "-framework Metal")
endif()

if (HAS_VULKAN)
target_link_libraries(LibCore PUBLIC Vulkan::Vulkan Vulkan::Headers)
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static_assert(false, "This file must only be used for macOS");
#include <AK/Forward.h>
#include <LibCore/IOSurface.h>

namespace Core {
namespace Gfx {

class MetalTexture {
public:
Expand All @@ -29,7 +29,7 @@ class MetalContext {
virtual void const* device() const = 0;
virtual void const* queue() const = 0;

virtual OwnPtr<MetalTexture> create_texture_from_iosurface(IOSurfaceHandle const&) = 0;
virtual OwnPtr<MetalTexture> create_texture_from_iosurface(Core::IOSurfaceHandle const&) = 0;

virtual ~MetalContext() {};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/

#include <AK/OwnPtr.h>
#include <LibCore/MetalContext.h>
#include <LibGfx/MetalContext.h>

#import <Metal/Metal.h>

namespace Core {
namespace Gfx {

class MetalTextureImpl final : public MetalTexture {
public:
Expand Down Expand Up @@ -42,7 +42,7 @@
void const* device() const override { return m_device; }
void const* queue() const override { return m_queue; }

OwnPtr<MetalTexture> create_texture_from_iosurface(IOSurfaceHandle const& iosurface) override
OwnPtr<MetalTexture> create_texture_from_iosurface(Core::IOSurfaceHandle const& iosurface) override
{
auto* const descriptor = [[MTLTextureDescriptor alloc] init];
descriptor.pixelFormat = MTLPixelFormatBGRA8Unorm;
Expand Down
4 changes: 3 additions & 1 deletion Userland/Libraries/LibGfx/Painter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@

#include <LibGfx/Painter.h>
#include <LibGfx/PainterSkia.h>
#include <LibGfx/PaintingSurface.h>

namespace Gfx {

Painter::~Painter() = default;

NonnullOwnPtr<Painter> Painter::create(NonnullRefPtr<Gfx::Bitmap> target_bitmap)
{
return make<PainterSkia>(move(target_bitmap));
auto painting_surface = PaintingSurface::wrap_bitmap(target_bitmap);
return make<PainterSkia>(painting_surface);
}

}
22 changes: 9 additions & 13 deletions Userland/Libraries/LibGfx/PainterSkia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,17 @@ static SkAlphaType to_skia_alpha_type(Gfx::AlphaType alpha_type)
}

struct PainterSkia::Impl {
NonnullRefPtr<Gfx::Bitmap> gfx_bitmap;
OwnPtr<SkBitmap> sk_bitmap;
OwnPtr<SkCanvas> sk_canvas;
RefPtr<Gfx::PaintingSurface> painting_surface;

Impl(NonnullRefPtr<Gfx::Bitmap> target_bitmap)
: gfx_bitmap(move(target_bitmap))
Impl(Gfx::PaintingSurface& surface)
: painting_surface(surface)
{
sk_bitmap = make<SkBitmap>();
SkImageInfo info = SkImageInfo::Make(gfx_bitmap->width(), gfx_bitmap->height(), to_skia_color_type(gfx_bitmap->format()), to_skia_alpha_type(gfx_bitmap->alpha_type()));
sk_bitmap->installPixels(info, gfx_bitmap->scanline(0), gfx_bitmap->pitch());

sk_canvas = make<SkCanvas>(*sk_bitmap);
}

SkCanvas* canvas() { return sk_canvas; }
SkCanvas* canvas() const
{
return &painting_surface->canvas();
}
};

static constexpr SkRect to_skia_rect(auto const& rect)
Expand Down Expand Up @@ -99,8 +95,8 @@ static SkPathFillType to_skia_path_fill_type(Gfx::WindingRule winding_rule)
VERIFY_NOT_REACHED();
}

PainterSkia::PainterSkia(NonnullRefPtr<Gfx::Bitmap> target_bitmap)
: m_impl(adopt_own(*new Impl { move(target_bitmap) }))
PainterSkia::PainterSkia(NonnullRefPtr<Gfx::PaintingSurface> painting_surface)
: m_impl(adopt_own(*new Impl { move(painting_surface) }))
{
}

Expand Down
3 changes: 2 additions & 1 deletion Userland/Libraries/LibGfx/PainterSkia.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
#include <AK/NonnullOwnPtr.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Painter.h>
#include <LibGfx/PaintingSurface.h>

namespace Gfx {

class PainterSkia final : public Painter {
public:
explicit PainterSkia(NonnullRefPtr<Gfx::Bitmap>);
explicit PainterSkia(NonnullRefPtr<Gfx::PaintingSurface>);
virtual ~PainterSkia() override;

virtual void clear_rect(Gfx::FloatRect const&, Color) override;
Expand Down
Loading
Loading