blob: 1bb892946cf7d8f680604c51187034895807683a [file] [log] [blame]
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04005 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040014 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040015 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040022 */
23
Kristian Høgsberg0b9334a2011-04-12 11:34:32 -040024#define _GNU_SOURCE
25
Jesse Barnes58ef3792012-02-23 09:45:49 -050026#include <errno.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040027#include <stdlib.h>
28#include <string.h>
29#include <fcntl.h>
30#include <unistd.h>
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -040031#include <linux/input.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040032
Benjamin Franzkec649a922011-03-02 11:56:04 +010033#include <xf86drm.h>
34#include <xf86drmMode.h>
Jesse Barnes58ef3792012-02-23 09:45:49 -050035#include <drm_fourcc.h>
Benjamin Franzkec649a922011-03-02 11:56:04 +010036
Benjamin Franzke060cf802011-04-30 09:32:11 +020037#include <gbm.h>
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +020038#include <libbacklight.h>
Benjamin Franzke060cf802011-04-30 09:32:11 +020039
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040040#include "compositor.h"
Tiago Vignattice03ec32011-12-19 01:14:03 +020041#include "evdev.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010042#include "launcher-util.h"
Martin Minarik6d118362012-06-07 18:01:59 +020043#include "log.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040044
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040045struct drm_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050046 struct weston_compositor base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040047
48 struct udev *udev;
49 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040050
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010051 struct udev_monitor *udev_monitor;
52 struct wl_event_source *udev_drm_source;
53
Benjamin Franzke2af7f102011-03-02 11:14:59 +010054 struct {
David Herrmannd7488c22012-03-11 20:05:21 +010055 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010056 int fd;
57 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +020058 struct gbm_device *gbm;
Jesse Barnes58ef3792012-02-23 09:45:49 -050059 uint32_t *crtcs;
60 int num_crtcs;
Marty Jack13d9db22011-02-09 19:01:42 -050061 uint32_t crtc_allocator;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010062 uint32_t connector_allocator;
Kristian Høgsberge4762a62011-01-14 14:59:13 -050063 struct tty *tty;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +020064
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -040065 struct gbm_surface *dummy_surface;
66 EGLSurface dummy_egl_surface;
67
Jesse Barnes58ef3792012-02-23 09:45:49 -050068 struct wl_list sprite_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -050069 int sprites_are_broken;
Jesse Barnes58ef3792012-02-23 09:45:49 -050070
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +020071 uint32_t prev_state;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040072};
73
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040074struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050075 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040076 drmModeModeInfo mode_info;
77};
78
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +030079struct drm_output;
80
81struct drm_fb {
82 struct gbm_bo *bo;
83 struct drm_output *output;
84 uint32_t fb_id;
85 int is_client_buffer;
86 struct wl_buffer *buffer;
87 struct wl_listener buffer_destroy_listener;
88};
89
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040090struct drm_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050091 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040092
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040093 uint32_t crtc_id;
94 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -070095 drmModeCrtcPtr original_crtc;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +020096
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -040097 struct gbm_surface *surface;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -040098 struct gbm_bo *cursor_bo[2];
99 int current_cursor;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400100 EGLSurface egl_surface;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300101 struct drm_fb *current, *next;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200102 struct backlight *backlight;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400103};
104
Jesse Barnes58ef3792012-02-23 09:45:49 -0500105/*
106 * An output has a primary display plane plus zero or more sprites for
107 * blending display contents.
108 */
109struct drm_sprite {
110 struct wl_list link;
111
112 uint32_t fb_id;
113 uint32_t pending_fb_id;
114 struct weston_surface *surface;
115 struct weston_surface *pending_surface;
116
117 struct drm_compositor *compositor;
118
119 struct wl_listener destroy_listener;
120 struct wl_listener pending_destroy_listener;
121
122 uint32_t possible_crtcs;
123 uint32_t plane_id;
124 uint32_t count_formats;
125
126 int32_t src_x, src_y;
127 uint32_t src_w, src_h;
128 uint32_t dest_x, dest_y;
129 uint32_t dest_w, dest_h;
130
131 uint32_t formats[];
132};
133
134static int
135surface_is_primary(struct weston_compositor *ec, struct weston_surface *es)
136{
137 struct weston_surface *primary;
138
139 primary = container_of(ec->surface_list.next, struct weston_surface,
140 link);
141 if (es == primary)
142 return -1;
143 return 0;
144}
145
146static int
147drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
148{
149 struct weston_compositor *ec = output_base->compositor;
150 struct drm_compositor *c =(struct drm_compositor *) ec;
151 struct drm_output *output = (struct drm_output *) output_base;
152 int crtc;
153
154 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
155 if (c->crtcs[crtc] != output->crtc_id)
156 continue;
157
158 if (supported & (1 << crtc))
159 return -1;
160 }
161
162 return 0;
163}
164
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300165static void
166drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
167{
168 struct drm_fb *fb = data;
169 struct gbm_device *gbm = gbm_bo_get_device(bo);
170
171 if (fb->fb_id)
172 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
173
174 if (fb->buffer) {
175 weston_buffer_post_release(fb->buffer);
176 wl_list_remove(&fb->buffer_destroy_listener.link);
177 }
178
179 free(data);
180}
181
182static struct drm_fb *
183drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_output *output)
184{
185 struct drm_fb *fb = gbm_bo_get_user_data(bo);
186 struct drm_compositor *compositor =
187 (struct drm_compositor *) output->base.compositor;
188 uint32_t width, height, stride, handle;
189 int ret;
190
191 if (fb)
192 return fb;
193
194 fb = malloc(sizeof *fb);
195
196 fb->bo = bo;
197 fb->output = output;
198 fb->is_client_buffer = 0;
199 fb->buffer = NULL;
200
201 width = gbm_bo_get_width(bo);
202 height = gbm_bo_get_height(bo);
203 stride = gbm_bo_get_pitch(bo);
204 handle = gbm_bo_get_handle(bo).u32;
205
206 ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32,
207 stride, handle, &fb->fb_id);
208 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200209 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300210 free(fb);
211 return NULL;
212 }
213
214 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
215
216 return fb;
217}
218
219static void
220fb_handle_buffer_destroy(struct wl_listener *listener, void *data)
221{
222 struct drm_fb *fb = container_of(listener, struct drm_fb,
223 buffer_destroy_listener);
224
225 fb->buffer = NULL;
226
227 if (fb == fb->output->next ||
228 (fb == fb->output->current && !fb->output->next))
229 weston_compositor_schedule_repaint(fb->output->base.compositor);
230}
231
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500232static int
233drm_output_prepare_scanout_surface(struct drm_output *output)
234{
235 struct drm_compositor *c =
236 (struct drm_compositor *) output->base.compositor;
237 struct weston_surface *es;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300238 struct gbm_bo *bo;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500239
240 es = container_of(c->base.surface_list.next,
241 struct weston_surface, link);
242
Kristian Høgsberg101cb652012-02-17 10:45:16 -0500243 if (es->geometry.x != output->base.x ||
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200244 es->geometry.y != output->base.y ||
Pekka Paalanen60921e52012-01-25 15:55:43 +0200245 es->geometry.width != output->base.current->width ||
246 es->geometry.height != output->base.current->height ||
Pekka Paalanenf1f5b362012-01-25 14:33:33 +0200247 es->transform.enabled ||
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500248 es->image == EGL_NO_IMAGE_KHR)
249 return -1;
250
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300251 bo = gbm_bo_create_from_egl_image(c->gbm,
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400252 c->base.egl_display,
253 es->image,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300254 es->geometry.width,
255 es->geometry.height,
256 GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500257
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300258 /* Need to verify output->region contained in surface opaque
259 * region. Or maybe just that format doesn't have alpha.
260 * For now, scanout only if format is XRGB8888. */
261 if (gbm_bo_get_format(bo) != GBM_FORMAT_XRGB8888) {
262 gbm_bo_destroy(bo);
263 return -1;
264 }
265
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300266 output->next = drm_fb_get_from_bo(bo, output);
267 if (!output->next) {
268 gbm_bo_destroy(bo);
269 return -1;
270 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500271
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300272 output->next->is_client_buffer = 1;
273 output->next->buffer = es->buffer;
274 output->next->buffer->busy_count++;
275 output->next->buffer_destroy_listener.notify = fb_handle_buffer_destroy;
276
277 wl_signal_add(&output->next->buffer->resource.destroy_signal,
278 &output->next->buffer_destroy_listener);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500279
280 pixman_region32_fini(&es->damage);
281 pixman_region32_init(&es->damage);
282
283 return 0;
284}
285
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500286static void
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400287drm_output_render(struct drm_output *output, pixman_region32_t *damage)
288{
289 struct drm_compositor *compositor =
290 (struct drm_compositor *) output->base.compositor;
291 struct weston_surface *surface;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300292 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400293
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400294 if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
295 output->egl_surface,
296 compositor->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200297 weston_log("failed to make current\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400298 return;
299 }
300
301 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
302 weston_surface_draw(surface, &output->base, damage);
303
Scott Moreau062be7e2012-04-20 13:37:33 -0600304 weston_output_do_read_pixels(&output->base);
305
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400306 eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300307 bo = gbm_surface_lock_front_buffer(output->surface);
308 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200309 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400310 return;
311 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300312
313 output->next = drm_fb_get_from_bo(bo, output);
314 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200315 weston_log("failed to get drm_fb for bo\n");
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300316 gbm_surface_release_buffer(output->surface, bo);
317 return;
318 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400319}
320
321static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500322drm_output_repaint(struct weston_output *output_base,
323 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100324{
325 struct drm_output *output = (struct drm_output *) output_base;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500326 struct drm_compositor *compositor =
327 (struct drm_compositor *) output->base.compositor;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500328 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400329 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500330 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100331
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500332 drm_output_prepare_scanout_surface(output);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300333 if (!output->next)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400334 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300335 if (!output->next)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400336 return;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100337
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400338 mode = container_of(output->base.current, struct drm_mode, base);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300339 if (!output->current) {
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400340 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300341 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400342 &output->connector_id, 1,
343 &mode->mode_info);
344 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200345 weston_log("set mode failed: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400346 return;
347 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200348 }
349
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500350 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300351 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500352 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200353 weston_log("queueing pageflip failed: %m\n");
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500354 return;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500355 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100356
Jesse Barnes58ef3792012-02-23 09:45:49 -0500357 /*
358 * Now, update all the sprite surfaces
359 */
360 wl_list_for_each(s, &compositor->sprite_list, link) {
361 uint32_t flags = 0;
362 drmVBlank vbl = {
363 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
364 .request.sequence = 1,
365 };
366
367 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
368 continue;
369
370 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
371 output->crtc_id, s->pending_fb_id, flags,
372 s->dest_x, s->dest_y,
373 s->dest_w, s->dest_h,
374 s->src_x, s->src_y,
375 s->src_w, s->src_h);
376 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200377 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500378 ret, strerror(errno));
379
380 /*
381 * Queue a vblank signal so we know when the surface
382 * becomes active on the display or has been replaced.
383 */
384 vbl.request.signal = (unsigned long)s;
385 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
386 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200387 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500388 ret, strerror(errno));
389 }
390 }
391
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500392 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400393}
394
395static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500396vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
397 void *data)
398{
399 struct drm_sprite *s = (struct drm_sprite *)data;
400 struct drm_compositor *c = s->compositor;
401
402 if (s->surface) {
403 weston_buffer_post_release(s->surface->buffer);
404 wl_list_remove(&s->destroy_listener.link);
405 s->surface = NULL;
406 drmModeRmFB(c->drm.fd, s->fb_id);
407 s->fb_id = 0;
408 }
409
410 if (s->pending_surface) {
411 wl_list_remove(&s->pending_destroy_listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400412 wl_signal_add(&s->pending_surface->buffer->resource.destroy_signal,
413 &s->destroy_listener);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500414 s->surface = s->pending_surface;
415 s->pending_surface = NULL;
416 s->fb_id = s->pending_fb_id;
417 s->pending_fb_id = 0;
418 }
419}
420
421static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400422page_flip_handler(int fd, unsigned int frame,
423 unsigned int sec, unsigned int usec, void *data)
424{
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200425 struct drm_output *output = (struct drm_output *) data;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400426 uint32_t msecs;
427
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300428 if (output->current) {
429 if (output->current->is_client_buffer)
430 gbm_bo_destroy(output->current->bo);
431 else
432 gbm_surface_release_buffer(output->surface,
433 output->current->bo);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200434 }
435
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300436 output->current = output->next;
437 output->next = NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400438
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100439 msecs = sec * 1000 + usec / 1000;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500440 weston_output_finish_frame(&output->base, msecs);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200441}
442
443static int
Jesse Barnes58ef3792012-02-23 09:45:49 -0500444drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
445{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400446 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500447
448 for (i = 0; i < s->count_formats; i++)
449 if (s->formats[i] == format)
450 return 1;
451
452 return 0;
453}
454
455static int
456drm_surface_transform_supported(struct weston_surface *es)
457{
458 if (es->transform.enabled)
459 return 0;
460
461 return 1;
462}
463
464static int
465drm_surface_overlap_supported(struct weston_output *output_base,
466 pixman_region32_t *overlap)
467{
468 /* We could potentially use a color key here if the surface left
469 * to display has rectangular regions
470 */
471 if (pixman_region32_not_empty(overlap))
472 return 0;
473 return 1;
474}
475
476static void
477drm_disable_unused_sprites(struct weston_output *output_base)
478{
479 struct weston_compositor *ec = output_base->compositor;
480 struct drm_compositor *c =(struct drm_compositor *) ec;
481 struct drm_output *output = (struct drm_output *) output_base;
482 struct drm_sprite *s;
483 int ret;
484
485 wl_list_for_each(s, &c->sprite_list, link) {
486 if (s->pending_fb_id)
487 continue;
488
489 ret = drmModeSetPlane(c->drm.fd, s->plane_id,
490 output->crtc_id, 0, 0,
491 0, 0, 0, 0, 0, 0, 0, 0);
492 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200493 weston_log("failed to disable plane: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500494 ret, strerror(errno));
495 drmModeRmFB(c->drm.fd, s->fb_id);
496 s->surface = NULL;
497 s->pending_surface = NULL;
498 s->fb_id = 0;
499 s->pending_fb_id = 0;
500 }
501}
502
503/*
504 * This function must take care to damage any previously assigned surface
505 * if the sprite ends up binding to a different surface than in the
506 * previous frame.
507 */
508static int
509drm_output_prepare_overlay_surface(struct weston_output *output_base,
510 struct weston_surface *es,
511 pixman_region32_t *overlap)
512{
513 struct weston_compositor *ec = output_base->compositor;
514 struct drm_compositor *c =(struct drm_compositor *) ec;
515 struct drm_sprite *s;
516 int found = 0;
517 EGLint handle, stride;
518 struct gbm_bo *bo;
519 uint32_t fb_id = 0;
520 uint32_t handles[4], pitches[4], offsets[4];
521 int ret = 0;
522 pixman_region32_t dest_rect, src_rect;
523 pixman_box32_t *box;
524 uint32_t format;
525
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500526 if (c->sprites_are_broken)
527 return -1;
528
Jesse Barnes58ef3792012-02-23 09:45:49 -0500529 if (surface_is_primary(ec, es))
530 return -1;
531
Kristian Høgsberg7a011ea2012-03-26 11:48:55 -0400532 if (es->image == EGL_NO_IMAGE_KHR)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500533 return -1;
534
535 if (!drm_surface_transform_supported(es))
536 return -1;
537
538 if (!drm_surface_overlap_supported(output_base, overlap))
539 return -1;
540
541 wl_list_for_each(s, &c->sprite_list, link) {
542 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
543 continue;
544
545 if (!s->pending_fb_id) {
546 found = 1;
547 break;
548 }
549 }
550
551 /* No sprites available */
552 if (!found)
553 return -1;
554
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400555 bo = gbm_bo_create_from_egl_image(c->gbm, c->base.egl_display,
556 es->image, es->geometry.width,
557 es->geometry.height,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500558 GBM_BO_USE_SCANOUT);
559 format = gbm_bo_get_format(bo);
560 handle = gbm_bo_get_handle(bo).s32;
561 stride = gbm_bo_get_pitch(bo);
562
563 gbm_bo_destroy(bo);
564
565 if (!drm_surface_format_supported(s, format))
566 return -1;
567
568 if (!handle)
569 return -1;
570
571 handles[0] = handle;
572 pitches[0] = stride;
573 offsets[0] = 0;
574
575 ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height,
576 format, handles, pitches, offsets,
577 &fb_id, 0);
578 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200579 weston_log("addfb2 failed: %d\n", ret);
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500580 c->sprites_are_broken = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500581 return -1;
582 }
583
584 if (s->surface && s->surface != es) {
585 struct weston_surface *old_surf = s->surface;
586 pixman_region32_fini(&old_surf->damage);
587 pixman_region32_init_rect(&old_surf->damage,
588 old_surf->geometry.x, old_surf->geometry.y,
589 old_surf->geometry.width, old_surf->geometry.height);
590 }
591
592 s->pending_fb_id = fb_id;
593 s->pending_surface = es;
594 es->buffer->busy_count++;
595
596 /*
597 * Calculate the source & dest rects properly based on actual
598 * postion (note the caller has called weston_surface_update_transform()
599 * for us already).
600 */
601 pixman_region32_init(&dest_rect);
602 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
603 &output_base->region);
604 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
605 box = pixman_region32_extents(&dest_rect);
606 s->dest_x = box->x1;
607 s->dest_y = box->y1;
608 s->dest_w = box->x2 - box->x1;
609 s->dest_h = box->y2 - box->y1;
610 pixman_region32_fini(&dest_rect);
611
612 pixman_region32_init(&src_rect);
613 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
614 &output_base->region);
615 pixman_region32_translate(&src_rect, -es->geometry.x, -es->geometry.y);
616 box = pixman_region32_extents(&src_rect);
Rob Clark3c97b902012-03-11 19:48:43 -0500617 s->src_x = box->x1 << 16;
618 s->src_y = box->y1 << 16;
619 s->src_w = (box->x2 - box->x1) << 16;
620 s->src_h = (box->y2 - box->y1) << 16;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500621 pixman_region32_fini(&src_rect);
622
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400623 wl_signal_add(&es->buffer->resource.destroy_signal,
624 &s->pending_destroy_listener);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500625 return 0;
626}
627
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500628static int
629drm_output_set_cursor(struct weston_output *output_base,
Daniel Stone37816df2012-05-16 18:45:18 +0100630 struct weston_seat *es);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500631
632static void
633weston_output_set_cursor(struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +0100634 struct weston_seat *seat,
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500635 pixman_region32_t *overlap)
636{
637 pixman_region32_t cursor_region;
638 int prior_was_hardware;
639
Daniel Stone37816df2012-05-16 18:45:18 +0100640 if (seat->sprite == NULL)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500641 return;
642
643 pixman_region32_init(&cursor_region);
644 pixman_region32_intersect(&cursor_region,
Daniel Stone37816df2012-05-16 18:45:18 +0100645 &seat->sprite->transform.boundingbox,
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500646 &output->region);
647
648 if (!pixman_region32_not_empty(&cursor_region)) {
649 drm_output_set_cursor(output, NULL);
650 goto out;
651 }
652
Daniel Stone37816df2012-05-16 18:45:18 +0100653 prior_was_hardware = seat->hw_cursor;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500654 if (pixman_region32_not_empty(overlap) ||
Daniel Stone37816df2012-05-16 18:45:18 +0100655 drm_output_set_cursor(output, seat) < 0) {
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500656 if (prior_was_hardware) {
Daniel Stone37816df2012-05-16 18:45:18 +0100657 weston_surface_damage(seat->sprite);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500658 drm_output_set_cursor(output, NULL);
659 }
Daniel Stone37816df2012-05-16 18:45:18 +0100660 seat->hw_cursor = 0;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500661 } else {
662 if (!prior_was_hardware)
Daniel Stone37816df2012-05-16 18:45:18 +0100663 weston_surface_damage_below(seat->sprite);
664 wl_list_remove(&seat->sprite->link);
665 seat->hw_cursor = 1;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500666 }
667
668out:
669 pixman_region32_fini(&cursor_region);
670}
671
Jesse Barnes58ef3792012-02-23 09:45:49 -0500672static void
673drm_assign_planes(struct weston_output *output)
674{
675 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400676 struct weston_surface *es, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500677 pixman_region32_t overlap, surface_overlap;
Daniel Stone37816df2012-05-16 18:45:18 +0100678 struct weston_seat *seat;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500679
680 /*
681 * Find a surface for each sprite in the output using some heuristics:
682 * 1) size
683 * 2) frequency of update
684 * 3) opacity (though some hw might support alpha blending)
685 * 4) clipping (this can be fixed with color keys)
686 *
687 * The idea is to save on blitting since this should save power.
688 * If we can get a large video surface on the sprite for example,
689 * the main display surface may not need to update at all, and
690 * the client buffer can be used directly for the sprite surface
691 * as we do for flipping full screen surfaces.
692 */
693 pixman_region32_init(&overlap);
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400694 wl_list_for_each_safe(es, next, &ec->surface_list, link) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500695 /*
696 * FIXME: try to assign hw cursors here too, they're just
697 * special overlays
698 */
699 pixman_region32_init(&surface_overlap);
700 pixman_region32_intersect(&surface_overlap, &overlap,
701 &es->transform.boundingbox);
702
Daniel Stone37816df2012-05-16 18:45:18 +0100703 seat = (struct weston_seat *) ec->seat;
704 if (es == seat->sprite) {
705 weston_output_set_cursor(output, seat,
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500706 &surface_overlap);
707
Daniel Stone37816df2012-05-16 18:45:18 +0100708 if (!seat->hw_cursor)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500709 pixman_region32_union(&overlap, &overlap,
710 &es->transform.boundingbox);
711 } else if (!drm_output_prepare_overlay_surface(output, es,
712 &surface_overlap)) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500713 pixman_region32_fini(&es->damage);
714 pixman_region32_init(&es->damage);
715 } else {
716 pixman_region32_union(&overlap, &overlap,
717 &es->transform.boundingbox);
718 }
719 pixman_region32_fini(&surface_overlap);
720 }
721 pixman_region32_fini(&overlap);
722
723 drm_disable_unused_sprites(output);
724}
725
726static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500727drm_output_set_cursor(struct weston_output *output_base,
Daniel Stone37816df2012-05-16 18:45:18 +0100728 struct weston_seat *es)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200729{
730 struct drm_output *output = (struct drm_output *) output_base;
731 struct drm_compositor *c =
732 (struct drm_compositor *) output->base.compositor;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200733 EGLint handle, stride;
734 int ret = -1;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200735 struct gbm_bo *bo;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400736 uint32_t buf[64 * 64];
737 unsigned char *d, *s, *end;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200738
Daniel Stone37816df2012-05-16 18:45:18 +0100739 if (es == NULL) {
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400740 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
741 return 0;
742 }
743
Daniel Stone37816df2012-05-16 18:45:18 +0100744 if (es->sprite->buffer == NULL ||
745 !wl_buffer_is_shm(es->sprite->buffer))
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200746 goto out;
747
Daniel Stone37816df2012-05-16 18:45:18 +0100748 if (es->sprite->geometry.width > 64 ||
749 es->sprite->geometry.height > 64)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200750 goto out;
Benjamin Franzke117483d2011-08-30 11:38:26 +0200751
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400752 output->current_cursor ^= 1;
753 bo = output->cursor_bo[output->current_cursor];
Kristian Høgsberg6916d9c2012-01-18 15:11:07 -0500754 if (bo == NULL)
755 goto out;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200756
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400757 memset(buf, 0, sizeof buf);
758 d = (unsigned char *) buf;
Daniel Stone37816df2012-05-16 18:45:18 +0100759 stride = wl_shm_buffer_get_stride(es->sprite->buffer);
760 s = wl_shm_buffer_get_data(es->sprite->buffer);
761 end = s + stride * es->sprite->geometry.height;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400762 while (s < end) {
Daniel Stone37816df2012-05-16 18:45:18 +0100763 memcpy(d, s, es->sprite->geometry.width * 4);
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400764 s += stride;
765 d += 64 * 4;
766 }
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200767
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400768 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200769 goto out;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200770
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400771 handle = gbm_bo_get_handle(bo).s32;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200772 ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
773 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200774 weston_log("failed to set cursor: %s\n", strerror(-ret));
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200775 goto out;
776 }
777
778 ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
Daniel Stone37816df2012-05-16 18:45:18 +0100779 es->sprite->geometry.x - output->base.x,
780 es->sprite->geometry.y - output->base.y);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200781 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200782 weston_log("failed to move cursor: %s\n", strerror(-ret));
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200783 goto out;
784 }
785
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200786out:
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200787 if (ret)
788 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
789 return ret;
790}
791
Matt Roper361d2ad2011-08-29 13:52:23 -0700792static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500793drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700794{
795 struct drm_output *output = (struct drm_output *) output_base;
796 struct drm_compositor *c =
Benjamin Franzke117483d2011-08-30 11:38:26 +0200797 (struct drm_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700798 drmModeCrtcPtr origcrtc = output->original_crtc;
Matt Roper361d2ad2011-08-29 13:52:23 -0700799
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200800 if (output->backlight)
801 backlight_destroy(output->backlight);
802
Matt Roper361d2ad2011-08-29 13:52:23 -0700803 /* Turn off hardware cursor */
Benjamin Franzke117483d2011-08-30 11:38:26 +0200804 drm_output_set_cursor(&output->base, NULL);
Matt Roper361d2ad2011-08-29 13:52:23 -0700805
806 /* Restore original CRTC state */
807 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +0200808 origcrtc->x, origcrtc->y,
809 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -0700810 drmModeFreeCrtc(origcrtc);
811
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200812 c->crtc_allocator &= ~(1 << output->crtc_id);
813 c->connector_allocator &= ~(1 << output->connector_id);
814
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400815 eglDestroySurface(c->base.egl_display, output->egl_surface);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400816 gbm_surface_destroy(output->surface);
817
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500818 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200819 wl_list_remove(&output->base.link);
820
Matt Roper361d2ad2011-08-29 13:52:23 -0700821 free(output);
822}
823
Alex Wub7b8bda2012-04-17 17:20:48 +0800824static struct drm_mode *
825choose_mode (struct drm_output *output, struct weston_mode *target_mode)
826{
827 struct drm_mode *tmp_mode = NULL, *mode;
828
829 if (output->base.current->width == target_mode->width &&
830 output->base.current->height == target_mode->height &&
831 (output->base.current->refresh == target_mode->refresh ||
832 target_mode->refresh == 0))
833 return (struct drm_mode *)output->base.current;
834
835 wl_list_for_each(mode, &output->base.mode_list, base.link) {
836 if (mode->mode_info.hdisplay == target_mode->width &&
837 mode->mode_info.vdisplay == target_mode->height) {
838 if (mode->mode_info.vrefresh == target_mode->refresh ||
839 target_mode->refresh == 0) {
840 return mode;
841 } else if (!tmp_mode)
842 tmp_mode = mode;
843 }
844 }
845
846 return tmp_mode;
847}
848
849static int
850drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
851{
852 struct drm_output *output;
853 struct drm_mode *drm_mode;
854 int ret;
855 struct drm_compositor *ec;
856 struct gbm_surface *surface;
857 EGLSurface egl_surface;
858
859 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200860 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800861 return -1;
862 }
863
864 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200865 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800866 return -1;
867 }
868
869 ec = (struct drm_compositor *)output_base->compositor;
870 output = (struct drm_output *)output_base;
871 drm_mode = choose_mode (output, mode);
872
873 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +0200874 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +0800875 return -1;
876 } else if (&drm_mode->base == output->base.current) {
877 return 0;
878 } else if (drm_mode->base.width == output->base.current->width &&
879 drm_mode->base.height == output->base.current->height) {
880 /* only change refresh value */
881 ret = drmModeSetCrtc(ec->drm.fd,
882 output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300883 output->current->fb_id, 0, 0,
Alex Wub7b8bda2012-04-17 17:20:48 +0800884 &output->connector_id, 1, &drm_mode->mode_info);
885
886 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200887 weston_log("failed to set mode (%dx%d) %u Hz\n",
Alex Wub7b8bda2012-04-17 17:20:48 +0800888 drm_mode->base.width,
889 drm_mode->base.height,
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400890 drm_mode->base.refresh / 1000);
Alex Wub7b8bda2012-04-17 17:20:48 +0800891 ret = -1;
892 } else {
893 output->base.current->flags = 0;
894 output->base.current = &drm_mode->base;
895 drm_mode->base.flags =
896 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
897 ret = 0;
898 }
899
900 return ret;
901 }
902
903 drm_mode->base.flags =
904 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
905
906 surface = gbm_surface_create(ec->gbm,
907 drm_mode->base.width,
908 drm_mode->base.height,
909 GBM_FORMAT_XRGB8888,
910 GBM_BO_USE_SCANOUT |
911 GBM_BO_USE_RENDERING);
912 if (!surface) {
Martin Minarik6d118362012-06-07 18:01:59 +0200913 weston_log("failed to create gbm surface\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800914 return -1;
915 }
916
917 egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400918 eglCreateWindowSurface(ec->base.egl_display,
919 ec->base.egl_config,
Alex Wub7b8bda2012-04-17 17:20:48 +0800920 surface, NULL);
921
922 if (egl_surface == EGL_NO_SURFACE) {
Martin Minarik6d118362012-06-07 18:01:59 +0200923 weston_log("failed to create egl surface\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800924 goto err;
925 }
926
927 ret = drmModeSetCrtc(ec->drm.fd,
928 output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300929 output->current->fb_id, 0, 0,
Alex Wub7b8bda2012-04-17 17:20:48 +0800930 &output->connector_id, 1, &drm_mode->mode_info);
931 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200932 weston_log("failed to set mode\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800933 goto err;
934 }
935
936 /* reset rendering stuff. */
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300937 if (output->current) {
938 if (output->current->is_client_buffer)
939 gbm_bo_destroy(output->current->bo);
940 else
941 gbm_surface_release_buffer(output->surface,
942 output->current->bo);
943 }
944 output->current = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +0800945
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300946 if (output->next) {
947 if (output->next->is_client_buffer)
948 gbm_bo_destroy(output->next->bo);
949 else
950 gbm_surface_release_buffer(output->surface,
951 output->next->bo);
952 }
953 output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +0800954
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400955 eglDestroySurface(ec->base.egl_display, output->egl_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +0800956 gbm_surface_destroy(output->surface);
957 output->egl_surface = egl_surface;
958 output->surface = surface;
959
960 /*update output*/
961 output->base.current = &drm_mode->base;
962 output->base.dirty = 1;
963 weston_output_move(&output->base, output->base.x, output->base.y);
964 return 0;
965
966err:
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400967 eglDestroySurface(ec->base.egl_display, egl_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +0800968 gbm_surface_destroy(surface);
969 return -1;
970}
971
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400972static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400973on_drm_input(int fd, uint32_t mask, void *data)
974{
975 drmEventContext evctx;
976
977 memset(&evctx, 0, sizeof evctx);
978 evctx.version = DRM_EVENT_CONTEXT_VERSION;
979 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500980 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400981 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400982
983 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400984}
985
986static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400987init_egl(struct drm_compositor *ec, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400988{
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400989 EGLint major, minor, n;
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400990 const char *filename, *sysnum;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400991 int fd;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400992 static const EGLint context_attribs[] = {
993 EGL_CONTEXT_CLIENT_VERSION, 2,
994 EGL_NONE
995 };
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400996
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400997 static const EGLint config_attribs[] = {
998 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
999 EGL_RED_SIZE, 1,
1000 EGL_GREEN_SIZE, 1,
1001 EGL_BLUE_SIZE, 1,
1002 EGL_ALPHA_SIZE, 0,
1003 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1004 EGL_NONE
1005 };
1006
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001007 sysnum = udev_device_get_sysnum(device);
1008 if (sysnum)
1009 ec->drm.id = atoi(sysnum);
1010 if (!sysnum || ec->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001011 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001012 return -1;
1013 }
1014
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001015 filename = udev_device_get_devnode(device);
David Herrmann63ff7062011-11-05 18:46:01 +01001016 fd = open(filename, O_RDWR | O_CLOEXEC);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001017 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001018 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001019 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001020 udev_device_get_devnode(device));
1021 return -1;
1022 }
1023
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001024 weston_log("using %s\n", filename);
1025
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001026 ec->drm.fd = fd;
Benjamin Franzke060cf802011-04-30 09:32:11 +02001027 ec->gbm = gbm_create_device(ec->drm.fd);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001028 ec->base.egl_display = eglGetDisplay(ec->gbm);
1029 if (ec->base.egl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001030 weston_log("failed to create display\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001031 return -1;
1032 }
1033
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001034 if (!eglInitialize(ec->base.egl_display, &major, &minor)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001035 weston_log("failed to initialize display\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001036 return -1;
1037 }
1038
Darxus55973f22010-11-22 21:24:39 -05001039 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001040 weston_log("failed to bind api EGL_OPENGL_ES_API\n");
Darxus55973f22010-11-22 21:24:39 -05001041 return -1;
1042 }
1043
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001044 if (!eglChooseConfig(ec->base.egl_display, config_attribs,
1045 &ec->base.egl_config, 1, &n) || n != 1) {
Martin Minarik6d118362012-06-07 18:01:59 +02001046 weston_log("failed to choose config: %d\n", n);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001047 return -1;
1048 }
1049
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001050 ec->base.egl_context =
1051 eglCreateContext(ec->base.egl_display, ec->base.egl_config,
1052 EGL_NO_CONTEXT, context_attribs);
1053 if (ec->base.egl_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001054 weston_log("failed to create context\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001055 return -1;
1056 }
1057
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001058 ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10,
1059 GBM_FORMAT_XRGB8888,
1060 GBM_BO_USE_RENDERING);
1061 if (!ec->dummy_surface) {
Martin Minarik6d118362012-06-07 18:01:59 +02001062 weston_log("failed to create dummy gbm surface\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001063 return -1;
1064 }
1065
1066 ec->dummy_egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001067 eglCreateWindowSurface(ec->base.egl_display,
1068 ec->base.egl_config,
1069 ec->dummy_surface,
1070 NULL);
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001071 if (ec->dummy_egl_surface == EGL_NO_SURFACE) {
Martin Minarik6d118362012-06-07 18:01:59 +02001072 weston_log("failed to create egl surface\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001073 return -1;
1074 }
1075
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001076 if (!eglMakeCurrent(ec->base.egl_display, ec->dummy_egl_surface,
1077 ec->dummy_egl_surface, ec->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001078 weston_log("failed to make context current\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001079 return -1;
1080 }
1081
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001082 return 0;
1083}
1084
1085static drmModeModeInfo builtin_1024x768 = {
1086 63500, /* clock */
1087 1024, 1072, 1176, 1328, 0,
1088 768, 771, 775, 798, 0,
1089 59920,
1090 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
1091 0,
1092 "1024x768"
1093};
1094
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001095
1096static int
1097drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1098{
1099 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001100 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001101
1102 mode = malloc(sizeof *mode);
1103 if (mode == NULL)
1104 return -1;
1105
1106 mode->base.flags = 0;
1107 mode->base.width = info->hdisplay;
1108 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001109
1110 /* Calculate higher precision (mHz) refresh rate */
1111 refresh = (info->clock * 1000000LL / info->htotal +
1112 info->vtotal / 2) / info->vtotal;
1113
1114 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1115 refresh *= 2;
1116 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1117 refresh /= 2;
1118 if (info->vscan > 1)
1119 refresh /= info->vscan;
1120
1121 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001122 mode->mode_info = *info;
1123 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1124
1125 return 0;
1126}
1127
1128static int
1129drm_subpixel_to_wayland(int drm_value)
1130{
1131 switch (drm_value) {
1132 default:
1133 case DRM_MODE_SUBPIXEL_UNKNOWN:
1134 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1135 case DRM_MODE_SUBPIXEL_NONE:
1136 return WL_OUTPUT_SUBPIXEL_NONE;
1137 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1138 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1139 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1140 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1141 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1142 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1143 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1144 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1145 }
1146}
1147
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001148static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001149sprite_handle_buffer_destroy(struct wl_listener *listener, void *data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001150{
1151 struct drm_sprite *sprite =
1152 container_of(listener, struct drm_sprite,
1153 destroy_listener);
1154
1155 sprite->surface = NULL;
1156}
1157
1158static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001159sprite_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001160{
1161 struct drm_sprite *sprite =
1162 container_of(listener, struct drm_sprite,
1163 pending_destroy_listener);
1164
1165 sprite->pending_surface = NULL;
1166}
1167
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001168/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001169static uint32_t
1170drm_get_backlight(struct drm_output *output)
1171{
1172 long brightness, max_brightness, norm;
1173
1174 brightness = backlight_get_brightness(output->backlight);
1175 max_brightness = backlight_get_max_brightness(output->backlight);
1176
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001177 /* convert it on a scale of 0 to 255 */
1178 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001179
1180 return (uint32_t) norm;
1181}
1182
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001183/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001184static void
1185drm_set_backlight(struct weston_output *output_base, uint32_t value)
1186{
1187 struct drm_output *output = (struct drm_output *) output_base;
1188 long max_brightness, new_brightness;
1189
1190 if (!output->backlight)
1191 return;
1192
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001193 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001194 return;
1195
1196 max_brightness = backlight_get_max_brightness(output->backlight);
1197
1198 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001199 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001200
1201 backlight_set_brightness(output->backlight, new_brightness);
1202}
1203
1204static drmModePropertyPtr
1205drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1206{
1207 drmModePropertyPtr props;
1208 int i;
1209
1210 for (i = 0; i < connector->count_props; i++) {
1211 props = drmModeGetProperty(fd, connector->props[i]);
1212 if (!props)
1213 continue;
1214
1215 if (!strcmp(props->name, name))
1216 return props;
1217
1218 drmModeFreeProperty(props);
1219 }
1220
1221 return NULL;
1222}
1223
1224static void
1225drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1226{
1227 struct drm_output *output = (struct drm_output *) output_base;
1228 struct weston_compositor *ec = output_base->compositor;
1229 struct drm_compositor *c = (struct drm_compositor *) ec;
1230 drmModeConnectorPtr connector;
1231 drmModePropertyPtr prop;
1232
1233 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1234 if (!connector)
1235 return;
1236
1237 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1238 if (!prop) {
1239 drmModeFreeConnector(connector);
1240 return;
1241 }
1242
1243 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1244 prop->prop_id, level);
1245 drmModeFreeProperty(prop);
1246 drmModeFreeConnector(connector);
1247}
1248
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001249static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001250create_output_for_connector(struct drm_compositor *ec,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001251 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001252 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001253 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001254{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001255 struct drm_output *output;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001256 struct drm_mode *drm_mode, *next;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001257 drmModeEncoder *encoder;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001258 int i, ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001259
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001260 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001261 if (encoder == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001262 weston_log("No encoder for connector.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001263 return -1;
1264 }
1265
1266 for (i = 0; i < resources->count_crtcs; i++) {
Marty Jack13d9db22011-02-09 19:01:42 -05001267 if (encoder->possible_crtcs & (1 << i) &&
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001268 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001269 break;
1270 }
1271 if (i == resources->count_crtcs) {
Martin Minarik6d118362012-06-07 18:01:59 +02001272 weston_log("No usable crtc for encoder.\n");
David Herrmanneb8bed52011-12-08 17:05:44 +01001273 drmModeFreeEncoder(encoder);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001274 return -1;
1275 }
1276
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001277 output = malloc(sizeof *output);
David Herrmanneb8bed52011-12-08 17:05:44 +01001278 if (output == NULL) {
1279 drmModeFreeEncoder(encoder);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001280 return -1;
David Herrmanneb8bed52011-12-08 17:05:44 +01001281 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001282
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001283 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001284 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1285 output->base.make = "unknown";
1286 output->base.model = "unknown";
1287 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001288
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001289 output->crtc_id = resources->crtcs[i];
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001290 ec->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001291 output->connector_id = connector->connector_id;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001292 ec->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001293
Matt Roper361d2ad2011-08-29 13:52:23 -07001294 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001295 drmModeFreeEncoder(encoder);
Matt Roper361d2ad2011-08-29 13:52:23 -07001296
David Herrmann0f0d54e2011-12-08 17:05:45 +01001297 for (i = 0; i < connector->count_modes; i++) {
1298 ret = drm_output_add_mode(output, &connector->modes[i]);
1299 if (ret)
1300 goto err_free;
1301 }
1302
1303 if (connector->count_modes == 0) {
1304 ret = drm_output_add_mode(output, &builtin_1024x768);
1305 if (ret)
1306 goto err_free;
1307 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001308
1309 drm_mode = container_of(output->base.mode_list.next,
1310 struct drm_mode, base.link);
1311 output->base.current = &drm_mode->base;
1312 drm_mode->base.flags =
1313 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1314
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001315 output->surface = gbm_surface_create(ec->gbm,
1316 output->base.current->width,
1317 output->base.current->height,
1318 GBM_FORMAT_XRGB8888,
1319 GBM_BO_USE_SCANOUT |
1320 GBM_BO_USE_RENDERING);
1321 if (!output->surface) {
Martin Minarik6d118362012-06-07 18:01:59 +02001322 weston_log("failed to create gbm surface\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001323 goto err_free;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001324 }
1325
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001326 output->egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001327 eglCreateWindowSurface(ec->base.egl_display,
1328 ec->base.egl_config,
1329 output->surface,
1330 NULL);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001331 if (output->egl_surface == EGL_NO_SURFACE) {
Martin Minarik6d118362012-06-07 18:01:59 +02001332 weston_log("failed to create egl surface\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001333 goto err_surface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001334 }
1335
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04001336 output->cursor_bo[0] =
1337 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1338 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1339 output->cursor_bo[1] =
1340 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1341 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1342
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001343 output->backlight = backlight_init(drm_device,
1344 connector->connector_type);
1345 if (output->backlight) {
1346 output->base.set_backlight = drm_set_backlight;
1347 output->base.backlight_current = drm_get_backlight(output);
1348 }
1349
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001350 weston_output_init(&output->base, &ec->base, x, y,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001351 connector->mmWidth, connector->mmHeight,
1352 WL_OUTPUT_FLIPPED);
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04001353
1354 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1355
Alex Wubd3354b2012-04-17 17:20:49 +08001356 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001357 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07001358 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001359 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001360 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08001361 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001362
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001363 weston_log("kms connector %d, crtc %d at mode %dx%d@%.1f\n",
1364 output->connector_id, output->crtc_id,
1365 output->base.current->width,
1366 output->base.current->height,
1367 output->base.current->refresh / 1000.0);
1368
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001369 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001370
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001371err_surface:
1372 gbm_surface_destroy(output->surface);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001373err_free:
1374 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1375 base.link) {
1376 wl_list_remove(&drm_mode->base.link);
1377 free(drm_mode);
1378 }
1379
1380 drmModeFreeCrtc(output->original_crtc);
1381 ec->crtc_allocator &= ~(1 << output->crtc_id);
1382 ec->connector_allocator &= ~(1 << output->connector_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001383 free(output);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001384
David Herrmann0f0d54e2011-12-08 17:05:45 +01001385 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001386}
1387
Jesse Barnes58ef3792012-02-23 09:45:49 -05001388static void
1389create_sprites(struct drm_compositor *ec)
1390{
1391 struct drm_sprite *sprite;
1392 drmModePlaneRes *plane_res;
1393 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001394 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001395
1396 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1397 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02001398 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001399 strerror(errno));
1400 return;
1401 }
1402
1403 for (i = 0; i < plane_res->count_planes; i++) {
1404 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1405 if (!plane)
1406 continue;
1407
1408 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1409 plane->count_formats));
1410 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02001411 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001412 __func__);
1413 free(plane);
1414 continue;
1415 }
1416
1417 memset(sprite, 0, sizeof *sprite);
1418
1419 sprite->possible_crtcs = plane->possible_crtcs;
1420 sprite->plane_id = plane->plane_id;
1421 sprite->surface = NULL;
1422 sprite->pending_surface = NULL;
1423 sprite->fb_id = 0;
1424 sprite->pending_fb_id = 0;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001425 sprite->destroy_listener.notify = sprite_handle_buffer_destroy;
1426 sprite->pending_destroy_listener.notify =
Jesse Barnes58ef3792012-02-23 09:45:49 -05001427 sprite_handle_pending_buffer_destroy;
1428 sprite->compositor = ec;
1429 sprite->count_formats = plane->count_formats;
1430 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05001431 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05001432 drmModeFreePlane(plane);
1433
1434 wl_list_insert(&ec->sprite_list, &sprite->link);
1435 }
1436
1437 free(plane_res->planes);
1438 free(plane_res);
1439}
1440
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001441static void
1442destroy_sprites(struct drm_compositor *compositor)
1443{
1444 struct drm_sprite *sprite, *next;
1445 struct drm_output *output;
1446
1447 output = container_of(compositor->base.output_list.next,
1448 struct drm_output, base.link);
1449
1450 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1451 drmModeSetPlane(compositor->drm.fd,
1452 sprite->plane_id,
1453 output->crtc_id, 0, 0,
1454 0, 0, 0, 0, 0, 0, 0, 0);
1455 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1456 free(sprite);
1457 }
1458}
Jesse Barnes58ef3792012-02-23 09:45:49 -05001459
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001460static int
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001461create_outputs(struct drm_compositor *ec, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001462 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001463{
1464 drmModeConnector *connector;
1465 drmModeRes *resources;
1466 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001467 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001468
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001469 resources = drmModeGetResources(ec->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001470 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02001471 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001472 return -1;
1473 }
1474
Jesse Barnes58ef3792012-02-23 09:45:49 -05001475 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001476 if (!ec->crtcs) {
1477 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001478 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001479 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001480
1481 ec->num_crtcs = resources->count_crtcs;
1482 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1483
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001484 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001485 connector = drmModeGetConnector(ec->drm.fd,
1486 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001487 if (connector == NULL)
1488 continue;
1489
1490 if (connector->connection == DRM_MODE_CONNECTED &&
1491 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001492 connector->connector_id == option_connector)) {
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001493 if (create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001494 connector, x, y,
1495 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02001496 drmModeFreeConnector(connector);
1497 continue;
1498 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001499
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001500 x += container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001501 struct weston_output,
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001502 link)->current->width;
1503 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001504
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001505 drmModeFreeConnector(connector);
1506 }
1507
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001508 if (wl_list_empty(&ec->base.output_list)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001509 weston_log("No currently active connector found.\n");
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001510 drmModeFreeResources(resources);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001511 return -1;
1512 }
1513
1514 drmModeFreeResources(resources);
1515
1516 return 0;
1517}
1518
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001519static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001520update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001521{
1522 drmModeConnector *connector;
1523 drmModeRes *resources;
1524 struct drm_output *output, *next;
1525 int x = 0, y = 0;
1526 int x_offset = 0, y_offset = 0;
1527 uint32_t connected = 0, disconnects = 0;
1528 int i;
1529
1530 resources = drmModeGetResources(ec->drm.fd);
1531 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02001532 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001533 return;
1534 }
1535
1536 /* collect new connects */
1537 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001538 int connector_id = resources->connectors[i];
1539
1540 connector = drmModeGetConnector(ec->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01001541 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001542 continue;
1543
David Herrmann7551cff2011-12-08 17:05:43 +01001544 if (connector->connection != DRM_MODE_CONNECTED) {
1545 drmModeFreeConnector(connector);
1546 continue;
1547 }
1548
Benjamin Franzke117483d2011-08-30 11:38:26 +02001549 connected |= (1 << connector_id);
1550
1551 if (!(ec->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001552 struct weston_output *last =
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001553 container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001554 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001555
1556 /* XXX: not yet needed, we die with 0 outputs */
1557 if (!wl_list_empty(&ec->base.output_list))
Benjamin Franzke117483d2011-08-30 11:38:26 +02001558 x = last->x + last->current->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001559 else
1560 x = 0;
1561 y = 0;
1562 create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001563 connector, x, y,
1564 drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02001565 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001566
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001567 }
1568 drmModeFreeConnector(connector);
1569 }
1570 drmModeFreeResources(resources);
1571
1572 disconnects = ec->connector_allocator & ~connected;
1573 if (disconnects) {
1574 wl_list_for_each_safe(output, next, &ec->base.output_list,
1575 base.link) {
1576 if (x_offset != 0 || y_offset != 0) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001577 weston_output_move(&output->base,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001578 output->base.x - x_offset,
1579 output->base.y - y_offset);
1580 }
1581
1582 if (disconnects & (1 << output->connector_id)) {
1583 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02001584 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001585 output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001586 x_offset += output->base.current->width;
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001587 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001588 }
1589 }
1590 }
1591
1592 /* FIXME: handle zero outputs, without terminating */
1593 if (ec->connector_allocator == 0)
1594 wl_display_terminate(ec->base.wl_display);
1595}
1596
1597static int
David Herrmannd7488c22012-03-11 20:05:21 +01001598udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001599{
David Herrmannd7488c22012-03-11 20:05:21 +01001600 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01001601 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01001602
1603 sysnum = udev_device_get_sysnum(device);
1604 if (!sysnum || atoi(sysnum) != ec->drm.id)
1605 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001606
David Herrmann6ac52db2012-03-11 20:05:22 +01001607 val = udev_device_get_property_value(device, "HOTPLUG");
1608 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001609 return 0;
1610
David Herrmann6ac52db2012-03-11 20:05:22 +01001611 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001612}
1613
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001614static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001615udev_drm_event(int fd, uint32_t mask, void *data)
1616{
1617 struct drm_compositor *ec = data;
1618 struct udev_device *event;
1619
1620 event = udev_monitor_receive_device(ec->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001621
David Herrmannd7488c22012-03-11 20:05:21 +01001622 if (udev_event_is_hotplug(ec, event))
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001623 update_outputs(ec, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001624
1625 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001626
1627 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001628}
1629
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001630static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001631drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001632{
1633 struct drm_compositor *d = (struct drm_compositor *) ec;
Daniel Stone37816df2012-05-16 18:45:18 +01001634 struct weston_seat *seat, *next;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001635
Daniel Stone37816df2012-05-16 18:45:18 +01001636 wl_list_for_each_safe(seat, next, &ec->seat_list, link)
1637 evdev_input_destroy(seat);
Jonas Ådahlc97af922012-03-28 22:36:09 +02001638
1639 wl_event_source_remove(d->udev_drm_source);
1640 wl_event_source_remove(d->drm_source);
1641
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001642 weston_compositor_shutdown(ec);
Jonas Ådahlc97af922012-03-28 22:36:09 +02001643
Ander Conselvan de Oliveira5f5f3192012-04-30 13:31:28 +03001644 /* Work around crash in egl_dri2.c's dri2_make_current() */
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001645 eglMakeCurrent(ec->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
Ander Conselvan de Oliveira5f5f3192012-04-30 13:31:28 +03001646 EGL_NO_CONTEXT);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001647 eglTerminate(ec->egl_display);
Ander Conselvan de Oliveira5f5f3192012-04-30 13:31:28 +03001648 eglReleaseThread();
1649
Matt Roper361d2ad2011-08-29 13:52:23 -07001650 gbm_device_destroy(d->gbm);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001651 destroy_sprites(d);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001652 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +02001653 weston_log("failed to drop master: %m\n");
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001654 tty_destroy(d->tty);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001655
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001656 free(d);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001657}
1658
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001659static void
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001660drm_compositor_set_modes(struct drm_compositor *compositor)
1661{
1662 struct drm_output *output;
1663 struct drm_mode *drm_mode;
1664 int ret;
1665
1666 wl_list_for_each(output, &compositor->base.output_list, base.link) {
1667 drm_mode = (struct drm_mode *) output->base.current;
1668 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001669 output->current->fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001670 &output->connector_id, 1,
1671 &drm_mode->mode_info);
1672 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001673 weston_log(
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001674 "failed to set mode %dx%d for output at %d,%d: %m\n",
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001675 drm_mode->base.width, drm_mode->base.height,
1676 output->base.x, output->base.y);
1677 }
1678 }
1679}
1680
1681static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001682vt_func(struct weston_compositor *compositor, int event)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001683{
1684 struct drm_compositor *ec = (struct drm_compositor *) compositor;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001685 struct weston_output *output;
Daniel Stone37816df2012-05-16 18:45:18 +01001686 struct weston_seat *seat;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001687 struct drm_sprite *sprite;
1688 struct drm_output *drm_output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001689
1690 switch (event) {
1691 case TTY_ENTER_VT:
1692 compositor->focus = 1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001693 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001694 weston_log("failed to set master: %m\n");
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001695 wl_display_terminate(compositor->wl_display);
1696 }
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001697 compositor->state = ec->prev_state;
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001698 drm_compositor_set_modes(ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001699 weston_compositor_damage_all(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001700 wl_list_for_each(seat, &compositor->seat_list, link) {
1701 evdev_add_devices(ec->udev, seat);
1702 evdev_enable_udev_monitor(ec->udev, seat);
Benjamin Franzke78d3afe2012-04-09 18:14:58 +02001703 }
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001704 break;
1705 case TTY_LEAVE_VT:
Daniel Stone37816df2012-05-16 18:45:18 +01001706 wl_list_for_each(seat, &compositor->seat_list, link) {
1707 evdev_disable_udev_monitor(seat);
1708 evdev_remove_devices(seat);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04001709 }
1710
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001711 compositor->focus = 0;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001712 ec->prev_state = compositor->state;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001713 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04001714
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001715 /* If we have a repaint scheduled (either from a
1716 * pending pageflip or the idle handler), make sure we
1717 * cancel that so we don't try to pageflip when we're
1718 * vt switched away. The SLEEPING state will prevent
1719 * further attemps at repainting. When we switch
1720 * back, we schedule a repaint, which will process
1721 * pending frame callbacks. */
1722
1723 wl_list_for_each(output, &ec->base.output_list, link) {
1724 output->repaint_needed = 0;
1725 drm_output_set_cursor(output, NULL);
1726 }
1727
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001728 drm_output = container_of(ec->base.output_list.next,
1729 struct drm_output, base.link);
1730
1731 wl_list_for_each(sprite, &ec->sprite_list, link)
1732 drmModeSetPlane(ec->drm.fd,
1733 sprite->plane_id,
1734 drm_output->crtc_id, 0, 0,
1735 0, 0, 0, 0, 0, 0, 0, 0);
1736
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001737 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +02001738 weston_log("failed to drop master: %m\n");
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001739
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001740 break;
1741 };
1742}
1743
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001744static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001745switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001746{
1747 struct drm_compositor *ec = data;
1748
Daniel Stone325fc2d2012-05-30 16:31:58 +01001749 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001750}
1751
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001752static const char default_seat[] = "seat0";
1753
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001754static struct weston_compositor *
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001755drm_compositor_create(struct wl_display *display,
Daniel Stonebaf43592012-06-01 11:11:10 -04001756 int connector, const char *seat, int tty,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001757 int argc, char *argv[], const char *config_file)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001758{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001759 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001760 struct udev_enumerate *e;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001761 struct udev_list_entry *entry;
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001762 struct udev_device *device, *drm_device;
1763 const char *path, *device_seat;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001764 struct wl_event_loop *loop;
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001765 uint32_t key;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001766
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001767 weston_log("initializing drm backend\n");
1768
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001769 ec = malloc(sizeof *ec);
1770 if (ec == NULL)
1771 return NULL;
1772
1773 memset(ec, 0, sizeof *ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001774 ec->udev = udev_new();
1775 if (ec->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001776 weston_log("failed to initialize udev context\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001777 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001778 }
1779
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05001780 ec->base.wl_display = display;
1781 ec->tty = tty_create(&ec->base, vt_func, tty);
1782 if (!ec->tty) {
Martin Minarik6d118362012-06-07 18:01:59 +02001783 weston_log("failed to initialize tty\n");
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05001784 free(ec);
1785 return NULL;
1786 }
1787
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001788 e = udev_enumerate_new(ec->udev);
1789 udev_enumerate_add_match_subsystem(e, "drm");
Benjamin Franzkea764ee52011-10-07 08:23:22 +02001790 udev_enumerate_add_match_sysname(e, "card[0-9]*");
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001791
Benjamin Franzke117483d2011-08-30 11:38:26 +02001792 udev_enumerate_scan_devices(e);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001793 drm_device = NULL;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001794 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001795 path = udev_list_entry_get_name(entry);
1796 device = udev_device_new_from_syspath(ec->udev, path);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001797 device_seat =
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001798 udev_device_get_property_value(device, "ID_SEAT");
1799 if (!device_seat)
1800 device_seat = default_seat;
1801 if (strcmp(device_seat, seat) == 0) {
1802 drm_device = device;
1803 break;
1804 }
1805 udev_device_unref(device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001806 }
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001807
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001808 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001809 weston_log("no drm device found\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001810 return NULL;
1811 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001812
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001813 if (init_egl(ec, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001814 weston_log("failed to initialize egl\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001815 return NULL;
1816 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001817
1818 ec->base.destroy = drm_destroy;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001819
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001820 ec->base.focus = 1;
1821
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001822 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001823
Benjamin Franzked59eb1c2011-04-29 22:14:54 +02001824 /* Can't init base class until we have a current egl context */
Daniel Stonec1be8e52012-06-01 11:14:02 -04001825 if (weston_compositor_init(&ec->base, display, argc, argv,
1826 config_file) < 0)
Benjamin Franzked59eb1c2011-04-29 22:14:54 +02001827 return NULL;
1828
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001829 for (key = KEY_F1; key < KEY_F9; key++)
Daniel Stone325fc2d2012-05-30 16:31:58 +01001830 weston_compositor_add_key_binding(&ec->base, key,
1831 MODIFIER_CTRL | MODIFIER_ALT,
1832 switch_vt_binding, ec);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001833
Jesse Barnes58ef3792012-02-23 09:45:49 -05001834 wl_list_init(&ec->sprite_list);
1835 create_sprites(ec);
1836
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001837 if (create_outputs(ec, connector, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001838 weston_log("failed to create output for %s\n", path);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001839 return NULL;
1840 }
1841
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001842 udev_device_unref(drm_device);
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02001843 udev_enumerate_unref(e);
1844 path = NULL;
1845
Tiago Vignattice03ec32011-12-19 01:14:03 +02001846 evdev_input_create(&ec->base, ec->udev, seat);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001847
1848 loop = wl_display_get_event_loop(ec->base.wl_display);
1849 ec->drm_source =
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001850 wl_event_loop_add_fd(loop, ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001851 WL_EVENT_READABLE, on_drm_input, ec);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001852
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001853 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
1854 if (ec->udev_monitor == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001855 weston_log("failed to intialize udev monitor\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001856 return NULL;
1857 }
1858 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
1859 "drm", NULL);
1860 ec->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02001861 wl_event_loop_add_fd(loop,
1862 udev_monitor_get_fd(ec->udev_monitor),
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001863 WL_EVENT_READABLE, udev_drm_event, ec);
1864
1865 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001866 weston_log("failed to enable udev-monitor receiving\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001867 return NULL;
1868 }
1869
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001870 return &ec->base;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001871}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001872
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001873WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -04001874backend_init(struct wl_display *display, int argc, char *argv[],
1875 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001876{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001877 int connector = 0, tty = 0;
1878 const char *seat = default_seat;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001879
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001880 const struct weston_option drm_options[] = {
1881 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
1882 { WESTON_OPTION_STRING, "seat", 0, &seat },
1883 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
1884 };
Benjamin Franzke117483d2011-08-30 11:38:26 +02001885
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001886 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001887
Daniel Stonec1be8e52012-06-01 11:14:02 -04001888 return drm_compositor_create(display, connector, seat, tty, argc, argv,
1889 config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001890}