-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathvgpu-display.c
More file actions
289 lines (249 loc) · 9.55 KB
/
vgpu-display.c
File metadata and controls
289 lines (249 loc) · 9.55 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <stdlib.h>
#include "vgpu-display.h"
/* 'PRIMARY_SET'/'CURSOR_SET' own CPU-frame snapshots, so each queued command
* can retain significantly more memory than an input event. Keep this backlog
* deliberately small: display updates are lossy and quickly become stale, and
* the emulator thread must be able to drop them rather than accumulate a large
* queue of old frames.
*/
#define VGPU_DISPLAY_CMD_QUEUE_SIZE 64U
#define VGPU_DISPLAY_CMD_QUEUE_MASK (VGPU_DISPLAY_CMD_QUEUE_SIZE - 1U)
/* Reliable state for plane clear/removal events. The producer advances
* 'generation' when the guest detaches a plane. The SDL consumer mirrors the
* last applied value in 'consumed_generation'. Frame payloads remain in the
* lossy SPSC queue below.
*/
struct vgpu_display_plane_clear_state {
uint32_t generation;
uint32_t consumed_generation;
};
static struct vgpu_display_plane_clear_state
vgpu_display_primary_clear[VIRTIO_GPU_MAX_SCANOUTS];
static struct vgpu_display_plane_clear_state
vgpu_display_cursor_clear[VIRTIO_GPU_MAX_SCANOUTS];
static uint32_t vgpu_display_scanout_count = 1U;
/* The SPSC queue carries lossy frame/move commands. It's process-wide and
* currently assumes one 'virtio-gpu' producer. The GPU backend is the only
* producer and the window backend is the only consumer. Commands entering this
* bridge carry 'scanout_id' values already validated by the guest-facing
* backend; the SDL consumer relies on that internal contract.
*/
static struct vgpu_display_cmd
vgpu_display_cmd_queue[VGPU_DISPLAY_CMD_QUEUE_SIZE];
static uint32_t vgpu_display_cmd_head;
static uint32_t vgpu_display_cmd_tail;
static bool vgpu_display_unavailable;
static bool vgpu_display_is_cmd_stale(const struct vgpu_display_cmd *cmd)
{
switch (cmd->type) {
case VGPU_DISPLAY_CMD_PRIMARY_SET:
return cmd->generation !=
__atomic_load_n(
&vgpu_display_primary_clear[cmd->scanout_id].generation,
__ATOMIC_ACQUIRE);
case VGPU_DISPLAY_CMD_CURSOR_SET:
case VGPU_DISPLAY_CMD_CURSOR_MOVE:
return cmd->generation !=
__atomic_load_n(
&vgpu_display_cursor_clear[cmd->scanout_id].generation,
__ATOMIC_ACQUIRE);
default:
return false;
}
}
static bool vgpu_display_pop_pending_clear_cmd(
struct vgpu_display_plane_clear_state *states,
enum vgpu_display_cmd_type type,
struct vgpu_display_cmd *cmd)
{
uint32_t scanout_count =
__atomic_load_n(&vgpu_display_scanout_count, __ATOMIC_ACQUIRE);
for (uint32_t i = 0; i < scanout_count; i++) {
struct vgpu_display_plane_clear_state *state = &states[i];
uint32_t generation =
__atomic_load_n(&state->generation, __ATOMIC_ACQUIRE);
if (state->consumed_generation == generation)
continue;
state->consumed_generation = generation;
*cmd = (struct vgpu_display_cmd) {
.type = type,
.scanout_id = i,
.generation = generation,
};
return true;
}
return false;
}
void vgpu_display_set_scanout_count(uint32_t scanout_count)
{
if (scanout_count > VIRTIO_GPU_MAX_SCANOUTS)
scanout_count = VIRTIO_GPU_MAX_SCANOUTS;
__atomic_store_n(&vgpu_display_scanout_count, scanout_count,
__ATOMIC_RELEASE);
}
void vgpu_display_publish_primary_clear(uint32_t scanout_id)
{
if (__atomic_load_n(&vgpu_display_unavailable, __ATOMIC_ACQUIRE))
return;
__atomic_add_fetch(&vgpu_display_primary_clear[scanout_id].generation, 1U,
__ATOMIC_ACQ_REL);
}
void vgpu_display_publish_cursor_clear(uint32_t scanout_id)
{
if (__atomic_load_n(&vgpu_display_unavailable, __ATOMIC_ACQUIRE))
return;
__atomic_add_fetch(&vgpu_display_cursor_clear[scanout_id].generation, 1U,
__ATOMIC_ACQ_REL);
}
static bool vgpu_display_is_cmd_queue_full(void)
{
uint32_t head = __atomic_load_n(&vgpu_display_cmd_head, __ATOMIC_RELAXED);
uint32_t tail = __atomic_load_n(&vgpu_display_cmd_tail, __ATOMIC_ACQUIRE);
uint32_t next = (head + 1U) & VGPU_DISPLAY_CMD_QUEUE_MASK;
return next == tail;
}
static void vgpu_display_push_cmd(struct vgpu_display_cmd *cmd)
{
uint32_t head = __atomic_load_n(&vgpu_display_cmd_head, __ATOMIC_RELAXED);
uint32_t tail = __atomic_load_n(&vgpu_display_cmd_tail, __ATOMIC_ACQUIRE);
uint32_t next = (head + 1U) & VGPU_DISPLAY_CMD_QUEUE_MASK;
/* Keep the producer non-blocking. If the window backend falls behind,
* prefer dropping lossy display updates over stalling guest/device
* execution on the emulator thread. Clear commands do not use this queue.
*/
if (next == tail) {
vgpu_display_release_cmd(cmd);
return;
}
vgpu_display_cmd_queue[head] = *cmd;
__atomic_store_n(&vgpu_display_cmd_head, next, __ATOMIC_RELEASE);
}
static bool vgpu_display_pop_queued_cmd(struct vgpu_display_cmd *cmd)
{
uint32_t tail = __atomic_load_n(&vgpu_display_cmd_tail, __ATOMIC_RELAXED);
uint32_t head = __atomic_load_n(&vgpu_display_cmd_head, __ATOMIC_ACQUIRE);
if (tail == head)
return false;
*cmd = vgpu_display_cmd_queue[tail];
__atomic_store_n(&vgpu_display_cmd_tail,
(tail + 1U) & VGPU_DISPLAY_CMD_QUEUE_MASK,
__ATOMIC_RELEASE);
return true;
}
void vgpu_display_release_cmd(struct vgpu_display_cmd *cmd)
{
switch (cmd->type) {
case VGPU_DISPLAY_CMD_PRIMARY_SET:
free(cmd->u.primary_set.payload);
break;
case VGPU_DISPLAY_CMD_CURSOR_SET:
free(cmd->u.cursor_set.payload);
break;
default:
break;
}
}
bool vgpu_display_pop_cmd(struct vgpu_display_cmd *cmd)
{
/* Return true when '*cmd' is filled with a clear command or a valid queued
* frame/move command. Stale queued commands are released and skipped;
* return false only when no command remains.
*/
for (;;) {
/* Check clear command for primary and cursor plane. */
if (vgpu_display_pop_pending_clear_cmd(vgpu_display_primary_clear,
VGPU_DISPLAY_CMD_PRIMARY_CLEAR,
cmd))
return true;
if (vgpu_display_pop_pending_clear_cmd(
vgpu_display_cursor_clear, VGPU_DISPLAY_CMD_CURSOR_CLEAR, cmd))
return true;
/* Pop the command and check if it is still valid. */
if (!vgpu_display_pop_queued_cmd(cmd))
return false;
if (!vgpu_display_is_cmd_stale(cmd))
return true;
/* Drop invalid command and continue. */
vgpu_display_release_cmd(cmd);
}
}
void vgpu_display_set_unavailable(void)
{
struct vgpu_display_cmd cmd;
/* This is an init-only fallback path for 'window-sw' initialization
* failure, before the emulator thread starts publishing display commands.
* It is not a concurrent shutdown primitive: a producer could otherwise
* observe 'vgpu_display_unavailable == false', race with this drain, and
* enqueue a payload after the queue was already drained.
*
* Still publish the latch atomically so later call sites keep the same
* one-way handoff rule.
*/
__atomic_store_n(&vgpu_display_unavailable, true, __ATOMIC_RELEASE);
while (vgpu_display_pop_cmd(&cmd))
vgpu_display_release_cmd(&cmd);
}
bool vgpu_display_can_publish(void)
{
return !__atomic_load_n(&vgpu_display_unavailable, __ATOMIC_ACQUIRE) &&
!vgpu_display_is_cmd_queue_full();
}
void vgpu_display_publish_primary_set(uint32_t scanout_id,
struct vgpu_display_payload *payload)
{
if (__atomic_load_n(&vgpu_display_unavailable, __ATOMIC_ACQUIRE)) {
free(payload);
return;
}
struct vgpu_display_cmd cmd = {
.type = VGPU_DISPLAY_CMD_PRIMARY_SET,
.scanout_id = scanout_id,
.generation =
__atomic_load_n(&vgpu_display_primary_clear[scanout_id].generation,
__ATOMIC_ACQUIRE),
.u.primary_set = {.payload = payload},
};
vgpu_display_push_cmd(&cmd);
}
void vgpu_display_publish_cursor_set(uint32_t scanout_id,
struct vgpu_display_payload *payload,
int32_t x,
int32_t y,
uint32_t hot_x,
uint32_t hot_y)
{
if (__atomic_load_n(&vgpu_display_unavailable, __ATOMIC_ACQUIRE)) {
free(payload);
return;
}
struct vgpu_display_cmd cmd = {
.type = VGPU_DISPLAY_CMD_CURSOR_SET,
.scanout_id = scanout_id,
.generation =
__atomic_load_n(&vgpu_display_cursor_clear[scanout_id].generation,
__ATOMIC_ACQUIRE),
.u.cursor_set =
{
.payload = payload,
.x = x,
.y = y,
.hot_x = hot_x,
.hot_y = hot_y,
},
};
vgpu_display_push_cmd(&cmd);
}
void vgpu_display_publish_cursor_move(uint32_t scanout_id, int32_t x, int32_t y)
{
if (__atomic_load_n(&vgpu_display_unavailable, __ATOMIC_ACQUIRE))
return;
struct vgpu_display_cmd cmd = {
.type = VGPU_DISPLAY_CMD_CURSOR_MOVE,
.scanout_id = scanout_id,
.generation =
__atomic_load_n(&vgpu_display_cursor_clear[scanout_id].generation,
__ATOMIC_ACQUIRE),
.u.cursor_move = {.x = x, .y = y},
};
vgpu_display_push_cmd(&cmd);
}