Path: blob/master/platform/macos/embedded_gl_manager.h
10277 views
/**************************************************************************/1/* embedded_gl_manager.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#if defined(MACOS_ENABLED) && defined(GLES3_ENABLED)3334#include "core/os/os.h"35#include "core/templates/local_vector.h"36#include "servers/display_server.h"3738#import <AppKit/AppKit.h>39#import <ApplicationServices/ApplicationServices.h>40#import <CoreVideo/CoreVideo.h>4142GODOT_CLANG_WARNING_PUSH_AND_IGNORE("-Wdeprecated-declarations") // OpenGL is deprecated in macOS 10.14.4344typedef CGLContextObj (*CGLGetCurrentContextPtr)(void);45typedef CGLError (*CGLTexImageIOSurface2DPtr)(CGLContextObj ctx, GLenum target, GLenum internal_format,46GLsizei width, GLsizei height, GLenum format, GLenum type, IOSurfaceRef ioSurface, GLuint plane);47typedef const char *(*CGLErrorStringPtr)(CGLError);4849class GLManagerEmbedded {50/// @brief The number of framebuffers to create for each window.51///52/// Triple-buffering is used to avoid stuttering.53static constexpr uint32_t BUFFER_COUNT = 3;5455// The display ID for which vsync is used. If this value is -1, vsync is disabled.56constexpr static uint32_t INVALID_DISPLAY_ID = static_cast<uint32_t>(-1);5758struct FrameBuffer {59IOSurfaceRef surface = nullptr;60unsigned int tex = 0;61unsigned int fbo = 0;62};6364struct GLWindow {65uint32_t width = 0;66uint32_t height = 0;67CALayer *layer = nullptr;68NSOpenGLContext *context = nullptr;69FrameBuffer framebuffers[BUFFER_COUNT];70uint32_t current_fb = 0;71bool is_valid = false;7273void destroy_framebuffers();7475~GLWindow() { destroy_framebuffers(); }76};7778RBMap<DisplayServer::WindowID, GLWindow> windows;79typedef RBMap<DisplayServer::WindowID, GLWindow>::Element GLWindowElement;8081NSOpenGLContext *shared_context = nullptr;82DisplayServer::WindowID current_window = DisplayServer::INVALID_WINDOW_ID;8384Error create_context(GLWindow &p_win);8586bool framework_loaded = false;87CGLGetCurrentContextPtr CGLGetCurrentContext = nullptr;88CGLTexImageIOSurface2DPtr CGLTexImageIOSurface2D = nullptr;89CGLErrorStringPtr CGLErrorString = nullptr;9091uint32_t display_id = INVALID_DISPLAY_ID;92CVDisplayLinkRef display_link = nullptr;93bool vsync_enabled = false;94bool display_link_running = false;95dispatch_semaphore_t display_semaphore = nullptr;9697void create_display_link();98void release_display_link();99100public:101Error window_create(DisplayServer::WindowID p_window_id, CALayer *p_layer, int p_width, int p_height);102void window_destroy(DisplayServer::WindowID p_window_id);103void window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height);104Size2i window_get_size(DisplayServer::WindowID p_window_id) const;105106void set_display_id(uint32_t p_display_id);107void set_vsync_enabled(bool p_enabled);108bool is_vsync_enabled() const { return vsync_enabled; }109110void release_current();111void swap_buffers();112113void window_make_current(DisplayServer::WindowID p_window_id);114115Error initialize();116117GLManagerEmbedded();118~GLManagerEmbedded();119};120121GODOT_CLANG_WARNING_PUSH122123#endif // MACOS_ENABLED && GLES3_ENABLED124125126