blob: 0b99a604d14919b99f2270865d1461f9d689ebe9 [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
Tomeu Vizoso5c3ea3b2013-10-24 15:38:30 +0200374 wl_list_init(&surface->views);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500375 surface->visible_views = 0;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300376 surface->single_buffer = renderer->single_buffer;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300377 rpi_resource_init(&surface->resources[0]);
378 rpi_resource_init(&surface->resources[1]);
379 surface->front = &surface->resources[0];
380 if (surface->single_buffer)
381 surface->back = &surface->resources[0];
382 else
383 surface->back = &surface->resources[1];
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200384 surface->buffer_type = BUFFER_TYPE_NULL;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300385
386 pixman_region32_init(&surface->prev_damage);
387
388 return surface;
389}
390
391static void
392rpir_surface_destroy(struct rpir_surface *surface)
393{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500394 if (surface->visible_views)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300395 weston_log("ERROR rpi: destroying on-screen element\n");
396
Jason Ekstranda7af7042013-10-12 22:38:11 -0500397 assert(wl_list_empty(&surface->views));
398
399 if (surface->surface)
400 surface->surface->renderer_state = NULL;
401
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300402 pixman_region32_fini(&surface->prev_damage);
403 rpi_resource_release(&surface->resources[0]);
404 rpi_resource_release(&surface->resources[1]);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500405 DBG("rpir_surface %p destroyed (%u)\n", surface, surface->visible_views);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300406
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200407 if (surface->egl_back != NULL) {
408 weston_buffer_reference(&surface->egl_back->buffer_ref, NULL);
409 free(surface->egl_back);
410 surface->egl_back = NULL;
411 }
412
413 if (surface->egl_front != NULL) {
414 weston_buffer_reference(&surface->egl_front->buffer_ref, NULL);
415 free(surface->egl_front);
416 surface->egl_front = NULL;
417 }
418
419 if (surface->egl_old_front != NULL) {
420 weston_buffer_reference(&surface->egl_old_front->buffer_ref, NULL);
421 free(surface->egl_old_front);
422 surface->egl_old_front = NULL;
423 }
424
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300425 free(surface);
426}
427
428static int
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500429rpir_surface_damage(struct rpir_surface *surface, struct weston_buffer *buffer,
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300430 pixman_region32_t *damage)
431{
432 pixman_region32_t upload;
433 int ret;
434
435 if (!pixman_region32_not_empty(damage))
436 return 0;
437
438 DBG("rpir_surface %p update resource %p\n", surface, surface->back);
439
440 /* XXX: todo: if no surface->handle, update front buffer directly
441 * to avoid creating a new back buffer */
442 if (surface->single_buffer) {
443 ret = rpi_resource_update(surface->front, buffer, damage);
444 } else {
445 pixman_region32_init(&upload);
446 pixman_region32_union(&upload, &surface->prev_damage, damage);
447 ret = rpi_resource_update(surface->back, buffer, &upload);
448 pixman_region32_fini(&upload);
449 }
450
451 pixman_region32_copy(&surface->prev_damage, damage);
452 surface->need_swap = 1;
453
454 return ret;
455}
456
Jason Ekstranda7af7042013-10-12 22:38:11 -0500457static struct rpir_view *
458rpir_view_create(struct rpir_surface *surface)
459{
460 struct rpir_view *view;
461
462 view = calloc(1, sizeof *view);
463 if (!view)
464 return NULL;
465
466 view->surface = surface;
467 wl_list_insert(&surface->views, &view->surface_link);
468
469 wl_list_init(&view->link);
470 view->handle = DISPMANX_NO_HANDLE;
471
472 return view;
473}
474
475static void
476rpir_view_destroy(struct rpir_view *view)
477{
478 wl_list_remove(&view->link);
479
480 if (view->handle != DISPMANX_NO_HANDLE) {
481 view->surface->visible_views--;
482 weston_log("ERROR rpi: destroying on-screen element\n");
483 }
484
485 if (view->view)
486 view->view->renderer_state = NULL;
487
488 wl_list_remove(&view->surface_link);
489 if (wl_list_empty(&view->surface->views) && view->surface->surface == NULL)
490 rpir_surface_destroy(view->surface);
491
492 DBG("rpir_view %p destroyed (%d)\n", view, view->handle);
493
494 free(view);
495}
496
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300497static void
498matrix_type_str(struct weston_matrix *matrix, char *buf, int len)
499{
500 static const char types[33] = "TSRO";
501 unsigned mask = matrix->type;
502 int i = 0;
503
504 while (mask && i < len - 1) {
505 if (mask & (1u << i))
506 *buf++ = types[i];
507 mask &= ~(1u << i);
508 i++;
509 }
510 *buf = '\0';
511}
512
513static void
514log_print_matrix(struct weston_matrix *matrix)
515{
516 char typestr[6];
517 float *d = matrix->d;
518
519 matrix_type_str(matrix, typestr, sizeof typestr);
520 weston_log_continue("%14.6e %14.6e %14.6e %14.6e\n",
521 d[0], d[4], d[8], d[12]);
522 weston_log_continue("%14.6e %14.6e %14.6e %14.6e\n",
523 d[1], d[5], d[9], d[13]);
524 weston_log_continue("%14.6e %14.6e %14.6e %14.6e\n",
525 d[2], d[6], d[10], d[14]);
526 weston_log_continue("%14.6e %14.6e %14.6e %14.6e type: %s\n",
527 d[3], d[7], d[11], d[15], typestr);
528}
529
530static void
531warn_bad_matrix(struct weston_matrix *total, struct weston_matrix *output,
532 struct weston_matrix *surface)
533{
534 static int n_warn;
535 char typestr[6];
536
537 if (n_warn++ == 10)
538 weston_log("%s: not showing more warnings\n", __func__);
539
540 if (n_warn > 10)
541 return;
542
543 weston_log("%s: warning: total transformation is not renderable:\n",
544 __func__);
545 log_print_matrix(total);
546
547 matrix_type_str(surface, typestr, sizeof typestr);
548 weston_log_continue("surface matrix type: %s\n", typestr);
549 matrix_type_str(output, typestr, sizeof typestr);
550 weston_log_continue("output matrix type: %s\n", typestr);
551}
552
553/*#define SURFACE_TRANSFORM */
554
555static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500556rpir_view_compute_rects(struct rpir_view *view,
557 VC_RECT_T *src_rect, VC_RECT_T *dst_rect,
558 VC_IMAGE_TRANSFORM_T *flipmask)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300559{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500560 struct weston_output *output_base = view->view->surface->output;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300561 struct rpir_output *output = to_rpir_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500562 struct weston_matrix matrix = view->view->transform.matrix;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300563 VC_IMAGE_TRANSFORM_T flipt = 0;
564 int src_x, src_y;
565 int dst_x, dst_y;
566 int src_width, src_height;
567 int dst_width, dst_height;
568 struct weston_vector p1 = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
569 struct weston_vector p2 = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
570 int t;
571 int over;
572
573 /* XXX: take buffer transform into account */
574
575 /* src is in 16.16, dst is in 32.0 fixed point.
576 * Negative values are not allowed in VC_RECT_T.
577 * Clip size to output boundaries, firmware ignores
578 * huge elements like 8192x8192.
579 */
580
581 src_x = 0 << 16;
582 src_y = 0 << 16;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200583
Jason Ekstranda7af7042013-10-12 22:38:11 -0500584 if (view->surface->buffer_type == BUFFER_TYPE_EGL) {
585 struct weston_buffer *buffer =
586 view->surface->egl_front->buffer_ref.buffer;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200587
588 src_width = buffer->width << 16;
589 src_height = buffer->height << 16;
590 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500591 src_width = view->surface->front->width << 16;
592 src_height = view->surface->front->height << 16;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200593 }
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300594
595 weston_matrix_multiply(&matrix, &output->matrix);
596
597#ifdef SURFACE_TRANSFORM
598 if (matrix.type >= WESTON_MATRIX_TRANSFORM_OTHER) {
599#else
600 if (matrix.type >= WESTON_MATRIX_TRANSFORM_ROTATE) {
601#endif
602 warn_bad_matrix(&matrix, &output->matrix,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500603 &view->view->transform.matrix);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300604 } else {
605 if (matrix.type & WESTON_MATRIX_TRANSFORM_ROTATE) {
606 if (fabsf(matrix.d[0]) < 1e-4f &&
607 fabsf(matrix.d[5]) < 1e-4f) {
608 flipt |= TRANSFORM_TRANSPOSE;
609 } else if (fabsf(matrix.d[1]) < 1e-4 &&
610 fabsf(matrix.d[4]) < 1e-4) {
611 /* no transpose */
612 } else {
613 warn_bad_matrix(&matrix, &output->matrix,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500614 &view->view->transform.matrix);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300615 }
616 }
617 }
618
Jason Ekstranda7af7042013-10-12 22:38:11 -0500619 p2.f[0] = view->view->geometry.width;
620 p2.f[1] = view->view->geometry.height;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300621
622 /* transform top-left and bot-right corner into screen coordinates */
623 weston_matrix_transform(&matrix, &p1);
624 weston_matrix_transform(&matrix, &p2);
625
626 /* Compute the destination rectangle on screen, converting
627 * negative dimensions to flips.
628 */
629
630 dst_width = round(p2.f[0] - p1.f[0]);
631 if (dst_width < 0) {
632 dst_x = round(p2.f[0]);
633 dst_width = -dst_width;
634
635 if (!(flipt & TRANSFORM_TRANSPOSE))
636 flipt |= TRANSFORM_HFLIP;
637 else
638 flipt |= TRANSFORM_VFLIP;
639 } else {
640 dst_x = round(p1.f[0]);
641 }
642
643 dst_height = round(p2.f[1] - p1.f[1]);
644 if (dst_height < 0) {
645 dst_y = round(p2.f[1]);
646 dst_height = -dst_height;
647
648 if (!(flipt & TRANSFORM_TRANSPOSE))
649 flipt |= TRANSFORM_VFLIP;
650 else
651 flipt |= TRANSFORM_HFLIP;
652 } else {
653 dst_y = round(p1.f[1]);
654 }
655
656 if (dst_width == 0 || dst_height == 0) {
657 DBG("ignored, zero surface area before clipping\n");
658 return -1;
659 }
660
661#ifdef SURFACE_TRANSFORM
662 /* Dispmanx works as if you flipped the whole screen, when
663 * you flip an element. But, we want to flip an element in place.
664 * XXX: fixme
665 */
666 if (flipt & TRANSFORM_HFLIP)
667 dst_x = output_base->width - dst_x;
668 if (flipt & TRANSFORM_VFLIP)
669 dst_y = output_base->height - dst_y;
670 if (flipt & TRANSFORM_TRANSPOSE) {
671 int_swap(&dst_x, &dst_y);
672 int_swap(&dst_width, &dst_height);
673 }
674#else
675 switch (output_base->transform) {
676 case WL_OUTPUT_TRANSFORM_FLIPPED:
677 flipt = TRANSFORM_HFLIP;
678 break;
679 case WL_OUTPUT_TRANSFORM_NORMAL:
680 flipt = 0;
681 break;
682
683 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
684 flipt = TRANSFORM_HFLIP | TRANSFORM_VFLIP | TRANSFORM_TRANSPOSE;
685 break;
686 case WL_OUTPUT_TRANSFORM_90:
687 flipt = TRANSFORM_VFLIP | TRANSFORM_TRANSPOSE;
688 break;
689 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
690 flipt = TRANSFORM_VFLIP;
691 break;
692 case WL_OUTPUT_TRANSFORM_180:
693 flipt = TRANSFORM_HFLIP | TRANSFORM_VFLIP;
694 break;
695
696 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
697 flipt = TRANSFORM_TRANSPOSE;
698 break;
699 case WL_OUTPUT_TRANSFORM_270:
700 flipt = TRANSFORM_HFLIP | TRANSFORM_TRANSPOSE;
701 break;
702 default:
703 break;
704 }
705#endif
706
707 /* clip destination rectangle to screen dimensions */
708
709 if (dst_x < 0) {
710 t = (int64_t)dst_x * src_width / dst_width;
711 src_width += t;
712 dst_width += dst_x;
713 src_x -= t;
714 dst_x = 0;
715 }
716
717 if (dst_y < 0) {
718 t = (int64_t)dst_y * src_height / dst_height;
719 src_height += t;
720 dst_height += dst_y;
721 src_y -= t;
722 dst_y = 0;
723 }
724
725 over = dst_x + dst_width - output_base->width;
726 if (over > 0) {
727 t = (int64_t)over * src_width / dst_width;
728 src_width -= t;
729 dst_width -= over;
730 }
731
732 over = dst_y + dst_height - output_base->height;
733 if (over > 0) {
734 t = (int64_t)over * src_height / dst_height;
735 src_height -= t;
736 dst_height -= over;
737 }
738
739 src_width = int_max(src_width, 0);
740 src_height = int_max(src_height, 0);
741
Jason Ekstranda7af7042013-10-12 22:38:11 -0500742 DBG("rpir_view %p %dx%d: p1 %f, %f; p2 %f, %f\n", view,
743 view->view->geometry.width,
744 view->view->geometry.height,
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300745 p1.f[0], p1.f[1], p2.f[0], p2.f[1]);
746 DBG("src rect %d;%d, %d;%d, %d;%dx%d;%d\n",
747 src_x >> 16, src_x & 0xffff,
748 src_y >> 16, src_y & 0xffff,
749 src_width >> 16, src_width & 0xffff,
750 src_height >> 16, src_height & 0xffff);
751 DBG("dest rect %d, %d, %dx%d%s%s%s\n",
752 dst_x, dst_y, dst_width, dst_height,
753 (flipt & TRANSFORM_HFLIP) ? " hflip" : "",
754 (flipt & TRANSFORM_VFLIP) ? " vflip" : "",
755 (flipt & TRANSFORM_TRANSPOSE) ? " transp" : "");
756
757 assert(src_x >= 0);
758 assert(src_y >= 0);
759 assert(dst_x >= 0);
760 assert(dst_y >= 0);
761
762 if (dst_width < 1 || dst_height < 1) {
763 DBG("ignored, zero surface area after clipping\n");
764 return -1;
765 }
766
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200767 /* EGL buffers will be upside-down related to what DispmanX expects */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500768 if (view->surface->buffer_type == BUFFER_TYPE_EGL)
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200769 flipt ^= TRANSFORM_VFLIP;
770
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300771 vc_dispmanx_rect_set(src_rect, src_x, src_y, src_width, src_height);
772 vc_dispmanx_rect_set(dst_rect, dst_x, dst_y, dst_width, dst_height);
773 *flipmask = flipt;
774
775 return 0;
776}
777
778static DISPMANX_TRANSFORM_T
779vc_image2dispmanx_transform(VC_IMAGE_TRANSFORM_T t)
780{
781 /* XXX: uhh, are these right? */
782 switch (t) {
783 case VC_IMAGE_ROT0:
784 return DISPMANX_NO_ROTATE;
785 case VC_IMAGE_MIRROR_ROT0:
786 return DISPMANX_FLIP_HRIZ;
787 case VC_IMAGE_MIRROR_ROT180:
788 return DISPMANX_FLIP_VERT;
789 case VC_IMAGE_ROT180:
790 return DISPMANX_ROTATE_180;
791 case VC_IMAGE_MIRROR_ROT90:
792 return DISPMANX_ROTATE_90 | DISPMANX_FLIP_HRIZ;
793 case VC_IMAGE_ROT270:
794 return DISPMANX_ROTATE_270;
795 case VC_IMAGE_ROT90:
796 return DISPMANX_ROTATE_90;
797 case VC_IMAGE_MIRROR_ROT270:
798 return DISPMANX_ROTATE_270 | DISPMANX_FLIP_VERT;
799 default:
800 assert(0 && "bad VC_IMAGE_TRANSFORM_T");
801 return DISPMANX_NO_ROTATE;
802 }
803}
804
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200805
806static DISPMANX_RESOURCE_HANDLE_T
807rpir_surface_get_resource(struct rpir_surface *surface)
808{
809 switch (surface->buffer_type) {
810 case BUFFER_TYPE_SHM:
811 case BUFFER_TYPE_NULL:
812 return surface->front->handle;
813 case BUFFER_TYPE_EGL:
814 if (surface->egl_front != NULL)
815 return surface->egl_front->resource_handle;
816 default:
817 return DISPMANX_NO_HANDLE;
818 }
819}
820
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300821static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500822rpir_view_dmx_add(struct rpir_view *view, struct rpir_output *output,
823 DISPMANX_UPDATE_HANDLE_T update, int layer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300824{
825 /* Do not use DISPMANX_FLAGS_ALPHA_PREMULT here.
826 * If you define PREMULT and ALPHA_MIX, the hardware will not
827 * multiply the source color with the element alpha, leading to
828 * bad colors. Instead, we define PREMULT during pixel data upload.
829 */
830 VC_DISPMANX_ALPHA_T alphasetup = {
831 DISPMANX_FLAGS_ALPHA_FROM_SOURCE |
832 DISPMANX_FLAGS_ALPHA_MIX,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500833 float2uint8(view->view->alpha), /* opacity 0-255 */
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300834 0 /* mask resource handle */
835 };
836 VC_RECT_T dst_rect;
837 VC_RECT_T src_rect;
838 VC_IMAGE_TRANSFORM_T flipmask;
839 int ret;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200840 DISPMANX_RESOURCE_HANDLE_T resource_handle;
841
Jason Ekstranda7af7042013-10-12 22:38:11 -0500842 resource_handle = rpir_surface_get_resource(view->surface);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200843 if (resource_handle == DISPMANX_NO_HANDLE) {
844 weston_log("%s: no buffer yet, aborting\n", __func__);
845 return 0;
846 }
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300847
Jason Ekstranda7af7042013-10-12 22:38:11 -0500848 ret = rpir_view_compute_rects(view, &src_rect, &dst_rect, &flipmask);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300849 if (ret < 0)
850 return 0;
851
Jason Ekstranda7af7042013-10-12 22:38:11 -0500852 view->handle = vc_dispmanx_element_add(
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300853 update,
854 output->display,
855 layer,
856 &dst_rect,
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200857 resource_handle,
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300858 &src_rect,
859 DISPMANX_PROTECTION_NONE,
860 &alphasetup,
861 NULL /* clamp */,
862 vc_image2dispmanx_transform(flipmask));
Jason Ekstranda7af7042013-10-12 22:38:11 -0500863 DBG("rpir_surface %p add %u, alpha %f resource %d\n", view,
864 view->handle, view->view->alpha, resource_handle);
865
866 if (view->handle == DISPMANX_NO_HANDLE)
867 return -1;
868
869 view->surface->visible_views++;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300870
871 return 1;
872}
873
874static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500875rpir_view_dmx_swap(struct rpir_view *view,
876 DISPMANX_UPDATE_HANDLE_T update)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300877{
878 VC_RECT_T rect;
879 pixman_box32_t *r;
880
881 /* XXX: skip, iff resource was not reallocated, and single-buffering */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500882 vc_dispmanx_element_change_source(update, view->handle,
883 view->surface->front->handle);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300884
885 /* This is current damage now, after rpir_surface_damage() */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500886 r = pixman_region32_extents(&view->surface->prev_damage);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300887
888 vc_dispmanx_rect_set(&rect, r->x1, r->y1,
889 r->x2 - r->x1, r->y2 - r->y1);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500890 vc_dispmanx_element_modified(update, view->handle, &rect);
891 DBG("rpir_view %p swap\n", view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300892}
893
894static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500895rpir_view_dmx_move(struct rpir_view *view,
896 DISPMANX_UPDATE_HANDLE_T update, int layer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300897{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500898 uint8_t alpha = float2uint8(view->view->alpha);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300899 VC_RECT_T dst_rect;
900 VC_RECT_T src_rect;
901 VC_IMAGE_TRANSFORM_T flipmask;
902 int ret;
903
904 /* XXX: return early, if all attributes stay the same */
905
Jason Ekstranda7af7042013-10-12 22:38:11 -0500906 if (view->surface->buffer_type == BUFFER_TYPE_EGL) {
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200907 DISPMANX_RESOURCE_HANDLE_T resource_handle;
908
Jason Ekstranda7af7042013-10-12 22:38:11 -0500909 resource_handle = rpir_surface_get_resource(view->surface);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200910 if (resource_handle == DISPMANX_NO_HANDLE) {
911 weston_log("%s: no buffer yet, aborting\n", __func__);
912 return 0;
913 }
914
915 vc_dispmanx_element_change_source(update,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500916 view->handle,
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200917 resource_handle);
918 }
919
Jason Ekstranda7af7042013-10-12 22:38:11 -0500920 ret = rpir_view_compute_rects(view, &src_rect, &dst_rect, &flipmask);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300921 if (ret < 0)
922 return 0;
923
924 ret = vc_dispmanx_element_change_attributes(
925 update,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500926 view->handle,
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300927 ELEMENT_CHANGE_LAYER |
928 ELEMENT_CHANGE_OPACITY |
929 ELEMENT_CHANGE_TRANSFORM |
930 ELEMENT_CHANGE_DEST_RECT |
931 ELEMENT_CHANGE_SRC_RECT,
932 layer,
933 alpha,
934 &dst_rect,
935 &src_rect,
936 DISPMANX_NO_HANDLE,
937 /* This really is DISPMANX_TRANSFORM_T, no matter
938 * what the header says. */
939 vc_image2dispmanx_transform(flipmask));
Jason Ekstranda7af7042013-10-12 22:38:11 -0500940 DBG("rpir_view %p move\n", view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300941
942 if (ret)
943 return -1;
944
945 return 1;
946}
947
948static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500949rpir_view_dmx_remove(struct rpir_view *view,
950 DISPMANX_UPDATE_HANDLE_T update)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300951{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500952 if (view->handle == DISPMANX_NO_HANDLE)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300953 return;
954
Jason Ekstranda7af7042013-10-12 22:38:11 -0500955 vc_dispmanx_element_remove(update, view->handle);
956 DBG("rpir_view %p remove %u\n", view, view->handle);
957 view->handle = DISPMANX_NO_HANDLE;
958 view->surface->visible_views--;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300959}
960
961static void
962rpir_surface_swap_pointers(struct rpir_surface *surface)
963{
964 struct rpi_resource *tmp;
965
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200966 if (surface->buffer_type == BUFFER_TYPE_EGL) {
967 if (surface->egl_back != NULL) {
968 assert(surface->egl_old_front == NULL);
969 surface->egl_old_front = surface->egl_front;
970 surface->egl_front = surface->egl_back;
971 surface->egl_back = NULL;
972 }
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200973 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500974 tmp = surface->front;
975 surface->front = surface->back;
976 surface->back = tmp;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200977 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500978 DBG("new back %p, new front %p\n", surface->back, surface->front);
979}
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300980
Jason Ekstranda7af7042013-10-12 22:38:11 -0500981static int
982is_view_not_visible(struct weston_view *view)
983{
984 /* Return true, if surface is guaranteed to be totally obscured. */
985 int ret;
986 pixman_region32_t unocc;
987
988 pixman_region32_init(&unocc);
989 pixman_region32_subtract(&unocc, &view->transform.boundingbox,
990 &view->clip);
991 ret = !pixman_region32_not_empty(&unocc);
992 pixman_region32_fini(&unocc);
993
994 return ret;
995}
996
997static void
998rpir_view_update(struct rpir_view *view, struct rpir_output *output,
999 DISPMANX_UPDATE_HANDLE_T update, int layer)
1000{
1001 int ret;
1002 int obscured;
1003
1004
1005 obscured = is_view_not_visible(view->view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001006 if (obscured) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001007 DBG("rpir_view %p totally obscured.\n", view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001008
Jason Ekstranda7af7042013-10-12 22:38:11 -05001009 wl_list_remove(&view->link);
1010 if (view->handle == DISPMANX_NO_HANDLE) {
1011 wl_list_init(&view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001012 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001013 rpir_view_dmx_remove(view, update);
1014 wl_list_insert(&output->view_cleanup_list,
1015 &view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001016 }
1017
1018 goto out;
1019 }
1020
Jason Ekstranda7af7042013-10-12 22:38:11 -05001021 if (view->handle == DISPMANX_NO_HANDLE) {
1022 ret = rpir_view_dmx_add(view, output, update, layer);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001023 if (ret == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001024 wl_list_remove(&view->link);
1025 wl_list_init(&view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001026 } else if (ret < 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 weston_log("ERROR rpir_view_dmx_add() failed.\n");
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001028 }
1029 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001030 if (view->surface->need_swap)
1031 rpir_view_dmx_swap(view, update);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001032
Jason Ekstranda7af7042013-10-12 22:38:11 -05001033 ret = rpir_view_dmx_move(view, update, layer);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001034 if (ret == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001035 rpir_view_dmx_remove(view, update);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001036
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037 wl_list_remove(&view->link);
1038 wl_list_insert(&output->view_cleanup_list,
1039 &view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001040 } else if (ret < 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001041 weston_log("ERROR rpir_view_dmx_move() failed.\n");
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001042 }
1043 }
1044
1045out:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001046 view->layer = layer;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001047}
1048
1049static int
1050rpi_renderer_read_pixels(struct weston_output *base,
1051 pixman_format_code_t format, void *pixels,
1052 uint32_t x, uint32_t y,
1053 uint32_t width, uint32_t height)
1054{
1055 struct rpir_output *output = to_rpir_output(base);
1056 struct rpi_resource *buffer = &output->capture_buffer;
1057 VC_RECT_T rect;
1058 uint32_t fb_width, fb_height;
1059 uint32_t dst_pitch;
1060 uint32_t i;
1061 int ret;
1062
Hardeningff39efa2013-09-18 23:56:35 +02001063 fb_width = base->current_mode->width;
1064 fb_height = base->current_mode->height;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001065
1066 DBG("%s(%u, %u, %u, %u), resource %p\n", __func__,
1067 x, y, width, height, buffer);
1068
1069 if (format != PIXMAN_a8r8g8b8) {
1070 weston_log("rpi-renderer error: bad read_format\n");
1071 return -1;
1072 }
1073
1074 dst_pitch = fb_width * 4;
1075
1076 if (buffer->handle == DISPMANX_NO_HANDLE) {
1077 free(output->capture_data);
1078 output->capture_data = NULL;
1079
1080 ret = rpi_resource_realloc(buffer, VC_IMAGE_ARGB8888,
1081 fb_width, fb_height,
1082 dst_pitch, fb_height);
1083 if (ret < 0) {
1084 weston_log("rpi-renderer error: "
1085 "allocating read buffer failed\n");
1086 return -1;
1087 }
1088
1089 ret = vc_dispmanx_snapshot(output->display, buffer->handle,
1090 VC_IMAGE_ROT0);
1091 if (ret) {
1092 weston_log("rpi-renderer error: "
1093 "vc_dispmanx_snapshot returned %d\n", ret);
1094 return -1;
1095 }
1096 DBG("%s: snapshot done.\n", __func__);
1097 }
1098
1099 /*
1100 * If vc_dispmanx_resource_read_data was able to read sub-rectangles,
1101 * we could read directly into 'pixels'. But it cannot, it does not
1102 * use rect.x or rect.width, and does this:
1103 * host_start = (uint8_t *)dst_address + (dst_pitch * p_rect->y);
1104 * In other words, it is only good for reading the full buffer in
1105 * one go.
1106 */
1107 vc_dispmanx_rect_set(&rect, 0, 0, fb_width, fb_height);
1108
1109 if (x == 0 && y == 0 && width == fb_width && height == fb_height) {
1110 ret = vc_dispmanx_resource_read_data(buffer->handle, &rect,
1111 pixels, dst_pitch);
1112 if (ret) {
1113 weston_log("rpi-renderer error: "
1114 "resource_read_data returned %d\n", ret);
1115 return -1;
1116 }
1117 DBG("%s: full frame done.\n", __func__);
1118 return 0;
1119 }
1120
1121 if (!output->capture_data) {
1122 output->capture_data = malloc(fb_height * dst_pitch);
1123 if (!output->capture_data) {
1124 weston_log("rpi-renderer error: "
1125 "out of memory\n");
1126 return -1;
1127 }
1128
1129 ret = vc_dispmanx_resource_read_data(buffer->handle, &rect,
1130 output->capture_data,
1131 dst_pitch);
1132 if (ret) {
1133 weston_log("rpi-renderer error: "
1134 "resource_read_data returned %d\n", ret);
1135 return -1;
1136 }
1137 }
1138
1139 for (i = 0; i < height; i++) {
1140 uint8_t *src = output->capture_data +
1141 (y + i) * dst_pitch + x * 4;
1142 uint8_t *dst = (uint8_t *)pixels + i * width * 4;
1143 memcpy(dst, src, width * 4);
1144 }
1145
1146 return 0;
1147}
1148
1149static void
1150rpir_output_dmx_remove_all(struct rpir_output *output,
1151 DISPMANX_UPDATE_HANDLE_T update)
1152{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001153 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001154
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155 while (!wl_list_empty(&output->view_list)) {
1156 view = container_of(output->view_list.next,
1157 struct rpir_view, link);
1158 rpir_view_dmx_remove(view, update);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001159
Jason Ekstranda7af7042013-10-12 22:38:11 -05001160 wl_list_remove(&view->link);
1161 wl_list_insert(&output->view_cleanup_list, &view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001162 }
1163}
1164
1165static void
1166output_compute_matrix(struct weston_output *base)
1167{
1168 struct rpir_output *output = to_rpir_output(base);
1169 struct weston_matrix *matrix = &output->matrix;
1170 const float half_w = 0.5f * base->width;
1171 const float half_h = 0.5f * base->height;
1172 float mag;
1173 float dx, dy;
1174
1175 weston_matrix_init(matrix);
1176 weston_matrix_translate(matrix, -base->x, -base->y, 0.0f);
1177
1178#ifdef SURFACE_TRANSFORM
1179 weston_matrix_translate(matrix, -half_w, -half_h, 0.0f);
1180 switch (base->transform) {
1181 case WL_OUTPUT_TRANSFORM_FLIPPED:
1182 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1183 case WL_OUTPUT_TRANSFORM_NORMAL:
1184 /* weston_matrix_rotate_xy(matrix, 1.0f, 0.0f); no-op */
1185 weston_matrix_translate(matrix, half_w, half_h, 0.0f);
1186 break;
1187
1188 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1189 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1190 case WL_OUTPUT_TRANSFORM_90:
1191 weston_matrix_rotate_xy(matrix, 0.0f, 1.0f);
1192 weston_matrix_translate(matrix, half_h, half_w, 0.0f);
1193 break;
1194
1195 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1196 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1197 case WL_OUTPUT_TRANSFORM_180:
1198 weston_matrix_rotate_xy(matrix, -1.0f, 0.0f);
1199 weston_matrix_translate(matrix, half_w, half_h, 0.0f);
1200 break;
1201
1202 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1203 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1204 case WL_OUTPUT_TRANSFORM_270:
1205 weston_matrix_rotate_xy(matrix, 0.0f, -1.0f);
1206 weston_matrix_translate(matrix, half_h, half_w, 0.0f);
1207 break;
1208
1209 default:
1210 break;
1211 }
1212#endif
1213
1214 if (base->zoom.active) {
1215 /* The base->zoom stuff is in GL coordinate system */
1216 mag = 1.0f / (1.0f - base->zoom.spring_z.current);
1217 dx = -(base->zoom.trans_x + 1.0f) * half_w;
1218 dy = -(base->zoom.trans_y + 1.0f) * half_h;
1219 weston_matrix_translate(matrix, dx, dy, 0.0f);
1220 weston_matrix_scale(matrix, mag, mag, 1.0f);
1221 weston_matrix_translate(matrix, half_w, half_h, 0.0f);
1222 }
1223}
1224
1225/* Note: this won't work right for multiple outputs. A DispmanX Element
1226 * is tied to one DispmanX Display, i.e. output.
1227 */
1228static void
1229rpi_renderer_repaint_output(struct weston_output *base,
1230 pixman_region32_t *output_damage)
1231{
1232 struct weston_compositor *compositor = base->compositor;
1233 struct rpir_output *output = to_rpir_output(base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001234 struct weston_view *wv;
1235 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001236 struct wl_list done_list;
1237 int layer = 1;
1238
1239 assert(output->update != DISPMANX_NO_HANDLE);
1240
1241 output_compute_matrix(base);
1242
1243 rpi_resource_release(&output->capture_buffer);
1244 free(output->capture_data);
1245 output->capture_data = NULL;
1246
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 /* Swap resources on surfaces as needed */
1248 wl_list_for_each_reverse(wv, &compositor->view_list, link)
1249 wv->surface->touched = 0;
1250
1251 wl_list_for_each_reverse(wv, &compositor->view_list, link) {
1252 view = to_rpir_view(wv);
1253
1254 if (!wv->surface->touched) {
1255 wv->surface->touched = 1;
1256
1257 if (view->surface->need_swap)
1258 rpir_surface_swap_pointers(view->surface);
1259 }
1260
1261 if (view->surface->egl_front->buffer_ref.buffer == NULL) {
1262 weston_log("warning: client unreffed current front buffer\n");
1263
1264 wl_list_remove(&view->link);
1265 if (view->handle == DISPMANX_NO_HANDLE) {
1266 wl_list_init(&view->link);
1267 } else {
1268 rpir_view_dmx_remove(view, output->update);
1269 wl_list_insert(&output->view_cleanup_list,
1270 &view->link);
1271 }
1272 }
1273 }
1274
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001275 /* update all renderable surfaces */
1276 wl_list_init(&done_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 wl_list_for_each_reverse(wv, &compositor->view_list, link) {
1278 if (wv->plane != &compositor->primary_plane)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001279 continue;
1280
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 view = to_rpir_view(wv);
1282 assert(!wl_list_empty(&view->link) ||
1283 view->handle == DISPMANX_NO_HANDLE);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001284
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285 wl_list_remove(&view->link);
1286 wl_list_insert(&done_list, &view->link);
1287 rpir_view_update(view, output, output->update, layer++);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001288 }
1289
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290 /* Mark all surfaces as swapped */
1291 wl_list_for_each_reverse(wv, &compositor->view_list, link)
1292 to_rpir_surface(wv->surface)->need_swap = 0;
1293
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001294 /* Remove all surfaces that are still on screen, but were
1295 * not rendered this time.
1296 */
1297 rpir_output_dmx_remove_all(output, output->update);
1298
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 wl_list_insert_list(&output->view_list, &done_list);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001300 output->update = DISPMANX_NO_HANDLE;
1301
1302 /* The frame_signal is emitted in rpi_renderer_finish_frame(),
1303 * so that the firmware can capture the up-to-date contents.
1304 */
1305}
1306
1307static void
1308rpi_renderer_flush_damage(struct weston_surface *base)
1309{
1310 /* Called for every surface just before repainting it, if
1311 * having an shm buffer.
1312 */
1313 struct rpir_surface *surface = to_rpir_surface(base);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001314 struct weston_buffer *buffer = surface->buffer_ref.buffer;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001315 int ret;
1316
1317 assert(buffer);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001318 assert(wl_shm_buffer_get(buffer->resource));
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001319
1320 ret = rpir_surface_damage(surface, buffer, &base->damage);
1321 if (ret)
1322 weston_log("%s error: updating Dispmanx resource failed.\n",
1323 __func__);
1324
1325 weston_buffer_reference(&surface->buffer_ref, NULL);
1326}
1327
1328static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001329rpi_renderer_attach(struct weston_surface *base, struct weston_buffer *buffer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001330{
1331 /* Called every time a client commits an attach. */
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001332 struct rpir_surface *surface = to_rpir_surface(base);
1333
1334 assert(surface);
1335 if (!surface)
1336 return;
1337
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001338 if (surface->buffer_type == BUFFER_TYPE_SHM) {
1339 if (!surface->single_buffer)
1340 /* XXX: need to check if in middle of update */
1341 rpi_resource_release(surface->back);
1342
Jason Ekstranda7af7042013-10-12 22:38:11 -05001343 if (!surface->visible_views)
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001344 /* XXX: cannot do this, if middle of an update */
1345 rpi_resource_release(surface->front);
1346
1347 weston_buffer_reference(&surface->buffer_ref, NULL);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001348 }
1349
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001350 /* If buffer is NULL, Weston core unmaps the surface, the surface
1351 * will not appear in repaint list, and so rpi_renderer_repaint_output
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001352 * will remove the DispmanX element. Later, for SHM, also the front
1353 * buffer will be released in the cleanup_list processing.
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001354 */
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001355 if (!buffer)
1356 return;
1357
1358 if (wl_shm_buffer_get(buffer->resource)) {
1359 surface->buffer_type = BUFFER_TYPE_SHM;
1360 buffer->shm_buffer = wl_shm_buffer_get(buffer->resource);
1361 buffer->width = wl_shm_buffer_get_width(buffer->shm_buffer);
1362 buffer->height = wl_shm_buffer_get_height(buffer->shm_buffer);
1363
1364 weston_buffer_reference(&surface->buffer_ref, buffer);
1365 } else {
1366#if ENABLE_EGL
1367 struct rpi_renderer *renderer = to_rpi_renderer(base->compositor);
Tomeu Vizosoa8e5f292013-10-09 11:29:45 +02001368 struct wl_resource *wl_resource = buffer->resource;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001369
1370 if (!renderer->has_bind_display ||
1371 !renderer->query_buffer(renderer->egl_display,
1372 wl_resource,
1373 EGL_WIDTH, &buffer->width)) {
1374 weston_log("unhandled buffer type!\n");
1375 weston_buffer_reference(&surface->buffer_ref, NULL);
1376 surface->buffer_type = BUFFER_TYPE_NULL;
1377 }
1378
1379 renderer->query_buffer(renderer->egl_display,
1380 wl_resource,
1381 EGL_HEIGHT, &buffer->height);
1382
1383 surface->buffer_type = BUFFER_TYPE_EGL;
1384
1385 if(surface->egl_back == NULL)
1386 surface->egl_back = calloc(1, sizeof *surface->egl_back);
1387
1388 weston_buffer_reference(&surface->egl_back->buffer_ref, buffer);
1389 surface->egl_back->resource_handle =
1390 vc_dispmanx_get_handle_from_wl_buffer(wl_resource);
1391#else
1392 weston_log("unhandled buffer type!\n");
1393 weston_buffer_reference(&surface->buffer_ref, NULL);
1394 surface->buffer_type = BUFFER_TYPE_NULL;
1395#endif
1396 }
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001397}
1398
1399static int
1400rpi_renderer_create_surface(struct weston_surface *base)
1401{
1402 struct rpi_renderer *renderer = to_rpi_renderer(base->compositor);
1403 struct rpir_surface *surface;
1404
1405 assert(base->renderer_state == NULL);
1406
1407 surface = rpir_surface_create(renderer);
1408 if (!surface)
1409 return -1;
1410
1411 surface->surface = base;
1412 base->renderer_state = surface;
1413 return 0;
1414}
1415
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416static int
1417rpi_renderer_create_view(struct weston_view *base)
1418{
1419 struct rpir_surface *surface = to_rpir_surface(base->surface);
1420 struct rpir_view *view;
1421
1422 assert(base->renderer_state == NULL);
1423
1424 view = rpir_view_create(surface);
1425 if (!view)
1426 return -1;
1427
1428 view->view = base;
1429 base->renderer_state = view;
1430 return 0;
1431}
1432
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001433static void
1434rpi_renderer_surface_set_color(struct weston_surface *base,
1435 float red, float green, float blue, float alpha)
1436{
1437 struct rpir_surface *surface = to_rpir_surface(base);
1438 uint8_t color[4];
1439 VC_RECT_T rect;
1440 int ret;
1441
1442 assert(surface);
1443
1444 ret = rpi_resource_realloc(surface->back, VC_IMAGE_ARGB8888,
1445 1, 1, 4, 1);
1446 if (ret < 0) {
1447 weston_log("Error: %s: rpi_resource_realloc failed.\n",
1448 __func__);
1449 return;
1450 }
1451
1452 color[0] = float2uint8(blue);
1453 color[1] = float2uint8(green);
1454 color[2] = float2uint8(red);
1455 color[3] = float2uint8(alpha);
1456
1457 vc_dispmanx_rect_set(&rect, 0, 0, 1, 1);
1458 ret = vc_dispmanx_resource_write_data(surface->back->handle,
1459 VC_IMAGE_ARGB8888,
1460 4, color, &rect);
1461 if (ret) {
1462 weston_log("Error: %s: resource_write_data failed.\n",
1463 __func__);
1464 return;
1465 }
1466
1467 DBG("%s: resource %p solid color BGRA %u,%u,%u,%u\n", __func__,
1468 surface->back, color[0], color[1], color[2], color[3]);
1469
1470 /*pixman_region32_copy(&surface->prev_damage, damage);*/
1471 surface->need_swap = 1;
1472}
1473
1474static void
1475rpi_renderer_destroy_surface(struct weston_surface *base)
1476{
1477 struct rpir_surface *surface = to_rpir_surface(base);
1478
1479 assert(surface);
1480 assert(surface->surface == base);
1481 if (!surface)
1482 return;
1483
1484 surface->surface = NULL;
1485 base->renderer_state = NULL;
1486
Jason Ekstranda7af7042013-10-12 22:38:11 -05001487 if (wl_list_empty(&surface->views))
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001488 rpir_surface_destroy(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001489}
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001490
Jason Ekstranda7af7042013-10-12 22:38:11 -05001491static void
1492rpi_renderer_destroy_view(struct weston_view *base)
1493{
1494 struct rpir_view *view = to_rpir_view(base);
1495
1496 assert(view);
1497 assert(view->view == base);
1498 if (!view)
1499 return;
1500
1501 /* If guaranteed to not be on screen, just detroy it. */
1502 if (wl_list_empty(&view->link))
1503 rpir_view_destroy(view);
1504
1505 /* Otherwise, the view is either on screen and needs
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001506 * to be removed by a repaint update, or it is in the
Jason Ekstranda7af7042013-10-12 22:38:11 -05001507 * view_cleanup_list, and will be destroyed by
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001508 * rpi_renderer_finish_frame().
1509 */
1510}
1511
1512static void
1513rpi_renderer_destroy(struct weston_compositor *compositor)
1514{
1515 struct rpi_renderer *renderer = to_rpi_renderer(compositor);
1516
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001517#if ENABLE_EGL
1518 if (renderer->has_bind_display)
1519 renderer->unbind_display(renderer->egl_display,
1520 compositor->wl_display);
1521#endif
1522
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001523 free(renderer);
1524 compositor->renderer = NULL;
1525}
1526
1527WL_EXPORT int
1528rpi_renderer_create(struct weston_compositor *compositor,
1529 const struct rpi_renderer_parameters *params)
1530{
1531 struct rpi_renderer *renderer;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001532#if ENABLE_EGL
1533 const char *extensions;
1534 EGLBoolean ret;
1535 EGLint major, minor;
1536#endif
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001537
1538 weston_log("Initializing the DispmanX compositing renderer\n");
1539
1540 renderer = calloc(1, sizeof *renderer);
1541 if (renderer == NULL)
1542 return -1;
1543
1544 renderer->single_buffer = params->single_buffer;
1545
1546 renderer->base.read_pixels = rpi_renderer_read_pixels;
1547 renderer->base.repaint_output = rpi_renderer_repaint_output;
1548 renderer->base.flush_damage = rpi_renderer_flush_damage;
1549 renderer->base.attach = rpi_renderer_attach;
1550 renderer->base.create_surface = rpi_renderer_create_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001551 renderer->base.create_view = rpi_renderer_create_view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001552 renderer->base.surface_set_color = rpi_renderer_surface_set_color;
1553 renderer->base.destroy_surface = rpi_renderer_destroy_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001554 renderer->base.destroy_view = rpi_renderer_destroy_view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001555 renderer->base.destroy = rpi_renderer_destroy;
1556
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001557#ifdef ENABLE_EGL
1558 renderer->egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1559 if (renderer->egl_display == EGL_NO_DISPLAY) {
1560 weston_log("failed to create EGL display\n");
1561 return -1;
1562 }
1563
1564 if (!eglInitialize(renderer->egl_display, &major, &minor)) {
1565 weston_log("failed to initialize EGL display\n");
1566 return -1;
1567 }
1568
1569 renderer->bind_display =
1570 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1571 renderer->unbind_display =
1572 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1573 renderer->query_buffer =
1574 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1575
1576 extensions = (const char *) eglQueryString(renderer->egl_display,
1577 EGL_EXTENSIONS);
1578 if (!extensions) {
1579 weston_log("Retrieving EGL extension string failed.\n");
1580 return -1;
1581 }
1582
1583 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1584 renderer->has_bind_display = 1;
1585
1586 if (renderer->has_bind_display) {
1587 ret = renderer->bind_display(renderer->egl_display,
1588 compositor->wl_display);
1589 if (!ret)
1590 renderer->has_bind_display = 0;
1591 }
1592#endif
1593
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001594 compositor->renderer = &renderer->base;
1595 compositor->read_format = PIXMAN_a8r8g8b8;
1596 /* WESTON_CAP_ROTATION_ANY not supported */
1597
Tomeu Vizoso03681892013-08-06 20:05:57 +02001598 wl_display_add_shm_format(compositor->wl_display, WL_SHM_FORMAT_RGB565);
1599
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001600 return 0;
1601}
1602
1603WL_EXPORT int
1604rpi_renderer_output_create(struct weston_output *base,
1605 DISPMANX_DISPLAY_HANDLE_T display)
1606{
1607 struct rpir_output *output;
1608
1609 assert(base->renderer_state == NULL);
1610
1611 output = calloc(1, sizeof *output);
1612 if (!output)
1613 return -1;
1614
1615 output->display = display;
1616 output->update = DISPMANX_NO_HANDLE;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001617 wl_list_init(&output->view_list);
1618 wl_list_init(&output->view_cleanup_list);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001619 rpi_resource_init(&output->capture_buffer);
1620 base->renderer_state = output;
1621
1622 return 0;
1623}
1624
1625WL_EXPORT void
1626rpi_renderer_output_destroy(struct weston_output *base)
1627{
1628 struct rpir_output *output = to_rpir_output(base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001629 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001630 DISPMANX_UPDATE_HANDLE_T update;
1631
1632 rpi_resource_release(&output->capture_buffer);
1633 free(output->capture_data);
1634 output->capture_data = NULL;
1635
1636 update = vc_dispmanx_update_start(0);
1637 rpir_output_dmx_remove_all(output, update);
1638 vc_dispmanx_update_submit_sync(update);
1639
Jason Ekstranda7af7042013-10-12 22:38:11 -05001640 while (!wl_list_empty(&output->view_cleanup_list)) {
1641 view = container_of(output->view_cleanup_list.next,
1642 struct rpir_view, link);
1643 rpir_view_destroy(view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001644 }
1645
1646 free(output);
1647 base->renderer_state = NULL;
1648}
1649
1650WL_EXPORT void
1651rpi_renderer_set_update_handle(struct weston_output *base,
1652 DISPMANX_UPDATE_HANDLE_T handle)
1653{
1654 struct rpir_output *output = to_rpir_output(base);
1655
1656 output->update = handle;
1657}
1658
1659WL_EXPORT void
1660rpi_renderer_finish_frame(struct weston_output *base)
1661{
1662 struct rpir_output *output = to_rpir_output(base);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001663 struct weston_compositor *compositor = base->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001664 struct weston_view *wv;
1665 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001666
Jason Ekstranda7af7042013-10-12 22:38:11 -05001667 while (!wl_list_empty(&output->view_cleanup_list)) {
1668 view = container_of(output->view_cleanup_list.next,
1669 struct rpir_view, link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001670
Jason Ekstranda7af7042013-10-12 22:38:11 -05001671 if (view->view) {
1672 /* The weston_view still exists, but is
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001673 * temporarily not visible, and hence its Element
1674 * was removed. The current front buffer contents
1675 * must be preserved.
1676 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001677 if (!view->surface->visible_views)
1678 rpi_resource_release(view->surface->back);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001679
Jason Ekstranda7af7042013-10-12 22:38:11 -05001680 wl_list_remove(&view->link);
1681 wl_list_init(&view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001682 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001683 rpir_view_destroy(view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001684 }
1685 }
1686
Jason Ekstranda7af7042013-10-12 22:38:11 -05001687 wl_list_for_each(wv, &compositor->view_list, link) {
1688 view = to_rpir_view(wv);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001689
Jason Ekstranda7af7042013-10-12 22:38:11 -05001690 if (view->surface->buffer_type != BUFFER_TYPE_EGL)
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001691 continue;
1692
Jason Ekstranda7af7042013-10-12 22:38:11 -05001693 if (view->surface->egl_old_front == NULL)
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001694 continue;
1695
Jason Ekstranda7af7042013-10-12 22:38:11 -05001696 weston_buffer_reference(&view->surface->egl_old_front->buffer_ref, NULL);
1697 free(view->surface->egl_old_front);
1698 view->surface->egl_old_front = NULL;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001699 }
1700
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001701 wl_signal_emit(&base->frame_signal, base);
1702}