2
0

vnc-jobs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * QEMU VNC display driver
  3. *
  4. * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
  5. * Copyright (C) 2006 Fabrice Bellard
  6. * Copyright (C) 2009 Red Hat, Inc
  7. * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include "qemu/osdep.h"
  28. #include "vnc.h"
  29. #include "vnc-jobs.h"
  30. #include "qemu/sockets.h"
  31. #include "qemu/main-loop.h"
  32. #include "block/aio.h"
  33. #include "trace.h"
  34. /*
  35. * Locking:
  36. *
  37. * There are three levels of locking:
  38. * - jobs queue lock: for each operation on the queue (push, pop, isEmpty?)
  39. * - VncDisplay global lock: mainly used for framebuffer updates to avoid
  40. * screen corruption if the framebuffer is updated
  41. * while the worker is doing something.
  42. * - VncState::output lock: used to make sure the output buffer is not corrupted
  43. * if two threads try to write on it at the same time
  44. *
  45. * While the VNC worker thread is working, the VncDisplay global lock is held
  46. * to avoid screen corruption (this does not block vnc_refresh() because it
  47. * uses trylock()) but the output lock is not held because the thread works on
  48. * its own output buffer.
  49. * When the encoding job is done, the worker thread will hold the output lock
  50. * and copy its output buffer in vs->output.
  51. */
  52. struct VncJobQueue {
  53. QemuCond cond;
  54. QemuMutex mutex;
  55. QemuThread thread;
  56. bool exit;
  57. QTAILQ_HEAD(, VncJob) jobs;
  58. };
  59. typedef struct VncJobQueue VncJobQueue;
  60. /*
  61. * We use a single global queue, but most of the functions are
  62. * already reentrant, so we can easily add more than one encoding thread
  63. */
  64. static VncJobQueue *queue;
  65. static void vnc_lock_queue(VncJobQueue *queue)
  66. {
  67. qemu_mutex_lock(&queue->mutex);
  68. }
  69. static void vnc_unlock_queue(VncJobQueue *queue)
  70. {
  71. qemu_mutex_unlock(&queue->mutex);
  72. }
  73. VncJob *vnc_job_new(VncState *vs)
  74. {
  75. VncJob *job = g_new0(VncJob, 1);
  76. assert(vs->magic == VNC_MAGIC);
  77. job->vs = vs;
  78. vnc_lock_queue(queue);
  79. QLIST_INIT(&job->rectangles);
  80. vnc_unlock_queue(queue);
  81. return job;
  82. }
  83. int vnc_job_add_rect(VncJob *job, int x, int y, int w, int h)
  84. {
  85. VncRectEntry *entry = g_new0(VncRectEntry, 1);
  86. trace_vnc_job_add_rect(job->vs, job, x, y, w, h);
  87. entry->rect.x = x;
  88. entry->rect.y = y;
  89. entry->rect.w = w;
  90. entry->rect.h = h;
  91. vnc_lock_queue(queue);
  92. QLIST_INSERT_HEAD(&job->rectangles, entry, next);
  93. vnc_unlock_queue(queue);
  94. return 1;
  95. }
  96. void vnc_job_push(VncJob *job)
  97. {
  98. vnc_lock_queue(queue);
  99. if (queue->exit || QLIST_EMPTY(&job->rectangles)) {
  100. g_free(job);
  101. } else {
  102. QTAILQ_INSERT_TAIL(&queue->jobs, job, next);
  103. qemu_cond_broadcast(&queue->cond);
  104. }
  105. vnc_unlock_queue(queue);
  106. }
  107. static bool vnc_has_job_locked(VncState *vs)
  108. {
  109. VncJob *job;
  110. QTAILQ_FOREACH(job, &queue->jobs, next) {
  111. if (job->vs == vs || !vs) {
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. void vnc_jobs_join(VncState *vs)
  118. {
  119. vnc_lock_queue(queue);
  120. while (vnc_has_job_locked(vs)) {
  121. qemu_cond_wait(&queue->cond, &queue->mutex);
  122. }
  123. vnc_unlock_queue(queue);
  124. vnc_jobs_consume_buffer(vs);
  125. }
  126. void vnc_jobs_consume_buffer(VncState *vs)
  127. {
  128. bool flush;
  129. vnc_lock_output(vs);
  130. if (vs->jobs_buffer.offset) {
  131. if (vs->ioc != NULL && buffer_empty(&vs->output)) {
  132. if (vs->ioc_tag) {
  133. g_source_remove(vs->ioc_tag);
  134. }
  135. if (vs->disconnecting == FALSE) {
  136. vs->ioc_tag = qio_channel_add_watch(
  137. vs->ioc, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_OUT,
  138. vnc_client_io, vs, NULL);
  139. }
  140. }
  141. buffer_move(&vs->output, &vs->jobs_buffer);
  142. if (vs->job_update == VNC_STATE_UPDATE_FORCE) {
  143. vs->force_update_offset = vs->output.offset;
  144. }
  145. vs->job_update = VNC_STATE_UPDATE_NONE;
  146. }
  147. flush = vs->ioc != NULL && vs->abort != true;
  148. vnc_unlock_output(vs);
  149. if (flush) {
  150. vnc_flush(vs);
  151. }
  152. }
  153. /*
  154. * Copy data for local use
  155. */
  156. static void vnc_async_encoding_start(VncState *orig, VncState *local)
  157. {
  158. buffer_init(&local->output, "vnc-worker-output");
  159. local->sioc = NULL; /* Don't do any network work on this thread */
  160. local->ioc = NULL; /* Don't do any network work on this thread */
  161. local->vnc_encoding = orig->vnc_encoding;
  162. local->features = orig->features;
  163. local->vd = orig->vd;
  164. local->lossy_rect = orig->lossy_rect;
  165. local->write_pixels = orig->write_pixels;
  166. local->client_pf = orig->client_pf;
  167. local->client_be = orig->client_be;
  168. local->tight = orig->tight;
  169. local->zlib = orig->zlib;
  170. local->hextile = orig->hextile;
  171. local->zrle = orig->zrle;
  172. local->client_width = orig->client_width;
  173. local->client_height = orig->client_height;
  174. }
  175. static void vnc_async_encoding_end(VncState *orig, VncState *local)
  176. {
  177. buffer_free(&local->output);
  178. orig->tight = local->tight;
  179. orig->zlib = local->zlib;
  180. orig->hextile = local->hextile;
  181. orig->zrle = local->zrle;
  182. orig->lossy_rect = local->lossy_rect;
  183. }
  184. static bool vnc_worker_clamp_rect(VncState *vs, VncJob *job, VncRect *rect)
  185. {
  186. trace_vnc_job_clamp_rect(vs, job, rect->x, rect->y, rect->w, rect->h);
  187. if (rect->x >= vs->client_width) {
  188. goto discard;
  189. }
  190. rect->w = MIN(vs->client_width - rect->x, rect->w);
  191. if (rect->w == 0) {
  192. goto discard;
  193. }
  194. if (rect->y >= vs->client_height) {
  195. goto discard;
  196. }
  197. rect->h = MIN(vs->client_height - rect->y, rect->h);
  198. if (rect->h == 0) {
  199. goto discard;
  200. }
  201. trace_vnc_job_clamped_rect(vs, job, rect->x, rect->y, rect->w, rect->h);
  202. return true;
  203. discard:
  204. trace_vnc_job_discard_rect(vs, job, rect->x, rect->y, rect->w, rect->h);
  205. return false;
  206. }
  207. static int vnc_worker_thread_loop(VncJobQueue *queue)
  208. {
  209. VncJob *job;
  210. VncRectEntry *entry, *tmp;
  211. VncState vs = {};
  212. int n_rectangles;
  213. int saved_offset;
  214. vnc_lock_queue(queue);
  215. while (QTAILQ_EMPTY(&queue->jobs) && !queue->exit) {
  216. qemu_cond_wait(&queue->cond, &queue->mutex);
  217. }
  218. /* Here job can only be NULL if queue->exit is true */
  219. job = QTAILQ_FIRST(&queue->jobs);
  220. vnc_unlock_queue(queue);
  221. if (queue->exit) {
  222. return -1;
  223. }
  224. assert(job->vs->magic == VNC_MAGIC);
  225. vnc_lock_output(job->vs);
  226. if (job->vs->ioc == NULL || job->vs->abort == true) {
  227. vnc_unlock_output(job->vs);
  228. goto disconnected;
  229. }
  230. if (buffer_empty(&job->vs->output)) {
  231. /*
  232. * Looks like a NOP as it obviously moves no data. But it
  233. * moves the empty buffer, so we don't have to malloc a new
  234. * one for vs.output
  235. */
  236. buffer_move_empty(&vs.output, &job->vs->output);
  237. }
  238. vnc_unlock_output(job->vs);
  239. /* Make a local copy of vs and switch output buffers */
  240. vnc_async_encoding_start(job->vs, &vs);
  241. vs.magic = VNC_MAGIC;
  242. /* Start sending rectangles */
  243. n_rectangles = 0;
  244. vnc_write_u8(&vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
  245. vnc_write_u8(&vs, 0);
  246. saved_offset = vs.output.offset;
  247. vnc_write_u16(&vs, 0);
  248. vnc_lock_display(job->vs->vd);
  249. QLIST_FOREACH_SAFE(entry, &job->rectangles, next, tmp) {
  250. int n;
  251. if (job->vs->ioc == NULL) {
  252. vnc_unlock_display(job->vs->vd);
  253. /* Copy persistent encoding data */
  254. vnc_async_encoding_end(job->vs, &vs);
  255. goto disconnected;
  256. }
  257. if (vnc_worker_clamp_rect(&vs, job, &entry->rect)) {
  258. n = vnc_send_framebuffer_update(&vs, entry->rect.x, entry->rect.y,
  259. entry->rect.w, entry->rect.h);
  260. if (n >= 0) {
  261. n_rectangles += n;
  262. }
  263. }
  264. g_free(entry);
  265. }
  266. trace_vnc_job_nrects(&vs, job, n_rectangles);
  267. vnc_unlock_display(job->vs->vd);
  268. /* Put n_rectangles at the beginning of the message */
  269. vs.output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
  270. vs.output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
  271. vnc_lock_output(job->vs);
  272. if (job->vs->ioc != NULL) {
  273. buffer_move(&job->vs->jobs_buffer, &vs.output);
  274. /* Copy persistent encoding data */
  275. vnc_async_encoding_end(job->vs, &vs);
  276. qemu_bh_schedule(job->vs->bh);
  277. } else {
  278. buffer_reset(&vs.output);
  279. /* Copy persistent encoding data */
  280. vnc_async_encoding_end(job->vs, &vs);
  281. }
  282. vnc_unlock_output(job->vs);
  283. disconnected:
  284. vnc_lock_queue(queue);
  285. QTAILQ_REMOVE(&queue->jobs, job, next);
  286. vnc_unlock_queue(queue);
  287. qemu_cond_broadcast(&queue->cond);
  288. g_free(job);
  289. vs.magic = 0;
  290. return 0;
  291. }
  292. static VncJobQueue *vnc_queue_init(void)
  293. {
  294. VncJobQueue *queue = g_new0(VncJobQueue, 1);
  295. qemu_cond_init(&queue->cond);
  296. qemu_mutex_init(&queue->mutex);
  297. QTAILQ_INIT(&queue->jobs);
  298. return queue;
  299. }
  300. static void vnc_queue_clear(VncJobQueue *q)
  301. {
  302. qemu_cond_destroy(&queue->cond);
  303. qemu_mutex_destroy(&queue->mutex);
  304. g_free(q);
  305. queue = NULL; /* Unset global queue */
  306. }
  307. static void *vnc_worker_thread(void *arg)
  308. {
  309. VncJobQueue *queue = arg;
  310. qemu_thread_get_self(&queue->thread);
  311. while (!vnc_worker_thread_loop(queue)) ;
  312. vnc_queue_clear(queue);
  313. return NULL;
  314. }
  315. static bool vnc_worker_thread_running(void)
  316. {
  317. return queue; /* Check global queue */
  318. }
  319. void vnc_start_worker_thread(void)
  320. {
  321. VncJobQueue *q;
  322. if (vnc_worker_thread_running())
  323. return;
  324. q = vnc_queue_init();
  325. qemu_thread_create(&q->thread, "vnc_worker", vnc_worker_thread, q,
  326. QEMU_THREAD_DETACHED);
  327. queue = q; /* Set global queue */
  328. }