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>
40 #include "pipe/p_screen.h"
41 #include "pipe/p_context.h"
42 #include "pipe/p_state.h"
43 #include "state_tracker/drm_driver.h"
45 #include "util/u_memory.h"
46 #include "util/u_hash.h"
47 #include "util/u_hash_table.h"
48 #include "util/u_inlines.h"
50 #include "vl/vl_compositor.h"
51 #include "vl/vl_winsys.h"
55 struct vl_screen base
;
56 xcb_connection_t
*conn
;
57 xcb_drawable_t drawable
;
59 unsigned width
, height
;
62 uint32_t buffer_names
[2];
63 struct u_rect dirty_areas
[2];
66 xcb_dri2_swap_buffers_cookie_t swap_cookie
;
67 xcb_dri2_wait_sbc_cookie_t wait_cookie
;
68 xcb_dri2_get_buffers_cookie_t buffers_cookie
;
70 int64_t last_ust
, ns_frame
, last_msc
, next_msc
;
73 static const unsigned int attachments
[1] = { XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT
};
76 vl_dri2_handle_stamps(struct vl_dri_screen
* scrn
,
77 uint32_t ust_hi
, uint32_t ust_lo
,
78 uint32_t msc_hi
, uint32_t msc_lo
)
80 int64_t ust
= ((((uint64_t)ust_hi
) << 32) | ust_lo
) * 1000;
81 int64_t msc
= (((uint64_t)msc_hi
) << 32) | msc_lo
;
83 if (scrn
->last_ust
&& scrn
->last_msc
&& (ust
> scrn
->last_ust
) && (msc
> scrn
->last_msc
))
84 scrn
->ns_frame
= (ust
- scrn
->last_ust
) / (msc
- scrn
->last_msc
);
90 static xcb_dri2_get_buffers_reply_t
*
91 vl_dri2_get_flush_reply(struct vl_dri_screen
*scrn
)
93 xcb_dri2_wait_sbc_reply_t
*wait_sbc_reply
;
100 scrn
->flushed
= false;
102 free(xcb_dri2_swap_buffers_reply(scrn
->conn
, scrn
->swap_cookie
, NULL
));
104 wait_sbc_reply
= xcb_dri2_wait_sbc_reply(scrn
->conn
, scrn
->wait_cookie
, NULL
);
107 vl_dri2_handle_stamps(scrn
, wait_sbc_reply
->ust_hi
, wait_sbc_reply
->ust_lo
,
108 wait_sbc_reply
->msc_hi
, wait_sbc_reply
->msc_lo
);
109 free(wait_sbc_reply
);
111 return xcb_dri2_get_buffers_reply(scrn
->conn
, scrn
->buffers_cookie
, NULL
);
115 vl_dri2_flush_frontbuffer(struct pipe_screen
*screen
,
116 struct pipe_resource
*resource
,
117 unsigned level
, unsigned layer
,
118 void *context_private
, struct pipe_box
*sub_box
)
120 struct vl_dri_screen
*scrn
= (struct vl_dri_screen
*)context_private
;
121 uint32_t msc_hi
, msc_lo
;
125 assert(context_private
);
127 free(vl_dri2_get_flush_reply(scrn
));
129 msc_hi
= scrn
->next_msc
>> 32;
130 msc_lo
= scrn
->next_msc
& 0xFFFFFFFF;
132 scrn
->swap_cookie
= xcb_dri2_swap_buffers_unchecked(scrn
->conn
, scrn
->drawable
, msc_hi
, msc_lo
, 0, 0, 0, 0);
133 scrn
->wait_cookie
= xcb_dri2_wait_sbc_unchecked(scrn
->conn
, scrn
->drawable
, 0, 0);
134 scrn
->buffers_cookie
= xcb_dri2_get_buffers_unchecked(scrn
->conn
, scrn
->drawable
, 1, 1, attachments
);
136 scrn
->flushed
= true;
137 scrn
->current_buffer
= !scrn
->current_buffer
;
141 vl_dri2_destroy_drawable(struct vl_dri_screen
*scrn
)
143 xcb_void_cookie_t destroy_cookie
;
144 if (scrn
->drawable
) {
145 free(vl_dri2_get_flush_reply(scrn
));
146 destroy_cookie
= xcb_dri2_destroy_drawable_checked(scrn
->conn
, scrn
->drawable
);
147 /* ignore any error here, since the drawable can be destroyed long ago */
148 free(xcb_request_check(scrn
->conn
, destroy_cookie
));
153 vl_dri2_set_drawable(struct vl_dri_screen
*scrn
, Drawable drawable
)
158 if (scrn
->drawable
== drawable
)
161 vl_dri2_destroy_drawable(scrn
);
163 xcb_dri2_create_drawable(scrn
->conn
, drawable
);
164 scrn
->current_buffer
= false;
165 vl_compositor_reset_dirty_area(&scrn
->dirty_areas
[0]);
166 vl_compositor_reset_dirty_area(&scrn
->dirty_areas
[1]);
167 scrn
->drawable
= drawable
;
170 struct pipe_resource
*
171 vl_screen_texture_from_drawable(struct vl_screen
*vscreen
, Drawable drawable
)
173 struct vl_dri_screen
*scrn
= (struct vl_dri_screen
*)vscreen
;
175 struct winsys_handle dri2_handle
;
176 struct pipe_resource
template, *tex
;
178 xcb_dri2_get_buffers_reply_t
*reply
;
179 xcb_dri2_dri2_buffer_t
*buffers
, *back_left
;
185 vl_dri2_set_drawable(scrn
, drawable
);
186 reply
= vl_dri2_get_flush_reply(scrn
);
188 xcb_dri2_get_buffers_cookie_t cookie
;
189 cookie
= xcb_dri2_get_buffers_unchecked(scrn
->conn
, drawable
, 1, 1, attachments
);
190 reply
= xcb_dri2_get_buffers_reply(scrn
->conn
, cookie
, NULL
);
195 buffers
= xcb_dri2_get_buffers_buffers(reply
);
201 for (i
= 0; i
< reply
->count
; ++i
) {
202 if (buffers
[i
].attachment
== XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT
) {
203 back_left
= &buffers
[i
];
208 if (i
== reply
->count
) {
213 if (reply
->width
!= scrn
->width
|| reply
->height
!= scrn
->height
) {
214 vl_compositor_reset_dirty_area(&scrn
->dirty_areas
[0]);
215 vl_compositor_reset_dirty_area(&scrn
->dirty_areas
[1]);
216 scrn
->width
= reply
->width
;
217 scrn
->height
= reply
->height
;
219 } else if (back_left
->name
!= scrn
->buffer_names
[scrn
->current_buffer
]) {
220 vl_compositor_reset_dirty_area(&scrn
->dirty_areas
[scrn
->current_buffer
]);
221 scrn
->buffer_names
[scrn
->current_buffer
] = back_left
->name
;
224 memset(&dri2_handle
, 0, sizeof(dri2_handle
));
225 dri2_handle
.type
= DRM_API_HANDLE_TYPE_SHARED
;
226 dri2_handle
.handle
= back_left
->name
;
227 dri2_handle
.stride
= back_left
->pitch
;
229 memset(&template, 0, sizeof(template));
230 template.target
= PIPE_TEXTURE_2D
;
231 template.format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
232 template.last_level
= 0;
233 template.width0
= reply
->width
;
234 template.height0
= reply
->height
;
236 template.array_size
= 1;
237 template.usage
= PIPE_USAGE_STATIC
;
238 template.bind
= PIPE_BIND_RENDER_TARGET
;
241 tex
= scrn
->base
.pscreen
->resource_from_handle(scrn
->base
.pscreen
, &template, &dri2_handle
);
248 vl_screen_get_dirty_area(struct vl_screen
*vscreen
)
250 struct vl_dri_screen
*scrn
= (struct vl_dri_screen
*)vscreen
;
252 return &scrn
->dirty_areas
[scrn
->current_buffer
];
256 vl_screen_get_timestamp(struct vl_screen
*vscreen
, Drawable drawable
)
258 struct vl_dri_screen
*scrn
= (struct vl_dri_screen
*)vscreen
;
259 xcb_dri2_get_msc_cookie_t cookie
;
260 xcb_dri2_get_msc_reply_t
*reply
;
264 vl_dri2_set_drawable(scrn
, drawable
);
265 if (!scrn
->last_ust
) {
266 cookie
= xcb_dri2_get_msc_unchecked(scrn
->conn
, drawable
);
267 reply
= xcb_dri2_get_msc_reply(scrn
->conn
, cookie
, NULL
);
270 vl_dri2_handle_stamps(scrn
, reply
->ust_hi
, reply
->ust_lo
,
271 reply
->msc_hi
, reply
->msc_lo
);
275 return scrn
->last_ust
;
279 vl_screen_set_next_timestamp(struct vl_screen
*vscreen
, uint64_t stamp
)
281 struct vl_dri_screen
*scrn
= (struct vl_dri_screen
*)vscreen
;
283 if (stamp
&& scrn
->last_ust
&& scrn
->ns_frame
&& scrn
->last_msc
)
284 scrn
->next_msc
= ((int64_t)stamp
- scrn
->last_ust
+ scrn
->ns_frame
/2) / scrn
->ns_frame
+ scrn
->last_msc
;
290 vl_screen_get_private(struct vl_screen
*vscreen
)
296 vl_screen_create(Display
*display
, int screen
)
298 struct vl_dri_screen
*scrn
;
299 const xcb_query_extension_reply_t
*extension
;
300 xcb_dri2_query_version_cookie_t dri2_query_cookie
;
301 xcb_dri2_query_version_reply_t
*dri2_query
= NULL
;
302 xcb_dri2_connect_cookie_t connect_cookie
;
303 xcb_dri2_connect_reply_t
*connect
= NULL
;
304 xcb_dri2_authenticate_cookie_t authenticate_cookie
;
305 xcb_dri2_authenticate_reply_t
*authenticate
= NULL
;
306 xcb_screen_iterator_t s
;
307 xcb_generic_error_t
*error
= NULL
;
309 int fd
, device_name_length
;
310 unsigned int driverType
;
316 scrn
= CALLOC_STRUCT(vl_dri_screen
);
320 scrn
->conn
= XGetXCBConnection(display
);
324 xcb_prefetch_extension_data(scrn
->conn
, &xcb_dri2_id
);
326 extension
= xcb_get_extension_data(scrn
->conn
, &xcb_dri2_id
);
327 if (!(extension
&& extension
->present
))
330 dri2_query_cookie
= xcb_dri2_query_version (scrn
->conn
, XCB_DRI2_MAJOR_VERSION
, XCB_DRI2_MINOR_VERSION
);
331 dri2_query
= xcb_dri2_query_version_reply (scrn
->conn
, dri2_query_cookie
, &error
);
332 if (dri2_query
== NULL
|| error
!= NULL
|| dri2_query
->minor_version
< 2)
335 s
= xcb_setup_roots_iterator(xcb_get_setup(scrn
->conn
));
338 driverType
= XCB_DRI2_DRIVER_TYPE_DRI
;
339 #ifdef DRI2DriverPrimeShift
341 char *prime
= getenv("DRI_PRIME");
343 unsigned int primeid
;
345 primeid
= strtoul(prime
, NULL
, 0);
348 ((primeid
& DRI2DriverPrimeMask
) << DRI2DriverPrimeShift
);
353 connect_cookie
= xcb_dri2_connect_unchecked(scrn
->conn
, s
.data
->root
, driverType
);
354 connect
= xcb_dri2_connect_reply(scrn
->conn
, connect_cookie
, NULL
);
355 if (connect
== NULL
|| connect
->driver_name_length
+ connect
->device_name_length
== 0)
358 device_name_length
= xcb_dri2_connect_device_name_length(connect
);
359 device_name
= CALLOC(1, device_name_length
+ 1);
360 memcpy(device_name
, xcb_dri2_connect_device_name(connect
), device_name_length
);
361 fd
= open(device_name
, O_RDWR
);
367 if (drmGetMagic(fd
, &magic
))
370 authenticate_cookie
= xcb_dri2_authenticate_unchecked(scrn
->conn
, s
.data
->root
, magic
);
371 authenticate
= xcb_dri2_authenticate_reply(scrn
->conn
, authenticate_cookie
, NULL
);
373 if (authenticate
== NULL
|| !authenticate
->authenticated
)
376 scrn
->base
.pscreen
= driver_descriptor
.create_screen(fd
);
377 if (!scrn
->base
.pscreen
)
380 scrn
->base
.pscreen
->flush_frontbuffer
= vl_dri2_flush_frontbuffer
;
381 vl_compositor_reset_dirty_area(&scrn
->dirty_areas
[0]);
382 vl_compositor_reset_dirty_area(&scrn
->dirty_areas
[1]);
401 void vl_screen_destroy(struct vl_screen
*vscreen
)
403 struct vl_dri_screen
*scrn
= (struct vl_dri_screen
*)vscreen
;
408 free(xcb_dri2_swap_buffers_reply(scrn
->conn
, scrn
->swap_cookie
, NULL
));
409 free(xcb_dri2_wait_sbc_reply(scrn
->conn
, scrn
->wait_cookie
, NULL
));
410 free(xcb_dri2_get_buffers_reply(scrn
->conn
, scrn
->buffers_cookie
, NULL
));
413 vl_dri2_destroy_drawable(scrn
);
414 scrn
->base
.pscreen
->destroy(scrn
->base
.pscreen
);