1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2016 Advanced Micro Devices, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include <fcntl.h>
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include <X11/Xlib-xcb.h>
31bf215546Sopenharmony_ci#include <X11/xshmfence.h>
32bf215546Sopenharmony_ci#include <xcb/dri3.h>
33bf215546Sopenharmony_ci#include <xcb/present.h>
34bf215546Sopenharmony_ci#include <xcb/xfixes.h>
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci#include "loader.h"
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci#include "pipe/p_screen.h"
39bf215546Sopenharmony_ci#include "pipe/p_state.h"
40bf215546Sopenharmony_ci#include "pipe-loader/pipe_loader.h"
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci#include "util/u_memory.h"
43bf215546Sopenharmony_ci#include "util/u_inlines.h"
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci#include "vl/vl_compositor.h"
46bf215546Sopenharmony_ci#include "vl/vl_winsys.h"
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci#include "drm-uapi/drm_fourcc.h"
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci#define BACK_BUFFER_NUM 3
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_cistruct vl_dri3_buffer
53bf215546Sopenharmony_ci{
54bf215546Sopenharmony_ci   struct pipe_resource *texture;
55bf215546Sopenharmony_ci   struct pipe_resource *linear_texture;
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   uint32_t pixmap;
58bf215546Sopenharmony_ci   uint32_t region;
59bf215546Sopenharmony_ci   uint32_t sync_fence;
60bf215546Sopenharmony_ci   struct xshmfence *shm_fence;
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci   bool busy;
63bf215546Sopenharmony_ci   uint32_t width, height, pitch;
64bf215546Sopenharmony_ci};
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_cistruct vl_dri3_screen
67bf215546Sopenharmony_ci{
68bf215546Sopenharmony_ci   struct vl_screen base;
69bf215546Sopenharmony_ci   xcb_connection_t *conn;
70bf215546Sopenharmony_ci   xcb_drawable_t drawable;
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   uint32_t width, height, depth;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   xcb_present_event_t eid;
75bf215546Sopenharmony_ci   xcb_special_event_t *special_event;
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   struct pipe_context *pipe;
78bf215546Sopenharmony_ci   struct pipe_resource *output_texture;
79bf215546Sopenharmony_ci   uint32_t clip_width, clip_height;
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   struct vl_dri3_buffer *back_buffers[BACK_BUFFER_NUM];
82bf215546Sopenharmony_ci   int cur_back;
83bf215546Sopenharmony_ci   int next_back;
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   struct u_rect dirty_areas[BACK_BUFFER_NUM];
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci   struct vl_dri3_buffer *front_buffer;
88bf215546Sopenharmony_ci   bool is_pixmap;
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci   uint32_t send_msc_serial, recv_msc_serial;
91bf215546Sopenharmony_ci   uint64_t send_sbc, recv_sbc;
92bf215546Sopenharmony_ci   int64_t last_ust, ns_frame, last_msc, next_msc;
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   bool is_different_gpu;
95bf215546Sopenharmony_ci};
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_cistatic void
98bf215546Sopenharmony_cidri3_free_front_buffer(struct vl_dri3_screen *scrn,
99bf215546Sopenharmony_ci                        struct vl_dri3_buffer *buffer)
100bf215546Sopenharmony_ci{
101bf215546Sopenharmony_ci   xcb_sync_destroy_fence(scrn->conn, buffer->sync_fence);
102bf215546Sopenharmony_ci   xshmfence_unmap_shm(buffer->shm_fence);
103bf215546Sopenharmony_ci   pipe_resource_reference(&buffer->texture, NULL);
104bf215546Sopenharmony_ci   FREE(buffer);
105bf215546Sopenharmony_ci}
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_cistatic void
108bf215546Sopenharmony_cidri3_free_back_buffer(struct vl_dri3_screen *scrn,
109bf215546Sopenharmony_ci                        struct vl_dri3_buffer *buffer)
110bf215546Sopenharmony_ci{
111bf215546Sopenharmony_ci   if (buffer->region)
112bf215546Sopenharmony_ci      xcb_xfixes_destroy_region(scrn->conn, buffer->region);
113bf215546Sopenharmony_ci   xcb_free_pixmap(scrn->conn, buffer->pixmap);
114bf215546Sopenharmony_ci   xcb_sync_destroy_fence(scrn->conn, buffer->sync_fence);
115bf215546Sopenharmony_ci   xshmfence_unmap_shm(buffer->shm_fence);
116bf215546Sopenharmony_ci   if (!scrn->output_texture)
117bf215546Sopenharmony_ci      pipe_resource_reference(&buffer->texture, NULL);
118bf215546Sopenharmony_ci   if (buffer->linear_texture)
119bf215546Sopenharmony_ci       pipe_resource_reference(&buffer->linear_texture, NULL);
120bf215546Sopenharmony_ci   FREE(buffer);
121bf215546Sopenharmony_ci}
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_cistatic void
124bf215546Sopenharmony_cidri3_handle_stamps(struct vl_dri3_screen *scrn, uint64_t ust, uint64_t msc)
125bf215546Sopenharmony_ci{
126bf215546Sopenharmony_ci   int64_t ust_ns =  ust * 1000;
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci   if (scrn->last_ust && (ust_ns > scrn->last_ust) &&
129bf215546Sopenharmony_ci       scrn->last_msc && (msc > scrn->last_msc))
130bf215546Sopenharmony_ci      scrn->ns_frame = (ust_ns - scrn->last_ust) / (msc - scrn->last_msc);
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci   scrn->last_ust = ust_ns;
133bf215546Sopenharmony_ci   scrn->last_msc = msc;
134bf215546Sopenharmony_ci}
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_cistatic void
137bf215546Sopenharmony_cidri3_handle_present_event(struct vl_dri3_screen *scrn,
138bf215546Sopenharmony_ci                          xcb_present_generic_event_t *ge)
139bf215546Sopenharmony_ci{
140bf215546Sopenharmony_ci   switch (ge->evtype) {
141bf215546Sopenharmony_ci   case XCB_PRESENT_CONFIGURE_NOTIFY: {
142bf215546Sopenharmony_ci      xcb_present_configure_notify_event_t *ce = (void *) ge;
143bf215546Sopenharmony_ci      scrn->width = ce->width;
144bf215546Sopenharmony_ci      scrn->height = ce->height;
145bf215546Sopenharmony_ci      break;
146bf215546Sopenharmony_ci   }
147bf215546Sopenharmony_ci   case XCB_PRESENT_COMPLETE_NOTIFY: {
148bf215546Sopenharmony_ci      xcb_present_complete_notify_event_t *ce = (void *) ge;
149bf215546Sopenharmony_ci      if (ce->kind == XCB_PRESENT_COMPLETE_KIND_PIXMAP) {
150bf215546Sopenharmony_ci         scrn->recv_sbc = (scrn->send_sbc & 0xffffffff00000000LL) | ce->serial;
151bf215546Sopenharmony_ci         if (scrn->recv_sbc > scrn->send_sbc)
152bf215546Sopenharmony_ci            scrn->recv_sbc -= 0x100000000;
153bf215546Sopenharmony_ci         dri3_handle_stamps(scrn, ce->ust, ce->msc);
154bf215546Sopenharmony_ci      } else if (ce->kind == XCB_PRESENT_COMPLETE_KIND_NOTIFY_MSC) {
155bf215546Sopenharmony_ci         scrn->recv_msc_serial = ce->serial;
156bf215546Sopenharmony_ci         dri3_handle_stamps(scrn, ce->ust, ce->msc);
157bf215546Sopenharmony_ci      }
158bf215546Sopenharmony_ci      break;
159bf215546Sopenharmony_ci   }
160bf215546Sopenharmony_ci   case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
161bf215546Sopenharmony_ci      xcb_present_idle_notify_event_t *ie = (void *) ge;
162bf215546Sopenharmony_ci      int b;
163bf215546Sopenharmony_ci      for (b = 0; b < BACK_BUFFER_NUM; b++) {
164bf215546Sopenharmony_ci         struct vl_dri3_buffer *buf = scrn->back_buffers[b];
165bf215546Sopenharmony_ci         if (buf && buf->pixmap == ie->pixmap) {
166bf215546Sopenharmony_ci            buf->busy = false;
167bf215546Sopenharmony_ci            break;
168bf215546Sopenharmony_ci         }
169bf215546Sopenharmony_ci      }
170bf215546Sopenharmony_ci      break;
171bf215546Sopenharmony_ci   }
172bf215546Sopenharmony_ci   }
173bf215546Sopenharmony_ci   free(ge);
174bf215546Sopenharmony_ci}
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_cistatic void
177bf215546Sopenharmony_cidri3_flush_present_events(struct vl_dri3_screen *scrn)
178bf215546Sopenharmony_ci{
179bf215546Sopenharmony_ci   if (scrn->special_event) {
180bf215546Sopenharmony_ci      xcb_generic_event_t *ev;
181bf215546Sopenharmony_ci      while ((ev = xcb_poll_for_special_event(
182bf215546Sopenharmony_ci                   scrn->conn, scrn->special_event)) != NULL)
183bf215546Sopenharmony_ci         dri3_handle_present_event(scrn, (xcb_present_generic_event_t *)ev);
184bf215546Sopenharmony_ci   }
185bf215546Sopenharmony_ci}
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_cistatic bool
188bf215546Sopenharmony_cidri3_wait_present_events(struct vl_dri3_screen *scrn)
189bf215546Sopenharmony_ci{
190bf215546Sopenharmony_ci   if (scrn->special_event) {
191bf215546Sopenharmony_ci      xcb_generic_event_t *ev;
192bf215546Sopenharmony_ci      ev = xcb_wait_for_special_event(scrn->conn, scrn->special_event);
193bf215546Sopenharmony_ci      if (!ev)
194bf215546Sopenharmony_ci         return false;
195bf215546Sopenharmony_ci      dri3_handle_present_event(scrn, (xcb_present_generic_event_t *)ev);
196bf215546Sopenharmony_ci      return true;
197bf215546Sopenharmony_ci   }
198bf215546Sopenharmony_ci   return false;
199bf215546Sopenharmony_ci}
200bf215546Sopenharmony_ci
201bf215546Sopenharmony_cistatic int
202bf215546Sopenharmony_cidri3_find_back(struct vl_dri3_screen *scrn)
203bf215546Sopenharmony_ci{
204bf215546Sopenharmony_ci   int b;
205bf215546Sopenharmony_ci
206bf215546Sopenharmony_ci   for (;;) {
207bf215546Sopenharmony_ci      for (b = 0; b < BACK_BUFFER_NUM; b++) {
208bf215546Sopenharmony_ci         int id = (b + scrn->cur_back) % BACK_BUFFER_NUM;
209bf215546Sopenharmony_ci         struct vl_dri3_buffer *buffer = scrn->back_buffers[id];
210bf215546Sopenharmony_ci         if (!buffer || !buffer->busy)
211bf215546Sopenharmony_ci            return id;
212bf215546Sopenharmony_ci      }
213bf215546Sopenharmony_ci      xcb_flush(scrn->conn);
214bf215546Sopenharmony_ci      if (!dri3_wait_present_events(scrn))
215bf215546Sopenharmony_ci         return -1;
216bf215546Sopenharmony_ci   }
217bf215546Sopenharmony_ci}
218bf215546Sopenharmony_ci
219bf215546Sopenharmony_cistatic struct vl_dri3_buffer *
220bf215546Sopenharmony_cidri3_alloc_back_buffer(struct vl_dri3_screen *scrn)
221bf215546Sopenharmony_ci{
222bf215546Sopenharmony_ci   struct vl_dri3_buffer *buffer;
223bf215546Sopenharmony_ci   xcb_pixmap_t pixmap;
224bf215546Sopenharmony_ci   xcb_sync_fence_t sync_fence;
225bf215546Sopenharmony_ci   struct xshmfence *shm_fence;
226bf215546Sopenharmony_ci   int buffer_fd, fence_fd;
227bf215546Sopenharmony_ci   struct pipe_resource templ, *pixmap_buffer_texture;
228bf215546Sopenharmony_ci   struct winsys_handle whandle;
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci   buffer = CALLOC_STRUCT(vl_dri3_buffer);
231bf215546Sopenharmony_ci   if (!buffer)
232bf215546Sopenharmony_ci      return NULL;
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_ci   fence_fd = xshmfence_alloc_shm();
235bf215546Sopenharmony_ci   if (fence_fd < 0)
236bf215546Sopenharmony_ci      goto free_buffer;
237bf215546Sopenharmony_ci
238bf215546Sopenharmony_ci   shm_fence = xshmfence_map_shm(fence_fd);
239bf215546Sopenharmony_ci   if (!shm_fence)
240bf215546Sopenharmony_ci      goto close_fd;
241bf215546Sopenharmony_ci
242bf215546Sopenharmony_ci   memset(&templ, 0, sizeof(templ));
243bf215546Sopenharmony_ci   templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
244bf215546Sopenharmony_ci   templ.format = vl_dri2_format_for_depth(&scrn->base, scrn->depth);
245bf215546Sopenharmony_ci   templ.target = PIPE_TEXTURE_2D;
246bf215546Sopenharmony_ci   templ.last_level = 0;
247bf215546Sopenharmony_ci   templ.width0 = (scrn->output_texture) ?
248bf215546Sopenharmony_ci                  scrn->output_texture->width0 : scrn->width;
249bf215546Sopenharmony_ci   templ.height0 = (scrn->output_texture) ?
250bf215546Sopenharmony_ci                   scrn->output_texture->height0 : scrn->height;
251bf215546Sopenharmony_ci   templ.depth0 = 1;
252bf215546Sopenharmony_ci   templ.array_size = 1;
253bf215546Sopenharmony_ci
254bf215546Sopenharmony_ci   if (scrn->is_different_gpu) {
255bf215546Sopenharmony_ci      buffer->texture = (scrn->output_texture) ? scrn->output_texture :
256bf215546Sopenharmony_ci                        scrn->base.pscreen->resource_create(scrn->base.pscreen, &templ);
257bf215546Sopenharmony_ci      if (!buffer->texture)
258bf215546Sopenharmony_ci         goto unmap_shm;
259bf215546Sopenharmony_ci
260bf215546Sopenharmony_ci      templ.bind |= PIPE_BIND_SCANOUT | PIPE_BIND_SHARED |
261bf215546Sopenharmony_ci                    PIPE_BIND_LINEAR;
262bf215546Sopenharmony_ci      buffer->linear_texture =
263bf215546Sopenharmony_ci          scrn->base.pscreen->resource_create(scrn->base.pscreen, &templ);
264bf215546Sopenharmony_ci      pixmap_buffer_texture = buffer->linear_texture;
265bf215546Sopenharmony_ci
266bf215546Sopenharmony_ci      if (!buffer->linear_texture)
267bf215546Sopenharmony_ci         goto no_linear_texture;
268bf215546Sopenharmony_ci   } else {
269bf215546Sopenharmony_ci      templ.bind |= PIPE_BIND_SCANOUT | PIPE_BIND_SHARED;
270bf215546Sopenharmony_ci      buffer->texture = (scrn->output_texture) ? scrn->output_texture :
271bf215546Sopenharmony_ci                        scrn->base.pscreen->resource_create(scrn->base.pscreen, &templ);
272bf215546Sopenharmony_ci      if (!buffer->texture)
273bf215546Sopenharmony_ci         goto unmap_shm;
274bf215546Sopenharmony_ci      pixmap_buffer_texture = buffer->texture;
275bf215546Sopenharmony_ci   }
276bf215546Sopenharmony_ci   memset(&whandle, 0, sizeof(whandle));
277bf215546Sopenharmony_ci   whandle.type= WINSYS_HANDLE_TYPE_FD;
278bf215546Sopenharmony_ci   scrn->base.pscreen->resource_get_handle(scrn->base.pscreen, NULL,
279bf215546Sopenharmony_ci                                           pixmap_buffer_texture, &whandle, 0);
280bf215546Sopenharmony_ci   buffer_fd = whandle.handle;
281bf215546Sopenharmony_ci   buffer->pitch = whandle.stride;
282bf215546Sopenharmony_ci   buffer->width = templ.width0;
283bf215546Sopenharmony_ci   buffer->height = templ.height0;
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ci   xcb_dri3_pixmap_from_buffer(scrn->conn,
286bf215546Sopenharmony_ci                               (pixmap = xcb_generate_id(scrn->conn)),
287bf215546Sopenharmony_ci                               scrn->drawable,
288bf215546Sopenharmony_ci                               0,
289bf215546Sopenharmony_ci                               buffer->width, buffer->height, buffer->pitch,
290bf215546Sopenharmony_ci                               scrn->depth, 32,
291bf215546Sopenharmony_ci                               buffer_fd);
292bf215546Sopenharmony_ci   xcb_dri3_fence_from_fd(scrn->conn,
293bf215546Sopenharmony_ci                          pixmap,
294bf215546Sopenharmony_ci                          (sync_fence = xcb_generate_id(scrn->conn)),
295bf215546Sopenharmony_ci                          false,
296bf215546Sopenharmony_ci                          fence_fd);
297bf215546Sopenharmony_ci
298bf215546Sopenharmony_ci   buffer->pixmap = pixmap;
299bf215546Sopenharmony_ci   buffer->sync_fence = sync_fence;
300bf215546Sopenharmony_ci   buffer->shm_fence = shm_fence;
301bf215546Sopenharmony_ci
302bf215546Sopenharmony_ci   xshmfence_trigger(buffer->shm_fence);
303bf215546Sopenharmony_ci
304bf215546Sopenharmony_ci   return buffer;
305bf215546Sopenharmony_ci
306bf215546Sopenharmony_cino_linear_texture:
307bf215546Sopenharmony_ci   pipe_resource_reference(&buffer->texture, NULL);
308bf215546Sopenharmony_ciunmap_shm:
309bf215546Sopenharmony_ci   xshmfence_unmap_shm(shm_fence);
310bf215546Sopenharmony_ciclose_fd:
311bf215546Sopenharmony_ci   close(fence_fd);
312bf215546Sopenharmony_cifree_buffer:
313bf215546Sopenharmony_ci   FREE(buffer);
314bf215546Sopenharmony_ci   return NULL;
315bf215546Sopenharmony_ci}
316bf215546Sopenharmony_ci
317bf215546Sopenharmony_cistatic struct vl_dri3_buffer *
318bf215546Sopenharmony_cidri3_get_back_buffer(struct vl_dri3_screen *scrn)
319bf215546Sopenharmony_ci{
320bf215546Sopenharmony_ci   struct vl_dri3_buffer *buffer;
321bf215546Sopenharmony_ci   struct pipe_resource *texture = NULL;
322bf215546Sopenharmony_ci   bool allocate_new_buffer = false;
323bf215546Sopenharmony_ci   int b, id;
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_ci   assert(scrn);
326bf215546Sopenharmony_ci
327bf215546Sopenharmony_ci   scrn->cur_back = dri3_find_back(scrn);
328bf215546Sopenharmony_ci   if (scrn->cur_back < 0)
329bf215546Sopenharmony_ci      return NULL;
330bf215546Sopenharmony_ci   buffer = scrn->back_buffers[scrn->cur_back];
331bf215546Sopenharmony_ci
332bf215546Sopenharmony_ci   if (scrn->output_texture) {
333bf215546Sopenharmony_ci      if (!buffer || buffer->width < scrn->width ||
334bf215546Sopenharmony_ci          buffer->height < scrn->height)
335bf215546Sopenharmony_ci         allocate_new_buffer = true;
336bf215546Sopenharmony_ci      else if (scrn->is_different_gpu)
337bf215546Sopenharmony_ci         /* In case of different gpu we can reuse the linear
338bf215546Sopenharmony_ci          * texture so we only need to set the external
339bf215546Sopenharmony_ci          * texture for copying
340bf215546Sopenharmony_ci          */
341bf215546Sopenharmony_ci         buffer->texture = scrn->output_texture;
342bf215546Sopenharmony_ci      else {
343bf215546Sopenharmony_ci         /* In case of a single gpu we search if the texture is
344bf215546Sopenharmony_ci          * already present as buffer if not we get the
345bf215546Sopenharmony_ci          * handle and pixmap for the texture that is set
346bf215546Sopenharmony_ci          */
347bf215546Sopenharmony_ci         for (b = 0; b < BACK_BUFFER_NUM; b++) {
348bf215546Sopenharmony_ci            id = (b + scrn->cur_back) % BACK_BUFFER_NUM;
349bf215546Sopenharmony_ci            buffer = scrn->back_buffers[id];
350bf215546Sopenharmony_ci            if (buffer && !buffer->busy &&
351bf215546Sopenharmony_ci                buffer->texture == scrn->output_texture) {
352bf215546Sopenharmony_ci               scrn->cur_back = id;
353bf215546Sopenharmony_ci               break;
354bf215546Sopenharmony_ci            }
355bf215546Sopenharmony_ci         }
356bf215546Sopenharmony_ci
357bf215546Sopenharmony_ci         if (b == BACK_BUFFER_NUM) {
358bf215546Sopenharmony_ci            allocate_new_buffer = true;
359bf215546Sopenharmony_ci            scrn->cur_back = scrn->next_back;
360bf215546Sopenharmony_ci            scrn->next_back = (scrn->next_back + 1) % BACK_BUFFER_NUM;
361bf215546Sopenharmony_ci            buffer = scrn->back_buffers[scrn->cur_back];
362bf215546Sopenharmony_ci         }
363bf215546Sopenharmony_ci      }
364bf215546Sopenharmony_ci
365bf215546Sopenharmony_ci   } else {
366bf215546Sopenharmony_ci      if (!buffer || buffer->width != scrn->width ||
367bf215546Sopenharmony_ci          buffer->height != scrn->height)
368bf215546Sopenharmony_ci         allocate_new_buffer = true;
369bf215546Sopenharmony_ci   }
370bf215546Sopenharmony_ci
371bf215546Sopenharmony_ci   if (allocate_new_buffer) {
372bf215546Sopenharmony_ci      struct vl_dri3_buffer *new_buffer;
373bf215546Sopenharmony_ci
374bf215546Sopenharmony_ci      new_buffer = dri3_alloc_back_buffer(scrn);
375bf215546Sopenharmony_ci      if (!new_buffer)
376bf215546Sopenharmony_ci         return NULL;
377bf215546Sopenharmony_ci
378bf215546Sopenharmony_ci      if (buffer)
379bf215546Sopenharmony_ci         dri3_free_back_buffer(scrn, buffer);
380bf215546Sopenharmony_ci
381bf215546Sopenharmony_ci      if (!scrn->output_texture)
382bf215546Sopenharmony_ci         vl_compositor_reset_dirty_area(&scrn->dirty_areas[scrn->cur_back]);
383bf215546Sopenharmony_ci      buffer = new_buffer;
384bf215546Sopenharmony_ci      scrn->back_buffers[scrn->cur_back] = buffer;
385bf215546Sopenharmony_ci   }
386bf215546Sopenharmony_ci
387bf215546Sopenharmony_ci   pipe_resource_reference(&texture, buffer->texture);
388bf215546Sopenharmony_ci   xcb_flush(scrn->conn);
389bf215546Sopenharmony_ci   xshmfence_await(buffer->shm_fence);
390bf215546Sopenharmony_ci
391bf215546Sopenharmony_ci   return buffer;
392bf215546Sopenharmony_ci}
393bf215546Sopenharmony_ci
394bf215546Sopenharmony_cistatic bool
395bf215546Sopenharmony_cidri3_set_drawable(struct vl_dri3_screen *scrn, Drawable drawable)
396bf215546Sopenharmony_ci{
397bf215546Sopenharmony_ci   xcb_get_geometry_cookie_t geom_cookie;
398bf215546Sopenharmony_ci   xcb_get_geometry_reply_t *geom_reply;
399bf215546Sopenharmony_ci   xcb_void_cookie_t cookie;
400bf215546Sopenharmony_ci   xcb_generic_error_t *error;
401bf215546Sopenharmony_ci   bool ret = true;
402bf215546Sopenharmony_ci
403bf215546Sopenharmony_ci   assert(drawable);
404bf215546Sopenharmony_ci
405bf215546Sopenharmony_ci   if (scrn->drawable == drawable)
406bf215546Sopenharmony_ci      return true;
407bf215546Sopenharmony_ci
408bf215546Sopenharmony_ci   scrn->drawable = drawable;
409bf215546Sopenharmony_ci
410bf215546Sopenharmony_ci   geom_cookie = xcb_get_geometry(scrn->conn, scrn->drawable);
411bf215546Sopenharmony_ci   geom_reply = xcb_get_geometry_reply(scrn->conn, geom_cookie, NULL);
412bf215546Sopenharmony_ci   if (!geom_reply)
413bf215546Sopenharmony_ci      return false;
414bf215546Sopenharmony_ci
415bf215546Sopenharmony_ci   scrn->width = geom_reply->width;
416bf215546Sopenharmony_ci   scrn->height = geom_reply->height;
417bf215546Sopenharmony_ci   scrn->depth = geom_reply->depth;
418bf215546Sopenharmony_ci   free(geom_reply);
419bf215546Sopenharmony_ci
420bf215546Sopenharmony_ci   if (scrn->special_event) {
421bf215546Sopenharmony_ci      xcb_unregister_for_special_event(scrn->conn, scrn->special_event);
422bf215546Sopenharmony_ci      scrn->special_event = NULL;
423bf215546Sopenharmony_ci      cookie = xcb_present_select_input_checked(scrn->conn, scrn->eid,
424bf215546Sopenharmony_ci                                                scrn->drawable,
425bf215546Sopenharmony_ci                                                XCB_PRESENT_EVENT_MASK_NO_EVENT);
426bf215546Sopenharmony_ci      xcb_discard_reply(scrn->conn, cookie.sequence);
427bf215546Sopenharmony_ci   }
428bf215546Sopenharmony_ci
429bf215546Sopenharmony_ci   scrn->is_pixmap = false;
430bf215546Sopenharmony_ci   scrn->eid = xcb_generate_id(scrn->conn);
431bf215546Sopenharmony_ci   cookie =
432bf215546Sopenharmony_ci      xcb_present_select_input_checked(scrn->conn, scrn->eid, scrn->drawable,
433bf215546Sopenharmony_ci                      XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
434bf215546Sopenharmony_ci                      XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
435bf215546Sopenharmony_ci                      XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
436bf215546Sopenharmony_ci
437bf215546Sopenharmony_ci   error = xcb_request_check(scrn->conn, cookie);
438bf215546Sopenharmony_ci   if (error) {
439bf215546Sopenharmony_ci      if (error->error_code != BadWindow)
440bf215546Sopenharmony_ci         ret = false;
441bf215546Sopenharmony_ci      else {
442bf215546Sopenharmony_ci         scrn->is_pixmap = true;
443bf215546Sopenharmony_ci         scrn->base.set_back_texture_from_output = NULL;
444bf215546Sopenharmony_ci         if (scrn->front_buffer) {
445bf215546Sopenharmony_ci            dri3_free_front_buffer(scrn, scrn->front_buffer);
446bf215546Sopenharmony_ci            scrn->front_buffer = NULL;
447bf215546Sopenharmony_ci         }
448bf215546Sopenharmony_ci      }
449bf215546Sopenharmony_ci      free(error);
450bf215546Sopenharmony_ci   } else
451bf215546Sopenharmony_ci      scrn->special_event =
452bf215546Sopenharmony_ci         xcb_register_for_special_xge(scrn->conn, &xcb_present_id, scrn->eid, 0);
453bf215546Sopenharmony_ci
454bf215546Sopenharmony_ci   dri3_flush_present_events(scrn);
455bf215546Sopenharmony_ci
456bf215546Sopenharmony_ci   return ret;
457bf215546Sopenharmony_ci}
458bf215546Sopenharmony_ci
459bf215546Sopenharmony_cistatic struct vl_dri3_buffer *
460bf215546Sopenharmony_cidri3_get_front_buffer(struct vl_dri3_screen *scrn)
461bf215546Sopenharmony_ci{
462bf215546Sopenharmony_ci   xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie;
463bf215546Sopenharmony_ci   xcb_dri3_buffer_from_pixmap_reply_t *bp_reply;
464bf215546Sopenharmony_ci   xcb_sync_fence_t sync_fence;
465bf215546Sopenharmony_ci   struct xshmfence *shm_fence;
466bf215546Sopenharmony_ci   int fence_fd, *fds;
467bf215546Sopenharmony_ci   struct winsys_handle whandle;
468bf215546Sopenharmony_ci   struct pipe_resource templ, *texture = NULL;
469bf215546Sopenharmony_ci
470bf215546Sopenharmony_ci   if (scrn->front_buffer) {
471bf215546Sopenharmony_ci      pipe_resource_reference(&texture, scrn->front_buffer->texture);
472bf215546Sopenharmony_ci      return scrn->front_buffer;
473bf215546Sopenharmony_ci   }
474bf215546Sopenharmony_ci
475bf215546Sopenharmony_ci   scrn->front_buffer = CALLOC_STRUCT(vl_dri3_buffer);
476bf215546Sopenharmony_ci   if (!scrn->front_buffer)
477bf215546Sopenharmony_ci      return NULL;
478bf215546Sopenharmony_ci
479bf215546Sopenharmony_ci   fence_fd = xshmfence_alloc_shm();
480bf215546Sopenharmony_ci   if (fence_fd < 0)
481bf215546Sopenharmony_ci      goto free_buffer;
482bf215546Sopenharmony_ci
483bf215546Sopenharmony_ci   shm_fence = xshmfence_map_shm(fence_fd);
484bf215546Sopenharmony_ci   if (!shm_fence)
485bf215546Sopenharmony_ci      goto close_fd;
486bf215546Sopenharmony_ci
487bf215546Sopenharmony_ci   bp_cookie = xcb_dri3_buffer_from_pixmap(scrn->conn, scrn->drawable);
488bf215546Sopenharmony_ci   bp_reply = xcb_dri3_buffer_from_pixmap_reply(scrn->conn, bp_cookie, NULL);
489bf215546Sopenharmony_ci   if (!bp_reply)
490bf215546Sopenharmony_ci      goto unmap_shm;
491bf215546Sopenharmony_ci
492bf215546Sopenharmony_ci   fds = xcb_dri3_buffer_from_pixmap_reply_fds(scrn->conn, bp_reply);
493bf215546Sopenharmony_ci   if (fds[0] < 0)
494bf215546Sopenharmony_ci      goto free_reply;
495bf215546Sopenharmony_ci
496bf215546Sopenharmony_ci   memset(&whandle, 0, sizeof(whandle));
497bf215546Sopenharmony_ci   whandle.type = WINSYS_HANDLE_TYPE_FD;
498bf215546Sopenharmony_ci   whandle.handle = (unsigned)fds[0];
499bf215546Sopenharmony_ci   whandle.stride = bp_reply->stride;
500bf215546Sopenharmony_ci   whandle.modifier = DRM_FORMAT_MOD_INVALID;
501bf215546Sopenharmony_ci   memset(&templ, 0, sizeof(templ));
502bf215546Sopenharmony_ci   templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
503bf215546Sopenharmony_ci   templ.format = vl_dri2_format_for_depth(&scrn->base, bp_reply->depth);
504bf215546Sopenharmony_ci   templ.target = PIPE_TEXTURE_2D;
505bf215546Sopenharmony_ci   templ.last_level = 0;
506bf215546Sopenharmony_ci   templ.width0 = bp_reply->width;
507bf215546Sopenharmony_ci   templ.height0 = bp_reply->height;
508bf215546Sopenharmony_ci   templ.depth0 = 1;
509bf215546Sopenharmony_ci   templ.array_size = 1;
510bf215546Sopenharmony_ci   scrn->front_buffer->texture =
511bf215546Sopenharmony_ci      scrn->base.pscreen->resource_from_handle(scrn->base.pscreen,
512bf215546Sopenharmony_ci                                               &templ, &whandle,
513bf215546Sopenharmony_ci                                               PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
514bf215546Sopenharmony_ci   close(fds[0]);
515bf215546Sopenharmony_ci   if (!scrn->front_buffer->texture)
516bf215546Sopenharmony_ci      goto free_reply;
517bf215546Sopenharmony_ci
518bf215546Sopenharmony_ci   xcb_dri3_fence_from_fd(scrn->conn,
519bf215546Sopenharmony_ci                          scrn->drawable,
520bf215546Sopenharmony_ci                          (sync_fence = xcb_generate_id(scrn->conn)),
521bf215546Sopenharmony_ci                          false,
522bf215546Sopenharmony_ci                          fence_fd);
523bf215546Sopenharmony_ci
524bf215546Sopenharmony_ci   pipe_resource_reference(&texture, scrn->front_buffer->texture);
525bf215546Sopenharmony_ci   scrn->front_buffer->pixmap = scrn->drawable;
526bf215546Sopenharmony_ci   scrn->front_buffer->width = bp_reply->width;
527bf215546Sopenharmony_ci   scrn->front_buffer->height = bp_reply->height;
528bf215546Sopenharmony_ci   scrn->front_buffer->shm_fence = shm_fence;
529bf215546Sopenharmony_ci   scrn->front_buffer->sync_fence = sync_fence;
530bf215546Sopenharmony_ci   free(bp_reply);
531bf215546Sopenharmony_ci
532bf215546Sopenharmony_ci   return scrn->front_buffer;
533bf215546Sopenharmony_ci
534bf215546Sopenharmony_cifree_reply:
535bf215546Sopenharmony_ci   free(bp_reply);
536bf215546Sopenharmony_ciunmap_shm:
537bf215546Sopenharmony_ci   xshmfence_unmap_shm(shm_fence);
538bf215546Sopenharmony_ciclose_fd:
539bf215546Sopenharmony_ci   close(fence_fd);
540bf215546Sopenharmony_cifree_buffer:
541bf215546Sopenharmony_ci   FREE(scrn->front_buffer);
542bf215546Sopenharmony_ci   return NULL;
543bf215546Sopenharmony_ci}
544bf215546Sopenharmony_ci
545bf215546Sopenharmony_cistatic xcb_screen_t *
546bf215546Sopenharmony_cidri3_get_screen_for_root(xcb_connection_t *conn, xcb_window_t root)
547bf215546Sopenharmony_ci{
548bf215546Sopenharmony_ci   xcb_screen_iterator_t screen_iter =
549bf215546Sopenharmony_ci   xcb_setup_roots_iterator(xcb_get_setup(conn));
550bf215546Sopenharmony_ci
551bf215546Sopenharmony_ci   for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
552bf215546Sopenharmony_ci      if (screen_iter.data->root == root)
553bf215546Sopenharmony_ci         return screen_iter.data;
554bf215546Sopenharmony_ci   }
555bf215546Sopenharmony_ci
556bf215546Sopenharmony_ci   return NULL;
557bf215546Sopenharmony_ci}
558bf215546Sopenharmony_ci
559bf215546Sopenharmony_cistatic void
560bf215546Sopenharmony_civl_dri3_flush_frontbuffer(struct pipe_screen *screen,
561bf215546Sopenharmony_ci                          struct pipe_context *pipe,
562bf215546Sopenharmony_ci                          struct pipe_resource *resource,
563bf215546Sopenharmony_ci                          unsigned level, unsigned layer,
564bf215546Sopenharmony_ci                          void *context_private, struct pipe_box *sub_box)
565bf215546Sopenharmony_ci{
566bf215546Sopenharmony_ci   struct vl_dri3_screen *scrn = (struct vl_dri3_screen *)context_private;
567bf215546Sopenharmony_ci   uint32_t options = XCB_PRESENT_OPTION_NONE;
568bf215546Sopenharmony_ci   struct vl_dri3_buffer *back;
569bf215546Sopenharmony_ci   struct pipe_box src_box;
570bf215546Sopenharmony_ci   xcb_rectangle_t rectangle;
571bf215546Sopenharmony_ci
572bf215546Sopenharmony_ci   back = scrn->back_buffers[scrn->cur_back];
573bf215546Sopenharmony_ci   if (!back)
574bf215546Sopenharmony_ci       return;
575bf215546Sopenharmony_ci
576bf215546Sopenharmony_ci   while (scrn->special_event && scrn->recv_sbc < scrn->send_sbc)
577bf215546Sopenharmony_ci      if (!dri3_wait_present_events(scrn))
578bf215546Sopenharmony_ci         return;
579bf215546Sopenharmony_ci
580bf215546Sopenharmony_ci   rectangle.x = 0;
581bf215546Sopenharmony_ci   rectangle.y = 0;
582bf215546Sopenharmony_ci   rectangle.width = (scrn->output_texture) ? scrn->clip_width : scrn->width;
583bf215546Sopenharmony_ci   rectangle.height = (scrn->output_texture) ? scrn->clip_height : scrn->height;
584bf215546Sopenharmony_ci
585bf215546Sopenharmony_ci   if (!back->region) {
586bf215546Sopenharmony_ci      back->region = xcb_generate_id(scrn->conn);
587bf215546Sopenharmony_ci      xcb_xfixes_create_region(scrn->conn, back->region, 0, NULL);
588bf215546Sopenharmony_ci   }
589bf215546Sopenharmony_ci   xcb_xfixes_set_region(scrn->conn, back->region, 1, &rectangle);
590bf215546Sopenharmony_ci
591bf215546Sopenharmony_ci   if (scrn->is_different_gpu) {
592bf215546Sopenharmony_ci      u_box_origin_2d(back->width, back->height, &src_box);
593bf215546Sopenharmony_ci      scrn->pipe->resource_copy_region(scrn->pipe,
594bf215546Sopenharmony_ci                                       back->linear_texture,
595bf215546Sopenharmony_ci                                       0, 0, 0, 0,
596bf215546Sopenharmony_ci                                       back->texture,
597bf215546Sopenharmony_ci                                       0, &src_box);
598bf215546Sopenharmony_ci
599bf215546Sopenharmony_ci      scrn->pipe->flush(scrn->pipe, NULL, 0);
600bf215546Sopenharmony_ci   }
601bf215546Sopenharmony_ci   xshmfence_reset(back->shm_fence);
602bf215546Sopenharmony_ci   back->busy = true;
603bf215546Sopenharmony_ci
604bf215546Sopenharmony_ci   xcb_present_pixmap(scrn->conn,
605bf215546Sopenharmony_ci                      scrn->drawable,
606bf215546Sopenharmony_ci                      back->pixmap,
607bf215546Sopenharmony_ci                      (uint32_t)(++scrn->send_sbc),
608bf215546Sopenharmony_ci                      0, back->region, 0, 0,
609bf215546Sopenharmony_ci                      None, None,
610bf215546Sopenharmony_ci                      back->sync_fence,
611bf215546Sopenharmony_ci                      options,
612bf215546Sopenharmony_ci                      scrn->next_msc,
613bf215546Sopenharmony_ci                      0, 0, 0, NULL);
614bf215546Sopenharmony_ci
615bf215546Sopenharmony_ci   xcb_flush(scrn->conn);
616bf215546Sopenharmony_ci
617bf215546Sopenharmony_ci   return;
618bf215546Sopenharmony_ci}
619bf215546Sopenharmony_ci
620bf215546Sopenharmony_cistatic struct pipe_resource *
621bf215546Sopenharmony_civl_dri3_screen_texture_from_drawable(struct vl_screen *vscreen, void *drawable)
622bf215546Sopenharmony_ci{
623bf215546Sopenharmony_ci   struct vl_dri3_screen *scrn = (struct vl_dri3_screen *)vscreen;
624bf215546Sopenharmony_ci   struct vl_dri3_buffer *buffer;
625bf215546Sopenharmony_ci
626bf215546Sopenharmony_ci   assert(scrn);
627bf215546Sopenharmony_ci
628bf215546Sopenharmony_ci   if (!dri3_set_drawable(scrn, (Drawable)drawable))
629bf215546Sopenharmony_ci      return NULL;
630bf215546Sopenharmony_ci
631bf215546Sopenharmony_ci   buffer = (scrn->is_pixmap) ?
632bf215546Sopenharmony_ci            dri3_get_front_buffer(scrn) :
633bf215546Sopenharmony_ci            dri3_get_back_buffer(scrn);
634bf215546Sopenharmony_ci   if (!buffer)
635bf215546Sopenharmony_ci      return NULL;
636bf215546Sopenharmony_ci
637bf215546Sopenharmony_ci   return buffer->texture;
638bf215546Sopenharmony_ci}
639bf215546Sopenharmony_ci
640bf215546Sopenharmony_cistatic struct u_rect *
641bf215546Sopenharmony_civl_dri3_screen_get_dirty_area(struct vl_screen *vscreen)
642bf215546Sopenharmony_ci{
643bf215546Sopenharmony_ci   struct vl_dri3_screen *scrn = (struct vl_dri3_screen *)vscreen;
644bf215546Sopenharmony_ci
645bf215546Sopenharmony_ci   assert(scrn);
646bf215546Sopenharmony_ci
647bf215546Sopenharmony_ci   return &scrn->dirty_areas[scrn->cur_back];
648bf215546Sopenharmony_ci}
649bf215546Sopenharmony_ci
650bf215546Sopenharmony_cistatic uint64_t
651bf215546Sopenharmony_civl_dri3_screen_get_timestamp(struct vl_screen *vscreen, void *drawable)
652bf215546Sopenharmony_ci{
653bf215546Sopenharmony_ci   struct vl_dri3_screen *scrn = (struct vl_dri3_screen *)vscreen;
654bf215546Sopenharmony_ci
655bf215546Sopenharmony_ci   assert(scrn);
656bf215546Sopenharmony_ci
657bf215546Sopenharmony_ci   if (!dri3_set_drawable(scrn, (Drawable)drawable))
658bf215546Sopenharmony_ci      return 0;
659bf215546Sopenharmony_ci
660bf215546Sopenharmony_ci   if (!scrn->last_ust) {
661bf215546Sopenharmony_ci      xcb_present_notify_msc(scrn->conn,
662bf215546Sopenharmony_ci                             scrn->drawable,
663bf215546Sopenharmony_ci                             ++scrn->send_msc_serial,
664bf215546Sopenharmony_ci                             0, 0, 0);
665bf215546Sopenharmony_ci      xcb_flush(scrn->conn);
666bf215546Sopenharmony_ci
667bf215546Sopenharmony_ci      while (scrn->special_event &&
668bf215546Sopenharmony_ci             scrn->send_msc_serial > scrn->recv_msc_serial) {
669bf215546Sopenharmony_ci         if (!dri3_wait_present_events(scrn))
670bf215546Sopenharmony_ci            return 0;
671bf215546Sopenharmony_ci      }
672bf215546Sopenharmony_ci   }
673bf215546Sopenharmony_ci
674bf215546Sopenharmony_ci   return scrn->last_ust;
675bf215546Sopenharmony_ci}
676bf215546Sopenharmony_ci
677bf215546Sopenharmony_cistatic void
678bf215546Sopenharmony_civl_dri3_screen_set_next_timestamp(struct vl_screen *vscreen, uint64_t stamp)
679bf215546Sopenharmony_ci{
680bf215546Sopenharmony_ci   struct vl_dri3_screen *scrn = (struct vl_dri3_screen *)vscreen;
681bf215546Sopenharmony_ci
682bf215546Sopenharmony_ci   assert(scrn);
683bf215546Sopenharmony_ci
684bf215546Sopenharmony_ci   if (stamp && scrn->last_ust && scrn->ns_frame && scrn->last_msc)
685bf215546Sopenharmony_ci      scrn->next_msc = ((int64_t)stamp - scrn->last_ust + scrn->ns_frame/2) /
686bf215546Sopenharmony_ci                       scrn->ns_frame + scrn->last_msc;
687bf215546Sopenharmony_ci   else
688bf215546Sopenharmony_ci      scrn->next_msc = 0;
689bf215546Sopenharmony_ci}
690bf215546Sopenharmony_ci
691bf215546Sopenharmony_cistatic void *
692bf215546Sopenharmony_civl_dri3_screen_get_private(struct vl_screen *vscreen)
693bf215546Sopenharmony_ci{
694bf215546Sopenharmony_ci   return vscreen;
695bf215546Sopenharmony_ci}
696bf215546Sopenharmony_ci
697bf215546Sopenharmony_cistatic void
698bf215546Sopenharmony_civl_dri3_screen_set_back_texture_from_output(struct vl_screen *vscreen,
699bf215546Sopenharmony_ci                                            struct pipe_resource *buffer,
700bf215546Sopenharmony_ci                                            uint32_t width, uint32_t height)
701bf215546Sopenharmony_ci{
702bf215546Sopenharmony_ci   struct vl_dri3_screen *scrn = (struct vl_dri3_screen *)vscreen;
703bf215546Sopenharmony_ci
704bf215546Sopenharmony_ci   assert(scrn);
705bf215546Sopenharmony_ci
706bf215546Sopenharmony_ci   scrn->output_texture = buffer;
707bf215546Sopenharmony_ci   scrn->clip_width = (width) ? width : scrn->width;
708bf215546Sopenharmony_ci   scrn->clip_height = (height) ? height : scrn->height;
709bf215546Sopenharmony_ci}
710bf215546Sopenharmony_ci
711bf215546Sopenharmony_cistatic void
712bf215546Sopenharmony_civl_dri3_screen_destroy(struct vl_screen *vscreen)
713bf215546Sopenharmony_ci{
714bf215546Sopenharmony_ci   struct vl_dri3_screen *scrn = (struct vl_dri3_screen *)vscreen;
715bf215546Sopenharmony_ci   int i;
716bf215546Sopenharmony_ci
717bf215546Sopenharmony_ci   assert(vscreen);
718bf215546Sopenharmony_ci
719bf215546Sopenharmony_ci   dri3_flush_present_events(scrn);
720bf215546Sopenharmony_ci
721bf215546Sopenharmony_ci   if (scrn->front_buffer) {
722bf215546Sopenharmony_ci      dri3_free_front_buffer(scrn, scrn->front_buffer);
723bf215546Sopenharmony_ci      scrn->front_buffer = NULL;
724bf215546Sopenharmony_ci   }
725bf215546Sopenharmony_ci
726bf215546Sopenharmony_ci   for (i = 0; i < BACK_BUFFER_NUM; ++i) {
727bf215546Sopenharmony_ci      if (scrn->back_buffers[i]) {
728bf215546Sopenharmony_ci         dri3_free_back_buffer(scrn, scrn->back_buffers[i]);
729bf215546Sopenharmony_ci         scrn->back_buffers[i] = NULL;
730bf215546Sopenharmony_ci      }
731bf215546Sopenharmony_ci   }
732bf215546Sopenharmony_ci
733bf215546Sopenharmony_ci   if (scrn->special_event) {
734bf215546Sopenharmony_ci      xcb_void_cookie_t cookie =
735bf215546Sopenharmony_ci         xcb_present_select_input_checked(scrn->conn, scrn->eid,
736bf215546Sopenharmony_ci                                          scrn->drawable,
737bf215546Sopenharmony_ci                                          XCB_PRESENT_EVENT_MASK_NO_EVENT);
738bf215546Sopenharmony_ci
739bf215546Sopenharmony_ci      xcb_discard_reply(scrn->conn, cookie.sequence);
740bf215546Sopenharmony_ci      xcb_unregister_for_special_event(scrn->conn, scrn->special_event);
741bf215546Sopenharmony_ci   }
742bf215546Sopenharmony_ci   scrn->pipe->destroy(scrn->pipe);
743bf215546Sopenharmony_ci   scrn->base.pscreen->destroy(scrn->base.pscreen);
744bf215546Sopenharmony_ci   pipe_loader_release(&scrn->base.dev, 1);
745bf215546Sopenharmony_ci   FREE(scrn);
746bf215546Sopenharmony_ci
747bf215546Sopenharmony_ci   return;
748bf215546Sopenharmony_ci}
749bf215546Sopenharmony_ci
750bf215546Sopenharmony_cistruct vl_screen *
751bf215546Sopenharmony_civl_dri3_screen_create(Display *display, int screen)
752bf215546Sopenharmony_ci{
753bf215546Sopenharmony_ci   struct vl_dri3_screen *scrn;
754bf215546Sopenharmony_ci   const xcb_query_extension_reply_t *extension;
755bf215546Sopenharmony_ci   xcb_dri3_open_cookie_t open_cookie;
756bf215546Sopenharmony_ci   xcb_dri3_open_reply_t *open_reply;
757bf215546Sopenharmony_ci   xcb_get_geometry_cookie_t geom_cookie;
758bf215546Sopenharmony_ci   xcb_get_geometry_reply_t *geom_reply;
759bf215546Sopenharmony_ci   xcb_xfixes_query_version_cookie_t xfixes_cookie;
760bf215546Sopenharmony_ci   xcb_xfixes_query_version_reply_t *xfixes_reply;
761bf215546Sopenharmony_ci   xcb_generic_error_t *error;
762bf215546Sopenharmony_ci   int fd;
763bf215546Sopenharmony_ci
764bf215546Sopenharmony_ci   assert(display);
765bf215546Sopenharmony_ci
766bf215546Sopenharmony_ci   scrn = CALLOC_STRUCT(vl_dri3_screen);
767bf215546Sopenharmony_ci   if (!scrn)
768bf215546Sopenharmony_ci      return NULL;
769bf215546Sopenharmony_ci
770bf215546Sopenharmony_ci   scrn->conn = XGetXCBConnection(display);
771bf215546Sopenharmony_ci   if (!scrn->conn)
772bf215546Sopenharmony_ci      goto free_screen;
773bf215546Sopenharmony_ci
774bf215546Sopenharmony_ci   xcb_prefetch_extension_data(scrn->conn , &xcb_dri3_id);
775bf215546Sopenharmony_ci   xcb_prefetch_extension_data(scrn->conn, &xcb_present_id);
776bf215546Sopenharmony_ci   xcb_prefetch_extension_data (scrn->conn, &xcb_xfixes_id);
777bf215546Sopenharmony_ci   extension = xcb_get_extension_data(scrn->conn, &xcb_dri3_id);
778bf215546Sopenharmony_ci   if (!(extension && extension->present))
779bf215546Sopenharmony_ci      goto free_screen;
780bf215546Sopenharmony_ci   extension = xcb_get_extension_data(scrn->conn, &xcb_present_id);
781bf215546Sopenharmony_ci   if (!(extension && extension->present))
782bf215546Sopenharmony_ci      goto free_screen;
783bf215546Sopenharmony_ci   extension = xcb_get_extension_data(scrn->conn, &xcb_xfixes_id);
784bf215546Sopenharmony_ci   if (!(extension && extension->present))
785bf215546Sopenharmony_ci      goto free_screen;
786bf215546Sopenharmony_ci
787bf215546Sopenharmony_ci   xfixes_cookie = xcb_xfixes_query_version(scrn->conn, XCB_XFIXES_MAJOR_VERSION,
788bf215546Sopenharmony_ci                                            XCB_XFIXES_MINOR_VERSION);
789bf215546Sopenharmony_ci   xfixes_reply = xcb_xfixes_query_version_reply(scrn->conn, xfixes_cookie, &error);
790bf215546Sopenharmony_ci   if (!xfixes_reply || error || xfixes_reply->major_version < 2) {
791bf215546Sopenharmony_ci      free(error);
792bf215546Sopenharmony_ci      free(xfixes_reply);
793bf215546Sopenharmony_ci      goto free_screen;
794bf215546Sopenharmony_ci   }
795bf215546Sopenharmony_ci   free(xfixes_reply);
796bf215546Sopenharmony_ci
797bf215546Sopenharmony_ci   open_cookie = xcb_dri3_open(scrn->conn, RootWindow(display, screen), None);
798bf215546Sopenharmony_ci   open_reply = xcb_dri3_open_reply(scrn->conn, open_cookie, NULL);
799bf215546Sopenharmony_ci   if (!open_reply)
800bf215546Sopenharmony_ci      goto free_screen;
801bf215546Sopenharmony_ci   if (open_reply->nfd != 1) {
802bf215546Sopenharmony_ci      free(open_reply);
803bf215546Sopenharmony_ci      goto free_screen;
804bf215546Sopenharmony_ci   }
805bf215546Sopenharmony_ci
806bf215546Sopenharmony_ci   fd = xcb_dri3_open_reply_fds(scrn->conn, open_reply)[0];
807bf215546Sopenharmony_ci   if (fd < 0) {
808bf215546Sopenharmony_ci      free(open_reply);
809bf215546Sopenharmony_ci      goto free_screen;
810bf215546Sopenharmony_ci   }
811bf215546Sopenharmony_ci   fcntl(fd, F_SETFD, FD_CLOEXEC);
812bf215546Sopenharmony_ci   free(open_reply);
813bf215546Sopenharmony_ci
814bf215546Sopenharmony_ci   fd = loader_get_user_preferred_fd(fd, &scrn->is_different_gpu);
815bf215546Sopenharmony_ci
816bf215546Sopenharmony_ci   geom_cookie = xcb_get_geometry(scrn->conn, RootWindow(display, screen));
817bf215546Sopenharmony_ci   geom_reply = xcb_get_geometry_reply(scrn->conn, geom_cookie, NULL);
818bf215546Sopenharmony_ci   if (!geom_reply)
819bf215546Sopenharmony_ci      goto close_fd;
820bf215546Sopenharmony_ci
821bf215546Sopenharmony_ci   scrn->base.xcb_screen = dri3_get_screen_for_root(scrn->conn, geom_reply->root);
822bf215546Sopenharmony_ci   if (!scrn->base.xcb_screen) {
823bf215546Sopenharmony_ci      free(geom_reply);
824bf215546Sopenharmony_ci      goto close_fd;
825bf215546Sopenharmony_ci   }
826bf215546Sopenharmony_ci
827bf215546Sopenharmony_ci   /* TODO support depth other than 24 or 30 */
828bf215546Sopenharmony_ci   if (geom_reply->depth != 24 && geom_reply->depth != 30) {
829bf215546Sopenharmony_ci      free(geom_reply);
830bf215546Sopenharmony_ci      goto close_fd;
831bf215546Sopenharmony_ci   }
832bf215546Sopenharmony_ci   scrn->base.color_depth = geom_reply->depth;
833bf215546Sopenharmony_ci   free(geom_reply);
834bf215546Sopenharmony_ci
835bf215546Sopenharmony_ci   if (pipe_loader_drm_probe_fd(&scrn->base.dev, fd))
836bf215546Sopenharmony_ci      scrn->base.pscreen = pipe_loader_create_screen(scrn->base.dev);
837bf215546Sopenharmony_ci
838bf215546Sopenharmony_ci   if (!scrn->base.pscreen)
839bf215546Sopenharmony_ci      goto release_pipe;
840bf215546Sopenharmony_ci
841bf215546Sopenharmony_ci   scrn->pipe = pipe_create_multimedia_context(scrn->base.pscreen);
842bf215546Sopenharmony_ci   if (!scrn->pipe)
843bf215546Sopenharmony_ci       goto no_context;
844bf215546Sopenharmony_ci
845bf215546Sopenharmony_ci   scrn->base.destroy = vl_dri3_screen_destroy;
846bf215546Sopenharmony_ci   scrn->base.texture_from_drawable = vl_dri3_screen_texture_from_drawable;
847bf215546Sopenharmony_ci   scrn->base.get_dirty_area = vl_dri3_screen_get_dirty_area;
848bf215546Sopenharmony_ci   scrn->base.get_timestamp = vl_dri3_screen_get_timestamp;
849bf215546Sopenharmony_ci   scrn->base.set_next_timestamp = vl_dri3_screen_set_next_timestamp;
850bf215546Sopenharmony_ci   scrn->base.get_private = vl_dri3_screen_get_private;
851bf215546Sopenharmony_ci   scrn->base.pscreen->flush_frontbuffer = vl_dri3_flush_frontbuffer;
852bf215546Sopenharmony_ci   scrn->base.set_back_texture_from_output = vl_dri3_screen_set_back_texture_from_output;
853bf215546Sopenharmony_ci
854bf215546Sopenharmony_ci   scrn->next_back = 1;
855bf215546Sopenharmony_ci
856bf215546Sopenharmony_ci   close(fd);
857bf215546Sopenharmony_ci
858bf215546Sopenharmony_ci   return &scrn->base;
859bf215546Sopenharmony_ci
860bf215546Sopenharmony_cino_context:
861bf215546Sopenharmony_ci   scrn->base.pscreen->destroy(scrn->base.pscreen);
862bf215546Sopenharmony_cirelease_pipe:
863bf215546Sopenharmony_ci   if (scrn->base.dev) {
864bf215546Sopenharmony_ci      pipe_loader_release(&scrn->base.dev, 1);
865bf215546Sopenharmony_ci      fd = -1;
866bf215546Sopenharmony_ci   }
867bf215546Sopenharmony_ciclose_fd:
868bf215546Sopenharmony_ci   if (fd != -1)
869bf215546Sopenharmony_ci      close(fd);
870bf215546Sopenharmony_cifree_screen:
871bf215546Sopenharmony_ci   FREE(scrn);
872bf215546Sopenharmony_ci   return NULL;
873bf215546Sopenharmony_ci}
874