Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/macos/embedded_gl_manager.h
10277 views
1
/**************************************************************************/
2
/* embedded_gl_manager.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#if defined(MACOS_ENABLED) && defined(GLES3_ENABLED)
34
35
#include "core/os/os.h"
36
#include "core/templates/local_vector.h"
37
#include "servers/display_server.h"
38
39
#import <AppKit/AppKit.h>
40
#import <ApplicationServices/ApplicationServices.h>
41
#import <CoreVideo/CoreVideo.h>
42
43
GODOT_CLANG_WARNING_PUSH_AND_IGNORE("-Wdeprecated-declarations") // OpenGL is deprecated in macOS 10.14.
44
45
typedef CGLContextObj (*CGLGetCurrentContextPtr)(void);
46
typedef CGLError (*CGLTexImageIOSurface2DPtr)(CGLContextObj ctx, GLenum target, GLenum internal_format,
47
GLsizei width, GLsizei height, GLenum format, GLenum type, IOSurfaceRef ioSurface, GLuint plane);
48
typedef const char *(*CGLErrorStringPtr)(CGLError);
49
50
class GLManagerEmbedded {
51
/// @brief The number of framebuffers to create for each window.
52
///
53
/// Triple-buffering is used to avoid stuttering.
54
static constexpr uint32_t BUFFER_COUNT = 3;
55
56
// The display ID for which vsync is used. If this value is -1, vsync is disabled.
57
constexpr static uint32_t INVALID_DISPLAY_ID = static_cast<uint32_t>(-1);
58
59
struct FrameBuffer {
60
IOSurfaceRef surface = nullptr;
61
unsigned int tex = 0;
62
unsigned int fbo = 0;
63
};
64
65
struct GLWindow {
66
uint32_t width = 0;
67
uint32_t height = 0;
68
CALayer *layer = nullptr;
69
NSOpenGLContext *context = nullptr;
70
FrameBuffer framebuffers[BUFFER_COUNT];
71
uint32_t current_fb = 0;
72
bool is_valid = false;
73
74
void destroy_framebuffers();
75
76
~GLWindow() { destroy_framebuffers(); }
77
};
78
79
RBMap<DisplayServer::WindowID, GLWindow> windows;
80
typedef RBMap<DisplayServer::WindowID, GLWindow>::Element GLWindowElement;
81
82
NSOpenGLContext *shared_context = nullptr;
83
DisplayServer::WindowID current_window = DisplayServer::INVALID_WINDOW_ID;
84
85
Error create_context(GLWindow &p_win);
86
87
bool framework_loaded = false;
88
CGLGetCurrentContextPtr CGLGetCurrentContext = nullptr;
89
CGLTexImageIOSurface2DPtr CGLTexImageIOSurface2D = nullptr;
90
CGLErrorStringPtr CGLErrorString = nullptr;
91
92
uint32_t display_id = INVALID_DISPLAY_ID;
93
CVDisplayLinkRef display_link = nullptr;
94
bool vsync_enabled = false;
95
bool display_link_running = false;
96
dispatch_semaphore_t display_semaphore = nullptr;
97
98
void create_display_link();
99
void release_display_link();
100
101
public:
102
Error window_create(DisplayServer::WindowID p_window_id, CALayer *p_layer, int p_width, int p_height);
103
void window_destroy(DisplayServer::WindowID p_window_id);
104
void window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height);
105
Size2i window_get_size(DisplayServer::WindowID p_window_id) const;
106
107
void set_display_id(uint32_t p_display_id);
108
void set_vsync_enabled(bool p_enabled);
109
bool is_vsync_enabled() const { return vsync_enabled; }
110
111
void release_current();
112
void swap_buffers();
113
114
void window_make_current(DisplayServer::WindowID p_window_id);
115
116
Error initialize();
117
118
GLManagerEmbedded();
119
~GLManagerEmbedded();
120
};
121
122
GODOT_CLANG_WARNING_PUSH
123
124
#endif // MACOS_ENABLED && GLES3_ENABLED
125
126