egl/wayland: resolve quirky try_damage_buffer() implementation
[mesa.git] / src / egl / drivers / dri2 / platform_wayland.c
1 /*
2 * Copyright © 2011-2012 Intel Corporation
3 * Copyright © 2012 Collabora, Ltd.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Kristian Høgsberg <krh@bitplanet.net>
27 * Benjamin Franzke <benjaminfranzke@googlemail.com>
28 */
29
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <dlfcn.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <xf86drm.h>
39 #include <sys/mman.h>
40
41 #include "egl_dri2.h"
42 #include "egl_dri2_fallbacks.h"
43 #include "loader.h"
44
45 #include <wayland-client.h>
46 #include "wayland-drm-client-protocol.h"
47
48 enum wl_drm_format_flags {
49 HAS_ARGB8888 = 1,
50 HAS_XRGB8888 = 2,
51 HAS_RGB565 = 4,
52 };
53
54 static EGLBoolean
55 dri2_wl_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
56 EGLint interval);
57
58 static void
59 sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
60 {
61 int *done = data;
62
63 *done = 1;
64 wl_callback_destroy(callback);
65 }
66
67 static const struct wl_callback_listener sync_listener = {
68 .done = sync_callback
69 };
70
71 static int
72 roundtrip(struct dri2_egl_display *dri2_dpy)
73 {
74 struct wl_callback *callback;
75 int done = 0, ret = 0;
76
77 callback = wl_display_sync(dri2_dpy->wl_dpy_wrapper);
78 wl_callback_add_listener(callback, &sync_listener, &done);
79 while (ret != -1 && !done)
80 ret = wl_display_dispatch_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
81
82 if (!done)
83 wl_callback_destroy(callback);
84
85 return ret;
86 }
87
88 static void
89 wl_buffer_release(void *data, struct wl_buffer *buffer)
90 {
91 struct dri2_egl_surface *dri2_surf = data;
92 int i;
93
94 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); ++i)
95 if (dri2_surf->color_buffers[i].wl_buffer == buffer)
96 break;
97
98 if (i == ARRAY_SIZE(dri2_surf->color_buffers)) {
99 wl_buffer_destroy(buffer);
100 return;
101 }
102
103 dri2_surf->color_buffers[i].locked = 0;
104 }
105
106 static const struct wl_buffer_listener wl_buffer_listener = {
107 .release = wl_buffer_release
108 };
109
110 static void
111 resize_callback(struct wl_egl_window *wl_win, void *data)
112 {
113 struct dri2_egl_surface *dri2_surf = data;
114 struct dri2_egl_display *dri2_dpy =
115 dri2_egl_display(dri2_surf->base.Resource.Display);
116
117 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
118 }
119
120 static void
121 destroy_window_callback(void *data)
122 {
123 struct dri2_egl_surface *dri2_surf = data;
124 dri2_surf->wl_win = NULL;
125 }
126
127 /**
128 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
129 */
130 static _EGLSurface *
131 dri2_wl_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
132 _EGLConfig *conf, void *native_window,
133 const EGLint *attrib_list)
134 {
135 __DRIcreateNewDrawableFunc createNewDrawable;
136 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
137 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
138 struct wl_egl_window *window = native_window;
139 struct dri2_egl_surface *dri2_surf;
140 const __DRIconfig *config;
141
142 dri2_surf = calloc(1, sizeof *dri2_surf);
143 if (!dri2_surf) {
144 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
145 return NULL;
146 }
147
148 if (!_eglInitSurface(&dri2_surf->base, disp, EGL_WINDOW_BIT, conf, attrib_list))
149 goto cleanup_surf;
150
151 if (conf->RedSize == 5)
152 dri2_surf->format = WL_DRM_FORMAT_RGB565;
153 else if (conf->AlphaSize == 0)
154 dri2_surf->format = WL_DRM_FORMAT_XRGB8888;
155 else
156 dri2_surf->format = WL_DRM_FORMAT_ARGB8888;
157
158 if (!window) {
159 _eglError(EGL_BAD_NATIVE_WINDOW, "dri2_create_surface");
160 goto cleanup_surf;
161 }
162
163 dri2_surf->wl_win = window;
164
165 dri2_surf->wl_win->private = dri2_surf;
166 dri2_surf->wl_win->destroy_window_callback = destroy_window_callback;
167
168 dri2_surf->base.Width = -1;
169 dri2_surf->base.Height = -1;
170
171 config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT,
172 dri2_surf->base.GLColorspace);
173
174 if (dri2_dpy->dri2) {
175 dri2_surf->wl_win->resize_callback = resize_callback;
176
177 createNewDrawable = dri2_dpy->dri2->createNewDrawable;
178 } else {
179 createNewDrawable = dri2_dpy->swrast->createNewDrawable;
180 }
181
182 dri2_surf->dri_drawable = (*createNewDrawable)(dri2_dpy->dri_screen, config,
183 dri2_surf);
184 if (dri2_surf->dri_drawable == NULL) {
185 _eglError(EGL_BAD_ALLOC, "createNewDrawable");
186 goto cleanup_surf;
187 }
188
189 dri2_wl_swap_interval(drv, disp, &dri2_surf->base,
190 dri2_dpy->default_swap_interval);
191
192 return &dri2_surf->base;
193
194 cleanup_surf:
195 free(dri2_surf);
196
197 return NULL;
198 }
199
200 static _EGLSurface *
201 dri2_wl_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
202 _EGLConfig *conf, void *native_window,
203 const EGLint *attrib_list)
204 {
205 /* From the EGL_EXT_platform_wayland spec, version 3:
206 *
207 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
208 * that belongs to Wayland. Any such call fails and generates
209 * EGL_BAD_PARAMETER.
210 */
211 _eglError(EGL_BAD_PARAMETER, "cannot create EGL pixmap surfaces on "
212 "Wayland");
213 return NULL;
214 }
215
216 /**
217 * Called via eglDestroySurface(), drv->API.DestroySurface().
218 */
219 static EGLBoolean
220 dri2_wl_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
221 {
222 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
223 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
224 int i;
225
226 (void) drv;
227
228 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
229
230 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
231 if (dri2_surf->color_buffers[i].wl_buffer)
232 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
233 if (dri2_surf->color_buffers[i].dri_image)
234 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
235 if (dri2_surf->color_buffers[i].linear_copy)
236 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy);
237 if (dri2_surf->color_buffers[i].data)
238 munmap(dri2_surf->color_buffers[i].data,
239 dri2_surf->color_buffers[i].data_size);
240 }
241
242 if (dri2_dpy->dri2) {
243 for (i = 0; i < __DRI_BUFFER_COUNT; i++)
244 if (dri2_surf->dri_buffers[i] &&
245 dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT)
246 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
247 dri2_surf->dri_buffers[i]);
248 }
249
250 if (dri2_surf->throttle_callback)
251 wl_callback_destroy(dri2_surf->throttle_callback);
252
253 if (dri2_surf->wl_win) {
254 dri2_surf->wl_win->private = NULL;
255 dri2_surf->wl_win->resize_callback = NULL;
256 dri2_surf->wl_win->destroy_window_callback = NULL;
257 }
258
259 free(surf);
260
261 return EGL_TRUE;
262 }
263
264 static void
265 dri2_wl_release_buffers(struct dri2_egl_surface *dri2_surf)
266 {
267 struct dri2_egl_display *dri2_dpy =
268 dri2_egl_display(dri2_surf->base.Resource.Display);
269 int i;
270
271 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
272 if (dri2_surf->color_buffers[i].wl_buffer &&
273 !dri2_surf->color_buffers[i].locked)
274 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
275 if (dri2_surf->color_buffers[i].dri_image)
276 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
277 if (dri2_surf->color_buffers[i].linear_copy)
278 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy);
279 if (dri2_surf->color_buffers[i].data)
280 munmap(dri2_surf->color_buffers[i].data,
281 dri2_surf->color_buffers[i].data_size);
282
283 dri2_surf->color_buffers[i].wl_buffer = NULL;
284 dri2_surf->color_buffers[i].dri_image = NULL;
285 dri2_surf->color_buffers[i].linear_copy = NULL;
286 dri2_surf->color_buffers[i].data = NULL;
287 dri2_surf->color_buffers[i].locked = 0;
288 }
289
290 if (dri2_dpy->dri2) {
291 for (i = 0; i < __DRI_BUFFER_COUNT; i++)
292 if (dri2_surf->dri_buffers[i] &&
293 dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT)
294 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
295 dri2_surf->dri_buffers[i]);
296 }
297 }
298
299 static int
300 get_back_bo(struct dri2_egl_surface *dri2_surf)
301 {
302 struct dri2_egl_display *dri2_dpy =
303 dri2_egl_display(dri2_surf->base.Resource.Display);
304 int i, use_flags;
305 unsigned int dri_image_format;
306
307 /* currently supports three WL DRM formats,
308 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
309 * and WL_DRM_FORMAT_RGB565
310 */
311 switch (dri2_surf->format) {
312 case WL_DRM_FORMAT_ARGB8888:
313 dri_image_format = __DRI_IMAGE_FORMAT_ARGB8888;
314 break;
315 case WL_DRM_FORMAT_XRGB8888:
316 dri_image_format = __DRI_IMAGE_FORMAT_XRGB8888;
317 break;
318 case WL_DRM_FORMAT_RGB565:
319 dri_image_format = __DRI_IMAGE_FORMAT_RGB565;
320 break;
321 default:
322 /* format is not supported */
323 return -1;
324 }
325
326 /* There might be a buffer release already queued that wasn't processed */
327 wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
328
329 if (dri2_surf->back == NULL) {
330 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
331 /* Get an unlocked buffer, preferrably one with a dri_buffer
332 * already allocated. */
333 if (dri2_surf->color_buffers[i].locked)
334 continue;
335 if (dri2_surf->back == NULL)
336 dri2_surf->back = &dri2_surf->color_buffers[i];
337 else if (dri2_surf->back->dri_image == NULL)
338 dri2_surf->back = &dri2_surf->color_buffers[i];
339 }
340 }
341
342 if (dri2_surf->back == NULL)
343 return -1;
344
345 use_flags = __DRI_IMAGE_USE_SHARE | __DRI_IMAGE_USE_BACKBUFFER;
346
347 if (dri2_dpy->is_different_gpu &&
348 dri2_surf->back->linear_copy == NULL) {
349 dri2_surf->back->linear_copy =
350 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
351 dri2_surf->base.Width,
352 dri2_surf->base.Height,
353 dri_image_format,
354 use_flags |
355 __DRI_IMAGE_USE_LINEAR,
356 NULL);
357 if (dri2_surf->back->linear_copy == NULL)
358 return -1;
359 }
360
361 if (dri2_surf->back->dri_image == NULL) {
362 dri2_surf->back->dri_image =
363 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
364 dri2_surf->base.Width,
365 dri2_surf->base.Height,
366 dri_image_format,
367 dri2_dpy->is_different_gpu ?
368 0 : use_flags,
369 NULL);
370 dri2_surf->back->age = 0;
371 }
372 if (dri2_surf->back->dri_image == NULL)
373 return -1;
374
375 dri2_surf->back->locked = 1;
376
377 return 0;
378 }
379
380
381 static void
382 back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
383 {
384 struct dri2_egl_display *dri2_dpy =
385 dri2_egl_display(dri2_surf->base.Resource.Display);
386 __DRIimage *image;
387 int name, pitch;
388
389 image = dri2_surf->back->dri_image;
390
391 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
392 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
393
394 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
395 buffer->name = name;
396 buffer->pitch = pitch;
397 buffer->cpp = 4;
398 buffer->flags = 0;
399 }
400
401 static int
402 get_aux_bo(struct dri2_egl_surface *dri2_surf,
403 unsigned int attachment, unsigned int format, __DRIbuffer *buffer)
404 {
405 struct dri2_egl_display *dri2_dpy =
406 dri2_egl_display(dri2_surf->base.Resource.Display);
407 __DRIbuffer *b = dri2_surf->dri_buffers[attachment];
408
409 if (b == NULL) {
410 b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
411 attachment, format,
412 dri2_surf->base.Width,
413 dri2_surf->base.Height);
414 dri2_surf->dri_buffers[attachment] = b;
415 }
416 if (b == NULL)
417 return -1;
418
419 memcpy(buffer, b, sizeof *buffer);
420
421 return 0;
422 }
423
424 static int
425 update_buffers(struct dri2_egl_surface *dri2_surf)
426 {
427 struct dri2_egl_display *dri2_dpy =
428 dri2_egl_display(dri2_surf->base.Resource.Display);
429 int i;
430
431 if (dri2_surf->base.Width != dri2_surf->wl_win->width ||
432 dri2_surf->base.Height != dri2_surf->wl_win->height) {
433
434 dri2_wl_release_buffers(dri2_surf);
435
436 dri2_surf->base.Width = dri2_surf->wl_win->width;
437 dri2_surf->base.Height = dri2_surf->wl_win->height;
438 dri2_surf->dx = dri2_surf->wl_win->dx;
439 dri2_surf->dy = dri2_surf->wl_win->dy;
440 }
441
442 if (get_back_bo(dri2_surf) < 0) {
443 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
444 return -1;
445 }
446
447 /* If we have an extra unlocked buffer at this point, we had to do triple
448 * buffering for a while, but now can go back to just double buffering.
449 * That means we can free any unlocked buffer now. */
450 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
451 if (!dri2_surf->color_buffers[i].locked &&
452 dri2_surf->color_buffers[i].wl_buffer) {
453 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
454 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
455 if (dri2_dpy->is_different_gpu)
456 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy);
457 dri2_surf->color_buffers[i].wl_buffer = NULL;
458 dri2_surf->color_buffers[i].dri_image = NULL;
459 dri2_surf->color_buffers[i].linear_copy = NULL;
460 }
461 }
462
463 return 0;
464 }
465
466 static __DRIbuffer *
467 dri2_wl_get_buffers_with_format(__DRIdrawable * driDrawable,
468 int *width, int *height,
469 unsigned int *attachments, int count,
470 int *out_count, void *loaderPrivate)
471 {
472 struct dri2_egl_surface *dri2_surf = loaderPrivate;
473 int i, j;
474
475 if (update_buffers(dri2_surf) < 0)
476 return NULL;
477
478 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
479 switch (attachments[i]) {
480 case __DRI_BUFFER_BACK_LEFT:
481 back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]);
482 break;
483 default:
484 if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1],
485 &dri2_surf->buffers[j]) < 0) {
486 _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer");
487 return NULL;
488 }
489 break;
490 }
491 }
492
493 *out_count = j;
494 if (j == 0)
495 return NULL;
496
497 *width = dri2_surf->base.Width;
498 *height = dri2_surf->base.Height;
499
500 return dri2_surf->buffers;
501 }
502
503 static __DRIbuffer *
504 dri2_wl_get_buffers(__DRIdrawable * driDrawable,
505 int *width, int *height,
506 unsigned int *attachments, int count,
507 int *out_count, void *loaderPrivate)
508 {
509 struct dri2_egl_surface *dri2_surf = loaderPrivate;
510 unsigned int *attachments_with_format;
511 __DRIbuffer *buffer;
512 unsigned int bpp;
513
514 int i;
515
516 switch (dri2_surf->format) {
517 case WL_DRM_FORMAT_ARGB8888:
518 case WL_DRM_FORMAT_XRGB8888:
519 bpp = 32;
520 break;
521 case WL_DRM_FORMAT_RGB565:
522 bpp = 16;
523 break;
524 default:
525 /* format is not supported */
526 return NULL;
527 }
528
529 attachments_with_format = calloc(count, 2 * sizeof(unsigned int));
530 if (!attachments_with_format) {
531 *out_count = 0;
532 return NULL;
533 }
534
535 for (i = 0; i < count; ++i) {
536 attachments_with_format[2*i] = attachments[i];
537 attachments_with_format[2*i + 1] = bpp;
538 }
539
540 buffer =
541 dri2_wl_get_buffers_with_format(driDrawable,
542 width, height,
543 attachments_with_format, count,
544 out_count, loaderPrivate);
545
546 free(attachments_with_format);
547
548 return buffer;
549 }
550
551 static int
552 image_get_buffers(__DRIdrawable *driDrawable,
553 unsigned int format,
554 uint32_t *stamp,
555 void *loaderPrivate,
556 uint32_t buffer_mask,
557 struct __DRIimageList *buffers)
558 {
559 struct dri2_egl_surface *dri2_surf = loaderPrivate;
560
561 if (update_buffers(dri2_surf) < 0)
562 return 0;
563
564 buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
565 buffers->back = dri2_surf->back->dri_image;
566
567 return 1;
568 }
569
570 static void
571 dri2_wl_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
572 {
573 (void) driDrawable;
574 (void) loaderPrivate;
575 }
576
577 static const __DRIdri2LoaderExtension dri2_loader_extension = {
578 .base = { __DRI_DRI2_LOADER, 3 },
579
580 .getBuffers = dri2_wl_get_buffers,
581 .flushFrontBuffer = dri2_wl_flush_front_buffer,
582 .getBuffersWithFormat = dri2_wl_get_buffers_with_format,
583 };
584
585 static const __DRIimageLoaderExtension image_loader_extension = {
586 .base = { __DRI_IMAGE_LOADER, 1 },
587
588 .getBuffers = image_get_buffers,
589 .flushFrontBuffer = dri2_wl_flush_front_buffer,
590 };
591
592 static void
593 wayland_throttle_callback(void *data,
594 struct wl_callback *callback,
595 uint32_t time)
596 {
597 struct dri2_egl_surface *dri2_surf = data;
598
599 dri2_surf->throttle_callback = NULL;
600 wl_callback_destroy(callback);
601 }
602
603 static const struct wl_callback_listener throttle_listener = {
604 .done = wayland_throttle_callback
605 };
606
607 static void
608 create_wl_buffer(struct dri2_egl_surface *dri2_surf)
609 {
610 struct dri2_egl_display *dri2_dpy =
611 dri2_egl_display(dri2_surf->base.Resource.Display);
612 __DRIimage *image;
613 int fd, stride, name;
614
615 if (dri2_surf->current->wl_buffer != NULL)
616 return;
617
618 if (dri2_dpy->is_different_gpu) {
619 image = dri2_surf->current->linear_copy;
620 } else {
621 image = dri2_surf->current->dri_image;
622 }
623 if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
624 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd);
625 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
626
627 dri2_surf->current->wl_buffer =
628 wl_drm_create_prime_buffer(dri2_dpy->wl_drm,
629 fd,
630 dri2_surf->base.Width,
631 dri2_surf->base.Height,
632 dri2_surf->format,
633 0, stride,
634 0, 0,
635 0, 0);
636 close(fd);
637 } else {
638 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
639 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
640
641 dri2_surf->current->wl_buffer =
642 wl_drm_create_buffer(dri2_dpy->wl_drm,
643 name,
644 dri2_surf->base.Width,
645 dri2_surf->base.Height,
646 stride,
647 dri2_surf->format);
648 }
649
650 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->current->wl_buffer,
651 dri2_dpy->wl_queue);
652 wl_buffer_add_listener(dri2_surf->current->wl_buffer,
653 &wl_buffer_listener, dri2_surf);
654 }
655
656 static EGLBoolean
657 try_damage_buffer(struct dri2_egl_surface *dri2_surf,
658 const EGLint *rects,
659 EGLint n_rects)
660 {
661 int i;
662
663 if (wl_proxy_get_version((struct wl_proxy *) dri2_surf->wl_win->surface)
664 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION)
665 return EGL_FALSE;
666
667 for (i = 0; i < n_rects; i++) {
668 const int *rect = &rects[i * 4];
669
670 wl_surface_damage_buffer(dri2_surf->wl_win->surface,
671 rect[0],
672 dri2_surf->base.Height - rect[1] - rect[3],
673 rect[2], rect[3]);
674 }
675 return EGL_TRUE;
676 }
677 /**
678 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
679 */
680 static EGLBoolean
681 dri2_wl_swap_buffers_with_damage(_EGLDriver *drv,
682 _EGLDisplay *disp,
683 _EGLSurface *draw,
684 const EGLint *rects,
685 EGLint n_rects)
686 {
687 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
688 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
689 int i;
690
691 while (dri2_surf->throttle_callback != NULL)
692 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
693 dri2_dpy->wl_queue) == -1)
694 return -1;
695
696 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
697 if (dri2_surf->color_buffers[i].age > 0)
698 dri2_surf->color_buffers[i].age++;
699
700 /* Make sure we have a back buffer in case we're swapping without ever
701 * rendering. */
702 if (get_back_bo(dri2_surf) < 0) {
703 _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
704 return EGL_FALSE;
705 }
706
707 if (draw->SwapInterval > 0) {
708 dri2_surf->throttle_callback =
709 wl_surface_frame(dri2_surf->wl_win->surface);
710 wl_callback_add_listener(dri2_surf->throttle_callback,
711 &throttle_listener, dri2_surf);
712 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->throttle_callback,
713 dri2_dpy->wl_queue);
714 }
715
716 dri2_surf->back->age = 1;
717 dri2_surf->current = dri2_surf->back;
718 dri2_surf->back = NULL;
719
720 create_wl_buffer(dri2_surf);
721
722 wl_surface_attach(dri2_surf->wl_win->surface,
723 dri2_surf->current->wl_buffer,
724 dri2_surf->dx, dri2_surf->dy);
725
726 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
727 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
728 /* reset resize growing parameters */
729 dri2_surf->dx = 0;
730 dri2_surf->dy = 0;
731
732 /* If the compositor doesn't support damage_buffer, we deliberately
733 * ignore the damage region and post maximum damage, due to
734 * https://bugs.freedesktop.org/78190 */
735 if (!n_rects || !try_damage_buffer(dri2_surf, rects, n_rects))
736 wl_surface_damage(dri2_surf->wl_win->surface,
737 0, 0, INT32_MAX, INT32_MAX);
738
739 if (dri2_dpy->is_different_gpu) {
740 _EGLContext *ctx = _eglGetCurrentContext();
741 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
742 dri2_dpy->image->blitImage(dri2_ctx->dri_context,
743 dri2_surf->current->linear_copy,
744 dri2_surf->current->dri_image,
745 0, 0, dri2_surf->base.Width,
746 dri2_surf->base.Height,
747 0, 0, dri2_surf->base.Width,
748 dri2_surf->base.Height, 0);
749 }
750
751 dri2_flush_drawable_for_swapbuffers(disp, draw);
752 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
753
754 wl_surface_commit(dri2_surf->wl_win->surface);
755
756 /* If we're not waiting for a frame callback then we'll at least throttle
757 * to a sync callback so that we always give a chance for the compositor to
758 * handle the commit and send a release event before checking for a free
759 * buffer */
760 if (dri2_surf->throttle_callback == NULL) {
761 dri2_surf->throttle_callback = wl_display_sync(dri2_dpy->wl_dpy_wrapper);
762 wl_callback_add_listener(dri2_surf->throttle_callback,
763 &throttle_listener, dri2_surf);
764 }
765
766 wl_display_flush(dri2_dpy->wl_dpy);
767
768 return EGL_TRUE;
769 }
770
771 static EGLint
772 dri2_wl_query_buffer_age(_EGLDriver *drv,
773 _EGLDisplay *disp, _EGLSurface *surface)
774 {
775 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
776
777 if (get_back_bo(dri2_surf) < 0) {
778 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
779 return 0;
780 }
781
782 return dri2_surf->back->age;
783 }
784
785 static EGLBoolean
786 dri2_wl_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
787 {
788 return dri2_wl_swap_buffers_with_damage (drv, disp, draw, NULL, 0);
789 }
790
791 static struct wl_buffer *
792 dri2_wl_create_wayland_buffer_from_image(_EGLDriver *drv,
793 _EGLDisplay *disp,
794 _EGLImage *img)
795 {
796 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
797 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
798 __DRIimage *image = dri2_img->dri_image;
799 struct wl_buffer *buffer;
800 int width, height, format, pitch;
801 enum wl_drm_format wl_format;
802
803 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &format);
804
805 switch (format) {
806 case __DRI_IMAGE_FORMAT_ARGB8888:
807 if (!(dri2_dpy->formats & HAS_ARGB8888))
808 goto bad_format;
809 wl_format = WL_DRM_FORMAT_ARGB8888;
810 break;
811 case __DRI_IMAGE_FORMAT_XRGB8888:
812 if (!(dri2_dpy->formats & HAS_XRGB8888))
813 goto bad_format;
814 wl_format = WL_DRM_FORMAT_XRGB8888;
815 break;
816 default:
817 goto bad_format;
818 }
819
820 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_WIDTH, &width);
821 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_HEIGHT, &height);
822 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
823
824 if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
825 int fd;
826
827 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd);
828
829 buffer =
830 wl_drm_create_prime_buffer(dri2_dpy->wl_drm,
831 fd,
832 width, height,
833 wl_format,
834 0, pitch,
835 0, 0,
836 0, 0);
837
838 close(fd);
839 } else {
840 int name;
841
842 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
843
844 buffer =
845 wl_drm_create_buffer(dri2_dpy->wl_drm,
846 name,
847 width, height,
848 pitch,
849 wl_format);
850 }
851
852 /* The buffer object will have been created with our internal event queue
853 * because it is using the wl_drm object as a proxy factory. We want the
854 * buffer to be used by the application so we'll reset it to the display's
855 * default event queue */
856 if (buffer)
857 wl_proxy_set_queue((struct wl_proxy *) buffer, NULL);
858
859 return buffer;
860
861 bad_format:
862 _eglError(EGL_BAD_MATCH, "unsupported image format");
863 return NULL;
864 }
865
866 static int
867 dri2_wl_authenticate(_EGLDisplay *disp, uint32_t id)
868 {
869 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
870 int ret = 0;
871
872 if (dri2_dpy->is_render_node) {
873 _eglLog(_EGL_WARNING, "wayland-egl: client asks server to "
874 "authenticate for render-nodes");
875 return 0;
876 }
877 dri2_dpy->authenticated = 0;
878
879 wl_drm_authenticate(dri2_dpy->wl_drm, id);
880 if (roundtrip(dri2_dpy) < 0)
881 ret = -1;
882
883 if (!dri2_dpy->authenticated)
884 ret = -1;
885
886 /* reset authenticated */
887 dri2_dpy->authenticated = 1;
888
889 return ret;
890 }
891
892 static void
893 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
894 {
895 struct dri2_egl_display *dri2_dpy = data;
896 drm_magic_t magic;
897
898 dri2_dpy->device_name = strdup(device);
899 if (!dri2_dpy->device_name)
900 return;
901
902 dri2_dpy->fd = loader_open_device(dri2_dpy->device_name);
903 if (dri2_dpy->fd == -1) {
904 _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
905 dri2_dpy->device_name, strerror(errno));
906 return;
907 }
908
909 if (drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER) {
910 dri2_dpy->authenticated = 1;
911 } else {
912 drmGetMagic(dri2_dpy->fd, &magic);
913 wl_drm_authenticate(dri2_dpy->wl_drm, magic);
914 }
915 }
916
917 static void
918 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
919 {
920 struct dri2_egl_display *dri2_dpy = data;
921
922 switch (format) {
923 case WL_DRM_FORMAT_ARGB8888:
924 dri2_dpy->formats |= HAS_ARGB8888;
925 break;
926 case WL_DRM_FORMAT_XRGB8888:
927 dri2_dpy->formats |= HAS_XRGB8888;
928 break;
929 case WL_DRM_FORMAT_RGB565:
930 dri2_dpy->formats |= HAS_RGB565;
931 break;
932 }
933 }
934
935 static void
936 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
937 {
938 struct dri2_egl_display *dri2_dpy = data;
939
940 dri2_dpy->capabilities = value;
941 }
942
943 static void
944 drm_handle_authenticated(void *data, struct wl_drm *drm)
945 {
946 struct dri2_egl_display *dri2_dpy = data;
947
948 dri2_dpy->authenticated = 1;
949 }
950
951 static const struct wl_drm_listener drm_listener = {
952 .device = drm_handle_device,
953 .format = drm_handle_format,
954 .authenticated = drm_handle_authenticated,
955 .capabilities = drm_handle_capabilities
956 };
957
958 static void
959 registry_handle_global_drm(void *data, struct wl_registry *registry, uint32_t name,
960 const char *interface, uint32_t version)
961 {
962 struct dri2_egl_display *dri2_dpy = data;
963
964 if (version > 1)
965 version = 2;
966 if (strcmp(interface, "wl_drm") == 0) {
967 dri2_dpy->wl_drm =
968 wl_registry_bind(registry, name, &wl_drm_interface, version);
969 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
970 }
971 }
972
973 static void
974 registry_handle_global_remove(void *data, struct wl_registry *registry,
975 uint32_t name)
976 {
977 }
978
979 static const struct wl_registry_listener registry_listener_drm = {
980 .global = registry_handle_global_drm,
981 .global_remove = registry_handle_global_remove
982 };
983
984 static EGLBoolean
985 dri2_wl_swap_interval(_EGLDriver *drv,
986 _EGLDisplay *disp,
987 _EGLSurface *surf,
988 EGLint interval)
989 {
990 if (interval > surf->Config->MaxSwapInterval)
991 interval = surf->Config->MaxSwapInterval;
992 else if (interval < surf->Config->MinSwapInterval)
993 interval = surf->Config->MinSwapInterval;
994
995 surf->SwapInterval = interval;
996
997 return EGL_TRUE;
998 }
999
1000 static void
1001 dri2_wl_setup_swap_interval(struct dri2_egl_display *dri2_dpy)
1002 {
1003 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
1004
1005 /* We can't use values greater than 1 on Wayland because we are using the
1006 * frame callback to synchronise the frame and the only way we be sure to
1007 * get a frame callback is to attach a new buffer. Therefore we can't just
1008 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1009
1010 if (dri2_dpy->config)
1011 dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
1012 "vblank_mode", &vblank_mode);
1013 switch (vblank_mode) {
1014 case DRI_CONF_VBLANK_NEVER:
1015 dri2_dpy->min_swap_interval = 0;
1016 dri2_dpy->max_swap_interval = 0;
1017 dri2_dpy->default_swap_interval = 0;
1018 break;
1019 case DRI_CONF_VBLANK_ALWAYS_SYNC:
1020 dri2_dpy->min_swap_interval = 1;
1021 dri2_dpy->max_swap_interval = 1;
1022 dri2_dpy->default_swap_interval = 1;
1023 break;
1024 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
1025 dri2_dpy->min_swap_interval = 0;
1026 dri2_dpy->max_swap_interval = 1;
1027 dri2_dpy->default_swap_interval = 0;
1028 break;
1029 default:
1030 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
1031 dri2_dpy->min_swap_interval = 0;
1032 dri2_dpy->max_swap_interval = 1;
1033 dri2_dpy->default_swap_interval = 1;
1034 break;
1035 }
1036 }
1037
1038 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl = {
1039 .authenticate = dri2_wl_authenticate,
1040 .create_window_surface = dri2_wl_create_window_surface,
1041 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1042 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
1043 .destroy_surface = dri2_wl_destroy_surface,
1044 .create_image = dri2_create_image_khr,
1045 .swap_interval = dri2_wl_swap_interval,
1046 .swap_buffers = dri2_wl_swap_buffers,
1047 .swap_buffers_with_damage = dri2_wl_swap_buffers_with_damage,
1048 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1049 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1050 .copy_buffers = dri2_fallback_copy_buffers,
1051 .query_buffer_age = dri2_wl_query_buffer_age,
1052 .create_wayland_buffer_from_image = dri2_wl_create_wayland_buffer_from_image,
1053 .get_sync_values = dri2_fallback_get_sync_values,
1054 .get_dri_drawable = dri2_surface_get_dri_drawable,
1055 };
1056
1057 static const __DRIextension *dri2_loader_extensions[] = {
1058 &dri2_loader_extension.base,
1059 &image_loader_extension.base,
1060 &image_lookup_extension.base,
1061 &use_invalidate.base,
1062 NULL,
1063 };
1064
1065 static const __DRIextension *image_loader_extensions[] = {
1066 &image_loader_extension.base,
1067 &image_lookup_extension.base,
1068 &use_invalidate.base,
1069 NULL,
1070 };
1071
1072 static EGLBoolean
1073 dri2_wl_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
1074 {
1075 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1076 static const struct {
1077 const char *format_name;
1078 int has_format;
1079 unsigned int rgba_masks[4];
1080 } visuals[] = {
1081 { "XRGB8888", HAS_XRGB8888, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1082 { "ARGB8888", HAS_ARGB8888, { 0xff0000, 0xff00, 0x00ff, 0 } },
1083 { "RGB565", HAS_RGB565, { 0x00f800, 0x07e0, 0x001f, 0 } },
1084 };
1085 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
1086 unsigned int count, i, j;
1087
1088 count = 0;
1089 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
1090 for (j = 0; j < ARRAY_SIZE(visuals); j++) {
1091 struct dri2_egl_config *dri2_conf;
1092
1093 if (!(dri2_dpy->formats & visuals[j].has_format))
1094 continue;
1095
1096 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
1097 count + 1, EGL_WINDOW_BIT, NULL, visuals[j].rgba_masks);
1098 if (dri2_conf) {
1099 count++;
1100 format_count[j]++;
1101 }
1102 }
1103 }
1104
1105 for (i = 0; i < ARRAY_SIZE(format_count); i++) {
1106 if (!format_count[i]) {
1107 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
1108 visuals[i].format_name);
1109 }
1110 }
1111
1112 return (count != 0);
1113 }
1114
1115 static EGLBoolean
1116 dri2_initialize_wayland_drm(_EGLDriver *drv, _EGLDisplay *disp)
1117 {
1118 struct dri2_egl_display *dri2_dpy;
1119
1120 loader_set_logger(_eglLog);
1121
1122 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1123 if (!dri2_dpy)
1124 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1125
1126 disp->DriverData = (void *) dri2_dpy;
1127 if (disp->PlatformDisplay == NULL) {
1128 dri2_dpy->wl_dpy = wl_display_connect(NULL);
1129 if (dri2_dpy->wl_dpy == NULL)
1130 goto cleanup_dpy;
1131 dri2_dpy->own_device = 1;
1132 } else {
1133 dri2_dpy->wl_dpy = disp->PlatformDisplay;
1134 }
1135
1136 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
1137
1138 dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
1139 if (dri2_dpy->wl_dpy_wrapper == NULL)
1140 goto cleanup_dpy_wrapper;
1141
1142 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper,
1143 dri2_dpy->wl_queue);
1144
1145 if (dri2_dpy->own_device)
1146 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1147
1148 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
1149 wl_registry_add_listener(dri2_dpy->wl_registry,
1150 &registry_listener_drm, dri2_dpy);
1151 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
1152 goto cleanup_registry;
1153
1154 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
1155 goto cleanup_drm;
1156
1157 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
1158 goto cleanup_fd;
1159
1160 dri2_dpy->fd = loader_get_user_preferred_fd(dri2_dpy->fd,
1161 &dri2_dpy->is_different_gpu);
1162 if (dri2_dpy->is_different_gpu) {
1163 free(dri2_dpy->device_name);
1164 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
1165 if (!dri2_dpy->device_name) {
1166 _eglError(EGL_BAD_ALLOC, "wayland-egl: failed to get device name "
1167 "for requested GPU");
1168 goto cleanup_fd;
1169 }
1170 }
1171
1172 /* we have to do the check now, because loader_get_user_preferred_fd
1173 * will return a render-node when the requested gpu is different
1174 * to the server, but also if the client asks for the same gpu than
1175 * the server by requesting its pci-id */
1176 dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
1177
1178 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
1179 if (dri2_dpy->driver_name == NULL) {
1180 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
1181 goto cleanup_fd;
1182 }
1183
1184 if (!dri2_load_driver(disp))
1185 goto cleanup_driver_name;
1186
1187 /* render nodes cannot use Gem names, and thus do not support
1188 * the __DRI_DRI2_LOADER extension */
1189 if (!dri2_dpy->is_render_node)
1190 dri2_dpy->loader_extensions = dri2_loader_extensions;
1191 else
1192 dri2_dpy->loader_extensions = image_loader_extensions;
1193
1194 if (!dri2_create_screen(disp))
1195 goto cleanup_driver;
1196
1197 dri2_wl_setup_swap_interval(dri2_dpy);
1198
1199 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1200 * createImageFromFds support indicates that Prime export/import
1201 * is supported by the driver. Fall back to
1202 * gem names if we don't have Prime support. */
1203
1204 if (dri2_dpy->image->base.version < 7 ||
1205 dri2_dpy->image->createImageFromFds == NULL)
1206 dri2_dpy->capabilities &= ~WL_DRM_CAPABILITY_PRIME;
1207
1208 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1209 * The server needs to accept them */
1210 if (dri2_dpy->is_render_node &&
1211 !(dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME)) {
1212 _eglLog(_EGL_WARNING, "wayland-egl: display is not render-node capable");
1213 goto cleanup_screen;
1214 }
1215
1216 if (dri2_dpy->is_different_gpu &&
1217 (dri2_dpy->image->base.version < 9 ||
1218 dri2_dpy->image->blitImage == NULL)) {
1219 _eglLog(_EGL_WARNING, "wayland-egl: Different GPU selected, but the "
1220 "Image extension in the driver is not "
1221 "compatible. Version 9 or later and blitImage() "
1222 "are required");
1223 goto cleanup_screen;
1224 }
1225
1226 if (!dri2_wl_add_configs_for_visuals(drv, disp)) {
1227 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs");
1228 goto cleanup_screen;
1229 }
1230
1231 dri2_set_WL_bind_wayland_display(drv, disp);
1232 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1233 * because the buffer of the EGLImage has likely a tiling mode the server
1234 * gpu won't support. These is no way to check for now. Thus do not support the
1235 * extension */
1236 if (!dri2_dpy->is_different_gpu) {
1237 disp->Extensions.WL_create_wayland_buffer_from_image = EGL_TRUE;
1238 } else {
1239 dri2_wl_display_vtbl.create_wayland_buffer_from_image =
1240 dri2_fallback_create_wayland_buffer_from_image;
1241 }
1242 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1243
1244 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
1245
1246 /* Fill vtbl last to prevent accidentally calling virtual function during
1247 * initialization.
1248 */
1249 dri2_dpy->vtbl = &dri2_wl_display_vtbl;
1250
1251 return EGL_TRUE;
1252
1253 cleanup_screen:
1254 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1255 cleanup_driver:
1256 dlclose(dri2_dpy->driver);
1257 cleanup_driver_name:
1258 free(dri2_dpy->driver_name);
1259 cleanup_fd:
1260 close(dri2_dpy->fd);
1261 cleanup_drm:
1262 free(dri2_dpy->device_name);
1263 wl_drm_destroy(dri2_dpy->wl_drm);
1264 cleanup_registry:
1265 wl_registry_destroy(dri2_dpy->wl_registry);
1266 wl_proxy_wrapper_destroy(dri2_dpy->wl_dpy_wrapper);
1267 cleanup_dpy_wrapper:
1268 wl_event_queue_destroy(dri2_dpy->wl_queue);
1269 if (disp->PlatformDisplay == NULL)
1270 wl_display_disconnect(dri2_dpy->wl_dpy);
1271 cleanup_dpy:
1272 free(dri2_dpy);
1273 disp->DriverData = NULL;
1274
1275 return EGL_FALSE;
1276 }
1277
1278 static int
1279 dri2_wl_swrast_get_stride_for_format(int format, int w)
1280 {
1281 if (format == WL_SHM_FORMAT_RGB565)
1282 return 2 * w;
1283 else /* ARGB8888 || XRGB8888 */
1284 return 4 * w;
1285 }
1286
1287 /*
1288 * Taken from weston shared/os-compatibility.c
1289 */
1290
1291 #ifndef HAVE_MKOSTEMP
1292
1293 static int
1294 set_cloexec_or_close(int fd)
1295 {
1296 long flags;
1297
1298 if (fd == -1)
1299 return -1;
1300
1301 flags = fcntl(fd, F_GETFD);
1302 if (flags == -1)
1303 goto err;
1304
1305 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
1306 goto err;
1307
1308 return fd;
1309
1310 err:
1311 close(fd);
1312 return -1;
1313 }
1314
1315 #endif
1316
1317 /*
1318 * Taken from weston shared/os-compatibility.c
1319 */
1320
1321 static int
1322 create_tmpfile_cloexec(char *tmpname)
1323 {
1324 int fd;
1325
1326 #ifdef HAVE_MKOSTEMP
1327 fd = mkostemp(tmpname, O_CLOEXEC);
1328 if (fd >= 0)
1329 unlink(tmpname);
1330 #else
1331 fd = mkstemp(tmpname);
1332 if (fd >= 0) {
1333 fd = set_cloexec_or_close(fd);
1334 unlink(tmpname);
1335 }
1336 #endif
1337
1338 return fd;
1339 }
1340
1341 /*
1342 * Taken from weston shared/os-compatibility.c
1343 *
1344 * Create a new, unique, anonymous file of the given size, and
1345 * return the file descriptor for it. The file descriptor is set
1346 * CLOEXEC. The file is immediately suitable for mmap()'ing
1347 * the given size at offset zero.
1348 *
1349 * The file should not have a permanent backing store like a disk,
1350 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1351 *
1352 * The file name is deleted from the file system.
1353 *
1354 * The file is suitable for buffer sharing between processes by
1355 * transmitting the file descriptor over Unix sockets using the
1356 * SCM_RIGHTS methods.
1357 *
1358 * If the C library implements posix_fallocate(), it is used to
1359 * guarantee that disk space is available for the file at the
1360 * given size. If disk space is insufficent, errno is set to ENOSPC.
1361 * If posix_fallocate() is not supported, program may receive
1362 * SIGBUS on accessing mmap()'ed file contents instead.
1363 */
1364 static int
1365 os_create_anonymous_file(off_t size)
1366 {
1367 static const char template[] = "/mesa-shared-XXXXXX";
1368 const char *path;
1369 char *name;
1370 int fd;
1371 int ret;
1372
1373 path = getenv("XDG_RUNTIME_DIR");
1374 if (!path) {
1375 errno = ENOENT;
1376 return -1;
1377 }
1378
1379 name = malloc(strlen(path) + sizeof(template));
1380 if (!name)
1381 return -1;
1382
1383 strcpy(name, path);
1384 strcat(name, template);
1385
1386 fd = create_tmpfile_cloexec(name);
1387
1388 free(name);
1389
1390 if (fd < 0)
1391 return -1;
1392
1393 ret = ftruncate(fd, size);
1394 if (ret < 0) {
1395 close(fd);
1396 return -1;
1397 }
1398
1399 return fd;
1400 }
1401
1402
1403 static EGLBoolean
1404 dri2_wl_swrast_allocate_buffer(struct dri2_egl_display *dri2_dpy,
1405 int format, int w, int h,
1406 void **data, int *size,
1407 struct wl_buffer **buffer)
1408 {
1409 struct wl_shm_pool *pool;
1410 int fd, stride, size_map;
1411 void *data_map;
1412
1413 stride = dri2_wl_swrast_get_stride_for_format(format, w);
1414 size_map = h * stride;
1415
1416 /* Create a sharable buffer */
1417 fd = os_create_anonymous_file(size_map);
1418 if (fd < 0)
1419 return EGL_FALSE;
1420
1421 data_map = mmap(NULL, size_map, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1422 if (data_map == MAP_FAILED) {
1423 close(fd);
1424 return EGL_FALSE;
1425 }
1426
1427 /* Share it in a wl_buffer */
1428 pool = wl_shm_create_pool(dri2_dpy->wl_shm, fd, size_map);
1429 *buffer = wl_shm_pool_create_buffer(pool, 0, w, h, stride, format);
1430 wl_shm_pool_destroy(pool);
1431 close(fd);
1432
1433 *data = data_map;
1434 *size = size_map;
1435 return EGL_TRUE;
1436 }
1437
1438 static int
1439 swrast_update_buffers(struct dri2_egl_surface *dri2_surf)
1440 {
1441 struct dri2_egl_display *dri2_dpy =
1442 dri2_egl_display(dri2_surf->base.Resource.Display);
1443 int i;
1444
1445 /* we need to do the following operations only once per frame */
1446 if (dri2_surf->back)
1447 return 0;
1448
1449 if (dri2_surf->base.Width != dri2_surf->wl_win->width ||
1450 dri2_surf->base.Height != dri2_surf->wl_win->height) {
1451
1452 dri2_wl_release_buffers(dri2_surf);
1453
1454 dri2_surf->base.Width = dri2_surf->wl_win->width;
1455 dri2_surf->base.Height = dri2_surf->wl_win->height;
1456 dri2_surf->dx = dri2_surf->wl_win->dx;
1457 dri2_surf->dy = dri2_surf->wl_win->dy;
1458 dri2_surf->current = NULL;
1459 }
1460
1461 /* find back buffer */
1462
1463 /* There might be a buffer release already queued that wasn't processed */
1464 wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
1465
1466 /* try get free buffer already created */
1467 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1468 if (!dri2_surf->color_buffers[i].locked &&
1469 dri2_surf->color_buffers[i].wl_buffer) {
1470 dri2_surf->back = &dri2_surf->color_buffers[i];
1471 break;
1472 }
1473 }
1474
1475 /* else choose any another free location */
1476 if (!dri2_surf->back) {
1477 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1478 if (!dri2_surf->color_buffers[i].locked) {
1479 dri2_surf->back = &dri2_surf->color_buffers[i];
1480 if (!dri2_wl_swrast_allocate_buffer(dri2_dpy,
1481 dri2_surf->format,
1482 dri2_surf->base.Width,
1483 dri2_surf->base.Height,
1484 &dri2_surf->back->data,
1485 &dri2_surf->back->data_size,
1486 &dri2_surf->back->wl_buffer)) {
1487 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
1488 return -1;
1489 }
1490 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->back->wl_buffer,
1491 dri2_dpy->wl_queue);
1492 wl_buffer_add_listener(dri2_surf->back->wl_buffer,
1493 &wl_buffer_listener, dri2_surf);
1494 break;
1495 }
1496 }
1497 }
1498
1499 if (!dri2_surf->back) {
1500 _eglError(EGL_BAD_ALLOC, "failed to find free buffer");
1501 return -1;
1502 }
1503
1504 dri2_surf->back->locked = 1;
1505
1506 /* If we have an extra unlocked buffer at this point, we had to do triple
1507 * buffering for a while, but now can go back to just double buffering.
1508 * That means we can free any unlocked buffer now. */
1509 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1510 if (!dri2_surf->color_buffers[i].locked &&
1511 dri2_surf->color_buffers[i].wl_buffer) {
1512 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
1513 munmap(dri2_surf->color_buffers[i].data,
1514 dri2_surf->color_buffers[i].data_size);
1515 dri2_surf->color_buffers[i].wl_buffer = NULL;
1516 dri2_surf->color_buffers[i].data = NULL;
1517 }
1518 }
1519
1520 return 0;
1521 }
1522
1523 static void*
1524 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface *dri2_surf)
1525 {
1526 /* if there has been a resize: */
1527 if (!dri2_surf->current)
1528 return NULL;
1529
1530 return dri2_surf->current->data;
1531 }
1532
1533 static void*
1534 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface *dri2_surf)
1535 {
1536 assert(dri2_surf->back);
1537 return dri2_surf->back->data;
1538 }
1539
1540 static void
1541 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface *dri2_surf)
1542 {
1543 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
1544
1545 while (dri2_surf->throttle_callback != NULL)
1546 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
1547 dri2_dpy->wl_queue) == -1)
1548 return;
1549
1550 if (dri2_surf->base.SwapInterval > 0) {
1551 dri2_surf->throttle_callback =
1552 wl_surface_frame(dri2_surf->wl_win->surface);
1553 wl_callback_add_listener(dri2_surf->throttle_callback,
1554 &throttle_listener, dri2_surf);
1555 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->throttle_callback,
1556 dri2_dpy->wl_queue);
1557 }
1558
1559 dri2_surf->current = dri2_surf->back;
1560 dri2_surf->back = NULL;
1561
1562 wl_surface_attach(dri2_surf->wl_win->surface,
1563 dri2_surf->current->wl_buffer,
1564 dri2_surf->dx, dri2_surf->dy);
1565
1566 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
1567 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
1568 /* reset resize growing parameters */
1569 dri2_surf->dx = 0;
1570 dri2_surf->dy = 0;
1571
1572 wl_surface_damage(dri2_surf->wl_win->surface,
1573 0, 0, INT32_MAX, INT32_MAX);
1574 wl_surface_commit(dri2_surf->wl_win->surface);
1575
1576 /* If we're not waiting for a frame callback then we'll at least throttle
1577 * to a sync callback so that we always give a chance for the compositor to
1578 * handle the commit and send a release event before checking for a free
1579 * buffer */
1580 if (dri2_surf->throttle_callback == NULL) {
1581 dri2_surf->throttle_callback = wl_display_sync(dri2_dpy->wl_dpy_wrapper);
1582 wl_callback_add_listener(dri2_surf->throttle_callback,
1583 &throttle_listener, dri2_surf);
1584 }
1585
1586 wl_display_flush(dri2_dpy->wl_dpy);
1587 }
1588
1589 static void
1590 dri2_wl_swrast_get_drawable_info(__DRIdrawable * draw,
1591 int *x, int *y, int *w, int *h,
1592 void *loaderPrivate)
1593 {
1594 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1595
1596 (void) swrast_update_buffers(dri2_surf);
1597 *x = 0;
1598 *y = 0;
1599 *w = dri2_surf->base.Width;
1600 *h = dri2_surf->base.Height;
1601 }
1602
1603 static void
1604 dri2_wl_swrast_get_image(__DRIdrawable * read,
1605 int x, int y, int w, int h,
1606 char *data, void *loaderPrivate)
1607 {
1608 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1609 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1610 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1611 int src_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1612 int dst_stride = copy_width;
1613 char *src, *dst;
1614
1615 src = dri2_wl_swrast_get_frontbuffer_data(dri2_surf);
1616 if (!src) {
1617 memset(data, 0, copy_width * h);
1618 return;
1619 }
1620
1621 assert(data != src);
1622 assert(copy_width <= src_stride);
1623
1624 src += x_offset;
1625 src += y * src_stride;
1626 dst = data;
1627
1628 if (copy_width > src_stride-x_offset)
1629 copy_width = src_stride-x_offset;
1630 if (h > dri2_surf->base.Height-y)
1631 h = dri2_surf->base.Height-y;
1632
1633 for (; h>0; h--) {
1634 memcpy(dst, src, copy_width);
1635 src += src_stride;
1636 dst += dst_stride;
1637 }
1638 }
1639
1640 static void
1641 dri2_wl_swrast_put_image2(__DRIdrawable * draw, int op,
1642 int x, int y, int w, int h, int stride,
1643 char *data, void *loaderPrivate)
1644 {
1645 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1646 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1647 int dst_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1648 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1649 char *src, *dst;
1650
1651 assert(copy_width <= stride);
1652
1653 (void) swrast_update_buffers(dri2_surf);
1654 dst = dri2_wl_swrast_get_backbuffer_data(dri2_surf);
1655
1656 /* partial copy, copy old content */
1657 if (copy_width < dst_stride)
1658 dri2_wl_swrast_get_image(draw, 0, 0,
1659 dri2_surf->base.Width, dri2_surf->base.Height,
1660 dst, loaderPrivate);
1661
1662 dst += x_offset;
1663 dst += y * dst_stride;
1664
1665 src = data;
1666
1667 /* drivers expect we do these checks (and some rely on it) */
1668 if (copy_width > dst_stride-x_offset)
1669 copy_width = dst_stride-x_offset;
1670 if (h > dri2_surf->base.Height-y)
1671 h = dri2_surf->base.Height-y;
1672
1673 for (; h>0; h--) {
1674 memcpy(dst, src, copy_width);
1675 src += stride;
1676 dst += dst_stride;
1677 }
1678 dri2_wl_swrast_commit_backbuffer(dri2_surf);
1679 }
1680
1681 static void
1682 dri2_wl_swrast_put_image(__DRIdrawable * draw, int op,
1683 int x, int y, int w, int h,
1684 char *data, void *loaderPrivate)
1685 {
1686 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1687 int stride;
1688
1689 stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1690 dri2_wl_swrast_put_image2(draw, op, x, y, w, h,
1691 stride, data, loaderPrivate);
1692 }
1693
1694 static EGLBoolean
1695 dri2_wl_swrast_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1696 {
1697 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1698 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1699
1700 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
1701 return EGL_TRUE;
1702 }
1703
1704 static void
1705 shm_handle_format(void *data, struct wl_shm *shm, uint32_t format)
1706 {
1707 struct dri2_egl_display *dri2_dpy = data;
1708
1709 switch (format) {
1710 case WL_SHM_FORMAT_ARGB8888:
1711 dri2_dpy->formats |= HAS_ARGB8888;
1712 break;
1713 case WL_SHM_FORMAT_XRGB8888:
1714 dri2_dpy->formats |= HAS_XRGB8888;
1715 break;
1716 case WL_SHM_FORMAT_RGB565:
1717 dri2_dpy->formats |= HAS_RGB565;
1718 break;
1719 }
1720 }
1721
1722 static const struct wl_shm_listener shm_listener = {
1723 .format = shm_handle_format
1724 };
1725
1726 static void
1727 registry_handle_global_swrast(void *data, struct wl_registry *registry, uint32_t name,
1728 const char *interface, uint32_t version)
1729 {
1730 struct dri2_egl_display *dri2_dpy = data;
1731
1732 if (strcmp(interface, "wl_shm") == 0) {
1733 dri2_dpy->wl_shm =
1734 wl_registry_bind(registry, name, &wl_shm_interface, 1);
1735 wl_shm_add_listener(dri2_dpy->wl_shm, &shm_listener, dri2_dpy);
1736 }
1737 }
1738
1739 static const struct wl_registry_listener registry_listener_swrast = {
1740 .global = registry_handle_global_swrast,
1741 .global_remove = registry_handle_global_remove
1742 };
1743
1744 static struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl = {
1745 .authenticate = NULL,
1746 .create_window_surface = dri2_wl_create_window_surface,
1747 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1748 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
1749 .destroy_surface = dri2_wl_destroy_surface,
1750 .create_image = dri2_fallback_create_image_khr,
1751 .swap_interval = dri2_wl_swap_interval,
1752 .swap_buffers = dri2_wl_swrast_swap_buffers,
1753 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
1754 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1755 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1756 .copy_buffers = dri2_fallback_copy_buffers,
1757 .query_buffer_age = dri2_fallback_query_buffer_age,
1758 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1759 .get_sync_values = dri2_fallback_get_sync_values,
1760 .get_dri_drawable = dri2_surface_get_dri_drawable,
1761 };
1762
1763 static const __DRIswrastLoaderExtension swrast_loader_extension = {
1764 .base = { __DRI_SWRAST_LOADER, 2 },
1765
1766 .getDrawableInfo = dri2_wl_swrast_get_drawable_info,
1767 .putImage = dri2_wl_swrast_put_image,
1768 .getImage = dri2_wl_swrast_get_image,
1769 .putImage2 = dri2_wl_swrast_put_image2,
1770 };
1771
1772 static const __DRIextension *swrast_loader_extensions[] = {
1773 &swrast_loader_extension.base,
1774 NULL,
1775 };
1776
1777 static EGLBoolean
1778 dri2_initialize_wayland_swrast(_EGLDriver *drv, _EGLDisplay *disp)
1779 {
1780 struct dri2_egl_display *dri2_dpy;
1781
1782 loader_set_logger(_eglLog);
1783
1784 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1785 if (!dri2_dpy)
1786 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1787
1788 disp->DriverData = (void *) dri2_dpy;
1789 if (disp->PlatformDisplay == NULL) {
1790 dri2_dpy->wl_dpy = wl_display_connect(NULL);
1791 if (dri2_dpy->wl_dpy == NULL)
1792 goto cleanup_dpy;
1793 dri2_dpy->own_device = 1;
1794 } else {
1795 dri2_dpy->wl_dpy = disp->PlatformDisplay;
1796 }
1797
1798 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
1799
1800 dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
1801 if (dri2_dpy->wl_dpy_wrapper == NULL)
1802 goto cleanup_dpy_wrapper;
1803
1804 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper,
1805 dri2_dpy->wl_queue);
1806
1807 if (dri2_dpy->own_device)
1808 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1809
1810 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
1811 wl_registry_add_listener(dri2_dpy->wl_registry,
1812 &registry_listener_swrast, dri2_dpy);
1813
1814 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_shm == NULL)
1815 goto cleanup_registry;
1816
1817 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->formats == 0)
1818 goto cleanup_shm;
1819
1820 dri2_dpy->fd = -1;
1821 dri2_dpy->driver_name = strdup("swrast");
1822 if (!dri2_load_driver_swrast(disp))
1823 goto cleanup_shm;
1824
1825 dri2_dpy->loader_extensions = swrast_loader_extensions;
1826
1827 if (!dri2_create_screen(disp))
1828 goto cleanup_driver;
1829
1830 dri2_wl_setup_swap_interval(dri2_dpy);
1831
1832 if (!dri2_wl_add_configs_for_visuals(drv, disp)) {
1833 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs");
1834 goto cleanup_screen;
1835 }
1836
1837 /* Fill vtbl last to prevent accidentally calling virtual function during
1838 * initialization.
1839 */
1840 dri2_dpy->vtbl = &dri2_wl_swrast_display_vtbl;
1841
1842 return EGL_TRUE;
1843
1844 cleanup_screen:
1845 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1846 cleanup_driver:
1847 dlclose(dri2_dpy->driver);
1848 cleanup_shm:
1849 wl_shm_destroy(dri2_dpy->wl_shm);
1850 cleanup_registry:
1851 wl_registry_destroy(dri2_dpy->wl_registry);
1852 wl_proxy_wrapper_destroy(dri2_dpy->wl_dpy_wrapper);
1853 cleanup_dpy_wrapper:
1854 wl_event_queue_destroy(dri2_dpy->wl_queue);
1855 if (disp->PlatformDisplay == NULL)
1856 wl_display_disconnect(dri2_dpy->wl_dpy);
1857 cleanup_dpy:
1858 free(dri2_dpy);
1859 disp->DriverData = NULL;
1860
1861 return EGL_FALSE;
1862 }
1863
1864 EGLBoolean
1865 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
1866 {
1867 EGLBoolean initialized = EGL_TRUE;
1868
1869 int hw_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);
1870
1871 if (hw_accel) {
1872 if (!dri2_initialize_wayland_drm(drv, disp)) {
1873 initialized = dri2_initialize_wayland_swrast(drv, disp);
1874 }
1875 } else {
1876 initialized = dri2_initialize_wayland_swrast(drv, disp);
1877 }
1878
1879 return initialized;
1880
1881 }