1 /* 2 * QEMU SDL display driver 3 * 4 * Copyright (c) 2003 Fabrice Bellard 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 /* Ported SDL 1.2 code to 2.0 by Dave Airlie. */ 25 26 /* Avoid compiler warning because macro is redefined in SDL_syswm.h. */ 27 #undef WIN32_LEAN_AND_MEAN 28 29 #include <SDL.h> 30 #include <SDL_syswm.h> 31 32 #include "qemu-common.h" 33 #include "ui/console.h" 34 #include "ui/input.h" 35 #include "ui/sdl2.h" 36 #include "sysemu/sysemu.h" 37 38 void sdl2_2d_update(DisplayChangeListener *dcl, 39 int x, int y, int w, int h) 40 { 41 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); 42 DisplaySurface *surf = qemu_console_surface(dcl->con); 43 SDL_Rect rect; 44 45 if (!surf) { 46 return; 47 } 48 if (!scon->texture) { 49 return; 50 } 51 52 rect.x = x; 53 rect.y = y; 54 rect.w = w; 55 rect.h = h; 56 57 SDL_UpdateTexture(scon->texture, NULL, surface_data(surf), 58 surface_stride(surf)); 59 SDL_RenderCopy(scon->real_renderer, scon->texture, &rect, &rect); 60 SDL_RenderPresent(scon->real_renderer); 61 } 62 63 void sdl2_2d_switch(DisplayChangeListener *dcl, 64 DisplaySurface *new_surface) 65 { 66 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); 67 DisplaySurface *old_surface = scon->surface; 68 int format = 0; 69 70 scon->surface = new_surface; 71 72 if (scon->texture) { 73 SDL_DestroyTexture(scon->texture); 74 scon->texture = NULL; 75 } 76 77 if (!new_surface) { 78 sdl2_window_destroy(scon); 79 return; 80 } 81 82 if (!scon->real_window) { 83 sdl2_window_create(scon); 84 } else if (old_surface && 85 ((surface_width(old_surface) != surface_width(new_surface)) || 86 (surface_height(old_surface) != surface_height(new_surface)))) { 87 sdl2_window_resize(scon); 88 } 89 90 SDL_RenderSetLogicalSize(scon->real_renderer, 91 surface_width(new_surface), 92 surface_height(new_surface)); 93 94 if (surface_bits_per_pixel(scon->surface) == 16) { 95 format = SDL_PIXELFORMAT_RGB565; 96 } else if (surface_bits_per_pixel(scon->surface) == 32) { 97 format = SDL_PIXELFORMAT_ARGB8888; 98 } 99 scon->texture = SDL_CreateTexture(scon->real_renderer, format, 100 SDL_TEXTUREACCESS_STREAMING, 101 surface_width(new_surface), 102 surface_height(new_surface)); 103 sdl2_2d_redraw(scon); 104 } 105 106 void sdl2_2d_refresh(DisplayChangeListener *dcl) 107 { 108 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); 109 110 graphic_hw_update(dcl->con); 111 sdl2_poll_events(scon); 112 } 113 114 void sdl2_2d_redraw(struct sdl2_console *scon) 115 { 116 if (!scon->surface) { 117 return; 118 } 119 sdl2_2d_update(&scon->dcl, 0, 0, 120 surface_width(scon->surface), 121 surface_height(scon->surface)); 122 } 123 124 bool sdl2_2d_check_format(DisplayChangeListener *dcl, 125 pixman_format_code_t format) 126 { 127 /* 128 * We let SDL convert for us a few more formats than, 129 * the native ones. Thes are the ones I have tested. 130 */ 131 return (format == PIXMAN_x8r8g8b8 || 132 format == PIXMAN_b8g8r8x8 || 133 format == PIXMAN_x1r5g5b5 || 134 format == PIXMAN_r5g6b5); 135 } 136