blob: 23b814ce779566e381a77e20e37818da832375d3 [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,
252 c->base.display, es->image,
253 es->geometry.width,
254 es->geometry.height,
255 GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500256
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300257 /* Need to verify output->region contained in surface opaque
258 * region. Or maybe just that format doesn't have alpha.
259 * For now, scanout only if format is XRGB8888. */
260 if (gbm_bo_get_format(bo) != GBM_FORMAT_XRGB8888) {
261 gbm_bo_destroy(bo);
262 return -1;
263 }
264
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300265 output->next = drm_fb_get_from_bo(bo, output);
266 if (!output->next) {
267 gbm_bo_destroy(bo);
268 return -1;
269 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500270
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300271 output->next->is_client_buffer = 1;
272 output->next->buffer = es->buffer;
273 output->next->buffer->busy_count++;
274 output->next->buffer_destroy_listener.notify = fb_handle_buffer_destroy;
275
276 wl_signal_add(&output->next->buffer->resource.destroy_signal,
277 &output->next->buffer_destroy_listener);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500278
279 pixman_region32_fini(&es->damage);
280 pixman_region32_init(&es->damage);
281
282 return 0;
283}
284
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500285static void
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400286drm_output_render(struct drm_output *output, pixman_region32_t *damage)
287{
288 struct drm_compositor *compositor =
289 (struct drm_compositor *) output->base.compositor;
290 struct weston_surface *surface;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300291 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400292
293 if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
294 output->egl_surface, compositor->base.context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200295 weston_log("failed to make current\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400296 return;
297 }
298
299 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
300 weston_surface_draw(surface, &output->base, damage);
301
Scott Moreau062be7e2012-04-20 13:37:33 -0600302 weston_output_do_read_pixels(&output->base);
303
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400304 eglSwapBuffers(compositor->base.display, output->egl_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300305 bo = gbm_surface_lock_front_buffer(output->surface);
306 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200307 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400308 return;
309 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300310
311 output->next = drm_fb_get_from_bo(bo, output);
312 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200313 weston_log("failed to get drm_fb for bo\n");
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300314 gbm_surface_release_buffer(output->surface, bo);
315 return;
316 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400317}
318
319static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500320drm_output_repaint(struct weston_output *output_base,
321 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100322{
323 struct drm_output *output = (struct drm_output *) output_base;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500324 struct drm_compositor *compositor =
325 (struct drm_compositor *) output->base.compositor;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500326 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400327 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500328 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100329
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500330 drm_output_prepare_scanout_surface(output);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300331 if (!output->next)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400332 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300333 if (!output->next)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400334 return;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100335
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400336 mode = container_of(output->base.current, struct drm_mode, base);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300337 if (!output->current) {
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400338 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300339 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400340 &output->connector_id, 1,
341 &mode->mode_info);
342 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200343 weston_log("set mode failed: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400344 return;
345 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200346 }
347
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500348 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300349 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500350 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200351 weston_log("queueing pageflip failed: %m\n");
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500352 return;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500353 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100354
Jesse Barnes58ef3792012-02-23 09:45:49 -0500355 /*
356 * Now, update all the sprite surfaces
357 */
358 wl_list_for_each(s, &compositor->sprite_list, link) {
359 uint32_t flags = 0;
360 drmVBlank vbl = {
361 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
362 .request.sequence = 1,
363 };
364
365 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
366 continue;
367
368 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
369 output->crtc_id, s->pending_fb_id, flags,
370 s->dest_x, s->dest_y,
371 s->dest_w, s->dest_h,
372 s->src_x, s->src_y,
373 s->src_w, s->src_h);
374 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200375 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500376 ret, strerror(errno));
377
378 /*
379 * Queue a vblank signal so we know when the surface
380 * becomes active on the display or has been replaced.
381 */
382 vbl.request.signal = (unsigned long)s;
383 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
384 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200385 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500386 ret, strerror(errno));
387 }
388 }
389
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500390 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400391}
392
393static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500394vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
395 void *data)
396{
397 struct drm_sprite *s = (struct drm_sprite *)data;
398 struct drm_compositor *c = s->compositor;
399
400 if (s->surface) {
401 weston_buffer_post_release(s->surface->buffer);
402 wl_list_remove(&s->destroy_listener.link);
403 s->surface = NULL;
404 drmModeRmFB(c->drm.fd, s->fb_id);
405 s->fb_id = 0;
406 }
407
408 if (s->pending_surface) {
409 wl_list_remove(&s->pending_destroy_listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400410 wl_signal_add(&s->pending_surface->buffer->resource.destroy_signal,
411 &s->destroy_listener);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500412 s->surface = s->pending_surface;
413 s->pending_surface = NULL;
414 s->fb_id = s->pending_fb_id;
415 s->pending_fb_id = 0;
416 }
417}
418
419static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400420page_flip_handler(int fd, unsigned int frame,
421 unsigned int sec, unsigned int usec, void *data)
422{
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200423 struct drm_output *output = (struct drm_output *) data;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400424 uint32_t msecs;
425
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300426 if (output->current) {
427 if (output->current->is_client_buffer)
428 gbm_bo_destroy(output->current->bo);
429 else
430 gbm_surface_release_buffer(output->surface,
431 output->current->bo);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200432 }
433
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300434 output->current = output->next;
435 output->next = NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400436
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100437 msecs = sec * 1000 + usec / 1000;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500438 weston_output_finish_frame(&output->base, msecs);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200439}
440
441static int
Jesse Barnes58ef3792012-02-23 09:45:49 -0500442drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
443{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400444 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500445
446 for (i = 0; i < s->count_formats; i++)
447 if (s->formats[i] == format)
448 return 1;
449
450 return 0;
451}
452
453static int
454drm_surface_transform_supported(struct weston_surface *es)
455{
456 if (es->transform.enabled)
457 return 0;
458
459 return 1;
460}
461
462static int
463drm_surface_overlap_supported(struct weston_output *output_base,
464 pixman_region32_t *overlap)
465{
466 /* We could potentially use a color key here if the surface left
467 * to display has rectangular regions
468 */
469 if (pixman_region32_not_empty(overlap))
470 return 0;
471 return 1;
472}
473
474static void
475drm_disable_unused_sprites(struct weston_output *output_base)
476{
477 struct weston_compositor *ec = output_base->compositor;
478 struct drm_compositor *c =(struct drm_compositor *) ec;
479 struct drm_output *output = (struct drm_output *) output_base;
480 struct drm_sprite *s;
481 int ret;
482
483 wl_list_for_each(s, &c->sprite_list, link) {
484 if (s->pending_fb_id)
485 continue;
486
487 ret = drmModeSetPlane(c->drm.fd, s->plane_id,
488 output->crtc_id, 0, 0,
489 0, 0, 0, 0, 0, 0, 0, 0);
490 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200491 weston_log("failed to disable plane: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500492 ret, strerror(errno));
493 drmModeRmFB(c->drm.fd, s->fb_id);
494 s->surface = NULL;
495 s->pending_surface = NULL;
496 s->fb_id = 0;
497 s->pending_fb_id = 0;
498 }
499}
500
501/*
502 * This function must take care to damage any previously assigned surface
503 * if the sprite ends up binding to a different surface than in the
504 * previous frame.
505 */
506static int
507drm_output_prepare_overlay_surface(struct weston_output *output_base,
508 struct weston_surface *es,
509 pixman_region32_t *overlap)
510{
511 struct weston_compositor *ec = output_base->compositor;
512 struct drm_compositor *c =(struct drm_compositor *) ec;
513 struct drm_sprite *s;
514 int found = 0;
515 EGLint handle, stride;
516 struct gbm_bo *bo;
517 uint32_t fb_id = 0;
518 uint32_t handles[4], pitches[4], offsets[4];
519 int ret = 0;
520 pixman_region32_t dest_rect, src_rect;
521 pixman_box32_t *box;
522 uint32_t format;
523
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500524 if (c->sprites_are_broken)
525 return -1;
526
Jesse Barnes58ef3792012-02-23 09:45:49 -0500527 if (surface_is_primary(ec, es))
528 return -1;
529
Kristian Høgsberg7a011ea2012-03-26 11:48:55 -0400530 if (es->image == EGL_NO_IMAGE_KHR)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500531 return -1;
532
533 if (!drm_surface_transform_supported(es))
534 return -1;
535
536 if (!drm_surface_overlap_supported(output_base, overlap))
537 return -1;
538
539 wl_list_for_each(s, &c->sprite_list, link) {
540 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
541 continue;
542
543 if (!s->pending_fb_id) {
544 found = 1;
545 break;
546 }
547 }
548
549 /* No sprites available */
550 if (!found)
551 return -1;
552
553 bo = gbm_bo_create_from_egl_image(c->gbm, c->base.display, es->image,
554 es->geometry.width, es->geometry.height,
555 GBM_BO_USE_SCANOUT);
556 format = gbm_bo_get_format(bo);
557 handle = gbm_bo_get_handle(bo).s32;
558 stride = gbm_bo_get_pitch(bo);
559
560 gbm_bo_destroy(bo);
561
562 if (!drm_surface_format_supported(s, format))
563 return -1;
564
565 if (!handle)
566 return -1;
567
568 handles[0] = handle;
569 pitches[0] = stride;
570 offsets[0] = 0;
571
572 ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height,
573 format, handles, pitches, offsets,
574 &fb_id, 0);
575 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200576 weston_log("addfb2 failed: %d\n", ret);
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500577 c->sprites_are_broken = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500578 return -1;
579 }
580
581 if (s->surface && s->surface != es) {
582 struct weston_surface *old_surf = s->surface;
583 pixman_region32_fini(&old_surf->damage);
584 pixman_region32_init_rect(&old_surf->damage,
585 old_surf->geometry.x, old_surf->geometry.y,
586 old_surf->geometry.width, old_surf->geometry.height);
587 }
588
589 s->pending_fb_id = fb_id;
590 s->pending_surface = es;
591 es->buffer->busy_count++;
592
593 /*
594 * Calculate the source & dest rects properly based on actual
595 * postion (note the caller has called weston_surface_update_transform()
596 * for us already).
597 */
598 pixman_region32_init(&dest_rect);
599 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
600 &output_base->region);
601 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
602 box = pixman_region32_extents(&dest_rect);
603 s->dest_x = box->x1;
604 s->dest_y = box->y1;
605 s->dest_w = box->x2 - box->x1;
606 s->dest_h = box->y2 - box->y1;
607 pixman_region32_fini(&dest_rect);
608
609 pixman_region32_init(&src_rect);
610 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
611 &output_base->region);
612 pixman_region32_translate(&src_rect, -es->geometry.x, -es->geometry.y);
613 box = pixman_region32_extents(&src_rect);
Rob Clark3c97b902012-03-11 19:48:43 -0500614 s->src_x = box->x1 << 16;
615 s->src_y = box->y1 << 16;
616 s->src_w = (box->x2 - box->x1) << 16;
617 s->src_h = (box->y2 - box->y1) << 16;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500618 pixman_region32_fini(&src_rect);
619
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400620 wl_signal_add(&es->buffer->resource.destroy_signal,
621 &s->pending_destroy_listener);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500622 return 0;
623}
624
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500625static int
626drm_output_set_cursor(struct weston_output *output_base,
Daniel Stone37816df2012-05-16 18:45:18 +0100627 struct weston_seat *es);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500628
629static void
630weston_output_set_cursor(struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +0100631 struct weston_seat *seat,
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500632 pixman_region32_t *overlap)
633{
634 pixman_region32_t cursor_region;
635 int prior_was_hardware;
636
Daniel Stone37816df2012-05-16 18:45:18 +0100637 if (seat->sprite == NULL)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500638 return;
639
640 pixman_region32_init(&cursor_region);
641 pixman_region32_intersect(&cursor_region,
Daniel Stone37816df2012-05-16 18:45:18 +0100642 &seat->sprite->transform.boundingbox,
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500643 &output->region);
644
645 if (!pixman_region32_not_empty(&cursor_region)) {
646 drm_output_set_cursor(output, NULL);
647 goto out;
648 }
649
Daniel Stone37816df2012-05-16 18:45:18 +0100650 prior_was_hardware = seat->hw_cursor;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500651 if (pixman_region32_not_empty(overlap) ||
Daniel Stone37816df2012-05-16 18:45:18 +0100652 drm_output_set_cursor(output, seat) < 0) {
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500653 if (prior_was_hardware) {
Daniel Stone37816df2012-05-16 18:45:18 +0100654 weston_surface_damage(seat->sprite);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500655 drm_output_set_cursor(output, NULL);
656 }
Daniel Stone37816df2012-05-16 18:45:18 +0100657 seat->hw_cursor = 0;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500658 } else {
659 if (!prior_was_hardware)
Daniel Stone37816df2012-05-16 18:45:18 +0100660 weston_surface_damage_below(seat->sprite);
661 wl_list_remove(&seat->sprite->link);
662 seat->hw_cursor = 1;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500663 }
664
665out:
666 pixman_region32_fini(&cursor_region);
667}
668
Jesse Barnes58ef3792012-02-23 09:45:49 -0500669static void
670drm_assign_planes(struct weston_output *output)
671{
672 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400673 struct weston_surface *es, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500674 pixman_region32_t overlap, surface_overlap;
Daniel Stone37816df2012-05-16 18:45:18 +0100675 struct weston_seat *seat;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500676
677 /*
678 * Find a surface for each sprite in the output using some heuristics:
679 * 1) size
680 * 2) frequency of update
681 * 3) opacity (though some hw might support alpha blending)
682 * 4) clipping (this can be fixed with color keys)
683 *
684 * The idea is to save on blitting since this should save power.
685 * If we can get a large video surface on the sprite for example,
686 * the main display surface may not need to update at all, and
687 * the client buffer can be used directly for the sprite surface
688 * as we do for flipping full screen surfaces.
689 */
690 pixman_region32_init(&overlap);
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400691 wl_list_for_each_safe(es, next, &ec->surface_list, link) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500692 /*
693 * FIXME: try to assign hw cursors here too, they're just
694 * special overlays
695 */
696 pixman_region32_init(&surface_overlap);
697 pixman_region32_intersect(&surface_overlap, &overlap,
698 &es->transform.boundingbox);
699
Daniel Stone37816df2012-05-16 18:45:18 +0100700 seat = (struct weston_seat *) ec->seat;
701 if (es == seat->sprite) {
702 weston_output_set_cursor(output, seat,
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500703 &surface_overlap);
704
Daniel Stone37816df2012-05-16 18:45:18 +0100705 if (!seat->hw_cursor)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500706 pixman_region32_union(&overlap, &overlap,
707 &es->transform.boundingbox);
708 } else if (!drm_output_prepare_overlay_surface(output, es,
709 &surface_overlap)) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500710 pixman_region32_fini(&es->damage);
711 pixman_region32_init(&es->damage);
712 } else {
713 pixman_region32_union(&overlap, &overlap,
714 &es->transform.boundingbox);
715 }
716 pixman_region32_fini(&surface_overlap);
717 }
718 pixman_region32_fini(&overlap);
719
720 drm_disable_unused_sprites(output);
721}
722
723static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500724drm_output_set_cursor(struct weston_output *output_base,
Daniel Stone37816df2012-05-16 18:45:18 +0100725 struct weston_seat *es)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200726{
727 struct drm_output *output = (struct drm_output *) output_base;
728 struct drm_compositor *c =
729 (struct drm_compositor *) output->base.compositor;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200730 EGLint handle, stride;
731 int ret = -1;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200732 struct gbm_bo *bo;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400733 uint32_t buf[64 * 64];
734 unsigned char *d, *s, *end;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200735
Daniel Stone37816df2012-05-16 18:45:18 +0100736 if (es == NULL) {
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400737 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
738 return 0;
739 }
740
Daniel Stone37816df2012-05-16 18:45:18 +0100741 if (es->sprite->buffer == NULL ||
742 !wl_buffer_is_shm(es->sprite->buffer))
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200743 goto out;
744
Daniel Stone37816df2012-05-16 18:45:18 +0100745 if (es->sprite->geometry.width > 64 ||
746 es->sprite->geometry.height > 64)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200747 goto out;
Benjamin Franzke117483d2011-08-30 11:38:26 +0200748
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400749 output->current_cursor ^= 1;
750 bo = output->cursor_bo[output->current_cursor];
Kristian Høgsberg6916d9c2012-01-18 15:11:07 -0500751 if (bo == NULL)
752 goto out;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200753
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400754 memset(buf, 0, sizeof buf);
755 d = (unsigned char *) buf;
Daniel Stone37816df2012-05-16 18:45:18 +0100756 stride = wl_shm_buffer_get_stride(es->sprite->buffer);
757 s = wl_shm_buffer_get_data(es->sprite->buffer);
758 end = s + stride * es->sprite->geometry.height;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400759 while (s < end) {
Daniel Stone37816df2012-05-16 18:45:18 +0100760 memcpy(d, s, es->sprite->geometry.width * 4);
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400761 s += stride;
762 d += 64 * 4;
763 }
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200764
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400765 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200766 goto out;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200767
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400768 handle = gbm_bo_get_handle(bo).s32;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200769 ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
770 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200771 weston_log("failed to set cursor: %s\n", strerror(-ret));
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200772 goto out;
773 }
774
775 ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
Daniel Stone37816df2012-05-16 18:45:18 +0100776 es->sprite->geometry.x - output->base.x,
777 es->sprite->geometry.y - output->base.y);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200778 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200779 weston_log("failed to move cursor: %s\n", strerror(-ret));
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200780 goto out;
781 }
782
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200783out:
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200784 if (ret)
785 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
786 return ret;
787}
788
Matt Roper361d2ad2011-08-29 13:52:23 -0700789static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500790drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700791{
792 struct drm_output *output = (struct drm_output *) output_base;
793 struct drm_compositor *c =
Benjamin Franzke117483d2011-08-30 11:38:26 +0200794 (struct drm_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700795 drmModeCrtcPtr origcrtc = output->original_crtc;
Matt Roper361d2ad2011-08-29 13:52:23 -0700796
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200797 if (output->backlight)
798 backlight_destroy(output->backlight);
799
Matt Roper361d2ad2011-08-29 13:52:23 -0700800 /* Turn off hardware cursor */
Benjamin Franzke117483d2011-08-30 11:38:26 +0200801 drm_output_set_cursor(&output->base, NULL);
Matt Roper361d2ad2011-08-29 13:52:23 -0700802
803 /* Restore original CRTC state */
804 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +0200805 origcrtc->x, origcrtc->y,
806 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -0700807 drmModeFreeCrtc(origcrtc);
808
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200809 c->crtc_allocator &= ~(1 << output->crtc_id);
810 c->connector_allocator &= ~(1 << output->connector_id);
811
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400812 eglDestroySurface(c->base.display, output->egl_surface);
813 gbm_surface_destroy(output->surface);
814
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500815 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200816 wl_list_remove(&output->base.link);
817
Matt Roper361d2ad2011-08-29 13:52:23 -0700818 free(output);
819}
820
Alex Wub7b8bda2012-04-17 17:20:48 +0800821static struct drm_mode *
822choose_mode (struct drm_output *output, struct weston_mode *target_mode)
823{
824 struct drm_mode *tmp_mode = NULL, *mode;
825
826 if (output->base.current->width == target_mode->width &&
827 output->base.current->height == target_mode->height &&
828 (output->base.current->refresh == target_mode->refresh ||
829 target_mode->refresh == 0))
830 return (struct drm_mode *)output->base.current;
831
832 wl_list_for_each(mode, &output->base.mode_list, base.link) {
833 if (mode->mode_info.hdisplay == target_mode->width &&
834 mode->mode_info.vdisplay == target_mode->height) {
835 if (mode->mode_info.vrefresh == target_mode->refresh ||
836 target_mode->refresh == 0) {
837 return mode;
838 } else if (!tmp_mode)
839 tmp_mode = mode;
840 }
841 }
842
843 return tmp_mode;
844}
845
846static int
847drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
848{
849 struct drm_output *output;
850 struct drm_mode *drm_mode;
851 int ret;
852 struct drm_compositor *ec;
853 struct gbm_surface *surface;
854 EGLSurface egl_surface;
855
856 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200857 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800858 return -1;
859 }
860
861 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200862 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800863 return -1;
864 }
865
866 ec = (struct drm_compositor *)output_base->compositor;
867 output = (struct drm_output *)output_base;
868 drm_mode = choose_mode (output, mode);
869
870 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +0200871 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +0800872 return -1;
873 } else if (&drm_mode->base == output->base.current) {
874 return 0;
875 } else if (drm_mode->base.width == output->base.current->width &&
876 drm_mode->base.height == output->base.current->height) {
877 /* only change refresh value */
878 ret = drmModeSetCrtc(ec->drm.fd,
879 output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300880 output->current->fb_id, 0, 0,
Alex Wub7b8bda2012-04-17 17:20:48 +0800881 &output->connector_id, 1, &drm_mode->mode_info);
882
883 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200884 weston_log("failed to set mode (%dx%d) %u Hz\n",
Alex Wub7b8bda2012-04-17 17:20:48 +0800885 drm_mode->base.width,
886 drm_mode->base.height,
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400887 drm_mode->base.refresh / 1000);
Alex Wub7b8bda2012-04-17 17:20:48 +0800888 ret = -1;
889 } else {
890 output->base.current->flags = 0;
891 output->base.current = &drm_mode->base;
892 drm_mode->base.flags =
893 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
894 ret = 0;
895 }
896
897 return ret;
898 }
899
900 drm_mode->base.flags =
901 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
902
903 surface = gbm_surface_create(ec->gbm,
904 drm_mode->base.width,
905 drm_mode->base.height,
906 GBM_FORMAT_XRGB8888,
907 GBM_BO_USE_SCANOUT |
908 GBM_BO_USE_RENDERING);
909 if (!surface) {
Martin Minarik6d118362012-06-07 18:01:59 +0200910 weston_log("failed to create gbm surface\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800911 return -1;
912 }
913
914 egl_surface =
915 eglCreateWindowSurface(ec->base.display,
916 ec->base.config,
917 surface, NULL);
918
919 if (egl_surface == EGL_NO_SURFACE) {
Martin Minarik6d118362012-06-07 18:01:59 +0200920 weston_log("failed to create egl surface\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800921 goto err;
922 }
923
924 ret = drmModeSetCrtc(ec->drm.fd,
925 output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300926 output->current->fb_id, 0, 0,
Alex Wub7b8bda2012-04-17 17:20:48 +0800927 &output->connector_id, 1, &drm_mode->mode_info);
928 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200929 weston_log("failed to set mode\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800930 goto err;
931 }
932
933 /* reset rendering stuff. */
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300934 if (output->current) {
935 if (output->current->is_client_buffer)
936 gbm_bo_destroy(output->current->bo);
937 else
938 gbm_surface_release_buffer(output->surface,
939 output->current->bo);
940 }
941 output->current = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +0800942
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300943 if (output->next) {
944 if (output->next->is_client_buffer)
945 gbm_bo_destroy(output->next->bo);
946 else
947 gbm_surface_release_buffer(output->surface,
948 output->next->bo);
949 }
950 output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +0800951
952 eglDestroySurface(ec->base.display, output->egl_surface);
953 gbm_surface_destroy(output->surface);
954 output->egl_surface = egl_surface;
955 output->surface = surface;
956
957 /*update output*/
958 output->base.current = &drm_mode->base;
959 output->base.dirty = 1;
960 weston_output_move(&output->base, output->base.x, output->base.y);
961 return 0;
962
963err:
964 eglDestroySurface(ec->base.display, egl_surface);
965 gbm_surface_destroy(surface);
966 return -1;
967}
968
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400969static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400970on_drm_input(int fd, uint32_t mask, void *data)
971{
972 drmEventContext evctx;
973
974 memset(&evctx, 0, sizeof evctx);
975 evctx.version = DRM_EVENT_CONTEXT_VERSION;
976 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500977 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400978 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400979
980 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400981}
982
983static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400984init_egl(struct drm_compositor *ec, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400985{
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400986 EGLint major, minor, n;
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400987 const char *filename, *sysnum;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400988 int fd;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400989 static const EGLint context_attribs[] = {
990 EGL_CONTEXT_CLIENT_VERSION, 2,
991 EGL_NONE
992 };
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400993
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400994 static const EGLint config_attribs[] = {
995 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
996 EGL_RED_SIZE, 1,
997 EGL_GREEN_SIZE, 1,
998 EGL_BLUE_SIZE, 1,
999 EGL_ALPHA_SIZE, 0,
1000 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1001 EGL_NONE
1002 };
1003
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001004 sysnum = udev_device_get_sysnum(device);
1005 if (sysnum)
1006 ec->drm.id = atoi(sysnum);
1007 if (!sysnum || ec->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001008 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001009 return -1;
1010 }
1011
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001012 filename = udev_device_get_devnode(device);
David Herrmann63ff7062011-11-05 18:46:01 +01001013 fd = open(filename, O_RDWR | O_CLOEXEC);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001014 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001015 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001016 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001017 udev_device_get_devnode(device));
1018 return -1;
1019 }
1020
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001021 ec->drm.fd = fd;
Benjamin Franzke060cf802011-04-30 09:32:11 +02001022 ec->gbm = gbm_create_device(ec->drm.fd);
1023 ec->base.display = eglGetDisplay(ec->gbm);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001024 if (ec->base.display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001025 weston_log("failed to create display\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001026 return -1;
1027 }
1028
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001029 if (!eglInitialize(ec->base.display, &major, &minor)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001030 weston_log("failed to initialize display\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001031 return -1;
1032 }
1033
Darxus55973f22010-11-22 21:24:39 -05001034 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001035 weston_log("failed to bind api EGL_OPENGL_ES_API\n");
Darxus55973f22010-11-22 21:24:39 -05001036 return -1;
1037 }
1038
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001039 if (!eglChooseConfig(ec->base.display, config_attribs,
1040 &ec->base.config, 1, &n) || n != 1) {
Martin Minarik6d118362012-06-07 18:01:59 +02001041 weston_log("failed to choose config: %d\n", n);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001042 return -1;
1043 }
1044
1045 ec->base.context = eglCreateContext(ec->base.display, ec->base.config,
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -04001046 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001047 if (ec->base.context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001048 weston_log("failed to create context\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001049 return -1;
1050 }
1051
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001052 ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10,
1053 GBM_FORMAT_XRGB8888,
1054 GBM_BO_USE_RENDERING);
1055 if (!ec->dummy_surface) {
Martin Minarik6d118362012-06-07 18:01:59 +02001056 weston_log("failed to create dummy gbm surface\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001057 return -1;
1058 }
1059
1060 ec->dummy_egl_surface =
1061 eglCreateWindowSurface(ec->base.display, ec->base.config,
1062 ec->dummy_surface, NULL);
1063 if (ec->dummy_egl_surface == EGL_NO_SURFACE) {
Martin Minarik6d118362012-06-07 18:01:59 +02001064 weston_log("failed to create egl surface\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001065 return -1;
1066 }
1067
1068 if (!eglMakeCurrent(ec->base.display, ec->dummy_egl_surface,
1069 ec->dummy_egl_surface, ec->base.context)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001070 weston_log("failed to make context current\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001071 return -1;
1072 }
1073
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001074 return 0;
1075}
1076
1077static drmModeModeInfo builtin_1024x768 = {
1078 63500, /* clock */
1079 1024, 1072, 1176, 1328, 0,
1080 768, 771, 775, 798, 0,
1081 59920,
1082 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
1083 0,
1084 "1024x768"
1085};
1086
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001087
1088static int
1089drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1090{
1091 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001092 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001093
1094 mode = malloc(sizeof *mode);
1095 if (mode == NULL)
1096 return -1;
1097
1098 mode->base.flags = 0;
1099 mode->base.width = info->hdisplay;
1100 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001101
1102 /* Calculate higher precision (mHz) refresh rate */
1103 refresh = (info->clock * 1000000LL / info->htotal +
1104 info->vtotal / 2) / info->vtotal;
1105
1106 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1107 refresh *= 2;
1108 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1109 refresh /= 2;
1110 if (info->vscan > 1)
1111 refresh /= info->vscan;
1112
1113 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001114 mode->mode_info = *info;
1115 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1116
1117 return 0;
1118}
1119
1120static int
1121drm_subpixel_to_wayland(int drm_value)
1122{
1123 switch (drm_value) {
1124 default:
1125 case DRM_MODE_SUBPIXEL_UNKNOWN:
1126 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1127 case DRM_MODE_SUBPIXEL_NONE:
1128 return WL_OUTPUT_SUBPIXEL_NONE;
1129 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1130 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1131 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1132 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1133 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1134 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1135 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1136 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1137 }
1138}
1139
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001140static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001141sprite_handle_buffer_destroy(struct wl_listener *listener, void *data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001142{
1143 struct drm_sprite *sprite =
1144 container_of(listener, struct drm_sprite,
1145 destroy_listener);
1146
1147 sprite->surface = NULL;
1148}
1149
1150static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001151sprite_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001152{
1153 struct drm_sprite *sprite =
1154 container_of(listener, struct drm_sprite,
1155 pending_destroy_listener);
1156
1157 sprite->pending_surface = NULL;
1158}
1159
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001160/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001161static uint32_t
1162drm_get_backlight(struct drm_output *output)
1163{
1164 long brightness, max_brightness, norm;
1165
1166 brightness = backlight_get_brightness(output->backlight);
1167 max_brightness = backlight_get_max_brightness(output->backlight);
1168
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001169 /* convert it on a scale of 0 to 255 */
1170 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001171
1172 return (uint32_t) norm;
1173}
1174
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001175/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001176static void
1177drm_set_backlight(struct weston_output *output_base, uint32_t value)
1178{
1179 struct drm_output *output = (struct drm_output *) output_base;
1180 long max_brightness, new_brightness;
1181
1182 if (!output->backlight)
1183 return;
1184
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001185 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001186 return;
1187
1188 max_brightness = backlight_get_max_brightness(output->backlight);
1189
1190 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001191 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001192
1193 backlight_set_brightness(output->backlight, new_brightness);
1194}
1195
1196static drmModePropertyPtr
1197drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1198{
1199 drmModePropertyPtr props;
1200 int i;
1201
1202 for (i = 0; i < connector->count_props; i++) {
1203 props = drmModeGetProperty(fd, connector->props[i]);
1204 if (!props)
1205 continue;
1206
1207 if (!strcmp(props->name, name))
1208 return props;
1209
1210 drmModeFreeProperty(props);
1211 }
1212
1213 return NULL;
1214}
1215
1216static void
1217drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1218{
1219 struct drm_output *output = (struct drm_output *) output_base;
1220 struct weston_compositor *ec = output_base->compositor;
1221 struct drm_compositor *c = (struct drm_compositor *) ec;
1222 drmModeConnectorPtr connector;
1223 drmModePropertyPtr prop;
1224
1225 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1226 if (!connector)
1227 return;
1228
1229 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1230 if (!prop) {
1231 drmModeFreeConnector(connector);
1232 return;
1233 }
1234
1235 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1236 prop->prop_id, level);
1237 drmModeFreeProperty(prop);
1238 drmModeFreeConnector(connector);
1239}
1240
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001241static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001242create_output_for_connector(struct drm_compositor *ec,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001243 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001244 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001245 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001246{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001247 struct drm_output *output;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001248 struct drm_mode *drm_mode, *next;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001249 drmModeEncoder *encoder;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001250 int i, ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001251
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001252 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001253 if (encoder == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001254 weston_log("No encoder for connector.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001255 return -1;
1256 }
1257
1258 for (i = 0; i < resources->count_crtcs; i++) {
Marty Jack13d9db22011-02-09 19:01:42 -05001259 if (encoder->possible_crtcs & (1 << i) &&
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001260 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001261 break;
1262 }
1263 if (i == resources->count_crtcs) {
Martin Minarik6d118362012-06-07 18:01:59 +02001264 weston_log("No usable crtc for encoder.\n");
David Herrmanneb8bed52011-12-08 17:05:44 +01001265 drmModeFreeEncoder(encoder);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001266 return -1;
1267 }
1268
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001269 output = malloc(sizeof *output);
David Herrmanneb8bed52011-12-08 17:05:44 +01001270 if (output == NULL) {
1271 drmModeFreeEncoder(encoder);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001272 return -1;
David Herrmanneb8bed52011-12-08 17:05:44 +01001273 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001274
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001275 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001276 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1277 output->base.make = "unknown";
1278 output->base.model = "unknown";
1279 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001280
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001281 output->crtc_id = resources->crtcs[i];
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001282 ec->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001283 output->connector_id = connector->connector_id;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001284 ec->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001285
Matt Roper361d2ad2011-08-29 13:52:23 -07001286 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001287 drmModeFreeEncoder(encoder);
Matt Roper361d2ad2011-08-29 13:52:23 -07001288
David Herrmann0f0d54e2011-12-08 17:05:45 +01001289 for (i = 0; i < connector->count_modes; i++) {
1290 ret = drm_output_add_mode(output, &connector->modes[i]);
1291 if (ret)
1292 goto err_free;
1293 }
1294
1295 if (connector->count_modes == 0) {
1296 ret = drm_output_add_mode(output, &builtin_1024x768);
1297 if (ret)
1298 goto err_free;
1299 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001300
1301 drm_mode = container_of(output->base.mode_list.next,
1302 struct drm_mode, base.link);
1303 output->base.current = &drm_mode->base;
1304 drm_mode->base.flags =
1305 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1306
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001307 output->surface = gbm_surface_create(ec->gbm,
1308 output->base.current->width,
1309 output->base.current->height,
1310 GBM_FORMAT_XRGB8888,
1311 GBM_BO_USE_SCANOUT |
1312 GBM_BO_USE_RENDERING);
1313 if (!output->surface) {
Martin Minarik6d118362012-06-07 18:01:59 +02001314 weston_log("failed to create gbm surface\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001315 goto err_free;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001316 }
1317
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001318 output->egl_surface =
1319 eglCreateWindowSurface(ec->base.display, ec->base.config,
1320 output->surface, NULL);
1321 if (output->egl_surface == EGL_NO_SURFACE) {
Martin Minarik6d118362012-06-07 18:01:59 +02001322 weston_log("failed to create egl surface\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001323 goto err_surface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001324 }
1325
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04001326 output->cursor_bo[0] =
1327 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1328 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1329 output->cursor_bo[1] =
1330 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1331 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1332
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001333 output->backlight = backlight_init(drm_device,
1334 connector->connector_type);
1335 if (output->backlight) {
1336 output->base.set_backlight = drm_set_backlight;
1337 output->base.backlight_current = drm_get_backlight(output);
1338 }
1339
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001340 weston_output_init(&output->base, &ec->base, x, y,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001341 connector->mmWidth, connector->mmHeight,
1342 WL_OUTPUT_FLIPPED);
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04001343
1344 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1345
Alex Wubd3354b2012-04-17 17:20:49 +08001346 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001347 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07001348 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001349 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001350 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08001351 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001352
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001353 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001354
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001355err_surface:
1356 gbm_surface_destroy(output->surface);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001357err_free:
1358 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1359 base.link) {
1360 wl_list_remove(&drm_mode->base.link);
1361 free(drm_mode);
1362 }
1363
1364 drmModeFreeCrtc(output->original_crtc);
1365 ec->crtc_allocator &= ~(1 << output->crtc_id);
1366 ec->connector_allocator &= ~(1 << output->connector_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001367 free(output);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001368
David Herrmann0f0d54e2011-12-08 17:05:45 +01001369 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001370}
1371
Jesse Barnes58ef3792012-02-23 09:45:49 -05001372static void
1373create_sprites(struct drm_compositor *ec)
1374{
1375 struct drm_sprite *sprite;
1376 drmModePlaneRes *plane_res;
1377 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001378 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001379
1380 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1381 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02001382 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001383 strerror(errno));
1384 return;
1385 }
1386
1387 for (i = 0; i < plane_res->count_planes; i++) {
1388 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1389 if (!plane)
1390 continue;
1391
1392 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1393 plane->count_formats));
1394 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02001395 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001396 __func__);
1397 free(plane);
1398 continue;
1399 }
1400
1401 memset(sprite, 0, sizeof *sprite);
1402
1403 sprite->possible_crtcs = plane->possible_crtcs;
1404 sprite->plane_id = plane->plane_id;
1405 sprite->surface = NULL;
1406 sprite->pending_surface = NULL;
1407 sprite->fb_id = 0;
1408 sprite->pending_fb_id = 0;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001409 sprite->destroy_listener.notify = sprite_handle_buffer_destroy;
1410 sprite->pending_destroy_listener.notify =
Jesse Barnes58ef3792012-02-23 09:45:49 -05001411 sprite_handle_pending_buffer_destroy;
1412 sprite->compositor = ec;
1413 sprite->count_formats = plane->count_formats;
1414 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05001415 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05001416 drmModeFreePlane(plane);
1417
1418 wl_list_insert(&ec->sprite_list, &sprite->link);
1419 }
1420
1421 free(plane_res->planes);
1422 free(plane_res);
1423}
1424
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001425static void
1426destroy_sprites(struct drm_compositor *compositor)
1427{
1428 struct drm_sprite *sprite, *next;
1429 struct drm_output *output;
1430
1431 output = container_of(compositor->base.output_list.next,
1432 struct drm_output, base.link);
1433
1434 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1435 drmModeSetPlane(compositor->drm.fd,
1436 sprite->plane_id,
1437 output->crtc_id, 0, 0,
1438 0, 0, 0, 0, 0, 0, 0, 0);
1439 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1440 free(sprite);
1441 }
1442}
Jesse Barnes58ef3792012-02-23 09:45:49 -05001443
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001444static int
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001445create_outputs(struct drm_compositor *ec, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001446 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001447{
1448 drmModeConnector *connector;
1449 drmModeRes *resources;
1450 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001451 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001452
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001453 resources = drmModeGetResources(ec->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001454 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02001455 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001456 return -1;
1457 }
1458
Jesse Barnes58ef3792012-02-23 09:45:49 -05001459 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001460 if (!ec->crtcs) {
1461 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001462 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001463 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001464
1465 ec->num_crtcs = resources->count_crtcs;
1466 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1467
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001468 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001469 connector = drmModeGetConnector(ec->drm.fd,
1470 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001471 if (connector == NULL)
1472 continue;
1473
1474 if (connector->connection == DRM_MODE_CONNECTED &&
1475 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001476 connector->connector_id == option_connector)) {
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001477 if (create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001478 connector, x, y,
1479 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02001480 drmModeFreeConnector(connector);
1481 continue;
1482 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001483
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001484 x += container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001485 struct weston_output,
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001486 link)->current->width;
1487 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001488
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001489 drmModeFreeConnector(connector);
1490 }
1491
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001492 if (wl_list_empty(&ec->base.output_list)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001493 weston_log("No currently active connector found.\n");
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001494 drmModeFreeResources(resources);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001495 return -1;
1496 }
1497
1498 drmModeFreeResources(resources);
1499
1500 return 0;
1501}
1502
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001503static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001504update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001505{
1506 drmModeConnector *connector;
1507 drmModeRes *resources;
1508 struct drm_output *output, *next;
1509 int x = 0, y = 0;
1510 int x_offset = 0, y_offset = 0;
1511 uint32_t connected = 0, disconnects = 0;
1512 int i;
1513
1514 resources = drmModeGetResources(ec->drm.fd);
1515 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02001516 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001517 return;
1518 }
1519
1520 /* collect new connects */
1521 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001522 int connector_id = resources->connectors[i];
1523
1524 connector = drmModeGetConnector(ec->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01001525 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001526 continue;
1527
David Herrmann7551cff2011-12-08 17:05:43 +01001528 if (connector->connection != DRM_MODE_CONNECTED) {
1529 drmModeFreeConnector(connector);
1530 continue;
1531 }
1532
Benjamin Franzke117483d2011-08-30 11:38:26 +02001533 connected |= (1 << connector_id);
1534
1535 if (!(ec->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001536 struct weston_output *last =
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001537 container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001538 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001539
1540 /* XXX: not yet needed, we die with 0 outputs */
1541 if (!wl_list_empty(&ec->base.output_list))
Benjamin Franzke117483d2011-08-30 11:38:26 +02001542 x = last->x + last->current->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001543 else
1544 x = 0;
1545 y = 0;
1546 create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001547 connector, x, y,
1548 drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02001549 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001550
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001551 }
1552 drmModeFreeConnector(connector);
1553 }
1554 drmModeFreeResources(resources);
1555
1556 disconnects = ec->connector_allocator & ~connected;
1557 if (disconnects) {
1558 wl_list_for_each_safe(output, next, &ec->base.output_list,
1559 base.link) {
1560 if (x_offset != 0 || y_offset != 0) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001561 weston_output_move(&output->base,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001562 output->base.x - x_offset,
1563 output->base.y - y_offset);
1564 }
1565
1566 if (disconnects & (1 << output->connector_id)) {
1567 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02001568 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001569 output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001570 x_offset += output->base.current->width;
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001571 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001572 }
1573 }
1574 }
1575
1576 /* FIXME: handle zero outputs, without terminating */
1577 if (ec->connector_allocator == 0)
1578 wl_display_terminate(ec->base.wl_display);
1579}
1580
1581static int
David Herrmannd7488c22012-03-11 20:05:21 +01001582udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001583{
David Herrmannd7488c22012-03-11 20:05:21 +01001584 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01001585 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01001586
1587 sysnum = udev_device_get_sysnum(device);
1588 if (!sysnum || atoi(sysnum) != ec->drm.id)
1589 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001590
David Herrmann6ac52db2012-03-11 20:05:22 +01001591 val = udev_device_get_property_value(device, "HOTPLUG");
1592 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001593 return 0;
1594
David Herrmann6ac52db2012-03-11 20:05:22 +01001595 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001596}
1597
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001598static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001599udev_drm_event(int fd, uint32_t mask, void *data)
1600{
1601 struct drm_compositor *ec = data;
1602 struct udev_device *event;
1603
1604 event = udev_monitor_receive_device(ec->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001605
David Herrmannd7488c22012-03-11 20:05:21 +01001606 if (udev_event_is_hotplug(ec, event))
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001607 update_outputs(ec, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001608
1609 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001610
1611 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001612}
1613
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001614static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001615drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001616{
1617 struct drm_compositor *d = (struct drm_compositor *) ec;
Daniel Stone37816df2012-05-16 18:45:18 +01001618 struct weston_seat *seat, *next;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001619
Daniel Stone37816df2012-05-16 18:45:18 +01001620 wl_list_for_each_safe(seat, next, &ec->seat_list, link)
1621 evdev_input_destroy(seat);
Jonas Ådahlc97af922012-03-28 22:36:09 +02001622
1623 wl_event_source_remove(d->udev_drm_source);
1624 wl_event_source_remove(d->drm_source);
1625
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001626 weston_compositor_shutdown(ec);
Jonas Ådahlc97af922012-03-28 22:36:09 +02001627
Ander Conselvan de Oliveira5f5f3192012-04-30 13:31:28 +03001628 /* Work around crash in egl_dri2.c's dri2_make_current() */
1629 eglMakeCurrent(ec->display, EGL_NO_SURFACE, EGL_NO_SURFACE,
1630 EGL_NO_CONTEXT);
1631 eglTerminate(ec->display);
1632 eglReleaseThread();
1633
Matt Roper361d2ad2011-08-29 13:52:23 -07001634 gbm_device_destroy(d->gbm);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001635 destroy_sprites(d);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001636 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +02001637 weston_log("failed to drop master: %m\n");
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001638 tty_destroy(d->tty);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001639
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001640 free(d);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001641}
1642
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001643static void
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001644drm_compositor_set_modes(struct drm_compositor *compositor)
1645{
1646 struct drm_output *output;
1647 struct drm_mode *drm_mode;
1648 int ret;
1649
1650 wl_list_for_each(output, &compositor->base.output_list, base.link) {
1651 drm_mode = (struct drm_mode *) output->base.current;
1652 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001653 output->current->fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001654 &output->connector_id, 1,
1655 &drm_mode->mode_info);
1656 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001657 weston_log(
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001658 "failed to set mode %dx%d for output at %d,%d: %m\n",
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001659 drm_mode->base.width, drm_mode->base.height,
1660 output->base.x, output->base.y);
1661 }
1662 }
1663}
1664
1665static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001666vt_func(struct weston_compositor *compositor, int event)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001667{
1668 struct drm_compositor *ec = (struct drm_compositor *) compositor;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001669 struct weston_output *output;
Daniel Stone37816df2012-05-16 18:45:18 +01001670 struct weston_seat *seat;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001671 struct drm_sprite *sprite;
1672 struct drm_output *drm_output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001673
1674 switch (event) {
1675 case TTY_ENTER_VT:
1676 compositor->focus = 1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001677 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001678 weston_log("failed to set master: %m\n");
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001679 wl_display_terminate(compositor->wl_display);
1680 }
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001681 compositor->state = ec->prev_state;
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001682 drm_compositor_set_modes(ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001683 weston_compositor_damage_all(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001684 wl_list_for_each(seat, &compositor->seat_list, link) {
1685 evdev_add_devices(ec->udev, seat);
1686 evdev_enable_udev_monitor(ec->udev, seat);
Benjamin Franzke78d3afe2012-04-09 18:14:58 +02001687 }
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001688 break;
1689 case TTY_LEAVE_VT:
Daniel Stone37816df2012-05-16 18:45:18 +01001690 wl_list_for_each(seat, &compositor->seat_list, link) {
1691 evdev_disable_udev_monitor(seat);
1692 evdev_remove_devices(seat);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04001693 }
1694
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001695 compositor->focus = 0;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001696 ec->prev_state = compositor->state;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001697 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04001698
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001699 /* If we have a repaint scheduled (either from a
1700 * pending pageflip or the idle handler), make sure we
1701 * cancel that so we don't try to pageflip when we're
1702 * vt switched away. The SLEEPING state will prevent
1703 * further attemps at repainting. When we switch
1704 * back, we schedule a repaint, which will process
1705 * pending frame callbacks. */
1706
1707 wl_list_for_each(output, &ec->base.output_list, link) {
1708 output->repaint_needed = 0;
1709 drm_output_set_cursor(output, NULL);
1710 }
1711
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001712 drm_output = container_of(ec->base.output_list.next,
1713 struct drm_output, base.link);
1714
1715 wl_list_for_each(sprite, &ec->sprite_list, link)
1716 drmModeSetPlane(ec->drm.fd,
1717 sprite->plane_id,
1718 drm_output->crtc_id, 0, 0,
1719 0, 0, 0, 0, 0, 0, 0, 0);
1720
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001721 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +02001722 weston_log("failed to drop master: %m\n");
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001723
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001724 break;
1725 };
1726}
1727
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001728static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001729switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001730{
1731 struct drm_compositor *ec = data;
1732
Daniel Stone325fc2d2012-05-30 16:31:58 +01001733 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001734}
1735
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001736static const char default_seat[] = "seat0";
1737
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001738static struct weston_compositor *
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001739drm_compositor_create(struct wl_display *display,
Daniel Stonebaf43592012-06-01 11:11:10 -04001740 int connector, const char *seat, int tty,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001741 int argc, char *argv[], const char *config_file)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001742{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001743 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001744 struct udev_enumerate *e;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001745 struct udev_list_entry *entry;
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001746 struct udev_device *device, *drm_device;
1747 const char *path, *device_seat;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001748 struct wl_event_loop *loop;
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001749 uint32_t key;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001750
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001751 ec = malloc(sizeof *ec);
1752 if (ec == NULL)
1753 return NULL;
1754
1755 memset(ec, 0, sizeof *ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001756 ec->udev = udev_new();
1757 if (ec->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001758 weston_log("failed to initialize udev context\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001759 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001760 }
1761
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05001762 ec->base.wl_display = display;
1763 ec->tty = tty_create(&ec->base, vt_func, tty);
1764 if (!ec->tty) {
Martin Minarik6d118362012-06-07 18:01:59 +02001765 weston_log("failed to initialize tty\n");
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05001766 free(ec);
1767 return NULL;
1768 }
1769
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001770 e = udev_enumerate_new(ec->udev);
1771 udev_enumerate_add_match_subsystem(e, "drm");
Benjamin Franzkea764ee52011-10-07 08:23:22 +02001772 udev_enumerate_add_match_sysname(e, "card[0-9]*");
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001773
Benjamin Franzke117483d2011-08-30 11:38:26 +02001774 udev_enumerate_scan_devices(e);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001775 drm_device = NULL;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001776 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001777 path = udev_list_entry_get_name(entry);
1778 device = udev_device_new_from_syspath(ec->udev, path);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001779 device_seat =
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001780 udev_device_get_property_value(device, "ID_SEAT");
1781 if (!device_seat)
1782 device_seat = default_seat;
1783 if (strcmp(device_seat, seat) == 0) {
1784 drm_device = device;
1785 break;
1786 }
1787 udev_device_unref(device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001788 }
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001789
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001790 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001791 weston_log("no drm device found\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001792 return NULL;
1793 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001794
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001795 if (init_egl(ec, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001796 weston_log("failed to initialize egl\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001797 return NULL;
1798 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001799
1800 ec->base.destroy = drm_destroy;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001801
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001802 ec->base.focus = 1;
1803
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001804 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001805
Benjamin Franzked59eb1c2011-04-29 22:14:54 +02001806 /* Can't init base class until we have a current egl context */
Daniel Stonec1be8e52012-06-01 11:14:02 -04001807 if (weston_compositor_init(&ec->base, display, argc, argv,
1808 config_file) < 0)
Benjamin Franzked59eb1c2011-04-29 22:14:54 +02001809 return NULL;
1810
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001811 for (key = KEY_F1; key < KEY_F9; key++)
Daniel Stone325fc2d2012-05-30 16:31:58 +01001812 weston_compositor_add_key_binding(&ec->base, key,
1813 MODIFIER_CTRL | MODIFIER_ALT,
1814 switch_vt_binding, ec);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001815
Jesse Barnes58ef3792012-02-23 09:45:49 -05001816 wl_list_init(&ec->sprite_list);
1817 create_sprites(ec);
1818
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001819 if (create_outputs(ec, connector, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001820 weston_log("failed to create output for %s\n", path);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001821 return NULL;
1822 }
1823
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001824 udev_device_unref(drm_device);
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02001825 udev_enumerate_unref(e);
1826 path = NULL;
1827
Tiago Vignattice03ec32011-12-19 01:14:03 +02001828 evdev_input_create(&ec->base, ec->udev, seat);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001829
1830 loop = wl_display_get_event_loop(ec->base.wl_display);
1831 ec->drm_source =
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001832 wl_event_loop_add_fd(loop, ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001833 WL_EVENT_READABLE, on_drm_input, ec);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001834
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001835 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
1836 if (ec->udev_monitor == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001837 weston_log("failed to intialize udev monitor\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001838 return NULL;
1839 }
1840 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
1841 "drm", NULL);
1842 ec->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02001843 wl_event_loop_add_fd(loop,
1844 udev_monitor_get_fd(ec->udev_monitor),
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001845 WL_EVENT_READABLE, udev_drm_event, ec);
1846
1847 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001848 weston_log("failed to enable udev-monitor receiving\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001849 return NULL;
1850 }
1851
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001852 return &ec->base;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001853}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001854
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001855WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -04001856backend_init(struct wl_display *display, int argc, char *argv[],
1857 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001858{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001859 int connector = 0, tty = 0;
1860 const char *seat = default_seat;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001861
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001862 const struct weston_option drm_options[] = {
1863 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
1864 { WESTON_OPTION_STRING, "seat", 0, &seat },
1865 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
1866 };
Benjamin Franzke117483d2011-08-30 11:38:26 +02001867
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001868 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001869
Daniel Stonec1be8e52012-06-01 11:14:02 -04001870 return drm_compositor_create(display, connector, seat, tty, argc, argv,
1871 config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001872}