-
Notifications
You must be signed in to change notification settings - Fork 17
/
sdl_backend.cpp
295 lines (233 loc) · 8.65 KB
/
sdl_backend.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
#include "common.h"
#include "audio.h"
#include "cpu.h"
#include "input.h"
#ifdef RECORD_MOVIE
# include "movie.h"
#endif
#include "save_states.h"
#include "sdl_backend.h"
#ifdef RUN_TESTS
# include "test.h"
#endif
#include <SDL.h>
//
// Video
//
// Each pixel is scaled to scale_factor*scale_factor pixels
unsigned const scale_factor = 3;
static SDL_Window *screen;
static SDL_Renderer *renderer;
static SDL_Texture *screen_tex;
// On Unity with the Nouveau driver, displaying the frame sometimes blocks for
// a very long time (to the tune of only managing 30 FPS with everything
// removed but render calls when the translucent Ubuntu menu is open, and often
// less than 60 with Firefox open too). This in turn slows down emulation and
// messes up audio. To get around it, we upload frames in the SDL thread and
// keep a back buffer for drawing into from the emulation thread while the
// frame is being uploaded (a kind of manual triple buffering). If the frame
// doesn't upload in time for the next frame, we drop the new frame. This gives
// us automatic frame skipping in general.
//
// TODO: This could probably be optimized to eliminate some copying and format
// conversions.
static Uint32 *front_buffer;
static Uint32 *back_buffer;
static SDL_mutex *frame_lock;
static SDL_cond *frame_available_cond;
static bool ready_to_draw_new_frame;
static bool frame_available;
void put_pixel(unsigned x, unsigned y, uint32_t color) {
assert(x < 256);
assert(y < 240);
back_buffer[256*y + x] = color;
}
void draw_frame() {
#ifdef RECORD_MOVIE
add_movie_video_frame(back_buffer);
#endif
// Signal to the SDL thread that the frame has ended
SDL_LockMutex(frame_lock);
// Drop the new frame if the old one is still being rendered. This also
// means that we drop event processing for one frame, but it's probably not
// a huge deal.
if (ready_to_draw_new_frame) {
frame_available = true;
swap(back_buffer, front_buffer);
SDL_CondSignal(frame_available_cond);
}
SDL_UnlockMutex(frame_lock);
}
//
// Audio
//
Uint16 const sdl_audio_buffer_size = 2048;
static SDL_AudioDeviceID audio_device_id;
static void audio_callback(void*, Uint8 *stream, int len) {
assert(len >= 0);
read_samples((int16_t*)stream, len/sizeof(int16_t));
}
void lock_audio() { SDL_LockAudioDevice(audio_device_id); }
void unlock_audio() { SDL_UnlockAudioDevice(audio_device_id); }
void start_audio_playback() { SDL_PauseAudioDevice(audio_device_id, 0); }
void stop_audio_playback() { SDL_PauseAudioDevice(audio_device_id, 1); }
//
// Input
//
Uint8 const *keys;
//
// SDL thread and events
//
// Runs from emulation thread
void handle_ui_keys() {
SDL_LockMutex(event_lock);
if (keys[SDL_SCANCODE_S])
save_state();
else if (keys[SDL_SCANCODE_L])
load_state();
handle_rewind(keys[SDL_SCANCODE_R]);
if (reset_pushed)
soft_reset();
SDL_UnlockMutex(event_lock);
}
static bool pending_sdl_thread_exit;
// Protects the 'keys' array from being read while being updated
SDL_mutex *event_lock;
static void process_events() {
SDL_Event event;
SDL_LockMutex(event_lock);
while (SDL_PollEvent(&event))
if (event.type == SDL_QUIT) {
end_emulation();
pending_sdl_thread_exit = true;
#ifdef RUN_TESTS
end_testing = true;
#endif
}
SDL_UnlockMutex(event_lock);
}
void sdl_thread() {
for (;;) {
// Wait for the emulation thread to signal that a frame has completed
SDL_LockMutex(frame_lock);
ready_to_draw_new_frame = true;
while (!frame_available && !pending_sdl_thread_exit)
SDL_CondWait(frame_available_cond, frame_lock);
if (pending_sdl_thread_exit) {
SDL_UnlockMutex(frame_lock);
return;
}
frame_available = ready_to_draw_new_frame = false;
SDL_UnlockMutex(frame_lock);
// Process events and calculate controller input state (which might
// need left+right/up+down elimination)
process_events();
// Draw the new frame
fail_if(SDL_UpdateTexture(screen_tex, 0, front_buffer, 256*sizeof(Uint32)),
"failed to update screen texture: %s", SDL_GetError());
fail_if(SDL_RenderCopy(renderer, screen_tex, 0, 0),
"failed to copy rendered frame to render target: %s", SDL_GetError());
SDL_RenderPresent(renderer);
}
}
void exit_sdl_thread() {
SDL_LockMutex(frame_lock);
pending_sdl_thread_exit = true;
SDL_CondSignal(frame_available_cond);
SDL_UnlockMutex(frame_lock);
}
//
// Initialization and de-initialization
//
void init_sdl() {
SDL_version sdl_compiled_version, sdl_linked_version;
SDL_VERSION(&sdl_compiled_version);
SDL_GetVersion(&sdl_linked_version);
printf("Using SDL backend. Compiled against SDL %d.%d.%d, linked to SDL %d.%d.%d.\n",
sdl_compiled_version.major, sdl_compiled_version.minor, sdl_compiled_version.patch,
sdl_linked_version.major, sdl_linked_version.minor, sdl_linked_version.patch);
// SDL and video
// Make this configurable later
SDL_DisableScreenSaver();
fail_if(SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) != 0,
"failed to initialize SDL: %s", SDL_GetError());
fail_if(!(screen =
SDL_CreateWindow(
"Nesalizer",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
scale_factor*256, scale_factor*240,
0)),
"failed to create window: %s", SDL_GetError());
fail_if(!(renderer = SDL_CreateRenderer(screen, -1, 0)),
"failed to create rendering context: %s", SDL_GetError());
// Display some information about the renderer
SDL_RendererInfo renderer_info;
if (SDL_GetRendererInfo(renderer, &renderer_info))
puts("Failed to get renderer information from SDL");
else {
if (renderer_info.name)
printf("renderer: uses renderer \"%s\"\n", renderer_info.name);
if (renderer_info.flags & SDL_RENDERER_SOFTWARE)
puts("renderer: uses software rendering");
if (renderer_info.flags & SDL_RENDERER_ACCELERATED)
puts("renderer: uses hardware-accelerated rendering");
if (renderer_info.flags & SDL_RENDERER_PRESENTVSYNC)
puts("renderer: uses vsync");
if (renderer_info.flags & SDL_RENDERER_TARGETTEXTURE)
puts("renderer: supports rendering to texture");
printf("renderer: available texture formats:");
unsigned const n_texture_formats = min(16u, (unsigned)renderer_info.num_texture_formats);
for (unsigned i = 0; i < n_texture_formats; ++i)
printf(" %s", SDL_GetPixelFormatName(renderer_info.texture_formats[i]));
putchar('\n');
}
fail_if(!(screen_tex =
SDL_CreateTexture(
renderer,
// SDL takes endianess into account, so this becomes GL_RGBA8
// internally on little-endian systems
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
256, 240)),
"failed to create texture for screen: %s", SDL_GetError());
static Uint32 render_buffers[2][240*256];
back_buffer = render_buffers[0];
front_buffer = render_buffers[1];
// Audio
SDL_AudioSpec want;
SDL_zero(want);
want.freq = sample_rate;
want.format = AUDIO_S16SYS;
want.channels = 1;
want.samples = sdl_audio_buffer_size;
want.callback = audio_callback;
fail_if(!(audio_device_id = SDL_OpenAudioDevice(0, 0, &want, 0, 0)),
"failed to initialize audio: %s\n", SDL_GetError());
// Input
// We use SDL_GetKey/MouseState() instead
SDL_EventState(SDL_KEYDOWN , SDL_IGNORE);
SDL_EventState(SDL_KEYUP , SDL_IGNORE);
SDL_EventState(SDL_MOUSEBUTTONDOWN, SDL_IGNORE);
SDL_EventState(SDL_MOUSEBUTTONUP , SDL_IGNORE);
SDL_EventState(SDL_KEYUP , SDL_IGNORE);
SDL_EventState(SDL_MOUSEMOTION , SDL_IGNORE);
// Ignore window events for now
SDL_EventState(SDL_WINDOWEVENT, SDL_IGNORE);
keys = SDL_GetKeyboardState(0);
// SDL thread synchronization
fail_if(!(event_lock = SDL_CreateMutex()),
"failed to create event mutex: %s", SDL_GetError());
fail_if(!(frame_lock = SDL_CreateMutex()),
"failed to create frame mutex: %s", SDL_GetError());
fail_if(!(frame_available_cond = SDL_CreateCond()),
"failed to create frame condition variable: %s", SDL_GetError());
}
void deinit_sdl() {
SDL_DestroyRenderer(renderer); // Also destroys the texture
SDL_DestroyWindow(screen);
SDL_DestroyMutex(event_lock);
SDL_DestroyMutex(frame_lock);
SDL_DestroyCond(frame_available_cond);
SDL_CloseAudioDevice(audio_device_id); // Prolly not needed, but play it safe
SDL_Quit();
}