blob: 7f79410ed6d4f8b6f9e08a11aa283b9bfe83e18c [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;
Tomeu Vizoso15767812013-10-24 15:38:31 +0200972 DBG("new front %d\n", surface->egl_front->resource_handle);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200973 }
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200974 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500975 tmp = surface->front;
976 surface->front = surface->back;
977 surface->back = tmp;
Tomeu Vizoso15767812013-10-24 15:38:31 +0200978 DBG("new back %p, new front %p\n", surface->back, surface->front);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200979 }
Jason Ekstranda7af7042013-10-12 22:38:11 -0500980}
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300981
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982static int
983is_view_not_visible(struct weston_view *view)
984{
985 /* Return true, if surface is guaranteed to be totally obscured. */
986 int ret;
987 pixman_region32_t unocc;
988
989 pixman_region32_init(&unocc);
990 pixman_region32_subtract(&unocc, &view->transform.boundingbox,
991 &view->clip);
992 ret = !pixman_region32_not_empty(&unocc);
993 pixman_region32_fini(&unocc);
994
995 return ret;
996}
997
998static void
999rpir_view_update(struct rpir_view *view, struct rpir_output *output,
1000 DISPMANX_UPDATE_HANDLE_T update, int layer)
1001{
1002 int ret;
1003 int obscured;
1004
1005
1006 obscured = is_view_not_visible(view->view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001007 if (obscured) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 DBG("rpir_view %p totally obscured.\n", view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001009
Jason Ekstranda7af7042013-10-12 22:38:11 -05001010 wl_list_remove(&view->link);
1011 if (view->handle == DISPMANX_NO_HANDLE) {
1012 wl_list_init(&view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001013 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001014 rpir_view_dmx_remove(view, update);
1015 wl_list_insert(&output->view_cleanup_list,
1016 &view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001017 }
1018
1019 goto out;
1020 }
1021
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 if (view->handle == DISPMANX_NO_HANDLE) {
1023 ret = rpir_view_dmx_add(view, output, update, layer);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001024 if (ret == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001025 wl_list_remove(&view->link);
1026 wl_list_init(&view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001027 } else if (ret < 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001028 weston_log("ERROR rpir_view_dmx_add() failed.\n");
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001029 }
1030 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001031 if (view->surface->need_swap)
1032 rpir_view_dmx_swap(view, update);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001033
Jason Ekstranda7af7042013-10-12 22:38:11 -05001034 ret = rpir_view_dmx_move(view, update, layer);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001035 if (ret == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001036 rpir_view_dmx_remove(view, update);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001037
Jason Ekstranda7af7042013-10-12 22:38:11 -05001038 wl_list_remove(&view->link);
1039 wl_list_insert(&output->view_cleanup_list,
1040 &view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001041 } else if (ret < 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001042 weston_log("ERROR rpir_view_dmx_move() failed.\n");
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001043 }
1044 }
1045
1046out:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001047 view->layer = layer;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001048}
1049
1050static int
1051rpi_renderer_read_pixels(struct weston_output *base,
1052 pixman_format_code_t format, void *pixels,
1053 uint32_t x, uint32_t y,
1054 uint32_t width, uint32_t height)
1055{
1056 struct rpir_output *output = to_rpir_output(base);
1057 struct rpi_resource *buffer = &output->capture_buffer;
1058 VC_RECT_T rect;
1059 uint32_t fb_width, fb_height;
1060 uint32_t dst_pitch;
1061 uint32_t i;
1062 int ret;
1063
Hardeningff39efa2013-09-18 23:56:35 +02001064 fb_width = base->current_mode->width;
1065 fb_height = base->current_mode->height;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001066
1067 DBG("%s(%u, %u, %u, %u), resource %p\n", __func__,
1068 x, y, width, height, buffer);
1069
1070 if (format != PIXMAN_a8r8g8b8) {
1071 weston_log("rpi-renderer error: bad read_format\n");
1072 return -1;
1073 }
1074
1075 dst_pitch = fb_width * 4;
1076
1077 if (buffer->handle == DISPMANX_NO_HANDLE) {
1078 free(output->capture_data);
1079 output->capture_data = NULL;
1080
1081 ret = rpi_resource_realloc(buffer, VC_IMAGE_ARGB8888,
1082 fb_width, fb_height,
1083 dst_pitch, fb_height);
1084 if (ret < 0) {
1085 weston_log("rpi-renderer error: "
1086 "allocating read buffer failed\n");
1087 return -1;
1088 }
1089
1090 ret = vc_dispmanx_snapshot(output->display, buffer->handle,
1091 VC_IMAGE_ROT0);
1092 if (ret) {
1093 weston_log("rpi-renderer error: "
1094 "vc_dispmanx_snapshot returned %d\n", ret);
1095 return -1;
1096 }
1097 DBG("%s: snapshot done.\n", __func__);
1098 }
1099
1100 /*
1101 * If vc_dispmanx_resource_read_data was able to read sub-rectangles,
1102 * we could read directly into 'pixels'. But it cannot, it does not
1103 * use rect.x or rect.width, and does this:
1104 * host_start = (uint8_t *)dst_address + (dst_pitch * p_rect->y);
1105 * In other words, it is only good for reading the full buffer in
1106 * one go.
1107 */
1108 vc_dispmanx_rect_set(&rect, 0, 0, fb_width, fb_height);
1109
1110 if (x == 0 && y == 0 && width == fb_width && height == fb_height) {
1111 ret = vc_dispmanx_resource_read_data(buffer->handle, &rect,
1112 pixels, dst_pitch);
1113 if (ret) {
1114 weston_log("rpi-renderer error: "
1115 "resource_read_data returned %d\n", ret);
1116 return -1;
1117 }
1118 DBG("%s: full frame done.\n", __func__);
1119 return 0;
1120 }
1121
1122 if (!output->capture_data) {
1123 output->capture_data = malloc(fb_height * dst_pitch);
1124 if (!output->capture_data) {
1125 weston_log("rpi-renderer error: "
1126 "out of memory\n");
1127 return -1;
1128 }
1129
1130 ret = vc_dispmanx_resource_read_data(buffer->handle, &rect,
1131 output->capture_data,
1132 dst_pitch);
1133 if (ret) {
1134 weston_log("rpi-renderer error: "
1135 "resource_read_data returned %d\n", ret);
1136 return -1;
1137 }
1138 }
1139
1140 for (i = 0; i < height; i++) {
1141 uint8_t *src = output->capture_data +
1142 (y + i) * dst_pitch + x * 4;
1143 uint8_t *dst = (uint8_t *)pixels + i * width * 4;
1144 memcpy(dst, src, width * 4);
1145 }
1146
1147 return 0;
1148}
1149
1150static void
1151rpir_output_dmx_remove_all(struct rpir_output *output,
1152 DISPMANX_UPDATE_HANDLE_T update)
1153{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001154 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001155
Jason Ekstranda7af7042013-10-12 22:38:11 -05001156 while (!wl_list_empty(&output->view_list)) {
1157 view = container_of(output->view_list.next,
1158 struct rpir_view, link);
1159 rpir_view_dmx_remove(view, update);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001160
Jason Ekstranda7af7042013-10-12 22:38:11 -05001161 wl_list_remove(&view->link);
1162 wl_list_insert(&output->view_cleanup_list, &view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001163 }
1164}
1165
1166static void
1167output_compute_matrix(struct weston_output *base)
1168{
1169 struct rpir_output *output = to_rpir_output(base);
1170 struct weston_matrix *matrix = &output->matrix;
1171 const float half_w = 0.5f * base->width;
1172 const float half_h = 0.5f * base->height;
1173 float mag;
1174 float dx, dy;
1175
1176 weston_matrix_init(matrix);
1177 weston_matrix_translate(matrix, -base->x, -base->y, 0.0f);
1178
1179#ifdef SURFACE_TRANSFORM
1180 weston_matrix_translate(matrix, -half_w, -half_h, 0.0f);
1181 switch (base->transform) {
1182 case WL_OUTPUT_TRANSFORM_FLIPPED:
1183 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1184 case WL_OUTPUT_TRANSFORM_NORMAL:
1185 /* weston_matrix_rotate_xy(matrix, 1.0f, 0.0f); no-op */
1186 weston_matrix_translate(matrix, half_w, half_h, 0.0f);
1187 break;
1188
1189 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1190 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1191 case WL_OUTPUT_TRANSFORM_90:
1192 weston_matrix_rotate_xy(matrix, 0.0f, 1.0f);
1193 weston_matrix_translate(matrix, half_h, half_w, 0.0f);
1194 break;
1195
1196 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1197 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1198 case WL_OUTPUT_TRANSFORM_180:
1199 weston_matrix_rotate_xy(matrix, -1.0f, 0.0f);
1200 weston_matrix_translate(matrix, half_w, half_h, 0.0f);
1201 break;
1202
1203 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1204 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1205 case WL_OUTPUT_TRANSFORM_270:
1206 weston_matrix_rotate_xy(matrix, 0.0f, -1.0f);
1207 weston_matrix_translate(matrix, half_h, half_w, 0.0f);
1208 break;
1209
1210 default:
1211 break;
1212 }
1213#endif
1214
1215 if (base->zoom.active) {
1216 /* The base->zoom stuff is in GL coordinate system */
1217 mag = 1.0f / (1.0f - base->zoom.spring_z.current);
1218 dx = -(base->zoom.trans_x + 1.0f) * half_w;
1219 dy = -(base->zoom.trans_y + 1.0f) * half_h;
1220 weston_matrix_translate(matrix, dx, dy, 0.0f);
1221 weston_matrix_scale(matrix, mag, mag, 1.0f);
1222 weston_matrix_translate(matrix, half_w, half_h, 0.0f);
1223 }
1224}
1225
1226/* Note: this won't work right for multiple outputs. A DispmanX Element
1227 * is tied to one DispmanX Display, i.e. output.
1228 */
1229static void
1230rpi_renderer_repaint_output(struct weston_output *base,
1231 pixman_region32_t *output_damage)
1232{
1233 struct weston_compositor *compositor = base->compositor;
1234 struct rpir_output *output = to_rpir_output(base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 struct weston_view *wv;
1236 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001237 struct wl_list done_list;
1238 int layer = 1;
1239
1240 assert(output->update != DISPMANX_NO_HANDLE);
1241
1242 output_compute_matrix(base);
1243
1244 rpi_resource_release(&output->capture_buffer);
1245 free(output->capture_data);
1246 output->capture_data = NULL;
1247
Jason Ekstranda7af7042013-10-12 22:38:11 -05001248 /* Swap resources on surfaces as needed */
1249 wl_list_for_each_reverse(wv, &compositor->view_list, link)
1250 wv->surface->touched = 0;
1251
1252 wl_list_for_each_reverse(wv, &compositor->view_list, link) {
1253 view = to_rpir_view(wv);
1254
1255 if (!wv->surface->touched) {
1256 wv->surface->touched = 1;
1257
Tomeu Vizoso44247742013-10-24 15:38:32 +02001258 if (view->surface->buffer_type == BUFFER_TYPE_EGL ||
1259 view->surface->need_swap)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001260 rpir_surface_swap_pointers(view->surface);
1261 }
1262
Tomeu Vizosoe3df6f12013-10-24 15:38:33 +02001263 if (view->surface->buffer_type == BUFFER_TYPE_EGL &&
1264 view->surface->egl_front->buffer_ref.buffer == NULL) {
1265 weston_log("warning: client destroyed current front buffer\n");
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266
1267 wl_list_remove(&view->link);
1268 if (view->handle == DISPMANX_NO_HANDLE) {
1269 wl_list_init(&view->link);
1270 } else {
1271 rpir_view_dmx_remove(view, output->update);
1272 wl_list_insert(&output->view_cleanup_list,
1273 &view->link);
1274 }
1275 }
1276 }
1277
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001278 /* update all renderable surfaces */
1279 wl_list_init(&done_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 wl_list_for_each_reverse(wv, &compositor->view_list, link) {
1281 if (wv->plane != &compositor->primary_plane)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001282 continue;
1283
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 view = to_rpir_view(wv);
1285 assert(!wl_list_empty(&view->link) ||
1286 view->handle == DISPMANX_NO_HANDLE);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001287
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 wl_list_remove(&view->link);
1289 wl_list_insert(&done_list, &view->link);
1290 rpir_view_update(view, output, output->update, layer++);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001291 }
1292
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293 /* Mark all surfaces as swapped */
1294 wl_list_for_each_reverse(wv, &compositor->view_list, link)
1295 to_rpir_surface(wv->surface)->need_swap = 0;
1296
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001297 /* Remove all surfaces that are still on screen, but were
1298 * not rendered this time.
1299 */
1300 rpir_output_dmx_remove_all(output, output->update);
1301
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302 wl_list_insert_list(&output->view_list, &done_list);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001303 output->update = DISPMANX_NO_HANDLE;
1304
1305 /* The frame_signal is emitted in rpi_renderer_finish_frame(),
1306 * so that the firmware can capture the up-to-date contents.
1307 */
1308}
1309
1310static void
1311rpi_renderer_flush_damage(struct weston_surface *base)
1312{
1313 /* Called for every surface just before repainting it, if
1314 * having an shm buffer.
1315 */
1316 struct rpir_surface *surface = to_rpir_surface(base);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001317 struct weston_buffer *buffer = surface->buffer_ref.buffer;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001318 int ret;
1319
1320 assert(buffer);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001321 assert(wl_shm_buffer_get(buffer->resource));
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001322
1323 ret = rpir_surface_damage(surface, buffer, &base->damage);
1324 if (ret)
1325 weston_log("%s error: updating Dispmanx resource failed.\n",
1326 __func__);
1327
1328 weston_buffer_reference(&surface->buffer_ref, NULL);
1329}
1330
1331static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001332rpi_renderer_attach(struct weston_surface *base, struct weston_buffer *buffer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001333{
1334 /* Called every time a client commits an attach. */
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001335 struct rpir_surface *surface = to_rpir_surface(base);
1336
1337 assert(surface);
1338 if (!surface)
1339 return;
1340
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001341 if (surface->buffer_type == BUFFER_TYPE_SHM) {
1342 if (!surface->single_buffer)
1343 /* XXX: need to check if in middle of update */
1344 rpi_resource_release(surface->back);
1345
Jason Ekstranda7af7042013-10-12 22:38:11 -05001346 if (!surface->visible_views)
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001347 /* XXX: cannot do this, if middle of an update */
1348 rpi_resource_release(surface->front);
1349
1350 weston_buffer_reference(&surface->buffer_ref, NULL);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001351 }
1352
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001353 /* If buffer is NULL, Weston core unmaps the surface, the surface
1354 * will not appear in repaint list, and so rpi_renderer_repaint_output
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001355 * will remove the DispmanX element. Later, for SHM, also the front
1356 * buffer will be released in the cleanup_list processing.
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001357 */
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001358 if (!buffer)
1359 return;
1360
1361 if (wl_shm_buffer_get(buffer->resource)) {
1362 surface->buffer_type = BUFFER_TYPE_SHM;
1363 buffer->shm_buffer = wl_shm_buffer_get(buffer->resource);
1364 buffer->width = wl_shm_buffer_get_width(buffer->shm_buffer);
1365 buffer->height = wl_shm_buffer_get_height(buffer->shm_buffer);
1366
1367 weston_buffer_reference(&surface->buffer_ref, buffer);
1368 } else {
1369#if ENABLE_EGL
1370 struct rpi_renderer *renderer = to_rpi_renderer(base->compositor);
Tomeu Vizosoa8e5f292013-10-09 11:29:45 +02001371 struct wl_resource *wl_resource = buffer->resource;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001372
1373 if (!renderer->has_bind_display ||
1374 !renderer->query_buffer(renderer->egl_display,
1375 wl_resource,
1376 EGL_WIDTH, &buffer->width)) {
1377 weston_log("unhandled buffer type!\n");
1378 weston_buffer_reference(&surface->buffer_ref, NULL);
1379 surface->buffer_type = BUFFER_TYPE_NULL;
1380 }
1381
1382 renderer->query_buffer(renderer->egl_display,
1383 wl_resource,
1384 EGL_HEIGHT, &buffer->height);
1385
1386 surface->buffer_type = BUFFER_TYPE_EGL;
1387
1388 if(surface->egl_back == NULL)
1389 surface->egl_back = calloc(1, sizeof *surface->egl_back);
1390
1391 weston_buffer_reference(&surface->egl_back->buffer_ref, buffer);
1392 surface->egl_back->resource_handle =
1393 vc_dispmanx_get_handle_from_wl_buffer(wl_resource);
1394#else
1395 weston_log("unhandled buffer type!\n");
1396 weston_buffer_reference(&surface->buffer_ref, NULL);
1397 surface->buffer_type = BUFFER_TYPE_NULL;
1398#endif
1399 }
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001400}
1401
1402static int
1403rpi_renderer_create_surface(struct weston_surface *base)
1404{
1405 struct rpi_renderer *renderer = to_rpi_renderer(base->compositor);
1406 struct rpir_surface *surface;
1407
1408 assert(base->renderer_state == NULL);
1409
1410 surface = rpir_surface_create(renderer);
1411 if (!surface)
1412 return -1;
1413
1414 surface->surface = base;
1415 base->renderer_state = surface;
1416 return 0;
1417}
1418
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419static int
1420rpi_renderer_create_view(struct weston_view *base)
1421{
1422 struct rpir_surface *surface = to_rpir_surface(base->surface);
1423 struct rpir_view *view;
1424
1425 assert(base->renderer_state == NULL);
1426
1427 view = rpir_view_create(surface);
1428 if (!view)
1429 return -1;
1430
1431 view->view = base;
1432 base->renderer_state = view;
1433 return 0;
1434}
1435
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001436static void
1437rpi_renderer_surface_set_color(struct weston_surface *base,
1438 float red, float green, float blue, float alpha)
1439{
1440 struct rpir_surface *surface = to_rpir_surface(base);
1441 uint8_t color[4];
1442 VC_RECT_T rect;
1443 int ret;
1444
1445 assert(surface);
1446
1447 ret = rpi_resource_realloc(surface->back, VC_IMAGE_ARGB8888,
1448 1, 1, 4, 1);
1449 if (ret < 0) {
1450 weston_log("Error: %s: rpi_resource_realloc failed.\n",
1451 __func__);
1452 return;
1453 }
1454
1455 color[0] = float2uint8(blue);
1456 color[1] = float2uint8(green);
1457 color[2] = float2uint8(red);
1458 color[3] = float2uint8(alpha);
1459
1460 vc_dispmanx_rect_set(&rect, 0, 0, 1, 1);
1461 ret = vc_dispmanx_resource_write_data(surface->back->handle,
1462 VC_IMAGE_ARGB8888,
1463 4, color, &rect);
1464 if (ret) {
1465 weston_log("Error: %s: resource_write_data failed.\n",
1466 __func__);
1467 return;
1468 }
1469
1470 DBG("%s: resource %p solid color BGRA %u,%u,%u,%u\n", __func__,
1471 surface->back, color[0], color[1], color[2], color[3]);
1472
1473 /*pixman_region32_copy(&surface->prev_damage, damage);*/
1474 surface->need_swap = 1;
1475}
1476
1477static void
1478rpi_renderer_destroy_surface(struct weston_surface *base)
1479{
1480 struct rpir_surface *surface = to_rpir_surface(base);
1481
1482 assert(surface);
1483 assert(surface->surface == base);
1484 if (!surface)
1485 return;
1486
1487 surface->surface = NULL;
1488 base->renderer_state = NULL;
1489
Jason Ekstranda7af7042013-10-12 22:38:11 -05001490 if (wl_list_empty(&surface->views))
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001491 rpir_surface_destroy(surface);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001492}
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001493
Jason Ekstranda7af7042013-10-12 22:38:11 -05001494static void
1495rpi_renderer_destroy_view(struct weston_view *base)
1496{
1497 struct rpir_view *view = to_rpir_view(base);
1498
1499 assert(view);
1500 assert(view->view == base);
1501 if (!view)
1502 return;
1503
Tomeu Vizoso34dad7d2013-10-25 10:34:37 +02001504 view->view = NULL;
1505
Jason Ekstranda7af7042013-10-12 22:38:11 -05001506 /* If guaranteed to not be on screen, just detroy it. */
1507 if (wl_list_empty(&view->link))
1508 rpir_view_destroy(view);
1509
1510 /* Otherwise, the view is either on screen and needs
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001511 * to be removed by a repaint update, or it is in the
Jason Ekstranda7af7042013-10-12 22:38:11 -05001512 * view_cleanup_list, and will be destroyed by
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001513 * rpi_renderer_finish_frame().
1514 */
1515}
1516
1517static void
1518rpi_renderer_destroy(struct weston_compositor *compositor)
1519{
1520 struct rpi_renderer *renderer = to_rpi_renderer(compositor);
1521
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001522#if ENABLE_EGL
1523 if (renderer->has_bind_display)
1524 renderer->unbind_display(renderer->egl_display,
1525 compositor->wl_display);
1526#endif
1527
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001528 free(renderer);
1529 compositor->renderer = NULL;
1530}
1531
1532WL_EXPORT int
1533rpi_renderer_create(struct weston_compositor *compositor,
1534 const struct rpi_renderer_parameters *params)
1535{
1536 struct rpi_renderer *renderer;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001537#if ENABLE_EGL
1538 const char *extensions;
1539 EGLBoolean ret;
1540 EGLint major, minor;
1541#endif
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001542
1543 weston_log("Initializing the DispmanX compositing renderer\n");
1544
1545 renderer = calloc(1, sizeof *renderer);
1546 if (renderer == NULL)
1547 return -1;
1548
1549 renderer->single_buffer = params->single_buffer;
1550
1551 renderer->base.read_pixels = rpi_renderer_read_pixels;
1552 renderer->base.repaint_output = rpi_renderer_repaint_output;
1553 renderer->base.flush_damage = rpi_renderer_flush_damage;
1554 renderer->base.attach = rpi_renderer_attach;
1555 renderer->base.create_surface = rpi_renderer_create_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001556 renderer->base.create_view = rpi_renderer_create_view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001557 renderer->base.surface_set_color = rpi_renderer_surface_set_color;
1558 renderer->base.destroy_surface = rpi_renderer_destroy_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001559 renderer->base.destroy_view = rpi_renderer_destroy_view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001560 renderer->base.destroy = rpi_renderer_destroy;
1561
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001562#ifdef ENABLE_EGL
1563 renderer->egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1564 if (renderer->egl_display == EGL_NO_DISPLAY) {
1565 weston_log("failed to create EGL display\n");
1566 return -1;
1567 }
1568
1569 if (!eglInitialize(renderer->egl_display, &major, &minor)) {
1570 weston_log("failed to initialize EGL display\n");
1571 return -1;
1572 }
1573
1574 renderer->bind_display =
1575 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1576 renderer->unbind_display =
1577 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1578 renderer->query_buffer =
1579 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1580
1581 extensions = (const char *) eglQueryString(renderer->egl_display,
1582 EGL_EXTENSIONS);
1583 if (!extensions) {
1584 weston_log("Retrieving EGL extension string failed.\n");
1585 return -1;
1586 }
1587
1588 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1589 renderer->has_bind_display = 1;
1590
1591 if (renderer->has_bind_display) {
1592 ret = renderer->bind_display(renderer->egl_display,
1593 compositor->wl_display);
1594 if (!ret)
1595 renderer->has_bind_display = 0;
1596 }
1597#endif
1598
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001599 compositor->renderer = &renderer->base;
1600 compositor->read_format = PIXMAN_a8r8g8b8;
1601 /* WESTON_CAP_ROTATION_ANY not supported */
1602
Tomeu Vizoso03681892013-08-06 20:05:57 +02001603 wl_display_add_shm_format(compositor->wl_display, WL_SHM_FORMAT_RGB565);
1604
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001605 return 0;
1606}
1607
1608WL_EXPORT int
1609rpi_renderer_output_create(struct weston_output *base,
1610 DISPMANX_DISPLAY_HANDLE_T display)
1611{
1612 struct rpir_output *output;
1613
1614 assert(base->renderer_state == NULL);
1615
1616 output = calloc(1, sizeof *output);
1617 if (!output)
1618 return -1;
1619
1620 output->display = display;
1621 output->update = DISPMANX_NO_HANDLE;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001622 wl_list_init(&output->view_list);
1623 wl_list_init(&output->view_cleanup_list);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001624 rpi_resource_init(&output->capture_buffer);
1625 base->renderer_state = output;
1626
1627 return 0;
1628}
1629
1630WL_EXPORT void
1631rpi_renderer_output_destroy(struct weston_output *base)
1632{
1633 struct rpir_output *output = to_rpir_output(base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001634 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001635 DISPMANX_UPDATE_HANDLE_T update;
1636
1637 rpi_resource_release(&output->capture_buffer);
1638 free(output->capture_data);
1639 output->capture_data = NULL;
1640
1641 update = vc_dispmanx_update_start(0);
1642 rpir_output_dmx_remove_all(output, update);
1643 vc_dispmanx_update_submit_sync(update);
1644
Jason Ekstranda7af7042013-10-12 22:38:11 -05001645 while (!wl_list_empty(&output->view_cleanup_list)) {
1646 view = container_of(output->view_cleanup_list.next,
1647 struct rpir_view, link);
1648 rpir_view_destroy(view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001649 }
1650
1651 free(output);
1652 base->renderer_state = NULL;
1653}
1654
1655WL_EXPORT void
1656rpi_renderer_set_update_handle(struct weston_output *base,
1657 DISPMANX_UPDATE_HANDLE_T handle)
1658{
1659 struct rpir_output *output = to_rpir_output(base);
1660
1661 output->update = handle;
1662}
1663
1664WL_EXPORT void
1665rpi_renderer_finish_frame(struct weston_output *base)
1666{
1667 struct rpir_output *output = to_rpir_output(base);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001668 struct weston_compositor *compositor = base->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001669 struct weston_view *wv;
1670 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001671
Jason Ekstranda7af7042013-10-12 22:38:11 -05001672 while (!wl_list_empty(&output->view_cleanup_list)) {
1673 view = container_of(output->view_cleanup_list.next,
1674 struct rpir_view, link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001675
Jason Ekstranda7af7042013-10-12 22:38:11 -05001676 if (view->view) {
1677 /* The weston_view still exists, but is
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001678 * temporarily not visible, and hence its Element
1679 * was removed. The current front buffer contents
1680 * must be preserved.
1681 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682 if (!view->surface->visible_views)
1683 rpi_resource_release(view->surface->back);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001684
Jason Ekstranda7af7042013-10-12 22:38:11 -05001685 wl_list_remove(&view->link);
1686 wl_list_init(&view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001687 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001688 rpir_view_destroy(view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001689 }
1690 }
1691
Jason Ekstranda7af7042013-10-12 22:38:11 -05001692 wl_list_for_each(wv, &compositor->view_list, link) {
1693 view = to_rpir_view(wv);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001694
Jason Ekstranda7af7042013-10-12 22:38:11 -05001695 if (view->surface->buffer_type != BUFFER_TYPE_EGL)
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001696 continue;
1697
Jason Ekstranda7af7042013-10-12 22:38:11 -05001698 if (view->surface->egl_old_front == NULL)
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001699 continue;
1700
Jason Ekstranda7af7042013-10-12 22:38:11 -05001701 weston_buffer_reference(&view->surface->egl_old_front->buffer_ref, NULL);
1702 free(view->surface->egl_old_front);
1703 view->surface->egl_old_front = NULL;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001704 }
1705
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001706 wl_signal_emit(&base->frame_signal, base);
1707}