Gerd Hoffmann | f1ddebd | 2014-11-11 11:09:26 +0100 | [diff] [blame] | 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 | |
Peter Maydell | e16f4c8 | 2016-01-29 17:49:51 +0000 | [diff] [blame] | 26 | #include "qemu/osdep.h" |
Gerd Hoffmann | f1ddebd | 2014-11-11 11:09:26 +0100 | [diff] [blame] | 27 | #include "qemu-common.h" |
| 28 | #include "ui/console.h" |
| 29 | #include "ui/input.h" |
| 30 | #include "ui/sdl2.h" |
| 31 | #include "sysemu/sysemu.h" |
| 32 | |
| 33 | void sdl2_2d_update(DisplayChangeListener *dcl, |
| 34 | int x, int y, int w, int h) |
| 35 | { |
| 36 | struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); |
| 37 | DisplaySurface *surf = qemu_console_surface(dcl->con); |
| 38 | SDL_Rect rect; |
| 39 | |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 40 | assert(!scon->opengl); |
| 41 | |
Gerd Hoffmann | f1ddebd | 2014-11-11 11:09:26 +0100 | [diff] [blame] | 42 | if (!surf) { |
| 43 | return; |
| 44 | } |
| 45 | if (!scon->texture) { |
| 46 | return; |
| 47 | } |
| 48 | |
Gerd Hoffmann | 136a8d9 | 2015-06-12 12:16:02 +0200 | [diff] [blame] | 49 | /* |
| 50 | * SDL2 seems to do some double-buffering, and trying to only |
| 51 | * update the changed areas results in only one of the two buffers |
| 52 | * being updated. Which flickers alot. So lets not try to be |
| 53 | * clever do a full update every time ... |
| 54 | */ |
| 55 | #if 0 |
Gerd Hoffmann | f1ddebd | 2014-11-11 11:09:26 +0100 | [diff] [blame] | 56 | rect.x = x; |
| 57 | rect.y = y; |
| 58 | rect.w = w; |
| 59 | rect.h = h; |
Gerd Hoffmann | 136a8d9 | 2015-06-12 12:16:02 +0200 | [diff] [blame] | 60 | #else |
| 61 | rect.x = 0; |
| 62 | rect.y = 0; |
| 63 | rect.w = surface_width(surf); |
| 64 | rect.h = surface_height(surf); |
| 65 | #endif |
Gerd Hoffmann | f1ddebd | 2014-11-11 11:09:26 +0100 | [diff] [blame] | 66 | |
| 67 | SDL_UpdateTexture(scon->texture, NULL, surface_data(surf), |
| 68 | surface_stride(surf)); |
| 69 | SDL_RenderCopy(scon->real_renderer, scon->texture, &rect, &rect); |
| 70 | SDL_RenderPresent(scon->real_renderer); |
| 71 | } |
Gerd Hoffmann | 2c3056f | 2014-11-11 13:22:49 +0100 | [diff] [blame] | 72 | |
| 73 | void sdl2_2d_switch(DisplayChangeListener *dcl, |
| 74 | DisplaySurface *new_surface) |
| 75 | { |
| 76 | struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); |
| 77 | DisplaySurface *old_surface = scon->surface; |
| 78 | int format = 0; |
| 79 | |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 80 | assert(!scon->opengl); |
| 81 | |
Gerd Hoffmann | 2c3056f | 2014-11-11 13:22:49 +0100 | [diff] [blame] | 82 | scon->surface = new_surface; |
| 83 | |
| 84 | if (scon->texture) { |
| 85 | SDL_DestroyTexture(scon->texture); |
| 86 | scon->texture = NULL; |
| 87 | } |
| 88 | |
| 89 | if (!new_surface) { |
| 90 | sdl2_window_destroy(scon); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | if (!scon->real_window) { |
| 95 | sdl2_window_create(scon); |
| 96 | } else if (old_surface && |
| 97 | ((surface_width(old_surface) != surface_width(new_surface)) || |
| 98 | (surface_height(old_surface) != surface_height(new_surface)))) { |
| 99 | sdl2_window_resize(scon); |
| 100 | } |
| 101 | |
| 102 | SDL_RenderSetLogicalSize(scon->real_renderer, |
| 103 | surface_width(new_surface), |
| 104 | surface_height(new_surface)); |
| 105 | |
Max Reitz | e444ea3 | 2015-03-03 12:25:27 -0500 | [diff] [blame] | 106 | switch (surface_format(scon->surface)) { |
| 107 | case PIXMAN_x1r5g5b5: |
| 108 | format = SDL_PIXELFORMAT_ARGB1555; |
| 109 | break; |
| 110 | case PIXMAN_r5g6b5: |
Gerd Hoffmann | 2c3056f | 2014-11-11 13:22:49 +0100 | [diff] [blame] | 111 | format = SDL_PIXELFORMAT_RGB565; |
Max Reitz | e444ea3 | 2015-03-03 12:25:27 -0500 | [diff] [blame] | 112 | break; |
| 113 | case PIXMAN_x8r8g8b8: |
Gerd Hoffmann | 2c3056f | 2014-11-11 13:22:49 +0100 | [diff] [blame] | 114 | format = SDL_PIXELFORMAT_ARGB8888; |
Max Reitz | e444ea3 | 2015-03-03 12:25:27 -0500 | [diff] [blame] | 115 | break; |
| 116 | case PIXMAN_r8g8b8x8: |
| 117 | format = SDL_PIXELFORMAT_RGBA8888; |
| 118 | break; |
Pavel Dovgalyuk | 435deff | 2016-05-17 10:28:48 +0300 | [diff] [blame] | 119 | case PIXMAN_b8g8r8x8: |
| 120 | format = SDL_PIXELFORMAT_BGRX8888; |
| 121 | break; |
Max Reitz | e444ea3 | 2015-03-03 12:25:27 -0500 | [diff] [blame] | 122 | default: |
| 123 | g_assert_not_reached(); |
Gerd Hoffmann | 2c3056f | 2014-11-11 13:22:49 +0100 | [diff] [blame] | 124 | } |
| 125 | scon->texture = SDL_CreateTexture(scon->real_renderer, format, |
| 126 | SDL_TEXTUREACCESS_STREAMING, |
| 127 | surface_width(new_surface), |
| 128 | surface_height(new_surface)); |
Gerd Hoffmann | 0d01b7c | 2014-11-11 13:31:08 +0100 | [diff] [blame] | 129 | sdl2_2d_redraw(scon); |
| 130 | } |
| 131 | |
Gerd Hoffmann | 62959ff | 2014-11-12 08:03:34 +0100 | [diff] [blame] | 132 | void sdl2_2d_refresh(DisplayChangeListener *dcl) |
| 133 | { |
| 134 | struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); |
| 135 | |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 136 | assert(!scon->opengl); |
Gerd Hoffmann | 62959ff | 2014-11-12 08:03:34 +0100 | [diff] [blame] | 137 | graphic_hw_update(dcl->con); |
| 138 | sdl2_poll_events(scon); |
| 139 | } |
| 140 | |
Gerd Hoffmann | 0d01b7c | 2014-11-11 13:31:08 +0100 | [diff] [blame] | 141 | void sdl2_2d_redraw(struct sdl2_console *scon) |
| 142 | { |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 143 | assert(!scon->opengl); |
| 144 | |
Gerd Hoffmann | 0d01b7c | 2014-11-11 13:31:08 +0100 | [diff] [blame] | 145 | if (!scon->surface) { |
| 146 | return; |
| 147 | } |
| 148 | sdl2_2d_update(&scon->dcl, 0, 0, |
| 149 | surface_width(scon->surface), |
| 150 | surface_height(scon->surface)); |
Gerd Hoffmann | 2c3056f | 2014-11-11 13:22:49 +0100 | [diff] [blame] | 151 | } |
Gerd Hoffmann | 877417d | 2015-01-09 09:27:09 +0100 | [diff] [blame] | 152 | |
| 153 | bool sdl2_2d_check_format(DisplayChangeListener *dcl, |
| 154 | pixman_format_code_t format) |
| 155 | { |
| 156 | /* |
| 157 | * We let SDL convert for us a few more formats than, |
| 158 | * the native ones. Thes are the ones I have tested. |
| 159 | */ |
| 160 | return (format == PIXMAN_x8r8g8b8 || |
| 161 | format == PIXMAN_b8g8r8x8 || |
| 162 | format == PIXMAN_x1r5g5b5 || |
| 163 | format == PIXMAN_r5g6b5); |
| 164 | } |