1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-24 11:05:08 +00:00
serenity/Userland/Libraries/LibAccelGfx/Canvas.cpp
Aliaksandr Kalenik 95c154d9bd LibAccelGfx+Meta: Introduce OpenGL painting library
This change introduces a new 2D graphics library that uses OpenGL to
perform painting operations. For now, it has extremely limited
functionality and supports only rectangle painting, but we have to
start somewhere.

Since this library is intended to be used by LibWeb, where the
WebContent process does not have an associated window, painting occurs
in an offscreen buffer created using EGL.

For now it is only possible to compile this library on linux.
Offscreen context creation on SerenityOS and MacOS will have to be
implemented separately in the future.

Co-Authored-By: Andreas Kling <awesomekling@gmail.com>
2023-10-29 17:13:23 +01:00

45 lines
963 B
C++

/*
* Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Canvas.h"
#include <GL/gl.h>
#include <LibGfx/Bitmap.h>
namespace AccelGfx {
Canvas Canvas::create(Context& context, NonnullRefPtr<Gfx::Bitmap> bitmap)
{
VERIFY(bitmap->format() == Gfx::BitmapFormat::BGRA8888);
Canvas canvas { move(bitmap), context };
canvas.initialize();
return canvas;
}
Canvas::Canvas(NonnullRefPtr<Gfx::Bitmap> bitmap, Context& context)
: m_bitmap(move(bitmap))
, m_context(context)
{
}
void Canvas::initialize()
{
m_surface = m_context.create_surface(width(), height());
m_context.set_active_surface(m_surface);
glViewport(0, 0, width(), height());
}
void Canvas::flush()
{
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, width(), height(), GL_BGRA, GL_UNSIGNED_BYTE, m_bitmap->scanline(0));
}
Canvas::~Canvas()
{
m_context.destroy_surface(m_surface);
}
}