blob: 763ffc830b048c1c02fa59aaed7f4ec032da8b2a [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"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010042#include "launcher-util.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040044struct drm_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050045 struct weston_compositor base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040046
47 struct udev *udev;
48 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040049
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010050 struct udev_monitor *udev_monitor;
51 struct wl_event_source *udev_drm_source;
52
Benjamin Franzke2af7f102011-03-02 11:14:59 +010053 struct {
David Herrmannd7488c22012-03-11 20:05:21 +010054 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010055 int fd;
56 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +020057 struct gbm_device *gbm;
Jesse Barnes58ef3792012-02-23 09:45:49 -050058 uint32_t *crtcs;
59 int num_crtcs;
Marty Jack13d9db22011-02-09 19:01:42 -050060 uint32_t crtc_allocator;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010061 uint32_t connector_allocator;
Kristian Høgsberge4762a62011-01-14 14:59:13 -050062 struct tty *tty;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +020063
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -040064 struct gbm_surface *dummy_surface;
65 EGLSurface dummy_egl_surface;
66
Jesse Barnes58ef3792012-02-23 09:45:49 -050067 struct wl_list sprite_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -050068 int sprites_are_broken;
Jesse Barnes58ef3792012-02-23 09:45:49 -050069
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +020070 uint32_t prev_state;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040071};
72
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040073struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050074 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040075 drmModeModeInfo mode_info;
76};
77
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040078struct drm_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050079 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040080
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040081 uint32_t crtc_id;
82 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -070083 drmModeCrtcPtr original_crtc;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +020084
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -040085 struct gbm_surface *surface;
86 EGLSurface egl_surface;
87 uint32_t current_fb_id;
88 uint32_t next_fb_id;
89 struct gbm_bo *current_bo;
90 struct gbm_bo *next_bo;
91
Kristian Høgsberg9f404b72012-01-26 00:11:01 -050092 struct wl_buffer *scanout_buffer;
93 struct wl_listener scanout_buffer_destroy_listener;
94 struct wl_buffer *pending_scanout_buffer;
95 struct wl_listener pending_scanout_buffer_destroy_listener;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +020096 struct backlight *backlight;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040097};
98
Jesse Barnes58ef3792012-02-23 09:45:49 -050099/*
100 * An output has a primary display plane plus zero or more sprites for
101 * blending display contents.
102 */
103struct drm_sprite {
104 struct wl_list link;
105
106 uint32_t fb_id;
107 uint32_t pending_fb_id;
108 struct weston_surface *surface;
109 struct weston_surface *pending_surface;
110
111 struct drm_compositor *compositor;
112
113 struct wl_listener destroy_listener;
114 struct wl_listener pending_destroy_listener;
115
116 uint32_t possible_crtcs;
117 uint32_t plane_id;
118 uint32_t count_formats;
119
120 int32_t src_x, src_y;
121 uint32_t src_w, src_h;
122 uint32_t dest_x, dest_y;
123 uint32_t dest_w, dest_h;
124
125 uint32_t formats[];
126};
127
128static int
129surface_is_primary(struct weston_compositor *ec, struct weston_surface *es)
130{
131 struct weston_surface *primary;
132
133 primary = container_of(ec->surface_list.next, struct weston_surface,
134 link);
135 if (es == primary)
136 return -1;
137 return 0;
138}
139
140static int
141drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
142{
143 struct weston_compositor *ec = output_base->compositor;
144 struct drm_compositor *c =(struct drm_compositor *) ec;
145 struct drm_output *output = (struct drm_output *) output_base;
146 int crtc;
147
148 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
149 if (c->crtcs[crtc] != output->crtc_id)
150 continue;
151
152 if (supported & (1 << crtc))
153 return -1;
154 }
155
156 return 0;
157}
158
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500159static int
160drm_output_prepare_scanout_surface(struct drm_output *output)
161{
162 struct drm_compositor *c =
163 (struct drm_compositor *) output->base.compositor;
164 struct weston_surface *es;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500165
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
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400181 output->next_bo =
182 gbm_bo_create_from_egl_image(c->gbm,
183 c->base.display, es->image,
184 es->geometry.width,
185 es->geometry.height,
186 GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500187
188 /* assert output->pending_scanout_buffer == NULL */
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500189 output->pending_scanout_buffer = es->buffer;
190 output->pending_scanout_buffer->busy_count++;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500191
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500192 wl_list_insert(output->pending_scanout_buffer->resource.destroy_listener_list.prev,
193 &output->pending_scanout_buffer_destroy_listener.link);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500194
195 pixman_region32_fini(&es->damage);
196 pixman_region32_init(&es->damage);
197
198 return 0;
199}
200
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500201static void
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400202drm_output_render(struct drm_output *output, pixman_region32_t *damage)
203{
204 struct drm_compositor *compositor =
205 (struct drm_compositor *) output->base.compositor;
206 struct weston_surface *surface;
207
208 if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
209 output->egl_surface, compositor->base.context)) {
210 fprintf(stderr, "failed to make current\n");
211 return;
212 }
213
214 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
215 weston_surface_draw(surface, &output->base, damage);
216
217 eglSwapBuffers(compositor->base.display, output->egl_surface);
218 output->next_bo = gbm_surface_lock_front_buffer(output->surface);
219 if (!output->next_bo) {
220 fprintf(stderr, "failed to lock front buffer: %m\n");
221 return;
222 }
223}
224
225static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500226drm_output_repaint(struct weston_output *output_base,
227 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100228{
229 struct drm_output *output = (struct drm_output *) output_base;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500230 struct drm_compositor *compositor =
231 (struct drm_compositor *) output->base.compositor;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500232 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400233 struct drm_mode *mode;
234 uint32_t stride, handle;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500235 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100236
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500237 drm_output_prepare_scanout_surface(output);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400238 if (!output->next_bo)
239 drm_output_render(output, damage);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500240
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400241 stride = gbm_bo_get_pitch(output->next_bo);
242 handle = gbm_bo_get_handle(output->next_bo).u32;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400243
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400244 /* Destroy and set to NULL now so we don't try to
245 * gbm_surface_release_buffer() a client buffer in the
246 * page_flip_handler. */
247 if (output->pending_scanout_buffer) {
248 gbm_bo_destroy(output->next_bo);
249 output->next_bo = NULL;
250 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400251
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400252 ret = drmModeAddFB(compositor->drm.fd,
253 output->base.current->width,
254 output->base.current->height,
255 24, 32, stride, handle, &output->next_fb_id);
256 if (ret) {
257 fprintf(stderr, "failed to create kms fb: %m\n");
258 gbm_surface_release_buffer(output->surface, output->next_bo);
259 output->next_bo = NULL;
260 return;
261 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100262
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400263 mode = container_of(output->base.current, struct drm_mode, base);
264 if (output->current_fb_id == 0) {
265 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
266 output->next_fb_id, 0, 0,
267 &output->connector_id, 1,
268 &mode->mode_info);
269 if (ret) {
270 fprintf(stderr, "set mode failed: %m\n");
271 return;
272 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200273 }
274
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500275 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400276 output->next_fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500277 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
278 fprintf(stderr, "queueing pageflip failed: %m\n");
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500279 return;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500280 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100281
Jesse Barnes58ef3792012-02-23 09:45:49 -0500282 /*
283 * Now, update all the sprite surfaces
284 */
285 wl_list_for_each(s, &compositor->sprite_list, link) {
286 uint32_t flags = 0;
287 drmVBlank vbl = {
288 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
289 .request.sequence = 1,
290 };
291
292 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
293 continue;
294
295 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
296 output->crtc_id, s->pending_fb_id, flags,
297 s->dest_x, s->dest_y,
298 s->dest_w, s->dest_h,
299 s->src_x, s->src_y,
300 s->src_w, s->src_h);
301 if (ret)
302 fprintf(stderr, "setplane failed: %d: %s\n",
303 ret, strerror(errno));
304
305 /*
306 * Queue a vblank signal so we know when the surface
307 * becomes active on the display or has been replaced.
308 */
309 vbl.request.signal = (unsigned long)s;
310 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
311 if (ret) {
312 fprintf(stderr, "vblank event request failed: %d: %s\n",
313 ret, strerror(errno));
314 }
315 }
316
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500317 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400318}
319
320static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500321vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
322 void *data)
323{
324 struct drm_sprite *s = (struct drm_sprite *)data;
325 struct drm_compositor *c = s->compositor;
326
327 if (s->surface) {
328 weston_buffer_post_release(s->surface->buffer);
329 wl_list_remove(&s->destroy_listener.link);
330 s->surface = NULL;
331 drmModeRmFB(c->drm.fd, s->fb_id);
332 s->fb_id = 0;
333 }
334
335 if (s->pending_surface) {
336 wl_list_remove(&s->pending_destroy_listener.link);
337 wl_list_insert(s->pending_surface->buffer->resource.destroy_listener_list.prev,
338 &s->destroy_listener.link);
339 s->surface = s->pending_surface;
340 s->pending_surface = NULL;
341 s->fb_id = s->pending_fb_id;
342 s->pending_fb_id = 0;
343 }
344}
345
346static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400347page_flip_handler(int fd, unsigned int frame,
348 unsigned int sec, unsigned int usec, void *data)
349{
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200350 struct drm_output *output = (struct drm_output *) data;
351 struct drm_compositor *c =
352 (struct drm_compositor *) output->base.compositor;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400353 uint32_t msecs;
354
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400355 if (output->current_fb_id)
356 drmModeRmFB(c->drm.fd, output->current_fb_id);
357 output->current_fb_id = output->next_fb_id;
358 output->next_fb_id = 0;
359
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500360 if (output->scanout_buffer) {
361 weston_buffer_post_release(output->scanout_buffer);
362 wl_list_remove(&output->scanout_buffer_destroy_listener.link);
363 output->scanout_buffer = NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400364 } else if (output->current_bo) {
365 gbm_surface_release_buffer(output->surface,
366 output->current_bo);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200367 }
368
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400369 output->current_bo = output->next_bo;
370 output->next_bo = NULL;
371
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500372 if (output->pending_scanout_buffer) {
373 output->scanout_buffer = output->pending_scanout_buffer;
374 wl_list_remove(&output->pending_scanout_buffer_destroy_listener.link);
375 wl_list_insert(output->scanout_buffer->resource.destroy_listener_list.prev,
376 &output->scanout_buffer_destroy_listener.link);
377 output->pending_scanout_buffer = NULL;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200378 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100379 msecs = sec * 1000 + usec / 1000;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500380 weston_output_finish_frame(&output->base, msecs);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200381}
382
383static int
Jesse Barnes58ef3792012-02-23 09:45:49 -0500384drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
385{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400386 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500387
388 for (i = 0; i < s->count_formats; i++)
389 if (s->formats[i] == format)
390 return 1;
391
392 return 0;
393}
394
395static int
396drm_surface_transform_supported(struct weston_surface *es)
397{
398 if (es->transform.enabled)
399 return 0;
400
401 return 1;
402}
403
404static int
405drm_surface_overlap_supported(struct weston_output *output_base,
406 pixman_region32_t *overlap)
407{
408 /* We could potentially use a color key here if the surface left
409 * to display has rectangular regions
410 */
411 if (pixman_region32_not_empty(overlap))
412 return 0;
413 return 1;
414}
415
416static void
417drm_disable_unused_sprites(struct weston_output *output_base)
418{
419 struct weston_compositor *ec = output_base->compositor;
420 struct drm_compositor *c =(struct drm_compositor *) ec;
421 struct drm_output *output = (struct drm_output *) output_base;
422 struct drm_sprite *s;
423 int ret;
424
425 wl_list_for_each(s, &c->sprite_list, link) {
426 if (s->pending_fb_id)
427 continue;
428
429 ret = drmModeSetPlane(c->drm.fd, s->plane_id,
430 output->crtc_id, 0, 0,
431 0, 0, 0, 0, 0, 0, 0, 0);
432 if (ret)
433 fprintf(stderr,
434 "failed to disable plane: %d: %s\n",
435 ret, strerror(errno));
436 drmModeRmFB(c->drm.fd, s->fb_id);
437 s->surface = NULL;
438 s->pending_surface = NULL;
439 s->fb_id = 0;
440 s->pending_fb_id = 0;
441 }
442}
443
444/*
445 * This function must take care to damage any previously assigned surface
446 * if the sprite ends up binding to a different surface than in the
447 * previous frame.
448 */
449static int
450drm_output_prepare_overlay_surface(struct weston_output *output_base,
451 struct weston_surface *es,
452 pixman_region32_t *overlap)
453{
454 struct weston_compositor *ec = output_base->compositor;
455 struct drm_compositor *c =(struct drm_compositor *) ec;
456 struct drm_sprite *s;
457 int found = 0;
458 EGLint handle, stride;
459 struct gbm_bo *bo;
460 uint32_t fb_id = 0;
461 uint32_t handles[4], pitches[4], offsets[4];
462 int ret = 0;
463 pixman_region32_t dest_rect, src_rect;
464 pixman_box32_t *box;
465 uint32_t format;
466
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500467 if (c->sprites_are_broken)
468 return -1;
469
Jesse Barnes58ef3792012-02-23 09:45:49 -0500470 if (surface_is_primary(ec, es))
471 return -1;
472
Kristian Høgsberg7a011ea2012-03-26 11:48:55 -0400473 if (es->image == EGL_NO_IMAGE_KHR)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500474 return -1;
475
476 if (!drm_surface_transform_supported(es))
477 return -1;
478
479 if (!drm_surface_overlap_supported(output_base, overlap))
480 return -1;
481
482 wl_list_for_each(s, &c->sprite_list, link) {
483 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
484 continue;
485
486 if (!s->pending_fb_id) {
487 found = 1;
488 break;
489 }
490 }
491
492 /* No sprites available */
493 if (!found)
494 return -1;
495
496 bo = gbm_bo_create_from_egl_image(c->gbm, c->base.display, es->image,
497 es->geometry.width, es->geometry.height,
498 GBM_BO_USE_SCANOUT);
499 format = gbm_bo_get_format(bo);
500 handle = gbm_bo_get_handle(bo).s32;
501 stride = gbm_bo_get_pitch(bo);
502
503 gbm_bo_destroy(bo);
504
505 if (!drm_surface_format_supported(s, format))
506 return -1;
507
508 if (!handle)
509 return -1;
510
511 handles[0] = handle;
512 pitches[0] = stride;
513 offsets[0] = 0;
514
515 ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height,
516 format, handles, pitches, offsets,
517 &fb_id, 0);
518 if (ret) {
519 fprintf(stderr, "addfb2 failed: %d\n", ret);
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500520 c->sprites_are_broken = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500521 return -1;
522 }
523
524 if (s->surface && s->surface != es) {
525 struct weston_surface *old_surf = s->surface;
526 pixman_region32_fini(&old_surf->damage);
527 pixman_region32_init_rect(&old_surf->damage,
528 old_surf->geometry.x, old_surf->geometry.y,
529 old_surf->geometry.width, old_surf->geometry.height);
530 }
531
532 s->pending_fb_id = fb_id;
533 s->pending_surface = es;
534 es->buffer->busy_count++;
535
536 /*
537 * Calculate the source & dest rects properly based on actual
538 * postion (note the caller has called weston_surface_update_transform()
539 * for us already).
540 */
541 pixman_region32_init(&dest_rect);
542 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
543 &output_base->region);
544 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
545 box = pixman_region32_extents(&dest_rect);
546 s->dest_x = box->x1;
547 s->dest_y = box->y1;
548 s->dest_w = box->x2 - box->x1;
549 s->dest_h = box->y2 - box->y1;
550 pixman_region32_fini(&dest_rect);
551
552 pixman_region32_init(&src_rect);
553 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
554 &output_base->region);
555 pixman_region32_translate(&src_rect, -es->geometry.x, -es->geometry.y);
556 box = pixman_region32_extents(&src_rect);
Rob Clark3c97b902012-03-11 19:48:43 -0500557 s->src_x = box->x1 << 16;
558 s->src_y = box->y1 << 16;
559 s->src_w = (box->x2 - box->x1) << 16;
560 s->src_h = (box->y2 - box->y1) << 16;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500561 pixman_region32_fini(&src_rect);
562
Jesse Barnes58ef3792012-02-23 09:45:49 -0500563 wl_list_insert(es->buffer->resource.destroy_listener_list.prev,
564 &s->pending_destroy_listener.link);
565 return 0;
566}
567
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500568static int
569drm_output_set_cursor(struct weston_output *output_base,
570 struct weston_input_device *eid);
571
572static void
573weston_output_set_cursor(struct weston_output *output,
574 struct weston_input_device *device,
575 pixman_region32_t *overlap)
576{
577 pixman_region32_t cursor_region;
578 int prior_was_hardware;
579
580 if (device->sprite == NULL)
581 return;
582
583 pixman_region32_init(&cursor_region);
584 pixman_region32_intersect(&cursor_region,
585 &device->sprite->transform.boundingbox,
586 &output->region);
587
588 if (!pixman_region32_not_empty(&cursor_region)) {
589 drm_output_set_cursor(output, NULL);
590 goto out;
591 }
592
593 prior_was_hardware = device->hw_cursor;
594 if (pixman_region32_not_empty(overlap) ||
595 drm_output_set_cursor(output, device) < 0) {
596 if (prior_was_hardware) {
597 weston_surface_damage(device->sprite);
598 drm_output_set_cursor(output, NULL);
599 }
600 device->hw_cursor = 0;
601 } else {
602 if (!prior_was_hardware)
603 weston_surface_damage_below(device->sprite);
604 pixman_region32_fini(&device->sprite->damage);
605 pixman_region32_init(&device->sprite->damage);
606 device->hw_cursor = 1;
607 }
608
609out:
610 pixman_region32_fini(&cursor_region);
611}
612
Jesse Barnes58ef3792012-02-23 09:45:49 -0500613static void
614drm_assign_planes(struct weston_output *output)
615{
616 struct weston_compositor *ec = output->compositor;
617 struct weston_surface *es;
618 pixman_region32_t overlap, surface_overlap;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500619 struct weston_input_device *device;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500620
621 /*
622 * Find a surface for each sprite in the output using some heuristics:
623 * 1) size
624 * 2) frequency of update
625 * 3) opacity (though some hw might support alpha blending)
626 * 4) clipping (this can be fixed with color keys)
627 *
628 * The idea is to save on blitting since this should save power.
629 * If we can get a large video surface on the sprite for example,
630 * the main display surface may not need to update at all, and
631 * the client buffer can be used directly for the sprite surface
632 * as we do for flipping full screen surfaces.
633 */
634 pixman_region32_init(&overlap);
635 wl_list_for_each(es, &ec->surface_list, link) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500636 /*
637 * FIXME: try to assign hw cursors here too, they're just
638 * special overlays
639 */
640 pixman_region32_init(&surface_overlap);
641 pixman_region32_intersect(&surface_overlap, &overlap,
642 &es->transform.boundingbox);
643
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500644 device = (struct weston_input_device *) ec->input_device;
645 if (es == device->sprite) {
646 weston_output_set_cursor(output, device,
647 &surface_overlap);
648
649 if (!device->hw_cursor)
650 pixman_region32_union(&overlap, &overlap,
651 &es->transform.boundingbox);
652 } else if (!drm_output_prepare_overlay_surface(output, es,
653 &surface_overlap)) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500654 pixman_region32_fini(&es->damage);
655 pixman_region32_init(&es->damage);
656 } else {
657 pixman_region32_union(&overlap, &overlap,
658 &es->transform.boundingbox);
659 }
660 pixman_region32_fini(&surface_overlap);
661 }
662 pixman_region32_fini(&overlap);
663
664 drm_disable_unused_sprites(output);
665}
666
667static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500668drm_output_set_cursor(struct weston_output *output_base,
669 struct weston_input_device *eid)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200670{
671 struct drm_output *output = (struct drm_output *) output_base;
672 struct drm_compositor *c =
673 (struct drm_compositor *) output->base.compositor;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200674 EGLint handle, stride;
675 int ret = -1;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200676 struct gbm_bo *bo;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200677
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400678 if (eid == NULL) {
679 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
680 return 0;
681 }
682
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200683 if (eid->sprite->image == EGL_NO_IMAGE_KHR)
684 goto out;
685
Pekka Paalanen60921e52012-01-25 15:55:43 +0200686 if (eid->sprite->geometry.width > 64 ||
687 eid->sprite->geometry.height > 64)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200688 goto out;
Benjamin Franzke117483d2011-08-30 11:38:26 +0200689
Benjamin Franzke060cf802011-04-30 09:32:11 +0200690 bo = gbm_bo_create_from_egl_image(c->gbm,
691 c->base.display,
692 eid->sprite->image, 64, 64,
693 GBM_BO_USE_CURSOR_64X64);
Kristian Høgsberg6916d9c2012-01-18 15:11:07 -0500694 /* Not suitable for hw cursor, fall back */
695 if (bo == NULL)
696 goto out;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200697
698 handle = gbm_bo_get_handle(bo).s32;
699 stride = gbm_bo_get_pitch(bo);
Benjamin Franzke060cf802011-04-30 09:32:11 +0200700 gbm_bo_destroy(bo);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200701
Kristian Høgsberg6916d9c2012-01-18 15:11:07 -0500702 /* gbm_bo_create_from_egl_image() didn't always validate the usage
703 * flags, and in that case we might end up with a bad stride. */
704 if (stride != 64 * 4)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200705 goto out;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200706
707 ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
708 if (ret) {
709 fprintf(stderr, "failed to set cursor: %s\n", strerror(-ret));
710 goto out;
711 }
712
713 ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200714 eid->sprite->geometry.x - output->base.x,
715 eid->sprite->geometry.y - output->base.y);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200716 if (ret) {
717 fprintf(stderr, "failed to move cursor: %s\n", strerror(-ret));
718 goto out;
719 }
720
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200721out:
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200722 if (ret)
723 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
724 return ret;
725}
726
Matt Roper361d2ad2011-08-29 13:52:23 -0700727static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500728drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700729{
730 struct drm_output *output = (struct drm_output *) output_base;
731 struct drm_compositor *c =
Benjamin Franzke117483d2011-08-30 11:38:26 +0200732 (struct drm_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700733 drmModeCrtcPtr origcrtc = output->original_crtc;
Matt Roper361d2ad2011-08-29 13:52:23 -0700734
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200735 if (output->backlight)
736 backlight_destroy(output->backlight);
737
Matt Roper361d2ad2011-08-29 13:52:23 -0700738 /* Turn off hardware cursor */
Benjamin Franzke117483d2011-08-30 11:38:26 +0200739 drm_output_set_cursor(&output->base, NULL);
Matt Roper361d2ad2011-08-29 13:52:23 -0700740
741 /* Restore original CRTC state */
742 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +0200743 origcrtc->x, origcrtc->y,
744 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -0700745 drmModeFreeCrtc(origcrtc);
746
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200747 c->crtc_allocator &= ~(1 << output->crtc_id);
748 c->connector_allocator &= ~(1 << output->connector_id);
749
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400750 eglDestroySurface(c->base.display, output->egl_surface);
751 gbm_surface_destroy(output->surface);
752
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500753 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200754 wl_list_remove(&output->base.link);
755
Matt Roper361d2ad2011-08-29 13:52:23 -0700756 free(output);
757}
758
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400759static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400760on_drm_input(int fd, uint32_t mask, void *data)
761{
762 drmEventContext evctx;
763
764 memset(&evctx, 0, sizeof evctx);
765 evctx.version = DRM_EVENT_CONTEXT_VERSION;
766 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500767 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400768 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400769
770 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400771}
772
773static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400774init_egl(struct drm_compositor *ec, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400775{
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400776 EGLint major, minor, n;
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400777 const char *filename, *sysnum;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400778 int fd;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400779 static const EGLint context_attribs[] = {
780 EGL_CONTEXT_CLIENT_VERSION, 2,
781 EGL_NONE
782 };
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400783
David Herrmannd7488c22012-03-11 20:05:21 +0100784 sysnum = udev_device_get_sysnum(device);
785 if (sysnum)
786 ec->drm.id = atoi(sysnum);
787 if (!sysnum || ec->drm.id < 0) {
788 fprintf(stderr, "cannot get device sysnum\n");
789 return -1;
790 }
791
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400792 static const EGLint config_attribs[] = {
793 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
794 EGL_RED_SIZE, 1,
795 EGL_GREEN_SIZE, 1,
796 EGL_BLUE_SIZE, 1,
797 EGL_ALPHA_SIZE, 0,
798 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
799 EGL_NONE
800 };
801
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400802 filename = udev_device_get_devnode(device);
David Herrmann63ff7062011-11-05 18:46:01 +0100803 fd = open(filename, O_RDWR | O_CLOEXEC);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400804 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400805 /* Probably permissions error */
806 fprintf(stderr, "couldn't open %s, skipping\n",
807 udev_device_get_devnode(device));
808 return -1;
809 }
810
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100811 ec->drm.fd = fd;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200812 ec->gbm = gbm_create_device(ec->drm.fd);
813 ec->base.display = eglGetDisplay(ec->gbm);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400814 if (ec->base.display == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400815 fprintf(stderr, "failed to create display\n");
816 return -1;
817 }
818
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400819 if (!eglInitialize(ec->base.display, &major, &minor)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400820 fprintf(stderr, "failed to initialize display\n");
821 return -1;
822 }
823
Darxus55973f22010-11-22 21:24:39 -0500824 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
825 fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n");
826 return -1;
827 }
828
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400829 if (!eglChooseConfig(ec->base.display, config_attribs,
830 &ec->base.config, 1, &n) || n != 1) {
831 fprintf(stderr, "failed to choose config: %d\n", n);
832 return -1;
833 }
834
835 ec->base.context = eglCreateContext(ec->base.display, ec->base.config,
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400836 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400837 if (ec->base.context == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400838 fprintf(stderr, "failed to create context\n");
839 return -1;
840 }
841
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400842 ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10,
843 GBM_FORMAT_XRGB8888,
844 GBM_BO_USE_RENDERING);
845 if (!ec->dummy_surface) {
846 fprintf(stderr, "failed to create dummy gbm surface\n");
847 return -1;
848 }
849
850 ec->dummy_egl_surface =
851 eglCreateWindowSurface(ec->base.display, ec->base.config,
852 ec->dummy_surface, NULL);
853 if (ec->dummy_egl_surface == EGL_NO_SURFACE) {
854 fprintf(stderr, "failed to create egl surface\n");
855 return -1;
856 }
857
858 if (!eglMakeCurrent(ec->base.display, ec->dummy_egl_surface,
859 ec->dummy_egl_surface, ec->base.context)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400860 fprintf(stderr, "failed to make context current\n");
861 return -1;
862 }
863
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400864 return 0;
865}
866
867static drmModeModeInfo builtin_1024x768 = {
868 63500, /* clock */
869 1024, 1072, 1176, 1328, 0,
870 768, 771, 775, 798, 0,
871 59920,
872 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
873 0,
874 "1024x768"
875};
876
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400877
878static int
879drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
880{
881 struct drm_mode *mode;
882
883 mode = malloc(sizeof *mode);
884 if (mode == NULL)
885 return -1;
886
887 mode->base.flags = 0;
888 mode->base.width = info->hdisplay;
889 mode->base.height = info->vdisplay;
890 mode->base.refresh = info->vrefresh;
891 mode->mode_info = *info;
892 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
893
894 return 0;
895}
896
897static int
898drm_subpixel_to_wayland(int drm_value)
899{
900 switch (drm_value) {
901 default:
902 case DRM_MODE_SUBPIXEL_UNKNOWN:
903 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
904 case DRM_MODE_SUBPIXEL_NONE:
905 return WL_OUTPUT_SUBPIXEL_NONE;
906 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
907 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
908 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
909 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
910 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
911 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
912 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
913 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
914 }
915}
916
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500917static void
918output_handle_scanout_buffer_destroy(struct wl_listener *listener,
919 struct wl_resource *resource,
920 uint32_t time)
921{
922 struct drm_output *output =
923 container_of(listener, struct drm_output,
924 scanout_buffer_destroy_listener);
925
926 output->scanout_buffer = NULL;
927
928 if (!output->pending_scanout_buffer)
929 weston_compositor_schedule_repaint(output->base.compositor);
930}
931
932static void
933output_handle_pending_scanout_buffer_destroy(struct wl_listener *listener,
934 struct wl_resource *resource,
935 uint32_t time)
936{
937 struct drm_output *output =
938 container_of(listener, struct drm_output,
939 pending_scanout_buffer_destroy_listener);
940
941 output->pending_scanout_buffer = NULL;
942
943 weston_compositor_schedule_repaint(output->base.compositor);
944}
945
Jesse Barnes58ef3792012-02-23 09:45:49 -0500946static void
947sprite_handle_buffer_destroy(struct wl_listener *listener,
948 struct wl_resource *resource,
949 uint32_t time)
950{
951 struct drm_sprite *sprite =
952 container_of(listener, struct drm_sprite,
953 destroy_listener);
954
955 sprite->surface = NULL;
956}
957
958static void
959sprite_handle_pending_buffer_destroy(struct wl_listener *listener,
960 struct wl_resource *resource,
961 uint32_t time)
962{
963 struct drm_sprite *sprite =
964 container_of(listener, struct drm_sprite,
965 pending_destroy_listener);
966
967 sprite->pending_surface = NULL;
968}
969
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300970/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200971static uint32_t
972drm_get_backlight(struct drm_output *output)
973{
974 long brightness, max_brightness, norm;
975
976 brightness = backlight_get_brightness(output->backlight);
977 max_brightness = backlight_get_max_brightness(output->backlight);
978
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300979 /* convert it on a scale of 0 to 255 */
980 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200981
982 return (uint32_t) norm;
983}
984
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -0300985/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200986static void
987drm_set_backlight(struct weston_output *output_base, uint32_t value)
988{
989 struct drm_output *output = (struct drm_output *) output_base;
990 long max_brightness, new_brightness;
991
992 if (!output->backlight)
993 return;
994
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400995 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200996 return;
997
998 max_brightness = backlight_get_max_brightness(output->backlight);
999
1000 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001001 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001002
1003 backlight_set_brightness(output->backlight, new_brightness);
1004}
1005
1006static drmModePropertyPtr
1007drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1008{
1009 drmModePropertyPtr props;
1010 int i;
1011
1012 for (i = 0; i < connector->count_props; i++) {
1013 props = drmModeGetProperty(fd, connector->props[i]);
1014 if (!props)
1015 continue;
1016
1017 if (!strcmp(props->name, name))
1018 return props;
1019
1020 drmModeFreeProperty(props);
1021 }
1022
1023 return NULL;
1024}
1025
1026static void
1027drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1028{
1029 struct drm_output *output = (struct drm_output *) output_base;
1030 struct weston_compositor *ec = output_base->compositor;
1031 struct drm_compositor *c = (struct drm_compositor *) ec;
1032 drmModeConnectorPtr connector;
1033 drmModePropertyPtr prop;
1034
1035 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1036 if (!connector)
1037 return;
1038
1039 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1040 if (!prop) {
1041 drmModeFreeConnector(connector);
1042 return;
1043 }
1044
1045 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1046 prop->prop_id, level);
1047 drmModeFreeProperty(prop);
1048 drmModeFreeConnector(connector);
1049}
1050
Scott Moreau80d27b72012-04-04 11:49:21 -06001051static void
1052drm_output_read_pixels(struct weston_output *output_base, void *data)
1053{
1054 struct drm_output *output = (struct drm_output *) output_base;
1055 struct drm_compositor *compositor =
1056 (struct drm_compositor *) output->base.compositor;
1057
1058 eglMakeCurrent(compositor->base.display, output->egl_surface,
1059 output->egl_surface, compositor->base.context);
1060
1061 glReadPixels(0, 0, output_base->current->width, output_base->current->height,
1062 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1063}
1064
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001065static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001066create_output_for_connector(struct drm_compositor *ec,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001067 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001068 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001069 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001070{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001071 struct drm_output *output;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001072 struct drm_mode *drm_mode, *next;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001073 drmModeEncoder *encoder;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001074 int i, ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001075
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001076 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001077 if (encoder == NULL) {
1078 fprintf(stderr, "No encoder for connector.\n");
1079 return -1;
1080 }
1081
1082 for (i = 0; i < resources->count_crtcs; i++) {
Marty Jack13d9db22011-02-09 19:01:42 -05001083 if (encoder->possible_crtcs & (1 << i) &&
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001084 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001085 break;
1086 }
1087 if (i == resources->count_crtcs) {
1088 fprintf(stderr, "No usable crtc for encoder.\n");
David Herrmanneb8bed52011-12-08 17:05:44 +01001089 drmModeFreeEncoder(encoder);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001090 return -1;
1091 }
1092
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001093 output = malloc(sizeof *output);
David Herrmanneb8bed52011-12-08 17:05:44 +01001094 if (output == NULL) {
1095 drmModeFreeEncoder(encoder);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001096 return -1;
David Herrmanneb8bed52011-12-08 17:05:44 +01001097 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001098
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001099 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001100 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1101 output->base.make = "unknown";
1102 output->base.model = "unknown";
1103 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001104
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001105 output->crtc_id = resources->crtcs[i];
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001106 ec->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001107 output->connector_id = connector->connector_id;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001108 ec->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001109
Matt Roper361d2ad2011-08-29 13:52:23 -07001110 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001111 drmModeFreeEncoder(encoder);
Matt Roper361d2ad2011-08-29 13:52:23 -07001112
David Herrmann0f0d54e2011-12-08 17:05:45 +01001113 for (i = 0; i < connector->count_modes; i++) {
1114 ret = drm_output_add_mode(output, &connector->modes[i]);
1115 if (ret)
1116 goto err_free;
1117 }
1118
1119 if (connector->count_modes == 0) {
1120 ret = drm_output_add_mode(output, &builtin_1024x768);
1121 if (ret)
1122 goto err_free;
1123 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001124
1125 drm_mode = container_of(output->base.mode_list.next,
1126 struct drm_mode, base.link);
1127 output->base.current = &drm_mode->base;
1128 drm_mode->base.flags =
1129 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1130
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001131 output->surface = gbm_surface_create(ec->gbm,
1132 output->base.current->width,
1133 output->base.current->height,
1134 GBM_FORMAT_XRGB8888,
1135 GBM_BO_USE_SCANOUT |
1136 GBM_BO_USE_RENDERING);
1137 if (!output->surface) {
1138 fprintf(stderr, "failed to create gbm surface\n");
1139 goto err_free;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001140 }
1141
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001142 output->egl_surface =
1143 eglCreateWindowSurface(ec->base.display, ec->base.config,
1144 output->surface, NULL);
1145 if (output->egl_surface == EGL_NO_SURFACE) {
1146 fprintf(stderr, "failed to create egl surface\n");
1147 goto err_surface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001148 }
1149
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001150 output->backlight = backlight_init(drm_device,
1151 connector->connector_type);
1152 if (output->backlight) {
1153 output->base.set_backlight = drm_set_backlight;
1154 output->base.backlight_current = drm_get_backlight(output);
1155 }
1156
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001157 weston_output_init(&output->base, &ec->base, x, y,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001158 connector->mmWidth, connector->mmHeight,
1159 WL_OUTPUT_FLIPPED);
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04001160
1161 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1162
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001163 output->scanout_buffer_destroy_listener.func =
1164 output_handle_scanout_buffer_destroy;
1165 output->pending_scanout_buffer_destroy_listener.func =
1166 output_handle_pending_scanout_buffer_destroy;
1167
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001168 output->next_fb_id = 0;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001169 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07001170 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001171 output->base.assign_planes = drm_assign_planes;
Scott Moreau80d27b72012-04-04 11:49:21 -06001172 output->base.read_pixels = drm_output_read_pixels;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001173 output->base.set_dpms = drm_set_dpms;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001174
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001175 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001176
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001177 eglDestroySurface(ec->base.display, output->egl_surface);
1178err_surface:
1179 gbm_surface_destroy(output->surface);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001180err_free:
1181 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1182 base.link) {
1183 wl_list_remove(&drm_mode->base.link);
1184 free(drm_mode);
1185 }
1186
1187 drmModeFreeCrtc(output->original_crtc);
1188 ec->crtc_allocator &= ~(1 << output->crtc_id);
1189 ec->connector_allocator &= ~(1 << output->connector_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001190 free(output);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001191
David Herrmann0f0d54e2011-12-08 17:05:45 +01001192 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001193}
1194
Jesse Barnes58ef3792012-02-23 09:45:49 -05001195static void
1196create_sprites(struct drm_compositor *ec)
1197{
1198 struct drm_sprite *sprite;
1199 drmModePlaneRes *plane_res;
1200 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001201 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001202
1203 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1204 if (!plane_res) {
1205 fprintf(stderr, "failed to get plane resources: %s\n",
1206 strerror(errno));
1207 return;
1208 }
1209
1210 for (i = 0; i < plane_res->count_planes; i++) {
1211 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1212 if (!plane)
1213 continue;
1214
1215 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1216 plane->count_formats));
1217 if (!sprite) {
1218 fprintf(stderr, "%s: out of memory\n",
1219 __func__);
1220 free(plane);
1221 continue;
1222 }
1223
1224 memset(sprite, 0, sizeof *sprite);
1225
1226 sprite->possible_crtcs = plane->possible_crtcs;
1227 sprite->plane_id = plane->plane_id;
1228 sprite->surface = NULL;
1229 sprite->pending_surface = NULL;
1230 sprite->fb_id = 0;
1231 sprite->pending_fb_id = 0;
1232 sprite->destroy_listener.func = sprite_handle_buffer_destroy;
1233 sprite->pending_destroy_listener.func =
1234 sprite_handle_pending_buffer_destroy;
1235 sprite->compositor = ec;
1236 sprite->count_formats = plane->count_formats;
1237 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05001238 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05001239 drmModeFreePlane(plane);
1240
1241 wl_list_insert(&ec->sprite_list, &sprite->link);
1242 }
1243
1244 free(plane_res->planes);
1245 free(plane_res);
1246}
1247
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001248static void
1249destroy_sprites(struct drm_compositor *compositor)
1250{
1251 struct drm_sprite *sprite, *next;
1252 struct drm_output *output;
1253
1254 output = container_of(compositor->base.output_list.next,
1255 struct drm_output, base.link);
1256
1257 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1258 drmModeSetPlane(compositor->drm.fd,
1259 sprite->plane_id,
1260 output->crtc_id, 0, 0,
1261 0, 0, 0, 0, 0, 0, 0, 0);
1262 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1263 free(sprite);
1264 }
1265}
Jesse Barnes58ef3792012-02-23 09:45:49 -05001266
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001267static int
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001268create_outputs(struct drm_compositor *ec, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001269 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001270{
1271 drmModeConnector *connector;
1272 drmModeRes *resources;
1273 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001274 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001275
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001276 resources = drmModeGetResources(ec->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001277 if (!resources) {
1278 fprintf(stderr, "drmModeGetResources failed\n");
1279 return -1;
1280 }
1281
Jesse Barnes58ef3792012-02-23 09:45:49 -05001282 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001283 if (!ec->crtcs) {
1284 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001285 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001286 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001287
1288 ec->num_crtcs = resources->count_crtcs;
1289 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1290
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001291 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001292 connector = drmModeGetConnector(ec->drm.fd,
1293 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001294 if (connector == NULL)
1295 continue;
1296
1297 if (connector->connection == DRM_MODE_CONNECTED &&
1298 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001299 connector->connector_id == option_connector)) {
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001300 if (create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001301 connector, x, y,
1302 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02001303 drmModeFreeConnector(connector);
1304 continue;
1305 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001306
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001307 x += container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001308 struct weston_output,
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001309 link)->current->width;
1310 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001311
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001312 drmModeFreeConnector(connector);
1313 }
1314
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001315 if (wl_list_empty(&ec->base.output_list)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001316 fprintf(stderr, "No currently active connector found.\n");
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001317 drmModeFreeResources(resources);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001318 return -1;
1319 }
1320
1321 drmModeFreeResources(resources);
1322
1323 return 0;
1324}
1325
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001326static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001327update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001328{
1329 drmModeConnector *connector;
1330 drmModeRes *resources;
1331 struct drm_output *output, *next;
1332 int x = 0, y = 0;
1333 int x_offset = 0, y_offset = 0;
1334 uint32_t connected = 0, disconnects = 0;
1335 int i;
1336
1337 resources = drmModeGetResources(ec->drm.fd);
1338 if (!resources) {
1339 fprintf(stderr, "drmModeGetResources failed\n");
1340 return;
1341 }
1342
1343 /* collect new connects */
1344 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001345 int connector_id = resources->connectors[i];
1346
1347 connector = drmModeGetConnector(ec->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01001348 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001349 continue;
1350
David Herrmann7551cff2011-12-08 17:05:43 +01001351 if (connector->connection != DRM_MODE_CONNECTED) {
1352 drmModeFreeConnector(connector);
1353 continue;
1354 }
1355
Benjamin Franzke117483d2011-08-30 11:38:26 +02001356 connected |= (1 << connector_id);
1357
1358 if (!(ec->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001359 struct weston_output *last =
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001360 container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001361 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001362
1363 /* XXX: not yet needed, we die with 0 outputs */
1364 if (!wl_list_empty(&ec->base.output_list))
Benjamin Franzke117483d2011-08-30 11:38:26 +02001365 x = last->x + last->current->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001366 else
1367 x = 0;
1368 y = 0;
1369 create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001370 connector, x, y,
1371 drm_device);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001372 printf("connector %d connected\n", connector_id);
1373
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001374 }
1375 drmModeFreeConnector(connector);
1376 }
1377 drmModeFreeResources(resources);
1378
1379 disconnects = ec->connector_allocator & ~connected;
1380 if (disconnects) {
1381 wl_list_for_each_safe(output, next, &ec->base.output_list,
1382 base.link) {
1383 if (x_offset != 0 || y_offset != 0) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001384 weston_output_move(&output->base,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001385 output->base.x - x_offset,
1386 output->base.y - y_offset);
1387 }
1388
1389 if (disconnects & (1 << output->connector_id)) {
1390 disconnects &= ~(1 << output->connector_id);
1391 printf("connector %d disconnected\n",
1392 output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001393 x_offset += output->base.current->width;
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001394 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001395 }
1396 }
1397 }
1398
1399 /* FIXME: handle zero outputs, without terminating */
1400 if (ec->connector_allocator == 0)
1401 wl_display_terminate(ec->base.wl_display);
1402}
1403
1404static int
David Herrmannd7488c22012-03-11 20:05:21 +01001405udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001406{
David Herrmannd7488c22012-03-11 20:05:21 +01001407 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01001408 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01001409
1410 sysnum = udev_device_get_sysnum(device);
1411 if (!sysnum || atoi(sysnum) != ec->drm.id)
1412 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001413
David Herrmann6ac52db2012-03-11 20:05:22 +01001414 val = udev_device_get_property_value(device, "HOTPLUG");
1415 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001416 return 0;
1417
David Herrmann6ac52db2012-03-11 20:05:22 +01001418 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001419}
1420
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001421static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001422udev_drm_event(int fd, uint32_t mask, void *data)
1423{
1424 struct drm_compositor *ec = data;
1425 struct udev_device *event;
1426
1427 event = udev_monitor_receive_device(ec->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001428
David Herrmannd7488c22012-03-11 20:05:21 +01001429 if (udev_event_is_hotplug(ec, event))
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001430 update_outputs(ec, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001431
1432 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001433
1434 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001435}
1436
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001437static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001438drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001439{
1440 struct drm_compositor *d = (struct drm_compositor *) ec;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001441 struct weston_input_device *input, *next;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001442
Jonas Ådahlc97af922012-03-28 22:36:09 +02001443 wl_list_for_each_safe(input, next, &ec->input_device_list, link)
1444 evdev_input_destroy(input);
1445
1446 wl_event_source_remove(d->udev_drm_source);
1447 wl_event_source_remove(d->drm_source);
1448
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001449 weston_compositor_shutdown(ec);
Jonas Ådahlc97af922012-03-28 22:36:09 +02001450
Matt Roper361d2ad2011-08-29 13:52:23 -07001451 gbm_device_destroy(d->gbm);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001452 destroy_sprites(d);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001453 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
1454 fprintf(stderr, "failed to drop master: %m\n");
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001455 tty_destroy(d->tty);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001456
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001457 free(d);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001458}
1459
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001460static void
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001461drm_compositor_set_modes(struct drm_compositor *compositor)
1462{
1463 struct drm_output *output;
1464 struct drm_mode *drm_mode;
1465 int ret;
1466
1467 wl_list_for_each(output, &compositor->base.output_list, base.link) {
1468 drm_mode = (struct drm_mode *) output->base.current;
1469 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001470 output->current_fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001471 &output->connector_id, 1,
1472 &drm_mode->mode_info);
1473 if (ret < 0) {
1474 fprintf(stderr,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001475 "failed to set mode %dx%d for output at %d,%d: %m\n",
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001476 drm_mode->base.width, drm_mode->base.height,
1477 output->base.x, output->base.y);
1478 }
1479 }
1480}
1481
1482static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001483vt_func(struct weston_compositor *compositor, int event)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001484{
1485 struct drm_compositor *ec = (struct drm_compositor *) compositor;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001486 struct weston_output *output;
1487 struct weston_input_device *input;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001488 struct drm_sprite *sprite;
1489 struct drm_output *drm_output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001490
1491 switch (event) {
1492 case TTY_ENTER_VT:
1493 compositor->focus = 1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001494 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001495 fprintf(stderr, "failed to set master: %m\n");
1496 wl_display_terminate(compositor->wl_display);
1497 }
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001498 compositor->state = ec->prev_state;
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001499 drm_compositor_set_modes(ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001500 weston_compositor_damage_all(compositor);
Benjamin Franzke78d3afe2012-04-09 18:14:58 +02001501 wl_list_for_each(input, &compositor->input_device_list, link) {
Tiago Vignatti6e2d5f12011-12-19 00:32:48 +02001502 evdev_add_devices(ec->udev, input);
Benjamin Franzke78d3afe2012-04-09 18:14:58 +02001503 evdev_enable_udev_monitor(ec->udev, input);
1504 }
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001505 break;
1506 case TTY_LEAVE_VT:
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04001507 wl_list_for_each(input, &compositor->input_device_list, link) {
1508 evdev_disable_udev_monitor(input);
1509 evdev_remove_devices(input);
1510 }
1511
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001512 compositor->focus = 0;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001513 ec->prev_state = compositor->state;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001514 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04001515
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001516 /* If we have a repaint scheduled (either from a
1517 * pending pageflip or the idle handler), make sure we
1518 * cancel that so we don't try to pageflip when we're
1519 * vt switched away. The SLEEPING state will prevent
1520 * further attemps at repainting. When we switch
1521 * back, we schedule a repaint, which will process
1522 * pending frame callbacks. */
1523
1524 wl_list_for_each(output, &ec->base.output_list, link) {
1525 output->repaint_needed = 0;
1526 drm_output_set_cursor(output, NULL);
1527 }
1528
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001529 drm_output = container_of(ec->base.output_list.next,
1530 struct drm_output, base.link);
1531
1532 wl_list_for_each(sprite, &ec->sprite_list, link)
1533 drmModeSetPlane(ec->drm.fd,
1534 sprite->plane_id,
1535 drm_output->crtc_id, 0, 0,
1536 0, 0, 0, 0, 0, 0, 0, 0);
1537
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001538 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001539 fprintf(stderr, "failed to drop master: %m\n");
1540
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001541 break;
1542 };
1543}
1544
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001545static const char default_seat[] = "seat0";
1546
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001547static struct weston_compositor *
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001548drm_compositor_create(struct wl_display *display,
Tiago Vignattifaee8012011-09-01 15:58:17 -04001549 int connector, const char *seat, int tty)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001550{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001551 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001552 struct udev_enumerate *e;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001553 struct udev_list_entry *entry;
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001554 struct udev_device *device, *drm_device;
1555 const char *path, *device_seat;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001556 struct wl_event_loop *loop;
1557
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001558 ec = malloc(sizeof *ec);
1559 if (ec == NULL)
1560 return NULL;
1561
1562 memset(ec, 0, sizeof *ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001563 ec->udev = udev_new();
1564 if (ec->udev == NULL) {
1565 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001566 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001567 }
1568
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05001569 ec->base.wl_display = display;
1570 ec->tty = tty_create(&ec->base, vt_func, tty);
1571 if (!ec->tty) {
1572 fprintf(stderr, "failed to initialize tty\n");
1573 free(ec);
1574 return NULL;
1575 }
1576
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001577 e = udev_enumerate_new(ec->udev);
1578 udev_enumerate_add_match_subsystem(e, "drm");
Benjamin Franzkea764ee52011-10-07 08:23:22 +02001579 udev_enumerate_add_match_sysname(e, "card[0-9]*");
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001580
Benjamin Franzke117483d2011-08-30 11:38:26 +02001581 udev_enumerate_scan_devices(e);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001582 drm_device = NULL;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001583 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001584 path = udev_list_entry_get_name(entry);
1585 device = udev_device_new_from_syspath(ec->udev, path);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001586 device_seat =
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001587 udev_device_get_property_value(device, "ID_SEAT");
1588 if (!device_seat)
1589 device_seat = default_seat;
1590 if (strcmp(device_seat, seat) == 0) {
1591 drm_device = device;
1592 break;
1593 }
1594 udev_device_unref(device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001595 }
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001596
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001597 if (drm_device == NULL) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001598 fprintf(stderr, "no drm device found\n");
1599 return NULL;
1600 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001601
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001602 if (init_egl(ec, drm_device) < 0) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001603 fprintf(stderr, "failed to initialize egl\n");
1604 return NULL;
1605 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001606
1607 ec->base.destroy = drm_destroy;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001608
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001609 ec->base.focus = 1;
1610
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001611 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001612
Benjamin Franzked59eb1c2011-04-29 22:14:54 +02001613 /* Can't init base class until we have a current egl context */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001614 if (weston_compositor_init(&ec->base, display) < 0)
Benjamin Franzked59eb1c2011-04-29 22:14:54 +02001615 return NULL;
1616
Jesse Barnes58ef3792012-02-23 09:45:49 -05001617 wl_list_init(&ec->sprite_list);
1618 create_sprites(ec);
1619
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001620 if (create_outputs(ec, connector, drm_device) < 0) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001621 fprintf(stderr, "failed to create output for %s\n", path);
1622 return NULL;
1623 }
1624
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001625 udev_device_unref(drm_device);
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02001626 udev_enumerate_unref(e);
1627 path = NULL;
1628
Tiago Vignattice03ec32011-12-19 01:14:03 +02001629 evdev_input_create(&ec->base, ec->udev, seat);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001630
1631 loop = wl_display_get_event_loop(ec->base.wl_display);
1632 ec->drm_source =
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001633 wl_event_loop_add_fd(loop, ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001634 WL_EVENT_READABLE, on_drm_input, ec);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001635
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001636 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
1637 if (ec->udev_monitor == NULL) {
1638 fprintf(stderr, "failed to intialize udev monitor\n");
1639 return NULL;
1640 }
1641 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
1642 "drm", NULL);
1643 ec->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02001644 wl_event_loop_add_fd(loop,
1645 udev_monitor_get_fd(ec->udev_monitor),
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001646 WL_EVENT_READABLE, udev_drm_event, ec);
1647
1648 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
1649 fprintf(stderr, "failed to enable udev-monitor receiving\n");
1650 return NULL;
1651 }
1652
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001653 return &ec->base;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001654}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001655
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001656WL_EXPORT struct weston_compositor *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001657backend_init(struct wl_display *display, int argc, char *argv[])
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001658{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001659 int connector = 0, tty = 0;
1660 const char *seat = default_seat;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001661
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001662 const struct weston_option drm_options[] = {
1663 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
1664 { WESTON_OPTION_STRING, "seat", 0, &seat },
1665 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
1666 };
Benjamin Franzke117483d2011-08-30 11:38:26 +02001667
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001668 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001669
Tiago Vignattifaee8012011-09-01 15:58:17 -04001670 return drm_compositor_create(display, connector, seat, tty);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001671}