blob: 8ea496595cd2dd576d4002b31544a5322d55f8e7 [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 <stdlib.h>
28#include <string.h>
29#include <fcntl.h>
30#include <unistd.h>
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -040031#include <linux/input.h>
Ander Conselvan de Oliveirafd1f4c62012-06-26 17:09:14 +030032#include <assert.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040033
Benjamin Franzkec649a922011-03-02 11:56:04 +010034#include <xf86drm.h>
35#include <xf86drmMode.h>
Jesse Barnes58ef3792012-02-23 09:45:49 -050036#include <drm_fourcc.h>
Benjamin Franzkec649a922011-03-02 11:56:04 +010037
Benjamin Franzke060cf802011-04-30 09:32:11 +020038#include <gbm.h>
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +020039#include <libbacklight.h>
Benjamin Franzke060cf802011-04-30 09:32:11 +020040
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040041#include "compositor.h"
Tiago Vignattice03ec32011-12-19 01:14:03 +020042#include "evdev.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010043#include "launcher-util.h"
Martin Minarik6d118362012-06-07 18:01:59 +020044#include "log.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040045
Kristian Høgsberg061c4252012-06-28 11:28:15 -040046static int option_current_mode = 0;
47
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -040048enum {
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -040049 WESTON_PLANE_DRM_CURSOR = 0x100,
50 WESTON_PLANE_DRM_PLANE = 0x101,
51 WESTON_PLANE_DRM_FB = 0x102
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -040052};
53
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040054struct drm_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050055 struct weston_compositor base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040056
57 struct udev *udev;
58 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040059
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010060 struct udev_monitor *udev_monitor;
61 struct wl_event_source *udev_drm_source;
62
Benjamin Franzke2af7f102011-03-02 11:14:59 +010063 struct {
David Herrmannd7488c22012-03-11 20:05:21 +010064 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010065 int fd;
66 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +020067 struct gbm_device *gbm;
Jesse Barnes58ef3792012-02-23 09:45:49 -050068 uint32_t *crtcs;
69 int num_crtcs;
Marty Jack13d9db22011-02-09 19:01:42 -050070 uint32_t crtc_allocator;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010071 uint32_t connector_allocator;
Kristian Høgsberge4762a62011-01-14 14:59:13 -050072 struct tty *tty;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +020073
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -040074 struct gbm_surface *dummy_surface;
75 EGLSurface dummy_egl_surface;
76
Jesse Barnes58ef3792012-02-23 09:45:49 -050077 struct wl_list sprite_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -050078 int sprites_are_broken;
Jesse Barnes58ef3792012-02-23 09:45:49 -050079
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +020080 uint32_t prev_state;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040081};
82
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040083struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050084 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040085 drmModeModeInfo mode_info;
86};
87
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +030088struct drm_output;
89
90struct drm_fb {
91 struct gbm_bo *bo;
92 struct drm_output *output;
93 uint32_t fb_id;
94 int is_client_buffer;
95 struct wl_buffer *buffer;
96 struct wl_listener buffer_destroy_listener;
97};
98
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040099struct drm_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500100 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400101
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400102 uint32_t crtc_id;
103 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700104 drmModeCrtcPtr original_crtc;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200105
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300106 int vblank_pending;
107 int page_flip_pending;
108
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400109 struct gbm_surface *surface;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400110 struct gbm_bo *cursor_bo[2];
Kristian Høgsberg24e42752012-07-18 12:08:37 -0400111 int current_cursor, cursor_free, cursor_x, cursor_y;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400112 EGLSurface egl_surface;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300113 struct drm_fb *current, *next;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200114 struct backlight *backlight;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400115};
116
Jesse Barnes58ef3792012-02-23 09:45:49 -0500117/*
118 * An output has a primary display plane plus zero or more sprites for
119 * blending display contents.
120 */
121struct drm_sprite {
122 struct wl_list link;
123
124 uint32_t fb_id;
125 uint32_t pending_fb_id;
126 struct weston_surface *surface;
127 struct weston_surface *pending_surface;
128
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300129 struct drm_output *output;
130
Jesse Barnes58ef3792012-02-23 09:45:49 -0500131 struct drm_compositor *compositor;
132
133 struct wl_listener destroy_listener;
134 struct wl_listener pending_destroy_listener;
135
136 uint32_t possible_crtcs;
137 uint32_t plane_id;
138 uint32_t count_formats;
139
140 int32_t src_x, src_y;
141 uint32_t src_w, src_h;
142 uint32_t dest_x, dest_y;
143 uint32_t dest_w, dest_h;
144
145 uint32_t formats[];
146};
147
148static int
Jesse Barnes58ef3792012-02-23 09:45:49 -0500149drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
150{
151 struct weston_compositor *ec = output_base->compositor;
152 struct drm_compositor *c =(struct drm_compositor *) ec;
153 struct drm_output *output = (struct drm_output *) output_base;
154 int crtc;
155
156 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
157 if (c->crtcs[crtc] != output->crtc_id)
158 continue;
159
160 if (supported & (1 << crtc))
161 return -1;
162 }
163
164 return 0;
165}
166
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300167static void
168drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
169{
170 struct drm_fb *fb = data;
171 struct gbm_device *gbm = gbm_bo_get_device(bo);
172
173 if (fb->fb_id)
174 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
175
176 if (fb->buffer) {
177 weston_buffer_post_release(fb->buffer);
178 wl_list_remove(&fb->buffer_destroy_listener.link);
179 }
180
181 free(data);
182}
183
184static struct drm_fb *
185drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_output *output)
186{
187 struct drm_fb *fb = gbm_bo_get_user_data(bo);
188 struct drm_compositor *compositor =
189 (struct drm_compositor *) output->base.compositor;
190 uint32_t width, height, stride, handle;
191 int ret;
192
193 if (fb)
194 return fb;
195
196 fb = malloc(sizeof *fb);
197
198 fb->bo = bo;
199 fb->output = output;
200 fb->is_client_buffer = 0;
201 fb->buffer = NULL;
202
203 width = gbm_bo_get_width(bo);
204 height = gbm_bo_get_height(bo);
Kristian Høgsberg270a7cb2012-07-16 16:44:16 -0400205 stride = gbm_bo_get_stride(bo);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300206 handle = gbm_bo_get_handle(bo).u32;
207
208 ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32,
209 stride, handle, &fb->fb_id);
210 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200211 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300212 free(fb);
213 return NULL;
214 }
215
216 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
217
218 return fb;
219}
220
221static void
222fb_handle_buffer_destroy(struct wl_listener *listener, void *data)
223{
224 struct drm_fb *fb = container_of(listener, struct drm_fb,
225 buffer_destroy_listener);
226
227 fb->buffer = NULL;
228
229 if (fb == fb->output->next ||
230 (fb == fb->output->current && !fb->output->next))
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400231 weston_output_schedule_repaint(&fb->output->base);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300232}
233
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500234static int
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -0400235drm_output_prepare_scanout_surface(struct weston_output *_output,
236 struct weston_surface *es)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500237{
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -0400238 struct drm_output *output = (struct drm_output *) _output;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500239 struct drm_compositor *c =
240 (struct drm_compositor *) output->base.compositor;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300241 struct gbm_bo *bo;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500242
Kristian Høgsberg101cb652012-02-17 10:45:16 -0500243 if (es->geometry.x != output->base.x ||
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200244 es->geometry.y != output->base.y ||
Pekka Paalanen60921e52012-01-25 15:55:43 +0200245 es->geometry.width != output->base.current->width ||
246 es->geometry.height != output->base.current->height ||
Pekka Paalanenf1f5b362012-01-25 14:33:33 +0200247 es->transform.enabled ||
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -0400248 es->buffer == NULL)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500249 return -1;
250
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -0400251 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
252 es->buffer, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500253
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300254 /* Need to verify output->region contained in surface opaque
255 * region. Or maybe just that format doesn't have alpha.
256 * For now, scanout only if format is XRGB8888. */
257 if (gbm_bo_get_format(bo) != GBM_FORMAT_XRGB8888) {
258 gbm_bo_destroy(bo);
259 return -1;
260 }
261
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300262 output->next = drm_fb_get_from_bo(bo, output);
263 if (!output->next) {
264 gbm_bo_destroy(bo);
265 return -1;
266 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500267
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300268 output->next->is_client_buffer = 1;
269 output->next->buffer = es->buffer;
270 output->next->buffer->busy_count++;
271 output->next->buffer_destroy_listener.notify = fb_handle_buffer_destroy;
272
273 wl_signal_add(&output->next->buffer->resource.destroy_signal,
274 &output->next->buffer_destroy_listener);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500275
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -0400276 es->plane = WESTON_PLANE_DRM_FB;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500277
278 return 0;
279}
280
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500281static void
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400282drm_output_render(struct drm_output *output, pixman_region32_t *damage)
283{
284 struct drm_compositor *compositor =
285 (struct drm_compositor *) output->base.compositor;
286 struct weston_surface *surface;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300287 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400288
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400289 if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
290 output->egl_surface,
291 compositor->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200292 weston_log("failed to make current\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400293 return;
294 }
295
296 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -0400297 if (surface->plane == WESTON_PLANE_PRIMARY)
298 weston_surface_draw(surface, &output->base, damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400299
Kristian Høgsberge0f832b2012-06-20 00:13:18 -0400300 wl_signal_emit(&output->base.frame_signal, output);
Scott Moreau062be7e2012-04-20 13:37:33 -0600301
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400302 eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300303 bo = gbm_surface_lock_front_buffer(output->surface);
304 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200305 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400306 return;
307 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300308
309 output->next = drm_fb_get_from_bo(bo, output);
310 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200311 weston_log("failed to get drm_fb for bo\n");
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300312 gbm_surface_release_buffer(output->surface, bo);
313 return;
314 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400315}
316
317static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500318drm_output_repaint(struct weston_output *output_base,
319 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100320{
321 struct drm_output *output = (struct drm_output *) output_base;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500322 struct drm_compositor *compositor =
323 (struct drm_compositor *) output->base.compositor;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500324 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400325 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500326 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100327
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300328 if (!output->next)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400329 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300330 if (!output->next)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400331 return;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100332
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400333 mode = container_of(output->base.current, struct drm_mode, base);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300334 if (!output->current) {
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400335 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300336 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400337 &output->connector_id, 1,
338 &mode->mode_info);
339 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200340 weston_log("set mode failed: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400341 return;
342 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200343 }
344
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500345 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300346 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500347 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200348 weston_log("queueing pageflip failed: %m\n");
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500349 return;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500350 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100351
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300352 output->page_flip_pending = 1;
353
Jesse Barnes58ef3792012-02-23 09:45:49 -0500354 /*
355 * Now, update all the sprite surfaces
356 */
357 wl_list_for_each(s, &compositor->sprite_list, link) {
358 uint32_t flags = 0;
359 drmVBlank vbl = {
360 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
361 .request.sequence = 1,
362 };
363
364 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
365 continue;
366
367 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
368 output->crtc_id, s->pending_fb_id, flags,
369 s->dest_x, s->dest_y,
370 s->dest_w, s->dest_h,
371 s->src_x, s->src_y,
372 s->src_w, s->src_h);
373 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200374 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500375 ret, strerror(errno));
376
377 /*
378 * Queue a vblank signal so we know when the surface
379 * becomes active on the display or has been replaced.
380 */
381 vbl.request.signal = (unsigned long)s;
382 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
383 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200384 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500385 ret, strerror(errno));
386 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300387
388 s->output = output;
389 output->vblank_pending = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500390 }
391
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500392 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400393}
394
395static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500396vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
397 void *data)
398{
399 struct drm_sprite *s = (struct drm_sprite *)data;
400 struct drm_compositor *c = s->compositor;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300401 struct drm_output *output = s->output;
402 uint32_t msecs;
403
404 output->vblank_pending = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500405
406 if (s->surface) {
407 weston_buffer_post_release(s->surface->buffer);
408 wl_list_remove(&s->destroy_listener.link);
409 s->surface = NULL;
410 drmModeRmFB(c->drm.fd, s->fb_id);
411 s->fb_id = 0;
412 }
413
414 if (s->pending_surface) {
415 wl_list_remove(&s->pending_destroy_listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400416 wl_signal_add(&s->pending_surface->buffer->resource.destroy_signal,
417 &s->destroy_listener);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500418 s->surface = s->pending_surface;
419 s->pending_surface = NULL;
420 s->fb_id = s->pending_fb_id;
421 s->pending_fb_id = 0;
422 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300423
424 if (!output->page_flip_pending) {
425 msecs = sec * 1000 + usec / 1000;
426 weston_output_finish_frame(&output->base, msecs);
427 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500428}
429
430static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400431page_flip_handler(int fd, unsigned int frame,
432 unsigned int sec, unsigned int usec, void *data)
433{
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200434 struct drm_output *output = (struct drm_output *) data;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400435 uint32_t msecs;
436
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300437 output->page_flip_pending = 0;
438
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300439 if (output->current) {
440 if (output->current->is_client_buffer)
441 gbm_bo_destroy(output->current->bo);
442 else
443 gbm_surface_release_buffer(output->surface,
444 output->current->bo);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200445 }
446
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300447 output->current = output->next;
448 output->next = NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400449
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300450 if (!output->vblank_pending) {
451 msecs = sec * 1000 + usec / 1000;
452 weston_output_finish_frame(&output->base, msecs);
453 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200454}
455
456static int
Jesse Barnes58ef3792012-02-23 09:45:49 -0500457drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
458{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400459 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500460
461 for (i = 0; i < s->count_formats; i++)
462 if (s->formats[i] == format)
463 return 1;
464
465 return 0;
466}
467
468static int
469drm_surface_transform_supported(struct weston_surface *es)
470{
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -0400471 struct weston_matrix *matrix = &es->transform.matrix;
472 int i;
473
474 if (!es->transform.enabled)
475 return 1;
476
477 for (i = 0; i < 16; i++) {
478 switch (i) {
479 case 10:
480 case 15:
481 if (matrix->d[i] != 1.0)
482 return 0;
483 break;
484 case 0:
485 case 5:
486 case 12:
487 case 13:
488 break;
489 default:
490 if (matrix->d[i] != 0.0)
491 return 0;
492 break;
493 }
494 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500495
496 return 1;
497}
498
Jesse Barnes58ef3792012-02-23 09:45:49 -0500499static void
500drm_disable_unused_sprites(struct weston_output *output_base)
501{
502 struct weston_compositor *ec = output_base->compositor;
503 struct drm_compositor *c =(struct drm_compositor *) ec;
504 struct drm_output *output = (struct drm_output *) output_base;
505 struct drm_sprite *s;
506 int ret;
507
508 wl_list_for_each(s, &c->sprite_list, link) {
509 if (s->pending_fb_id)
510 continue;
511
512 ret = drmModeSetPlane(c->drm.fd, s->plane_id,
513 output->crtc_id, 0, 0,
514 0, 0, 0, 0, 0, 0, 0, 0);
515 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200516 weston_log("failed to disable plane: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500517 ret, strerror(errno));
518 drmModeRmFB(c->drm.fd, s->fb_id);
Ander Conselvan de Oliveirafd1f4c62012-06-26 17:09:14 +0300519
520 if (s->surface) {
521 s->surface = NULL;
522 wl_list_remove(&s->destroy_listener.link);
523 }
524
525 assert(!s->pending_surface);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500526 s->fb_id = 0;
527 s->pending_fb_id = 0;
528 }
529}
530
531/*
532 * This function must take care to damage any previously assigned surface
533 * if the sprite ends up binding to a different surface than in the
534 * previous frame.
535 */
536static int
537drm_output_prepare_overlay_surface(struct weston_output *output_base,
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -0400538 struct weston_surface *es)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500539{
540 struct weston_compositor *ec = output_base->compositor;
541 struct drm_compositor *c =(struct drm_compositor *) ec;
542 struct drm_sprite *s;
543 int found = 0;
544 EGLint handle, stride;
545 struct gbm_bo *bo;
546 uint32_t fb_id = 0;
547 uint32_t handles[4], pitches[4], offsets[4];
548 int ret = 0;
549 pixman_region32_t dest_rect, src_rect;
550 pixman_box32_t *box;
551 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -0400552 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500553
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500554 if (c->sprites_are_broken)
555 return -1;
556
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +0300557 if (es->output_mask != (1u << output_base->id))
558 return -1;
559
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -0400560 if (es->buffer == NULL)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500561 return -1;
562
563 if (!drm_surface_transform_supported(es))
564 return -1;
565
Jesse Barnes58ef3792012-02-23 09:45:49 -0500566 wl_list_for_each(s, &c->sprite_list, link) {
567 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
568 continue;
569
570 if (!s->pending_fb_id) {
571 found = 1;
572 break;
573 }
574 }
575
576 /* No sprites available */
577 if (!found)
578 return -1;
579
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -0400580 bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
581 es->buffer, GBM_BO_USE_SCANOUT);
582 if (!bo)
583 return -1;
584
Jesse Barnes58ef3792012-02-23 09:45:49 -0500585 format = gbm_bo_get_format(bo);
586 handle = gbm_bo_get_handle(bo).s32;
Kristian Høgsberg270a7cb2012-07-16 16:44:16 -0400587 stride = gbm_bo_get_stride(bo);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500588
589 gbm_bo_destroy(bo);
590
591 if (!drm_surface_format_supported(s, format))
592 return -1;
593
594 if (!handle)
595 return -1;
596
597 handles[0] = handle;
598 pitches[0] = stride;
599 offsets[0] = 0;
600
601 ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height,
602 format, handles, pitches, offsets,
603 &fb_id, 0);
604 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200605 weston_log("addfb2 failed: %d\n", ret);
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500606 c->sprites_are_broken = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500607 return -1;
608 }
609
Jesse Barnes58ef3792012-02-23 09:45:49 -0500610 s->pending_fb_id = fb_id;
611 s->pending_surface = es;
612 es->buffer->busy_count++;
613
614 /*
615 * Calculate the source & dest rects properly based on actual
616 * postion (note the caller has called weston_surface_update_transform()
617 * for us already).
618 */
619 pixman_region32_init(&dest_rect);
620 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
621 &output_base->region);
622 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
623 box = pixman_region32_extents(&dest_rect);
624 s->dest_x = box->x1;
625 s->dest_y = box->y1;
626 s->dest_w = box->x2 - box->x1;
627 s->dest_h = box->y2 - box->y1;
628 pixman_region32_fini(&dest_rect);
629
630 pixman_region32_init(&src_rect);
631 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
632 &output_base->region);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500633 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -0400634
635 weston_surface_from_global_fixed(es,
636 wl_fixed_from_int(box->x1),
637 wl_fixed_from_int(box->y1),
638 &sx1, &sy1);
639 weston_surface_from_global_fixed(es,
640 wl_fixed_from_int(box->x2),
641 wl_fixed_from_int(box->y2),
642 &sx2, &sy2);
643
644 if (sx1 < 0)
645 sx1 = 0;
646 if (sy1 < 0)
647 sy1 = 0;
648 if (sx2 > wl_fixed_from_int(es->geometry.width))
649 sx2 = wl_fixed_from_int(es->geometry.width);
650 if (sy2 > wl_fixed_from_int(es->geometry.height))
651 sy2 = wl_fixed_from_int(es->geometry.height);
652
653 s->src_x = sx1 << 8;
654 s->src_y = sy1 << 8;
655 s->src_w = (sx2 - sx1) << 8;
656 s->src_h = (sy2 - sy1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500657 pixman_region32_fini(&src_rect);
658
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -0400659 es->plane = WESTON_PLANE_DRM_PLANE;
660
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400661 wl_signal_add(&es->buffer->resource.destroy_signal,
662 &s->pending_destroy_listener);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -0400663
Jesse Barnes58ef3792012-02-23 09:45:49 -0500664 return 0;
665}
666
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500667static void
Kristian Høgsberga6edab32012-07-14 01:06:28 -0400668drm_output_set_cursor(struct weston_output *output_base,
Kristian Høgsberg4901f6c2012-07-14 01:24:58 -0400669 struct weston_surface *es)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500670{
Kristian Høgsberga6edab32012-07-14 01:06:28 -0400671 struct drm_output *output = (struct drm_output *) output_base;
672 struct drm_compositor *c =
673 (struct drm_compositor *) output->base.compositor;
674 EGLint handle, stride;
675 struct gbm_bo *bo;
676 uint32_t buf[64 * 64];
677 unsigned char *s;
Kristian Høgsberg24e42752012-07-18 12:08:37 -0400678 int i, x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500679
Kristian Høgsberg4901f6c2012-07-14 01:24:58 -0400680 if (!output->cursor_free)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500681 return;
Kristian Høgsberg4901f6c2012-07-14 01:24:58 -0400682 if (es->output_mask != (1u << output_base->id))
683 return;
684 if (es->buffer == NULL || !wl_buffer_is_shm(es->buffer) ||
685 es->geometry.width > 64 || es->geometry.height > 64)
Kristian Høgsberga6edab32012-07-14 01:06:28 -0400686 return;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500687
Kristian Høgsberg1f5de352012-07-18 12:09:58 -0400688 if (es->buffer && pixman_region32_not_empty(&es->damage)) {
689 output->current_cursor ^= 1;
690 bo = output->cursor_bo[output->current_cursor];
691 memset(buf, 0, sizeof buf);
692 stride = wl_shm_buffer_get_stride(es->buffer);
693 s = wl_shm_buffer_get_data(es->buffer);
694 for (i = 0; i < es->geometry.height; i++)
695 memcpy(buf + i * 64, s + i * stride,
696 es->geometry.width * 4);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500697
Kristian Høgsberg1f5de352012-07-18 12:09:58 -0400698 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
699 return;
Kristian Høgsberga6edab32012-07-14 01:06:28 -0400700
Kristian Høgsberg1f5de352012-07-18 12:09:58 -0400701 handle = gbm_bo_get_handle(bo).s32;
702 if (drmModeSetCursor(c->drm.fd,
703 output->crtc_id, handle, 64, 64)) {
704 weston_log("failed to set cursor: %n\n");
705 return;
706 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -0400707 }
708
Kristian Høgsberg24e42752012-07-18 12:08:37 -0400709 x = es->geometry.x - output->base.x;
710 y = es->geometry.y - output->base.y;
711 if (output->cursor_x != x || output->cursor_y != y) {
712 if (drmModeMoveCursor(c->drm.fd, output->crtc_id, x, y)) {
713 weston_log("failed to move cursor: %m\n");
714 return;
715 }
716 output->cursor_x = x;
717 output->cursor_y = y;
Kristian Høgsberga6edab32012-07-14 01:06:28 -0400718 }
719
Kristian Høgsberg4901f6c2012-07-14 01:24:58 -0400720 es->plane = WESTON_PLANE_DRM_CURSOR;
721 output->cursor_free = 0;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500722}
723
Jesse Barnes58ef3792012-02-23 09:45:49 -0500724static void
725drm_assign_planes(struct weston_output *output)
726{
Kristian Høgsberga6edab32012-07-14 01:06:28 -0400727 struct drm_compositor *c =
728 (struct drm_compositor *) output->compositor;
729 struct drm_output *drm_output = (struct drm_output *) output;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400730 struct weston_surface *es, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500731 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -0400732 int prev_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500733
734 /*
735 * Find a surface for each sprite in the output using some heuristics:
736 * 1) size
737 * 2) frequency of update
738 * 3) opacity (though some hw might support alpha blending)
739 * 4) clipping (this can be fixed with color keys)
740 *
741 * The idea is to save on blitting since this should save power.
742 * If we can get a large video surface on the sprite for example,
743 * the main display surface may not need to update at all, and
744 * the client buffer can be used directly for the sprite surface
745 * as we do for flipping full screen surfaces.
746 */
747 pixman_region32_init(&overlap);
Kristian Høgsberg4901f6c2012-07-14 01:24:58 -0400748 drm_output->cursor_free = 1;
Kristian Høgsberga6edab32012-07-14 01:06:28 -0400749 wl_list_for_each_safe(es, next, &c->base.surface_list, link) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500750 pixman_region32_init(&surface_overlap);
751 pixman_region32_intersect(&surface_overlap, &overlap,
752 &es->transform.boundingbox);
753
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -0400754 prev_plane = es->plane;
755 es->plane = WESTON_PLANE_PRIMARY;
756 if (pixman_region32_not_empty(&surface_overlap))
757 goto bail;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500758
Kristian Høgsberg4901f6c2012-07-14 01:24:58 -0400759 if (es->plane == WESTON_PLANE_PRIMARY)
760 drm_output_set_cursor(output, es);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -0400761 if (es->plane == WESTON_PLANE_PRIMARY)
762 drm_output_prepare_scanout_surface(output, es);
763 if (es->plane == WESTON_PLANE_PRIMARY)
764 drm_output_prepare_overlay_surface(output, es);
765
766 bail:
767 if (es->plane == WESTON_PLANE_PRIMARY)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500768 pixman_region32_union(&overlap, &overlap,
769 &es->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -0400770
771 if (prev_plane != WESTON_PLANE_PRIMARY &&
772 es->plane == WESTON_PLANE_PRIMARY)
773 weston_surface_damage(es);
774 else if (prev_plane == WESTON_PLANE_PRIMARY &&
775 es->plane != WESTON_PLANE_PRIMARY)
776 weston_surface_damage_below(es);
777
Jesse Barnes58ef3792012-02-23 09:45:49 -0500778 pixman_region32_fini(&surface_overlap);
779 }
780 pixman_region32_fini(&overlap);
781
Kristian Høgsberg4901f6c2012-07-14 01:24:58 -0400782 if (drm_output->cursor_free)
Kristian Høgsberga6edab32012-07-14 01:06:28 -0400783 drmModeSetCursor(c->drm.fd, drm_output->crtc_id, 0, 0, 0);
Kristian Høgsbergf2735ea2012-06-20 23:03:53 -0400784
Jesse Barnes58ef3792012-02-23 09:45:49 -0500785 drm_disable_unused_sprites(output);
786}
787
Matt Roper361d2ad2011-08-29 13:52:23 -0700788static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500789drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700790{
791 struct drm_output *output = (struct drm_output *) output_base;
792 struct drm_compositor *c =
Benjamin Franzke117483d2011-08-30 11:38:26 +0200793 (struct drm_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700794 drmModeCrtcPtr origcrtc = output->original_crtc;
Matt Roper361d2ad2011-08-29 13:52:23 -0700795
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200796 if (output->backlight)
797 backlight_destroy(output->backlight);
798
Matt Roper361d2ad2011-08-29 13:52:23 -0700799 /* Turn off hardware cursor */
Kristian Høgsberga6edab32012-07-14 01:06:28 -0400800 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
Matt Roper361d2ad2011-08-29 13:52:23 -0700801
802 /* Restore original CRTC state */
803 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +0200804 origcrtc->x, origcrtc->y,
805 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -0700806 drmModeFreeCrtc(origcrtc);
807
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200808 c->crtc_allocator &= ~(1 << output->crtc_id);
809 c->connector_allocator &= ~(1 << output->connector_id);
810
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400811 eglDestroySurface(c->base.egl_display, output->egl_surface);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400812 gbm_surface_destroy(output->surface);
813
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500814 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +0200815 wl_list_remove(&output->base.link);
816
Matt Roper361d2ad2011-08-29 13:52:23 -0700817 free(output);
818}
819
Alex Wub7b8bda2012-04-17 17:20:48 +0800820static struct drm_mode *
821choose_mode (struct drm_output *output, struct weston_mode *target_mode)
822{
823 struct drm_mode *tmp_mode = NULL, *mode;
824
825 if (output->base.current->width == target_mode->width &&
826 output->base.current->height == target_mode->height &&
827 (output->base.current->refresh == target_mode->refresh ||
828 target_mode->refresh == 0))
829 return (struct drm_mode *)output->base.current;
830
831 wl_list_for_each(mode, &output->base.mode_list, base.link) {
832 if (mode->mode_info.hdisplay == target_mode->width &&
833 mode->mode_info.vdisplay == target_mode->height) {
834 if (mode->mode_info.vrefresh == target_mode->refresh ||
835 target_mode->refresh == 0) {
836 return mode;
837 } else if (!tmp_mode)
838 tmp_mode = mode;
839 }
840 }
841
842 return tmp_mode;
843}
844
845static int
846drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
847{
848 struct drm_output *output;
849 struct drm_mode *drm_mode;
850 int ret;
851 struct drm_compositor *ec;
852 struct gbm_surface *surface;
853 EGLSurface egl_surface;
854
855 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200856 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800857 return -1;
858 }
859
860 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200861 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800862 return -1;
863 }
864
865 ec = (struct drm_compositor *)output_base->compositor;
866 output = (struct drm_output *)output_base;
867 drm_mode = choose_mode (output, mode);
868
869 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +0200870 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +0800871 return -1;
872 } else if (&drm_mode->base == output->base.current) {
873 return 0;
874 } else if (drm_mode->base.width == output->base.current->width &&
875 drm_mode->base.height == output->base.current->height) {
876 /* only change refresh value */
877 ret = drmModeSetCrtc(ec->drm.fd,
878 output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300879 output->current->fb_id, 0, 0,
Alex Wub7b8bda2012-04-17 17:20:48 +0800880 &output->connector_id, 1, &drm_mode->mode_info);
881
882 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200883 weston_log("failed to set mode (%dx%d) %u Hz\n",
Alex Wub7b8bda2012-04-17 17:20:48 +0800884 drm_mode->base.width,
885 drm_mode->base.height,
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400886 drm_mode->base.refresh / 1000);
Alex Wub7b8bda2012-04-17 17:20:48 +0800887 ret = -1;
888 } else {
889 output->base.current->flags = 0;
890 output->base.current = &drm_mode->base;
891 drm_mode->base.flags =
892 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
893 ret = 0;
894 }
895
896 return ret;
897 }
898
899 drm_mode->base.flags =
900 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
901
902 surface = gbm_surface_create(ec->gbm,
903 drm_mode->base.width,
904 drm_mode->base.height,
905 GBM_FORMAT_XRGB8888,
906 GBM_BO_USE_SCANOUT |
907 GBM_BO_USE_RENDERING);
908 if (!surface) {
Martin Minarik6d118362012-06-07 18:01:59 +0200909 weston_log("failed to create gbm surface\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800910 return -1;
911 }
912
913 egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400914 eglCreateWindowSurface(ec->base.egl_display,
915 ec->base.egl_config,
Alex Wub7b8bda2012-04-17 17:20:48 +0800916 surface, NULL);
917
918 if (egl_surface == EGL_NO_SURFACE) {
Martin Minarik6d118362012-06-07 18:01:59 +0200919 weston_log("failed to create egl surface\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800920 goto err;
921 }
922
923 ret = drmModeSetCrtc(ec->drm.fd,
924 output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300925 output->current->fb_id, 0, 0,
Alex Wub7b8bda2012-04-17 17:20:48 +0800926 &output->connector_id, 1, &drm_mode->mode_info);
927 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200928 weston_log("failed to set mode\n");
Alex Wub7b8bda2012-04-17 17:20:48 +0800929 goto err;
930 }
931
932 /* reset rendering stuff. */
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300933 if (output->current) {
934 if (output->current->is_client_buffer)
935 gbm_bo_destroy(output->current->bo);
936 else
937 gbm_surface_release_buffer(output->surface,
938 output->current->bo);
939 }
940 output->current = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +0800941
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300942 if (output->next) {
943 if (output->next->is_client_buffer)
944 gbm_bo_destroy(output->next->bo);
945 else
946 gbm_surface_release_buffer(output->surface,
947 output->next->bo);
948 }
949 output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +0800950
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400951 eglDestroySurface(ec->base.egl_display, output->egl_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +0800952 gbm_surface_destroy(output->surface);
953 output->egl_surface = egl_surface;
954 output->surface = surface;
955
956 /*update output*/
957 output->base.current = &drm_mode->base;
958 output->base.dirty = 1;
959 weston_output_move(&output->base, output->base.x, output->base.y);
960 return 0;
961
962err:
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400963 eglDestroySurface(ec->base.egl_display, egl_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +0800964 gbm_surface_destroy(surface);
965 return -1;
966}
967
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400968static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400969on_drm_input(int fd, uint32_t mask, void *data)
970{
971 drmEventContext evctx;
972
973 memset(&evctx, 0, sizeof evctx);
974 evctx.version = DRM_EVENT_CONTEXT_VERSION;
975 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500976 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400977 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400978
979 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400980}
981
982static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400983init_egl(struct drm_compositor *ec, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400984{
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400985 EGLint major, minor, n;
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400986 const char *filename, *sysnum;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400987 int fd;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400988 static const EGLint context_attribs[] = {
989 EGL_CONTEXT_CLIENT_VERSION, 2,
990 EGL_NONE
991 };
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400992
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400993 static const EGLint config_attribs[] = {
994 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
995 EGL_RED_SIZE, 1,
996 EGL_GREEN_SIZE, 1,
997 EGL_BLUE_SIZE, 1,
998 EGL_ALPHA_SIZE, 0,
999 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1000 EGL_NONE
1001 };
1002
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001003 sysnum = udev_device_get_sysnum(device);
1004 if (sysnum)
1005 ec->drm.id = atoi(sysnum);
1006 if (!sysnum || ec->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001007 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001008 return -1;
1009 }
1010
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001011 filename = udev_device_get_devnode(device);
David Herrmann63ff7062011-11-05 18:46:01 +01001012 fd = open(filename, O_RDWR | O_CLOEXEC);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001013 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001014 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001015 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001016 udev_device_get_devnode(device));
1017 return -1;
1018 }
1019
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001020 weston_log("using %s\n", filename);
1021
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001022 ec->drm.fd = fd;
Benjamin Franzke060cf802011-04-30 09:32:11 +02001023 ec->gbm = gbm_create_device(ec->drm.fd);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001024 ec->base.egl_display = eglGetDisplay(ec->gbm);
1025 if (ec->base.egl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001026 weston_log("failed to create display\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001027 return -1;
1028 }
1029
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001030 if (!eglInitialize(ec->base.egl_display, &major, &minor)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001031 weston_log("failed to initialize display\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001032 return -1;
1033 }
1034
Darxus55973f22010-11-22 21:24:39 -05001035 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001036 weston_log("failed to bind api EGL_OPENGL_ES_API\n");
Darxus55973f22010-11-22 21:24:39 -05001037 return -1;
1038 }
1039
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001040 if (!eglChooseConfig(ec->base.egl_display, config_attribs,
1041 &ec->base.egl_config, 1, &n) || n != 1) {
Martin Minarik6d118362012-06-07 18:01:59 +02001042 weston_log("failed to choose config: %d\n", n);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001043 return -1;
1044 }
1045
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001046 ec->base.egl_context =
1047 eglCreateContext(ec->base.egl_display, ec->base.egl_config,
1048 EGL_NO_CONTEXT, context_attribs);
1049 if (ec->base.egl_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001050 weston_log("failed to create context\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001051 return -1;
1052 }
1053
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001054 ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10,
1055 GBM_FORMAT_XRGB8888,
1056 GBM_BO_USE_RENDERING);
1057 if (!ec->dummy_surface) {
Martin Minarik6d118362012-06-07 18:01:59 +02001058 weston_log("failed to create dummy gbm surface\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001059 return -1;
1060 }
1061
1062 ec->dummy_egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001063 eglCreateWindowSurface(ec->base.egl_display,
1064 ec->base.egl_config,
1065 ec->dummy_surface,
1066 NULL);
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001067 if (ec->dummy_egl_surface == EGL_NO_SURFACE) {
Martin Minarik6d118362012-06-07 18:01:59 +02001068 weston_log("failed to create egl surface\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001069 return -1;
1070 }
1071
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001072 if (!eglMakeCurrent(ec->base.egl_display, ec->dummy_egl_surface,
1073 ec->dummy_egl_surface, ec->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001074 weston_log("failed to make context current\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001075 return -1;
1076 }
1077
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001078 return 0;
1079}
1080
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001081static int
1082drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1083{
1084 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001085 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001086
1087 mode = malloc(sizeof *mode);
1088 if (mode == NULL)
1089 return -1;
1090
1091 mode->base.flags = 0;
1092 mode->base.width = info->hdisplay;
1093 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001094
1095 /* Calculate higher precision (mHz) refresh rate */
1096 refresh = (info->clock * 1000000LL / info->htotal +
1097 info->vtotal / 2) / info->vtotal;
1098
1099 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1100 refresh *= 2;
1101 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1102 refresh /= 2;
1103 if (info->vscan > 1)
1104 refresh /= info->vscan;
1105
1106 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001107 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001108
1109 if (info->type & DRM_MODE_TYPE_PREFERRED)
1110 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1111
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001112 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1113
1114 return 0;
1115}
1116
1117static int
1118drm_subpixel_to_wayland(int drm_value)
1119{
1120 switch (drm_value) {
1121 default:
1122 case DRM_MODE_SUBPIXEL_UNKNOWN:
1123 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1124 case DRM_MODE_SUBPIXEL_NONE:
1125 return WL_OUTPUT_SUBPIXEL_NONE;
1126 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1127 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1128 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1129 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1130 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1131 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1132 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1133 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1134 }
1135}
1136
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001137static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001138sprite_handle_buffer_destroy(struct wl_listener *listener, void *data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001139{
1140 struct drm_sprite *sprite =
1141 container_of(listener, struct drm_sprite,
1142 destroy_listener);
Ander Conselvan de Oliveira01a57ed2012-06-26 17:09:15 +03001143 struct drm_compositor *compositor = sprite->compositor;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001144
1145 sprite->surface = NULL;
Ander Conselvan de Oliveira01a57ed2012-06-26 17:09:15 +03001146 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1147 sprite->fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001148}
1149
1150static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001151sprite_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001152{
1153 struct drm_sprite *sprite =
1154 container_of(listener, struct drm_sprite,
1155 pending_destroy_listener);
Ander Conselvan de Oliveira01a57ed2012-06-26 17:09:15 +03001156 struct drm_compositor *compositor = sprite->compositor;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001157
1158 sprite->pending_surface = NULL;
Ander Conselvan de Oliveira01a57ed2012-06-26 17:09:15 +03001159 drmModeRmFB(compositor->drm.fd, sprite->pending_fb_id);
1160 sprite->pending_fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001161}
1162
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001163/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001164static uint32_t
1165drm_get_backlight(struct drm_output *output)
1166{
1167 long brightness, max_brightness, norm;
1168
1169 brightness = backlight_get_brightness(output->backlight);
1170 max_brightness = backlight_get_max_brightness(output->backlight);
1171
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001172 /* convert it on a scale of 0 to 255 */
1173 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001174
1175 return (uint32_t) norm;
1176}
1177
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001178/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001179static void
1180drm_set_backlight(struct weston_output *output_base, uint32_t value)
1181{
1182 struct drm_output *output = (struct drm_output *) output_base;
1183 long max_brightness, new_brightness;
1184
1185 if (!output->backlight)
1186 return;
1187
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001188 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001189 return;
1190
1191 max_brightness = backlight_get_max_brightness(output->backlight);
1192
1193 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001194 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001195
1196 backlight_set_brightness(output->backlight, new_brightness);
1197}
1198
1199static drmModePropertyPtr
1200drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1201{
1202 drmModePropertyPtr props;
1203 int i;
1204
1205 for (i = 0; i < connector->count_props; i++) {
1206 props = drmModeGetProperty(fd, connector->props[i]);
1207 if (!props)
1208 continue;
1209
1210 if (!strcmp(props->name, name))
1211 return props;
1212
1213 drmModeFreeProperty(props);
1214 }
1215
1216 return NULL;
1217}
1218
1219static void
1220drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1221{
1222 struct drm_output *output = (struct drm_output *) output_base;
1223 struct weston_compositor *ec = output_base->compositor;
1224 struct drm_compositor *c = (struct drm_compositor *) ec;
1225 drmModeConnectorPtr connector;
1226 drmModePropertyPtr prop;
1227
1228 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1229 if (!connector)
1230 return;
1231
1232 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1233 if (!prop) {
1234 drmModeFreeConnector(connector);
1235 return;
1236 }
1237
1238 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1239 prop->prop_id, level);
1240 drmModeFreeProperty(prop);
1241 drmModeFreeConnector(connector);
1242}
1243
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001244static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001245create_output_for_connector(struct drm_compositor *ec,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001246 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001247 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001248 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001249{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001250 struct drm_output *output;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001251 struct drm_mode *drm_mode, *next;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001252 struct weston_mode *m, *preferred, *current;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001253 drmModeEncoder *encoder;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001254 drmModeModeInfo crtc_mode;
1255 drmModeCrtc *crtc;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001256 int i, ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001257
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001258 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001259 if (encoder == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001260 weston_log("No encoder for connector.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001261 return -1;
1262 }
1263
1264 for (i = 0; i < resources->count_crtcs; i++) {
Marty Jack13d9db22011-02-09 19:01:42 -05001265 if (encoder->possible_crtcs & (1 << i) &&
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001266 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001267 break;
1268 }
1269 if (i == resources->count_crtcs) {
Martin Minarik6d118362012-06-07 18:01:59 +02001270 weston_log("No usable crtc for encoder.\n");
David Herrmanneb8bed52011-12-08 17:05:44 +01001271 drmModeFreeEncoder(encoder);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001272 return -1;
1273 }
1274
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001275 output = malloc(sizeof *output);
David Herrmanneb8bed52011-12-08 17:05:44 +01001276 if (output == NULL) {
1277 drmModeFreeEncoder(encoder);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001278 return -1;
David Herrmanneb8bed52011-12-08 17:05:44 +01001279 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001280
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001281 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001282 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1283 output->base.make = "unknown";
1284 output->base.model = "unknown";
1285 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001286
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001287 output->crtc_id = resources->crtcs[i];
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001288 ec->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001289 output->connector_id = connector->connector_id;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001290 ec->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001291
Matt Roper361d2ad2011-08-29 13:52:23 -07001292 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001293 drmModeFreeEncoder(encoder);
Matt Roper361d2ad2011-08-29 13:52:23 -07001294
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001295 /* Get the current mode on the crtc that's currently driving
1296 * this connector. */
1297 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoder_id);
1298 if (encoder == NULL)
1299 goto err_free;
1300 crtc = drmModeGetCrtc(ec->drm.fd, encoder->crtc_id);
1301 drmModeFreeEncoder(encoder);
1302 if (crtc == NULL)
1303 goto err_free;
1304 crtc_mode = crtc->mode;
1305 drmModeFreeCrtc(crtc);
1306
David Herrmann0f0d54e2011-12-08 17:05:45 +01001307 for (i = 0; i < connector->count_modes; i++) {
1308 ret = drm_output_add_mode(output, &connector->modes[i]);
1309 if (ret)
1310 goto err_free;
1311 }
1312
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001313 preferred = NULL;
1314 current = NULL;
1315 wl_list_for_each(drm_mode, &output->base.mode_list, base.link) {
1316 if (!memcmp(&crtc_mode, &drm_mode->mode_info, sizeof crtc_mode)) {
1317 drm_mode->base.flags |= WL_OUTPUT_MODE_CURRENT;
1318 current = &drm_mode->base;
1319 }
1320 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
1321 preferred = &drm_mode->base;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001322 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001323
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001324 if (current == NULL) {
1325 ret = drm_output_add_mode(output, &crtc_mode);
1326 if (ret)
1327 goto err_free;
1328 current = container_of(output->base.mode_list.prev,
1329 struct weston_mode, link);
1330 current->flags |= WL_OUTPUT_MODE_CURRENT;
1331 }
1332
1333 if (preferred == NULL)
1334 preferred = current;
1335
1336 if (option_current_mode) {
1337 output->base.current = current;
1338 } else {
1339 output->base.current = preferred;
1340 current->flags &= ~WL_OUTPUT_MODE_CURRENT;
1341 preferred->flags |= WL_OUTPUT_MODE_CURRENT;
1342 }
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001343
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001344 output->surface = gbm_surface_create(ec->gbm,
1345 output->base.current->width,
1346 output->base.current->height,
1347 GBM_FORMAT_XRGB8888,
1348 GBM_BO_USE_SCANOUT |
1349 GBM_BO_USE_RENDERING);
1350 if (!output->surface) {
Martin Minarik6d118362012-06-07 18:01:59 +02001351 weston_log("failed to create gbm surface\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001352 goto err_free;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001353 }
1354
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001355 output->egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001356 eglCreateWindowSurface(ec->base.egl_display,
1357 ec->base.egl_config,
1358 output->surface,
1359 NULL);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001360 if (output->egl_surface == EGL_NO_SURFACE) {
Martin Minarik6d118362012-06-07 18:01:59 +02001361 weston_log("failed to create egl surface\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001362 goto err_surface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001363 }
1364
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04001365 output->cursor_bo[0] =
1366 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1367 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1368 output->cursor_bo[1] =
1369 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1370 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1371
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001372 output->backlight = backlight_init(drm_device,
1373 connector->connector_type);
1374 if (output->backlight) {
1375 output->base.set_backlight = drm_set_backlight;
1376 output->base.backlight_current = drm_get_backlight(output);
1377 }
1378
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001379 weston_output_init(&output->base, &ec->base, x, y,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001380 connector->mmWidth, connector->mmHeight,
1381 WL_OUTPUT_FLIPPED);
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04001382
1383 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1384
Alex Wubd3354b2012-04-17 17:20:49 +08001385 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001386 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07001387 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001388 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001389 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08001390 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001391
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001392 weston_log("kms connector %d, crtc %d\n",
1393 output->connector_id, output->crtc_id);
1394 wl_list_for_each(m, &output->base.mode_list, link)
1395 weston_log_continue(" mode %dx%d@%.1f%s%s%s\n",
1396 m->width, m->height, m->refresh / 1000.0,
1397 m->flags & WL_OUTPUT_MODE_PREFERRED ?
1398 ", preferred" : "",
1399 m->flags & WL_OUTPUT_MODE_CURRENT ?
1400 ", current" : "",
1401 connector->count_modes == 0 ?
1402 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001403
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001404 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01001405
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001406err_surface:
1407 gbm_surface_destroy(output->surface);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001408err_free:
1409 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1410 base.link) {
1411 wl_list_remove(&drm_mode->base.link);
1412 free(drm_mode);
1413 }
1414
1415 drmModeFreeCrtc(output->original_crtc);
1416 ec->crtc_allocator &= ~(1 << output->crtc_id);
1417 ec->connector_allocator &= ~(1 << output->connector_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01001418 free(output);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001419
David Herrmann0f0d54e2011-12-08 17:05:45 +01001420 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001421}
1422
Jesse Barnes58ef3792012-02-23 09:45:49 -05001423static void
1424create_sprites(struct drm_compositor *ec)
1425{
1426 struct drm_sprite *sprite;
1427 drmModePlaneRes *plane_res;
1428 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001429 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001430
1431 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1432 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02001433 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001434 strerror(errno));
1435 return;
1436 }
1437
1438 for (i = 0; i < plane_res->count_planes; i++) {
1439 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1440 if (!plane)
1441 continue;
1442
1443 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1444 plane->count_formats));
1445 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02001446 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001447 __func__);
1448 free(plane);
1449 continue;
1450 }
1451
1452 memset(sprite, 0, sizeof *sprite);
1453
1454 sprite->possible_crtcs = plane->possible_crtcs;
1455 sprite->plane_id = plane->plane_id;
1456 sprite->surface = NULL;
1457 sprite->pending_surface = NULL;
1458 sprite->fb_id = 0;
1459 sprite->pending_fb_id = 0;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001460 sprite->destroy_listener.notify = sprite_handle_buffer_destroy;
1461 sprite->pending_destroy_listener.notify =
Jesse Barnes58ef3792012-02-23 09:45:49 -05001462 sprite_handle_pending_buffer_destroy;
1463 sprite->compositor = ec;
1464 sprite->count_formats = plane->count_formats;
1465 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05001466 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05001467 drmModeFreePlane(plane);
1468
1469 wl_list_insert(&ec->sprite_list, &sprite->link);
1470 }
1471
1472 free(plane_res->planes);
1473 free(plane_res);
1474}
1475
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001476static void
1477destroy_sprites(struct drm_compositor *compositor)
1478{
1479 struct drm_sprite *sprite, *next;
1480 struct drm_output *output;
1481
1482 output = container_of(compositor->base.output_list.next,
1483 struct drm_output, base.link);
1484
1485 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1486 drmModeSetPlane(compositor->drm.fd,
1487 sprite->plane_id,
1488 output->crtc_id, 0, 0,
1489 0, 0, 0, 0, 0, 0, 0, 0);
1490 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1491 free(sprite);
1492 }
1493}
Jesse Barnes58ef3792012-02-23 09:45:49 -05001494
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001495static int
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001496create_outputs(struct drm_compositor *ec, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001497 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001498{
1499 drmModeConnector *connector;
1500 drmModeRes *resources;
1501 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001502 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001503
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001504 resources = drmModeGetResources(ec->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001505 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02001506 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001507 return -1;
1508 }
1509
Jesse Barnes58ef3792012-02-23 09:45:49 -05001510 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001511 if (!ec->crtcs) {
1512 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001513 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001514 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001515
1516 ec->num_crtcs = resources->count_crtcs;
1517 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1518
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001519 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001520 connector = drmModeGetConnector(ec->drm.fd,
1521 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001522 if (connector == NULL)
1523 continue;
1524
1525 if (connector->connection == DRM_MODE_CONNECTED &&
1526 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001527 connector->connector_id == option_connector)) {
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001528 if (create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001529 connector, x, y,
1530 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02001531 drmModeFreeConnector(connector);
1532 continue;
1533 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001534
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001535 x += container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001536 struct weston_output,
Benjamin Franzke9eaee352011-08-02 13:03:54 +02001537 link)->current->width;
1538 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001539
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001540 drmModeFreeConnector(connector);
1541 }
1542
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001543 if (wl_list_empty(&ec->base.output_list)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001544 weston_log("No currently active connector found.\n");
Christopher Michaeleb866cd2012-03-07 14:55:21 -05001545 drmModeFreeResources(resources);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001546 return -1;
1547 }
1548
1549 drmModeFreeResources(resources);
1550
1551 return 0;
1552}
1553
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001554static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001555update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001556{
1557 drmModeConnector *connector;
1558 drmModeRes *resources;
1559 struct drm_output *output, *next;
1560 int x = 0, y = 0;
1561 int x_offset = 0, y_offset = 0;
1562 uint32_t connected = 0, disconnects = 0;
1563 int i;
1564
1565 resources = drmModeGetResources(ec->drm.fd);
1566 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02001567 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001568 return;
1569 }
1570
1571 /* collect new connects */
1572 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02001573 int connector_id = resources->connectors[i];
1574
1575 connector = drmModeGetConnector(ec->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01001576 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001577 continue;
1578
David Herrmann7551cff2011-12-08 17:05:43 +01001579 if (connector->connection != DRM_MODE_CONNECTED) {
1580 drmModeFreeConnector(connector);
1581 continue;
1582 }
1583
Benjamin Franzke117483d2011-08-30 11:38:26 +02001584 connected |= (1 << connector_id);
1585
1586 if (!(ec->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001587 struct weston_output *last =
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001588 container_of(ec->base.output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001589 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001590
1591 /* XXX: not yet needed, we die with 0 outputs */
1592 if (!wl_list_empty(&ec->base.output_list))
Benjamin Franzke117483d2011-08-30 11:38:26 +02001593 x = last->x + last->current->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001594 else
1595 x = 0;
1596 y = 0;
1597 create_output_for_connector(ec, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001598 connector, x, y,
1599 drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02001600 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001601
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001602 }
1603 drmModeFreeConnector(connector);
1604 }
1605 drmModeFreeResources(resources);
1606
1607 disconnects = ec->connector_allocator & ~connected;
1608 if (disconnects) {
1609 wl_list_for_each_safe(output, next, &ec->base.output_list,
1610 base.link) {
1611 if (x_offset != 0 || y_offset != 0) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001612 weston_output_move(&output->base,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001613 output->base.x - x_offset,
1614 output->base.y - y_offset);
1615 }
1616
1617 if (disconnects & (1 << output->connector_id)) {
1618 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02001619 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001620 output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001621 x_offset += output->base.current->width;
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001622 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001623 }
1624 }
1625 }
1626
1627 /* FIXME: handle zero outputs, without terminating */
1628 if (ec->connector_allocator == 0)
1629 wl_display_terminate(ec->base.wl_display);
1630}
1631
1632static int
David Herrmannd7488c22012-03-11 20:05:21 +01001633udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001634{
David Herrmannd7488c22012-03-11 20:05:21 +01001635 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01001636 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01001637
1638 sysnum = udev_device_get_sysnum(device);
1639 if (!sysnum || atoi(sysnum) != ec->drm.id)
1640 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001641
David Herrmann6ac52db2012-03-11 20:05:22 +01001642 val = udev_device_get_property_value(device, "HOTPLUG");
1643 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001644 return 0;
1645
David Herrmann6ac52db2012-03-11 20:05:22 +01001646 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001647}
1648
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001649static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001650udev_drm_event(int fd, uint32_t mask, void *data)
1651{
1652 struct drm_compositor *ec = data;
1653 struct udev_device *event;
1654
1655 event = udev_monitor_receive_device(ec->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001656
David Herrmannd7488c22012-03-11 20:05:21 +01001657 if (udev_event_is_hotplug(ec, event))
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001658 update_outputs(ec, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001659
1660 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001661
1662 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001663}
1664
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001665static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001666drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001667{
1668 struct drm_compositor *d = (struct drm_compositor *) ec;
Daniel Stone37816df2012-05-16 18:45:18 +01001669 struct weston_seat *seat, *next;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001670
Daniel Stone37816df2012-05-16 18:45:18 +01001671 wl_list_for_each_safe(seat, next, &ec->seat_list, link)
1672 evdev_input_destroy(seat);
Jonas Ådahlc97af922012-03-28 22:36:09 +02001673
1674 wl_event_source_remove(d->udev_drm_source);
1675 wl_event_source_remove(d->drm_source);
1676
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001677 weston_compositor_shutdown(ec);
Jonas Ådahlc97af922012-03-28 22:36:09 +02001678
Ander Conselvan de Oliveira5f5f3192012-04-30 13:31:28 +03001679 /* Work around crash in egl_dri2.c's dri2_make_current() */
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001680 eglMakeCurrent(ec->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
Ander Conselvan de Oliveira5f5f3192012-04-30 13:31:28 +03001681 EGL_NO_CONTEXT);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001682 eglTerminate(ec->egl_display);
Ander Conselvan de Oliveira5f5f3192012-04-30 13:31:28 +03001683 eglReleaseThread();
1684
Matt Roper361d2ad2011-08-29 13:52:23 -07001685 gbm_device_destroy(d->gbm);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001686 destroy_sprites(d);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001687 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +02001688 weston_log("failed to drop master: %m\n");
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001689 tty_destroy(d->tty);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001690
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001691 free(d);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001692}
1693
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001694static void
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001695drm_compositor_set_modes(struct drm_compositor *compositor)
1696{
1697 struct drm_output *output;
1698 struct drm_mode *drm_mode;
1699 int ret;
1700
1701 wl_list_for_each(output, &compositor->base.output_list, base.link) {
1702 drm_mode = (struct drm_mode *) output->base.current;
1703 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001704 output->current->fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001705 &output->connector_id, 1,
1706 &drm_mode->mode_info);
1707 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001708 weston_log(
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001709 "failed to set mode %dx%d for output at %d,%d: %m\n",
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001710 drm_mode->base.width, drm_mode->base.height,
1711 output->base.x, output->base.y);
1712 }
1713 }
1714}
1715
1716static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001717vt_func(struct weston_compositor *compositor, int event)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001718{
1719 struct drm_compositor *ec = (struct drm_compositor *) compositor;
Daniel Stone37816df2012-05-16 18:45:18 +01001720 struct weston_seat *seat;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001721 struct drm_sprite *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001722 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001723
1724 switch (event) {
1725 case TTY_ENTER_VT:
1726 compositor->focus = 1;
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001727 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001728 weston_log("failed to set master: %m\n");
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001729 wl_display_terminate(compositor->wl_display);
1730 }
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001731 compositor->state = ec->prev_state;
Kristian Høgsberg835cd492012-01-18 11:48:46 -05001732 drm_compositor_set_modes(ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001733 weston_compositor_damage_all(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001734 wl_list_for_each(seat, &compositor->seat_list, link) {
1735 evdev_add_devices(ec->udev, seat);
1736 evdev_enable_udev_monitor(ec->udev, seat);
Benjamin Franzke78d3afe2012-04-09 18:14:58 +02001737 }
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001738 break;
1739 case TTY_LEAVE_VT:
Daniel Stone37816df2012-05-16 18:45:18 +01001740 wl_list_for_each(seat, &compositor->seat_list, link) {
1741 evdev_disable_udev_monitor(seat);
1742 evdev_remove_devices(seat);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04001743 }
1744
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001745 compositor->focus = 0;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001746 ec->prev_state = compositor->state;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001747 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04001748
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001749 /* If we have a repaint scheduled (either from a
1750 * pending pageflip or the idle handler), make sure we
1751 * cancel that so we don't try to pageflip when we're
1752 * vt switched away. The SLEEPING state will prevent
1753 * further attemps at repainting. When we switch
1754 * back, we schedule a repaint, which will process
1755 * pending frame callbacks. */
1756
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001757 wl_list_for_each(output, &ec->base.output_list, base.link) {
1758 output->base.repaint_needed = 0;
1759 drmModeSetCursor(ec->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001760 }
1761
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001762 output = container_of(ec->base.output_list.next,
1763 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001764
1765 wl_list_for_each(sprite, &ec->sprite_list, link)
1766 drmModeSetPlane(ec->drm.fd,
1767 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001768 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05001769 0, 0, 0, 0, 0, 0, 0, 0);
1770
Benjamin Franzkebfeda132012-01-30 14:04:04 +01001771 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +02001772 weston_log("failed to drop master: %m\n");
Kristian Høgsberga018fb02012-01-16 10:52:52 -05001773
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04001774 break;
1775 };
1776}
1777
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001778static void
Daniel Stone325fc2d2012-05-30 16:31:58 +01001779switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001780{
1781 struct drm_compositor *ec = data;
1782
Daniel Stone325fc2d2012-05-30 16:31:58 +01001783 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001784}
1785
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001786static const char default_seat[] = "seat0";
1787
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001788static struct weston_compositor *
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001789drm_compositor_create(struct wl_display *display,
Daniel Stonebaf43592012-06-01 11:11:10 -04001790 int connector, const char *seat, int tty,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001791 int argc, char *argv[], const char *config_file)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001792{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001793 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001794 struct udev_enumerate *e;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001795 struct udev_list_entry *entry;
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001796 struct udev_device *device, *drm_device;
1797 const char *path, *device_seat;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001798 struct wl_event_loop *loop;
Daniel Stonea96b93c2012-06-22 14:04:37 +01001799 struct weston_seat *weston_seat, *next;
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001800 uint32_t key;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001801
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001802 weston_log("initializing drm backend\n");
1803
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001804 ec = malloc(sizeof *ec);
1805 if (ec == NULL)
1806 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001807 memset(ec, 0, sizeof *ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01001808
1809 if (weston_compositor_init(&ec->base, display, argc, argv,
Daniel Stonea96b93c2012-06-22 14:04:37 +01001810 config_file) < 0) {
1811 weston_log("weston_compositor_init failed\n");
1812 goto err_base;
1813 }
Daniel Stone725c2c32012-06-22 14:04:36 +01001814
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001815 ec->udev = udev_new();
1816 if (ec->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001817 weston_log("failed to initialize udev context\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001818 goto err_compositor;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001819 }
1820
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05001821 ec->base.wl_display = display;
1822 ec->tty = tty_create(&ec->base, vt_func, tty);
1823 if (!ec->tty) {
Martin Minarik6d118362012-06-07 18:01:59 +02001824 weston_log("failed to initialize tty\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001825 goto err_udev;
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05001826 }
1827
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001828 e = udev_enumerate_new(ec->udev);
1829 udev_enumerate_add_match_subsystem(e, "drm");
Benjamin Franzkea764ee52011-10-07 08:23:22 +02001830 udev_enumerate_add_match_sysname(e, "card[0-9]*");
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001831
Benjamin Franzke117483d2011-08-30 11:38:26 +02001832 udev_enumerate_scan_devices(e);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001833 drm_device = NULL;
Benjamin Franzke117483d2011-08-30 11:38:26 +02001834 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001835 path = udev_list_entry_get_name(entry);
1836 device = udev_device_new_from_syspath(ec->udev, path);
Benjamin Franzke117483d2011-08-30 11:38:26 +02001837 device_seat =
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001838 udev_device_get_property_value(device, "ID_SEAT");
1839 if (!device_seat)
1840 device_seat = default_seat;
1841 if (strcmp(device_seat, seat) == 0) {
1842 drm_device = device;
1843 break;
1844 }
1845 udev_device_unref(device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001846 }
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001847
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001848 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001849 weston_log("no drm device found\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001850 goto err_udev_enum;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001851 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001852
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04001853 if (init_egl(ec, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001854 weston_log("failed to initialize egl\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001855 goto err_udev_dev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001856 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001857
1858 ec->base.destroy = drm_destroy;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02001859
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001860 ec->base.focus = 1;
1861
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001862 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001863
Daniel Stone725c2c32012-06-22 14:04:36 +01001864 if (weston_compositor_init_gl(&ec->base) < 0)
Daniel Stonea96b93c2012-06-22 14:04:37 +01001865 goto err_egl;
Benjamin Franzked59eb1c2011-04-29 22:14:54 +02001866
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001867 for (key = KEY_F1; key < KEY_F9; key++)
Daniel Stone325fc2d2012-05-30 16:31:58 +01001868 weston_compositor_add_key_binding(&ec->base, key,
1869 MODIFIER_CTRL | MODIFIER_ALT,
1870 switch_vt_binding, ec);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04001871
Jesse Barnes58ef3792012-02-23 09:45:49 -05001872 wl_list_init(&ec->sprite_list);
1873 create_sprites(ec);
1874
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001875 if (create_outputs(ec, connector, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001876 weston_log("failed to create output for %s\n", path);
Daniel Stonea96b93c2012-06-22 14:04:37 +01001877 goto err_sprite;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001878 }
1879
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02001880 path = NULL;
1881
Tiago Vignattice03ec32011-12-19 01:14:03 +02001882 evdev_input_create(&ec->base, ec->udev, seat);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001883
1884 loop = wl_display_get_event_loop(ec->base.wl_display);
1885 ec->drm_source =
Benjamin Franzke2af7f102011-03-02 11:14:59 +01001886 wl_event_loop_add_fd(loop, ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001887 WL_EVENT_READABLE, on_drm_input, ec);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001888
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001889 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
1890 if (ec->udev_monitor == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001891 weston_log("failed to intialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001892 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001893 }
1894 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
1895 "drm", NULL);
1896 ec->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02001897 wl_event_loop_add_fd(loop,
1898 udev_monitor_get_fd(ec->udev_monitor),
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001899 WL_EVENT_READABLE, udev_drm_event, ec);
1900
1901 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001902 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01001903 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01001904 }
1905
Daniel Stonea96b93c2012-06-22 14:04:37 +01001906 udev_device_unref(drm_device);
1907 udev_enumerate_unref(e);
1908
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001909 return &ec->base;
Daniel Stonea96b93c2012-06-22 14:04:37 +01001910
1911err_udev_monitor:
1912 wl_event_source_remove(ec->udev_drm_source);
1913 udev_monitor_unref(ec->udev_monitor);
1914err_drm_source:
1915 wl_event_source_remove(ec->drm_source);
1916 wl_list_for_each_safe(weston_seat, next, &ec->base.seat_list, link)
1917 evdev_input_destroy(weston_seat);
1918err_sprite:
1919 destroy_sprites(ec);
1920err_egl:
1921 eglMakeCurrent(ec->base.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
1922 EGL_NO_CONTEXT);
1923 eglTerminate(ec->base.egl_display);
1924 eglReleaseThread();
1925 gbm_device_destroy(ec->gbm);
1926err_udev_dev:
1927 udev_device_unref(drm_device);
1928err_udev_enum:
1929 udev_enumerate_unref(e);
1930 tty_destroy(ec->tty);
1931err_udev:
1932 udev_unref(ec->udev);
1933err_compositor:
1934 weston_compositor_shutdown(&ec->base);
1935err_base:
1936 free(ec);
1937 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001938}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001939
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001940WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -04001941backend_init(struct wl_display *display, int argc, char *argv[],
1942 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001943{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001944 int connector = 0, tty = 0;
1945 const char *seat = default_seat;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001946
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001947 const struct weston_option drm_options[] = {
1948 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
1949 { WESTON_OPTION_STRING, "seat", 0, &seat },
1950 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001951 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001952 };
Benjamin Franzke117483d2011-08-30 11:38:26 +02001953
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001954 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001955
Daniel Stonec1be8e52012-06-01 11:14:02 -04001956 return drm_compositor_create(display, connector, seat, tty, argc, argv,
1957 config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001958}