Blender V5.0
GHOST_ContextSDL.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2014 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "GHOST_ContextSDL.hh"
12
13#include <cassert>
14#include <cstring>
15
16SDL_GLContext GHOST_ContextSDL::s_sharedContext = nullptr;
17int GHOST_ContextSDL::s_sharedCount = 0;
18
20 SDL_Window *window,
21 int contextProfileMask,
22 int contextMajorVersion,
23 int contextMinorVersion,
24 int contextFlags,
25 int contextResetNotificationStrategy)
26 : GHOST_Context(context_params),
27 window_(window),
28 hidden_window_(nullptr),
29 context_profile_mask_(contextProfileMask),
30 context_major_version_(contextMajorVersion),
31 context_minor_version_(contextMinorVersion),
32 context_flags_(contextFlags),
33 context_reset_notification_strategy_(contextResetNotificationStrategy),
34 context_(nullptr)
35{
36 // assert(window_ != nullptr);
37}
38
40{
41 if (context_ == nullptr) {
42 return;
43 }
44
45 if (window_ != nullptr && context_ == SDL_GL_GetCurrentContext()) {
46 SDL_GL_MakeCurrent(window_, nullptr);
47 }
48 if (context_ != s_sharedContext || s_sharedCount == 1) {
49 assert(s_sharedCount > 0);
50
51 s_sharedCount--;
52
53 if (s_sharedCount == 0) {
54 s_sharedContext = nullptr;
55 }
56 SDL_GL_DeleteContext(context_);
57 }
58
59 if (hidden_window_ != nullptr) {
60 SDL_DestroyWindow(hidden_window_);
61 }
62}
63
65{
66 SDL_GL_SwapWindow(window_);
67
68 return GHOST_kSuccess;
69}
70
72{
73 if (context_ == nullptr) {
74 return GHOST_kFailure;
75 }
76 active_context_ = this;
77 return SDL_GL_MakeCurrent(window_, context_) ? GHOST_kSuccess : GHOST_kFailure;
78}
79
81{
82 if (context_ == nullptr) {
83 return GHOST_kFailure;
84 }
85 active_context_ = nullptr;
86 /* Untested, may not work. */
87 return SDL_GL_MakeCurrent(nullptr, nullptr) ? GHOST_kSuccess : GHOST_kFailure;
88}
89
91{
92 const bool needAlpha = false;
93
94 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, context_profile_mask_);
95 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, context_major_version_);
96 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, context_minor_version_);
97 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, context_flags_);
98
99 SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
100 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
101 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
102 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
103 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
104
105 if (needAlpha) {
106 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
107 }
108
109 if (context_params_.is_stereo_visual) {
110 SDL_GL_SetAttribute(SDL_GL_STEREO, 1);
111 }
112
113 if (window_ == nullptr) {
114 hidden_window_ = SDL_CreateWindow("Offscreen Context Windows",
115 SDL_WINDOWPOS_UNDEFINED,
116 SDL_WINDOWPOS_UNDEFINED,
117 1,
118 1,
119 SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS |
120 SDL_WINDOW_HIDDEN);
121
122 window_ = hidden_window_;
123 }
124
125 context_ = SDL_GL_CreateContext(window_);
126
127 GHOST_TSuccess success;
128
129 if (context_ != nullptr) {
130 if (!s_sharedContext) {
131 s_sharedContext = context_;
132 }
133 s_sharedCount++;
134
135 success = (SDL_GL_MakeCurrent(window_, context_) < 0) ? GHOST_kFailure : GHOST_kSuccess;
136
137 {
138 const GHOST_TVSyncModes vsync = getVSync();
139 if (vsync != GHOST_kVSyncModeUnset) {
140 setSwapInterval(int(vsync));
141 }
142 }
143
144 initClearGL();
145 SDL_GL_SwapWindow(window_);
146
147 active_context_ = this;
148 success = GHOST_kSuccess;
149 }
150 else {
151 success = GHOST_kFailure;
152 }
153
154 return success;
155}
156
158{
159 window_ = nullptr;
160
161 return GHOST_kSuccess;
162}
163
165{
166 if (SDL_GL_SetSwapInterval(interval) == -1) {
167 return GHOST_kFailure;
168 }
169 return GHOST_kSuccess;
170}
171
173{
174 interval_out = SDL_GL_GetSwapInterval();
175 return GHOST_kSuccess;
176}
GHOST_TSuccess
Definition GHOST_Types.h:57
@ GHOST_kFailure
Definition GHOST_Types.h:57
@ GHOST_kSuccess
Definition GHOST_Types.h:57
GHOST_TVSyncModes
@ GHOST_kVSyncModeUnset
~GHOST_ContextSDL() override
GHOST_TSuccess swapBufferRelease() override
GHOST_TSuccess releaseDrawingContext() override
GHOST_ContextSDL(const GHOST_ContextParams &context_params, SDL_Window *window, int contextProfileMask, int contextMajorVersion, int contextMinorVersion, int contextFlags, int contextResetNotificationStrategy)
GHOST_TSuccess initializeDrawingContext() override
GHOST_TSuccess activateDrawingContext() override
GHOST_TSuccess releaseNativeHandles() override
GHOST_TSuccess setSwapInterval(int interval) override
GHOST_TSuccess getSwapInterval(int &interval_out) override
GHOST_Context(const GHOST_ContextParams &context_params)
static GHOST_Context * active_context_
virtual GHOST_TVSyncModes getVSync()
GHOST_ContextParams context_params_
#define assert(assertion)