Skip to content

Truly independent web browser

2.5 KB
Raw Blame
/*
 * Copyright (c) 2024-2026, Aliaksandr Kalenik <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/AtomicRefCounted.h>
#include <AK/Function.h>
#include <AK/Noncopyable.h>
#include <AK/Optional.h>
#include <LibCore/Timer.h>
#include <LibGfx/GpuDeviceIdentity.h>
#include <LibGfx/Size.h>

#ifdef USE_DIRECTX
#    include <LibGfx/Direct3DContext.h>
#endif

#ifdef USE_VULKAN
#    include <LibGfx/VulkanContext.h>
#endif

#ifdef AK_OS_MACOS
#    include <LibGfx/MetalContext.h>
#endif

class GrDirectContext;
class SkSurface;

namespace Gfx {

struct VulkanContext;
class Direct3DContext;
class MetalContext;
class PaintingSurface;

class SkiaBackendContext : public AtomicRefCounted<SkiaBackendContext> {
    AK_MAKE_NONCOPYABLE(SkiaBackendContext);
    AK_MAKE_NONMOVABLE(SkiaBackendContext);

public:
#ifdef USE_DIRECTX
    static RefPtr<SkiaBackendContext> create_direct3d_context(NonnullRefPtr<Direct3DContext>);
#endif

#ifdef USE_VULKAN
    static RefPtr<SkiaBackendContext> create_vulkan_context(VulkanContext const& vulkan_context);
#endif

#ifdef AK_OS_MACOS
    static RefPtr<SkiaBackendContext> create_metal_context(NonnullRefPtr<MetalContext>);
#endif

    // The preferred GPU is used when there is one, so that the backing stores can be shared with the presentation.
    static void initialize_gpu_backend(Optional<GpuDeviceIdentity> const& preferred_device = {});
    static RefPtr<SkiaBackendContext> create_independent_gpu_backend(Optional<GpuDeviceIdentity> const& preferred_device = {});
    static RefPtr<SkiaBackendContext> the_main_thread_context();

    SkiaBackendContext() { }
    virtual ~SkiaBackendContext() { }

    void flush_and_submit(PaintingSurface&);
    void flush_and_submit_async(PaintingSurface&, Function<void()>&&);
    // Flushes the work drawn into every surface.
    void flush_and_submit_async(Function<void()>&&);
    void check_async_work_completion();
    size_t resource_cache_bytes() const;
    // Whether a texture of this size fits within the largest the GPU allows. On macOS, asking Metal for a larger
    // texture over an IOSurface aborts the process, rather than failing.
    bool can_hold_texture_of_size(IntSize) const;
    virtual GrDirectContext* sk_context() const = 0;

    virtual MetalContext& metal_context() = 0;
    virtual VulkanContext const& vulkan_context() = 0;
    virtual Direct3DContext& direct3d_context() { VERIFY_NOT_REACHED(); }

private:
    void perform_post_flush_cleanup();

    RefPtr<Core::Timer> m_idle_cleanup_timer;
};

}