summaryrefslogtreecommitdiff
path: root/hw/kdrive/ephyr/ephyrhostglx.c
blob: 007524d04cef7b84611d565054ef23d9d5f7f519 (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
480
481
482
483
484
485
486
487
488
489
490
/*
 * Xephyr - A kdrive X server thats runs in a host X window.
 *          Authored by Matthew Allum <mallum@openedhand.com>
 * 
 * Copyright © 2007 OpenedHand Ltd 
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of OpenedHand Ltd not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission. OpenedHand Ltd makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * OpenedHand Ltd DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL OpenedHand Ltd BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 * a lots of the content of this file has been adapted from the mesa source
 * code.
 * Authors:
 *    Dodji Seketeli <dodji@openedhand.com>
 */
#ifdef HAVE_CONFIG_H
#include <kdrive-config.h>
#endif

#include <X11/Xdefs.h>
#include <X11/Xmd.h>
#include <GL/glxproto.h>
#include <xcb/glx.h>
#include "ephyrhostglx.h"
#define _HAVE_XALLOC_DECLS
#include "ephyrlog.h"
#include "hostx.h"

static int glx_major, glx_minor;

enum VisualConfRequestType {
    EPHYR_GET_FB_CONFIG,
    EPHYR_VENDOR_PRIV_GET_FB_CONFIG_SGIX,
    EPHYR_GET_VISUAL_CONFIGS
};

static Bool ephyrHostGLXGetVisualConfigsInternal
    (enum VisualConfRequestType a_type,
     xcb_glx_get_visual_configs_reply_t *reply,
     int32_t a_screen,
     int32_t *a_num_visuals,
     int32_t *a_num_props,
     int32_t *a_props_buf_size,
     int32_t **a_props_buf);

Bool
ephyrHostGLXQueryVersion(int *a_major, int *a_minor)
{
    Bool is_ok = FALSE;
    xcb_connection_t *conn = hostx_get_xcbconn();
    xcb_glx_query_version_cookie_t cookie;
    xcb_glx_query_version_reply_t *reply;

    EPHYR_RETURN_VAL_IF_FAIL(a_major && a_minor, FALSE);
    EPHYR_LOG("enter\n");

    if (glx_major) {
        *a_major = glx_major;
        *a_minor = glx_minor;
        return TRUE;
    }

    /* Send the glXQueryVersion request */
    cookie = xcb_glx_query_version(conn, 2, 1);
    reply = xcb_glx_query_version_reply(conn, cookie, NULL);
    if (!reply)
        goto out;
    *a_major = reply->major_version;
    *a_minor = reply->minor_version;
    free(reply);

    EPHYR_LOG("major:%d, minor:%d\n", *a_major, *a_minor);

    is_ok = TRUE;
 out:
    EPHYR_LOG("leave\n");
    return is_ok;
}

Bool
ephyrHostGLXGetString(int a_context_tag,
                      int a_string_name,
                      char **a_string)
{
    Bool is_ok = FALSE;
    xcb_connection_t *conn = hostx_get_xcbconn();
    xcb_glx_get_string_cookie_t cookie;
    xcb_glx_get_string_reply_t *reply;

    EPHYR_RETURN_VAL_IF_FAIL(conn && a_string, FALSE);

    EPHYR_LOG("enter\n");
    cookie = xcb_glx_get_string(conn, a_context_tag, a_string_name);
    reply = xcb_glx_get_string_reply(conn, cookie, NULL);
    if (!reply)
        goto out;
    *a_string = malloc(reply->n + 1);
    memcpy(*a_string, xcb_glx_get_string_string(reply), reply->n);
    (*a_string)[reply->n] = '\0';
    free(reply);
    is_ok = TRUE;
out:
    EPHYR_LOG("leave\n");
    return is_ok;
}

Bool ephyrHostGLXQueryServerString(int a_screen_number,
                                   int a_string_name,
                                   char **a_string)
{
    Bool is_ok = FALSE;
    xcb_connection_t *conn = hostx_get_xcbconn();
    int default_screen = hostx_get_screen();
    xcb_glx_query_server_string_cookie_t cookie;
    xcb_glx_query_server_string_reply_t *reply;

    EPHYR_RETURN_VAL_IF_FAIL(conn && a_string, FALSE);

    EPHYR_LOG("enter\n");
    cookie = xcb_glx_query_server_string(conn, default_screen, a_string_name);
    reply = xcb_glx_query_server_string_reply(conn, cookie, NULL);
    if (!reply)
        goto out;
    *a_string = malloc(reply->str_len + 1);
    memcpy(*a_string, xcb_glx_query_server_string_string(reply), reply->str_len);
    (*a_string)[reply->str_len] = '\0';
    free(reply);
    is_ok = TRUE;
out:
    EPHYR_LOG("leave\n");
    return is_ok;
}

static Bool
ephyrHostGLXGetVisualConfigsInternal(enum VisualConfRequestType a_type,
                                     xcb_glx_get_visual_configs_reply_t *reply,
                                     int32_t a_screen,
                                     int32_t * a_num_visuals,
                                     int32_t * a_num_props,
                                     int32_t * a_props_buf_size,
                                     int32_t ** a_props_buf)
{
    Bool is_ok = FALSE;
    int num_props = 0, num_visuals = 0, props_buf_size = 0;
    int props_per_visual_size = 0;
    int32_t *props_buf = NULL;

   if (!reply->num_visuals) {
        EPHYR_LOG_ERROR("screen does not support GL rendering\n");
        goto out;
    }
    num_visuals = reply->num_visuals;

    num_props = reply->num_properties;

    if (a_type != EPHYR_GET_VISUAL_CONFIGS) {
        num_props *= 2;
    }
    props_per_visual_size = num_props * sizeof(uint32_t);
    props_buf_size = props_per_visual_size * reply->num_visuals;
    props_buf = malloc(props_buf_size);
    if (!props_buf)
        goto out;
    memcpy(props_buf, xcb_glx_get_visual_configs_property_list(reply),
           props_buf_size);

    *a_num_visuals = num_visuals;
    *a_num_props = reply->num_properties;
    *a_props_buf_size = props_buf_size;
    *a_props_buf = props_buf;
    is_ok = TRUE;

out:
    return is_ok;
}

Bool
ephyrHostGLXGetVisualConfigs(int32_t a_screen,
                             int32_t * a_num_visuals,
                             int32_t * a_num_props,
                             int32_t * a_props_buf_size, int32_t ** a_props_buf)
{
    Bool is_ok = FALSE;
    xcb_glx_get_visual_configs_cookie_t cookie;
    xcb_glx_get_visual_configs_reply_t *reply;
    xcb_connection_t *conn = hostx_get_xcbconn();
    int screen = hostx_get_screen();

    EPHYR_LOG("enter\n");
    cookie = xcb_glx_get_visual_configs(conn, screen);
    reply = xcb_glx_get_visual_configs_reply(conn, cookie, NULL);
    if (!reply)
        goto out;
    is_ok = ephyrHostGLXGetVisualConfigsInternal
        (EPHYR_GET_VISUAL_CONFIGS,
         reply,
         a_screen,
         a_num_visuals,
         a_num_props,
         a_props_buf_size,
         a_props_buf);

out:
    free(reply);
    EPHYR_LOG("leave:%d\n", is_ok);
    return is_ok;
}

Bool
ephyrHostGLXVendorPrivGetFBConfigsSGIX(int a_screen,
                                       int32_t * a_num_visuals,
                                       int32_t * a_num_props,
                                       int32_t * a_props_buf_size,
                                       int32_t ** a_props_buf)
{
    Bool is_ok=FALSE;
    xcb_connection_t *conn = hostx_get_xcbconn();
    int screen = hostx_get_screen();
    xcb_glx_vendor_private_with_reply_cookie_t cookie;
    union {
        xcb_glx_vendor_private_with_reply_reply_t *vprep;
        xcb_glx_get_visual_configs_reply_t *rep;
    } reply;

    EPHYR_LOG("enter\n");
    cookie = xcb_glx_vendor_private_with_reply(conn,
                                               X_GLXvop_GetFBConfigsSGIX,
                                               0, 4, (uint8_t *)&screen);
    reply.vprep = xcb_glx_vendor_private_with_reply_reply(conn, cookie, NULL);
    if (!reply.vprep)
        goto out;
    is_ok = ephyrHostGLXGetVisualConfigsInternal
        (EPHYR_VENDOR_PRIV_GET_FB_CONFIG_SGIX,
         reply.rep,
         a_screen,
         a_num_visuals,
         a_num_props,
         a_props_buf_size,
         a_props_buf);
out:
    free(reply.vprep);
    EPHYR_LOG("leave\n");
    return is_ok;
}

Bool
ephyrHostGLXSendClientInfo(int32_t a_major, int32_t a_minor,
                           const char *a_extension_list)
{
    xcb_connection_t *conn = hostx_get_xcbconn();
    int size;

    EPHYR_RETURN_VAL_IF_FAIL(conn && a_extension_list, FALSE);

    size = strlen (a_extension_list) + 1;
    xcb_glx_client_info(conn, a_major, a_minor, size, a_extension_list);

    return TRUE;
}

Bool
ephyrHostGLXCreateContext(int a_screen,
                          int a_generic_id,
                          int a_context_id,
                          int a_share_list_ctxt_id,
                          int a_render_type,
                          Bool a_direct,
                          int code)
{
    xcb_connection_t *conn = hostx_get_xcbconn();
    Bool is_ok = FALSE;
    int remote_context_id = 0;

    EPHYR_LOG("enter. screen:%d, generic_id:%d, contextid:%d, rendertype:%d, "
                 "direct:%d\n", a_screen, a_generic_id, a_context_id,
                 a_render_type, a_direct);

    if (!hostx_allocate_resource_id_peer(a_context_id, &remote_context_id)) {
        EPHYR_LOG_ERROR("failed to peer the context id %d host X",
                        remote_context_id);
        goto out;
    }

    switch (code) {
    case X_GLXCreateContext: {
        xcb_glx_create_context(conn,
                               remote_context_id,
                               a_generic_id,
                               hostx_get_screen(),
                               a_share_list_ctxt_id,
                               a_direct);
   }

    case X_GLXCreateNewContext: {
        xcb_glx_create_new_context(conn,
                                   remote_context_id,
                                   a_generic_id,
                                   hostx_get_screen(),
                                   a_render_type,
                                   a_share_list_ctxt_id,
                                   a_direct);
    }

    default:
        /* This should never be reached !*/
        EPHYR_LOG("Internal error! Invalid CreateContext code!\n");
    }

    is_ok = TRUE;

 out:
    EPHYR_LOG("leave\n");
    return is_ok;
}

Bool
ephyrHostDestroyContext(int a_ctxt_id)
{
    xcb_connection_t *conn = hostx_get_xcbconn();
    Bool is_ok = FALSE;
    int remote_ctxt_id = 0;

    EPHYR_LOG("enter:%d\n", a_ctxt_id);

    if (!hostx_get_resource_id_peer(a_ctxt_id, &remote_ctxt_id)) {
        EPHYR_LOG_ERROR("failed to get remote glx ctxt id\n");
        goto out;
    }
    EPHYR_LOG("host context id:%d\n", remote_ctxt_id);

    xcb_glx_destroy_context(conn, remote_ctxt_id);

    is_ok = TRUE;

 out:
    EPHYR_LOG("leave\n");
    return is_ok;
}

Bool
ephyrHostGLXMakeCurrent(int a_drawable, int a_readable,
                        int a_glx_ctxt_id, int a_old_ctxt_tag, int *a_ctxt_tag)
{
    xcb_connection_t *conn = hostx_get_xcbconn();
    Bool is_ok = FALSE;
    int remote_glx_ctxt_id = 0;

    EPHYR_RETURN_VAL_IF_FAIL(a_ctxt_tag, FALSE);

    EPHYR_LOG("enter. drawable:%d, read:%d, context:%d, oldtag:%d\n",
              a_drawable, a_readable, a_glx_ctxt_id, a_old_ctxt_tag);

    if (!hostx_get_resource_id_peer(a_glx_ctxt_id, &remote_glx_ctxt_id)) {
        EPHYR_LOG_ERROR("failed to get remote glx ctxt id\n");
        goto out;
    }

    /* If both drawables are the same, use the old MakeCurrent request.
     * Otherwise, if we have GLX 1.3 or higher, use the MakeContextCurrent
     * request which supports separate read and draw targets.  Failing that,
     * try the SGI MakeCurrentRead extension.  Logic cribbed from Mesa. */
    if (a_drawable == a_readable) {
        xcb_glx_make_current_cookie_t cookie;
        xcb_glx_make_current_reply_t *reply;
        cookie = xcb_glx_make_current(conn,
                                      a_drawable,
                                      remote_glx_ctxt_id,
                                      a_old_ctxt_tag);
        reply = xcb_glx_make_current_reply(conn, cookie, NULL);
        if (!reply)
            goto out;
        *a_ctxt_tag = reply->context_tag;
        free(reply);
    }
    else if (glx_major > 1 || glx_minor >= 3) {
        xcb_glx_make_context_current_cookie_t cookie;
        xcb_glx_make_context_current_reply_t *reply;
        cookie = xcb_glx_make_context_current(conn,
                                              a_old_ctxt_tag,
                                              a_drawable,
                                              a_readable,
                                              remote_glx_ctxt_id);
        reply = xcb_glx_make_context_current_reply(conn, cookie, NULL);
        if (!reply)
            goto out;
        *a_ctxt_tag = reply->context_tag;
        free(reply);
    }
    else {
        xcb_glx_vendor_private_with_reply_cookie_t cookie;
        xcb_glx_vendor_private_with_reply_reply_t *reply;
        uint32_t data[3] = {
            a_drawable, a_readable, remote_glx_ctxt_id,
        };

        EPHYR_LOG("enter\n");
        cookie = xcb_glx_vendor_private_with_reply(conn,
                                                   X_GLXvop_MakeCurrentReadSGI,
                                                   a_old_ctxt_tag,
                                                   sizeof(data),
                                                   (uint8_t *)data);
        reply = xcb_glx_vendor_private_with_reply_reply(conn, cookie, NULL);

        *a_ctxt_tag = reply->retval;

        free(reply);
    }

    EPHYR_LOG("context tag:%d\n", *a_ctxt_tag);
    is_ok = TRUE;

 out:
    EPHYR_LOG("leave\n");
    return is_ok;
}

Bool
ephyrHostGetIntegerValue(int a_current_context_tag, int a_int, int *a_val)
{
    xcb_connection_t *conn = hostx_get_xcbconn();
    Bool is_ok = FALSE;
    int size = 0;
    xcb_glx_get_integerv_cookie_t cookie;
    xcb_glx_get_integerv_reply_t *reply;

    EPHYR_RETURN_VAL_IF_FAIL(a_val, FALSE);

    EPHYR_LOG("enter\n");
    cookie = xcb_glx_get_integerv(conn, a_current_context_tag, a_int);
    reply = xcb_glx_get_integerv_reply(conn, cookie, NULL);
    if (!reply)
        goto out;
    size = reply->n;
    if (!size) {
        EPHYR_LOG_ERROR("X_GLsop_GetIngerv failed\n");
        goto out;
    }
    *a_val = reply->datum;
    is_ok = TRUE;

out:
    free(reply);
    EPHYR_LOG("leave\n");
    return is_ok;
}

Bool
ephyrHostIsContextDirect(int a_ctxt_id, int *a_is_direct)
{
    Bool is_ok = FALSE;
    xcb_connection_t *conn = hostx_get_xcbconn();
    xcb_glx_is_direct_cookie_t cookie;
    xcb_glx_is_direct_reply_t *reply = NULL;
    int remote_glx_ctxt_id = 0;

    EPHYR_LOG("enter\n");
    if (!hostx_get_resource_id_peer (a_ctxt_id, &remote_glx_ctxt_id)) {
        EPHYR_LOG_ERROR ("failed to get remote glx ctxt id\n");
        goto out;
    }

    /* Send the glXIsDirect request */
    cookie = xcb_glx_is_direct(conn, remote_glx_ctxt_id);
    reply = xcb_glx_is_direct_reply(conn, cookie, NULL);
    if (!reply) {
        EPHYR_LOG_ERROR("fail in reading reply from host\n");
        goto out;
    }
    *a_is_direct = reply->is_direct;
    is_ok = TRUE;

out:
    free(reply);
    EPHYR_LOG("leave\n");
    return is_ok;
}