blob: 4305cd5b771d66fa29cf883e6e8b34688683bc6d [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
Kristian Høgsberg7a011ea2012-03-26 11:48:55 -0400445 if (es->image == EGL_NO_IMAGE_KHR)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500446 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);
Rob Clark3c97b902012-03-11 19:48:43 -0500529 s->src_x = box->x1 << 16;
530 s->src_y = box->y1 << 16;
531 s->src_w = (box->x2 - box->x1) << 16;
532 s->src_h = (box->y2 - box->y1) << 16;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500533 pixman_region32_fini(&src_rect);
534
Jesse Barnes58ef3792012-02-23 09:45:49 -0500535 wl_list_insert(es->buffer->resource.destroy_listener_list.prev,
536 &s->pending_destroy_listener.link);
537 return 0;
538}
539
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500540static int
541drm_output_set_cursor(struct weston_output *output_base,
542 struct weston_input_device *eid);
543
544static void
545weston_output_set_cursor(struct weston_output *output,
546 struct weston_input_device *device,
547 pixman_region32_t *overlap)
548{
549 pixman_region32_t cursor_region;
550 int prior_was_hardware;
551
552 if (device->sprite == NULL)
553 return;
554
555 pixman_region32_init(&cursor_region);
556 pixman_region32_intersect(&cursor_region,
557 &device->sprite->transform.boundingbox,
558 &output->region);
559
560 if (!pixman_region32_not_empty(&cursor_region)) {
561 drm_output_set_cursor(output, NULL);
562 goto out;
563 }
564
565 prior_was_hardware = device->hw_cursor;
566 if (pixman_region32_not_empty(overlap) ||
567 drm_output_set_cursor(output, device) < 0) {
568 if (prior_was_hardware) {
569 weston_surface_damage(device->sprite);
570 drm_output_set_cursor(output, NULL);
571 }
572 device->hw_cursor = 0;
573 } else {
574 if (!prior_was_hardware)
575 weston_surface_damage_below(device->sprite);
576 pixman_region32_fini(&device->sprite->damage);
577 pixman_region32_init(&device->sprite->damage);
578 device->hw_cursor = 1;
579 }
580
581out:
582 pixman_region32_fini(&cursor_region);
583}
584
Jesse Barnes58ef3792012-02-23 09:45:49 -0500585static void
586drm_assign_planes(struct weston_output *output)
587{
588 struct weston_compositor *ec = output->compositor;
589 struct weston_surface *es;
590 pixman_region32_t overlap, surface_overlap;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500591 struct weston_input_device *device;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500592
593 /*
594 * Find a surface for each sprite in the output using some heuristics:
595 * 1) size
596 * 2) frequency of update
597 * 3) opacity (though some hw might support alpha blending)
598 * 4) clipping (this can be fixed with color keys)
599 *
600 * The idea is to save on blitting since this should save power.
601 * If we can get a large video surface on the sprite for example,
602 * the main display surface may not need to update at all, and
603 * the client buffer can be used directly for the sprite surface
604 * as we do for flipping full screen surfaces.
605 */
606 pixman_region32_init(&overlap);
607 wl_list_for_each(es, &ec->surface_list, link) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500608 /*
609 * FIXME: try to assign hw cursors here too, they're just
610 * special overlays
611 */
612 pixman_region32_init(&surface_overlap);
613 pixman_region32_intersect(&surface_overlap, &overlap,
614 &es->transform.boundingbox);
615
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500616 device = (struct weston_input_device *) ec->input_device;
617 if (es == device->sprite) {
618 weston_output_set_cursor(output, device,
619 &surface_overlap);
620
621 if (!device->hw_cursor)
622 pixman_region32_union(&overlap, &overlap,
623 &es->transform.boundingbox);
624 } else if (!drm_output_prepare_overlay_surface(output, es,
625 &surface_overlap)) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500626 pixman_region32_fini(&es->damage);
627 pixman_region32_init(&es->damage);
628 } else {
629 pixman_region32_union(&overlap, &overlap,
630 &es->transform.boundingbox);
631 }
632 pixman_region32_fini(&surface_overlap);
633 }
634 pixman_region32_fini(&overlap);
635
636 drm_disable_unused_sprites(output);
637}
638
639static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500640drm_output_set_cursor(struct weston_output *output_base,
641 struct weston_input_device *eid)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200642{
643 struct drm_output *output = (struct drm_output *) output_base;
644 struct drm_compositor *c =
645 (struct drm_compositor *) output->base.compositor;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200646 EGLint handle, stride;
647 int ret = -1;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200648 struct gbm_bo *bo;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200649
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400650 if (eid == NULL) {
651 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
652 return 0;
653 }
654
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200655 if (eid->sprite->image == EGL_NO_IMAGE_KHR)
656 goto out;
657
Pekka Paalanen60921e52012-01-25 15:55:43 +0200658 if (eid->sprite->geometry.width > 64 ||
659 eid->sprite->geometry.height > 64)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200660 goto out;
Benjamin Franzke117483d2011-08-30 11:38:26 +0200661
Benjamin Franzke060cf802011-04-30 09:32:11 +0200662 bo = gbm_bo_create_from_egl_image(c->gbm,
663 c->base.display,
664 eid->sprite->image, 64, 64,
665 GBM_BO_USE_CURSOR_64X64);
Kristian Høgsberg6916d9c2012-01-18 15:11:07 -0500666 /* Not suitable for hw cursor, fall back */
667 if (bo == NULL)
668 goto out;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200669
670 handle = gbm_bo_get_handle(bo).s32;
671 stride = gbm_bo_get_pitch(bo);
Benjamin Franzke060cf802011-04-30 09:32:11 +0200672 gbm_bo_destroy(bo);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200673
Kristian Høgsberg6916d9c2012-01-18 15:11:07 -0500674 /* gbm_bo_create_from_egl_image() didn't always validate the usage
675 * flags, and in that case we might end up with a bad stride. */
676 if (stride != 64 * 4)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200677 goto out;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200678
679 ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
680 if (ret) {
681 fprintf(stderr, "failed to set cursor: %s\n", strerror(-ret));
682 goto out;
683 }
684
685 ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200686 eid->sprite->geometry.x - output->base.x,
687 eid->sprite->geometry.y - output->base.y);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200688 if (ret) {
689 fprintf(stderr, "failed to move cursor: %s\n", strerror(-ret));
690 goto out;
691 }
692
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200693out:
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200694 if (ret)
695 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
696 return ret;
697}
698
Matt Roper361d2ad2011-08-29 13:52:23 -0700699static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500700drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700701{
702 struct drm_output *output = (struct drm_output *) output_base;
703 struct drm_compositor *c =
Benjamin Franzke117483d2011-08-30 11:38:26 +0200704 (struct drm_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700705 drmModeCrtcPtr origcrtc = output->original_crtc;
706 int i;
707
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200708 if (output->backlight)
709 backlight_destroy(output->backlight);
710
Matt Roper361d2ad2011-08-29 13:52:23 -0700711 /* Turn off hardware cursor */
Benjamin Franzke117483d2011-08-30 11:38:26 +0200712 drm_output_set_cursor(&output->base, NULL);
Matt Roper361d2ad2011-08-29 13:52:23 -0700713
714 /* Restore original CRTC state */
715 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +0200716 origcrtc->x, origcrtc->y,
717 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -0700718 drmModeFreeCrtc(origcrtc);
719
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200720 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
721 GL_COLOR_ATTACHMENT0,
722 GL_RENDERBUFFER,
723 0);
724
725 glBindRenderbuffer(GL_RENDERBUFFER, 0);
726 glDeleteRenderbuffers(2, output->rbo);
727
Matt Roper361d2ad2011-08-29 13:52:23 -0700728 /* Destroy output buffers */
729 for (i = 0; i < 2; i++) {
730 drmModeRmFB(c->drm.fd, output->fb_id[i]);
731 c->base.destroy_image(c->base.display, output->image[i]);
732 gbm_bo_destroy(output->bo[i]);
733 }
734
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200735 c->crtc_allocator &= ~(1 << output->crtc_id);
736 c->connector_allocator &= ~(1 << output->connector_id);
737
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500738 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200739 wl_list_remove(&output->base.link);
740
Matt Roper361d2ad2011-08-29 13:52:23 -0700741 free(output);
742}
743
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400744static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400745on_drm_input(int fd, uint32_t mask, void *data)
746{
747 drmEventContext evctx;
748
749 memset(&evctx, 0, sizeof evctx);
750 evctx.version = DRM_EVENT_CONTEXT_VERSION;
751 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500752 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400753 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400754
755 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400756}
757
758static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400759init_egl(struct drm_compositor *ec, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400760{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400761 EGLint major, minor;
David Herrmannd7488c22012-03-11 20:05:21 +0100762 const char *extensions, *filename, *sysnum;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400763 int fd;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400764 static const EGLint context_attribs[] = {
765 EGL_CONTEXT_CLIENT_VERSION, 2,
766 EGL_NONE
767 };
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400768
David Herrmannd7488c22012-03-11 20:05:21 +0100769 sysnum = udev_device_get_sysnum(device);
770 if (sysnum)
771 ec->drm.id = atoi(sysnum);
772 if (!sysnum || ec->drm.id < 0) {
773 fprintf(stderr, "cannot get device sysnum\n");
774 return -1;
775 }
776
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400777 filename = udev_device_get_devnode(device);
David Herrmann63ff7062011-11-05 18:46:01 +0100778 fd = open(filename, O_RDWR | O_CLOEXEC);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400779 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400780 /* Probably permissions error */
781 fprintf(stderr, "couldn't open %s, skipping\n",
782 udev_device_get_devnode(device));
783 return -1;
784 }
785
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100786 ec->drm.fd = fd;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200787 ec->gbm = gbm_create_device(ec->drm.fd);
788 ec->base.display = eglGetDisplay(ec->gbm);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400789 if (ec->base.display == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400790 fprintf(stderr, "failed to create display\n");
791 return -1;
792 }
793
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400794 if (!eglInitialize(ec->base.display, &major, &minor)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400795 fprintf(stderr, "failed to initialize display\n");
796 return -1;
797 }
798
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400799 extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS);
Ander Conselvan de Oliveiraef7c8d92011-11-01 16:37:41 +0200800 if (!strstr(extensions, "EGL_KHR_surfaceless_gles2")) {
801 fprintf(stderr, "EGL_KHR_surfaceless_gles2 not available\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400802 return -1;
803 }
804
Darxus55973f22010-11-22 21:24:39 -0500805 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
806 fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n");
807 return -1;
808 }
809
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400810 ec->base.context = eglCreateContext(ec->base.display, NULL,
811 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400812 if (ec->base.context == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400813 fprintf(stderr, "failed to create context\n");
814 return -1;
815 }
816
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400817 if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE,
818 EGL_NO_SURFACE, ec->base.context)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400819 fprintf(stderr, "failed to make context current\n");
820 return -1;
821 }
822
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400823 return 0;
824}
825
826static drmModeModeInfo builtin_1024x768 = {
827 63500, /* clock */
828 1024, 1072, 1176, 1328, 0,
829 768, 771, 775, 798, 0,
830 59920,
831 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
832 0,
833 "1024x768"
834};
835
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400836
837static int
838drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
839{
840 struct drm_mode *mode;
841
842 mode = malloc(sizeof *mode);
843 if (mode == NULL)
844 return -1;
845
846 mode->base.flags = 0;
847 mode->base.width = info->hdisplay;
848 mode->base.height = info->vdisplay;
849 mode->base.refresh = info->vrefresh;
850 mode->mode_info = *info;
851 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
852
853 return 0;
854}
855
856static int
857drm_subpixel_to_wayland(int drm_value)
858{
859 switch (drm_value) {
860 default:
861 case DRM_MODE_SUBPIXEL_UNKNOWN:
862 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
863 case DRM_MODE_SUBPIXEL_NONE:
864 return WL_OUTPUT_SUBPIXEL_NONE;
865 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
866 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
867 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
868 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
869 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
870 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
871 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
872 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
873 }
874}
875
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500876static void
877output_handle_scanout_buffer_destroy(struct wl_listener *listener,
878 struct wl_resource *resource,
879 uint32_t time)
880{
881 struct drm_output *output =
882 container_of(listener, struct drm_output,
883 scanout_buffer_destroy_listener);
884
885 output->scanout_buffer = NULL;
886
887 if (!output->pending_scanout_buffer)
888 weston_compositor_schedule_repaint(output->base.compositor);
889}
890
891static void
892output_handle_pending_scanout_buffer_destroy(struct wl_listener *listener,
893 struct wl_resource *resource,
894 uint32_t time)
895{
896 struct drm_output *output =
897 container_of(listener, struct drm_output,
898 pending_scanout_buffer_destroy_listener);
899
900 output->pending_scanout_buffer = NULL;
901
902 weston_compositor_schedule_repaint(output->base.compositor);
903}
904
Jesse Barnes58ef3792012-02-23 09:45:49 -0500905static void
906sprite_handle_buffer_destroy(struct wl_listener *listener,
907 struct wl_resource *resource,
908 uint32_t time)
909{
910 struct drm_sprite *sprite =
911 container_of(listener, struct drm_sprite,
912 destroy_listener);
913
914 sprite->surface = NULL;
915}
916
917static void
918sprite_handle_pending_buffer_destroy(struct wl_listener *listener,
919 struct wl_resource *resource,
920 uint32_t time)
921{
922 struct drm_sprite *sprite =
923 container_of(listener, struct drm_sprite,
924 pending_destroy_listener);
925
926 sprite->pending_surface = NULL;
927}
928
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300929/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200930static uint32_t
931drm_get_backlight(struct drm_output *output)
932{
933 long brightness, max_brightness, norm;
934
935 brightness = backlight_get_brightness(output->backlight);
936 max_brightness = backlight_get_max_brightness(output->backlight);
937
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300938 /* convert it on a scale of 0 to 255 */
939 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200940
941 return (uint32_t) norm;
942}
943
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300944/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200945static void
946drm_set_backlight(struct weston_output *output_base, uint32_t value)
947{
948 struct drm_output *output = (struct drm_output *) output_base;
949 long max_brightness, new_brightness;
950
951 if (!output->backlight)
952 return;
953
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300954 if (value < 0 || value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200955 return;
956
957 max_brightness = backlight_get_max_brightness(output->backlight);
958
959 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300960 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200961
962 backlight_set_brightness(output->backlight, new_brightness);
963}
964
965static drmModePropertyPtr
966drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
967{
968 drmModePropertyPtr props;
969 int i;
970
971 for (i = 0; i < connector->count_props; i++) {
972 props = drmModeGetProperty(fd, connector->props[i]);
973 if (!props)
974 continue;
975
976 if (!strcmp(props->name, name))
977 return props;
978
979 drmModeFreeProperty(props);
980 }
981
982 return NULL;
983}
984
985static void
986drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
987{
988 struct drm_output *output = (struct drm_output *) output_base;
989 struct weston_compositor *ec = output_base->compositor;
990 struct drm_compositor *c = (struct drm_compositor *) ec;
991 drmModeConnectorPtr connector;
992 drmModePropertyPtr prop;
993
994 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
995 if (!connector)
996 return;
997
998 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
999 if (!prop) {
1000 drmModeFreeConnector(connector);
1001 return;
1002 }
1003
1004 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1005 prop->prop_id, level);
1006 drmModeFreeProperty(prop);
1007 drmModeFreeConnector(connector);
1008}
1009
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001010static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001011create_output_for_connector(struct drm_compositor *ec,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001012 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001013 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001014 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001015{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001016 struct drm_output *output;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001017 struct drm_mode *drm_mode, *next;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001018 drmModeEncoder *encoder;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001019 int i, ret;
Benjamin Franzke060cf802011-04-30 09:32:11 +02001020 unsigned handle, stride;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001021
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001022 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001023 if (encoder == NULL) {
1024 fprintf(stderr, "No encoder for connector.\n");
1025 return -1;
1026 }
1027
1028 for (i = 0; i < resources->count_crtcs; i++) {
Marty Jack13d9db22011-02-09 19:01:42 -05001029 if (encoder->possible_crtcs & (1 << i) &&
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001030 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001031 break;
1032 }
1033 if (i == resources->count_crtcs) {
1034 fprintf(stderr, "No usable crtc for encoder.\n");
David Herrmanneb8bed52011-12-08 17:05:44 +01001035 drmModeFreeEncoder(encoder);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001036 return -1;
1037 }
1038
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001039 output = malloc(sizeof *output);
David Herrmanneb8bed52011-12-08 17:05:44 +01001040 if (output == NULL) {
1041 drmModeFreeEncoder(encoder);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001042 return -1;
David Herrmanneb8bed52011-12-08 17:05:44 +01001043 }
David Herrmannd7c45512011-12-08 17:05:46 +01001044 output->fb_id[0] = -1;
1045 output->fb_id[1] = -1;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001046
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001047 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001048 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1049 output->base.make = "unknown";
1050 output->base.model = "unknown";
1051 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001052
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001053 output->crtc_id = resources->crtcs[i];
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001054 ec->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001055 output->connector_id = connector->connector_id;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001056 ec->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001057
Matt Roper361d2ad2011-08-29 13:52:23 -07001058 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001059 drmModeFreeEncoder(encoder);
Matt Roper361d2ad2011-08-29 13:52:23 -07001060
David Herrmann0f0d54e2011-12-08 17:05:45 +01001061 for (i = 0; i < connector->count_modes; i++) {
1062 ret = drm_output_add_mode(output, &connector->modes[i]);
1063 if (ret)
1064 goto err_free;
1065 }
1066
1067 if (connector->count_modes == 0) {
1068 ret = drm_output_add_mode(output, &builtin_1024x768);
1069 if (ret)
1070 goto err_free;
1071 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001072
1073 drm_mode = container_of(output->base.mode_list.next,
1074 struct drm_mode, base.link);
1075 output->base.current = &drm_mode->base;
1076 drm_mode->base.flags =
1077 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1078
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001079 glGenRenderbuffers(2, output->rbo);
1080 for (i = 0; i < 2; i++) {
1081 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
1082
Benjamin Franzke060cf802011-04-30 09:32:11 +02001083 output->bo[i] =
1084 gbm_bo_create(ec->gbm,
1085 output->base.current->width,
1086 output->base.current->height,
1087 GBM_BO_FORMAT_XRGB8888,
Benjamin Franzke117483d2011-08-30 11:38:26 +02001088 GBM_BO_USE_SCANOUT |
1089 GBM_BO_USE_RENDERING);
David Herrmannd7c45512011-12-08 17:05:46 +01001090 if (!output->bo[i])
1091 goto err_bufs;
1092
Benjamin Franzke060cf802011-04-30 09:32:11 +02001093 output->image[i] = ec->base.create_image(ec->base.display,
1094 NULL,
1095 EGL_NATIVE_PIXMAP_KHR,
1096 output->bo[i], NULL);
David Herrmannd7c45512011-12-08 17:05:46 +01001097 if (!output->image[i])
1098 goto err_bufs;
Benjamin Franzke060cf802011-04-30 09:32:11 +02001099
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04001100 ec->base.image_target_renderbuffer_storage(GL_RENDERBUFFER,
1101 output->image[i]);
Benjamin Franzke060cf802011-04-30 09:32:11 +02001102 stride = gbm_bo_get_pitch(output->bo[i]);
1103 handle = gbm_bo_get_handle(output->bo[i]).u32;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001104
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001105 ret = drmModeAddFB(ec->drm.fd,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001106 output->base.current->width,
1107 output->base.current->height,
Kristian Høgsberg9c46ff12011-10-24 13:13:22 -04001108 24, 32, stride, handle, &output->fb_id[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001109 if (ret) {
1110 fprintf(stderr, "failed to add fb %d: %m\n", i);
David Herrmannd7c45512011-12-08 17:05:46 +01001111 goto err_bufs;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001112 }
1113 }
1114
1115 output->current = 0;
1116 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
1117 GL_COLOR_ATTACHMENT0,
1118 GL_RENDERBUFFER,
1119 output->rbo[output->current]);
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001120 ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001121 output->fb_id[output->current ^ 1], 0, 0,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001122 &output->connector_id, 1,
1123 &drm_mode->mode_info);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001124 if (ret) {
1125 fprintf(stderr, "failed to set mode: %m\n");
David Herrmannd7c45512011-12-08 17:05:46 +01001126 goto err_fb;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001127 }
1128
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001129 output->backlight = backlight_init(drm_device,
1130 connector->connector_type);
1131 if (output->backlight) {
1132 output->base.set_backlight = drm_set_backlight;
1133 output->base.backlight_current = drm_get_backlight(output);
1134 }
1135
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001136 weston_output_init(&output->base, &ec->base, x, y,
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04001137 connector->mmWidth, connector->mmHeight, 0);
1138
1139 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1140
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001141 output->scanout_buffer_destroy_listener.func =
1142 output_handle_scanout_buffer_destroy;
1143 output->pending_scanout_buffer_destroy_listener.func =
1144 output_handle_pending_scanout_buffer_destroy;
1145
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04001146 output->pending_fs_surf_fb_id = 0;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001147 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07001148 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001149 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001150 output->base.set_dpms = drm_set_dpms;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001151
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001152 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001153
David Herrmannd7c45512011-12-08 17:05:46 +01001154err_fb:
1155 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
1156 GL_COLOR_ATTACHMENT0,
1157 GL_RENDERBUFFER,
1158 0);
1159err_bufs:
1160 for (i = 0; i < 2; i++) {
1161 if (output->fb_id[i] != -1)
1162 drmModeRmFB(ec->drm.fd, output->fb_id[i]);
1163 if (output->image[i])
1164 ec->base.destroy_image(ec->base.display,
1165 output->image[i]);
1166 if (output->bo[i])
1167 gbm_bo_destroy(output->bo[i]);
1168 }
1169 glBindRenderbuffer(GL_RENDERBUFFER, 0);
1170 glDeleteRenderbuffers(2, output->rbo);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001171err_free:
1172 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1173 base.link) {
1174 wl_list_remove(&drm_mode->base.link);
1175 free(drm_mode);
1176 }
1177
1178 drmModeFreeCrtc(output->original_crtc);
1179 ec->crtc_allocator &= ~(1 << output->crtc_id);
1180 ec->connector_allocator &= ~(1 << output->connector_id);
1181
1182 free(output);
1183 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001184}
1185
Jesse Barnes58ef3792012-02-23 09:45:49 -05001186static void
1187create_sprites(struct drm_compositor *ec)
1188{
1189 struct drm_sprite *sprite;
1190 drmModePlaneRes *plane_res;
1191 drmModePlane *plane;
1192 int i;
1193
1194 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1195 if (!plane_res) {
1196 fprintf(stderr, "failed to get plane resources: %s\n",
1197 strerror(errno));
1198 return;
1199 }
1200
1201 for (i = 0; i < plane_res->count_planes; i++) {
1202 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1203 if (!plane)
1204 continue;
1205
1206 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1207 plane->count_formats));
1208 if (!sprite) {
1209 fprintf(stderr, "%s: out of memory\n",
1210 __func__);
1211 free(plane);
1212 continue;
1213 }
1214
1215 memset(sprite, 0, sizeof *sprite);
1216
1217 sprite->possible_crtcs = plane->possible_crtcs;
1218 sprite->plane_id = plane->plane_id;
1219 sprite->surface = NULL;
1220 sprite->pending_surface = NULL;
1221 sprite->fb_id = 0;
1222 sprite->pending_fb_id = 0;
1223 sprite->destroy_listener.func = sprite_handle_buffer_destroy;
1224 sprite->pending_destroy_listener.func =
1225 sprite_handle_pending_buffer_destroy;
1226 sprite->compositor = ec;
1227 sprite->count_formats = plane->count_formats;
1228 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05001229 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05001230 drmModeFreePlane(plane);
1231
1232 wl_list_insert(&ec->sprite_list, &sprite->link);
1233 }
1234
1235 free(plane_res->planes);
1236 free(plane_res);
1237}
1238
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001239static void
1240destroy_sprites(struct drm_compositor *compositor)
1241{
1242 struct drm_sprite *sprite, *next;
1243 struct drm_output *output;
1244
1245 output = container_of(compositor->base.output_list.next,
1246 struct drm_output, base.link);
1247
1248 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1249 drmModeSetPlane(compositor->drm.fd,
1250 sprite->plane_id,
1251 output->crtc_id, 0, 0,
1252 0, 0, 0, 0, 0, 0, 0, 0);
1253 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1254 free(sprite);
1255 }
1256}
Jesse Barnes58ef3792012-02-23 09:45:49 -05001257
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001258static int
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001259create_outputs(struct drm_compositor *ec, int option_connector,
1260 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001261{
1262 drmModeConnector *connector;
1263 drmModeRes *resources;
1264 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001265 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001266
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001267 resources = drmModeGetResources(ec->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001268 if (!resources) {
1269 fprintf(stderr, "drmModeGetResources failed\n");
1270 return -1;
1271 }
1272
Jesse Barnes58ef3792012-02-23 09:45:49 -05001273 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001274 if (!ec->crtcs) {
1275 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001276 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001277 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001278
1279 ec->num_crtcs = resources->count_crtcs;
1280 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1281
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001282 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001283 connector = drmModeGetConnector(ec->drm.fd,
1284 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001285 if (connector == NULL)
1286 continue;
1287
1288 if (connector->connection == DRM_MODE_CONNECTED &&
1289 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001290 connector->connector_id == option_connector)) {
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001291 if (create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001292 connector, x, y,
1293 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02001294 drmModeFreeConnector(connector);
1295 continue;
1296 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001297
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001298 x += container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001299 struct weston_output,
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001300 link)->current->width;
1301 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001302
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001303 drmModeFreeConnector(connector);
1304 }
1305
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001306 if (wl_list_empty(&ec->base.output_list)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001307 fprintf(stderr, "No currently active connector found.\n");
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001308 drmModeFreeResources(resources);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001309 return -1;
1310 }
1311
1312 drmModeFreeResources(resources);
1313
1314 return 0;
1315}
1316
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001317static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001318update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001319{
1320 drmModeConnector *connector;
1321 drmModeRes *resources;
1322 struct drm_output *output, *next;
1323 int x = 0, y = 0;
1324 int x_offset = 0, y_offset = 0;
1325 uint32_t connected = 0, disconnects = 0;
1326 int i;
1327
1328 resources = drmModeGetResources(ec->drm.fd);
1329 if (!resources) {
1330 fprintf(stderr, "drmModeGetResources failed\n");
1331 return;
1332 }
1333
1334 /* collect new connects */
1335 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001336 int connector_id = resources->connectors[i];
1337
1338 connector = drmModeGetConnector(ec->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01001339 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001340 continue;
1341
David Herrmann7551cff2011-12-08 17:05:43 +01001342 if (connector->connection != DRM_MODE_CONNECTED) {
1343 drmModeFreeConnector(connector);
1344 continue;
1345 }
1346
Benjamin Franzke117483d2011-08-30 11:38:26 +02001347 connected |= (1 << connector_id);
1348
1349 if (!(ec->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001350 struct weston_output *last =
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001351 container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001352 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001353
1354 /* XXX: not yet needed, we die with 0 outputs */
1355 if (!wl_list_empty(&ec->base.output_list))
Benjamin Franzke117483d2011-08-30 11:38:26 +02001356 x = last->x + last->current->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001357 else
1358 x = 0;
1359 y = 0;
1360 create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001361 connector, x, y,
1362 drm_device);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001363 printf("connector %d connected\n", connector_id);
1364
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001365 }
1366 drmModeFreeConnector(connector);
1367 }
1368 drmModeFreeResources(resources);
1369
1370 disconnects = ec->connector_allocator & ~connected;
1371 if (disconnects) {
1372 wl_list_for_each_safe(output, next, &ec->base.output_list,
1373 base.link) {
1374 if (x_offset != 0 || y_offset != 0) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001375 weston_output_move(&output->base,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001376 output->base.x - x_offset,
1377 output->base.y - y_offset);
1378 }
1379
1380 if (disconnects & (1 << output->connector_id)) {
1381 disconnects &= ~(1 << output->connector_id);
1382 printf("connector %d disconnected\n",
1383 output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001384 x_offset += output->base.current->width;
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001385 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001386 }
1387 }
1388 }
1389
1390 /* FIXME: handle zero outputs, without terminating */
1391 if (ec->connector_allocator == 0)
1392 wl_display_terminate(ec->base.wl_display);
1393}
1394
1395static int
David Herrmannd7488c22012-03-11 20:05:21 +01001396udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001397{
David Herrmannd7488c22012-03-11 20:05:21 +01001398 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01001399 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01001400
1401 sysnum = udev_device_get_sysnum(device);
1402 if (!sysnum || atoi(sysnum) != ec->drm.id)
1403 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001404
David Herrmann6ac52db2012-03-11 20:05:22 +01001405 val = udev_device_get_property_value(device, "HOTPLUG");
1406 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001407 return 0;
1408
David Herrmann6ac52db2012-03-11 20:05:22 +01001409 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001410}
1411
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001412static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001413udev_drm_event(int fd, uint32_t mask, void *data)
1414{
1415 struct drm_compositor *ec = data;
1416 struct udev_device *event;
1417
1418 event = udev_monitor_receive_device(ec->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001419
David Herrmannd7488c22012-03-11 20:05:21 +01001420 if (udev_event_is_hotplug(ec, event))
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001421 update_outputs(ec, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001422
1423 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001424
1425 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001426}
1427
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001428static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001429drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001430{
1431 struct drm_compositor *d = (struct drm_compositor *) ec;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001432 struct weston_input_device *input, *next;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001433
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001434 weston_compositor_shutdown(ec);
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
Tiago Vignattic349e1d2011-12-18 23:52:18 +02001440 wl_list_for_each_safe(input, next, &ec->input_device_list, link)
1441 evdev_input_destroy(input);
1442
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001443 free(d);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001444}
1445
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001446static void
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001447drm_compositor_set_modes(struct drm_compositor *compositor)
1448{
1449 struct drm_output *output;
1450 struct drm_mode *drm_mode;
1451 int ret;
1452
1453 wl_list_for_each(output, &compositor->base.output_list, base.link) {
1454 drm_mode = (struct drm_mode *) output->base.current;
1455 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
1456 output->fb_id[output->current ^ 1], 0, 0,
1457 &output->connector_id, 1,
1458 &drm_mode->mode_info);
1459 if (ret < 0) {
1460 fprintf(stderr,
1461 "failed to set mode %dx%d for output at %d,%d: %m",
1462 drm_mode->base.width, drm_mode->base.height,
1463 output->base.x, output->base.y);
1464 }
1465 }
1466}
1467
1468static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001469vt_func(struct weston_compositor *compositor, int event)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001470{
1471 struct drm_compositor *ec = (struct drm_compositor *) compositor;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001472 struct weston_output *output;
1473 struct weston_input_device *input;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001474 struct drm_sprite *sprite;
1475 struct drm_output *drm_output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001476
1477 switch (event) {
1478 case TTY_ENTER_VT:
1479 compositor->focus = 1;
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001480 if (drmSetMaster(ec->drm.fd)) {
1481 fprintf(stderr, "failed to set master: %m\n");
1482 wl_display_terminate(compositor->wl_display);
1483 }
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001484 compositor->state = ec->prev_state;
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001485 drm_compositor_set_modes(ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001486 weston_compositor_damage_all(compositor);
Tiago Vignatti6e2d5f12011-12-19 00:32:48 +02001487 wl_list_for_each(input, &compositor->input_device_list, link)
1488 evdev_add_devices(ec->udev, input);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001489 break;
1490 case TTY_LEAVE_VT:
1491 compositor->focus = 0;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001492 ec->prev_state = compositor->state;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001493 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04001494
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001495 /* If we have a repaint scheduled (either from a
1496 * pending pageflip or the idle handler), make sure we
1497 * cancel that so we don't try to pageflip when we're
1498 * vt switched away. The SLEEPING state will prevent
1499 * further attemps at repainting. When we switch
1500 * back, we schedule a repaint, which will process
1501 * pending frame callbacks. */
1502
1503 wl_list_for_each(output, &ec->base.output_list, link) {
1504 output->repaint_needed = 0;
1505 drm_output_set_cursor(output, NULL);
1506 }
1507
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001508 drm_output = container_of(ec->base.output_list.next,
1509 struct drm_output, base.link);
1510
1511 wl_list_for_each(sprite, &ec->sprite_list, link)
1512 drmModeSetPlane(ec->drm.fd,
1513 sprite->plane_id,
1514 drm_output->crtc_id, 0, 0,
1515 0, 0, 0, 0, 0, 0, 0, 0);
1516
Tiago Vignatti6e2d5f12011-12-19 00:32:48 +02001517 wl_list_for_each(input, &compositor->input_device_list, link)
1518 evdev_remove_devices(input);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04001519
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001520 if (drmDropMaster(ec->drm.fd) < 0)
1521 fprintf(stderr, "failed to drop master: %m\n");
1522
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001523 break;
1524 };
1525}
1526
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001527static const char default_seat[] = "seat0";
1528
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001529static struct weston_compositor *
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001530drm_compositor_create(struct wl_display *display,
Tiago Vignattifaee8012011-09-01 15:58:17 -04001531 int connector, const char *seat, int tty)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001532{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001533 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001534 struct udev_enumerate *e;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001535 struct udev_list_entry *entry;
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001536 struct udev_device *device, *drm_device;
1537 const char *path, *device_seat;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001538 struct wl_event_loop *loop;
1539
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001540 ec = malloc(sizeof *ec);
1541 if (ec == NULL)
1542 return NULL;
1543
1544 memset(ec, 0, sizeof *ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001545 ec->udev = udev_new();
1546 if (ec->udev == NULL) {
1547 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001548 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001549 }
1550
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05001551 ec->base.wl_display = display;
1552 ec->tty = tty_create(&ec->base, vt_func, tty);
1553 if (!ec->tty) {
1554 fprintf(stderr, "failed to initialize tty\n");
1555 free(ec);
1556 return NULL;
1557 }
1558
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001559 e = udev_enumerate_new(ec->udev);
1560 udev_enumerate_add_match_subsystem(e, "drm");
Benjamin Franzkea764ee52011-10-07 08:23:22 +02001561 udev_enumerate_add_match_sysname(e, "card[0-9]*");
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001562
Benjamin Franzke117483d2011-08-30 11:38:26 +02001563 udev_enumerate_scan_devices(e);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001564 drm_device = NULL;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001565 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001566 path = udev_list_entry_get_name(entry);
1567 device = udev_device_new_from_syspath(ec->udev, path);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001568 device_seat =
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001569 udev_device_get_property_value(device, "ID_SEAT");
1570 if (!device_seat)
1571 device_seat = default_seat;
1572 if (strcmp(device_seat, seat) == 0) {
1573 drm_device = device;
1574 break;
1575 }
1576 udev_device_unref(device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001577 }
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001578
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001579 if (drm_device == NULL) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001580 fprintf(stderr, "no drm device found\n");
1581 return NULL;
1582 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001583
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001584 if (init_egl(ec, drm_device) < 0) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001585 fprintf(stderr, "failed to initialize egl\n");
1586 return NULL;
1587 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001588
1589 ec->base.destroy = drm_destroy;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001590
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001591 ec->base.focus = 1;
1592
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001593 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001594
Benjamin Franzke5b2cb6f2011-02-18 16:54:55 +01001595 glGenFramebuffers(1, &ec->base.fbo);
1596 glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo);
1597
Benjamin Franzked59eb1c2011-04-29 22:14:54 +02001598 /* Can't init base class until we have a current egl context */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001599 if (weston_compositor_init(&ec->base, display) < 0)
Benjamin Franzked59eb1c2011-04-29 22:14:54 +02001600 return NULL;
1601
Jesse Barnes58ef3792012-02-23 09:45:49 -05001602 wl_list_init(&ec->sprite_list);
1603 create_sprites(ec);
1604
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001605 if (create_outputs(ec, connector, drm_device) < 0) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001606 fprintf(stderr, "failed to create output for %s\n", path);
1607 return NULL;
1608 }
1609
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001610 udev_device_unref(drm_device);
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02001611 udev_enumerate_unref(e);
1612 path = NULL;
1613
Tiago Vignattice03ec32011-12-19 01:14:03 +02001614 evdev_input_create(&ec->base, ec->udev, seat);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001615
1616 loop = wl_display_get_event_loop(ec->base.wl_display);
1617 ec->drm_source =
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001618 wl_event_loop_add_fd(loop, ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001619 WL_EVENT_READABLE, on_drm_input, ec);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001620
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001621 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
1622 if (ec->udev_monitor == NULL) {
1623 fprintf(stderr, "failed to intialize udev monitor\n");
1624 return NULL;
1625 }
1626 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
1627 "drm", NULL);
1628 ec->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02001629 wl_event_loop_add_fd(loop,
1630 udev_monitor_get_fd(ec->udev_monitor),
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001631 WL_EVENT_READABLE, udev_drm_event, ec);
1632
1633 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
1634 fprintf(stderr, "failed to enable udev-monitor receiving\n");
1635 return NULL;
1636 }
1637
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001638 return &ec->base;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001639}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001640
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001641WL_EXPORT struct weston_compositor *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001642backend_init(struct wl_display *display, int argc, char *argv[])
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001643{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001644 int connector = 0, tty = 0;
1645 const char *seat = default_seat;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001646
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001647 const struct weston_option drm_options[] = {
1648 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
1649 { WESTON_OPTION_STRING, "seat", 0, &seat },
1650 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
1651 };
Benjamin Franzke117483d2011-08-30 11:38:26 +02001652
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001653 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001654
Tiago Vignattifaee8012011-09-01 15:58:17 -04001655 return drm_compositor_create(display, connector, seat, tty);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001656}