AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
OpenGLRenderingContextGtk.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12#pragma once
   13
   14#include "RenderingContextGtk.h"
   15#include <AUI/Platform/OpenGLRenderingContext.h>
   16#include "gtk_functions.h"
   17
   18class OpenGLRenderingContextGtk: public OpenGLRenderingContext, public RenderingContextGtk {
   19public:
   20    OpenGLRenderingContextGtk(const ARenderingContextOptions::OpenGL& config, AWindowBase& window)
   21      : OpenGLRenderingContext(config), RenderingContextGtk(window) {}
   22
   23    ~OpenGLRenderingContextGtk() override = default;
   24    void init(const Init& init) override;
   25    void destroyNativeWindow(AWindowBase& window) override;
   26    void gtkRealize(aui::gtk4_fake::GtkWidget* widget) override;
   27    void gtkSnapshot(aui::gtk4_fake::GtkWidget* widget, aui::gtk4_fake::GtkSnapshot* snapshot) override;
   28    void gtkUnrealize(aui::gtk4_fake::GtkWidget* widget) override;
   29    void beginResize(AWindowBase& window) override;
   30    void endResize(AWindowBase& window) override;
   31
   32    auto contextScope() {
   33        using namespace aui::gtk4_fake;
   34        auto prev = gdk_gl_context_get_current();
   35        auto ctx = ourContext;
   36        if (ctx != nullptr) {
   37            AUI_ASSERT(prev != ctx);
   38            gdk_gl_context_make_current(ctx);
   39        }
   40        return aui::ptr::make_unique_with_deleter(ctx, [prev](GdkGLContext*) {
   41            gdk_gl_context_clear_current();
   42            gdk_gl_context_make_current(prev);
   43        });
   44    }
   45    void gtkDoUnderContext(const std::function<void()>& callback) override; // this is soo bad
   46
   47private:
   48    struct Texture {
   49        aui::gtk4_fake::GdkGLTextureBuilder* builder {};
   50        aui::gtk4_fake::GdkTexture* gl_texture {};
   51        aui::gtk4_fake::GdkTexture* dmabuf_texture {};
   52
   53        ~Texture();
   54    };
   55    AOptional<Texture> mTexture;
   56    static aui::gtk4_fake::GdkGLContext* ourContext;
   57    GLuint mFramebufferForGtk = 0;
   58    bool mNeedsResize = false;
   59    bool mHaveBuffers = false;
   60    aui::gtk4_fake::GdkGLAPI mAllowedApis = static_cast<aui::gtk4_fake::GdkGLAPI>(aui::gtk4_fake::GDK_GL_API_GL | aui::gtk4_fake::GDK_GL_API_GLES);
   61
   62    void realCreateContext(aui::gtk4_fake::GtkWidget* widget);
   63    void ensureTexture(aui::gtk4_fake::GtkWidget* widget);
   64    void allocateTexture(aui::gtk4_fake::GtkWidget* widget);
   65    void ensureBuffers(aui::gtk4_fake::GtkWidget* widget);
   66    void attachBuffers(aui::gtk4_fake::GtkWidget* widget);
   67    void deleteBuffers();
   68    void deleteTextures();
   69
   70protected:
   71    void endFramebuffer() override;
   72};
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition AOptional.h:33
Definition AWindowBase.h:33
#define AUI_ASSERT(condition)
Asserts that the passed condition evaluates to true.
Definition Assert.h:55
Provides a fake implementation of GTK4 types and constants, so we wont depend on the gtk4 dev package...
Definition gtk_functions.h:16
Definition ARenderingContextOptions.h:55
Definition IRenderingContext.h:38
static _unique< T, Deleter > make_unique_with_deleter(T *ptr, Deleter deleter=Deleter{})
Creates unique_ptr from raw pointer and a deleter.
Definition SharedPtrTypes.h:126