summaryrefslogtreecommitdiffstats
path: root/server/red_dispatcher.c
blob: 58de5a33594bde7d53bad51cb6cfbb1eca560661 (plain)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
   Copyright (C) 2009 Red Hat, Inc.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <sys/socket.h>
#include <signal.h>

#include "qxl_dev.h"
#include "vd_interface.h"
#include "red_worker.h"
#include "quic.h"
#include "cairo_canvas.h"
#include "gl_canvas.h"
#include "reds.h"
#include "red_dispatcher.h"

static int num_active_workers = 0;

//volatile

typedef struct RedDispatcher RedDispatcher;
struct RedDispatcher {
    QXLWorker base;
    QXLInterface *qxl_interface;
    int channel;
    pthread_t worker_thread;
    uint32_t pending;
    int active;
    int x_res;
    int y_res;
    int use_hardware_cursor;
    RedDispatcher *next;
};

typedef struct RedWorkeState {
    uint8_t *io_base;
    unsigned long phys_delta;

    uint32_t x_res;
    uint32_t y_res;
    uint32_t bits;
    uint32_t stride;
} RedWorkeState;

extern uint32_t streaming_video;
extern image_compression_t image_compression;

static RedDispatcher *dispatchers = NULL;

static void red_dispatcher_set_peer(Channel *channel, RedsStreamContext *peer, int migration,
                                    int num_common_caps, uint32_t *common_caps, int num_caps,
                                    uint32_t *caps)
{
    RedDispatcher *dispatcher;

    red_printf("");
    dispatcher = (RedDispatcher *)channel->data;
    RedWorkeMessage message = RED_WORKER_MESSAGE_DISPLAY_CONNECT;
    write_message(dispatcher->channel, &message);
    send_data(dispatcher->channel, &peer, sizeof(RedsStreamContext *));
    send_data(dispatcher->channel, &migration, sizeof(int));
}

static void red_dispatcher_shutdown_peer(Channel *channel)
{
    RedDispatcher *dispatcher = (RedDispatcher *)channel->data;
    red_printf("");
    RedWorkeMessage message = RED_WORKER_MESSAGE_DISPLAY_DISCONNECT;
    write_message(dispatcher->channel, &message);
}

static void red_dispatcher_migrate(Channel *channel)
{
    RedDispatcher *dispatcher = (RedDispatcher *)channel->data;
    red_printf("channel type %u id %u", channel->type, channel->id);
    RedWorkeMessage message = RED_WORKER_MESSAGE_DISPLAY_MIGRATE;
    write_message(dispatcher->channel, &message);
}

static void red_dispatcher_set_cursor_peer(Channel *channel, RedsStreamContext *peer,
                                           int migration, int num_common_caps,
                                           uint32_t *common_caps, int num_caps,
                                           uint32_t *caps)
{
    RedDispatcher *dispatcher = (RedDispatcher *)channel->data;
    red_printf("");
    RedWorkeMessage message = RED_WORKER_MESSAGE_CURSOR_CONNECT;
    write_message(dispatcher->channel, &message);
    send_data(dispatcher->channel, &peer, sizeof(RedsStreamContext *));
    send_data(dispatcher->channel, &migration, sizeof(int));
}

static void red_dispatcher_shutdown_cursor_peer(Channel *channel)
{
    RedDispatcher *dispatcher = (RedDispatcher *)channel->data;
    red_printf("");
    RedWorkeMessage message = RED_WORKER_MESSAGE_CURSOR_DISCONNECT;
    write_message(dispatcher->channel, &message);
}

static void red_dispatcher_cursor_migrate(Channel *channel)
{
    RedDispatcher *dispatcher = (RedDispatcher *)channel->data;
    red_printf("channel type %u id %u", channel->type, channel->id);
    RedWorkeMessage message = RED_WORKER_MESSAGE_CURSOR_MIGRATE;
    write_message(dispatcher->channel, &message);
}

typedef struct RendererInfo {
    int id;
    const char *name;
} RendererInfo;

static RendererInfo renderers_info[] = {
    {RED_RENDERER_CAIRO, "cairo"},
    {RED_RENDERER_OGL_PBUF, "oglpbuf"},
    {RED_RENDERER_OGL_PIXMAP, "oglpixmap"},
    {RED_RENDERER_INVALID, NULL},
};

static uint32_t renderers[RED_MAX_RENDERERS];
static uint32_t num_renderers = 0;

static RendererInfo *find_renderer(const char *name)
{
    RendererInfo *inf = renderers_info;
    while (inf->name) {
        if (strcmp(name, inf->name) == 0) {
            return inf;
        }
        inf++;
    }
    return NULL;
}

int red_dispatcher_add_renderer(const char *name)
{
    RendererInfo *inf;

    if (num_renderers == RED_MAX_RENDERERS || !(inf = find_renderer(name))) {
        return FALSE;
    }
    renderers[num_renderers++] = inf->id;
    return TRUE;
}

int red_dispatcher_qxl_count()
{
    return num_active_workers;
}

static void update_client_mouse_allowed()
{
    static int allowed = FALSE;
    int allow_now = FALSE;
    int x_res = 0;
    int y_res = 0;

    if (num_active_workers > 0) {
        allow_now = TRUE;
        RedDispatcher *now = dispatchers;
        while (now && allow_now) {
            if (now->active) {
                allow_now = now->use_hardware_cursor;
                if (num_active_workers == 1) {
                    if (allow_now) {
                        x_res = now->x_res;
                        y_res = now->y_res;
                    }
                    break;
                }
            }
            now = now->next;
        }
    }

    if (allow_now || allow_now != allowed) {
        allowed = allow_now;
        reds_set_client_mouse_allowed(allowed, x_res, y_res);
    }
}

static void qxl_worker_attach(QXLWorker *qxl_worker)
{
    RedDispatcher *dispatcher = (RedDispatcher *)qxl_worker;
    RedWorkeMessage message = RED_WORKER_MESSAGE_ATTACH;
    QXLDevInfo info;

    dispatcher->qxl_interface->get_info(dispatcher->qxl_interface, &info);
    dispatcher->x_res = info.x_res;
    dispatcher->y_res = info.y_res;
    dispatcher->use_hardware_cursor = info.use_hardware_cursor;
    dispatcher->active = TRUE;

    write_message(dispatcher->channel, &message);
    send_data(dispatcher->channel, &info, sizeof(QXLDevInfo));
    read_message(dispatcher->channel, &message);
    ASSERT(message == RED_WORKER_MESSAGE_READY);

    num_active_workers++;
    update_client_mouse_allowed();
}

static void qxl_worker_detach(QXLWorker *qxl_worker)
{
    RedDispatcher *dispatcher = (RedDispatcher *)qxl_worker;
    RedWorkeMessage message = RED_WORKER_MESSAGE_DETACH;

    write_message(dispatcher->channel, &message);
    read_message(dispatcher->channel, &message);
    ASSERT(message == RED_WORKER_MESSAGE_READY);

    dispatcher->x_res = 0;
    dispatcher->y_res = 0;
    dispatcher->use_hardware_cursor = FALSE;
    dispatcher->active = FALSE;
    num_active_workers--;
    update_client_mouse_allowed();
}

static void qxl_worker_update_area(QXLWorker *qxl_worker)
{
    RedDispatcher *dispatcher = (RedDispatcher *)qxl_worker;
    RedWorkeMessage message = RED_WORKER_MESSAGE_UPDATE;

    write_message(dispatcher->channel, &message);
    read_message(dispatcher->channel, &message);
    ASSERT(message == RED_WORKER_MESSAGE_READY);
}

static void qxl_worker_wakeup(QXLWorker *qxl_worker)
{
    RedDispatcher *dispatcher = (RedDispatcher *)qxl_worker;

    if (!test_bit(RED_WORKER_PENDING_WAKEUP, dispatcher->pending)) {
        RedWorkeMessage message = RED_WORKER_MESSAGE_WAKEUP;
        set_bit(RED_WORKER_PENDING_WAKEUP, &dispatcher->pending);
        write_message(dispatcher->channel, &message);
    }
}

static void qxl_worker_oom(QXLWorker *qxl_worker)
{
    RedDispatcher *dispatcher = (RedDispatcher *)qxl_worker;
    if (!test_bit(RED_WORKER_PENDING_OOM, dispatcher->pending)) {
        RedWorkeMessage message = RED_WORKER_MESSAGE_OOM;
        set_bit(RED_WORKER_PENDING_OOM, &dispatcher->pending);
        write_message(dispatcher->channel, &message);
    }
}

static void qxl_worker_save(QXLWorker *qxl_worker)
{
    RedDispatcher *dispatcher = (RedDispatcher *)qxl_worker;
    RedWorkeMessage message = RED_WORKER_MESSAGE_SAVE;

    write_message(dispatcher->channel, &message);
    read_message(dispatcher->channel, &message);
    ASSERT(message == RED_WORKER_MESSAGE_READY);
}

static void qxl_worker_load(QXLWorker *qxl_worker)
{
    RedDispatcher *dispatcher = (RedDispatcher *)qxl_worker;
    RedWorkeMessage message = RED_WORKER_MESSAGE_LOAD;

    write_message(dispatcher->channel, &message);
    read_message(dispatcher->channel, &message);
    ASSERT(message == RED_WORKER_MESSAGE_READY);
}

static void qxl_worker_start(QXLWorker *qxl_worker)
{
    RedDispatcher *dispatcher = (RedDispatcher *)qxl_worker;
    RedWorkeMessage message = RED_WORKER_MESSAGE_START;

    write_message(dispatcher->channel, &message);
}

static void qxl_worker_stop(QXLWorker *qxl_worker)
{
    RedDispatcher *dispatcher = (RedDispatcher *)qxl_worker;
    RedWorkeMessage message = RED_WORKER_MESSAGE_STOP;

    write_message(dispatcher->channel, &message);
    read_message(dispatcher->channel, &message);
    ASSERT(message == RED_WORKER_MESSAGE_READY);
}

void red_dispatcher_set_mm_time(uint32_t mm_time)
{
    RedDispatcher *now = dispatchers;
    while (now) {
        now->qxl_interface->set_mm_time(now->qxl_interface, mm_time);
        now = now->next;
    }
}

static inline int calc_compression_level()
{
    if (streaming_video || (image_compression != IMAGE_COMPRESS_QUIC)) {
        return 0;
    } else {
        return 1;
    }
}

void red_dispatcher_on_ic_change()
{
    int compression_level = calc_compression_level();
    RedDispatcher *now = dispatchers;
    while (now) {
        RedWorkeMessage message = RED_WORKER_MESSAGE_SET_COMPRESSION;
        now->qxl_interface->set_compression_level(now->qxl_interface, compression_level);
        write_message(now->channel, &message);
        send_data(now->channel, &image_compression, sizeof(image_compression_t));
        now = now->next;
    }
}

void red_dispatcher_on_sv_change()
{
    int compression_level = calc_compression_level();
    RedDispatcher *now = dispatchers;
    while (now) {
        RedWorkeMessage message = RED_WORKER_MESSAGE_SET_STREAMING_VIDEO;
        now->qxl_interface->set_compression_level(now->qxl_interface, compression_level);
        write_message(now->channel, &message);
        send_data(now->channel, &streaming_video, sizeof(uint32_t));
        now = now->next;
    }
}

void red_dispatcher_set_mouse_mode(uint32_t mode)
{
    RedDispatcher *now = dispatchers;
    while (now) {
        RedWorkeMessage message = RED_WORKER_MESSAGE_SET_MOUSE_MODE;
        write_message(now->channel, &message);
        send_data(now->channel, &mode, sizeof(uint32_t));
        now = now->next;
    }
}

int red_dispatcher_count()
{
    RedDispatcher *now = dispatchers;
    int ret = 0;

    while (now) {
        ret++;
        now = now->next;
    }
    return ret;
}

uint32_t red_dispatcher_qxl_ram_size()
{
    QXLDevInfo qxl_info;
    dispatchers->qxl_interface->get_info(dispatchers->qxl_interface, &qxl_info);
    return qxl_info.ram_size;
}

RedDispatcher *red_dispatcher_init(QXLInterface *qxl_interface)
{
    RedDispatcher *dispatcher;
    int channels[2];
    RedWorkeMessage message;
    WorkerInitData init_data;
    int r;
    Channel *reds_channel;
    Channel *cursor_channel;
    sigset_t thread_sig_mask;
    sigset_t curr_sig_mask;

    if (qxl_interface->pci_vendor != REDHAT_PCI_VENDOR_ID ||
        qxl_interface->pci_id != QXL_DEVICE_ID ||
        qxl_interface->pci_revision != QXL_REVISION) {
        red_printf("pci mismatch");
        return NULL;
    }

    quic_init();
    cairo_canvas_init();
    gl_canvas_init();

    if (socketpair(AF_LOCAL, SOCK_STREAM, 0, channels) == -1) {
        red_error("socketpair failed %s", strerror(errno));
    }

    if (!(dispatcher = malloc(sizeof(RedDispatcher)))) {
        red_error("malloc failed");
    }
    memset(dispatcher, 0, sizeof(RedDispatcher));
    dispatcher->channel = channels[0];
    init_data.qxl_interface = dispatcher->qxl_interface = qxl_interface;
    init_data.id = qxl_interface->base.id;
    init_data.channel = channels[1];
    init_data.pending = &dispatcher->pending;
    init_data.num_renderers = num_renderers;
    memcpy(init_data.renderers, renderers, sizeof(init_data.renderers));

    init_data.image_compression = image_compression;
    init_data.streaming_video = streaming_video;

    dispatcher->base.major_version = VD_INTERFACE_QXL_MAJOR;
    dispatcher->base.major_version = VD_INTERFACE_QXL_MINOR;
    dispatcher->base.attach = qxl_worker_attach;
    dispatcher->base.detach = qxl_worker_detach;
    dispatcher->base.wakeup = qxl_worker_wakeup;
    dispatcher->base.oom = qxl_worker_oom;
    dispatcher->base.save = qxl_worker_save;
    dispatcher->base.load = qxl_worker_load;
    dispatcher->base.start = qxl_worker_start;
    dispatcher->base.stop = qxl_worker_stop;
    dispatcher->base.update_area = qxl_worker_update_area;

    sigfillset(&thread_sig_mask);
    sigdelset(&thread_sig_mask, SIGILL);
    sigdelset(&thread_sig_mask, SIGFPE);
    sigdelset(&thread_sig_mask, SIGSEGV);
    pthread_sigmask(SIG_SETMASK, &thread_sig_mask, &curr_sig_mask);
    if ((r = pthread_create(&dispatcher->worker_thread, NULL, red_worker_main, &init_data))) {
        red_error("create thread failed %d", r);
    }
    pthread_sigmask(SIG_SETMASK, &curr_sig_mask, NULL);

    read_message(dispatcher->channel, &message);
    ASSERT(message == RED_WORKER_MESSAGE_READY);
    if (!(reds_channel = malloc(sizeof(Channel)))) {
        red_error("reds channel malloc failed");
    }
    memset(reds_channel, 0, sizeof(Channel));
    reds_channel->type = RED_CHANNEL_DISPLAY;
    reds_channel->id = qxl_interface->base.id;
    reds_channel->link = red_dispatcher_set_peer;
    reds_channel->shutdown = red_dispatcher_shutdown_peer;
    reds_channel->migrate = red_dispatcher_migrate;
    reds_channel->data = dispatcher;
    reds_register_channel(reds_channel);

    if (!(cursor_channel = malloc(sizeof(Channel)))) {
        red_error("reds channel malloc failed");
    }
    memset(cursor_channel, 0, sizeof(Channel));
    cursor_channel->type = RED_CHANNEL_CURSOR;
    cursor_channel->id = qxl_interface->base.id;
    cursor_channel->link = red_dispatcher_set_cursor_peer;
    cursor_channel->shutdown = red_dispatcher_shutdown_cursor_peer;
    cursor_channel->migrate = red_dispatcher_cursor_migrate;
    cursor_channel->data = dispatcher;
    reds_register_channel(cursor_channel);
    qxl_interface->attache_worker(qxl_interface, &dispatcher->base);
    qxl_interface->set_compression_level(qxl_interface, calc_compression_level());

    dispatcher->next = dispatchers;
    dispatchers = dispatcher;
    return dispatcher;
}