blob: 8fb562d65e3aec5d3472c43a311003089c2cfc35 [file] [log] [blame]
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001/*
2 * Copyright © 2012-2013 Raspberry Pi Foundation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
24
Pekka Paalanend7265bc2013-05-22 18:03:06 +030025#include <stdlib.h>
26#include <assert.h>
27#include <string.h>
28
Pekka Paalanend7265bc2013-05-22 18:03:06 +030029#ifdef HAVE_BCM_HOST
30# include <bcm_host.h>
31#else
32# include "rpi-bcm-stubs.h"
33#endif
34
35#include "compositor.h"
36#include "rpi-renderer.h"
37
Tomeu Vizosob4659eb2013-10-07 11:02:20 +020038#ifdef ENABLE_EGL
39#include <EGL/egl.h>
40#include <EGL/eglext.h>
41#include "weston-egl-ext.h"
42#endif
43
Pekka Paalanend7265bc2013-05-22 18:03:06 +030044/*
45 * Dispmanx API offers alpha-blended overlays for hardware compositing.
46 * The final composite consists of dispmanx elements, and their contents:
47 * the dispmanx resource assigned to the element. The elements may be
48 * scanned out directly, or composited to a temporary surface, depending on
49 * how the firmware decides to handle the scene. Updates to multiple elements
50 * may be queued in a single dispmanx update object, resulting in atomic and
51 * vblank synchronized display updates.
52 *
53 * To avoid tearing and display artifacts, the current dispmanx resource in a
54 * dispmanx element must not be touched. Therefore each element must be
55 * double-buffered, using two resources, the front and the back. While a
56 * dispmanx update is running, the both resources must be considered in use.
57 *
58 * A resource may be destroyed only, when the update removing the element has
59 * completed. Otherwise you risk showing an incomplete composition.
60 */
61
62#ifndef ELEMENT_CHANGE_LAYER
63/* copied from interface/vmcs_host/vc_vchi_dispmanx.h of userland.git */
64#define ELEMENT_CHANGE_LAYER (1<<0)
65#define ELEMENT_CHANGE_OPACITY (1<<1)
66#define ELEMENT_CHANGE_DEST_RECT (1<<2)
67#define ELEMENT_CHANGE_SRC_RECT (1<<3)
68#define ELEMENT_CHANGE_MASK_RESOURCE (1<<4)
69#define ELEMENT_CHANGE_TRANSFORM (1<<5)
70#endif
71
72#if 0
73#define DBG(...) \
74 weston_log(__VA_ARGS__)
75#else
76#define DBG(...) do {} while (0)
77#endif
78
79/* If we had a fully featured vc_dispmanx_resource_write_data()... */
80/*#define HAVE_RESOURCE_WRITE_DATA_RECT 1*/
81
82struct rpi_resource {
83 DISPMANX_RESOURCE_HANDLE_T handle;
84 int width;
85 int height; /* height of the image (valid pixel data) */
86 int stride; /* bytes */
87 int buffer_height; /* height of the buffer */
88 VC_IMAGE_TYPE_T ifmt;
89};
90
91struct rpir_output;
92
Tomeu Vizosob4659eb2013-10-07 11:02:20 +020093struct rpir_egl_buffer {
94 struct weston_buffer_reference buffer_ref;
95 DISPMANX_RESOURCE_HANDLE_T resource_handle;
96};
97
98enum buffer_type {
99 BUFFER_TYPE_NULL,
100 BUFFER_TYPE_SHM,
101 BUFFER_TYPE_EGL
102};
103
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300104struct rpir_surface {
105 struct weston_surface *surface;
106
Jason Ekstranda7af7042013-10-12 22:38:11 -0500107 struct wl_list views;
108 int visible_views;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300109 int need_swap;
110 int single_buffer;
111
112 struct rpi_resource resources[2];
113 struct rpi_resource *front;
114 struct rpi_resource *back;
115 pixman_region32_t prev_damage;
116
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200117 struct rpir_egl_buffer *egl_front;
118 struct rpir_egl_buffer *egl_back;
119 struct rpir_egl_buffer *egl_old_front;
120
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300121 struct weston_buffer_reference buffer_ref;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200122 enum buffer_type buffer_type;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300123
124 struct wl_listener surface_destroy_listener;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300125};
126
Jason Ekstranda7af7042013-10-12 22:38:11 -0500127struct rpir_view {
128 struct rpir_surface *surface;
129 struct wl_list surface_link;
130 struct weston_view *view;
131
132 /* If link is empty, the view is guaranteed to not be on screen,
133 * i.e. updates removing Elements have completed.
134 */
135 struct wl_list link;
136
137 DISPMANX_ELEMENT_HANDLE_T handle;
138 int layer;
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +0100139
140 struct wl_listener view_destroy_listener;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500141};
142
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300143struct rpir_output {
144 DISPMANX_DISPLAY_HANDLE_T display;
145
146 DISPMANX_UPDATE_HANDLE_T update;
147 struct weston_matrix matrix;
148
149 /* all Elements currently on screen */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500150 struct wl_list view_list; /* struct rpir_surface::link */
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300151
152 /* Elements just removed, waiting for update completion */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500153 struct wl_list view_cleanup_list; /* struct rpir_surface::link */
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300154
155 struct rpi_resource capture_buffer;
156 uint8_t *capture_data;
157};
158
159struct rpi_renderer {
160 struct weston_renderer base;
161
162 int single_buffer;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200163
164#ifdef ENABLE_EGL
165 EGLDisplay egl_display;
166
167 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
168 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
169 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
170#endif
171 int has_bind_display;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300172};
173
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300174static int
175rpi_renderer_create_surface(struct weston_surface *base);
176
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +0100177static int
178rpi_renderer_create_view(struct weston_view *base);
179
180static void
181rpir_view_handle_view_destroy(struct wl_listener *listener, void *data);
182
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300183static inline struct rpir_surface *
184to_rpir_surface(struct weston_surface *surface)
185{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300186 if (!surface->renderer_state)
187 rpi_renderer_create_surface(surface);
188
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300189 return surface->renderer_state;
190}
191
Jason Ekstranda7af7042013-10-12 22:38:11 -0500192static inline struct rpir_view *
193to_rpir_view(struct weston_view *view)
194{
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +0100195 if (!view->renderer_state)
196 rpi_renderer_create_view(view);
197
Jason Ekstranda7af7042013-10-12 22:38:11 -0500198 return view->renderer_state;
199}
200
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300201static inline struct rpir_output *
202to_rpir_output(struct weston_output *output)
203{
204 return output->renderer_state;
205}
206
207static inline struct rpi_renderer *
208to_rpi_renderer(struct weston_compositor *compositor)
209{
210 return container_of(compositor->renderer, struct rpi_renderer, base);
211}
212
213static inline int
214int_max(int a, int b)
215{
216 return a > b ? a : b;
217}
218
219static inline void
220int_swap(int *a, int *b)
221{
222 int tmp = *a;
223 *a = *b;
224 *b = tmp;
225}
226
227static uint8_t
228float2uint8(float f)
229{
230 int v = roundf(f * 255.0f);
231
232 return v < 0 ? 0 : (v > 255 ? 255 : v);
233}
234
235static void
236rpi_resource_init(struct rpi_resource *resource)
237{
238 resource->handle = DISPMANX_NO_HANDLE;
239}
240
241static void
242rpi_resource_release(struct rpi_resource *resource)
243{
244 if (resource->handle == DISPMANX_NO_HANDLE)
245 return;
246
247 vc_dispmanx_resource_delete(resource->handle);
248 DBG("resource %p release\n", resource);
249 resource->handle = DISPMANX_NO_HANDLE;
250}
251
252static int
253rpi_resource_realloc(struct rpi_resource *resource, VC_IMAGE_TYPE_T ifmt,
254 int width, int height, int stride, int buffer_height)
255{
256 uint32_t dummy;
257
258 if (resource->handle != DISPMANX_NO_HANDLE &&
259 resource->width == width &&
260 resource->height == height &&
261 resource->stride == stride &&
262 resource->buffer_height == buffer_height &&
263 resource->ifmt == ifmt)
264 return 0;
265
266 rpi_resource_release(resource);
267
268 /* NOTE: if stride is not a multiple of 16 pixels in bytes,
269 * the vc_image_* functions may break. Dispmanx elements
270 * should be fine, though. Buffer_height probably has similar
271 * constraints, too.
272 */
273 resource->handle =
274 vc_dispmanx_resource_create(ifmt,
275 width | (stride << 16),
276 height | (buffer_height << 16),
277 &dummy);
278 if (resource->handle == DISPMANX_NO_HANDLE)
279 return -1;
280
281 resource->width = width;
282 resource->height = height;
283 resource->stride = stride;
284 resource->buffer_height = buffer_height;
285 resource->ifmt = ifmt;
286 DBG("resource %p alloc\n", resource);
287 return 1;
288}
289
290/* A firmware workaround for broken ALPHA_PREMULT + ALPHA_MIX hardware. */
291#define PREMULT_ALPHA_FLAG (1 << 31)
292
293static VC_IMAGE_TYPE_T
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500294shm_buffer_get_vc_format(struct wl_shm_buffer *buffer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300295{
296 switch (wl_shm_buffer_get_format(buffer)) {
297 case WL_SHM_FORMAT_XRGB8888:
298 return VC_IMAGE_XRGB8888;
299 case WL_SHM_FORMAT_ARGB8888:
300 return VC_IMAGE_ARGB8888 | PREMULT_ALPHA_FLAG;
Tomeu Vizoso03681892013-08-06 20:05:57 +0200301 case WL_SHM_FORMAT_RGB565:
302 return VC_IMAGE_RGB565;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300303 default:
304 /* invalid format */
305 return VC_IMAGE_MIN;
306 }
307}
308
309static int
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500310rpi_resource_update(struct rpi_resource *resource, struct weston_buffer *buffer,
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300311 pixman_region32_t *region)
312{
313 pixman_region32_t write_region;
314 pixman_box32_t *r;
315 VC_RECT_T rect;
316 VC_IMAGE_TYPE_T ifmt;
317 uint32_t *pixels;
318 int width;
319 int height;
320 int stride;
321 int ret;
322#ifdef HAVE_RESOURCE_WRITE_DATA_RECT
323 int n;
324#endif
325
326 if (!buffer)
327 return -1;
328
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500329 ifmt = shm_buffer_get_vc_format(buffer->shm_buffer);
330 width = wl_shm_buffer_get_width(buffer->shm_buffer);
331 height = wl_shm_buffer_get_height(buffer->shm_buffer);
332 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
333 pixels = wl_shm_buffer_get_data(buffer->shm_buffer);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300334
335 ret = rpi_resource_realloc(resource, ifmt & ~PREMULT_ALPHA_FLAG,
336 width, height, stride, height);
337 if (ret < 0)
338 return -1;
339
340 pixman_region32_init_rect(&write_region, 0, 0, width, height);
341 if (ret == 0)
342 pixman_region32_intersect(&write_region,
343 &write_region, region);
344
345#ifdef HAVE_RESOURCE_WRITE_DATA_RECT
346 /* XXX: Can this do a format conversion, so that scanout does not have to? */
347 r = pixman_region32_rectangles(&write_region, &n);
348 while (n--) {
349 vc_dispmanx_rect_set(&rect, r[n].x1, r[n].y1,
350 r[n].x2 - r[n].x1, r[n].y2 - r[n].y1);
351
352 ret = vc_dispmanx_resource_write_data_rect(resource->handle,
353 ifmt, stride,
354 pixels, &rect,
355 rect.x, rect.y);
356 DBG("%s: %p %ux%u@%u,%u, ret %d\n", __func__, resource,
357 rect.width, rect.height, rect.x, rect.y, ret);
358 if (ret)
359 break;
360 }
361#else
362 /* vc_dispmanx_resource_write_data() ignores ifmt,
363 * rect.x, rect.width, and uses stride only for computing
364 * the size of the transfer as rect.height * stride.
365 * Therefore we can only write rows starting at x=0.
366 * To be able to write more than one scanline at a time,
367 * the resource must have been created with the same stride
368 * as used here, and we must write full scanlines.
369 */
370
371 r = pixman_region32_extents(&write_region);
372 vc_dispmanx_rect_set(&rect, 0, r->y1, width, r->y2 - r->y1);
373 ret = vc_dispmanx_resource_write_data(resource->handle,
374 ifmt, stride, pixels, &rect);
375 DBG("%s: %p %ux%u@%u,%u, ret %d\n", __func__, resource,
376 width, r->y2 - r->y1, 0, r->y1, ret);
377#endif
378
379 pixman_region32_fini(&write_region);
380
381 return ret ? -1 : 0;
382}
383
Tomeu Vizoso74987582013-10-25 10:34:38 +0200384static void
385rpir_egl_buffer_destroy(struct rpir_egl_buffer *egl_buffer)
386{
387 struct weston_buffer *buffer;
388
389 if (egl_buffer == NULL)
390 return;
391
392 buffer = egl_buffer->buffer_ref.buffer;
393 if (buffer == NULL) {
394 /* The client has already destroyed the wl_buffer, the
395 * compositor has the responsibility to delete the resource.
396 */
397 vc_dispmanx_resource_delete(egl_buffer->resource_handle);
398 } else {
399 vc_dispmanx_set_wl_buffer_in_use(buffer->resource, 0);
400 weston_buffer_reference(&egl_buffer->buffer_ref, NULL);
401 }
402
403 free(egl_buffer);
404}
405
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300406static struct rpir_surface *
407rpir_surface_create(struct rpi_renderer *renderer)
408{
409 struct rpir_surface *surface;
410
411 surface = calloc(1, sizeof *surface);
412 if (!surface)
413 return NULL;
414
Tomeu Vizoso5c3ea3b2013-10-24 15:38:30 +0200415 wl_list_init(&surface->views);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500416 surface->visible_views = 0;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300417 surface->single_buffer = renderer->single_buffer;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300418 rpi_resource_init(&surface->resources[0]);
419 rpi_resource_init(&surface->resources[1]);
420 surface->front = &surface->resources[0];
421 if (surface->single_buffer)
422 surface->back = &surface->resources[0];
423 else
424 surface->back = &surface->resources[1];
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200425 surface->buffer_type = BUFFER_TYPE_NULL;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300426
427 pixman_region32_init(&surface->prev_damage);
428
429 return surface;
430}
431
432static void
433rpir_surface_destroy(struct rpir_surface *surface)
434{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500435 if (surface->visible_views)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300436 weston_log("ERROR rpi: destroying on-screen element\n");
437
Jason Ekstranda7af7042013-10-12 22:38:11 -0500438 assert(wl_list_empty(&surface->views));
439
440 if (surface->surface)
441 surface->surface->renderer_state = NULL;
442
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300443 pixman_region32_fini(&surface->prev_damage);
444 rpi_resource_release(&surface->resources[0]);
445 rpi_resource_release(&surface->resources[1]);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500446 DBG("rpir_surface %p destroyed (%u)\n", surface, surface->visible_views);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300447
Tomeu Vizoso74987582013-10-25 10:34:38 +0200448 rpir_egl_buffer_destroy(surface->egl_back);
449 rpir_egl_buffer_destroy(surface->egl_front);
450 rpir_egl_buffer_destroy(surface->egl_old_front);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200451
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300452 free(surface);
453}
454
455static int
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500456rpir_surface_damage(struct rpir_surface *surface, struct weston_buffer *buffer,
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300457 pixman_region32_t *damage)
458{
459 pixman_region32_t upload;
460 int ret;
461
462 if (!pixman_region32_not_empty(damage))
463 return 0;
464
465 DBG("rpir_surface %p update resource %p\n", surface, surface->back);
466
467 /* XXX: todo: if no surface->handle, update front buffer directly
468 * to avoid creating a new back buffer */
469 if (surface->single_buffer) {
470 ret = rpi_resource_update(surface->front, buffer, damage);
471 } else {
472 pixman_region32_init(&upload);
473 pixman_region32_union(&upload, &surface->prev_damage, damage);
474 ret = rpi_resource_update(surface->back, buffer, &upload);
475 pixman_region32_fini(&upload);
476 }
477
478 pixman_region32_copy(&surface->prev_damage, damage);
479 surface->need_swap = 1;
480
481 return ret;
482}
483
Jason Ekstranda7af7042013-10-12 22:38:11 -0500484static struct rpir_view *
485rpir_view_create(struct rpir_surface *surface)
486{
487 struct rpir_view *view;
488
489 view = calloc(1, sizeof *view);
490 if (!view)
491 return NULL;
492
493 view->surface = surface;
494 wl_list_insert(&surface->views, &view->surface_link);
495
496 wl_list_init(&view->link);
497 view->handle = DISPMANX_NO_HANDLE;
498
499 return view;
500}
501
502static void
503rpir_view_destroy(struct rpir_view *view)
504{
505 wl_list_remove(&view->link);
506
507 if (view->handle != DISPMANX_NO_HANDLE) {
508 view->surface->visible_views--;
509 weston_log("ERROR rpi: destroying on-screen element\n");
510 }
511
512 if (view->view)
513 view->view->renderer_state = NULL;
514
515 wl_list_remove(&view->surface_link);
516 if (wl_list_empty(&view->surface->views) && view->surface->surface == NULL)
517 rpir_surface_destroy(view->surface);
518
519 DBG("rpir_view %p destroyed (%d)\n", view, view->handle);
520
521 free(view);
522}
523
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300524static void
525matrix_type_str(struct weston_matrix *matrix, char *buf, int len)
526{
527 static const char types[33] = "TSRO";
528 unsigned mask = matrix->type;
529 int i = 0;
530
531 while (mask && i < len - 1) {
532 if (mask & (1u << i))
533 *buf++ = types[i];
534 mask &= ~(1u << i);
535 i++;
536 }
537 *buf = '\0';
538}
539
540static void
541log_print_matrix(struct weston_matrix *matrix)
542{
543 char typestr[6];
544 float *d = matrix->d;
545
546 matrix_type_str(matrix, typestr, sizeof typestr);
547 weston_log_continue("%14.6e %14.6e %14.6e %14.6e\n",
548 d[0], d[4], d[8], d[12]);
549 weston_log_continue("%14.6e %14.6e %14.6e %14.6e\n",
550 d[1], d[5], d[9], d[13]);
551 weston_log_continue("%14.6e %14.6e %14.6e %14.6e\n",
552 d[2], d[6], d[10], d[14]);
553 weston_log_continue("%14.6e %14.6e %14.6e %14.6e type: %s\n",
554 d[3], d[7], d[11], d[15], typestr);
555}
556
557static void
558warn_bad_matrix(struct weston_matrix *total, struct weston_matrix *output,
559 struct weston_matrix *surface)
560{
561 static int n_warn;
562 char typestr[6];
563
564 if (n_warn++ == 10)
565 weston_log("%s: not showing more warnings\n", __func__);
566
567 if (n_warn > 10)
568 return;
569
570 weston_log("%s: warning: total transformation is not renderable:\n",
571 __func__);
572 log_print_matrix(total);
573
574 matrix_type_str(surface, typestr, sizeof typestr);
575 weston_log_continue("surface matrix type: %s\n", typestr);
576 matrix_type_str(output, typestr, sizeof typestr);
577 weston_log_continue("output matrix type: %s\n", typestr);
578}
579
580/*#define SURFACE_TRANSFORM */
581
582static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500583rpir_view_compute_rects(struct rpir_view *view,
584 VC_RECT_T *src_rect, VC_RECT_T *dst_rect,
585 VC_IMAGE_TRANSFORM_T *flipmask)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300586{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500587 struct weston_output *output_base = view->view->surface->output;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300588 struct rpir_output *output = to_rpir_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500589 struct weston_matrix matrix = view->view->transform.matrix;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300590 VC_IMAGE_TRANSFORM_T flipt = 0;
591 int src_x, src_y;
592 int dst_x, dst_y;
593 int src_width, src_height;
594 int dst_width, dst_height;
595 struct weston_vector p1 = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
596 struct weston_vector p2 = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
597 int t;
598 int over;
599
600 /* XXX: take buffer transform into account */
601
602 /* src is in 16.16, dst is in 32.0 fixed point.
603 * Negative values are not allowed in VC_RECT_T.
604 * Clip size to output boundaries, firmware ignores
605 * huge elements like 8192x8192.
606 */
607
608 src_x = 0 << 16;
609 src_y = 0 << 16;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200610
Jason Ekstranda7af7042013-10-12 22:38:11 -0500611 if (view->surface->buffer_type == BUFFER_TYPE_EGL) {
612 struct weston_buffer *buffer =
613 view->surface->egl_front->buffer_ref.buffer;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200614
615 src_width = buffer->width << 16;
616 src_height = buffer->height << 16;
617 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500618 src_width = view->surface->front->width << 16;
619 src_height = view->surface->front->height << 16;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200620 }
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300621
622 weston_matrix_multiply(&matrix, &output->matrix);
623
624#ifdef SURFACE_TRANSFORM
625 if (matrix.type >= WESTON_MATRIX_TRANSFORM_OTHER) {
626#else
627 if (matrix.type >= WESTON_MATRIX_TRANSFORM_ROTATE) {
628#endif
629 warn_bad_matrix(&matrix, &output->matrix,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500630 &view->view->transform.matrix);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300631 } else {
632 if (matrix.type & WESTON_MATRIX_TRANSFORM_ROTATE) {
633 if (fabsf(matrix.d[0]) < 1e-4f &&
634 fabsf(matrix.d[5]) < 1e-4f) {
635 flipt |= TRANSFORM_TRANSPOSE;
636 } else if (fabsf(matrix.d[1]) < 1e-4 &&
637 fabsf(matrix.d[4]) < 1e-4) {
638 /* no transpose */
639 } else {
640 warn_bad_matrix(&matrix, &output->matrix,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500641 &view->view->transform.matrix);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300642 }
643 }
644 }
645
Jason Ekstranda7af7042013-10-12 22:38:11 -0500646 p2.f[0] = view->view->geometry.width;
647 p2.f[1] = view->view->geometry.height;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300648
649 /* transform top-left and bot-right corner into screen coordinates */
650 weston_matrix_transform(&matrix, &p1);
651 weston_matrix_transform(&matrix, &p2);
652
653 /* Compute the destination rectangle on screen, converting
654 * negative dimensions to flips.
655 */
656
657 dst_width = round(p2.f[0] - p1.f[0]);
658 if (dst_width < 0) {
659 dst_x = round(p2.f[0]);
660 dst_width = -dst_width;
661
662 if (!(flipt & TRANSFORM_TRANSPOSE))
663 flipt |= TRANSFORM_HFLIP;
664 else
665 flipt |= TRANSFORM_VFLIP;
666 } else {
667 dst_x = round(p1.f[0]);
668 }
669
670 dst_height = round(p2.f[1] - p1.f[1]);
671 if (dst_height < 0) {
672 dst_y = round(p2.f[1]);
673 dst_height = -dst_height;
674
675 if (!(flipt & TRANSFORM_TRANSPOSE))
676 flipt |= TRANSFORM_VFLIP;
677 else
678 flipt |= TRANSFORM_HFLIP;
679 } else {
680 dst_y = round(p1.f[1]);
681 }
682
683 if (dst_width == 0 || dst_height == 0) {
684 DBG("ignored, zero surface area before clipping\n");
685 return -1;
686 }
687
688#ifdef SURFACE_TRANSFORM
689 /* Dispmanx works as if you flipped the whole screen, when
690 * you flip an element. But, we want to flip an element in place.
691 * XXX: fixme
692 */
693 if (flipt & TRANSFORM_HFLIP)
694 dst_x = output_base->width - dst_x;
695 if (flipt & TRANSFORM_VFLIP)
696 dst_y = output_base->height - dst_y;
697 if (flipt & TRANSFORM_TRANSPOSE) {
698 int_swap(&dst_x, &dst_y);
699 int_swap(&dst_width, &dst_height);
700 }
701#else
702 switch (output_base->transform) {
703 case WL_OUTPUT_TRANSFORM_FLIPPED:
704 flipt = TRANSFORM_HFLIP;
705 break;
706 case WL_OUTPUT_TRANSFORM_NORMAL:
707 flipt = 0;
708 break;
709
710 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
711 flipt = TRANSFORM_HFLIP | TRANSFORM_VFLIP | TRANSFORM_TRANSPOSE;
712 break;
713 case WL_OUTPUT_TRANSFORM_90:
714 flipt = TRANSFORM_VFLIP | TRANSFORM_TRANSPOSE;
715 break;
716 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
717 flipt = TRANSFORM_VFLIP;
718 break;
719 case WL_OUTPUT_TRANSFORM_180:
720 flipt = TRANSFORM_HFLIP | TRANSFORM_VFLIP;
721 break;
722
723 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
724 flipt = TRANSFORM_TRANSPOSE;
725 break;
726 case WL_OUTPUT_TRANSFORM_270:
727 flipt = TRANSFORM_HFLIP | TRANSFORM_TRANSPOSE;
728 break;
729 default:
730 break;
731 }
732#endif
733
734 /* clip destination rectangle to screen dimensions */
735
736 if (dst_x < 0) {
737 t = (int64_t)dst_x * src_width / dst_width;
738 src_width += t;
739 dst_width += dst_x;
740 src_x -= t;
741 dst_x = 0;
742 }
743
744 if (dst_y < 0) {
745 t = (int64_t)dst_y * src_height / dst_height;
746 src_height += t;
747 dst_height += dst_y;
748 src_y -= t;
749 dst_y = 0;
750 }
751
752 over = dst_x + dst_width - output_base->width;
753 if (over > 0) {
754 t = (int64_t)over * src_width / dst_width;
755 src_width -= t;
756 dst_width -= over;
757 }
758
759 over = dst_y + dst_height - output_base->height;
760 if (over > 0) {
761 t = (int64_t)over * src_height / dst_height;
762 src_height -= t;
763 dst_height -= over;
764 }
765
766 src_width = int_max(src_width, 0);
767 src_height = int_max(src_height, 0);
768
Jason Ekstranda7af7042013-10-12 22:38:11 -0500769 DBG("rpir_view %p %dx%d: p1 %f, %f; p2 %f, %f\n", view,
770 view->view->geometry.width,
771 view->view->geometry.height,
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300772 p1.f[0], p1.f[1], p2.f[0], p2.f[1]);
773 DBG("src rect %d;%d, %d;%d, %d;%dx%d;%d\n",
774 src_x >> 16, src_x & 0xffff,
775 src_y >> 16, src_y & 0xffff,
776 src_width >> 16, src_width & 0xffff,
777 src_height >> 16, src_height & 0xffff);
778 DBG("dest rect %d, %d, %dx%d%s%s%s\n",
779 dst_x, dst_y, dst_width, dst_height,
780 (flipt & TRANSFORM_HFLIP) ? " hflip" : "",
781 (flipt & TRANSFORM_VFLIP) ? " vflip" : "",
782 (flipt & TRANSFORM_TRANSPOSE) ? " transp" : "");
783
784 assert(src_x >= 0);
785 assert(src_y >= 0);
786 assert(dst_x >= 0);
787 assert(dst_y >= 0);
788
789 if (dst_width < 1 || dst_height < 1) {
790 DBG("ignored, zero surface area after clipping\n");
791 return -1;
792 }
793
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200794 /* EGL buffers will be upside-down related to what DispmanX expects */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500795 if (view->surface->buffer_type == BUFFER_TYPE_EGL)
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200796 flipt ^= TRANSFORM_VFLIP;
797
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300798 vc_dispmanx_rect_set(src_rect, src_x, src_y, src_width, src_height);
799 vc_dispmanx_rect_set(dst_rect, dst_x, dst_y, dst_width, dst_height);
800 *flipmask = flipt;
801
802 return 0;
803}
804
805static DISPMANX_TRANSFORM_T
806vc_image2dispmanx_transform(VC_IMAGE_TRANSFORM_T t)
807{
808 /* XXX: uhh, are these right? */
809 switch (t) {
810 case VC_IMAGE_ROT0:
811 return DISPMANX_NO_ROTATE;
812 case VC_IMAGE_MIRROR_ROT0:
813 return DISPMANX_FLIP_HRIZ;
814 case VC_IMAGE_MIRROR_ROT180:
815 return DISPMANX_FLIP_VERT;
816 case VC_IMAGE_ROT180:
817 return DISPMANX_ROTATE_180;
818 case VC_IMAGE_MIRROR_ROT90:
819 return DISPMANX_ROTATE_90 | DISPMANX_FLIP_HRIZ;
820 case VC_IMAGE_ROT270:
821 return DISPMANX_ROTATE_270;
822 case VC_IMAGE_ROT90:
823 return DISPMANX_ROTATE_90;
824 case VC_IMAGE_MIRROR_ROT270:
825 return DISPMANX_ROTATE_270 | DISPMANX_FLIP_VERT;
826 default:
827 assert(0 && "bad VC_IMAGE_TRANSFORM_T");
828 return DISPMANX_NO_ROTATE;
829 }
830}
831
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200832
833static DISPMANX_RESOURCE_HANDLE_T
834rpir_surface_get_resource(struct rpir_surface *surface)
835{
836 switch (surface->buffer_type) {
837 case BUFFER_TYPE_SHM:
838 case BUFFER_TYPE_NULL:
839 return surface->front->handle;
840 case BUFFER_TYPE_EGL:
841 if (surface->egl_front != NULL)
842 return surface->egl_front->resource_handle;
843 default:
844 return DISPMANX_NO_HANDLE;
845 }
846}
847
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300848static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500849rpir_view_dmx_add(struct rpir_view *view, struct rpir_output *output,
850 DISPMANX_UPDATE_HANDLE_T update, int layer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300851{
852 /* Do not use DISPMANX_FLAGS_ALPHA_PREMULT here.
853 * If you define PREMULT and ALPHA_MIX, the hardware will not
854 * multiply the source color with the element alpha, leading to
855 * bad colors. Instead, we define PREMULT during pixel data upload.
856 */
857 VC_DISPMANX_ALPHA_T alphasetup = {
858 DISPMANX_FLAGS_ALPHA_FROM_SOURCE |
859 DISPMANX_FLAGS_ALPHA_MIX,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500860 float2uint8(view->view->alpha), /* opacity 0-255 */
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300861 0 /* mask resource handle */
862 };
863 VC_RECT_T dst_rect;
864 VC_RECT_T src_rect;
865 VC_IMAGE_TRANSFORM_T flipmask;
866 int ret;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200867 DISPMANX_RESOURCE_HANDLE_T resource_handle;
868
Jason Ekstranda7af7042013-10-12 22:38:11 -0500869 resource_handle = rpir_surface_get_resource(view->surface);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200870 if (resource_handle == DISPMANX_NO_HANDLE) {
871 weston_log("%s: no buffer yet, aborting\n", __func__);
872 return 0;
873 }
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300874
Jason Ekstranda7af7042013-10-12 22:38:11 -0500875 ret = rpir_view_compute_rects(view, &src_rect, &dst_rect, &flipmask);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300876 if (ret < 0)
877 return 0;
878
Jason Ekstranda7af7042013-10-12 22:38:11 -0500879 view->handle = vc_dispmanx_element_add(
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300880 update,
881 output->display,
882 layer,
883 &dst_rect,
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200884 resource_handle,
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300885 &src_rect,
886 DISPMANX_PROTECTION_NONE,
887 &alphasetup,
888 NULL /* clamp */,
889 vc_image2dispmanx_transform(flipmask));
Jason Ekstranda7af7042013-10-12 22:38:11 -0500890 DBG("rpir_surface %p add %u, alpha %f resource %d\n", view,
891 view->handle, view->view->alpha, resource_handle);
892
893 if (view->handle == DISPMANX_NO_HANDLE)
894 return -1;
895
896 view->surface->visible_views++;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300897
898 return 1;
899}
900
901static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500902rpir_view_dmx_swap(struct rpir_view *view,
903 DISPMANX_UPDATE_HANDLE_T update)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300904{
905 VC_RECT_T rect;
906 pixman_box32_t *r;
907
908 /* XXX: skip, iff resource was not reallocated, and single-buffering */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500909 vc_dispmanx_element_change_source(update, view->handle,
910 view->surface->front->handle);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300911
912 /* This is current damage now, after rpir_surface_damage() */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500913 r = pixman_region32_extents(&view->surface->prev_damage);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300914
915 vc_dispmanx_rect_set(&rect, r->x1, r->y1,
916 r->x2 - r->x1, r->y2 - r->y1);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500917 vc_dispmanx_element_modified(update, view->handle, &rect);
918 DBG("rpir_view %p swap\n", view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300919}
920
921static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500922rpir_view_dmx_move(struct rpir_view *view,
923 DISPMANX_UPDATE_HANDLE_T update, int layer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300924{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500925 uint8_t alpha = float2uint8(view->view->alpha);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300926 VC_RECT_T dst_rect;
927 VC_RECT_T src_rect;
928 VC_IMAGE_TRANSFORM_T flipmask;
929 int ret;
930
931 /* XXX: return early, if all attributes stay the same */
932
Jason Ekstranda7af7042013-10-12 22:38:11 -0500933 if (view->surface->buffer_type == BUFFER_TYPE_EGL) {
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200934 DISPMANX_RESOURCE_HANDLE_T resource_handle;
935
Jason Ekstranda7af7042013-10-12 22:38:11 -0500936 resource_handle = rpir_surface_get_resource(view->surface);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200937 if (resource_handle == DISPMANX_NO_HANDLE) {
938 weston_log("%s: no buffer yet, aborting\n", __func__);
939 return 0;
940 }
941
942 vc_dispmanx_element_change_source(update,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500943 view->handle,
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200944 resource_handle);
945 }
946
Jason Ekstranda7af7042013-10-12 22:38:11 -0500947 ret = rpir_view_compute_rects(view, &src_rect, &dst_rect, &flipmask);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300948 if (ret < 0)
949 return 0;
950
951 ret = vc_dispmanx_element_change_attributes(
952 update,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500953 view->handle,
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300954 ELEMENT_CHANGE_LAYER |
955 ELEMENT_CHANGE_OPACITY |
956 ELEMENT_CHANGE_TRANSFORM |
957 ELEMENT_CHANGE_DEST_RECT |
958 ELEMENT_CHANGE_SRC_RECT,
959 layer,
960 alpha,
961 &dst_rect,
962 &src_rect,
963 DISPMANX_NO_HANDLE,
964 /* This really is DISPMANX_TRANSFORM_T, no matter
965 * what the header says. */
966 vc_image2dispmanx_transform(flipmask));
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 DBG("rpir_view %p move\n", view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300968
969 if (ret)
970 return -1;
971
972 return 1;
973}
974
975static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976rpir_view_dmx_remove(struct rpir_view *view,
977 DISPMANX_UPDATE_HANDLE_T update)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300978{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500979 if (view->handle == DISPMANX_NO_HANDLE)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300980 return;
981
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982 vc_dispmanx_element_remove(update, view->handle);
983 DBG("rpir_view %p remove %u\n", view, view->handle);
984 view->handle = DISPMANX_NO_HANDLE;
985 view->surface->visible_views--;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300986}
987
988static void
989rpir_surface_swap_pointers(struct rpir_surface *surface)
990{
991 struct rpi_resource *tmp;
992
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200993 if (surface->buffer_type == BUFFER_TYPE_EGL) {
994 if (surface->egl_back != NULL) {
995 assert(surface->egl_old_front == NULL);
996 surface->egl_old_front = surface->egl_front;
997 surface->egl_front = surface->egl_back;
998 surface->egl_back = NULL;
Tomeu Vizoso15767812013-10-24 15:38:31 +0200999 DBG("new front %d\n", surface->egl_front->resource_handle);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001000 }
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001001 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001002 tmp = surface->front;
1003 surface->front = surface->back;
1004 surface->back = tmp;
Tomeu Vizoso15767812013-10-24 15:38:31 +02001005 DBG("new back %p, new front %p\n", surface->back, surface->front);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001006 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001007}
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001008
Jason Ekstranda7af7042013-10-12 22:38:11 -05001009static int
1010is_view_not_visible(struct weston_view *view)
1011{
1012 /* Return true, if surface is guaranteed to be totally obscured. */
1013 int ret;
1014 pixman_region32_t unocc;
1015
1016 pixman_region32_init(&unocc);
1017 pixman_region32_subtract(&unocc, &view->transform.boundingbox,
1018 &view->clip);
1019 ret = !pixman_region32_not_empty(&unocc);
1020 pixman_region32_fini(&unocc);
1021
1022 return ret;
1023}
1024
1025static void
1026rpir_view_update(struct rpir_view *view, struct rpir_output *output,
1027 DISPMANX_UPDATE_HANDLE_T update, int layer)
1028{
1029 int ret;
1030 int obscured;
1031
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 obscured = is_view_not_visible(view->view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001033 if (obscured) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001034 DBG("rpir_view %p totally obscured.\n", view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001035
Jason Ekstranda7af7042013-10-12 22:38:11 -05001036 wl_list_remove(&view->link);
1037 if (view->handle == DISPMANX_NO_HANDLE) {
1038 wl_list_init(&view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001039 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001040 rpir_view_dmx_remove(view, update);
1041 wl_list_insert(&output->view_cleanup_list,
1042 &view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001043 }
1044
1045 goto out;
1046 }
1047
Jason Ekstranda7af7042013-10-12 22:38:11 -05001048 if (view->handle == DISPMANX_NO_HANDLE) {
1049 ret = rpir_view_dmx_add(view, output, update, layer);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001050 if (ret == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001051 wl_list_remove(&view->link);
1052 wl_list_init(&view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001053 } else if (ret < 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001054 weston_log("ERROR rpir_view_dmx_add() failed.\n");
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001055 }
1056 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001057 if (view->surface->need_swap)
1058 rpir_view_dmx_swap(view, update);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001059
Jason Ekstranda7af7042013-10-12 22:38:11 -05001060 ret = rpir_view_dmx_move(view, update, layer);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001061 if (ret == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001062 rpir_view_dmx_remove(view, update);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001063
Jason Ekstranda7af7042013-10-12 22:38:11 -05001064 wl_list_remove(&view->link);
1065 wl_list_insert(&output->view_cleanup_list,
1066 &view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001067 } else if (ret < 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001068 weston_log("ERROR rpir_view_dmx_move() failed.\n");
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001069 }
1070 }
1071
1072out:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001073 view->layer = layer;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001074}
1075
1076static int
1077rpi_renderer_read_pixels(struct weston_output *base,
1078 pixman_format_code_t format, void *pixels,
1079 uint32_t x, uint32_t y,
1080 uint32_t width, uint32_t height)
1081{
1082 struct rpir_output *output = to_rpir_output(base);
1083 struct rpi_resource *buffer = &output->capture_buffer;
1084 VC_RECT_T rect;
1085 uint32_t fb_width, fb_height;
1086 uint32_t dst_pitch;
1087 uint32_t i;
1088 int ret;
1089
Hardeningff39efa2013-09-18 23:56:35 +02001090 fb_width = base->current_mode->width;
1091 fb_height = base->current_mode->height;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001092
1093 DBG("%s(%u, %u, %u, %u), resource %p\n", __func__,
1094 x, y, width, height, buffer);
1095
1096 if (format != PIXMAN_a8r8g8b8) {
1097 weston_log("rpi-renderer error: bad read_format\n");
1098 return -1;
1099 }
1100
1101 dst_pitch = fb_width * 4;
1102
1103 if (buffer->handle == DISPMANX_NO_HANDLE) {
1104 free(output->capture_data);
1105 output->capture_data = NULL;
1106
1107 ret = rpi_resource_realloc(buffer, VC_IMAGE_ARGB8888,
1108 fb_width, fb_height,
1109 dst_pitch, fb_height);
1110 if (ret < 0) {
1111 weston_log("rpi-renderer error: "
1112 "allocating read buffer failed\n");
1113 return -1;
1114 }
1115
1116 ret = vc_dispmanx_snapshot(output->display, buffer->handle,
1117 VC_IMAGE_ROT0);
1118 if (ret) {
1119 weston_log("rpi-renderer error: "
1120 "vc_dispmanx_snapshot returned %d\n", ret);
1121 return -1;
1122 }
1123 DBG("%s: snapshot done.\n", __func__);
1124 }
1125
1126 /*
1127 * If vc_dispmanx_resource_read_data was able to read sub-rectangles,
1128 * we could read directly into 'pixels'. But it cannot, it does not
1129 * use rect.x or rect.width, and does this:
1130 * host_start = (uint8_t *)dst_address + (dst_pitch * p_rect->y);
1131 * In other words, it is only good for reading the full buffer in
1132 * one go.
1133 */
1134 vc_dispmanx_rect_set(&rect, 0, 0, fb_width, fb_height);
1135
1136 if (x == 0 && y == 0 && width == fb_width && height == fb_height) {
1137 ret = vc_dispmanx_resource_read_data(buffer->handle, &rect,
1138 pixels, dst_pitch);
1139 if (ret) {
1140 weston_log("rpi-renderer error: "
1141 "resource_read_data returned %d\n", ret);
1142 return -1;
1143 }
1144 DBG("%s: full frame done.\n", __func__);
1145 return 0;
1146 }
1147
1148 if (!output->capture_data) {
1149 output->capture_data = malloc(fb_height * dst_pitch);
1150 if (!output->capture_data) {
1151 weston_log("rpi-renderer error: "
1152 "out of memory\n");
1153 return -1;
1154 }
1155
1156 ret = vc_dispmanx_resource_read_data(buffer->handle, &rect,
1157 output->capture_data,
1158 dst_pitch);
1159 if (ret) {
1160 weston_log("rpi-renderer error: "
1161 "resource_read_data returned %d\n", ret);
1162 return -1;
1163 }
1164 }
1165
1166 for (i = 0; i < height; i++) {
1167 uint8_t *src = output->capture_data +
1168 (y + i) * dst_pitch + x * 4;
1169 uint8_t *dst = (uint8_t *)pixels + i * width * 4;
1170 memcpy(dst, src, width * 4);
1171 }
1172
1173 return 0;
1174}
1175
1176static void
1177rpir_output_dmx_remove_all(struct rpir_output *output,
1178 DISPMANX_UPDATE_HANDLE_T update)
1179{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001180 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001181
Jason Ekstranda7af7042013-10-12 22:38:11 -05001182 while (!wl_list_empty(&output->view_list)) {
1183 view = container_of(output->view_list.next,
1184 struct rpir_view, link);
1185 rpir_view_dmx_remove(view, update);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001186
Jason Ekstranda7af7042013-10-12 22:38:11 -05001187 wl_list_remove(&view->link);
1188 wl_list_insert(&output->view_cleanup_list, &view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001189 }
1190}
1191
1192static void
1193output_compute_matrix(struct weston_output *base)
1194{
1195 struct rpir_output *output = to_rpir_output(base);
1196 struct weston_matrix *matrix = &output->matrix;
1197 const float half_w = 0.5f * base->width;
1198 const float half_h = 0.5f * base->height;
1199 float mag;
1200 float dx, dy;
1201
1202 weston_matrix_init(matrix);
1203 weston_matrix_translate(matrix, -base->x, -base->y, 0.0f);
1204
1205#ifdef SURFACE_TRANSFORM
1206 weston_matrix_translate(matrix, -half_w, -half_h, 0.0f);
1207 switch (base->transform) {
1208 case WL_OUTPUT_TRANSFORM_FLIPPED:
1209 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1210 case WL_OUTPUT_TRANSFORM_NORMAL:
1211 /* weston_matrix_rotate_xy(matrix, 1.0f, 0.0f); no-op */
1212 weston_matrix_translate(matrix, half_w, half_h, 0.0f);
1213 break;
1214
1215 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1216 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1217 case WL_OUTPUT_TRANSFORM_90:
1218 weston_matrix_rotate_xy(matrix, 0.0f, 1.0f);
1219 weston_matrix_translate(matrix, half_h, half_w, 0.0f);
1220 break;
1221
1222 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1223 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1224 case WL_OUTPUT_TRANSFORM_180:
1225 weston_matrix_rotate_xy(matrix, -1.0f, 0.0f);
1226 weston_matrix_translate(matrix, half_w, half_h, 0.0f);
1227 break;
1228
1229 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1230 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1231 case WL_OUTPUT_TRANSFORM_270:
1232 weston_matrix_rotate_xy(matrix, 0.0f, -1.0f);
1233 weston_matrix_translate(matrix, half_h, half_w, 0.0f);
1234 break;
1235
1236 default:
1237 break;
1238 }
1239#endif
1240
1241 if (base->zoom.active) {
1242 /* The base->zoom stuff is in GL coordinate system */
1243 mag = 1.0f / (1.0f - base->zoom.spring_z.current);
1244 dx = -(base->zoom.trans_x + 1.0f) * half_w;
1245 dy = -(base->zoom.trans_y + 1.0f) * half_h;
1246 weston_matrix_translate(matrix, dx, dy, 0.0f);
1247 weston_matrix_scale(matrix, mag, mag, 1.0f);
1248 weston_matrix_translate(matrix, half_w, half_h, 0.0f);
1249 }
1250}
1251
1252/* Note: this won't work right for multiple outputs. A DispmanX Element
1253 * is tied to one DispmanX Display, i.e. output.
1254 */
1255static void
1256rpi_renderer_repaint_output(struct weston_output *base,
1257 pixman_region32_t *output_damage)
1258{
1259 struct weston_compositor *compositor = base->compositor;
1260 struct rpir_output *output = to_rpir_output(base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001261 struct weston_view *wv;
1262 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001263 struct wl_list done_list;
1264 int layer = 1;
1265
1266 assert(output->update != DISPMANX_NO_HANDLE);
1267
1268 output_compute_matrix(base);
1269
1270 rpi_resource_release(&output->capture_buffer);
1271 free(output->capture_data);
1272 output->capture_data = NULL;
1273
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 /* Swap resources on surfaces as needed */
1275 wl_list_for_each_reverse(wv, &compositor->view_list, link)
1276 wv->surface->touched = 0;
1277
1278 wl_list_for_each_reverse(wv, &compositor->view_list, link) {
1279 view = to_rpir_view(wv);
1280
1281 if (!wv->surface->touched) {
1282 wv->surface->touched = 1;
1283
Tomeu Vizoso44247742013-10-24 15:38:32 +02001284 if (view->surface->buffer_type == BUFFER_TYPE_EGL ||
1285 view->surface->need_swap)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286 rpir_surface_swap_pointers(view->surface);
1287 }
1288
Tomeu Vizoso74987582013-10-25 10:34:38 +02001289 if (view->surface->buffer_type == BUFFER_TYPE_EGL) {
1290 struct weston_buffer *buffer;
1291 buffer = view->surface->egl_front->buffer_ref.buffer;
1292 if (buffer != NULL) {
1293 vc_dispmanx_set_wl_buffer_in_use(buffer->resource, 1);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294 } else {
Tomeu Vizoso74987582013-10-25 10:34:38 +02001295 weston_log("warning: client destroyed current front buffer\n");
1296
1297 wl_list_remove(&view->link);
1298 if (view->handle == DISPMANX_NO_HANDLE) {
1299 wl_list_init(&view->link);
1300 } else {
1301 rpir_view_dmx_remove(view, output->update);
1302 wl_list_insert(&output->view_cleanup_list,
1303 &view->link);
1304 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305 }
1306 }
1307 }
1308
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001309 /* update all renderable surfaces */
1310 wl_list_init(&done_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 wl_list_for_each_reverse(wv, &compositor->view_list, link) {
1312 if (wv->plane != &compositor->primary_plane)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001313 continue;
1314
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315 view = to_rpir_view(wv);
1316 assert(!wl_list_empty(&view->link) ||
1317 view->handle == DISPMANX_NO_HANDLE);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001318
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 wl_list_remove(&view->link);
1320 wl_list_insert(&done_list, &view->link);
1321 rpir_view_update(view, output, output->update, layer++);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001322 }
1323
Jason Ekstranda7af7042013-10-12 22:38:11 -05001324 /* Mark all surfaces as swapped */
1325 wl_list_for_each_reverse(wv, &compositor->view_list, link)
1326 to_rpir_surface(wv->surface)->need_swap = 0;
1327
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001328 /* Remove all surfaces that are still on screen, but were
1329 * not rendered this time.
1330 */
1331 rpir_output_dmx_remove_all(output, output->update);
1332
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333 wl_list_insert_list(&output->view_list, &done_list);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001334 output->update = DISPMANX_NO_HANDLE;
1335
1336 /* The frame_signal is emitted in rpi_renderer_finish_frame(),
1337 * so that the firmware can capture the up-to-date contents.
1338 */
1339}
1340
1341static void
1342rpi_renderer_flush_damage(struct weston_surface *base)
1343{
1344 /* Called for every surface just before repainting it, if
1345 * having an shm buffer.
1346 */
1347 struct rpir_surface *surface = to_rpir_surface(base);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001348 struct weston_buffer *buffer = surface->buffer_ref.buffer;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001349 int ret;
1350
1351 assert(buffer);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001352 assert(wl_shm_buffer_get(buffer->resource));
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001353
1354 ret = rpir_surface_damage(surface, buffer, &base->damage);
1355 if (ret)
1356 weston_log("%s error: updating Dispmanx resource failed.\n",
1357 __func__);
1358
1359 weston_buffer_reference(&surface->buffer_ref, NULL);
1360}
1361
1362static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001363rpi_renderer_attach(struct weston_surface *base, struct weston_buffer *buffer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001364{
1365 /* Called every time a client commits an attach. */
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001366 struct rpir_surface *surface = to_rpir_surface(base);
1367
1368 assert(surface);
1369 if (!surface)
1370 return;
1371
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001372 if (surface->buffer_type == BUFFER_TYPE_SHM) {
1373 if (!surface->single_buffer)
1374 /* XXX: need to check if in middle of update */
1375 rpi_resource_release(surface->back);
1376
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 if (!surface->visible_views)
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001378 /* XXX: cannot do this, if middle of an update */
1379 rpi_resource_release(surface->front);
1380
1381 weston_buffer_reference(&surface->buffer_ref, NULL);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001382 }
1383
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001384 /* If buffer is NULL, Weston core unmaps the surface, the surface
1385 * will not appear in repaint list, and so rpi_renderer_repaint_output
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001386 * will remove the DispmanX element. Later, for SHM, also the front
1387 * buffer will be released in the cleanup_list processing.
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001388 */
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001389 if (!buffer)
1390 return;
1391
1392 if (wl_shm_buffer_get(buffer->resource)) {
1393 surface->buffer_type = BUFFER_TYPE_SHM;
1394 buffer->shm_buffer = wl_shm_buffer_get(buffer->resource);
1395 buffer->width = wl_shm_buffer_get_width(buffer->shm_buffer);
1396 buffer->height = wl_shm_buffer_get_height(buffer->shm_buffer);
1397
1398 weston_buffer_reference(&surface->buffer_ref, buffer);
1399 } else {
1400#if ENABLE_EGL
1401 struct rpi_renderer *renderer = to_rpi_renderer(base->compositor);
Tomeu Vizosoa8e5f292013-10-09 11:29:45 +02001402 struct wl_resource *wl_resource = buffer->resource;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001403
1404 if (!renderer->has_bind_display ||
1405 !renderer->query_buffer(renderer->egl_display,
1406 wl_resource,
1407 EGL_WIDTH, &buffer->width)) {
1408 weston_log("unhandled buffer type!\n");
1409 weston_buffer_reference(&surface->buffer_ref, NULL);
1410 surface->buffer_type = BUFFER_TYPE_NULL;
1411 }
1412
1413 renderer->query_buffer(renderer->egl_display,
1414 wl_resource,
1415 EGL_HEIGHT, &buffer->height);
1416
1417 surface->buffer_type = BUFFER_TYPE_EGL;
1418
1419 if(surface->egl_back == NULL)
1420 surface->egl_back = calloc(1, sizeof *surface->egl_back);
1421
1422 weston_buffer_reference(&surface->egl_back->buffer_ref, buffer);
1423 surface->egl_back->resource_handle =
1424 vc_dispmanx_get_handle_from_wl_buffer(wl_resource);
1425#else
1426 weston_log("unhandled buffer type!\n");
1427 weston_buffer_reference(&surface->buffer_ref, NULL);
1428 surface->buffer_type = BUFFER_TYPE_NULL;
1429#endif
1430 }
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001431}
1432
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001433static void
1434rpir_surface_handle_surface_destroy(struct wl_listener *listener, void *data)
1435{
1436 struct rpir_surface *surface;
1437 struct weston_surface *base = data;
1438
1439 surface = container_of(listener, struct rpir_surface,
1440 surface_destroy_listener);
1441
1442 assert(surface);
1443 assert(surface->surface == base);
1444 if (!surface)
1445 return;
1446
1447 surface->surface = NULL;
1448 base->renderer_state = NULL;
1449
1450 if (wl_list_empty(&surface->views))
1451 rpir_surface_destroy(surface);
1452}
1453
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001454static int
1455rpi_renderer_create_surface(struct weston_surface *base)
1456{
1457 struct rpi_renderer *renderer = to_rpi_renderer(base->compositor);
1458 struct rpir_surface *surface;
1459
1460 assert(base->renderer_state == NULL);
1461
1462 surface = rpir_surface_create(renderer);
1463 if (!surface)
1464 return -1;
1465
1466 surface->surface = base;
1467 base->renderer_state = surface;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001468
1469 surface->surface_destroy_listener.notify =
1470 rpir_surface_handle_surface_destroy;
1471 wl_signal_add(&base->destroy_signal,
1472 &surface->surface_destroy_listener);
1473
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001474 return 0;
1475}
1476
Jason Ekstranda7af7042013-10-12 22:38:11 -05001477static int
1478rpi_renderer_create_view(struct weston_view *base)
1479{
1480 struct rpir_surface *surface = to_rpir_surface(base->surface);
1481 struct rpir_view *view;
1482
1483 assert(base->renderer_state == NULL);
1484
1485 view = rpir_view_create(surface);
1486 if (!view)
1487 return -1;
1488
1489 view->view = base;
1490 base->renderer_state = view;
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +01001491
1492 view->view_destroy_listener.notify =
1493 rpir_view_handle_view_destroy;
1494 wl_signal_add(&base->destroy_signal,
1495 &view->view_destroy_listener);
1496
Jason Ekstranda7af7042013-10-12 22:38:11 -05001497 return 0;
1498}
1499
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001500static void
1501rpi_renderer_surface_set_color(struct weston_surface *base,
1502 float red, float green, float blue, float alpha)
1503{
1504 struct rpir_surface *surface = to_rpir_surface(base);
1505 uint8_t color[4];
1506 VC_RECT_T rect;
1507 int ret;
1508
1509 assert(surface);
1510
1511 ret = rpi_resource_realloc(surface->back, VC_IMAGE_ARGB8888,
1512 1, 1, 4, 1);
1513 if (ret < 0) {
1514 weston_log("Error: %s: rpi_resource_realloc failed.\n",
1515 __func__);
1516 return;
1517 }
1518
1519 color[0] = float2uint8(blue);
1520 color[1] = float2uint8(green);
1521 color[2] = float2uint8(red);
1522 color[3] = float2uint8(alpha);
1523
1524 vc_dispmanx_rect_set(&rect, 0, 0, 1, 1);
1525 ret = vc_dispmanx_resource_write_data(surface->back->handle,
1526 VC_IMAGE_ARGB8888,
1527 4, color, &rect);
1528 if (ret) {
1529 weston_log("Error: %s: resource_write_data failed.\n",
1530 __func__);
1531 return;
1532 }
1533
1534 DBG("%s: resource %p solid color BGRA %u,%u,%u,%u\n", __func__,
1535 surface->back, color[0], color[1], color[2], color[3]);
1536
1537 /*pixman_region32_copy(&surface->prev_damage, damage);*/
1538 surface->need_swap = 1;
1539}
1540
1541static void
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +01001542rpir_view_handle_view_destroy(struct wl_listener *listener, void *data)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001543{
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +01001544 struct rpir_view *view;
1545 struct weston_view *base = data;
1546
1547 view = container_of(listener, struct rpir_view, view_destroy_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001548
1549 assert(view);
1550 assert(view->view == base);
1551 if (!view)
1552 return;
1553
Tomeu Vizoso34dad7d2013-10-25 10:34:37 +02001554 view->view = NULL;
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +01001555 base->renderer_state = NULL;
Tomeu Vizoso34dad7d2013-10-25 10:34:37 +02001556
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +01001557 /* If guaranteed to not be on screen, just destroy it. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001558 if (wl_list_empty(&view->link))
1559 rpir_view_destroy(view);
1560
1561 /* Otherwise, the view is either on screen and needs
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001562 * to be removed by a repaint update, or it is in the
Jason Ekstranda7af7042013-10-12 22:38:11 -05001563 * view_cleanup_list, and will be destroyed by
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001564 * rpi_renderer_finish_frame().
1565 */
1566}
1567
1568static void
1569rpi_renderer_destroy(struct weston_compositor *compositor)
1570{
1571 struct rpi_renderer *renderer = to_rpi_renderer(compositor);
1572
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001573#if ENABLE_EGL
1574 if (renderer->has_bind_display)
1575 renderer->unbind_display(renderer->egl_display,
1576 compositor->wl_display);
1577#endif
1578
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001579 free(renderer);
1580 compositor->renderer = NULL;
1581}
1582
1583WL_EXPORT int
1584rpi_renderer_create(struct weston_compositor *compositor,
1585 const struct rpi_renderer_parameters *params)
1586{
1587 struct rpi_renderer *renderer;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001588#if ENABLE_EGL
1589 const char *extensions;
1590 EGLBoolean ret;
1591 EGLint major, minor;
1592#endif
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001593
1594 weston_log("Initializing the DispmanX compositing renderer\n");
1595
1596 renderer = calloc(1, sizeof *renderer);
1597 if (renderer == NULL)
1598 return -1;
1599
1600 renderer->single_buffer = params->single_buffer;
1601
1602 renderer->base.read_pixels = rpi_renderer_read_pixels;
1603 renderer->base.repaint_output = rpi_renderer_repaint_output;
1604 renderer->base.flush_damage = rpi_renderer_flush_damage;
1605 renderer->base.attach = rpi_renderer_attach;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001606 renderer->base.surface_set_color = rpi_renderer_surface_set_color;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001607 renderer->base.destroy = rpi_renderer_destroy;
1608
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001609#ifdef ENABLE_EGL
1610 renderer->egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1611 if (renderer->egl_display == EGL_NO_DISPLAY) {
1612 weston_log("failed to create EGL display\n");
1613 return -1;
1614 }
1615
1616 if (!eglInitialize(renderer->egl_display, &major, &minor)) {
1617 weston_log("failed to initialize EGL display\n");
1618 return -1;
1619 }
1620
1621 renderer->bind_display =
1622 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1623 renderer->unbind_display =
1624 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1625 renderer->query_buffer =
1626 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1627
1628 extensions = (const char *) eglQueryString(renderer->egl_display,
1629 EGL_EXTENSIONS);
1630 if (!extensions) {
1631 weston_log("Retrieving EGL extension string failed.\n");
1632 return -1;
1633 }
1634
1635 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1636 renderer->has_bind_display = 1;
1637
1638 if (renderer->has_bind_display) {
1639 ret = renderer->bind_display(renderer->egl_display,
1640 compositor->wl_display);
1641 if (!ret)
1642 renderer->has_bind_display = 0;
1643 }
1644#endif
1645
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001646 compositor->renderer = &renderer->base;
1647 compositor->read_format = PIXMAN_a8r8g8b8;
1648 /* WESTON_CAP_ROTATION_ANY not supported */
1649
Tomeu Vizoso03681892013-08-06 20:05:57 +02001650 wl_display_add_shm_format(compositor->wl_display, WL_SHM_FORMAT_RGB565);
1651
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001652 return 0;
1653}
1654
1655WL_EXPORT int
1656rpi_renderer_output_create(struct weston_output *base,
1657 DISPMANX_DISPLAY_HANDLE_T display)
1658{
1659 struct rpir_output *output;
1660
1661 assert(base->renderer_state == NULL);
1662
1663 output = calloc(1, sizeof *output);
1664 if (!output)
1665 return -1;
1666
1667 output->display = display;
1668 output->update = DISPMANX_NO_HANDLE;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001669 wl_list_init(&output->view_list);
1670 wl_list_init(&output->view_cleanup_list);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001671 rpi_resource_init(&output->capture_buffer);
1672 base->renderer_state = output;
1673
1674 return 0;
1675}
1676
1677WL_EXPORT void
1678rpi_renderer_output_destroy(struct weston_output *base)
1679{
1680 struct rpir_output *output = to_rpir_output(base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001681 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001682 DISPMANX_UPDATE_HANDLE_T update;
1683
1684 rpi_resource_release(&output->capture_buffer);
1685 free(output->capture_data);
1686 output->capture_data = NULL;
1687
1688 update = vc_dispmanx_update_start(0);
1689 rpir_output_dmx_remove_all(output, update);
1690 vc_dispmanx_update_submit_sync(update);
1691
Jason Ekstranda7af7042013-10-12 22:38:11 -05001692 while (!wl_list_empty(&output->view_cleanup_list)) {
1693 view = container_of(output->view_cleanup_list.next,
1694 struct rpir_view, link);
1695 rpir_view_destroy(view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001696 }
1697
1698 free(output);
1699 base->renderer_state = NULL;
1700}
1701
1702WL_EXPORT void
1703rpi_renderer_set_update_handle(struct weston_output *base,
1704 DISPMANX_UPDATE_HANDLE_T handle)
1705{
1706 struct rpir_output *output = to_rpir_output(base);
1707
1708 output->update = handle;
1709}
1710
1711WL_EXPORT void
1712rpi_renderer_finish_frame(struct weston_output *base)
1713{
1714 struct rpir_output *output = to_rpir_output(base);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001715 struct weston_compositor *compositor = base->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001716 struct weston_view *wv;
1717 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001718
Jason Ekstranda7af7042013-10-12 22:38:11 -05001719 while (!wl_list_empty(&output->view_cleanup_list)) {
1720 view = container_of(output->view_cleanup_list.next,
1721 struct rpir_view, link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001722
Jason Ekstranda7af7042013-10-12 22:38:11 -05001723 if (view->view) {
1724 /* The weston_view still exists, but is
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001725 * temporarily not visible, and hence its Element
1726 * was removed. The current front buffer contents
1727 * must be preserved.
1728 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001729 if (!view->surface->visible_views)
1730 rpi_resource_release(view->surface->back);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001731
Jason Ekstranda7af7042013-10-12 22:38:11 -05001732 wl_list_remove(&view->link);
1733 wl_list_init(&view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001734 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001735 rpir_view_destroy(view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001736 }
1737 }
1738
Jason Ekstranda7af7042013-10-12 22:38:11 -05001739 wl_list_for_each(wv, &compositor->view_list, link) {
1740 view = to_rpir_view(wv);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001741
Jason Ekstranda7af7042013-10-12 22:38:11 -05001742 if (view->surface->buffer_type != BUFFER_TYPE_EGL)
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001743 continue;
1744
Tomeu Vizoso74987582013-10-25 10:34:38 +02001745 rpir_egl_buffer_destroy(view->surface->egl_old_front);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746 view->surface->egl_old_front = NULL;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001747 }
1748
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001749 wl_signal_emit(&base->frame_signal, base);
1750}