blob: fa4fec57ee1d4944db573f8efaba8b861640df16 [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;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +020080
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -040081 struct gbm_surface *surface;
82 EGLSurface egl_surface;
83 uint32_t current_fb_id;
84 uint32_t next_fb_id;
85 struct gbm_bo *current_bo;
86 struct gbm_bo *next_bo;
87
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;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500161
162 es = container_of(c->base.surface_list.next,
163 struct weston_surface, link);
164
Kristian Høgsberg101cb652012-02-17 10:45:16 -0500165 /* Need to verify output->region contained in surface opaque
166 * region. Or maybe just that format doesn't have alpha. */
167 return -1;
168
169 if (es->geometry.x != output->base.x ||
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200170 es->geometry.y != output->base.y ||
Pekka Paalanen60921e52012-01-25 15:55:43 +0200171 es->geometry.width != output->base.current->width ||
172 es->geometry.height != output->base.current->height ||
Pekka Paalanenf1f5b362012-01-25 14:33:33 +0200173 es->transform.enabled ||
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500174 es->image == EGL_NO_IMAGE_KHR)
175 return -1;
176
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400177 output->next_bo =
178 gbm_bo_create_from_egl_image(c->gbm,
179 c->base.display, es->image,
180 es->geometry.width,
181 es->geometry.height,
182 GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500183
184 /* assert output->pending_scanout_buffer == NULL */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500185 output->pending_scanout_buffer = es->buffer;
186 output->pending_scanout_buffer->busy_count++;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500187
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500188 wl_list_insert(output->pending_scanout_buffer->resource.destroy_listener_list.prev,
189 &output->pending_scanout_buffer_destroy_listener.link);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500190
191 pixman_region32_fini(&es->damage);
192 pixman_region32_init(&es->damage);
193
194 return 0;
195}
196
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500197static void
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400198drm_output_render(struct drm_output *output, pixman_region32_t *damage)
199{
200 struct drm_compositor *compositor =
201 (struct drm_compositor *) output->base.compositor;
202 struct weston_surface *surface;
203
204 if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
205 output->egl_surface, compositor->base.context)) {
206 fprintf(stderr, "failed to make current\n");
207 return;
208 }
209
210 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
211 weston_surface_draw(surface, &output->base, damage);
212
213 eglSwapBuffers(compositor->base.display, output->egl_surface);
214 output->next_bo = gbm_surface_lock_front_buffer(output->surface);
215 if (!output->next_bo) {
216 fprintf(stderr, "failed to lock front buffer: %m\n");
217 return;
218 }
219}
220
221static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500222drm_output_repaint(struct weston_output *output_base,
223 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100224{
225 struct drm_output *output = (struct drm_output *) output_base;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500226 struct drm_compositor *compositor =
227 (struct drm_compositor *) output->base.compositor;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500228 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400229 struct drm_mode *mode;
230 uint32_t stride, handle;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500231 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100232
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500233 drm_output_prepare_scanout_surface(output);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400234 if (!output->next_bo)
235 drm_output_render(output, damage);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500236
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400237 stride = gbm_bo_get_pitch(output->next_bo);
238 handle = gbm_bo_get_handle(output->next_bo).u32;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400239
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400240 /* Destroy and set to NULL now so we don't try to
241 * gbm_surface_release_buffer() a client buffer in the
242 * page_flip_handler. */
243 if (output->pending_scanout_buffer) {
244 gbm_bo_destroy(output->next_bo);
245 output->next_bo = NULL;
246 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400247
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400248 ret = drmModeAddFB(compositor->drm.fd,
249 output->base.current->width,
250 output->base.current->height,
251 24, 32, stride, handle, &output->next_fb_id);
252 if (ret) {
253 fprintf(stderr, "failed to create kms fb: %m\n");
254 gbm_surface_release_buffer(output->surface, output->next_bo);
255 output->next_bo = NULL;
256 return;
257 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100258
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400259 mode = container_of(output->base.current, struct drm_mode, base);
260 if (output->current_fb_id == 0) {
261 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
262 output->next_fb_id, 0, 0,
263 &output->connector_id, 1,
264 &mode->mode_info);
265 if (ret) {
266 fprintf(stderr, "set mode failed: %m\n");
267 return;
268 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200269 }
270
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500271 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400272 output->next_fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500273 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
274 fprintf(stderr, "queueing pageflip failed: %m\n");
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500275 return;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500276 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100277
Jesse Barnes58ef3792012-02-23 09:45:49 -0500278 /*
279 * Now, update all the sprite surfaces
280 */
281 wl_list_for_each(s, &compositor->sprite_list, link) {
282 uint32_t flags = 0;
283 drmVBlank vbl = {
284 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
285 .request.sequence = 1,
286 };
287
288 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
289 continue;
290
291 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
292 output->crtc_id, s->pending_fb_id, flags,
293 s->dest_x, s->dest_y,
294 s->dest_w, s->dest_h,
295 s->src_x, s->src_y,
296 s->src_w, s->src_h);
297 if (ret)
298 fprintf(stderr, "setplane failed: %d: %s\n",
299 ret, strerror(errno));
300
301 /*
302 * Queue a vblank signal so we know when the surface
303 * becomes active on the display or has been replaced.
304 */
305 vbl.request.signal = (unsigned long)s;
306 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
307 if (ret) {
308 fprintf(stderr, "vblank event request failed: %d: %s\n",
309 ret, strerror(errno));
310 }
311 }
312
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500313 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400314}
315
316static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500317vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
318 void *data)
319{
320 struct drm_sprite *s = (struct drm_sprite *)data;
321 struct drm_compositor *c = s->compositor;
322
323 if (s->surface) {
324 weston_buffer_post_release(s->surface->buffer);
325 wl_list_remove(&s->destroy_listener.link);
326 s->surface = NULL;
327 drmModeRmFB(c->drm.fd, s->fb_id);
328 s->fb_id = 0;
329 }
330
331 if (s->pending_surface) {
332 wl_list_remove(&s->pending_destroy_listener.link);
333 wl_list_insert(s->pending_surface->buffer->resource.destroy_listener_list.prev,
334 &s->destroy_listener.link);
335 s->surface = s->pending_surface;
336 s->pending_surface = NULL;
337 s->fb_id = s->pending_fb_id;
338 s->pending_fb_id = 0;
339 }
340}
341
342static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400343page_flip_handler(int fd, unsigned int frame,
344 unsigned int sec, unsigned int usec, void *data)
345{
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200346 struct drm_output *output = (struct drm_output *) data;
347 struct drm_compositor *c =
348 (struct drm_compositor *) output->base.compositor;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400349 uint32_t msecs;
350
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400351 if (output->current_fb_id)
352 drmModeRmFB(c->drm.fd, output->current_fb_id);
353 output->current_fb_id = output->next_fb_id;
354 output->next_fb_id = 0;
355
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500356 if (output->scanout_buffer) {
357 weston_buffer_post_release(output->scanout_buffer);
358 wl_list_remove(&output->scanout_buffer_destroy_listener.link);
359 output->scanout_buffer = NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400360 } else if (output->current_bo) {
361 gbm_surface_release_buffer(output->surface,
362 output->current_bo);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200363 }
364
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400365 output->current_bo = output->next_bo;
366 output->next_bo = NULL;
367
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500368 if (output->pending_scanout_buffer) {
369 output->scanout_buffer = output->pending_scanout_buffer;
370 wl_list_remove(&output->pending_scanout_buffer_destroy_listener.link);
371 wl_list_insert(output->scanout_buffer->resource.destroy_listener_list.prev,
372 &output->scanout_buffer_destroy_listener.link);
373 output->pending_scanout_buffer = NULL;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200374 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100375 msecs = sec * 1000 + usec / 1000;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500376 weston_output_finish_frame(&output->base, msecs);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200377}
378
379static int
Jesse Barnes58ef3792012-02-23 09:45:49 -0500380drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
381{
382 int i;
383
384 for (i = 0; i < s->count_formats; i++)
385 if (s->formats[i] == format)
386 return 1;
387
388 return 0;
389}
390
391static int
392drm_surface_transform_supported(struct weston_surface *es)
393{
394 if (es->transform.enabled)
395 return 0;
396
397 return 1;
398}
399
400static int
401drm_surface_overlap_supported(struct weston_output *output_base,
402 pixman_region32_t *overlap)
403{
404 /* We could potentially use a color key here if the surface left
405 * to display has rectangular regions
406 */
407 if (pixman_region32_not_empty(overlap))
408 return 0;
409 return 1;
410}
411
412static void
413drm_disable_unused_sprites(struct weston_output *output_base)
414{
415 struct weston_compositor *ec = output_base->compositor;
416 struct drm_compositor *c =(struct drm_compositor *) ec;
417 struct drm_output *output = (struct drm_output *) output_base;
418 struct drm_sprite *s;
419 int ret;
420
421 wl_list_for_each(s, &c->sprite_list, link) {
422 if (s->pending_fb_id)
423 continue;
424
425 ret = drmModeSetPlane(c->drm.fd, s->plane_id,
426 output->crtc_id, 0, 0,
427 0, 0, 0, 0, 0, 0, 0, 0);
428 if (ret)
429 fprintf(stderr,
430 "failed to disable plane: %d: %s\n",
431 ret, strerror(errno));
432 drmModeRmFB(c->drm.fd, s->fb_id);
433 s->surface = NULL;
434 s->pending_surface = NULL;
435 s->fb_id = 0;
436 s->pending_fb_id = 0;
437 }
438}
439
440/*
441 * This function must take care to damage any previously assigned surface
442 * if the sprite ends up binding to a different surface than in the
443 * previous frame.
444 */
445static int
446drm_output_prepare_overlay_surface(struct weston_output *output_base,
447 struct weston_surface *es,
448 pixman_region32_t *overlap)
449{
450 struct weston_compositor *ec = output_base->compositor;
451 struct drm_compositor *c =(struct drm_compositor *) ec;
452 struct drm_sprite *s;
453 int found = 0;
454 EGLint handle, stride;
455 struct gbm_bo *bo;
456 uint32_t fb_id = 0;
457 uint32_t handles[4], pitches[4], offsets[4];
458 int ret = 0;
459 pixman_region32_t dest_rect, src_rect;
460 pixman_box32_t *box;
461 uint32_t format;
462
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500463 if (c->sprites_are_broken)
464 return -1;
465
Jesse Barnes58ef3792012-02-23 09:45:49 -0500466 if (surface_is_primary(ec, es))
467 return -1;
468
Kristian Høgsberg7a011ea2012-03-26 11:48:55 -0400469 if (es->image == EGL_NO_IMAGE_KHR)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500470 return -1;
471
472 if (!drm_surface_transform_supported(es))
473 return -1;
474
475 if (!drm_surface_overlap_supported(output_base, overlap))
476 return -1;
477
478 wl_list_for_each(s, &c->sprite_list, link) {
479 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
480 continue;
481
482 if (!s->pending_fb_id) {
483 found = 1;
484 break;
485 }
486 }
487
488 /* No sprites available */
489 if (!found)
490 return -1;
491
492 bo = gbm_bo_create_from_egl_image(c->gbm, c->base.display, es->image,
493 es->geometry.width, es->geometry.height,
494 GBM_BO_USE_SCANOUT);
495 format = gbm_bo_get_format(bo);
496 handle = gbm_bo_get_handle(bo).s32;
497 stride = gbm_bo_get_pitch(bo);
498
499 gbm_bo_destroy(bo);
500
501 if (!drm_surface_format_supported(s, format))
502 return -1;
503
504 if (!handle)
505 return -1;
506
507 handles[0] = handle;
508 pitches[0] = stride;
509 offsets[0] = 0;
510
511 ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height,
512 format, handles, pitches, offsets,
513 &fb_id, 0);
514 if (ret) {
515 fprintf(stderr, "addfb2 failed: %d\n", ret);
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500516 c->sprites_are_broken = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500517 return -1;
518 }
519
520 if (s->surface && s->surface != es) {
521 struct weston_surface *old_surf = s->surface;
522 pixman_region32_fini(&old_surf->damage);
523 pixman_region32_init_rect(&old_surf->damage,
524 old_surf->geometry.x, old_surf->geometry.y,
525 old_surf->geometry.width, old_surf->geometry.height);
526 }
527
528 s->pending_fb_id = fb_id;
529 s->pending_surface = es;
530 es->buffer->busy_count++;
531
532 /*
533 * Calculate the source & dest rects properly based on actual
534 * postion (note the caller has called weston_surface_update_transform()
535 * for us already).
536 */
537 pixman_region32_init(&dest_rect);
538 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
539 &output_base->region);
540 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
541 box = pixman_region32_extents(&dest_rect);
542 s->dest_x = box->x1;
543 s->dest_y = box->y1;
544 s->dest_w = box->x2 - box->x1;
545 s->dest_h = box->y2 - box->y1;
546 pixman_region32_fini(&dest_rect);
547
548 pixman_region32_init(&src_rect);
549 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
550 &output_base->region);
551 pixman_region32_translate(&src_rect, -es->geometry.x, -es->geometry.y);
552 box = pixman_region32_extents(&src_rect);
Rob Clark3c97b902012-03-11 19:48:43 -0500553 s->src_x = box->x1 << 16;
554 s->src_y = box->y1 << 16;
555 s->src_w = (box->x2 - box->x1) << 16;
556 s->src_h = (box->y2 - box->y1) << 16;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500557 pixman_region32_fini(&src_rect);
558
Jesse Barnes58ef3792012-02-23 09:45:49 -0500559 wl_list_insert(es->buffer->resource.destroy_listener_list.prev,
560 &s->pending_destroy_listener.link);
561 return 0;
562}
563
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500564static int
565drm_output_set_cursor(struct weston_output *output_base,
566 struct weston_input_device *eid);
567
568static void
569weston_output_set_cursor(struct weston_output *output,
570 struct weston_input_device *device,
571 pixman_region32_t *overlap)
572{
573 pixman_region32_t cursor_region;
574 int prior_was_hardware;
575
576 if (device->sprite == NULL)
577 return;
578
579 pixman_region32_init(&cursor_region);
580 pixman_region32_intersect(&cursor_region,
581 &device->sprite->transform.boundingbox,
582 &output->region);
583
584 if (!pixman_region32_not_empty(&cursor_region)) {
585 drm_output_set_cursor(output, NULL);
586 goto out;
587 }
588
589 prior_was_hardware = device->hw_cursor;
590 if (pixman_region32_not_empty(overlap) ||
591 drm_output_set_cursor(output, device) < 0) {
592 if (prior_was_hardware) {
593 weston_surface_damage(device->sprite);
594 drm_output_set_cursor(output, NULL);
595 }
596 device->hw_cursor = 0;
597 } else {
598 if (!prior_was_hardware)
599 weston_surface_damage_below(device->sprite);
600 pixman_region32_fini(&device->sprite->damage);
601 pixman_region32_init(&device->sprite->damage);
602 device->hw_cursor = 1;
603 }
604
605out:
606 pixman_region32_fini(&cursor_region);
607}
608
Jesse Barnes58ef3792012-02-23 09:45:49 -0500609static void
610drm_assign_planes(struct weston_output *output)
611{
612 struct weston_compositor *ec = output->compositor;
613 struct weston_surface *es;
614 pixman_region32_t overlap, surface_overlap;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500615 struct weston_input_device *device;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500616
617 /*
618 * Find a surface for each sprite in the output using some heuristics:
619 * 1) size
620 * 2) frequency of update
621 * 3) opacity (though some hw might support alpha blending)
622 * 4) clipping (this can be fixed with color keys)
623 *
624 * The idea is to save on blitting since this should save power.
625 * If we can get a large video surface on the sprite for example,
626 * the main display surface may not need to update at all, and
627 * the client buffer can be used directly for the sprite surface
628 * as we do for flipping full screen surfaces.
629 */
630 pixman_region32_init(&overlap);
631 wl_list_for_each(es, &ec->surface_list, link) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500632 /*
633 * FIXME: try to assign hw cursors here too, they're just
634 * special overlays
635 */
636 pixman_region32_init(&surface_overlap);
637 pixman_region32_intersect(&surface_overlap, &overlap,
638 &es->transform.boundingbox);
639
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500640 device = (struct weston_input_device *) ec->input_device;
641 if (es == device->sprite) {
642 weston_output_set_cursor(output, device,
643 &surface_overlap);
644
645 if (!device->hw_cursor)
646 pixman_region32_union(&overlap, &overlap,
647 &es->transform.boundingbox);
648 } else if (!drm_output_prepare_overlay_surface(output, es,
649 &surface_overlap)) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500650 pixman_region32_fini(&es->damage);
651 pixman_region32_init(&es->damage);
652 } else {
653 pixman_region32_union(&overlap, &overlap,
654 &es->transform.boundingbox);
655 }
656 pixman_region32_fini(&surface_overlap);
657 }
658 pixman_region32_fini(&overlap);
659
660 drm_disable_unused_sprites(output);
661}
662
663static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500664drm_output_set_cursor(struct weston_output *output_base,
665 struct weston_input_device *eid)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200666{
667 struct drm_output *output = (struct drm_output *) output_base;
668 struct drm_compositor *c =
669 (struct drm_compositor *) output->base.compositor;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200670 EGLint handle, stride;
671 int ret = -1;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200672 struct gbm_bo *bo;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200673
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400674 if (eid == NULL) {
675 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
676 return 0;
677 }
678
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200679 if (eid->sprite->image == EGL_NO_IMAGE_KHR)
680 goto out;
681
Pekka Paalanen60921e52012-01-25 15:55:43 +0200682 if (eid->sprite->geometry.width > 64 ||
683 eid->sprite->geometry.height > 64)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200684 goto out;
Benjamin Franzke117483d2011-08-30 11:38:26 +0200685
Benjamin Franzke060cf802011-04-30 09:32:11 +0200686 bo = gbm_bo_create_from_egl_image(c->gbm,
687 c->base.display,
688 eid->sprite->image, 64, 64,
689 GBM_BO_USE_CURSOR_64X64);
Kristian Høgsberg6916d9c2012-01-18 15:11:07 -0500690 /* Not suitable for hw cursor, fall back */
691 if (bo == NULL)
692 goto out;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200693
694 handle = gbm_bo_get_handle(bo).s32;
695 stride = gbm_bo_get_pitch(bo);
Benjamin Franzke060cf802011-04-30 09:32:11 +0200696 gbm_bo_destroy(bo);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200697
Kristian Høgsberg6916d9c2012-01-18 15:11:07 -0500698 /* gbm_bo_create_from_egl_image() didn't always validate the usage
699 * flags, and in that case we might end up with a bad stride. */
700 if (stride != 64 * 4)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200701 goto out;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200702
703 ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
704 if (ret) {
705 fprintf(stderr, "failed to set cursor: %s\n", strerror(-ret));
706 goto out;
707 }
708
709 ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200710 eid->sprite->geometry.x - output->base.x,
711 eid->sprite->geometry.y - output->base.y);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200712 if (ret) {
713 fprintf(stderr, "failed to move cursor: %s\n", strerror(-ret));
714 goto out;
715 }
716
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200717out:
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200718 if (ret)
719 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
720 return ret;
721}
722
Matt Roper361d2ad2011-08-29 13:52:23 -0700723static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500724drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700725{
726 struct drm_output *output = (struct drm_output *) output_base;
727 struct drm_compositor *c =
Benjamin Franzke117483d2011-08-30 11:38:26 +0200728 (struct drm_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700729 drmModeCrtcPtr origcrtc = output->original_crtc;
Matt Roper361d2ad2011-08-29 13:52:23 -0700730
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200731 if (output->backlight)
732 backlight_destroy(output->backlight);
733
Matt Roper361d2ad2011-08-29 13:52:23 -0700734 /* Turn off hardware cursor */
Benjamin Franzke117483d2011-08-30 11:38:26 +0200735 drm_output_set_cursor(&output->base, NULL);
Matt Roper361d2ad2011-08-29 13:52:23 -0700736
737 /* Restore original CRTC state */
738 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +0200739 origcrtc->x, origcrtc->y,
740 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -0700741 drmModeFreeCrtc(origcrtc);
742
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200743 c->crtc_allocator &= ~(1 << output->crtc_id);
744 c->connector_allocator &= ~(1 << output->connector_id);
745
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400746 eglDestroySurface(c->base.display, output->egl_surface);
747 gbm_surface_destroy(output->surface);
748
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500749 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200750 wl_list_remove(&output->base.link);
751
Matt Roper361d2ad2011-08-29 13:52:23 -0700752 free(output);
753}
754
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400755static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400756on_drm_input(int fd, uint32_t mask, void *data)
757{
758 drmEventContext evctx;
759
760 memset(&evctx, 0, sizeof evctx);
761 evctx.version = DRM_EVENT_CONTEXT_VERSION;
762 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500763 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400764 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400765
766 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400767}
768
769static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400770init_egl(struct drm_compositor *ec, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400771{
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400772 EGLint major, minor, n;
David Herrmannd7488c22012-03-11 20:05:21 +0100773 const char *extensions, *filename, *sysnum;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400774 int fd;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400775 static const EGLint context_attribs[] = {
776 EGL_CONTEXT_CLIENT_VERSION, 2,
777 EGL_NONE
778 };
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400779
David Herrmannd7488c22012-03-11 20:05:21 +0100780 sysnum = udev_device_get_sysnum(device);
781 if (sysnum)
782 ec->drm.id = atoi(sysnum);
783 if (!sysnum || ec->drm.id < 0) {
784 fprintf(stderr, "cannot get device sysnum\n");
785 return -1;
786 }
787
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400788 static const EGLint config_attribs[] = {
789 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
790 EGL_RED_SIZE, 1,
791 EGL_GREEN_SIZE, 1,
792 EGL_BLUE_SIZE, 1,
793 EGL_ALPHA_SIZE, 0,
794 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
795 EGL_NONE
796 };
797
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400798 filename = udev_device_get_devnode(device);
David Herrmann63ff7062011-11-05 18:46:01 +0100799 fd = open(filename, O_RDWR | O_CLOEXEC);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400800 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400801 /* Probably permissions error */
802 fprintf(stderr, "couldn't open %s, skipping\n",
803 udev_device_get_devnode(device));
804 return -1;
805 }
806
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100807 ec->drm.fd = fd;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200808 ec->gbm = gbm_create_device(ec->drm.fd);
809 ec->base.display = eglGetDisplay(ec->gbm);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400810 if (ec->base.display == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400811 fprintf(stderr, "failed to create display\n");
812 return -1;
813 }
814
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400815 if (!eglInitialize(ec->base.display, &major, &minor)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400816 fprintf(stderr, "failed to initialize display\n");
817 return -1;
818 }
819
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400820 extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS);
Ander Conselvan de Oliveiraef7c8d92011-11-01 16:37:41 +0200821 if (!strstr(extensions, "EGL_KHR_surfaceless_gles2")) {
822 fprintf(stderr, "EGL_KHR_surfaceless_gles2 not available\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400823 return -1;
824 }
825
Darxus55973f22010-11-22 21:24:39 -0500826 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
827 fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n");
828 return -1;
829 }
830
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400831 if (!eglChooseConfig(ec->base.display, config_attribs,
832 &ec->base.config, 1, &n) || n != 1) {
833 fprintf(stderr, "failed to choose config: %d\n", n);
834 return -1;
835 }
836
837 ec->base.context = eglCreateContext(ec->base.display, ec->base.config,
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400838 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400839 if (ec->base.context == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400840 fprintf(stderr, "failed to create context\n");
841 return -1;
842 }
843
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400844 if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE,
845 EGL_NO_SURFACE, ec->base.context)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400846 fprintf(stderr, "failed to make context current\n");
847 return -1;
848 }
849
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400850 return 0;
851}
852
853static drmModeModeInfo builtin_1024x768 = {
854 63500, /* clock */
855 1024, 1072, 1176, 1328, 0,
856 768, 771, 775, 798, 0,
857 59920,
858 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
859 0,
860 "1024x768"
861};
862
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400863
864static int
865drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
866{
867 struct drm_mode *mode;
868
869 mode = malloc(sizeof *mode);
870 if (mode == NULL)
871 return -1;
872
873 mode->base.flags = 0;
874 mode->base.width = info->hdisplay;
875 mode->base.height = info->vdisplay;
876 mode->base.refresh = info->vrefresh;
877 mode->mode_info = *info;
878 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
879
880 return 0;
881}
882
883static int
884drm_subpixel_to_wayland(int drm_value)
885{
886 switch (drm_value) {
887 default:
888 case DRM_MODE_SUBPIXEL_UNKNOWN:
889 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
890 case DRM_MODE_SUBPIXEL_NONE:
891 return WL_OUTPUT_SUBPIXEL_NONE;
892 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
893 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
894 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
895 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
896 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
897 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
898 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
899 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
900 }
901}
902
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500903static void
904output_handle_scanout_buffer_destroy(struct wl_listener *listener,
905 struct wl_resource *resource,
906 uint32_t time)
907{
908 struct drm_output *output =
909 container_of(listener, struct drm_output,
910 scanout_buffer_destroy_listener);
911
912 output->scanout_buffer = NULL;
913
914 if (!output->pending_scanout_buffer)
915 weston_compositor_schedule_repaint(output->base.compositor);
916}
917
918static void
919output_handle_pending_scanout_buffer_destroy(struct wl_listener *listener,
920 struct wl_resource *resource,
921 uint32_t time)
922{
923 struct drm_output *output =
924 container_of(listener, struct drm_output,
925 pending_scanout_buffer_destroy_listener);
926
927 output->pending_scanout_buffer = NULL;
928
929 weston_compositor_schedule_repaint(output->base.compositor);
930}
931
Jesse Barnes58ef3792012-02-23 09:45:49 -0500932static void
933sprite_handle_buffer_destroy(struct wl_listener *listener,
934 struct wl_resource *resource,
935 uint32_t time)
936{
937 struct drm_sprite *sprite =
938 container_of(listener, struct drm_sprite,
939 destroy_listener);
940
941 sprite->surface = NULL;
942}
943
944static void
945sprite_handle_pending_buffer_destroy(struct wl_listener *listener,
946 struct wl_resource *resource,
947 uint32_t time)
948{
949 struct drm_sprite *sprite =
950 container_of(listener, struct drm_sprite,
951 pending_destroy_listener);
952
953 sprite->pending_surface = NULL;
954}
955
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300956/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200957static uint32_t
958drm_get_backlight(struct drm_output *output)
959{
960 long brightness, max_brightness, norm;
961
962 brightness = backlight_get_brightness(output->backlight);
963 max_brightness = backlight_get_max_brightness(output->backlight);
964
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300965 /* convert it on a scale of 0 to 255 */
966 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200967
968 return (uint32_t) norm;
969}
970
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300971/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200972static void
973drm_set_backlight(struct weston_output *output_base, uint32_t value)
974{
975 struct drm_output *output = (struct drm_output *) output_base;
976 long max_brightness, new_brightness;
977
978 if (!output->backlight)
979 return;
980
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300981 if (value < 0 || value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200982 return;
983
984 max_brightness = backlight_get_max_brightness(output->backlight);
985
986 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300987 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200988
989 backlight_set_brightness(output->backlight, new_brightness);
990}
991
992static drmModePropertyPtr
993drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
994{
995 drmModePropertyPtr props;
996 int i;
997
998 for (i = 0; i < connector->count_props; i++) {
999 props = drmModeGetProperty(fd, connector->props[i]);
1000 if (!props)
1001 continue;
1002
1003 if (!strcmp(props->name, name))
1004 return props;
1005
1006 drmModeFreeProperty(props);
1007 }
1008
1009 return NULL;
1010}
1011
1012static void
1013drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1014{
1015 struct drm_output *output = (struct drm_output *) output_base;
1016 struct weston_compositor *ec = output_base->compositor;
1017 struct drm_compositor *c = (struct drm_compositor *) ec;
1018 drmModeConnectorPtr connector;
1019 drmModePropertyPtr prop;
1020
1021 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1022 if (!connector)
1023 return;
1024
1025 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1026 if (!prop) {
1027 drmModeFreeConnector(connector);
1028 return;
1029 }
1030
1031 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1032 prop->prop_id, level);
1033 drmModeFreeProperty(prop);
1034 drmModeFreeConnector(connector);
1035}
1036
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001037static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001038create_output_for_connector(struct drm_compositor *ec,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001039 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001040 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001041 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001042{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001043 struct drm_output *output;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001044 struct drm_mode *drm_mode, *next;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001045 drmModeEncoder *encoder;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001046 int i, ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001047
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001048 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001049 if (encoder == NULL) {
1050 fprintf(stderr, "No encoder for connector.\n");
1051 return -1;
1052 }
1053
1054 for (i = 0; i < resources->count_crtcs; i++) {
Marty Jack13d9db22011-02-09 19:01:42 -05001055 if (encoder->possible_crtcs & (1 << i) &&
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001056 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001057 break;
1058 }
1059 if (i == resources->count_crtcs) {
1060 fprintf(stderr, "No usable crtc for encoder.\n");
David Herrmanneb8bed52011-12-08 17:05:44 +01001061 drmModeFreeEncoder(encoder);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001062 return -1;
1063 }
1064
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001065 output = malloc(sizeof *output);
David Herrmanneb8bed52011-12-08 17:05:44 +01001066 if (output == NULL) {
1067 drmModeFreeEncoder(encoder);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001068 return -1;
David Herrmanneb8bed52011-12-08 17:05:44 +01001069 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001070
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001071 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001072 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1073 output->base.make = "unknown";
1074 output->base.model = "unknown";
1075 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001076
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001077 output->crtc_id = resources->crtcs[i];
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001078 ec->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001079 output->connector_id = connector->connector_id;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001080 ec->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001081
Matt Roper361d2ad2011-08-29 13:52:23 -07001082 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001083 drmModeFreeEncoder(encoder);
Matt Roper361d2ad2011-08-29 13:52:23 -07001084
David Herrmann0f0d54e2011-12-08 17:05:45 +01001085 for (i = 0; i < connector->count_modes; i++) {
1086 ret = drm_output_add_mode(output, &connector->modes[i]);
1087 if (ret)
1088 goto err_free;
1089 }
1090
1091 if (connector->count_modes == 0) {
1092 ret = drm_output_add_mode(output, &builtin_1024x768);
1093 if (ret)
1094 goto err_free;
1095 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001096
1097 drm_mode = container_of(output->base.mode_list.next,
1098 struct drm_mode, base.link);
1099 output->base.current = &drm_mode->base;
1100 drm_mode->base.flags =
1101 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1102
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001103 output->surface = gbm_surface_create(ec->gbm,
1104 output->base.current->width,
1105 output->base.current->height,
1106 GBM_FORMAT_XRGB8888,
1107 GBM_BO_USE_SCANOUT |
1108 GBM_BO_USE_RENDERING);
1109 if (!output->surface) {
1110 fprintf(stderr, "failed to create gbm surface\n");
1111 goto err_free;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001112 }
1113
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001114 output->egl_surface =
1115 eglCreateWindowSurface(ec->base.display, ec->base.config,
1116 output->surface, NULL);
1117 if (output->egl_surface == EGL_NO_SURFACE) {
1118 fprintf(stderr, "failed to create egl surface\n");
1119 goto err_surface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001120 }
1121
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001122 output->backlight = backlight_init(drm_device,
1123 connector->connector_type);
1124 if (output->backlight) {
1125 output->base.set_backlight = drm_set_backlight;
1126 output->base.backlight_current = drm_get_backlight(output);
1127 }
1128
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001129 weston_output_init(&output->base, &ec->base, x, y,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001130 connector->mmWidth, connector->mmHeight,
1131 WL_OUTPUT_FLIPPED);
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04001132
1133 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1134
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001135 output->scanout_buffer_destroy_listener.func =
1136 output_handle_scanout_buffer_destroy;
1137 output->pending_scanout_buffer_destroy_listener.func =
1138 output_handle_pending_scanout_buffer_destroy;
1139
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001140 output->next_fb_id = 0;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001141 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07001142 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001143 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001144 output->base.set_dpms = drm_set_dpms;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001145
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001146 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001147
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001148 eglDestroySurface(ec->base.display, output->egl_surface);
1149err_surface:
1150 gbm_surface_destroy(output->surface);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001151err_free:
1152 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1153 base.link) {
1154 wl_list_remove(&drm_mode->base.link);
1155 free(drm_mode);
1156 }
1157
1158 drmModeFreeCrtc(output->original_crtc);
1159 ec->crtc_allocator &= ~(1 << output->crtc_id);
1160 ec->connector_allocator &= ~(1 << output->connector_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001161 free(output);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001162
David Herrmann0f0d54e2011-12-08 17:05:45 +01001163 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001164}
1165
Jesse Barnes58ef3792012-02-23 09:45:49 -05001166static void
1167create_sprites(struct drm_compositor *ec)
1168{
1169 struct drm_sprite *sprite;
1170 drmModePlaneRes *plane_res;
1171 drmModePlane *plane;
1172 int i;
1173
1174 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1175 if (!plane_res) {
1176 fprintf(stderr, "failed to get plane resources: %s\n",
1177 strerror(errno));
1178 return;
1179 }
1180
1181 for (i = 0; i < plane_res->count_planes; i++) {
1182 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1183 if (!plane)
1184 continue;
1185
1186 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1187 plane->count_formats));
1188 if (!sprite) {
1189 fprintf(stderr, "%s: out of memory\n",
1190 __func__);
1191 free(plane);
1192 continue;
1193 }
1194
1195 memset(sprite, 0, sizeof *sprite);
1196
1197 sprite->possible_crtcs = plane->possible_crtcs;
1198 sprite->plane_id = plane->plane_id;
1199 sprite->surface = NULL;
1200 sprite->pending_surface = NULL;
1201 sprite->fb_id = 0;
1202 sprite->pending_fb_id = 0;
1203 sprite->destroy_listener.func = sprite_handle_buffer_destroy;
1204 sprite->pending_destroy_listener.func =
1205 sprite_handle_pending_buffer_destroy;
1206 sprite->compositor = ec;
1207 sprite->count_formats = plane->count_formats;
1208 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05001209 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05001210 drmModeFreePlane(plane);
1211
1212 wl_list_insert(&ec->sprite_list, &sprite->link);
1213 }
1214
1215 free(plane_res->planes);
1216 free(plane_res);
1217}
1218
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001219static void
1220destroy_sprites(struct drm_compositor *compositor)
1221{
1222 struct drm_sprite *sprite, *next;
1223 struct drm_output *output;
1224
1225 output = container_of(compositor->base.output_list.next,
1226 struct drm_output, base.link);
1227
1228 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1229 drmModeSetPlane(compositor->drm.fd,
1230 sprite->plane_id,
1231 output->crtc_id, 0, 0,
1232 0, 0, 0, 0, 0, 0, 0, 0);
1233 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1234 free(sprite);
1235 }
1236}
Jesse Barnes58ef3792012-02-23 09:45:49 -05001237
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001238static int
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001239create_outputs(struct drm_compositor *ec, int option_connector,
1240 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001241{
1242 drmModeConnector *connector;
1243 drmModeRes *resources;
1244 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001245 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001246
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001247 resources = drmModeGetResources(ec->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001248 if (!resources) {
1249 fprintf(stderr, "drmModeGetResources failed\n");
1250 return -1;
1251 }
1252
Jesse Barnes58ef3792012-02-23 09:45:49 -05001253 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001254 if (!ec->crtcs) {
1255 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001256 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001257 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001258
1259 ec->num_crtcs = resources->count_crtcs;
1260 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1261
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001262 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001263 connector = drmModeGetConnector(ec->drm.fd,
1264 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001265 if (connector == NULL)
1266 continue;
1267
1268 if (connector->connection == DRM_MODE_CONNECTED &&
1269 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001270 connector->connector_id == option_connector)) {
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001271 if (create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001272 connector, x, y,
1273 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02001274 drmModeFreeConnector(connector);
1275 continue;
1276 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001277
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001278 x += container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001279 struct weston_output,
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001280 link)->current->width;
1281 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001282
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001283 drmModeFreeConnector(connector);
1284 }
1285
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001286 if (wl_list_empty(&ec->base.output_list)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001287 fprintf(stderr, "No currently active connector found.\n");
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001288 drmModeFreeResources(resources);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001289 return -1;
1290 }
1291
1292 drmModeFreeResources(resources);
1293
1294 return 0;
1295}
1296
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001297static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001298update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001299{
1300 drmModeConnector *connector;
1301 drmModeRes *resources;
1302 struct drm_output *output, *next;
1303 int x = 0, y = 0;
1304 int x_offset = 0, y_offset = 0;
1305 uint32_t connected = 0, disconnects = 0;
1306 int i;
1307
1308 resources = drmModeGetResources(ec->drm.fd);
1309 if (!resources) {
1310 fprintf(stderr, "drmModeGetResources failed\n");
1311 return;
1312 }
1313
1314 /* collect new connects */
1315 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001316 int connector_id = resources->connectors[i];
1317
1318 connector = drmModeGetConnector(ec->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01001319 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001320 continue;
1321
David Herrmann7551cff2011-12-08 17:05:43 +01001322 if (connector->connection != DRM_MODE_CONNECTED) {
1323 drmModeFreeConnector(connector);
1324 continue;
1325 }
1326
Benjamin Franzke117483d2011-08-30 11:38:26 +02001327 connected |= (1 << connector_id);
1328
1329 if (!(ec->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001330 struct weston_output *last =
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001331 container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001332 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001333
1334 /* XXX: not yet needed, we die with 0 outputs */
1335 if (!wl_list_empty(&ec->base.output_list))
Benjamin Franzke117483d2011-08-30 11:38:26 +02001336 x = last->x + last->current->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001337 else
1338 x = 0;
1339 y = 0;
1340 create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001341 connector, x, y,
1342 drm_device);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001343 printf("connector %d connected\n", connector_id);
1344
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001345 }
1346 drmModeFreeConnector(connector);
1347 }
1348 drmModeFreeResources(resources);
1349
1350 disconnects = ec->connector_allocator & ~connected;
1351 if (disconnects) {
1352 wl_list_for_each_safe(output, next, &ec->base.output_list,
1353 base.link) {
1354 if (x_offset != 0 || y_offset != 0) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001355 weston_output_move(&output->base,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001356 output->base.x - x_offset,
1357 output->base.y - y_offset);
1358 }
1359
1360 if (disconnects & (1 << output->connector_id)) {
1361 disconnects &= ~(1 << output->connector_id);
1362 printf("connector %d disconnected\n",
1363 output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001364 x_offset += output->base.current->width;
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001365 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001366 }
1367 }
1368 }
1369
1370 /* FIXME: handle zero outputs, without terminating */
1371 if (ec->connector_allocator == 0)
1372 wl_display_terminate(ec->base.wl_display);
1373}
1374
1375static int
David Herrmannd7488c22012-03-11 20:05:21 +01001376udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001377{
David Herrmannd7488c22012-03-11 20:05:21 +01001378 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01001379 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01001380
1381 sysnum = udev_device_get_sysnum(device);
1382 if (!sysnum || atoi(sysnum) != ec->drm.id)
1383 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001384
David Herrmann6ac52db2012-03-11 20:05:22 +01001385 val = udev_device_get_property_value(device, "HOTPLUG");
1386 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001387 return 0;
1388
David Herrmann6ac52db2012-03-11 20:05:22 +01001389 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001390}
1391
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001392static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001393udev_drm_event(int fd, uint32_t mask, void *data)
1394{
1395 struct drm_compositor *ec = data;
1396 struct udev_device *event;
1397
1398 event = udev_monitor_receive_device(ec->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001399
David Herrmannd7488c22012-03-11 20:05:21 +01001400 if (udev_event_is_hotplug(ec, event))
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001401 update_outputs(ec, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001402
1403 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001404
1405 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001406}
1407
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001408static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001409drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001410{
1411 struct drm_compositor *d = (struct drm_compositor *) ec;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001412 struct weston_input_device *input, *next;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001413
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001414 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001415 gbm_device_destroy(d->gbm);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001416 destroy_sprites(d);
Kristian Høgsberg62d27742012-01-18 12:38:33 -05001417 drmDropMaster(d->drm.fd);
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001418 tty_destroy(d->tty);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001419
Tiago Vignattic349e1d2011-12-18 23:52:18 +02001420 wl_list_for_each_safe(input, next, &ec->input_device_list, link)
1421 evdev_input_destroy(input);
1422
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001423 free(d);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001424}
1425
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001426static void
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001427drm_compositor_set_modes(struct drm_compositor *compositor)
1428{
1429 struct drm_output *output;
1430 struct drm_mode *drm_mode;
1431 int ret;
1432
1433 wl_list_for_each(output, &compositor->base.output_list, base.link) {
1434 drm_mode = (struct drm_mode *) output->base.current;
1435 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001436 output->current_fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001437 &output->connector_id, 1,
1438 &drm_mode->mode_info);
1439 if (ret < 0) {
1440 fprintf(stderr,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001441 "failed to set mode %dx%d for output at %d,%d: %m\n",
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001442 drm_mode->base.width, drm_mode->base.height,
1443 output->base.x, output->base.y);
1444 }
1445 }
1446}
1447
1448static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001449vt_func(struct weston_compositor *compositor, int event)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001450{
1451 struct drm_compositor *ec = (struct drm_compositor *) compositor;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001452 struct weston_output *output;
1453 struct weston_input_device *input;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001454 struct drm_sprite *sprite;
1455 struct drm_output *drm_output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001456
1457 switch (event) {
1458 case TTY_ENTER_VT:
1459 compositor->focus = 1;
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001460 if (drmSetMaster(ec->drm.fd)) {
1461 fprintf(stderr, "failed to set master: %m\n");
1462 wl_display_terminate(compositor->wl_display);
1463 }
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001464 compositor->state = ec->prev_state;
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001465 drm_compositor_set_modes(ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001466 weston_compositor_damage_all(compositor);
Tiago Vignatti6e2d5f12011-12-19 00:32:48 +02001467 wl_list_for_each(input, &compositor->input_device_list, link)
1468 evdev_add_devices(ec->udev, input);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001469 break;
1470 case TTY_LEAVE_VT:
1471 compositor->focus = 0;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001472 ec->prev_state = compositor->state;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001473 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04001474
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001475 /* If we have a repaint scheduled (either from a
1476 * pending pageflip or the idle handler), make sure we
1477 * cancel that so we don't try to pageflip when we're
1478 * vt switched away. The SLEEPING state will prevent
1479 * further attemps at repainting. When we switch
1480 * back, we schedule a repaint, which will process
1481 * pending frame callbacks. */
1482
1483 wl_list_for_each(output, &ec->base.output_list, link) {
1484 output->repaint_needed = 0;
1485 drm_output_set_cursor(output, NULL);
1486 }
1487
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001488 drm_output = container_of(ec->base.output_list.next,
1489 struct drm_output, base.link);
1490
1491 wl_list_for_each(sprite, &ec->sprite_list, link)
1492 drmModeSetPlane(ec->drm.fd,
1493 sprite->plane_id,
1494 drm_output->crtc_id, 0, 0,
1495 0, 0, 0, 0, 0, 0, 0, 0);
1496
Tiago Vignatti6e2d5f12011-12-19 00:32:48 +02001497 wl_list_for_each(input, &compositor->input_device_list, link)
1498 evdev_remove_devices(input);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04001499
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001500 if (drmDropMaster(ec->drm.fd) < 0)
1501 fprintf(stderr, "failed to drop master: %m\n");
1502
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001503 break;
1504 };
1505}
1506
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001507static const char default_seat[] = "seat0";
1508
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001509static struct weston_compositor *
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001510drm_compositor_create(struct wl_display *display,
Tiago Vignattifaee8012011-09-01 15:58:17 -04001511 int connector, const char *seat, int tty)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001512{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001513 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001514 struct udev_enumerate *e;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001515 struct udev_list_entry *entry;
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001516 struct udev_device *device, *drm_device;
1517 const char *path, *device_seat;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001518 struct wl_event_loop *loop;
1519
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001520 ec = malloc(sizeof *ec);
1521 if (ec == NULL)
1522 return NULL;
1523
1524 memset(ec, 0, sizeof *ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001525 ec->udev = udev_new();
1526 if (ec->udev == NULL) {
1527 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001528 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001529 }
1530
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05001531 ec->base.wl_display = display;
1532 ec->tty = tty_create(&ec->base, vt_func, tty);
1533 if (!ec->tty) {
1534 fprintf(stderr, "failed to initialize tty\n");
1535 free(ec);
1536 return NULL;
1537 }
1538
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001539 e = udev_enumerate_new(ec->udev);
1540 udev_enumerate_add_match_subsystem(e, "drm");
Benjamin Franzkea764ee52011-10-07 08:23:22 +02001541 udev_enumerate_add_match_sysname(e, "card[0-9]*");
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001542
Benjamin Franzke117483d2011-08-30 11:38:26 +02001543 udev_enumerate_scan_devices(e);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001544 drm_device = NULL;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001545 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001546 path = udev_list_entry_get_name(entry);
1547 device = udev_device_new_from_syspath(ec->udev, path);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001548 device_seat =
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001549 udev_device_get_property_value(device, "ID_SEAT");
1550 if (!device_seat)
1551 device_seat = default_seat;
1552 if (strcmp(device_seat, seat) == 0) {
1553 drm_device = device;
1554 break;
1555 }
1556 udev_device_unref(device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001557 }
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001558
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001559 if (drm_device == NULL) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001560 fprintf(stderr, "no drm device found\n");
1561 return NULL;
1562 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001563
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001564 if (init_egl(ec, drm_device) < 0) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001565 fprintf(stderr, "failed to initialize egl\n");
1566 return NULL;
1567 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001568
1569 ec->base.destroy = drm_destroy;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001570
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001571 ec->base.focus = 1;
1572
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001573 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001574
Benjamin Franzked59eb1c2011-04-29 22:14:54 +02001575 /* Can't init base class until we have a current egl context */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001576 if (weston_compositor_init(&ec->base, display) < 0)
Benjamin Franzked59eb1c2011-04-29 22:14:54 +02001577 return NULL;
1578
Jesse Barnes58ef3792012-02-23 09:45:49 -05001579 wl_list_init(&ec->sprite_list);
1580 create_sprites(ec);
1581
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001582 if (create_outputs(ec, connector, drm_device) < 0) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001583 fprintf(stderr, "failed to create output for %s\n", path);
1584 return NULL;
1585 }
1586
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001587 udev_device_unref(drm_device);
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02001588 udev_enumerate_unref(e);
1589 path = NULL;
1590
Tiago Vignattice03ec32011-12-19 01:14:03 +02001591 evdev_input_create(&ec->base, ec->udev, seat);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001592
1593 loop = wl_display_get_event_loop(ec->base.wl_display);
1594 ec->drm_source =
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001595 wl_event_loop_add_fd(loop, ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001596 WL_EVENT_READABLE, on_drm_input, ec);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001597
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001598 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
1599 if (ec->udev_monitor == NULL) {
1600 fprintf(stderr, "failed to intialize udev monitor\n");
1601 return NULL;
1602 }
1603 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
1604 "drm", NULL);
1605 ec->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02001606 wl_event_loop_add_fd(loop,
1607 udev_monitor_get_fd(ec->udev_monitor),
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001608 WL_EVENT_READABLE, udev_drm_event, ec);
1609
1610 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
1611 fprintf(stderr, "failed to enable udev-monitor receiving\n");
1612 return NULL;
1613 }
1614
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001615 return &ec->base;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001616}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001617
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001618WL_EXPORT struct weston_compositor *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001619backend_init(struct wl_display *display, int argc, char *argv[])
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001620{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001621 int connector = 0, tty = 0;
1622 const char *seat = default_seat;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001623
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001624 const struct weston_option drm_options[] = {
1625 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
1626 { WESTON_OPTION_STRING, "seat", 0, &seat },
1627 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
1628 };
Benjamin Franzke117483d2011-08-30 11:38:26 +02001629
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001630 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001631
Tiago Vignattifaee8012011-09-01 15:58:17 -04001632 return drm_compositor_create(display, connector, seat, tty);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001633}