blob: ba376eb9510e073b0b754aa512ee99039aeb829b [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
Jesse Barnes58ef3792012-02-23 09:45:49 -050063 struct wl_list sprite_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -050064 int sprites_are_broken;
Jesse Barnes58ef3792012-02-23 09:45:49 -050065
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +020066 uint32_t prev_state;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040067};
68
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040069struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050070 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040071 drmModeModeInfo mode_info;
72};
73
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040074struct drm_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050075 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040076
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040077 uint32_t crtc_id;
78 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -070079 drmModeCrtcPtr original_crtc;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040080 GLuint rbo[2];
81 uint32_t fb_id[2];
82 EGLImageKHR image[2];
Benjamin Franzke060cf802011-04-30 09:32:11 +020083 struct gbm_bo *bo[2];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040084 uint32_t current;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +020085
86 uint32_t fs_surf_fb_id;
87 uint32_t pending_fs_surf_fb_id;
Kristian Høgsberg9f404b72012-01-26 00:11:01 -050088 struct wl_buffer *scanout_buffer;
89 struct wl_listener scanout_buffer_destroy_listener;
90 struct wl_buffer *pending_scanout_buffer;
91 struct wl_listener pending_scanout_buffer_destroy_listener;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +020092 struct backlight *backlight;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040093};
94
Jesse Barnes58ef3792012-02-23 09:45:49 -050095/*
96 * An output has a primary display plane plus zero or more sprites for
97 * blending display contents.
98 */
99struct drm_sprite {
100 struct wl_list link;
101
102 uint32_t fb_id;
103 uint32_t pending_fb_id;
104 struct weston_surface *surface;
105 struct weston_surface *pending_surface;
106
107 struct drm_compositor *compositor;
108
109 struct wl_listener destroy_listener;
110 struct wl_listener pending_destroy_listener;
111
112 uint32_t possible_crtcs;
113 uint32_t plane_id;
114 uint32_t count_formats;
115
116 int32_t src_x, src_y;
117 uint32_t src_w, src_h;
118 uint32_t dest_x, dest_y;
119 uint32_t dest_w, dest_h;
120
121 uint32_t formats[];
122};
123
124static int
125surface_is_primary(struct weston_compositor *ec, struct weston_surface *es)
126{
127 struct weston_surface *primary;
128
129 primary = container_of(ec->surface_list.next, struct weston_surface,
130 link);
131 if (es == primary)
132 return -1;
133 return 0;
134}
135
136static int
137drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
138{
139 struct weston_compositor *ec = output_base->compositor;
140 struct drm_compositor *c =(struct drm_compositor *) ec;
141 struct drm_output *output = (struct drm_output *) output_base;
142 int crtc;
143
144 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
145 if (c->crtcs[crtc] != output->crtc_id)
146 continue;
147
148 if (supported & (1 << crtc))
149 return -1;
150 }
151
152 return 0;
153}
154
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500155static int
156drm_output_prepare_scanout_surface(struct drm_output *output)
157{
158 struct drm_compositor *c =
159 (struct drm_compositor *) output->base.compositor;
160 struct weston_surface *es;
161 EGLint handle, stride;
162 int ret;
163 uint32_t fb_id = 0;
164 struct gbm_bo *bo;
165
166 es = container_of(c->base.surface_list.next,
167 struct weston_surface, link);
168
Kristian Høgsberg101cb652012-02-17 10:45:16 -0500169 /* Need to verify output->region contained in surface opaque
170 * region. Or maybe just that format doesn't have alpha. */
171 return -1;
172
173 if (es->geometry.x != output->base.x ||
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200174 es->geometry.y != output->base.y ||
Pekka Paalanen60921e52012-01-25 15:55:43 +0200175 es->geometry.width != output->base.current->width ||
176 es->geometry.height != output->base.current->height ||
Pekka Paalanenf1f5b362012-01-25 14:33:33 +0200177 es->transform.enabled ||
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500178 es->image == EGL_NO_IMAGE_KHR)
179 return -1;
180
181 bo = gbm_bo_create_from_egl_image(c->gbm,
182 c->base.display, es->image,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200183 es->geometry.width,
184 es->geometry.height,
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500185 GBM_BO_USE_SCANOUT);
186
187 handle = gbm_bo_get_handle(bo).s32;
188 stride = gbm_bo_get_pitch(bo);
189
190 gbm_bo_destroy(bo);
191
192 if (handle == 0)
193 return -1;
194
195 ret = drmModeAddFB(c->drm.fd,
196 output->base.current->width,
197 output->base.current->height,
198 24, 32, stride, handle, &fb_id);
199
200 if (ret)
201 return -1;
202
203 output->pending_fs_surf_fb_id = fb_id;
204
205 /* assert output->pending_scanout_buffer == NULL */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500206 output->pending_scanout_buffer = es->buffer;
207 output->pending_scanout_buffer->busy_count++;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500208
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500209 wl_list_insert(output->pending_scanout_buffer->resource.destroy_listener_list.prev,
210 &output->pending_scanout_buffer_destroy_listener.link);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500211
212 pixman_region32_fini(&es->damage);
213 pixman_region32_init(&es->damage);
214
215 return 0;
216}
217
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500218static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500219drm_output_repaint(struct weston_output *output_base,
220 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100221{
222 struct drm_output *output = (struct drm_output *) output_base;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500223 struct drm_compositor *compositor =
224 (struct drm_compositor *) output->base.compositor;
225 struct weston_surface *surface;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500226 struct drm_sprite *s;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500227 uint32_t fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500228 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100229
230 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
231 GL_COLOR_ATTACHMENT0,
232 GL_RENDERBUFFER,
233 output->rbo[output->current]);
234
235 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500236 return;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100237
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500238 drm_output_prepare_scanout_surface(output);
239
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500240 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500241 weston_surface_draw(surface, &output->base, damage);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400242
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100243 glFlush();
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400244
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100245 output->current ^= 1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100246
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400247 if (output->pending_fs_surf_fb_id != 0) {
248 fb_id = output->pending_fs_surf_fb_id;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200249 } else {
250 fb_id = output->fb_id[output->current ^ 1];
251 }
252
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500253 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500254 fb_id,
255 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
256 fprintf(stderr, "queueing pageflip failed: %m\n");
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500257 return;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500258 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100259
Jesse Barnes58ef3792012-02-23 09:45:49 -0500260 /*
261 * Now, update all the sprite surfaces
262 */
263 wl_list_for_each(s, &compositor->sprite_list, link) {
264 uint32_t flags = 0;
265 drmVBlank vbl = {
266 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
267 .request.sequence = 1,
268 };
269
270 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
271 continue;
272
273 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
274 output->crtc_id, s->pending_fb_id, flags,
275 s->dest_x, s->dest_y,
276 s->dest_w, s->dest_h,
277 s->src_x, s->src_y,
278 s->src_w, s->src_h);
279 if (ret)
280 fprintf(stderr, "setplane failed: %d: %s\n",
281 ret, strerror(errno));
282
283 /*
284 * Queue a vblank signal so we know when the surface
285 * becomes active on the display or has been replaced.
286 */
287 vbl.request.signal = (unsigned long)s;
288 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
289 if (ret) {
290 fprintf(stderr, "vblank event request failed: %d: %s\n",
291 ret, strerror(errno));
292 }
293 }
294
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500295 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400296}
297
298static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500299vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
300 void *data)
301{
302 struct drm_sprite *s = (struct drm_sprite *)data;
303 struct drm_compositor *c = s->compositor;
304
305 if (s->surface) {
306 weston_buffer_post_release(s->surface->buffer);
307 wl_list_remove(&s->destroy_listener.link);
308 s->surface = NULL;
309 drmModeRmFB(c->drm.fd, s->fb_id);
310 s->fb_id = 0;
311 }
312
313 if (s->pending_surface) {
314 wl_list_remove(&s->pending_destroy_listener.link);
315 wl_list_insert(s->pending_surface->buffer->resource.destroy_listener_list.prev,
316 &s->destroy_listener.link);
317 s->surface = s->pending_surface;
318 s->pending_surface = NULL;
319 s->fb_id = s->pending_fb_id;
320 s->pending_fb_id = 0;
321 }
322}
323
324static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400325page_flip_handler(int fd, unsigned int frame,
326 unsigned int sec, unsigned int usec, void *data)
327{
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200328 struct drm_output *output = (struct drm_output *) data;
329 struct drm_compositor *c =
330 (struct drm_compositor *) output->base.compositor;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400331 uint32_t msecs;
332
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500333 if (output->scanout_buffer) {
334 weston_buffer_post_release(output->scanout_buffer);
335 wl_list_remove(&output->scanout_buffer_destroy_listener.link);
336 output->scanout_buffer = NULL;
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400337 drmModeRmFB(c->drm.fd, output->fs_surf_fb_id);
338 output->fs_surf_fb_id = 0;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200339 }
340
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500341 if (output->pending_scanout_buffer) {
342 output->scanout_buffer = output->pending_scanout_buffer;
343 wl_list_remove(&output->pending_scanout_buffer_destroy_listener.link);
344 wl_list_insert(output->scanout_buffer->resource.destroy_listener_list.prev,
345 &output->scanout_buffer_destroy_listener.link);
346 output->pending_scanout_buffer = NULL;
Kristian Høgsberg191454e2011-10-20 17:51:45 -0400347 output->fs_surf_fb_id = output->pending_fs_surf_fb_id;
348 output->pending_fs_surf_fb_id = 0;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200349 }
350
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100351 msecs = sec * 1000 + usec / 1000;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500352 weston_output_finish_frame(&output->base, msecs);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200353}
354
355static int
Jesse Barnes58ef3792012-02-23 09:45:49 -0500356drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
357{
358 int i;
359
360 for (i = 0; i < s->count_formats; i++)
361 if (s->formats[i] == format)
362 return 1;
363
364 return 0;
365}
366
367static int
368drm_surface_transform_supported(struct weston_surface *es)
369{
370 if (es->transform.enabled)
371 return 0;
372
373 return 1;
374}
375
376static int
377drm_surface_overlap_supported(struct weston_output *output_base,
378 pixman_region32_t *overlap)
379{
380 /* We could potentially use a color key here if the surface left
381 * to display has rectangular regions
382 */
383 if (pixman_region32_not_empty(overlap))
384 return 0;
385 return 1;
386}
387
388static void
389drm_disable_unused_sprites(struct weston_output *output_base)
390{
391 struct weston_compositor *ec = output_base->compositor;
392 struct drm_compositor *c =(struct drm_compositor *) ec;
393 struct drm_output *output = (struct drm_output *) output_base;
394 struct drm_sprite *s;
395 int ret;
396
397 wl_list_for_each(s, &c->sprite_list, link) {
398 if (s->pending_fb_id)
399 continue;
400
401 ret = drmModeSetPlane(c->drm.fd, s->plane_id,
402 output->crtc_id, 0, 0,
403 0, 0, 0, 0, 0, 0, 0, 0);
404 if (ret)
405 fprintf(stderr,
406 "failed to disable plane: %d: %s\n",
407 ret, strerror(errno));
408 drmModeRmFB(c->drm.fd, s->fb_id);
409 s->surface = NULL;
410 s->pending_surface = NULL;
411 s->fb_id = 0;
412 s->pending_fb_id = 0;
413 }
414}
415
416/*
417 * This function must take care to damage any previously assigned surface
418 * if the sprite ends up binding to a different surface than in the
419 * previous frame.
420 */
421static int
422drm_output_prepare_overlay_surface(struct weston_output *output_base,
423 struct weston_surface *es,
424 pixman_region32_t *overlap)
425{
426 struct weston_compositor *ec = output_base->compositor;
427 struct drm_compositor *c =(struct drm_compositor *) ec;
428 struct drm_sprite *s;
429 int found = 0;
430 EGLint handle, stride;
431 struct gbm_bo *bo;
432 uint32_t fb_id = 0;
433 uint32_t handles[4], pitches[4], offsets[4];
434 int ret = 0;
435 pixman_region32_t dest_rect, src_rect;
436 pixman_box32_t *box;
437 uint32_t format;
438
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500439 if (c->sprites_are_broken)
440 return -1;
441
Jesse Barnes58ef3792012-02-23 09:45:49 -0500442 if (surface_is_primary(ec, es))
443 return -1;
444
445 if (!es->buffer)
446 return -1;
447
448 if (!drm_surface_transform_supported(es))
449 return -1;
450
451 if (!drm_surface_overlap_supported(output_base, overlap))
452 return -1;
453
454 wl_list_for_each(s, &c->sprite_list, link) {
455 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
456 continue;
457
458 if (!s->pending_fb_id) {
459 found = 1;
460 break;
461 }
462 }
463
464 /* No sprites available */
465 if (!found)
466 return -1;
467
468 bo = gbm_bo_create_from_egl_image(c->gbm, c->base.display, es->image,
469 es->geometry.width, es->geometry.height,
470 GBM_BO_USE_SCANOUT);
471 format = gbm_bo_get_format(bo);
472 handle = gbm_bo_get_handle(bo).s32;
473 stride = gbm_bo_get_pitch(bo);
474
475 gbm_bo_destroy(bo);
476
477 if (!drm_surface_format_supported(s, format))
478 return -1;
479
480 if (!handle)
481 return -1;
482
483 handles[0] = handle;
484 pitches[0] = stride;
485 offsets[0] = 0;
486
487 ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height,
488 format, handles, pitches, offsets,
489 &fb_id, 0);
490 if (ret) {
491 fprintf(stderr, "addfb2 failed: %d\n", ret);
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500492 c->sprites_are_broken = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500493 return -1;
494 }
495
496 if (s->surface && s->surface != es) {
497 struct weston_surface *old_surf = s->surface;
498 pixman_region32_fini(&old_surf->damage);
499 pixman_region32_init_rect(&old_surf->damage,
500 old_surf->geometry.x, old_surf->geometry.y,
501 old_surf->geometry.width, old_surf->geometry.height);
502 }
503
504 s->pending_fb_id = fb_id;
505 s->pending_surface = es;
506 es->buffer->busy_count++;
507
508 /*
509 * Calculate the source & dest rects properly based on actual
510 * postion (note the caller has called weston_surface_update_transform()
511 * for us already).
512 */
513 pixman_region32_init(&dest_rect);
514 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
515 &output_base->region);
516 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
517 box = pixman_region32_extents(&dest_rect);
518 s->dest_x = box->x1;
519 s->dest_y = box->y1;
520 s->dest_w = box->x2 - box->x1;
521 s->dest_h = box->y2 - box->y1;
522 pixman_region32_fini(&dest_rect);
523
524 pixman_region32_init(&src_rect);
525 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
526 &output_base->region);
527 pixman_region32_translate(&src_rect, -es->geometry.x, -es->geometry.y);
528 box = pixman_region32_extents(&src_rect);
529 s->src_x = box->x1;
530 s->src_y = box->y1;
531 s->src_w = box->x2 - box->x1;
532 s->src_h = box->y2 - box->y1;
533 pixman_region32_fini(&src_rect);
534
535
536 wl_list_insert(es->buffer->resource.destroy_listener_list.prev,
537 &s->pending_destroy_listener.link);
538 return 0;
539}
540
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500541static int
542drm_output_set_cursor(struct weston_output *output_base,
543 struct weston_input_device *eid);
544
545static void
546weston_output_set_cursor(struct weston_output *output,
547 struct weston_input_device *device,
548 pixman_region32_t *overlap)
549{
550 pixman_region32_t cursor_region;
551 int prior_was_hardware;
552
553 if (device->sprite == NULL)
554 return;
555
556 pixman_region32_init(&cursor_region);
557 pixman_region32_intersect(&cursor_region,
558 &device->sprite->transform.boundingbox,
559 &output->region);
560
561 if (!pixman_region32_not_empty(&cursor_region)) {
562 drm_output_set_cursor(output, NULL);
563 goto out;
564 }
565
566 prior_was_hardware = device->hw_cursor;
567 if (pixman_region32_not_empty(overlap) ||
568 drm_output_set_cursor(output, device) < 0) {
569 if (prior_was_hardware) {
570 weston_surface_damage(device->sprite);
571 drm_output_set_cursor(output, NULL);
572 }
573 device->hw_cursor = 0;
574 } else {
575 if (!prior_was_hardware)
576 weston_surface_damage_below(device->sprite);
577 pixman_region32_fini(&device->sprite->damage);
578 pixman_region32_init(&device->sprite->damage);
579 device->hw_cursor = 1;
580 }
581
582out:
583 pixman_region32_fini(&cursor_region);
584}
585
Jesse Barnes58ef3792012-02-23 09:45:49 -0500586static void
587drm_assign_planes(struct weston_output *output)
588{
589 struct weston_compositor *ec = output->compositor;
590 struct weston_surface *es;
591 pixman_region32_t overlap, surface_overlap;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500592 struct weston_input_device *device;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500593
594 /*
595 * Find a surface for each sprite in the output using some heuristics:
596 * 1) size
597 * 2) frequency of update
598 * 3) opacity (though some hw might support alpha blending)
599 * 4) clipping (this can be fixed with color keys)
600 *
601 * The idea is to save on blitting since this should save power.
602 * If we can get a large video surface on the sprite for example,
603 * the main display surface may not need to update at all, and
604 * the client buffer can be used directly for the sprite surface
605 * as we do for flipping full screen surfaces.
606 */
607 pixman_region32_init(&overlap);
608 wl_list_for_each(es, &ec->surface_list, link) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500609 /*
610 * FIXME: try to assign hw cursors here too, they're just
611 * special overlays
612 */
613 pixman_region32_init(&surface_overlap);
614 pixman_region32_intersect(&surface_overlap, &overlap,
615 &es->transform.boundingbox);
616
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500617 device = (struct weston_input_device *) ec->input_device;
618 if (es == device->sprite) {
619 weston_output_set_cursor(output, device,
620 &surface_overlap);
621
622 if (!device->hw_cursor)
623 pixman_region32_union(&overlap, &overlap,
624 &es->transform.boundingbox);
625 } else if (!drm_output_prepare_overlay_surface(output, es,
626 &surface_overlap)) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500627 pixman_region32_fini(&es->damage);
628 pixman_region32_init(&es->damage);
629 } else {
630 pixman_region32_union(&overlap, &overlap,
631 &es->transform.boundingbox);
632 }
633 pixman_region32_fini(&surface_overlap);
634 }
635 pixman_region32_fini(&overlap);
636
637 drm_disable_unused_sprites(output);
638}
639
640static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500641drm_output_set_cursor(struct weston_output *output_base,
642 struct weston_input_device *eid)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200643{
644 struct drm_output *output = (struct drm_output *) output_base;
645 struct drm_compositor *c =
646 (struct drm_compositor *) output->base.compositor;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200647 EGLint handle, stride;
648 int ret = -1;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200649 struct gbm_bo *bo;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200650
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400651 if (eid == NULL) {
652 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
653 return 0;
654 }
655
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200656 if (eid->sprite->image == EGL_NO_IMAGE_KHR)
657 goto out;
658
Pekka Paalanen60921e52012-01-25 15:55:43 +0200659 if (eid->sprite->geometry.width > 64 ||
660 eid->sprite->geometry.height > 64)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200661 goto out;
Benjamin Franzke117483d2011-08-30 11:38:26 +0200662
Benjamin Franzke060cf802011-04-30 09:32:11 +0200663 bo = gbm_bo_create_from_egl_image(c->gbm,
664 c->base.display,
665 eid->sprite->image, 64, 64,
666 GBM_BO_USE_CURSOR_64X64);
Kristian Høgsberg6916d9c2012-01-18 15:11:07 -0500667 /* Not suitable for hw cursor, fall back */
668 if (bo == NULL)
669 goto out;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200670
671 handle = gbm_bo_get_handle(bo).s32;
672 stride = gbm_bo_get_pitch(bo);
Benjamin Franzke060cf802011-04-30 09:32:11 +0200673 gbm_bo_destroy(bo);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200674
Kristian Høgsberg6916d9c2012-01-18 15:11:07 -0500675 /* gbm_bo_create_from_egl_image() didn't always validate the usage
676 * flags, and in that case we might end up with a bad stride. */
677 if (stride != 64 * 4)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200678 goto out;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200679
680 ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
681 if (ret) {
682 fprintf(stderr, "failed to set cursor: %s\n", strerror(-ret));
683 goto out;
684 }
685
686 ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200687 eid->sprite->geometry.x - output->base.x,
688 eid->sprite->geometry.y - output->base.y);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200689 if (ret) {
690 fprintf(stderr, "failed to move cursor: %s\n", strerror(-ret));
691 goto out;
692 }
693
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200694out:
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200695 if (ret)
696 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
697 return ret;
698}
699
Matt Roper361d2ad2011-08-29 13:52:23 -0700700static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500701drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700702{
703 struct drm_output *output = (struct drm_output *) output_base;
704 struct drm_compositor *c =
Benjamin Franzke117483d2011-08-30 11:38:26 +0200705 (struct drm_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700706 drmModeCrtcPtr origcrtc = output->original_crtc;
707 int i;
708
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200709 if (output->backlight)
710 backlight_destroy(output->backlight);
711
Matt Roper361d2ad2011-08-29 13:52:23 -0700712 /* Turn off hardware cursor */
Benjamin Franzke117483d2011-08-30 11:38:26 +0200713 drm_output_set_cursor(&output->base, NULL);
Matt Roper361d2ad2011-08-29 13:52:23 -0700714
715 /* Restore original CRTC state */
716 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +0200717 origcrtc->x, origcrtc->y,
718 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -0700719 drmModeFreeCrtc(origcrtc);
720
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200721 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
722 GL_COLOR_ATTACHMENT0,
723 GL_RENDERBUFFER,
724 0);
725
726 glBindRenderbuffer(GL_RENDERBUFFER, 0);
727 glDeleteRenderbuffers(2, output->rbo);
728
Matt Roper361d2ad2011-08-29 13:52:23 -0700729 /* Destroy output buffers */
730 for (i = 0; i < 2; i++) {
731 drmModeRmFB(c->drm.fd, output->fb_id[i]);
732 c->base.destroy_image(c->base.display, output->image[i]);
733 gbm_bo_destroy(output->bo[i]);
734 }
735
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200736 c->crtc_allocator &= ~(1 << output->crtc_id);
737 c->connector_allocator &= ~(1 << output->connector_id);
738
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500739 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200740 wl_list_remove(&output->base.link);
741
Matt Roper361d2ad2011-08-29 13:52:23 -0700742 free(output);
743}
744
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400745static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400746on_drm_input(int fd, uint32_t mask, void *data)
747{
748 drmEventContext evctx;
749
750 memset(&evctx, 0, sizeof evctx);
751 evctx.version = DRM_EVENT_CONTEXT_VERSION;
752 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500753 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400754 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400755
756 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400757}
758
759static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400760init_egl(struct drm_compositor *ec, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400761{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400762 EGLint major, minor;
David Herrmannd7488c22012-03-11 20:05:21 +0100763 const char *extensions, *filename, *sysnum;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400764 int fd;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400765 static const EGLint context_attribs[] = {
766 EGL_CONTEXT_CLIENT_VERSION, 2,
767 EGL_NONE
768 };
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400769
David Herrmannd7488c22012-03-11 20:05:21 +0100770 sysnum = udev_device_get_sysnum(device);
771 if (sysnum)
772 ec->drm.id = atoi(sysnum);
773 if (!sysnum || ec->drm.id < 0) {
774 fprintf(stderr, "cannot get device sysnum\n");
775 return -1;
776 }
777
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400778 filename = udev_device_get_devnode(device);
David Herrmann63ff7062011-11-05 18:46:01 +0100779 fd = open(filename, O_RDWR | O_CLOEXEC);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400780 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400781 /* Probably permissions error */
782 fprintf(stderr, "couldn't open %s, skipping\n",
783 udev_device_get_devnode(device));
784 return -1;
785 }
786
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100787 ec->drm.fd = fd;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200788 ec->gbm = gbm_create_device(ec->drm.fd);
789 ec->base.display = eglGetDisplay(ec->gbm);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400790 if (ec->base.display == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400791 fprintf(stderr, "failed to create display\n");
792 return -1;
793 }
794
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400795 if (!eglInitialize(ec->base.display, &major, &minor)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400796 fprintf(stderr, "failed to initialize display\n");
797 return -1;
798 }
799
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400800 extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS);
Ander Conselvan de Oliveiraef7c8d92011-11-01 16:37:41 +0200801 if (!strstr(extensions, "EGL_KHR_surfaceless_gles2")) {
802 fprintf(stderr, "EGL_KHR_surfaceless_gles2 not available\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400803 return -1;
804 }
805
Darxus55973f22010-11-22 21:24:39 -0500806 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
807 fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n");
808 return -1;
809 }
810
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400811 ec->base.context = eglCreateContext(ec->base.display, NULL,
812 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400813 if (ec->base.context == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400814 fprintf(stderr, "failed to create context\n");
815 return -1;
816 }
817
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400818 if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE,
819 EGL_NO_SURFACE, ec->base.context)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400820 fprintf(stderr, "failed to make context current\n");
821 return -1;
822 }
823
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400824 return 0;
825}
826
827static drmModeModeInfo builtin_1024x768 = {
828 63500, /* clock */
829 1024, 1072, 1176, 1328, 0,
830 768, 771, 775, 798, 0,
831 59920,
832 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
833 0,
834 "1024x768"
835};
836
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400837
838static int
839drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
840{
841 struct drm_mode *mode;
842
843 mode = malloc(sizeof *mode);
844 if (mode == NULL)
845 return -1;
846
847 mode->base.flags = 0;
848 mode->base.width = info->hdisplay;
849 mode->base.height = info->vdisplay;
850 mode->base.refresh = info->vrefresh;
851 mode->mode_info = *info;
852 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
853
854 return 0;
855}
856
857static int
858drm_subpixel_to_wayland(int drm_value)
859{
860 switch (drm_value) {
861 default:
862 case DRM_MODE_SUBPIXEL_UNKNOWN:
863 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
864 case DRM_MODE_SUBPIXEL_NONE:
865 return WL_OUTPUT_SUBPIXEL_NONE;
866 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
867 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
868 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
869 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
870 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
871 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
872 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
873 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
874 }
875}
876
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500877static void
878output_handle_scanout_buffer_destroy(struct wl_listener *listener,
879 struct wl_resource *resource,
880 uint32_t time)
881{
882 struct drm_output *output =
883 container_of(listener, struct drm_output,
884 scanout_buffer_destroy_listener);
885
886 output->scanout_buffer = NULL;
887
888 if (!output->pending_scanout_buffer)
889 weston_compositor_schedule_repaint(output->base.compositor);
890}
891
892static void
893output_handle_pending_scanout_buffer_destroy(struct wl_listener *listener,
894 struct wl_resource *resource,
895 uint32_t time)
896{
897 struct drm_output *output =
898 container_of(listener, struct drm_output,
899 pending_scanout_buffer_destroy_listener);
900
901 output->pending_scanout_buffer = NULL;
902
903 weston_compositor_schedule_repaint(output->base.compositor);
904}
905
Jesse Barnes58ef3792012-02-23 09:45:49 -0500906static void
907sprite_handle_buffer_destroy(struct wl_listener *listener,
908 struct wl_resource *resource,
909 uint32_t time)
910{
911 struct drm_sprite *sprite =
912 container_of(listener, struct drm_sprite,
913 destroy_listener);
914
915 sprite->surface = NULL;
916}
917
918static void
919sprite_handle_pending_buffer_destroy(struct wl_listener *listener,
920 struct wl_resource *resource,
921 uint32_t time)
922{
923 struct drm_sprite *sprite =
924 container_of(listener, struct drm_sprite,
925 pending_destroy_listener);
926
927 sprite->pending_surface = NULL;
928}
929
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200930/* returns a value between 1-10 range, where higher is brighter */
931static uint32_t
932drm_get_backlight(struct drm_output *output)
933{
934 long brightness, max_brightness, norm;
935
936 brightness = backlight_get_brightness(output->backlight);
937 max_brightness = backlight_get_max_brightness(output->backlight);
938
939 /* convert it on a scale of 1 to 10 */
940 norm = 1 + ((brightness) * 9)/(max_brightness);
941
942 return (uint32_t) norm;
943}
944
945/* values accepted are between 1-10 range */
946static void
947drm_set_backlight(struct weston_output *output_base, uint32_t value)
948{
949 struct drm_output *output = (struct drm_output *) output_base;
950 long max_brightness, new_brightness;
951
952 if (!output->backlight)
953 return;
954
955 if (value < 1 || value > 10)
956 return;
957
958 max_brightness = backlight_get_max_brightness(output->backlight);
959
960 /* get denormalized value */
961 new_brightness = ((value - 1) * (max_brightness)) / 9;
962
963 backlight_set_brightness(output->backlight, new_brightness);
964}
965
966static drmModePropertyPtr
967drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
968{
969 drmModePropertyPtr props;
970 int i;
971
972 for (i = 0; i < connector->count_props; i++) {
973 props = drmModeGetProperty(fd, connector->props[i]);
974 if (!props)
975 continue;
976
977 if (!strcmp(props->name, name))
978 return props;
979
980 drmModeFreeProperty(props);
981 }
982
983 return NULL;
984}
985
986static void
987drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
988{
989 struct drm_output *output = (struct drm_output *) output_base;
990 struct weston_compositor *ec = output_base->compositor;
991 struct drm_compositor *c = (struct drm_compositor *) ec;
992 drmModeConnectorPtr connector;
993 drmModePropertyPtr prop;
994
995 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
996 if (!connector)
997 return;
998
999 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1000 if (!prop) {
1001 drmModeFreeConnector(connector);
1002 return;
1003 }
1004
1005 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1006 prop->prop_id, level);
1007 drmModeFreeProperty(prop);
1008 drmModeFreeConnector(connector);
1009}
1010
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001011static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001012create_output_for_connector(struct drm_compositor *ec,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001013 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001014 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001015 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001016{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001017 struct drm_output *output;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001018 struct drm_mode *drm_mode, *next;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001019 drmModeEncoder *encoder;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001020 int i, ret;
Benjamin Franzke060cf802011-04-30 09:32:11 +02001021 unsigned handle, stride;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001022
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001023 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001024 if (encoder == NULL) {
1025 fprintf(stderr, "No encoder for connector.\n");
1026 return -1;
1027 }
1028
1029 for (i = 0; i < resources->count_crtcs; i++) {
Marty Jack13d9db22011-02-09 19:01:42 -05001030 if (encoder->possible_crtcs & (1 << i) &&
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001031 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001032 break;
1033 }
1034 if (i == resources->count_crtcs) {
1035 fprintf(stderr, "No usable crtc for encoder.\n");
David Herrmanneb8bed52011-12-08 17:05:44 +01001036 drmModeFreeEncoder(encoder);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001037 return -1;
1038 }
1039
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001040 output = malloc(sizeof *output);
David Herrmanneb8bed52011-12-08 17:05:44 +01001041 if (output == NULL) {
1042 drmModeFreeEncoder(encoder);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001043 return -1;
David Herrmanneb8bed52011-12-08 17:05:44 +01001044 }
David Herrmannd7c45512011-12-08 17:05:46 +01001045 output->fb_id[0] = -1;
1046 output->fb_id[1] = -1;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001047
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001048 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001049 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1050 output->base.make = "unknown";
1051 output->base.model = "unknown";
1052 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001053
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001054 output->crtc_id = resources->crtcs[i];
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001055 ec->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001056 output->connector_id = connector->connector_id;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001057 ec->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001058
Matt Roper361d2ad2011-08-29 13:52:23 -07001059 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001060 drmModeFreeEncoder(encoder);
Matt Roper361d2ad2011-08-29 13:52:23 -07001061
David Herrmann0f0d54e2011-12-08 17:05:45 +01001062 for (i = 0; i < connector->count_modes; i++) {
1063 ret = drm_output_add_mode(output, &connector->modes[i]);
1064 if (ret)
1065 goto err_free;
1066 }
1067
1068 if (connector->count_modes == 0) {
1069 ret = drm_output_add_mode(output, &builtin_1024x768);
1070 if (ret)
1071 goto err_free;
1072 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001073
1074 drm_mode = container_of(output->base.mode_list.next,
1075 struct drm_mode, base.link);
1076 output->base.current = &drm_mode->base;
1077 drm_mode->base.flags =
1078 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1079
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001080 glGenRenderbuffers(2, output->rbo);
1081 for (i = 0; i < 2; i++) {
1082 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
1083
Benjamin Franzke060cf802011-04-30 09:32:11 +02001084 output->bo[i] =
1085 gbm_bo_create(ec->gbm,
1086 output->base.current->width,
1087 output->base.current->height,
1088 GBM_BO_FORMAT_XRGB8888,
Benjamin Franzke117483d2011-08-30 11:38:26 +02001089 GBM_BO_USE_SCANOUT |
1090 GBM_BO_USE_RENDERING);
David Herrmannd7c45512011-12-08 17:05:46 +01001091 if (!output->bo[i])
1092 goto err_bufs;
1093
Benjamin Franzke060cf802011-04-30 09:32:11 +02001094 output->image[i] = ec->base.create_image(ec->base.display,
1095 NULL,
1096 EGL_NATIVE_PIXMAP_KHR,
1097 output->bo[i], NULL);
David Herrmannd7c45512011-12-08 17:05:46 +01001098 if (!output->image[i])
1099 goto err_bufs;
Benjamin Franzke060cf802011-04-30 09:32:11 +02001100
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04001101 ec->base.image_target_renderbuffer_storage(GL_RENDERBUFFER,
1102 output->image[i]);
Benjamin Franzke060cf802011-04-30 09:32:11 +02001103 stride = gbm_bo_get_pitch(output->bo[i]);
1104 handle = gbm_bo_get_handle(output->bo[i]).u32;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001105
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001106 ret = drmModeAddFB(ec->drm.fd,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001107 output->base.current->width,
1108 output->base.current->height,
Kristian Høgsberg9c46ff12011-10-24 13:13:22 -04001109 24, 32, stride, handle, &output->fb_id[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001110 if (ret) {
1111 fprintf(stderr, "failed to add fb %d: %m\n", i);
David Herrmannd7c45512011-12-08 17:05:46 +01001112 goto err_bufs;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001113 }
1114 }
1115
1116 output->current = 0;
1117 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
1118 GL_COLOR_ATTACHMENT0,
1119 GL_RENDERBUFFER,
1120 output->rbo[output->current]);
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001121 ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001122 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001123 &output->connector_id, 1,
1124 &drm_mode->mode_info);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001125 if (ret) {
1126 fprintf(stderr, "failed to set mode: %m\n");
David Herrmannd7c45512011-12-08 17:05:46 +01001127 goto err_fb;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001128 }
1129
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001130 output->backlight = backlight_init(drm_device,
1131 connector->connector_type);
1132 if (output->backlight) {
1133 output->base.set_backlight = drm_set_backlight;
1134 output->base.backlight_current = drm_get_backlight(output);
1135 }
1136
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001137 weston_output_init(&output->base, &ec->base, x, y,
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04001138 connector->mmWidth, connector->mmHeight, 0);
1139
1140 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1141
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001142 output->scanout_buffer_destroy_listener.func =
1143 output_handle_scanout_buffer_destroy;
1144 output->pending_scanout_buffer_destroy_listener.func =
1145 output_handle_pending_scanout_buffer_destroy;
1146
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04001147 output->pending_fs_surf_fb_id = 0;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001148 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07001149 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001150 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001151 output->base.set_dpms = drm_set_dpms;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001152
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001153 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001154
David Herrmannd7c45512011-12-08 17:05:46 +01001155err_fb:
1156 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
1157 GL_COLOR_ATTACHMENT0,
1158 GL_RENDERBUFFER,
1159 0);
1160err_bufs:
1161 for (i = 0; i < 2; i++) {
1162 if (output->fb_id[i] != -1)
1163 drmModeRmFB(ec->drm.fd, output->fb_id[i]);
1164 if (output->image[i])
1165 ec->base.destroy_image(ec->base.display,
1166 output->image[i]);
1167 if (output->bo[i])
1168 gbm_bo_destroy(output->bo[i]);
1169 }
1170 glBindRenderbuffer(GL_RENDERBUFFER, 0);
1171 glDeleteRenderbuffers(2, output->rbo);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001172err_free:
1173 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1174 base.link) {
1175 wl_list_remove(&drm_mode->base.link);
1176 free(drm_mode);
1177 }
1178
1179 drmModeFreeCrtc(output->original_crtc);
1180 ec->crtc_allocator &= ~(1 << output->crtc_id);
1181 ec->connector_allocator &= ~(1 << output->connector_id);
1182
1183 free(output);
1184 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001185}
1186
Jesse Barnes58ef3792012-02-23 09:45:49 -05001187static void
1188create_sprites(struct drm_compositor *ec)
1189{
1190 struct drm_sprite *sprite;
1191 drmModePlaneRes *plane_res;
1192 drmModePlane *plane;
1193 int i;
1194
1195 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1196 if (!plane_res) {
1197 fprintf(stderr, "failed to get plane resources: %s\n",
1198 strerror(errno));
1199 return;
1200 }
1201
1202 for (i = 0; i < plane_res->count_planes; i++) {
1203 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1204 if (!plane)
1205 continue;
1206
1207 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1208 plane->count_formats));
1209 if (!sprite) {
1210 fprintf(stderr, "%s: out of memory\n",
1211 __func__);
1212 free(plane);
1213 continue;
1214 }
1215
1216 memset(sprite, 0, sizeof *sprite);
1217
1218 sprite->possible_crtcs = plane->possible_crtcs;
1219 sprite->plane_id = plane->plane_id;
1220 sprite->surface = NULL;
1221 sprite->pending_surface = NULL;
1222 sprite->fb_id = 0;
1223 sprite->pending_fb_id = 0;
1224 sprite->destroy_listener.func = sprite_handle_buffer_destroy;
1225 sprite->pending_destroy_listener.func =
1226 sprite_handle_pending_buffer_destroy;
1227 sprite->compositor = ec;
1228 sprite->count_formats = plane->count_formats;
1229 memcpy(sprite->formats, plane->formats,
1230 plane->count_formats);
1231 drmModeFreePlane(plane);
1232
1233 wl_list_insert(&ec->sprite_list, &sprite->link);
1234 }
1235
1236 free(plane_res->planes);
1237 free(plane_res);
1238}
1239
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001240static void
1241destroy_sprites(struct drm_compositor *compositor)
1242{
1243 struct drm_sprite *sprite, *next;
1244 struct drm_output *output;
1245
1246 output = container_of(compositor->base.output_list.next,
1247 struct drm_output, base.link);
1248
1249 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1250 drmModeSetPlane(compositor->drm.fd,
1251 sprite->plane_id,
1252 output->crtc_id, 0, 0,
1253 0, 0, 0, 0, 0, 0, 0, 0);
1254 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1255 free(sprite);
1256 }
1257}
Jesse Barnes58ef3792012-02-23 09:45:49 -05001258
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001259static int
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001260create_outputs(struct drm_compositor *ec, int option_connector,
1261 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001262{
1263 drmModeConnector *connector;
1264 drmModeRes *resources;
1265 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001266 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001267
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001268 resources = drmModeGetResources(ec->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001269 if (!resources) {
1270 fprintf(stderr, "drmModeGetResources failed\n");
1271 return -1;
1272 }
1273
Jesse Barnes58ef3792012-02-23 09:45:49 -05001274 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001275 if (!ec->crtcs) {
1276 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001277 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001278 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001279
1280 ec->num_crtcs = resources->count_crtcs;
1281 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1282
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001283 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001284 connector = drmModeGetConnector(ec->drm.fd,
1285 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001286 if (connector == NULL)
1287 continue;
1288
1289 if (connector->connection == DRM_MODE_CONNECTED &&
1290 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001291 connector->connector_id == option_connector)) {
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001292 if (create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001293 connector, x, y,
1294 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02001295 drmModeFreeConnector(connector);
1296 continue;
1297 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001298
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001299 x += container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001300 struct weston_output,
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001301 link)->current->width;
1302 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001303
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001304 drmModeFreeConnector(connector);
1305 }
1306
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001307 if (wl_list_empty(&ec->base.output_list)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001308 fprintf(stderr, "No currently active connector found.\n");
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001309 drmModeFreeResources(resources);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001310 return -1;
1311 }
1312
1313 drmModeFreeResources(resources);
1314
1315 return 0;
1316}
1317
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001318static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001319update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001320{
1321 drmModeConnector *connector;
1322 drmModeRes *resources;
1323 struct drm_output *output, *next;
1324 int x = 0, y = 0;
1325 int x_offset = 0, y_offset = 0;
1326 uint32_t connected = 0, disconnects = 0;
1327 int i;
1328
1329 resources = drmModeGetResources(ec->drm.fd);
1330 if (!resources) {
1331 fprintf(stderr, "drmModeGetResources failed\n");
1332 return;
1333 }
1334
1335 /* collect new connects */
1336 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001337 int connector_id = resources->connectors[i];
1338
1339 connector = drmModeGetConnector(ec->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01001340 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001341 continue;
1342
David Herrmann7551cff2011-12-08 17:05:43 +01001343 if (connector->connection != DRM_MODE_CONNECTED) {
1344 drmModeFreeConnector(connector);
1345 continue;
1346 }
1347
Benjamin Franzke117483d2011-08-30 11:38:26 +02001348 connected |= (1 << connector_id);
1349
1350 if (!(ec->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001351 struct weston_output *last =
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001352 container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001353 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001354
1355 /* XXX: not yet needed, we die with 0 outputs */
1356 if (!wl_list_empty(&ec->base.output_list))
Benjamin Franzke117483d2011-08-30 11:38:26 +02001357 x = last->x + last->current->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001358 else
1359 x = 0;
1360 y = 0;
1361 create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001362 connector, x, y,
1363 drm_device);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001364 printf("connector %d connected\n", connector_id);
1365
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001366 }
1367 drmModeFreeConnector(connector);
1368 }
1369 drmModeFreeResources(resources);
1370
1371 disconnects = ec->connector_allocator & ~connected;
1372 if (disconnects) {
1373 wl_list_for_each_safe(output, next, &ec->base.output_list,
1374 base.link) {
1375 if (x_offset != 0 || y_offset != 0) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001376 weston_output_move(&output->base,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001377 output->base.x - x_offset,
1378 output->base.y - y_offset);
1379 }
1380
1381 if (disconnects & (1 << output->connector_id)) {
1382 disconnects &= ~(1 << output->connector_id);
1383 printf("connector %d disconnected\n",
1384 output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001385 x_offset += output->base.current->width;
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001386 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001387 }
1388 }
1389 }
1390
1391 /* FIXME: handle zero outputs, without terminating */
1392 if (ec->connector_allocator == 0)
1393 wl_display_terminate(ec->base.wl_display);
1394}
1395
1396static int
David Herrmannd7488c22012-03-11 20:05:21 +01001397udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001398{
David Herrmannd7488c22012-03-11 20:05:21 +01001399 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01001400 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01001401
1402 sysnum = udev_device_get_sysnum(device);
1403 if (!sysnum || atoi(sysnum) != ec->drm.id)
1404 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001405
David Herrmann6ac52db2012-03-11 20:05:22 +01001406 val = udev_device_get_property_value(device, "HOTPLUG");
1407 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001408 return 0;
1409
David Herrmann6ac52db2012-03-11 20:05:22 +01001410 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001411}
1412
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001413static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001414udev_drm_event(int fd, uint32_t mask, void *data)
1415{
1416 struct drm_compositor *ec = data;
1417 struct udev_device *event;
1418
1419 event = udev_monitor_receive_device(ec->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001420
David Herrmannd7488c22012-03-11 20:05:21 +01001421 if (udev_event_is_hotplug(ec, event))
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001422 update_outputs(ec, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001423
1424 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001425
1426 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001427}
1428
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001429static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001430drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001431{
1432 struct drm_compositor *d = (struct drm_compositor *) ec;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001433 struct weston_input_device *input, *next;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001434
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001435 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001436 gbm_device_destroy(d->gbm);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001437 destroy_sprites(d);
Kristian Høgsberg62d27742012-01-18 12:38:33 -05001438 drmDropMaster(d->drm.fd);
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001439 tty_destroy(d->tty);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001440
Tiago Vignattic349e1d2011-12-18 23:52:18 +02001441 wl_list_for_each_safe(input, next, &ec->input_device_list, link)
1442 evdev_input_destroy(input);
1443
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001444 free(d);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001445}
1446
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001447static void
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001448drm_compositor_set_modes(struct drm_compositor *compositor)
1449{
1450 struct drm_output *output;
1451 struct drm_mode *drm_mode;
1452 int ret;
1453
1454 wl_list_for_each(output, &compositor->base.output_list, base.link) {
1455 drm_mode = (struct drm_mode *) output->base.current;
1456 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
1457 output->fb_id[output->current ^ 1], 0, 0,
1458 &output->connector_id, 1,
1459 &drm_mode->mode_info);
1460 if (ret < 0) {
1461 fprintf(stderr,
1462 "failed to set mode %dx%d for output at %d,%d: %m",
1463 drm_mode->base.width, drm_mode->base.height,
1464 output->base.x, output->base.y);
1465 }
1466 }
1467}
1468
1469static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001470vt_func(struct weston_compositor *compositor, int event)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001471{
1472 struct drm_compositor *ec = (struct drm_compositor *) compositor;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001473 struct weston_output *output;
1474 struct weston_input_device *input;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001475 struct drm_sprite *sprite;
1476 struct drm_output *drm_output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001477
1478 switch (event) {
1479 case TTY_ENTER_VT:
1480 compositor->focus = 1;
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001481 if (drmSetMaster(ec->drm.fd)) {
1482 fprintf(stderr, "failed to set master: %m\n");
1483 wl_display_terminate(compositor->wl_display);
1484 }
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001485 compositor->state = ec->prev_state;
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001486 drm_compositor_set_modes(ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001487 weston_compositor_damage_all(compositor);
Tiago Vignatti6e2d5f12011-12-19 00:32:48 +02001488 wl_list_for_each(input, &compositor->input_device_list, link)
1489 evdev_add_devices(ec->udev, input);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001490 break;
1491 case TTY_LEAVE_VT:
1492 compositor->focus = 0;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001493 ec->prev_state = compositor->state;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001494 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04001495
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001496 /* If we have a repaint scheduled (either from a
1497 * pending pageflip or the idle handler), make sure we
1498 * cancel that so we don't try to pageflip when we're
1499 * vt switched away. The SLEEPING state will prevent
1500 * further attemps at repainting. When we switch
1501 * back, we schedule a repaint, which will process
1502 * pending frame callbacks. */
1503
1504 wl_list_for_each(output, &ec->base.output_list, link) {
1505 output->repaint_needed = 0;
1506 drm_output_set_cursor(output, NULL);
1507 }
1508
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001509 drm_output = container_of(ec->base.output_list.next,
1510 struct drm_output, base.link);
1511
1512 wl_list_for_each(sprite, &ec->sprite_list, link)
1513 drmModeSetPlane(ec->drm.fd,
1514 sprite->plane_id,
1515 drm_output->crtc_id, 0, 0,
1516 0, 0, 0, 0, 0, 0, 0, 0);
1517
Tiago Vignatti6e2d5f12011-12-19 00:32:48 +02001518 wl_list_for_each(input, &compositor->input_device_list, link)
1519 evdev_remove_devices(input);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04001520
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001521 if (drmDropMaster(ec->drm.fd) < 0)
1522 fprintf(stderr, "failed to drop master: %m\n");
1523
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001524 break;
1525 };
1526}
1527
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001528static const char default_seat[] = "seat0";
1529
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001530static struct weston_compositor *
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001531drm_compositor_create(struct wl_display *display,
Tiago Vignattifaee8012011-09-01 15:58:17 -04001532 int connector, const char *seat, int tty)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001533{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001534 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001535 struct udev_enumerate *e;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001536 struct udev_list_entry *entry;
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001537 struct udev_device *device, *drm_device;
1538 const char *path, *device_seat;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001539 struct wl_event_loop *loop;
1540
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001541 ec = malloc(sizeof *ec);
1542 if (ec == NULL)
1543 return NULL;
1544
1545 memset(ec, 0, sizeof *ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001546 ec->udev = udev_new();
1547 if (ec->udev == NULL) {
1548 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001549 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001550 }
1551
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05001552 ec->base.wl_display = display;
1553 ec->tty = tty_create(&ec->base, vt_func, tty);
1554 if (!ec->tty) {
1555 fprintf(stderr, "failed to initialize tty\n");
1556 free(ec);
1557 return NULL;
1558 }
1559
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001560 e = udev_enumerate_new(ec->udev);
1561 udev_enumerate_add_match_subsystem(e, "drm");
Benjamin Franzkea764ee52011-10-07 08:23:22 +02001562 udev_enumerate_add_match_sysname(e, "card[0-9]*");
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001563
Benjamin Franzke117483d2011-08-30 11:38:26 +02001564 udev_enumerate_scan_devices(e);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001565 drm_device = NULL;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001566 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001567 path = udev_list_entry_get_name(entry);
1568 device = udev_device_new_from_syspath(ec->udev, path);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001569 device_seat =
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001570 udev_device_get_property_value(device, "ID_SEAT");
1571 if (!device_seat)
1572 device_seat = default_seat;
1573 if (strcmp(device_seat, seat) == 0) {
1574 drm_device = device;
1575 break;
1576 }
1577 udev_device_unref(device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001578 }
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001579
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001580 if (drm_device == NULL) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001581 fprintf(stderr, "no drm device found\n");
1582 return NULL;
1583 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001584
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001585 if (init_egl(ec, drm_device) < 0) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001586 fprintf(stderr, "failed to initialize egl\n");
1587 return NULL;
1588 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001589
1590 ec->base.destroy = drm_destroy;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001591
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001592 ec->base.focus = 1;
1593
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001594 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001595
Benjamin Franzke5b2cb6f2011-02-18 16:54:55 +01001596 glGenFramebuffers(1, &ec->base.fbo);
1597 glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo);
1598
Benjamin Franzked59eb1c2011-04-29 22:14:54 +02001599 /* Can't init base class until we have a current egl context */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001600 if (weston_compositor_init(&ec->base, display) < 0)
Benjamin Franzked59eb1c2011-04-29 22:14:54 +02001601 return NULL;
1602
Jesse Barnes58ef3792012-02-23 09:45:49 -05001603 wl_list_init(&ec->sprite_list);
1604 create_sprites(ec);
1605
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001606 if (create_outputs(ec, connector, drm_device) < 0) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001607 fprintf(stderr, "failed to create output for %s\n", path);
1608 return NULL;
1609 }
1610
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001611 udev_device_unref(drm_device);
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02001612 udev_enumerate_unref(e);
1613 path = NULL;
1614
Tiago Vignattice03ec32011-12-19 01:14:03 +02001615 evdev_input_create(&ec->base, ec->udev, seat);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001616
1617 loop = wl_display_get_event_loop(ec->base.wl_display);
1618 ec->drm_source =
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001619 wl_event_loop_add_fd(loop, ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001620 WL_EVENT_READABLE, on_drm_input, ec);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001621
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001622 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
1623 if (ec->udev_monitor == NULL) {
1624 fprintf(stderr, "failed to intialize udev monitor\n");
1625 return NULL;
1626 }
1627 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
1628 "drm", NULL);
1629 ec->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02001630 wl_event_loop_add_fd(loop,
1631 udev_monitor_get_fd(ec->udev_monitor),
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001632 WL_EVENT_READABLE, udev_drm_event, ec);
1633
1634 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
1635 fprintf(stderr, "failed to enable udev-monitor receiving\n");
1636 return NULL;
1637 }
1638
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001639 return &ec->base;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001640}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001641
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001642struct weston_compositor *
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001643backend_init(struct wl_display *display, char *options);
1644
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001645WL_EXPORT struct weston_compositor *
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001646backend_init(struct wl_display *display, char *options)
1647{
Kristian Høgsberg8050bd22011-05-04 15:08:04 -04001648 int connector = 0, i;
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001649 const char *seat;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001650 char *p, *value;
Kristian Høgsberg1201b752012-01-15 14:27:10 -05001651 int tty = 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001652
Tiago Vignattifaee8012011-09-01 15:58:17 -04001653 static char * const tokens[] = { "connector", "seat", "tty", NULL };
Benjamin Franzke117483d2011-08-30 11:38:26 +02001654
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001655 p = options;
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001656 seat = default_seat;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001657 while (i = getsubopt(&p, tokens, &value), i != -1) {
1658 switch (i) {
1659 case 0:
Kristian Høgsberg8050bd22011-05-04 15:08:04 -04001660 connector = strtol(value, NULL, 0);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001661 break;
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001662 case 1:
1663 seat = value;
1664 break;
Tiago Vignattifaee8012011-09-01 15:58:17 -04001665 case 2:
1666 tty = strtol(value, NULL, 0);
1667 break;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001668 }
1669 }
1670
Tiago Vignattifaee8012011-09-01 15:58:17 -04001671 return drm_compositor_create(display, connector, seat, tty);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001672}