blob: 6c182d7c8682e824f52b2f4f7f741cc90b402182 [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>
Ander Conselvan de Oliveirafd1f4c62012-06-26 17:09:14 +030032#include <assert.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040033
Benjamin Franzkec649a922011-03-02 11:56:04 +010034#include <xf86drm.h>
35#include <xf86drmMode.h>
Jesse Barnes58ef3792012-02-23 09:45:49 -050036#include <drm_fourcc.h>
Benjamin Franzkec649a922011-03-02 11:56:04 +010037
Benjamin Franzke060cf802011-04-30 09:32:11 +020038#include <gbm.h>
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +020039#include <libbacklight.h>
Benjamin Franzke060cf802011-04-30 09:32:11 +020040
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040041#include "compositor.h"
Tiago Vignattice03ec32011-12-19 01:14:03 +020042#include "evdev.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010043#include "launcher-util.h"
Martin Minarik6d118362012-06-07 18:01:59 +020044#include "log.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040045
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -040046enum {
47 WESTON_PLANE_DRM_CURSOR = 0x100
48};
49
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040050struct drm_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050051 struct weston_compositor base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040052
53 struct udev *udev;
54 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040055
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010056 struct udev_monitor *udev_monitor;
57 struct wl_event_source *udev_drm_source;
58
Benjamin Franzke2af7f102011-03-02 11:14:59 +010059 struct {
David Herrmannd7488c22012-03-11 20:05:21 +010060 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010061 int fd;
62 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +020063 struct gbm_device *gbm;
Jesse Barnes58ef3792012-02-23 09:45:49 -050064 uint32_t *crtcs;
65 int num_crtcs;
Marty Jack13d9db22011-02-09 19:01:42 -050066 uint32_t crtc_allocator;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010067 uint32_t connector_allocator;
Kristian Høgsberge4762a62011-01-14 14:59:13 -050068 struct tty *tty;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +020069
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -040070 struct gbm_surface *dummy_surface;
71 EGLSurface dummy_egl_surface;
72
Jesse Barnes58ef3792012-02-23 09:45:49 -050073 struct wl_list sprite_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -050074 int sprites_are_broken;
Jesse Barnes58ef3792012-02-23 09:45:49 -050075
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +020076 uint32_t prev_state;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040077};
78
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040079struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050080 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040081 drmModeModeInfo mode_info;
82};
83
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +030084struct drm_output;
85
86struct drm_fb {
87 struct gbm_bo *bo;
88 struct drm_output *output;
89 uint32_t fb_id;
90 int is_client_buffer;
91 struct wl_buffer *buffer;
92 struct wl_listener buffer_destroy_listener;
93};
94
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040095struct drm_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050096 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040097
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040098 uint32_t crtc_id;
99 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700100 drmModeCrtcPtr original_crtc;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200101
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300102 int vblank_pending;
103 int page_flip_pending;
104
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400105 struct gbm_surface *surface;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400106 struct gbm_bo *cursor_bo[2];
107 int current_cursor;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400108 EGLSurface egl_surface;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300109 struct drm_fb *current, *next;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200110 struct backlight *backlight;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400111};
112
Jesse Barnes58ef3792012-02-23 09:45:49 -0500113/*
114 * An output has a primary display plane plus zero or more sprites for
115 * blending display contents.
116 */
117struct drm_sprite {
118 struct wl_list link;
119
120 uint32_t fb_id;
121 uint32_t pending_fb_id;
122 struct weston_surface *surface;
123 struct weston_surface *pending_surface;
124
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300125 struct drm_output *output;
126
Jesse Barnes58ef3792012-02-23 09:45:49 -0500127 struct drm_compositor *compositor;
128
129 struct wl_listener destroy_listener;
130 struct wl_listener pending_destroy_listener;
131
132 uint32_t possible_crtcs;
133 uint32_t plane_id;
134 uint32_t count_formats;
135
136 int32_t src_x, src_y;
137 uint32_t src_w, src_h;
138 uint32_t dest_x, dest_y;
139 uint32_t dest_w, dest_h;
140
141 uint32_t formats[];
142};
143
144static int
145surface_is_primary(struct weston_compositor *ec, struct weston_surface *es)
146{
147 struct weston_surface *primary;
148
149 primary = container_of(ec->surface_list.next, struct weston_surface,
150 link);
151 if (es == primary)
152 return -1;
153 return 0;
154}
155
156static int
157drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
158{
159 struct weston_compositor *ec = output_base->compositor;
160 struct drm_compositor *c =(struct drm_compositor *) ec;
161 struct drm_output *output = (struct drm_output *) output_base;
162 int crtc;
163
164 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
165 if (c->crtcs[crtc] != output->crtc_id)
166 continue;
167
168 if (supported & (1 << crtc))
169 return -1;
170 }
171
172 return 0;
173}
174
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300175static void
176drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
177{
178 struct drm_fb *fb = data;
179 struct gbm_device *gbm = gbm_bo_get_device(bo);
180
181 if (fb->fb_id)
182 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
183
184 if (fb->buffer) {
185 weston_buffer_post_release(fb->buffer);
186 wl_list_remove(&fb->buffer_destroy_listener.link);
187 }
188
189 free(data);
190}
191
192static struct drm_fb *
193drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_output *output)
194{
195 struct drm_fb *fb = gbm_bo_get_user_data(bo);
196 struct drm_compositor *compositor =
197 (struct drm_compositor *) output->base.compositor;
198 uint32_t width, height, stride, handle;
199 int ret;
200
201 if (fb)
202 return fb;
203
204 fb = malloc(sizeof *fb);
205
206 fb->bo = bo;
207 fb->output = output;
208 fb->is_client_buffer = 0;
209 fb->buffer = NULL;
210
211 width = gbm_bo_get_width(bo);
212 height = gbm_bo_get_height(bo);
213 stride = gbm_bo_get_pitch(bo);
214 handle = gbm_bo_get_handle(bo).u32;
215
216 ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32,
217 stride, handle, &fb->fb_id);
218 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200219 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300220 free(fb);
221 return NULL;
222 }
223
224 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
225
226 return fb;
227}
228
229static void
230fb_handle_buffer_destroy(struct wl_listener *listener, void *data)
231{
232 struct drm_fb *fb = container_of(listener, struct drm_fb,
233 buffer_destroy_listener);
234
235 fb->buffer = NULL;
236
237 if (fb == fb->output->next ||
238 (fb == fb->output->current && !fb->output->next))
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400239 weston_output_schedule_repaint(&fb->output->base);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300240}
241
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500242static int
243drm_output_prepare_scanout_surface(struct drm_output *output)
244{
245 struct drm_compositor *c =
246 (struct drm_compositor *) output->base.compositor;
247 struct weston_surface *es;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300248 struct gbm_bo *bo;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500249
250 es = container_of(c->base.surface_list.next,
251 struct weston_surface, link);
252
Kristian Høgsberg101cb652012-02-17 10:45:16 -0500253 if (es->geometry.x != output->base.x ||
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200254 es->geometry.y != output->base.y ||
Pekka Paalanen60921e52012-01-25 15:55:43 +0200255 es->geometry.width != output->base.current->width ||
256 es->geometry.height != output->base.current->height ||
Pekka Paalanenf1f5b362012-01-25 14:33:33 +0200257 es->transform.enabled ||
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500258 es->image == EGL_NO_IMAGE_KHR)
259 return -1;
260
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300261 bo = gbm_bo_create_from_egl_image(c->gbm,
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400262 c->base.egl_display,
263 es->image,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300264 es->geometry.width,
265 es->geometry.height,
266 GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500267
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300268 /* Need to verify output->region contained in surface opaque
269 * region. Or maybe just that format doesn't have alpha.
270 * For now, scanout only if format is XRGB8888. */
271 if (gbm_bo_get_format(bo) != GBM_FORMAT_XRGB8888) {
272 gbm_bo_destroy(bo);
273 return -1;
274 }
275
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300276 output->next = drm_fb_get_from_bo(bo, output);
277 if (!output->next) {
278 gbm_bo_destroy(bo);
279 return -1;
280 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500281
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300282 output->next->is_client_buffer = 1;
283 output->next->buffer = es->buffer;
284 output->next->buffer->busy_count++;
285 output->next->buffer_destroy_listener.notify = fb_handle_buffer_destroy;
286
287 wl_signal_add(&output->next->buffer->resource.destroy_signal,
288 &output->next->buffer_destroy_listener);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500289
290 pixman_region32_fini(&es->damage);
291 pixman_region32_init(&es->damage);
292
293 return 0;
294}
295
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500296static void
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400297drm_output_render(struct drm_output *output, pixman_region32_t *damage)
298{
299 struct drm_compositor *compositor =
300 (struct drm_compositor *) output->base.compositor;
301 struct weston_surface *surface;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300302 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400303
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400304 if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
305 output->egl_surface,
306 compositor->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200307 weston_log("failed to make current\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400308 return;
309 }
310
311 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
312 weston_surface_draw(surface, &output->base, damage);
313
Kristian Høgsberge0f832b2012-06-20 00:13:18 -0400314 wl_signal_emit(&output->base.frame_signal, output);
Scott Moreau062be7e2012-04-20 13:37:33 -0600315
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400316 eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300317 bo = gbm_surface_lock_front_buffer(output->surface);
318 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200319 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400320 return;
321 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300322
323 output->next = drm_fb_get_from_bo(bo, output);
324 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200325 weston_log("failed to get drm_fb for bo\n");
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300326 gbm_surface_release_buffer(output->surface, bo);
327 return;
328 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400329}
330
331static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500332drm_output_repaint(struct weston_output *output_base,
333 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100334{
335 struct drm_output *output = (struct drm_output *) output_base;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500336 struct drm_compositor *compositor =
337 (struct drm_compositor *) output->base.compositor;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500338 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400339 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500340 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100341
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500342 drm_output_prepare_scanout_surface(output);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300343 if (!output->next)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400344 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300345 if (!output->next)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400346 return;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100347
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400348 mode = container_of(output->base.current, struct drm_mode, base);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300349 if (!output->current) {
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400350 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300351 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400352 &output->connector_id, 1,
353 &mode->mode_info);
354 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200355 weston_log("set mode failed: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400356 return;
357 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200358 }
359
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500360 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300361 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500362 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200363 weston_log("queueing pageflip failed: %m\n");
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500364 return;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500365 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100366
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300367 output->page_flip_pending = 1;
368
Jesse Barnes58ef3792012-02-23 09:45:49 -0500369 /*
370 * Now, update all the sprite surfaces
371 */
372 wl_list_for_each(s, &compositor->sprite_list, link) {
373 uint32_t flags = 0;
374 drmVBlank vbl = {
375 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
376 .request.sequence = 1,
377 };
378
379 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
380 continue;
381
382 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
383 output->crtc_id, s->pending_fb_id, flags,
384 s->dest_x, s->dest_y,
385 s->dest_w, s->dest_h,
386 s->src_x, s->src_y,
387 s->src_w, s->src_h);
388 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200389 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500390 ret, strerror(errno));
391
392 /*
393 * Queue a vblank signal so we know when the surface
394 * becomes active on the display or has been replaced.
395 */
396 vbl.request.signal = (unsigned long)s;
397 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
398 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200399 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500400 ret, strerror(errno));
401 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300402
403 s->output = output;
404 output->vblank_pending = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500405 }
406
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500407 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400408}
409
410static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500411vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
412 void *data)
413{
414 struct drm_sprite *s = (struct drm_sprite *)data;
415 struct drm_compositor *c = s->compositor;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300416 struct drm_output *output = s->output;
417 uint32_t msecs;
418
419 output->vblank_pending = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500420
421 if (s->surface) {
422 weston_buffer_post_release(s->surface->buffer);
423 wl_list_remove(&s->destroy_listener.link);
424 s->surface = NULL;
425 drmModeRmFB(c->drm.fd, s->fb_id);
426 s->fb_id = 0;
427 }
428
429 if (s->pending_surface) {
430 wl_list_remove(&s->pending_destroy_listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400431 wl_signal_add(&s->pending_surface->buffer->resource.destroy_signal,
432 &s->destroy_listener);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500433 s->surface = s->pending_surface;
434 s->pending_surface = NULL;
435 s->fb_id = s->pending_fb_id;
436 s->pending_fb_id = 0;
437 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300438
439 if (!output->page_flip_pending) {
440 msecs = sec * 1000 + usec / 1000;
441 weston_output_finish_frame(&output->base, msecs);
442 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500443}
444
445static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400446page_flip_handler(int fd, unsigned int frame,
447 unsigned int sec, unsigned int usec, void *data)
448{
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200449 struct drm_output *output = (struct drm_output *) data;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400450 uint32_t msecs;
451
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300452 output->page_flip_pending = 0;
453
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300454 if (output->current) {
455 if (output->current->is_client_buffer)
456 gbm_bo_destroy(output->current->bo);
457 else
458 gbm_surface_release_buffer(output->surface,
459 output->current->bo);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200460 }
461
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300462 output->current = output->next;
463 output->next = NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400464
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300465 if (!output->vblank_pending) {
466 msecs = sec * 1000 + usec / 1000;
467 weston_output_finish_frame(&output->base, msecs);
468 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200469}
470
471static int
Jesse Barnes58ef3792012-02-23 09:45:49 -0500472drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
473{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400474 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500475
476 for (i = 0; i < s->count_formats; i++)
477 if (s->formats[i] == format)
478 return 1;
479
480 return 0;
481}
482
483static int
484drm_surface_transform_supported(struct weston_surface *es)
485{
486 if (es->transform.enabled)
487 return 0;
488
489 return 1;
490}
491
492static int
493drm_surface_overlap_supported(struct weston_output *output_base,
494 pixman_region32_t *overlap)
495{
496 /* We could potentially use a color key here if the surface left
497 * to display has rectangular regions
498 */
499 if (pixman_region32_not_empty(overlap))
500 return 0;
501 return 1;
502}
503
504static void
505drm_disable_unused_sprites(struct weston_output *output_base)
506{
507 struct weston_compositor *ec = output_base->compositor;
508 struct drm_compositor *c =(struct drm_compositor *) ec;
509 struct drm_output *output = (struct drm_output *) output_base;
510 struct drm_sprite *s;
511 int ret;
512
513 wl_list_for_each(s, &c->sprite_list, link) {
514 if (s->pending_fb_id)
515 continue;
516
517 ret = drmModeSetPlane(c->drm.fd, s->plane_id,
518 output->crtc_id, 0, 0,
519 0, 0, 0, 0, 0, 0, 0, 0);
520 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200521 weston_log("failed to disable plane: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500522 ret, strerror(errno));
523 drmModeRmFB(c->drm.fd, s->fb_id);
Ander Conselvan de Oliveirafd1f4c62012-06-26 17:09:14 +0300524
525 if (s->surface) {
526 s->surface = NULL;
527 wl_list_remove(&s->destroy_listener.link);
528 }
529
530 assert(!s->pending_surface);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500531 s->fb_id = 0;
532 s->pending_fb_id = 0;
533 }
534}
535
536/*
537 * This function must take care to damage any previously assigned surface
538 * if the sprite ends up binding to a different surface than in the
539 * previous frame.
540 */
541static int
542drm_output_prepare_overlay_surface(struct weston_output *output_base,
543 struct weston_surface *es,
544 pixman_region32_t *overlap)
545{
546 struct weston_compositor *ec = output_base->compositor;
547 struct drm_compositor *c =(struct drm_compositor *) ec;
548 struct drm_sprite *s;
549 int found = 0;
550 EGLint handle, stride;
551 struct gbm_bo *bo;
552 uint32_t fb_id = 0;
553 uint32_t handles[4], pitches[4], offsets[4];
554 int ret = 0;
555 pixman_region32_t dest_rect, src_rect;
556 pixman_box32_t *box;
557 uint32_t format;
558
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500559 if (c->sprites_are_broken)
560 return -1;
561
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +0300562 if (es->output_mask != (1u << output_base->id))
563 return -1;
564
Jesse Barnes58ef3792012-02-23 09:45:49 -0500565 if (surface_is_primary(ec, es))
566 return -1;
567
Kristian Høgsberg7a011ea2012-03-26 11:48:55 -0400568 if (es->image == EGL_NO_IMAGE_KHR)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500569 return -1;
570
571 if (!drm_surface_transform_supported(es))
572 return -1;
573
574 if (!drm_surface_overlap_supported(output_base, overlap))
575 return -1;
576
577 wl_list_for_each(s, &c->sprite_list, link) {
578 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
579 continue;
580
581 if (!s->pending_fb_id) {
582 found = 1;
583 break;
584 }
585 }
586
587 /* No sprites available */
588 if (!found)
589 return -1;
590
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400591 bo = gbm_bo_create_from_egl_image(c->gbm, c->base.egl_display,
592 es->image, es->geometry.width,
593 es->geometry.height,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500594 GBM_BO_USE_SCANOUT);
595 format = gbm_bo_get_format(bo);
596 handle = gbm_bo_get_handle(bo).s32;
597 stride = gbm_bo_get_pitch(bo);
598
599 gbm_bo_destroy(bo);
600
601 if (!drm_surface_format_supported(s, format))
602 return -1;
603
604 if (!handle)
605 return -1;
606
607 handles[0] = handle;
608 pitches[0] = stride;
609 offsets[0] = 0;
610
611 ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height,
612 format, handles, pitches, offsets,
613 &fb_id, 0);
614 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200615 weston_log("addfb2 failed: %d\n", ret);
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500616 c->sprites_are_broken = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500617 return -1;
618 }
619
620 if (s->surface && s->surface != es) {
621 struct weston_surface *old_surf = s->surface;
622 pixman_region32_fini(&old_surf->damage);
623 pixman_region32_init_rect(&old_surf->damage,
624 old_surf->geometry.x, old_surf->geometry.y,
625 old_surf->geometry.width, old_surf->geometry.height);
626 }
627
628 s->pending_fb_id = fb_id;
629 s->pending_surface = es;
630 es->buffer->busy_count++;
631
632 /*
633 * Calculate the source & dest rects properly based on actual
634 * postion (note the caller has called weston_surface_update_transform()
635 * for us already).
636 */
637 pixman_region32_init(&dest_rect);
638 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
639 &output_base->region);
640 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
641 box = pixman_region32_extents(&dest_rect);
642 s->dest_x = box->x1;
643 s->dest_y = box->y1;
644 s->dest_w = box->x2 - box->x1;
645 s->dest_h = box->y2 - box->y1;
646 pixman_region32_fini(&dest_rect);
647
648 pixman_region32_init(&src_rect);
649 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
650 &output_base->region);
651 pixman_region32_translate(&src_rect, -es->geometry.x, -es->geometry.y);
652 box = pixman_region32_extents(&src_rect);
Rob Clark3c97b902012-03-11 19:48:43 -0500653 s->src_x = box->x1 << 16;
654 s->src_y = box->y1 << 16;
655 s->src_w = (box->x2 - box->x1) << 16;
656 s->src_h = (box->y2 - box->y1) << 16;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500657 pixman_region32_fini(&src_rect);
658
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400659 wl_signal_add(&es->buffer->resource.destroy_signal,
660 &s->pending_destroy_listener);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500661 return 0;
662}
663
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500664static int
665drm_output_set_cursor(struct weston_output *output_base,
Daniel Stone37816df2012-05-16 18:45:18 +0100666 struct weston_seat *es);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500667
668static void
669weston_output_set_cursor(struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +0100670 struct weston_seat *seat,
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500671 pixman_region32_t *overlap)
672{
673 pixman_region32_t cursor_region;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500674
Daniel Stone37816df2012-05-16 18:45:18 +0100675 if (seat->sprite == NULL)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500676 return;
677
678 pixman_region32_init(&cursor_region);
679 pixman_region32_intersect(&cursor_region,
Daniel Stone37816df2012-05-16 18:45:18 +0100680 &seat->sprite->transform.boundingbox,
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500681 &output->region);
682
683 if (!pixman_region32_not_empty(&cursor_region)) {
684 drm_output_set_cursor(output, NULL);
685 goto out;
686 }
687
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500688 if (pixman_region32_not_empty(overlap) ||
Daniel Stone37816df2012-05-16 18:45:18 +0100689 drm_output_set_cursor(output, seat) < 0) {
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -0400690 if (seat->sprite->plane == WESTON_PLANE_DRM_CURSOR) {
Daniel Stone37816df2012-05-16 18:45:18 +0100691 weston_surface_damage(seat->sprite);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500692 drm_output_set_cursor(output, NULL);
693 }
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -0400694 seat->sprite->plane = WESTON_PLANE_PRIMARY;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500695 } else {
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -0400696 if (seat->sprite->plane == WESTON_PLANE_PRIMARY)
Daniel Stone37816df2012-05-16 18:45:18 +0100697 weston_surface_damage_below(seat->sprite);
698 wl_list_remove(&seat->sprite->link);
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -0400699 seat->sprite->plane = WESTON_PLANE_DRM_CURSOR;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500700 }
701
702out:
703 pixman_region32_fini(&cursor_region);
704}
705
Jesse Barnes58ef3792012-02-23 09:45:49 -0500706static void
707drm_assign_planes(struct weston_output *output)
708{
709 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400710 struct weston_surface *es, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500711 pixman_region32_t overlap, surface_overlap;
Daniel Stone37816df2012-05-16 18:45:18 +0100712 struct weston_seat *seat;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500713
714 /*
715 * Find a surface for each sprite in the output using some heuristics:
716 * 1) size
717 * 2) frequency of update
718 * 3) opacity (though some hw might support alpha blending)
719 * 4) clipping (this can be fixed with color keys)
720 *
721 * The idea is to save on blitting since this should save power.
722 * If we can get a large video surface on the sprite for example,
723 * the main display surface may not need to update at all, and
724 * the client buffer can be used directly for the sprite surface
725 * as we do for flipping full screen surfaces.
726 */
Kristian Høgsbergf2735ea2012-06-20 23:03:53 -0400727 seat = (struct weston_seat *) ec->seat;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500728 pixman_region32_init(&overlap);
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400729 wl_list_for_each_safe(es, next, &ec->surface_list, link) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500730 /*
731 * FIXME: try to assign hw cursors here too, they're just
732 * special overlays
733 */
734 pixman_region32_init(&surface_overlap);
735 pixman_region32_intersect(&surface_overlap, &overlap,
736 &es->transform.boundingbox);
737
Daniel Stone37816df2012-05-16 18:45:18 +0100738 if (es == seat->sprite) {
739 weston_output_set_cursor(output, seat,
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500740 &surface_overlap);
741
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -0400742 if (seat->sprite->plane == WESTON_PLANE_PRIMARY)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500743 pixman_region32_union(&overlap, &overlap,
744 &es->transform.boundingbox);
745 } else if (!drm_output_prepare_overlay_surface(output, es,
746 &surface_overlap)) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500747 pixman_region32_fini(&es->damage);
748 pixman_region32_init(&es->damage);
749 } else {
750 pixman_region32_union(&overlap, &overlap,
751 &es->transform.boundingbox);
752 }
753 pixman_region32_fini(&surface_overlap);
754 }
755 pixman_region32_fini(&overlap);
756
Kristian Høgsbergf2735ea2012-06-20 23:03:53 -0400757 if (!seat->sprite || !weston_surface_is_mapped(seat->sprite))
758 drm_output_set_cursor(output, NULL);
759
Jesse Barnes58ef3792012-02-23 09:45:49 -0500760 drm_disable_unused_sprites(output);
761}
762
763static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500764drm_output_set_cursor(struct weston_output *output_base,
Daniel Stone37816df2012-05-16 18:45:18 +0100765 struct weston_seat *es)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200766{
767 struct drm_output *output = (struct drm_output *) output_base;
768 struct drm_compositor *c =
769 (struct drm_compositor *) output->base.compositor;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200770 EGLint handle, stride;
771 int ret = -1;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200772 struct gbm_bo *bo;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400773 uint32_t buf[64 * 64];
774 unsigned char *d, *s, *end;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200775
Daniel Stone37816df2012-05-16 18:45:18 +0100776 if (es == NULL) {
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400777 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
778 return 0;
779 }
780
Daniel Stone37816df2012-05-16 18:45:18 +0100781 if (es->sprite->buffer == NULL ||
782 !wl_buffer_is_shm(es->sprite->buffer))
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200783 goto out;
784
Daniel Stone37816df2012-05-16 18:45:18 +0100785 if (es->sprite->geometry.width > 64 ||
786 es->sprite->geometry.height > 64)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200787 goto out;
Benjamin Franzke117483d2011-08-30 11:38:26 +0200788
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400789 output->current_cursor ^= 1;
790 bo = output->cursor_bo[output->current_cursor];
Kristian Høgsberg6916d9c2012-01-18 15:11:07 -0500791 if (bo == NULL)
792 goto out;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200793
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400794 memset(buf, 0, sizeof buf);
795 d = (unsigned char *) buf;
Daniel Stone37816df2012-05-16 18:45:18 +0100796 stride = wl_shm_buffer_get_stride(es->sprite->buffer);
797 s = wl_shm_buffer_get_data(es->sprite->buffer);
798 end = s + stride * es->sprite->geometry.height;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400799 while (s < end) {
Daniel Stone37816df2012-05-16 18:45:18 +0100800 memcpy(d, s, es->sprite->geometry.width * 4);
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400801 s += stride;
802 d += 64 * 4;
803 }
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200804
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400805 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200806 goto out;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200807
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400808 handle = gbm_bo_get_handle(bo).s32;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200809 ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
810 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200811 weston_log("failed to set cursor: %s\n", strerror(-ret));
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200812 goto out;
813 }
814
815 ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
Daniel Stone37816df2012-05-16 18:45:18 +0100816 es->sprite->geometry.x - output->base.x,
817 es->sprite->geometry.y - output->base.y);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200818 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200819 weston_log("failed to move cursor: %s\n", strerror(-ret));
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200820 goto out;
821 }
822
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200823out:
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200824 if (ret)
825 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
826 return ret;
827}
828
Matt Roper361d2ad2011-08-29 13:52:23 -0700829static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500830drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700831{
832 struct drm_output *output = (struct drm_output *) output_base;
833 struct drm_compositor *c =
Benjamin Franzke117483d2011-08-30 11:38:26 +0200834 (struct drm_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700835 drmModeCrtcPtr origcrtc = output->original_crtc;
Matt Roper361d2ad2011-08-29 13:52:23 -0700836
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200837 if (output->backlight)
838 backlight_destroy(output->backlight);
839
Matt Roper361d2ad2011-08-29 13:52:23 -0700840 /* Turn off hardware cursor */
Benjamin Franzke117483d2011-08-30 11:38:26 +0200841 drm_output_set_cursor(&output->base, NULL);
Matt Roper361d2ad2011-08-29 13:52:23 -0700842
843 /* Restore original CRTC state */
844 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +0200845 origcrtc->x, origcrtc->y,
846 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -0700847 drmModeFreeCrtc(origcrtc);
848
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200849 c->crtc_allocator &= ~(1 << output->crtc_id);
850 c->connector_allocator &= ~(1 << output->connector_id);
851
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400852 eglDestroySurface(c->base.egl_display, output->egl_surface);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400853 gbm_surface_destroy(output->surface);
854
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500855 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200856 wl_list_remove(&output->base.link);
857
Matt Roper361d2ad2011-08-29 13:52:23 -0700858 free(output);
859}
860
Alex Wub7b8bda2012-04-17 17:20:48 +0800861static struct drm_mode *
862choose_mode (struct drm_output *output, struct weston_mode *target_mode)
863{
864 struct drm_mode *tmp_mode = NULL, *mode;
865
866 if (output->base.current->width == target_mode->width &&
867 output->base.current->height == target_mode->height &&
868 (output->base.current->refresh == target_mode->refresh ||
869 target_mode->refresh == 0))
870 return (struct drm_mode *)output->base.current;
871
872 wl_list_for_each(mode, &output->base.mode_list, base.link) {
873 if (mode->mode_info.hdisplay == target_mode->width &&
874 mode->mode_info.vdisplay == target_mode->height) {
875 if (mode->mode_info.vrefresh == target_mode->refresh ||
876 target_mode->refresh == 0) {
877 return mode;
878 } else if (!tmp_mode)
879 tmp_mode = mode;
880 }
881 }
882
883 return tmp_mode;
884}
885
886static int
887drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
888{
889 struct drm_output *output;
890 struct drm_mode *drm_mode;
891 int ret;
892 struct drm_compositor *ec;
893 struct gbm_surface *surface;
894 EGLSurface egl_surface;
895
896 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200897 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800898 return -1;
899 }
900
901 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200902 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800903 return -1;
904 }
905
906 ec = (struct drm_compositor *)output_base->compositor;
907 output = (struct drm_output *)output_base;
908 drm_mode = choose_mode (output, mode);
909
910 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +0200911 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +0800912 return -1;
913 } else if (&drm_mode->base == output->base.current) {
914 return 0;
915 } else if (drm_mode->base.width == output->base.current->width &&
916 drm_mode->base.height == output->base.current->height) {
917 /* only change refresh value */
918 ret = drmModeSetCrtc(ec->drm.fd,
919 output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300920 output->current->fb_id, 0, 0,
Alex Wub7b8bda2012-04-17 17:20:48 +0800921 &output->connector_id, 1, &drm_mode->mode_info);
922
923 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200924 weston_log("failed to set mode (%dx%d) %u Hz\n",
Alex Wub7b8bda2012-04-17 17:20:48 +0800925 drm_mode->base.width,
926 drm_mode->base.height,
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400927 drm_mode->base.refresh / 1000);
Alex Wub7b8bda2012-04-17 17:20:48 +0800928 ret = -1;
929 } else {
930 output->base.current->flags = 0;
931 output->base.current = &drm_mode->base;
932 drm_mode->base.flags =
933 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
934 ret = 0;
935 }
936
937 return ret;
938 }
939
940 drm_mode->base.flags =
941 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
942
943 surface = gbm_surface_create(ec->gbm,
944 drm_mode->base.width,
945 drm_mode->base.height,
946 GBM_FORMAT_XRGB8888,
947 GBM_BO_USE_SCANOUT |
948 GBM_BO_USE_RENDERING);
949 if (!surface) {
Martin Minarik6d118362012-06-07 18:01:59 +0200950 weston_log("failed to create gbm surface\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800951 return -1;
952 }
953
954 egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400955 eglCreateWindowSurface(ec->base.egl_display,
956 ec->base.egl_config,
Alex Wub7b8bda2012-04-17 17:20:48 +0800957 surface, NULL);
958
959 if (egl_surface == EGL_NO_SURFACE) {
Martin Minarik6d118362012-06-07 18:01:59 +0200960 weston_log("failed to create egl surface\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800961 goto err;
962 }
963
964 ret = drmModeSetCrtc(ec->drm.fd,
965 output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300966 output->current->fb_id, 0, 0,
Alex Wub7b8bda2012-04-17 17:20:48 +0800967 &output->connector_id, 1, &drm_mode->mode_info);
968 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200969 weston_log("failed to set mode\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800970 goto err;
971 }
972
973 /* reset rendering stuff. */
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300974 if (output->current) {
975 if (output->current->is_client_buffer)
976 gbm_bo_destroy(output->current->bo);
977 else
978 gbm_surface_release_buffer(output->surface,
979 output->current->bo);
980 }
981 output->current = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +0800982
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300983 if (output->next) {
984 if (output->next->is_client_buffer)
985 gbm_bo_destroy(output->next->bo);
986 else
987 gbm_surface_release_buffer(output->surface,
988 output->next->bo);
989 }
990 output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +0800991
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400992 eglDestroySurface(ec->base.egl_display, output->egl_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +0800993 gbm_surface_destroy(output->surface);
994 output->egl_surface = egl_surface;
995 output->surface = surface;
996
997 /*update output*/
998 output->base.current = &drm_mode->base;
999 output->base.dirty = 1;
1000 weston_output_move(&output->base, output->base.x, output->base.y);
1001 return 0;
1002
1003err:
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001004 eglDestroySurface(ec->base.egl_display, egl_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +08001005 gbm_surface_destroy(surface);
1006 return -1;
1007}
1008
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001009static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001010on_drm_input(int fd, uint32_t mask, void *data)
1011{
1012 drmEventContext evctx;
1013
1014 memset(&evctx, 0, sizeof evctx);
1015 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1016 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001017 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001018 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001019
1020 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001021}
1022
1023static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001024init_egl(struct drm_compositor *ec, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001025{
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001026 EGLint major, minor, n;
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001027 const char *filename, *sysnum;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001028 int fd;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -04001029 static const EGLint context_attribs[] = {
1030 EGL_CONTEXT_CLIENT_VERSION, 2,
1031 EGL_NONE
1032 };
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001033
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001034 static const EGLint config_attribs[] = {
1035 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1036 EGL_RED_SIZE, 1,
1037 EGL_GREEN_SIZE, 1,
1038 EGL_BLUE_SIZE, 1,
1039 EGL_ALPHA_SIZE, 0,
1040 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1041 EGL_NONE
1042 };
1043
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001044 sysnum = udev_device_get_sysnum(device);
1045 if (sysnum)
1046 ec->drm.id = atoi(sysnum);
1047 if (!sysnum || ec->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001048 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001049 return -1;
1050 }
1051
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001052 filename = udev_device_get_devnode(device);
David Herrmann63ff7062011-11-05 18:46:01 +01001053 fd = open(filename, O_RDWR | O_CLOEXEC);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001054 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001055 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001056 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001057 udev_device_get_devnode(device));
1058 return -1;
1059 }
1060
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001061 weston_log("using %s\n", filename);
1062
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001063 ec->drm.fd = fd;
Benjamin Franzke060cf802011-04-30 09:32:11 +02001064 ec->gbm = gbm_create_device(ec->drm.fd);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001065 ec->base.egl_display = eglGetDisplay(ec->gbm);
1066 if (ec->base.egl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001067 weston_log("failed to create display\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001068 return -1;
1069 }
1070
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001071 if (!eglInitialize(ec->base.egl_display, &major, &minor)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001072 weston_log("failed to initialize display\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001073 return -1;
1074 }
1075
Darxus55973f22010-11-22 21:24:39 -05001076 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001077 weston_log("failed to bind api EGL_OPENGL_ES_API\n");
Darxus55973f22010-11-22 21:24:39 -05001078 return -1;
1079 }
1080
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001081 if (!eglChooseConfig(ec->base.egl_display, config_attribs,
1082 &ec->base.egl_config, 1, &n) || n != 1) {
Martin Minarik6d118362012-06-07 18:01:59 +02001083 weston_log("failed to choose config: %d\n", n);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001084 return -1;
1085 }
1086
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001087 ec->base.egl_context =
1088 eglCreateContext(ec->base.egl_display, ec->base.egl_config,
1089 EGL_NO_CONTEXT, context_attribs);
1090 if (ec->base.egl_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001091 weston_log("failed to create context\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001092 return -1;
1093 }
1094
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001095 ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10,
1096 GBM_FORMAT_XRGB8888,
1097 GBM_BO_USE_RENDERING);
1098 if (!ec->dummy_surface) {
Martin Minarik6d118362012-06-07 18:01:59 +02001099 weston_log("failed to create dummy gbm surface\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001100 return -1;
1101 }
1102
1103 ec->dummy_egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001104 eglCreateWindowSurface(ec->base.egl_display,
1105 ec->base.egl_config,
1106 ec->dummy_surface,
1107 NULL);
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001108 if (ec->dummy_egl_surface == EGL_NO_SURFACE) {
Martin Minarik6d118362012-06-07 18:01:59 +02001109 weston_log("failed to create egl surface\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001110 return -1;
1111 }
1112
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001113 if (!eglMakeCurrent(ec->base.egl_display, ec->dummy_egl_surface,
1114 ec->dummy_egl_surface, ec->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001115 weston_log("failed to make context current\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001116 return -1;
1117 }
1118
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001119 return 0;
1120}
1121
1122static drmModeModeInfo builtin_1024x768 = {
1123 63500, /* clock */
1124 1024, 1072, 1176, 1328, 0,
1125 768, 771, 775, 798, 0,
1126 59920,
1127 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
1128 0,
1129 "1024x768"
1130};
1131
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001132
1133static int
1134drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1135{
1136 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001137 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001138
1139 mode = malloc(sizeof *mode);
1140 if (mode == NULL)
1141 return -1;
1142
1143 mode->base.flags = 0;
1144 mode->base.width = info->hdisplay;
1145 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001146
1147 /* Calculate higher precision (mHz) refresh rate */
1148 refresh = (info->clock * 1000000LL / info->htotal +
1149 info->vtotal / 2) / info->vtotal;
1150
1151 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1152 refresh *= 2;
1153 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1154 refresh /= 2;
1155 if (info->vscan > 1)
1156 refresh /= info->vscan;
1157
1158 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001159 mode->mode_info = *info;
1160 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1161
1162 return 0;
1163}
1164
1165static int
1166drm_subpixel_to_wayland(int drm_value)
1167{
1168 switch (drm_value) {
1169 default:
1170 case DRM_MODE_SUBPIXEL_UNKNOWN:
1171 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1172 case DRM_MODE_SUBPIXEL_NONE:
1173 return WL_OUTPUT_SUBPIXEL_NONE;
1174 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1175 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1176 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1177 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1178 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1179 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1180 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1181 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1182 }
1183}
1184
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001185static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001186sprite_handle_buffer_destroy(struct wl_listener *listener, void *data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001187{
1188 struct drm_sprite *sprite =
1189 container_of(listener, struct drm_sprite,
1190 destroy_listener);
1191
1192 sprite->surface = NULL;
1193}
1194
1195static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001196sprite_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001197{
1198 struct drm_sprite *sprite =
1199 container_of(listener, struct drm_sprite,
1200 pending_destroy_listener);
1201
1202 sprite->pending_surface = NULL;
1203}
1204
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001205/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001206static uint32_t
1207drm_get_backlight(struct drm_output *output)
1208{
1209 long brightness, max_brightness, norm;
1210
1211 brightness = backlight_get_brightness(output->backlight);
1212 max_brightness = backlight_get_max_brightness(output->backlight);
1213
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001214 /* convert it on a scale of 0 to 255 */
1215 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001216
1217 return (uint32_t) norm;
1218}
1219
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001220/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001221static void
1222drm_set_backlight(struct weston_output *output_base, uint32_t value)
1223{
1224 struct drm_output *output = (struct drm_output *) output_base;
1225 long max_brightness, new_brightness;
1226
1227 if (!output->backlight)
1228 return;
1229
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001230 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001231 return;
1232
1233 max_brightness = backlight_get_max_brightness(output->backlight);
1234
1235 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001236 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001237
1238 backlight_set_brightness(output->backlight, new_brightness);
1239}
1240
1241static drmModePropertyPtr
1242drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1243{
1244 drmModePropertyPtr props;
1245 int i;
1246
1247 for (i = 0; i < connector->count_props; i++) {
1248 props = drmModeGetProperty(fd, connector->props[i]);
1249 if (!props)
1250 continue;
1251
1252 if (!strcmp(props->name, name))
1253 return props;
1254
1255 drmModeFreeProperty(props);
1256 }
1257
1258 return NULL;
1259}
1260
1261static void
1262drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1263{
1264 struct drm_output *output = (struct drm_output *) output_base;
1265 struct weston_compositor *ec = output_base->compositor;
1266 struct drm_compositor *c = (struct drm_compositor *) ec;
1267 drmModeConnectorPtr connector;
1268 drmModePropertyPtr prop;
1269
1270 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1271 if (!connector)
1272 return;
1273
1274 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1275 if (!prop) {
1276 drmModeFreeConnector(connector);
1277 return;
1278 }
1279
1280 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1281 prop->prop_id, level);
1282 drmModeFreeProperty(prop);
1283 drmModeFreeConnector(connector);
1284}
1285
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001286static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001287create_output_for_connector(struct drm_compositor *ec,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001288 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001289 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001290 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001291{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001292 struct drm_output *output;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001293 struct drm_mode *drm_mode, *next;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001294 drmModeEncoder *encoder;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001295 int i, ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001296
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001297 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001298 if (encoder == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001299 weston_log("No encoder for connector.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001300 return -1;
1301 }
1302
1303 for (i = 0; i < resources->count_crtcs; i++) {
Marty Jack13d9db22011-02-09 19:01:42 -05001304 if (encoder->possible_crtcs & (1 << i) &&
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001305 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001306 break;
1307 }
1308 if (i == resources->count_crtcs) {
Martin Minarik6d118362012-06-07 18:01:59 +02001309 weston_log("No usable crtc for encoder.\n");
David Herrmanneb8bed52011-12-08 17:05:44 +01001310 drmModeFreeEncoder(encoder);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001311 return -1;
1312 }
1313
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001314 output = malloc(sizeof *output);
David Herrmanneb8bed52011-12-08 17:05:44 +01001315 if (output == NULL) {
1316 drmModeFreeEncoder(encoder);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001317 return -1;
David Herrmanneb8bed52011-12-08 17:05:44 +01001318 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001319
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001320 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001321 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1322 output->base.make = "unknown";
1323 output->base.model = "unknown";
1324 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001325
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001326 output->crtc_id = resources->crtcs[i];
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001327 ec->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001328 output->connector_id = connector->connector_id;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001329 ec->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001330
Matt Roper361d2ad2011-08-29 13:52:23 -07001331 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001332 drmModeFreeEncoder(encoder);
Matt Roper361d2ad2011-08-29 13:52:23 -07001333
David Herrmann0f0d54e2011-12-08 17:05:45 +01001334 for (i = 0; i < connector->count_modes; i++) {
1335 ret = drm_output_add_mode(output, &connector->modes[i]);
1336 if (ret)
1337 goto err_free;
1338 }
1339
1340 if (connector->count_modes == 0) {
1341 ret = drm_output_add_mode(output, &builtin_1024x768);
1342 if (ret)
1343 goto err_free;
1344 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001345
1346 drm_mode = container_of(output->base.mode_list.next,
1347 struct drm_mode, base.link);
1348 output->base.current = &drm_mode->base;
1349 drm_mode->base.flags =
1350 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1351
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001352 output->surface = gbm_surface_create(ec->gbm,
1353 output->base.current->width,
1354 output->base.current->height,
1355 GBM_FORMAT_XRGB8888,
1356 GBM_BO_USE_SCANOUT |
1357 GBM_BO_USE_RENDERING);
1358 if (!output->surface) {
Martin Minarik6d118362012-06-07 18:01:59 +02001359 weston_log("failed to create gbm surface\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001360 goto err_free;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001361 }
1362
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001363 output->egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001364 eglCreateWindowSurface(ec->base.egl_display,
1365 ec->base.egl_config,
1366 output->surface,
1367 NULL);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001368 if (output->egl_surface == EGL_NO_SURFACE) {
Martin Minarik6d118362012-06-07 18:01:59 +02001369 weston_log("failed to create egl surface\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001370 goto err_surface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001371 }
1372
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04001373 output->cursor_bo[0] =
1374 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1375 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1376 output->cursor_bo[1] =
1377 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1378 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1379
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001380 output->backlight = backlight_init(drm_device,
1381 connector->connector_type);
1382 if (output->backlight) {
1383 output->base.set_backlight = drm_set_backlight;
1384 output->base.backlight_current = drm_get_backlight(output);
1385 }
1386
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001387 weston_output_init(&output->base, &ec->base, x, y,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001388 connector->mmWidth, connector->mmHeight,
1389 WL_OUTPUT_FLIPPED);
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04001390
1391 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1392
Alex Wubd3354b2012-04-17 17:20:49 +08001393 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001394 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07001395 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001396 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001397 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08001398 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001399
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001400 weston_log("kms connector %d, crtc %d at mode %dx%d@%.1f\n",
1401 output->connector_id, output->crtc_id,
1402 output->base.current->width,
1403 output->base.current->height,
1404 output->base.current->refresh / 1000.0);
1405
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001406 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001407
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001408err_surface:
1409 gbm_surface_destroy(output->surface);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001410err_free:
1411 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1412 base.link) {
1413 wl_list_remove(&drm_mode->base.link);
1414 free(drm_mode);
1415 }
1416
1417 drmModeFreeCrtc(output->original_crtc);
1418 ec->crtc_allocator &= ~(1 << output->crtc_id);
1419 ec->connector_allocator &= ~(1 << output->connector_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001420 free(output);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001421
David Herrmann0f0d54e2011-12-08 17:05:45 +01001422 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001423}
1424
Jesse Barnes58ef3792012-02-23 09:45:49 -05001425static void
1426create_sprites(struct drm_compositor *ec)
1427{
1428 struct drm_sprite *sprite;
1429 drmModePlaneRes *plane_res;
1430 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001431 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001432
1433 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1434 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02001435 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001436 strerror(errno));
1437 return;
1438 }
1439
1440 for (i = 0; i < plane_res->count_planes; i++) {
1441 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1442 if (!plane)
1443 continue;
1444
1445 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1446 plane->count_formats));
1447 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02001448 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001449 __func__);
1450 free(plane);
1451 continue;
1452 }
1453
1454 memset(sprite, 0, sizeof *sprite);
1455
1456 sprite->possible_crtcs = plane->possible_crtcs;
1457 sprite->plane_id = plane->plane_id;
1458 sprite->surface = NULL;
1459 sprite->pending_surface = NULL;
1460 sprite->fb_id = 0;
1461 sprite->pending_fb_id = 0;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001462 sprite->destroy_listener.notify = sprite_handle_buffer_destroy;
1463 sprite->pending_destroy_listener.notify =
Jesse Barnes58ef3792012-02-23 09:45:49 -05001464 sprite_handle_pending_buffer_destroy;
1465 sprite->compositor = ec;
1466 sprite->count_formats = plane->count_formats;
1467 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05001468 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05001469 drmModeFreePlane(plane);
1470
1471 wl_list_insert(&ec->sprite_list, &sprite->link);
1472 }
1473
1474 free(plane_res->planes);
1475 free(plane_res);
1476}
1477
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001478static void
1479destroy_sprites(struct drm_compositor *compositor)
1480{
1481 struct drm_sprite *sprite, *next;
1482 struct drm_output *output;
1483
1484 output = container_of(compositor->base.output_list.next,
1485 struct drm_output, base.link);
1486
1487 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1488 drmModeSetPlane(compositor->drm.fd,
1489 sprite->plane_id,
1490 output->crtc_id, 0, 0,
1491 0, 0, 0, 0, 0, 0, 0, 0);
1492 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1493 free(sprite);
1494 }
1495}
Jesse Barnes58ef3792012-02-23 09:45:49 -05001496
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001497static int
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001498create_outputs(struct drm_compositor *ec, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001499 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001500{
1501 drmModeConnector *connector;
1502 drmModeRes *resources;
1503 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001504 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001505
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001506 resources = drmModeGetResources(ec->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001507 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02001508 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001509 return -1;
1510 }
1511
Jesse Barnes58ef3792012-02-23 09:45:49 -05001512 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001513 if (!ec->crtcs) {
1514 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001515 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001516 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001517
1518 ec->num_crtcs = resources->count_crtcs;
1519 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1520
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001521 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001522 connector = drmModeGetConnector(ec->drm.fd,
1523 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001524 if (connector == NULL)
1525 continue;
1526
1527 if (connector->connection == DRM_MODE_CONNECTED &&
1528 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001529 connector->connector_id == option_connector)) {
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001530 if (create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001531 connector, x, y,
1532 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02001533 drmModeFreeConnector(connector);
1534 continue;
1535 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001536
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001537 x += container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001538 struct weston_output,
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001539 link)->current->width;
1540 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001541
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001542 drmModeFreeConnector(connector);
1543 }
1544
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001545 if (wl_list_empty(&ec->base.output_list)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001546 weston_log("No currently active connector found.\n");
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001547 drmModeFreeResources(resources);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001548 return -1;
1549 }
1550
1551 drmModeFreeResources(resources);
1552
1553 return 0;
1554}
1555
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001556static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001557update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001558{
1559 drmModeConnector *connector;
1560 drmModeRes *resources;
1561 struct drm_output *output, *next;
1562 int x = 0, y = 0;
1563 int x_offset = 0, y_offset = 0;
1564 uint32_t connected = 0, disconnects = 0;
1565 int i;
1566
1567 resources = drmModeGetResources(ec->drm.fd);
1568 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02001569 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001570 return;
1571 }
1572
1573 /* collect new connects */
1574 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001575 int connector_id = resources->connectors[i];
1576
1577 connector = drmModeGetConnector(ec->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01001578 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001579 continue;
1580
David Herrmann7551cff2011-12-08 17:05:43 +01001581 if (connector->connection != DRM_MODE_CONNECTED) {
1582 drmModeFreeConnector(connector);
1583 continue;
1584 }
1585
Benjamin Franzke117483d2011-08-30 11:38:26 +02001586 connected |= (1 << connector_id);
1587
1588 if (!(ec->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001589 struct weston_output *last =
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001590 container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001591 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001592
1593 /* XXX: not yet needed, we die with 0 outputs */
1594 if (!wl_list_empty(&ec->base.output_list))
Benjamin Franzke117483d2011-08-30 11:38:26 +02001595 x = last->x + last->current->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001596 else
1597 x = 0;
1598 y = 0;
1599 create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001600 connector, x, y,
1601 drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02001602 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001603
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001604 }
1605 drmModeFreeConnector(connector);
1606 }
1607 drmModeFreeResources(resources);
1608
1609 disconnects = ec->connector_allocator & ~connected;
1610 if (disconnects) {
1611 wl_list_for_each_safe(output, next, &ec->base.output_list,
1612 base.link) {
1613 if (x_offset != 0 || y_offset != 0) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001614 weston_output_move(&output->base,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001615 output->base.x - x_offset,
1616 output->base.y - y_offset);
1617 }
1618
1619 if (disconnects & (1 << output->connector_id)) {
1620 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02001621 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001622 output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001623 x_offset += output->base.current->width;
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001624 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001625 }
1626 }
1627 }
1628
1629 /* FIXME: handle zero outputs, without terminating */
1630 if (ec->connector_allocator == 0)
1631 wl_display_terminate(ec->base.wl_display);
1632}
1633
1634static int
David Herrmannd7488c22012-03-11 20:05:21 +01001635udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001636{
David Herrmannd7488c22012-03-11 20:05:21 +01001637 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01001638 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01001639
1640 sysnum = udev_device_get_sysnum(device);
1641 if (!sysnum || atoi(sysnum) != ec->drm.id)
1642 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001643
David Herrmann6ac52db2012-03-11 20:05:22 +01001644 val = udev_device_get_property_value(device, "HOTPLUG");
1645 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001646 return 0;
1647
David Herrmann6ac52db2012-03-11 20:05:22 +01001648 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001649}
1650
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001651static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001652udev_drm_event(int fd, uint32_t mask, void *data)
1653{
1654 struct drm_compositor *ec = data;
1655 struct udev_device *event;
1656
1657 event = udev_monitor_receive_device(ec->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001658
David Herrmannd7488c22012-03-11 20:05:21 +01001659 if (udev_event_is_hotplug(ec, event))
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001660 update_outputs(ec, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001661
1662 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001663
1664 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001665}
1666
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001667static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001668drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001669{
1670 struct drm_compositor *d = (struct drm_compositor *) ec;
Daniel Stone37816df2012-05-16 18:45:18 +01001671 struct weston_seat *seat, *next;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001672
Daniel Stone37816df2012-05-16 18:45:18 +01001673 wl_list_for_each_safe(seat, next, &ec->seat_list, link)
1674 evdev_input_destroy(seat);
Jonas Ådahlc97af922012-03-28 22:36:09 +02001675
1676 wl_event_source_remove(d->udev_drm_source);
1677 wl_event_source_remove(d->drm_source);
1678
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001679 weston_compositor_shutdown(ec);
Jonas Ådahlc97af922012-03-28 22:36:09 +02001680
Ander Conselvan de Oliveira5f5f3192012-04-30 13:31:28 +03001681 /* Work around crash in egl_dri2.c's dri2_make_current() */
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001682 eglMakeCurrent(ec->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
Ander Conselvan de Oliveira5f5f3192012-04-30 13:31:28 +03001683 EGL_NO_CONTEXT);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001684 eglTerminate(ec->egl_display);
Ander Conselvan de Oliveira5f5f3192012-04-30 13:31:28 +03001685 eglReleaseThread();
1686
Matt Roper361d2ad2011-08-29 13:52:23 -07001687 gbm_device_destroy(d->gbm);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001688 destroy_sprites(d);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001689 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +02001690 weston_log("failed to drop master: %m\n");
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001691 tty_destroy(d->tty);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001692
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001693 free(d);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001694}
1695
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001696static void
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001697drm_compositor_set_modes(struct drm_compositor *compositor)
1698{
1699 struct drm_output *output;
1700 struct drm_mode *drm_mode;
1701 int ret;
1702
1703 wl_list_for_each(output, &compositor->base.output_list, base.link) {
1704 drm_mode = (struct drm_mode *) output->base.current;
1705 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001706 output->current->fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001707 &output->connector_id, 1,
1708 &drm_mode->mode_info);
1709 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001710 weston_log(
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001711 "failed to set mode %dx%d for output at %d,%d: %m\n",
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001712 drm_mode->base.width, drm_mode->base.height,
1713 output->base.x, output->base.y);
1714 }
1715 }
1716}
1717
1718static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001719vt_func(struct weston_compositor *compositor, int event)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001720{
1721 struct drm_compositor *ec = (struct drm_compositor *) compositor;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001722 struct weston_output *output;
Daniel Stone37816df2012-05-16 18:45:18 +01001723 struct weston_seat *seat;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001724 struct drm_sprite *sprite;
1725 struct drm_output *drm_output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001726
1727 switch (event) {
1728 case TTY_ENTER_VT:
1729 compositor->focus = 1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001730 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001731 weston_log("failed to set master: %m\n");
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001732 wl_display_terminate(compositor->wl_display);
1733 }
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001734 compositor->state = ec->prev_state;
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001735 drm_compositor_set_modes(ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001736 weston_compositor_damage_all(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001737 wl_list_for_each(seat, &compositor->seat_list, link) {
1738 evdev_add_devices(ec->udev, seat);
1739 evdev_enable_udev_monitor(ec->udev, seat);
Benjamin Franzke78d3afe2012-04-09 18:14:58 +02001740 }
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001741 break;
1742 case TTY_LEAVE_VT:
Daniel Stone37816df2012-05-16 18:45:18 +01001743 wl_list_for_each(seat, &compositor->seat_list, link) {
1744 evdev_disable_udev_monitor(seat);
1745 evdev_remove_devices(seat);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04001746 }
1747
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001748 compositor->focus = 0;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001749 ec->prev_state = compositor->state;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001750 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04001751
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001752 /* If we have a repaint scheduled (either from a
1753 * pending pageflip or the idle handler), make sure we
1754 * cancel that so we don't try to pageflip when we're
1755 * vt switched away. The SLEEPING state will prevent
1756 * further attemps at repainting. When we switch
1757 * back, we schedule a repaint, which will process
1758 * pending frame callbacks. */
1759
1760 wl_list_for_each(output, &ec->base.output_list, link) {
1761 output->repaint_needed = 0;
1762 drm_output_set_cursor(output, NULL);
1763 }
1764
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001765 drm_output = container_of(ec->base.output_list.next,
1766 struct drm_output, base.link);
1767
1768 wl_list_for_each(sprite, &ec->sprite_list, link)
1769 drmModeSetPlane(ec->drm.fd,
1770 sprite->plane_id,
1771 drm_output->crtc_id, 0, 0,
1772 0, 0, 0, 0, 0, 0, 0, 0);
1773
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001774 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +02001775 weston_log("failed to drop master: %m\n");
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001776
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001777 break;
1778 };
1779}
1780
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001781static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001782switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001783{
1784 struct drm_compositor *ec = data;
1785
Daniel Stone325fc2d2012-05-30 16:31:58 +01001786 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001787}
1788
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001789static const char default_seat[] = "seat0";
1790
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001791static struct weston_compositor *
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001792drm_compositor_create(struct wl_display *display,
Daniel Stonebaf43592012-06-01 11:11:10 -04001793 int connector, const char *seat, int tty,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001794 int argc, char *argv[], const char *config_file)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001795{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001796 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001797 struct udev_enumerate *e;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001798 struct udev_list_entry *entry;
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001799 struct udev_device *device, *drm_device;
1800 const char *path, *device_seat;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001801 struct wl_event_loop *loop;
Daniel Stonea96b93c2012-06-22 14:04:37 +01001802 struct weston_seat *weston_seat, *next;
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001803 uint32_t key;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001804
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001805 weston_log("initializing drm backend\n");
1806
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001807 ec = malloc(sizeof *ec);
1808 if (ec == NULL)
1809 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001810 memset(ec, 0, sizeof *ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01001811
1812 if (weston_compositor_init(&ec->base, display, argc, argv,
Daniel Stonea96b93c2012-06-22 14:04:37 +01001813 config_file) < 0) {
1814 weston_log("weston_compositor_init failed\n");
1815 goto err_base;
1816 }
Daniel Stone725c2c32012-06-22 14:04:36 +01001817
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001818 ec->udev = udev_new();
1819 if (ec->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001820 weston_log("failed to initialize udev context\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001821 goto err_compositor;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001822 }
1823
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05001824 ec->base.wl_display = display;
1825 ec->tty = tty_create(&ec->base, vt_func, tty);
1826 if (!ec->tty) {
Martin Minarik6d118362012-06-07 18:01:59 +02001827 weston_log("failed to initialize tty\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001828 goto err_udev;
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05001829 }
1830
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001831 e = udev_enumerate_new(ec->udev);
1832 udev_enumerate_add_match_subsystem(e, "drm");
Benjamin Franzkea764ee52011-10-07 08:23:22 +02001833 udev_enumerate_add_match_sysname(e, "card[0-9]*");
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001834
Benjamin Franzke117483d2011-08-30 11:38:26 +02001835 udev_enumerate_scan_devices(e);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001836 drm_device = NULL;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001837 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001838 path = udev_list_entry_get_name(entry);
1839 device = udev_device_new_from_syspath(ec->udev, path);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001840 device_seat =
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001841 udev_device_get_property_value(device, "ID_SEAT");
1842 if (!device_seat)
1843 device_seat = default_seat;
1844 if (strcmp(device_seat, seat) == 0) {
1845 drm_device = device;
1846 break;
1847 }
1848 udev_device_unref(device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001849 }
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001850
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001851 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001852 weston_log("no drm device found\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001853 goto err_udev_enum;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001854 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001855
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001856 if (init_egl(ec, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001857 weston_log("failed to initialize egl\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001858 goto err_udev_dev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001859 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001860
1861 ec->base.destroy = drm_destroy;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001862
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001863 ec->base.focus = 1;
1864
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001865 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001866
Daniel Stone725c2c32012-06-22 14:04:36 +01001867 if (weston_compositor_init_gl(&ec->base) < 0)
Daniel Stonea96b93c2012-06-22 14:04:37 +01001868 goto err_egl;
Benjamin Franzked59eb1c2011-04-29 22:14:54 +02001869
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001870 for (key = KEY_F1; key < KEY_F9; key++)
Daniel Stone325fc2d2012-05-30 16:31:58 +01001871 weston_compositor_add_key_binding(&ec->base, key,
1872 MODIFIER_CTRL | MODIFIER_ALT,
1873 switch_vt_binding, ec);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001874
Jesse Barnes58ef3792012-02-23 09:45:49 -05001875 wl_list_init(&ec->sprite_list);
1876 create_sprites(ec);
1877
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001878 if (create_outputs(ec, connector, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001879 weston_log("failed to create output for %s\n", path);
Daniel Stonea96b93c2012-06-22 14:04:37 +01001880 goto err_sprite;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001881 }
1882
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02001883 path = NULL;
1884
Tiago Vignattice03ec32011-12-19 01:14:03 +02001885 evdev_input_create(&ec->base, ec->udev, seat);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001886
1887 loop = wl_display_get_event_loop(ec->base.wl_display);
1888 ec->drm_source =
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001889 wl_event_loop_add_fd(loop, ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001890 WL_EVENT_READABLE, on_drm_input, ec);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001891
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001892 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
1893 if (ec->udev_monitor == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001894 weston_log("failed to intialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001895 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001896 }
1897 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
1898 "drm", NULL);
1899 ec->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02001900 wl_event_loop_add_fd(loop,
1901 udev_monitor_get_fd(ec->udev_monitor),
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001902 WL_EVENT_READABLE, udev_drm_event, ec);
1903
1904 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001905 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001906 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001907 }
1908
Daniel Stonea96b93c2012-06-22 14:04:37 +01001909 udev_device_unref(drm_device);
1910 udev_enumerate_unref(e);
1911
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001912 return &ec->base;
Daniel Stonea96b93c2012-06-22 14:04:37 +01001913
1914err_udev_monitor:
1915 wl_event_source_remove(ec->udev_drm_source);
1916 udev_monitor_unref(ec->udev_monitor);
1917err_drm_source:
1918 wl_event_source_remove(ec->drm_source);
1919 wl_list_for_each_safe(weston_seat, next, &ec->base.seat_list, link)
1920 evdev_input_destroy(weston_seat);
1921err_sprite:
1922 destroy_sprites(ec);
1923err_egl:
1924 eglMakeCurrent(ec->base.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
1925 EGL_NO_CONTEXT);
1926 eglTerminate(ec->base.egl_display);
1927 eglReleaseThread();
1928 gbm_device_destroy(ec->gbm);
1929err_udev_dev:
1930 udev_device_unref(drm_device);
1931err_udev_enum:
1932 udev_enumerate_unref(e);
1933 tty_destroy(ec->tty);
1934err_udev:
1935 udev_unref(ec->udev);
1936err_compositor:
1937 weston_compositor_shutdown(&ec->base);
1938err_base:
1939 free(ec);
1940 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001941}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001942
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001943WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -04001944backend_init(struct wl_display *display, int argc, char *argv[],
1945 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001946{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001947 int connector = 0, tty = 0;
1948 const char *seat = default_seat;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001949
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001950 const struct weston_option drm_options[] = {
1951 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
1952 { WESTON_OPTION_STRING, "seat", 0, &seat },
1953 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
1954 };
Benjamin Franzke117483d2011-08-30 11:38:26 +02001955
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001956 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001957
Daniel Stonec1be8e52012-06-01 11:14:02 -04001958 return drm_compositor_create(display, connector, seat, tty, argc, argv,
1959 config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001960}