/
WinSystemEGL.cpp
525 lines (440 loc) · 14.3 KB
/
WinSystemEGL.cpp
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
* Copyright (C) 2011-2013 Team XBMC
* http://xbmc.org
*
* This Program 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, or (at your option)
* any later version.
*
* This Program 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 XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "system.h"
#ifdef HAS_EGL
#include "WinSystemEGL.h"
#include "filesystem/SpecialProtocol.h"
#include "guilib/GraphicContext.h"
#include "settings/DisplaySettings.h"
#include "guilib/IDirtyRegionSolver.h"
#include "settings/AdvancedSettings.h"
#include "settings/Settings.h"
#include "settings/DisplaySettings.h"
#include "utils/log.h"
#include "EGLWrapper.h"
#include "EGLQuirks.h"
#include <vector>
////////////////////////////////////////////////////////////////////////////////////////////
CWinSystemEGL::CWinSystemEGL() : CWinSystemBase()
{
m_eWindowSystem = WINDOW_SYSTEM_EGL;
m_displayWidth = 0;
m_displayHeight = 0;
m_display = EGL_NO_DISPLAY;
m_surface = EGL_NO_SURFACE;
m_context = EGL_NO_CONTEXT;
m_config = NULL;
m_egl = NULL;
m_iVSyncMode = 0;
}
CWinSystemEGL::~CWinSystemEGL()
{
if (m_egl)
{
DestroyWindowSystem();
delete m_egl;
}
}
bool CWinSystemEGL::InitWindowSystem()
{
RESOLUTION_INFO preferred_resolution;
if (!m_egl)
m_egl = new CEGLWrapper;
if (!m_egl)
{
CLog::Log(LOGERROR, "%s: EGL not in a good state",__FUNCTION__);
return false;
}
if (!m_egl->Initialize("auto"))
{
CLog::Log(LOGERROR, "%s: Could not initialize",__FUNCTION__);
return false;
}
CLog::Log(LOGNOTICE, "%s: Using EGL Implementation: %s",__FUNCTION__,m_egl->GetNativeName().c_str());
if (!m_egl->CreateNativeDisplay())
{
CLog::Log(LOGERROR, "%s: Could not get native display",__FUNCTION__);
return false;
}
if (!m_egl->CreateNativeWindow())
{
CLog::Log(LOGERROR, "%s: Could not get native window",__FUNCTION__);
return false;
}
if (!m_egl->InitDisplay(&m_display))
{
CLog::Log(LOGERROR, "%s: Could not create display",__FUNCTION__);
return false;
}
EGLint surface_type = EGL_WINDOW_BIT;
// for the non-trivial dirty region modes, we need the EGL buffer to be preserved across updates
if (g_advancedSettings.m_guiAlgorithmDirtyRegions == DIRTYREGION_SOLVER_COST_REDUCTION ||
g_advancedSettings.m_guiAlgorithmDirtyRegions == DIRTYREGION_SOLVER_UNION)
surface_type |= EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
EGLint configAttrs [] = {
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 16,
EGL_STENCIL_SIZE, 0,
EGL_SAMPLE_BUFFERS, 0,
EGL_SAMPLES, 0,
EGL_SURFACE_TYPE, surface_type,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
if (!m_egl->ChooseConfig(m_display, configAttrs, &m_config))
{
CLog::Log(LOGERROR, "%s: Could not find a compatible configuration",__FUNCTION__);
return false;
}
// Some platforms require a surface before we can probe the resolution.
// Create the window here, then the creation in CreateNewWindow() will be skipped.
int quirks;
m_egl->GetQuirks(&quirks);
if (quirks & EGL_QUIRK_NEED_WINDOW_FOR_RES)
{
RESOLUTION_INFO temp;
CreateWindow(temp);
}
m_extensions = m_egl->GetExtensions(m_display);
return CWinSystemBase::InitWindowSystem();
}
bool CWinSystemEGL::CreateWindow(RESOLUTION_INFO &res)
{
if (!m_egl)
{
CLog::Log(LOGERROR, "CWinSystemEGL::CreateWindow no EGL!");
return false;
}
if(m_egl)
m_egl->SetNativeResolution(res);
if (!m_egl->CreateSurface(m_display, m_config, &m_surface))
{
CLog::Log(LOGNOTICE, "%s: Could not create a surface. Trying with a fresh Native Window.",__FUNCTION__);
m_egl->DestroyNativeWindow();
if (!m_egl->CreateNativeWindow())
{
CLog::Log(LOGERROR, "%s: Could not get native window",__FUNCTION__);
return false;
}
if (!m_egl->CreateSurface(m_display, m_config, &m_surface))
{
CLog::Log(LOGERROR, "%s: Could not create surface",__FUNCTION__);
return false;
}
}
/* The intel driver on wayland is broken and always returns a surface
* size of -1, -1. Work around it for now */
if (m_egl->TrustSurfaceSize())
{
int width = 0, height = 0;
if (!m_egl->GetSurfaceSize(m_display, m_surface, &width, &height))
{
CLog::Log(LOGERROR, "%s: Surface is invalid",__FUNCTION__);
return false;
}
CLog::Log(LOGDEBUG, "%s: Created surface of size %ix%i",__FUNCTION__, width, height);
}
else
CLog::Log(LOGDEBUG, "%s: Cannot reliably get surface size with this backend",__FUNCTION__);
EGLint contextAttrs[] =
{
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
if (!m_egl->BindAPI(EGL_OPENGL_ES_API))
{
CLog::Log(LOGERROR, "%s: Could not bind %i api",__FUNCTION__, EGL_OPENGL_ES_API);
return false;
}
if (m_context == EGL_NO_CONTEXT)
{
if (!m_egl->CreateContext(m_display, m_config, contextAttrs, &m_context))
{
CLog::Log(LOGERROR, "%s: Could not create context",__FUNCTION__);
return false;
}
}
if (!m_egl->BindContext(m_display, m_surface, m_context))
{
CLog::Log(LOGERROR, "%s: Could not bind to context",__FUNCTION__);
return false;
}
// for the non-trivial dirty region modes, we need the EGL buffer to be preserved across updates
if (g_advancedSettings.m_guiAlgorithmDirtyRegions == DIRTYREGION_SOLVER_COST_REDUCTION ||
g_advancedSettings.m_guiAlgorithmDirtyRegions == DIRTYREGION_SOLVER_UNION)
{
if (!m_egl->SurfaceAttrib(m_display, m_surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_PRESERVED))
CLog::Log(LOGDEBUG, "%s: Could not set EGL_SWAP_BEHAVIOR",__FUNCTION__);
}
m_bWindowCreated = true;
return true;
}
bool CWinSystemEGL::DestroyWindowSystem()
{
if (!m_egl)
return true;
DestroyWindow();
if (m_context != EGL_NO_CONTEXT)
m_egl->DestroyContext(m_display, m_context);
m_context = EGL_NO_CONTEXT;
if (m_display != EGL_NO_DISPLAY)
m_egl->DestroyDisplay(m_display);
m_display = EGL_NO_DISPLAY;
m_egl->DestroyNativeWindow();
m_egl->DestroyNativeDisplay();
m_egl->Destroy();
delete m_egl;
m_egl = NULL;
return true;
}
bool CWinSystemEGL::CreateNewWindow(const CStdString& name, bool fullScreen, RESOLUTION_INFO& res, PHANDLE_EVENT_FUNC userFunction)
{
RESOLUTION_INFO current_resolution;
current_resolution.iWidth = current_resolution.iHeight = 0;
m_nWidth = res.iWidth;
m_nHeight = res.iHeight;
m_displayWidth = res.iScreenWidth;
m_displayHeight = res.iScreenHeight;
m_fRefreshRate = res.fRefreshRate;
if ((m_bWindowCreated && m_egl && m_egl->GetNativeResolution(¤t_resolution)) &&
current_resolution.iWidth == res.iWidth && current_resolution.iHeight == res.iHeight &&
current_resolution.iScreenWidth == res.iScreenWidth && current_resolution.iScreenHeight == res.iScreenHeight &&
m_bFullScreen == fullScreen && current_resolution.fRefreshRate == res.fRefreshRate &&
(current_resolution.dwFlags & D3DPRESENTFLAG_MODEMASK) == (res.dwFlags & D3DPRESENTFLAG_MODEMASK))
{
CLog::Log(LOGDEBUG, "CWinSystemEGL::CreateNewWindow: No need to create a new window");
return true;
}
m_bFullScreen = fullScreen;
// Destroy any existing window
if (m_surface != EGL_NO_SURFACE)
DestroyWindow();
// If we previously destroyed an existing window we need to create a new one
// (otherwise this is taken care of by InitWindowSystem())
if (!CreateWindow(res))
{
CLog::Log(LOGERROR, "%s: Could not create new window",__FUNCTION__);
return false;
}
Show();
return true;
}
bool CWinSystemEGL::DestroyWindow()
{
if (!m_egl)
return false;
m_egl->ReleaseContext(m_display);
if (m_surface != EGL_NO_SURFACE)
m_egl->DestroySurface(m_surface, m_display);
int quirks;
m_egl->GetQuirks(&quirks);
if (quirks & EGL_QUIRK_DESTROY_NATIVE_WINDOW_WITH_SURFACE)
m_egl->DestroyNativeWindow();
m_surface = EGL_NO_SURFACE;
m_bWindowCreated = false;
return true;
}
bool CWinSystemEGL::ResizeWindow(int newWidth, int newHeight, int newLeft, int newTop)
{
CRenderSystemGLES::ResetRenderSystem(newWidth, newHeight, true, 0);
int vsync_mode = CSettings::Get().GetInt("videoscreen.vsync");
if (vsync_mode != VSYNC_DRIVER)
SetVSyncImpl(m_iVSyncMode);
return true;
}
bool CWinSystemEGL::SetFullScreen(bool fullScreen, RESOLUTION_INFO& res, bool blankOtherDisplays)
{
CreateNewWindow("", fullScreen, res, NULL);
CRenderSystemGLES::ResetRenderSystem(res.iWidth, res.iHeight, fullScreen, res.fRefreshRate);
int vsync_mode = CSettings::Get().GetInt("videoscreen.vsync");
if (vsync_mode != VSYNC_DRIVER)
SetVSyncImpl(m_iVSyncMode);
return true;
}
void CWinSystemEGL::UpdateResolutions()
{
CWinSystemBase::UpdateResolutions();
RESOLUTION_INFO resDesktop, curDisplay;
std::vector<RESOLUTION_INFO> resolutions;
if (!m_egl->ProbeResolutions(resolutions) || resolutions.empty())
{
CLog::Log(LOGWARNING, "%s: ProbeResolutions failed. Trying safe default.",__FUNCTION__);
RESOLUTION_INFO fallback;
if (m_egl->GetPreferredResolution(&fallback))
{
resolutions.push_back(fallback);
}
else
{
CLog::Log(LOGERROR, "%s: Fatal Error, GetPreferredResolution failed",__FUNCTION__);
return;
}
}
/* ProbeResolutions includes already all resolutions.
* Only get desktop resolution so we can replace xbmc's desktop res
*/
if (m_egl->GetNativeResolution(&curDisplay))
resDesktop = curDisplay;
RESOLUTION ResDesktop = RES_INVALID;
RESOLUTION res_index = RES_DESKTOP;
for (size_t i = 0; i < resolutions.size(); i++)
{
// if this is a new setting,
// create a new empty setting to fill in.
if ((int)CDisplaySettings::Get().ResolutionInfoSize() <= res_index)
{
RESOLUTION_INFO res;
CDisplaySettings::Get().AddResolutionInfo(res);
}
g_graphicsContext.ResetOverscan(resolutions[i]);
CDisplaySettings::Get().GetResolutionInfo(res_index) = resolutions[i];
CLog::Log(LOGNOTICE, "Found resolution %d x %d for display %d with %d x %d%s @ %f Hz\n",
resolutions[i].iWidth,
resolutions[i].iHeight,
resolutions[i].iScreen,
resolutions[i].iScreenWidth,
resolutions[i].iScreenHeight,
resolutions[i].dwFlags & D3DPRESENTFLAG_INTERLACED ? "i" : "",
resolutions[i].fRefreshRate);
if(resDesktop.iWidth == resolutions[i].iWidth &&
resDesktop.iHeight == resolutions[i].iHeight &&
resDesktop.iScreenWidth == resolutions[i].iScreenWidth &&
resDesktop.iScreenHeight == resolutions[i].iScreenHeight &&
(resDesktop.dwFlags & D3DPRESENTFLAG_MODEMASK) == (resolutions[i].dwFlags & D3DPRESENTFLAG_MODEMASK) &&
resDesktop.fRefreshRate == resolutions[i].fRefreshRate)
{
ResDesktop = res_index;
}
res_index = (RESOLUTION)((int)res_index + 1);
}
// swap desktop index for desktop res if available
if (ResDesktop != RES_INVALID)
{
CLog::Log(LOGNOTICE, "Found (%dx%d%s@%f) at %d, setting to RES_DESKTOP at %d",
resDesktop.iWidth, resDesktop.iHeight,
resDesktop.dwFlags & D3DPRESENTFLAG_INTERLACED ? "i" : "",
resDesktop.fRefreshRate,
(int)ResDesktop, (int)RES_DESKTOP);
RESOLUTION_INFO desktop = CDisplaySettings::Get().GetResolutionInfo(RES_DESKTOP);
CDisplaySettings::Get().GetResolutionInfo(RES_DESKTOP) = CDisplaySettings::Get().GetResolutionInfo(ResDesktop);
CDisplaySettings::Get().GetResolutionInfo(ResDesktop) = desktop;
}
}
bool CWinSystemEGL::IsExtSupported(const char* extension)
{
std::string name;
name = " ";
name += extension;
name += " ";
return (m_extensions.find(name) != std::string::npos || CRenderSystemGLES::IsExtSupported(extension));
}
bool CWinSystemEGL::PresentRenderImpl(const CDirtyRegionList &dirty)
{
m_egl->SwapBuffers(m_display, m_surface);
return true;
}
void CWinSystemEGL::SetVSyncImpl(bool enable)
{
m_iVSyncMode = enable ? 10:0;
if (!m_egl->SetVSync(m_display, enable))
{
m_iVSyncMode = 0;
CLog::Log(LOGERROR, "%s,Could not set egl vsync", __FUNCTION__);
}
}
void CWinSystemEGL::ShowOSMouse(bool show)
{
}
bool CWinSystemEGL::HasCursor()
{
#ifdef TARGET_ANDROID
return false;
#else
return true;
#endif
}
void CWinSystemEGL::NotifyAppActiveChange(bool bActivated)
{
}
bool CWinSystemEGL::Minimize()
{
Hide();
return true;
}
bool CWinSystemEGL::Restore()
{
Show(true);
return false;
}
bool CWinSystemEGL::Hide()
{
return m_egl->ShowWindow(false);
}
bool CWinSystemEGL::Show(bool raise)
{
return m_egl->ShowWindow(true);
}
EGLDisplay CWinSystemEGL::GetEGLDisplay()
{
return m_display;
}
EGLContext CWinSystemEGL::GetEGLContext()
{
return m_context;
}
EGLConfig CWinSystemEGL::GetEGLConfig()
{
return m_config;
}
// the logic in this function should match whether CBaseRenderer::FindClosestResolution picks a 3D mode
bool CWinSystemEGL::Support3D(int width, int height, uint32_t mode) const
{
RESOLUTION_INFO &curr = CDisplaySettings::Get().GetResolutionInfo(g_graphicsContext.GetVideoResolution());
// if we are using automatic hdmi mode switching
if (CSettings::Get().GetInt("videoplayer.adjustrefreshrate") != ADJUST_REFRESHRATE_OFF)
{
int searchWidth = curr.iScreenWidth;
int searchHeight = curr.iScreenHeight;
// only search the custom resolutions
for (unsigned int i = (int)RES_DESKTOP; i < CDisplaySettings::Get().ResolutionInfoSize(); i++)
{
RESOLUTION_INFO res = CDisplaySettings::Get().GetResolutionInfo(i);
if(res.iScreenWidth == searchWidth && res.iScreenHeight == searchHeight && (res.dwFlags & mode))
return true;
}
}
// otherwise just consider current mode
else
{
if (curr.dwFlags & mode)
return true;
}
return false;
}
bool CWinSystemEGL::ClampToGUIDisplayLimits(int &width, int &height)
{
width = width > m_nWidth ? m_nWidth : width;
height = height > m_nHeight ? m_nHeight : height;
return true;
}
#endif