forked from cebix/macemu
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathvideo_sdl2.cpp
2725 lines (2365 loc) · 71.6 KB
/
video_sdl2.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* video_sdl2.cpp - Video/graphics emulation, SDL 2.x specific stuff
*
* Basilisk II (C) 1997-2008 Christian Bauer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* NOTES:
* The Ctrl key works like a qualifier for special actions:
* Ctrl-Tab = suspend DGA mode (TODO)
* Ctrl-Esc = emergency quit
* Ctrl-F1 = mount floppy
* Ctrl-F5 = grab mouse (in windowed mode)
*
* FIXMEs and TODOs:
* - Windows requires an extra mouse event to update the actual cursor image?
* - Ctr-Tab for suspend/resume but how? SDL does not support that for non-Linux
* - Ctrl-Fn doesn't generate SDL_KEYDOWN events (SDL bug?)
* - Mouse acceleration, there is no API in SDL yet for that
* - Gamma tables support is likely to be broken here
* - Events processing is bound to the general emulation thread as SDL requires
* to PumpEvents() within the same thread as the one that called SetVideoMode().
* Besides, there can't seem to be a way to call SetVideoMode() from a child thread.
* - Backport hw cursor acceleration to Basilisk II?
* - Factor out code
*/
#include "sysdeps.h"
#include <SDL.h>
#if SDL_VERSION_ATLEAST(2,0,0)
#include <SDL_mutex.h>
#include <SDL_thread.h>
#include <errno.h>
#include <vector>
#include <string>
#ifdef WIN32
#include <malloc.h> /* alloca() */
#endif
#include <cpu_emulation.h>
#include "main.h"
#include "adb.h"
#include "macos_util.h"
#include "prefs.h"
#include "user_strings.h"
#include "video.h"
#include "video_defs.h"
#include "video_blit.h"
#include "vm_alloc.h"
#define DEBUG 0
#include "debug.h"
// Supported video modes
using std::vector;
static vector<VIDEO_MODE> VideoModes;
// Display types
#ifdef SHEEPSHAVER
enum {
DISPLAY_WINDOW = DIS_WINDOW, // windowed display
DISPLAY_SCREEN = DIS_SCREEN // fullscreen display
};
extern int display_type; // See enum above
#else
enum {
DISPLAY_WINDOW, // windowed display
DISPLAY_SCREEN // fullscreen display
};
static int display_type = DISPLAY_WINDOW; // See enum above
#endif
// Constants
#ifdef WIN32
const char KEYCODE_FILE_NAME[] = "BasiliskII_keycodes";
#elif __MACOSX__
const char KEYCODE_FILE_NAME[] = "BasiliskII_keycodes";
#else
const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
#endif
// Global variables
static uint32 frame_skip; // Prefs items
static int16 mouse_wheel_mode;
static int16 mouse_wheel_lines;
static uint8 *the_buffer = NULL; // Mac frame buffer (where MacOS draws into)
static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer (for refreshed modes)
static uint32 the_buffer_size; // Size of allocated the_buffer
static bool redraw_thread_active = false; // Flag: Redraw thread installed
#ifndef USE_CPU_EMUL_SERVICES
static volatile bool redraw_thread_cancel; // Flag: Cancel Redraw thread
static SDL_Thread *redraw_thread = NULL; // Redraw thread
static volatile bool thread_stop_req = false;
static volatile bool thread_stop_ack = false; // Acknowledge for thread_stop_req
#endif
#ifdef ENABLE_VOSF
static bool use_vosf = false; // Flag: VOSF enabled
#else
static const bool use_vosf = false; // VOSF not possible
#endif
static bool ctrl_down = false; // Flag: Ctrl key pressed
static bool opt_down = false; // Flag: Opt key pressed
static bool cmd_down = false; // Flag: Cmd key pressed
static bool quit_full_screen = false; // Flag: DGA close requested from redraw thread
static bool emerg_quit = false; // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread
static bool emul_suspended = false; // Flag: Emulator suspended
static bool classic_mode = false; // Flag: Classic Mac video mode
static bool use_keycodes = false; // Flag: Use keycodes rather than keysyms
static int keycode_table[256]; // X keycode -> Mac keycode translation table
// SDL variables
SDL_Window * sdl_window = NULL; // Wraps an OS-native window
static SDL_Surface * host_surface = NULL; // Surface in host-OS display format
static SDL_Surface * guest_surface = NULL; // Surface in guest-OS display format
static SDL_Renderer * sdl_renderer = NULL; // Handle to SDL2 renderer
static SDL_threadID sdl_renderer_thread_id = 0; // Thread ID where the SDL_renderer was created, and SDL_renderer ops should run (for compatibility w/ d3d9)
static SDL_Texture * sdl_texture = NULL; // Handle to a GPU texture, with which to draw guest_surface to
static SDL_Rect sdl_update_video_rect = {0,0,0,0}; // Union of all rects to update, when updating sdl_texture
static SDL_mutex * sdl_update_video_mutex = NULL; // Mutex to protect sdl_update_video_rect
static int screen_depth; // Depth of current screen
static SDL_Cursor *sdl_cursor = NULL; // Copy of Mac cursor
static SDL_Palette *sdl_palette = NULL; // Color palette to be used as CLUT and gamma table
static bool sdl_palette_changed = false; // Flag: Palette changed, redraw thread must set new colors
static bool toggle_fullscreen = false;
static bool did_add_event_watch = false;
static bool mouse_grabbed = false;
// Mutex to protect SDL events
static SDL_mutex *sdl_events_lock = NULL;
#define LOCK_EVENTS SDL_LockMutex(sdl_events_lock)
#define UNLOCK_EVENTS SDL_UnlockMutex(sdl_events_lock)
// Mutex to protect palette
static SDL_mutex *sdl_palette_lock = NULL;
#define LOCK_PALETTE SDL_LockMutex(sdl_palette_lock)
#define UNLOCK_PALETTE SDL_UnlockMutex(sdl_palette_lock)
// Mutex to protect frame buffer
static SDL_mutex *frame_buffer_lock = NULL;
#define LOCK_FRAME_BUFFER SDL_LockMutex(frame_buffer_lock)
#define UNLOCK_FRAME_BUFFER SDL_UnlockMutex(frame_buffer_lock)
// Video refresh function
static void VideoRefreshInit(void);
static void (*video_refresh)(void);
// Prototypes
static int redraw_func(void *arg);
static int present_sdl_video();
static int SDLCALL on_sdl_event_generated(void *userdata, SDL_Event * event);
static bool is_fullscreen(SDL_Window *);
// From sys_unix.cpp
extern void SysMountFirstFloppy(void);
/*
* SDL surface locking glue
*/
#ifdef ENABLE_VOSF
#define SDL_VIDEO_LOCK_VOSF_SURFACE(SURFACE) do { \
if (sdl_window && SDL_GetWindowFlags(sdl_window) & (SDL_WINDOW_FULLSCREEN)) \
the_host_buffer = (uint8 *)(SURFACE)->pixels; \
} while (0)
#else
#define SDL_VIDEO_LOCK_VOSF_SURFACE(SURFACE)
#endif
#define SDL_VIDEO_LOCK_SURFACE(SURFACE) do { \
if (SDL_MUSTLOCK(SURFACE)) { \
SDL_LockSurface(SURFACE); \
SDL_VIDEO_LOCK_VOSF_SURFACE(SURFACE); \
} \
} while (0)
#define SDL_VIDEO_UNLOCK_SURFACE(SURFACE) do { \
if (SDL_MUSTLOCK(SURFACE)) \
SDL_UnlockSurface(SURFACE); \
} while (0)
/*
* Framebuffer allocation routines
*/
static void *vm_acquire_framebuffer(uint32 size)
{
// always try to reallocate framebuffer at the same address
static void *fb = VM_MAP_FAILED;
if (fb != VM_MAP_FAILED) {
if (vm_acquire_fixed(fb, size) < 0) {
#ifndef SHEEPSHAVER
printf("FATAL: Could not reallocate framebuffer at previous address\n");
#endif
fb = VM_MAP_FAILED;
}
}
if (fb == VM_MAP_FAILED)
fb = vm_acquire(size, VM_MAP_DEFAULT | VM_MAP_32BIT);
return fb;
}
static inline void vm_release_framebuffer(void *fb, uint32 size)
{
vm_release(fb, size);
}
static inline int get_customized_color_depth(int default_depth)
{
int display_color_depth = PrefsFindInt32("displaycolordepth");
D(bug("Get displaycolordepth %d\n", display_color_depth));
if(0 == display_color_depth)
return default_depth;
else{
switch (display_color_depth) {
case 8:
return VIDEO_DEPTH_8BIT;
case 15: case 16:
return VIDEO_DEPTH_16BIT;
case 24: case 32:
return VIDEO_DEPTH_32BIT;
default:
return default_depth;
}
}
}
/*
* Windows message handler
*/
#ifdef WIN32
#include <dbt.h>
static WNDPROC sdl_window_proc = NULL; // Window proc used by SDL
extern void SysMediaArrived(void);
extern void SysMediaRemoved(void);
extern HWND GetMainWindowHandle(void);
static LRESULT CALLBACK windows_message_handler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_DEVICECHANGE:
if (wParam == DBT_DEVICEREMOVECOMPLETE) {
DEV_BROADCAST_HDR *p = (DEV_BROADCAST_HDR *)lParam;
if (p->dbch_devicetype == DBT_DEVTYP_VOLUME)
SysMediaRemoved();
}
else if (wParam == DBT_DEVICEARRIVAL) {
DEV_BROADCAST_HDR *p = (DEV_BROADCAST_HDR *)lParam;
if (p->dbch_devicetype == DBT_DEVTYP_VOLUME)
SysMediaArrived();
}
return 0;
default:
if (sdl_window_proc)
return CallWindowProc(sdl_window_proc, hwnd, msg, wParam, lParam);
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
#endif
/*
* SheepShaver glue
*/
#ifdef SHEEPSHAVER
// Color depth modes type
typedef int video_depth;
// 1, 2, 4 and 8 bit depths use a color palette
static inline bool IsDirectMode(VIDEO_MODE const & mode)
{
return IsDirectMode(mode.viAppleMode);
}
// Abstract base class representing one (possibly virtual) monitor
// ("monitor" = rectangular display with a contiguous frame buffer)
class monitor_desc {
public:
monitor_desc(const vector<VIDEO_MODE> &available_modes, video_depth default_depth, uint32 default_id) {}
virtual ~monitor_desc() {}
// Get current Mac frame buffer base address
uint32 get_mac_frame_base(void) const {return screen_base;}
// Set Mac frame buffer base address (called from switch_to_mode())
void set_mac_frame_base(uint32 base) {screen_base = base;}
// Get current video mode
const VIDEO_MODE &get_current_mode(void) const {return VModes[cur_mode];}
// Called by the video driver to switch the video mode on this display
// (must call set_mac_frame_base())
virtual void switch_to_current_mode(void) = 0;
// Called by the video driver to set the color palette (in indexed modes)
// or the gamma table (in direct modes)
virtual void set_palette(uint8 *pal, int num) = 0;
};
// Vector of pointers to available monitor descriptions, filled by VideoInit()
static vector<monitor_desc *> VideoMonitors;
// Find Apple mode matching best specified dimensions
static int find_apple_resolution(int xsize, int ysize)
{
if (xsize == 640 && ysize == 480)
return APPLE_640x480;
if (xsize == 800 && ysize == 600)
return APPLE_800x600;
if (xsize == 1024 && ysize == 768)
return APPLE_1024x768;
if (xsize == 1152 && ysize == 768)
return APPLE_1152x768;
if (xsize == 1152 && ysize == 900)
return APPLE_1152x900;
if (xsize == 1280 && ysize == 1024)
return APPLE_1280x1024;
if (xsize == 1600 && ysize == 1200)
return APPLE_1600x1200;
return APPLE_CUSTOM;
}
// Display error alert
static void ErrorAlert(int error)
{
ErrorAlert(GetString(error));
}
#endif
/*
* monitor_desc subclass for SDL display
*/
class SDL_monitor_desc : public monitor_desc {
public:
SDL_monitor_desc(const vector<VIDEO_MODE> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
~SDL_monitor_desc() {}
virtual void switch_to_current_mode(void);
virtual void set_palette(uint8 *pal, int num);
bool video_open(void);
void video_close(void);
};
/*
* Utility functions
*/
// Find palette size for given color depth
static int palette_size(int mode)
{
switch (mode) {
case VIDEO_DEPTH_1BIT: return 2;
case VIDEO_DEPTH_2BIT: return 4;
case VIDEO_DEPTH_4BIT: return 16;
case VIDEO_DEPTH_8BIT: return 256;
case VIDEO_DEPTH_16BIT: return 32;
case VIDEO_DEPTH_32BIT: return 256;
default: return 0;
}
}
// Map video_mode depth ID to numerical depth value
static int mac_depth_of_video_depth(int video_depth)
{
int depth = -1;
switch (video_depth) {
case VIDEO_DEPTH_1BIT:
depth = 1;
break;
case VIDEO_DEPTH_2BIT:
depth = 2;
break;
case VIDEO_DEPTH_4BIT:
depth = 4;
break;
case VIDEO_DEPTH_8BIT:
depth = 8;
break;
case VIDEO_DEPTH_16BIT:
depth = 16;
break;
case VIDEO_DEPTH_32BIT:
depth = 32;
break;
default:
abort();
}
return depth;
}
// Map video_mode depth ID to SDL screen depth
static int sdl_depth_of_video_depth(int video_depth)
{
return (video_depth <= VIDEO_DEPTH_8BIT) ? 8 : mac_depth_of_video_depth(video_depth);
}
// Get screen dimensions
static void sdl_display_dimensions(int &width, int &height)
{
SDL_DisplayMode desktop_mode;
const int display_index = 0; // TODO: try supporting multiple displays
if (SDL_GetDesktopDisplayMode(display_index, &desktop_mode) != 0) {
// TODO: report a warning, here?
width = height = 0;
return;
}
width = desktop_mode.w;
height = desktop_mode.h;
}
static inline int sdl_display_width(void)
{
int width, height;
sdl_display_dimensions(width, height);
return width;
}
static inline int sdl_display_height(void)
{
int width, height;
sdl_display_dimensions(width, height);
return height;
}
// Check whether specified mode is available
static bool has_mode(int type, int width, int height, int depth)
{
// Filter out out-of-bounds resolutions
if (width > sdl_display_width() || height > sdl_display_height())
return false;
// Whatever size it is, beyond what we've checked, we'll scale to/from as appropriate.
return true;
}
// Add mode to list of supported modes
static void add_mode(int type, int width, int height, int resolution_id, int bytes_per_row, int depth)
{
// Filter out unsupported modes
if (!has_mode(type, width, height, depth))
return;
// Fill in VideoMode entry
VIDEO_MODE mode;
#ifdef SHEEPSHAVER
resolution_id = find_apple_resolution(width, height);
mode.viType = type;
#endif
VIDEO_MODE_X = width;
VIDEO_MODE_Y = height;
VIDEO_MODE_RESOLUTION = resolution_id;
VIDEO_MODE_ROW_BYTES = bytes_per_row;
VIDEO_MODE_DEPTH = (video_depth)depth;
VideoModes.push_back(mode);
}
// Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
static void set_mac_frame_buffer(SDL_monitor_desc &monitor, int depth, bool native_byte_order)
{
#if !REAL_ADDRESSING && !DIRECT_ADDRESSING
int layout = FLAYOUT_DIRECT;
if (depth == VIDEO_DEPTH_16BIT)
layout = (screen_depth == 15) ? FLAYOUT_HOST_555 : FLAYOUT_HOST_565;
else if (depth == VIDEO_DEPTH_32BIT)
layout = (screen_depth == 24) ? FLAYOUT_HOST_888 : FLAYOUT_DIRECT;
if (native_byte_order)
MacFrameLayout = layout;
else
MacFrameLayout = FLAYOUT_DIRECT;
monitor.set_mac_frame_base(MacFrameBaseMac);
// Set variables used by UAE memory banking
const VIDEO_MODE &mode = monitor.get_current_mode();
MacFrameBaseHost = the_buffer;
MacFrameSize = VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y;
InitFrameBufferMapping();
#else
monitor.set_mac_frame_base(Host2MacAddr(the_buffer));
#endif
D(bug("monitor.mac_frame_base = %08x\n", monitor.get_mac_frame_base()));
}
// Set window name and class
static void set_window_name(int name)
{
if (!sdl_window) {
return;
}
const char *str = GetString(name);
SDL_SetWindowTitle(sdl_window, str);
}
static void set_window_name_grabbed() {
if (!sdl_window) return;
int hotkey = PrefsFindInt32("hotkey");
if (!hotkey) hotkey = 1;
std::string s = GetString(STR_WINDOW_TITLE_GRABBED0);
if (hotkey & 1) s += GetString(STR_WINDOW_TITLE_GRABBED1);
if (hotkey & 2) s += GetString(STR_WINDOW_TITLE_GRABBED2);
if (hotkey & 4) s += GetString(STR_WINDOW_TITLE_GRABBED3);
s += GetString(STR_WINDOW_TITLE_GRABBED4);
SDL_SetWindowTitle(sdl_window, s.c_str());
}
// Set mouse grab mode
static void set_grab_mode(bool grab)
{
if (!sdl_window) {
return;
}
SDL_SetWindowGrab(sdl_window, grab ? SDL_TRUE : SDL_FALSE);
}
// Migrate preferences items (XXX to be handled in MigratePrefs())
static void migrate_screen_prefs(void)
{
#ifdef SHEEPSHAVER
// Look-up priorities are: "screen", "screenmodes", "windowmodes".
if (PrefsFindString("screen"))
return;
uint32 window_modes = PrefsFindInt32("windowmodes");
uint32 screen_modes = PrefsFindInt32("screenmodes");
int width = 0, height = 0;
if (screen_modes) {
static const struct {
int id;
int width;
int height;
}
modes[] = {
{ 1, 640, 480 },
{ 2, 800, 600 },
{ 4, 1024, 768 },
{ 64, 1152, 768 },
{ 8, 1152, 900 },
{ 16, 1280, 1024 },
{ 32, 1600, 1200 },
{ 0, }
};
for (int i = 0; modes[i].id != 0; i++) {
if (screen_modes & modes[i].id) {
if (width < modes[i].width && height < modes[i].height) {
width = modes[i].width;
height = modes[i].height;
}
}
}
} else {
if (window_modes & 1)
width = 640, height = 480;
if (window_modes & 2)
width = 800, height = 600;
}
if (width && height) {
char str[32];
sprintf(str, "%s/%d/%d", screen_modes ? "dga" : "win", width, height);
PrefsReplaceString("screen", str);
}
#endif
}
/*
* Display "driver" classes
*/
class driver_base {
public:
driver_base(SDL_monitor_desc &m);
~driver_base();
void init(); // One-time init
void set_video_mode(int flags);
void adapt_to_video_mode();
void update_palette(void);
void suspend(void) {}
void resume(void) {}
void toggle_mouse_grab(void);
void mouse_moved(int x, int y) { ADBMouseMoved(x, y); }
void disable_mouse_accel(void);
void restore_mouse_accel(void);
void grab_mouse(void);
void ungrab_mouse(void);
public:
SDL_monitor_desc &monitor; // Associated video monitor
const VIDEO_MODE &mode; // Video mode handled by the driver
bool init_ok; // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
SDL_Surface *s; // The surface we draw into
};
#ifdef ENABLE_VOSF
static void update_display_window_vosf(driver_base *drv);
#endif
static void update_display_static(driver_base *drv);
static driver_base *drv = NULL; // Pointer to currently used driver object
#ifdef ENABLE_VOSF
# include "video_vosf.h"
#endif
driver_base::driver_base(SDL_monitor_desc &m)
: monitor(m), mode(m.get_current_mode()), init_ok(false), s(NULL)
{
the_buffer = NULL;
the_buffer_copy = NULL;
}
static void delete_sdl_video_surfaces()
{
if (sdl_texture) {
SDL_DestroyTexture(sdl_texture);
sdl_texture = NULL;
}
if (host_surface) {
if (host_surface == guest_surface) {
guest_surface = NULL;
}
SDL_FreeSurface(host_surface);
host_surface = NULL;
}
if (guest_surface) {
SDL_FreeSurface(guest_surface);
guest_surface = NULL;
}
}
static void delete_sdl_video_window()
{
if (sdl_renderer) {
SDL_DestroyRenderer(sdl_renderer);
sdl_renderer = NULL;
}
if (sdl_window) {
SDL_DestroyWindow(sdl_window);
sdl_window = NULL;
}
}
static void shutdown_sdl_video()
{
delete_sdl_video_surfaces();
delete_sdl_video_window();
}
static SDL_Surface * init_sdl_video(int width, int height, int bpp, Uint32 flags)
{
if (guest_surface) {
delete_sdl_video_surfaces();
}
int window_width = width;
int window_height = height;
Uint32 window_flags = SDL_WINDOW_ALLOW_HIGHDPI;
const int window_flags_to_monitor = SDL_WINDOW_FULLSCREEN;
if (flags & SDL_WINDOW_FULLSCREEN) {
SDL_DisplayMode desktop_mode;
if (SDL_GetDesktopDisplayMode(0, &desktop_mode) != 0) {
shutdown_sdl_video();
return NULL;
}
#ifdef __MACOSX__
window_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
#else
window_flags |= SDL_WINDOW_FULLSCREEN;
#endif
window_width = desktop_mode.w;
window_height = desktop_mode.h;
}
if (sdl_window) {
int old_window_width, old_window_height, old_window_flags;
SDL_GetWindowSize(sdl_window, &old_window_width, &old_window_height);
old_window_flags = SDL_GetWindowFlags(sdl_window);
if (old_window_width != window_width ||
old_window_height != window_height ||
(old_window_flags & window_flags_to_monitor) != (window_flags & window_flags_to_monitor))
{
delete_sdl_video_window();
}
}
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, PrefsFindBool("scale_nearest") ? "nearest" : "linear");
/*
// Always use a resize-able window. This helps allow SDL to manage
// transitions involving fullscreen to or from windowed-mode.
window_flags |= SDL_WINDOW_RESIZABLE;
*/
if (!sdl_window) {
sdl_window = SDL_CreateWindow(
"Basilisk II",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
window_width,
window_height,
window_flags);
if (!sdl_window) {
shutdown_sdl_video();
return NULL;
}
}
if (flags & SDL_WINDOW_FULLSCREEN) SDL_SetWindowGrab(sdl_window, SDL_TRUE);
// Some SDL events (regarding some native-window events), need processing
// as they are generated. SDL2 has a facility, SDL_AddEventWatch(), which
// allows events to be processed as they are generated.
if (!did_add_event_watch) {
SDL_AddEventWatch(&on_sdl_event_generated, NULL);
did_add_event_watch = true;
}
if (!sdl_renderer) {
#ifdef WIN32
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software");
#else
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "");
#endif
sdl_renderer = SDL_CreateRenderer(sdl_window, -1, 0);
if (!sdl_renderer) {
shutdown_sdl_video();
return NULL;
}
sdl_renderer_thread_id = SDL_ThreadID();
SDL_RendererInfo info;
memset(&info, 0, sizeof(info));
SDL_GetRendererInfo(sdl_renderer, &info);
printf("Using SDL_Renderer driver: %s\n", (info.name ? info.name : "(null)"));
}
if (!sdl_update_video_mutex) {
sdl_update_video_mutex = SDL_CreateMutex();
}
SDL_assert(sdl_texture == NULL);
sdl_texture = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height);
if (!sdl_texture) {
shutdown_sdl_video();
return NULL;
}
sdl_update_video_rect.x = 0;
sdl_update_video_rect.y = 0;
sdl_update_video_rect.w = 0;
sdl_update_video_rect.h = 0;
SDL_assert(guest_surface == NULL);
SDL_assert(host_surface == NULL);
switch (bpp) {
case 8:
guest_surface = SDL_CreateRGBSurface(0, width, height, 8, 0, 0, 0, 0);
break;
case 16:
guest_surface = SDL_CreateRGBSurface(0, width, height, 16, 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000);
break;
case 32:
guest_surface = SDL_CreateRGBSurface(0, width, height, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
host_surface = guest_surface;
break;
default:
printf("WARNING: An unsupported bpp of %d was used\n", bpp);
break;
}
if (!guest_surface) {
shutdown_sdl_video();
return NULL;
}
if (!host_surface) {
Uint32 texture_format;
if (SDL_QueryTexture(sdl_texture, &texture_format, NULL, NULL, NULL) != 0) {
printf("ERROR: Unable to get the SDL texture's pixel format: %s\n", SDL_GetError());
shutdown_sdl_video();
return NULL;
}
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
if (!SDL_PixelFormatEnumToMasks(texture_format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
printf("ERROR: Unable to determine format for host SDL_surface: %s\n", SDL_GetError());
shutdown_sdl_video();
return NULL;
}
host_surface = SDL_CreateRGBSurface(0, width, height, bpp, Rmask, Gmask, Bmask, Amask);
if (!host_surface) {
printf("ERROR: Unable to create host SDL_surface: %s\n", SDL_GetError());
shutdown_sdl_video();
return NULL;
}
}
if (SDL_RenderSetLogicalSize(sdl_renderer, width, height) != 0) {
printf("ERROR: Unable to set SDL rendeer's logical size (to %dx%d): %s\n",
width, height, SDL_GetError());
shutdown_sdl_video();
return NULL;
}
SDL_RenderSetIntegerScale(sdl_renderer, PrefsFindBool("scale_integer") ? SDL_TRUE : SDL_FALSE);
return guest_surface;
}
static int present_sdl_video()
{
if (SDL_RectEmpty(&sdl_update_video_rect)) return 0;
if (!sdl_renderer || !sdl_texture || !guest_surface) {
printf("WARNING: A video mode does not appear to have been set.\n");
return -1;
}
// Some systems, such as D3D9, can fail if and when they are used across
// certain operations. To address this, only utilize SDL_Renderer in a
// single thread, preferably the main thread.
//
// This was added as part of a fix for /~https://github.com/DavidLudwig/macemu/issues/21
// "BasiliskII, Win32: resizing a window does not stretch "
SDL_assert(SDL_ThreadID() == sdl_renderer_thread_id);
// Make sure the display's internal (to SDL, possibly the OS) buffer gets
// cleared. Not doing so can, if and when letterboxing is applied (whereby
// colored bars are drawn on the screen's sides to help with aspect-ratio
// correction), the colored bars can be an unknown color.
SDL_SetRenderDrawColor(sdl_renderer, 0, 0, 0, 0); // Use black
SDL_RenderClear(sdl_renderer); // Clear the display
// We're about to work with sdl_update_video_rect, so stop other threads from
// modifying it!
LOCK_PALETTE;
SDL_LockMutex(sdl_update_video_mutex);
// Convert from the guest OS' pixel format, to the host OS' texture, if necessary.
if (host_surface != guest_surface &&
host_surface != NULL &&
guest_surface != NULL)
{
SDL_Rect destRect = sdl_update_video_rect;
int result = SDL_BlitSurface(guest_surface, &sdl_update_video_rect, host_surface, &destRect);
if (result != 0) {
SDL_UnlockMutex(sdl_update_video_mutex);
UNLOCK_PALETTE;
return -1;
}
}
UNLOCK_PALETTE; // passed potential deadlock, can unlock palette
// Update the host OS' texture
void * srcPixels = (void *)((uint8_t *)host_surface->pixels +
sdl_update_video_rect.y * host_surface->pitch +
sdl_update_video_rect.x * host_surface->format->BytesPerPixel);
if (SDL_UpdateTexture(sdl_texture, &sdl_update_video_rect, srcPixels, host_surface->pitch) != 0) {
SDL_UnlockMutex(sdl_update_video_mutex);
return -1;
}
// We are done working with pixels in host_surface. Reset sdl_update_video_rect, then let
// other threads modify it, as-needed.
sdl_update_video_rect.x = 0;
sdl_update_video_rect.y = 0;
sdl_update_video_rect.w = 0;
sdl_update_video_rect.h = 0;
SDL_UnlockMutex(sdl_update_video_mutex);
// Copy the texture to the display
if (SDL_RenderCopy(sdl_renderer, sdl_texture, NULL, NULL) != 0) {
return -1;
}
// Update the display
SDL_RenderPresent(sdl_renderer);
// Indicate success to the caller!
return 0;
}
void update_sdl_video(SDL_Surface *s, int numrects, SDL_Rect *rects)
{
// TODO: make sure SDL_Renderer resources get displayed, if and when
// MacsBug is running (and VideoInterrupt() might not get called)
SDL_LockMutex(sdl_update_video_mutex);
for (int i = 0; i < numrects; ++i) {
SDL_UnionRect(&sdl_update_video_rect, &rects[i], &sdl_update_video_rect);
}
SDL_UnlockMutex(sdl_update_video_mutex);
}
void update_sdl_video(SDL_Surface *s, Sint32 x, Sint32 y, Sint32 w, Sint32 h)
{
SDL_Rect temp = {x, y, w, h};
update_sdl_video(s, 1, &temp);
}
#ifdef SHEEPSHAVER
static void MagBits(Uint8 *dst, Uint8 *src, int mag) {
for (int y = 0; y < 16; y++)
for (int x = 0; x < 16; x++) {
int sa = 16 * y + x;
if (!(src[sa >> 3] & 0x80 >> (sa & 7))) continue;
for (int dy = 0; dy < mag; dy++)
for (int dx = 0; dx < mag; dx++) {
int da = 16 * mag * (mag * y + dy) + mag * x + dx;
dst[da >> 3] |= 0x80 >> (da & 7);
}
}
}
static SDL_Cursor *MagCursor(bool hot) {
int w, h;
SDL_GetWindowSize(sdl_window, &w, &h);
int mag = std::min(w / drv->VIDEO_MODE_X, h / drv->VIDEO_MODE_Y);
Uint8 *data = (Uint8 *)SDL_calloc(1, 32 * mag * mag);
Uint8 *mask = (Uint8 *)SDL_calloc(1, 32 * mag * mag);
MagBits(data, &MacCursor[4], mag);
MagBits(mask, &MacCursor[36], mag);
SDL_Cursor *cursor = SDL_CreateCursor(data, mask, 16 * mag, 16 * mag, hot ? MacCursor[2] * mag : 0, hot ? MacCursor[3] * mag : 0);
SDL_free(data);
SDL_free(mask);
return cursor;
}
#endif
void driver_base::set_video_mode(int flags)
{
int depth = sdl_depth_of_video_depth(VIDEO_MODE_DEPTH);
if ((s = init_sdl_video(VIDEO_MODE_X, VIDEO_MODE_Y, depth, flags)) == NULL)
return;
#ifdef ENABLE_VOSF
the_host_buffer = (uint8 *)s->pixels;
#endif
}
void driver_base::init()
{
set_video_mode(display_type == DISPLAY_SCREEN ? SDL_WINDOW_FULLSCREEN : 0);
int aligned_height = (VIDEO_MODE_Y + 15) & ~15;
#ifdef ENABLE_VOSF
use_vosf = true;
// Allocate memory for frame buffer (SIZE is extended to page-boundary)
the_buffer_size = page_extend((aligned_height + 2) * s->pitch);
the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
the_buffer_copy = (uint8 *)malloc(the_buffer_size);
D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
// Check whether we can initialize the VOSF subsystem and it's profitable
if (!video_vosf_init(monitor)) {
WarningAlert(GetString(STR_VOSF_INIT_ERR));
use_vosf = false;
}