2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-03-05 14:23:08 +02:00
|
|
|
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2021-03-05 14:23:08 +02:00
|
|
|
#include <AK/Checked.h>
|
2021-09-22 17:13:12 +03:00
|
|
|
#include <AK/Try.h>
|
2021-03-05 14:23:08 +02:00
|
|
|
#include <Kernel/Debug.h>
|
2021-09-11 09:19:20 +03:00
|
|
|
#include <Kernel/Devices/DeviceManagement.h>
|
2021-03-05 14:23:08 +02:00
|
|
|
#include <Kernel/Graphics/FramebufferDevice.h>
|
|
|
|
#include <Kernel/Graphics/GraphicsManagement.h>
|
2021-08-06 10:45:34 +02:00
|
|
|
#include <Kernel/Memory/AnonymousVMObject.h>
|
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
2019-08-18 14:54:52 +10:00
|
|
|
#include <Kernel/Process.h>
|
2021-06-22 17:40:16 +02:00
|
|
|
#include <Kernel/Sections.h>
|
2019-08-18 14:54:52 +10:00
|
|
|
#include <LibC/errno_numbers.h>
|
|
|
|
#include <LibC/sys/ioctl_numbers.h>
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-09-22 17:13:12 +03:00
|
|
|
NonnullRefPtr<FramebufferDevice> FramebufferDevice::create(const GenericGraphicsAdapter& adapter, PhysicalAddress paddr, size_t width, size_t height, size_t pitch)
|
2021-05-22 09:51:55 +03:00
|
|
|
{
|
2021-09-22 17:13:12 +03:00
|
|
|
auto framebuffer_device_or_error = DeviceManagement::try_create_device<FramebufferDevice>(adapter, paddr, width, height, pitch);
|
2021-09-10 14:44:46 +03:00
|
|
|
// FIXME: Find a way to propagate errors
|
|
|
|
VERIFY(!framebuffer_device_or_error.is_error());
|
|
|
|
return framebuffer_device_or_error.release_value();
|
2021-05-22 09:51:55 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<Memory::Region*> FramebufferDevice::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared)
|
2019-08-18 14:54:52 +10:00
|
|
|
{
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker lock(m_activation_lock);
|
2020-01-12 02:17:30 +01:00
|
|
|
REQUIRE_PROMISE(video);
|
2020-02-28 20:47:27 +01:00
|
|
|
if (!shared)
|
2021-01-20 23:11:17 +01:00
|
|
|
return ENODEV;
|
2021-01-27 21:23:20 -07:00
|
|
|
if (offset != 0)
|
|
|
|
return ENXIO;
|
2021-09-22 17:13:12 +03:00
|
|
|
auto framebuffer_length = TRY(buffer_length(0));
|
|
|
|
if (range.size() != Memory::page_round_up(framebuffer_length))
|
2021-01-27 21:23:20 -07:00
|
|
|
return EOVERFLOW;
|
|
|
|
|
2021-09-22 17:13:12 +03:00
|
|
|
m_userspace_real_framebuffer_vmobject = TRY(Memory::AnonymousVMObject::try_create_for_physical_range(m_framebuffer_address, Memory::page_round_up(framebuffer_length)));
|
|
|
|
m_real_framebuffer_vmobject = TRY(Memory::AnonymousVMObject::try_create_for_physical_range(m_framebuffer_address, Memory::page_round_up(framebuffer_length)));
|
|
|
|
m_swapped_framebuffer_vmobject = TRY(Memory::AnonymousVMObject::try_create_with_size(Memory::page_round_up(framebuffer_length), AllocationStrategy::AllocateNow));
|
|
|
|
m_real_framebuffer_region = TRY(MM.allocate_kernel_region_with_vmobject(*m_real_framebuffer_vmobject, Memory::page_round_up(framebuffer_length), "Framebuffer", Memory::Region::Access::ReadWrite));
|
|
|
|
m_swapped_framebuffer_region = TRY(MM.allocate_kernel_region_with_vmobject(*m_swapped_framebuffer_vmobject, Memory::page_round_up(framebuffer_length), "Framebuffer Swap (Blank)", Memory::Region::Access::ReadWrite));
|
2021-05-18 21:34:22 +03:00
|
|
|
|
2021-08-06 13:49:36 +02:00
|
|
|
RefPtr<Memory::VMObject> chosen_vmobject;
|
2021-05-20 21:28:18 +03:00
|
|
|
if (m_graphical_writes_enabled) {
|
|
|
|
chosen_vmobject = m_real_framebuffer_vmobject;
|
|
|
|
} else {
|
|
|
|
chosen_vmobject = m_swapped_framebuffer_vmobject;
|
|
|
|
}
|
2021-09-06 02:09:42 +02:00
|
|
|
m_userspace_framebuffer_region = TRY(process.address_space().allocate_region_with_vmobject(
|
2021-01-25 14:52:36 +01:00
|
|
|
range,
|
2021-05-20 21:28:18 +03:00
|
|
|
chosen_vmobject.release_nonnull(),
|
2019-08-18 14:54:52 +10:00
|
|
|
0,
|
2021-03-05 14:23:08 +02:00
|
|
|
"Framebuffer",
|
2021-01-02 16:38:05 +01:00
|
|
|
prot,
|
2021-09-06 02:09:42 +02:00
|
|
|
shared));
|
|
|
|
return m_userspace_framebuffer_region;
|
2021-04-16 22:58:51 +03:00
|
|
|
}
|
|
|
|
|
2021-05-21 06:53:31 +03:00
|
|
|
void FramebufferDevice::deactivate_writes()
|
2021-04-16 22:58:51 +03:00
|
|
|
{
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker lock(m_activation_lock);
|
2021-04-16 22:58:51 +03:00
|
|
|
if (!m_userspace_framebuffer_region)
|
|
|
|
return;
|
2021-09-22 17:13:12 +03:00
|
|
|
auto framebuffer_length_or_error = buffer_length(0);
|
|
|
|
VERIFY(!framebuffer_length_or_error.is_error());
|
|
|
|
memcpy(m_swapped_framebuffer_region->vaddr().as_ptr(), m_real_framebuffer_region->vaddr().as_ptr(), Memory::page_round_up(framebuffer_length_or_error.release_value()));
|
2021-04-16 22:58:51 +03:00
|
|
|
auto vmobject = m_swapped_framebuffer_vmobject;
|
|
|
|
m_userspace_framebuffer_region->set_vmobject(vmobject.release_nonnull());
|
|
|
|
m_userspace_framebuffer_region->remap();
|
2021-05-20 21:28:18 +03:00
|
|
|
m_graphical_writes_enabled = false;
|
2021-04-16 22:58:51 +03:00
|
|
|
}
|
|
|
|
void FramebufferDevice::activate_writes()
|
|
|
|
{
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker lock(m_activation_lock);
|
2021-04-16 22:58:51 +03:00
|
|
|
if (!m_userspace_framebuffer_region || !m_real_framebuffer_vmobject)
|
|
|
|
return;
|
|
|
|
// restore the image we had in the void area
|
|
|
|
// FIXME: if we happen to have multiple Framebuffers that are writing to that location
|
|
|
|
// we will experience glitches...
|
2021-09-22 17:13:12 +03:00
|
|
|
auto framebuffer_length_or_error = buffer_length(0);
|
|
|
|
VERIFY(!framebuffer_length_or_error.is_error());
|
|
|
|
|
|
|
|
memcpy(m_real_framebuffer_region->vaddr().as_ptr(), m_swapped_framebuffer_region->vaddr().as_ptr(), Memory::page_round_up(framebuffer_length_or_error.release_value()));
|
2021-04-16 22:58:51 +03:00
|
|
|
auto vmobject = m_userspace_real_framebuffer_vmobject;
|
|
|
|
m_userspace_framebuffer_region->set_vmobject(vmobject.release_nonnull());
|
|
|
|
m_userspace_framebuffer_region->remap();
|
2021-05-20 21:28:18 +03:00
|
|
|
m_graphical_writes_enabled = true;
|
2019-08-18 14:54:52 +10:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
UNMAP_AFTER_INIT ErrorOr<void> FramebufferDevice::try_to_initialize()
|
2021-04-16 22:58:51 +03:00
|
|
|
{
|
2021-08-15 09:07:59 +00:00
|
|
|
// FIXME: Would be nice to be able to unify this with mmap above, but this
|
|
|
|
// function is UNMAP_AFTER_INIT for the time being.
|
2021-09-22 17:13:12 +03:00
|
|
|
auto framebuffer_length = TRY(buffer_length(0));
|
|
|
|
m_real_framebuffer_vmobject = TRY(Memory::AnonymousVMObject::try_create_for_physical_range(m_framebuffer_address, Memory::page_round_up(framebuffer_length)));
|
|
|
|
m_swapped_framebuffer_vmobject = TRY(Memory::AnonymousVMObject::try_create_with_size(Memory::page_round_up(framebuffer_length), AllocationStrategy::AllocateNow));
|
|
|
|
m_real_framebuffer_region = TRY(MM.allocate_kernel_region_with_vmobject(*m_real_framebuffer_vmobject, Memory::page_round_up(framebuffer_length), "Framebuffer", Memory::Region::Access::ReadWrite));
|
|
|
|
m_swapped_framebuffer_region = TRY(MM.allocate_kernel_region_with_vmobject(*m_swapped_framebuffer_vmobject, Memory::page_round_up(framebuffer_length), "Framebuffer Swap (Blank)", Memory::Region::Access::ReadWrite));
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2021-04-16 22:58:51 +03:00
|
|
|
}
|
|
|
|
|
2021-09-22 17:13:12 +03:00
|
|
|
UNMAP_AFTER_INIT FramebufferDevice::FramebufferDevice(const GenericGraphicsAdapter& adapter, PhysicalAddress addr, size_t width, size_t height, size_t pitch)
|
|
|
|
: GenericFramebufferDevice(adapter)
|
2021-03-05 14:23:08 +02:00
|
|
|
, m_framebuffer_address(addr)
|
|
|
|
, m_framebuffer_pitch(pitch)
|
|
|
|
, m_framebuffer_width(width)
|
|
|
|
, m_framebuffer_height(height)
|
|
|
|
{
|
2021-04-16 22:58:51 +03:00
|
|
|
VERIFY(!m_framebuffer_address.is_null());
|
|
|
|
VERIFY(m_framebuffer_pitch);
|
|
|
|
VERIFY(m_framebuffer_width);
|
|
|
|
VERIFY(m_framebuffer_height);
|
2021-03-05 14:23:08 +02:00
|
|
|
dbgln("Framebuffer {}: address={}, pitch={}, width={}, height={}", minor(), addr, pitch, width, height);
|
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<size_t> FramebufferDevice::buffer_length(size_t head) const
|
2021-03-05 14:23:08 +02:00
|
|
|
{
|
2021-09-22 17:13:12 +03:00
|
|
|
// Note: This FramebufferDevice class doesn't support multihead setup.
|
|
|
|
// We take care to verify this at the GenericFramebufferDevice::ioctl method
|
|
|
|
// so if we happen to accidentally have a value different than 0, assert.
|
|
|
|
VERIFY(head == 0);
|
|
|
|
MutexLocker locker(m_resolution_lock);
|
|
|
|
auto adapter = m_graphics_adapter.strong_ref();
|
|
|
|
if (!adapter)
|
2021-11-08 00:51:39 +01:00
|
|
|
return Error::from_errno(EIO);
|
2021-09-22 17:13:12 +03:00
|
|
|
if (adapter->double_framebuffering_capable())
|
2021-05-22 09:51:55 +03:00
|
|
|
return m_framebuffer_pitch * m_framebuffer_height * 2;
|
|
|
|
return m_framebuffer_pitch * m_framebuffer_height;
|
2021-03-05 14:23:08 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<size_t> FramebufferDevice::pitch(size_t head) const
|
2019-08-18 14:54:52 +10:00
|
|
|
{
|
2021-09-22 17:13:12 +03:00
|
|
|
// Note: This FramebufferDevice class doesn't support multihead setup.
|
|
|
|
// We take care to verify this at the GenericFramebufferDevice::ioctl method
|
|
|
|
// so if we happen to accidentally have a value different than 0, assert.
|
|
|
|
VERIFY(head == 0);
|
|
|
|
MutexLocker locker(m_resolution_lock);
|
|
|
|
return m_framebuffer_pitch;
|
|
|
|
}
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<size_t> FramebufferDevice::height(size_t head) const
|
2021-09-22 17:13:12 +03:00
|
|
|
{
|
|
|
|
// Note: This FramebufferDevice class doesn't support multihead setup.
|
|
|
|
// We take care to verify this at the GenericFramebufferDevice::ioctl method
|
|
|
|
// so if we happen to accidentally have a value different than 0, assert.
|
|
|
|
VERIFY(head == 0);
|
|
|
|
MutexLocker locker(m_resolution_lock);
|
|
|
|
return m_framebuffer_height;
|
|
|
|
}
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<size_t> FramebufferDevice::width(size_t head) const
|
2021-09-22 17:13:12 +03:00
|
|
|
{
|
|
|
|
// Note: This FramebufferDevice class doesn't support multihead setup.
|
|
|
|
// We take care to verify this at the GenericFramebufferDevice::ioctl method
|
|
|
|
// so if we happen to accidentally have a value different than 0, assert.
|
|
|
|
VERIFY(head == 0);
|
|
|
|
MutexLocker locker(m_resolution_lock);
|
|
|
|
return m_framebuffer_width;
|
|
|
|
}
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<size_t> FramebufferDevice::vertical_offset(size_t head) const
|
2021-09-22 17:13:12 +03:00
|
|
|
{
|
|
|
|
// Note: This FramebufferDevice class doesn't support multihead setup.
|
|
|
|
// We take care to verify this at the GenericFramebufferDevice::ioctl method
|
|
|
|
// so if we happen to accidentally have a value different than 0, assert.
|
|
|
|
VERIFY(head == 0);
|
|
|
|
MutexLocker locker(m_buffer_offset_lock);
|
|
|
|
return m_y_offset;
|
|
|
|
}
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<bool> FramebufferDevice::vertical_offseted(size_t head) const
|
2021-09-22 17:13:12 +03:00
|
|
|
{
|
|
|
|
// Note: This FramebufferDevice class doesn't support multihead setup.
|
|
|
|
// We take care to verify this at the GenericFramebufferDevice::ioctl method
|
|
|
|
// so if we happen to accidentally have a value different than 0, assert.
|
|
|
|
VERIFY(head == 0);
|
|
|
|
MutexLocker locker(m_buffer_offset_lock);
|
|
|
|
return m_y_offset == 0 ? 0 : 1;
|
|
|
|
}
|
2021-05-22 09:51:55 +03:00
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> FramebufferDevice::set_head_resolution(size_t head, size_t width, size_t height, size_t)
|
2021-09-22 17:13:12 +03:00
|
|
|
{
|
|
|
|
// Note: This FramebufferDevice class doesn't support multihead setup.
|
|
|
|
// We take care to verify this at the GenericFramebufferDevice::ioctl method
|
|
|
|
// so if we happen to accidentally have a value different than 0, assert.
|
|
|
|
VERIFY(head == 0);
|
|
|
|
MutexLocker buffer_offset_locker(m_buffer_offset_lock);
|
|
|
|
MutexLocker resolution_locker(m_resolution_lock);
|
|
|
|
auto adapter = m_graphics_adapter.strong_ref();
|
|
|
|
if (!adapter)
|
2021-11-08 00:51:39 +01:00
|
|
|
return Error::from_errno(EIO);
|
2021-09-22 17:13:12 +03:00
|
|
|
auto result = adapter->try_to_set_resolution(0, width, height);
|
2021-11-08 00:51:39 +01:00
|
|
|
// FIXME: Find a better way to return here a ErrorOr<void>.
|
2021-09-22 17:13:12 +03:00
|
|
|
if (!result)
|
2021-11-08 00:51:39 +01:00
|
|
|
return Error::from_errno(ENOTSUP);
|
2021-09-22 17:13:12 +03:00
|
|
|
m_framebuffer_width = width;
|
|
|
|
m_framebuffer_height = height;
|
|
|
|
m_framebuffer_pitch = width * sizeof(u32);
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2021-09-22 17:13:12 +03:00
|
|
|
}
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> FramebufferDevice::set_head_buffer(size_t head, bool second_buffer)
|
2021-09-22 17:13:12 +03:00
|
|
|
{
|
|
|
|
// Note: This FramebufferDevice class doesn't support multihead setup.
|
|
|
|
// We take care to verify this at the GenericFramebufferDevice::ioctl method
|
|
|
|
// so if we happen to accidentally have a value different than 0, assert.
|
|
|
|
VERIFY(head == 0);
|
|
|
|
MutexLocker locker(m_buffer_offset_lock);
|
|
|
|
auto adapter = m_graphics_adapter.strong_ref();
|
|
|
|
if (!adapter)
|
2021-11-08 00:51:39 +01:00
|
|
|
return Error::from_errno(EIO);
|
2021-09-22 17:13:12 +03:00
|
|
|
if (second_buffer) {
|
|
|
|
if (!adapter->set_y_offset(0, m_framebuffer_height)) {
|
2021-11-08 00:51:39 +01:00
|
|
|
// FIXME: Find a better ErrorOr<void> here.
|
|
|
|
return Error::from_errno(ENOTSUP);
|
2021-05-22 09:51:55 +03:00
|
|
|
}
|
2021-09-22 17:13:12 +03:00
|
|
|
m_y_offset = m_framebuffer_height;
|
|
|
|
} else {
|
|
|
|
if (!adapter->set_y_offset(0, 0)) {
|
2021-11-08 00:51:39 +01:00
|
|
|
// FIXME: Find a better ErrorOr<void> here.
|
|
|
|
return Error::from_errno(ENOTSUP);
|
2021-05-22 09:51:55 +03:00
|
|
|
}
|
2021-09-22 17:13:12 +03:00
|
|
|
m_y_offset = 0;
|
2019-08-18 14:54:52 +10:00
|
|
|
}
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2021-09-22 17:13:12 +03:00
|
|
|
}
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> FramebufferDevice::flush_head_buffer(size_t)
|
2021-09-22 17:13:12 +03:00
|
|
|
{
|
|
|
|
// Note: This FramebufferDevice class doesn't support flushing.
|
|
|
|
// We take care to verify this at the GenericFramebufferDevice::ioctl method
|
|
|
|
// so if we happen to accidentally reach this code, assert.
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> FramebufferDevice::flush_rectangle(size_t, FBRect const&)
|
2021-09-22 17:13:12 +03:00
|
|
|
{
|
|
|
|
// Note: This FramebufferDevice class doesn't support partial flushing.
|
|
|
|
// We take care to verify this at the GenericFramebufferDevice::ioctl method
|
|
|
|
// so if we happen to accidentally reach this code, assert.
|
|
|
|
VERIFY_NOT_REACHED();
|
2019-08-18 14:54:52 +10:00
|
|
|
}
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
}
|