trunk/src/osd/sdl/drawbgfx.c
| r0 | r243301 | |
| 1 | // license:BSD-3-Clause |
| 2 | // copyright-holders:Miodrag Milanovic |
| 3 | //============================================================ |
| 4 | // |
| 5 | // drawbgfx.c - BGFX drawer |
| 6 | // |
| 7 | //============================================================ |
| 8 | |
| 9 | #ifdef SDLMAME_WIN32 |
| 10 | // standard windows headers |
| 11 | #define __STDC_LIMIT_MACROS |
| 12 | #define __STDC_FORMAT_MACROS |
| 13 | #define __STDC_CONSTANT_MACROS |
| 14 | #define WIN32_LEAN_AND_MEAN |
| 15 | #include <windows.h> |
| 16 | #endif |
| 17 | |
| 18 | // standard C headers |
| 19 | #include <math.h> |
| 20 | #include <stdio.h> |
| 21 | |
| 22 | // MAME headers |
| 23 | #include "osdcomm.h" |
| 24 | #include "emu.h" |
| 25 | #include "options.h" |
| 26 | #include "emuopts.h" |
| 27 | |
| 28 | // standard SDL headers |
| 29 | #include "sdlinc.h" |
| 30 | #include "modules/lib/osdlib.h" |
| 31 | |
| 32 | // OSD headers |
| 33 | #include "osdsdl.h" |
| 34 | #include "window.h" |
| 35 | |
| 36 | #include <bgfxplatform.h> |
| 37 | #include <bgfx.h> |
| 38 | |
| 39 | //============================================================ |
| 40 | // DEBUGGING |
| 41 | //============================================================ |
| 42 | |
| 43 | //============================================================ |
| 44 | // CONSTANTS |
| 45 | //============================================================ |
| 46 | |
| 47 | //============================================================ |
| 48 | // MACROS |
| 49 | //============================================================ |
| 50 | |
| 51 | //============================================================ |
| 52 | // TYPES |
| 53 | //============================================================ |
| 54 | |
| 55 | //============================================================ |
| 56 | // INLINES |
| 57 | //============================================================ |
| 58 | |
| 59 | |
| 60 | //============================================================ |
| 61 | // PROTOTYPES |
| 62 | //============================================================ |
| 63 | |
| 64 | // core functions |
| 65 | |
| 66 | static void drawbgfx_exit(void); |
| 67 | static void drawbgfx_attach(sdl_draw_info *info, sdl_window_info *window); |
| 68 | static int drawbgfx_window_create(sdl_window_info *window, int width, int height); |
| 69 | static void drawbgfx_window_resize(sdl_window_info *window, int width, int height); |
| 70 | static void drawbgfx_window_destroy(sdl_window_info *window); |
| 71 | static int drawbgfx_window_draw(sdl_window_info *window, UINT32 dc, int update); |
| 72 | static void drawbgfx_set_target_bounds(sdl_window_info *window); |
| 73 | static void drawbgfx_destroy_all_textures(sdl_window_info *window); |
| 74 | static void drawbgfx_window_clear(sdl_window_info *window); |
| 75 | static int drawbgfx_xy_to_render_target(sdl_window_info *window, int x, int y, int *xt, int *yt); |
| 76 | |
| 77 | //============================================================ |
| 78 | // Textures |
| 79 | //============================================================ |
| 80 | |
| 81 | /* sdl_info is the information about SDL for the current screen */ |
| 82 | struct sdl_info13 |
| 83 | { |
| 84 | sdl_info13() |
| 85 | : m_blittimer(0), m_renderer(NULL), |
| 86 | m_hofs(0), m_vofs(0), |
| 87 | m_resize_pending(0), m_resize_width(0), m_resize_height(0), |
| 88 | m_last_blit_time(0), m_last_blit_pixels(0) |
| 89 | {} |
| 90 | |
| 91 | // void render_quad(texture_info *texture, const render_primitive *prim, const int x, const int y); |
| 92 | |
| 93 | //texture_info *texture_find(const render_primitive &prim, const quad_setup_data &setup); |
| 94 | //texture_info *texture_update(const render_primitive &prim); |
| 95 | |
| 96 | INT32 m_blittimer; |
| 97 | |
| 98 | SDL_Renderer * m_renderer; |
| 99 | //simple_list<texture_info> m_texlist; // list of active textures |
| 100 | |
| 101 | float m_hofs; |
| 102 | float m_vofs; |
| 103 | |
| 104 | // resize information |
| 105 | |
| 106 | UINT8 m_resize_pending; |
| 107 | UINT32 m_resize_width; |
| 108 | UINT32 m_resize_height; |
| 109 | |
| 110 | // Stats |
| 111 | INT64 m_last_blit_time; |
| 112 | INT64 m_last_blit_pixels; |
| 113 | |
| 114 | // Original display_mode |
| 115 | SDL_DisplayMode m_original_mode; |
| 116 | }; |
| 117 | |
| 118 | //============================================================ |
| 119 | // Static Variables |
| 120 | //============================================================ |
| 121 | |
| 122 | //============================================================ |
| 123 | // drawbgfx_init |
| 124 | //============================================================ |
| 125 | |
| 126 | int drawbgfx_init(running_machine &machine, sdl_draw_info *callbacks) |
| 127 | { |
| 128 | // fill in the callbacks |
| 129 | callbacks->exit = drawbgfx_exit; |
| 130 | callbacks->attach = drawbgfx_attach; |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | //============================================================ |
| 137 | // drawbgfx_attach |
| 138 | //============================================================ |
| 139 | |
| 140 | static void drawbgfx_attach(sdl_draw_info *info, sdl_window_info *window) |
| 141 | { |
| 142 | // fill in the callbacks |
| 143 | window->create = drawbgfx_window_create; |
| 144 | window->resize = drawbgfx_window_resize; |
| 145 | window->set_target_bounds = drawbgfx_set_target_bounds; |
| 146 | window->draw = drawbgfx_window_draw; |
| 147 | window->destroy = drawbgfx_window_destroy; |
| 148 | window->destroy_all_textures = drawbgfx_destroy_all_textures; |
| 149 | window->clear = drawbgfx_window_clear; |
| 150 | window->xy_to_render_target = drawbgfx_xy_to_render_target; |
| 151 | } |
| 152 | |
| 153 | //============================================================ |
| 154 | // drawbgfx_window_create |
| 155 | //============================================================ |
| 156 | |
| 157 | static int drawbgfx_window_create(sdl_window_info *window, int width, int height) |
| 158 | { |
| 159 | // allocate memory for our structures |
| 160 | sdl_info13 *sdl = global_alloc(sdl_info13); |
| 161 | |
| 162 | /* FIXME: On Ubuntu and potentially other Linux OS you should use |
| 163 | * to disable panning. This has to be done before every invocation of mame. |
| 164 | * |
| 165 | * xrandr --output HDMI-0 --panning 0x0+0+0 --fb 0x0 |
| 166 | * |
| 167 | */ |
| 168 | |
| 169 | osd_printf_verbose("Enter drawsdl2_window_create\n"); |
| 170 | |
| 171 | window->dxdata = sdl; |
| 172 | |
| 173 | UINT32 extra_flags = (window->fullscreen() ? |
| 174 | SDL_WINDOW_BORDERLESS | SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE); |
| 175 | |
| 176 | #if defined(SDLMAME_WIN32) |
| 177 | SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0"); |
| 178 | #endif |
| 179 | // create the SDL window |
| 180 | window->sdl_window = SDL_CreateWindow(window->title, |
| 181 | window->monitor()->position_size().x, window->monitor()->position_size().y, |
| 182 | width, height, extra_flags); |
| 183 | |
| 184 | if (window->fullscreen() && video_config.switchres) |
| 185 | { |
| 186 | SDL_DisplayMode mode; |
| 187 | //SDL_GetCurrentDisplayMode(window->monitor()->handle, &mode); |
| 188 | SDL_GetWindowDisplayMode(window->sdl_window, &mode); |
| 189 | sdl->m_original_mode = mode; |
| 190 | mode.w = width; |
| 191 | mode.h = height; |
| 192 | if (window->refresh) |
| 193 | mode.refresh_rate = window->refresh; |
| 194 | |
| 195 | SDL_SetWindowDisplayMode(window->sdl_window, &mode); // Try to set mode |
| 196 | #ifndef SDLMAME_WIN32 |
| 197 | /* FIXME: Warp the mouse to 0,0 in case a virtual desktop resolution |
| 198 | * is in place after the mode switch - which will most likely be the case |
| 199 | * This is a hack to work around a deficiency in SDL2 |
| 200 | */ |
| 201 | SDL_WarpMouseInWindow(window->sdl_window, 1, 1); |
| 202 | #endif |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | //SDL_SetWindowDisplayMode(window->sdl_window, NULL); // Use desktop |
| 207 | } |
| 208 | // create renderer |
| 209 | |
| 210 | if (video_config.waitvsync) |
| 211 | sdl->m_renderer = SDL_CreateRenderer(window->sdl_window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED); |
| 212 | else |
| 213 | sdl->m_renderer = SDL_CreateRenderer(window->sdl_window, -1, SDL_RENDERER_ACCELERATED); |
| 214 | |
| 215 | if (!sdl->m_renderer) |
| 216 | { |
| 217 | fatalerror("Error on creating renderer: %s\n", SDL_GetError()); |
| 218 | } |
| 219 | |
| 220 | //SDL_SelectRenderer(window->sdl_window); |
| 221 | SDL_ShowWindow(window->sdl_window); |
| 222 | //SDL_SetWindowFullscreen(window->window_id, window->fullscreen); |
| 223 | SDL_RaiseWindow(window->sdl_window); |
| 224 | |
| 225 | SDL_GetWindowSize(window->sdl_window, &window->width, &window->height); |
| 226 | |
| 227 | sdl->m_blittimer = 3; |
| 228 | |
| 229 | SDL_RenderPresent(sdl->m_renderer); |
| 230 | |
| 231 | bgfx::sdlSetWindow(window->sdl_window); |
| 232 | bgfx::init(); |
| 233 | bgfx::reset(window->width, window->height, BGFX_RESET_VSYNC); |
| 234 | |
| 235 | // Enable debug text. |
| 236 | bgfx::setDebug(BGFX_DEBUG_STATS);// BGFX_DEBUG_TEXT); |
| 237 | osd_printf_verbose("Leave drawsdl2_window_create\n"); |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | //============================================================ |
| 242 | // drawbgfx_window_resize |
| 243 | //============================================================ |
| 244 | |
| 245 | static void drawbgfx_window_resize(sdl_window_info *window, int width, int height) |
| 246 | { |
| 247 | sdl_info13 *sdl = (sdl_info13 *) window->dxdata; |
| 248 | |
| 249 | sdl->m_resize_pending = 1; |
| 250 | sdl->m_resize_height = height; |
| 251 | sdl->m_resize_width = width; |
| 252 | |
| 253 | window->width = width; |
| 254 | window->height = height; |
| 255 | |
| 256 | sdl->m_blittimer = 3; |
| 257 | } |
| 258 | |
| 259 | //============================================================ |
| 260 | // drawsdl_xy_to_render_target |
| 261 | //============================================================ |
| 262 | |
| 263 | static int drawbgfx_xy_to_render_target(sdl_window_info *window, int x, int y, int *xt, int *yt) |
| 264 | { |
| 265 | sdl_info13 *sdl = (sdl_info13 *) window->dxdata; |
| 266 | |
| 267 | *xt = x - sdl->m_hofs; |
| 268 | *yt = y - sdl->m_vofs; |
| 269 | if (*xt<0 || *xt >= window->blitwidth) |
| 270 | return 0; |
| 271 | if (*yt<0 || *yt >= window->blitheight) |
| 272 | return 0; |
| 273 | return 1; |
| 274 | } |
| 275 | |
| 276 | //============================================================ |
| 277 | // drawbgfx_window_get_primitives |
| 278 | //============================================================ |
| 279 | |
| 280 | static void drawbgfx_set_target_bounds(sdl_window_info *window) |
| 281 | { |
| 282 | window->target->set_bounds(window->blitwidth, window->blitheight, window->monitor()->aspect()); |
| 283 | } |
| 284 | |
| 285 | //============================================================ |
| 286 | // drawbgfx_window_draw |
| 287 | //============================================================ |
| 288 | |
| 289 | static int drawbgfx_window_draw(sdl_window_info *window, UINT32 dc, int update) |
| 290 | { |
| 291 | bgfx::setViewClear(0 |
| 292 | , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH |
| 293 | , 0x000000ff |
| 294 | , 1.0f |
| 295 | , 0 |
| 296 | ); |
| 297 | // Set view 0 default viewport. |
| 298 | bgfx::setViewRect(0, 0, 0, window->blitwidth, window->blitheight); |
| 299 | |
| 300 | // This dummy draw call is here to make sure that view 0 is cleared |
| 301 | // if no other draw calls are submitted to view 0. |
| 302 | bgfx::submit(0); |
| 303 | |
| 304 | window->primlist->acquire_lock(); |
| 305 | window->primlist->release_lock(); |
| 306 | // Advance to next frame. Rendering thread will be kicked to |
| 307 | // process submitted rendering primitives. |
| 308 | bgfx::frame(); |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | //============================================================ |
| 314 | // drawbgfx_exit |
| 315 | //============================================================ |
| 316 | |
| 317 | static void drawbgfx_exit(void) |
| 318 | { |
| 319 | } |
| 320 | |
| 321 | //============================================================ |
| 322 | // drawbgfx_window_destroy |
| 323 | //============================================================ |
| 324 | |
| 325 | static void drawbgfx_window_destroy(sdl_window_info *window) |
| 326 | { |
| 327 | sdl_info13 *sdl = (sdl_info13 *) window->dxdata; |
| 328 | |
| 329 | // skip if nothing |
| 330 | if (sdl == NULL) |
| 331 | return; |
| 332 | |
| 333 | // free the memory in the window |
| 334 | |
| 335 | drawbgfx_destroy_all_textures(window); |
| 336 | |
| 337 | if (window->fullscreen() && video_config.switchres) |
| 338 | { |
| 339 | SDL_SetWindowFullscreen(window->sdl_window, 0); // Try to set mode |
| 340 | SDL_SetWindowDisplayMode(window->sdl_window, &sdl->m_original_mode); // Try to set mode |
| 341 | SDL_SetWindowFullscreen(window->sdl_window, SDL_WINDOW_FULLSCREEN); // Try to set mode |
| 342 | } |
| 343 | |
| 344 | SDL_DestroyWindow(window->sdl_window); |
| 345 | |
| 346 | global_free(sdl); |
| 347 | window->dxdata = NULL; |
| 348 | |
| 349 | // Shutdown bgfx. |
| 350 | bgfx::shutdown(); |
| 351 | } |
| 352 | |
| 353 | static void drawbgfx_destroy_all_textures(sdl_window_info *window) |
| 354 | { |
| 355 | } |
| 356 | |
| 357 | //============================================================ |
| 358 | // TEXCOPY FUNCS |
| 359 | //============================================================ |
| 360 | |
| 361 | static void drawbgfx_window_clear(sdl_window_info *window) |
| 362 | { |
| 363 | sdl_info13 *sdl = (sdl_info13 *) window->dxdata; |
| 364 | |
| 365 | sdl->m_blittimer = 2; |
| 366 | } |