blob: d969d5217379e4f21161390d00839f2576d9f5e5 [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
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300101 int vblank_pending;
102 int page_flip_pending;
103
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400104 struct gbm_surface *surface;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400105 struct gbm_bo *cursor_bo[2];
106 int current_cursor;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400107 EGLSurface egl_surface;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300108 struct drm_fb *current, *next;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200109 struct backlight *backlight;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400110};
111
Jesse Barnes58ef3792012-02-23 09:45:49 -0500112/*
113 * An output has a primary display plane plus zero or more sprites for
114 * blending display contents.
115 */
116struct drm_sprite {
117 struct wl_list link;
118
119 uint32_t fb_id;
120 uint32_t pending_fb_id;
121 struct weston_surface *surface;
122 struct weston_surface *pending_surface;
123
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300124 struct drm_output *output;
125
Jesse Barnes58ef3792012-02-23 09:45:49 -0500126 struct drm_compositor *compositor;
127
128 struct wl_listener destroy_listener;
129 struct wl_listener pending_destroy_listener;
130
131 uint32_t possible_crtcs;
132 uint32_t plane_id;
133 uint32_t count_formats;
134
135 int32_t src_x, src_y;
136 uint32_t src_w, src_h;
137 uint32_t dest_x, dest_y;
138 uint32_t dest_w, dest_h;
139
140 uint32_t formats[];
141};
142
143static int
144surface_is_primary(struct weston_compositor *ec, struct weston_surface *es)
145{
146 struct weston_surface *primary;
147
148 primary = container_of(ec->surface_list.next, struct weston_surface,
149 link);
150 if (es == primary)
151 return -1;
152 return 0;
153}
154
155static int
156drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
157{
158 struct weston_compositor *ec = output_base->compositor;
159 struct drm_compositor *c =(struct drm_compositor *) ec;
160 struct drm_output *output = (struct drm_output *) output_base;
161 int crtc;
162
163 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
164 if (c->crtcs[crtc] != output->crtc_id)
165 continue;
166
167 if (supported & (1 << crtc))
168 return -1;
169 }
170
171 return 0;
172}
173
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300174static void
175drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
176{
177 struct drm_fb *fb = data;
178 struct gbm_device *gbm = gbm_bo_get_device(bo);
179
180 if (fb->fb_id)
181 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
182
183 if (fb->buffer) {
184 weston_buffer_post_release(fb->buffer);
185 wl_list_remove(&fb->buffer_destroy_listener.link);
186 }
187
188 free(data);
189}
190
191static struct drm_fb *
192drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_output *output)
193{
194 struct drm_fb *fb = gbm_bo_get_user_data(bo);
195 struct drm_compositor *compositor =
196 (struct drm_compositor *) output->base.compositor;
197 uint32_t width, height, stride, handle;
198 int ret;
199
200 if (fb)
201 return fb;
202
203 fb = malloc(sizeof *fb);
204
205 fb->bo = bo;
206 fb->output = output;
207 fb->is_client_buffer = 0;
208 fb->buffer = NULL;
209
210 width = gbm_bo_get_width(bo);
211 height = gbm_bo_get_height(bo);
212 stride = gbm_bo_get_pitch(bo);
213 handle = gbm_bo_get_handle(bo).u32;
214
215 ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32,
216 stride, handle, &fb->fb_id);
217 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200218 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300219 free(fb);
220 return NULL;
221 }
222
223 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
224
225 return fb;
226}
227
228static void
229fb_handle_buffer_destroy(struct wl_listener *listener, void *data)
230{
231 struct drm_fb *fb = container_of(listener, struct drm_fb,
232 buffer_destroy_listener);
233
234 fb->buffer = NULL;
235
236 if (fb == fb->output->next ||
237 (fb == fb->output->current && !fb->output->next))
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400238 weston_output_schedule_repaint(&fb->output->base);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300239}
240
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500241static int
242drm_output_prepare_scanout_surface(struct drm_output *output)
243{
244 struct drm_compositor *c =
245 (struct drm_compositor *) output->base.compositor;
246 struct weston_surface *es;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300247 struct gbm_bo *bo;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500248
249 es = container_of(c->base.surface_list.next,
250 struct weston_surface, link);
251
Kristian Høgsberg101cb652012-02-17 10:45:16 -0500252 if (es->geometry.x != output->base.x ||
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200253 es->geometry.y != output->base.y ||
Pekka Paalanen60921e52012-01-25 15:55:43 +0200254 es->geometry.width != output->base.current->width ||
255 es->geometry.height != output->base.current->height ||
Pekka Paalanenf1f5b362012-01-25 14:33:33 +0200256 es->transform.enabled ||
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500257 es->image == EGL_NO_IMAGE_KHR)
258 return -1;
259
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300260 bo = gbm_bo_create_from_egl_image(c->gbm,
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400261 c->base.egl_display,
262 es->image,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300263 es->geometry.width,
264 es->geometry.height,
265 GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500266
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300267 /* Need to verify output->region contained in surface opaque
268 * region. Or maybe just that format doesn't have alpha.
269 * For now, scanout only if format is XRGB8888. */
270 if (gbm_bo_get_format(bo) != GBM_FORMAT_XRGB8888) {
271 gbm_bo_destroy(bo);
272 return -1;
273 }
274
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300275 output->next = drm_fb_get_from_bo(bo, output);
276 if (!output->next) {
277 gbm_bo_destroy(bo);
278 return -1;
279 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500280
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300281 output->next->is_client_buffer = 1;
282 output->next->buffer = es->buffer;
283 output->next->buffer->busy_count++;
284 output->next->buffer_destroy_listener.notify = fb_handle_buffer_destroy;
285
286 wl_signal_add(&output->next->buffer->resource.destroy_signal,
287 &output->next->buffer_destroy_listener);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500288
289 pixman_region32_fini(&es->damage);
290 pixman_region32_init(&es->damage);
291
292 return 0;
293}
294
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500295static void
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400296drm_output_render(struct drm_output *output, pixman_region32_t *damage)
297{
298 struct drm_compositor *compositor =
299 (struct drm_compositor *) output->base.compositor;
300 struct weston_surface *surface;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300301 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400302
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400303 if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
304 output->egl_surface,
305 compositor->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200306 weston_log("failed to make current\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400307 return;
308 }
309
310 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
311 weston_surface_draw(surface, &output->base, damage);
312
Kristian Høgsberge0f832b2012-06-20 00:13:18 -0400313 wl_signal_emit(&output->base.frame_signal, output);
Scott Moreau062be7e2012-04-20 13:37:33 -0600314
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400315 eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300316 bo = gbm_surface_lock_front_buffer(output->surface);
317 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200318 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400319 return;
320 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300321
322 output->next = drm_fb_get_from_bo(bo, output);
323 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200324 weston_log("failed to get drm_fb for bo\n");
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300325 gbm_surface_release_buffer(output->surface, bo);
326 return;
327 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400328}
329
330static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500331drm_output_repaint(struct weston_output *output_base,
332 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100333{
334 struct drm_output *output = (struct drm_output *) output_base;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500335 struct drm_compositor *compositor =
336 (struct drm_compositor *) output->base.compositor;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500337 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400338 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500339 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100340
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500341 drm_output_prepare_scanout_surface(output);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300342 if (!output->next)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400343 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300344 if (!output->next)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400345 return;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100346
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400347 mode = container_of(output->base.current, struct drm_mode, base);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300348 if (!output->current) {
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400349 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300350 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400351 &output->connector_id, 1,
352 &mode->mode_info);
353 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200354 weston_log("set mode failed: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400355 return;
356 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200357 }
358
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500359 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300360 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500361 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200362 weston_log("queueing pageflip failed: %m\n");
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500363 return;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500364 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100365
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300366 output->page_flip_pending = 1;
367
Jesse Barnes58ef3792012-02-23 09:45:49 -0500368 /*
369 * Now, update all the sprite surfaces
370 */
371 wl_list_for_each(s, &compositor->sprite_list, link) {
372 uint32_t flags = 0;
373 drmVBlank vbl = {
374 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
375 .request.sequence = 1,
376 };
377
378 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
379 continue;
380
381 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
382 output->crtc_id, s->pending_fb_id, flags,
383 s->dest_x, s->dest_y,
384 s->dest_w, s->dest_h,
385 s->src_x, s->src_y,
386 s->src_w, s->src_h);
387 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200388 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500389 ret, strerror(errno));
390
391 /*
392 * Queue a vblank signal so we know when the surface
393 * becomes active on the display or has been replaced.
394 */
395 vbl.request.signal = (unsigned long)s;
396 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
397 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200398 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500399 ret, strerror(errno));
400 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300401
402 s->output = output;
403 output->vblank_pending = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500404 }
405
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500406 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400407}
408
409static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500410vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
411 void *data)
412{
413 struct drm_sprite *s = (struct drm_sprite *)data;
414 struct drm_compositor *c = s->compositor;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300415 struct drm_output *output = s->output;
416 uint32_t msecs;
417
418 output->vblank_pending = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500419
420 if (s->surface) {
421 weston_buffer_post_release(s->surface->buffer);
422 wl_list_remove(&s->destroy_listener.link);
423 s->surface = NULL;
424 drmModeRmFB(c->drm.fd, s->fb_id);
425 s->fb_id = 0;
426 }
427
428 if (s->pending_surface) {
429 wl_list_remove(&s->pending_destroy_listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400430 wl_signal_add(&s->pending_surface->buffer->resource.destroy_signal,
431 &s->destroy_listener);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500432 s->surface = s->pending_surface;
433 s->pending_surface = NULL;
434 s->fb_id = s->pending_fb_id;
435 s->pending_fb_id = 0;
436 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300437
438 if (!output->page_flip_pending) {
439 msecs = sec * 1000 + usec / 1000;
440 weston_output_finish_frame(&output->base, msecs);
441 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500442}
443
444static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400445page_flip_handler(int fd, unsigned int frame,
446 unsigned int sec, unsigned int usec, void *data)
447{
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200448 struct drm_output *output = (struct drm_output *) data;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400449 uint32_t msecs;
450
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300451 output->page_flip_pending = 0;
452
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300453 if (output->current) {
454 if (output->current->is_client_buffer)
455 gbm_bo_destroy(output->current->bo);
456 else
457 gbm_surface_release_buffer(output->surface,
458 output->current->bo);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200459 }
460
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300461 output->current = output->next;
462 output->next = NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400463
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300464 if (!output->vblank_pending) {
465 msecs = sec * 1000 + usec / 1000;
466 weston_output_finish_frame(&output->base, msecs);
467 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200468}
469
470static int
Jesse Barnes58ef3792012-02-23 09:45:49 -0500471drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
472{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400473 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500474
475 for (i = 0; i < s->count_formats; i++)
476 if (s->formats[i] == format)
477 return 1;
478
479 return 0;
480}
481
482static int
483drm_surface_transform_supported(struct weston_surface *es)
484{
485 if (es->transform.enabled)
486 return 0;
487
488 return 1;
489}
490
491static int
492drm_surface_overlap_supported(struct weston_output *output_base,
493 pixman_region32_t *overlap)
494{
495 /* We could potentially use a color key here if the surface left
496 * to display has rectangular regions
497 */
498 if (pixman_region32_not_empty(overlap))
499 return 0;
500 return 1;
501}
502
503static void
504drm_disable_unused_sprites(struct weston_output *output_base)
505{
506 struct weston_compositor *ec = output_base->compositor;
507 struct drm_compositor *c =(struct drm_compositor *) ec;
508 struct drm_output *output = (struct drm_output *) output_base;
509 struct drm_sprite *s;
510 int ret;
511
512 wl_list_for_each(s, &c->sprite_list, link) {
513 if (s->pending_fb_id)
514 continue;
515
516 ret = drmModeSetPlane(c->drm.fd, s->plane_id,
517 output->crtc_id, 0, 0,
518 0, 0, 0, 0, 0, 0, 0, 0);
519 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200520 weston_log("failed to disable plane: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500521 ret, strerror(errno));
522 drmModeRmFB(c->drm.fd, s->fb_id);
523 s->surface = NULL;
524 s->pending_surface = NULL;
525 s->fb_id = 0;
526 s->pending_fb_id = 0;
527 }
528}
529
530/*
531 * This function must take care to damage any previously assigned surface
532 * if the sprite ends up binding to a different surface than in the
533 * previous frame.
534 */
535static int
536drm_output_prepare_overlay_surface(struct weston_output *output_base,
537 struct weston_surface *es,
538 pixman_region32_t *overlap)
539{
540 struct weston_compositor *ec = output_base->compositor;
541 struct drm_compositor *c =(struct drm_compositor *) ec;
542 struct drm_sprite *s;
543 int found = 0;
544 EGLint handle, stride;
545 struct gbm_bo *bo;
546 uint32_t fb_id = 0;
547 uint32_t handles[4], pitches[4], offsets[4];
548 int ret = 0;
549 pixman_region32_t dest_rect, src_rect;
550 pixman_box32_t *box;
551 uint32_t format;
552
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500553 if (c->sprites_are_broken)
554 return -1;
555
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +0300556 if (es->output_mask != (1u << output_base->id))
557 return -1;
558
Jesse Barnes58ef3792012-02-23 09:45:49 -0500559 if (surface_is_primary(ec, es))
560 return -1;
561
Kristian Høgsberg7a011ea2012-03-26 11:48:55 -0400562 if (es->image == EGL_NO_IMAGE_KHR)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500563 return -1;
564
565 if (!drm_surface_transform_supported(es))
566 return -1;
567
568 if (!drm_surface_overlap_supported(output_base, overlap))
569 return -1;
570
571 wl_list_for_each(s, &c->sprite_list, link) {
572 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
573 continue;
574
575 if (!s->pending_fb_id) {
576 found = 1;
577 break;
578 }
579 }
580
581 /* No sprites available */
582 if (!found)
583 return -1;
584
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400585 bo = gbm_bo_create_from_egl_image(c->gbm, c->base.egl_display,
586 es->image, es->geometry.width,
587 es->geometry.height,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500588 GBM_BO_USE_SCANOUT);
589 format = gbm_bo_get_format(bo);
590 handle = gbm_bo_get_handle(bo).s32;
591 stride = gbm_bo_get_pitch(bo);
592
593 gbm_bo_destroy(bo);
594
595 if (!drm_surface_format_supported(s, format))
596 return -1;
597
598 if (!handle)
599 return -1;
600
601 handles[0] = handle;
602 pitches[0] = stride;
603 offsets[0] = 0;
604
605 ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height,
606 format, handles, pitches, offsets,
607 &fb_id, 0);
608 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200609 weston_log("addfb2 failed: %d\n", ret);
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500610 c->sprites_are_broken = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500611 return -1;
612 }
613
614 if (s->surface && s->surface != es) {
615 struct weston_surface *old_surf = s->surface;
616 pixman_region32_fini(&old_surf->damage);
617 pixman_region32_init_rect(&old_surf->damage,
618 old_surf->geometry.x, old_surf->geometry.y,
619 old_surf->geometry.width, old_surf->geometry.height);
620 }
621
622 s->pending_fb_id = fb_id;
623 s->pending_surface = es;
624 es->buffer->busy_count++;
625
626 /*
627 * Calculate the source & dest rects properly based on actual
628 * postion (note the caller has called weston_surface_update_transform()
629 * for us already).
630 */
631 pixman_region32_init(&dest_rect);
632 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
633 &output_base->region);
634 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
635 box = pixman_region32_extents(&dest_rect);
636 s->dest_x = box->x1;
637 s->dest_y = box->y1;
638 s->dest_w = box->x2 - box->x1;
639 s->dest_h = box->y2 - box->y1;
640 pixman_region32_fini(&dest_rect);
641
642 pixman_region32_init(&src_rect);
643 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
644 &output_base->region);
645 pixman_region32_translate(&src_rect, -es->geometry.x, -es->geometry.y);
646 box = pixman_region32_extents(&src_rect);
Rob Clark3c97b902012-03-11 19:48:43 -0500647 s->src_x = box->x1 << 16;
648 s->src_y = box->y1 << 16;
649 s->src_w = (box->x2 - box->x1) << 16;
650 s->src_h = (box->y2 - box->y1) << 16;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500651 pixman_region32_fini(&src_rect);
652
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400653 wl_signal_add(&es->buffer->resource.destroy_signal,
654 &s->pending_destroy_listener);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500655 return 0;
656}
657
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500658static int
659drm_output_set_cursor(struct weston_output *output_base,
Daniel Stone37816df2012-05-16 18:45:18 +0100660 struct weston_seat *es);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500661
662static void
663weston_output_set_cursor(struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +0100664 struct weston_seat *seat,
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500665 pixman_region32_t *overlap)
666{
667 pixman_region32_t cursor_region;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500668
Daniel Stone37816df2012-05-16 18:45:18 +0100669 if (seat->sprite == NULL)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500670 return;
671
672 pixman_region32_init(&cursor_region);
673 pixman_region32_intersect(&cursor_region,
Daniel Stone37816df2012-05-16 18:45:18 +0100674 &seat->sprite->transform.boundingbox,
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500675 &output->region);
676
677 if (!pixman_region32_not_empty(&cursor_region)) {
678 drm_output_set_cursor(output, NULL);
679 goto out;
680 }
681
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500682 if (pixman_region32_not_empty(overlap) ||
Daniel Stone37816df2012-05-16 18:45:18 +0100683 drm_output_set_cursor(output, seat) < 0) {
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -0400684 if (seat->sprite->plane == WESTON_PLANE_DRM_CURSOR) {
Daniel Stone37816df2012-05-16 18:45:18 +0100685 weston_surface_damage(seat->sprite);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500686 drm_output_set_cursor(output, NULL);
687 }
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -0400688 seat->sprite->plane = WESTON_PLANE_PRIMARY;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500689 } else {
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -0400690 if (seat->sprite->plane == WESTON_PLANE_PRIMARY)
Daniel Stone37816df2012-05-16 18:45:18 +0100691 weston_surface_damage_below(seat->sprite);
692 wl_list_remove(&seat->sprite->link);
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -0400693 seat->sprite->plane = WESTON_PLANE_DRM_CURSOR;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500694 }
695
696out:
697 pixman_region32_fini(&cursor_region);
698}
699
Jesse Barnes58ef3792012-02-23 09:45:49 -0500700static void
701drm_assign_planes(struct weston_output *output)
702{
703 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400704 struct weston_surface *es, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500705 pixman_region32_t overlap, surface_overlap;
Daniel Stone37816df2012-05-16 18:45:18 +0100706 struct weston_seat *seat;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500707
708 /*
709 * Find a surface for each sprite in the output using some heuristics:
710 * 1) size
711 * 2) frequency of update
712 * 3) opacity (though some hw might support alpha blending)
713 * 4) clipping (this can be fixed with color keys)
714 *
715 * The idea is to save on blitting since this should save power.
716 * If we can get a large video surface on the sprite for example,
717 * the main display surface may not need to update at all, and
718 * the client buffer can be used directly for the sprite surface
719 * as we do for flipping full screen surfaces.
720 */
Kristian Høgsbergf2735ea2012-06-20 23:03:53 -0400721 seat = (struct weston_seat *) ec->seat;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500722 pixman_region32_init(&overlap);
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400723 wl_list_for_each_safe(es, next, &ec->surface_list, link) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500724 /*
725 * FIXME: try to assign hw cursors here too, they're just
726 * special overlays
727 */
728 pixman_region32_init(&surface_overlap);
729 pixman_region32_intersect(&surface_overlap, &overlap,
730 &es->transform.boundingbox);
731
Daniel Stone37816df2012-05-16 18:45:18 +0100732 if (es == seat->sprite) {
733 weston_output_set_cursor(output, seat,
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500734 &surface_overlap);
735
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -0400736 if (seat->sprite->plane == WESTON_PLANE_PRIMARY)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500737 pixman_region32_union(&overlap, &overlap,
738 &es->transform.boundingbox);
739 } else if (!drm_output_prepare_overlay_surface(output, es,
740 &surface_overlap)) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500741 pixman_region32_fini(&es->damage);
742 pixman_region32_init(&es->damage);
743 } else {
744 pixman_region32_union(&overlap, &overlap,
745 &es->transform.boundingbox);
746 }
747 pixman_region32_fini(&surface_overlap);
748 }
749 pixman_region32_fini(&overlap);
750
Kristian Høgsbergf2735ea2012-06-20 23:03:53 -0400751 if (!seat->sprite || !weston_surface_is_mapped(seat->sprite))
752 drm_output_set_cursor(output, NULL);
753
Jesse Barnes58ef3792012-02-23 09:45:49 -0500754 drm_disable_unused_sprites(output);
755}
756
757static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500758drm_output_set_cursor(struct weston_output *output_base,
Daniel Stone37816df2012-05-16 18:45:18 +0100759 struct weston_seat *es)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200760{
761 struct drm_output *output = (struct drm_output *) output_base;
762 struct drm_compositor *c =
763 (struct drm_compositor *) output->base.compositor;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200764 EGLint handle, stride;
765 int ret = -1;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200766 struct gbm_bo *bo;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400767 uint32_t buf[64 * 64];
768 unsigned char *d, *s, *end;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200769
Daniel Stone37816df2012-05-16 18:45:18 +0100770 if (es == NULL) {
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400771 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
772 return 0;
773 }
774
Daniel Stone37816df2012-05-16 18:45:18 +0100775 if (es->sprite->buffer == NULL ||
776 !wl_buffer_is_shm(es->sprite->buffer))
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200777 goto out;
778
Daniel Stone37816df2012-05-16 18:45:18 +0100779 if (es->sprite->geometry.width > 64 ||
780 es->sprite->geometry.height > 64)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200781 goto out;
Benjamin Franzke117483d2011-08-30 11:38:26 +0200782
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400783 output->current_cursor ^= 1;
784 bo = output->cursor_bo[output->current_cursor];
Kristian Høgsberg6916d9c2012-01-18 15:11:07 -0500785 if (bo == NULL)
786 goto out;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200787
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400788 memset(buf, 0, sizeof buf);
789 d = (unsigned char *) buf;
Daniel Stone37816df2012-05-16 18:45:18 +0100790 stride = wl_shm_buffer_get_stride(es->sprite->buffer);
791 s = wl_shm_buffer_get_data(es->sprite->buffer);
792 end = s + stride * es->sprite->geometry.height;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400793 while (s < end) {
Daniel Stone37816df2012-05-16 18:45:18 +0100794 memcpy(d, s, es->sprite->geometry.width * 4);
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400795 s += stride;
796 d += 64 * 4;
797 }
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200798
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400799 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200800 goto out;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200801
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400802 handle = gbm_bo_get_handle(bo).s32;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200803 ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
804 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200805 weston_log("failed to set cursor: %s\n", strerror(-ret));
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200806 goto out;
807 }
808
809 ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
Daniel Stone37816df2012-05-16 18:45:18 +0100810 es->sprite->geometry.x - output->base.x,
811 es->sprite->geometry.y - output->base.y);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200812 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200813 weston_log("failed to move cursor: %s\n", strerror(-ret));
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200814 goto out;
815 }
816
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200817out:
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200818 if (ret)
819 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
820 return ret;
821}
822
Matt Roper361d2ad2011-08-29 13:52:23 -0700823static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500824drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700825{
826 struct drm_output *output = (struct drm_output *) output_base;
827 struct drm_compositor *c =
Benjamin Franzke117483d2011-08-30 11:38:26 +0200828 (struct drm_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700829 drmModeCrtcPtr origcrtc = output->original_crtc;
Matt Roper361d2ad2011-08-29 13:52:23 -0700830
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200831 if (output->backlight)
832 backlight_destroy(output->backlight);
833
Matt Roper361d2ad2011-08-29 13:52:23 -0700834 /* Turn off hardware cursor */
Benjamin Franzke117483d2011-08-30 11:38:26 +0200835 drm_output_set_cursor(&output->base, NULL);
Matt Roper361d2ad2011-08-29 13:52:23 -0700836
837 /* Restore original CRTC state */
838 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +0200839 origcrtc->x, origcrtc->y,
840 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -0700841 drmModeFreeCrtc(origcrtc);
842
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200843 c->crtc_allocator &= ~(1 << output->crtc_id);
844 c->connector_allocator &= ~(1 << output->connector_id);
845
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400846 eglDestroySurface(c->base.egl_display, output->egl_surface);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400847 gbm_surface_destroy(output->surface);
848
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500849 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200850 wl_list_remove(&output->base.link);
851
Matt Roper361d2ad2011-08-29 13:52:23 -0700852 free(output);
853}
854
Alex Wub7b8bda2012-04-17 17:20:48 +0800855static struct drm_mode *
856choose_mode (struct drm_output *output, struct weston_mode *target_mode)
857{
858 struct drm_mode *tmp_mode = NULL, *mode;
859
860 if (output->base.current->width == target_mode->width &&
861 output->base.current->height == target_mode->height &&
862 (output->base.current->refresh == target_mode->refresh ||
863 target_mode->refresh == 0))
864 return (struct drm_mode *)output->base.current;
865
866 wl_list_for_each(mode, &output->base.mode_list, base.link) {
867 if (mode->mode_info.hdisplay == target_mode->width &&
868 mode->mode_info.vdisplay == target_mode->height) {
869 if (mode->mode_info.vrefresh == target_mode->refresh ||
870 target_mode->refresh == 0) {
871 return mode;
872 } else if (!tmp_mode)
873 tmp_mode = mode;
874 }
875 }
876
877 return tmp_mode;
878}
879
880static int
881drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
882{
883 struct drm_output *output;
884 struct drm_mode *drm_mode;
885 int ret;
886 struct drm_compositor *ec;
887 struct gbm_surface *surface;
888 EGLSurface egl_surface;
889
890 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200891 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800892 return -1;
893 }
894
895 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200896 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800897 return -1;
898 }
899
900 ec = (struct drm_compositor *)output_base->compositor;
901 output = (struct drm_output *)output_base;
902 drm_mode = choose_mode (output, mode);
903
904 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +0200905 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +0800906 return -1;
907 } else if (&drm_mode->base == output->base.current) {
908 return 0;
909 } else if (drm_mode->base.width == output->base.current->width &&
910 drm_mode->base.height == output->base.current->height) {
911 /* only change refresh value */
912 ret = drmModeSetCrtc(ec->drm.fd,
913 output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300914 output->current->fb_id, 0, 0,
Alex Wub7b8bda2012-04-17 17:20:48 +0800915 &output->connector_id, 1, &drm_mode->mode_info);
916
917 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200918 weston_log("failed to set mode (%dx%d) %u Hz\n",
Alex Wub7b8bda2012-04-17 17:20:48 +0800919 drm_mode->base.width,
920 drm_mode->base.height,
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400921 drm_mode->base.refresh / 1000);
Alex Wub7b8bda2012-04-17 17:20:48 +0800922 ret = -1;
923 } else {
924 output->base.current->flags = 0;
925 output->base.current = &drm_mode->base;
926 drm_mode->base.flags =
927 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
928 ret = 0;
929 }
930
931 return ret;
932 }
933
934 drm_mode->base.flags =
935 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
936
937 surface = gbm_surface_create(ec->gbm,
938 drm_mode->base.width,
939 drm_mode->base.height,
940 GBM_FORMAT_XRGB8888,
941 GBM_BO_USE_SCANOUT |
942 GBM_BO_USE_RENDERING);
943 if (!surface) {
Martin Minarik6d118362012-06-07 18:01:59 +0200944 weston_log("failed to create gbm surface\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800945 return -1;
946 }
947
948 egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400949 eglCreateWindowSurface(ec->base.egl_display,
950 ec->base.egl_config,
Alex Wub7b8bda2012-04-17 17:20:48 +0800951 surface, NULL);
952
953 if (egl_surface == EGL_NO_SURFACE) {
Martin Minarik6d118362012-06-07 18:01:59 +0200954 weston_log("failed to create egl surface\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800955 goto err;
956 }
957
958 ret = drmModeSetCrtc(ec->drm.fd,
959 output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300960 output->current->fb_id, 0, 0,
Alex Wub7b8bda2012-04-17 17:20:48 +0800961 &output->connector_id, 1, &drm_mode->mode_info);
962 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200963 weston_log("failed to set mode\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800964 goto err;
965 }
966
967 /* reset rendering stuff. */
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300968 if (output->current) {
969 if (output->current->is_client_buffer)
970 gbm_bo_destroy(output->current->bo);
971 else
972 gbm_surface_release_buffer(output->surface,
973 output->current->bo);
974 }
975 output->current = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +0800976
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300977 if (output->next) {
978 if (output->next->is_client_buffer)
979 gbm_bo_destroy(output->next->bo);
980 else
981 gbm_surface_release_buffer(output->surface,
982 output->next->bo);
983 }
984 output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +0800985
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400986 eglDestroySurface(ec->base.egl_display, output->egl_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +0800987 gbm_surface_destroy(output->surface);
988 output->egl_surface = egl_surface;
989 output->surface = surface;
990
991 /*update output*/
992 output->base.current = &drm_mode->base;
993 output->base.dirty = 1;
994 weston_output_move(&output->base, output->base.x, output->base.y);
995 return 0;
996
997err:
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400998 eglDestroySurface(ec->base.egl_display, egl_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +0800999 gbm_surface_destroy(surface);
1000 return -1;
1001}
1002
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001003static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001004on_drm_input(int fd, uint32_t mask, void *data)
1005{
1006 drmEventContext evctx;
1007
1008 memset(&evctx, 0, sizeof evctx);
1009 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1010 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001011 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001012 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001013
1014 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001015}
1016
1017static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001018init_egl(struct drm_compositor *ec, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001019{
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001020 EGLint major, minor, n;
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001021 const char *filename, *sysnum;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001022 int fd;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -04001023 static const EGLint context_attribs[] = {
1024 EGL_CONTEXT_CLIENT_VERSION, 2,
1025 EGL_NONE
1026 };
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001027
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001028 static const EGLint config_attribs[] = {
1029 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1030 EGL_RED_SIZE, 1,
1031 EGL_GREEN_SIZE, 1,
1032 EGL_BLUE_SIZE, 1,
1033 EGL_ALPHA_SIZE, 0,
1034 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1035 EGL_NONE
1036 };
1037
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001038 sysnum = udev_device_get_sysnum(device);
1039 if (sysnum)
1040 ec->drm.id = atoi(sysnum);
1041 if (!sysnum || ec->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001042 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001043 return -1;
1044 }
1045
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001046 filename = udev_device_get_devnode(device);
David Herrmann63ff7062011-11-05 18:46:01 +01001047 fd = open(filename, O_RDWR | O_CLOEXEC);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001048 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001049 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001050 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001051 udev_device_get_devnode(device));
1052 return -1;
1053 }
1054
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001055 weston_log("using %s\n", filename);
1056
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001057 ec->drm.fd = fd;
Benjamin Franzke060cf802011-04-30 09:32:11 +02001058 ec->gbm = gbm_create_device(ec->drm.fd);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001059 ec->base.egl_display = eglGetDisplay(ec->gbm);
1060 if (ec->base.egl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001061 weston_log("failed to create display\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001062 return -1;
1063 }
1064
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001065 if (!eglInitialize(ec->base.egl_display, &major, &minor)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001066 weston_log("failed to initialize display\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001067 return -1;
1068 }
1069
Darxus55973f22010-11-22 21:24:39 -05001070 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001071 weston_log("failed to bind api EGL_OPENGL_ES_API\n");
Darxus55973f22010-11-22 21:24:39 -05001072 return -1;
1073 }
1074
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001075 if (!eglChooseConfig(ec->base.egl_display, config_attribs,
1076 &ec->base.egl_config, 1, &n) || n != 1) {
Martin Minarik6d118362012-06-07 18:01:59 +02001077 weston_log("failed to choose config: %d\n", n);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001078 return -1;
1079 }
1080
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001081 ec->base.egl_context =
1082 eglCreateContext(ec->base.egl_display, ec->base.egl_config,
1083 EGL_NO_CONTEXT, context_attribs);
1084 if (ec->base.egl_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001085 weston_log("failed to create context\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001086 return -1;
1087 }
1088
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001089 ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10,
1090 GBM_FORMAT_XRGB8888,
1091 GBM_BO_USE_RENDERING);
1092 if (!ec->dummy_surface) {
Martin Minarik6d118362012-06-07 18:01:59 +02001093 weston_log("failed to create dummy gbm surface\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001094 return -1;
1095 }
1096
1097 ec->dummy_egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001098 eglCreateWindowSurface(ec->base.egl_display,
1099 ec->base.egl_config,
1100 ec->dummy_surface,
1101 NULL);
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001102 if (ec->dummy_egl_surface == EGL_NO_SURFACE) {
Martin Minarik6d118362012-06-07 18:01:59 +02001103 weston_log("failed to create egl surface\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001104 return -1;
1105 }
1106
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001107 if (!eglMakeCurrent(ec->base.egl_display, ec->dummy_egl_surface,
1108 ec->dummy_egl_surface, ec->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001109 weston_log("failed to make context current\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001110 return -1;
1111 }
1112
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001113 return 0;
1114}
1115
1116static drmModeModeInfo builtin_1024x768 = {
1117 63500, /* clock */
1118 1024, 1072, 1176, 1328, 0,
1119 768, 771, 775, 798, 0,
1120 59920,
1121 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
1122 0,
1123 "1024x768"
1124};
1125
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001126
1127static int
1128drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1129{
1130 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001131 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001132
1133 mode = malloc(sizeof *mode);
1134 if (mode == NULL)
1135 return -1;
1136
1137 mode->base.flags = 0;
1138 mode->base.width = info->hdisplay;
1139 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001140
1141 /* Calculate higher precision (mHz) refresh rate */
1142 refresh = (info->clock * 1000000LL / info->htotal +
1143 info->vtotal / 2) / info->vtotal;
1144
1145 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1146 refresh *= 2;
1147 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1148 refresh /= 2;
1149 if (info->vscan > 1)
1150 refresh /= info->vscan;
1151
1152 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001153 mode->mode_info = *info;
1154 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1155
1156 return 0;
1157}
1158
1159static int
1160drm_subpixel_to_wayland(int drm_value)
1161{
1162 switch (drm_value) {
1163 default:
1164 case DRM_MODE_SUBPIXEL_UNKNOWN:
1165 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1166 case DRM_MODE_SUBPIXEL_NONE:
1167 return WL_OUTPUT_SUBPIXEL_NONE;
1168 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1169 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1170 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1171 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1172 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1173 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1174 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1175 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1176 }
1177}
1178
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001179static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001180sprite_handle_buffer_destroy(struct wl_listener *listener, void *data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001181{
1182 struct drm_sprite *sprite =
1183 container_of(listener, struct drm_sprite,
1184 destroy_listener);
1185
1186 sprite->surface = NULL;
1187}
1188
1189static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001190sprite_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001191{
1192 struct drm_sprite *sprite =
1193 container_of(listener, struct drm_sprite,
1194 pending_destroy_listener);
1195
1196 sprite->pending_surface = NULL;
1197}
1198
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001199/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001200static uint32_t
1201drm_get_backlight(struct drm_output *output)
1202{
1203 long brightness, max_brightness, norm;
1204
1205 brightness = backlight_get_brightness(output->backlight);
1206 max_brightness = backlight_get_max_brightness(output->backlight);
1207
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001208 /* convert it on a scale of 0 to 255 */
1209 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001210
1211 return (uint32_t) norm;
1212}
1213
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001214/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001215static void
1216drm_set_backlight(struct weston_output *output_base, uint32_t value)
1217{
1218 struct drm_output *output = (struct drm_output *) output_base;
1219 long max_brightness, new_brightness;
1220
1221 if (!output->backlight)
1222 return;
1223
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001224 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001225 return;
1226
1227 max_brightness = backlight_get_max_brightness(output->backlight);
1228
1229 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001230 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001231
1232 backlight_set_brightness(output->backlight, new_brightness);
1233}
1234
1235static drmModePropertyPtr
1236drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1237{
1238 drmModePropertyPtr props;
1239 int i;
1240
1241 for (i = 0; i < connector->count_props; i++) {
1242 props = drmModeGetProperty(fd, connector->props[i]);
1243 if (!props)
1244 continue;
1245
1246 if (!strcmp(props->name, name))
1247 return props;
1248
1249 drmModeFreeProperty(props);
1250 }
1251
1252 return NULL;
1253}
1254
1255static void
1256drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1257{
1258 struct drm_output *output = (struct drm_output *) output_base;
1259 struct weston_compositor *ec = output_base->compositor;
1260 struct drm_compositor *c = (struct drm_compositor *) ec;
1261 drmModeConnectorPtr connector;
1262 drmModePropertyPtr prop;
1263
1264 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1265 if (!connector)
1266 return;
1267
1268 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1269 if (!prop) {
1270 drmModeFreeConnector(connector);
1271 return;
1272 }
1273
1274 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1275 prop->prop_id, level);
1276 drmModeFreeProperty(prop);
1277 drmModeFreeConnector(connector);
1278}
1279
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001280static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001281create_output_for_connector(struct drm_compositor *ec,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001282 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001283 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001284 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001285{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001286 struct drm_output *output;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001287 struct drm_mode *drm_mode, *next;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001288 drmModeEncoder *encoder;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001289 int i, ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001290
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001291 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001292 if (encoder == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001293 weston_log("No encoder for connector.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001294 return -1;
1295 }
1296
1297 for (i = 0; i < resources->count_crtcs; i++) {
Marty Jack13d9db22011-02-09 19:01:42 -05001298 if (encoder->possible_crtcs & (1 << i) &&
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001299 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001300 break;
1301 }
1302 if (i == resources->count_crtcs) {
Martin Minarik6d118362012-06-07 18:01:59 +02001303 weston_log("No usable crtc for encoder.\n");
David Herrmanneb8bed52011-12-08 17:05:44 +01001304 drmModeFreeEncoder(encoder);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001305 return -1;
1306 }
1307
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001308 output = malloc(sizeof *output);
David Herrmanneb8bed52011-12-08 17:05:44 +01001309 if (output == NULL) {
1310 drmModeFreeEncoder(encoder);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001311 return -1;
David Herrmanneb8bed52011-12-08 17:05:44 +01001312 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001313
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001314 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001315 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1316 output->base.make = "unknown";
1317 output->base.model = "unknown";
1318 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001319
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001320 output->crtc_id = resources->crtcs[i];
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001321 ec->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001322 output->connector_id = connector->connector_id;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001323 ec->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001324
Matt Roper361d2ad2011-08-29 13:52:23 -07001325 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001326 drmModeFreeEncoder(encoder);
Matt Roper361d2ad2011-08-29 13:52:23 -07001327
David Herrmann0f0d54e2011-12-08 17:05:45 +01001328 for (i = 0; i < connector->count_modes; i++) {
1329 ret = drm_output_add_mode(output, &connector->modes[i]);
1330 if (ret)
1331 goto err_free;
1332 }
1333
1334 if (connector->count_modes == 0) {
1335 ret = drm_output_add_mode(output, &builtin_1024x768);
1336 if (ret)
1337 goto err_free;
1338 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001339
1340 drm_mode = container_of(output->base.mode_list.next,
1341 struct drm_mode, base.link);
1342 output->base.current = &drm_mode->base;
1343 drm_mode->base.flags =
1344 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1345
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001346 output->surface = gbm_surface_create(ec->gbm,
1347 output->base.current->width,
1348 output->base.current->height,
1349 GBM_FORMAT_XRGB8888,
1350 GBM_BO_USE_SCANOUT |
1351 GBM_BO_USE_RENDERING);
1352 if (!output->surface) {
Martin Minarik6d118362012-06-07 18:01:59 +02001353 weston_log("failed to create gbm surface\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001354 goto err_free;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001355 }
1356
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001357 output->egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001358 eglCreateWindowSurface(ec->base.egl_display,
1359 ec->base.egl_config,
1360 output->surface,
1361 NULL);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001362 if (output->egl_surface == EGL_NO_SURFACE) {
Martin Minarik6d118362012-06-07 18:01:59 +02001363 weston_log("failed to create egl surface\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001364 goto err_surface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001365 }
1366
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04001367 output->cursor_bo[0] =
1368 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1369 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1370 output->cursor_bo[1] =
1371 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1372 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1373
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001374 output->backlight = backlight_init(drm_device,
1375 connector->connector_type);
1376 if (output->backlight) {
1377 output->base.set_backlight = drm_set_backlight;
1378 output->base.backlight_current = drm_get_backlight(output);
1379 }
1380
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001381 weston_output_init(&output->base, &ec->base, x, y,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001382 connector->mmWidth, connector->mmHeight,
1383 WL_OUTPUT_FLIPPED);
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04001384
1385 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1386
Alex Wubd3354b2012-04-17 17:20:49 +08001387 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001388 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07001389 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001390 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001391 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08001392 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001393
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001394 weston_log("kms connector %d, crtc %d at mode %dx%d@%.1f\n",
1395 output->connector_id, output->crtc_id,
1396 output->base.current->width,
1397 output->base.current->height,
1398 output->base.current->refresh / 1000.0);
1399
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001400 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001401
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001402err_surface:
1403 gbm_surface_destroy(output->surface);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001404err_free:
1405 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1406 base.link) {
1407 wl_list_remove(&drm_mode->base.link);
1408 free(drm_mode);
1409 }
1410
1411 drmModeFreeCrtc(output->original_crtc);
1412 ec->crtc_allocator &= ~(1 << output->crtc_id);
1413 ec->connector_allocator &= ~(1 << output->connector_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001414 free(output);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001415
David Herrmann0f0d54e2011-12-08 17:05:45 +01001416 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001417}
1418
Jesse Barnes58ef3792012-02-23 09:45:49 -05001419static void
1420create_sprites(struct drm_compositor *ec)
1421{
1422 struct drm_sprite *sprite;
1423 drmModePlaneRes *plane_res;
1424 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001425 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001426
1427 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1428 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02001429 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001430 strerror(errno));
1431 return;
1432 }
1433
1434 for (i = 0; i < plane_res->count_planes; i++) {
1435 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1436 if (!plane)
1437 continue;
1438
1439 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1440 plane->count_formats));
1441 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02001442 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001443 __func__);
1444 free(plane);
1445 continue;
1446 }
1447
1448 memset(sprite, 0, sizeof *sprite);
1449
1450 sprite->possible_crtcs = plane->possible_crtcs;
1451 sprite->plane_id = plane->plane_id;
1452 sprite->surface = NULL;
1453 sprite->pending_surface = NULL;
1454 sprite->fb_id = 0;
1455 sprite->pending_fb_id = 0;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001456 sprite->destroy_listener.notify = sprite_handle_buffer_destroy;
1457 sprite->pending_destroy_listener.notify =
Jesse Barnes58ef3792012-02-23 09:45:49 -05001458 sprite_handle_pending_buffer_destroy;
1459 sprite->compositor = ec;
1460 sprite->count_formats = plane->count_formats;
1461 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05001462 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05001463 drmModeFreePlane(plane);
1464
1465 wl_list_insert(&ec->sprite_list, &sprite->link);
1466 }
1467
1468 free(plane_res->planes);
1469 free(plane_res);
1470}
1471
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001472static void
1473destroy_sprites(struct drm_compositor *compositor)
1474{
1475 struct drm_sprite *sprite, *next;
1476 struct drm_output *output;
1477
1478 output = container_of(compositor->base.output_list.next,
1479 struct drm_output, base.link);
1480
1481 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1482 drmModeSetPlane(compositor->drm.fd,
1483 sprite->plane_id,
1484 output->crtc_id, 0, 0,
1485 0, 0, 0, 0, 0, 0, 0, 0);
1486 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1487 free(sprite);
1488 }
1489}
Jesse Barnes58ef3792012-02-23 09:45:49 -05001490
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001491static int
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001492create_outputs(struct drm_compositor *ec, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001493 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001494{
1495 drmModeConnector *connector;
1496 drmModeRes *resources;
1497 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001498 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001499
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001500 resources = drmModeGetResources(ec->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001501 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02001502 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001503 return -1;
1504 }
1505
Jesse Barnes58ef3792012-02-23 09:45:49 -05001506 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001507 if (!ec->crtcs) {
1508 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001509 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001510 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001511
1512 ec->num_crtcs = resources->count_crtcs;
1513 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1514
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001515 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001516 connector = drmModeGetConnector(ec->drm.fd,
1517 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001518 if (connector == NULL)
1519 continue;
1520
1521 if (connector->connection == DRM_MODE_CONNECTED &&
1522 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001523 connector->connector_id == option_connector)) {
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001524 if (create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001525 connector, x, y,
1526 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02001527 drmModeFreeConnector(connector);
1528 continue;
1529 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001530
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001531 x += container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001532 struct weston_output,
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001533 link)->current->width;
1534 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001535
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001536 drmModeFreeConnector(connector);
1537 }
1538
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001539 if (wl_list_empty(&ec->base.output_list)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001540 weston_log("No currently active connector found.\n");
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001541 drmModeFreeResources(resources);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001542 return -1;
1543 }
1544
1545 drmModeFreeResources(resources);
1546
1547 return 0;
1548}
1549
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001550static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001551update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001552{
1553 drmModeConnector *connector;
1554 drmModeRes *resources;
1555 struct drm_output *output, *next;
1556 int x = 0, y = 0;
1557 int x_offset = 0, y_offset = 0;
1558 uint32_t connected = 0, disconnects = 0;
1559 int i;
1560
1561 resources = drmModeGetResources(ec->drm.fd);
1562 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02001563 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001564 return;
1565 }
1566
1567 /* collect new connects */
1568 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001569 int connector_id = resources->connectors[i];
1570
1571 connector = drmModeGetConnector(ec->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01001572 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001573 continue;
1574
David Herrmann7551cff2011-12-08 17:05:43 +01001575 if (connector->connection != DRM_MODE_CONNECTED) {
1576 drmModeFreeConnector(connector);
1577 continue;
1578 }
1579
Benjamin Franzke117483d2011-08-30 11:38:26 +02001580 connected |= (1 << connector_id);
1581
1582 if (!(ec->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001583 struct weston_output *last =
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001584 container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001585 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001586
1587 /* XXX: not yet needed, we die with 0 outputs */
1588 if (!wl_list_empty(&ec->base.output_list))
Benjamin Franzke117483d2011-08-30 11:38:26 +02001589 x = last->x + last->current->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001590 else
1591 x = 0;
1592 y = 0;
1593 create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001594 connector, x, y,
1595 drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02001596 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001597
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001598 }
1599 drmModeFreeConnector(connector);
1600 }
1601 drmModeFreeResources(resources);
1602
1603 disconnects = ec->connector_allocator & ~connected;
1604 if (disconnects) {
1605 wl_list_for_each_safe(output, next, &ec->base.output_list,
1606 base.link) {
1607 if (x_offset != 0 || y_offset != 0) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001608 weston_output_move(&output->base,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001609 output->base.x - x_offset,
1610 output->base.y - y_offset);
1611 }
1612
1613 if (disconnects & (1 << output->connector_id)) {
1614 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02001615 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001616 output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001617 x_offset += output->base.current->width;
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001618 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001619 }
1620 }
1621 }
1622
1623 /* FIXME: handle zero outputs, without terminating */
1624 if (ec->connector_allocator == 0)
1625 wl_display_terminate(ec->base.wl_display);
1626}
1627
1628static int
David Herrmannd7488c22012-03-11 20:05:21 +01001629udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001630{
David Herrmannd7488c22012-03-11 20:05:21 +01001631 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01001632 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01001633
1634 sysnum = udev_device_get_sysnum(device);
1635 if (!sysnum || atoi(sysnum) != ec->drm.id)
1636 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001637
David Herrmann6ac52db2012-03-11 20:05:22 +01001638 val = udev_device_get_property_value(device, "HOTPLUG");
1639 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001640 return 0;
1641
David Herrmann6ac52db2012-03-11 20:05:22 +01001642 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001643}
1644
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001645static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001646udev_drm_event(int fd, uint32_t mask, void *data)
1647{
1648 struct drm_compositor *ec = data;
1649 struct udev_device *event;
1650
1651 event = udev_monitor_receive_device(ec->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001652
David Herrmannd7488c22012-03-11 20:05:21 +01001653 if (udev_event_is_hotplug(ec, event))
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001654 update_outputs(ec, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001655
1656 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001657
1658 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001659}
1660
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001661static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001662drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001663{
1664 struct drm_compositor *d = (struct drm_compositor *) ec;
Daniel Stone37816df2012-05-16 18:45:18 +01001665 struct weston_seat *seat, *next;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001666
Daniel Stone37816df2012-05-16 18:45:18 +01001667 wl_list_for_each_safe(seat, next, &ec->seat_list, link)
1668 evdev_input_destroy(seat);
Jonas Ådahlc97af922012-03-28 22:36:09 +02001669
1670 wl_event_source_remove(d->udev_drm_source);
1671 wl_event_source_remove(d->drm_source);
1672
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001673 weston_compositor_shutdown(ec);
Jonas Ådahlc97af922012-03-28 22:36:09 +02001674
Ander Conselvan de Oliveira5f5f3192012-04-30 13:31:28 +03001675 /* Work around crash in egl_dri2.c's dri2_make_current() */
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001676 eglMakeCurrent(ec->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
Ander Conselvan de Oliveira5f5f3192012-04-30 13:31:28 +03001677 EGL_NO_CONTEXT);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001678 eglTerminate(ec->egl_display);
Ander Conselvan de Oliveira5f5f3192012-04-30 13:31:28 +03001679 eglReleaseThread();
1680
Matt Roper361d2ad2011-08-29 13:52:23 -07001681 gbm_device_destroy(d->gbm);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001682 destroy_sprites(d);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001683 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +02001684 weston_log("failed to drop master: %m\n");
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001685 tty_destroy(d->tty);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001686
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001687 free(d);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001688}
1689
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001690static void
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001691drm_compositor_set_modes(struct drm_compositor *compositor)
1692{
1693 struct drm_output *output;
1694 struct drm_mode *drm_mode;
1695 int ret;
1696
1697 wl_list_for_each(output, &compositor->base.output_list, base.link) {
1698 drm_mode = (struct drm_mode *) output->base.current;
1699 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001700 output->current->fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001701 &output->connector_id, 1,
1702 &drm_mode->mode_info);
1703 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001704 weston_log(
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001705 "failed to set mode %dx%d for output at %d,%d: %m\n",
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001706 drm_mode->base.width, drm_mode->base.height,
1707 output->base.x, output->base.y);
1708 }
1709 }
1710}
1711
1712static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001713vt_func(struct weston_compositor *compositor, int event)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001714{
1715 struct drm_compositor *ec = (struct drm_compositor *) compositor;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001716 struct weston_output *output;
Daniel Stone37816df2012-05-16 18:45:18 +01001717 struct weston_seat *seat;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001718 struct drm_sprite *sprite;
1719 struct drm_output *drm_output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001720
1721 switch (event) {
1722 case TTY_ENTER_VT:
1723 compositor->focus = 1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001724 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001725 weston_log("failed to set master: %m\n");
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001726 wl_display_terminate(compositor->wl_display);
1727 }
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001728 compositor->state = ec->prev_state;
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001729 drm_compositor_set_modes(ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001730 weston_compositor_damage_all(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001731 wl_list_for_each(seat, &compositor->seat_list, link) {
1732 evdev_add_devices(ec->udev, seat);
1733 evdev_enable_udev_monitor(ec->udev, seat);
Benjamin Franzke78d3afe2012-04-09 18:14:58 +02001734 }
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001735 break;
1736 case TTY_LEAVE_VT:
Daniel Stone37816df2012-05-16 18:45:18 +01001737 wl_list_for_each(seat, &compositor->seat_list, link) {
1738 evdev_disable_udev_monitor(seat);
1739 evdev_remove_devices(seat);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04001740 }
1741
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001742 compositor->focus = 0;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001743 ec->prev_state = compositor->state;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001744 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04001745
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001746 /* If we have a repaint scheduled (either from a
1747 * pending pageflip or the idle handler), make sure we
1748 * cancel that so we don't try to pageflip when we're
1749 * vt switched away. The SLEEPING state will prevent
1750 * further attemps at repainting. When we switch
1751 * back, we schedule a repaint, which will process
1752 * pending frame callbacks. */
1753
1754 wl_list_for_each(output, &ec->base.output_list, link) {
1755 output->repaint_needed = 0;
1756 drm_output_set_cursor(output, NULL);
1757 }
1758
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001759 drm_output = container_of(ec->base.output_list.next,
1760 struct drm_output, base.link);
1761
1762 wl_list_for_each(sprite, &ec->sprite_list, link)
1763 drmModeSetPlane(ec->drm.fd,
1764 sprite->plane_id,
1765 drm_output->crtc_id, 0, 0,
1766 0, 0, 0, 0, 0, 0, 0, 0);
1767
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001768 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +02001769 weston_log("failed to drop master: %m\n");
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001770
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001771 break;
1772 };
1773}
1774
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001775static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001776switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001777{
1778 struct drm_compositor *ec = data;
1779
Daniel Stone325fc2d2012-05-30 16:31:58 +01001780 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001781}
1782
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001783static const char default_seat[] = "seat0";
1784
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001785static struct weston_compositor *
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001786drm_compositor_create(struct wl_display *display,
Daniel Stonebaf43592012-06-01 11:11:10 -04001787 int connector, const char *seat, int tty,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001788 int argc, char *argv[], const char *config_file)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001789{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001790 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001791 struct udev_enumerate *e;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001792 struct udev_list_entry *entry;
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001793 struct udev_device *device, *drm_device;
1794 const char *path, *device_seat;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001795 struct wl_event_loop *loop;
Daniel Stonea96b93c2012-06-22 14:04:37 +01001796 struct weston_seat *weston_seat, *next;
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001797 uint32_t key;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001798
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001799 weston_log("initializing drm backend\n");
1800
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001801 ec = malloc(sizeof *ec);
1802 if (ec == NULL)
1803 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001804 memset(ec, 0, sizeof *ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01001805
1806 if (weston_compositor_init(&ec->base, display, argc, argv,
Daniel Stonea96b93c2012-06-22 14:04:37 +01001807 config_file) < 0) {
1808 weston_log("weston_compositor_init failed\n");
1809 goto err_base;
1810 }
Daniel Stone725c2c32012-06-22 14:04:36 +01001811
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001812 ec->udev = udev_new();
1813 if (ec->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001814 weston_log("failed to initialize udev context\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001815 goto err_compositor;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001816 }
1817
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05001818 ec->base.wl_display = display;
1819 ec->tty = tty_create(&ec->base, vt_func, tty);
1820 if (!ec->tty) {
Martin Minarik6d118362012-06-07 18:01:59 +02001821 weston_log("failed to initialize tty\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001822 goto err_udev;
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05001823 }
1824
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001825 e = udev_enumerate_new(ec->udev);
1826 udev_enumerate_add_match_subsystem(e, "drm");
Benjamin Franzkea764ee52011-10-07 08:23:22 +02001827 udev_enumerate_add_match_sysname(e, "card[0-9]*");
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001828
Benjamin Franzke117483d2011-08-30 11:38:26 +02001829 udev_enumerate_scan_devices(e);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001830 drm_device = NULL;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001831 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001832 path = udev_list_entry_get_name(entry);
1833 device = udev_device_new_from_syspath(ec->udev, path);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001834 device_seat =
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001835 udev_device_get_property_value(device, "ID_SEAT");
1836 if (!device_seat)
1837 device_seat = default_seat;
1838 if (strcmp(device_seat, seat) == 0) {
1839 drm_device = device;
1840 break;
1841 }
1842 udev_device_unref(device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001843 }
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001844
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001845 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001846 weston_log("no drm device found\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001847 goto err_udev_enum;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001848 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001849
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001850 if (init_egl(ec, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001851 weston_log("failed to initialize egl\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001852 goto err_udev_dev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001853 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001854
1855 ec->base.destroy = drm_destroy;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001856
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001857 ec->base.focus = 1;
1858
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001859 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001860
Daniel Stone725c2c32012-06-22 14:04:36 +01001861 if (weston_compositor_init_gl(&ec->base) < 0)
Daniel Stonea96b93c2012-06-22 14:04:37 +01001862 goto err_egl;
Benjamin Franzked59eb1c2011-04-29 22:14:54 +02001863
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001864 for (key = KEY_F1; key < KEY_F9; key++)
Daniel Stone325fc2d2012-05-30 16:31:58 +01001865 weston_compositor_add_key_binding(&ec->base, key,
1866 MODIFIER_CTRL | MODIFIER_ALT,
1867 switch_vt_binding, ec);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001868
Jesse Barnes58ef3792012-02-23 09:45:49 -05001869 wl_list_init(&ec->sprite_list);
1870 create_sprites(ec);
1871
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001872 if (create_outputs(ec, connector, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001873 weston_log("failed to create output for %s\n", path);
Daniel Stonea96b93c2012-06-22 14:04:37 +01001874 goto err_sprite;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001875 }
1876
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02001877 path = NULL;
1878
Tiago Vignattice03ec32011-12-19 01:14:03 +02001879 evdev_input_create(&ec->base, ec->udev, seat);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001880
1881 loop = wl_display_get_event_loop(ec->base.wl_display);
1882 ec->drm_source =
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001883 wl_event_loop_add_fd(loop, ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001884 WL_EVENT_READABLE, on_drm_input, ec);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001885
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001886 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
1887 if (ec->udev_monitor == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001888 weston_log("failed to intialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001889 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001890 }
1891 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
1892 "drm", NULL);
1893 ec->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02001894 wl_event_loop_add_fd(loop,
1895 udev_monitor_get_fd(ec->udev_monitor),
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001896 WL_EVENT_READABLE, udev_drm_event, ec);
1897
1898 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001899 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001900 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001901 }
1902
Daniel Stonea96b93c2012-06-22 14:04:37 +01001903 udev_device_unref(drm_device);
1904 udev_enumerate_unref(e);
1905
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001906 return &ec->base;
Daniel Stonea96b93c2012-06-22 14:04:37 +01001907
1908err_udev_monitor:
1909 wl_event_source_remove(ec->udev_drm_source);
1910 udev_monitor_unref(ec->udev_monitor);
1911err_drm_source:
1912 wl_event_source_remove(ec->drm_source);
1913 wl_list_for_each_safe(weston_seat, next, &ec->base.seat_list, link)
1914 evdev_input_destroy(weston_seat);
1915err_sprite:
1916 destroy_sprites(ec);
1917err_egl:
1918 eglMakeCurrent(ec->base.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
1919 EGL_NO_CONTEXT);
1920 eglTerminate(ec->base.egl_display);
1921 eglReleaseThread();
1922 gbm_device_destroy(ec->gbm);
1923err_udev_dev:
1924 udev_device_unref(drm_device);
1925err_udev_enum:
1926 udev_enumerate_unref(e);
1927 tty_destroy(ec->tty);
1928err_udev:
1929 udev_unref(ec->udev);
1930err_compositor:
1931 weston_compositor_shutdown(&ec->base);
1932err_base:
1933 free(ec);
1934 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001935}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001936
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001937WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -04001938backend_init(struct wl_display *display, int argc, char *argv[],
1939 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001940{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001941 int connector = 0, tty = 0;
1942 const char *seat = default_seat;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001943
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001944 const struct weston_option drm_options[] = {
1945 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
1946 { WESTON_OPTION_STRING, "seat", 0, &seat },
1947 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
1948 };
Benjamin Franzke117483d2011-08-30 11:38:26 +02001949
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001950 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001951
Daniel Stonec1be8e52012-06-01 11:14:02 -04001952 return drm_compositor_create(display, connector, seat, tty, argc, argv,
1953 config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001954}