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#include "vnc.h"
30#include "vnc-jobs.h"
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51struct VncJobQueue {
52 QemuCond cond;
53 QemuMutex mutex;
54 QemuThread thread;
55 Buffer buffer;
56 bool exit;
57 QTAILQ_HEAD(, VncJob) jobs;
58};
59
60typedef struct VncJobQueue VncJobQueue;
61
62
63
64
65
66static VncJobQueue *queue;
67
68static void vnc_lock_queue(VncJobQueue *queue)
69{
70 qemu_mutex_lock(&queue->mutex);
71}
72
73static void vnc_unlock_queue(VncJobQueue *queue)
74{
75 qemu_mutex_unlock(&queue->mutex);
76}
77
78VncJob *vnc_job_new(VncState *vs)
79{
80 VncJob *job = qemu_mallocz(sizeof(VncJob));
81
82 job->vs = vs;
83 vnc_lock_queue(queue);
84 QLIST_INIT(&job->rectangles);
85 vnc_unlock_queue(queue);
86 return job;
87}
88
89int vnc_job_add_rect(VncJob *job, int x, int y, int w, int h)
90{
91 VncRectEntry *entry = qemu_mallocz(sizeof(VncRectEntry));
92
93 entry->rect.x = x;
94 entry->rect.y = y;
95 entry->rect.w = w;
96 entry->rect.h = h;
97
98 vnc_lock_queue(queue);
99 QLIST_INSERT_HEAD(&job->rectangles, entry, next);
100 vnc_unlock_queue(queue);
101 return 1;
102}
103
104void vnc_job_push(VncJob *job)
105{
106 vnc_lock_queue(queue);
107 if (queue->exit || QLIST_EMPTY(&job->rectangles)) {
108 qemu_free(job);
109 } else {
110 QTAILQ_INSERT_TAIL(&queue->jobs, job, next);
111 qemu_cond_broadcast(&queue->cond);
112 }
113 vnc_unlock_queue(queue);
114}
115
116static bool vnc_has_job_locked(VncState *vs)
117{
118 VncJob *job;
119
120 QTAILQ_FOREACH(job, &queue->jobs, next) {
121 if (job->vs == vs || !vs) {
122 return true;
123 }
124 }
125 return false;
126}
127
128bool vnc_has_job(VncState *vs)
129{
130 bool ret;
131
132 vnc_lock_queue(queue);
133 ret = vnc_has_job_locked(vs);
134 vnc_unlock_queue(queue);
135 return ret;
136}
137
138void vnc_jobs_clear(VncState *vs)
139{
140 VncJob *job, *tmp;
141
142 vnc_lock_queue(queue);
143 QTAILQ_FOREACH_SAFE(job, &queue->jobs, next, tmp) {
144 if (job->vs == vs || !vs) {
145 QTAILQ_REMOVE(&queue->jobs, job, next);
146 }
147 }
148 vnc_unlock_queue(queue);
149}
150
151void vnc_jobs_join(VncState *vs)
152{
153 vnc_lock_queue(queue);
154 while (vnc_has_job_locked(vs)) {
155 qemu_cond_wait(&queue->cond, &queue->mutex);
156 }
157 vnc_unlock_queue(queue);
158}
159
160
161
162
163static void vnc_async_encoding_start(VncState *orig, VncState *local)
164{
165 local->vnc_encoding = orig->vnc_encoding;
166 local->features = orig->features;
167 local->ds = orig->ds;
168 local->vd = orig->vd;
169 local->write_pixels = orig->write_pixels;
170 local->clientds = orig->clientds;
171 local->tight = orig->tight;
172 local->zlib = orig->zlib;
173 local->hextile = orig->hextile;
174 local->output = queue->buffer;
175 local->csock = -1;
176
177 buffer_reset(&local->output);
178}
179
180static void vnc_async_encoding_end(VncState *orig, VncState *local)
181{
182 orig->tight = local->tight;
183 orig->zlib = local->zlib;
184 orig->hextile = local->hextile;
185}
186
187static int vnc_worker_thread_loop(VncJobQueue *queue)
188{
189 VncJob *job;
190 VncRectEntry *entry, *tmp;
191 VncState vs;
192 int n_rectangles;
193 int saved_offset;
194 bool flush;
195
196 vnc_lock_queue(queue);
197 while (QTAILQ_EMPTY(&queue->jobs) && !queue->exit) {
198 qemu_cond_wait(&queue->cond, &queue->mutex);
199 }
200
201 job = QTAILQ_FIRST(&queue->jobs);
202 vnc_unlock_queue(queue);
203
204 if (queue->exit) {
205 return -1;
206 }
207
208 vnc_lock_output(job->vs);
209 if (job->vs->csock == -1 || job->vs->abort == true) {
210 goto disconnected;
211 }
212 vnc_unlock_output(job->vs);
213
214
215 vnc_async_encoding_start(job->vs, &vs);
216
217
218 n_rectangles = 0;
219 vnc_write_u8(&vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
220 vnc_write_u8(&vs, 0);
221 saved_offset = vs.output.offset;
222 vnc_write_u16(&vs, 0);
223
224 vnc_lock_display(job->vs->vd);
225 QLIST_FOREACH_SAFE(entry, &job->rectangles, next, tmp) {
226 int n;
227
228 if (job->vs->csock == -1) {
229 vnc_unlock_display(job->vs->vd);
230
231
232
233 vnc_lock_output(job->vs);
234 goto disconnected;
235 }
236
237 n = vnc_send_framebuffer_update(&vs, entry->rect.x, entry->rect.y,
238 entry->rect.w, entry->rect.h);
239
240 if (n >= 0) {
241 n_rectangles += n;
242 }
243 qemu_free(entry);
244 }
245 vnc_unlock_display(job->vs->vd);
246
247
248 vs.output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
249 vs.output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
250
251
252 vnc_lock_output(job->vs);
253 if (job->vs->csock == -1) {
254 goto disconnected;
255 }
256
257 vnc_write(job->vs, vs.output.buffer, vs.output.offset);
258
259disconnected:
260
261 vnc_async_encoding_end(job->vs, &vs);
262 flush = (job->vs->csock != -1 && job->vs->abort != true);
263 vnc_unlock_output(job->vs);
264
265 if (flush) {
266 vnc_flush(job->vs);
267 }
268
269 vnc_lock_queue(queue);
270 QTAILQ_REMOVE(&queue->jobs, job, next);
271 vnc_unlock_queue(queue);
272 qemu_cond_broadcast(&queue->cond);
273 qemu_free(job);
274 return 0;
275}
276
277static VncJobQueue *vnc_queue_init(void)
278{
279 VncJobQueue *queue = qemu_mallocz(sizeof(VncJobQueue));
280
281 qemu_cond_init(&queue->cond);
282 qemu_mutex_init(&queue->mutex);
283 QTAILQ_INIT(&queue->jobs);
284 return queue;
285}
286
287static void vnc_queue_clear(VncJobQueue *q)
288{
289 qemu_cond_destroy(&queue->cond);
290 qemu_mutex_destroy(&queue->mutex);
291 buffer_free(&queue->buffer);
292 qemu_free(q);
293 queue = NULL;
294}
295
296static void *vnc_worker_thread(void *arg)
297{
298 VncJobQueue *queue = arg;
299
300 qemu_thread_self(&queue->thread);
301
302 while (!vnc_worker_thread_loop(queue)) ;
303 vnc_queue_clear(queue);
304 return NULL;
305}
306
307void vnc_start_worker_thread(void)
308{
309 VncJobQueue *q;
310
311 if (vnc_worker_thread_running())
312 return ;
313
314 q = vnc_queue_init();
315 qemu_thread_create(&q->thread, vnc_worker_thread, q);
316 queue = q;
317}
318
319bool vnc_worker_thread_running(void)
320{
321 return queue;
322}
323
324void vnc_stop_worker_thread(void)
325{
326 if (!vnc_worker_thread_running())
327 return ;
328
329
330 vnc_lock_queue(queue);
331 queue->exit = true;
332 vnc_unlock_queue(queue);
333 vnc_jobs_clear(NULL);
334 qemu_cond_broadcast(&queue->cond);
335}
336