blob: 4d0f522f1ac8dfb0325580c0891520c1baea8ca5 [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
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +010082/* If we had a vc_dispmanx_element_set_opaque_rect()... */
83/*#define HAVE_ELEMENT_SET_OPAQUE_RECT 1*/
84
Pekka Paalanend7265bc2013-05-22 18:03:06 +030085struct rpi_resource {
86 DISPMANX_RESOURCE_HANDLE_T handle;
87 int width;
88 int height; /* height of the image (valid pixel data) */
89 int stride; /* bytes */
90 int buffer_height; /* height of the buffer */
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +010091 int enable_opaque_regions;
Pekka Paalanend7265bc2013-05-22 18:03:06 +030092 VC_IMAGE_TYPE_T ifmt;
93};
94
95struct rpir_output;
96
Tomeu Vizosob4659eb2013-10-07 11:02:20 +020097struct rpir_egl_buffer {
98 struct weston_buffer_reference buffer_ref;
99 DISPMANX_RESOURCE_HANDLE_T resource_handle;
100};
101
102enum buffer_type {
103 BUFFER_TYPE_NULL,
104 BUFFER_TYPE_SHM,
105 BUFFER_TYPE_EGL
106};
107
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300108struct rpir_surface {
109 struct weston_surface *surface;
110
Jason Ekstranda7af7042013-10-12 22:38:11 -0500111 struct wl_list views;
112 int visible_views;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300113 int need_swap;
114 int single_buffer;
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100115 int enable_opaque_regions;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300116
117 struct rpi_resource resources[2];
118 struct rpi_resource *front;
119 struct rpi_resource *back;
120 pixman_region32_t prev_damage;
121
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200122 struct rpir_egl_buffer *egl_front;
123 struct rpir_egl_buffer *egl_back;
124 struct rpir_egl_buffer *egl_old_front;
125
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300126 struct weston_buffer_reference buffer_ref;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200127 enum buffer_type buffer_type;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300128
129 struct wl_listener surface_destroy_listener;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300130};
131
Jason Ekstranda7af7042013-10-12 22:38:11 -0500132struct rpir_view {
133 struct rpir_surface *surface;
134 struct wl_list surface_link;
135 struct weston_view *view;
136
137 /* If link is empty, the view is guaranteed to not be on screen,
138 * i.e. updates removing Elements have completed.
139 */
140 struct wl_list link;
141
142 DISPMANX_ELEMENT_HANDLE_T handle;
143 int layer;
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +0100144
145 struct wl_listener view_destroy_listener;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500146};
147
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300148struct rpir_output {
149 DISPMANX_DISPLAY_HANDLE_T display;
150
151 DISPMANX_UPDATE_HANDLE_T update;
152 struct weston_matrix matrix;
153
154 /* all Elements currently on screen */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500155 struct wl_list view_list; /* struct rpir_surface::link */
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300156
157 /* Elements just removed, waiting for update completion */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500158 struct wl_list view_cleanup_list; /* struct rpir_surface::link */
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300159
160 struct rpi_resource capture_buffer;
161 uint8_t *capture_data;
162};
163
164struct rpi_renderer {
165 struct weston_renderer base;
166
167 int single_buffer;
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100168 int enable_opaque_regions;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200169
170#ifdef ENABLE_EGL
171 EGLDisplay egl_display;
172
173 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
174 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
175 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
176#endif
177 int has_bind_display;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300178};
179
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300180static int
181rpi_renderer_create_surface(struct weston_surface *base);
182
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +0100183static int
184rpi_renderer_create_view(struct weston_view *base);
185
186static void
187rpir_view_handle_view_destroy(struct wl_listener *listener, void *data);
188
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300189static inline struct rpir_surface *
190to_rpir_surface(struct weston_surface *surface)
191{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300192 if (!surface->renderer_state)
193 rpi_renderer_create_surface(surface);
194
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300195 return surface->renderer_state;
196}
197
Jason Ekstranda7af7042013-10-12 22:38:11 -0500198static inline struct rpir_view *
199to_rpir_view(struct weston_view *view)
200{
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +0100201 if (!view->renderer_state)
202 rpi_renderer_create_view(view);
203
Jason Ekstranda7af7042013-10-12 22:38:11 -0500204 return view->renderer_state;
205}
206
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300207static inline struct rpir_output *
208to_rpir_output(struct weston_output *output)
209{
210 return output->renderer_state;
211}
212
213static inline struct rpi_renderer *
214to_rpi_renderer(struct weston_compositor *compositor)
215{
216 return container_of(compositor->renderer, struct rpi_renderer, base);
217}
218
219static inline int
220int_max(int a, int b)
221{
222 return a > b ? a : b;
223}
224
225static inline void
226int_swap(int *a, int *b)
227{
228 int tmp = *a;
229 *a = *b;
230 *b = tmp;
231}
232
233static uint8_t
234float2uint8(float f)
235{
236 int v = roundf(f * 255.0f);
237
238 return v < 0 ? 0 : (v > 255 ? 255 : v);
239}
240
241static void
242rpi_resource_init(struct rpi_resource *resource)
243{
244 resource->handle = DISPMANX_NO_HANDLE;
245}
246
247static void
248rpi_resource_release(struct rpi_resource *resource)
249{
250 if (resource->handle == DISPMANX_NO_HANDLE)
251 return;
252
253 vc_dispmanx_resource_delete(resource->handle);
254 DBG("resource %p release\n", resource);
255 resource->handle = DISPMANX_NO_HANDLE;
256}
257
258static int
259rpi_resource_realloc(struct rpi_resource *resource, VC_IMAGE_TYPE_T ifmt,
260 int width, int height, int stride, int buffer_height)
261{
262 uint32_t dummy;
263
264 if (resource->handle != DISPMANX_NO_HANDLE &&
265 resource->width == width &&
266 resource->height == height &&
267 resource->stride == stride &&
268 resource->buffer_height == buffer_height &&
269 resource->ifmt == ifmt)
270 return 0;
271
272 rpi_resource_release(resource);
273
274 /* NOTE: if stride is not a multiple of 16 pixels in bytes,
275 * the vc_image_* functions may break. Dispmanx elements
276 * should be fine, though. Buffer_height probably has similar
277 * constraints, too.
278 */
279 resource->handle =
280 vc_dispmanx_resource_create(ifmt,
281 width | (stride << 16),
282 height | (buffer_height << 16),
283 &dummy);
284 if (resource->handle == DISPMANX_NO_HANDLE)
285 return -1;
286
287 resource->width = width;
288 resource->height = height;
289 resource->stride = stride;
290 resource->buffer_height = buffer_height;
291 resource->ifmt = ifmt;
292 DBG("resource %p alloc\n", resource);
293 return 1;
294}
295
296/* A firmware workaround for broken ALPHA_PREMULT + ALPHA_MIX hardware. */
297#define PREMULT_ALPHA_FLAG (1 << 31)
298
299static VC_IMAGE_TYPE_T
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500300shm_buffer_get_vc_format(struct wl_shm_buffer *buffer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300301{
302 switch (wl_shm_buffer_get_format(buffer)) {
303 case WL_SHM_FORMAT_XRGB8888:
304 return VC_IMAGE_XRGB8888;
305 case WL_SHM_FORMAT_ARGB8888:
306 return VC_IMAGE_ARGB8888 | PREMULT_ALPHA_FLAG;
Tomeu Vizoso03681892013-08-06 20:05:57 +0200307 case WL_SHM_FORMAT_RGB565:
308 return VC_IMAGE_RGB565;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300309 default:
310 /* invalid format */
311 return VC_IMAGE_MIN;
312 }
313}
314
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100315#ifndef HAVE_ELEMENT_SET_OPAQUE_RECT
316static uint32_t *
317apply_opaque_region(struct wl_shm_buffer *buffer,
318 pixman_region32_t *opaque_region)
319{
320 uint32_t *src, *dst;
321 int width;
322 int height;
323 int stride;
324 int x, y;
325
326 width = wl_shm_buffer_get_width(buffer);
327 height = wl_shm_buffer_get_height(buffer);
328 stride = wl_shm_buffer_get_stride(buffer);
329 src = wl_shm_buffer_get_data(buffer);
330
331 dst = malloc(height * stride);
332 if (dst == NULL) {
333 weston_log("rpi-renderer error: out of memory\n");
334 return NULL;
335 }
336
337 for (y = 0; y < height; y++) {
338 for (x = 0; x < width; x++) {
339 int i = y * stride / 4 + x;
340 pixman_box32_t box;
341 if (pixman_region32_contains_point (opaque_region, x, y, &box)) {
342 dst[i] = src[i] | 0xff000000;
343 } else {
344 dst[i] = src[i];
345 }
346 }
347 }
348
349 return dst;
350}
351#endif
352
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300353static int
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500354rpi_resource_update(struct rpi_resource *resource, struct weston_buffer *buffer,
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100355 pixman_region32_t *region, pixman_region32_t *opaque_region)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300356{
357 pixman_region32_t write_region;
358 pixman_box32_t *r;
359 VC_RECT_T rect;
360 VC_IMAGE_TYPE_T ifmt;
361 uint32_t *pixels;
362 int width;
363 int height;
364 int stride;
365 int ret;
U. Artie Eoff5e854bc2014-01-17 13:56:41 -0800366 int applied_opaque_region = 0;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300367#ifdef HAVE_RESOURCE_WRITE_DATA_RECT
368 int n;
369#endif
370
371 if (!buffer)
372 return -1;
373
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500374 ifmt = shm_buffer_get_vc_format(buffer->shm_buffer);
375 width = wl_shm_buffer_get_width(buffer->shm_buffer);
376 height = wl_shm_buffer_get_height(buffer->shm_buffer);
377 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
378 pixels = wl_shm_buffer_get_data(buffer->shm_buffer);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300379
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100380#ifndef HAVE_ELEMENT_SET_OPAQUE_RECT
381 if (pixman_region32_not_empty(opaque_region) &&
382 wl_shm_buffer_get_format(buffer->shm_buffer) == WL_SHM_FORMAT_ARGB8888 &&
383 resource->enable_opaque_regions) {
384 pixels = apply_opaque_region(buffer->shm_buffer, opaque_region);
385
386 if (!pixels)
387 return -1;
U. Artie Eoff5e854bc2014-01-17 13:56:41 -0800388
389 applied_opaque_region = 1;
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100390 }
391#endif
392
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300393 ret = rpi_resource_realloc(resource, ifmt & ~PREMULT_ALPHA_FLAG,
394 width, height, stride, height);
U. Artie Eoff5e854bc2014-01-17 13:56:41 -0800395 if (ret < 0) {
396 if (applied_opaque_region)
397 free(pixels);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300398 return -1;
U. Artie Eoff5e854bc2014-01-17 13:56:41 -0800399 }
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300400
401 pixman_region32_init_rect(&write_region, 0, 0, width, height);
402 if (ret == 0)
403 pixman_region32_intersect(&write_region,
404 &write_region, region);
405
Neil Robertse5051712013-11-13 15:44:06 +0000406 wl_shm_buffer_begin_access(buffer->shm_buffer);
407
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300408#ifdef HAVE_RESOURCE_WRITE_DATA_RECT
409 /* XXX: Can this do a format conversion, so that scanout does not have to? */
410 r = pixman_region32_rectangles(&write_region, &n);
411 while (n--) {
412 vc_dispmanx_rect_set(&rect, r[n].x1, r[n].y1,
413 r[n].x2 - r[n].x1, r[n].y2 - r[n].y1);
414
415 ret = vc_dispmanx_resource_write_data_rect(resource->handle,
416 ifmt, stride,
417 pixels, &rect,
418 rect.x, rect.y);
419 DBG("%s: %p %ux%u@%u,%u, ret %d\n", __func__, resource,
420 rect.width, rect.height, rect.x, rect.y, ret);
421 if (ret)
422 break;
423 }
424#else
425 /* vc_dispmanx_resource_write_data() ignores ifmt,
426 * rect.x, rect.width, and uses stride only for computing
427 * the size of the transfer as rect.height * stride.
428 * Therefore we can only write rows starting at x=0.
429 * To be able to write more than one scanline at a time,
430 * the resource must have been created with the same stride
431 * as used here, and we must write full scanlines.
432 */
433
434 r = pixman_region32_extents(&write_region);
435 vc_dispmanx_rect_set(&rect, 0, r->y1, width, r->y2 - r->y1);
436 ret = vc_dispmanx_resource_write_data(resource->handle,
437 ifmt, stride, pixels, &rect);
438 DBG("%s: %p %ux%u@%u,%u, ret %d\n", __func__, resource,
439 width, r->y2 - r->y1, 0, r->y1, ret);
440#endif
441
Neil Robertse5051712013-11-13 15:44:06 +0000442 wl_shm_buffer_end_access(buffer->shm_buffer);
443
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300444 pixman_region32_fini(&write_region);
445
U. Artie Eoff5e854bc2014-01-17 13:56:41 -0800446 if (applied_opaque_region)
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100447 free(pixels);
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100448
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300449 return ret ? -1 : 0;
450}
451
Pekka Paalanend5fbfb22013-11-22 17:30:39 +0200452static inline void
453rpi_buffer_egl_lock(struct weston_buffer *buffer)
454{
455#ifdef ENABLE_EGL
456 vc_dispmanx_set_wl_buffer_in_use(buffer->resource, 1);
457#endif
458}
459
460static inline void
461rpi_buffer_egl_unlock(struct weston_buffer *buffer)
462{
463#ifdef ENABLE_EGL
464 vc_dispmanx_set_wl_buffer_in_use(buffer->resource, 0);
465#endif
466}
467
Tomeu Vizoso74987582013-10-25 10:34:38 +0200468static void
469rpir_egl_buffer_destroy(struct rpir_egl_buffer *egl_buffer)
470{
471 struct weston_buffer *buffer;
472
473 if (egl_buffer == NULL)
474 return;
475
476 buffer = egl_buffer->buffer_ref.buffer;
477 if (buffer == NULL) {
478 /* The client has already destroyed the wl_buffer, the
479 * compositor has the responsibility to delete the resource.
480 */
481 vc_dispmanx_resource_delete(egl_buffer->resource_handle);
482 } else {
Pekka Paalanend5fbfb22013-11-22 17:30:39 +0200483 rpi_buffer_egl_unlock(buffer);
Tomeu Vizoso74987582013-10-25 10:34:38 +0200484 weston_buffer_reference(&egl_buffer->buffer_ref, NULL);
485 }
486
487 free(egl_buffer);
488}
489
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300490static struct rpir_surface *
491rpir_surface_create(struct rpi_renderer *renderer)
492{
493 struct rpir_surface *surface;
494
495 surface = calloc(1, sizeof *surface);
496 if (!surface)
497 return NULL;
498
Tomeu Vizoso5c3ea3b2013-10-24 15:38:30 +0200499 wl_list_init(&surface->views);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300500 surface->single_buffer = renderer->single_buffer;
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100501 surface->enable_opaque_regions = renderer->enable_opaque_regions;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300502 rpi_resource_init(&surface->resources[0]);
503 rpi_resource_init(&surface->resources[1]);
504 surface->front = &surface->resources[0];
505 if (surface->single_buffer)
506 surface->back = &surface->resources[0];
507 else
508 surface->back = &surface->resources[1];
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100509
510 surface->front->enable_opaque_regions = renderer->enable_opaque_regions;
511 surface->back->enable_opaque_regions = renderer->enable_opaque_regions;
512
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200513 surface->buffer_type = BUFFER_TYPE_NULL;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300514
515 pixman_region32_init(&surface->prev_damage);
516
517 return surface;
518}
519
520static void
521rpir_surface_destroy(struct rpir_surface *surface)
522{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500523 if (surface->visible_views)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300524 weston_log("ERROR rpi: destroying on-screen element\n");
525
Jason Ekstranda7af7042013-10-12 22:38:11 -0500526 assert(wl_list_empty(&surface->views));
527
528 if (surface->surface)
529 surface->surface->renderer_state = NULL;
530
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300531 pixman_region32_fini(&surface->prev_damage);
532 rpi_resource_release(&surface->resources[0]);
533 rpi_resource_release(&surface->resources[1]);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500534 DBG("rpir_surface %p destroyed (%u)\n", surface, surface->visible_views);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300535
Tomeu Vizoso74987582013-10-25 10:34:38 +0200536 rpir_egl_buffer_destroy(surface->egl_back);
537 rpir_egl_buffer_destroy(surface->egl_front);
538 rpir_egl_buffer_destroy(surface->egl_old_front);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200539
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300540 free(surface);
541}
542
543static int
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500544rpir_surface_damage(struct rpir_surface *surface, struct weston_buffer *buffer,
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300545 pixman_region32_t *damage)
546{
547 pixman_region32_t upload;
548 int ret;
549
550 if (!pixman_region32_not_empty(damage))
551 return 0;
552
553 DBG("rpir_surface %p update resource %p\n", surface, surface->back);
554
555 /* XXX: todo: if no surface->handle, update front buffer directly
556 * to avoid creating a new back buffer */
557 if (surface->single_buffer) {
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100558 ret = rpi_resource_update(surface->front, buffer, damage,
559 &surface->surface->opaque);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300560 } else {
561 pixman_region32_init(&upload);
562 pixman_region32_union(&upload, &surface->prev_damage, damage);
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100563 ret = rpi_resource_update(surface->back, buffer, &upload,
564 &surface->surface->opaque);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300565 pixman_region32_fini(&upload);
566 }
567
568 pixman_region32_copy(&surface->prev_damage, damage);
569 surface->need_swap = 1;
570
571 return ret;
572}
573
Jason Ekstranda7af7042013-10-12 22:38:11 -0500574static struct rpir_view *
575rpir_view_create(struct rpir_surface *surface)
576{
577 struct rpir_view *view;
578
579 view = calloc(1, sizeof *view);
580 if (!view)
581 return NULL;
582
583 view->surface = surface;
584 wl_list_insert(&surface->views, &view->surface_link);
585
586 wl_list_init(&view->link);
587 view->handle = DISPMANX_NO_HANDLE;
588
589 return view;
590}
591
592static void
593rpir_view_destroy(struct rpir_view *view)
594{
595 wl_list_remove(&view->link);
596
597 if (view->handle != DISPMANX_NO_HANDLE) {
598 view->surface->visible_views--;
599 weston_log("ERROR rpi: destroying on-screen element\n");
600 }
601
602 if (view->view)
603 view->view->renderer_state = NULL;
604
605 wl_list_remove(&view->surface_link);
606 if (wl_list_empty(&view->surface->views) && view->surface->surface == NULL)
607 rpir_surface_destroy(view->surface);
608
609 DBG("rpir_view %p destroyed (%d)\n", view, view->handle);
610
611 free(view);
612}
613
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300614static void
615matrix_type_str(struct weston_matrix *matrix, char *buf, int len)
616{
617 static const char types[33] = "TSRO";
618 unsigned mask = matrix->type;
619 int i = 0;
620
621 while (mask && i < len - 1) {
622 if (mask & (1u << i))
623 *buf++ = types[i];
624 mask &= ~(1u << i);
625 i++;
626 }
627 *buf = '\0';
628}
629
630static void
631log_print_matrix(struct weston_matrix *matrix)
632{
633 char typestr[6];
634 float *d = matrix->d;
635
636 matrix_type_str(matrix, typestr, sizeof typestr);
637 weston_log_continue("%14.6e %14.6e %14.6e %14.6e\n",
638 d[0], d[4], d[8], d[12]);
639 weston_log_continue("%14.6e %14.6e %14.6e %14.6e\n",
640 d[1], d[5], d[9], d[13]);
641 weston_log_continue("%14.6e %14.6e %14.6e %14.6e\n",
642 d[2], d[6], d[10], d[14]);
643 weston_log_continue("%14.6e %14.6e %14.6e %14.6e type: %s\n",
644 d[3], d[7], d[11], d[15], typestr);
645}
646
647static void
648warn_bad_matrix(struct weston_matrix *total, struct weston_matrix *output,
649 struct weston_matrix *surface)
650{
651 static int n_warn;
652 char typestr[6];
653
654 if (n_warn++ == 10)
655 weston_log("%s: not showing more warnings\n", __func__);
656
657 if (n_warn > 10)
658 return;
659
660 weston_log("%s: warning: total transformation is not renderable:\n",
661 __func__);
662 log_print_matrix(total);
663
664 matrix_type_str(surface, typestr, sizeof typestr);
665 weston_log_continue("surface matrix type: %s\n", typestr);
666 matrix_type_str(output, typestr, sizeof typestr);
667 weston_log_continue("output matrix type: %s\n", typestr);
668}
669
670/*#define SURFACE_TRANSFORM */
671
672static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500673rpir_view_compute_rects(struct rpir_view *view,
674 VC_RECT_T *src_rect, VC_RECT_T *dst_rect,
675 VC_IMAGE_TRANSFORM_T *flipmask)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300676{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500677 struct weston_output *output_base = view->view->surface->output;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300678 struct rpir_output *output = to_rpir_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500679 struct weston_matrix matrix = view->view->transform.matrix;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300680 VC_IMAGE_TRANSFORM_T flipt = 0;
681 int src_x, src_y;
682 int dst_x, dst_y;
683 int src_width, src_height;
684 int dst_width, dst_height;
685 struct weston_vector p1 = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
686 struct weston_vector p2 = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
687 int t;
688 int over;
689
690 /* XXX: take buffer transform into account */
691
692 /* src is in 16.16, dst is in 32.0 fixed point.
693 * Negative values are not allowed in VC_RECT_T.
694 * Clip size to output boundaries, firmware ignores
695 * huge elements like 8192x8192.
696 */
697
698 src_x = 0 << 16;
699 src_y = 0 << 16;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200700
Jason Ekstranda7af7042013-10-12 22:38:11 -0500701 if (view->surface->buffer_type == BUFFER_TYPE_EGL) {
702 struct weston_buffer *buffer =
703 view->surface->egl_front->buffer_ref.buffer;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200704
705 src_width = buffer->width << 16;
706 src_height = buffer->height << 16;
707 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500708 src_width = view->surface->front->width << 16;
709 src_height = view->surface->front->height << 16;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200710 }
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300711
712 weston_matrix_multiply(&matrix, &output->matrix);
713
714#ifdef SURFACE_TRANSFORM
715 if (matrix.type >= WESTON_MATRIX_TRANSFORM_OTHER) {
716#else
717 if (matrix.type >= WESTON_MATRIX_TRANSFORM_ROTATE) {
718#endif
719 warn_bad_matrix(&matrix, &output->matrix,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500720 &view->view->transform.matrix);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300721 } else {
722 if (matrix.type & WESTON_MATRIX_TRANSFORM_ROTATE) {
723 if (fabsf(matrix.d[0]) < 1e-4f &&
724 fabsf(matrix.d[5]) < 1e-4f) {
725 flipt |= TRANSFORM_TRANSPOSE;
726 } else if (fabsf(matrix.d[1]) < 1e-4 &&
727 fabsf(matrix.d[4]) < 1e-4) {
728 /* no transpose */
729 } else {
730 warn_bad_matrix(&matrix, &output->matrix,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500731 &view->view->transform.matrix);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300732 }
733 }
734 }
735
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600736 p2.f[0] = view->view->surface->width;
737 p2.f[1] = view->view->surface->height;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300738
739 /* transform top-left and bot-right corner into screen coordinates */
740 weston_matrix_transform(&matrix, &p1);
741 weston_matrix_transform(&matrix, &p2);
742
743 /* Compute the destination rectangle on screen, converting
744 * negative dimensions to flips.
745 */
746
747 dst_width = round(p2.f[0] - p1.f[0]);
748 if (dst_width < 0) {
749 dst_x = round(p2.f[0]);
750 dst_width = -dst_width;
751
752 if (!(flipt & TRANSFORM_TRANSPOSE))
753 flipt |= TRANSFORM_HFLIP;
754 else
755 flipt |= TRANSFORM_VFLIP;
756 } else {
757 dst_x = round(p1.f[0]);
758 }
759
760 dst_height = round(p2.f[1] - p1.f[1]);
761 if (dst_height < 0) {
762 dst_y = round(p2.f[1]);
763 dst_height = -dst_height;
764
765 if (!(flipt & TRANSFORM_TRANSPOSE))
766 flipt |= TRANSFORM_VFLIP;
767 else
768 flipt |= TRANSFORM_HFLIP;
769 } else {
770 dst_y = round(p1.f[1]);
771 }
772
773 if (dst_width == 0 || dst_height == 0) {
774 DBG("ignored, zero surface area before clipping\n");
775 return -1;
776 }
777
778#ifdef SURFACE_TRANSFORM
779 /* Dispmanx works as if you flipped the whole screen, when
780 * you flip an element. But, we want to flip an element in place.
781 * XXX: fixme
782 */
783 if (flipt & TRANSFORM_HFLIP)
784 dst_x = output_base->width - dst_x;
785 if (flipt & TRANSFORM_VFLIP)
786 dst_y = output_base->height - dst_y;
787 if (flipt & TRANSFORM_TRANSPOSE) {
788 int_swap(&dst_x, &dst_y);
789 int_swap(&dst_width, &dst_height);
790 }
791#else
792 switch (output_base->transform) {
793 case WL_OUTPUT_TRANSFORM_FLIPPED:
794 flipt = TRANSFORM_HFLIP;
795 break;
796 case WL_OUTPUT_TRANSFORM_NORMAL:
797 flipt = 0;
798 break;
799
800 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
801 flipt = TRANSFORM_HFLIP | TRANSFORM_VFLIP | TRANSFORM_TRANSPOSE;
802 break;
803 case WL_OUTPUT_TRANSFORM_90:
804 flipt = TRANSFORM_VFLIP | TRANSFORM_TRANSPOSE;
805 break;
806 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
807 flipt = TRANSFORM_VFLIP;
808 break;
809 case WL_OUTPUT_TRANSFORM_180:
810 flipt = TRANSFORM_HFLIP | TRANSFORM_VFLIP;
811 break;
812
813 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
814 flipt = TRANSFORM_TRANSPOSE;
815 break;
816 case WL_OUTPUT_TRANSFORM_270:
817 flipt = TRANSFORM_HFLIP | TRANSFORM_TRANSPOSE;
818 break;
819 default:
820 break;
821 }
822#endif
823
824 /* clip destination rectangle to screen dimensions */
825
826 if (dst_x < 0) {
827 t = (int64_t)dst_x * src_width / dst_width;
828 src_width += t;
829 dst_width += dst_x;
830 src_x -= t;
831 dst_x = 0;
832 }
833
834 if (dst_y < 0) {
835 t = (int64_t)dst_y * src_height / dst_height;
836 src_height += t;
837 dst_height += dst_y;
838 src_y -= t;
839 dst_y = 0;
840 }
841
842 over = dst_x + dst_width - output_base->width;
843 if (over > 0) {
844 t = (int64_t)over * src_width / dst_width;
845 src_width -= t;
846 dst_width -= over;
847 }
848
849 over = dst_y + dst_height - output_base->height;
850 if (over > 0) {
851 t = (int64_t)over * src_height / dst_height;
852 src_height -= t;
853 dst_height -= over;
854 }
855
856 src_width = int_max(src_width, 0);
857 src_height = int_max(src_height, 0);
858
Jason Ekstranda7af7042013-10-12 22:38:11 -0500859 DBG("rpir_view %p %dx%d: p1 %f, %f; p2 %f, %f\n", view,
Pekka Paalanen2d0f8b72014-05-09 15:08:06 +0300860 view->view->surface->width,
861 view->view->surface->height,
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300862 p1.f[0], p1.f[1], p2.f[0], p2.f[1]);
863 DBG("src rect %d;%d, %d;%d, %d;%dx%d;%d\n",
864 src_x >> 16, src_x & 0xffff,
865 src_y >> 16, src_y & 0xffff,
866 src_width >> 16, src_width & 0xffff,
867 src_height >> 16, src_height & 0xffff);
868 DBG("dest rect %d, %d, %dx%d%s%s%s\n",
869 dst_x, dst_y, dst_width, dst_height,
870 (flipt & TRANSFORM_HFLIP) ? " hflip" : "",
871 (flipt & TRANSFORM_VFLIP) ? " vflip" : "",
872 (flipt & TRANSFORM_TRANSPOSE) ? " transp" : "");
873
874 assert(src_x >= 0);
875 assert(src_y >= 0);
876 assert(dst_x >= 0);
877 assert(dst_y >= 0);
878
879 if (dst_width < 1 || dst_height < 1) {
880 DBG("ignored, zero surface area after clipping\n");
881 return -1;
882 }
883
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200884 /* EGL buffers will be upside-down related to what DispmanX expects */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500885 if (view->surface->buffer_type == BUFFER_TYPE_EGL)
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200886 flipt ^= TRANSFORM_VFLIP;
887
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300888 vc_dispmanx_rect_set(src_rect, src_x, src_y, src_width, src_height);
889 vc_dispmanx_rect_set(dst_rect, dst_x, dst_y, dst_width, dst_height);
890 *flipmask = flipt;
891
892 return 0;
893}
894
895static DISPMANX_TRANSFORM_T
896vc_image2dispmanx_transform(VC_IMAGE_TRANSFORM_T t)
897{
898 /* XXX: uhh, are these right? */
899 switch (t) {
900 case VC_IMAGE_ROT0:
901 return DISPMANX_NO_ROTATE;
902 case VC_IMAGE_MIRROR_ROT0:
903 return DISPMANX_FLIP_HRIZ;
904 case VC_IMAGE_MIRROR_ROT180:
905 return DISPMANX_FLIP_VERT;
906 case VC_IMAGE_ROT180:
907 return DISPMANX_ROTATE_180;
908 case VC_IMAGE_MIRROR_ROT90:
909 return DISPMANX_ROTATE_90 | DISPMANX_FLIP_HRIZ;
910 case VC_IMAGE_ROT270:
911 return DISPMANX_ROTATE_270;
912 case VC_IMAGE_ROT90:
913 return DISPMANX_ROTATE_90;
914 case VC_IMAGE_MIRROR_ROT270:
915 return DISPMANX_ROTATE_270 | DISPMANX_FLIP_VERT;
916 default:
917 assert(0 && "bad VC_IMAGE_TRANSFORM_T");
918 return DISPMANX_NO_ROTATE;
919 }
920}
921
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200922static DISPMANX_RESOURCE_HANDLE_T
923rpir_surface_get_resource(struct rpir_surface *surface)
924{
925 switch (surface->buffer_type) {
926 case BUFFER_TYPE_SHM:
927 case BUFFER_TYPE_NULL:
928 return surface->front->handle;
929 case BUFFER_TYPE_EGL:
930 if (surface->egl_front != NULL)
931 return surface->egl_front->resource_handle;
932 default:
933 return DISPMANX_NO_HANDLE;
934 }
935}
936
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100937#ifdef HAVE_ELEMENT_SET_OPAQUE_RECT
938static int
939rpir_surface_set_opaque_rect(struct rpir_surface *surface,
940 DISPMANX_UPDATE_HANDLE_T update)
941{
942 int ret;
943
944 if (pixman_region32_not_empty(&surface->surface->opaque) &&
945 surface->opaque_regions) {
946 pixman_box32_t *box;
947 VC_RECT_T opaque_rect;
948
949 box = pixman_region32_extents(&surface->surface->opaque);
950 opaque_rect.x = box->x1;
951 opaque_rect.y = box->y1;
952 opaque_rect.width = box->x2 - box->x1;
953 opaque_rect.height = box->y2 - box->y1;
954
955 ret = vc_dispmanx_element_set_opaque_rect(update,
956 surface->handle,
957 &opaque_rect);
958 if (ret) {
959 weston_log("vc_dispmanx_element_set_opaque_rect failed\n");
960 return -1;
961 }
962 }
963
964 return 0;
965}
966#endif
967
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300968static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500969rpir_view_dmx_add(struct rpir_view *view, struct rpir_output *output,
970 DISPMANX_UPDATE_HANDLE_T update, int layer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300971{
972 /* Do not use DISPMANX_FLAGS_ALPHA_PREMULT here.
973 * If you define PREMULT and ALPHA_MIX, the hardware will not
974 * multiply the source color with the element alpha, leading to
975 * bad colors. Instead, we define PREMULT during pixel data upload.
976 */
977 VC_DISPMANX_ALPHA_T alphasetup = {
978 DISPMANX_FLAGS_ALPHA_FROM_SOURCE |
979 DISPMANX_FLAGS_ALPHA_MIX,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500980 float2uint8(view->view->alpha), /* opacity 0-255 */
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300981 0 /* mask resource handle */
982 };
983 VC_RECT_T dst_rect;
984 VC_RECT_T src_rect;
985 VC_IMAGE_TRANSFORM_T flipmask;
986 int ret;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200987 DISPMANX_RESOURCE_HANDLE_T resource_handle;
988
Jason Ekstranda7af7042013-10-12 22:38:11 -0500989 resource_handle = rpir_surface_get_resource(view->surface);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200990 if (resource_handle == DISPMANX_NO_HANDLE) {
991 weston_log("%s: no buffer yet, aborting\n", __func__);
992 return 0;
993 }
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300994
Jason Ekstranda7af7042013-10-12 22:38:11 -0500995 ret = rpir_view_compute_rects(view, &src_rect, &dst_rect, &flipmask);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300996 if (ret < 0)
997 return 0;
998
Jason Ekstranda7af7042013-10-12 22:38:11 -0500999 view->handle = vc_dispmanx_element_add(
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001000 update,
1001 output->display,
1002 layer,
1003 &dst_rect,
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001004 resource_handle,
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001005 &src_rect,
1006 DISPMANX_PROTECTION_NONE,
1007 &alphasetup,
1008 NULL /* clamp */,
1009 vc_image2dispmanx_transform(flipmask));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001010 DBG("rpir_surface %p add %u, alpha %f resource %d\n", view,
1011 view->handle, view->view->alpha, resource_handle);
1012
1013 if (view->handle == DISPMANX_NO_HANDLE)
1014 return -1;
1015
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01001016#ifdef HAVE_ELEMENT_SET_OPAQUE_RECT
1017 ret = rpir_surface_set_opaque_rect(surface, update);
1018 if (ret < 0)
1019 return -1;
1020#endif
1021
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 view->surface->visible_views++;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001023
1024 return 1;
1025}
1026
1027static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001028rpir_view_dmx_swap(struct rpir_view *view,
1029 DISPMANX_UPDATE_HANDLE_T update)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001030{
1031 VC_RECT_T rect;
1032 pixman_box32_t *r;
1033
1034 /* XXX: skip, iff resource was not reallocated, and single-buffering */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001035 vc_dispmanx_element_change_source(update, view->handle,
1036 view->surface->front->handle);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001037
1038 /* This is current damage now, after rpir_surface_damage() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001039 r = pixman_region32_extents(&view->surface->prev_damage);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001040
1041 vc_dispmanx_rect_set(&rect, r->x1, r->y1,
1042 r->x2 - r->x1, r->y2 - r->y1);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001043 vc_dispmanx_element_modified(update, view->handle, &rect);
1044 DBG("rpir_view %p swap\n", view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001045}
1046
1047static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001048rpir_view_dmx_move(struct rpir_view *view,
1049 DISPMANX_UPDATE_HANDLE_T update, int layer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001050{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001051 uint8_t alpha = float2uint8(view->view->alpha);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001052 VC_RECT_T dst_rect;
1053 VC_RECT_T src_rect;
1054 VC_IMAGE_TRANSFORM_T flipmask;
1055 int ret;
1056
1057 /* XXX: return early, if all attributes stay the same */
1058
Jason Ekstranda7af7042013-10-12 22:38:11 -05001059 if (view->surface->buffer_type == BUFFER_TYPE_EGL) {
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001060 DISPMANX_RESOURCE_HANDLE_T resource_handle;
1061
Jason Ekstranda7af7042013-10-12 22:38:11 -05001062 resource_handle = rpir_surface_get_resource(view->surface);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001063 if (resource_handle == DISPMANX_NO_HANDLE) {
1064 weston_log("%s: no buffer yet, aborting\n", __func__);
1065 return 0;
1066 }
1067
1068 vc_dispmanx_element_change_source(update,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001069 view->handle,
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001070 resource_handle);
1071 }
1072
Jason Ekstranda7af7042013-10-12 22:38:11 -05001073 ret = rpir_view_compute_rects(view, &src_rect, &dst_rect, &flipmask);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001074 if (ret < 0)
1075 return 0;
1076
1077 ret = vc_dispmanx_element_change_attributes(
1078 update,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001079 view->handle,
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001080 ELEMENT_CHANGE_LAYER |
1081 ELEMENT_CHANGE_OPACITY |
1082 ELEMENT_CHANGE_TRANSFORM |
1083 ELEMENT_CHANGE_DEST_RECT |
1084 ELEMENT_CHANGE_SRC_RECT,
1085 layer,
1086 alpha,
1087 &dst_rect,
1088 &src_rect,
1089 DISPMANX_NO_HANDLE,
1090 /* This really is DISPMANX_TRANSFORM_T, no matter
1091 * what the header says. */
1092 vc_image2dispmanx_transform(flipmask));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001093 DBG("rpir_view %p move\n", view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001094
1095 if (ret)
1096 return -1;
1097
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01001098#ifdef HAVE_ELEMENT_SET_OPAQUE_RECT
1099 ret = rpir_surface_set_opaque_rect(surface, update);
1100 if (ret < 0)
1101 return -1;
1102#endif
1103
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001104 return 1;
1105}
1106
1107static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001108rpir_view_dmx_remove(struct rpir_view *view,
1109 DISPMANX_UPDATE_HANDLE_T update)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001110{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001111 if (view->handle == DISPMANX_NO_HANDLE)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001112 return;
1113
Jason Ekstranda7af7042013-10-12 22:38:11 -05001114 vc_dispmanx_element_remove(update, view->handle);
1115 DBG("rpir_view %p remove %u\n", view, view->handle);
1116 view->handle = DISPMANX_NO_HANDLE;
1117 view->surface->visible_views--;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001118}
1119
1120static void
1121rpir_surface_swap_pointers(struct rpir_surface *surface)
1122{
1123 struct rpi_resource *tmp;
1124
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001125 if (surface->buffer_type == BUFFER_TYPE_EGL) {
1126 if (surface->egl_back != NULL) {
1127 assert(surface->egl_old_front == NULL);
1128 surface->egl_old_front = surface->egl_front;
1129 surface->egl_front = surface->egl_back;
1130 surface->egl_back = NULL;
Tomeu Vizoso15767812013-10-24 15:38:31 +02001131 DBG("new front %d\n", surface->egl_front->resource_handle);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001132 }
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001133 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001134 tmp = surface->front;
1135 surface->front = surface->back;
1136 surface->back = tmp;
Tomeu Vizoso15767812013-10-24 15:38:31 +02001137 DBG("new back %p, new front %p\n", surface->back, surface->front);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001138 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139}
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001140
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141static int
1142is_view_not_visible(struct weston_view *view)
1143{
1144 /* Return true, if surface is guaranteed to be totally obscured. */
1145 int ret;
1146 pixman_region32_t unocc;
1147
1148 pixman_region32_init(&unocc);
1149 pixman_region32_subtract(&unocc, &view->transform.boundingbox,
1150 &view->clip);
1151 ret = !pixman_region32_not_empty(&unocc);
1152 pixman_region32_fini(&unocc);
1153
1154 return ret;
1155}
1156
1157static void
1158rpir_view_update(struct rpir_view *view, struct rpir_output *output,
1159 DISPMANX_UPDATE_HANDLE_T update, int layer)
1160{
1161 int ret;
1162 int obscured;
1163
Jason Ekstranda7af7042013-10-12 22:38:11 -05001164 obscured = is_view_not_visible(view->view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001165 if (obscured) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001166 DBG("rpir_view %p totally obscured.\n", view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001167
Jason Ekstranda7af7042013-10-12 22:38:11 -05001168 wl_list_remove(&view->link);
1169 if (view->handle == DISPMANX_NO_HANDLE) {
1170 wl_list_init(&view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001171 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 rpir_view_dmx_remove(view, update);
1173 wl_list_insert(&output->view_cleanup_list,
1174 &view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001175 }
1176
1177 goto out;
1178 }
1179
Jason Ekstranda7af7042013-10-12 22:38:11 -05001180 if (view->handle == DISPMANX_NO_HANDLE) {
1181 ret = rpir_view_dmx_add(view, output, update, layer);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001182 if (ret == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001183 wl_list_remove(&view->link);
1184 wl_list_init(&view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001185 } else if (ret < 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 weston_log("ERROR rpir_view_dmx_add() failed.\n");
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001187 }
1188 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001189 if (view->surface->need_swap)
1190 rpir_view_dmx_swap(view, update);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001191
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 ret = rpir_view_dmx_move(view, update, layer);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001193 if (ret == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001194 rpir_view_dmx_remove(view, update);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001195
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 wl_list_remove(&view->link);
1197 wl_list_insert(&output->view_cleanup_list,
1198 &view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001199 } else if (ret < 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 weston_log("ERROR rpir_view_dmx_move() failed.\n");
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001201 }
1202 }
1203
1204out:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 view->layer = layer;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001206}
1207
1208static int
1209rpi_renderer_read_pixels(struct weston_output *base,
1210 pixman_format_code_t format, void *pixels,
1211 uint32_t x, uint32_t y,
1212 uint32_t width, uint32_t height)
1213{
1214 struct rpir_output *output = to_rpir_output(base);
1215 struct rpi_resource *buffer = &output->capture_buffer;
1216 VC_RECT_T rect;
1217 uint32_t fb_width, fb_height;
1218 uint32_t dst_pitch;
1219 uint32_t i;
1220 int ret;
1221
Hardeningff39efa2013-09-18 23:56:35 +02001222 fb_width = base->current_mode->width;
1223 fb_height = base->current_mode->height;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001224
1225 DBG("%s(%u, %u, %u, %u), resource %p\n", __func__,
1226 x, y, width, height, buffer);
1227
1228 if (format != PIXMAN_a8r8g8b8) {
1229 weston_log("rpi-renderer error: bad read_format\n");
1230 return -1;
1231 }
1232
1233 dst_pitch = fb_width * 4;
1234
1235 if (buffer->handle == DISPMANX_NO_HANDLE) {
1236 free(output->capture_data);
1237 output->capture_data = NULL;
1238
1239 ret = rpi_resource_realloc(buffer, VC_IMAGE_ARGB8888,
1240 fb_width, fb_height,
1241 dst_pitch, fb_height);
1242 if (ret < 0) {
1243 weston_log("rpi-renderer error: "
1244 "allocating read buffer failed\n");
1245 return -1;
1246 }
1247
1248 ret = vc_dispmanx_snapshot(output->display, buffer->handle,
1249 VC_IMAGE_ROT0);
1250 if (ret) {
1251 weston_log("rpi-renderer error: "
1252 "vc_dispmanx_snapshot returned %d\n", ret);
1253 return -1;
1254 }
1255 DBG("%s: snapshot done.\n", __func__);
1256 }
1257
1258 /*
1259 * If vc_dispmanx_resource_read_data was able to read sub-rectangles,
1260 * we could read directly into 'pixels'. But it cannot, it does not
1261 * use rect.x or rect.width, and does this:
1262 * host_start = (uint8_t *)dst_address + (dst_pitch * p_rect->y);
1263 * In other words, it is only good for reading the full buffer in
1264 * one go.
1265 */
1266 vc_dispmanx_rect_set(&rect, 0, 0, fb_width, fb_height);
1267
1268 if (x == 0 && y == 0 && width == fb_width && height == fb_height) {
1269 ret = vc_dispmanx_resource_read_data(buffer->handle, &rect,
1270 pixels, dst_pitch);
1271 if (ret) {
1272 weston_log("rpi-renderer error: "
1273 "resource_read_data returned %d\n", ret);
1274 return -1;
1275 }
1276 DBG("%s: full frame done.\n", __func__);
1277 return 0;
1278 }
1279
1280 if (!output->capture_data) {
1281 output->capture_data = malloc(fb_height * dst_pitch);
1282 if (!output->capture_data) {
1283 weston_log("rpi-renderer error: "
1284 "out of memory\n");
1285 return -1;
1286 }
1287
1288 ret = vc_dispmanx_resource_read_data(buffer->handle, &rect,
1289 output->capture_data,
1290 dst_pitch);
1291 if (ret) {
1292 weston_log("rpi-renderer error: "
1293 "resource_read_data returned %d\n", ret);
1294 return -1;
1295 }
1296 }
1297
1298 for (i = 0; i < height; i++) {
1299 uint8_t *src = output->capture_data +
1300 (y + i) * dst_pitch + x * 4;
1301 uint8_t *dst = (uint8_t *)pixels + i * width * 4;
1302 memcpy(dst, src, width * 4);
1303 }
1304
1305 return 0;
1306}
1307
1308static void
1309rpir_output_dmx_remove_all(struct rpir_output *output,
1310 DISPMANX_UPDATE_HANDLE_T update)
1311{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001313
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 while (!wl_list_empty(&output->view_list)) {
1315 view = container_of(output->view_list.next,
1316 struct rpir_view, link);
1317 rpir_view_dmx_remove(view, update);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001318
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 wl_list_remove(&view->link);
1320 wl_list_insert(&output->view_cleanup_list, &view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001321 }
1322}
1323
1324static void
1325output_compute_matrix(struct weston_output *base)
1326{
1327 struct rpir_output *output = to_rpir_output(base);
1328 struct weston_matrix *matrix = &output->matrix;
1329 const float half_w = 0.5f * base->width;
1330 const float half_h = 0.5f * base->height;
1331 float mag;
1332 float dx, dy;
1333
1334 weston_matrix_init(matrix);
1335 weston_matrix_translate(matrix, -base->x, -base->y, 0.0f);
1336
1337#ifdef SURFACE_TRANSFORM
1338 weston_matrix_translate(matrix, -half_w, -half_h, 0.0f);
1339 switch (base->transform) {
1340 case WL_OUTPUT_TRANSFORM_FLIPPED:
1341 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1342 case WL_OUTPUT_TRANSFORM_NORMAL:
1343 /* weston_matrix_rotate_xy(matrix, 1.0f, 0.0f); no-op */
1344 weston_matrix_translate(matrix, half_w, half_h, 0.0f);
1345 break;
1346
1347 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1348 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1349 case WL_OUTPUT_TRANSFORM_90:
1350 weston_matrix_rotate_xy(matrix, 0.0f, 1.0f);
1351 weston_matrix_translate(matrix, half_h, half_w, 0.0f);
1352 break;
1353
1354 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1355 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1356 case WL_OUTPUT_TRANSFORM_180:
1357 weston_matrix_rotate_xy(matrix, -1.0f, 0.0f);
1358 weston_matrix_translate(matrix, half_w, half_h, 0.0f);
1359 break;
1360
1361 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1362 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1363 case WL_OUTPUT_TRANSFORM_270:
1364 weston_matrix_rotate_xy(matrix, 0.0f, -1.0f);
1365 weston_matrix_translate(matrix, half_h, half_w, 0.0f);
1366 break;
1367
1368 default:
1369 break;
1370 }
1371#endif
1372
1373 if (base->zoom.active) {
1374 /* The base->zoom stuff is in GL coordinate system */
1375 mag = 1.0f / (1.0f - base->zoom.spring_z.current);
1376 dx = -(base->zoom.trans_x + 1.0f) * half_w;
1377 dy = -(base->zoom.trans_y + 1.0f) * half_h;
1378 weston_matrix_translate(matrix, dx, dy, 0.0f);
1379 weston_matrix_scale(matrix, mag, mag, 1.0f);
1380 weston_matrix_translate(matrix, half_w, half_h, 0.0f);
1381 }
1382}
1383
1384/* Note: this won't work right for multiple outputs. A DispmanX Element
1385 * is tied to one DispmanX Display, i.e. output.
1386 */
1387static void
1388rpi_renderer_repaint_output(struct weston_output *base,
1389 pixman_region32_t *output_damage)
1390{
1391 struct weston_compositor *compositor = base->compositor;
1392 struct rpir_output *output = to_rpir_output(base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001393 struct weston_view *wv;
1394 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001395 struct wl_list done_list;
1396 int layer = 1;
1397
1398 assert(output->update != DISPMANX_NO_HANDLE);
1399
1400 output_compute_matrix(base);
1401
1402 rpi_resource_release(&output->capture_buffer);
1403 free(output->capture_data);
1404 output->capture_data = NULL;
1405
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 /* Swap resources on surfaces as needed */
1407 wl_list_for_each_reverse(wv, &compositor->view_list, link)
1408 wv->surface->touched = 0;
1409
1410 wl_list_for_each_reverse(wv, &compositor->view_list, link) {
1411 view = to_rpir_view(wv);
1412
1413 if (!wv->surface->touched) {
1414 wv->surface->touched = 1;
1415
Tomeu Vizoso44247742013-10-24 15:38:32 +02001416 if (view->surface->buffer_type == BUFFER_TYPE_EGL ||
1417 view->surface->need_swap)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418 rpir_surface_swap_pointers(view->surface);
1419 }
1420
Tomeu Vizoso74987582013-10-25 10:34:38 +02001421 if (view->surface->buffer_type == BUFFER_TYPE_EGL) {
1422 struct weston_buffer *buffer;
1423 buffer = view->surface->egl_front->buffer_ref.buffer;
1424 if (buffer != NULL) {
Pekka Paalanend5fbfb22013-11-22 17:30:39 +02001425 rpi_buffer_egl_lock(buffer);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001426 } else {
Tomeu Vizoso74987582013-10-25 10:34:38 +02001427 weston_log("warning: client destroyed current front buffer\n");
1428
1429 wl_list_remove(&view->link);
1430 if (view->handle == DISPMANX_NO_HANDLE) {
1431 wl_list_init(&view->link);
1432 } else {
1433 rpir_view_dmx_remove(view, output->update);
1434 wl_list_insert(&output->view_cleanup_list,
1435 &view->link);
1436 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001437 }
1438 }
1439 }
1440
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001441 /* update all renderable surfaces */
1442 wl_list_init(&done_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001443 wl_list_for_each_reverse(wv, &compositor->view_list, link) {
1444 if (wv->plane != &compositor->primary_plane)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001445 continue;
1446
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 view = to_rpir_view(wv);
1448 assert(!wl_list_empty(&view->link) ||
1449 view->handle == DISPMANX_NO_HANDLE);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001450
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 wl_list_remove(&view->link);
1452 wl_list_insert(&done_list, &view->link);
1453 rpir_view_update(view, output, output->update, layer++);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001454 }
1455
Jason Ekstranda7af7042013-10-12 22:38:11 -05001456 /* Mark all surfaces as swapped */
1457 wl_list_for_each_reverse(wv, &compositor->view_list, link)
1458 to_rpir_surface(wv->surface)->need_swap = 0;
1459
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001460 /* Remove all surfaces that are still on screen, but were
1461 * not rendered this time.
1462 */
1463 rpir_output_dmx_remove_all(output, output->update);
1464
Jason Ekstranda7af7042013-10-12 22:38:11 -05001465 wl_list_insert_list(&output->view_list, &done_list);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001466 output->update = DISPMANX_NO_HANDLE;
1467
1468 /* The frame_signal is emitted in rpi_renderer_finish_frame(),
1469 * so that the firmware can capture the up-to-date contents.
1470 */
1471}
1472
1473static void
1474rpi_renderer_flush_damage(struct weston_surface *base)
1475{
1476 /* Called for every surface just before repainting it, if
1477 * having an shm buffer.
1478 */
1479 struct rpir_surface *surface = to_rpir_surface(base);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001480 struct weston_buffer *buffer = surface->buffer_ref.buffer;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001481 int ret;
1482
1483 assert(buffer);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001484 assert(wl_shm_buffer_get(buffer->resource));
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001485
1486 ret = rpir_surface_damage(surface, buffer, &base->damage);
1487 if (ret)
1488 weston_log("%s error: updating Dispmanx resource failed.\n",
1489 __func__);
1490
1491 weston_buffer_reference(&surface->buffer_ref, NULL);
1492}
1493
1494static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001495rpi_renderer_attach(struct weston_surface *base, struct weston_buffer *buffer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001496{
1497 /* Called every time a client commits an attach. */
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001498 struct rpir_surface *surface = to_rpir_surface(base);
1499
1500 assert(surface);
1501 if (!surface)
1502 return;
1503
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001504 if (surface->buffer_type == BUFFER_TYPE_SHM) {
1505 if (!surface->single_buffer)
1506 /* XXX: need to check if in middle of update */
1507 rpi_resource_release(surface->back);
1508
Jason Ekstranda7af7042013-10-12 22:38:11 -05001509 if (!surface->visible_views)
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001510 /* XXX: cannot do this, if middle of an update */
1511 rpi_resource_release(surface->front);
1512
1513 weston_buffer_reference(&surface->buffer_ref, NULL);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001514 }
1515
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001516 /* If buffer is NULL, Weston core unmaps the surface, the surface
1517 * will not appear in repaint list, and so rpi_renderer_repaint_output
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001518 * will remove the DispmanX element. Later, for SHM, also the front
1519 * buffer will be released in the cleanup_list processing.
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001520 */
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001521 if (!buffer)
1522 return;
1523
1524 if (wl_shm_buffer_get(buffer->resource)) {
1525 surface->buffer_type = BUFFER_TYPE_SHM;
1526 buffer->shm_buffer = wl_shm_buffer_get(buffer->resource);
1527 buffer->width = wl_shm_buffer_get_width(buffer->shm_buffer);
1528 buffer->height = wl_shm_buffer_get_height(buffer->shm_buffer);
1529
1530 weston_buffer_reference(&surface->buffer_ref, buffer);
1531 } else {
1532#if ENABLE_EGL
1533 struct rpi_renderer *renderer = to_rpi_renderer(base->compositor);
Tomeu Vizosoa8e5f292013-10-09 11:29:45 +02001534 struct wl_resource *wl_resource = buffer->resource;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001535
1536 if (!renderer->has_bind_display ||
1537 !renderer->query_buffer(renderer->egl_display,
1538 wl_resource,
1539 EGL_WIDTH, &buffer->width)) {
1540 weston_log("unhandled buffer type!\n");
1541 weston_buffer_reference(&surface->buffer_ref, NULL);
1542 surface->buffer_type = BUFFER_TYPE_NULL;
1543 }
1544
1545 renderer->query_buffer(renderer->egl_display,
1546 wl_resource,
1547 EGL_HEIGHT, &buffer->height);
1548
1549 surface->buffer_type = BUFFER_TYPE_EGL;
1550
1551 if(surface->egl_back == NULL)
1552 surface->egl_back = calloc(1, sizeof *surface->egl_back);
1553
1554 weston_buffer_reference(&surface->egl_back->buffer_ref, buffer);
1555 surface->egl_back->resource_handle =
1556 vc_dispmanx_get_handle_from_wl_buffer(wl_resource);
1557#else
1558 weston_log("unhandled buffer type!\n");
1559 weston_buffer_reference(&surface->buffer_ref, NULL);
1560 surface->buffer_type = BUFFER_TYPE_NULL;
1561#endif
1562 }
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001563}
1564
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001565static void
1566rpir_surface_handle_surface_destroy(struct wl_listener *listener, void *data)
1567{
1568 struct rpir_surface *surface;
1569 struct weston_surface *base = data;
1570
1571 surface = container_of(listener, struct rpir_surface,
1572 surface_destroy_listener);
1573
1574 assert(surface);
1575 assert(surface->surface == base);
1576 if (!surface)
1577 return;
1578
1579 surface->surface = NULL;
1580 base->renderer_state = NULL;
1581
1582 if (wl_list_empty(&surface->views))
1583 rpir_surface_destroy(surface);
1584}
1585
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001586static int
1587rpi_renderer_create_surface(struct weston_surface *base)
1588{
1589 struct rpi_renderer *renderer = to_rpi_renderer(base->compositor);
1590 struct rpir_surface *surface;
1591
1592 assert(base->renderer_state == NULL);
1593
1594 surface = rpir_surface_create(renderer);
1595 if (!surface)
1596 return -1;
1597
1598 surface->surface = base;
1599 base->renderer_state = surface;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +03001600
1601 surface->surface_destroy_listener.notify =
1602 rpir_surface_handle_surface_destroy;
1603 wl_signal_add(&base->destroy_signal,
1604 &surface->surface_destroy_listener);
1605
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001606 return 0;
1607}
1608
Jason Ekstranda7af7042013-10-12 22:38:11 -05001609static int
1610rpi_renderer_create_view(struct weston_view *base)
1611{
1612 struct rpir_surface *surface = to_rpir_surface(base->surface);
1613 struct rpir_view *view;
1614
1615 assert(base->renderer_state == NULL);
1616
1617 view = rpir_view_create(surface);
1618 if (!view)
1619 return -1;
1620
1621 view->view = base;
1622 base->renderer_state = view;
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +01001623
1624 view->view_destroy_listener.notify =
1625 rpir_view_handle_view_destroy;
1626 wl_signal_add(&base->destroy_signal,
1627 &view->view_destroy_listener);
1628
Jason Ekstranda7af7042013-10-12 22:38:11 -05001629 return 0;
1630}
1631
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001632static void
1633rpi_renderer_surface_set_color(struct weston_surface *base,
1634 float red, float green, float blue, float alpha)
1635{
1636 struct rpir_surface *surface = to_rpir_surface(base);
1637 uint8_t color[4];
1638 VC_RECT_T rect;
1639 int ret;
1640
1641 assert(surface);
1642
1643 ret = rpi_resource_realloc(surface->back, VC_IMAGE_ARGB8888,
1644 1, 1, 4, 1);
1645 if (ret < 0) {
1646 weston_log("Error: %s: rpi_resource_realloc failed.\n",
1647 __func__);
1648 return;
1649 }
1650
1651 color[0] = float2uint8(blue);
1652 color[1] = float2uint8(green);
1653 color[2] = float2uint8(red);
1654 color[3] = float2uint8(alpha);
1655
1656 vc_dispmanx_rect_set(&rect, 0, 0, 1, 1);
1657 ret = vc_dispmanx_resource_write_data(surface->back->handle,
1658 VC_IMAGE_ARGB8888,
1659 4, color, &rect);
1660 if (ret) {
1661 weston_log("Error: %s: resource_write_data failed.\n",
1662 __func__);
1663 return;
1664 }
1665
1666 DBG("%s: resource %p solid color BGRA %u,%u,%u,%u\n", __func__,
1667 surface->back, color[0], color[1], color[2], color[3]);
1668
1669 /*pixman_region32_copy(&surface->prev_damage, damage);*/
1670 surface->need_swap = 1;
1671}
1672
1673static void
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +01001674rpir_view_handle_view_destroy(struct wl_listener *listener, void *data)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001675{
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +01001676 struct rpir_view *view;
1677 struct weston_view *base = data;
1678
1679 view = container_of(listener, struct rpir_view, view_destroy_listener);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001680
1681 assert(view);
1682 assert(view->view == base);
1683 if (!view)
1684 return;
1685
Tomeu Vizoso34dad7d2013-10-25 10:34:37 +02001686 view->view = NULL;
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +01001687 base->renderer_state = NULL;
Tomeu Vizoso34dad7d2013-10-25 10:34:37 +02001688
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +01001689 /* If guaranteed to not be on screen, just destroy it. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001690 if (wl_list_empty(&view->link))
1691 rpir_view_destroy(view);
1692
1693 /* Otherwise, the view is either on screen and needs
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001694 * to be removed by a repaint update, or it is in the
Jason Ekstranda7af7042013-10-12 22:38:11 -05001695 * view_cleanup_list, and will be destroyed by
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001696 * rpi_renderer_finish_frame().
1697 */
1698}
1699
1700static void
1701rpi_renderer_destroy(struct weston_compositor *compositor)
1702{
1703 struct rpi_renderer *renderer = to_rpi_renderer(compositor);
1704
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001705#if ENABLE_EGL
1706 if (renderer->has_bind_display)
1707 renderer->unbind_display(renderer->egl_display,
1708 compositor->wl_display);
1709#endif
1710
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001711 free(renderer);
1712 compositor->renderer = NULL;
1713}
1714
1715WL_EXPORT int
1716rpi_renderer_create(struct weston_compositor *compositor,
1717 const struct rpi_renderer_parameters *params)
1718{
1719 struct rpi_renderer *renderer;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001720#if ENABLE_EGL
1721 const char *extensions;
1722 EGLBoolean ret;
1723 EGLint major, minor;
1724#endif
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001725
1726 weston_log("Initializing the DispmanX compositing renderer\n");
1727
1728 renderer = calloc(1, sizeof *renderer);
1729 if (renderer == NULL)
1730 return -1;
1731
1732 renderer->single_buffer = params->single_buffer;
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01001733 renderer->enable_opaque_regions = params->opaque_regions;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001734
1735 renderer->base.read_pixels = rpi_renderer_read_pixels;
1736 renderer->base.repaint_output = rpi_renderer_repaint_output;
1737 renderer->base.flush_damage = rpi_renderer_flush_damage;
1738 renderer->base.attach = rpi_renderer_attach;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001739 renderer->base.surface_set_color = rpi_renderer_surface_set_color;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001740 renderer->base.destroy = rpi_renderer_destroy;
1741
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001742#ifdef ENABLE_EGL
1743 renderer->egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1744 if (renderer->egl_display == EGL_NO_DISPLAY) {
1745 weston_log("failed to create EGL display\n");
U. Artie Eoffe067b302014-01-17 14:00:18 -08001746 free(renderer);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001747 return -1;
1748 }
1749
1750 if (!eglInitialize(renderer->egl_display, &major, &minor)) {
1751 weston_log("failed to initialize EGL display\n");
U. Artie Eoffe067b302014-01-17 14:00:18 -08001752 free(renderer);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001753 return -1;
1754 }
1755
1756 renderer->bind_display =
1757 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1758 renderer->unbind_display =
1759 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1760 renderer->query_buffer =
1761 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
1762
1763 extensions = (const char *) eglQueryString(renderer->egl_display,
1764 EGL_EXTENSIONS);
1765 if (!extensions) {
1766 weston_log("Retrieving EGL extension string failed.\n");
U. Artie Eoffac9f35a2014-01-17 16:28:15 -08001767 eglTerminate(renderer->egl_display);
U. Artie Eoffe067b302014-01-17 14:00:18 -08001768 free(renderer);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001769 return -1;
1770 }
1771
1772 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1773 renderer->has_bind_display = 1;
1774
1775 if (renderer->has_bind_display) {
1776 ret = renderer->bind_display(renderer->egl_display,
1777 compositor->wl_display);
1778 if (!ret)
1779 renderer->has_bind_display = 0;
1780 }
1781#endif
1782
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001783 compositor->renderer = &renderer->base;
1784 compositor->read_format = PIXMAN_a8r8g8b8;
1785 /* WESTON_CAP_ROTATION_ANY not supported */
1786
Tomeu Vizoso03681892013-08-06 20:05:57 +02001787 wl_display_add_shm_format(compositor->wl_display, WL_SHM_FORMAT_RGB565);
1788
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001789 return 0;
1790}
1791
1792WL_EXPORT int
1793rpi_renderer_output_create(struct weston_output *base,
1794 DISPMANX_DISPLAY_HANDLE_T display)
1795{
1796 struct rpir_output *output;
1797
1798 assert(base->renderer_state == NULL);
1799
1800 output = calloc(1, sizeof *output);
1801 if (!output)
1802 return -1;
1803
1804 output->display = display;
1805 output->update = DISPMANX_NO_HANDLE;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001806 wl_list_init(&output->view_list);
1807 wl_list_init(&output->view_cleanup_list);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001808 rpi_resource_init(&output->capture_buffer);
1809 base->renderer_state = output;
1810
1811 return 0;
1812}
1813
1814WL_EXPORT void
1815rpi_renderer_output_destroy(struct weston_output *base)
1816{
1817 struct rpir_output *output = to_rpir_output(base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001819 DISPMANX_UPDATE_HANDLE_T update;
1820
1821 rpi_resource_release(&output->capture_buffer);
1822 free(output->capture_data);
1823 output->capture_data = NULL;
1824
1825 update = vc_dispmanx_update_start(0);
1826 rpir_output_dmx_remove_all(output, update);
1827 vc_dispmanx_update_submit_sync(update);
1828
Jason Ekstranda7af7042013-10-12 22:38:11 -05001829 while (!wl_list_empty(&output->view_cleanup_list)) {
1830 view = container_of(output->view_cleanup_list.next,
1831 struct rpir_view, link);
1832 rpir_view_destroy(view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001833 }
1834
1835 free(output);
1836 base->renderer_state = NULL;
1837}
1838
1839WL_EXPORT void
1840rpi_renderer_set_update_handle(struct weston_output *base,
1841 DISPMANX_UPDATE_HANDLE_T handle)
1842{
1843 struct rpir_output *output = to_rpir_output(base);
1844
1845 output->update = handle;
1846}
1847
1848WL_EXPORT void
1849rpi_renderer_finish_frame(struct weston_output *base)
1850{
1851 struct rpir_output *output = to_rpir_output(base);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001852 struct weston_compositor *compositor = base->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001853 struct weston_view *wv;
1854 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001855
Jason Ekstranda7af7042013-10-12 22:38:11 -05001856 while (!wl_list_empty(&output->view_cleanup_list)) {
1857 view = container_of(output->view_cleanup_list.next,
1858 struct rpir_view, link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001859
Jason Ekstranda7af7042013-10-12 22:38:11 -05001860 if (view->view) {
1861 /* The weston_view still exists, but is
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001862 * temporarily not visible, and hence its Element
1863 * was removed. The current front buffer contents
1864 * must be preserved.
1865 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001866 if (!view->surface->visible_views)
1867 rpi_resource_release(view->surface->back);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001868
Jason Ekstranda7af7042013-10-12 22:38:11 -05001869 wl_list_remove(&view->link);
1870 wl_list_init(&view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001871 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001872 rpir_view_destroy(view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001873 }
1874 }
1875
Jason Ekstranda7af7042013-10-12 22:38:11 -05001876 wl_list_for_each(wv, &compositor->view_list, link) {
1877 view = to_rpir_view(wv);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001878
Jason Ekstranda7af7042013-10-12 22:38:11 -05001879 if (view->surface->buffer_type != BUFFER_TYPE_EGL)
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001880 continue;
1881
Tomeu Vizoso74987582013-10-25 10:34:38 +02001882 rpir_egl_buffer_destroy(view->surface->egl_old_front);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001883 view->surface->egl_old_front = NULL;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001884 }
1885
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001886 wl_signal_emit(&base->frame_signal, base);
1887}