1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include "qemu/osdep.h"
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
33void 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 size_t surface_data_offset = surface_bytes_per_pixel(surf) * x +
40 surface_stride(surf) * y;
41
42 assert(!scon->opengl);
43
44 if (!surf) {
45 return;
46 }
47 if (!scon->texture) {
48 return;
49 }
50
51 rect.x = x;
52 rect.y = y;
53 rect.w = w;
54 rect.h = h;
55
56 SDL_UpdateTexture(scon->texture, &rect,
57 surface_data(surf) + surface_data_offset,
58 surface_stride(surf));
59 SDL_RenderClear(scon->real_renderer);
60 SDL_RenderCopy(scon->real_renderer, scon->texture, NULL, NULL);
61 SDL_RenderPresent(scon->real_renderer);
62}
63
64void sdl2_2d_switch(DisplayChangeListener *dcl,
65 DisplaySurface *new_surface)
66{
67 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
68 DisplaySurface *old_surface = scon->surface;
69 int format = 0;
70
71 assert(!scon->opengl);
72
73 scon->surface = new_surface;
74
75 if (scon->texture) {
76 SDL_DestroyTexture(scon->texture);
77 scon->texture = NULL;
78 }
79
80 if (!new_surface) {
81 sdl2_window_destroy(scon);
82 return;
83 }
84
85 if (!scon->real_window) {
86 sdl2_window_create(scon);
87 } else if (old_surface &&
88 ((surface_width(old_surface) != surface_width(new_surface)) ||
89 (surface_height(old_surface) != surface_height(new_surface)))) {
90 sdl2_window_resize(scon);
91 }
92
93 SDL_RenderSetLogicalSize(scon->real_renderer,
94 surface_width(new_surface),
95 surface_height(new_surface));
96
97 switch (surface_format(scon->surface)) {
98 case PIXMAN_x1r5g5b5:
99 format = SDL_PIXELFORMAT_ARGB1555;
100 break;
101 case PIXMAN_r5g6b5:
102 format = SDL_PIXELFORMAT_RGB565;
103 break;
104 case PIXMAN_x8r8g8b8:
105 format = SDL_PIXELFORMAT_ARGB8888;
106 break;
107 case PIXMAN_r8g8b8x8:
108 format = SDL_PIXELFORMAT_RGBA8888;
109 break;
110 case PIXMAN_b8g8r8x8:
111 format = SDL_PIXELFORMAT_BGRX8888;
112 break;
113 default:
114 g_assert_not_reached();
115 }
116 scon->texture = SDL_CreateTexture(scon->real_renderer, format,
117 SDL_TEXTUREACCESS_STREAMING,
118 surface_width(new_surface),
119 surface_height(new_surface));
120 sdl2_2d_redraw(scon);
121}
122
123void sdl2_2d_refresh(DisplayChangeListener *dcl)
124{
125 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
126
127 assert(!scon->opengl);
128 graphic_hw_update(dcl->con);
129 sdl2_poll_events(scon);
130}
131
132void sdl2_2d_redraw(struct sdl2_console *scon)
133{
134 assert(!scon->opengl);
135
136 if (!scon->surface) {
137 return;
138 }
139 sdl2_2d_update(&scon->dcl, 0, 0,
140 surface_width(scon->surface),
141 surface_height(scon->surface));
142}
143
144bool sdl2_2d_check_format(DisplayChangeListener *dcl,
145 pixman_format_code_t format)
146{
147
148
149
150
151 return (format == PIXMAN_x8r8g8b8 ||
152 format == PIXMAN_b8g8r8x8 ||
153 format == PIXMAN_x1r5g5b5 ||
154 format == PIXMAN_r5g6b5);
155}
156