<commit_before>/*
 * LinuxGLContext.cpp
 * 
 * This file is part of the "LLGL" project (Copyright (c) 2015 by <NAME>)
 * See "LICENSE.txt" for license information.
 */

#include "LinuxGLContext.h"
#include "../../Ext/GLExtensions.h"
#include "../../Ext/GLExtensionLoader.h"
#include "../../../CheckedCast.h"
#include "../../../../Core/Helper.h"
#include <LLGL/Log.h>
#include <algorithm>


namespace LLGL
{


#ifndef GLX_CONTEXT_MAJOR_VERSION_ARB
#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
#endif

#ifndef GLX_CONTEXT_MINOR_VERSION_ARB
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
#endif

typedef GLXContext (*GXLCREATECONTEXTATTRIBARBPROC)(Display*, GLXFBConfig, GLXContext, Bool, const int*);


/*
 * GLContext class
 */

std::unique_ptr<GLContext> GLContext::Create(RenderContextDescriptor& desc, Surface& surface, GLContext* sharedContext)
{
    LinuxGLContext* sharedContextGLX = (sharedContext != nullptr ? LLGL_CAST(LinuxGLContext*, sharedContext) : nullptr);
    return MakeUnique<LinuxGLContext>(desc, surface, sharedContextGLX);
}


/*
 * LinuxGLContext class
 */

LinuxGLContext::LinuxGLContext(RenderContextDescriptor& desc, Surface& surface, LinuxGLContext* sharedContext) :
    GLContext { sharedContext }
{
    NativeHandle nativeHandle;
    surface.GetNativeHandle(&nativeHandle);
    
    if (sharedContext)
    {
        auto sharedContextGLX = LLGL_CAST(LinuxGLContext*, sharedContext);
        CreateContext(desc, nativeHandle, sharedContextGLX);
    }
    else
        CreateContext(desc, nativeHandle, nullptr);
}

LinuxGLContext::~LinuxGLContext()
{
    DeleteContext();
}

bool LinuxGLContext::SetSwapInterval(int interval)
{
    /* Load GL extension "glXSwapIntervalSGI" to set v-sync interval */
    if (glXSwapIntervalSGI || LoadSwapIntervalProcs())
        return (glXSwapIntervalSGI(interval) == 0);
    else
        return false;
}

bool LinuxGLContext::SwapBuffers()
{
    glXSwapBuffers(display_, wnd_);
    return true;
}

void LinuxGLContext::Resize(const Size& resolution)
{
    //TODO...
}


/*
 * ======= Private: =======
 */

bool LinuxGLContext::Activate(bool activate)
{
    if (activate)
        return glXMakeCurrent(display_, wnd_, glc_);
    else
        return glXMakeCurrent(nullptr, 0, 0);
}

void LinuxGLContext::CreateContext(const RenderContextDescriptor& contextDesc, const NativeHandle& nativeHandle, LinuxGLContext* sharedContext)
{
    GLXContext glcShared = (sharedContext != nullptr ? sharedContext->glc_ : nullptr);
    
    /* Get X11 display, window, and visual information */
    display_    = nativeHandle.display;
    wnd_        = nativeHandle.window;
    visual_     = nativeHandle.visual;
    
    if (!display_ || !wnd_ || !visual_)
        throw std::invalid_argument("failed to create OpenGL context on X11 client, due to missing arguments");
    
    /* Create OpenGL context with X11 lib */
    const auto& profileDesc = contextDesc.profileOpenGL;
    
    if (profileDesc.extProfile && profileDesc.coreProfile)
    {
        /* Create core profile */
        int major = GetMajorVersion(profileDesc.version);
        int minor = GetMinorVersion(profileDesc.version);
        Log::StdOut() << "major = " << major << ", minor = " << minor << std::endl;
        glc_ = CreateContextCoreProfile(glcShared, major, minor);
    }
    
    if (!glc_)<commit_msg>Removed test code from Linux port.<commit_after>/*
 * LinuxGLContext.cpp
 * 
 * This file is part of the "LLGL" project (Copyright (c) 2015 by <NAME>)
 * See "LICENSE.txt" for license information.
 */

#include "LinuxGLContext.h"
#include "../../Ext/GLExtensions.h"
#include "../../Ext/GLExtensionLoader.h"
#include "../../../CheckedCast.h"
#include "../../../../Core/Helper.h"
#include <LLGL/Log.h>
#include <algorithm>


namespace LLGL
{


#ifndef GLX_CONTEXT_MAJOR_VERSION_ARB
#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
#endif

#ifndef GLX_CONTEXT_MINOR_VERSION_ARB
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
#endif

typedef GLXContext (*GXLCREATECONTEXTATTRIBARBPROC)(Display*, GLXFBConfig, GLXContext, Bool, const int*);


/*
 * GLContext class
 */

std::unique_ptr<GLContext> GLContext::Create(RenderContextDescriptor& desc, Surface& surface, GLContext* sharedContext)
{
    LinuxGLContext* sharedContextGLX = (sharedContext != nullptr ? LLGL_CAST(LinuxGLContext*, sharedContext) : nullptr);
    return MakeUnique<LinuxGLContext>(desc, surface, sharedContextGLX);
}


/*
 * LinuxGLContext class
 */

LinuxGLContext::LinuxGLContext(RenderContextDescriptor& desc, Surface& surface, LinuxGLContext* sharedContext) :
    GLContext { sharedContext }
{
    NativeHandle nativeHandle;
    surface.GetNativeHandle(&nativeHandle);
    
    if (sharedContext)
    {
        auto sharedContextGLX = LLGL_CAST(LinuxGLContext*, sharedContext);
        CreateContext(desc, nativeHandle, sharedContextGLX);
    }
    else
        CreateContext(desc, nativeHandle, nullptr);
}

LinuxGLContext::~LinuxGLContext()
{
    DeleteContext();
}

bool LinuxGLContext::SetSwapInterval(int interval)
{
    /* Load GL extension "glXSwapIntervalSGI" to set v-sync interval */
    if (glXSwapIntervalSGI || LoadSwapIntervalProcs())
        return (glXSwapIntervalSGI(interval) == 0);
    else
        return false;
}

bool LinuxGLContext::SwapBuffers()
{
    glXSwapBuffers(display_, wnd_);
    return true;
}

void LinuxGLContext::Resize(const Size& resolution)
{
    //TODO...
}


/*
 * ======= Private: =======
 */

bool LinuxGLContext::Activate(bool activate)
{
    if (activate)
        return glXMakeCurrent(display_, wnd_, glc_);
    else
        return glXMakeCurrent(nullptr, 0, 0);
}

void LinuxGLContext::CreateContext(const RenderContextDescriptor& contextDesc, const NativeHandle& nativeHandle, LinuxGLContext* sharedContext)
{
    GLXContext glcShared = (sharedContext != nullptr ? sharedContext->glc_ : nullptr);
    
    /* Get X11 display, window, and visual information */
    display_    = nativeHandle.display;
    wnd_        = nativeHandle.window;
    visual_     = nativeHandle.visual;
    
    if (!display_ || !wnd_ || !visual_)
        throw std::invalid_argument("failed to create OpenGL context on X11 client, due to missing arguments");
    
    /* Create OpenGL context with X11 lib */
    const auto& profileDesc = contextDesc.profileOpenGL;
    
    if (profileDesc.extProfile && profileDesc.coreProfile)
    {
        /* Create core profile */
        int major = GetMajorVersion(profileDesc.version);
        int minor = GetMinorVersion(profileDesc.version);
        glc_ = CreateContextCoreProfile(glcShared, major, minor);
    }
    
    if (!glc_)
{'fraction_non_alphanumeric': 0.11940524958276437, 'fraction_numerical': 0.006979214079805796, 'mean_word_length': 3.8721359940872135, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1886777', 'n_tokens_mistral': 2063, 'n_tokens_neox': 2018, 'n_words': 456}