2 * Copyright © 2012 Collabora, Ltd.
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.
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.
29 #include <sys/types.h>
36 #include <GLES2/gl2.h>
38 #include "compositor.h"
39 #include "android-framebuffer.h"
42 struct android_compositor;
44 struct android_output {
45 struct android_compositor *compositor;
46 struct weston_output base;
48 struct weston_mode mode;
49 struct android_framebuffer *fb;
53 struct weston_seat base;
54 struct wl_list devices_list;
57 struct android_compositor {
58 struct weston_compositor base;
60 struct android_seat *seat;
63 static inline struct android_output *
64 to_android_output(struct weston_output *base)
66 return container_of(base, struct android_output, base);
69 static inline struct android_seat *
70 to_android_seat(struct weston_seat *base)
72 return container_of(base, struct android_seat, base);
75 static inline struct android_compositor *
76 to_android_compositor(struct weston_compositor *base)
78 return container_of(base, struct android_compositor, base);
82 egl_error_string(EGLint code)
84 #define MYERRCODE(x) case x: return #x;
86 MYERRCODE(EGL_SUCCESS)
87 MYERRCODE(EGL_NOT_INITIALIZED)
88 MYERRCODE(EGL_BAD_ACCESS)
89 MYERRCODE(EGL_BAD_ALLOC)
90 MYERRCODE(EGL_BAD_ATTRIBUTE)
91 MYERRCODE(EGL_BAD_CONTEXT)
92 MYERRCODE(EGL_BAD_CONFIG)
93 MYERRCODE(EGL_BAD_CURRENT_SURFACE)
94 MYERRCODE(EGL_BAD_DISPLAY)
95 MYERRCODE(EGL_BAD_SURFACE)
96 MYERRCODE(EGL_BAD_MATCH)
97 MYERRCODE(EGL_BAD_PARAMETER)
98 MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
99 MYERRCODE(EGL_BAD_NATIVE_WINDOW)
100 MYERRCODE(EGL_CONTEXT_LOST)
108 print_egl_error_state(void)
112 code = eglGetError();
113 weston_log("EGL error state: %s (0x%04lx)\n",
114 egl_error_string(code), (long)code);
118 android_finish_frame(void *data)
120 struct android_output *output = data;
122 weston_output_finish_frame(&output->base,
123 weston_compositor_get_time());
127 android_output_repaint(struct weston_output *base, pixman_region32_t *damage)
129 struct android_output *output = to_android_output(base);
130 struct android_compositor *compositor = output->compositor;
131 struct weston_plane *primary_plane = &compositor->base.primary_plane;
132 struct wl_event_loop *loop;
134 compositor->base.renderer->repaint_output(&output->base, damage);
136 pixman_region32_subtract(&primary_plane->damage,
137 &primary_plane->damage, damage);
139 /* FIXME: does Android have a way to signal page flip done? */
140 loop = wl_display_get_event_loop(compositor->base.wl_display);
141 wl_event_loop_add_idle(loop, android_finish_frame, output);
145 android_output_destroy(struct weston_output *base)
147 struct android_output *output = to_android_output(base);
149 wl_list_remove(&output->base.link);
150 weston_output_destroy(&output->base);
152 android_framebuffer_destroy(output->fb);
157 static struct android_output *
158 android_output_create(struct android_compositor *compositor)
160 struct android_output *output;
162 output = calloc(1, sizeof *output);
166 output->fb = android_framebuffer_create();
172 output->compositor = compositor;
177 android_compositor_add_output(struct android_compositor *compositor,
178 struct android_output *output)
180 float mm_width, mm_height;
182 output->base.repaint = android_output_repaint;
183 output->base.destroy = android_output_destroy;
184 output->base.assign_planes = NULL;
185 output->base.set_backlight = NULL;
186 output->base.set_dpms = NULL;
187 output->base.switch_mode = NULL;
189 /* only one static mode in list */
191 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
192 output->mode.width = output->fb->width;
193 output->mode.height = output->fb->height;
194 output->mode.refresh = ceilf(1000.0f * output->fb->refresh_rate);
195 wl_list_init(&output->base.mode_list);
196 wl_list_insert(&output->base.mode_list, &output->mode.link);
198 output->base.current = &output->mode;
199 output->base.origin = &output->mode;
200 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
201 output->base.make = "unknown";
202 output->base.model = "unknown";
204 mm_width = output->fb->width / output->fb->xdpi * 25.4f;
205 mm_height = output->fb->height / output->fb->ydpi * 25.4f;
206 weston_output_init(&output->base, &compositor->base,
207 0, 0, round(mm_width), round(mm_height),
208 WL_OUTPUT_TRANSFORM_NORMAL);
209 wl_list_insert(compositor->base.output_list.prev, &output->base.link);
213 android_led_update(struct weston_seat *seat_base, enum weston_led leds)
215 struct android_seat *seat = to_android_seat(seat_base);
216 struct evdev_device *device;
218 wl_list_for_each(device, &seat->devices_list, link)
219 evdev_led_update(device, leds);
223 android_seat_open_device(struct android_seat *seat, const char *devnode)
225 struct evdev_device *device;
228 /* XXX: check the Android excluded list */
230 fd = open(devnode, O_RDWR | O_NONBLOCK | O_CLOEXEC);
232 weston_log_continue("opening '%s' failed: %s\n", devnode,
237 device = evdev_device_create(&seat->base, devnode, fd);
243 wl_list_insert(seat->devices_list.prev, &device->link);
247 is_dot_or_dotdot(const char *str)
249 return (str[0] == '.' &&
250 (str[1] == 0 || (str[1] == '.' && str[2] == 0)));
254 android_seat_scan_devices(struct android_seat *seat, const char *dirpath)
259 char *devnode = NULL;
261 dir = opendir(dirpath);
263 weston_log("Could not open input device directory '%s': %s\n",
264 dirpath, strerror(errno));
268 while ((dent = readdir(dir)) != NULL) {
269 if (is_dot_or_dotdot(dent->d_name))
272 ret = asprintf(&devnode, "%s/%s", dirpath, dent->d_name);
276 android_seat_open_device(seat, devnode);
284 android_seat_destroy(struct android_seat *seat)
286 struct evdev_device *device, *next;
288 wl_list_for_each_safe(device, next, &seat->devices_list, link)
289 evdev_device_destroy(device);
291 if (seat->base.seat.keyboard)
292 notify_keyboard_focus_out(&seat->base);
294 weston_seat_release(&seat->base);
298 static struct android_seat *
299 android_seat_create(struct android_compositor *compositor)
301 struct android_seat *seat;
303 seat = calloc(1, sizeof *seat);
307 weston_seat_init(&seat->base, &compositor->base);
308 seat->base.led_update = android_led_update;
309 wl_list_init(&seat->devices_list);
311 android_seat_scan_devices(seat, "/dev/input");
313 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
315 if (wl_list_empty(&seat->devices_list))
316 weston_log("Warning: no input devices found.\n");
318 /* XXX: implement hotplug support */
324 android_egl_choose_config(struct android_compositor *compositor,
325 struct android_framebuffer *fb,
326 const EGLint *attribs)
335 * The logic is copied from Android frameworks/base/services/
336 * surfaceflinger/DisplayHardware/DisplayHardware.cpp
339 compositor->base.egl_config = NULL;
341 ret = eglGetConfigs(compositor->base.egl_display, NULL, 0, &count);
342 if (ret == EGL_FALSE || count < 1)
345 configs = calloc(count, sizeof *configs);
349 ret = eglChooseConfig(compositor->base.egl_display, attribs, configs,
351 if (ret == EGL_FALSE || matched < 1)
354 for (i = 0; i < matched; ++i) {
356 ret = eglGetConfigAttrib(compositor->base.egl_display,
357 configs[i], EGL_NATIVE_VISUAL_ID,
359 if (ret == EGL_FALSE)
361 if (id > 0 && fb->format == id) {
362 compositor->base.egl_config = configs[i];
369 if (!compositor->base.egl_config)
376 android_init_egl(struct android_compositor *compositor,
377 struct android_output *output)
379 EGLint eglmajor, eglminor;
382 static const EGLint config_attrs[] = {
383 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
388 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
392 compositor->base.egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
393 if (compositor->base.egl_display == EGL_NO_DISPLAY) {
394 weston_log("Failed to create EGL display.\n");
395 print_egl_error_state();
399 ret = eglInitialize(compositor->base.egl_display, &eglmajor, &eglminor);
401 weston_log("Failed to initialise EGL.\n");
402 print_egl_error_state();
406 ret = android_egl_choose_config(compositor, output->fb, config_attrs);
408 weston_log("Failed to find an EGL config.\n");
409 print_egl_error_state();
413 output->base.egl_surface =
414 eglCreateWindowSurface(compositor->base.egl_display,
415 compositor->base.egl_config,
416 output->fb->native_window,
418 if (output->base.egl_surface == EGL_NO_SURFACE) {
419 weston_log("Failed to create FB EGLSurface.\n");
420 print_egl_error_state();
428 android_fini_egl(struct android_compositor *compositor)
430 gles2_renderer_destroy(&compositor->base);
432 eglMakeCurrent(compositor->base.egl_display,
433 EGL_NO_SURFACE, EGL_NO_SURFACE,
436 eglTerminate(compositor->base.egl_display);
441 android_compositor_destroy(struct weston_compositor *base)
443 struct android_compositor *compositor = to_android_compositor(base);
445 android_seat_destroy(compositor->seat);
447 /* destroys outputs, too */
448 weston_compositor_shutdown(&compositor->base);
450 android_fini_egl(compositor);
455 static struct weston_compositor *
456 android_compositor_create(struct wl_display *display, int argc, char *argv[],
457 const char *config_file)
459 struct android_compositor *compositor;
460 struct android_output *output;
462 weston_log("initializing android backend\n");
464 compositor = calloc(1, sizeof *compositor);
465 if (compositor == NULL)
468 if (weston_compositor_init(&compositor->base, display, argc, argv,
472 compositor->base.destroy = android_compositor_destroy;
474 compositor->base.focus = 1;
476 output = android_output_create(compositor);
480 if (android_init_egl(compositor, output) < 0)
483 android_compositor_add_output(compositor, output);
485 if (gles2_renderer_init(&compositor->base) < 0)
488 compositor->seat = android_seat_create(compositor);
489 if (!compositor->seat)
492 return &compositor->base;
495 android_fini_egl(compositor);
497 android_output_destroy(&output->base);
499 weston_compositor_shutdown(&compositor->base);
505 WL_EXPORT struct weston_compositor *
506 backend_init(struct wl_display *display, int argc, char *argv[],
507 const char *config_file)
509 return android_compositor_create(display, argc, argv, config_file);