From c43377a5a15b5dc283bd45c826461509792be0d1 Mon Sep 17 00:00:00 2001 From: "keldu.magnus" Date: Tue, 27 Oct 2020 05:04:44 +0100 Subject: [PATCH] added new clang format and fixed window creation endless loops + implemented missing functions --- .clang-format | 121 +++++++++++++ driver/device_xcb.cpp | 149 ++++++++-------- driver/device_xcb.h | 42 ++--- driver/gl/gl_context_xcb.cpp | 329 ++++++++++++++++++----------------- driver/gl/gl_context_xcb.h | 36 ++-- driver/gl/gl_window_xcb.cpp | 31 +++- driver/gl/gl_window_xcb.h | 20 ++- driver/window_xcb.cpp | 31 ++-- driver/window_xcb.h | 32 ++-- source/device.h | 14 +- source/gl/gl_context.h | 66 +++---- source/gl/gl_window.h | 14 +- source/video_mode.h | 8 +- source/window.h | 14 +- 14 files changed, 536 insertions(+), 371 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..6205f67 --- /dev/null +++ b/.clang-format @@ -0,0 +1,121 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Right +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: false +IndentPPDirectives: None +IndentWidth: 4 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 4 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 4 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Right +ReflowComments: true +SortIncludes: true +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp11 +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 4 +UseTab: ForContinuationAndIndentation +... + diff --git a/driver/device_xcb.cpp b/driver/device_xcb.cpp index f4a0f07..c8a498b 100644 --- a/driver/device_xcb.cpp +++ b/driver/device_xcb.cpp @@ -5,110 +5,109 @@ #include "window_xcb.h" namespace gin { -XcbDevice::XcbDevice(::Display* display, int screen, - xcb_connection_t* xcb_connection, xcb_screen_t* xcb_screen, - Own&& an) - : display{display}, - screen{screen}, - xcb_connection{xcb_connection}, - xcb_screen{xcb_screen}, - async_notifier{std::move(an)}, - async_conveyor{async_notifier->readReady() - .then([this]() { handleEvents(); }) - .buffer(1)} {} +XcbDevice::XcbDevice(::Display *display, int screen, + xcb_connection_t *xcb_connection, xcb_screen_t *xcb_screen, + Own &&an) + : display{display}, screen{screen}, xcb_connection{xcb_connection}, + xcb_screen{xcb_screen}, async_notifier{std::move(an)}, + async_conveyor{async_notifier->readReady() + .then([this]() { handleEvents(); }) + .buffer(1)} {} XcbDevice::~XcbDevice() { - if (display) { - xcb_flush(xcb_connection); - ::XCloseDisplay(display); - } + if (display) { + xcb_flush(xcb_connection); + ::XCloseDisplay(display); + } } void XcbDevice::windowDestroyed(xcb_window_t window_id) { - windows.erase(window_id); + windows.erase(window_id); } -Own XcbDevice::createXcbWindow(const VideoMode& video_mode, - std::string_view title_view, - int visual_id) { - assert(xcb_screen); - assert(xcb_connection); +void XcbDevice::handleEvents() {} - xcb_colormap_t xcb_colormap = xcb_generate_id(xcb_connection); - xcb_window_t xcb_window = xcb_generate_id(xcb_connection); +Own XcbDevice::createXcbWindow(const VideoMode &video_mode, + std::string_view title_view, + int visual_id) { + assert(xcb_screen); + assert(xcb_connection); - xcb_create_colormap(xcb_connection, XCB_COLORMAP_ALLOC_NONE, xcb_colormap, - xcb_screen->root, visual_id); + xcb_colormap_t xcb_colormap = xcb_generate_id(xcb_connection); + xcb_window_t xcb_window = xcb_generate_id(xcb_connection); - uint32_t eventmask = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS; - uint32_t valuelist[] = {eventmask, xcb_colormap, 0}; - uint32_t valuemask = XCB_CW_EVENT_MASK | XCB_CW_COLORMAP; + xcb_create_colormap(xcb_connection, XCB_COLORMAP_ALLOC_NONE, xcb_colormap, + xcb_screen->root, visual_id); - xcb_create_window(xcb_connection, XCB_COPY_FROM_PARENT, xcb_window, - xcb_screen->root, 0, 0, video_mode.width, video_mode.height, - 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, visual_id, valuemask, - valuelist); + uint32_t eventmask = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS; + uint32_t valuelist[] = {eventmask, xcb_colormap, 0}; + uint32_t valuemask = XCB_CW_EVENT_MASK | XCB_CW_COLORMAP; - xcb_change_property(xcb_connection, XCB_PROP_MODE_REPLACE, xcb_window, - XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, title_view.size(), - title_view.data()); + xcb_create_window(xcb_connection, XCB_COPY_FROM_PARENT, xcb_window, + xcb_screen->root, 0, 0, video_mode.width, + video_mode.height, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, + visual_id, valuemask, valuelist); - xcb_flush(xcb_connection); + xcb_change_property(xcb_connection, XCB_PROP_MODE_REPLACE, xcb_window, + XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, title_view.size(), + title_view.data()); - return heap(*this, xcb_window, xcb_colormap, video_mode, - title_view); + xcb_flush(xcb_connection); + + return heap(*this, xcb_window, xcb_colormap, video_mode, + title_view); } -Own XcbDevice::createWindow(const VideoMode& video_mode, - std::string_view title_view) { - assert(xcb_screen); - return createXcbWindow(video_mode, title_view, xcb_screen->root_visual); +Own XcbDevice::createWindow(const VideoMode &video_mode, + std::string_view title_view) { + assert(xcb_screen); + return createXcbWindow(video_mode, title_view, xcb_screen->root_visual); } void XcbDevice::flush() { - assert(xcb_connection); - xcb_flush(xcb_connection); + assert(xcb_connection); + xcb_flush(xcb_connection); } -Own createXcbDevice(AsyncIoProvider& provider) { - ::Display* display = ::XOpenDisplay(nullptr); - if (!display) { - /// @todo log errors - return nullptr; - } +Own createXcbDevice(AsyncIoProvider &provider) { + ::Display *display = ::XOpenDisplay(nullptr); + if (!display) { + /// @todo log errors + return nullptr; + } - int screen = ::XDefaultScreen(display); + int screen = ::XDefaultScreen(display); - xcb_connection_t* xcb_connection = ::XGetXCBConnection(display); - if (!xcb_connection) { - /// @todo log errors - ::XCloseDisplay(display); - return nullptr; - } + xcb_connection_t *xcb_connection = ::XGetXCBConnection(display); + if (!xcb_connection) { + /// @todo log errors + ::XCloseDisplay(display); + return nullptr; + } - int fd = xcb_get_file_descriptor(xcb_connection); + int fd = xcb_get_file_descriptor(xcb_connection); - Own fd_wrapped = provider.wrapInputFd(fd); - if (!fd_wrapped) { - ::XCloseDisplay(display); - return nullptr; - } + Own fd_wrapped = provider.wrapInputFd(fd); + if (!fd_wrapped) { + ::XCloseDisplay(display); + return nullptr; + } - ::XSetEventQueueOwner(display, XCBOwnsEventQueue); + ::XSetEventQueueOwner(display, XCBOwnsEventQueue); - xcb_screen_iterator_t screen_iter = - xcb_setup_roots_iterator(xcb_get_setup(xcb_connection)); - for (int screen_i = screen; screen_iter.rem && screen_i > 0; - --screen_i, xcb_screen_next(&screen_iter)) - ; + xcb_screen_iterator_t screen_iter = + xcb_setup_roots_iterator(xcb_get_setup(xcb_connection)); + for (int screen_i = screen; screen_iter.rem && screen_i > 0; + --screen_i, xcb_screen_next(&screen_iter)) + ; - xcb_screen_t* xcb_screen = screen_iter.data; + xcb_screen_t *xcb_screen = screen_iter.data; - return heap(display, screen, xcb_connection, xcb_screen, - std::move(fd_wrapped)); + return heap(display, screen, xcb_connection, xcb_screen, + std::move(fd_wrapped)); } -Own createDevice(AsyncIoProvider& provider) { - return createXcbDevice(provider); +Own createDevice(AsyncIoProvider &provider) { + return createXcbDevice(provider); } -} // namespace gin \ No newline at end of file +} // namespace gin \ No newline at end of file diff --git a/driver/device_xcb.h b/driver/device_xcb.h index 4ee101a..a888e09 100644 --- a/driver/device_xcb.h +++ b/driver/device_xcb.h @@ -12,33 +12,33 @@ namespace gin { class XcbDevice final : public Device { - public: - ::Display* display; - int screen; +public: + ::Display *display; + int screen; - xcb_connection_t* xcb_connection; - xcb_screen_t* xcb_screen; + xcb_connection_t *xcb_connection; + xcb_screen_t *xcb_screen; - Own async_notifier; - Conveyor async_conveyor; + Own async_notifier; + Conveyor async_conveyor; - std::map windows; + std::map windows; - public: - XcbDevice(::Display* display, int screen, xcb_connection_t* xcb_connection, - xcb_screen_t* xcb_screen, Own&& an); - ~XcbDevice(); +public: + XcbDevice(::Display *display, int screen, xcb_connection_t *xcb_connection, + xcb_screen_t *xcb_screen, Own &&an); + ~XcbDevice(); - void windowDestroyed(xcb_window_t window_id); - void handleEvents(); + void windowDestroyed(xcb_window_t window_id); + void handleEvents(); - Own createXcbWindow(const VideoMode& mode, - std::string_view title_view, int visual_id); - Own createWindow(const VideoMode& video_mode, - std::string_view title_view) override; + Own createXcbWindow(const VideoMode &mode, + std::string_view title_view, int visual_id); + Own createWindow(const VideoMode &video_mode, + std::string_view title_view) override; - void flush() override; + void flush() override; }; -Own createXcbDevice(AsyncIoProvider& provider); -} // namespace gin +Own createXcbDevice(AsyncIoProvider &provider); +} // namespace gin diff --git a/driver/gl/gl_context_xcb.cpp b/driver/gl/gl_context_xcb.cpp index f6975ba..5c0e87a 100644 --- a/driver/gl/gl_context_xcb.cpp +++ b/driver/gl/gl_context_xcb.cpp @@ -7,178 +7,195 @@ #include #include -#include "device_xcb.h" +#include "../device_xcb.h" +#include "gl_window_xcb.h" namespace gin { namespace { -GlxLibraryExtensions glxLibraryExtensions(const char* extension_string) { - std::string_view extensions_view{extension_string}; - std::set extensions; - while (1) { - size_t n = extensions_view.find_first_not_of(' '); - if (n != extensions_view.npos && n < extensions_view.size()) { - std::string_view sub_glx_ext = extensions_view.substr(0, n); - extensions.insert(sub_glx_ext); - } else { - break; - } - } +GlxLibraryExtensions glxLibraryExtensions(const char *extension_string) { + std::string_view extensions_view{extension_string}; + std::set extensions; + while (1) { + size_t n = extensions_view.find_first_of(' '); + if (n != extensions_view.npos && n < extensions_view.size()) { + std::string_view sub_glx_ext = extensions_view.substr(0, n); + extensions.insert(sub_glx_ext); + extensions_view.remove_prefix(n + 1); + } else { + break; + } + } - auto find = extensions.find("glXCreateContextAttribsARB"); - GLXContext (*glXCreateContextAttribsARB)(Display*, GLXFBConfig, GLXContext, - Bool, const int*) = nullptr; - if (find != extensions.end()) { - glXCreateContextAttribsARB = reinterpret_cast( - glXGetProcAddress( - reinterpret_cast("glXCreateContextAttribsARB"))); - } - return {extensions_view, glXCreateContextAttribsARB}; + auto find = extensions.find("GLX_ARB_create_context"); + GLXContext (*glXCreateContextAttribsARB)(Display *, GLXFBConfig, GLXContext, + Bool, const int *) = nullptr; + if (find != extensions.end()) { + glXCreateContextAttribsARB = reinterpret_cast( + glXGetProcAddress(reinterpret_cast( + "glXCreateContextAttribsARB"))); + } + return {extensions_view, glXCreateContextAttribsARB}; } int translateRenderTypeSetting(GlSettings::RenderType cmp) { - switch (cmp) { - case GlSettings::RenderType::RGBA: - return GLX_RGBA_BIT; - break; - } - return 0; + switch (cmp) { + case GlSettings::RenderType::RGBA: + return GLX_RGBA_BIT; + break; + } + return 0; } int translateDrawableTypeSetting(GlSettings::DrawableType cmp) { - int i = 0; - if (static_cast(cmp) & - static_cast(GlSettings::DrawableType::WindowBit)) { - i |= static_cast(GLX_WINDOW_BIT); - } - if (static_cast(cmp) & - static_cast(GlSettings::DrawableType::PixMapBit)) { - i |= static_cast(GLX_PIXMAP_BIT); - } - if (static_cast(cmp) & - static_cast(GlSettings::DrawableType::PBufferBit)) { - i |= static_cast(GLX_PBUFFER_BIT); - } - return i; + int i = 0; + if (static_cast(cmp) & + static_cast(GlSettings::DrawableType::WindowBit)) { + i |= static_cast(GLX_WINDOW_BIT); + } + if (static_cast(cmp) & + static_cast(GlSettings::DrawableType::PixMapBit)) { + i |= static_cast(GLX_PIXMAP_BIT); + } + if (static_cast(cmp) & + static_cast(GlSettings::DrawableType::PBufferBit)) { + i |= static_cast(GLX_PBUFFER_BIT); + } + return i; } -} // namespace +} // namespace -XcbGlContext::XcbGlContext(const GlxLibraryExtensions& ext_lib, - Own&& dev, int visual_id, - GLXContext context, GLXFBConfig fb_config) - : ext_lib{ext_lib}, - device{std::move(dev)}, - visual_id{visual_id}, - context{context}, - fb_config{fb_config} {} +XcbGlContext::XcbGlContext(const GlxLibraryExtensions &ext_lib, + Own &&dev, int visual_id, + GLXContext context, GLXFBConfig fb_config) + : ext_lib{ext_lib}, device{std::move(dev)}, visual_id{visual_id}, + context{context}, fb_config{fb_config} {} XcbGlContext::~XcbGlContext() { - assert(device); - assert(device->display); - if (context) { - ::glXMakeContextCurrent(device->display, None, None, nullptr); - ::glXDestroyContext(device->display, context); - } - device->flush(); + assert(device); + assert(device->display); + if (context) { + ::glXMakeContextCurrent(device->display, None, None, nullptr); + ::glXDestroyContext(device->display, context); + } + device->flush(); } void XcbGlContext::bind() {} -Own XcbGlContext::createWindow(const VideoMode& video_mode, - std::string_view title_view) { - return nullptr; +Own XcbGlContext::createWindow(const VideoMode &video_mode, + std::string_view title_view) { + assert(device); + if (!device) { + return nullptr; + } + gin::Own window = + device->createXcbWindow(video_mode, title_view, visual_id); + if (!window) { + return nullptr; + } + + ::GLXWindow glx_window = glXCreateWindow(device->display, fb_config, + window->xcb_window, nullptr); + + return gin::heap(std::move(window), *this, glx_window); } -void XcbGlContext::flush() {} - -Own createGlContext(AsyncIoProvider& provider, - const GlSettings& settings) { - Own device = createXcbDevice(provider); - if (!device) { - return nullptr; - } - - /* - * Translate all attributes - */ - std::vector attributes; - attributes.reserve(33); - - attributes.push_back(GLX_X_RENDERABLE); - attributes.push_back(settings.renderable ? True : False); - - attributes.push_back(GLX_RENDER_TYPE); - attributes.push_back(translateRenderTypeSetting(settings.render_type)); - - attributes.push_back(GLX_RED_SIZE); - attributes.push_back(settings.red_bits); - - attributes.push_back(GLX_GREEN_SIZE); - attributes.push_back(settings.green_bits); - - attributes.push_back(GLX_BLUE_SIZE); - attributes.push_back(settings.blue_bits); - - attributes.push_back(GLX_ALPHA_SIZE); - attributes.push_back(settings.alpha_bits); - - attributes.push_back(GLX_DEPTH_SIZE); - attributes.push_back(settings.depth_bits); - - attributes.push_back(GLX_STENCIL_SIZE); - attributes.push_back(settings.stencil_bits); - - attributes.push_back(GLX_DOUBLEBUFFER); - attributes.push_back(settings.double_buffer ? True : False); - - attributes.push_back(GLX_DRAWABLE_TYPE); - attributes.push_back(translateDrawableTypeSetting(settings.drawable_type)); - - attributes.push_back(GLX_X_VISUAL_TYPE); - attributes.push_back(GLX_TRUE_COLOR); - - attributes.push_back(None); - - int num_fb_configs = 0; - - GlxLibraryExtensions lib_ext = glxLibraryExtensions( - glXQueryExtensionsString(device->display, device->screen)); - - GLXFBConfig* fb_configs = glXChooseFBConfig(device->display, device->screen, - &attributes[0], &num_fb_configs); - if (!fb_configs || num_fb_configs == 0) { - /// @todo log errors - return nullptr; - } - ::GLXFBConfig fb_config = fb_configs[0]; - ::XFree(fb_configs); - - ::GLXContext context; - - if (lib_ext.glXCreateContextAttribsARB) { - std::vector glx_attribs; - glx_attribs.reserve(11); - - glx_attribs.push_back(GLX_CONTEXT_MAJOR_VERSION_ARB); - glx_attribs.push_back(settings.gl_major); - glx_attribs.push_back(GLX_CONTEXT_MINOR_VERSION_ARB); - glx_attribs.push_back(settings.gl_minor); - glx_attribs.push_back(GLX_CONTEXT_PROFILE_MASK_ARB); - glx_attribs.push_back(settings.core_profile - ? GLX_CONTEXT_CORE_PROFILE_BIT_ARB - : GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB); - glx_attribs.push_back(None); - - context = lib_ext.glXCreateContextAttribsARB(device->display, fb_config, - NULL, True, &glx_attribs[0]); - if (!context) { - return nullptr; - } - } else { - return nullptr; - } - - int visual_id = 0; - glXGetFBConfigAttrib(device->display, fb_config, GLX_VISUAL_ID, &visual_id); - return heap(lib_ext, std::move(device), visual_id, context, - fb_config); +void XcbGlContext::flush() { + assert(device); + if (device) { + device->flush(); + } } -} // namespace gin + +Own createGlContext(AsyncIoProvider &provider, + const GlSettings &settings) { + Own device = createXcbDevice(provider); + if (!device) { + return nullptr; + } + + /* + * Translate all attributes + */ + std::vector attributes; + attributes.reserve(33); + + attributes.push_back(GLX_X_RENDERABLE); + attributes.push_back(settings.renderable ? True : False); + + attributes.push_back(GLX_RENDER_TYPE); + attributes.push_back(translateRenderTypeSetting(settings.render_type)); + + attributes.push_back(GLX_RED_SIZE); + attributes.push_back(settings.red_bits); + + attributes.push_back(GLX_GREEN_SIZE); + attributes.push_back(settings.green_bits); + + attributes.push_back(GLX_BLUE_SIZE); + attributes.push_back(settings.blue_bits); + + attributes.push_back(GLX_ALPHA_SIZE); + attributes.push_back(settings.alpha_bits); + + attributes.push_back(GLX_DEPTH_SIZE); + attributes.push_back(settings.depth_bits); + + attributes.push_back(GLX_STENCIL_SIZE); + attributes.push_back(settings.stencil_bits); + + attributes.push_back(GLX_DOUBLEBUFFER); + attributes.push_back(settings.double_buffer ? True : False); + + attributes.push_back(GLX_DRAWABLE_TYPE); + attributes.push_back(translateDrawableTypeSetting(settings.drawable_type)); + + attributes.push_back(GLX_X_VISUAL_TYPE); + attributes.push_back(GLX_TRUE_COLOR); + + attributes.push_back(None); + + int num_fb_configs = 0; + + GlxLibraryExtensions lib_ext = glxLibraryExtensions( + glXQueryExtensionsString(device->display, device->screen)); + + GLXFBConfig *fb_configs = glXChooseFBConfig( + device->display, device->screen, &attributes[0], &num_fb_configs); + if (!fb_configs || num_fb_configs == 0) { + /// @todo log errors + return nullptr; + } + ::GLXFBConfig fb_config = fb_configs[0]; + ::XFree(fb_configs); + + ::GLXContext context; + + if (lib_ext.glXCreateContextAttribsARB) { + std::vector glx_attribs; + glx_attribs.reserve(11); + + glx_attribs.push_back(GLX_CONTEXT_MAJOR_VERSION_ARB); + glx_attribs.push_back(settings.gl_major); + glx_attribs.push_back(GLX_CONTEXT_MINOR_VERSION_ARB); + glx_attribs.push_back(settings.gl_minor); + glx_attribs.push_back(GLX_CONTEXT_PROFILE_MASK_ARB); + glx_attribs.push_back(settings.core_profile + ? GLX_CONTEXT_CORE_PROFILE_BIT_ARB + : GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB); + glx_attribs.push_back(None); + + context = lib_ext.glXCreateContextAttribsARB( + device->display, fb_config, NULL, True, &glx_attribs[0]); + if (!context) { + return nullptr; + } + } else { + return nullptr; + } + + int visual_id = 0; + glXGetFBConfigAttrib(device->display, fb_config, GLX_VISUAL_ID, &visual_id); + return heap(lib_ext, std::move(device), visual_id, context, + fb_config); +} +} // namespace gin diff --git a/driver/gl/gl_context_xcb.h b/driver/gl/gl_context_xcb.h index 4fe811a..3fe296e 100644 --- a/driver/gl/gl_context_xcb.h +++ b/driver/gl/gl_context_xcb.h @@ -9,30 +9,30 @@ namespace gin { struct GlxLibraryExtensions { - public: - std::string_view raw_extension_string; +public: + std::string_view raw_extension_string; - GLXContext (*glXCreateContextAttribsARB)(Display*, GLXFBConfig, GLXContext, - Bool, const int*) = nullptr; + GLXContext (*glXCreateContextAttribsARB)(Display *, GLXFBConfig, GLXContext, + Bool, const int *) = nullptr; }; class XcbDevice; class XcbGlContext final : public GlContext { - private: - GlxLibraryExtensions ext_lib; - Own device; - int visual_id; - GLXContext context; - GLXFBConfig fb_config; +public: + GlxLibraryExtensions ext_lib; + Own device; + int visual_id; + GLXContext context; + GLXFBConfig fb_config; - public: - XcbGlContext(const GlxLibraryExtensions&, Own&&, int, GLXContext, - GLXFBConfig); - ~XcbGlContext(); +public: + XcbGlContext(const GlxLibraryExtensions &, Own &&, int, + GLXContext, GLXFBConfig); + ~XcbGlContext(); - void bind() override; - Own createWindow(const VideoMode&, std::string_view) override; + void bind() override; + Own createWindow(const VideoMode &, std::string_view) override; - void flush() override; + void flush() override; }; -} // namespace gin +} // namespace gin diff --git a/driver/gl/gl_window_xcb.cpp b/driver/gl/gl_window_xcb.cpp index e3eaffc..1b38b18 100644 --- a/driver/gl/gl_window_xcb.cpp +++ b/driver/gl/gl_window_xcb.cpp @@ -1,5 +1,32 @@ #include "gl_window_xcb.h" -#include "window_xcb.h" +#include "gl_context_xcb.h" -namespace gin {} \ No newline at end of file +#include "../device_xcb.h" +#include "../window_xcb.h" + +#include + +namespace gin { +XcbGlWindow::XcbGlWindow(Own &&win, XcbGlContext &ctx, + ::GLXWindow glx_win) + : window{std::move(win)}, context{ctx}, glx_window{glx_win} {} + +XcbGlWindow::~XcbGlWindow() { + assert(context.device); + if (context.device) { + glXDestroyWindow(context.device->display, glx_window); + } +} + +void XcbGlWindow::bind() {} + +void XcbGlWindow::show() { + assert(window); + if (window) { + window->show(); + } +} + +void XcbGlWindow::swap() {} +} // namespace gin \ No newline at end of file diff --git a/driver/gl/gl_window_xcb.h b/driver/gl/gl_window_xcb.h index 10e796e..6c889cb 100644 --- a/driver/gl/gl_window_xcb.h +++ b/driver/gl/gl_window_xcb.h @@ -9,14 +9,18 @@ namespace gin { class XcbWindow; class XcbGlContext; class XcbGlWindow final : public GlWindow { - public: - Own window; - XcbGlContext& context; +public: + Own window; + XcbGlContext &context; - ::GLXWindow glx_window; + ::GLXWindow glx_window; - public: - XcbGlWindow(Own&&, XcbGlContext&, ::GLXWindow); - ~XcbGlWindow(); +public: + XcbGlWindow(Own &&, XcbGlContext &, ::GLXWindow); + ~XcbGlWindow(); + + void bind() override; + void swap() override; + void show() override; }; -} // namespace gin \ No newline at end of file +} // namespace gin \ No newline at end of file diff --git a/driver/window_xcb.cpp b/driver/window_xcb.cpp index 62bd132..8c66e04 100644 --- a/driver/window_xcb.cpp +++ b/driver/window_xcb.cpp @@ -5,32 +5,29 @@ #include "device_xcb.h" namespace gin { -XcbWindow::XcbWindow(XcbDevice& device, xcb_window_t xcb_window, - xcb_colormap_t xcb_colormap, const VideoMode& video_mode, - std::string_view title_view) - : device{device}, - xcb_window{xcb_window}, - xcb_colormap{xcb_colormap}, - video_mode{video_mode}, - window_title{title_view} {} +XcbWindow::XcbWindow(XcbDevice &device, xcb_window_t xcb_window, + xcb_colormap_t xcb_colormap, const VideoMode &video_mode, + std::string_view title_view) + : device{device}, xcb_window{xcb_window}, xcb_colormap{xcb_colormap}, + video_mode{video_mode}, window_title{title_view} {} XcbWindow::~XcbWindow() { - device.windowDestroyed(xcb_window); - xcb_destroy_window(device.xcb_connection, xcb_window); - device.flush(); + device.windowDestroyed(xcb_window); + xcb_destroy_window(device.xcb_connection, xcb_window); + device.flush(); } void XcbWindow::show() { - assert(device.xcb_connection); - xcb_map_window(device.xcb_connection, xcb_window); + assert(device.xcb_connection); + xcb_map_window(device.xcb_connection, xcb_window); } void XcbWindow::hide() { - assert(device.xcb_connection); - xcb_unmap_window(device.xcb_connection, xcb_window); + assert(device.xcb_connection); + xcb_unmap_window(device.xcb_connection, xcb_window); } -const VideoMode& XcbWindow::videoMode() const { return video_mode; } +const VideoMode &XcbWindow::videoMode() const { return video_mode; } const std::string_view XcbWindow::title() const { return window_title; } -} // namespace gin +} // namespace gin diff --git a/driver/window_xcb.h b/driver/window_xcb.h index 4ef7d2e..f0f00ee 100644 --- a/driver/window_xcb.h +++ b/driver/window_xcb.h @@ -11,25 +11,25 @@ namespace gin { class XcbDevice; class XcbWindow final : public Window { - public: - XcbDevice& device; +public: + XcbDevice &device; - xcb_window_t xcb_window; - xcb_colormap_t xcb_colormap; + xcb_window_t xcb_window; + xcb_colormap_t xcb_colormap; - VideoMode video_mode; - std::string window_title; + VideoMode video_mode; + std::string window_title; - public: - XcbWindow(XcbDevice& device, xcb_window_t xcb_window, - xcb_colormap_t xcb_colormap, const VideoMode& video_mode, - std::string_view title_view); - ~XcbWindow(); +public: + XcbWindow(XcbDevice &device, xcb_window_t xcb_window, + xcb_colormap_t xcb_colormap, const VideoMode &video_mode, + std::string_view title_view); + ~XcbWindow(); - void show() override; - void hide() override; + void show() override; + void hide() override; - const VideoMode& videoMode() const override; - const std::string_view title() const override; + const VideoMode &videoMode() const override; + const std::string_view title() const override; }; -} // namespace gin \ No newline at end of file +} // namespace gin \ No newline at end of file diff --git a/source/device.h b/source/device.h index c9e853f..7b4bd7b 100644 --- a/source/device.h +++ b/source/device.h @@ -8,14 +8,14 @@ namespace gin { class Device { - public: - virtual ~Device() = default; +public: + virtual ~Device() = default; - virtual Own createWindow(const VideoMode& mode, - std::string_view title_view) = 0; - virtual void flush() = 0; + virtual Own createWindow(const VideoMode &mode, + std::string_view title_view) = 0; + virtual void flush() = 0; }; class AsyncIoProvider; -Own createDevice(AsyncIoProvider& provider); -} // namespace gin +Own createDevice(AsyncIoProvider &provider); +} // namespace gin diff --git a/source/gl/gl_context.h b/source/gl/gl_context.h index 137e3f1..fe3b0a4 100644 --- a/source/gl/gl_context.h +++ b/source/gl/gl_context.h @@ -8,55 +8,55 @@ namespace gin { class GlSettings { - public: - uint8_t gl_major = 3; - uint8_t gl_minor = 3; +public: + uint8_t gl_major = 3; + uint8_t gl_minor = 3; - enum class RenderType : int32_t { RGBA }; + enum class RenderType : int32_t { RGBA }; - RenderType render_type = RenderType::RGBA; + RenderType render_type = RenderType::RGBA; - bool renderable = true; + bool renderable = true; - // gl drawable - bool window_type = true; + // gl drawable + bool window_type = true; - // Pix and PBuffer are currently ignored. + // Pix and PBuffer are currently ignored. - /// @hint don't change this unless you want to change the static cast in - /// glcontext-xcb.cpp and other occurences - /// Alternatively implement bitwise operations & and | - enum class DrawableType : int32_t { - WindowBit = 0x01, - PixMapBit = 0x02, - PBufferBit = 0x04 - }; - DrawableType drawable_type = DrawableType::WindowBit; + /// @hint don't change this unless you want to change the static cast in + /// glcontext-xcb.cpp and other occurences + /// Alternatively implement bitwise operations & and | + enum class DrawableType : int32_t { + WindowBit = 0x01, + PixMapBit = 0x02, + PBufferBit = 0x04 + }; + DrawableType drawable_type = DrawableType::WindowBit; - bool double_buffer = true; + bool double_buffer = true; - int red_bits = 8; - int green_bits = 8; - int blue_bits = 8; - int alpha_bits = 8; + int red_bits = 8; + int green_bits = 8; + int blue_bits = 8; + int alpha_bits = 8; - int depth_bits = 24; + int depth_bits = 24; - int stencil_bits = 8; + int stencil_bits = 8; - bool core_profile = true; + bool core_profile = true; }; class GlContext { - public: - virtual ~GlContext() = default; +public: + virtual ~GlContext() = default; - virtual void bind() = 0; - virtual Own createWindow(const VideoMode&, std::string_view) = 0; + virtual void bind() = 0; + virtual Own createWindow(const VideoMode &, std::string_view) = 0; - virtual void flush() = 0; + virtual void flush() = 0; }; class AsyncIoProvider; -Own createGlContext(AsyncIoProvider&, const GlSettings&); -} // namespace gin +Own createGlContext(AsyncIoProvider &, const GlSettings &); +} // namespace gin diff --git a/source/gl/gl_window.h b/source/gl/gl_window.h index aac2d3c..856b481 100644 --- a/source/gl/gl_window.h +++ b/source/gl/gl_window.h @@ -1,14 +1,14 @@ #pragma once -#include "video_mode.h" +#include "../video_mode.h" namespace gin { class GlWindow { - public: - virtual ~GlWindow() = default; +public: + virtual ~GlWindow() = default; - virtual void bind() = 0; - virtual void show() = 0; - virtual void swap() = 0; + virtual void bind() = 0; + virtual void show() = 0; + virtual void swap() = 0; }; -} // namespace gin +} // namespace gin diff --git a/source/video_mode.h b/source/video_mode.h index 24e578d..dd3d8c6 100644 --- a/source/video_mode.h +++ b/source/video_mode.h @@ -4,8 +4,8 @@ namespace gin { class VideoMode { - public: - size_t height = 128; - size_t width = 128; +public: + size_t height = 128; + size_t width = 128; }; -} // namespace gin +} // namespace gin diff --git a/source/window.h b/source/window.h index 8945410..213aa92 100644 --- a/source/window.h +++ b/source/window.h @@ -8,13 +8,13 @@ namespace gin { class Window { - public: - virtual ~Window() = default; +public: + virtual ~Window() = default; - virtual void show() = 0; - virtual void hide() = 0; + virtual void show() = 0; + virtual void hide() = 0; - virtual const VideoMode& videoMode() const = 0; - virtual const std::string_view title() const = 0; + virtual const VideoMode &videoMode() const = 0; + virtual const std::string_view title() const = 0; }; -} // namespace gin +} // namespace gin