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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/*
Copyright (C) 2025 Carl Hetherington <cth@carlh.net>
This file is part of DCP-o-matic.
DCP-o-matic is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
DCP-o-matic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lib/cross.h"
#include "lib/dcpomatic_log.h"
#include "lib/exceptions.h"
#include "gl_view.h"
#include <wx/wx.h>
#if BOOST_VERSION >= 106100
using namespace boost::placeholders;
#endif
/* This will only build on an new-enough wxWidgets: see the comment in gl_view.h */
#if wxCHECK_VERSION(3,1,0)
GLView::GLView(wxWindow* parent)
: _context(nullptr)
, _vsync_enabled(false)
, _playing(false)
, _one_shot(false)
{
wxGLAttributes attributes;
/* We don't need a depth buffer, and indeed there is apparently a bug with Windows/Intel HD 630
* which puts green lines over the OpenGL display if you have a non-zero depth buffer size.
* https://community.intel.com/t5/Graphics/Request-for-details-on-Intel-HD-630-green-lines-in-OpenGL-apps/m-p/1202179
*/
attributes.PlatformDefaults().MinRGBA(8, 8, 8, 8).DoubleBuffer().Depth(0).EndList();
_canvas = new wxGLCanvas(
parent, attributes, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE
);
_canvas->Bind(wxEVT_SIZE, boost::bind(&GLView::size_changed, this, _1));
_canvas->Bind(wxEVT_PAINT, boost::bind(&GLView::update, this));
}
GLView::~GLView()
{
boost::this_thread::disable_interruption dis;
try {
_thread.interrupt();
_thread.join();
} catch (...) {}
}
void
GLView::size_changed(wxSizeEvent const& ev)
{
auto const scale = _canvas->GetDPIScaleFactor();
int const width = std::round(ev.GetSize().GetWidth() * scale);
int const height = std::round(ev.GetSize().GetHeight() * scale);
_canvas_size = { width, height };
LOG_GENERAL("GLView canvas size changed to %1x%2", width, height);
}
void
GLView::start_thread_if_required()
{
if (!_thread.joinable()) {
_thread = boost::thread(boost::bind(&GLView::thread, this));
}
}
void
GLView::thread()
try
{
thread_setup();
#if defined(DCPOMATIC_LINUX) && defined(DCPOMATIC_HAVE_GLX_SWAP_INTERVAL_EXT)
if (_canvas->IsExtensionSupported("GLX_EXT_swap_control")) {
/* Enable vsync */
Display* dpy = wxGetX11Display();
glXSwapIntervalEXT(dpy, DefaultScreen(dpy), 1);
_vsync_enabled = true;
}
#endif
#ifdef DCPOMATIC_WINDOWS
if (_canvas->IsExtensionSupported("WGL_EXT_swap_control")) {
/* Enable vsync */
PFNWGLSWAPINTERVALEXTPROC swap = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
if (swap) {
swap(1);
_vsync_enabled = true;
}
}
#endif
#ifdef DCPOMATIC_OSX
/* Enable vsync */
GLint swapInterval = 1;
CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
_vsync_enabled = true;
#endif
while (true) {
boost::mutex::scoped_lock lm(_playing_mutex);
while (!_playing && !_one_shot) {
_thread_work_condition.wait(lm);
}
lm.unlock();
if (_playing) {
thread_playing();
} else if (_one_shot) {
_one_shot = false;
draw();
}
boost::this_thread::interruption_point();
dcpomatic_sleep_milliseconds(time_until_next_frame().get_value_or(0));
}
}
catch (boost::thread_interrupted&)
{
}
catch (...)
{
store_current();
}
void
GLView::start()
{
boost::mutex::scoped_lock lm(_playing_mutex);
_playing = true;
_thread_work_condition.notify_all();
}
void
GLView::stop()
{
boost::mutex::scoped_lock lm(_playing_mutex);
_playing = false;
}
void
GLView::request_one_shot()
{
boost::mutex::scoped_lock lm(_playing_mutex);
_one_shot = true;
_thread_work_condition.notify_all();
}
void
GLView::update()
{
if (!_canvas->IsShownOnScreen()) {
return;
}
/* It appears important to do this from the GUI thread; if we do it from the GL thread
* on Linux we get strange failures to create the context for any version of GL higher
* than 3.2.
*/
ensure_context();
#ifdef DCPOMATIC_OSX
/* macOS gives errors if we don't do this (and therefore [NSOpenGLContext setView:]) from the main thread */
if (!_setup_shaders_done) {
setup_shaders();
_setup_shaders_done = true;
}
#endif
start_thread_if_required();
request_one_shot();
rethrow();
}
void
GLView::ensure_context()
{
if (!_context) {
wxGLContextAttrs attrs;
attrs.PlatformDefaults().CoreProfile().OGLVersion(4, 1).EndList();
_context = new wxGLContext(_canvas, nullptr, &attrs);
if (!_context->IsOK()) {
boost::throw_exception(GLError("Making GL context", -1));
}
}
}
#endif
|