blob: d2185327b52a56e1e1c8f70c8415be807afe8fe1 [file] [log] [blame]
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001/*
2 * Copyright © 2012-2013 Raspberry Pi Foundation
3 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Pekka Paalanend7265bc2013-05-22 18:03:06 +030011 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Pekka Paalanend7265bc2013-05-22 18:03:06 +030024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Pekka Paalanend7265bc2013-05-22 18:03:06 +030028#include <stdlib.h>
29#include <assert.h>
30#include <string.h>
31
Pekka Paalanend7265bc2013-05-22 18:03:06 +030032#ifdef HAVE_BCM_HOST
33# include <bcm_host.h>
34#else
35# include "rpi-bcm-stubs.h"
36#endif
37
38#include "compositor.h"
39#include "rpi-renderer.h"
40
Tomeu Vizosob4659eb2013-10-07 11:02:20 +020041#ifdef ENABLE_EGL
42#include <EGL/egl.h>
43#include <EGL/eglext.h>
44#include "weston-egl-ext.h"
45#endif
46
Pekka Paalanend7265bc2013-05-22 18:03:06 +030047/*
48 * Dispmanx API offers alpha-blended overlays for hardware compositing.
49 * The final composite consists of dispmanx elements, and their contents:
50 * the dispmanx resource assigned to the element. The elements may be
51 * scanned out directly, or composited to a temporary surface, depending on
52 * how the firmware decides to handle the scene. Updates to multiple elements
53 * may be queued in a single dispmanx update object, resulting in atomic and
54 * vblank synchronized display updates.
55 *
56 * To avoid tearing and display artifacts, the current dispmanx resource in a
57 * dispmanx element must not be touched. Therefore each element must be
58 * double-buffered, using two resources, the front and the back. While a
59 * dispmanx update is running, the both resources must be considered in use.
60 *
61 * A resource may be destroyed only, when the update removing the element has
62 * completed. Otherwise you risk showing an incomplete composition.
63 */
64
65#ifndef ELEMENT_CHANGE_LAYER
66/* copied from interface/vmcs_host/vc_vchi_dispmanx.h of userland.git */
67#define ELEMENT_CHANGE_LAYER (1<<0)
68#define ELEMENT_CHANGE_OPACITY (1<<1)
69#define ELEMENT_CHANGE_DEST_RECT (1<<2)
70#define ELEMENT_CHANGE_SRC_RECT (1<<3)
71#define ELEMENT_CHANGE_MASK_RESOURCE (1<<4)
72#define ELEMENT_CHANGE_TRANSFORM (1<<5)
73#endif
74
75#if 0
76#define DBG(...) \
77 weston_log(__VA_ARGS__)
78#else
79#define DBG(...) do {} while (0)
80#endif
81
82/* If we had a fully featured vc_dispmanx_resource_write_data()... */
83/*#define HAVE_RESOURCE_WRITE_DATA_RECT 1*/
84
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +010085/* If we had a vc_dispmanx_element_set_opaque_rect()... */
86/*#define HAVE_ELEMENT_SET_OPAQUE_RECT 1*/
87
Pekka Paalanend7265bc2013-05-22 18:03:06 +030088struct rpi_resource {
89 DISPMANX_RESOURCE_HANDLE_T handle;
90 int width;
91 int height; /* height of the image (valid pixel data) */
92 int stride; /* bytes */
93 int buffer_height; /* height of the buffer */
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +010094 int enable_opaque_regions;
Pekka Paalanend7265bc2013-05-22 18:03:06 +030095 VC_IMAGE_TYPE_T ifmt;
96};
97
98struct rpir_output;
99
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200100struct rpir_egl_buffer {
101 struct weston_buffer_reference buffer_ref;
102 DISPMANX_RESOURCE_HANDLE_T resource_handle;
103};
104
105enum buffer_type {
106 BUFFER_TYPE_NULL,
107 BUFFER_TYPE_SHM,
108 BUFFER_TYPE_EGL
109};
110
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300111struct rpir_surface {
112 struct weston_surface *surface;
113
Jason Ekstranda7af7042013-10-12 22:38:11 -0500114 struct wl_list views;
115 int visible_views;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300116 int need_swap;
117 int single_buffer;
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100118 int enable_opaque_regions;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300119
120 struct rpi_resource resources[2];
121 struct rpi_resource *front;
122 struct rpi_resource *back;
123 pixman_region32_t prev_damage;
124
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200125 struct rpir_egl_buffer *egl_front;
126 struct rpir_egl_buffer *egl_back;
127 struct rpir_egl_buffer *egl_old_front;
128
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300129 struct weston_buffer_reference buffer_ref;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200130 enum buffer_type buffer_type;
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300131
132 struct wl_listener surface_destroy_listener;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300133};
134
Jason Ekstranda7af7042013-10-12 22:38:11 -0500135struct rpir_view {
136 struct rpir_surface *surface;
137 struct wl_list surface_link;
138 struct weston_view *view;
139
140 /* If link is empty, the view is guaranteed to not be on screen,
141 * i.e. updates removing Elements have completed.
142 */
143 struct wl_list link;
144
145 DISPMANX_ELEMENT_HANDLE_T handle;
146 int layer;
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +0100147
148 struct wl_listener view_destroy_listener;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500149};
150
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300151struct rpir_output {
152 DISPMANX_DISPLAY_HANDLE_T display;
153
154 DISPMANX_UPDATE_HANDLE_T update;
155 struct weston_matrix matrix;
156
157 /* all Elements currently on screen */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500158 struct wl_list view_list; /* struct rpir_surface::link */
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300159
160 /* Elements just removed, waiting for update completion */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500161 struct wl_list view_cleanup_list; /* struct rpir_surface::link */
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300162
163 struct rpi_resource capture_buffer;
164 uint8_t *capture_data;
165};
166
167struct rpi_renderer {
168 struct weston_renderer base;
169
170 int single_buffer;
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100171 int enable_opaque_regions;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200172
173#ifdef ENABLE_EGL
174 EGLDisplay egl_display;
175
176 PFNEGLBINDWAYLANDDISPLAYWL bind_display;
177 PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
178 PFNEGLQUERYWAYLANDBUFFERWL query_buffer;
179#endif
180 int has_bind_display;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300181};
182
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300183static int
184rpi_renderer_create_surface(struct weston_surface *base);
185
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +0100186static int
187rpi_renderer_create_view(struct weston_view *base);
188
189static void
190rpir_view_handle_view_destroy(struct wl_listener *listener, void *data);
191
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300192static inline struct rpir_surface *
193to_rpir_surface(struct weston_surface *surface)
194{
Ander Conselvan de Oliveiraaa398ae2013-10-25 16:26:33 +0300195 if (!surface->renderer_state)
196 rpi_renderer_create_surface(surface);
197
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300198 return surface->renderer_state;
199}
200
Jason Ekstranda7af7042013-10-12 22:38:11 -0500201static inline struct rpir_view *
202to_rpir_view(struct weston_view *view)
203{
Tomeu Vizoso96dc9e42013-10-28 10:17:45 +0100204 if (!view->renderer_state)
205 rpi_renderer_create_view(view);
206
Jason Ekstranda7af7042013-10-12 22:38:11 -0500207 return view->renderer_state;
208}
209
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300210static inline struct rpir_output *
211to_rpir_output(struct weston_output *output)
212{
213 return output->renderer_state;
214}
215
216static inline struct rpi_renderer *
217to_rpi_renderer(struct weston_compositor *compositor)
218{
219 return container_of(compositor->renderer, struct rpi_renderer, base);
220}
221
222static inline int
223int_max(int a, int b)
224{
225 return a > b ? a : b;
226}
227
228static inline void
229int_swap(int *a, int *b)
230{
231 int tmp = *a;
232 *a = *b;
233 *b = tmp;
234}
235
236static uint8_t
237float2uint8(float f)
238{
239 int v = roundf(f * 255.0f);
240
241 return v < 0 ? 0 : (v > 255 ? 255 : v);
242}
243
244static void
245rpi_resource_init(struct rpi_resource *resource)
246{
247 resource->handle = DISPMANX_NO_HANDLE;
248}
249
250static void
251rpi_resource_release(struct rpi_resource *resource)
252{
253 if (resource->handle == DISPMANX_NO_HANDLE)
254 return;
255
256 vc_dispmanx_resource_delete(resource->handle);
257 DBG("resource %p release\n", resource);
258 resource->handle = DISPMANX_NO_HANDLE;
259}
260
261static int
262rpi_resource_realloc(struct rpi_resource *resource, VC_IMAGE_TYPE_T ifmt,
263 int width, int height, int stride, int buffer_height)
264{
265 uint32_t dummy;
266
267 if (resource->handle != DISPMANX_NO_HANDLE &&
268 resource->width == width &&
269 resource->height == height &&
270 resource->stride == stride &&
271 resource->buffer_height == buffer_height &&
272 resource->ifmt == ifmt)
273 return 0;
274
275 rpi_resource_release(resource);
276
277 /* NOTE: if stride is not a multiple of 16 pixels in bytes,
278 * the vc_image_* functions may break. Dispmanx elements
279 * should be fine, though. Buffer_height probably has similar
280 * constraints, too.
281 */
282 resource->handle =
283 vc_dispmanx_resource_create(ifmt,
284 width | (stride << 16),
285 height | (buffer_height << 16),
286 &dummy);
287 if (resource->handle == DISPMANX_NO_HANDLE)
288 return -1;
289
290 resource->width = width;
291 resource->height = height;
292 resource->stride = stride;
293 resource->buffer_height = buffer_height;
294 resource->ifmt = ifmt;
295 DBG("resource %p alloc\n", resource);
296 return 1;
297}
298
299/* A firmware workaround for broken ALPHA_PREMULT + ALPHA_MIX hardware. */
300#define PREMULT_ALPHA_FLAG (1 << 31)
301
302static VC_IMAGE_TYPE_T
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500303shm_buffer_get_vc_format(struct wl_shm_buffer *buffer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300304{
305 switch (wl_shm_buffer_get_format(buffer)) {
306 case WL_SHM_FORMAT_XRGB8888:
307 return VC_IMAGE_XRGB8888;
308 case WL_SHM_FORMAT_ARGB8888:
309 return VC_IMAGE_ARGB8888 | PREMULT_ALPHA_FLAG;
Tomeu Vizoso03681892013-08-06 20:05:57 +0200310 case WL_SHM_FORMAT_RGB565:
311 return VC_IMAGE_RGB565;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300312 default:
313 /* invalid format */
314 return VC_IMAGE_MIN;
315 }
316}
317
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100318#ifndef HAVE_ELEMENT_SET_OPAQUE_RECT
319static uint32_t *
320apply_opaque_region(struct wl_shm_buffer *buffer,
321 pixman_region32_t *opaque_region)
322{
323 uint32_t *src, *dst;
324 int width;
325 int height;
326 int stride;
327 int x, y;
328
329 width = wl_shm_buffer_get_width(buffer);
330 height = wl_shm_buffer_get_height(buffer);
331 stride = wl_shm_buffer_get_stride(buffer);
332 src = wl_shm_buffer_get_data(buffer);
333
334 dst = malloc(height * stride);
335 if (dst == NULL) {
336 weston_log("rpi-renderer error: out of memory\n");
337 return NULL;
338 }
339
340 for (y = 0; y < height; y++) {
341 for (x = 0; x < width; x++) {
342 int i = y * stride / 4 + x;
Derek Foremand5e21872014-11-21 12:31:04 -0600343 if (pixman_region32_contains_point (opaque_region, x, y, NULL)) {
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100344 dst[i] = src[i] | 0xff000000;
345 } else {
346 dst[i] = src[i];
347 }
348 }
349 }
350
351 return dst;
352}
353#endif
354
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300355static int
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500356rpi_resource_update(struct rpi_resource *resource, struct weston_buffer *buffer,
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100357 pixman_region32_t *region, pixman_region32_t *opaque_region)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300358{
359 pixman_region32_t write_region;
360 pixman_box32_t *r;
361 VC_RECT_T rect;
362 VC_IMAGE_TYPE_T ifmt;
363 uint32_t *pixels;
364 int width;
365 int height;
366 int stride;
367 int ret;
U. Artie Eoff5e854bc2014-01-17 13:56:41 -0800368 int applied_opaque_region = 0;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300369#ifdef HAVE_RESOURCE_WRITE_DATA_RECT
370 int n;
371#endif
372
373 if (!buffer)
374 return -1;
375
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500376 ifmt = shm_buffer_get_vc_format(buffer->shm_buffer);
377 width = wl_shm_buffer_get_width(buffer->shm_buffer);
378 height = wl_shm_buffer_get_height(buffer->shm_buffer);
379 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
380 pixels = wl_shm_buffer_get_data(buffer->shm_buffer);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300381
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100382#ifndef HAVE_ELEMENT_SET_OPAQUE_RECT
383 if (pixman_region32_not_empty(opaque_region) &&
384 wl_shm_buffer_get_format(buffer->shm_buffer) == WL_SHM_FORMAT_ARGB8888 &&
385 resource->enable_opaque_regions) {
386 pixels = apply_opaque_region(buffer->shm_buffer, opaque_region);
387
388 if (!pixels)
389 return -1;
U. Artie Eoff5e854bc2014-01-17 13:56:41 -0800390
391 applied_opaque_region = 1;
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100392 }
393#endif
394
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300395 ret = rpi_resource_realloc(resource, ifmt & ~PREMULT_ALPHA_FLAG,
396 width, height, stride, height);
U. Artie Eoff5e854bc2014-01-17 13:56:41 -0800397 if (ret < 0) {
398 if (applied_opaque_region)
399 free(pixels);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300400 return -1;
U. Artie Eoff5e854bc2014-01-17 13:56:41 -0800401 }
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300402
403 pixman_region32_init_rect(&write_region, 0, 0, width, height);
404 if (ret == 0)
405 pixman_region32_intersect(&write_region,
406 &write_region, region);
407
Neil Robertse5051712013-11-13 15:44:06 +0000408 wl_shm_buffer_begin_access(buffer->shm_buffer);
409
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300410#ifdef HAVE_RESOURCE_WRITE_DATA_RECT
411 /* XXX: Can this do a format conversion, so that scanout does not have to? */
412 r = pixman_region32_rectangles(&write_region, &n);
413 while (n--) {
414 vc_dispmanx_rect_set(&rect, r[n].x1, r[n].y1,
415 r[n].x2 - r[n].x1, r[n].y2 - r[n].y1);
416
417 ret = vc_dispmanx_resource_write_data_rect(resource->handle,
418 ifmt, stride,
419 pixels, &rect,
420 rect.x, rect.y);
421 DBG("%s: %p %ux%u@%u,%u, ret %d\n", __func__, resource,
422 rect.width, rect.height, rect.x, rect.y, ret);
423 if (ret)
424 break;
425 }
426#else
427 /* vc_dispmanx_resource_write_data() ignores ifmt,
428 * rect.x, rect.width, and uses stride only for computing
429 * the size of the transfer as rect.height * stride.
430 * Therefore we can only write rows starting at x=0.
431 * To be able to write more than one scanline at a time,
432 * the resource must have been created with the same stride
433 * as used here, and we must write full scanlines.
434 */
435
436 r = pixman_region32_extents(&write_region);
437 vc_dispmanx_rect_set(&rect, 0, r->y1, width, r->y2 - r->y1);
438 ret = vc_dispmanx_resource_write_data(resource->handle,
439 ifmt, stride, pixels, &rect);
440 DBG("%s: %p %ux%u@%u,%u, ret %d\n", __func__, resource,
441 width, r->y2 - r->y1, 0, r->y1, ret);
442#endif
443
Neil Robertse5051712013-11-13 15:44:06 +0000444 wl_shm_buffer_end_access(buffer->shm_buffer);
445
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300446 pixman_region32_fini(&write_region);
447
U. Artie Eoff5e854bc2014-01-17 13:56:41 -0800448 if (applied_opaque_region)
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100449 free(pixels);
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100450
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300451 return ret ? -1 : 0;
452}
453
Pekka Paalanend5fbfb22013-11-22 17:30:39 +0200454static inline void
455rpi_buffer_egl_lock(struct weston_buffer *buffer)
456{
457#ifdef ENABLE_EGL
458 vc_dispmanx_set_wl_buffer_in_use(buffer->resource, 1);
459#endif
460}
461
462static inline void
463rpi_buffer_egl_unlock(struct weston_buffer *buffer)
464{
465#ifdef ENABLE_EGL
466 vc_dispmanx_set_wl_buffer_in_use(buffer->resource, 0);
467#endif
468}
469
Tomeu Vizoso74987582013-10-25 10:34:38 +0200470static void
471rpir_egl_buffer_destroy(struct rpir_egl_buffer *egl_buffer)
472{
473 struct weston_buffer *buffer;
474
475 if (egl_buffer == NULL)
476 return;
477
478 buffer = egl_buffer->buffer_ref.buffer;
479 if (buffer == NULL) {
480 /* The client has already destroyed the wl_buffer, the
481 * compositor has the responsibility to delete the resource.
482 */
483 vc_dispmanx_resource_delete(egl_buffer->resource_handle);
484 } else {
Pekka Paalanend5fbfb22013-11-22 17:30:39 +0200485 rpi_buffer_egl_unlock(buffer);
Tomeu Vizoso74987582013-10-25 10:34:38 +0200486 weston_buffer_reference(&egl_buffer->buffer_ref, NULL);
487 }
488
489 free(egl_buffer);
490}
491
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300492static struct rpir_surface *
493rpir_surface_create(struct rpi_renderer *renderer)
494{
495 struct rpir_surface *surface;
496
Bryce Harringtonde16d892014-11-20 22:21:57 -0800497 surface = zalloc(sizeof *surface);
498 if (surface == NULL)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300499 return NULL;
500
Tomeu Vizoso5c3ea3b2013-10-24 15:38:30 +0200501 wl_list_init(&surface->views);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300502 surface->single_buffer = renderer->single_buffer;
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100503 surface->enable_opaque_regions = renderer->enable_opaque_regions;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300504 rpi_resource_init(&surface->resources[0]);
505 rpi_resource_init(&surface->resources[1]);
506 surface->front = &surface->resources[0];
507 if (surface->single_buffer)
508 surface->back = &surface->resources[0];
509 else
510 surface->back = &surface->resources[1];
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100511
512 surface->front->enable_opaque_regions = renderer->enable_opaque_regions;
513 surface->back->enable_opaque_regions = renderer->enable_opaque_regions;
514
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200515 surface->buffer_type = BUFFER_TYPE_NULL;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300516
517 pixman_region32_init(&surface->prev_damage);
518
519 return surface;
520}
521
522static void
523rpir_surface_destroy(struct rpir_surface *surface)
524{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500525 if (surface->visible_views)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300526 weston_log("ERROR rpi: destroying on-screen element\n");
527
Jason Ekstranda7af7042013-10-12 22:38:11 -0500528 assert(wl_list_empty(&surface->views));
529
530 if (surface->surface)
531 surface->surface->renderer_state = NULL;
532
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300533 pixman_region32_fini(&surface->prev_damage);
534 rpi_resource_release(&surface->resources[0]);
535 rpi_resource_release(&surface->resources[1]);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500536 DBG("rpir_surface %p destroyed (%u)\n", surface, surface->visible_views);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300537
Tomeu Vizoso74987582013-10-25 10:34:38 +0200538 rpir_egl_buffer_destroy(surface->egl_back);
539 rpir_egl_buffer_destroy(surface->egl_front);
540 rpir_egl_buffer_destroy(surface->egl_old_front);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200541
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300542 free(surface);
543}
544
545static int
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500546rpir_surface_damage(struct rpir_surface *surface, struct weston_buffer *buffer,
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300547 pixman_region32_t *damage)
548{
549 pixman_region32_t upload;
550 int ret;
551
552 if (!pixman_region32_not_empty(damage))
553 return 0;
554
555 DBG("rpir_surface %p update resource %p\n", surface, surface->back);
556
557 /* XXX: todo: if no surface->handle, update front buffer directly
558 * to avoid creating a new back buffer */
559 if (surface->single_buffer) {
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100560 ret = rpi_resource_update(surface->front, buffer, damage,
561 &surface->surface->opaque);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300562 } else {
563 pixman_region32_init(&upload);
564 pixman_region32_union(&upload, &surface->prev_damage, damage);
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100565 ret = rpi_resource_update(surface->back, buffer, &upload,
566 &surface->surface->opaque);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300567 pixman_region32_fini(&upload);
568 }
569
570 pixman_region32_copy(&surface->prev_damage, damage);
571 surface->need_swap = 1;
572
573 return ret;
574}
575
Jason Ekstranda7af7042013-10-12 22:38:11 -0500576static struct rpir_view *
577rpir_view_create(struct rpir_surface *surface)
578{
579 struct rpir_view *view;
580
Bryce Harringtonde16d892014-11-20 22:21:57 -0800581 view = zalloc(sizeof *view);
582 if (view == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500583 return NULL;
584
585 view->surface = surface;
586 wl_list_insert(&surface->views, &view->surface_link);
587
588 wl_list_init(&view->link);
589 view->handle = DISPMANX_NO_HANDLE;
590
591 return view;
592}
593
594static void
595rpir_view_destroy(struct rpir_view *view)
596{
597 wl_list_remove(&view->link);
598
599 if (view->handle != DISPMANX_NO_HANDLE) {
600 view->surface->visible_views--;
601 weston_log("ERROR rpi: destroying on-screen element\n");
602 }
603
604 if (view->view)
605 view->view->renderer_state = NULL;
606
607 wl_list_remove(&view->surface_link);
608 if (wl_list_empty(&view->surface->views) && view->surface->surface == NULL)
609 rpir_surface_destroy(view->surface);
610
611 DBG("rpir_view %p destroyed (%d)\n", view, view->handle);
612
613 free(view);
614}
615
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300616static void
617matrix_type_str(struct weston_matrix *matrix, char *buf, int len)
618{
619 static const char types[33] = "TSRO";
620 unsigned mask = matrix->type;
621 int i = 0;
622
623 while (mask && i < len - 1) {
624 if (mask & (1u << i))
625 *buf++ = types[i];
626 mask &= ~(1u << i);
627 i++;
628 }
629 *buf = '\0';
630}
631
632static void
633log_print_matrix(struct weston_matrix *matrix)
634{
635 char typestr[6];
636 float *d = matrix->d;
637
638 matrix_type_str(matrix, typestr, sizeof typestr);
639 weston_log_continue("%14.6e %14.6e %14.6e %14.6e\n",
640 d[0], d[4], d[8], d[12]);
641 weston_log_continue("%14.6e %14.6e %14.6e %14.6e\n",
642 d[1], d[5], d[9], d[13]);
643 weston_log_continue("%14.6e %14.6e %14.6e %14.6e\n",
644 d[2], d[6], d[10], d[14]);
645 weston_log_continue("%14.6e %14.6e %14.6e %14.6e type: %s\n",
646 d[3], d[7], d[11], d[15], typestr);
647}
648
649static void
650warn_bad_matrix(struct weston_matrix *total, struct weston_matrix *output,
651 struct weston_matrix *surface)
652{
653 static int n_warn;
654 char typestr[6];
655
656 if (n_warn++ == 10)
657 weston_log("%s: not showing more warnings\n", __func__);
658
659 if (n_warn > 10)
660 return;
661
662 weston_log("%s: warning: total transformation is not renderable:\n",
663 __func__);
664 log_print_matrix(total);
665
666 matrix_type_str(surface, typestr, sizeof typestr);
667 weston_log_continue("surface matrix type: %s\n", typestr);
668 matrix_type_str(output, typestr, sizeof typestr);
669 weston_log_continue("output matrix type: %s\n", typestr);
670}
671
672/*#define SURFACE_TRANSFORM */
673
674static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500675rpir_view_compute_rects(struct rpir_view *view,
676 VC_RECT_T *src_rect, VC_RECT_T *dst_rect,
677 VC_IMAGE_TRANSFORM_T *flipmask)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300678{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500679 struct weston_output *output_base = view->view->surface->output;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300680 struct rpir_output *output = to_rpir_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500681 struct weston_matrix matrix = view->view->transform.matrix;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300682 VC_IMAGE_TRANSFORM_T flipt = 0;
683 int src_x, src_y;
684 int dst_x, dst_y;
685 int src_width, src_height;
686 int dst_width, dst_height;
687 struct weston_vector p1 = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
688 struct weston_vector p2 = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
689 int t;
690 int over;
691
692 /* XXX: take buffer transform into account */
693
694 /* src is in 16.16, dst is in 32.0 fixed point.
695 * Negative values are not allowed in VC_RECT_T.
696 * Clip size to output boundaries, firmware ignores
697 * huge elements like 8192x8192.
698 */
699
700 src_x = 0 << 16;
701 src_y = 0 << 16;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200702
Jason Ekstranda7af7042013-10-12 22:38:11 -0500703 if (view->surface->buffer_type == BUFFER_TYPE_EGL) {
704 struct weston_buffer *buffer =
705 view->surface->egl_front->buffer_ref.buffer;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200706
707 src_width = buffer->width << 16;
708 src_height = buffer->height << 16;
709 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500710 src_width = view->surface->front->width << 16;
711 src_height = view->surface->front->height << 16;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200712 }
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300713
714 weston_matrix_multiply(&matrix, &output->matrix);
715
716#ifdef SURFACE_TRANSFORM
717 if (matrix.type >= WESTON_MATRIX_TRANSFORM_OTHER) {
718#else
719 if (matrix.type >= WESTON_MATRIX_TRANSFORM_ROTATE) {
720#endif
721 warn_bad_matrix(&matrix, &output->matrix,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500722 &view->view->transform.matrix);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300723 } else {
724 if (matrix.type & WESTON_MATRIX_TRANSFORM_ROTATE) {
725 if (fabsf(matrix.d[0]) < 1e-4f &&
726 fabsf(matrix.d[5]) < 1e-4f) {
727 flipt |= TRANSFORM_TRANSPOSE;
728 } else if (fabsf(matrix.d[1]) < 1e-4 &&
729 fabsf(matrix.d[4]) < 1e-4) {
730 /* no transpose */
731 } else {
732 warn_bad_matrix(&matrix, &output->matrix,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500733 &view->view->transform.matrix);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300734 }
735 }
736 }
737
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600738 p2.f[0] = view->view->surface->width;
739 p2.f[1] = view->view->surface->height;
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300740
741 /* transform top-left and bot-right corner into screen coordinates */
742 weston_matrix_transform(&matrix, &p1);
743 weston_matrix_transform(&matrix, &p2);
744
745 /* Compute the destination rectangle on screen, converting
746 * negative dimensions to flips.
747 */
748
749 dst_width = round(p2.f[0] - p1.f[0]);
750 if (dst_width < 0) {
751 dst_x = round(p2.f[0]);
752 dst_width = -dst_width;
753
754 if (!(flipt & TRANSFORM_TRANSPOSE))
755 flipt |= TRANSFORM_HFLIP;
756 else
757 flipt |= TRANSFORM_VFLIP;
758 } else {
759 dst_x = round(p1.f[0]);
760 }
761
762 dst_height = round(p2.f[1] - p1.f[1]);
763 if (dst_height < 0) {
764 dst_y = round(p2.f[1]);
765 dst_height = -dst_height;
766
767 if (!(flipt & TRANSFORM_TRANSPOSE))
768 flipt |= TRANSFORM_VFLIP;
769 else
770 flipt |= TRANSFORM_HFLIP;
771 } else {
772 dst_y = round(p1.f[1]);
773 }
774
775 if (dst_width == 0 || dst_height == 0) {
776 DBG("ignored, zero surface area before clipping\n");
777 return -1;
778 }
779
780#ifdef SURFACE_TRANSFORM
781 /* Dispmanx works as if you flipped the whole screen, when
782 * you flip an element. But, we want to flip an element in place.
783 * XXX: fixme
784 */
785 if (flipt & TRANSFORM_HFLIP)
786 dst_x = output_base->width - dst_x;
787 if (flipt & TRANSFORM_VFLIP)
788 dst_y = output_base->height - dst_y;
789 if (flipt & TRANSFORM_TRANSPOSE) {
790 int_swap(&dst_x, &dst_y);
791 int_swap(&dst_width, &dst_height);
792 }
793#else
794 switch (output_base->transform) {
795 case WL_OUTPUT_TRANSFORM_FLIPPED:
796 flipt = TRANSFORM_HFLIP;
797 break;
798 case WL_OUTPUT_TRANSFORM_NORMAL:
799 flipt = 0;
800 break;
801
802 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
803 flipt = TRANSFORM_HFLIP | TRANSFORM_VFLIP | TRANSFORM_TRANSPOSE;
804 break;
805 case WL_OUTPUT_TRANSFORM_90:
806 flipt = TRANSFORM_VFLIP | TRANSFORM_TRANSPOSE;
807 break;
808 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
809 flipt = TRANSFORM_VFLIP;
810 break;
811 case WL_OUTPUT_TRANSFORM_180:
812 flipt = TRANSFORM_HFLIP | TRANSFORM_VFLIP;
813 break;
814
815 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
816 flipt = TRANSFORM_TRANSPOSE;
817 break;
818 case WL_OUTPUT_TRANSFORM_270:
819 flipt = TRANSFORM_HFLIP | TRANSFORM_TRANSPOSE;
820 break;
821 default:
822 break;
823 }
824#endif
825
826 /* clip destination rectangle to screen dimensions */
827
828 if (dst_x < 0) {
829 t = (int64_t)dst_x * src_width / dst_width;
830 src_width += t;
831 dst_width += dst_x;
832 src_x -= t;
833 dst_x = 0;
834 }
835
836 if (dst_y < 0) {
837 t = (int64_t)dst_y * src_height / dst_height;
838 src_height += t;
839 dst_height += dst_y;
840 src_y -= t;
841 dst_y = 0;
842 }
843
844 over = dst_x + dst_width - output_base->width;
845 if (over > 0) {
846 t = (int64_t)over * src_width / dst_width;
847 src_width -= t;
848 dst_width -= over;
849 }
850
851 over = dst_y + dst_height - output_base->height;
852 if (over > 0) {
853 t = (int64_t)over * src_height / dst_height;
854 src_height -= t;
855 dst_height -= over;
856 }
857
858 src_width = int_max(src_width, 0);
859 src_height = int_max(src_height, 0);
860
Jason Ekstranda7af7042013-10-12 22:38:11 -0500861 DBG("rpir_view %p %dx%d: p1 %f, %f; p2 %f, %f\n", view,
Pekka Paalanen2d0f8b72014-05-09 15:08:06 +0300862 view->view->surface->width,
863 view->view->surface->height,
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300864 p1.f[0], p1.f[1], p2.f[0], p2.f[1]);
865 DBG("src rect %d;%d, %d;%d, %d;%dx%d;%d\n",
866 src_x >> 16, src_x & 0xffff,
867 src_y >> 16, src_y & 0xffff,
868 src_width >> 16, src_width & 0xffff,
869 src_height >> 16, src_height & 0xffff);
870 DBG("dest rect %d, %d, %dx%d%s%s%s\n",
871 dst_x, dst_y, dst_width, dst_height,
872 (flipt & TRANSFORM_HFLIP) ? " hflip" : "",
873 (flipt & TRANSFORM_VFLIP) ? " vflip" : "",
874 (flipt & TRANSFORM_TRANSPOSE) ? " transp" : "");
875
876 assert(src_x >= 0);
877 assert(src_y >= 0);
878 assert(dst_x >= 0);
879 assert(dst_y >= 0);
880
881 if (dst_width < 1 || dst_height < 1) {
882 DBG("ignored, zero surface area after clipping\n");
883 return -1;
884 }
885
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200886 /* EGL buffers will be upside-down related to what DispmanX expects */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500887 if (view->surface->buffer_type == BUFFER_TYPE_EGL)
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200888 flipt ^= TRANSFORM_VFLIP;
889
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300890 vc_dispmanx_rect_set(src_rect, src_x, src_y, src_width, src_height);
891 vc_dispmanx_rect_set(dst_rect, dst_x, dst_y, dst_width, dst_height);
892 *flipmask = flipt;
893
894 return 0;
895}
896
897static DISPMANX_TRANSFORM_T
898vc_image2dispmanx_transform(VC_IMAGE_TRANSFORM_T t)
899{
900 /* XXX: uhh, are these right? */
901 switch (t) {
902 case VC_IMAGE_ROT0:
903 return DISPMANX_NO_ROTATE;
904 case VC_IMAGE_MIRROR_ROT0:
905 return DISPMANX_FLIP_HRIZ;
906 case VC_IMAGE_MIRROR_ROT180:
907 return DISPMANX_FLIP_VERT;
908 case VC_IMAGE_ROT180:
909 return DISPMANX_ROTATE_180;
910 case VC_IMAGE_MIRROR_ROT90:
911 return DISPMANX_ROTATE_90 | DISPMANX_FLIP_HRIZ;
912 case VC_IMAGE_ROT270:
913 return DISPMANX_ROTATE_270;
914 case VC_IMAGE_ROT90:
915 return DISPMANX_ROTATE_90;
916 case VC_IMAGE_MIRROR_ROT270:
917 return DISPMANX_ROTATE_270 | DISPMANX_FLIP_VERT;
918 default:
919 assert(0 && "bad VC_IMAGE_TRANSFORM_T");
920 return DISPMANX_NO_ROTATE;
921 }
922}
923
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200924static DISPMANX_RESOURCE_HANDLE_T
925rpir_surface_get_resource(struct rpir_surface *surface)
926{
927 switch (surface->buffer_type) {
928 case BUFFER_TYPE_SHM:
929 case BUFFER_TYPE_NULL:
930 return surface->front->handle;
931 case BUFFER_TYPE_EGL:
932 if (surface->egl_front != NULL)
933 return surface->egl_front->resource_handle;
934 default:
935 return DISPMANX_NO_HANDLE;
936 }
937}
938
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100939#ifdef HAVE_ELEMENT_SET_OPAQUE_RECT
940static int
941rpir_surface_set_opaque_rect(struct rpir_surface *surface,
942 DISPMANX_UPDATE_HANDLE_T update)
943{
944 int ret;
945
946 if (pixman_region32_not_empty(&surface->surface->opaque) &&
947 surface->opaque_regions) {
948 pixman_box32_t *box;
949 VC_RECT_T opaque_rect;
950
951 box = pixman_region32_extents(&surface->surface->opaque);
952 opaque_rect.x = box->x1;
953 opaque_rect.y = box->y1;
954 opaque_rect.width = box->x2 - box->x1;
955 opaque_rect.height = box->y2 - box->y1;
956
957 ret = vc_dispmanx_element_set_opaque_rect(update,
958 surface->handle,
959 &opaque_rect);
960 if (ret) {
961 weston_log("vc_dispmanx_element_set_opaque_rect failed\n");
962 return -1;
963 }
964 }
965
966 return 0;
967}
968#endif
969
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300970static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500971rpir_view_dmx_add(struct rpir_view *view, struct rpir_output *output,
972 DISPMANX_UPDATE_HANDLE_T update, int layer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300973{
974 /* Do not use DISPMANX_FLAGS_ALPHA_PREMULT here.
975 * If you define PREMULT and ALPHA_MIX, the hardware will not
976 * multiply the source color with the element alpha, leading to
977 * bad colors. Instead, we define PREMULT during pixel data upload.
978 */
979 VC_DISPMANX_ALPHA_T alphasetup = {
980 DISPMANX_FLAGS_ALPHA_FROM_SOURCE |
981 DISPMANX_FLAGS_ALPHA_MIX,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982 float2uint8(view->view->alpha), /* opacity 0-255 */
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300983 0 /* mask resource handle */
984 };
985 VC_RECT_T dst_rect;
986 VC_RECT_T src_rect;
987 VC_IMAGE_TRANSFORM_T flipmask;
988 int ret;
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200989 DISPMANX_RESOURCE_HANDLE_T resource_handle;
990
Jason Ekstranda7af7042013-10-12 22:38:11 -0500991 resource_handle = rpir_surface_get_resource(view->surface);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +0200992 if (resource_handle == DISPMANX_NO_HANDLE) {
993 weston_log("%s: no buffer yet, aborting\n", __func__);
994 return 0;
995 }
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300996
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 ret = rpir_view_compute_rects(view, &src_rect, &dst_rect, &flipmask);
Pekka Paalanend7265bc2013-05-22 18:03:06 +0300998 if (ret < 0)
999 return 0;
1000
Jason Ekstranda7af7042013-10-12 22:38:11 -05001001 view->handle = vc_dispmanx_element_add(
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001002 update,
1003 output->display,
1004 layer,
1005 &dst_rect,
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001006 resource_handle,
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001007 &src_rect,
1008 DISPMANX_PROTECTION_NONE,
1009 &alphasetup,
1010 NULL /* clamp */,
1011 vc_image2dispmanx_transform(flipmask));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012 DBG("rpir_surface %p add %u, alpha %f resource %d\n", view,
1013 view->handle, view->view->alpha, resource_handle);
1014
1015 if (view->handle == DISPMANX_NO_HANDLE)
1016 return -1;
1017
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01001018#ifdef HAVE_ELEMENT_SET_OPAQUE_RECT
1019 ret = rpir_surface_set_opaque_rect(surface, update);
1020 if (ret < 0)
1021 return -1;
1022#endif
1023
Jason Ekstranda7af7042013-10-12 22:38:11 -05001024 view->surface->visible_views++;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001025
1026 return 1;
1027}
1028
1029static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001030rpir_view_dmx_swap(struct rpir_view *view,
1031 DISPMANX_UPDATE_HANDLE_T update)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001032{
1033 VC_RECT_T rect;
1034 pixman_box32_t *r;
1035
1036 /* XXX: skip, iff resource was not reallocated, and single-buffering */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037 vc_dispmanx_element_change_source(update, view->handle,
1038 view->surface->front->handle);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001039
1040 /* This is current damage now, after rpir_surface_damage() */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001041 r = pixman_region32_extents(&view->surface->prev_damage);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001042
1043 vc_dispmanx_rect_set(&rect, r->x1, r->y1,
1044 r->x2 - r->x1, r->y2 - r->y1);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001045 vc_dispmanx_element_modified(update, view->handle, &rect);
1046 DBG("rpir_view %p swap\n", view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001047}
1048
1049static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001050rpir_view_dmx_move(struct rpir_view *view,
1051 DISPMANX_UPDATE_HANDLE_T update, int layer)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001052{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001053 uint8_t alpha = float2uint8(view->view->alpha);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001054 VC_RECT_T dst_rect;
1055 VC_RECT_T src_rect;
1056 VC_IMAGE_TRANSFORM_T flipmask;
1057 int ret;
1058
1059 /* XXX: return early, if all attributes stay the same */
1060
Jason Ekstranda7af7042013-10-12 22:38:11 -05001061 if (view->surface->buffer_type == BUFFER_TYPE_EGL) {
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001062 DISPMANX_RESOURCE_HANDLE_T resource_handle;
1063
Jason Ekstranda7af7042013-10-12 22:38:11 -05001064 resource_handle = rpir_surface_get_resource(view->surface);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001065 if (resource_handle == DISPMANX_NO_HANDLE) {
1066 weston_log("%s: no buffer yet, aborting\n", __func__);
1067 return 0;
1068 }
1069
1070 vc_dispmanx_element_change_source(update,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001071 view->handle,
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001072 resource_handle);
1073 }
1074
Jason Ekstranda7af7042013-10-12 22:38:11 -05001075 ret = rpir_view_compute_rects(view, &src_rect, &dst_rect, &flipmask);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001076 if (ret < 0)
1077 return 0;
1078
1079 ret = vc_dispmanx_element_change_attributes(
1080 update,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001081 view->handle,
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001082 ELEMENT_CHANGE_LAYER |
1083 ELEMENT_CHANGE_OPACITY |
1084 ELEMENT_CHANGE_TRANSFORM |
1085 ELEMENT_CHANGE_DEST_RECT |
1086 ELEMENT_CHANGE_SRC_RECT,
1087 layer,
1088 alpha,
1089 &dst_rect,
1090 &src_rect,
1091 DISPMANX_NO_HANDLE,
1092 /* This really is DISPMANX_TRANSFORM_T, no matter
1093 * what the header says. */
1094 vc_image2dispmanx_transform(flipmask));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001095 DBG("rpir_view %p move\n", view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001096
1097 if (ret)
1098 return -1;
1099
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01001100#ifdef HAVE_ELEMENT_SET_OPAQUE_RECT
1101 ret = rpir_surface_set_opaque_rect(surface, update);
1102 if (ret < 0)
1103 return -1;
1104#endif
1105
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001106 return 1;
1107}
1108
1109static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001110rpir_view_dmx_remove(struct rpir_view *view,
1111 DISPMANX_UPDATE_HANDLE_T update)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001112{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001113 if (view->handle == DISPMANX_NO_HANDLE)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001114 return;
1115
Jason Ekstranda7af7042013-10-12 22:38:11 -05001116 vc_dispmanx_element_remove(update, view->handle);
1117 DBG("rpir_view %p remove %u\n", view, view->handle);
1118 view->handle = DISPMANX_NO_HANDLE;
1119 view->surface->visible_views--;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001120}
1121
1122static void
1123rpir_surface_swap_pointers(struct rpir_surface *surface)
1124{
1125 struct rpi_resource *tmp;
1126
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001127 if (surface->buffer_type == BUFFER_TYPE_EGL) {
1128 if (surface->egl_back != NULL) {
1129 assert(surface->egl_old_front == NULL);
1130 surface->egl_old_front = surface->egl_front;
1131 surface->egl_front = surface->egl_back;
1132 surface->egl_back = NULL;
Tomeu Vizoso15767812013-10-24 15:38:31 +02001133 DBG("new front %d\n", surface->egl_front->resource_handle);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001134 }
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001135 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001136 tmp = surface->front;
1137 surface->front = surface->back;
1138 surface->back = tmp;
Tomeu Vizoso15767812013-10-24 15:38:31 +02001139 DBG("new back %p, new front %p\n", surface->back, surface->front);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001140 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141}
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001142
Jason Ekstranda7af7042013-10-12 22:38:11 -05001143static int
1144is_view_not_visible(struct weston_view *view)
1145{
1146 /* Return true, if surface is guaranteed to be totally obscured. */
1147 int ret;
1148 pixman_region32_t unocc;
1149
1150 pixman_region32_init(&unocc);
1151 pixman_region32_subtract(&unocc, &view->transform.boundingbox,
1152 &view->clip);
1153 ret = !pixman_region32_not_empty(&unocc);
1154 pixman_region32_fini(&unocc);
1155
1156 return ret;
1157}
1158
1159static void
1160rpir_view_update(struct rpir_view *view, struct rpir_output *output,
1161 DISPMANX_UPDATE_HANDLE_T update, int layer)
1162{
1163 int ret;
1164 int obscured;
1165
Jason Ekstranda7af7042013-10-12 22:38:11 -05001166 obscured = is_view_not_visible(view->view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001167 if (obscured) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001168 DBG("rpir_view %p totally obscured.\n", view);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001169
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 wl_list_remove(&view->link);
1171 if (view->handle == DISPMANX_NO_HANDLE) {
1172 wl_list_init(&view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001173 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001174 rpir_view_dmx_remove(view, update);
1175 wl_list_insert(&output->view_cleanup_list,
1176 &view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001177 }
1178
1179 goto out;
1180 }
1181
Jason Ekstranda7af7042013-10-12 22:38:11 -05001182 if (view->handle == DISPMANX_NO_HANDLE) {
1183 ret = rpir_view_dmx_add(view, output, update, layer);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001184 if (ret == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001185 wl_list_remove(&view->link);
1186 wl_list_init(&view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001187 } else if (ret < 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001188 weston_log("ERROR rpir_view_dmx_add() failed.\n");
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001189 }
1190 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191 if (view->surface->need_swap)
1192 rpir_view_dmx_swap(view, update);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001193
Jason Ekstranda7af7042013-10-12 22:38:11 -05001194 ret = rpir_view_dmx_move(view, update, layer);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001195 if (ret == 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 rpir_view_dmx_remove(view, update);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001197
Jason Ekstranda7af7042013-10-12 22:38:11 -05001198 wl_list_remove(&view->link);
1199 wl_list_insert(&output->view_cleanup_list,
1200 &view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001201 } else if (ret < 0) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001202 weston_log("ERROR rpir_view_dmx_move() failed.\n");
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001203 }
1204 }
1205
1206out:
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 view->layer = layer;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001208}
1209
1210static int
1211rpi_renderer_read_pixels(struct weston_output *base,
1212 pixman_format_code_t format, void *pixels,
1213 uint32_t x, uint32_t y,
1214 uint32_t width, uint32_t height)
1215{
1216 struct rpir_output *output = to_rpir_output(base);
1217 struct rpi_resource *buffer = &output->capture_buffer;
1218 VC_RECT_T rect;
1219 uint32_t fb_width, fb_height;
1220 uint32_t dst_pitch;
1221 uint32_t i;
1222 int ret;
1223
Hardeningff39efa2013-09-18 23:56:35 +02001224 fb_width = base->current_mode->width;
1225 fb_height = base->current_mode->height;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001226
1227 DBG("%s(%u, %u, %u, %u), resource %p\n", __func__,
1228 x, y, width, height, buffer);
1229
1230 if (format != PIXMAN_a8r8g8b8) {
1231 weston_log("rpi-renderer error: bad read_format\n");
1232 return -1;
1233 }
1234
1235 dst_pitch = fb_width * 4;
1236
1237 if (buffer->handle == DISPMANX_NO_HANDLE) {
1238 free(output->capture_data);
1239 output->capture_data = NULL;
1240
1241 ret = rpi_resource_realloc(buffer, VC_IMAGE_ARGB8888,
1242 fb_width, fb_height,
1243 dst_pitch, fb_height);
1244 if (ret < 0) {
1245 weston_log("rpi-renderer error: "
1246 "allocating read buffer failed\n");
1247 return -1;
1248 }
1249
1250 ret = vc_dispmanx_snapshot(output->display, buffer->handle,
1251 VC_IMAGE_ROT0);
1252 if (ret) {
1253 weston_log("rpi-renderer error: "
1254 "vc_dispmanx_snapshot returned %d\n", ret);
1255 return -1;
1256 }
1257 DBG("%s: snapshot done.\n", __func__);
1258 }
1259
1260 /*
1261 * If vc_dispmanx_resource_read_data was able to read sub-rectangles,
1262 * we could read directly into 'pixels'. But it cannot, it does not
1263 * use rect.x or rect.width, and does this:
1264 * host_start = (uint8_t *)dst_address + (dst_pitch * p_rect->y);
1265 * In other words, it is only good for reading the full buffer in
1266 * one go.
1267 */
1268 vc_dispmanx_rect_set(&rect, 0, 0, fb_width, fb_height);
1269
1270 if (x == 0 && y == 0 && width == fb_width && height == fb_height) {
1271 ret = vc_dispmanx_resource_read_data(buffer->handle, &rect,
1272 pixels, dst_pitch);
1273 if (ret) {
1274 weston_log("rpi-renderer error: "
1275 "resource_read_data returned %d\n", ret);
1276 return -1;
1277 }
1278 DBG("%s: full frame done.\n", __func__);
1279 return 0;
1280 }
1281
1282 if (!output->capture_data) {
1283 output->capture_data = malloc(fb_height * dst_pitch);
1284 if (!output->capture_data) {
1285 weston_log("rpi-renderer error: "
1286 "out of memory\n");
1287 return -1;
1288 }
1289
1290 ret = vc_dispmanx_resource_read_data(buffer->handle, &rect,
1291 output->capture_data,
1292 dst_pitch);
1293 if (ret) {
1294 weston_log("rpi-renderer error: "
1295 "resource_read_data returned %d\n", ret);
1296 return -1;
1297 }
1298 }
1299
1300 for (i = 0; i < height; i++) {
1301 uint8_t *src = output->capture_data +
1302 (y + i) * dst_pitch + x * 4;
1303 uint8_t *dst = (uint8_t *)pixels + i * width * 4;
1304 memcpy(dst, src, width * 4);
1305 }
1306
1307 return 0;
1308}
1309
1310static void
1311rpir_output_dmx_remove_all(struct rpir_output *output,
1312 DISPMANX_UPDATE_HANDLE_T update)
1313{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 struct rpir_view *view;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001315
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 while (!wl_list_empty(&output->view_list)) {
1317 view = container_of(output->view_list.next,
1318 struct rpir_view, link);
1319 rpir_view_dmx_remove(view, update);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001320
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 wl_list_remove(&view->link);
1322 wl_list_insert(&output->view_cleanup_list, &view->link);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001323 }
1324}
1325
1326static void
1327output_compute_matrix(struct weston_output *base)
1328{
1329 struct rpir_output *output = to_rpir_output(base);
1330 struct weston_matrix *matrix = &output->matrix;
Pekka Paalanenc143df12015-03-10 12:52:14 +02001331#ifdef SURFACE_TRANSFORM
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001332 const float half_w = 0.5f * base->width;
1333 const float half_h = 0.5f * base->height;
Pekka Paalanenc143df12015-03-10 12:52:14 +02001334#endif
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001335 float mag;
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001336
1337 weston_matrix_init(matrix);
1338 weston_matrix_translate(matrix, -base->x, -base->y, 0.0f);
1339
1340#ifdef SURFACE_TRANSFORM
1341 weston_matrix_translate(matrix, -half_w, -half_h, 0.0f);
1342 switch (base->transform) {
1343 case WL_OUTPUT_TRANSFORM_FLIPPED:
1344 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1345 case WL_OUTPUT_TRANSFORM_NORMAL:
1346 /* weston_matrix_rotate_xy(matrix, 1.0f, 0.0f); no-op */
1347 weston_matrix_translate(matrix, half_w, half_h, 0.0f);
1348 break;
1349
1350 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1351 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1352 case WL_OUTPUT_TRANSFORM_90:
1353 weston_matrix_rotate_xy(matrix, 0.0f, 1.0f);
1354 weston_matrix_translate(matrix, half_h, half_w, 0.0f);
1355 break;
1356
1357 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1358 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1359 case WL_OUTPUT_TRANSFORM_180:
1360 weston_matrix_rotate_xy(matrix, -1.0f, 0.0f);
1361 weston_matrix_translate(matrix, half_w, half_h, 0.0f);
1362 break;
1363
1364 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1365 weston_matrix_scale(matrix, -1.0f, 1.0f, 1.0f);
1366 case WL_OUTPUT_TRANSFORM_270:
1367 weston_matrix_rotate_xy(matrix, 0.0f, -1.0f);
1368 weston_matrix_translate(matrix, half_h, half_w, 0.0f);
1369 break;
1370
1371 default:
1372 break;
1373 }
1374#endif
1375
1376 if (base->zoom.active) {
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001377 mag = 1.0f / (1.0f - base->zoom.spring_z.current);
Pekka Paalanenc143df12015-03-10 12:52:14 +02001378 weston_matrix_translate(matrix, base->zoom.trans_x,
1379 base->zoom.trans_y, 0.0f);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001380 weston_matrix_scale(matrix, mag, mag, 1.0f);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001381 }
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)
Bryce Harringtonde16d892014-11-20 22:21:57 -08001552 surface->egl_back = zalloc(sizeof *surface->egl_back);
Tomeu Vizosob4659eb2013-10-07 11:02:20 +02001553
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
Bryce Harringtonde16d892014-11-20 22:21:57 -08001728 renderer = zalloc(sizeof *renderer);
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001729 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
Bryce Harringtonde16d892014-11-20 22:21:57 -08001800 output = zalloc(sizeof *output);
1801 if (output == NULL)
Pekka Paalanend7265bc2013-05-22 18:03:06 +03001802 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}