CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/headless/SDLHeadlessHost.cpp
Views: 1401
1
// Copyright (c) 2017- 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
#ifdef SDL
19
20
#include <cstdio>
21
22
#include "ppsspp_config.h"
23
#if PPSSPP_PLATFORM(MAC)
24
#include "SDL2/SDL.h"
25
#else
26
#include "SDL.h"
27
#endif
28
29
#include "headless/SDLHeadlessHost.h"
30
#include "Common/GPU/OpenGL/GLCommon.h"
31
#include "Common/GPU/OpenGL/GLFeatures.h"
32
#include "Common/GPU/thin3d_create.h"
33
#include "Common/GPU/OpenGL/GLRenderManager.h"
34
#include "Common/File/VFS/VFS.h"
35
#include "Common/File/VFS/DirectoryReader.h"
36
#include "Common/GraphicsContext.h"
37
#include "Common/TimeUtil.h"
38
#include "Core/Config.h"
39
#include "Core/System.h"
40
#include "GPU/GPUState.h"
41
42
const bool WINDOW_VISIBLE = false;
43
const int WINDOW_WIDTH = 480;
44
const int WINDOW_HEIGHT = 272;
45
46
SDL_Window *CreateHiddenWindow() {
47
Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS;
48
if (!WINDOW_VISIBLE) {
49
flags |= SDL_WINDOW_HIDDEN;
50
}
51
return SDL_CreateWindow("PPSSPPHeadless", 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, flags);
52
}
53
54
class GLDummyGraphicsContext : public GraphicsContext {
55
public:
56
GLDummyGraphicsContext() {
57
}
58
~GLDummyGraphicsContext() { delete draw_; }
59
60
bool InitFromRenderThread(std::string *errorMessage) override;
61
62
void ShutdownFromRenderThread() override {
63
delete draw_;
64
draw_ = nullptr;
65
66
SDL_GL_DeleteContext(glContext_);
67
glContext_ = nullptr;
68
SDL_DestroyWindow(screen_);
69
screen_ = nullptr;
70
71
SDL_Quit();
72
}
73
74
Draw::DrawContext *GetDrawContext() override {
75
return draw_;
76
}
77
78
void ThreadStart() override {
79
renderManager_->ThreadStart(draw_);
80
}
81
82
bool ThreadFrame() override {
83
return renderManager_->ThreadFrame();
84
}
85
86
void ThreadEnd() override {
87
renderManager_->ThreadEnd();
88
}
89
90
void StopThread() override {
91
if (renderManager_) {
92
renderManager_->StopThread();
93
}
94
}
95
96
void Shutdown() override {}
97
void Resize() override {}
98
99
private:
100
Draw::DrawContext *draw_ = nullptr;
101
GLRenderManager *renderManager_ = nullptr;
102
SDL_Window *screen_;
103
SDL_GLContext glContext_;
104
};
105
106
bool GLDummyGraphicsContext::InitFromRenderThread(std::string *errorMessage) {
107
SDL_Init(SDL_INIT_VIDEO);
108
109
// TODO
110
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
111
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
112
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
113
114
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
115
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
116
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
117
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
118
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
119
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
120
121
screen_ = CreateHiddenWindow();
122
if (!screen_) {
123
const char *err = SDL_GetError();
124
printf("Failed to create offscreen window: %s\n", err ? err : "(unknown error)");
125
return false;
126
}
127
glContext_ = SDL_GL_CreateContext(screen_);
128
if (!glContext_) {
129
const char *err = SDL_GetError();
130
printf("Failed to create GL context: %s\n", err ? err : "(unknown error)");
131
return false;
132
}
133
134
// Ensure that the swap interval is set after context creation (needed for kmsdrm)
135
SDL_GL_SetSwapInterval(0);
136
137
#ifndef USING_GLES2
138
// Some core profile drivers elide certain extensions from GL_EXTENSIONS/etc.
139
// glewExperimental allows us to force GLEW to search for the pointers anyway.
140
if (gl_extensions.IsCoreContext)
141
glewExperimental = true;
142
if (GLEW_OK != glewInit()) {
143
printf("Failed to initialize glew!\n");
144
return false;
145
}
146
// Unfortunately, glew will generate an invalid enum error, ignore.
147
if (gl_extensions.IsCoreContext)
148
glGetError();
149
150
if (GLEW_VERSION_2_0) {
151
printf("OpenGL 2.0 or higher.\n");
152
} else {
153
printf("Sorry, this program requires OpenGL 2.0.\n");
154
return false;
155
}
156
#endif
157
158
CheckGLExtensions();
159
draw_ = Draw::T3DCreateGLContext(false);
160
renderManager_ = (GLRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
161
renderManager_->SetInflightFrames(g_Config.iInflightFrames);
162
SetGPUBackend(GPUBackend::OPENGL);
163
bool success = draw_->CreatePresets();
164
_assert_(success);
165
renderManager_->SetSwapFunction([&]() {
166
SDL_GL_SwapWindow(screen_);
167
});
168
return success;
169
}
170
171
bool SDLHeadlessHost::InitGraphics(std::string *error_message, GraphicsContext **ctx, GPUCore core) {
172
GraphicsContext *graphicsContext = new GLDummyGraphicsContext();
173
*ctx = graphicsContext;
174
gfx_ = graphicsContext;
175
176
std::thread th([&]{
177
while (threadState_ == RenderThreadState::IDLE)
178
sleep_ms(1);
179
threadState_ = RenderThreadState::STARTING;
180
181
std::string err;
182
if (!gfx_->InitFromRenderThread(&err)) {
183
threadState_ = RenderThreadState::START_FAILED;
184
return;
185
}
186
gfx_->ThreadStart();
187
threadState_ = RenderThreadState::STARTED;
188
189
while (threadState_ != RenderThreadState::STOP_REQUESTED) {
190
if (!gfx_->ThreadFrame()) {
191
break;
192
}
193
}
194
195
threadState_ = RenderThreadState::STOPPING;
196
gfx_->ThreadEnd();
197
gfx_->ShutdownFromRenderThread();
198
threadState_ = RenderThreadState::STOPPED;
199
});
200
th.detach();
201
202
threadState_ = RenderThreadState::START_REQUESTED;
203
while (threadState_ == RenderThreadState::START_REQUESTED || threadState_ == RenderThreadState::STARTING)
204
sleep_ms(1);
205
206
return threadState_ == RenderThreadState::STARTED;
207
}
208
209
void SDLHeadlessHost::ShutdownGraphics() {
210
gfx_->StopThread();
211
while (threadState_ != RenderThreadState::STOPPED && threadState_ != RenderThreadState::START_FAILED)
212
sleep_ms(1);
213
214
gfx_->Shutdown();
215
delete gfx_;
216
gfx_ = nullptr;
217
}
218
219
void SDLHeadlessHost::SwapBuffers() {
220
}
221
222
#endif
223
224