blob: fa1f125df67e1341a5c555747593a724c50c56ab [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 */
Chia-I Wu3d0f59c2015-03-07 06:00:46 +0800234 if (intel_bo_set_tiling(mem->bo, img->layout.tiling,
235 img->layout.bo_stride))
236 return XGL_ERROR_UNKNOWN;
Chia-I Wu1db76e02014-09-15 14:21:14 +0800237 export.type = INTEL_WINSYS_HANDLE_FD;
238 if (intel_winsys_export_handle(dev->winsys, mem->bo, img->layout.tiling,
239 img->layout.bo_stride, img->layout.bo_height, &export))
240 return XGL_ERROR_UNKNOWN;
241
242 pixmap = xcb_generate_id(x11->c);
243
244 /* create a pixmap from the prime fd */
245 xcb_dri3_pixmap_from_buffer(x11->c, pixmap,
246 x11->root, img->total_size,
247 img->layout.width0, img->layout.height0,
248 img->layout.bo_stride, x11->root_depth,
249 img->layout.block_size * 8, export.handle);
250
251 img->x11_prime_fd = export.handle;
252 img->x11_pixmap = pixmap;
253
254 return XGL_SUCCESS;
255}
256
257/**
258 * Send a PresentSelectInput to select interested events.
259 */
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800260static XGL_RESULT wsi_x11_present_select_input(struct intel_wsi_x11 *x11,
261 struct intel_wsi_x11_window *win)
Chia-I Wu1db76e02014-09-15 14:21:14 +0800262{
263 xcb_void_cookie_t cookie;
264 xcb_generic_error_t *error;
265
266 /* create the event queue */
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800267 win->present_special_event_id = xcb_generate_id(x11->c);
268 win->present_special_event = xcb_register_for_special_xge(x11->c,
269 &xcb_present_id, win->present_special_event_id, NULL);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800270
271 cookie = xcb_present_select_input_checked(x11->c,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800272 win->present_special_event_id, win->window_id,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800273 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY);
274
275 error = xcb_request_check(x11->c, cookie);
276 if (error) {
277 free(error);
278 return XGL_ERROR_UNKNOWN;
279 }
280
281 return XGL_SUCCESS;
282}
283
284/**
285 * Send a PresentPixmap.
286 */
287static XGL_RESULT wsi_x11_present_pixmap(struct intel_wsi_x11 *x11,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800288 struct intel_wsi_x11_window *win,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800289 const XGL_WSI_X11_PRESENT_INFO *info)
290{
291 struct intel_img *img = intel_img(info->srcImage);
292 uint32_t options = XCB_PRESENT_OPTION_NONE;
293 xcb_void_cookie_t cookie;
294 xcb_generic_error_t *err;
295
296 if (info->async)
297 options |= XCB_PRESENT_OPTION_ASYNC;
298 if (!info->flip)
299 options |= XCB_PRESENT_OPTION_COPY;
300
301 cookie = xcb_present_pixmap(x11->c,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800302 win->window_id,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800303 img->x11_pixmap,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800304 ++win->local.serial,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800305 0, /* valid-area */
306 0, /* update-area */
307 0, /* x-off */
308 0, /* y-off */
309 info->crtc,
310 0, /* wait-fence */
311 0, /* idle-fence */
312 options,
313 info->target_msc,
314 info->divisor,
315 info->remainder,
316 0, NULL);
317
318 err = xcb_request_check(x11->c, cookie);
319 if (err) {
320 free(err);
321 return XGL_ERROR_UNKNOWN;
322 }
323
324 return XGL_SUCCESS;
325}
326
327/**
328 * Send a PresentNotifyMSC for the current MSC.
329 */
330static void wsi_x11_present_notify_msc(struct intel_wsi_x11 *x11,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800331 struct intel_wsi_x11_window *win)
Chia-I Wu1db76e02014-09-15 14:21:14 +0800332{
333 /* cannot specify CRTC? */
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800334 xcb_present_notify_msc(x11->c, win->window_id, ++win->local.serial,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800335 0, 0, 0);
336
337 xcb_flush(x11->c);
338}
339
340/**
341 * Handle a Present event.
342 */
343static void wsi_x11_present_event(struct intel_wsi_x11 *x11,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800344 struct intel_wsi_x11_window *win,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800345 const xcb_present_generic_event_t *ev)
346{
347 union {
348 const xcb_present_generic_event_t *ev;
349 const xcb_present_complete_notify_event_t *complete;
350 } u = { .ev = ev };
351
352 switch (u.ev->evtype) {
353 case XCB_PRESENT_COMPLETE_NOTIFY:
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800354 win->remote.serial = u.complete->serial;
355 win->remote.msc = u.complete->msc;
Chia-I Wu1db76e02014-09-15 14:21:14 +0800356 break;
357 default:
358 break;
359 }
360}
361
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800362static struct intel_wsi_x11_window *wsi_x11_create_window(const struct intel_gpu *gpu,
363 struct intel_wsi_x11 *x11,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800364 xcb_window_t win_id)
365{
366 struct intel_wsi_x11_window *win;
367
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800368 win = intel_alloc(gpu, sizeof(*win), 0, XGL_SYSTEM_ALLOC_INTERNAL);
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800369 if (!win)
370 return NULL;
371
372 memset(win, 0, sizeof(*win));
373
374 win->window_id = win_id;
375
376 if (wsi_x11_present_select_input(x11, win) != XGL_SUCCESS) {
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800377 intel_free(gpu, win);
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800378 return NULL;
379 }
380
381 return win;
382}
383
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800384static void wsi_x11_destroy_window(const struct intel_gpu *gpu,
385 struct intel_wsi_x11 *x11,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800386 struct intel_wsi_x11_window *win)
387{
388 if (win->present_special_event)
389 xcb_unregister_for_special_event(x11->c, win->present_special_event);
390
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800391 intel_free(gpu, win);
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800392}
393
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800394static struct intel_wsi_x11_window *wsi_x11_lookup_window(const struct intel_gpu *gpu,
395 struct intel_wsi_x11 *x11,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800396 xcb_window_t win_id)
397{
398 struct intel_wsi_x11_window *win = x11->windows;
399
400 while (win) {
401 if (win->window_id == win_id)
402 break;
403 win = win->next;
404 }
405
406 /* lookup failed */
407 if (!win) {
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800408 win = wsi_x11_create_window(gpu, x11, win_id);
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800409 if (win) {
410 win->next = x11->windows;
411 x11->windows = win;
412 }
413 }
414
415 return win;
416}
417
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800418void intel_wsi_x11_destroy(const struct intel_gpu *gpu,
419 struct intel_wsi_x11 *x11)
Chia-I Wu1db76e02014-09-15 14:21:14 +0800420{
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800421 struct intel_wsi_x11_window *win = x11->windows;
422
423 while (win) {
424 struct intel_wsi_x11_window *next = win->next;
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800425 wsi_x11_destroy_window(gpu, x11, win);
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800426 win = next;
427 }
Chia-I Wu1db76e02014-09-15 14:21:14 +0800428
429 if (x11->fd >= 0)
430 close(x11->fd);
431
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800432 intel_free(gpu, x11);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800433}
434
435XGL_RESULT intel_wsi_x11_wait(struct intel_wsi_x11 *x11,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800436 struct intel_wsi_x11_window *win,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800437 uint32_t serial, bool wait)
438{
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800439 while (win->remote.serial < serial) {
Chia-I Wu1db76e02014-09-15 14:21:14 +0800440 xcb_present_generic_event_t *ev;
441
442 if (wait) {
443 ev = (xcb_present_generic_event_t *)
444 xcb_wait_for_special_event(x11->c,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800445 win->present_special_event);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800446 if (!ev)
447 return XGL_ERROR_UNKNOWN;
448 } else {
449 ev = (xcb_present_generic_event_t *)
450 xcb_poll_for_special_event(x11->c,
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800451 win->present_special_event);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800452 if (!ev)
453 return XGL_NOT_READY;
454 }
455
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800456 wsi_x11_present_event(x11, win, ev);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800457
458 free(ev);
459 }
460
461 return XGL_SUCCESS;
462}
463
Chia-I Wuc505a6f2014-11-13 00:04:01 +0800464static bool wsi_x11_is_format_presentable(struct intel_wsi_x11 *x11,
465 struct intel_dev *dev,
466 XGL_FORMAT format)
467{
468 /* this is what DDX expects */
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700469 switch (format) {
470 case XGL_FMT_B5G6R5_UNORM:
471 case XGL_FMT_B8G8R8A8_UNORM:
472 case XGL_FMT_B8G8R8A8_SRGB:
473 return true;
Chia-I Wuc505a6f2014-11-13 00:04:01 +0800474 default:
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700475 return false;
Chia-I Wuc505a6f2014-11-13 00:04:01 +0800476 }
Chia-I Wuc505a6f2014-11-13 00:04:01 +0800477}
478
Chia-I Wu1db76e02014-09-15 14:21:14 +0800479/**
480 * Create a presentable image.
481 */
482static XGL_RESULT wsi_x11_img_create(struct intel_wsi_x11 *x11,
483 struct intel_dev *dev,
484 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO *info,
485 struct intel_img **img_ret)
486{
487 XGL_IMAGE_CREATE_INFO img_info;
488 XGL_MEMORY_ALLOC_INFO mem_info;
489 struct intel_img *img;
490 struct intel_mem *mem;
491 XGL_RESULT ret;
492
Chia-I Wuc505a6f2014-11-13 00:04:01 +0800493 if (!wsi_x11_is_format_presentable(x11, dev, info->format)) {
494 intel_dev_log(dev, XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0,
495 XGL_NULL_HANDLE, 0, 0, "invalid presentable image format");
496 return XGL_ERROR_INVALID_VALUE;
497 }
498
Chia-I Wu1db76e02014-09-15 14:21:14 +0800499 /* create image */
500 memset(&img_info, 0, sizeof(img_info));
501 img_info.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
502 img_info.imageType = XGL_IMAGE_2D;
503 img_info.format = info->format;
504 img_info.extent.width = info->extent.width;
505 img_info.extent.height = info->extent.height;
506 img_info.extent.depth = 1;
507 img_info.mipLevels = 1;
508 img_info.arraySize = 1;
509 img_info.samples = 1;
510 img_info.tiling = XGL_OPTIMAL_TILING;
511 img_info.usage = info->usage;
512 img_info.flags = 0;
513
514 ret = intel_img_create(dev, &img_info, true, &img);
515 if (ret != XGL_SUCCESS)
516 return ret;
517
518 /* allocate memory */
519 memset(&mem_info, 0, sizeof(mem_info));
520 mem_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
521 mem_info.allocationSize = img->total_size;
Jon Ashburn542cd092015-01-20 13:55:32 -0700522 mem_info.memProps = 0;
Jon Ashburn32769172015-01-20 15:06:59 -0700523 mem_info.memType = XGL_MEMORY_TYPE_IMAGE;
Chia-I Wu1db76e02014-09-15 14:21:14 +0800524 mem_info.memPriority = XGL_MEMORY_PRIORITY_HIGH;
525
526 ret = intel_mem_alloc(dev, &mem_info, &mem);
527 if (ret != XGL_SUCCESS) {
528 intel_img_destroy(img);
529 return ret;
530 }
531
532 ret = wsi_x11_dri3_pixmap_from_buffer(x11, dev, img, mem);
533 if (ret != XGL_SUCCESS) {
534 intel_mem_free(mem);
535 intel_img_destroy(img);
536 return ret;
537 }
538
539 intel_obj_bind_mem(&img->obj, mem, 0);
540
541 *img_ret = img;
542
543 return XGL_SUCCESS;
544}
545
Chia-I Wu96177272015-01-03 15:27:41 +0800546ICD_EXPORT XGL_RESULT XGLAPI xglWsiX11AssociateConnection(
Chia-I Wu1db76e02014-09-15 14:21:14 +0800547 XGL_PHYSICAL_GPU gpu_,
548 const XGL_WSI_X11_CONNECTION_INFO* pConnectionInfo)
549{
550 struct intel_gpu *gpu = intel_gpu(gpu_);
551 struct intel_wsi_x11 *x11;
552 XGL_RESULT ret;
553
554 if (gpu->x11)
555 return XGL_SUCCESS;
556
Chia-I Wud8965932014-10-13 13:32:37 +0800557 if (gpu->winsys)
Chia-I Wu1db76e02014-09-15 14:21:14 +0800558 return XGL_ERROR_DEVICE_ALREADY_CREATED;
559
560 if (!wsi_x11_has_dri3_and_present(pConnectionInfo->pConnection))
561 return XGL_ERROR_UNKNOWN;
562
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800563 x11 = wsi_x11_create(gpu, pConnectionInfo->pConnection,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800564 pConnectionInfo->root, pConnectionInfo->provider);
565 if (!x11)
566 return XGL_ERROR_UNKNOWN;
567
568 ret = wsi_x11_dri3_open(x11);
569 if (ret != XGL_SUCCESS) {
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800570 intel_wsi_x11_destroy(gpu, x11);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800571 return ret;
572 }
573
574 if (!wsi_x11_uses_gpu(x11, gpu)) {
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800575 intel_wsi_x11_destroy(gpu, x11);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800576 return XGL_ERROR_UNKNOWN;
577 }
578
Chia-I Wu1db76e02014-09-15 14:21:14 +0800579 intel_gpu_associate_x11(gpu, x11, x11->fd);
580
581 return XGL_SUCCESS;
582}
583
Chia-I Wu96177272015-01-03 15:27:41 +0800584ICD_EXPORT XGL_RESULT XGLAPI xglWsiX11GetMSC(
Chia-I Wu1db76e02014-09-15 14:21:14 +0800585 XGL_DEVICE device,
Chia-I Wu6204f342014-11-07 13:33:45 +0800586 xcb_window_t window,
Chia-I Wu1db76e02014-09-15 14:21:14 +0800587 xcb_randr_crtc_t crtc,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600588 uint64_t * pMsc)
Chia-I Wu1db76e02014-09-15 14:21:14 +0800589{
590 struct intel_dev *dev = intel_dev(device);
591 struct intel_wsi_x11 *x11 = dev->gpu->x11;
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800592 struct intel_wsi_x11_window *win;
Chia-I Wu1db76e02014-09-15 14:21:14 +0800593 XGL_RESULT ret;
594
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800595 win = wsi_x11_lookup_window(dev->gpu, x11, window);
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800596 if (!win)
597 return XGL_ERROR_UNKNOWN;
598
599 wsi_x11_present_notify_msc(x11, win);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800600
601 /* wait for the event */
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800602 ret = intel_wsi_x11_wait(x11, win, win->local.serial, -1);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800603 if (ret != XGL_SUCCESS)
604 return ret;
605
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800606 *pMsc = win->remote.msc;
Chia-I Wu1db76e02014-09-15 14:21:14 +0800607
608 return XGL_SUCCESS;
609}
610
Chia-I Wu96177272015-01-03 15:27:41 +0800611ICD_EXPORT XGL_RESULT XGLAPI xglWsiX11CreatePresentableImage(
Chia-I Wu1db76e02014-09-15 14:21:14 +0800612 XGL_DEVICE device,
613 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO* pCreateInfo,
614 XGL_IMAGE* pImage,
615 XGL_GPU_MEMORY* pMem)
616{
617 struct intel_dev *dev = intel_dev(device);
618 struct intel_wsi_x11 *x11 = dev->gpu->x11;
619 struct intel_img *img;
620 XGL_RESULT ret;
621
622 ret = wsi_x11_img_create(x11, dev, pCreateInfo, &img);
623 if (ret == XGL_SUCCESS) {
624 *pImage = (XGL_IMAGE) img;
625 *pMem = (XGL_GPU_MEMORY) img->obj.mem;
626 }
627
628 return ret;
629}
630
Chia-I Wu96177272015-01-03 15:27:41 +0800631ICD_EXPORT XGL_RESULT XGLAPI xglWsiX11QueuePresent(
Chia-I Wu1db76e02014-09-15 14:21:14 +0800632 XGL_QUEUE queue_,
633 const XGL_WSI_X11_PRESENT_INFO* pPresentInfo,
634 XGL_FENCE fence_)
635{
636 struct intel_queue *queue = intel_queue(queue_);
637 struct intel_fence *fence = intel_fence(fence_);
638 struct intel_wsi_x11 *x11 = queue->dev->gpu->x11;
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800639 struct intel_wsi_x11_window *win;
Chia-I Wu1db76e02014-09-15 14:21:14 +0800640 XGL_RESULT ret;
641
Chia-I Wuf13ed3c2015-02-22 14:09:00 +0800642 win = wsi_x11_lookup_window(queue->dev->gpu,
643 x11, pPresentInfo->destWindow);
Chia-I Wu7fbe2ab2014-11-07 13:26:45 +0800644 if (!win)
645 return XGL_ERROR_UNKNOWN;
646
647 ret = wsi_x11_present_pixmap(x11, win, pPresentInfo);
Chia-I Wu1db76e02014-09-15 14:21:14 +0800648 if (ret != XGL_SUCCESS)
649 return ret;
650
Chia-I Wubda4f622015-02-25 15:06:15 -0700651 if (fence) {
652 struct intel_img *img = intel_img(pPresentInfo->srcImage);
653
654 intel_fence_set_x11(fence, x11, win, win->local.serial,
655 img->obj.mem->bo);
656 }
Chia-I Wu1db76e02014-09-15 14:21:14 +0800657
658 return XGL_SUCCESS;
659}