blob: 39b7a1b5431c88fd774b2ca96951c28ab103602f [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 <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <fcntl.h>
31#include <unistd.h>
32
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"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040042
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040043struct drm_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050044 struct weston_compositor base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040045
46 struct udev *udev;
47 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040048
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010049 struct udev_monitor *udev_monitor;
50 struct wl_event_source *udev_drm_source;
51
Benjamin Franzke2af7f102011-03-02 11:14:59 +010052 struct {
David Herrmannd7488c22012-03-11 20:05:21 +010053 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010054 int fd;
55 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +020056 struct gbm_device *gbm;
Jesse Barnes58ef3792012-02-23 09:45:49 -050057 uint32_t *crtcs;
58 int num_crtcs;
Marty Jack13d9db22011-02-09 19:01:42 -050059 uint32_t crtc_allocator;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010060 uint32_t connector_allocator;
Kristian Høgsberge4762a62011-01-14 14:59:13 -050061 struct tty *tty;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +020062
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -040063 struct gbm_surface *dummy_surface;
64 EGLSurface dummy_egl_surface;
65
Jesse Barnes58ef3792012-02-23 09:45:49 -050066 struct wl_list sprite_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -050067 int sprites_are_broken;
Jesse Barnes58ef3792012-02-23 09:45:49 -050068
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +020069 uint32_t prev_state;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040070};
71
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040072struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050073 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040074 drmModeModeInfo mode_info;
75};
76
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040077struct drm_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050078 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040079
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040080 uint32_t crtc_id;
81 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -070082 drmModeCrtcPtr original_crtc;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +020083
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -040084 struct gbm_surface *surface;
85 EGLSurface egl_surface;
86 uint32_t current_fb_id;
87 uint32_t next_fb_id;
88 struct gbm_bo *current_bo;
89 struct gbm_bo *next_bo;
90
Kristian Høgsberg9f404b72012-01-26 00:11:01 -050091 struct wl_buffer *scanout_buffer;
92 struct wl_listener scanout_buffer_destroy_listener;
93 struct wl_buffer *pending_scanout_buffer;
94 struct wl_listener pending_scanout_buffer_destroy_listener;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +020095 struct backlight *backlight;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040096};
97
Jesse Barnes58ef3792012-02-23 09:45:49 -050098/*
99 * An output has a primary display plane plus zero or more sprites for
100 * blending display contents.
101 */
102struct drm_sprite {
103 struct wl_list link;
104
105 uint32_t fb_id;
106 uint32_t pending_fb_id;
107 struct weston_surface *surface;
108 struct weston_surface *pending_surface;
109
110 struct drm_compositor *compositor;
111
112 struct wl_listener destroy_listener;
113 struct wl_listener pending_destroy_listener;
114
115 uint32_t possible_crtcs;
116 uint32_t plane_id;
117 uint32_t count_formats;
118
119 int32_t src_x, src_y;
120 uint32_t src_w, src_h;
121 uint32_t dest_x, dest_y;
122 uint32_t dest_w, dest_h;
123
124 uint32_t formats[];
125};
126
127static int
128surface_is_primary(struct weston_compositor *ec, struct weston_surface *es)
129{
130 struct weston_surface *primary;
131
132 primary = container_of(ec->surface_list.next, struct weston_surface,
133 link);
134 if (es == primary)
135 return -1;
136 return 0;
137}
138
139static int
140drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
141{
142 struct weston_compositor *ec = output_base->compositor;
143 struct drm_compositor *c =(struct drm_compositor *) ec;
144 struct drm_output *output = (struct drm_output *) output_base;
145 int crtc;
146
147 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
148 if (c->crtcs[crtc] != output->crtc_id)
149 continue;
150
151 if (supported & (1 << crtc))
152 return -1;
153 }
154
155 return 0;
156}
157
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500158static int
159drm_output_prepare_scanout_surface(struct drm_output *output)
160{
161 struct drm_compositor *c =
162 (struct drm_compositor *) output->base.compositor;
163 struct weston_surface *es;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500164
165 es = container_of(c->base.surface_list.next,
166 struct weston_surface, link);
167
Kristian Høgsberg101cb652012-02-17 10:45:16 -0500168 /* Need to verify output->region contained in surface opaque
169 * region. Or maybe just that format doesn't have alpha. */
170 return -1;
171
172 if (es->geometry.x != output->base.x ||
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200173 es->geometry.y != output->base.y ||
Pekka Paalanen60921e52012-01-25 15:55:43 +0200174 es->geometry.width != output->base.current->width ||
175 es->geometry.height != output->base.current->height ||
Pekka Paalanenf1f5b362012-01-25 14:33:33 +0200176 es->transform.enabled ||
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500177 es->image == EGL_NO_IMAGE_KHR)
178 return -1;
179
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400180 output->next_bo =
181 gbm_bo_create_from_egl_image(c->gbm,
182 c->base.display, es->image,
183 es->geometry.width,
184 es->geometry.height,
185 GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500186
187 /* assert output->pending_scanout_buffer == NULL */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500188 output->pending_scanout_buffer = es->buffer;
189 output->pending_scanout_buffer->busy_count++;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500190
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500191 wl_list_insert(output->pending_scanout_buffer->resource.destroy_listener_list.prev,
192 &output->pending_scanout_buffer_destroy_listener.link);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500193
194 pixman_region32_fini(&es->damage);
195 pixman_region32_init(&es->damage);
196
197 return 0;
198}
199
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500200static void
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400201drm_output_render(struct drm_output *output, pixman_region32_t *damage)
202{
203 struct drm_compositor *compositor =
204 (struct drm_compositor *) output->base.compositor;
205 struct weston_surface *surface;
206
207 if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
208 output->egl_surface, compositor->base.context)) {
209 fprintf(stderr, "failed to make current\n");
210 return;
211 }
212
213 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
214 weston_surface_draw(surface, &output->base, damage);
215
216 eglSwapBuffers(compositor->base.display, output->egl_surface);
217 output->next_bo = gbm_surface_lock_front_buffer(output->surface);
218 if (!output->next_bo) {
219 fprintf(stderr, "failed to lock front buffer: %m\n");
220 return;
221 }
222}
223
224static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500225drm_output_repaint(struct weston_output *output_base,
226 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100227{
228 struct drm_output *output = (struct drm_output *) output_base;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500229 struct drm_compositor *compositor =
230 (struct drm_compositor *) output->base.compositor;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500231 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400232 struct drm_mode *mode;
233 uint32_t stride, handle;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500234 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100235
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500236 drm_output_prepare_scanout_surface(output);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400237 if (!output->next_bo)
238 drm_output_render(output, damage);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500239
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400240 stride = gbm_bo_get_pitch(output->next_bo);
241 handle = gbm_bo_get_handle(output->next_bo).u32;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400242
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400243 /* Destroy and set to NULL now so we don't try to
244 * gbm_surface_release_buffer() a client buffer in the
245 * page_flip_handler. */
246 if (output->pending_scanout_buffer) {
247 gbm_bo_destroy(output->next_bo);
248 output->next_bo = NULL;
249 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400250
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400251 ret = drmModeAddFB(compositor->drm.fd,
252 output->base.current->width,
253 output->base.current->height,
254 24, 32, stride, handle, &output->next_fb_id);
255 if (ret) {
256 fprintf(stderr, "failed to create kms fb: %m\n");
257 gbm_surface_release_buffer(output->surface, output->next_bo);
258 output->next_bo = NULL;
259 return;
260 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100261
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400262 mode = container_of(output->base.current, struct drm_mode, base);
263 if (output->current_fb_id == 0) {
264 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
265 output->next_fb_id, 0, 0,
266 &output->connector_id, 1,
267 &mode->mode_info);
268 if (ret) {
269 fprintf(stderr, "set mode failed: %m\n");
270 return;
271 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200272 }
273
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500274 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400275 output->next_fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500276 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
277 fprintf(stderr, "queueing pageflip failed: %m\n");
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500278 return;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500279 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100280
Jesse Barnes58ef3792012-02-23 09:45:49 -0500281 /*
282 * Now, update all the sprite surfaces
283 */
284 wl_list_for_each(s, &compositor->sprite_list, link) {
285 uint32_t flags = 0;
286 drmVBlank vbl = {
287 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
288 .request.sequence = 1,
289 };
290
291 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
292 continue;
293
294 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
295 output->crtc_id, s->pending_fb_id, flags,
296 s->dest_x, s->dest_y,
297 s->dest_w, s->dest_h,
298 s->src_x, s->src_y,
299 s->src_w, s->src_h);
300 if (ret)
301 fprintf(stderr, "setplane failed: %d: %s\n",
302 ret, strerror(errno));
303
304 /*
305 * Queue a vblank signal so we know when the surface
306 * becomes active on the display or has been replaced.
307 */
308 vbl.request.signal = (unsigned long)s;
309 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
310 if (ret) {
311 fprintf(stderr, "vblank event request failed: %d: %s\n",
312 ret, strerror(errno));
313 }
314 }
315
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500316 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400317}
318
319static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500320vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
321 void *data)
322{
323 struct drm_sprite *s = (struct drm_sprite *)data;
324 struct drm_compositor *c = s->compositor;
325
326 if (s->surface) {
327 weston_buffer_post_release(s->surface->buffer);
328 wl_list_remove(&s->destroy_listener.link);
329 s->surface = NULL;
330 drmModeRmFB(c->drm.fd, s->fb_id);
331 s->fb_id = 0;
332 }
333
334 if (s->pending_surface) {
335 wl_list_remove(&s->pending_destroy_listener.link);
336 wl_list_insert(s->pending_surface->buffer->resource.destroy_listener_list.prev,
337 &s->destroy_listener.link);
338 s->surface = s->pending_surface;
339 s->pending_surface = NULL;
340 s->fb_id = s->pending_fb_id;
341 s->pending_fb_id = 0;
342 }
343}
344
345static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400346page_flip_handler(int fd, unsigned int frame,
347 unsigned int sec, unsigned int usec, void *data)
348{
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200349 struct drm_output *output = (struct drm_output *) data;
350 struct drm_compositor *c =
351 (struct drm_compositor *) output->base.compositor;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400352 uint32_t msecs;
353
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400354 if (output->current_fb_id)
355 drmModeRmFB(c->drm.fd, output->current_fb_id);
356 output->current_fb_id = output->next_fb_id;
357 output->next_fb_id = 0;
358
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500359 if (output->scanout_buffer) {
360 weston_buffer_post_release(output->scanout_buffer);
361 wl_list_remove(&output->scanout_buffer_destroy_listener.link);
362 output->scanout_buffer = NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400363 } else if (output->current_bo) {
364 gbm_surface_release_buffer(output->surface,
365 output->current_bo);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200366 }
367
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400368 output->current_bo = output->next_bo;
369 output->next_bo = NULL;
370
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500371 if (output->pending_scanout_buffer) {
372 output->scanout_buffer = output->pending_scanout_buffer;
373 wl_list_remove(&output->pending_scanout_buffer_destroy_listener.link);
374 wl_list_insert(output->scanout_buffer->resource.destroy_listener_list.prev,
375 &output->scanout_buffer_destroy_listener.link);
376 output->pending_scanout_buffer = NULL;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200377 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100378 msecs = sec * 1000 + usec / 1000;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500379 weston_output_finish_frame(&output->base, msecs);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200380}
381
382static int
Jesse Barnes58ef3792012-02-23 09:45:49 -0500383drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
384{
385 int i;
386
387 for (i = 0; i < s->count_formats; i++)
388 if (s->formats[i] == format)
389 return 1;
390
391 return 0;
392}
393
394static int
395drm_surface_transform_supported(struct weston_surface *es)
396{
397 if (es->transform.enabled)
398 return 0;
399
400 return 1;
401}
402
403static int
404drm_surface_overlap_supported(struct weston_output *output_base,
405 pixman_region32_t *overlap)
406{
407 /* We could potentially use a color key here if the surface left
408 * to display has rectangular regions
409 */
410 if (pixman_region32_not_empty(overlap))
411 return 0;
412 return 1;
413}
414
415static void
416drm_disable_unused_sprites(struct weston_output *output_base)
417{
418 struct weston_compositor *ec = output_base->compositor;
419 struct drm_compositor *c =(struct drm_compositor *) ec;
420 struct drm_output *output = (struct drm_output *) output_base;
421 struct drm_sprite *s;
422 int ret;
423
424 wl_list_for_each(s, &c->sprite_list, link) {
425 if (s->pending_fb_id)
426 continue;
427
428 ret = drmModeSetPlane(c->drm.fd, s->plane_id,
429 output->crtc_id, 0, 0,
430 0, 0, 0, 0, 0, 0, 0, 0);
431 if (ret)
432 fprintf(stderr,
433 "failed to disable plane: %d: %s\n",
434 ret, strerror(errno));
435 drmModeRmFB(c->drm.fd, s->fb_id);
436 s->surface = NULL;
437 s->pending_surface = NULL;
438 s->fb_id = 0;
439 s->pending_fb_id = 0;
440 }
441}
442
443/*
444 * This function must take care to damage any previously assigned surface
445 * if the sprite ends up binding to a different surface than in the
446 * previous frame.
447 */
448static int
449drm_output_prepare_overlay_surface(struct weston_output *output_base,
450 struct weston_surface *es,
451 pixman_region32_t *overlap)
452{
453 struct weston_compositor *ec = output_base->compositor;
454 struct drm_compositor *c =(struct drm_compositor *) ec;
455 struct drm_sprite *s;
456 int found = 0;
457 EGLint handle, stride;
458 struct gbm_bo *bo;
459 uint32_t fb_id = 0;
460 uint32_t handles[4], pitches[4], offsets[4];
461 int ret = 0;
462 pixman_region32_t dest_rect, src_rect;
463 pixman_box32_t *box;
464 uint32_t format;
465
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500466 if (c->sprites_are_broken)
467 return -1;
468
Jesse Barnes58ef3792012-02-23 09:45:49 -0500469 if (surface_is_primary(ec, es))
470 return -1;
471
Kristian Høgsberg7a011ea2012-03-26 11:48:55 -0400472 if (es->image == EGL_NO_IMAGE_KHR)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500473 return -1;
474
475 if (!drm_surface_transform_supported(es))
476 return -1;
477
478 if (!drm_surface_overlap_supported(output_base, overlap))
479 return -1;
480
481 wl_list_for_each(s, &c->sprite_list, link) {
482 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
483 continue;
484
485 if (!s->pending_fb_id) {
486 found = 1;
487 break;
488 }
489 }
490
491 /* No sprites available */
492 if (!found)
493 return -1;
494
495 bo = gbm_bo_create_from_egl_image(c->gbm, c->base.display, es->image,
496 es->geometry.width, es->geometry.height,
497 GBM_BO_USE_SCANOUT);
498 format = gbm_bo_get_format(bo);
499 handle = gbm_bo_get_handle(bo).s32;
500 stride = gbm_bo_get_pitch(bo);
501
502 gbm_bo_destroy(bo);
503
504 if (!drm_surface_format_supported(s, format))
505 return -1;
506
507 if (!handle)
508 return -1;
509
510 handles[0] = handle;
511 pitches[0] = stride;
512 offsets[0] = 0;
513
514 ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height,
515 format, handles, pitches, offsets,
516 &fb_id, 0);
517 if (ret) {
518 fprintf(stderr, "addfb2 failed: %d\n", ret);
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500519 c->sprites_are_broken = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500520 return -1;
521 }
522
523 if (s->surface && s->surface != es) {
524 struct weston_surface *old_surf = s->surface;
525 pixman_region32_fini(&old_surf->damage);
526 pixman_region32_init_rect(&old_surf->damage,
527 old_surf->geometry.x, old_surf->geometry.y,
528 old_surf->geometry.width, old_surf->geometry.height);
529 }
530
531 s->pending_fb_id = fb_id;
532 s->pending_surface = es;
533 es->buffer->busy_count++;
534
535 /*
536 * Calculate the source & dest rects properly based on actual
537 * postion (note the caller has called weston_surface_update_transform()
538 * for us already).
539 */
540 pixman_region32_init(&dest_rect);
541 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
542 &output_base->region);
543 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
544 box = pixman_region32_extents(&dest_rect);
545 s->dest_x = box->x1;
546 s->dest_y = box->y1;
547 s->dest_w = box->x2 - box->x1;
548 s->dest_h = box->y2 - box->y1;
549 pixman_region32_fini(&dest_rect);
550
551 pixman_region32_init(&src_rect);
552 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
553 &output_base->region);
554 pixman_region32_translate(&src_rect, -es->geometry.x, -es->geometry.y);
555 box = pixman_region32_extents(&src_rect);
Rob Clark3c97b902012-03-11 19:48:43 -0500556 s->src_x = box->x1 << 16;
557 s->src_y = box->y1 << 16;
558 s->src_w = (box->x2 - box->x1) << 16;
559 s->src_h = (box->y2 - box->y1) << 16;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500560 pixman_region32_fini(&src_rect);
561
Jesse Barnes58ef3792012-02-23 09:45:49 -0500562 wl_list_insert(es->buffer->resource.destroy_listener_list.prev,
563 &s->pending_destroy_listener.link);
564 return 0;
565}
566
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500567static int
568drm_output_set_cursor(struct weston_output *output_base,
569 struct weston_input_device *eid);
570
571static void
572weston_output_set_cursor(struct weston_output *output,
573 struct weston_input_device *device,
574 pixman_region32_t *overlap)
575{
576 pixman_region32_t cursor_region;
577 int prior_was_hardware;
578
579 if (device->sprite == NULL)
580 return;
581
582 pixman_region32_init(&cursor_region);
583 pixman_region32_intersect(&cursor_region,
584 &device->sprite->transform.boundingbox,
585 &output->region);
586
587 if (!pixman_region32_not_empty(&cursor_region)) {
588 drm_output_set_cursor(output, NULL);
589 goto out;
590 }
591
592 prior_was_hardware = device->hw_cursor;
593 if (pixman_region32_not_empty(overlap) ||
594 drm_output_set_cursor(output, device) < 0) {
595 if (prior_was_hardware) {
596 weston_surface_damage(device->sprite);
597 drm_output_set_cursor(output, NULL);
598 }
599 device->hw_cursor = 0;
600 } else {
601 if (!prior_was_hardware)
602 weston_surface_damage_below(device->sprite);
603 pixman_region32_fini(&device->sprite->damage);
604 pixman_region32_init(&device->sprite->damage);
605 device->hw_cursor = 1;
606 }
607
608out:
609 pixman_region32_fini(&cursor_region);
610}
611
Jesse Barnes58ef3792012-02-23 09:45:49 -0500612static void
613drm_assign_planes(struct weston_output *output)
614{
615 struct weston_compositor *ec = output->compositor;
616 struct weston_surface *es;
617 pixman_region32_t overlap, surface_overlap;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500618 struct weston_input_device *device;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500619
620 /*
621 * Find a surface for each sprite in the output using some heuristics:
622 * 1) size
623 * 2) frequency of update
624 * 3) opacity (though some hw might support alpha blending)
625 * 4) clipping (this can be fixed with color keys)
626 *
627 * The idea is to save on blitting since this should save power.
628 * If we can get a large video surface on the sprite for example,
629 * the main display surface may not need to update at all, and
630 * the client buffer can be used directly for the sprite surface
631 * as we do for flipping full screen surfaces.
632 */
633 pixman_region32_init(&overlap);
634 wl_list_for_each(es, &ec->surface_list, link) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500635 /*
636 * FIXME: try to assign hw cursors here too, they're just
637 * special overlays
638 */
639 pixman_region32_init(&surface_overlap);
640 pixman_region32_intersect(&surface_overlap, &overlap,
641 &es->transform.boundingbox);
642
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500643 device = (struct weston_input_device *) ec->input_device;
644 if (es == device->sprite) {
645 weston_output_set_cursor(output, device,
646 &surface_overlap);
647
648 if (!device->hw_cursor)
649 pixman_region32_union(&overlap, &overlap,
650 &es->transform.boundingbox);
651 } else if (!drm_output_prepare_overlay_surface(output, es,
652 &surface_overlap)) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500653 pixman_region32_fini(&es->damage);
654 pixman_region32_init(&es->damage);
655 } else {
656 pixman_region32_union(&overlap, &overlap,
657 &es->transform.boundingbox);
658 }
659 pixman_region32_fini(&surface_overlap);
660 }
661 pixman_region32_fini(&overlap);
662
663 drm_disable_unused_sprites(output);
664}
665
666static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500667drm_output_set_cursor(struct weston_output *output_base,
668 struct weston_input_device *eid)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200669{
670 struct drm_output *output = (struct drm_output *) output_base;
671 struct drm_compositor *c =
672 (struct drm_compositor *) output->base.compositor;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200673 EGLint handle, stride;
674 int ret = -1;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200675 struct gbm_bo *bo;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200676
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400677 if (eid == NULL) {
678 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
679 return 0;
680 }
681
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200682 if (eid->sprite->image == EGL_NO_IMAGE_KHR)
683 goto out;
684
Pekka Paalanen60921e52012-01-25 15:55:43 +0200685 if (eid->sprite->geometry.width > 64 ||
686 eid->sprite->geometry.height > 64)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200687 goto out;
Benjamin Franzke117483d2011-08-30 11:38:26 +0200688
Benjamin Franzke060cf802011-04-30 09:32:11 +0200689 bo = gbm_bo_create_from_egl_image(c->gbm,
690 c->base.display,
691 eid->sprite->image, 64, 64,
692 GBM_BO_USE_CURSOR_64X64);
Kristian Høgsberg6916d9c2012-01-18 15:11:07 -0500693 /* Not suitable for hw cursor, fall back */
694 if (bo == NULL)
695 goto out;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200696
697 handle = gbm_bo_get_handle(bo).s32;
698 stride = gbm_bo_get_pitch(bo);
Benjamin Franzke060cf802011-04-30 09:32:11 +0200699 gbm_bo_destroy(bo);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200700
Kristian Høgsberg6916d9c2012-01-18 15:11:07 -0500701 /* gbm_bo_create_from_egl_image() didn't always validate the usage
702 * flags, and in that case we might end up with a bad stride. */
703 if (stride != 64 * 4)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200704 goto out;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200705
706 ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
707 if (ret) {
708 fprintf(stderr, "failed to set cursor: %s\n", strerror(-ret));
709 goto out;
710 }
711
712 ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200713 eid->sprite->geometry.x - output->base.x,
714 eid->sprite->geometry.y - output->base.y);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200715 if (ret) {
716 fprintf(stderr, "failed to move cursor: %s\n", strerror(-ret));
717 goto out;
718 }
719
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200720out:
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200721 if (ret)
722 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
723 return ret;
724}
725
Matt Roper361d2ad2011-08-29 13:52:23 -0700726static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500727drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700728{
729 struct drm_output *output = (struct drm_output *) output_base;
730 struct drm_compositor *c =
Benjamin Franzke117483d2011-08-30 11:38:26 +0200731 (struct drm_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700732 drmModeCrtcPtr origcrtc = output->original_crtc;
Matt Roper361d2ad2011-08-29 13:52:23 -0700733
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200734 if (output->backlight)
735 backlight_destroy(output->backlight);
736
Matt Roper361d2ad2011-08-29 13:52:23 -0700737 /* Turn off hardware cursor */
Benjamin Franzke117483d2011-08-30 11:38:26 +0200738 drm_output_set_cursor(&output->base, NULL);
Matt Roper361d2ad2011-08-29 13:52:23 -0700739
740 /* Restore original CRTC state */
741 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +0200742 origcrtc->x, origcrtc->y,
743 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -0700744 drmModeFreeCrtc(origcrtc);
745
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200746 c->crtc_allocator &= ~(1 << output->crtc_id);
747 c->connector_allocator &= ~(1 << output->connector_id);
748
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400749 eglDestroySurface(c->base.display, output->egl_surface);
750 gbm_surface_destroy(output->surface);
751
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500752 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200753 wl_list_remove(&output->base.link);
754
Matt Roper361d2ad2011-08-29 13:52:23 -0700755 free(output);
756}
757
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400758static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400759on_drm_input(int fd, uint32_t mask, void *data)
760{
761 drmEventContext evctx;
762
763 memset(&evctx, 0, sizeof evctx);
764 evctx.version = DRM_EVENT_CONTEXT_VERSION;
765 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500766 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400767 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400768
769 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400770}
771
772static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400773init_egl(struct drm_compositor *ec, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400774{
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400775 EGLint major, minor, n;
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400776 const char *filename, *sysnum;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400777 int fd;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400778 static const EGLint context_attribs[] = {
779 EGL_CONTEXT_CLIENT_VERSION, 2,
780 EGL_NONE
781 };
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400782
David Herrmannd7488c22012-03-11 20:05:21 +0100783 sysnum = udev_device_get_sysnum(device);
784 if (sysnum)
785 ec->drm.id = atoi(sysnum);
786 if (!sysnum || ec->drm.id < 0) {
787 fprintf(stderr, "cannot get device sysnum\n");
788 return -1;
789 }
790
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400791 static const EGLint config_attribs[] = {
792 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
793 EGL_RED_SIZE, 1,
794 EGL_GREEN_SIZE, 1,
795 EGL_BLUE_SIZE, 1,
796 EGL_ALPHA_SIZE, 0,
797 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
798 EGL_NONE
799 };
800
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400801 filename = udev_device_get_devnode(device);
David Herrmann63ff7062011-11-05 18:46:01 +0100802 fd = open(filename, O_RDWR | O_CLOEXEC);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400803 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400804 /* Probably permissions error */
805 fprintf(stderr, "couldn't open %s, skipping\n",
806 udev_device_get_devnode(device));
807 return -1;
808 }
809
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100810 ec->drm.fd = fd;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200811 ec->gbm = gbm_create_device(ec->drm.fd);
812 ec->base.display = eglGetDisplay(ec->gbm);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400813 if (ec->base.display == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400814 fprintf(stderr, "failed to create display\n");
815 return -1;
816 }
817
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400818 if (!eglInitialize(ec->base.display, &major, &minor)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400819 fprintf(stderr, "failed to initialize display\n");
820 return -1;
821 }
822
Darxus55973f22010-11-22 21:24:39 -0500823 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
824 fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n");
825 return -1;
826 }
827
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400828 if (!eglChooseConfig(ec->base.display, config_attribs,
829 &ec->base.config, 1, &n) || n != 1) {
830 fprintf(stderr, "failed to choose config: %d\n", n);
831 return -1;
832 }
833
834 ec->base.context = eglCreateContext(ec->base.display, ec->base.config,
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400835 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400836 if (ec->base.context == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400837 fprintf(stderr, "failed to create context\n");
838 return -1;
839 }
840
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400841 ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10,
842 GBM_FORMAT_XRGB8888,
843 GBM_BO_USE_RENDERING);
844 if (!ec->dummy_surface) {
845 fprintf(stderr, "failed to create dummy gbm surface\n");
846 return -1;
847 }
848
849 ec->dummy_egl_surface =
850 eglCreateWindowSurface(ec->base.display, ec->base.config,
851 ec->dummy_surface, NULL);
852 if (ec->dummy_egl_surface == EGL_NO_SURFACE) {
853 fprintf(stderr, "failed to create egl surface\n");
854 return -1;
855 }
856
857 if (!eglMakeCurrent(ec->base.display, ec->dummy_egl_surface,
858 ec->dummy_egl_surface, ec->base.context)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400859 fprintf(stderr, "failed to make context current\n");
860 return -1;
861 }
862
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400863 return 0;
864}
865
866static drmModeModeInfo builtin_1024x768 = {
867 63500, /* clock */
868 1024, 1072, 1176, 1328, 0,
869 768, 771, 775, 798, 0,
870 59920,
871 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
872 0,
873 "1024x768"
874};
875
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400876
877static int
878drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
879{
880 struct drm_mode *mode;
881
882 mode = malloc(sizeof *mode);
883 if (mode == NULL)
884 return -1;
885
886 mode->base.flags = 0;
887 mode->base.width = info->hdisplay;
888 mode->base.height = info->vdisplay;
889 mode->base.refresh = info->vrefresh;
890 mode->mode_info = *info;
891 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
892
893 return 0;
894}
895
896static int
897drm_subpixel_to_wayland(int drm_value)
898{
899 switch (drm_value) {
900 default:
901 case DRM_MODE_SUBPIXEL_UNKNOWN:
902 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
903 case DRM_MODE_SUBPIXEL_NONE:
904 return WL_OUTPUT_SUBPIXEL_NONE;
905 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
906 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
907 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
908 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
909 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
910 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
911 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
912 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
913 }
914}
915
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500916static void
917output_handle_scanout_buffer_destroy(struct wl_listener *listener,
918 struct wl_resource *resource,
919 uint32_t time)
920{
921 struct drm_output *output =
922 container_of(listener, struct drm_output,
923 scanout_buffer_destroy_listener);
924
925 output->scanout_buffer = NULL;
926
927 if (!output->pending_scanout_buffer)
928 weston_compositor_schedule_repaint(output->base.compositor);
929}
930
931static void
932output_handle_pending_scanout_buffer_destroy(struct wl_listener *listener,
933 struct wl_resource *resource,
934 uint32_t time)
935{
936 struct drm_output *output =
937 container_of(listener, struct drm_output,
938 pending_scanout_buffer_destroy_listener);
939
940 output->pending_scanout_buffer = NULL;
941
942 weston_compositor_schedule_repaint(output->base.compositor);
943}
944
Jesse Barnes58ef3792012-02-23 09:45:49 -0500945static void
946sprite_handle_buffer_destroy(struct wl_listener *listener,
947 struct wl_resource *resource,
948 uint32_t time)
949{
950 struct drm_sprite *sprite =
951 container_of(listener, struct drm_sprite,
952 destroy_listener);
953
954 sprite->surface = NULL;
955}
956
957static void
958sprite_handle_pending_buffer_destroy(struct wl_listener *listener,
959 struct wl_resource *resource,
960 uint32_t time)
961{
962 struct drm_sprite *sprite =
963 container_of(listener, struct drm_sprite,
964 pending_destroy_listener);
965
966 sprite->pending_surface = NULL;
967}
968
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300969/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200970static uint32_t
971drm_get_backlight(struct drm_output *output)
972{
973 long brightness, max_brightness, norm;
974
975 brightness = backlight_get_brightness(output->backlight);
976 max_brightness = backlight_get_max_brightness(output->backlight);
977
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300978 /* convert it on a scale of 0 to 255 */
979 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200980
981 return (uint32_t) norm;
982}
983
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300984/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200985static void
986drm_set_backlight(struct weston_output *output_base, uint32_t value)
987{
988 struct drm_output *output = (struct drm_output *) output_base;
989 long max_brightness, new_brightness;
990
991 if (!output->backlight)
992 return;
993
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300994 if (value < 0 || value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200995 return;
996
997 max_brightness = backlight_get_max_brightness(output->backlight);
998
999 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001000 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001001
1002 backlight_set_brightness(output->backlight, new_brightness);
1003}
1004
1005static drmModePropertyPtr
1006drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1007{
1008 drmModePropertyPtr props;
1009 int i;
1010
1011 for (i = 0; i < connector->count_props; i++) {
1012 props = drmModeGetProperty(fd, connector->props[i]);
1013 if (!props)
1014 continue;
1015
1016 if (!strcmp(props->name, name))
1017 return props;
1018
1019 drmModeFreeProperty(props);
1020 }
1021
1022 return NULL;
1023}
1024
1025static void
1026drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1027{
1028 struct drm_output *output = (struct drm_output *) output_base;
1029 struct weston_compositor *ec = output_base->compositor;
1030 struct drm_compositor *c = (struct drm_compositor *) ec;
1031 drmModeConnectorPtr connector;
1032 drmModePropertyPtr prop;
1033
1034 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1035 if (!connector)
1036 return;
1037
1038 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1039 if (!prop) {
1040 drmModeFreeConnector(connector);
1041 return;
1042 }
1043
1044 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1045 prop->prop_id, level);
1046 drmModeFreeProperty(prop);
1047 drmModeFreeConnector(connector);
1048}
1049
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001050static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001051create_output_for_connector(struct drm_compositor *ec,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001052 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001053 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001054 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001055{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001056 struct drm_output *output;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001057 struct drm_mode *drm_mode, *next;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001058 drmModeEncoder *encoder;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001059 int i, ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001060
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001061 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001062 if (encoder == NULL) {
1063 fprintf(stderr, "No encoder for connector.\n");
1064 return -1;
1065 }
1066
1067 for (i = 0; i < resources->count_crtcs; i++) {
Marty Jack13d9db22011-02-09 19:01:42 -05001068 if (encoder->possible_crtcs & (1 << i) &&
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001069 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001070 break;
1071 }
1072 if (i == resources->count_crtcs) {
1073 fprintf(stderr, "No usable crtc for encoder.\n");
David Herrmanneb8bed52011-12-08 17:05:44 +01001074 drmModeFreeEncoder(encoder);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001075 return -1;
1076 }
1077
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001078 output = malloc(sizeof *output);
David Herrmanneb8bed52011-12-08 17:05:44 +01001079 if (output == NULL) {
1080 drmModeFreeEncoder(encoder);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001081 return -1;
David Herrmanneb8bed52011-12-08 17:05:44 +01001082 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001083
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001084 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001085 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1086 output->base.make = "unknown";
1087 output->base.model = "unknown";
1088 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001089
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001090 output->crtc_id = resources->crtcs[i];
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001091 ec->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001092 output->connector_id = connector->connector_id;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001093 ec->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001094
Matt Roper361d2ad2011-08-29 13:52:23 -07001095 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001096 drmModeFreeEncoder(encoder);
Matt Roper361d2ad2011-08-29 13:52:23 -07001097
David Herrmann0f0d54e2011-12-08 17:05:45 +01001098 for (i = 0; i < connector->count_modes; i++) {
1099 ret = drm_output_add_mode(output, &connector->modes[i]);
1100 if (ret)
1101 goto err_free;
1102 }
1103
1104 if (connector->count_modes == 0) {
1105 ret = drm_output_add_mode(output, &builtin_1024x768);
1106 if (ret)
1107 goto err_free;
1108 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001109
1110 drm_mode = container_of(output->base.mode_list.next,
1111 struct drm_mode, base.link);
1112 output->base.current = &drm_mode->base;
1113 drm_mode->base.flags =
1114 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1115
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001116 output->surface = gbm_surface_create(ec->gbm,
1117 output->base.current->width,
1118 output->base.current->height,
1119 GBM_FORMAT_XRGB8888,
1120 GBM_BO_USE_SCANOUT |
1121 GBM_BO_USE_RENDERING);
1122 if (!output->surface) {
1123 fprintf(stderr, "failed to create gbm surface\n");
1124 goto err_free;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001125 }
1126
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001127 output->egl_surface =
1128 eglCreateWindowSurface(ec->base.display, ec->base.config,
1129 output->surface, NULL);
1130 if (output->egl_surface == EGL_NO_SURFACE) {
1131 fprintf(stderr, "failed to create egl surface\n");
1132 goto err_surface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001133 }
1134
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001135 output->backlight = backlight_init(drm_device,
1136 connector->connector_type);
1137 if (output->backlight) {
1138 output->base.set_backlight = drm_set_backlight;
1139 output->base.backlight_current = drm_get_backlight(output);
1140 }
1141
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001142 weston_output_init(&output->base, &ec->base, x, y,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001143 connector->mmWidth, connector->mmHeight,
1144 WL_OUTPUT_FLIPPED);
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04001145
1146 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1147
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001148 output->scanout_buffer_destroy_listener.func =
1149 output_handle_scanout_buffer_destroy;
1150 output->pending_scanout_buffer_destroy_listener.func =
1151 output_handle_pending_scanout_buffer_destroy;
1152
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001153 output->next_fb_id = 0;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001154 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07001155 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001156 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001157 output->base.set_dpms = drm_set_dpms;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001158
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001159 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001160
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001161 eglDestroySurface(ec->base.display, output->egl_surface);
1162err_surface:
1163 gbm_surface_destroy(output->surface);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001164err_free:
1165 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1166 base.link) {
1167 wl_list_remove(&drm_mode->base.link);
1168 free(drm_mode);
1169 }
1170
1171 drmModeFreeCrtc(output->original_crtc);
1172 ec->crtc_allocator &= ~(1 << output->crtc_id);
1173 ec->connector_allocator &= ~(1 << output->connector_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001174 free(output);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001175
David Herrmann0f0d54e2011-12-08 17:05:45 +01001176 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001177}
1178
Jesse Barnes58ef3792012-02-23 09:45:49 -05001179static void
1180create_sprites(struct drm_compositor *ec)
1181{
1182 struct drm_sprite *sprite;
1183 drmModePlaneRes *plane_res;
1184 drmModePlane *plane;
1185 int i;
1186
1187 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1188 if (!plane_res) {
1189 fprintf(stderr, "failed to get plane resources: %s\n",
1190 strerror(errno));
1191 return;
1192 }
1193
1194 for (i = 0; i < plane_res->count_planes; i++) {
1195 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1196 if (!plane)
1197 continue;
1198
1199 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1200 plane->count_formats));
1201 if (!sprite) {
1202 fprintf(stderr, "%s: out of memory\n",
1203 __func__);
1204 free(plane);
1205 continue;
1206 }
1207
1208 memset(sprite, 0, sizeof *sprite);
1209
1210 sprite->possible_crtcs = plane->possible_crtcs;
1211 sprite->plane_id = plane->plane_id;
1212 sprite->surface = NULL;
1213 sprite->pending_surface = NULL;
1214 sprite->fb_id = 0;
1215 sprite->pending_fb_id = 0;
1216 sprite->destroy_listener.func = sprite_handle_buffer_destroy;
1217 sprite->pending_destroy_listener.func =
1218 sprite_handle_pending_buffer_destroy;
1219 sprite->compositor = ec;
1220 sprite->count_formats = plane->count_formats;
1221 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05001222 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05001223 drmModeFreePlane(plane);
1224
1225 wl_list_insert(&ec->sprite_list, &sprite->link);
1226 }
1227
1228 free(plane_res->planes);
1229 free(plane_res);
1230}
1231
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001232static void
1233destroy_sprites(struct drm_compositor *compositor)
1234{
1235 struct drm_sprite *sprite, *next;
1236 struct drm_output *output;
1237
1238 output = container_of(compositor->base.output_list.next,
1239 struct drm_output, base.link);
1240
1241 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1242 drmModeSetPlane(compositor->drm.fd,
1243 sprite->plane_id,
1244 output->crtc_id, 0, 0,
1245 0, 0, 0, 0, 0, 0, 0, 0);
1246 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1247 free(sprite);
1248 }
1249}
Jesse Barnes58ef3792012-02-23 09:45:49 -05001250
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001251static int
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001252create_outputs(struct drm_compositor *ec, int option_connector,
1253 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001254{
1255 drmModeConnector *connector;
1256 drmModeRes *resources;
1257 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001258 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001259
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001260 resources = drmModeGetResources(ec->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001261 if (!resources) {
1262 fprintf(stderr, "drmModeGetResources failed\n");
1263 return -1;
1264 }
1265
Jesse Barnes58ef3792012-02-23 09:45:49 -05001266 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001267 if (!ec->crtcs) {
1268 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001269 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001270 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001271
1272 ec->num_crtcs = resources->count_crtcs;
1273 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1274
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001275 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001276 connector = drmModeGetConnector(ec->drm.fd,
1277 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001278 if (connector == NULL)
1279 continue;
1280
1281 if (connector->connection == DRM_MODE_CONNECTED &&
1282 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001283 connector->connector_id == option_connector)) {
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001284 if (create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001285 connector, x, y,
1286 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02001287 drmModeFreeConnector(connector);
1288 continue;
1289 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001290
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001291 x += container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001292 struct weston_output,
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001293 link)->current->width;
1294 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001295
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001296 drmModeFreeConnector(connector);
1297 }
1298
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001299 if (wl_list_empty(&ec->base.output_list)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001300 fprintf(stderr, "No currently active connector found.\n");
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001301 drmModeFreeResources(resources);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001302 return -1;
1303 }
1304
1305 drmModeFreeResources(resources);
1306
1307 return 0;
1308}
1309
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001310static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001311update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001312{
1313 drmModeConnector *connector;
1314 drmModeRes *resources;
1315 struct drm_output *output, *next;
1316 int x = 0, y = 0;
1317 int x_offset = 0, y_offset = 0;
1318 uint32_t connected = 0, disconnects = 0;
1319 int i;
1320
1321 resources = drmModeGetResources(ec->drm.fd);
1322 if (!resources) {
1323 fprintf(stderr, "drmModeGetResources failed\n");
1324 return;
1325 }
1326
1327 /* collect new connects */
1328 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001329 int connector_id = resources->connectors[i];
1330
1331 connector = drmModeGetConnector(ec->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01001332 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001333 continue;
1334
David Herrmann7551cff2011-12-08 17:05:43 +01001335 if (connector->connection != DRM_MODE_CONNECTED) {
1336 drmModeFreeConnector(connector);
1337 continue;
1338 }
1339
Benjamin Franzke117483d2011-08-30 11:38:26 +02001340 connected |= (1 << connector_id);
1341
1342 if (!(ec->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001343 struct weston_output *last =
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001344 container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001345 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001346
1347 /* XXX: not yet needed, we die with 0 outputs */
1348 if (!wl_list_empty(&ec->base.output_list))
Benjamin Franzke117483d2011-08-30 11:38:26 +02001349 x = last->x + last->current->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001350 else
1351 x = 0;
1352 y = 0;
1353 create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001354 connector, x, y,
1355 drm_device);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001356 printf("connector %d connected\n", connector_id);
1357
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001358 }
1359 drmModeFreeConnector(connector);
1360 }
1361 drmModeFreeResources(resources);
1362
1363 disconnects = ec->connector_allocator & ~connected;
1364 if (disconnects) {
1365 wl_list_for_each_safe(output, next, &ec->base.output_list,
1366 base.link) {
1367 if (x_offset != 0 || y_offset != 0) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001368 weston_output_move(&output->base,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001369 output->base.x - x_offset,
1370 output->base.y - y_offset);
1371 }
1372
1373 if (disconnects & (1 << output->connector_id)) {
1374 disconnects &= ~(1 << output->connector_id);
1375 printf("connector %d disconnected\n",
1376 output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001377 x_offset += output->base.current->width;
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001378 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001379 }
1380 }
1381 }
1382
1383 /* FIXME: handle zero outputs, without terminating */
1384 if (ec->connector_allocator == 0)
1385 wl_display_terminate(ec->base.wl_display);
1386}
1387
1388static int
David Herrmannd7488c22012-03-11 20:05:21 +01001389udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001390{
David Herrmannd7488c22012-03-11 20:05:21 +01001391 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01001392 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01001393
1394 sysnum = udev_device_get_sysnum(device);
1395 if (!sysnum || atoi(sysnum) != ec->drm.id)
1396 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001397
David Herrmann6ac52db2012-03-11 20:05:22 +01001398 val = udev_device_get_property_value(device, "HOTPLUG");
1399 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001400 return 0;
1401
David Herrmann6ac52db2012-03-11 20:05:22 +01001402 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001403}
1404
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001405static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001406udev_drm_event(int fd, uint32_t mask, void *data)
1407{
1408 struct drm_compositor *ec = data;
1409 struct udev_device *event;
1410
1411 event = udev_monitor_receive_device(ec->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001412
David Herrmannd7488c22012-03-11 20:05:21 +01001413 if (udev_event_is_hotplug(ec, event))
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001414 update_outputs(ec, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001415
1416 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001417
1418 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001419}
1420
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001421static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001422drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001423{
1424 struct drm_compositor *d = (struct drm_compositor *) ec;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001425 struct weston_input_device *input, *next;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001426
Jonas Ådahlc97af922012-03-28 22:36:09 +02001427 wl_list_for_each_safe(input, next, &ec->input_device_list, link)
1428 evdev_input_destroy(input);
1429
1430 wl_event_source_remove(d->udev_drm_source);
1431 wl_event_source_remove(d->drm_source);
1432
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001433 weston_compositor_shutdown(ec);
Jonas Ådahlc97af922012-03-28 22:36:09 +02001434
Matt Roper361d2ad2011-08-29 13:52:23 -07001435 gbm_device_destroy(d->gbm);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001436 destroy_sprites(d);
Kristian Høgsberg62d27742012-01-18 12:38:33 -05001437 drmDropMaster(d->drm.fd);
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001438 tty_destroy(d->tty);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001439
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001440 free(d);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001441}
1442
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001443static void
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001444drm_compositor_set_modes(struct drm_compositor *compositor)
1445{
1446 struct drm_output *output;
1447 struct drm_mode *drm_mode;
1448 int ret;
1449
1450 wl_list_for_each(output, &compositor->base.output_list, base.link) {
1451 drm_mode = (struct drm_mode *) output->base.current;
1452 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001453 output->current_fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001454 &output->connector_id, 1,
1455 &drm_mode->mode_info);
1456 if (ret < 0) {
1457 fprintf(stderr,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001458 "failed to set mode %dx%d for output at %d,%d: %m\n",
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001459 drm_mode->base.width, drm_mode->base.height,
1460 output->base.x, output->base.y);
1461 }
1462 }
1463}
1464
1465static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001466vt_func(struct weston_compositor *compositor, int event)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001467{
1468 struct drm_compositor *ec = (struct drm_compositor *) compositor;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001469 struct weston_output *output;
1470 struct weston_input_device *input;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001471 struct drm_sprite *sprite;
1472 struct drm_output *drm_output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001473
1474 switch (event) {
1475 case TTY_ENTER_VT:
1476 compositor->focus = 1;
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001477 if (drmSetMaster(ec->drm.fd)) {
1478 fprintf(stderr, "failed to set master: %m\n");
1479 wl_display_terminate(compositor->wl_display);
1480 }
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001481 compositor->state = ec->prev_state;
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001482 drm_compositor_set_modes(ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001483 weston_compositor_damage_all(compositor);
Tiago Vignatti6e2d5f12011-12-19 00:32:48 +02001484 wl_list_for_each(input, &compositor->input_device_list, link)
1485 evdev_add_devices(ec->udev, input);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001486 break;
1487 case TTY_LEAVE_VT:
1488 compositor->focus = 0;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001489 ec->prev_state = compositor->state;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001490 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04001491
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001492 /* If we have a repaint scheduled (either from a
1493 * pending pageflip or the idle handler), make sure we
1494 * cancel that so we don't try to pageflip when we're
1495 * vt switched away. The SLEEPING state will prevent
1496 * further attemps at repainting. When we switch
1497 * back, we schedule a repaint, which will process
1498 * pending frame callbacks. */
1499
1500 wl_list_for_each(output, &ec->base.output_list, link) {
1501 output->repaint_needed = 0;
1502 drm_output_set_cursor(output, NULL);
1503 }
1504
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001505 drm_output = container_of(ec->base.output_list.next,
1506 struct drm_output, base.link);
1507
1508 wl_list_for_each(sprite, &ec->sprite_list, link)
1509 drmModeSetPlane(ec->drm.fd,
1510 sprite->plane_id,
1511 drm_output->crtc_id, 0, 0,
1512 0, 0, 0, 0, 0, 0, 0, 0);
1513
Tiago Vignatti6e2d5f12011-12-19 00:32:48 +02001514 wl_list_for_each(input, &compositor->input_device_list, link)
1515 evdev_remove_devices(input);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04001516
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001517 if (drmDropMaster(ec->drm.fd) < 0)
1518 fprintf(stderr, "failed to drop master: %m\n");
1519
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001520 break;
1521 };
1522}
1523
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001524static const char default_seat[] = "seat0";
1525
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001526static struct weston_compositor *
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001527drm_compositor_create(struct wl_display *display,
Tiago Vignattifaee8012011-09-01 15:58:17 -04001528 int connector, const char *seat, int tty)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001529{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001530 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001531 struct udev_enumerate *e;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001532 struct udev_list_entry *entry;
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001533 struct udev_device *device, *drm_device;
1534 const char *path, *device_seat;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001535 struct wl_event_loop *loop;
1536
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001537 ec = malloc(sizeof *ec);
1538 if (ec == NULL)
1539 return NULL;
1540
1541 memset(ec, 0, sizeof *ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001542 ec->udev = udev_new();
1543 if (ec->udev == NULL) {
1544 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001545 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001546 }
1547
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05001548 ec->base.wl_display = display;
1549 ec->tty = tty_create(&ec->base, vt_func, tty);
1550 if (!ec->tty) {
1551 fprintf(stderr, "failed to initialize tty\n");
1552 free(ec);
1553 return NULL;
1554 }
1555
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001556 e = udev_enumerate_new(ec->udev);
1557 udev_enumerate_add_match_subsystem(e, "drm");
Benjamin Franzkea764ee52011-10-07 08:23:22 +02001558 udev_enumerate_add_match_sysname(e, "card[0-9]*");
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001559
Benjamin Franzke117483d2011-08-30 11:38:26 +02001560 udev_enumerate_scan_devices(e);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001561 drm_device = NULL;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001562 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001563 path = udev_list_entry_get_name(entry);
1564 device = udev_device_new_from_syspath(ec->udev, path);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001565 device_seat =
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001566 udev_device_get_property_value(device, "ID_SEAT");
1567 if (!device_seat)
1568 device_seat = default_seat;
1569 if (strcmp(device_seat, seat) == 0) {
1570 drm_device = device;
1571 break;
1572 }
1573 udev_device_unref(device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001574 }
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001575
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001576 if (drm_device == NULL) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001577 fprintf(stderr, "no drm device found\n");
1578 return NULL;
1579 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001580
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001581 if (init_egl(ec, drm_device) < 0) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001582 fprintf(stderr, "failed to initialize egl\n");
1583 return NULL;
1584 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001585
1586 ec->base.destroy = drm_destroy;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001587
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001588 ec->base.focus = 1;
1589
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001590 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001591
Benjamin Franzked59eb1c2011-04-29 22:14:54 +02001592 /* Can't init base class until we have a current egl context */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001593 if (weston_compositor_init(&ec->base, display) < 0)
Benjamin Franzked59eb1c2011-04-29 22:14:54 +02001594 return NULL;
1595
Jesse Barnes58ef3792012-02-23 09:45:49 -05001596 wl_list_init(&ec->sprite_list);
1597 create_sprites(ec);
1598
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001599 if (create_outputs(ec, connector, drm_device) < 0) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001600 fprintf(stderr, "failed to create output for %s\n", path);
1601 return NULL;
1602 }
1603
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001604 udev_device_unref(drm_device);
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02001605 udev_enumerate_unref(e);
1606 path = NULL;
1607
Tiago Vignattice03ec32011-12-19 01:14:03 +02001608 evdev_input_create(&ec->base, ec->udev, seat);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001609
1610 loop = wl_display_get_event_loop(ec->base.wl_display);
1611 ec->drm_source =
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001612 wl_event_loop_add_fd(loop, ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001613 WL_EVENT_READABLE, on_drm_input, ec);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001614
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001615 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
1616 if (ec->udev_monitor == NULL) {
1617 fprintf(stderr, "failed to intialize udev monitor\n");
1618 return NULL;
1619 }
1620 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
1621 "drm", NULL);
1622 ec->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02001623 wl_event_loop_add_fd(loop,
1624 udev_monitor_get_fd(ec->udev_monitor),
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001625 WL_EVENT_READABLE, udev_drm_event, ec);
1626
1627 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
1628 fprintf(stderr, "failed to enable udev-monitor receiving\n");
1629 return NULL;
1630 }
1631
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001632 return &ec->base;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001633}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001634
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001635WL_EXPORT struct weston_compositor *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001636backend_init(struct wl_display *display, int argc, char *argv[])
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001637{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001638 int connector = 0, tty = 0;
1639 const char *seat = default_seat;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001640
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001641 const struct weston_option drm_options[] = {
1642 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
1643 { WESTON_OPTION_STRING, "seat", 0, &seat },
1644 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
1645 };
Benjamin Franzke117483d2011-08-30 11:38:26 +02001646
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001647 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001648
Tiago Vignattifaee8012011-09-01 15:58:17 -04001649 return drm_compositor_create(display, connector, seat, tty);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001650}