2024-09-25 15:42:15 +02:00
|
|
|
/*
|
2025-06-27 04:39:16 +02:00
|
|
|
* Copyright (c) 2024-2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
2024-09-25 15:42:15 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibGfx/Bitmap.h>
|
|
|
|
#include <LibGfx/PaintingSurface.h>
|
2024-11-10 23:42:27 +04:00
|
|
|
#include <LibGfx/SkiaUtils.h>
|
2024-09-25 15:42:15 +02:00
|
|
|
|
|
|
|
#include <core/SkColorSpace.h>
|
|
|
|
#include <core/SkSurface.h>
|
|
|
|
#include <gpu/GrBackendSurface.h>
|
|
|
|
#include <gpu/GrDirectContext.h>
|
|
|
|
#include <gpu/ganesh/SkSurfaceGanesh.h>
|
|
|
|
|
|
|
|
#ifdef AK_OS_MACOS
|
|
|
|
# include <gpu/ganesh/mtl/GrMtlBackendSurface.h>
|
2025-08-19 12:07:03 -04:00
|
|
|
#elif defined(USE_VULKAN_IMAGES)
|
2025-08-15 09:25:53 -04:00
|
|
|
# include <gpu/ganesh/vk/GrVkBackendSurface.h>
|
|
|
|
# include <gpu/vk/GrVkTypes.h>
|
|
|
|
#endif
|
2024-09-25 15:42:15 +02:00
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
|
|
|
struct PaintingSurface::Impl {
|
2025-04-01 20:18:15 +02:00
|
|
|
RefPtr<SkiaBackendContext> context;
|
2024-09-25 15:42:15 +02:00
|
|
|
IntSize size;
|
|
|
|
sk_sp<SkSurface> surface;
|
|
|
|
RefPtr<Bitmap> bitmap;
|
|
|
|
};
|
|
|
|
|
2025-08-19 12:07:03 -04:00
|
|
|
#if defined(AK_OS_MACOS) || defined(USE_VULKAN_IMAGES)
|
2025-08-15 09:25:53 -04:00
|
|
|
static GrSurfaceOrigin origin_to_sk_origin(PaintingSurface::Origin origin)
|
|
|
|
{
|
|
|
|
switch (origin) {
|
|
|
|
case PaintingSurface::Origin::BottomLeft:
|
|
|
|
return kBottomLeft_GrSurfaceOrigin;
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
case PaintingSurface::Origin::TopLeft:
|
|
|
|
return kTopLeft_GrSurfaceOrigin;
|
|
|
|
}
|
|
|
|
}
|
2025-08-19 12:07:03 -04:00
|
|
|
#endif
|
2025-08-15 09:25:53 -04:00
|
|
|
|
2025-08-19 12:07:03 -04:00
|
|
|
#ifdef USE_VULKAN_IMAGES
|
2025-08-15 09:25:53 -04:00
|
|
|
static SkColorType vk_format_to_sk_color_type(VkFormat format)
|
|
|
|
{
|
|
|
|
switch (format) {
|
|
|
|
case VK_FORMAT_B8G8R8A8_UNORM:
|
|
|
|
return kBGRA_8888_SkColorType;
|
|
|
|
// add more as needed
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
return kUnknown_SkColorType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void release_vulkan_image(void* context)
|
|
|
|
{
|
|
|
|
VulkanImage* image = static_cast<VulkanImage*>(context);
|
|
|
|
image->unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
NonnullRefPtr<PaintingSurface> PaintingSurface::create_from_vkimage(NonnullRefPtr<SkiaBackendContext> context, NonnullRefPtr<VulkanImage> vulkan_image, Origin origin)
|
|
|
|
{
|
|
|
|
context->lock();
|
|
|
|
ScopeGuard unlock_guard([&context] {
|
|
|
|
context->unlock();
|
|
|
|
});
|
|
|
|
|
|
|
|
IntSize size(vulkan_image->info.extent.width, vulkan_image->info.extent.height);
|
|
|
|
GrVkImageInfo info = {
|
|
|
|
.fImage = vulkan_image->image,
|
|
|
|
.fAlloc = {}, // we're managing the memory ourselves
|
|
|
|
.fImageTiling = vulkan_image->info.tiling,
|
|
|
|
.fImageLayout = vulkan_image->info.layout,
|
|
|
|
.fFormat = vulkan_image->info.format,
|
|
|
|
.fImageUsageFlags = vulkan_image->info.usage,
|
|
|
|
.fSampleCount = 1,
|
|
|
|
.fLevelCount = 1,
|
|
|
|
.fCurrentQueueFamily = VK_QUEUE_FAMILY_IGNORED,
|
|
|
|
.fProtected = skgpu::Protected::kNo,
|
|
|
|
.fYcbcrConversionInfo = {},
|
|
|
|
.fSharingMode = vulkan_image->info.sharing_mode,
|
|
|
|
};
|
|
|
|
GrBackendRenderTarget rt = GrBackendRenderTargets::MakeVk(size.width(), size.height(), info);
|
|
|
|
// Note, we're implicitly giving Skia a reference to vulkan_image. It will eventually be released by the callback function.
|
|
|
|
vulkan_image->ref();
|
|
|
|
sk_sp<SkSurface> surface = SkSurfaces::WrapBackendRenderTarget(context->sk_context(), rt, origin_to_sk_origin(origin), vk_format_to_sk_color_type(vulkan_image->info.format),
|
|
|
|
nullptr, nullptr, release_vulkan_image, vulkan_image.ptr());
|
|
|
|
return adopt_ref(*new PaintingSurface(make<Impl>(context, size, surface, nullptr)));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2025-01-29 10:11:05 +01:00
|
|
|
NonnullRefPtr<PaintingSurface> PaintingSurface::create_with_size(RefPtr<SkiaBackendContext> context, IntSize size, BitmapFormat color_type, AlphaType alpha_type)
|
2024-09-25 15:42:15 +02:00
|
|
|
{
|
|
|
|
auto sk_color_type = to_skia_color_type(color_type);
|
2025-04-28 16:12:30 +02:00
|
|
|
auto sk_alpha_type = to_skia_alpha_type(color_type, alpha_type);
|
2024-12-02 19:00:41 -05:00
|
|
|
auto image_info = SkImageInfo::Make(size.width(), size.height(), sk_color_type, sk_alpha_type, SkColorSpace::MakeSRGB());
|
2024-09-25 15:42:15 +02:00
|
|
|
|
|
|
|
if (!context) {
|
2025-01-29 10:11:05 +01:00
|
|
|
auto bitmap = Bitmap::create(color_type, alpha_type, size).value();
|
2024-09-25 15:42:15 +02:00
|
|
|
auto surface = SkSurfaces::WrapPixels(image_info, bitmap->begin(), bitmap->pitch());
|
|
|
|
VERIFY(surface);
|
2025-04-01 20:18:15 +02:00
|
|
|
return adopt_ref(*new PaintingSurface(make<Impl>(context, size, surface, bitmap)));
|
2024-09-25 15:42:15 +02:00
|
|
|
}
|
|
|
|
|
2025-04-15 02:07:47 +02:00
|
|
|
context->lock();
|
2024-09-25 15:42:15 +02:00
|
|
|
auto surface = SkSurfaces::RenderTarget(context->sk_context(), skgpu::Budgeted::kNo, image_info);
|
|
|
|
VERIFY(surface);
|
2025-04-15 02:07:47 +02:00
|
|
|
context->unlock();
|
2025-04-01 20:18:15 +02:00
|
|
|
return adopt_ref(*new PaintingSurface(make<Impl>(context, size, surface, nullptr)));
|
2024-09-25 15:42:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NonnullRefPtr<PaintingSurface> PaintingSurface::wrap_bitmap(Bitmap& bitmap)
|
|
|
|
{
|
|
|
|
auto color_type = to_skia_color_type(bitmap.format());
|
2025-04-28 16:12:30 +02:00
|
|
|
auto alpha_type = to_skia_alpha_type(bitmap.format(), bitmap.alpha_type());
|
2024-09-25 15:42:15 +02:00
|
|
|
auto size = bitmap.size();
|
2024-12-02 19:00:41 -05:00
|
|
|
auto image_info = SkImageInfo::Make(bitmap.width(), bitmap.height(), color_type, alpha_type, SkColorSpace::MakeSRGB());
|
2024-09-25 15:42:15 +02:00
|
|
|
auto surface = SkSurfaces::WrapPixels(image_info, bitmap.begin(), bitmap.pitch());
|
2025-04-01 20:18:15 +02:00
|
|
|
return adopt_ref(*new PaintingSurface(make<Impl>(RefPtr<SkiaBackendContext> {}, size, surface, bitmap)));
|
2024-09-25 15:42:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef AK_OS_MACOS
|
2025-06-27 04:39:16 +02:00
|
|
|
NonnullRefPtr<PaintingSurface> PaintingSurface::create_from_iosurface(Core::IOSurfaceHandle&& iosurface_handle, NonnullRefPtr<SkiaBackendContext> context, Origin origin)
|
2024-09-25 15:42:15 +02:00
|
|
|
{
|
2025-04-15 02:07:47 +02:00
|
|
|
context->lock();
|
|
|
|
ScopeGuard unlock_guard([&context] {
|
|
|
|
context->unlock();
|
|
|
|
});
|
|
|
|
|
2024-11-29 20:11:20 +01:00
|
|
|
auto metal_texture = context->metal_context().create_texture_from_iosurface(iosurface_handle);
|
|
|
|
IntSize const size { metal_texture->width(), metal_texture->height() };
|
2024-12-02 19:00:41 -05:00
|
|
|
auto image_info = SkImageInfo::Make(size.width(), size.height(), kBGRA_8888_SkColorType, kPremul_SkAlphaType, SkColorSpace::MakeSRGB());
|
2024-09-25 15:42:15 +02:00
|
|
|
GrMtlTextureInfo mtl_info;
|
2024-11-29 20:11:20 +01:00
|
|
|
mtl_info.fTexture = sk_ret_cfp(metal_texture->texture());
|
|
|
|
auto backend_render_target = GrBackendRenderTargets::MakeMtl(metal_texture->width(), metal_texture->height(), mtl_info);
|
2025-08-15 09:25:53 -04:00
|
|
|
auto surface = SkSurfaces::WrapBackendRenderTarget(context->sk_context(), backend_render_target, origin_to_sk_origin(origin), kBGRA_8888_SkColorType, nullptr, nullptr);
|
2025-04-01 20:18:15 +02:00
|
|
|
return adopt_ref(*new PaintingSurface(make<Impl>(context, size, surface, nullptr)));
|
2024-09-25 15:42:15 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PaintingSurface::PaintingSurface(NonnullOwnPtr<Impl>&& impl)
|
|
|
|
: m_impl(move(impl))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2025-04-15 02:07:47 +02:00
|
|
|
PaintingSurface::~PaintingSurface()
|
|
|
|
{
|
|
|
|
lock_context();
|
|
|
|
m_impl->surface = nullptr;
|
|
|
|
unlock_context();
|
|
|
|
}
|
2024-09-25 15:42:15 +02:00
|
|
|
|
2025-01-29 10:11:05 +01:00
|
|
|
void PaintingSurface::read_into_bitmap(Bitmap& bitmap)
|
2024-09-25 15:42:15 +02:00
|
|
|
{
|
|
|
|
auto color_type = to_skia_color_type(bitmap.format());
|
2025-04-28 16:12:30 +02:00
|
|
|
auto alpha_type = to_skia_alpha_type(bitmap.format(), bitmap.alpha_type());
|
2024-12-02 19:00:41 -05:00
|
|
|
auto image_info = SkImageInfo::Make(bitmap.width(), bitmap.height(), color_type, alpha_type, SkColorSpace::MakeSRGB());
|
2024-09-25 15:42:15 +02:00
|
|
|
SkPixmap const pixmap(image_info, bitmap.begin(), bitmap.pitch());
|
|
|
|
m_impl->surface->readPixels(pixmap, 0, 0);
|
|
|
|
}
|
|
|
|
|
2025-01-29 10:11:05 +01:00
|
|
|
void PaintingSurface::write_from_bitmap(Bitmap const& bitmap)
|
2024-11-13 10:44:50 +00:00
|
|
|
{
|
|
|
|
auto color_type = to_skia_color_type(bitmap.format());
|
2025-04-28 16:12:30 +02:00
|
|
|
auto alpha_type = to_skia_alpha_type(bitmap.format(), bitmap.alpha_type());
|
2024-12-02 19:00:41 -05:00
|
|
|
auto image_info = SkImageInfo::Make(bitmap.width(), bitmap.height(), color_type, alpha_type, SkColorSpace::MakeSRGB());
|
2024-11-13 10:44:50 +00:00
|
|
|
SkPixmap const pixmap(image_info, bitmap.begin(), bitmap.pitch());
|
|
|
|
m_impl->surface->writePixels(pixmap, 0, 0);
|
|
|
|
}
|
|
|
|
|
2024-09-25 15:42:15 +02:00
|
|
|
IntSize PaintingSurface::size() const
|
|
|
|
{
|
|
|
|
return m_impl->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
IntRect PaintingSurface::rect() const
|
|
|
|
{
|
|
|
|
return { {}, m_impl->size };
|
|
|
|
}
|
|
|
|
|
|
|
|
SkCanvas& PaintingSurface::canvas() const
|
|
|
|
{
|
|
|
|
return *m_impl->surface->getCanvas();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkSurface& PaintingSurface::sk_surface() const
|
|
|
|
{
|
|
|
|
return *m_impl->surface;
|
|
|
|
}
|
|
|
|
|
2024-11-29 23:15:43 +01:00
|
|
|
void PaintingSurface::notify_content_will_change()
|
|
|
|
{
|
2025-04-15 02:07:47 +02:00
|
|
|
lock_context();
|
2024-11-29 23:15:43 +01:00
|
|
|
m_impl->surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
|
2025-04-15 02:07:47 +02:00
|
|
|
unlock_context();
|
2024-11-29 23:15:43 +01:00
|
|
|
}
|
|
|
|
|
2024-11-09 06:47:16 +01:00
|
|
|
template<>
|
|
|
|
sk_sp<SkImage> PaintingSurface::sk_image_snapshot() const
|
|
|
|
{
|
|
|
|
return m_impl->surface->makeImageSnapshot();
|
|
|
|
}
|
|
|
|
|
2025-01-29 10:24:57 +01:00
|
|
|
void PaintingSurface::flush()
|
2024-09-25 15:42:15 +02:00
|
|
|
{
|
2025-01-29 10:24:57 +01:00
|
|
|
if (on_flush)
|
|
|
|
on_flush(*this);
|
2024-09-25 15:42:15 +02:00
|
|
|
}
|
|
|
|
|
2025-04-01 20:18:15 +02:00
|
|
|
void PaintingSurface::lock_context() const
|
|
|
|
{
|
|
|
|
auto& context = m_impl->context;
|
|
|
|
if (context)
|
|
|
|
context->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PaintingSurface::unlock_context() const
|
|
|
|
{
|
|
|
|
auto& context = m_impl->context;
|
|
|
|
if (context)
|
|
|
|
context->unlock();
|
|
|
|
}
|
|
|
|
|
2024-09-25 15:42:15 +02:00
|
|
|
}
|