Blender V4.5
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(stereoVisual),
27 m_window(window),
28 m_hidden_window(nullptr),
29 m_contextProfileMask(contextProfileMask),
30 m_contextMajorVersion(contextMajorVersion),
31 m_contextMinorVersion(contextMinorVersion),
32 m_contextFlags(contextFlags),
33 m_contextResetNotificationStrategy(contextResetNotificationStrategy),
34 m_context(nullptr)
35{
36 // assert(m_window != nullptr);
37}
38
40{
41 if (m_context == nullptr) {
42 return;
43 }
44
45 if (m_window != nullptr && m_context == SDL_GL_GetCurrentContext()) {
46 SDL_GL_MakeCurrent(m_window, nullptr);
47 }
48 if (m_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(m_context);
57 }
58
59 if (m_hidden_window != nullptr) {
60 SDL_DestroyWindow(m_hidden_window);
61 }
62}
63
65{
66 SDL_GL_SwapWindow(m_window);
67
68 return GHOST_kSuccess;
69}
70
72{
73 if (m_context == nullptr) {
74 return GHOST_kFailure;
75 }
76 active_context_ = this;
77 return SDL_GL_MakeCurrent(m_window, m_context) ? GHOST_kSuccess : GHOST_kFailure;
78}
79
81{
82 if (m_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, m_contextProfileMask);
95 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, m_contextMajorVersion);
96 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, m_contextMinorVersion);
97 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, m_contextFlags);
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 (m_stereoVisual) {
110 SDL_GL_SetAttribute(SDL_GL_STEREO, 1);
111 }
112
113 if (m_window == nullptr) {
114 m_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 m_window = m_hidden_window;
123 }
124
125 m_context = SDL_GL_CreateContext(m_window);
126
127 GHOST_TSuccess success;
128
129 if (m_context != nullptr) {
130 if (!s_sharedContext) {
131 s_sharedContext = m_context;
132 }
133 s_sharedCount++;
134
135 success = (SDL_GL_MakeCurrent(m_window, m_context) < 0) ? GHOST_kFailure : GHOST_kSuccess;
136
137 initClearGL();
138 SDL_GL_SwapWindow(m_window);
139
140 active_context_ = this;
141 success = GHOST_kSuccess;
142 }
143 else {
144 success = GHOST_kFailure;
145 }
146
147 return success;
148}
149
151{
152 m_window = nullptr;
153
154 return GHOST_kSuccess;
155}
156
158{
159 if (SDL_GL_SetSwapInterval(interval) == -1) {
160 return GHOST_kFailure;
161 }
162 return GHOST_kSuccess;
163}
164
166{
167 intervalOut = SDL_GL_GetSwapInterval();
168 return GHOST_kSuccess;
169}
GHOST_TSuccess
Definition GHOST_Types.h:80
@ GHOST_kFailure
Definition GHOST_Types.h:80
@ GHOST_kSuccess
Definition GHOST_Types.h:80
GHOST_TSuccess swapBuffers() override
~GHOST_ContextSDL() override
GHOST_TSuccess releaseDrawingContext() override
GHOST_TSuccess getSwapInterval(int &intervalOut) override
GHOST_TSuccess initializeDrawingContext() override
GHOST_TSuccess activateDrawingContext() override
GHOST_TSuccess releaseNativeHandles() override
GHOST_ContextSDL(bool stereoVisual, SDL_Window *window, int contextProfileMask, int contextMajorVersion, int contextMinorVersion, int contextFlags, int contextResetNotificationStrategy)
GHOST_TSuccess setSwapInterval(int interval) override
static GHOST_Context * active_context_
GHOST_Context(bool stereoVisual)
#define assert(assertion)