blob: 42d971f31211ce4d3fd2169cbbc6b5a026136d70 [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øgsbergd553bfc2012-06-18 22:37:35 -040045enum {
46 WESTON_PLANE_DRM_CURSOR = 0x100
47};
48
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040049struct drm_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050050 struct weston_compositor base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040051
52 struct udev *udev;
53 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040054
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010055 struct udev_monitor *udev_monitor;
56 struct wl_event_source *udev_drm_source;
57
Benjamin Franzke2af7f102011-03-02 11:14:59 +010058 struct {
David Herrmannd7488c22012-03-11 20:05:21 +010059 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010060 int fd;
61 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +020062 struct gbm_device *gbm;
Jesse Barnes58ef3792012-02-23 09:45:49 -050063 uint32_t *crtcs;
64 int num_crtcs;
Marty Jack13d9db22011-02-09 19:01:42 -050065 uint32_t crtc_allocator;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010066 uint32_t connector_allocator;
Kristian Høgsberge4762a62011-01-14 14:59:13 -050067 struct tty *tty;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +020068
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -040069 struct gbm_surface *dummy_surface;
70 EGLSurface dummy_egl_surface;
71
Jesse Barnes58ef3792012-02-23 09:45:49 -050072 struct wl_list sprite_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -050073 int sprites_are_broken;
Jesse Barnes58ef3792012-02-23 09:45:49 -050074
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +020075 uint32_t prev_state;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040076};
77
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040078struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050079 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040080 drmModeModeInfo mode_info;
81};
82
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +030083struct drm_output;
84
85struct drm_fb {
86 struct gbm_bo *bo;
87 struct drm_output *output;
88 uint32_t fb_id;
89 int is_client_buffer;
90 struct wl_buffer *buffer;
91 struct wl_listener buffer_destroy_listener;
92};
93
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040094struct drm_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050095 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040096
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040097 uint32_t crtc_id;
98 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -070099 drmModeCrtcPtr original_crtc;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200100
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400101 struct gbm_surface *surface;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400102 struct gbm_bo *cursor_bo[2];
103 int current_cursor;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400104 EGLSurface egl_surface;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300105 struct drm_fb *current, *next;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200106 struct backlight *backlight;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400107};
108
Jesse Barnes58ef3792012-02-23 09:45:49 -0500109/*
110 * An output has a primary display plane plus zero or more sprites for
111 * blending display contents.
112 */
113struct drm_sprite {
114 struct wl_list link;
115
116 uint32_t fb_id;
117 uint32_t pending_fb_id;
118 struct weston_surface *surface;
119 struct weston_surface *pending_surface;
120
121 struct drm_compositor *compositor;
122
123 struct wl_listener destroy_listener;
124 struct wl_listener pending_destroy_listener;
125
126 uint32_t possible_crtcs;
127 uint32_t plane_id;
128 uint32_t count_formats;
129
130 int32_t src_x, src_y;
131 uint32_t src_w, src_h;
132 uint32_t dest_x, dest_y;
133 uint32_t dest_w, dest_h;
134
135 uint32_t formats[];
136};
137
138static int
139surface_is_primary(struct weston_compositor *ec, struct weston_surface *es)
140{
141 struct weston_surface *primary;
142
143 primary = container_of(ec->surface_list.next, struct weston_surface,
144 link);
145 if (es == primary)
146 return -1;
147 return 0;
148}
149
150static int
151drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
152{
153 struct weston_compositor *ec = output_base->compositor;
154 struct drm_compositor *c =(struct drm_compositor *) ec;
155 struct drm_output *output = (struct drm_output *) output_base;
156 int crtc;
157
158 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
159 if (c->crtcs[crtc] != output->crtc_id)
160 continue;
161
162 if (supported & (1 << crtc))
163 return -1;
164 }
165
166 return 0;
167}
168
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300169static void
170drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
171{
172 struct drm_fb *fb = data;
173 struct gbm_device *gbm = gbm_bo_get_device(bo);
174
175 if (fb->fb_id)
176 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
177
178 if (fb->buffer) {
179 weston_buffer_post_release(fb->buffer);
180 wl_list_remove(&fb->buffer_destroy_listener.link);
181 }
182
183 free(data);
184}
185
186static struct drm_fb *
187drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_output *output)
188{
189 struct drm_fb *fb = gbm_bo_get_user_data(bo);
190 struct drm_compositor *compositor =
191 (struct drm_compositor *) output->base.compositor;
192 uint32_t width, height, stride, handle;
193 int ret;
194
195 if (fb)
196 return fb;
197
198 fb = malloc(sizeof *fb);
199
200 fb->bo = bo;
201 fb->output = output;
202 fb->is_client_buffer = 0;
203 fb->buffer = NULL;
204
205 width = gbm_bo_get_width(bo);
206 height = gbm_bo_get_height(bo);
207 stride = gbm_bo_get_pitch(bo);
208 handle = gbm_bo_get_handle(bo).u32;
209
210 ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32,
211 stride, handle, &fb->fb_id);
212 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200213 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300214 free(fb);
215 return NULL;
216 }
217
218 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
219
220 return fb;
221}
222
223static void
224fb_handle_buffer_destroy(struct wl_listener *listener, void *data)
225{
226 struct drm_fb *fb = container_of(listener, struct drm_fb,
227 buffer_destroy_listener);
228
229 fb->buffer = NULL;
230
231 if (fb == fb->output->next ||
232 (fb == fb->output->current && !fb->output->next))
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400233 weston_output_schedule_repaint(&fb->output->base);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300234}
235
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500236static int
237drm_output_prepare_scanout_surface(struct drm_output *output)
238{
239 struct drm_compositor *c =
240 (struct drm_compositor *) output->base.compositor;
241 struct weston_surface *es;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300242 struct gbm_bo *bo;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500243
244 es = container_of(c->base.surface_list.next,
245 struct weston_surface, link);
246
Kristian Høgsberg101cb652012-02-17 10:45:16 -0500247 if (es->geometry.x != output->base.x ||
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200248 es->geometry.y != output->base.y ||
Pekka Paalanen60921e52012-01-25 15:55:43 +0200249 es->geometry.width != output->base.current->width ||
250 es->geometry.height != output->base.current->height ||
Pekka Paalanenf1f5b362012-01-25 14:33:33 +0200251 es->transform.enabled ||
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500252 es->image == EGL_NO_IMAGE_KHR)
253 return -1;
254
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300255 bo = gbm_bo_create_from_egl_image(c->gbm,
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400256 c->base.egl_display,
257 es->image,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300258 es->geometry.width,
259 es->geometry.height,
260 GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500261
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300262 /* Need to verify output->region contained in surface opaque
263 * region. Or maybe just that format doesn't have alpha.
264 * For now, scanout only if format is XRGB8888. */
265 if (gbm_bo_get_format(bo) != GBM_FORMAT_XRGB8888) {
266 gbm_bo_destroy(bo);
267 return -1;
268 }
269
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300270 output->next = drm_fb_get_from_bo(bo, output);
271 if (!output->next) {
272 gbm_bo_destroy(bo);
273 return -1;
274 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500275
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300276 output->next->is_client_buffer = 1;
277 output->next->buffer = es->buffer;
278 output->next->buffer->busy_count++;
279 output->next->buffer_destroy_listener.notify = fb_handle_buffer_destroy;
280
281 wl_signal_add(&output->next->buffer->resource.destroy_signal,
282 &output->next->buffer_destroy_listener);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500283
284 pixman_region32_fini(&es->damage);
285 pixman_region32_init(&es->damage);
286
287 return 0;
288}
289
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500290static void
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400291drm_output_render(struct drm_output *output, pixman_region32_t *damage)
292{
293 struct drm_compositor *compositor =
294 (struct drm_compositor *) output->base.compositor;
295 struct weston_surface *surface;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300296 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400297
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400298 if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
299 output->egl_surface,
300 compositor->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200301 weston_log("failed to make current\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400302 return;
303 }
304
305 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
306 weston_surface_draw(surface, &output->base, damage);
307
Kristian Høgsberge0f832b2012-06-20 00:13:18 -0400308 wl_signal_emit(&output->base.frame_signal, output);
Scott Moreau062be7e2012-04-20 13:37:33 -0600309
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400310 eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300311 bo = gbm_surface_lock_front_buffer(output->surface);
312 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200313 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400314 return;
315 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300316
317 output->next = drm_fb_get_from_bo(bo, output);
318 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200319 weston_log("failed to get drm_fb for bo\n");
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300320 gbm_surface_release_buffer(output->surface, bo);
321 return;
322 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400323}
324
325static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500326drm_output_repaint(struct weston_output *output_base,
327 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100328{
329 struct drm_output *output = (struct drm_output *) output_base;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500330 struct drm_compositor *compositor =
331 (struct drm_compositor *) output->base.compositor;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500332 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400333 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500334 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100335
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500336 drm_output_prepare_scanout_surface(output);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300337 if (!output->next)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400338 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300339 if (!output->next)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400340 return;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100341
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400342 mode = container_of(output->base.current, struct drm_mode, base);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300343 if (!output->current) {
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400344 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300345 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400346 &output->connector_id, 1,
347 &mode->mode_info);
348 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200349 weston_log("set mode failed: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400350 return;
351 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200352 }
353
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500354 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300355 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500356 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200357 weston_log("queueing pageflip failed: %m\n");
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500358 return;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500359 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100360
Jesse Barnes58ef3792012-02-23 09:45:49 -0500361 /*
362 * Now, update all the sprite surfaces
363 */
364 wl_list_for_each(s, &compositor->sprite_list, link) {
365 uint32_t flags = 0;
366 drmVBlank vbl = {
367 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
368 .request.sequence = 1,
369 };
370
371 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
372 continue;
373
374 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
375 output->crtc_id, s->pending_fb_id, flags,
376 s->dest_x, s->dest_y,
377 s->dest_w, s->dest_h,
378 s->src_x, s->src_y,
379 s->src_w, s->src_h);
380 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200381 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500382 ret, strerror(errno));
383
384 /*
385 * Queue a vblank signal so we know when the surface
386 * becomes active on the display or has been replaced.
387 */
388 vbl.request.signal = (unsigned long)s;
389 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
390 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200391 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500392 ret, strerror(errno));
393 }
394 }
395
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500396 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400397}
398
399static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500400vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
401 void *data)
402{
403 struct drm_sprite *s = (struct drm_sprite *)data;
404 struct drm_compositor *c = s->compositor;
405
406 if (s->surface) {
407 weston_buffer_post_release(s->surface->buffer);
408 wl_list_remove(&s->destroy_listener.link);
409 s->surface = NULL;
410 drmModeRmFB(c->drm.fd, s->fb_id);
411 s->fb_id = 0;
412 }
413
414 if (s->pending_surface) {
415 wl_list_remove(&s->pending_destroy_listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400416 wl_signal_add(&s->pending_surface->buffer->resource.destroy_signal,
417 &s->destroy_listener);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500418 s->surface = s->pending_surface;
419 s->pending_surface = NULL;
420 s->fb_id = s->pending_fb_id;
421 s->pending_fb_id = 0;
422 }
423}
424
425static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400426page_flip_handler(int fd, unsigned int frame,
427 unsigned int sec, unsigned int usec, void *data)
428{
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200429 struct drm_output *output = (struct drm_output *) data;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400430 uint32_t msecs;
431
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300432 if (output->current) {
433 if (output->current->is_client_buffer)
434 gbm_bo_destroy(output->current->bo);
435 else
436 gbm_surface_release_buffer(output->surface,
437 output->current->bo);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200438 }
439
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300440 output->current = output->next;
441 output->next = NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400442
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100443 msecs = sec * 1000 + usec / 1000;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500444 weston_output_finish_frame(&output->base, msecs);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200445}
446
447static int
Jesse Barnes58ef3792012-02-23 09:45:49 -0500448drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
449{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400450 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500451
452 for (i = 0; i < s->count_formats; i++)
453 if (s->formats[i] == format)
454 return 1;
455
456 return 0;
457}
458
459static int
460drm_surface_transform_supported(struct weston_surface *es)
461{
462 if (es->transform.enabled)
463 return 0;
464
465 return 1;
466}
467
468static int
469drm_surface_overlap_supported(struct weston_output *output_base,
470 pixman_region32_t *overlap)
471{
472 /* We could potentially use a color key here if the surface left
473 * to display has rectangular regions
474 */
475 if (pixman_region32_not_empty(overlap))
476 return 0;
477 return 1;
478}
479
480static void
481drm_disable_unused_sprites(struct weston_output *output_base)
482{
483 struct weston_compositor *ec = output_base->compositor;
484 struct drm_compositor *c =(struct drm_compositor *) ec;
485 struct drm_output *output = (struct drm_output *) output_base;
486 struct drm_sprite *s;
487 int ret;
488
489 wl_list_for_each(s, &c->sprite_list, link) {
490 if (s->pending_fb_id)
491 continue;
492
493 ret = drmModeSetPlane(c->drm.fd, s->plane_id,
494 output->crtc_id, 0, 0,
495 0, 0, 0, 0, 0, 0, 0, 0);
496 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200497 weston_log("failed to disable plane: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500498 ret, strerror(errno));
499 drmModeRmFB(c->drm.fd, s->fb_id);
500 s->surface = NULL;
501 s->pending_surface = NULL;
502 s->fb_id = 0;
503 s->pending_fb_id = 0;
504 }
505}
506
507/*
508 * This function must take care to damage any previously assigned surface
509 * if the sprite ends up binding to a different surface than in the
510 * previous frame.
511 */
512static int
513drm_output_prepare_overlay_surface(struct weston_output *output_base,
514 struct weston_surface *es,
515 pixman_region32_t *overlap)
516{
517 struct weston_compositor *ec = output_base->compositor;
518 struct drm_compositor *c =(struct drm_compositor *) ec;
519 struct drm_sprite *s;
520 int found = 0;
521 EGLint handle, stride;
522 struct gbm_bo *bo;
523 uint32_t fb_id = 0;
524 uint32_t handles[4], pitches[4], offsets[4];
525 int ret = 0;
526 pixman_region32_t dest_rect, src_rect;
527 pixman_box32_t *box;
528 uint32_t format;
529
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500530 if (c->sprites_are_broken)
531 return -1;
532
Jesse Barnes58ef3792012-02-23 09:45:49 -0500533 if (surface_is_primary(ec, es))
534 return -1;
535
Kristian Høgsberg7a011ea2012-03-26 11:48:55 -0400536 if (es->image == EGL_NO_IMAGE_KHR)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500537 return -1;
538
539 if (!drm_surface_transform_supported(es))
540 return -1;
541
542 if (!drm_surface_overlap_supported(output_base, overlap))
543 return -1;
544
545 wl_list_for_each(s, &c->sprite_list, link) {
546 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
547 continue;
548
549 if (!s->pending_fb_id) {
550 found = 1;
551 break;
552 }
553 }
554
555 /* No sprites available */
556 if (!found)
557 return -1;
558
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400559 bo = gbm_bo_create_from_egl_image(c->gbm, c->base.egl_display,
560 es->image, es->geometry.width,
561 es->geometry.height,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500562 GBM_BO_USE_SCANOUT);
563 format = gbm_bo_get_format(bo);
564 handle = gbm_bo_get_handle(bo).s32;
565 stride = gbm_bo_get_pitch(bo);
566
567 gbm_bo_destroy(bo);
568
569 if (!drm_surface_format_supported(s, format))
570 return -1;
571
572 if (!handle)
573 return -1;
574
575 handles[0] = handle;
576 pitches[0] = stride;
577 offsets[0] = 0;
578
579 ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height,
580 format, handles, pitches, offsets,
581 &fb_id, 0);
582 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200583 weston_log("addfb2 failed: %d\n", ret);
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500584 c->sprites_are_broken = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500585 return -1;
586 }
587
588 if (s->surface && s->surface != es) {
589 struct weston_surface *old_surf = s->surface;
590 pixman_region32_fini(&old_surf->damage);
591 pixman_region32_init_rect(&old_surf->damage,
592 old_surf->geometry.x, old_surf->geometry.y,
593 old_surf->geometry.width, old_surf->geometry.height);
594 }
595
596 s->pending_fb_id = fb_id;
597 s->pending_surface = es;
598 es->buffer->busy_count++;
599
600 /*
601 * Calculate the source & dest rects properly based on actual
602 * postion (note the caller has called weston_surface_update_transform()
603 * for us already).
604 */
605 pixman_region32_init(&dest_rect);
606 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
607 &output_base->region);
608 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
609 box = pixman_region32_extents(&dest_rect);
610 s->dest_x = box->x1;
611 s->dest_y = box->y1;
612 s->dest_w = box->x2 - box->x1;
613 s->dest_h = box->y2 - box->y1;
614 pixman_region32_fini(&dest_rect);
615
616 pixman_region32_init(&src_rect);
617 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
618 &output_base->region);
619 pixman_region32_translate(&src_rect, -es->geometry.x, -es->geometry.y);
620 box = pixman_region32_extents(&src_rect);
Rob Clark3c97b902012-03-11 19:48:43 -0500621 s->src_x = box->x1 << 16;
622 s->src_y = box->y1 << 16;
623 s->src_w = (box->x2 - box->x1) << 16;
624 s->src_h = (box->y2 - box->y1) << 16;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500625 pixman_region32_fini(&src_rect);
626
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400627 wl_signal_add(&es->buffer->resource.destroy_signal,
628 &s->pending_destroy_listener);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500629 return 0;
630}
631
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500632static int
633drm_output_set_cursor(struct weston_output *output_base,
Daniel Stone37816df2012-05-16 18:45:18 +0100634 struct weston_seat *es);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500635
636static void
637weston_output_set_cursor(struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +0100638 struct weston_seat *seat,
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500639 pixman_region32_t *overlap)
640{
641 pixman_region32_t cursor_region;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500642
Daniel Stone37816df2012-05-16 18:45:18 +0100643 if (seat->sprite == NULL)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500644 return;
645
646 pixman_region32_init(&cursor_region);
647 pixman_region32_intersect(&cursor_region,
Daniel Stone37816df2012-05-16 18:45:18 +0100648 &seat->sprite->transform.boundingbox,
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500649 &output->region);
650
651 if (!pixman_region32_not_empty(&cursor_region)) {
652 drm_output_set_cursor(output, NULL);
653 goto out;
654 }
655
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500656 if (pixman_region32_not_empty(overlap) ||
Daniel Stone37816df2012-05-16 18:45:18 +0100657 drm_output_set_cursor(output, seat) < 0) {
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -0400658 if (seat->sprite->plane == WESTON_PLANE_DRM_CURSOR) {
Daniel Stone37816df2012-05-16 18:45:18 +0100659 weston_surface_damage(seat->sprite);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500660 drm_output_set_cursor(output, NULL);
661 }
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -0400662 seat->sprite->plane = WESTON_PLANE_PRIMARY;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500663 } else {
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -0400664 if (seat->sprite->plane == WESTON_PLANE_PRIMARY)
Daniel Stone37816df2012-05-16 18:45:18 +0100665 weston_surface_damage_below(seat->sprite);
666 wl_list_remove(&seat->sprite->link);
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -0400667 seat->sprite->plane = WESTON_PLANE_DRM_CURSOR;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500668 }
669
670out:
671 pixman_region32_fini(&cursor_region);
672}
673
Jesse Barnes58ef3792012-02-23 09:45:49 -0500674static void
675drm_assign_planes(struct weston_output *output)
676{
677 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400678 struct weston_surface *es, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500679 pixman_region32_t overlap, surface_overlap;
Daniel Stone37816df2012-05-16 18:45:18 +0100680 struct weston_seat *seat;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500681
682 /*
683 * Find a surface for each sprite in the output using some heuristics:
684 * 1) size
685 * 2) frequency of update
686 * 3) opacity (though some hw might support alpha blending)
687 * 4) clipping (this can be fixed with color keys)
688 *
689 * The idea is to save on blitting since this should save power.
690 * If we can get a large video surface on the sprite for example,
691 * the main display surface may not need to update at all, and
692 * the client buffer can be used directly for the sprite surface
693 * as we do for flipping full screen surfaces.
694 */
Kristian Høgsbergf2735ea2012-06-20 23:03:53 -0400695 seat = (struct weston_seat *) ec->seat;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500696 pixman_region32_init(&overlap);
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400697 wl_list_for_each_safe(es, next, &ec->surface_list, link) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500698 /*
699 * FIXME: try to assign hw cursors here too, they're just
700 * special overlays
701 */
702 pixman_region32_init(&surface_overlap);
703 pixman_region32_intersect(&surface_overlap, &overlap,
704 &es->transform.boundingbox);
705
Daniel Stone37816df2012-05-16 18:45:18 +0100706 if (es == seat->sprite) {
707 weston_output_set_cursor(output, seat,
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500708 &surface_overlap);
709
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -0400710 if (seat->sprite->plane == WESTON_PLANE_PRIMARY)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500711 pixman_region32_union(&overlap, &overlap,
712 &es->transform.boundingbox);
713 } else if (!drm_output_prepare_overlay_surface(output, es,
714 &surface_overlap)) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500715 pixman_region32_fini(&es->damage);
716 pixman_region32_init(&es->damage);
717 } else {
718 pixman_region32_union(&overlap, &overlap,
719 &es->transform.boundingbox);
720 }
721 pixman_region32_fini(&surface_overlap);
722 }
723 pixman_region32_fini(&overlap);
724
Kristian Høgsbergf2735ea2012-06-20 23:03:53 -0400725 if (!seat->sprite || !weston_surface_is_mapped(seat->sprite))
726 drm_output_set_cursor(output, NULL);
727
Jesse Barnes58ef3792012-02-23 09:45:49 -0500728 drm_disable_unused_sprites(output);
729}
730
731static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500732drm_output_set_cursor(struct weston_output *output_base,
Daniel Stone37816df2012-05-16 18:45:18 +0100733 struct weston_seat *es)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200734{
735 struct drm_output *output = (struct drm_output *) output_base;
736 struct drm_compositor *c =
737 (struct drm_compositor *) output->base.compositor;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200738 EGLint handle, stride;
739 int ret = -1;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200740 struct gbm_bo *bo;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400741 uint32_t buf[64 * 64];
742 unsigned char *d, *s, *end;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200743
Daniel Stone37816df2012-05-16 18:45:18 +0100744 if (es == NULL) {
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400745 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
746 return 0;
747 }
748
Daniel Stone37816df2012-05-16 18:45:18 +0100749 if (es->sprite->buffer == NULL ||
750 !wl_buffer_is_shm(es->sprite->buffer))
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200751 goto out;
752
Daniel Stone37816df2012-05-16 18:45:18 +0100753 if (es->sprite->geometry.width > 64 ||
754 es->sprite->geometry.height > 64)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200755 goto out;
Benjamin Franzke117483d2011-08-30 11:38:26 +0200756
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400757 output->current_cursor ^= 1;
758 bo = output->cursor_bo[output->current_cursor];
Kristian Høgsberg6916d9c2012-01-18 15:11:07 -0500759 if (bo == NULL)
760 goto out;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200761
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400762 memset(buf, 0, sizeof buf);
763 d = (unsigned char *) buf;
Daniel Stone37816df2012-05-16 18:45:18 +0100764 stride = wl_shm_buffer_get_stride(es->sprite->buffer);
765 s = wl_shm_buffer_get_data(es->sprite->buffer);
766 end = s + stride * es->sprite->geometry.height;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400767 while (s < end) {
Daniel Stone37816df2012-05-16 18:45:18 +0100768 memcpy(d, s, es->sprite->geometry.width * 4);
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400769 s += stride;
770 d += 64 * 4;
771 }
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200772
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400773 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200774 goto out;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200775
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400776 handle = gbm_bo_get_handle(bo).s32;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200777 ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
778 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200779 weston_log("failed to set cursor: %s\n", strerror(-ret));
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200780 goto out;
781 }
782
783 ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
Daniel Stone37816df2012-05-16 18:45:18 +0100784 es->sprite->geometry.x - output->base.x,
785 es->sprite->geometry.y - output->base.y);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200786 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200787 weston_log("failed to move cursor: %s\n", strerror(-ret));
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200788 goto out;
789 }
790
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200791out:
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200792 if (ret)
793 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
794 return ret;
795}
796
Matt Roper361d2ad2011-08-29 13:52:23 -0700797static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500798drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700799{
800 struct drm_output *output = (struct drm_output *) output_base;
801 struct drm_compositor *c =
Benjamin Franzke117483d2011-08-30 11:38:26 +0200802 (struct drm_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700803 drmModeCrtcPtr origcrtc = output->original_crtc;
Matt Roper361d2ad2011-08-29 13:52:23 -0700804
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200805 if (output->backlight)
806 backlight_destroy(output->backlight);
807
Matt Roper361d2ad2011-08-29 13:52:23 -0700808 /* Turn off hardware cursor */
Benjamin Franzke117483d2011-08-30 11:38:26 +0200809 drm_output_set_cursor(&output->base, NULL);
Matt Roper361d2ad2011-08-29 13:52:23 -0700810
811 /* Restore original CRTC state */
812 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +0200813 origcrtc->x, origcrtc->y,
814 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -0700815 drmModeFreeCrtc(origcrtc);
816
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200817 c->crtc_allocator &= ~(1 << output->crtc_id);
818 c->connector_allocator &= ~(1 << output->connector_id);
819
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400820 eglDestroySurface(c->base.egl_display, output->egl_surface);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400821 gbm_surface_destroy(output->surface);
822
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500823 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200824 wl_list_remove(&output->base.link);
825
Matt Roper361d2ad2011-08-29 13:52:23 -0700826 free(output);
827}
828
Alex Wub7b8bda2012-04-17 17:20:48 +0800829static struct drm_mode *
830choose_mode (struct drm_output *output, struct weston_mode *target_mode)
831{
832 struct drm_mode *tmp_mode = NULL, *mode;
833
834 if (output->base.current->width == target_mode->width &&
835 output->base.current->height == target_mode->height &&
836 (output->base.current->refresh == target_mode->refresh ||
837 target_mode->refresh == 0))
838 return (struct drm_mode *)output->base.current;
839
840 wl_list_for_each(mode, &output->base.mode_list, base.link) {
841 if (mode->mode_info.hdisplay == target_mode->width &&
842 mode->mode_info.vdisplay == target_mode->height) {
843 if (mode->mode_info.vrefresh == target_mode->refresh ||
844 target_mode->refresh == 0) {
845 return mode;
846 } else if (!tmp_mode)
847 tmp_mode = mode;
848 }
849 }
850
851 return tmp_mode;
852}
853
854static int
855drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
856{
857 struct drm_output *output;
858 struct drm_mode *drm_mode;
859 int ret;
860 struct drm_compositor *ec;
861 struct gbm_surface *surface;
862 EGLSurface egl_surface;
863
864 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200865 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800866 return -1;
867 }
868
869 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200870 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800871 return -1;
872 }
873
874 ec = (struct drm_compositor *)output_base->compositor;
875 output = (struct drm_output *)output_base;
876 drm_mode = choose_mode (output, mode);
877
878 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +0200879 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +0800880 return -1;
881 } else if (&drm_mode->base == output->base.current) {
882 return 0;
883 } else if (drm_mode->base.width == output->base.current->width &&
884 drm_mode->base.height == output->base.current->height) {
885 /* only change refresh value */
886 ret = drmModeSetCrtc(ec->drm.fd,
887 output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300888 output->current->fb_id, 0, 0,
Alex Wub7b8bda2012-04-17 17:20:48 +0800889 &output->connector_id, 1, &drm_mode->mode_info);
890
891 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200892 weston_log("failed to set mode (%dx%d) %u Hz\n",
Alex Wub7b8bda2012-04-17 17:20:48 +0800893 drm_mode->base.width,
894 drm_mode->base.height,
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400895 drm_mode->base.refresh / 1000);
Alex Wub7b8bda2012-04-17 17:20:48 +0800896 ret = -1;
897 } else {
898 output->base.current->flags = 0;
899 output->base.current = &drm_mode->base;
900 drm_mode->base.flags =
901 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
902 ret = 0;
903 }
904
905 return ret;
906 }
907
908 drm_mode->base.flags =
909 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
910
911 surface = gbm_surface_create(ec->gbm,
912 drm_mode->base.width,
913 drm_mode->base.height,
914 GBM_FORMAT_XRGB8888,
915 GBM_BO_USE_SCANOUT |
916 GBM_BO_USE_RENDERING);
917 if (!surface) {
Martin Minarik6d118362012-06-07 18:01:59 +0200918 weston_log("failed to create gbm surface\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800919 return -1;
920 }
921
922 egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400923 eglCreateWindowSurface(ec->base.egl_display,
924 ec->base.egl_config,
Alex Wub7b8bda2012-04-17 17:20:48 +0800925 surface, NULL);
926
927 if (egl_surface == EGL_NO_SURFACE) {
Martin Minarik6d118362012-06-07 18:01:59 +0200928 weston_log("failed to create egl surface\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800929 goto err;
930 }
931
932 ret = drmModeSetCrtc(ec->drm.fd,
933 output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300934 output->current->fb_id, 0, 0,
Alex Wub7b8bda2012-04-17 17:20:48 +0800935 &output->connector_id, 1, &drm_mode->mode_info);
936 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200937 weston_log("failed to set mode\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800938 goto err;
939 }
940
941 /* reset rendering stuff. */
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300942 if (output->current) {
943 if (output->current->is_client_buffer)
944 gbm_bo_destroy(output->current->bo);
945 else
946 gbm_surface_release_buffer(output->surface,
947 output->current->bo);
948 }
949 output->current = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +0800950
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300951 if (output->next) {
952 if (output->next->is_client_buffer)
953 gbm_bo_destroy(output->next->bo);
954 else
955 gbm_surface_release_buffer(output->surface,
956 output->next->bo);
957 }
958 output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +0800959
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400960 eglDestroySurface(ec->base.egl_display, output->egl_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +0800961 gbm_surface_destroy(output->surface);
962 output->egl_surface = egl_surface;
963 output->surface = surface;
964
965 /*update output*/
966 output->base.current = &drm_mode->base;
967 output->base.dirty = 1;
968 weston_output_move(&output->base, output->base.x, output->base.y);
969 return 0;
970
971err:
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400972 eglDestroySurface(ec->base.egl_display, egl_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +0800973 gbm_surface_destroy(surface);
974 return -1;
975}
976
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400977static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400978on_drm_input(int fd, uint32_t mask, void *data)
979{
980 drmEventContext evctx;
981
982 memset(&evctx, 0, sizeof evctx);
983 evctx.version = DRM_EVENT_CONTEXT_VERSION;
984 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500985 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400986 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400987
988 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400989}
990
991static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400992init_egl(struct drm_compositor *ec, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400993{
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400994 EGLint major, minor, n;
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400995 const char *filename, *sysnum;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400996 int fd;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400997 static const EGLint context_attribs[] = {
998 EGL_CONTEXT_CLIENT_VERSION, 2,
999 EGL_NONE
1000 };
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001001
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001002 static const EGLint config_attribs[] = {
1003 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1004 EGL_RED_SIZE, 1,
1005 EGL_GREEN_SIZE, 1,
1006 EGL_BLUE_SIZE, 1,
1007 EGL_ALPHA_SIZE, 0,
1008 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1009 EGL_NONE
1010 };
1011
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001012 sysnum = udev_device_get_sysnum(device);
1013 if (sysnum)
1014 ec->drm.id = atoi(sysnum);
1015 if (!sysnum || ec->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001016 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001017 return -1;
1018 }
1019
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001020 filename = udev_device_get_devnode(device);
David Herrmann63ff7062011-11-05 18:46:01 +01001021 fd = open(filename, O_RDWR | O_CLOEXEC);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001022 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001023 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001024 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001025 udev_device_get_devnode(device));
1026 return -1;
1027 }
1028
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001029 weston_log("using %s\n", filename);
1030
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001031 ec->drm.fd = fd;
Benjamin Franzke060cf802011-04-30 09:32:11 +02001032 ec->gbm = gbm_create_device(ec->drm.fd);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001033 ec->base.egl_display = eglGetDisplay(ec->gbm);
1034 if (ec->base.egl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001035 weston_log("failed to create display\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001036 return -1;
1037 }
1038
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001039 if (!eglInitialize(ec->base.egl_display, &major, &minor)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001040 weston_log("failed to initialize display\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001041 return -1;
1042 }
1043
Darxus55973f22010-11-22 21:24:39 -05001044 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001045 weston_log("failed to bind api EGL_OPENGL_ES_API\n");
Darxus55973f22010-11-22 21:24:39 -05001046 return -1;
1047 }
1048
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001049 if (!eglChooseConfig(ec->base.egl_display, config_attribs,
1050 &ec->base.egl_config, 1, &n) || n != 1) {
Martin Minarik6d118362012-06-07 18:01:59 +02001051 weston_log("failed to choose config: %d\n", n);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001052 return -1;
1053 }
1054
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001055 ec->base.egl_context =
1056 eglCreateContext(ec->base.egl_display, ec->base.egl_config,
1057 EGL_NO_CONTEXT, context_attribs);
1058 if (ec->base.egl_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001059 weston_log("failed to create context\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001060 return -1;
1061 }
1062
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001063 ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10,
1064 GBM_FORMAT_XRGB8888,
1065 GBM_BO_USE_RENDERING);
1066 if (!ec->dummy_surface) {
Martin Minarik6d118362012-06-07 18:01:59 +02001067 weston_log("failed to create dummy gbm surface\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001068 return -1;
1069 }
1070
1071 ec->dummy_egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001072 eglCreateWindowSurface(ec->base.egl_display,
1073 ec->base.egl_config,
1074 ec->dummy_surface,
1075 NULL);
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001076 if (ec->dummy_egl_surface == EGL_NO_SURFACE) {
Martin Minarik6d118362012-06-07 18:01:59 +02001077 weston_log("failed to create egl surface\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001078 return -1;
1079 }
1080
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001081 if (!eglMakeCurrent(ec->base.egl_display, ec->dummy_egl_surface,
1082 ec->dummy_egl_surface, ec->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001083 weston_log("failed to make context current\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001084 return -1;
1085 }
1086
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001087 return 0;
1088}
1089
1090static drmModeModeInfo builtin_1024x768 = {
1091 63500, /* clock */
1092 1024, 1072, 1176, 1328, 0,
1093 768, 771, 775, 798, 0,
1094 59920,
1095 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
1096 0,
1097 "1024x768"
1098};
1099
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001100
1101static int
1102drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1103{
1104 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001105 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001106
1107 mode = malloc(sizeof *mode);
1108 if (mode == NULL)
1109 return -1;
1110
1111 mode->base.flags = 0;
1112 mode->base.width = info->hdisplay;
1113 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001114
1115 /* Calculate higher precision (mHz) refresh rate */
1116 refresh = (info->clock * 1000000LL / info->htotal +
1117 info->vtotal / 2) / info->vtotal;
1118
1119 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1120 refresh *= 2;
1121 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1122 refresh /= 2;
1123 if (info->vscan > 1)
1124 refresh /= info->vscan;
1125
1126 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001127 mode->mode_info = *info;
1128 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1129
1130 return 0;
1131}
1132
1133static int
1134drm_subpixel_to_wayland(int drm_value)
1135{
1136 switch (drm_value) {
1137 default:
1138 case DRM_MODE_SUBPIXEL_UNKNOWN:
1139 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1140 case DRM_MODE_SUBPIXEL_NONE:
1141 return WL_OUTPUT_SUBPIXEL_NONE;
1142 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1143 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1144 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1145 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1146 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1147 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1148 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1149 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1150 }
1151}
1152
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001153static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001154sprite_handle_buffer_destroy(struct wl_listener *listener, void *data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001155{
1156 struct drm_sprite *sprite =
1157 container_of(listener, struct drm_sprite,
1158 destroy_listener);
1159
1160 sprite->surface = NULL;
1161}
1162
1163static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001164sprite_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001165{
1166 struct drm_sprite *sprite =
1167 container_of(listener, struct drm_sprite,
1168 pending_destroy_listener);
1169
1170 sprite->pending_surface = NULL;
1171}
1172
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001173/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001174static uint32_t
1175drm_get_backlight(struct drm_output *output)
1176{
1177 long brightness, max_brightness, norm;
1178
1179 brightness = backlight_get_brightness(output->backlight);
1180 max_brightness = backlight_get_max_brightness(output->backlight);
1181
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001182 /* convert it on a scale of 0 to 255 */
1183 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001184
1185 return (uint32_t) norm;
1186}
1187
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001188/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001189static void
1190drm_set_backlight(struct weston_output *output_base, uint32_t value)
1191{
1192 struct drm_output *output = (struct drm_output *) output_base;
1193 long max_brightness, new_brightness;
1194
1195 if (!output->backlight)
1196 return;
1197
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001198 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001199 return;
1200
1201 max_brightness = backlight_get_max_brightness(output->backlight);
1202
1203 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001204 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001205
1206 backlight_set_brightness(output->backlight, new_brightness);
1207}
1208
1209static drmModePropertyPtr
1210drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1211{
1212 drmModePropertyPtr props;
1213 int i;
1214
1215 for (i = 0; i < connector->count_props; i++) {
1216 props = drmModeGetProperty(fd, connector->props[i]);
1217 if (!props)
1218 continue;
1219
1220 if (!strcmp(props->name, name))
1221 return props;
1222
1223 drmModeFreeProperty(props);
1224 }
1225
1226 return NULL;
1227}
1228
1229static void
1230drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1231{
1232 struct drm_output *output = (struct drm_output *) output_base;
1233 struct weston_compositor *ec = output_base->compositor;
1234 struct drm_compositor *c = (struct drm_compositor *) ec;
1235 drmModeConnectorPtr connector;
1236 drmModePropertyPtr prop;
1237
1238 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1239 if (!connector)
1240 return;
1241
1242 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1243 if (!prop) {
1244 drmModeFreeConnector(connector);
1245 return;
1246 }
1247
1248 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1249 prop->prop_id, level);
1250 drmModeFreeProperty(prop);
1251 drmModeFreeConnector(connector);
1252}
1253
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001254static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001255create_output_for_connector(struct drm_compositor *ec,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001256 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001257 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001258 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001259{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001260 struct drm_output *output;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001261 struct drm_mode *drm_mode, *next;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001262 drmModeEncoder *encoder;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001263 int i, ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001264
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001265 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001266 if (encoder == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001267 weston_log("No encoder for connector.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001268 return -1;
1269 }
1270
1271 for (i = 0; i < resources->count_crtcs; i++) {
Marty Jack13d9db22011-02-09 19:01:42 -05001272 if (encoder->possible_crtcs & (1 << i) &&
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001273 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001274 break;
1275 }
1276 if (i == resources->count_crtcs) {
Martin Minarik6d118362012-06-07 18:01:59 +02001277 weston_log("No usable crtc for encoder.\n");
David Herrmanneb8bed52011-12-08 17:05:44 +01001278 drmModeFreeEncoder(encoder);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001279 return -1;
1280 }
1281
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001282 output = malloc(sizeof *output);
David Herrmanneb8bed52011-12-08 17:05:44 +01001283 if (output == NULL) {
1284 drmModeFreeEncoder(encoder);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001285 return -1;
David Herrmanneb8bed52011-12-08 17:05:44 +01001286 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001287
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001288 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001289 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1290 output->base.make = "unknown";
1291 output->base.model = "unknown";
1292 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001293
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001294 output->crtc_id = resources->crtcs[i];
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001295 ec->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001296 output->connector_id = connector->connector_id;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001297 ec->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001298
Matt Roper361d2ad2011-08-29 13:52:23 -07001299 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001300 drmModeFreeEncoder(encoder);
Matt Roper361d2ad2011-08-29 13:52:23 -07001301
David Herrmann0f0d54e2011-12-08 17:05:45 +01001302 for (i = 0; i < connector->count_modes; i++) {
1303 ret = drm_output_add_mode(output, &connector->modes[i]);
1304 if (ret)
1305 goto err_free;
1306 }
1307
1308 if (connector->count_modes == 0) {
1309 ret = drm_output_add_mode(output, &builtin_1024x768);
1310 if (ret)
1311 goto err_free;
1312 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001313
1314 drm_mode = container_of(output->base.mode_list.next,
1315 struct drm_mode, base.link);
1316 output->base.current = &drm_mode->base;
1317 drm_mode->base.flags =
1318 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1319
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001320 output->surface = gbm_surface_create(ec->gbm,
1321 output->base.current->width,
1322 output->base.current->height,
1323 GBM_FORMAT_XRGB8888,
1324 GBM_BO_USE_SCANOUT |
1325 GBM_BO_USE_RENDERING);
1326 if (!output->surface) {
Martin Minarik6d118362012-06-07 18:01:59 +02001327 weston_log("failed to create gbm surface\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001328 goto err_free;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001329 }
1330
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001331 output->egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001332 eglCreateWindowSurface(ec->base.egl_display,
1333 ec->base.egl_config,
1334 output->surface,
1335 NULL);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001336 if (output->egl_surface == EGL_NO_SURFACE) {
Martin Minarik6d118362012-06-07 18:01:59 +02001337 weston_log("failed to create egl surface\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001338 goto err_surface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001339 }
1340
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04001341 output->cursor_bo[0] =
1342 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1343 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1344 output->cursor_bo[1] =
1345 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1346 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1347
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001348 output->backlight = backlight_init(drm_device,
1349 connector->connector_type);
1350 if (output->backlight) {
1351 output->base.set_backlight = drm_set_backlight;
1352 output->base.backlight_current = drm_get_backlight(output);
1353 }
1354
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001355 weston_output_init(&output->base, &ec->base, x, y,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001356 connector->mmWidth, connector->mmHeight,
1357 WL_OUTPUT_FLIPPED);
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04001358
1359 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1360
Alex Wubd3354b2012-04-17 17:20:49 +08001361 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001362 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07001363 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001364 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001365 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08001366 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001367
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001368 weston_log("kms connector %d, crtc %d at mode %dx%d@%.1f\n",
1369 output->connector_id, output->crtc_id,
1370 output->base.current->width,
1371 output->base.current->height,
1372 output->base.current->refresh / 1000.0);
1373
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001374 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001375
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001376err_surface:
1377 gbm_surface_destroy(output->surface);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001378err_free:
1379 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1380 base.link) {
1381 wl_list_remove(&drm_mode->base.link);
1382 free(drm_mode);
1383 }
1384
1385 drmModeFreeCrtc(output->original_crtc);
1386 ec->crtc_allocator &= ~(1 << output->crtc_id);
1387 ec->connector_allocator &= ~(1 << output->connector_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001388 free(output);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001389
David Herrmann0f0d54e2011-12-08 17:05:45 +01001390 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001391}
1392
Jesse Barnes58ef3792012-02-23 09:45:49 -05001393static void
1394create_sprites(struct drm_compositor *ec)
1395{
1396 struct drm_sprite *sprite;
1397 drmModePlaneRes *plane_res;
1398 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001399 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001400
1401 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1402 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02001403 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001404 strerror(errno));
1405 return;
1406 }
1407
1408 for (i = 0; i < plane_res->count_planes; i++) {
1409 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1410 if (!plane)
1411 continue;
1412
1413 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1414 plane->count_formats));
1415 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02001416 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001417 __func__);
1418 free(plane);
1419 continue;
1420 }
1421
1422 memset(sprite, 0, sizeof *sprite);
1423
1424 sprite->possible_crtcs = plane->possible_crtcs;
1425 sprite->plane_id = plane->plane_id;
1426 sprite->surface = NULL;
1427 sprite->pending_surface = NULL;
1428 sprite->fb_id = 0;
1429 sprite->pending_fb_id = 0;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001430 sprite->destroy_listener.notify = sprite_handle_buffer_destroy;
1431 sprite->pending_destroy_listener.notify =
Jesse Barnes58ef3792012-02-23 09:45:49 -05001432 sprite_handle_pending_buffer_destroy;
1433 sprite->compositor = ec;
1434 sprite->count_formats = plane->count_formats;
1435 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05001436 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05001437 drmModeFreePlane(plane);
1438
1439 wl_list_insert(&ec->sprite_list, &sprite->link);
1440 }
1441
1442 free(plane_res->planes);
1443 free(plane_res);
1444}
1445
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001446static void
1447destroy_sprites(struct drm_compositor *compositor)
1448{
1449 struct drm_sprite *sprite, *next;
1450 struct drm_output *output;
1451
1452 output = container_of(compositor->base.output_list.next,
1453 struct drm_output, base.link);
1454
1455 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1456 drmModeSetPlane(compositor->drm.fd,
1457 sprite->plane_id,
1458 output->crtc_id, 0, 0,
1459 0, 0, 0, 0, 0, 0, 0, 0);
1460 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1461 free(sprite);
1462 }
1463}
Jesse Barnes58ef3792012-02-23 09:45:49 -05001464
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001465static int
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001466create_outputs(struct drm_compositor *ec, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001467 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001468{
1469 drmModeConnector *connector;
1470 drmModeRes *resources;
1471 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001472 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001473
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001474 resources = drmModeGetResources(ec->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001475 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02001476 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001477 return -1;
1478 }
1479
Jesse Barnes58ef3792012-02-23 09:45:49 -05001480 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001481 if (!ec->crtcs) {
1482 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001483 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001484 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001485
1486 ec->num_crtcs = resources->count_crtcs;
1487 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1488
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001489 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001490 connector = drmModeGetConnector(ec->drm.fd,
1491 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001492 if (connector == NULL)
1493 continue;
1494
1495 if (connector->connection == DRM_MODE_CONNECTED &&
1496 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001497 connector->connector_id == option_connector)) {
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001498 if (create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001499 connector, x, y,
1500 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02001501 drmModeFreeConnector(connector);
1502 continue;
1503 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001504
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001505 x += container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001506 struct weston_output,
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001507 link)->current->width;
1508 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001509
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001510 drmModeFreeConnector(connector);
1511 }
1512
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001513 if (wl_list_empty(&ec->base.output_list)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001514 weston_log("No currently active connector found.\n");
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001515 drmModeFreeResources(resources);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001516 return -1;
1517 }
1518
1519 drmModeFreeResources(resources);
1520
1521 return 0;
1522}
1523
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001524static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001525update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001526{
1527 drmModeConnector *connector;
1528 drmModeRes *resources;
1529 struct drm_output *output, *next;
1530 int x = 0, y = 0;
1531 int x_offset = 0, y_offset = 0;
1532 uint32_t connected = 0, disconnects = 0;
1533 int i;
1534
1535 resources = drmModeGetResources(ec->drm.fd);
1536 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02001537 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001538 return;
1539 }
1540
1541 /* collect new connects */
1542 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001543 int connector_id = resources->connectors[i];
1544
1545 connector = drmModeGetConnector(ec->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01001546 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001547 continue;
1548
David Herrmann7551cff2011-12-08 17:05:43 +01001549 if (connector->connection != DRM_MODE_CONNECTED) {
1550 drmModeFreeConnector(connector);
1551 continue;
1552 }
1553
Benjamin Franzke117483d2011-08-30 11:38:26 +02001554 connected |= (1 << connector_id);
1555
1556 if (!(ec->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001557 struct weston_output *last =
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001558 container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001559 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001560
1561 /* XXX: not yet needed, we die with 0 outputs */
1562 if (!wl_list_empty(&ec->base.output_list))
Benjamin Franzke117483d2011-08-30 11:38:26 +02001563 x = last->x + last->current->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001564 else
1565 x = 0;
1566 y = 0;
1567 create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001568 connector, x, y,
1569 drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02001570 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001571
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001572 }
1573 drmModeFreeConnector(connector);
1574 }
1575 drmModeFreeResources(resources);
1576
1577 disconnects = ec->connector_allocator & ~connected;
1578 if (disconnects) {
1579 wl_list_for_each_safe(output, next, &ec->base.output_list,
1580 base.link) {
1581 if (x_offset != 0 || y_offset != 0) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001582 weston_output_move(&output->base,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001583 output->base.x - x_offset,
1584 output->base.y - y_offset);
1585 }
1586
1587 if (disconnects & (1 << output->connector_id)) {
1588 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02001589 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001590 output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001591 x_offset += output->base.current->width;
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001592 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001593 }
1594 }
1595 }
1596
1597 /* FIXME: handle zero outputs, without terminating */
1598 if (ec->connector_allocator == 0)
1599 wl_display_terminate(ec->base.wl_display);
1600}
1601
1602static int
David Herrmannd7488c22012-03-11 20:05:21 +01001603udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001604{
David Herrmannd7488c22012-03-11 20:05:21 +01001605 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01001606 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01001607
1608 sysnum = udev_device_get_sysnum(device);
1609 if (!sysnum || atoi(sysnum) != ec->drm.id)
1610 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001611
David Herrmann6ac52db2012-03-11 20:05:22 +01001612 val = udev_device_get_property_value(device, "HOTPLUG");
1613 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001614 return 0;
1615
David Herrmann6ac52db2012-03-11 20:05:22 +01001616 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001617}
1618
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001619static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001620udev_drm_event(int fd, uint32_t mask, void *data)
1621{
1622 struct drm_compositor *ec = data;
1623 struct udev_device *event;
1624
1625 event = udev_monitor_receive_device(ec->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001626
David Herrmannd7488c22012-03-11 20:05:21 +01001627 if (udev_event_is_hotplug(ec, event))
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001628 update_outputs(ec, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001629
1630 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001631
1632 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001633}
1634
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001635static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001636drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001637{
1638 struct drm_compositor *d = (struct drm_compositor *) ec;
Daniel Stone37816df2012-05-16 18:45:18 +01001639 struct weston_seat *seat, *next;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001640
Daniel Stone37816df2012-05-16 18:45:18 +01001641 wl_list_for_each_safe(seat, next, &ec->seat_list, link)
1642 evdev_input_destroy(seat);
Jonas Ådahlc97af922012-03-28 22:36:09 +02001643
1644 wl_event_source_remove(d->udev_drm_source);
1645 wl_event_source_remove(d->drm_source);
1646
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001647 weston_compositor_shutdown(ec);
Jonas Ådahlc97af922012-03-28 22:36:09 +02001648
Ander Conselvan de Oliveira5f5f3192012-04-30 13:31:28 +03001649 /* Work around crash in egl_dri2.c's dri2_make_current() */
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001650 eglMakeCurrent(ec->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
Ander Conselvan de Oliveira5f5f3192012-04-30 13:31:28 +03001651 EGL_NO_CONTEXT);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001652 eglTerminate(ec->egl_display);
Ander Conselvan de Oliveira5f5f3192012-04-30 13:31:28 +03001653 eglReleaseThread();
1654
Matt Roper361d2ad2011-08-29 13:52:23 -07001655 gbm_device_destroy(d->gbm);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001656 destroy_sprites(d);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001657 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +02001658 weston_log("failed to drop master: %m\n");
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001659 tty_destroy(d->tty);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001660
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001661 free(d);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001662}
1663
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001664static void
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001665drm_compositor_set_modes(struct drm_compositor *compositor)
1666{
1667 struct drm_output *output;
1668 struct drm_mode *drm_mode;
1669 int ret;
1670
1671 wl_list_for_each(output, &compositor->base.output_list, base.link) {
1672 drm_mode = (struct drm_mode *) output->base.current;
1673 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001674 output->current->fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001675 &output->connector_id, 1,
1676 &drm_mode->mode_info);
1677 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001678 weston_log(
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001679 "failed to set mode %dx%d for output at %d,%d: %m\n",
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001680 drm_mode->base.width, drm_mode->base.height,
1681 output->base.x, output->base.y);
1682 }
1683 }
1684}
1685
1686static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001687vt_func(struct weston_compositor *compositor, int event)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001688{
1689 struct drm_compositor *ec = (struct drm_compositor *) compositor;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001690 struct weston_output *output;
Daniel Stone37816df2012-05-16 18:45:18 +01001691 struct weston_seat *seat;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001692 struct drm_sprite *sprite;
1693 struct drm_output *drm_output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001694
1695 switch (event) {
1696 case TTY_ENTER_VT:
1697 compositor->focus = 1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001698 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001699 weston_log("failed to set master: %m\n");
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001700 wl_display_terminate(compositor->wl_display);
1701 }
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001702 compositor->state = ec->prev_state;
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001703 drm_compositor_set_modes(ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001704 weston_compositor_damage_all(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001705 wl_list_for_each(seat, &compositor->seat_list, link) {
1706 evdev_add_devices(ec->udev, seat);
1707 evdev_enable_udev_monitor(ec->udev, seat);
Benjamin Franzke78d3afe2012-04-09 18:14:58 +02001708 }
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001709 break;
1710 case TTY_LEAVE_VT:
Daniel Stone37816df2012-05-16 18:45:18 +01001711 wl_list_for_each(seat, &compositor->seat_list, link) {
1712 evdev_disable_udev_monitor(seat);
1713 evdev_remove_devices(seat);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04001714 }
1715
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001716 compositor->focus = 0;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001717 ec->prev_state = compositor->state;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001718 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04001719
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001720 /* If we have a repaint scheduled (either from a
1721 * pending pageflip or the idle handler), make sure we
1722 * cancel that so we don't try to pageflip when we're
1723 * vt switched away. The SLEEPING state will prevent
1724 * further attemps at repainting. When we switch
1725 * back, we schedule a repaint, which will process
1726 * pending frame callbacks. */
1727
1728 wl_list_for_each(output, &ec->base.output_list, link) {
1729 output->repaint_needed = 0;
1730 drm_output_set_cursor(output, NULL);
1731 }
1732
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001733 drm_output = container_of(ec->base.output_list.next,
1734 struct drm_output, base.link);
1735
1736 wl_list_for_each(sprite, &ec->sprite_list, link)
1737 drmModeSetPlane(ec->drm.fd,
1738 sprite->plane_id,
1739 drm_output->crtc_id, 0, 0,
1740 0, 0, 0, 0, 0, 0, 0, 0);
1741
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001742 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +02001743 weston_log("failed to drop master: %m\n");
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001744
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001745 break;
1746 };
1747}
1748
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001749static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001750switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001751{
1752 struct drm_compositor *ec = data;
1753
Daniel Stone325fc2d2012-05-30 16:31:58 +01001754 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001755}
1756
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001757static const char default_seat[] = "seat0";
1758
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001759static struct weston_compositor *
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001760drm_compositor_create(struct wl_display *display,
Daniel Stonebaf43592012-06-01 11:11:10 -04001761 int connector, const char *seat, int tty,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001762 int argc, char *argv[], const char *config_file)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001763{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001764 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001765 struct udev_enumerate *e;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001766 struct udev_list_entry *entry;
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001767 struct udev_device *device, *drm_device;
1768 const char *path, *device_seat;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001769 struct wl_event_loop *loop;
Daniel Stonea96b93c2012-06-22 14:04:37 +01001770 struct weston_seat *weston_seat, *next;
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001771 uint32_t key;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001772
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001773 weston_log("initializing drm backend\n");
1774
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001775 ec = malloc(sizeof *ec);
1776 if (ec == NULL)
1777 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001778 memset(ec, 0, sizeof *ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01001779
1780 if (weston_compositor_init(&ec->base, display, argc, argv,
Daniel Stonea96b93c2012-06-22 14:04:37 +01001781 config_file) < 0) {
1782 weston_log("weston_compositor_init failed\n");
1783 goto err_base;
1784 }
Daniel Stone725c2c32012-06-22 14:04:36 +01001785
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001786 ec->udev = udev_new();
1787 if (ec->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001788 weston_log("failed to initialize udev context\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001789 goto err_compositor;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001790 }
1791
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05001792 ec->base.wl_display = display;
1793 ec->tty = tty_create(&ec->base, vt_func, tty);
1794 if (!ec->tty) {
Martin Minarik6d118362012-06-07 18:01:59 +02001795 weston_log("failed to initialize tty\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001796 goto err_udev;
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05001797 }
1798
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001799 e = udev_enumerate_new(ec->udev);
1800 udev_enumerate_add_match_subsystem(e, "drm");
Benjamin Franzkea764ee52011-10-07 08:23:22 +02001801 udev_enumerate_add_match_sysname(e, "card[0-9]*");
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001802
Benjamin Franzke117483d2011-08-30 11:38:26 +02001803 udev_enumerate_scan_devices(e);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001804 drm_device = NULL;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001805 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001806 path = udev_list_entry_get_name(entry);
1807 device = udev_device_new_from_syspath(ec->udev, path);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001808 device_seat =
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001809 udev_device_get_property_value(device, "ID_SEAT");
1810 if (!device_seat)
1811 device_seat = default_seat;
1812 if (strcmp(device_seat, seat) == 0) {
1813 drm_device = device;
1814 break;
1815 }
1816 udev_device_unref(device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001817 }
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001818
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001819 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001820 weston_log("no drm device found\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001821 goto err_udev_enum;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001822 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001823
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001824 if (init_egl(ec, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001825 weston_log("failed to initialize egl\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001826 goto err_udev_dev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001827 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001828
1829 ec->base.destroy = drm_destroy;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001830
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001831 ec->base.focus = 1;
1832
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001833 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001834
Daniel Stone725c2c32012-06-22 14:04:36 +01001835 if (weston_compositor_init_gl(&ec->base) < 0)
Daniel Stonea96b93c2012-06-22 14:04:37 +01001836 goto err_egl;
Benjamin Franzked59eb1c2011-04-29 22:14:54 +02001837
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001838 for (key = KEY_F1; key < KEY_F9; key++)
Daniel Stone325fc2d2012-05-30 16:31:58 +01001839 weston_compositor_add_key_binding(&ec->base, key,
1840 MODIFIER_CTRL | MODIFIER_ALT,
1841 switch_vt_binding, ec);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001842
Jesse Barnes58ef3792012-02-23 09:45:49 -05001843 wl_list_init(&ec->sprite_list);
1844 create_sprites(ec);
1845
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001846 if (create_outputs(ec, connector, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001847 weston_log("failed to create output for %s\n", path);
Daniel Stonea96b93c2012-06-22 14:04:37 +01001848 goto err_sprite;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001849 }
1850
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02001851 path = NULL;
1852
Tiago Vignattice03ec32011-12-19 01:14:03 +02001853 evdev_input_create(&ec->base, ec->udev, seat);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001854
1855 loop = wl_display_get_event_loop(ec->base.wl_display);
1856 ec->drm_source =
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001857 wl_event_loop_add_fd(loop, ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001858 WL_EVENT_READABLE, on_drm_input, ec);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001859
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001860 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
1861 if (ec->udev_monitor == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001862 weston_log("failed to intialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001863 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001864 }
1865 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
1866 "drm", NULL);
1867 ec->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02001868 wl_event_loop_add_fd(loop,
1869 udev_monitor_get_fd(ec->udev_monitor),
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001870 WL_EVENT_READABLE, udev_drm_event, ec);
1871
1872 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001873 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001874 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001875 }
1876
Daniel Stonea96b93c2012-06-22 14:04:37 +01001877 udev_device_unref(drm_device);
1878 udev_enumerate_unref(e);
1879
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001880 return &ec->base;
Daniel Stonea96b93c2012-06-22 14:04:37 +01001881
1882err_udev_monitor:
1883 wl_event_source_remove(ec->udev_drm_source);
1884 udev_monitor_unref(ec->udev_monitor);
1885err_drm_source:
1886 wl_event_source_remove(ec->drm_source);
1887 wl_list_for_each_safe(weston_seat, next, &ec->base.seat_list, link)
1888 evdev_input_destroy(weston_seat);
1889err_sprite:
1890 destroy_sprites(ec);
1891err_egl:
1892 eglMakeCurrent(ec->base.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
1893 EGL_NO_CONTEXT);
1894 eglTerminate(ec->base.egl_display);
1895 eglReleaseThread();
1896 gbm_device_destroy(ec->gbm);
1897err_udev_dev:
1898 udev_device_unref(drm_device);
1899err_udev_enum:
1900 udev_enumerate_unref(e);
1901 tty_destroy(ec->tty);
1902err_udev:
1903 udev_unref(ec->udev);
1904err_compositor:
1905 weston_compositor_shutdown(&ec->base);
1906err_base:
1907 free(ec);
1908 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001909}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001910
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001911WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -04001912backend_init(struct wl_display *display, int argc, char *argv[],
1913 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001914{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001915 int connector = 0, tty = 0;
1916 const char *seat = default_seat;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001917
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001918 const struct weston_option drm_options[] = {
1919 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
1920 { WESTON_OPTION_STRING, "seat", 0, &seat },
1921 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
1922 };
Benjamin Franzke117483d2011-08-30 11:38:26 +02001923
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001924 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001925
Daniel Stonec1be8e52012-06-01 11:14:02 -04001926 return drm_compositor_create(display, connector, seat, tty, argc, argv,
1927 config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001928}