| 1 |
/* |
| 2 |
* Copyright (C) 2005-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "WinSystemAndroidGLESContext.h" |
| 10 |
|
| 11 |
#include "ServiceBroker.h" |
| 12 |
#include "VideoSyncAndroid.h" |
| 13 |
#include "cores/VideoPlayer/DVDCodecs/Video/DVDVideoCodec.h" |
| 14 |
#include "threads/SingleLock.h" |
| 15 |
#include "utils/log.h" |
| 16 |
#include "windowing/WindowSystemFactory.h" |
| 17 |
|
| 18 |
#include "platform/android/activity/XBMCApp.h" |
| 19 |
|
| 20 |
#include <memory> |
| 21 |
|
| 22 |
#include <unistd.h> |
| 23 |
|
| 24 |
#include "PlatformDefs.h" |
| 25 |
|
| 26 |
#include <EGL/eglext.h> |
| 27 |
|
| 28 |
void CWinSystemAndroidGLESContext::Register() |
| 29 |
{ |
| 30 |
KODI::WINDOWING::CWindowSystemFactory::RegisterWindowSystem(CreateWinSystem); |
| 31 |
} |
| 32 |
|
| 33 |
std::unique_ptr<CWinSystemBase> CWinSystemAndroidGLESContext::CreateWinSystem() |
| 34 |
{ |
| 35 |
return std::make_unique<CWinSystemAndroidGLESContext>(); |
| 36 |
} |
| 37 |
|
| 38 |
bool CWinSystemAndroidGLESContext::InitWindowSystem() |
| 39 |
{ |
| 40 |
if (!CWinSystemAndroid::InitWindowSystem()) |
| 41 |
{ |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
if (!m_pGLContext.CreateDisplay(m_nativeDisplay)) |
| 46 |
{ |
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
if (!m_pGLContext.InitializeDisplay(EGL_OPENGL_ES_API)) |
| 51 |
{ |
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
if (!m_pGLContext.ChooseConfig(EGL_OPENGL_ES2_BIT)) |
| 56 |
{ |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
m_hasHDRConfig = m_pGLContext.ChooseConfig(EGL_OPENGL_ES2_BIT, 0, true); |
| 61 |
|
| 62 |
m_hasEGL_BT2020_PQ_Colorspace_Extension = |
| 63 |
CEGLUtils::HasExtension(m_pGLContext.GetEGLDisplay(), "EGL_EXT_gl_colorspace_bt2020_pq"); |
| 64 |
m_hasEGL_ST2086_Extension = |
| 65 |
CEGLUtils::HasExtension(m_pGLContext.GetEGLDisplay(), "EGL_EXT_surface_SMPTE2086_metadata"); |
| 66 |
|
| 67 |
bool hasEGLHDRExtensions = m_hasEGL_BT2020_PQ_Colorspace_Extension && m_hasEGL_ST2086_Extension; |
| 68 |
|
| 69 |
CLog::Log(LOGDEBUG, |
| 70 |
"CWinSystemAndroidGLESContext::InitWindowSystem: HDRConfig: {}, HDRExtensions: {}", |
| 71 |
static_cast<int>(m_hasHDRConfig), static_cast<int>(hasEGLHDRExtensions)); |
| 72 |
|
| 73 |
CEGLAttributesVec contextAttribs; |
| 74 |
contextAttribs.Add({{EGL_CONTEXT_CLIENT_VERSION, 2}}); |
| 75 |
|
| 76 |
if (!m_pGLContext.CreateContext(contextAttribs)) |
| 77 |
{ |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
bool CWinSystemAndroidGLESContext::CreateNewWindow(const std::string& name, |
| 85 |
bool fullScreen, |
| 86 |
RESOLUTION_INFO& res) |
| 87 |
{ |
| 88 |
m_pGLContext.DestroySurface(); |
| 89 |
|
| 90 |
if (!CWinSystemAndroid::CreateNewWindow(name, fullScreen, res)) |
| 91 |
{ |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
if (!CreateSurface()) |
| 96 |
{ |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
if (!m_pGLContext.BindContext()) |
| 101 |
{ |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
bool CWinSystemAndroidGLESContext::ResizeWindow(int newWidth, int newHeight, int newLeft, int newTop) |
| 109 |
{ |
| 110 |
CRenderSystemGLES::ResetRenderSystem(newWidth, newHeight); |
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
bool CWinSystemAndroidGLESContext::SetFullScreen(bool fullScreen, RESOLUTION_INFO& res, bool blankOtherDisplays) |
| 115 |
{ |
| 116 |
CreateNewWindow("", fullScreen, res); |
| 117 |
CRenderSystemGLES::ResetRenderSystem(res.iWidth, res.iHeight); |
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
void CWinSystemAndroidGLESContext::SetVSyncImpl(bool enable) |
| 122 |
{ |
| 123 |
// We use Choreographer for timing |
| 124 |
m_pGLContext.SetVSync(false); |
| 125 |
} |
| 126 |
|
| 127 |
void CWinSystemAndroidGLESContext::PresentRenderImpl(bool rendered) |
| 128 |
{ |
| 129 |
if (!m_nativeWindow) |
| 130 |
{ |
| 131 |
usleep(10000); |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
// Mode change finalization was triggered by timer |
| 136 |
if (IsHdmiModeTriggered()) |
| 137 |
SetHdmiState(true); |
| 138 |
|
| 139 |
// Ignore EGL_BAD_SURFACE: It seems to happen during/after mode changes, but |
| 140 |
// we can't actually do anything about it |
| 141 |
if (rendered && !m_pGLContext.TrySwapBuffers()) |
| 142 |
CEGLUtils::Log(LOGERROR, "eglSwapBuffers failed"); |
| 143 |
|
| 144 |
CXBMCApp::Get().WaitVSync(1000); |
| 145 |
} |
| 146 |
|
| 147 |
float CWinSystemAndroidGLESContext::GetFrameLatencyAdjustment() |
| 148 |
{ |
| 149 |
return CXBMCApp::Get().GetFrameLatencyMs(); |
| 150 |
} |
| 151 |
|
| 152 |
EGLDisplay CWinSystemAndroidGLESContext::GetEGLDisplay() const |
| 153 |
{ |
| 154 |
return m_pGLContext.GetEGLDisplay(); |
| 155 |
} |
| 156 |
|
| 157 |
EGLSurface CWinSystemAndroidGLESContext::GetEGLSurface() const |
| 158 |
{ |
| 159 |
return m_pGLContext.GetEGLSurface(); |
| 160 |
} |
| 161 |
|
| 162 |
EGLContext CWinSystemAndroidGLESContext::GetEGLContext() const |
| 163 |
{ |
| 164 |
return m_pGLContext.GetEGLContext(); |
| 165 |
} |
| 166 |
|
| 167 |
EGLConfig CWinSystemAndroidGLESContext::GetEGLConfig() const |
| 168 |
{ |
| 169 |
return m_pGLContext.GetEGLConfig(); |
| 170 |
} |
| 171 |
|
| 172 |
std::unique_ptr<CVideoSync> CWinSystemAndroidGLESContext::GetVideoSync(CVideoReferenceClock* clock) |
| 173 |
{ |
| 174 |
std::unique_ptr<CVideoSync> pVSync(new CVideoSyncAndroid(clock)); |
| 175 |
return pVSync; |
| 176 |
} |
| 177 |
|
| 178 |
bool CWinSystemAndroidGLESContext::CreateSurface() |
| 179 |
{ |
| 180 |
if (!m_pGLContext.CreateSurface(static_cast<EGLNativeWindowType>(m_nativeWindow->m_window), |
| 181 |
m_HDRColorSpace)) |
| 182 |
{ |
| 183 |
if (m_HDRColorSpace != EGL_NONE) |
| 184 |
{ |
| 185 |
m_HDRColorSpace = EGL_NONE; |
| 186 |
m_displayMetadata = nullptr; |
| 187 |
m_lightMetadata = nullptr; |
| 188 |
if (!m_pGLContext.CreateSurface(static_cast<EGLNativeWindowType>(m_nativeWindow->m_window))) |
| 189 |
return false; |
| 190 |
} |
| 191 |
else |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
#if EGL_EXT_surface_SMPTE2086_metadata |
| 196 |
if (m_displayMetadata) |
| 197 |
{ |
| 198 |
m_pGLContext.SurfaceAttrib(EGL_SMPTE2086_DISPLAY_PRIMARY_RX_EXT, static_cast<int>(av_q2d(m_displayMetadata->display_primaries[0][0]) * EGL_METADATA_SCALING_EXT + 0.5)); |
| 199 |
m_pGLContext.SurfaceAttrib(EGL_SMPTE2086_DISPLAY_PRIMARY_RY_EXT, static_cast<int>(av_q2d(m_displayMetadata->display_primaries[0][1]) * EGL_METADATA_SCALING_EXT + 0.5)); |
| 200 |
m_pGLContext.SurfaceAttrib(EGL_SMPTE2086_DISPLAY_PRIMARY_GX_EXT, static_cast<int>(av_q2d(m_displayMetadata->display_primaries[1][0]) * EGL_METADATA_SCALING_EXT + 0.5)); |
| 201 |
m_pGLContext.SurfaceAttrib(EGL_SMPTE2086_DISPLAY_PRIMARY_GY_EXT, static_cast<int>(av_q2d(m_displayMetadata->display_primaries[1][1]) * EGL_METADATA_SCALING_EXT + 0.5)); |
| 202 |
m_pGLContext.SurfaceAttrib(EGL_SMPTE2086_DISPLAY_PRIMARY_BX_EXT, static_cast<int>(av_q2d(m_displayMetadata->display_primaries[2][0]) * EGL_METADATA_SCALING_EXT + 0.5)); |
| 203 |
m_pGLContext.SurfaceAttrib(EGL_SMPTE2086_DISPLAY_PRIMARY_BY_EXT, static_cast<int>(av_q2d(m_displayMetadata->display_primaries[2][1]) * EGL_METADATA_SCALING_EXT + 0.5)); |
| 204 |
m_pGLContext.SurfaceAttrib(EGL_SMPTE2086_WHITE_POINT_X_EXT, static_cast<int>(av_q2d(m_displayMetadata->white_point[0]) * EGL_METADATA_SCALING_EXT + 0.5)); |
| 205 |
m_pGLContext.SurfaceAttrib(EGL_SMPTE2086_WHITE_POINT_Y_EXT, static_cast<int>(av_q2d(m_displayMetadata->white_point[1]) * EGL_METADATA_SCALING_EXT + 0.5)); |
| 206 |
m_pGLContext.SurfaceAttrib(EGL_SMPTE2086_MAX_LUMINANCE_EXT, static_cast<int>(av_q2d(m_displayMetadata->max_luminance) * EGL_METADATA_SCALING_EXT + 0.5)); |
| 207 |
m_pGLContext.SurfaceAttrib(EGL_SMPTE2086_MIN_LUMINANCE_EXT, static_cast<int>(av_q2d(m_displayMetadata->min_luminance) * EGL_METADATA_SCALING_EXT + 0.5)); |
| 208 |
} |
| 209 |
if (m_lightMetadata) |
| 210 |
{ |
| 211 |
m_pGLContext.SurfaceAttrib(EGL_CTA861_3_MAX_CONTENT_LIGHT_LEVEL_EXT, static_cast<int>(m_lightMetadata->MaxCLL * EGL_METADATA_SCALING_EXT)); |
| 212 |
m_pGLContext.SurfaceAttrib(EGL_CTA861_3_MAX_FRAME_AVERAGE_LEVEL_EXT, static_cast<int>(m_lightMetadata->MaxFALL * EGL_METADATA_SCALING_EXT)); |
| 213 |
} |
| 214 |
#endif |
| 215 |
return true; |
| 216 |
} |
| 217 |
|
| 218 |
bool CWinSystemAndroidGLESContext::IsHDRDisplay() |
| 219 |
{ |
| 220 |
return m_hasHDRConfig && (m_hasEGL_BT2020_PQ_Colorspace_Extension || m_hasEGL_ST2086_Extension) && |
| 221 |
CWinSystemAndroid::IsHDRDisplay(); |
| 222 |
} |
| 223 |
|
| 224 |
bool CWinSystemAndroidGLESContext::SetHDR(const VideoPicture* videoPicture) |
| 225 |
{ |
| 226 |
if (!CServiceBroker::GetWinSystem()->IsHDRDisplaySettingEnabled()) |
| 227 |
return false; |
| 228 |
|
| 229 |
EGLint HDRColorSpace = 0; |
| 230 |
|
| 231 |
#if EGL_EXT_gl_colorspace_bt2020_linear |
| 232 |
if (m_hasHDRConfig && m_hasEGL_BT2020_PQ_Colorspace_Extension && m_hasEGL_ST2086_Extension) |
| 233 |
{ |
| 234 |
HDRColorSpace = EGL_NONE; |
| 235 |
if (videoPicture && videoPicture->hasDisplayMetadata) |
| 236 |
{ |
| 237 |
switch (videoPicture->color_space) |
| 238 |
{ |
| 239 |
case AVCOL_SPC_BT2020_NCL: |
| 240 |
case AVCOL_SPC_BT2020_CL: |
| 241 |
case AVCOL_SPC_BT709: |
| 242 |
HDRColorSpace = EGL_GL_COLORSPACE_BT2020_PQ_EXT; |
| 243 |
break; |
| 244 |
default: |
| 245 |
m_displayMetadata = nullptr; |
| 246 |
m_lightMetadata = nullptr; |
| 247 |
} |
| 248 |
} |
| 249 |
else |
| 250 |
{ |
| 251 |
m_displayMetadata = nullptr; |
| 252 |
m_lightMetadata = nullptr; |
| 253 |
} |
| 254 |
|
| 255 |
if (HDRColorSpace != m_HDRColorSpace) |
| 256 |
{ |
| 257 |
CLog::Log(LOGDEBUG, "CWinSystemAndroidGLESContext::SetHDR: ColorSpace: {}", HDRColorSpace); |
| 258 |
|
| 259 |
m_HDRColorSpace = HDRColorSpace; |
| 260 |
m_displayMetadata = |
| 261 |
m_HDRColorSpace == EGL_NONE |
| 262 |
? nullptr |
| 263 |
: std::make_unique<AVMasteringDisplayMetadata>(videoPicture->displayMetadata); |
| 264 |
// TODO: discuss with NVIDIA why this prevent turning HDR display off |
| 265 |
//m_lightMetadata = !videoPicture || m_HDRColorSpace == EGL_NONE ? nullptr : std::unique_ptr<AVContentLightMetadata>(new AVContentLightMetadata(videoPicture->lightMetadata)); |
| 266 |
m_pGLContext.DestroySurface(); |
| 267 |
CreateSurface(); |
| 268 |
m_pGLContext.BindContext(); |
| 269 |
} |
| 270 |
} |
| 271 |
#endif |
| 272 |
|
| 273 |
return m_HDRColorSpace == HDRColorSpace; |
| 274 |
} |