1 /**************************************************************************
3 * Copyright 2009 Younes Manton.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 /* directly referenced from target Makefile, because of X dependencies */
30 #include <sys/types.h>
34 #include <X11/Xlib-xcb.h>
35 #include <X11/extensions/dri2tokens.h>
42 #include "pipe/p_screen.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_state.h"
45 #include "pipe-loader/pipe_loader.h"
46 #include "state_tracker/drm_driver.h"
48 #include "util/u_memory.h"
49 #include "util/crc32.h"
50 #include "util/u_hash_table.h"
51 #include "util/u_inlines.h"
53 #include "vl/vl_compositor.h"
54 #include "vl/vl_winsys.h"
58 struct vl_screen base
;
59 xcb_connection_t
*conn
;
60 xcb_drawable_t drawable
;
62 unsigned width
, height
;
65 uint32_t buffer_names
[2];
66 struct u_rect dirty_areas
[2];
69 xcb_dri2_swap_buffers_cookie_t swap_cookie
;
70 xcb_dri2_wait_sbc_cookie_t wait_cookie
;
71 xcb_dri2_get_buffers_cookie_t buffers_cookie
;
73 int64_t last_ust
, ns_frame
, last_msc
, next_msc
;
76 static const unsigned attachments
[1] = { XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT
};
78 static void vl_dri2_screen_destroy(struct vl_screen
*vscreen
);
81 vl_dri2_handle_stamps(struct vl_dri_screen
*scrn
,
82 uint32_t ust_hi
, uint32_t ust_lo
,
83 uint32_t msc_hi
, uint32_t msc_lo
)
85 int64_t ust
= ((((uint64_t)ust_hi
) << 32) | ust_lo
) * 1000;
86 int64_t msc
= (((uint64_t)msc_hi
) << 32) | msc_lo
;
88 if (scrn
->last_ust
&& (ust
> scrn
->last_ust
) &&
89 scrn
->last_msc
&& (msc
> scrn
->last_msc
))
90 scrn
->ns_frame
= (ust
- scrn
->last_ust
) / (msc
- scrn
->last_msc
);
96 static xcb_dri2_get_buffers_reply_t
*
97 vl_dri2_get_flush_reply(struct vl_dri_screen
*scrn
)
99 xcb_dri2_wait_sbc_reply_t
*wait_sbc_reply
;
106 scrn
->flushed
= false;
108 free(xcb_dri2_swap_buffers_reply(scrn
->conn
, scrn
->swap_cookie
, NULL
));
110 wait_sbc_reply
= xcb_dri2_wait_sbc_reply(scrn
->conn
, scrn
->wait_cookie
, NULL
);
113 vl_dri2_handle_stamps(scrn
, wait_sbc_reply
->ust_hi
, wait_sbc_reply
->ust_lo
,
114 wait_sbc_reply
->msc_hi
, wait_sbc_reply
->msc_lo
);
115 free(wait_sbc_reply
);
117 return xcb_dri2_get_buffers_reply(scrn
->conn
, scrn
->buffers_cookie
, NULL
);
121 vl_dri2_flush_frontbuffer(struct pipe_screen
*screen
,
122 struct pipe_resource
*resource
,
123 unsigned level
, unsigned layer
,
124 void *context_private
, struct pipe_box
*sub_box
)
126 struct vl_dri_screen
*scrn
= (struct vl_dri_screen
*)context_private
;
127 uint32_t msc_hi
, msc_lo
;
131 assert(context_private
);
133 free(vl_dri2_get_flush_reply(scrn
));
135 msc_hi
= scrn
->next_msc
>> 32;
136 msc_lo
= scrn
->next_msc
& 0xFFFFFFFF;
138 scrn
->swap_cookie
= xcb_dri2_swap_buffers_unchecked(scrn
->conn
, scrn
->drawable
,
139 msc_hi
, msc_lo
, 0, 0, 0, 0);
140 scrn
->wait_cookie
= xcb_dri2_wait_sbc_unchecked(scrn
->conn
, scrn
->drawable
, 0, 0);
141 scrn
->buffers_cookie
= xcb_dri2_get_buffers_unchecked(scrn
->conn
, scrn
->drawable
,
144 scrn
->flushed
= true;
145 scrn
->current_buffer
= !scrn
->current_buffer
;
149 vl_dri2_destroy_drawable(struct vl_dri_screen
*scrn
)
151 xcb_void_cookie_t destroy_cookie
;
152 if (scrn
->drawable
) {
153 free(vl_dri2_get_flush_reply(scrn
));
154 destroy_cookie
= xcb_dri2_destroy_drawable_checked(scrn
->conn
, scrn
->drawable
);
155 /* ignore any error here, since the drawable can be destroyed long ago */
156 free(xcb_request_check(scrn
->conn
, destroy_cookie
));
161 vl_dri2_set_drawable(struct vl_dri_screen
*scrn
, Drawable drawable
)
166 if (scrn
->drawable
== drawable
)
169 vl_dri2_destroy_drawable(scrn
);
171 xcb_dri2_create_drawable(scrn
->conn
, drawable
);
172 scrn
->current_buffer
= false;
173 vl_compositor_reset_dirty_area(&scrn
->dirty_areas
[0]);
174 vl_compositor_reset_dirty_area(&scrn
->dirty_areas
[1]);
175 scrn
->drawable
= drawable
;
178 static struct pipe_resource
*
179 vl_dri2_screen_texture_from_drawable(struct vl_screen
*vscreen
, void *drawable
)
181 struct vl_dri_screen
*scrn
= (struct vl_dri_screen
*)vscreen
;
183 struct winsys_handle dri2_handle
;
184 struct pipe_resource
template, *tex
;
186 xcb_dri2_get_buffers_reply_t
*reply
;
187 xcb_dri2_dri2_buffer_t
*buffers
, *back_left
;
193 vl_dri2_set_drawable(scrn
, (Drawable
)drawable
);
194 reply
= vl_dri2_get_flush_reply(scrn
);
196 xcb_dri2_get_buffers_cookie_t cookie
;
197 cookie
= xcb_dri2_get_buffers_unchecked(scrn
->conn
, (Drawable
)drawable
,
199 reply
= xcb_dri2_get_buffers_reply(scrn
->conn
, cookie
, NULL
);
204 buffers
= xcb_dri2_get_buffers_buffers(reply
);
210 for (i
= 0; i
< reply
->count
; ++i
) {
211 if (buffers
[i
].attachment
== XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT
) {
212 back_left
= &buffers
[i
];
217 if (i
== reply
->count
) {
222 if (reply
->width
!= scrn
->width
|| reply
->height
!= scrn
->height
) {
223 vl_compositor_reset_dirty_area(&scrn
->dirty_areas
[0]);
224 vl_compositor_reset_dirty_area(&scrn
->dirty_areas
[1]);
225 scrn
->width
= reply
->width
;
226 scrn
->height
= reply
->height
;
228 } else if (back_left
->name
!= scrn
->buffer_names
[scrn
->current_buffer
]) {
229 vl_compositor_reset_dirty_area(&scrn
->dirty_areas
[scrn
->current_buffer
]);
230 scrn
->buffer_names
[scrn
->current_buffer
] = back_left
->name
;
233 memset(&dri2_handle
, 0, sizeof(dri2_handle
));
234 dri2_handle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
235 dri2_handle
.handle
= back_left
->name
;
236 dri2_handle
.stride
= back_left
->pitch
;
238 memset(&template, 0, sizeof(template));
239 template.target
= PIPE_TEXTURE_2D
;
240 template.format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
241 template.last_level
= 0;
242 template.width0
= reply
->width
;
243 template.height0
= reply
->height
;
245 template.array_size
= 1;
246 template.usage
= PIPE_USAGE_DEFAULT
;
247 template.bind
= PIPE_BIND_RENDER_TARGET
;
250 tex
= scrn
->base
.pscreen
->resource_from_handle(scrn
->base
.pscreen
, &template,
252 PIPE_HANDLE_USAGE_READ_WRITE
);
258 static struct u_rect
*
259 vl_dri2_screen_get_dirty_area(struct vl_screen
*vscreen
)
261 struct vl_dri_screen
*scrn
= (struct vl_dri_screen
*)vscreen
;
263 return &scrn
->dirty_areas
[scrn
->current_buffer
];
267 vl_dri2_screen_get_timestamp(struct vl_screen
*vscreen
, void *drawable
)
269 struct vl_dri_screen
*scrn
= (struct vl_dri_screen
*)vscreen
;
270 xcb_dri2_get_msc_cookie_t cookie
;
271 xcb_dri2_get_msc_reply_t
*reply
;
275 vl_dri2_set_drawable(scrn
, (Drawable
)drawable
);
276 if (!scrn
->last_ust
) {
277 cookie
= xcb_dri2_get_msc_unchecked(scrn
->conn
, (Drawable
)drawable
);
278 reply
= xcb_dri2_get_msc_reply(scrn
->conn
, cookie
, NULL
);
281 vl_dri2_handle_stamps(scrn
, reply
->ust_hi
, reply
->ust_lo
,
282 reply
->msc_hi
, reply
->msc_lo
);
286 return scrn
->last_ust
;
290 vl_dri2_screen_set_next_timestamp(struct vl_screen
*vscreen
, uint64_t stamp
)
292 struct vl_dri_screen
*scrn
= (struct vl_dri_screen
*)vscreen
;
294 if (stamp
&& scrn
->last_ust
&& scrn
->ns_frame
&& scrn
->last_msc
)
295 scrn
->next_msc
= ((int64_t)stamp
- scrn
->last_ust
+ scrn
->ns_frame
/2) /
296 scrn
->ns_frame
+ scrn
->last_msc
;
302 vl_dri2_screen_get_private(struct vl_screen
*vscreen
)
307 static xcb_screen_t
*
308 get_xcb_screen(xcb_screen_iterator_t iter
, int screen
)
310 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
318 vl_dri2_screen_create(Display
*display
, int screen
)
320 struct vl_dri_screen
*scrn
;
321 const xcb_query_extension_reply_t
*extension
;
322 xcb_dri2_query_version_cookie_t dri2_query_cookie
;
323 xcb_dri2_query_version_reply_t
*dri2_query
= NULL
;
324 xcb_dri2_connect_cookie_t connect_cookie
;
325 xcb_dri2_connect_reply_t
*connect
= NULL
;
326 xcb_dri2_authenticate_cookie_t authenticate_cookie
;
327 xcb_dri2_authenticate_reply_t
*authenticate
= NULL
;
328 xcb_screen_iterator_t s
;
329 xcb_screen_t
*xcb_screen
;
330 xcb_generic_error_t
*error
= NULL
;
332 int fd
, device_name_length
;
339 scrn
= CALLOC_STRUCT(vl_dri_screen
);
343 scrn
->conn
= XGetXCBConnection(display
);
347 xcb_prefetch_extension_data(scrn
->conn
, &xcb_dri2_id
);
349 extension
= xcb_get_extension_data(scrn
->conn
, &xcb_dri2_id
);
350 if (!(extension
&& extension
->present
))
353 dri2_query_cookie
= xcb_dri2_query_version (scrn
->conn
,
354 XCB_DRI2_MAJOR_VERSION
,
355 XCB_DRI2_MINOR_VERSION
);
356 dri2_query
= xcb_dri2_query_version_reply (scrn
->conn
, dri2_query_cookie
, &error
);
357 if (dri2_query
== NULL
|| error
!= NULL
|| dri2_query
->minor_version
< 2)
360 s
= xcb_setup_roots_iterator(xcb_get_setup(scrn
->conn
));
361 xcb_screen
= get_xcb_screen(s
, screen
);
365 driverType
= XCB_DRI2_DRIVER_TYPE_DRI
;
366 #ifdef DRI2DriverPrimeShift
368 char *prime
= getenv("DRI_PRIME");
372 primeid
= strtoul(prime
, NULL
, 0);
375 ((primeid
& DRI2DriverPrimeMask
) << DRI2DriverPrimeShift
);
380 connect_cookie
= xcb_dri2_connect_unchecked(scrn
->conn
,
383 connect
= xcb_dri2_connect_reply(scrn
->conn
, connect_cookie
, NULL
);
384 if (connect
== NULL
||
385 connect
->driver_name_length
+ connect
->device_name_length
== 0)
388 device_name_length
= xcb_dri2_connect_device_name_length(connect
);
389 device_name
= CALLOC(1, device_name_length
+ 1);
392 memcpy(device_name
, xcb_dri2_connect_device_name(connect
), device_name_length
);
393 fd
= loader_open_device(device_name
);
399 if (drmGetMagic(fd
, &magic
))
402 authenticate_cookie
= xcb_dri2_authenticate_unchecked(scrn
->conn
,
405 authenticate
= xcb_dri2_authenticate_reply(scrn
->conn
, authenticate_cookie
, NULL
);
407 if (authenticate
== NULL
|| !authenticate
->authenticated
)
408 goto free_authenticate
;
410 if (pipe_loader_drm_probe_fd(&scrn
->base
.dev
, fd
))
411 scrn
->base
.pscreen
= pipe_loader_create_screen(scrn
->base
.dev
);
413 if (!scrn
->base
.pscreen
)
416 scrn
->base
.destroy
= vl_dri2_screen_destroy
;
417 scrn
->base
.texture_from_drawable
= vl_dri2_screen_texture_from_drawable
;
418 scrn
->base
.get_dirty_area
= vl_dri2_screen_get_dirty_area
;
419 scrn
->base
.get_timestamp
= vl_dri2_screen_get_timestamp
;
420 scrn
->base
.set_next_timestamp
= vl_dri2_screen_set_next_timestamp
;
421 scrn
->base
.get_private
= vl_dri2_screen_get_private
;
422 scrn
->base
.pscreen
->flush_frontbuffer
= vl_dri2_flush_frontbuffer
;
423 vl_compositor_reset_dirty_area(&scrn
->dirty_areas
[0]);
424 vl_compositor_reset_dirty_area(&scrn
->dirty_areas
[1]);
434 if (scrn
->base
.dev
) {
435 pipe_loader_release(&scrn
->base
.dev
, 1);
455 vl_dri2_screen_destroy(struct vl_screen
*vscreen
)
457 struct vl_dri_screen
*scrn
= (struct vl_dri_screen
*)vscreen
;
462 free(xcb_dri2_swap_buffers_reply(scrn
->conn
, scrn
->swap_cookie
, NULL
));
463 free(xcb_dri2_wait_sbc_reply(scrn
->conn
, scrn
->wait_cookie
, NULL
));
464 free(xcb_dri2_get_buffers_reply(scrn
->conn
, scrn
->buffers_cookie
, NULL
));
467 vl_dri2_destroy_drawable(scrn
);
468 scrn
->base
.pscreen
->destroy(scrn
->base
.pscreen
);
469 pipe_loader_release(&scrn
->base
.dev
, 1);