blob: ea48b087bc40377a485b12a0361261ebdceddaeb [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;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300123};
124
Jason Ekstranda7af7042013-10-12 22:38:11 -0500125struct rpir_view {
126 struct rpir_surface *surface;
127 struct wl_list surface_link;
128 struct weston_view *view;
129
130 /* If link is empty, the view is guaranteed to not be on screen,
131 * i.e. updates removing Elements have completed.
132 */
133 struct wl_list link;
134
135 DISPMANX_ELEMENT_HANDLE_T handle;
136 int layer;
137};
138
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300139struct rpir_output {
140 DISPMANX_DISPLAY_HANDLE_T display;
141
142 DISPMANX_UPDATE_HANDLE_T update;
143 struct weston_matrix matrix;
144
145 /* all Elements currently on screen */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500146 struct wl_list view_list; /* struct rpir_surface::link */
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300147
148 /* Elements just removed, waiting for update completion */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500149 struct wl_list view_cleanup_list; /* struct rpir_surface::link */
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300150
151 struct rpi_resource capture_buffer;
152 uint8_t *capture_data;
153};
154
155struct rpi_renderer {
156 struct weston_renderer base;
157
158 int single_buffer;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200159
160#ifdef ENABLE_EGL
161 EGLDisplay egl_display;
162
163 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
164 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
165 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
166#endif
167 int has_bind_display;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300168};
169
170static inline struct rpir_surface *
171to_rpir_surface(struct weston_surface *surface)
172{
173 return surface->renderer_state;
174}
175
Jason Ekstranda7af7042013-10-12 22:38:11 -0500176static inline struct rpir_view *
177to_rpir_view(struct weston_view *view)
178{
179 return view->renderer_state;
180}
181
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300182static inline struct rpir_output *
183to_rpir_output(struct weston_output *output)
184{
185 return output->renderer_state;
186}
187
188static inline struct rpi_renderer *
189to_rpi_renderer(struct weston_compositor *compositor)
190{
191 return container_of(compositor->renderer, struct rpi_renderer, base);
192}
193
194static inline int
195int_max(int a, int b)
196{
197 return a > b ? a : b;
198}
199
200static inline void
201int_swap(int *a, int *b)
202{
203 int tmp = *a;
204 *a = *b;
205 *b = tmp;
206}
207
208static uint8_t
209float2uint8(float f)
210{
211 int v = roundf(f * 255.0f);
212
213 return v < 0 ? 0 : (v > 255 ? 255 : v);
214}
215
216static void
217rpi_resource_init(struct rpi_resource *resource)
218{
219 resource->handle = DISPMANX_NO_HANDLE;
220}
221
222static void
223rpi_resource_release(struct rpi_resource *resource)
224{
225 if (resource->handle == DISPMANX_NO_HANDLE)
226 return;
227
228 vc_dispmanx_resource_delete(resource->handle);
229 DBG("resource %p release\n", resource);
230 resource->handle = DISPMANX_NO_HANDLE;
231}
232
233static int
234rpi_resource_realloc(struct rpi_resource *resource, VC_IMAGE_TYPE_T ifmt,
235 int width, int height, int stride, int buffer_height)
236{
237 uint32_t dummy;
238
239 if (resource->handle != DISPMANX_NO_HANDLE &&
240 resource->width == width &&
241 resource->height == height &&
242 resource->stride == stride &&
243 resource->buffer_height == buffer_height &&
244 resource->ifmt == ifmt)
245 return 0;
246
247 rpi_resource_release(resource);
248
249 /* NOTE: if stride is not a multiple of 16 pixels in bytes,
250 * the vc_image_* functions may break. Dispmanx elements
251 * should be fine, though. Buffer_height probably has similar
252 * constraints, too.
253 */
254 resource->handle =
255 vc_dispmanx_resource_create(ifmt,
256 width | (stride << 16),
257 height | (buffer_height << 16),
258 &dummy);
259 if (resource->handle == DISPMANX_NO_HANDLE)
260 return -1;
261
262 resource->width = width;
263 resource->height = height;
264 resource->stride = stride;
265 resource->buffer_height = buffer_height;
266 resource->ifmt = ifmt;
267 DBG("resource %p alloc\n", resource);
268 return 1;
269}
270
271/* A firmware workaround for broken ALPHA_PREMULT + ALPHA_MIX hardware. */
272#define PREMULT_ALPHA_FLAG (1 << 31)
273
274static VC_IMAGE_TYPE_T
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500275shm_buffer_get_vc_format(struct wl_shm_buffer *buffer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300276{
277 switch (wl_shm_buffer_get_format(buffer)) {
278 case WL_SHM_FORMAT_XRGB8888:
279 return VC_IMAGE_XRGB8888;
280 case WL_SHM_FORMAT_ARGB8888:
281 return VC_IMAGE_ARGB8888 | PREMULT_ALPHA_FLAG;
Tomeu Vizoso03681892013-08-06 20:05:57 +0200282 case WL_SHM_FORMAT_RGB565:
283 return VC_IMAGE_RGB565;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300284 default:
285 /* invalid format */
286 return VC_IMAGE_MIN;
287 }
288}
289
290static int
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500291rpi_resource_update(struct rpi_resource *resource, struct weston_buffer *buffer,
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300292 pixman_region32_t *region)
293{
294 pixman_region32_t write_region;
295 pixman_box32_t *r;
296 VC_RECT_T rect;
297 VC_IMAGE_TYPE_T ifmt;
298 uint32_t *pixels;
299 int width;
300 int height;
301 int stride;
302 int ret;
303#ifdef HAVE_RESOURCE_WRITE_DATA_RECT
304 int n;
305#endif
306
307 if (!buffer)
308 return -1;
309
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500310 ifmt = shm_buffer_get_vc_format(buffer->shm_buffer);
311 width = wl_shm_buffer_get_width(buffer->shm_buffer);
312 height = wl_shm_buffer_get_height(buffer->shm_buffer);
313 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
314 pixels = wl_shm_buffer_get_data(buffer->shm_buffer);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300315
316 ret = rpi_resource_realloc(resource, ifmt & ~PREMULT_ALPHA_FLAG,
317 width, height, stride, height);
318 if (ret < 0)
319 return -1;
320
321 pixman_region32_init_rect(&write_region, 0, 0, width, height);
322 if (ret == 0)
323 pixman_region32_intersect(&write_region,
324 &write_region, region);
325
326#ifdef HAVE_RESOURCE_WRITE_DATA_RECT
327 /* XXX: Can this do a format conversion, so that scanout does not have to? */
328 r = pixman_region32_rectangles(&write_region, &n);
329 while (n--) {
330 vc_dispmanx_rect_set(&rect, r[n].x1, r[n].y1,
331 r[n].x2 - r[n].x1, r[n].y2 - r[n].y1);
332
333 ret = vc_dispmanx_resource_write_data_rect(resource->handle,
334 ifmt, stride,
335 pixels, &rect,
336 rect.x, rect.y);
337 DBG("%s: %p %ux%u@%u,%u, ret %d\n", __func__, resource,
338 rect.width, rect.height, rect.x, rect.y, ret);
339 if (ret)
340 break;
341 }
342#else
343 /* vc_dispmanx_resource_write_data() ignores ifmt,
344 * rect.x, rect.width, and uses stride only for computing
345 * the size of the transfer as rect.height * stride.
346 * Therefore we can only write rows starting at x=0.
347 * To be able to write more than one scanline at a time,
348 * the resource must have been created with the same stride
349 * as used here, and we must write full scanlines.
350 */
351
352 r = pixman_region32_extents(&write_region);
353 vc_dispmanx_rect_set(&rect, 0, r->y1, width, r->y2 - r->y1);
354 ret = vc_dispmanx_resource_write_data(resource->handle,
355 ifmt, stride, pixels, &rect);
356 DBG("%s: %p %ux%u@%u,%u, ret %d\n", __func__, resource,
357 width, r->y2 - r->y1, 0, r->y1, ret);
358#endif
359
360 pixman_region32_fini(&write_region);
361
362 return ret ? -1 : 0;
363}
364
365static struct rpir_surface *
366rpir_surface_create(struct rpi_renderer *renderer)
367{
368 struct rpir_surface *surface;
369
370 surface = calloc(1, sizeof *surface);
371 if (!surface)
372 return NULL;
373
Jason Ekstranda7af7042013-10-12 22:38:11 -0500374 surface->visible_views = 0;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300375 surface->single_buffer = renderer->single_buffer;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300376 rpi_resource_init(&surface->resources[0]);
377 rpi_resource_init(&surface->resources[1]);
378 surface->front = &surface->resources[0];
379 if (surface->single_buffer)
380 surface->back = &surface->resources[0];
381 else
382 surface->back = &surface->resources[1];
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200383 surface->buffer_type = BUFFER_TYPE_NULL;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300384
385 pixman_region32_init(&surface->prev_damage);
386
387 return surface;
388}
389
390static void
391rpir_surface_destroy(struct rpir_surface *surface)
392{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500393 if (surface->visible_views)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300394 weston_log("ERROR rpi: destroying on-screen element\n");
395
Jason Ekstranda7af7042013-10-12 22:38:11 -0500396 assert(wl_list_empty(&surface->views));
397
398 if (surface->surface)
399 surface->surface->renderer_state = NULL;
400
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300401 pixman_region32_fini(&surface->prev_damage);
402 rpi_resource_release(&surface->resources[0]);
403 rpi_resource_release(&surface->resources[1]);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500404 DBG("rpir_surface %p destroyed (%u)\n", surface, surface->visible_views);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300405
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200406 if (surface->egl_back != NULL) {
407 weston_buffer_reference(&surface->egl_back->buffer_ref, NULL);
408 free(surface->egl_back);
409 surface->egl_back = NULL;
410 }
411
412 if (surface->egl_front != NULL) {
413 weston_buffer_reference(&surface->egl_front->buffer_ref, NULL);
414 free(surface->egl_front);
415 surface->egl_front = NULL;
416 }
417
418 if (surface->egl_old_front != NULL) {
419 weston_buffer_reference(&surface->egl_old_front->buffer_ref, NULL);
420 free(surface->egl_old_front);
421 surface->egl_old_front = NULL;
422 }
423
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300424 free(surface);
425}
426
427static int
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500428rpir_surface_damage(struct rpir_surface *surface, struct weston_buffer *buffer,
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300429 pixman_region32_t *damage)
430{
431 pixman_region32_t upload;
432 int ret;
433
434 if (!pixman_region32_not_empty(damage))
435 return 0;
436
437 DBG("rpir_surface %p update resource %p\n", surface, surface->back);
438
439 /* XXX: todo: if no surface->handle, update front buffer directly
440 * to avoid creating a new back buffer */
441 if (surface->single_buffer) {
442 ret = rpi_resource_update(surface->front, buffer, damage);
443 } else {
444 pixman_region32_init(&upload);
445 pixman_region32_union(&upload, &surface->prev_damage, damage);
446 ret = rpi_resource_update(surface->back, buffer, &upload);
447 pixman_region32_fini(&upload);
448 }
449
450 pixman_region32_copy(&surface->prev_damage, damage);
451 surface->need_swap = 1;
452
453 return ret;
454}
455
Jason Ekstranda7af7042013-10-12 22:38:11 -0500456static struct rpir_view *
457rpir_view_create(struct rpir_surface *surface)
458{
459 struct rpir_view *view;
460
461 view = calloc(1, sizeof *view);
462 if (!view)
463 return NULL;
464
465 view->surface = surface;
466 wl_list_insert(&surface->views, &view->surface_link);
467
468 wl_list_init(&view->link);
469 view->handle = DISPMANX_NO_HANDLE;
470
471 return view;
472}
473
474static void
475rpir_view_destroy(struct rpir_view *view)
476{
477 wl_list_remove(&view->link);
478
479 if (view->handle != DISPMANX_NO_HANDLE) {
480 view->surface->visible_views--;
481 weston_log("ERROR rpi: destroying on-screen element\n");
482 }
483
484 if (view->view)
485 view->view->renderer_state = NULL;
486
487 wl_list_remove(&view->surface_link);
488 if (wl_list_empty(&view->surface->views) && view->surface->surface == NULL)
489 rpir_surface_destroy(view->surface);
490
491 DBG("rpir_view %p destroyed (%d)\n", view, view->handle);
492
493 free(view);
494}
495
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300496static void
497matrix_type_str(struct weston_matrix *matrix, char *buf, int len)
498{
499 static const char types[33] = "TSRO";
500 unsigned mask = matrix->type;
501 int i = 0;
502
503 while (mask && i < len - 1) {
504 if (mask & (1u << i))
505 *buf++ = types[i];
506 mask &= ~(1u << i);
507 i++;
508 }
509 *buf = '\0';
510}
511
512static void
513log_print_matrix(struct weston_matrix *matrix)
514{
515 char typestr[6];
516 float *d = matrix->d;
517
518 matrix_type_str(matrix, typestr, sizeof typestr);
519 weston_log_continue("%14.6e %14.6e %14.6e %14.6e\n",
520 d[0], d[4], d[8], d[12]);
521 weston_log_continue("%14.6e %14.6e %14.6e %14.6e\n",
522 d[1], d[5], d[9], d[13]);
523 weston_log_continue("%14.6e %14.6e %14.6e %14.6e\n",
524 d[2], d[6], d[10], d[14]);
525 weston_log_continue("%14.6e %14.6e %14.6e %14.6e type: %s\n",
526 d[3], d[7], d[11], d[15], typestr);
527}
528
529static void
530warn_bad_matrix(struct weston_matrix *total, struct weston_matrix *output,
531 struct weston_matrix *surface)
532{
533 static int n_warn;
534 char typestr[6];
535
536 if (n_warn++ == 10)
537 weston_log("%s: not showing more warnings\n", __func__);
538
539 if (n_warn > 10)
540 return;
541
542 weston_log("%s: warning: total transformation is not renderable:\n",
543 __func__);
544 log_print_matrix(total);
545
546 matrix_type_str(surface, typestr, sizeof typestr);
547 weston_log_continue("surface matrix type: %s\n", typestr);
548 matrix_type_str(output, typestr, sizeof typestr);
549 weston_log_continue("output matrix type: %s\n", typestr);
550}
551
552/*#define SURFACE_TRANSFORM */
553
554static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500555rpir_view_compute_rects(struct rpir_view *view,
556 VC_RECT_T *src_rect, VC_RECT_T *dst_rect,
557 VC_IMAGE_TRANSFORM_T *flipmask)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300558{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500559 struct weston_output *output_base = view->view->surface->output;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300560 struct rpir_output *output = to_rpir_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500561 struct weston_matrix matrix = view->view->transform.matrix;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300562 VC_IMAGE_TRANSFORM_T flipt = 0;
563 int src_x, src_y;
564 int dst_x, dst_y;
565 int src_width, src_height;
566 int dst_width, dst_height;
567 struct weston_vector p1 = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
568 struct weston_vector p2 = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
569 int t;
570 int over;
571
572 /* XXX: take buffer transform into account */
573
574 /* src is in 16.16, dst is in 32.0 fixed point.
575 * Negative values are not allowed in VC_RECT_T.
576 * Clip size to output boundaries, firmware ignores
577 * huge elements like 8192x8192.
578 */
579
580 src_x = 0 << 16;
581 src_y = 0 << 16;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200582
Jason Ekstranda7af7042013-10-12 22:38:11 -0500583 if (view->surface->buffer_type == BUFFER_TYPE_EGL) {
584 struct weston_buffer *buffer =
585 view->surface->egl_front->buffer_ref.buffer;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200586
587 src_width = buffer->width << 16;
588 src_height = buffer->height << 16;
589 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500590 src_width = view->surface->front->width << 16;
591 src_height = view->surface->front->height << 16;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200592 }
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300593
594 weston_matrix_multiply(&matrix, &output->matrix);
595
596#ifdef SURFACE_TRANSFORM
597 if (matrix.type >= WESTON_MATRIX_TRANSFORM_OTHER) {
598#else
599 if (matrix.type >= WESTON_MATRIX_TRANSFORM_ROTATE) {
600#endif
601 warn_bad_matrix(&matrix, &output->matrix,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500602 &view->view->transform.matrix);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300603 } else {
604 if (matrix.type & WESTON_MATRIX_TRANSFORM_ROTATE) {
605 if (fabsf(matrix.d[0]) < 1e-4f &&
606 fabsf(matrix.d[5]) < 1e-4f) {
607 flipt |= TRANSFORM_TRANSPOSE;
608 } else if (fabsf(matrix.d[1]) < 1e-4 &&
609 fabsf(matrix.d[4]) < 1e-4) {
610 /* no transpose */
611 } else {
612 warn_bad_matrix(&matrix, &output->matrix,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500613 &view->view->transform.matrix);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300614 }
615 }
616 }
617
Jason Ekstranda7af7042013-10-12 22:38:11 -0500618 p2.f[0] = view->view->geometry.width;
619 p2.f[1] = view->view->geometry.height;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300620
621 /* transform top-left and bot-right corner into screen coordinates */
622 weston_matrix_transform(&matrix, &p1);
623 weston_matrix_transform(&matrix, &p2);
624
625 /* Compute the destination rectangle on screen, converting
626 * negative dimensions to flips.
627 */
628
629 dst_width = round(p2.f[0] - p1.f[0]);
630 if (dst_width < 0) {
631 dst_x = round(p2.f[0]);
632 dst_width = -dst_width;
633
634 if (!(flipt & TRANSFORM_TRANSPOSE))
635 flipt |= TRANSFORM_HFLIP;
636 else
637 flipt |= TRANSFORM_VFLIP;
638 } else {
639 dst_x = round(p1.f[0]);
640 }
641
642 dst_height = round(p2.f[1] - p1.f[1]);
643 if (dst_height < 0) {
644 dst_y = round(p2.f[1]);
645 dst_height = -dst_height;
646
647 if (!(flipt & TRANSFORM_TRANSPOSE))
648 flipt |= TRANSFORM_VFLIP;
649 else
650 flipt |= TRANSFORM_HFLIP;
651 } else {
652 dst_y = round(p1.f[1]);
653 }
654
655 if (dst_width == 0 || dst_height == 0) {
656 DBG("ignored, zero surface area before clipping\n");
657 return -1;
658 }
659
660#ifdef SURFACE_TRANSFORM
661 /* Dispmanx works as if you flipped the whole screen, when
662 * you flip an element. But, we want to flip an element in place.
663 * XXX: fixme
664 */
665 if (flipt & TRANSFORM_HFLIP)
666 dst_x = output_base->width - dst_x;
667 if (flipt & TRANSFORM_VFLIP)
668 dst_y = output_base->height - dst_y;
669 if (flipt & TRANSFORM_TRANSPOSE) {
670 int_swap(&dst_x, &dst_y);
671 int_swap(&dst_width, &dst_height);
672 }
673#else
674 switch (output_base->transform) {
675 case WL_OUTPUT_TRANSFORM_FLIPPED:
676 flipt = TRANSFORM_HFLIP;
677 break;
678 case WL_OUTPUT_TRANSFORM_NORMAL:
679 flipt = 0;
680 break;
681
682 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
683 flipt = TRANSFORM_HFLIP | TRANSFORM_VFLIP | TRANSFORM_TRANSPOSE;
684 break;
685 case WL_OUTPUT_TRANSFORM_90:
686 flipt = TRANSFORM_VFLIP | TRANSFORM_TRANSPOSE;
687 break;
688 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
689 flipt = TRANSFORM_VFLIP;
690 break;
691 case WL_OUTPUT_TRANSFORM_180:
692 flipt = TRANSFORM_HFLIP | TRANSFORM_VFLIP;
693 break;
694
695 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
696 flipt = TRANSFORM_TRANSPOSE;
697 break;
698 case WL_OUTPUT_TRANSFORM_270:
699 flipt = TRANSFORM_HFLIP | TRANSFORM_TRANSPOSE;
700 break;
701 default:
702 break;
703 }
704#endif
705
706 /* clip destination rectangle to screen dimensions */
707
708 if (dst_x < 0) {
709 t = (int64_t)dst_x * src_width / dst_width;
710 src_width += t;
711 dst_width += dst_x;
712 src_x -= t;
713 dst_x = 0;
714 }
715
716 if (dst_y < 0) {
717 t = (int64_t)dst_y * src_height / dst_height;
718 src_height += t;
719 dst_height += dst_y;
720 src_y -= t;
721 dst_y = 0;
722 }
723
724 over = dst_x + dst_width - output_base->width;
725 if (over > 0) {
726 t = (int64_t)over * src_width / dst_width;
727 src_width -= t;
728 dst_width -= over;
729 }
730
731 over = dst_y + dst_height - output_base->height;
732 if (over > 0) {
733 t = (int64_t)over * src_height / dst_height;
734 src_height -= t;
735 dst_height -= over;
736 }
737
738 src_width = int_max(src_width, 0);
739 src_height = int_max(src_height, 0);
740
Jason Ekstranda7af7042013-10-12 22:38:11 -0500741 DBG("rpir_view %p %dx%d: p1 %f, %f; p2 %f, %f\n", view,
742 view->view->geometry.width,
743 view->view->geometry.height,
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300744 p1.f[0], p1.f[1], p2.f[0], p2.f[1]);
745 DBG("src rect %d;%d, %d;%d, %d;%dx%d;%d\n",
746 src_x >> 16, src_x & 0xffff,
747 src_y >> 16, src_y & 0xffff,
748 src_width >> 16, src_width & 0xffff,
749 src_height >> 16, src_height & 0xffff);
750 DBG("dest rect %d, %d, %dx%d%s%s%s\n",
751 dst_x, dst_y, dst_width, dst_height,
752 (flipt & TRANSFORM_HFLIP) ? " hflip" : "",
753 (flipt & TRANSFORM_VFLIP) ? " vflip" : "",
754 (flipt & TRANSFORM_TRANSPOSE) ? " transp" : "");
755
756 assert(src_x >= 0);
757 assert(src_y >= 0);
758 assert(dst_x >= 0);
759 assert(dst_y >= 0);
760
761 if (dst_width < 1 || dst_height < 1) {
762 DBG("ignored, zero surface area after clipping\n");
763 return -1;
764 }
765
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200766 /* EGL buffers will be upside-down related to what DispmanX expects */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500767 if (view->surface->buffer_type == BUFFER_TYPE_EGL)
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200768 flipt ^= TRANSFORM_VFLIP;
769
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300770 vc_dispmanx_rect_set(src_rect, src_x, src_y, src_width, src_height);
771 vc_dispmanx_rect_set(dst_rect, dst_x, dst_y, dst_width, dst_height);
772 *flipmask = flipt;
773
774 return 0;
775}
776
777static DISPMANX_TRANSFORM_T
778vc_image2dispmanx_transform(VC_IMAGE_TRANSFORM_T t)
779{
780 /* XXX: uhh, are these right? */
781 switch (t) {
782 case VC_IMAGE_ROT0:
783 return DISPMANX_NO_ROTATE;
784 case VC_IMAGE_MIRROR_ROT0:
785 return DISPMANX_FLIP_HRIZ;
786 case VC_IMAGE_MIRROR_ROT180:
787 return DISPMANX_FLIP_VERT;
788 case VC_IMAGE_ROT180:
789 return DISPMANX_ROTATE_180;
790 case VC_IMAGE_MIRROR_ROT90:
791 return DISPMANX_ROTATE_90 | DISPMANX_FLIP_HRIZ;
792 case VC_IMAGE_ROT270:
793 return DISPMANX_ROTATE_270;
794 case VC_IMAGE_ROT90:
795 return DISPMANX_ROTATE_90;
796 case VC_IMAGE_MIRROR_ROT270:
797 return DISPMANX_ROTATE_270 | DISPMANX_FLIP_VERT;
798 default:
799 assert(0 && "bad VC_IMAGE_TRANSFORM_T");
800 return DISPMANX_NO_ROTATE;
801 }
802}
803
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200804
805static DISPMANX_RESOURCE_HANDLE_T
806rpir_surface_get_resource(struct rpir_surface *surface)
807{
808 switch (surface->buffer_type) {
809 case BUFFER_TYPE_SHM:
810 case BUFFER_TYPE_NULL:
811 return surface->front->handle;
812 case BUFFER_TYPE_EGL:
813 if (surface->egl_front != NULL)
814 return surface->egl_front->resource_handle;
815 default:
816 return DISPMANX_NO_HANDLE;
817 }
818}
819
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300820static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500821rpir_view_dmx_add(struct rpir_view *view, struct rpir_output *output,
822 DISPMANX_UPDATE_HANDLE_T update, int layer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300823{
824 /* Do not use DISPMANX_FLAGS_ALPHA_PREMULT here.
825 * If you define PREMULT and ALPHA_MIX, the hardware will not
826 * multiply the source color with the element alpha, leading to
827 * bad colors. Instead, we define PREMULT during pixel data upload.
828 */
829 VC_DISPMANX_ALPHA_T alphasetup = {
830 DISPMANX_FLAGS_ALPHA_FROM_SOURCE |
831 DISPMANX_FLAGS_ALPHA_MIX,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500832 float2uint8(view->view->alpha), /* opacity 0-255 */
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300833 0 /* mask resource handle */
834 };
835 VC_RECT_T dst_rect;
836 VC_RECT_T src_rect;
837 VC_IMAGE_TRANSFORM_T flipmask;
838 int ret;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200839 DISPMANX_RESOURCE_HANDLE_T resource_handle;
840
Jason Ekstranda7af7042013-10-12 22:38:11 -0500841 resource_handle = rpir_surface_get_resource(view->surface);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200842 if (resource_handle == DISPMANX_NO_HANDLE) {
843 weston_log("%s: no buffer yet, aborting\n", __func__);
844 return 0;
845 }
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300846
Jason Ekstranda7af7042013-10-12 22:38:11 -0500847 ret = rpir_view_compute_rects(view, &src_rect, &dst_rect, &flipmask);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300848 if (ret < 0)
849 return 0;
850
Jason Ekstranda7af7042013-10-12 22:38:11 -0500851 view->handle = vc_dispmanx_element_add(
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300852 update,
853 output->display,
854 layer,
855 &dst_rect,
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200856 resource_handle,
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300857 &src_rect,
858 DISPMANX_PROTECTION_NONE,
859 &alphasetup,
860 NULL /* clamp */,
861 vc_image2dispmanx_transform(flipmask));
Jason Ekstranda7af7042013-10-12 22:38:11 -0500862 DBG("rpir_surface %p add %u, alpha %f resource %d\n", view,
863 view->handle, view->view->alpha, resource_handle);
864
865 if (view->handle == DISPMANX_NO_HANDLE)
866 return -1;
867
868 view->surface->visible_views++;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300869
870 return 1;
871}
872
873static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500874rpir_view_dmx_swap(struct rpir_view *view,
875 DISPMANX_UPDATE_HANDLE_T update)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300876{
877 VC_RECT_T rect;
878 pixman_box32_t *r;
879
880 /* XXX: skip, iff resource was not reallocated, and single-buffering */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500881 vc_dispmanx_element_change_source(update, view->handle,
882 view->surface->front->handle);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300883
884 /* This is current damage now, after rpir_surface_damage() */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500885 r = pixman_region32_extents(&view->surface->prev_damage);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300886
887 vc_dispmanx_rect_set(&rect, r->x1, r->y1,
888 r->x2 - r->x1, r->y2 - r->y1);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500889 vc_dispmanx_element_modified(update, view->handle, &rect);
890 DBG("rpir_view %p swap\n", view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300891}
892
893static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500894rpir_view_dmx_move(struct rpir_view *view,
895 DISPMANX_UPDATE_HANDLE_T update, int layer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300896{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500897 uint8_t alpha = float2uint8(view->view->alpha);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300898 VC_RECT_T dst_rect;
899 VC_RECT_T src_rect;
900 VC_IMAGE_TRANSFORM_T flipmask;
901 int ret;
902
903 /* XXX: return early, if all attributes stay the same */
904
Jason Ekstranda7af7042013-10-12 22:38:11 -0500905 if (view->surface->buffer_type == BUFFER_TYPE_EGL) {
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200906 DISPMANX_RESOURCE_HANDLE_T resource_handle;
907
Jason Ekstranda7af7042013-10-12 22:38:11 -0500908 resource_handle = rpir_surface_get_resource(view->surface);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200909 if (resource_handle == DISPMANX_NO_HANDLE) {
910 weston_log("%s: no buffer yet, aborting\n", __func__);
911 return 0;
912 }
913
914 vc_dispmanx_element_change_source(update,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500915 view->handle,
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200916 resource_handle);
917 }
918
Jason Ekstranda7af7042013-10-12 22:38:11 -0500919 ret = rpir_view_compute_rects(view, &src_rect, &dst_rect, &flipmask);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300920 if (ret < 0)
921 return 0;
922
923 ret = vc_dispmanx_element_change_attributes(
924 update,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500925 view->handle,
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300926 ELEMENT_CHANGE_LAYER |
927 ELEMENT_CHANGE_OPACITY |
928 ELEMENT_CHANGE_TRANSFORM |
929 ELEMENT_CHANGE_DEST_RECT |
930 ELEMENT_CHANGE_SRC_RECT,
931 layer,
932 alpha,
933 &dst_rect,
934 &src_rect,
935 DISPMANX_NO_HANDLE,
936 /* This really is DISPMANX_TRANSFORM_T, no matter
937 * what the header says. */
938 vc_image2dispmanx_transform(flipmask));
Jason Ekstranda7af7042013-10-12 22:38:11 -0500939 DBG("rpir_view %p move\n", view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300940
941 if (ret)
942 return -1;
943
944 return 1;
945}
946
947static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500948rpir_view_dmx_remove(struct rpir_view *view,
949 DISPMANX_UPDATE_HANDLE_T update)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300950{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500951 if (view->handle == DISPMANX_NO_HANDLE)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300952 return;
953
Jason Ekstranda7af7042013-10-12 22:38:11 -0500954 vc_dispmanx_element_remove(update, view->handle);
955 DBG("rpir_view %p remove %u\n", view, view->handle);
956 view->handle = DISPMANX_NO_HANDLE;
957 view->surface->visible_views--;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300958}
959
960static void
961rpir_surface_swap_pointers(struct rpir_surface *surface)
962{
963 struct rpi_resource *tmp;
964
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200965 if (surface->buffer_type == BUFFER_TYPE_EGL) {
966 if (surface->egl_back != NULL) {
967 assert(surface->egl_old_front == NULL);
968 surface->egl_old_front = surface->egl_front;
969 surface->egl_front = surface->egl_back;
970 surface->egl_back = NULL;
971 }
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200972 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500973 tmp = surface->front;
974 surface->front = surface->back;
975 surface->back = tmp;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200976 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977 DBG("new back %p, new front %p\n", surface->back, surface->front);
978}
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300979
Jason Ekstranda7af7042013-10-12 22:38:11 -0500980static int
981is_view_not_visible(struct weston_view *view)
982{
983 /* Return true, if surface is guaranteed to be totally obscured. */
984 int ret;
985 pixman_region32_t unocc;
986
987 pixman_region32_init(&unocc);
988 pixman_region32_subtract(&unocc, &view->transform.boundingbox,
989 &view->clip);
990 ret = !pixman_region32_not_empty(&unocc);
991 pixman_region32_fini(&unocc);
992
993 return ret;
994}
995
996static void
997rpir_view_update(struct rpir_view *view, struct rpir_output *output,
998 DISPMANX_UPDATE_HANDLE_T update, int layer)
999{
1000 int ret;
1001 int obscured;
1002
1003
1004 obscured = is_view_not_visible(view->view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001005 if (obscured) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001006 DBG("rpir_view %p totally obscured.\n", view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001007
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 wl_list_remove(&view->link);
1009 if (view->handle == DISPMANX_NO_HANDLE) {
1010 wl_list_init(&view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001011 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012 rpir_view_dmx_remove(view, update);
1013 wl_list_insert(&output->view_cleanup_list,
1014 &view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001015 }
1016
1017 goto out;
1018 }
1019
Jason Ekstranda7af7042013-10-12 22:38:11 -05001020 if (view->handle == DISPMANX_NO_HANDLE) {
1021 ret = rpir_view_dmx_add(view, output, update, layer);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001022 if (ret == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001023 wl_list_remove(&view->link);
1024 wl_list_init(&view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001025 } else if (ret < 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001026 weston_log("ERROR rpir_view_dmx_add() failed.\n");
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001027 }
1028 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001029 if (view->surface->need_swap)
1030 rpir_view_dmx_swap(view, update);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001031
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 ret = rpir_view_dmx_move(view, update, layer);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001033 if (ret == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001034 rpir_view_dmx_remove(view, update);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001035
Jason Ekstranda7af7042013-10-12 22:38:11 -05001036 wl_list_remove(&view->link);
1037 wl_list_insert(&output->view_cleanup_list,
1038 &view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001039 } else if (ret < 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001040 weston_log("ERROR rpir_view_dmx_move() failed.\n");
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001041 }
1042 }
1043
1044out:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001045 view->layer = layer;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001046}
1047
1048static int
1049rpi_renderer_read_pixels(struct weston_output *base,
1050 pixman_format_code_t format, void *pixels,
1051 uint32_t x, uint32_t y,
1052 uint32_t width, uint32_t height)
1053{
1054 struct rpir_output *output = to_rpir_output(base);
1055 struct rpi_resource *buffer = &output->capture_buffer;
1056 VC_RECT_T rect;
1057 uint32_t fb_width, fb_height;
1058 uint32_t dst_pitch;
1059 uint32_t i;
1060 int ret;
1061
Hardeningff39efa2013-09-18 23:56:35 +02001062 fb_width = base->current_mode->width;
1063 fb_height = base->current_mode->height;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001064
1065 DBG("%s(%u, %u, %u, %u), resource %p\n", __func__,
1066 x, y, width, height, buffer);
1067
1068 if (format != PIXMAN_a8r8g8b8) {
1069 weston_log("rpi-renderer error: bad read_format\n");
1070 return -1;
1071 }
1072
1073 dst_pitch = fb_width * 4;
1074
1075 if (buffer->handle == DISPMANX_NO_HANDLE) {
1076 free(output->capture_data);
1077 output->capture_data = NULL;
1078
1079 ret = rpi_resource_realloc(buffer, VC_IMAGE_ARGB8888,
1080 fb_width, fb_height,
1081 dst_pitch, fb_height);
1082 if (ret < 0) {
1083 weston_log("rpi-renderer error: "
1084 "allocating read buffer failed\n");
1085 return -1;
1086 }
1087
1088 ret = vc_dispmanx_snapshot(output->display, buffer->handle,
1089 VC_IMAGE_ROT0);
1090 if (ret) {
1091 weston_log("rpi-renderer error: "
1092 "vc_dispmanx_snapshot returned %d\n", ret);
1093 return -1;
1094 }
1095 DBG("%s: snapshot done.\n", __func__);
1096 }
1097
1098 /*
1099 * If vc_dispmanx_resource_read_data was able to read sub-rectangles,
1100 * we could read directly into 'pixels'. But it cannot, it does not
1101 * use rect.x or rect.width, and does this:
1102 * host_start = (uint8_t *)dst_address + (dst_pitch * p_rect->y);
1103 * In other words, it is only good for reading the full buffer in
1104 * one go.
1105 */
1106 vc_dispmanx_rect_set(&rect, 0, 0, fb_width, fb_height);
1107
1108 if (x == 0 && y == 0 && width == fb_width && height == fb_height) {
1109 ret = vc_dispmanx_resource_read_data(buffer->handle, &rect,
1110 pixels, dst_pitch);
1111 if (ret) {
1112 weston_log("rpi-renderer error: "
1113 "resource_read_data returned %d\n", ret);
1114 return -1;
1115 }
1116 DBG("%s: full frame done.\n", __func__);
1117 return 0;
1118 }
1119
1120 if (!output->capture_data) {
1121 output->capture_data = malloc(fb_height * dst_pitch);
1122 if (!output->capture_data) {
1123 weston_log("rpi-renderer error: "
1124 "out of memory\n");
1125 return -1;
1126 }
1127
1128 ret = vc_dispmanx_resource_read_data(buffer->handle, &rect,
1129 output->capture_data,
1130 dst_pitch);
1131 if (ret) {
1132 weston_log("rpi-renderer error: "
1133 "resource_read_data returned %d\n", ret);
1134 return -1;
1135 }
1136 }
1137
1138 for (i = 0; i < height; i++) {
1139 uint8_t *src = output->capture_data +
1140 (y + i) * dst_pitch + x * 4;
1141 uint8_t *dst = (uint8_t *)pixels + i * width * 4;
1142 memcpy(dst, src, width * 4);
1143 }
1144
1145 return 0;
1146}
1147
1148static void
1149rpir_output_dmx_remove_all(struct rpir_output *output,
1150 DISPMANX_UPDATE_HANDLE_T update)
1151{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001153
Jason Ekstranda7af7042013-10-12 22:38:11 -05001154 while (!wl_list_empty(&output->view_list)) {
1155 view = container_of(output->view_list.next,
1156 struct rpir_view, link);
1157 rpir_view_dmx_remove(view, update);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001158
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 wl_list_remove(&view->link);
1160 wl_list_insert(&output->view_cleanup_list, &view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001161 }
1162}
1163
1164static void
1165output_compute_matrix(struct weston_output *base)
1166{
1167 struct rpir_output *output = to_rpir_output(base);
1168 struct weston_matrix *matrix = &output->matrix;
1169 const float half_w = 0.5f * base->width;
1170 const float half_h = 0.5f * base->height;
1171 float mag;
1172 float dx, dy;
1173
1174 weston_matrix_init(matrix);
1175 weston_matrix_translate(matrix, -base->x, -base->y, 0.0f);
1176
1177#ifdef SURFACE_TRANSFORM
1178 weston_matrix_translate(matrix, -half_w, -half_h, 0.0f);
1179 switch (base->transform) {
1180 case WL_OUTPUT_TRANSFORM_FLIPPED:
1181 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1182 case WL_OUTPUT_TRANSFORM_NORMAL:
1183 /* weston_matrix_rotate_xy(matrix, 1.0f, 0.0f); no-op */
1184 weston_matrix_translate(matrix, half_w, half_h, 0.0f);
1185 break;
1186
1187 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1188 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1189 case WL_OUTPUT_TRANSFORM_90:
1190 weston_matrix_rotate_xy(matrix, 0.0f, 1.0f);
1191 weston_matrix_translate(matrix, half_h, half_w, 0.0f);
1192 break;
1193
1194 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1195 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1196 case WL_OUTPUT_TRANSFORM_180:
1197 weston_matrix_rotate_xy(matrix, -1.0f, 0.0f);
1198 weston_matrix_translate(matrix, half_w, half_h, 0.0f);
1199 break;
1200
1201 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1202 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1203 case WL_OUTPUT_TRANSFORM_270:
1204 weston_matrix_rotate_xy(matrix, 0.0f, -1.0f);
1205 weston_matrix_translate(matrix, half_h, half_w, 0.0f);
1206 break;
1207
1208 default:
1209 break;
1210 }
1211#endif
1212
1213 if (base->zoom.active) {
1214 /* The base->zoom stuff is in GL coordinate system */
1215 mag = 1.0f / (1.0f - base->zoom.spring_z.current);
1216 dx = -(base->zoom.trans_x + 1.0f) * half_w;
1217 dy = -(base->zoom.trans_y + 1.0f) * half_h;
1218 weston_matrix_translate(matrix, dx, dy, 0.0f);
1219 weston_matrix_scale(matrix, mag, mag, 1.0f);
1220 weston_matrix_translate(matrix, half_w, half_h, 0.0f);
1221 }
1222}
1223
1224/* Note: this won't work right for multiple outputs. A DispmanX Element
1225 * is tied to one DispmanX Display, i.e. output.
1226 */
1227static void
1228rpi_renderer_repaint_output(struct weston_output *base,
1229 pixman_region32_t *output_damage)
1230{
1231 struct weston_compositor *compositor = base->compositor;
1232 struct rpir_output *output = to_rpir_output(base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 struct weston_view *wv;
1234 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001235 struct wl_list done_list;
1236 int layer = 1;
1237
1238 assert(output->update != DISPMANX_NO_HANDLE);
1239
1240 output_compute_matrix(base);
1241
1242 rpi_resource_release(&output->capture_buffer);
1243 free(output->capture_data);
1244 output->capture_data = NULL;
1245
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 /* Swap resources on surfaces as needed */
1247 wl_list_for_each_reverse(wv, &compositor->view_list, link)
1248 wv->surface->touched = 0;
1249
1250 wl_list_for_each_reverse(wv, &compositor->view_list, link) {
1251 view = to_rpir_view(wv);
1252
1253 if (!wv->surface->touched) {
1254 wv->surface->touched = 1;
1255
1256 if (view->surface->need_swap)
1257 rpir_surface_swap_pointers(view->surface);
1258 }
1259
1260 if (view->surface->egl_front->buffer_ref.buffer == NULL) {
1261 weston_log("warning: client unreffed current front buffer\n");
1262
1263 wl_list_remove(&view->link);
1264 if (view->handle == DISPMANX_NO_HANDLE) {
1265 wl_list_init(&view->link);
1266 } else {
1267 rpir_view_dmx_remove(view, output->update);
1268 wl_list_insert(&output->view_cleanup_list,
1269 &view->link);
1270 }
1271 }
1272 }
1273
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001274 /* update all renderable surfaces */
1275 wl_list_init(&done_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 wl_list_for_each_reverse(wv, &compositor->view_list, link) {
1277 if (wv->plane != &compositor->primary_plane)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001278 continue;
1279
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 view = to_rpir_view(wv);
1281 assert(!wl_list_empty(&view->link) ||
1282 view->handle == DISPMANX_NO_HANDLE);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001283
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 wl_list_remove(&view->link);
1285 wl_list_insert(&done_list, &view->link);
1286 rpir_view_update(view, output, output->update, layer++);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001287 }
1288
Jason Ekstranda7af7042013-10-12 22:38:11 -05001289 /* Mark all surfaces as swapped */
1290 wl_list_for_each_reverse(wv, &compositor->view_list, link)
1291 to_rpir_surface(wv->surface)->need_swap = 0;
1292
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001293 /* Remove all surfaces that are still on screen, but were
1294 * not rendered this time.
1295 */
1296 rpir_output_dmx_remove_all(output, output->update);
1297
Jason Ekstranda7af7042013-10-12 22:38:11 -05001298 wl_list_insert_list(&output->view_list, &done_list);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001299 output->update = DISPMANX_NO_HANDLE;
1300
1301 /* The frame_signal is emitted in rpi_renderer_finish_frame(),
1302 * so that the firmware can capture the up-to-date contents.
1303 */
1304}
1305
1306static void
1307rpi_renderer_flush_damage(struct weston_surface *base)
1308{
1309 /* Called for every surface just before repainting it, if
1310 * having an shm buffer.
1311 */
1312 struct rpir_surface *surface = to_rpir_surface(base);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001313 struct weston_buffer *buffer = surface->buffer_ref.buffer;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001314 int ret;
1315
1316 assert(buffer);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001317 assert(wl_shm_buffer_get(buffer->resource));
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001318
1319 ret = rpir_surface_damage(surface, buffer, &base->damage);
1320 if (ret)
1321 weston_log("%s error: updating Dispmanx resource failed.\n",
1322 __func__);
1323
1324 weston_buffer_reference(&surface->buffer_ref, NULL);
1325}
1326
1327static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001328rpi_renderer_attach(struct weston_surface *base, struct weston_buffer *buffer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001329{
1330 /* Called every time a client commits an attach. */
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001331 struct rpir_surface *surface = to_rpir_surface(base);
1332
1333 assert(surface);
1334 if (!surface)
1335 return;
1336
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001337 if (surface->buffer_type == BUFFER_TYPE_SHM) {
1338 if (!surface->single_buffer)
1339 /* XXX: need to check if in middle of update */
1340 rpi_resource_release(surface->back);
1341
Jason Ekstranda7af7042013-10-12 22:38:11 -05001342 if (!surface->visible_views)
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001343 /* XXX: cannot do this, if middle of an update */
1344 rpi_resource_release(surface->front);
1345
1346 weston_buffer_reference(&surface->buffer_ref, NULL);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001347 }
1348
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001349 /* If buffer is NULL, Weston core unmaps the surface, the surface
1350 * will not appear in repaint list, and so rpi_renderer_repaint_output
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001351 * will remove the DispmanX element. Later, for SHM, also the front
1352 * buffer will be released in the cleanup_list processing.
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001353 */
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001354 if (!buffer)
1355 return;
1356
1357 if (wl_shm_buffer_get(buffer->resource)) {
1358 surface->buffer_type = BUFFER_TYPE_SHM;
1359 buffer->shm_buffer = wl_shm_buffer_get(buffer->resource);
1360 buffer->width = wl_shm_buffer_get_width(buffer->shm_buffer);
1361 buffer->height = wl_shm_buffer_get_height(buffer->shm_buffer);
1362
1363 weston_buffer_reference(&surface->buffer_ref, buffer);
1364 } else {
1365#if ENABLE_EGL
1366 struct rpi_renderer *renderer = to_rpi_renderer(base->compositor);
Tomeu Vizosoa8e5f292013-10-09 11:29:45 +02001367 struct wl_resource *wl_resource = buffer->resource;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001368
1369 if (!renderer->has_bind_display ||
1370 !renderer->query_buffer(renderer->egl_display,
1371 wl_resource,
1372 EGL_WIDTH, &buffer->width)) {
1373 weston_log("unhandled buffer type!\n");
1374 weston_buffer_reference(&surface->buffer_ref, NULL);
1375 surface->buffer_type = BUFFER_TYPE_NULL;
1376 }
1377
1378 renderer->query_buffer(renderer->egl_display,
1379 wl_resource,
1380 EGL_HEIGHT, &buffer->height);
1381
1382 surface->buffer_type = BUFFER_TYPE_EGL;
1383
1384 if(surface->egl_back == NULL)
1385 surface->egl_back = calloc(1, sizeof *surface->egl_back);
1386
1387 weston_buffer_reference(&surface->egl_back->buffer_ref, buffer);
1388 surface->egl_back->resource_handle =
1389 vc_dispmanx_get_handle_from_wl_buffer(wl_resource);
1390#else
1391 weston_log("unhandled buffer type!\n");
1392 weston_buffer_reference(&surface->buffer_ref, NULL);
1393 surface->buffer_type = BUFFER_TYPE_NULL;
1394#endif
1395 }
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001396}
1397
1398static int
1399rpi_renderer_create_surface(struct weston_surface *base)
1400{
1401 struct rpi_renderer *renderer = to_rpi_renderer(base->compositor);
1402 struct rpir_surface *surface;
1403
1404 assert(base->renderer_state == NULL);
1405
1406 surface = rpir_surface_create(renderer);
1407 if (!surface)
1408 return -1;
1409
1410 surface->surface = base;
1411 base->renderer_state = surface;
1412 return 0;
1413}
1414
Jason Ekstranda7af7042013-10-12 22:38:11 -05001415static int
1416rpi_renderer_create_view(struct weston_view *base)
1417{
1418 struct rpir_surface *surface = to_rpir_surface(base->surface);
1419 struct rpir_view *view;
1420
1421 assert(base->renderer_state == NULL);
1422
1423 view = rpir_view_create(surface);
1424 if (!view)
1425 return -1;
1426
1427 view->view = base;
1428 base->renderer_state = view;
1429 return 0;
1430}
1431
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001432static void
1433rpi_renderer_surface_set_color(struct weston_surface *base,
1434 float red, float green, float blue, float alpha)
1435{
1436 struct rpir_surface *surface = to_rpir_surface(base);
1437 uint8_t color[4];
1438 VC_RECT_T rect;
1439 int ret;
1440
1441 assert(surface);
1442
1443 ret = rpi_resource_realloc(surface->back, VC_IMAGE_ARGB8888,
1444 1, 1, 4, 1);
1445 if (ret < 0) {
1446 weston_log("Error: %s: rpi_resource_realloc failed.\n",
1447 __func__);
1448 return;
1449 }
1450
1451 color[0] = float2uint8(blue);
1452 color[1] = float2uint8(green);
1453 color[2] = float2uint8(red);
1454 color[3] = float2uint8(alpha);
1455
1456 vc_dispmanx_rect_set(&rect, 0, 0, 1, 1);
1457 ret = vc_dispmanx_resource_write_data(surface->back->handle,
1458 VC_IMAGE_ARGB8888,
1459 4, color, &rect);
1460 if (ret) {
1461 weston_log("Error: %s: resource_write_data failed.\n",
1462 __func__);
1463 return;
1464 }
1465
1466 DBG("%s: resource %p solid color BGRA %u,%u,%u,%u\n", __func__,
1467 surface->back, color[0], color[1], color[2], color[3]);
1468
1469 /*pixman_region32_copy(&surface->prev_damage, damage);*/
1470 surface->need_swap = 1;
1471}
1472
1473static void
1474rpi_renderer_destroy_surface(struct weston_surface *base)
1475{
1476 struct rpir_surface *surface = to_rpir_surface(base);
1477
1478 assert(surface);
1479 assert(surface->surface == base);
1480 if (!surface)
1481 return;
1482
1483 surface->surface = NULL;
1484 base->renderer_state = NULL;
1485
Jason Ekstranda7af7042013-10-12 22:38:11 -05001486 if (wl_list_empty(&surface->views))
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001487 rpir_surface_destroy(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001488}
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001489
Jason Ekstranda7af7042013-10-12 22:38:11 -05001490static void
1491rpi_renderer_destroy_view(struct weston_view *base)
1492{
1493 struct rpir_view *view = to_rpir_view(base);
1494
1495 assert(view);
1496 assert(view->view == base);
1497 if (!view)
1498 return;
1499
1500 /* If guaranteed to not be on screen, just detroy it. */
1501 if (wl_list_empty(&view->link))
1502 rpir_view_destroy(view);
1503
1504 /* Otherwise, the view is either on screen and needs
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001505 * to be removed by a repaint update, or it is in the
Jason Ekstranda7af7042013-10-12 22:38:11 -05001506 * view_cleanup_list, and will be destroyed by
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001507 * rpi_renderer_finish_frame().
1508 */
1509}
1510
1511static void
1512rpi_renderer_destroy(struct weston_compositor *compositor)
1513{
1514 struct rpi_renderer *renderer = to_rpi_renderer(compositor);
1515
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001516#if ENABLE_EGL
1517 if (renderer->has_bind_display)
1518 renderer->unbind_display(renderer->egl_display,
1519 compositor->wl_display);
1520#endif
1521
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001522 free(renderer);
1523 compositor->renderer = NULL;
1524}
1525
1526WL_EXPORT int
1527rpi_renderer_create(struct weston_compositor *compositor,
1528 const struct rpi_renderer_parameters *params)
1529{
1530 struct rpi_renderer *renderer;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001531#if ENABLE_EGL
1532 const char *extensions;
1533 EGLBoolean ret;
1534 EGLint major, minor;
1535#endif
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001536
1537 weston_log("Initializing the DispmanX compositing renderer\n");
1538
1539 renderer = calloc(1, sizeof *renderer);
1540 if (renderer == NULL)
1541 return -1;
1542
1543 renderer->single_buffer = params->single_buffer;
1544
1545 renderer->base.read_pixels = rpi_renderer_read_pixels;
1546 renderer->base.repaint_output = rpi_renderer_repaint_output;
1547 renderer->base.flush_damage = rpi_renderer_flush_damage;
1548 renderer->base.attach = rpi_renderer_attach;
1549 renderer->base.create_surface = rpi_renderer_create_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001550 renderer->base.create_view = rpi_renderer_create_view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001551 renderer->base.surface_set_color = rpi_renderer_surface_set_color;
1552 renderer->base.destroy_surface = rpi_renderer_destroy_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001553 renderer->base.destroy_view = rpi_renderer_destroy_view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001554 renderer->base.destroy = rpi_renderer_destroy;
1555
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001556#ifdef ENABLE_EGL
1557 renderer->egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1558 if (renderer->egl_display == EGL_NO_DISPLAY) {
1559 weston_log("failed to create EGL display\n");
1560 return -1;
1561 }
1562
1563 if (!eglInitialize(renderer->egl_display, &major, &minor)) {
1564 weston_log("failed to initialize EGL display\n");
1565 return -1;
1566 }
1567
1568 renderer->bind_display =
1569 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1570 renderer->unbind_display =
1571 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1572 renderer->query_buffer =
1573 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1574
1575 extensions = (const char *) eglQueryString(renderer->egl_display,
1576 EGL_EXTENSIONS);
1577 if (!extensions) {
1578 weston_log("Retrieving EGL extension string failed.\n");
1579 return -1;
1580 }
1581
1582 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1583 renderer->has_bind_display = 1;
1584
1585 if (renderer->has_bind_display) {
1586 ret = renderer->bind_display(renderer->egl_display,
1587 compositor->wl_display);
1588 if (!ret)
1589 renderer->has_bind_display = 0;
1590 }
1591#endif
1592
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001593 compositor->renderer = &renderer->base;
1594 compositor->read_format = PIXMAN_a8r8g8b8;
1595 /* WESTON_CAP_ROTATION_ANY not supported */
1596
Tomeu Vizoso03681892013-08-06 20:05:57 +02001597 wl_display_add_shm_format(compositor->wl_display, WL_SHM_FORMAT_RGB565);
1598
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001599 return 0;
1600}
1601
1602WL_EXPORT int
1603rpi_renderer_output_create(struct weston_output *base,
1604 DISPMANX_DISPLAY_HANDLE_T display)
1605{
1606 struct rpir_output *output;
1607
1608 assert(base->renderer_state == NULL);
1609
1610 output = calloc(1, sizeof *output);
1611 if (!output)
1612 return -1;
1613
1614 output->display = display;
1615 output->update = DISPMANX_NO_HANDLE;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001616 wl_list_init(&output->view_list);
1617 wl_list_init(&output->view_cleanup_list);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001618 rpi_resource_init(&output->capture_buffer);
1619 base->renderer_state = output;
1620
1621 return 0;
1622}
1623
1624WL_EXPORT void
1625rpi_renderer_output_destroy(struct weston_output *base)
1626{
1627 struct rpir_output *output = to_rpir_output(base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001628 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001629 DISPMANX_UPDATE_HANDLE_T update;
1630
1631 rpi_resource_release(&output->capture_buffer);
1632 free(output->capture_data);
1633 output->capture_data = NULL;
1634
1635 update = vc_dispmanx_update_start(0);
1636 rpir_output_dmx_remove_all(output, update);
1637 vc_dispmanx_update_submit_sync(update);
1638
Jason Ekstranda7af7042013-10-12 22:38:11 -05001639 while (!wl_list_empty(&output->view_cleanup_list)) {
1640 view = container_of(output->view_cleanup_list.next,
1641 struct rpir_view, link);
1642 rpir_view_destroy(view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001643 }
1644
1645 free(output);
1646 base->renderer_state = NULL;
1647}
1648
1649WL_EXPORT void
1650rpi_renderer_set_update_handle(struct weston_output *base,
1651 DISPMANX_UPDATE_HANDLE_T handle)
1652{
1653 struct rpir_output *output = to_rpir_output(base);
1654
1655 output->update = handle;
1656}
1657
1658WL_EXPORT void
1659rpi_renderer_finish_frame(struct weston_output *base)
1660{
1661 struct rpir_output *output = to_rpir_output(base);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001662 struct weston_compositor *compositor = base->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001663 struct weston_view *wv;
1664 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001665
Jason Ekstranda7af7042013-10-12 22:38:11 -05001666 while (!wl_list_empty(&output->view_cleanup_list)) {
1667 view = container_of(output->view_cleanup_list.next,
1668 struct rpir_view, link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001669
Jason Ekstranda7af7042013-10-12 22:38:11 -05001670 if (view->view) {
1671 /* The weston_view still exists, but is
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001672 * temporarily not visible, and hence its Element
1673 * was removed. The current front buffer contents
1674 * must be preserved.
1675 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001676 if (!view->surface->visible_views)
1677 rpi_resource_release(view->surface->back);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001678
Jason Ekstranda7af7042013-10-12 22:38:11 -05001679 wl_list_remove(&view->link);
1680 wl_list_init(&view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001681 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682 rpir_view_destroy(view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001683 }
1684 }
1685
Jason Ekstranda7af7042013-10-12 22:38:11 -05001686 wl_list_for_each(wv, &compositor->view_list, link) {
1687 view = to_rpir_view(wv);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001688
Jason Ekstranda7af7042013-10-12 22:38:11 -05001689 if (view->surface->buffer_type != BUFFER_TYPE_EGL)
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001690 continue;
1691
Jason Ekstranda7af7042013-10-12 22:38:11 -05001692 if (view->surface->egl_old_front == NULL)
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001693 continue;
1694
Jason Ekstranda7af7042013-10-12 22:38:11 -05001695 weston_buffer_reference(&view->surface->egl_old_front->buffer_ref, NULL);
1696 free(view->surface->egl_old_front);
1697 view->surface->egl_old_front = NULL;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001698 }
1699
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001700 wl_signal_emit(&base->frame_signal, base);
1701}