1 // Copyright (c) 2012- PPSSPP Project.
2
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
6
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 2.0 for more details.
11
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18 #include "ppsspp_config.h"
19 #include <cstdio>
20
21 #include "headless/WindowsHeadlessHost.h"
22
23 #include "Common/GPU/OpenGL/GLCommon.h"
24 #include "Common/GPU/OpenGL/GLFeatures.h"
25 #include "Common/File/VFS/VFS.h"
26 #include "Common/File/VFS/AssetReader.h"
27
28 #include "Common/CommonWindows.h"
29 #include "Common/Log.h"
30 #include "Common/File/FileUtil.h"
31 #include "Common/TimeUtil.h"
32
33 #include "Core/CoreParameter.h"
34 #include "Core/System.h"
35 #include "GPU/Common/GPUDebugInterface.h"
36 #include "GPU/GPUState.h"
37 #if PPSSPP_API(ANY_GL)
38 #include "Windows/GPU/WindowsGLContext.h"
39 #endif
40 #include "Windows/GPU/D3D9Context.h"
41 #include "Windows/GPU/D3D11Context.h"
42 #include "Windows/GPU/WindowsVulkanContext.h"
43
44 const bool WINDOW_VISIBLE = false;
45 const int WINDOW_WIDTH = 480;
46 const int WINDOW_HEIGHT = 272;
47
CreateHiddenWindow()48 HWND CreateHiddenWindow() {
49 static WNDCLASSEX wndClass = {
50 sizeof(WNDCLASSEX),
51 CS_HREDRAW | CS_VREDRAW | CS_OWNDC,
52 DefWindowProc,
53 0,
54 0,
55 NULL,
56 NULL,
57 LoadCursor(NULL, IDC_ARROW),
58 (HBRUSH) GetStockObject(BLACK_BRUSH),
59 NULL,
60 L"PPSSPPHeadless",
61 NULL,
62 };
63 RegisterClassEx(&wndClass);
64
65 DWORD style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_POPUP;
66 return CreateWindowEx(0, L"PPSSPPHeadless", L"PPSSPPHeadless", style, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, NULL, NULL);
67 }
68
LoadNativeAssets()69 void WindowsHeadlessHost::LoadNativeAssets()
70 {
71 VFSRegister("", new DirectoryAssetReader(Path("assets")));
72 VFSRegister("", new DirectoryAssetReader(Path("")));
73 VFSRegister("", new DirectoryAssetReader(Path("..")));
74 VFSRegister("", new DirectoryAssetReader(Path("../Windows/assets")));
75 VFSRegister("", new DirectoryAssetReader(Path("../Windows")));
76 }
77
SendDebugOutput(const std::string & output)78 void WindowsHeadlessHost::SendDebugOutput(const std::string &output)
79 {
80 fwrite(output.data(), sizeof(char), output.length(), stdout);
81 OutputDebugStringUTF8(output.c_str());
82 }
83
InitGraphics(std::string * error_message,GraphicsContext ** ctx)84 bool WindowsHeadlessHost::InitGraphics(std::string *error_message, GraphicsContext **ctx) {
85 hWnd = CreateHiddenWindow();
86
87 if (WINDOW_VISIBLE) {
88 ShowWindow(hWnd, TRUE);
89 SetFocus(hWnd);
90 }
91
92 WindowsGraphicsContext *graphicsContext = nullptr;
93 bool needRenderThread = false;
94 switch (gpuCore_) {
95 case GPUCORE_GLES:
96 #if PPSSPP_API(ANY_GL)
97 case GPUCORE_SOFTWARE:
98 graphicsContext = new WindowsGLContext();
99 needRenderThread = true;
100 break;
101 #endif
102 case GPUCORE_DIRECTX9:
103 graphicsContext = new D3D9Context();
104 break;
105
106 case GPUCORE_DIRECTX11:
107 graphicsContext = new D3D11Context();
108 break;
109
110 case GPUCORE_VULKAN:
111 graphicsContext = new WindowsVulkanContext();
112 break;
113 }
114
115 if (graphicsContext->Init(NULL, hWnd, error_message)) {
116 *ctx = graphicsContext;
117 gfx_ = graphicsContext;
118 } else {
119 delete graphicsContext;
120 *ctx = nullptr;
121 gfx_ = nullptr;
122 return false;
123 }
124
125 if (needRenderThread) {
126 std::thread th([&]{
127 while (threadState_ == RenderThreadState::IDLE)
128 sleep_ms(1);
129 threadState_ = RenderThreadState::STARTING;
130
131 std::string err;
132 if (!gfx_->InitFromRenderThread(&err)) {
133 threadState_ = RenderThreadState::START_FAILED;
134 return;
135 }
136 gfx_->ThreadStart();
137 threadState_ = RenderThreadState::STARTED;
138
139 while (threadState_ != RenderThreadState::STOP_REQUESTED) {
140 if (!gfx_->ThreadFrame()) {
141 break;
142 }
143 gfx_->SwapBuffers();
144 }
145
146 threadState_ = RenderThreadState::STOPPING;
147 gfx_->ThreadEnd();
148 gfx_->ShutdownFromRenderThread();
149 threadState_ = RenderThreadState::STOPPED;
150 });
151 th.detach();
152 }
153
154 LoadNativeAssets();
155
156 if (needRenderThread) {
157 threadState_ = RenderThreadState::START_REQUESTED;
158 while (threadState_ == RenderThreadState::START_REQUESTED || threadState_ == RenderThreadState::STARTING)
159 sleep_ms(1);
160
161 return threadState_ == RenderThreadState::STARTED;
162 }
163
164 return true;
165 }
166
ShutdownGraphics()167 void WindowsHeadlessHost::ShutdownGraphics() {
168 gfx_->StopThread();
169 while (threadState_ != RenderThreadState::STOPPED && threadState_ != RenderThreadState::IDLE)
170 sleep_ms(1);
171
172 gfx_->Shutdown();
173 delete gfx_;
174 gfx_ = nullptr;
175 DestroyWindow(hWnd);
176 hWnd = NULL;
177 }
178
SwapBuffers()179 void WindowsHeadlessHost::SwapBuffers() {
180 if (gpuCore_ == GPUCORE_DIRECTX9) {
181 MSG msg;
182 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
183 TranslateMessage(&msg);
184 DispatchMessage(&msg);
185 }
186 gfx_->SwapBuffers();
187 }
188