| 1 |
/* |
| 2 |
* Copyright (C) 2017-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 "GUIGameRenderManager.h" |
| 10 |
|
| 11 |
#include "GUIGameSettingsHandle.h" |
| 12 |
#include "GUIGameVideoHandle.h" |
| 13 |
#include "GUIRenderHandle.h" |
| 14 |
#include "GUIRenderTarget.h" |
| 15 |
#include "GUIRenderTargetFactory.h" |
| 16 |
#include "IGameCallback.h" |
| 17 |
#include "IRenderCallback.h" |
| 18 |
|
| 19 |
#include <mutex> |
| 20 |
|
| 21 |
using namespace KODI; |
| 22 |
using namespace RETRO; |
| 23 |
|
| 24 |
CGUIGameRenderManager::~CGUIGameRenderManager() = default; |
| 25 |
|
| 26 |
void CGUIGameRenderManager::RegisterPlayer(CGUIRenderTargetFactory* factory, |
| 27 |
IRenderCallback* callback, |
| 28 |
IGameCallback* gameCallback) |
| 29 |
{ |
| 30 |
// Set factory |
| 31 |
{ |
| 32 |
std::unique_lock<CCriticalSection> lock(m_targetMutex); |
| 33 |
m_factory = factory; |
| 34 |
UpdateRenderTargets(); |
| 35 |
} |
| 36 |
|
| 37 |
// Set callback |
| 38 |
{ |
| 39 |
std::unique_lock<CCriticalSection> lock(m_callbackMutex); |
| 40 |
m_callback = callback; |
| 41 |
} |
| 42 |
|
| 43 |
// Set game callback |
| 44 |
{ |
| 45 |
std::unique_lock<CCriticalSection> lock(m_gameCallbackMutex); |
| 46 |
m_gameCallback = gameCallback; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
void CGUIGameRenderManager::UnregisterPlayer() |
| 51 |
{ |
| 52 |
// Reset game callback |
| 53 |
{ |
| 54 |
std::unique_lock<CCriticalSection> lock(m_gameCallbackMutex); |
| 55 |
m_gameCallback = nullptr; |
| 56 |
} |
| 57 |
|
| 58 |
// Reset callback |
| 59 |
{ |
| 60 |
std::unique_lock<CCriticalSection> lock(m_callbackMutex); |
| 61 |
m_callback = nullptr; |
| 62 |
} |
| 63 |
|
| 64 |
// Reset factory |
| 65 |
{ |
| 66 |
std::unique_lock<CCriticalSection> lock(m_targetMutex); |
| 67 |
m_factory = nullptr; |
| 68 |
UpdateRenderTargets(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
std::shared_ptr<CGUIRenderHandle> CGUIGameRenderManager::RegisterControl(CGUIGameControl& control) |
| 73 |
{ |
| 74 |
std::unique_lock<CCriticalSection> lock(m_targetMutex); |
| 75 |
|
| 76 |
// Create handle for game control |
| 77 |
std::shared_ptr<CGUIRenderHandle> renderHandle(new CGUIRenderControlHandle(*this, control)); |
| 78 |
|
| 79 |
std::shared_ptr<CGUIRenderTarget> renderTarget; |
| 80 |
if (m_factory != nullptr) |
| 81 |
renderTarget.reset(m_factory->CreateRenderControl(control)); |
| 82 |
|
| 83 |
m_renderTargets.insert(std::make_pair(renderHandle.get(), std::move(renderTarget))); |
| 84 |
|
| 85 |
return renderHandle; |
| 86 |
} |
| 87 |
|
| 88 |
std::shared_ptr<CGUIRenderHandle> CGUIGameRenderManager::RegisterWindow( |
| 89 |
CGameWindowFullScreen& window) |
| 90 |
{ |
| 91 |
std::unique_lock<CCriticalSection> lock(m_targetMutex); |
| 92 |
|
| 93 |
// Create handle for game window |
| 94 |
std::shared_ptr<CGUIRenderHandle> renderHandle(new CGUIRenderFullScreenHandle(*this, window)); |
| 95 |
|
| 96 |
std::shared_ptr<CGUIRenderTarget> renderTarget; |
| 97 |
if (m_factory != nullptr) |
| 98 |
renderTarget.reset(m_factory->CreateRenderFullScreen(window)); |
| 99 |
|
| 100 |
m_renderTargets.insert(std::make_pair(renderHandle.get(), std::move(renderTarget))); |
| 101 |
|
| 102 |
return renderHandle; |
| 103 |
} |
| 104 |
|
| 105 |
std::shared_ptr<CGUIGameVideoHandle> CGUIGameRenderManager::RegisterDialog( |
| 106 |
GAME::CDialogGameVideoSelect& dialog) |
| 107 |
{ |
| 108 |
return std::make_shared<CGUIGameVideoHandle>(*this); |
| 109 |
} |
| 110 |
|
| 111 |
std::shared_ptr<CGUIGameSettingsHandle> CGUIGameRenderManager::RegisterGameSettingsDialog() |
| 112 |
{ |
| 113 |
return std::make_shared<CGUIGameSettingsHandle>(*this); |
| 114 |
} |
| 115 |
|
| 116 |
void CGUIGameRenderManager::UnregisterHandle(CGUIRenderHandle* handle) |
| 117 |
{ |
| 118 |
std::unique_lock<CCriticalSection> lock(m_targetMutex); |
| 119 |
|
| 120 |
m_renderTargets.erase(handle); |
| 121 |
} |
| 122 |
|
| 123 |
void CGUIGameRenderManager::Render(CGUIRenderHandle* handle) |
| 124 |
{ |
| 125 |
std::unique_lock<CCriticalSection> lock(m_targetMutex); |
| 126 |
|
| 127 |
auto it = m_renderTargets.find(handle); |
| 128 |
if (it != m_renderTargets.end()) |
| 129 |
{ |
| 130 |
const std::shared_ptr<CGUIRenderTarget>& renderTarget = it->second; |
| 131 |
if (renderTarget) |
| 132 |
renderTarget->Render(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
void CGUIGameRenderManager::RenderEx(CGUIRenderHandle* handle) |
| 137 |
{ |
| 138 |
std::unique_lock<CCriticalSection> lock(m_targetMutex); |
| 139 |
|
| 140 |
auto it = m_renderTargets.find(handle); |
| 141 |
if (it != m_renderTargets.end()) |
| 142 |
{ |
| 143 |
const std::shared_ptr<CGUIRenderTarget>& renderTarget = it->second; |
| 144 |
if (renderTarget) |
| 145 |
renderTarget->RenderEx(); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
void CGUIGameRenderManager::ClearBackground(CGUIRenderHandle* handle) |
| 150 |
{ |
| 151 |
std::unique_lock<CCriticalSection> lock(m_targetMutex); |
| 152 |
|
| 153 |
auto it = m_renderTargets.find(handle); |
| 154 |
if (it != m_renderTargets.end()) |
| 155 |
{ |
| 156 |
const std::shared_ptr<CGUIRenderTarget>& renderTarget = it->second; |
| 157 |
if (renderTarget) |
| 158 |
renderTarget->ClearBackground(); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
bool CGUIGameRenderManager::IsDirty(CGUIRenderHandle* handle) |
| 163 |
{ |
| 164 |
std::unique_lock<CCriticalSection> lock(m_targetMutex); |
| 165 |
|
| 166 |
auto it = m_renderTargets.find(handle); |
| 167 |
if (it != m_renderTargets.end()) |
| 168 |
{ |
| 169 |
const std::shared_ptr<CGUIRenderTarget>& renderTarget = it->second; |
| 170 |
if (renderTarget) |
| 171 |
return renderTarget->IsDirty(); |
| 172 |
} |
| 173 |
|
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
bool CGUIGameRenderManager::IsPlayingGame() |
| 178 |
{ |
| 179 |
std::unique_lock<CCriticalSection> lock(m_callbackMutex); |
| 180 |
|
| 181 |
return m_callback != nullptr; |
| 182 |
} |
| 183 |
|
| 184 |
bool CGUIGameRenderManager::SupportsRenderFeature(RENDERFEATURE feature) |
| 185 |
{ |
| 186 |
std::unique_lock<CCriticalSection> lock(m_callbackMutex); |
| 187 |
|
| 188 |
if (m_callback != nullptr) |
| 189 |
return m_callback->SupportsRenderFeature(feature); |
| 190 |
|
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
bool CGUIGameRenderManager::SupportsScalingMethod(SCALINGMETHOD method) |
| 195 |
{ |
| 196 |
std::unique_lock<CCriticalSection> lock(m_callbackMutex); |
| 197 |
|
| 198 |
if (m_callback != nullptr) |
| 199 |
return m_callback->SupportsScalingMethod(method); |
| 200 |
|
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
std::string CGUIGameRenderManager::GameClientID() |
| 205 |
{ |
| 206 |
std::unique_lock<CCriticalSection> lock(m_callbackMutex); |
| 207 |
|
| 208 |
if (m_gameCallback != nullptr) |
| 209 |
return m_gameCallback->GameClientID(); |
| 210 |
|
| 211 |
return ""; |
| 212 |
} |
| 213 |
|
| 214 |
std::string CGUIGameRenderManager::GetPlayingGame() |
| 215 |
{ |
| 216 |
std::unique_lock<CCriticalSection> lock(m_callbackMutex); |
| 217 |
|
| 218 |
if (m_gameCallback != nullptr) |
| 219 |
return m_gameCallback->GetPlayingGame(); |
| 220 |
|
| 221 |
return ""; |
| 222 |
} |
| 223 |
|
| 224 |
std::string CGUIGameRenderManager::CreateSavestate(bool autosave) |
| 225 |
{ |
| 226 |
std::unique_lock<CCriticalSection> lock(m_callbackMutex); |
| 227 |
|
| 228 |
if (m_gameCallback != nullptr) |
| 229 |
return m_gameCallback->CreateSavestate(autosave); |
| 230 |
|
| 231 |
return ""; |
| 232 |
} |
| 233 |
|
| 234 |
bool CGUIGameRenderManager::UpdateSavestate(const std::string& savestatePath) |
| 235 |
{ |
| 236 |
std::unique_lock<CCriticalSection> lock(m_callbackMutex); |
| 237 |
|
| 238 |
if (m_gameCallback != nullptr) |
| 239 |
return m_gameCallback->UpdateSavestate(savestatePath); |
| 240 |
|
| 241 |
return false; |
| 242 |
} |
| 243 |
|
| 244 |
bool CGUIGameRenderManager::LoadSavestate(const std::string& savestatePath) |
| 245 |
{ |
| 246 |
std::unique_lock<CCriticalSection> lock(m_callbackMutex); |
| 247 |
|
| 248 |
if (m_gameCallback != nullptr) |
| 249 |
return m_gameCallback->LoadSavestate(savestatePath); |
| 250 |
|
| 251 |
return false; |
| 252 |
} |
| 253 |
|
| 254 |
void CGUIGameRenderManager::FreeSavestateResources(const std::string& savestatePath) |
| 255 |
{ |
| 256 |
std::unique_lock<CCriticalSection> lock(m_callbackMutex); |
| 257 |
|
| 258 |
if (m_gameCallback != nullptr) |
| 259 |
m_gameCallback->FreeSavestateResources(savestatePath); |
| 260 |
} |
| 261 |
|
| 262 |
void CGUIGameRenderManager::CloseOSD() |
| 263 |
{ |
| 264 |
std::unique_lock<CCriticalSection> lock(m_callbackMutex); |
| 265 |
|
| 266 |
if (m_gameCallback != nullptr) |
| 267 |
m_gameCallback->CloseOSDCallback(); |
| 268 |
} |
| 269 |
|
| 270 |
void CGUIGameRenderManager::UpdateRenderTargets() |
| 271 |
{ |
| 272 |
if (m_factory != nullptr) |
| 273 |
{ |
| 274 |
for (auto& it : m_renderTargets) |
| 275 |
{ |
| 276 |
CGUIRenderHandle* handle = it.first; |
| 277 |
std::shared_ptr<CGUIRenderTarget>& renderTarget = it.second; |
| 278 |
|
| 279 |
if (!renderTarget) |
| 280 |
renderTarget.reset(CreateRenderTarget(handle)); |
| 281 |
} |
| 282 |
} |
| 283 |
else |
| 284 |
{ |
| 285 |
for (auto& it : m_renderTargets) |
| 286 |
it.second.reset(); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
CGUIRenderTarget* CGUIGameRenderManager::CreateRenderTarget(CGUIRenderHandle* handle) |
| 291 |
{ |
| 292 |
switch (handle->Type()) |
| 293 |
{ |
| 294 |
case RENDER_HANDLE::CONTROL: |
| 295 |
{ |
| 296 |
CGUIRenderControlHandle* controlHandle = static_cast<CGUIRenderControlHandle*>(handle); |
| 297 |
return m_factory->CreateRenderControl(controlHandle->GetControl()); |
| 298 |
} |
| 299 |
case RENDER_HANDLE::WINDOW: |
| 300 |
{ |
| 301 |
CGUIRenderFullScreenHandle* fullScreenHandle = |
| 302 |
static_cast<CGUIRenderFullScreenHandle*>(handle); |
| 303 |
return m_factory->CreateRenderFullScreen(fullScreenHandle->GetWindow()); |
| 304 |
} |
| 305 |
default: |
| 306 |
break; |
| 307 |
} |
| 308 |
|
| 309 |
return nullptr; |
| 310 |
} |