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