compositor: Don't set DPMS state on start up
[profile/ivi/weston.git] / src / compositor-drm.c
index eab5f5c..84c51ff 100644 (file)
@@ -46,6 +46,7 @@
 static int option_current_mode = 0;
 static char *output_name;
 static char *output_mode;
+static char *output_transform;
 static struct wl_list configured_output_list;
 
 enum output_config {
@@ -60,6 +61,7 @@ enum output_config {
 struct drm_configured_output {
        char *name;
        char *mode;
+       uint32_t transform;
        int32_t width, height;
        drmModeModeInfo crtc_mode;
        enum output_config config;
@@ -86,11 +88,18 @@ struct drm_compositor {
        uint32_t connector_allocator;
        struct tty *tty;
 
-       struct gbm_surface *dummy_surface;
-       EGLSurface dummy_egl_surface;
+       /* we need these parameters in order to not fail drmModeAddFB2()
+        * due to out of bounds dimensions, and then mistakenly set
+        * sprites_are_broken:
+        */
+       int32_t min_width, max_width;
+       int32_t min_height, max_height;
 
        struct wl_list sprite_list;
        int sprites_are_broken;
+       int sprites_hidden;
+
+       int cursors_are_broken;
 
        uint32_t prev_state;
 };
@@ -116,6 +125,7 @@ struct drm_output {
 
        char *name;
        uint32_t crtc_id;
+       int pipe;
        uint32_t connector_id;
        drmModeCrtcPtr original_crtc;
 
@@ -128,7 +138,6 @@ struct drm_output {
        struct weston_plane fb_plane;
        struct weston_surface *cursor_surface;
        int current_cursor;
-       EGLSurface egl_surface;
        struct drm_fb *current, *next;
        struct backlight *backlight;
 };
@@ -175,8 +184,6 @@ struct drm_seat {
 
 static void
 drm_output_set_cursor(struct drm_output *output);
-static void
-drm_disable_unused_sprites(struct weston_output *output_base);
 
 static int
 drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
@@ -264,6 +271,34 @@ fb_handle_buffer_destroy(struct wl_listener *listener, void *data)
                weston_output_schedule_repaint(&fb->output->base);
 }
 
+static int
+drm_output_check_scanout_format(struct drm_output *output,
+                               struct weston_surface *es, struct gbm_bo *bo)
+{
+       int ret = 0;
+       uint32_t format;
+       pixman_region32_t r;
+
+       format = gbm_bo_get_format(bo);
+
+       if (format == GBM_FORMAT_XRGB8888)
+               ret = 1;
+       else if (format == GBM_FORMAT_ARGB8888) {
+               /* We can only scanout an ARGB buffer if the surface's
+                * opaque region covers the whole output */
+               pixman_region32_init(&r);
+               pixman_region32_subtract(&r, &output->base.region,
+                                        &es->opaque);
+
+               if (!pixman_region32_not_empty(&r))
+                       ret = 1;
+
+               pixman_region32_fini(&r);
+       }
+
+       return ret;
+}
+
 static struct weston_plane *
 drm_output_prepare_scanout_surface(struct weston_output *_output,
                                   struct weston_surface *es)
@@ -284,10 +319,11 @@ drm_output_prepare_scanout_surface(struct weston_output *_output,
        bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
                           es->buffer, GBM_BO_USE_SCANOUT);
 
-       /* Need to verify output->region contained in surface opaque
-        * region.  Or maybe just that format doesn't have alpha.
-        * For now, scanout only if format is XRGB8888. */
-       if (gbm_bo_get_format(bo) != GBM_FORMAT_XRGB8888) {
+       /* Unable to use the buffer for scanout */
+       if (!bo)
+               return NULL;
+
+       if (!drm_output_check_scanout_format(output, es, bo)) {
                gbm_bo_destroy(bo);
                return NULL;
        }
@@ -312,25 +348,14 @@ drm_output_prepare_scanout_surface(struct weston_output *_output,
 static void
 drm_output_render(struct drm_output *output, pixman_region32_t *damage)
 {
-       struct drm_compositor *compositor =
-               (struct drm_compositor *) output->base.compositor;
-       struct weston_surface *surface;
+       struct weston_compositor *ec = output->base.compositor;
        struct gbm_bo *bo;
 
-       if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
-                           output->egl_surface,
-                           compositor->base.egl_context)) {
-               weston_log("failed to make current\n");
-               return;
-       }
-
-       wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
-               if (surface->plane == &compositor->base.primary_plane)
-                       weston_surface_draw(surface, &output->base, damage);
+       ec->renderer->repaint_output(&output->base, damage);
 
-       wl_signal_emit(&output->base.frame_signal, output);
+       pixman_region32_subtract(&ec->primary_plane.damage,
+                                &ec->primary_plane.damage, damage);
 
-       eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
        bo = gbm_surface_lock_front_buffer(output->surface);
        if (!bo) {
                weston_log("failed to lock front buffer: %m\n");
@@ -398,7 +423,10 @@ drm_output_repaint(struct weston_output *output_base,
                        continue;
 
                ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
-                                     output->crtc_id, s->pending_fb_id, flags,
+                                     output->crtc_id,
+                                     compositor->sprites_hidden ?
+                                     0 : s->pending_fb_id,
+                                     flags,
                                      s->dest_x, s->dest_y,
                                      s->dest_w, s->dest_h,
                                      s->src_x, s->src_y,
@@ -407,6 +435,9 @@ drm_output_repaint(struct weston_output *output_base,
                        weston_log("setplane failed: %d: %s\n",
                                ret, strerror(errno));
 
+               if (output->pipe > 0)
+                       vbl.request.type |= DRM_VBLANK_SECONDARY;
+
                /*
                 * Queue a vblank signal so we know when the surface
                 * becomes active on the display or has been replaced.
@@ -422,8 +453,6 @@ drm_output_repaint(struct weston_output *output_base,
                output->vblank_pending = 1;
        }
 
-       drm_disable_unused_sprites(&output->base);
-
        return;
 }
 
@@ -531,43 +560,6 @@ drm_surface_transform_supported(struct weston_surface *es)
        return 1;
 }
 
-static void
-drm_disable_unused_sprites(struct weston_output *output_base)
-{
-       struct weston_compositor *ec = output_base->compositor;
-       struct drm_compositor *c =(struct drm_compositor *) ec;
-       struct drm_output *output = (struct drm_output *) output_base;
-       struct drm_sprite *s;
-       int ret;
-
-       wl_list_for_each(s, &c->sprite_list, link) {
-               if (s->pending_fb_id)
-                       continue;
-
-               ret = drmModeSetPlane(c->drm.fd, s->plane_id,
-                                     output->crtc_id, 0, 0,
-                                     0, 0, 0, 0, 0, 0, 0, 0);
-               if (ret)
-                       weston_log("failed to disable plane: %d: %s\n",
-                               ret, strerror(errno));
-               drmModeRmFB(c->drm.fd, s->fb_id);
-
-               if (s->surface) {
-                       s->surface = NULL;
-                       wl_list_remove(&s->destroy_listener.link);
-               }
-
-               assert(!s->pending_surface);
-               s->fb_id = 0;
-               s->pending_fb_id = 0;
-       }
-}
-
-/*
- * This function must take care to damage any previously assigned surface
- * if the sprite ends up binding to a different surface than in the
- * previous frame.
- */
 static struct weston_plane *
 drm_output_prepare_overlay_surface(struct weston_output *output_base,
                                   struct weston_surface *es)
@@ -585,6 +577,7 @@ drm_output_prepare_overlay_surface(struct weston_output *output_base,
        pixman_box32_t *box;
        uint32_t format;
        wl_fixed_t sx1, sy1, sx2, sy2;
+       int32_t width, height;
 
        if (c->sprites_are_broken)
                return NULL;
@@ -595,6 +588,12 @@ drm_output_prepare_overlay_surface(struct weston_output *output_base,
        if (es->buffer == NULL)
                return NULL;
 
+       if (es->alpha != 1.0f)
+               return NULL;
+
+       if (wl_buffer_is_shm(es->buffer))
+               return NULL;
+
        if (!drm_surface_transform_supported(es))
                return NULL;
 
@@ -612,6 +611,16 @@ drm_output_prepare_overlay_surface(struct weston_output *output_base,
        if (!found)
                return NULL;
 
+       width = es->geometry.width;
+       height = es->geometry.height;
+
+       /* If geometry is out of bounds, don't even bother trying because
+        * we know the AddFB2() call will fail:
+        */
+       if (c->min_width > width || width > c->max_width ||
+           c->min_height > height || height > c->max_height)
+               return NULL;
+
        bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
                           es->buffer, GBM_BO_USE_SCANOUT);
        if (!bo)
@@ -633,7 +642,7 @@ drm_output_prepare_overlay_surface(struct weston_output *output_base,
        pitches[0] = stride;
        offsets[0] = 0;
 
-       ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height,
+       ret = drmModeAddFB2(c->drm.fd, width, height,
                            format, handles, pitches, offsets,
                            &fb_id, 0);
        if (ret) {
@@ -652,7 +661,7 @@ drm_output_prepare_overlay_surface(struct weston_output *output_base,
 
        /*
         * Calculate the source & dest rects properly based on actual
-        * postion (note the caller has called weston_surface_update_transform()
+        * position (note the caller has called weston_surface_update_transform()
         * for us already).
         */
        pixman_region32_init(&dest_rect);
@@ -705,12 +714,16 @@ static struct weston_plane *
 drm_output_prepare_cursor_surface(struct weston_output *output_base,
                                  struct weston_surface *es)
 {
+       struct drm_compositor *c =
+               (struct drm_compositor *) output_base->compositor;
        struct drm_output *output = (struct drm_output *) output_base;
 
        if (output->cursor_surface)
                return NULL;
        if (es->output_mask != (1u << output_base->id))
                return NULL;
+       if (c->cursors_are_broken)
+               return NULL;
        if (es->buffer == NULL || !wl_buffer_is_shm(es->buffer) ||
            es->geometry.width > 64 || es->geometry.height > 64)
                return NULL;
@@ -732,6 +745,7 @@ drm_output_set_cursor(struct drm_output *output)
        unsigned char *s;
        int i, x, y;
 
+       output->cursor_surface = NULL;
        if (es == NULL) {
                drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
                return;
@@ -750,19 +764,24 @@ drm_output_set_cursor(struct drm_output *output)
                               es->geometry.width * 4);
 
                if (gbm_bo_write(bo, buf, sizeof buf) < 0)
-                       weston_log("failed update cursor: %n\n");
+                       weston_log("failed update cursor: %m\n");
 
                handle = gbm_bo_get_handle(bo).s32;
                if (drmModeSetCursor(c->drm.fd,
-                                    output->crtc_id, handle, 64, 64))
-                       weston_log("failed to set cursor: %n\n");
+                                    output->crtc_id, handle, 64, 64)) {
+                       weston_log("failed to set cursor: %m\n");
+                       c->cursors_are_broken = 1;
+               }
        }
 
        x = es->geometry.x - output->base.x;
        y = es->geometry.y - output->base.y;
        if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
-               if (drmModeMoveCursor(c->drm.fd, output->crtc_id, x, y))
+               if (drmModeMoveCursor(c->drm.fd, output->crtc_id, x, y)) {
                        weston_log("failed to move cursor: %m\n");
+                       c->cursors_are_broken = 1;
+               }
+
                output->cursor_plane.x = x;
                output->cursor_plane.y = y;
        }
@@ -774,10 +793,25 @@ drm_assign_planes(struct weston_output *output)
        struct drm_compositor *c =
                (struct drm_compositor *) output->compositor;
        struct drm_output *drm_output = (struct drm_output *) output;
+       struct drm_sprite *s;
        struct weston_surface *es, *next;
        pixman_region32_t overlap, surface_overlap;
        struct weston_plane *primary, *next_plane;
 
+       /* Reset the opaque region of the planes */
+       pixman_region32_fini(&drm_output->cursor_plane.opaque);
+       pixman_region32_init(&drm_output->cursor_plane.opaque);
+       pixman_region32_fini(&drm_output->fb_plane.opaque);
+       pixman_region32_init(&drm_output->fb_plane.opaque);
+
+       wl_list_for_each (s, &c->sprite_list, link) {
+               if (!drm_sprite_crtc_supported(output, s->possible_crtcs))
+                       continue;
+
+               pixman_region32_fini(&s->plane.opaque);
+               pixman_region32_init(&s->plane.opaque);
+       }
+
        /*
         * Find a surface for each sprite in the output using some heuristics:
         * 1) size
@@ -792,7 +826,6 @@ drm_assign_planes(struct weston_output *output)
         * as we do for flipping full screen surfaces.
         */
        pixman_region32_init(&overlap);
-       drm_output->cursor_surface = NULL;
        primary = &c->base.primary_plane;
        wl_list_for_each_safe(es, next, &c->base.surface_list, link) {
                pixman_region32_init(&surface_overlap);
@@ -843,7 +876,7 @@ drm_output_destroy(struct weston_output *output_base)
        c->crtc_allocator &= ~(1 << output->crtc_id);
        c->connector_allocator &= ~(1 << output->connector_id);
 
-       eglDestroySurface(c->base.egl_display, output->egl_surface);
+       eglDestroySurface(c->base.egl_display, output->base.egl_surface);
        gbm_surface_destroy(output->surface);
 
        weston_plane_release(&output->fb_plane);
@@ -987,9 +1020,9 @@ drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mo
        }
        output->next = NULL;
 
-       eglDestroySurface(ec->base.egl_display, output->egl_surface);
+       eglDestroySurface(ec->base.egl_display, output->base.egl_surface);
        gbm_surface_destroy(output->surface);
-       output->egl_surface = egl_surface;
+       output->base.egl_surface = egl_surface;
        output->surface = surface;
 
        /*update output*/
@@ -1024,11 +1057,6 @@ init_egl(struct drm_compositor *ec, struct udev_device *device)
        EGLint major, minor, n;
        const char *filename, *sysnum;
        int fd;
-       static const EGLint context_attribs[] = {
-               EGL_CONTEXT_CLIENT_VERSION, 2,
-               EGL_NONE
-       };
-
        static const EGLint config_attribs[] = {
                EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
                EGL_RED_SIZE, 1,
@@ -1071,53 +1099,16 @@ init_egl(struct drm_compositor *ec, struct udev_device *device)
                return -1;
        }
 
-       if (!eglBindAPI(EGL_OPENGL_ES_API)) {
-               weston_log("failed to bind api EGL_OPENGL_ES_API\n");
-               return -1;
-       }
-
        if (!eglChooseConfig(ec->base.egl_display, config_attribs,
                             &ec->base.egl_config, 1, &n) || n != 1) {
                weston_log("failed to choose config: %d\n", n);
                return -1;
        }
 
-       ec->base.egl_context =
-               eglCreateContext(ec->base.egl_display, ec->base.egl_config,
-                                EGL_NO_CONTEXT, context_attribs);
-       if (ec->base.egl_context == NULL) {
-               weston_log("failed to create context\n");
-               return -1;
-       }
-
-       ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10,
-                                              GBM_FORMAT_XRGB8888,
-                                              GBM_BO_USE_RENDERING);
-       if (!ec->dummy_surface) {
-               weston_log("failed to create dummy gbm surface\n");
-               return -1;
-       }
-
-       ec->dummy_egl_surface =
-               eglCreateWindowSurface(ec->base.egl_display,
-                                      ec->base.egl_config,
-                                      ec->dummy_surface,
-                                      NULL);
-       if (ec->dummy_egl_surface == EGL_NO_SURFACE) {
-               weston_log("failed to create egl surface\n");
-               return -1;
-       }
-
-       if (!eglMakeCurrent(ec->base.egl_display, ec->dummy_egl_surface,
-                           ec->dummy_egl_surface, ec->base.egl_context)) {
-               weston_log("failed to make context current\n");
-               return -1;
-       }
-
        return 0;
 }
 
-static int
+static struct drm_mode *
 drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
 {
        struct drm_mode *mode;
@@ -1125,7 +1116,7 @@ drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
 
        mode = malloc(sizeof *mode);
        if (mode == NULL)
-               return -1;
+               return NULL;
 
        mode->base.flags = 0;
        mode->base.width = info->hdisplay;
@@ -1150,7 +1141,7 @@ drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
 
        wl_list_insert(output->base.mode_list.prev, &mode->base.link);
 
-       return 0;
+       return mode;
 }
 
 static int
@@ -1332,13 +1323,13 @@ create_output_for_connector(struct drm_compositor *ec,
                            int x, int y, struct udev_device *drm_device)
 {
        struct drm_output *output;
-       struct drm_mode *drm_mode, *next;
-       struct weston_mode *m, *preferred, *current, *configured;
+       struct drm_mode *drm_mode, *next, *preferred, *current, *configured;
+       struct weston_mode *m;
        struct drm_configured_output *o = NULL, *temp;
        drmModeEncoder *encoder;
        drmModeModeInfo crtc_mode;
        drmModeCrtc *crtc;
-       int i, ret;
+       int i;
        char name[32];
        const char *type_name;
 
@@ -1366,6 +1357,7 @@ create_output_for_connector(struct drm_compositor *ec,
        output->name = strdup(name);
 
        output->crtc_id = resources->crtcs[i];
+       output->pipe = i;
        ec->crtc_allocator |= (1 << output->crtc_id);
        output->connector_id = connector->connector_id;
        ec->connector_allocator |= (1 << output->connector_id);
@@ -1387,8 +1379,8 @@ create_output_for_connector(struct drm_compositor *ec,
        }
 
        for (i = 0; i < connector->count_modes; i++) {
-               ret = drm_output_add_mode(output, &connector->modes[i]);
-               if (ret)
+               drm_mode = drm_output_add_mode(output, &connector->modes[i]);
+               if (!drm_mode)
                        goto err_free;
        }
 
@@ -1398,14 +1390,15 @@ create_output_for_connector(struct drm_compositor *ec,
 
        wl_list_for_each(temp, &configured_output_list, link) {
                if (strcmp(temp->name, output->name) == 0) {
-                       weston_log("%s mode \"%s\" in config\n",
+                       if (temp->mode)
+                               weston_log("%s mode \"%s\" in config\n",
                                                        temp->name, temp->mode);
                        o = temp;
                        break;
                }
        }
 
-       if (o && strcmp("off", o->mode) == 0) {
+       if (o && o->config == OUTPUT_CONFIG_OFF) {
                weston_log("Disabling output %s\n", o->name);
 
                drmModeSetCrtc(ec->drm.fd, output->crtc_id,
@@ -1414,44 +1407,40 @@ create_output_for_connector(struct drm_compositor *ec,
        }
 
        wl_list_for_each(drm_mode, &output->base.mode_list, base.link) {
-               if (o && o->width == drm_mode->base.width &&
-                       o->height == drm_mode->base.height &&
-                       o->config == OUTPUT_CONFIG_MODE)
-                       configured = &drm_mode->base;
+               if (o && o->config == OUTPUT_CONFIG_MODE &&
+                       o->width == drm_mode->base.width &&
+                       o->height == drm_mode->base.height)
+                       configured = drm_mode;
                if (!memcmp(&crtc_mode, &drm_mode->mode_info, sizeof crtc_mode))
-                       current = &drm_mode->base;
+                       current = drm_mode;
                if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
-                       preferred = &drm_mode->base;
+                       preferred = drm_mode;
        }
 
        if (o && o->config == OUTPUT_CONFIG_MODELINE) {
-               ret = drm_output_add_mode(output, &o->crtc_mode);
-               if (ret)
+               configured = drm_output_add_mode(output, &o->crtc_mode);
+               if (!configured)
                        goto err_free;
-               configured = container_of(output->base.mode_list.prev,
-                                      struct weston_mode, link);
                current = configured;
        }
 
        if (current == NULL && crtc_mode.clock != 0) {
-               ret = drm_output_add_mode(output, &crtc_mode);
-               if (ret)
+               current = drm_output_add_mode(output, &crtc_mode);
+               if (!current)
                        goto err_free;
-               current = container_of(output->base.mode_list.prev,
-                                      struct weston_mode, link);
        }
 
        if (o && o->config == OUTPUT_CONFIG_CURRENT)
                configured = current;
 
        if (option_current_mode && current)
-               output->base.current = current;
+               output->base.current = &current->base;
        else if (configured)
-               output->base.current = configured;
+               output->base.current = &configured->base;
        else if (preferred)
-               output->base.current = preferred;
+               output->base.current = &preferred->base;
        else if (current)
-               output->base.current = current;
+               output->base.current = &current->base;
 
        if (output->base.current == NULL) {
                weston_log("no available modes for %s\n", output->name);
@@ -1471,12 +1460,12 @@ create_output_for_connector(struct drm_compositor *ec,
                goto err_free;
        }
 
-       output->egl_surface =
+       output->base.egl_surface =
                eglCreateWindowSurface(ec->base.egl_display,
                                       ec->base.egl_config,
                                       output->surface,
                                       NULL);
-       if (output->egl_surface == EGL_NO_SURFACE) {
+       if (output->base.egl_surface == EGL_NO_SURFACE) {
                weston_log("failed to create egl surface\n");
                goto err_surface;
        }
@@ -1487,6 +1476,10 @@ create_output_for_connector(struct drm_compositor *ec,
        output->cursor_bo[1] =
                gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
                              GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
+       if (output->cursor_bo[0] == NULL || output->cursor_bo[1] == NULL) {
+               weston_log("cursor buffers unavailable, using gl cursors\n");
+               ec->cursors_are_broken = 1;
+       }
 
        output->backlight = backlight_init(drm_device,
                                           connector->connector_type);
@@ -1497,7 +1490,7 @@ create_output_for_connector(struct drm_compositor *ec,
 
        weston_output_init(&output->base, &ec->base, x, y,
                           connector->mmWidth, connector->mmHeight,
-                          WL_OUTPUT_FLIPPED);
+                          o ? o->transform : WL_OUTPUT_TRANSFORM_NORMAL);
 
        wl_list_insert(ec->base.output_list.prev, &output->base.link);
 
@@ -1638,6 +1631,11 @@ create_outputs(struct drm_compositor *ec, uint32_t option_connector,
                return -1;
        }
 
+       ec->min_width  = resources->min_width;
+       ec->max_width  = resources->max_width;
+       ec->min_height = resources->min_height;
+       ec->max_height = resources->max_height;
+
        ec->num_crtcs = resources->count_crtcs;
        memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
 
@@ -1659,7 +1657,7 @@ create_outputs(struct drm_compositor *ec, uint32_t option_connector,
 
                        x += container_of(ec->base.output_list.prev,
                                          struct weston_output,
-                                         link)->current->width;
+                                         link)->width;
                }
 
                drmModeFreeConnector(connector);
@@ -1715,7 +1713,7 @@ update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
 
                        /* XXX: not yet needed, we die with 0 outputs */
                        if (!wl_list_empty(&ec->base.output_list))
-                               x = last->x + last->current->width;
+                               x = last->x + last->width;
                        else
                                x = 0;
                        y = 0;
@@ -1743,7 +1741,7 @@ update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
                                disconnects &= ~(1 << output->connector_id);
                                weston_log("connector %d disconnected\n",
                                       output->connector_id);
-                               x_offset += output->base.current->width;
+                               x_offset += output->base.width;
                                drm_output_destroy(&output->base);
                        }
                }
@@ -1799,11 +1797,11 @@ drm_restore(struct weston_compositor *ec)
 
 static const char default_seat[] = "seat0";
 
-static void
+static int
 device_added(struct udev_device *udev_device, struct drm_seat *master)
 {
        struct weston_compositor *c;
-       struct evdev_input_device *device;
+       struct evdev_device *device;
        const char *devnode;
        const char *device_seat;
        int fd;
@@ -1813,31 +1811,37 @@ device_added(struct udev_device *udev_device, struct drm_seat *master)
                device_seat = default_seat;
 
        if (strcmp(device_seat, master->seat_id))
-               return;
+               return 0;
 
        c = master->base.compositor;
        devnode = udev_device_get_devnode(udev_device);
 
        /* Use non-blocking mode so that we can loop on read on
-        * evdev_input_device_data() until all events on the fd are
+        * evdev_device_data() until all events on the fd are
         * read.  mtdev_get() also expects this. */
        fd = weston_launcher_open(c, devnode, O_RDWR | O_NONBLOCK);
        if (fd < 0) {
                weston_log("opening input device '%s' failed.\n", devnode);
-               return;
+               return -1;
        }
 
-       device = evdev_input_device_create(&master->base, devnode, fd);
-       if (!device) {
+       device = evdev_device_create(&master->base, devnode, fd);
+       if (device == EVDEV_UNHANDLED_DEVICE) {
                close(fd);
                weston_log("not using input device '%s'.\n", devnode);
-               return;
+               return 0;
+       } else if (device == NULL) {
+               close(fd);
+               weston_log("failed to create input device '%s'.\n", devnode);
+               return -1;
        }
 
        wl_list_insert(master->devices_list.prev, &device->link);
+
+       return 0;
 }
 
-static void
+static int
 evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
 {
        struct drm_seat *seat = (struct drm_seat *) seat_base;
@@ -1859,7 +1863,11 @@ evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
                        continue;
                }
 
-               device_added(device, seat);
+               if (device_added(device, seat) < 0) {
+                       udev_device_unref(device);
+                       udev_enumerate_unref(e);
+                       return -1;
+               }
 
                udev_device_unref(device);
        }
@@ -1876,6 +1884,8 @@ evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
                        "(Weston backend option 'seat', "
                        "udev device property ID_SEAT)\n");
        }
+
+       return 0;
 }
 
 static int
@@ -1883,7 +1893,7 @@ evdev_udev_handler(int fd, uint32_t mask, void *data)
 {
        struct drm_seat *seat = data;
        struct udev_device *udev_device;
-       struct evdev_input_device *device, *next;
+       struct evdev_device *device, *next;
        const char *action;
        const char *devnode;
 
@@ -1907,7 +1917,7 @@ evdev_udev_handler(int fd, uint32_t mask, void *data)
                        if (!strcmp(device->devnode, devnode)) {
                                weston_log("input device %s, %s removed\n",
                                           device->devname, device->devnode);
-                               evdev_input_device_destroy(device);
+                               evdev_device_destroy(device);
                                break;
                        }
        }
@@ -1972,11 +1982,13 @@ static void
 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
 {
        struct drm_seat *seat = (struct drm_seat *) seat_base;
+       struct evdev_device *device;
 
-       evdev_led_update(&seat->devices_list, leds);
+       wl_list_for_each(device, &seat->devices_list, link)
+               evdev_led_update(device, leds);
 }
 
-static void
+static struct drm_seat *
 evdev_input_create(struct weston_compositor *c, struct udev *udev,
                   const char *seat_id)
 {
@@ -1984,7 +1996,7 @@ evdev_input_create(struct weston_compositor *c, struct udev *udev,
 
        seat = malloc(sizeof *seat);
        if (seat == NULL)
-               return;
+               return NULL;
 
        memset(seat, 0, sizeof *seat);
        weston_seat_init(&seat->base, c);
@@ -1992,27 +2004,30 @@ evdev_input_create(struct weston_compositor *c, struct udev *udev,
 
        wl_list_init(&seat->devices_list);
        seat->seat_id = strdup(seat_id);
-       if (!evdev_enable_udev_monitor(udev, &seat->base)) {
-               free(seat->seat_id);
-               free(seat);
-               return;
-       }
+       if (!evdev_enable_udev_monitor(udev, &seat->base))
+               goto err;
+       if (evdev_add_devices(udev, &seat->base) < 0)
+               goto err;
 
-       evdev_add_devices(udev, &seat->base);
+       return seat;
 
-       c->seat = &seat->base;
+ err:
+       free(seat->seat_id);
+       free(seat);
+       return NULL;
 }
 
 static void
 evdev_remove_devices(struct weston_seat *seat_base)
 {
        struct drm_seat *seat = (struct drm_seat *) seat_base;
-       struct evdev_input_device *device, *next;
+       struct evdev_device *device, *next;
 
        wl_list_for_each_safe(device, next, &seat->devices_list, link)
-               evdev_input_device_destroy(device);
+               evdev_device_destroy(device);
 
-       notify_keyboard_focus_out(&seat->base.seat);
+       if (seat->base.seat.keyboard)
+               notify_keyboard_focus_out(&seat->base);
 }
 
 static void
@@ -2053,6 +2068,8 @@ drm_destroy(struct weston_compositor *ec)
 
        weston_compositor_shutdown(ec);
 
+       gles2_renderer_destroy(ec);
+
        /* Work around crash in egl_dri2.c's dri2_make_current() */
        eglMakeCurrent(ec->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
                       EGL_NO_CONTEXT);
@@ -2162,18 +2179,81 @@ switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
        tty_activate_vt(ec->tty, key - KEY_F1 + 1);
 }
 
+/*
+ * Find primary GPU
+ * Some systems may have multiple DRM devices attached to a single seat. This
+ * function loops over all devices and tries to find a PCI device with the
+ * boot_vga sysfs attribute set to 1.
+ * If no such device is found, the first DRM device reported by udev is used.
+ */
+static struct udev_device*
+find_primary_gpu(struct drm_compositor *ec, const char *seat)
+{
+       struct udev_enumerate *e;
+       struct udev_list_entry *entry;
+       const char *path, *device_seat, *id;
+       struct udev_device *device, *drm_device, *pci;
+
+       e = udev_enumerate_new(ec->udev);
+       udev_enumerate_add_match_subsystem(e, "drm");
+       udev_enumerate_add_match_sysname(e, "card[0-9]*");
+
+       udev_enumerate_scan_devices(e);
+       drm_device = NULL;
+       udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
+               path = udev_list_entry_get_name(entry);
+               device = udev_device_new_from_syspath(ec->udev, path);
+               if (!device)
+                       continue;
+               device_seat = udev_device_get_property_value(device, "ID_SEAT");
+               if (!device_seat)
+                       device_seat = default_seat;
+               if (strcmp(device_seat, seat)) {
+                       udev_device_unref(device);
+                       continue;
+               }
+
+               pci = udev_device_get_parent_with_subsystem_devtype(device,
+                                                               "pci", NULL);
+               if (pci) {
+                       id = udev_device_get_sysattr_value(pci, "boot_vga");
+                       if (id && !strcmp(id, "1")) {
+                               if (drm_device)
+                                       udev_device_unref(drm_device);
+                               drm_device = device;
+                               break;
+                       }
+               }
+
+               if (!drm_device)
+                       drm_device = device;
+               else
+                       udev_device_unref(device);
+       }
+
+       udev_enumerate_unref(e);
+       return drm_device;
+}
+
+static void
+hide_sprites_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
+                    void *data)
+{
+       struct drm_compositor *c = data;
+
+       c->sprites_hidden ^= 1;
+}
+
 static struct weston_compositor *
 drm_compositor_create(struct wl_display *display,
                      int connector, const char *seat, int tty,
                      int argc, char *argv[], const char *config_file)
 {
        struct drm_compositor *ec;
-       struct udev_enumerate *e;
-       struct udev_list_entry *entry;
-       struct udev_device *device, *drm_device;
-       const char *path, *device_seat;
+       struct udev_device *drm_device;
        struct wl_event_loop *loop;
        struct weston_seat *weston_seat, *next;
+       const char *path;
        uint32_t key;
 
        weston_log("initializing drm backend\n");
@@ -2183,6 +2263,10 @@ drm_compositor_create(struct wl_display *display,
                return NULL;
        memset(ec, 0, sizeof *ec);
 
+       /* KMS support for sprites is not complete yet, so disable the
+        * functionality for now. */
+       ec->sprites_are_broken = 1;
+
        if (weston_compositor_init(&ec->base, display, argc, argv,
                                   config_file) < 0) {
                weston_log("weston_compositor_init failed\n");
@@ -2202,30 +2286,12 @@ drm_compositor_create(struct wl_display *display,
                goto err_udev;
        }
 
-       e = udev_enumerate_new(ec->udev);
-       udev_enumerate_add_match_subsystem(e, "drm");
-       udev_enumerate_add_match_sysname(e, "card[0-9]*");
-
-       udev_enumerate_scan_devices(e);
-       drm_device = NULL;
-       udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
-               path = udev_list_entry_get_name(entry);
-               device = udev_device_new_from_syspath(ec->udev, path);
-               device_seat =
-                       udev_device_get_property_value(device, "ID_SEAT");
-               if (!device_seat)
-                       device_seat = default_seat;
-               if (strcmp(device_seat, seat) == 0) {
-                       drm_device = device;
-                       break;
-               }
-               udev_device_unref(device);
-       }
-
+       drm_device = find_primary_gpu(ec, seat);
        if (drm_device == NULL) {
                weston_log("no drm device found\n");
-               goto err_udev_enum;
+               goto err_tty;
        }
+       path = udev_device_get_syspath(drm_device);
 
        if (init_egl(ec, drm_device) < 0) {
                weston_log("failed to initialize egl\n");
@@ -2239,9 +2305,6 @@ drm_compositor_create(struct wl_display *display,
 
        ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
 
-       if (weston_compositor_init_gl(&ec->base) < 0)
-               goto err_egl;
-
        for (key = KEY_F1; key < KEY_F9; key++)
                weston_compositor_add_key_binding(&ec->base, key,
                                                  MODIFIER_CTRL | MODIFIER_ALT,
@@ -2255,9 +2318,15 @@ drm_compositor_create(struct wl_display *display,
                goto err_sprite;
        }
 
+       if (gles2_renderer_init(&ec->base) < 0)
+               goto err_egl;
+
        path = NULL;
 
-       evdev_input_create(&ec->base, ec->udev, seat);
+       if (evdev_input_create(&ec->base, ec->udev, seat) == NULL) {
+               weston_log("failed to create input devices\n");
+               goto err_sprite;
+       }
 
        loop = wl_display_get_event_loop(ec->base.wl_display);
        ec->drm_source =
@@ -2282,7 +2351,10 @@ drm_compositor_create(struct wl_display *display,
        }
 
        udev_device_unref(drm_device);
-       udev_enumerate_unref(e);
+
+       weston_compositor_add_key_binding(&ec->base, KEY_O,
+                                         MODIFIER_CTRL | MODIFIER_ALT,
+                                         hide_sprites_binding, ec);
 
        return &ec->base;
 
@@ -2293,18 +2365,19 @@ err_drm_source:
        wl_event_source_remove(ec->drm_source);
        wl_list_for_each_safe(weston_seat, next, &ec->base.seat_list, link)
                evdev_input_destroy(weston_seat);
-err_sprite:
-       destroy_sprites(ec);
 err_egl:
        eglMakeCurrent(ec->base.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
                       EGL_NO_CONTEXT);
        eglTerminate(ec->base.egl_display);
        eglReleaseThread();
        gbm_device_destroy(ec->gbm);
+err_sprite:
+       destroy_sprites(ec);
 err_udev_dev:
        udev_device_unref(drm_device);
-err_udev_enum:
-       udev_enumerate_unref(e);
+err_tty:
+       if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
+               weston_log("failed to drop master: %m\n");
        tty_destroy(ec->tty);
 err_udev:
        udev_unref(ec->udev);
@@ -2375,17 +2448,55 @@ check_for_modeline(struct drm_configured_output *output)
 }
 
 static void
+drm_output_set_transform(struct drm_configured_output *output)
+{
+       if (!output_transform) {
+               output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
+               return;
+       }
+
+       if (!strcmp(output_transform, "normal"))
+               output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
+       else if (!strcmp(output_transform, "90"))
+               output->transform = WL_OUTPUT_TRANSFORM_90;
+       else if (!strcmp(output_transform, "180"))
+               output->transform = WL_OUTPUT_TRANSFORM_180;
+       else if (!strcmp(output_transform, "270"))
+               output->transform = WL_OUTPUT_TRANSFORM_270;
+       else if (!strcmp(output_transform, "flipped"))
+               output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
+       else if (!strcmp(output_transform, "flipped-90"))
+               output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
+       else if (!strcmp(output_transform, "flipped-180"))
+               output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
+       else if (!strcmp(output_transform, "flipped-270"))
+               output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
+       else {
+               weston_log("Invalid transform \"%s\" for output %s\n",
+                                               output_transform, output_name);
+               output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
+       }
+
+       free(output_transform);
+       output_transform = NULL;
+}
+
+static void
 output_section_done(void *data)
 {
        struct drm_configured_output *output;
 
        output = malloc(sizeof *output);
 
-       if (!output || !output_name || !output_mode) {
+       if (!output || !output_name || (output_name[0] == 'X') ||
+                                       (!output_mode && !output_transform)) {
                free(output_name);
-               output_name = NULL;
                free(output_mode);
+               free(output_transform);
+               free(output);
+               output_name = NULL;
                output_mode = NULL;
+               output_transform = NULL;
                return;
        }
 
@@ -2393,24 +2504,32 @@ output_section_done(void *data)
        output->name = output_name;
        output->mode = output_mode;
 
-       if (strcmp(output_mode, "off") == 0)
-               output->config = OUTPUT_CONFIG_OFF;
-       else if (strcmp(output_mode, "preferred") == 0)
-               output->config = OUTPUT_CONFIG_PREFERRED;
-       else if (strcmp(output_mode, "current") == 0)
-               output->config = OUTPUT_CONFIG_CURRENT;
-       else if (sscanf(output_mode, "%dx%d", &output->width, &output->height) == 2)
-               output->config = OUTPUT_CONFIG_MODE;
-       else if (check_for_modeline(output) == 0)
-               output->config = OUTPUT_CONFIG_MODELINE;
-
-       if (output->config != OUTPUT_CONFIG_INVALID)
-               wl_list_insert(&configured_output_list, &output->link);
-       else {
-               weston_log("Invalid mode \"%s\" for output %s\n",
-                                               output_mode, output_name);
-               drm_free_configured_output(output);
+       if (output_mode) {
+               if (strcmp(output_mode, "off") == 0)
+                       output->config = OUTPUT_CONFIG_OFF;
+               else if (strcmp(output_mode, "preferred") == 0)
+                       output->config = OUTPUT_CONFIG_PREFERRED;
+               else if (strcmp(output_mode, "current") == 0)
+                       output->config = OUTPUT_CONFIG_CURRENT;
+               else if (sscanf(output_mode, "%dx%d",
+                                       &output->width, &output->height) == 2)
+                       output->config = OUTPUT_CONFIG_MODE;
+               else if (check_for_modeline(output) == 0)
+                       output->config = OUTPUT_CONFIG_MODELINE;
+
+               if (output->config == OUTPUT_CONFIG_INVALID)
+                       weston_log("Invalid mode \"%s\" for output %s\n",
+                                                       output_mode, output_name);
+               output_mode = NULL;
        }
+
+       drm_output_set_transform(output);
+
+       wl_list_insert(&configured_output_list, &output->link);
+
+       if (output_transform)
+               free(output_transform);
+       output_transform = NULL;
 }
 
 WL_EXPORT struct weston_compositor *
@@ -2434,6 +2553,7 @@ backend_init(struct wl_display *display, int argc, char *argv[],
        const struct config_key drm_config_keys[] = {
                { "name", CONFIG_KEY_STRING, &output_name },
                { "mode", CONFIG_KEY_STRING, &output_mode },
+               { "transform", CONFIG_KEY_STRING, &output_transform },
        };
 
        const struct config_section config_section[] = {