Blender  V3.3
GHOST_ContextSDL.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2014 Blender Foundation. All rights reserved. */
3 
10 #include "GHOST_ContextSDL.h"
11 
12 #include <vector>
13 
14 #include <cassert>
15 #include <cstdio>
16 #include <cstring>
17 
18 SDL_GLContext GHOST_ContextSDL::s_sharedContext = nullptr;
19 int GHOST_ContextSDL::s_sharedCount = 0;
20 
22  SDL_Window *window,
23  int contextProfileMask,
24  int contextMajorVersion,
25  int contextMinorVersion,
26  int contextFlags,
27  int contextResetNotificationStrategy)
28  : GHOST_Context(stereoVisual),
29  m_window(window),
30  m_hidden_window(nullptr),
31  m_contextProfileMask(contextProfileMask),
32  m_contextMajorVersion(contextMajorVersion),
33  m_contextMinorVersion(contextMinorVersion),
34  m_contextFlags(contextFlags),
35  m_contextResetNotificationStrategy(contextResetNotificationStrategy),
36  m_context(nullptr)
37 {
38  // assert(m_window != nullptr);
39 }
40 
42 {
43  if (m_context == nullptr) {
44  return;
45  }
46 
47  if (m_window != nullptr && m_context == SDL_GL_GetCurrentContext()) {
48  SDL_GL_MakeCurrent(m_window, nullptr);
49  }
50  if (m_context != s_sharedContext || s_sharedCount == 1) {
51  assert(s_sharedCount > 0);
52 
53  s_sharedCount--;
54 
55  if (s_sharedCount == 0) {
56  s_sharedContext = nullptr;
57  }
58  SDL_GL_DeleteContext(m_context);
59  }
60 
61  if (m_hidden_window != nullptr) {
62  SDL_DestroyWindow(m_hidden_window);
63  }
64 }
65 
67 {
68  SDL_GL_SwapWindow(m_window);
69 
70  return GHOST_kSuccess;
71 }
72 
74 {
75  if (m_context == nullptr) {
76  return GHOST_kFailure;
77  }
78  return SDL_GL_MakeCurrent(m_window, m_context) ? GHOST_kSuccess : GHOST_kFailure;
79 }
80 
82 {
83  if (m_context == nullptr) {
84  return GHOST_kFailure;
85  }
86  /* Untested, may not work. */
87  return SDL_GL_MakeCurrent(nullptr, nullptr) ? GHOST_kSuccess : GHOST_kFailure;
88 }
89 
91 {
92 #ifdef GHOST_OPENGL_ALPHA
93  const bool needAlpha = true;
94 #else
95  const bool needAlpha = false;
96 #endif
97 
98  SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, m_contextProfileMask);
99  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, m_contextMajorVersion);
100  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, m_contextMinorVersion);
101  SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, m_contextFlags);
102 
103  SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
104  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
105  SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
106  SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
107  SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
108 
109  if (needAlpha) {
110  SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
111  }
112 
113  if (m_stereoVisual) {
114  SDL_GL_SetAttribute(SDL_GL_STEREO, 1);
115  }
116 
117  if (m_window == nullptr) {
118  m_hidden_window = SDL_CreateWindow("Offscreen Context Windows",
119  SDL_WINDOWPOS_UNDEFINED,
120  SDL_WINDOWPOS_UNDEFINED,
121  1,
122  1,
123  SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS |
124  SDL_WINDOW_HIDDEN);
125 
126  m_window = m_hidden_window;
127  }
128 
129  m_context = SDL_GL_CreateContext(m_window);
130 
131  GHOST_TSuccess success;
132 
133  if (m_context != nullptr) {
134  if (!s_sharedContext) {
135  s_sharedContext = m_context;
136  }
137  s_sharedCount++;
138 
139  success = (SDL_GL_MakeCurrent(m_window, m_context) < 0) ? GHOST_kFailure : GHOST_kSuccess;
140 
141  initContextGLEW();
142 
143  initClearGL();
144  SDL_GL_SwapWindow(m_window);
145 
146  success = GHOST_kSuccess;
147  }
148  else {
149  success = GHOST_kFailure;
150  }
151 
152  return success;
153 }
154 
156 {
157  m_window = nullptr;
158 
159  return GHOST_kSuccess;
160 }
161 
163 {
164  if (SDL_GL_SetSwapInterval(interval) == -1) {
165  return GHOST_kFailure;
166  }
167  return GHOST_kSuccess;
168 }
169 
171 {
172  intervalOut = SDL_GL_GetSwapInterval();
173  return GHOST_kSuccess;
174 }
GHOST_TSuccess
Definition: GHOST_Types.h:74
@ GHOST_kFailure
Definition: GHOST_Types.h:74
@ GHOST_kSuccess
Definition: GHOST_Types.h:74
GHOST_TSuccess swapBuffers()
GHOST_TSuccess initializeDrawingContext()
GHOST_TSuccess releaseNativeHandles()
GHOST_TSuccess getSwapInterval(int &intervalOut)
GHOST_ContextSDL(bool stereoVisual, SDL_Window *window, int contextProfileMask, int contextMajorVersion, int contextMinorVersion, int contextFlags, int contextResetNotificationStrategy)
GHOST_TSuccess activateDrawingContext()
GHOST_TSuccess releaseDrawingContext()
GHOST_TSuccess setSwapInterval(int interval)
void initContextGLEW()
static void initClearGL()