Blender V4.3
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
11#include "GHOST_ContextSDL.hh"
12
13#include <vector>
14
15#include <cassert>
16#include <cstdio>
17#include <cstring>
18
19SDL_GLContext GHOST_ContextSDL::s_sharedContext = nullptr;
20int GHOST_ContextSDL::s_sharedCount = 0;
21
23 SDL_Window *window,
24 int contextProfileMask,
25 int contextMajorVersion,
26 int contextMinorVersion,
27 int contextFlags,
28 int contextResetNotificationStrategy)
29 : GHOST_Context(stereoVisual),
30 m_window(window),
31 m_hidden_window(nullptr),
32 m_contextProfileMask(contextProfileMask),
33 m_contextMajorVersion(contextMajorVersion),
34 m_contextMinorVersion(contextMinorVersion),
35 m_contextFlags(contextFlags),
36 m_contextResetNotificationStrategy(contextResetNotificationStrategy),
37 m_context(nullptr)
38{
39 // assert(m_window != nullptr);
40}
41
43{
44 if (m_context == nullptr) {
45 return;
46 }
47
48 if (m_window != nullptr && m_context == SDL_GL_GetCurrentContext()) {
49 SDL_GL_MakeCurrent(m_window, nullptr);
50 }
51 if (m_context != s_sharedContext || s_sharedCount == 1) {
52 assert(s_sharedCount > 0);
53
54 s_sharedCount--;
55
56 if (s_sharedCount == 0) {
57 s_sharedContext = nullptr;
58 }
59 SDL_GL_DeleteContext(m_context);
60 }
61
62 if (m_hidden_window != nullptr) {
63 SDL_DestroyWindow(m_hidden_window);
64 }
65}
66
68{
69 SDL_GL_SwapWindow(m_window);
70
71 return GHOST_kSuccess;
72}
73
75{
76 if (m_context == nullptr) {
77 return GHOST_kFailure;
78 }
79 return SDL_GL_MakeCurrent(m_window, m_context) ? GHOST_kSuccess : GHOST_kFailure;
80}
81
83{
84 if (m_context == nullptr) {
85 return GHOST_kFailure;
86 }
87 /* Untested, may not work. */
88 return SDL_GL_MakeCurrent(nullptr, nullptr) ? GHOST_kSuccess : GHOST_kFailure;
89}
90
92{
93 const bool needAlpha = false;
94
95 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, m_contextProfileMask);
96 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, m_contextMajorVersion);
97 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, m_contextMinorVersion);
98 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, m_contextFlags);
99
100 SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
101 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
102 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
103 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
104 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
105
106 if (needAlpha) {
107 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
108 }
109
110 if (m_stereoVisual) {
111 SDL_GL_SetAttribute(SDL_GL_STEREO, 1);
112 }
113
114 if (m_window == nullptr) {
115 m_hidden_window = SDL_CreateWindow("Offscreen Context Windows",
116 SDL_WINDOWPOS_UNDEFINED,
117 SDL_WINDOWPOS_UNDEFINED,
118 1,
119 1,
120 SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS |
121 SDL_WINDOW_HIDDEN);
122
123 m_window = m_hidden_window;
124 }
125
126 m_context = SDL_GL_CreateContext(m_window);
127
128 GHOST_TSuccess success;
129
130 if (m_context != nullptr) {
131 if (!s_sharedContext) {
132 s_sharedContext = m_context;
133 }
134 s_sharedCount++;
135
136 success = (SDL_GL_MakeCurrent(m_window, m_context) < 0) ? GHOST_kFailure : GHOST_kSuccess;
137
138 initClearGL();
139 SDL_GL_SwapWindow(m_window);
140
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:87
@ GHOST_kFailure
Definition GHOST_Types.h:87
@ GHOST_kSuccess
Definition GHOST_Types.h:87
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