blob: ef0f95fcaccb4966728482822fb075f9d12c7968 [file] [log] [blame]
Chia-I Wu1db76e02014-09-15 14:21:14 +08001/*
2 * XGL
3 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <unistd.h>
31#include <fcntl.h>
32#include <xcb/xcb.h>
33#include <xcb/dri3.h>
34#include <xcb/present.h>
35
36#include "kmd/winsys.h"
37#include "dev.h"
38#include "fence.h"
39#include "gpu.h"
40#include "img.h"
41#include "mem.h"
42#include "queue.h"
43#include "wsi_x11.h"
44
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +080045struct intel_wsi_x11_window {
46 xcb_window_t window_id;
Chia-I Wu1db76e02014-09-15 14:21:14 +080047
48 xcb_present_event_t present_special_event_id;
49 xcb_special_event_t *present_special_event;
50
51 struct {
52 uint32_t serial;
53 } local;
54
55 struct {
56 uint32_t serial;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060057 uint64_t msc;
Chia-I Wu1db76e02014-09-15 14:21:14 +080058 } remote;
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +080059
60 struct intel_wsi_x11_window *next;
61};
62
63struct intel_wsi_x11 {
64 xcb_connection_t *c;
65 xcb_window_t root;
66 xcb_randr_provider_t provider;
67 int root_depth;
68
69 int dri3_major, dri3_minor;
70 int present_major, present_minor;
71
72 int fd;
73
74 struct intel_wsi_x11_window *windows;
Chia-I Wu1db76e02014-09-15 14:21:14 +080075};
76
77/**
78 * Return true if DRI3 and Present are supported by the server.
79 */
80static bool wsi_x11_has_dri3_and_present(xcb_connection_t *c)
81{
82 const xcb_query_extension_reply_t *ext;
83
84 xcb_prefetch_extension_data(c, &xcb_dri3_id);
85 xcb_prefetch_extension_data(c, &xcb_present_id);
86
87 ext = xcb_get_extension_data(c, &xcb_dri3_id);
88 if (!ext || !ext->present)
89 return false;
90
91 ext = xcb_get_extension_data(c, &xcb_present_id);
92 if (!ext || !ext->present)
93 return false;
94
95 return true;
96}
97
98/**
99 * Return the depth of the root window.
100 */
101static int wsi_x11_get_root_depth(struct intel_wsi_x11 *x11)
102{
103 const xcb_setup_t *setup;
104 xcb_screen_iterator_t iter;
105
106 setup = xcb_get_setup(x11->c);
107
108 iter = xcb_setup_roots_iterator(setup);
109 for (; iter.rem; xcb_screen_next(&iter)) {
110 if (iter.data->root == x11->root)
111 return iter.data->root_depth;
112 }
113
114 return 0;
115}
116
117/**
118 * Query DRI3 and Present versions and return an intel_wsi_x11.
119 */
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800120static struct intel_wsi_x11 *wsi_x11_create(const struct intel_gpu *gpu,
121 xcb_connection_t *c,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800122 xcb_window_t root,
123 xcb_randr_provider_t provider)
124{
125 xcb_dri3_query_version_cookie_t dri3_cookie;
126 xcb_dri3_query_version_reply_t *dri3_reply;
127 xcb_present_query_version_cookie_t present_cookie;
128 xcb_present_query_version_reply_t *present_reply;
129 struct intel_wsi_x11 *x11;
130
131 dri3_cookie = xcb_dri3_query_version(c,
132 XCB_DRI3_MAJOR_VERSION, XCB_DRI3_MINOR_VERSION);
133 present_cookie = xcb_present_query_version(c,
134 XCB_PRESENT_MAJOR_VERSION, XCB_PRESENT_MINOR_VERSION);
135
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800136 x11 = intel_alloc(gpu, sizeof(*x11), 0, XGL_SYSTEM_ALLOC_INTERNAL);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800137 if (!x11)
138 return NULL;
139 memset(x11, 0, sizeof(*x11));
140
141 x11->c = c;
142 x11->root = root;
143 x11->provider = provider;
144
145 x11->root_depth = wsi_x11_get_root_depth(x11);
146
147 dri3_reply = xcb_dri3_query_version_reply(c, dri3_cookie, NULL);
148 if (!dri3_reply) {
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800149 intel_free(gpu, x11);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800150 return NULL;
151 }
152
153 x11->dri3_major = dri3_reply->major_version;
154 x11->dri3_minor = dri3_reply->minor_version;
155 free(dri3_reply);
156
157 present_reply = xcb_present_query_version_reply(c, present_cookie, NULL);
158 if (!present_reply) {
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800159 intel_free(gpu, x11);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800160 return NULL;
161 }
162
163 x11->present_major = present_reply->major_version;
164 x11->present_minor = present_reply->minor_version;
165 free(present_reply);
166
167 x11->fd = -1;
168
169 return x11;
170}
171
172/**
173 * Return true if x11->fd points to the primary or render node of the GPU.
174 */
175static bool wsi_x11_uses_gpu(const struct intel_wsi_x11 *x11,
176 const struct intel_gpu *gpu)
177{
178 struct stat x11_stat, gpu_stat;
179
180 if (fstat(x11->fd, &x11_stat))
181 return false;
182
183 /* is it the primary node? */
184 if (!stat(gpu->primary_node, &gpu_stat) &&
185 !memcmp(&x11_stat, &gpu_stat, sizeof(x11_stat)))
186 return true;
187
188 /* is it the render node? */
189 if (gpu->render_node && !stat(gpu->render_node, &gpu_stat) &&
190 !memcmp(&x11_stat, &gpu_stat, sizeof(x11_stat)))
191 return true;
192
193 return false;
194}
195
196/**
197 * Send a DRI3Open to get the server GPU fd.
198 */
199static XGL_RESULT wsi_x11_dri3_open(struct intel_wsi_x11 *x11)
200{
201 xcb_dri3_open_cookie_t cookie;
202 xcb_dri3_open_reply_t *reply;
203 int fd;
204
205 cookie = xcb_dri3_open(x11->c, x11->root, x11->provider);
206 reply = xcb_dri3_open_reply(x11->c, cookie, NULL);
207 if (!reply)
208 return XGL_ERROR_UNKNOWN;
209
210 fd = (reply->nfd == 1) ? xcb_dri3_open_reply_fds(x11->c, reply)[0] : -1;
211 free(reply);
212
213 if (fd < 0)
214 return XGL_ERROR_UNKNOWN;
215
216 fcntl(fd, F_SETFD, FD_CLOEXEC);
217 x11->fd = fd;
218
219 return XGL_SUCCESS;
220}
221
222/**
223 * Send a DRI3PixmapFromBuffer to create a Pixmap from \p mem for \p img.
224 */
225static XGL_RESULT wsi_x11_dri3_pixmap_from_buffer(struct intel_wsi_x11 *x11,
226 struct intel_dev *dev,
227 struct intel_img *img,
228 struct intel_mem *mem)
229{
230 struct intel_winsys_handle export;
231 xcb_pixmap_t pixmap;
232
233 /* get prime fd of the bo first */
234 export.type = INTEL_WINSYS_HANDLE_FD;
235 if (intel_winsys_export_handle(dev->winsys, mem->bo, img->layout.tiling,
236 img->layout.bo_stride, img->layout.bo_height, &export))
237 return XGL_ERROR_UNKNOWN;
238
239 pixmap = xcb_generate_id(x11->c);
240
241 /* create a pixmap from the prime fd */
242 xcb_dri3_pixmap_from_buffer(x11->c, pixmap,
243 x11->root, img->total_size,
244 img->layout.width0, img->layout.height0,
245 img->layout.bo_stride, x11->root_depth,
246 img->layout.block_size * 8, export.handle);
247
248 img->x11_prime_fd = export.handle;
249 img->x11_pixmap = pixmap;
250
251 return XGL_SUCCESS;
252}
253
254/**
255 * Send a PresentSelectInput to select interested events.
256 */
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800257static XGL_RESULT wsi_x11_present_select_input(struct intel_wsi_x11 *x11,
258 struct intel_wsi_x11_window *win)
Chia-I Wu1db76e02014-09-15 14:21:14 +0800259{
260 xcb_void_cookie_t cookie;
261 xcb_generic_error_t *error;
262
263 /* create the event queue */
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800264 win->present_special_event_id = xcb_generate_id(x11->c);
265 win->present_special_event = xcb_register_for_special_xge(x11->c,
266 &xcb_present_id, win->present_special_event_id, NULL);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800267
268 cookie = xcb_present_select_input_checked(x11->c,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800269 win->present_special_event_id, win->window_id,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800270 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY);
271
272 error = xcb_request_check(x11->c, cookie);
273 if (error) {
274 free(error);
275 return XGL_ERROR_UNKNOWN;
276 }
277
278 return XGL_SUCCESS;
279}
280
281/**
282 * Send a PresentPixmap.
283 */
284static XGL_RESULT wsi_x11_present_pixmap(struct intel_wsi_x11 *x11,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800285 struct intel_wsi_x11_window *win,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800286 const XGL_WSI_X11_PRESENT_INFO *info)
287{
288 struct intel_img *img = intel_img(info->srcImage);
289 uint32_t options = XCB_PRESENT_OPTION_NONE;
290 xcb_void_cookie_t cookie;
291 xcb_generic_error_t *err;
292
293 if (info->async)
294 options |= XCB_PRESENT_OPTION_ASYNC;
295 if (!info->flip)
296 options |= XCB_PRESENT_OPTION_COPY;
297
298 cookie = xcb_present_pixmap(x11->c,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800299 win->window_id,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800300 img->x11_pixmap,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800301 ++win->local.serial,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800302 0, /* valid-area */
303 0, /* update-area */
304 0, /* x-off */
305 0, /* y-off */
306 info->crtc,
307 0, /* wait-fence */
308 0, /* idle-fence */
309 options,
310 info->target_msc,
311 info->divisor,
312 info->remainder,
313 0, NULL);
314
315 err = xcb_request_check(x11->c, cookie);
316 if (err) {
317 free(err);
318 return XGL_ERROR_UNKNOWN;
319 }
320
321 return XGL_SUCCESS;
322}
323
324/**
325 * Send a PresentNotifyMSC for the current MSC.
326 */
327static void wsi_x11_present_notify_msc(struct intel_wsi_x11 *x11,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800328 struct intel_wsi_x11_window *win)
Chia-I Wu1db76e02014-09-15 14:21:14 +0800329{
330 /* cannot specify CRTC? */
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800331 xcb_present_notify_msc(x11->c, win->window_id, ++win->local.serial,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800332 0, 0, 0);
333
334 xcb_flush(x11->c);
335}
336
337/**
338 * Handle a Present event.
339 */
340static void wsi_x11_present_event(struct intel_wsi_x11 *x11,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800341 struct intel_wsi_x11_window *win,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800342 const xcb_present_generic_event_t *ev)
343{
344 union {
345 const xcb_present_generic_event_t *ev;
346 const xcb_present_complete_notify_event_t *complete;
347 } u = { .ev = ev };
348
349 switch (u.ev->evtype) {
350 case XCB_PRESENT_COMPLETE_NOTIFY:
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800351 win->remote.serial = u.complete->serial;
352 win->remote.msc = u.complete->msc;
Chia-I Wu1db76e02014-09-15 14:21:14 +0800353 break;
354 default:
355 break;
356 }
357}
358
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800359static struct intel_wsi_x11_window *wsi_x11_create_window(const struct intel_gpu *gpu,
360 struct intel_wsi_x11 *x11,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800361 xcb_window_t win_id)
362{
363 struct intel_wsi_x11_window *win;
364
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800365 win = intel_alloc(gpu, sizeof(*win), 0, XGL_SYSTEM_ALLOC_INTERNAL);
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800366 if (!win)
367 return NULL;
368
369 memset(win, 0, sizeof(*win));
370
371 win->window_id = win_id;
372
373 if (wsi_x11_present_select_input(x11, win) != XGL_SUCCESS) {
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800374 intel_free(gpu, win);
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800375 return NULL;
376 }
377
378 return win;
379}
380
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800381static void wsi_x11_destroy_window(const struct intel_gpu *gpu,
382 struct intel_wsi_x11 *x11,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800383 struct intel_wsi_x11_window *win)
384{
385 if (win->present_special_event)
386 xcb_unregister_for_special_event(x11->c, win->present_special_event);
387
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800388 intel_free(gpu, win);
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800389}
390
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800391static struct intel_wsi_x11_window *wsi_x11_lookup_window(const struct intel_gpu *gpu,
392 struct intel_wsi_x11 *x11,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800393 xcb_window_t win_id)
394{
395 struct intel_wsi_x11_window *win = x11->windows;
396
397 while (win) {
398 if (win->window_id == win_id)
399 break;
400 win = win->next;
401 }
402
403 /* lookup failed */
404 if (!win) {
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800405 win = wsi_x11_create_window(gpu, x11, win_id);
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800406 if (win) {
407 win->next = x11->windows;
408 x11->windows = win;
409 }
410 }
411
412 return win;
413}
414
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800415void intel_wsi_x11_destroy(const struct intel_gpu *gpu,
416 struct intel_wsi_x11 *x11)
Chia-I Wu1db76e02014-09-15 14:21:14 +0800417{
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800418 struct intel_wsi_x11_window *win = x11->windows;
419
420 while (win) {
421 struct intel_wsi_x11_window *next = win->next;
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800422 wsi_x11_destroy_window(gpu, x11, win);
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800423 win = next;
424 }
Chia-I Wu1db76e02014-09-15 14:21:14 +0800425
426 if (x11->fd >= 0)
427 close(x11->fd);
428
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800429 intel_free(gpu, x11);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800430}
431
432XGL_RESULT intel_wsi_x11_wait(struct intel_wsi_x11 *x11,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800433 struct intel_wsi_x11_window *win,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800434 uint32_t serial, bool wait)
435{
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800436 while (win->remote.serial < serial) {
Chia-I Wu1db76e02014-09-15 14:21:14 +0800437 xcb_present_generic_event_t *ev;
438
439 if (wait) {
440 ev = (xcb_present_generic_event_t *)
441 xcb_wait_for_special_event(x11->c,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800442 win->present_special_event);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800443 if (!ev)
444 return XGL_ERROR_UNKNOWN;
445 } else {
446 ev = (xcb_present_generic_event_t *)
447 xcb_poll_for_special_event(x11->c,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800448 win->present_special_event);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800449 if (!ev)
450 return XGL_NOT_READY;
451 }
452
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800453 wsi_x11_present_event(x11, win, ev);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800454
455 free(ev);
456 }
457
458 return XGL_SUCCESS;
459}
460
Chia-I Wuc505a6f2014-11-13 00:04:01 +0800461static bool wsi_x11_is_format_presentable(struct intel_wsi_x11 *x11,
462 struct intel_dev *dev,
463 XGL_FORMAT format)
464{
465 /* this is what DDX expects */
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700466 switch (format) {
467 case XGL_FMT_B5G6R5_UNORM:
468 case XGL_FMT_B8G8R8A8_UNORM:
469 case XGL_FMT_B8G8R8A8_SRGB:
470 return true;
Chia-I Wuc505a6f2014-11-13 00:04:01 +0800471 default:
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700472 return false;
Chia-I Wuc505a6f2014-11-13 00:04:01 +0800473 }
Chia-I Wuc505a6f2014-11-13 00:04:01 +0800474}
475
Chia-I Wu1db76e02014-09-15 14:21:14 +0800476/**
477 * Create a presentable image.
478 */
479static XGL_RESULT wsi_x11_img_create(struct intel_wsi_x11 *x11,
480 struct intel_dev *dev,
481 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO *info,
482 struct intel_img **img_ret)
483{
484 XGL_IMAGE_CREATE_INFO img_info;
485 XGL_MEMORY_ALLOC_INFO mem_info;
486 struct intel_img *img;
487 struct intel_mem *mem;
488 XGL_RESULT ret;
489
Chia-I Wuc505a6f2014-11-13 00:04:01 +0800490 if (!wsi_x11_is_format_presentable(x11, dev, info->format)) {
491 intel_dev_log(dev, XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0,
492 XGL_NULL_HANDLE, 0, 0, "invalid presentable image format");
493 return XGL_ERROR_INVALID_VALUE;
494 }
495
Chia-I Wu1db76e02014-09-15 14:21:14 +0800496 /* create image */
497 memset(&img_info, 0, sizeof(img_info));
498 img_info.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
499 img_info.imageType = XGL_IMAGE_2D;
500 img_info.format = info->format;
501 img_info.extent.width = info->extent.width;
502 img_info.extent.height = info->extent.height;
503 img_info.extent.depth = 1;
504 img_info.mipLevels = 1;
505 img_info.arraySize = 1;
506 img_info.samples = 1;
507 img_info.tiling = XGL_OPTIMAL_TILING;
508 img_info.usage = info->usage;
509 img_info.flags = 0;
510
511 ret = intel_img_create(dev, &img_info, true, &img);
512 if (ret != XGL_SUCCESS)
513 return ret;
514
515 /* allocate memory */
516 memset(&mem_info, 0, sizeof(mem_info));
517 mem_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
518 mem_info.allocationSize = img->total_size;
Jon Ashburn542cd092015-01-20 13:55:32 -0700519 mem_info.memProps = 0;
Jon Ashburn32769172015-01-20 15:06:59 -0700520 mem_info.memType = XGL_MEMORY_TYPE_IMAGE;
Chia-I Wu1db76e02014-09-15 14:21:14 +0800521 mem_info.memPriority = XGL_MEMORY_PRIORITY_HIGH;
522
523 ret = intel_mem_alloc(dev, &mem_info, &mem);
524 if (ret != XGL_SUCCESS) {
525 intel_img_destroy(img);
526 return ret;
527 }
528
529 ret = wsi_x11_dri3_pixmap_from_buffer(x11, dev, img, mem);
530 if (ret != XGL_SUCCESS) {
531 intel_mem_free(mem);
532 intel_img_destroy(img);
533 return ret;
534 }
535
536 intel_obj_bind_mem(&img->obj, mem, 0);
537
538 *img_ret = img;
539
540 return XGL_SUCCESS;
541}
542
Chia-I Wu96177272015-01-03 15:27:41 +0800543ICD_EXPORT XGL_RESULT XGLAPI xglWsiX11AssociateConnection(
Chia-I Wu1db76e02014-09-15 14:21:14 +0800544 XGL_PHYSICAL_GPU gpu_,
545 const XGL_WSI_X11_CONNECTION_INFO* pConnectionInfo)
546{
547 struct intel_gpu *gpu = intel_gpu(gpu_);
548 struct intel_wsi_x11 *x11;
549 XGL_RESULT ret;
550
551 if (gpu->x11)
552 return XGL_SUCCESS;
553
Chia-I Wud8965932014-10-13 13:32:37 +0800554 if (gpu->winsys)
Chia-I Wu1db76e02014-09-15 14:21:14 +0800555 return XGL_ERROR_DEVICE_ALREADY_CREATED;
556
557 if (!wsi_x11_has_dri3_and_present(pConnectionInfo->pConnection))
558 return XGL_ERROR_UNKNOWN;
559
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800560 x11 = wsi_x11_create(gpu, pConnectionInfo->pConnection,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800561 pConnectionInfo->root, pConnectionInfo->provider);
562 if (!x11)
563 return XGL_ERROR_UNKNOWN;
564
565 ret = wsi_x11_dri3_open(x11);
566 if (ret != XGL_SUCCESS) {
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800567 intel_wsi_x11_destroy(gpu, x11);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800568 return ret;
569 }
570
571 if (!wsi_x11_uses_gpu(x11, gpu)) {
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800572 intel_wsi_x11_destroy(gpu, x11);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800573 return XGL_ERROR_UNKNOWN;
574 }
575
Chia-I Wu1db76e02014-09-15 14:21:14 +0800576 intel_gpu_associate_x11(gpu, x11, x11->fd);
577
578 return XGL_SUCCESS;
579}
580
Chia-I Wu96177272015-01-03 15:27:41 +0800581ICD_EXPORT XGL_RESULT XGLAPI xglWsiX11GetMSC(
Chia-I Wu1db76e02014-09-15 14:21:14 +0800582 XGL_DEVICE device,
Chia-I Wu6204f342014-11-07 13:33:45 +0800583 xcb_window_t window,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800584 xcb_randr_crtc_t crtc,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600585 uint64_t * pMsc)
Chia-I Wu1db76e02014-09-15 14:21:14 +0800586{
587 struct intel_dev *dev = intel_dev(device);
588 struct intel_wsi_x11 *x11 = dev->gpu->x11;
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800589 struct intel_wsi_x11_window *win;
Chia-I Wu1db76e02014-09-15 14:21:14 +0800590 XGL_RESULT ret;
591
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800592 win = wsi_x11_lookup_window(dev->gpu, x11, window);
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800593 if (!win)
594 return XGL_ERROR_UNKNOWN;
595
596 wsi_x11_present_notify_msc(x11, win);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800597
598 /* wait for the event */
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800599 ret = intel_wsi_x11_wait(x11, win, win->local.serial, -1);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800600 if (ret != XGL_SUCCESS)
601 return ret;
602
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800603 *pMsc = win->remote.msc;
Chia-I Wu1db76e02014-09-15 14:21:14 +0800604
605 return XGL_SUCCESS;
606}
607
Chia-I Wu96177272015-01-03 15:27:41 +0800608ICD_EXPORT XGL_RESULT XGLAPI xglWsiX11CreatePresentableImage(
Chia-I Wu1db76e02014-09-15 14:21:14 +0800609 XGL_DEVICE device,
610 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO* pCreateInfo,
611 XGL_IMAGE* pImage,
612 XGL_GPU_MEMORY* pMem)
613{
614 struct intel_dev *dev = intel_dev(device);
615 struct intel_wsi_x11 *x11 = dev->gpu->x11;
616 struct intel_img *img;
617 XGL_RESULT ret;
618
619 ret = wsi_x11_img_create(x11, dev, pCreateInfo, &img);
620 if (ret == XGL_SUCCESS) {
621 *pImage = (XGL_IMAGE) img;
622 *pMem = (XGL_GPU_MEMORY) img->obj.mem;
623 }
624
625 return ret;
626}
627
Chia-I Wu96177272015-01-03 15:27:41 +0800628ICD_EXPORT XGL_RESULT XGLAPI xglWsiX11QueuePresent(
Chia-I Wu1db76e02014-09-15 14:21:14 +0800629 XGL_QUEUE queue_,
630 const XGL_WSI_X11_PRESENT_INFO* pPresentInfo,
631 XGL_FENCE fence_)
632{
633 struct intel_queue *queue = intel_queue(queue_);
634 struct intel_fence *fence = intel_fence(fence_);
635 struct intel_wsi_x11 *x11 = queue->dev->gpu->x11;
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800636 struct intel_wsi_x11_window *win;
Chia-I Wu1db76e02014-09-15 14:21:14 +0800637 XGL_RESULT ret;
638
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800639 win = wsi_x11_lookup_window(queue->dev->gpu,
640 x11, pPresentInfo->destWindow);
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800641 if (!win)
642 return XGL_ERROR_UNKNOWN;
643
644 ret = wsi_x11_present_pixmap(x11, win, pPresentInfo);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800645 if (ret != XGL_SUCCESS)
646 return ret;
647
Chia-I Wubda4f622015-02-25 15:06:15 -0700648 if (fence) {
649 struct intel_img *img = intel_img(pPresentInfo->srcImage);
650
651 intel_fence_set_x11(fence, x11, win, win->local.serial,
652 img->obj.mem->bo);
653 }
Chia-I Wu1db76e02014-09-15 14:21:14 +0800654
655 return XGL_SUCCESS;
656}