Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GHOST_ContextSDL.cpp « intern « ghost « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5b02fe1c1e6d2c7c3fc5956a1dc34b11960cdd6e (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2014 Blender Foundation. All rights reserved. */

/** \file
 * \ingroup GHOST
 *
 * Definition of GHOST_ContextSDL class.
 */

#include "GHOST_ContextSDL.h"

#include <vector>

#include <cassert>
#include <cstdio>
#include <cstring>

SDL_GLContext GHOST_ContextSDL::s_sharedContext = nullptr;
int GHOST_ContextSDL::s_sharedCount = 0;

GHOST_ContextSDL::GHOST_ContextSDL(bool stereoVisual,
                                   SDL_Window *window,
                                   int contextProfileMask,
                                   int contextMajorVersion,
                                   int contextMinorVersion,
                                   int contextFlags,
                                   int contextResetNotificationStrategy)
    : GHOST_Context(stereoVisual),
      m_window(window),
      m_hidden_window(nullptr),
      m_contextProfileMask(contextProfileMask),
      m_contextMajorVersion(contextMajorVersion),
      m_contextMinorVersion(contextMinorVersion),
      m_contextFlags(contextFlags),
      m_contextResetNotificationStrategy(contextResetNotificationStrategy),
      m_context(nullptr)
{
  // assert(m_window  != nullptr);
}

GHOST_ContextSDL::~GHOST_ContextSDL()
{
  if (m_context == nullptr) {
    return;
  }

  if (m_window != nullptr && m_context == SDL_GL_GetCurrentContext()) {
    SDL_GL_MakeCurrent(m_window, nullptr);
  }
  if (m_context != s_sharedContext || s_sharedCount == 1) {
    assert(s_sharedCount > 0);

    s_sharedCount--;

    if (s_sharedCount == 0) {
      s_sharedContext = nullptr;
    }
    SDL_GL_DeleteContext(m_context);
  }

  if (m_hidden_window != nullptr) {
    SDL_DestroyWindow(m_hidden_window);
  }
}

GHOST_TSuccess GHOST_ContextSDL::swapBuffers()
{
  SDL_GL_SwapWindow(m_window);

  return GHOST_kSuccess;
}

GHOST_TSuccess GHOST_ContextSDL::activateDrawingContext()
{
  if (m_context == nullptr) {
    return GHOST_kFailure;
  }
  return SDL_GL_MakeCurrent(m_window, m_context) ? GHOST_kSuccess : GHOST_kFailure;
}

GHOST_TSuccess GHOST_ContextSDL::releaseDrawingContext()
{
  if (m_context == nullptr) {
    return GHOST_kFailure;
  }
  /* Untested, may not work. */
  return SDL_GL_MakeCurrent(nullptr, nullptr) ? GHOST_kSuccess : GHOST_kFailure;
}

GHOST_TSuccess GHOST_ContextSDL::initializeDrawingContext()
{
#ifdef GHOST_OPENGL_ALPHA
  const bool needAlpha = true;
#else
  const bool needAlpha = false;
#endif

  SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, m_contextProfileMask);
  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, m_contextMajorVersion);
  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, m_contextMinorVersion);
  SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, m_contextFlags);

  SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);

  if (needAlpha) {
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
  }

  if (m_stereoVisual) {
    SDL_GL_SetAttribute(SDL_GL_STEREO, 1);
  }

  if (m_window == nullptr) {
    m_hidden_window = SDL_CreateWindow("Offscreen Context Windows",
                                       SDL_WINDOWPOS_UNDEFINED,
                                       SDL_WINDOWPOS_UNDEFINED,
                                       1,
                                       1,
                                       SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS |
                                           SDL_WINDOW_HIDDEN);

    m_window = m_hidden_window;
  }

  m_context = SDL_GL_CreateContext(m_window);

  GHOST_TSuccess success;

  if (m_context != nullptr) {
    if (!s_sharedContext) {
      s_sharedContext = m_context;
    }
    s_sharedCount++;

    success = (SDL_GL_MakeCurrent(m_window, m_context) < 0) ? GHOST_kFailure : GHOST_kSuccess;

    initContextGLEW();

    initClearGL();
    SDL_GL_SwapWindow(m_window);

    success = GHOST_kSuccess;
  }
  else {
    success = GHOST_kFailure;
  }

  return success;
}

GHOST_TSuccess GHOST_ContextSDL::releaseNativeHandles()
{
  m_window = nullptr;

  return GHOST_kSuccess;
}

GHOST_TSuccess GHOST_ContextSDL::setSwapInterval(int interval)
{
  if (SDL_GL_SetSwapInterval(interval) == -1) {
    return GHOST_kFailure;
  }
  return GHOST_kSuccess;
}

GHOST_TSuccess GHOST_ContextSDL::getSwapInterval(int &intervalOut)
{
  intervalOut = SDL_GL_GetSwapInterval();
  return GHOST_kSuccess;
}