Implement SerenityOSGLContext

This commit is contained in:
mrkubax10 2023-07-28 16:10:39 +00:00
parent ce4f65782f
commit c4d9df60d9
6 changed files with 143 additions and 3 deletions

View File

@ -207,6 +207,11 @@ if(BUILD_CLIENT)
list(APPEND CLIENT_SOURCES list(APPEND CLIENT_SOURCES
src/game/window/serenityos_window.cpp src/game/window/serenityos_window.cpp
) )
if(RENDERER_LEGACY_GL)
list(APPEND CLIENT_SOURCES
src/game/renderer/gl/serenityos_gl_context.cpp
)
endif()
endif() endif()
endif() endif()
if(BUILD_SERVER) if(BUILD_SERVER)
@ -263,7 +268,7 @@ if(BUILD_CLIENT)
set(RENDERER_GL_LIBRARIES OpenGL::GL) set(RENDERER_GL_LIBRARIES OpenGL::GL)
if(SERENITYOS) if(SERENITYOS)
set(OPENGL_INCLUDE_DIR "/usr/include/LibGL") set(OPENGL_INCLUDE_DIR "/usr/include/LibGL")
set(RENDERER_GL_LIBRARIES "gl") set(RENDERER_GL_LIBRARIES "gl;gfx")
set(OPENGL_LIBRARIES "gl") set(OPENGL_LIBRARIES "gl")
endif() endif()
if(NOT OPENGL_INCLUDE_DIR OR NOT OPENGL_LIBRARIES) if(NOT OPENGL_INCLUDE_DIR OR NOT OPENGL_LIBRARIES)

View File

@ -73,7 +73,6 @@ GLContext* GLContext::create_context(window::Window* window) {
case window::WindowType::WINDOW_TYPE_WAYLAND: case window::WindowType::WINDOW_TYPE_WAYLAND:
return nullptr; return nullptr;
case window::WindowType::WINDOW_TYPE_SERENITYOS: case window::WindowType::WINDOW_TYPE_SERENITYOS:
// TODO
return nullptr; return nullptr;
} }
return nullptr; return nullptr;

View File

@ -0,0 +1,39 @@
/*
PolyGun
Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "game/renderer/gl/serenityos_gl_context.hpp"
#include "game/window/serenityos_window.hpp"
using namespace polygun::renderer;
SerenityOSGLContext::SerenityOSGLContext(Gfx::Bitmap* framebuffer) :
m_context(MUST(GL::create_context(*framebuffer)))
{
GL::make_context_current(m_context);
}
SerenityOSGLContext::~SerenityOSGLContext() {
GL::make_context_current(nullptr);
}

View File

@ -0,0 +1,46 @@
/*
PolyGun
Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef POLYGUN_RENDERER_SERENITYOS_GL_CONTEXT_HPP
#define POLYGUN_RENDERER_SERENITYOS_GL_CONTEXT_HPP
#include "game/renderer/gl/gl_context.hpp"
#define AK_DONT_REPLACE_STD 1
#define USING_AK_GLOBALLY 1
#include <LibGL/GLContext.h>
#include <LibGfx/Bitmap.h>
namespace polygun::renderer {
class SerenityOSGLContext final : public GLContext {
public:
SerenityOSGLContext(Gfx::Bitmap* framebuffer);
virtual ~SerenityOSGLContext() override;
private:
AK::OwnPtr<GL::GLContext> m_context;
};
}
#endif // POLYGUN_RENDERER_SERENITYOS_GL_CONTEXT_HPP

View File

@ -26,6 +26,9 @@ SOFTWARE.
#include <LibGUI/Application.h> #include <LibGUI/Application.h>
#include <LibGUI/Window.h> #include <LibGUI/Window.h>
#if defined(RENDERER_LEGACY_GL)
#include <LibGUI/Painter.h>
#endif
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <map> #include <map>
@ -212,6 +215,15 @@ SerenityOSWindowFrame::SerenityOSWindowFrame(SerenityOSWindow* window) :
m_first_resize_event(true) m_first_resize_event(true)
{} {}
void SerenityOSWindowFrame::paint_event(GUI::PaintEvent& ev) {
GUI::Frame::paint_event(ev);
GUI::Painter painter(*this);
painter.add_clip_rect(ev.rect());
m_window->m_window_mutex.lock();
painter.draw_scaled_bitmap(frame_inner_rect(), *m_window->m_framebuffer, m_window->m_framebuffer->rect());
m_window->m_window_mutex.unlock();
}
void SerenityOSWindowFrame::resize_event(GUI::ResizeEvent& ev) { void SerenityOSWindowFrame::resize_event(GUI::ResizeEvent& ev) {
GUI::Frame::resize_event(ev); GUI::Frame::resize_event(ev);
if(m_first_resize_event) { if(m_first_resize_event) {
@ -224,6 +236,9 @@ void SerenityOSWindowFrame::resize_event(GUI::ResizeEvent& ev) {
event.m_data.m_window_event.m_height = ev.size().height(); event.m_data.m_window_event.m_height = ev.size().height();
m_window->m_window_mutex.lock(); m_window->m_window_mutex.lock();
m_window->push_event(event); m_window->push_event(event);
#if defined(RENDERER_LEGACY_GL)
m_window->m_gl_context = m_window->create_gl_context(ev.size().width(), ev.size().height());
#endif
m_window->m_window_mutex.unlock(); m_window->m_window_mutex.unlock();
} }
@ -324,10 +339,17 @@ void SerenityOSWindowFrame::mouse_button_event(GUI::MouseEvent& ev) {
} }
SerenityOSWindow::SerenityOSWindow(const std::string& title, unsigned width, unsigned height) : SerenityOSWindow::SerenityOSWindow(const std::string& title, unsigned width, unsigned height) :
#if defined(RENDERER_LEGACY_GL)
m_gl_context(nullptr),
#endif
m_window_thread(MUST(Threading::Thread::try_create(window_thread_func))), m_window_thread(MUST(Threading::Thread::try_create(window_thread_func))),
m_window_mutex() m_window_mutex(),
m_framebuffer()
{ {
m_type = WindowType::WINDOW_TYPE_SERENITYOS; m_type = WindowType::WINDOW_TYPE_SERENITYOS;
#if defined(RENDERER_LEGACY_GL)
m_gl_context = create_gl_context(width, height);
#endif
WindowParams params { WindowParams params {
title, title,
width, width,
@ -345,6 +367,15 @@ SerenityOSWindow::~SerenityOSWindow() {
(void)m_window_thread->join(); (void)m_window_thread->join();
} }
#if defined(RENDERER_LEGACY_GL)
polygun::renderer::SerenityOSGLContext* SerenityOSWindow::create_gl_context(unsigned width, unsigned height) {
if(m_gl_context)
delete m_gl_context;
m_framebuffer = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, Gfx::IntSize{width, height}));
return new renderer::SerenityOSGLContext(m_framebuffer.ptr());
}
#endif
size_t SerenityOSWindow::poll_events(Event& event) { size_t SerenityOSWindow::poll_events(Event& event) {
m_window_mutex.lock(); m_window_mutex.lock();
size_t result = Window::poll_events(event); size_t result = Window::poll_events(event);

View File

@ -32,6 +32,13 @@ SOFTWARE.
#include <LibThreading/Thread.h> #include <LibThreading/Thread.h>
#include <LibThreading/Mutex.h> #include <LibThreading/Mutex.h>
#include <LibGUI/Frame.h> #include <LibGUI/Frame.h>
#if defined(RENDERERL_LEGACY_GL)
#include <LibGfx/Bitmap.h>
#endif
#if defined(RENDERER_LEGACY_GL)
#include "game/renderer/gl/serenityos_gl_context.hpp"
#endif
namespace polygun::window { namespace polygun::window {
class SerenityOSWindow final : public Window { class SerenityOSWindow final : public Window {
@ -40,19 +47,32 @@ namespace polygun::window {
SerenityOSWindow(const std::string& title, unsigned width, unsigned height); SerenityOSWindow(const std::string& title, unsigned width, unsigned height);
virtual ~SerenityOSWindow() override; virtual ~SerenityOSWindow() override;
#if defined(RENDERER_LEGACY_GL)
renderer::SerenityOSGLContext* create_gl_context(unsigned width, unsigned height);
#endif
virtual size_t poll_events(Event& event) override; virtual size_t poll_events(Event& event) override;
virtual void grab_mouse(bool grab) override; virtual void grab_mouse(bool grab) override;
#if defined(RENDERER_LEGACY_GL)
virtual bool window_creates_gl_context() const override { return true; }
virtual renderer::GLContext* get_window_gl_context() override { return m_gl_context; }
#endif
private: private:
#if defined(RENDERER_LEGACY_GL)
renderer::SerenityOSGLContext* m_gl_context;
#endif
AK::RefPtr<Threading::Thread> m_window_thread; AK::RefPtr<Threading::Thread> m_window_thread;
// Note: Cannot use ThreadSafe util class because SerenityOSWindow doesn't use std::thread // Note: Cannot use ThreadSafe util class because SerenityOSWindow doesn't use std::thread
Threading::Mutex m_window_mutex; Threading::Mutex m_window_mutex;
AK::RefPtr<Gfx::Bitmap> m_framebuffer;
}; };
class SerenityOSWindowFrame final : public GUI::Frame { class SerenityOSWindowFrame final : public GUI::Frame {
public: public:
SerenityOSWindowFrame(SerenityOSWindow* window); SerenityOSWindowFrame(SerenityOSWindow* window);
virtual void paint_event(GUI::PaintEvent& ev) override;
virtual void resize_event(GUI::ResizeEvent& ev) override; virtual void resize_event(GUI::ResizeEvent& ev) override;
virtual void keydown_event(GUI::KeyEvent& ev) override; virtual void keydown_event(GUI::KeyEvent& ev) override;
virtual void keyup_event(GUI::KeyEvent& ev) override; virtual void keyup_event(GUI::KeyEvent& ev) override;