| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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/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") |
| 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 |
|
| 52 |
|
| 53 |
|
| 54 |
static constexpr uint32_t BUFFER_COUNT = 3; |
| 55 |
|
| 56 |
|
| 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 |