<commit_before>// Copyright 2015-2020 <NAME>. All rights reserved.

#include "../../../core/Setup.h"

#if defined(__APPLE__)
#  include <TargetConditionals.h>
#endif

#if TARGET_OS_MAC && !TARGET_OS_IOS && !TARGET_OS_TV && OUZEL_COMPILE_OPENGL

#include "OGLRenderDeviceMacOS.hpp"
#include "OpenGLView.h"
#include "../../../core/Engine.hpp"
#include "../../../core/cocoa/AutoreleasePool.hpp"
#include "../../../core/corevideo/CoreVideoErrorCategory.hpp"
#include "../../../core/macos/NativeWindowMacOS.hpp"
#include "../../../utils/Log.hpp"

namespace ouzel::graphics::opengl::macos
{
    namespace
    {
        CVReturn renderCallback(CVDisplayLinkRef,
                                const CVTimeStamp*,
                                const CVTimeStamp*,
                                CVOptionFlags,
                                CVOptionFlags*,
                                void* userInfo)
        {
            ouzel::core::cocoa::AutoreleasePool autoreleasePool;

            try
            {
                auto renderDevice = static_cast<RenderDevice*>(userInfo);
                renderDevice->renderCallback();
            }
            catch (const std::exception& e)
            {
                logger.log(Log::Level::error) << e.what();
                return kCVReturnError;
            }

            return kCVReturnSuccess;
        }
    }

    RenderDevice::RenderDevice(const Settings& settings,
                               core::Window& initWindow,
                               const std::function<void(const Event&)>& initCallback):
        opengl::RenderDevice(settings, initWindow, initCallback)
    {
        embedded = false;

        constexpr NSOpenGLPixelFormatAttribute openGLVersions[] = {
            NSOpenGLProfileVersion4_1Core,
            NSOpenGLProfileVersion3_2Core,
            NSOpenGLProfileVersionLegacy
        };

        for (const auto openGLVersion : openGLVersions)
        {
            // Create pixel format
            std::vector<NSOpenGLPixelFormatAttribute> attributes = {
                NSOpenGLPFAAccelerated,
                NSOpenGLPFANoRecovery,
                NSOpenGLPFADoubleBuffer,
                NSOpenGLPFAOpenGLProfile, openGLVersion,
                NSOpenGLPFAColorSize, 24,
                NSOpenGLPFAAlphaSize, 8,
                NSOpenGLPFADepthSize, static_cast<NSOpenGLPixelFormatAttribute>(settings.depth ? 24 : 0),
                NSOpenGLPFAStencilSize, static_cast<NSOpenGLPixelFormatAttribute>(settings.stencil ? 8 : 0)
            };

            if (settings.sampleCount)
            {
                attributes.push_back(NSOpenGLPFAMultisample);
                attributes.push_back(NSOpenGLPFASampleBuffers);
                attributes.push_back(1);
                attributes.push_back(NSOpenGLPFASamples);
                attributes.push_back(settings.sampleCount);
            }

            attributes.push_back(0);

            pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes.data()];

            if (pixelFormat)
            {
                switch (openGLVersion)
                {
                    case NSOpenGLProfileVersionLegacy:
                        apiVersion = ApiVersion(2, 0);
                        logger.log(Log::Level::info) << "OpenGL 2 pixel format created";
                        break;
                    case NSOpenGLProfileVersion3_2Core:
                        apiVersion = ApiVersion(3, 2);
                        logger.log(Log::Level::info) << "OpenGL 3.2 pixel format created";
                        break;
                    case NSOpenGLProfileVersion4_1Core:
                        apiVersion = ApiVersion(4, 1);
                        logger.log(Log::Level::info) << "OpenGL 4.1 pixel format created";
                        break;
                }
                break;
            }
        }

        if (!pixelFormat)
            throw std::runtime_error("Failed to crete OpenGL pixel format");

        // Create OpenGL context
        openGLContext = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
        [openGLContext makeCurrentContext];

        auto windowMacOS = static_cast<core::macos::NativeWindow*>(window.getNativeWindow());
        OpenGLView* openGLView = (OpenGLView*)windowMacOS->getNativeView();

        [openGLView setOpenGLContext:openGLContext];
        [openGLContext setView:openGLView];

        const GLint swapInterval = settings.verticalSync ? 1 : 0;
        [openGLContext setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];<commit_msg>Store the api version in the OpenGL version list<commit_after>// Copyright 2015-2020 Elviss Strazdins. All rights reserved.

#include "../../../core/Setup.h"

#if defined(__APPLE__)
#  include <TargetConditionals.h>
#endif

#if TARGET_OS_MAC && !TARGET_OS_IOS && !TARGET_OS_TV && OUZEL_COMPILE_OPENGL

#include <utility>
#include "OGLRenderDeviceMacOS.hpp"
#include "OpenGLView.h"
#include "../../../core/Engine.hpp"
#include "../../../core/cocoa/AutoreleasePool.hpp"
#include "../../../core/corevideo/CoreVideoErrorCategory.hpp"
#include "../../../core/macos/NativeWindowMacOS.hpp"
#include "../../../utils/Log.hpp"

namespace ouzel::graphics::opengl::macos
{
    namespace
    {
        CVReturn renderCallback(CVDisplayLinkRef,
                                const CVTimeStamp*,
                                const CVTimeStamp*,
                                CVOptionFlags,
                                CVOptionFlags*,
                                void* userInfo)
        {
            ouzel::core::cocoa::AutoreleasePool autoreleasePool;

            try
            {
                auto renderDevice = static_cast<RenderDevice*>(userInfo);
                renderDevice->renderCallback();
            }
            catch (const std::exception& e)
            {
                logger.log(Log::Level::error) << e.what();
                return kCVReturnError;
            }

            return kCVReturnSuccess;
        }
    }

    RenderDevice::RenderDevice(const Settings& settings,
                               core::Window& initWindow,
                               const std::function<void(const Event&)>& initCallback):
        opengl::RenderDevice(settings, initWindow, initCallback)
    {
        embedded = false;

        constexpr std::pair<NSOpenGLPixelFormatAttribute, ApiVersion> openGLVersions[] = {
            {NSOpenGLProfileVersion4_1Core, ApiVersion(4, 1)},
            {NSOpenGLProfileVersion3_2Core, ApiVersion(3, 2)},
            {NSOpenGLProfileVersionLegacy, ApiVersion(2, 0)}
        };

        for (const auto openGLVersion : openGLVersions)
        {
            // Create pixel format
            std::vector<NSOpenGLPixelFormatAttribute> attributes = {
                NSOpenGLPFAAccelerated,
                NSOpenGLPFANoRecovery,
                NSOpenGLPFADoubleBuffer,
                NSOpenGLPFAOpenGLProfile, openGLVersion.first,
                NSOpenGLPFAColorSize, 24,
                NSOpenGLPFAAlphaSize, 8,
                NSOpenGLPFADepthSize, static_cast<NSOpenGLPixelFormatAttribute>(settings.depth ? 24 : 0),
                NSOpenGLPFAStencilSize, static_cast<NSOpenGLPixelFormatAttribute>(settings.stencil ? 8 : 0)
            };

            if (settings.sampleCount)
            {
                attributes.push_back(NSOpenGLPFAMultisample);
                attributes.push_back(NSOpenGLPFASampleBuffers);
                attributes.push_back(1);
                attributes.push_back(NSOpenGLPFASamples);
                attributes.push_back(settings.sampleCount);
            }

            attributes.push_back(0);

            pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes.data()];

            if (pixelFormat)
            {
                apiVersion = openGLVersion.second;
                logger.log(Log::Level::info) << "OpenGL " << apiVersion.v[0] << '.' << apiVersion.v[1] << " pixel format created";
                break;
            }
        }

        if (!pixelFormat)
            throw std::runtime_error("Failed to crete OpenGL pixel format");

        // Create OpenGL context
        openGLContext = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
        [openGLContext makeCurrentContext];

        auto windowMacOS = static_cast<core::macos::NativeWindow*>(window.getNativeWindow());
        OpenGLView* openGLView = (OpenGLView*)windowMacOS->getNativeView();

        [openGLView setOpenGLContext:openGLContext];
        [openGLContext setView:openGLView];

        const GLint swapInterval = settings.verticalSync ? 1 : 0;
        [openGLContext setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];
{'fraction_non_alphanumeric': 0.09972552607502287, 'fraction_numerical': 0.008119853613906679, 'mean_word_length': 1.8880449141347424, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 24, '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': '3221546', 'n_tokens_mistral': 2437, 'n_tokens_neox': 2270, 'n_words': 390}