blob: a2ea85127d5729baccf0b2a341f535a8c56bc03c [file] [log] [blame]
Gerd Hoffmannf1ddebd2014-11-11 11:09:26 +01001/*
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 Maydelle16f4c82016-01-29 17:49:51 +000026#include "qemu/osdep.h"
Gerd Hoffmannf1ddebd2014-11-11 11:09:26 +010027#include "ui/console.h"
28#include "ui/input.h"
29#include "ui/sdl2.h"
Gerd Hoffmannf1ddebd2014-11-11 11:09:26 +010030
31void 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);
35 DisplaySurface *surf = qemu_console_surface(dcl->con);
36 SDL_Rect rect;
Peter Maydelle8dcb8a2018-05-15 19:58:14 +010037 size_t surface_data_offset;
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +010038 assert(!scon->opengl);
39
Gerd Hoffmannf1ddebd2014-11-11 11:09:26 +010040 if (!surf) {
41 return;
42 }
43 if (!scon->texture) {
44 return;
45 }
46
Peter Maydelle8dcb8a2018-05-15 19:58:14 +010047 surface_data_offset = surface_bytes_per_pixel(surf) * x +
48 surface_stride(surf) * y;
Gerd Hoffmannf1ddebd2014-11-11 11:09:26 +010049 rect.x = x;
50 rect.y = y;
51 rect.w = w;
52 rect.h = h;
53
Anatoly Trosinenko2ab858c2018-02-05 16:32:28 +030054 SDL_UpdateTexture(scon->texture, &rect,
55 surface_data(surf) + surface_data_offset,
Gerd Hoffmannf1ddebd2014-11-11 11:09:26 +010056 surface_stride(surf));
Anatoly Trosinenko2ab858c2018-02-05 16:32:28 +030057 SDL_RenderClear(scon->real_renderer);
58 SDL_RenderCopy(scon->real_renderer, scon->texture, NULL, NULL);
Gerd Hoffmannf1ddebd2014-11-11 11:09:26 +010059 SDL_RenderPresent(scon->real_renderer);
60}
Gerd Hoffmann2c3056f2014-11-11 13:22:49 +010061
62void sdl2_2d_switch(DisplayChangeListener *dcl,
63 DisplaySurface *new_surface)
64{
65 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
66 DisplaySurface *old_surface = scon->surface;
67 int format = 0;
68
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +010069 assert(!scon->opengl);
70
Gerd Hoffmann2c3056f2014-11-11 13:22:49 +010071 scon->surface = new_surface;
72
73 if (scon->texture) {
74 SDL_DestroyTexture(scon->texture);
75 scon->texture = NULL;
76 }
77
78 if (!new_surface) {
79 sdl2_window_destroy(scon);
80 return;
81 }
82
83 if (!scon->real_window) {
84 sdl2_window_create(scon);
85 } else if (old_surface &&
86 ((surface_width(old_surface) != surface_width(new_surface)) ||
87 (surface_height(old_surface) != surface_height(new_surface)))) {
88 sdl2_window_resize(scon);
89 }
90
91 SDL_RenderSetLogicalSize(scon->real_renderer,
92 surface_width(new_surface),
93 surface_height(new_surface));
94
Max Reitze444ea32015-03-03 12:25:27 -050095 switch (surface_format(scon->surface)) {
96 case PIXMAN_x1r5g5b5:
97 format = SDL_PIXELFORMAT_ARGB1555;
98 break;
99 case PIXMAN_r5g6b5:
Gerd Hoffmann2c3056f2014-11-11 13:22:49 +0100100 format = SDL_PIXELFORMAT_RGB565;
Max Reitze444ea32015-03-03 12:25:27 -0500101 break;
Max Reitz1abcfe92018-10-08 20:50:13 +0200102 case PIXMAN_a8r8g8b8:
Max Reitze444ea32015-03-03 12:25:27 -0500103 case PIXMAN_x8r8g8b8:
Gerd Hoffmann2c3056f2014-11-11 13:22:49 +0100104 format = SDL_PIXELFORMAT_ARGB8888;
Max Reitze444ea32015-03-03 12:25:27 -0500105 break;
Max Reitz1abcfe92018-10-08 20:50:13 +0200106 case PIXMAN_a8b8g8r8:
107 case PIXMAN_x8b8g8r8:
108 format = SDL_PIXELFORMAT_ABGR8888;
109 break;
110 case PIXMAN_r8g8b8a8:
Max Reitze444ea32015-03-03 12:25:27 -0500111 case PIXMAN_r8g8b8x8:
112 format = SDL_PIXELFORMAT_RGBA8888;
113 break;
Pavel Dovgalyuk435deff2016-05-17 10:28:48 +0300114 case PIXMAN_b8g8r8x8:
115 format = SDL_PIXELFORMAT_BGRX8888;
116 break;
Max Reitz1abcfe92018-10-08 20:50:13 +0200117 case PIXMAN_b8g8r8a8:
118 format = SDL_PIXELFORMAT_BGRA8888;
119 break;
Max Reitze444ea32015-03-03 12:25:27 -0500120 default:
121 g_assert_not_reached();
Gerd Hoffmann2c3056f2014-11-11 13:22:49 +0100122 }
123 scon->texture = SDL_CreateTexture(scon->real_renderer, format,
124 SDL_TEXTUREACCESS_STREAMING,
125 surface_width(new_surface),
126 surface_height(new_surface));
Gerd Hoffmann0d01b7c2014-11-11 13:31:08 +0100127 sdl2_2d_redraw(scon);
128}
129
Gerd Hoffmann62959ff2014-11-12 08:03:34 +0100130void sdl2_2d_refresh(DisplayChangeListener *dcl)
131{
132 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
133
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100134 assert(!scon->opengl);
Gerd Hoffmann62959ff2014-11-12 08:03:34 +0100135 graphic_hw_update(dcl->con);
136 sdl2_poll_events(scon);
137}
138
Gerd Hoffmann0d01b7c2014-11-11 13:31:08 +0100139void sdl2_2d_redraw(struct sdl2_console *scon)
140{
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100141 assert(!scon->opengl);
142
Gerd Hoffmann0d01b7c2014-11-11 13:31:08 +0100143 if (!scon->surface) {
144 return;
145 }
146 sdl2_2d_update(&scon->dcl, 0, 0,
147 surface_width(scon->surface),
148 surface_height(scon->surface));
Gerd Hoffmann2c3056f2014-11-11 13:22:49 +0100149}
Gerd Hoffmann877417d2015-01-09 09:27:09 +0100150
151bool sdl2_2d_check_format(DisplayChangeListener *dcl,
152 pixman_format_code_t format)
153{
154 /*
155 * We let SDL convert for us a few more formats than,
156 * the native ones. Thes are the ones I have tested.
157 */
158 return (format == PIXMAN_x8r8g8b8 ||
Max Reitz1abcfe92018-10-08 20:50:13 +0200159 format == PIXMAN_a8r8g8b8 ||
160 format == PIXMAN_a8b8g8r8 ||
161 format == PIXMAN_x8b8g8r8 ||
Gerd Hoffmann877417d2015-01-09 09:27:09 +0100162 format == PIXMAN_b8g8r8x8 ||
Max Reitz1abcfe92018-10-08 20:50:13 +0200163 format == PIXMAN_b8g8r8a8 ||
164 format == PIXMAN_r8g8b8x8 ||
165 format == PIXMAN_r8g8b8a8 ||
Gerd Hoffmann877417d2015-01-09 09:27:09 +0100166 format == PIXMAN_x1r5g5b5 ||
167 format == PIXMAN_r5g6b5);
168}