OSDN Git Service

modetest: Fix segmentation fault
[android-x86/external-libdrm.git] / tests / modetest / modetest.c
index 62d9332..09fd565 100644 (file)
  *       the mode has been programmed, along with possible test patterns.
  */
 
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
 #include <assert.h>
 #include <ctype.h>
 #include <stdbool.h>
@@ -58,6 +54,7 @@
 #ifdef HAVE_SYS_SELECT_H
 #include <sys/select.h>
 #endif
+#include <math.h>
 
 #include "xf86drm.h"
 #include "xf86drmMode.h"
@@ -71,6 +68,9 @@
 #include "buffers.h"
 #include "cursor.h"
 
+static enum util_fill_pattern primary_fill = UTIL_PATTERN_SMPTE;
+static enum util_fill_pattern secondary_fill = UTIL_PATTERN_TILES;
+
 struct crtc {
        drmModeCrtc *crtc;
        drmModeObjectProperties *props;
@@ -123,6 +123,9 @@ struct device {
                struct bo *bo;
                struct bo *cursor_bo;
        } mode;
+
+       int use_atomic;
+       drmModeAtomicReq *req;
 };
 
 static inline int64_t U642I64(uint64_t val)
@@ -130,6 +133,12 @@ static inline int64_t U642I64(uint64_t val)
        return (int64_t)*((int64_t *)&val);
 }
 
+static float mode_vrefresh(drmModeModeInfo *mode)
+{
+       return  mode->clock * 1000.00
+                       / (mode->htotal * mode->vtotal);
+}
+
 #define bit_name_fn(res)                                       \
 const char * res##_str(int type) {                             \
        unsigned int i;                                         \
@@ -205,11 +214,12 @@ static void dump_encoders(struct device *dev)
        printf("\n");
 }
 
-static void dump_mode(drmModeModeInfo *mode)
+static void dump_mode(drmModeModeInfo *mode, int index)
 {
-       printf("  %s %d %d %d %d %d %d %d %d %d %d",
+       printf("  #%i %s %.2f %d %d %d %d %d %d %d %d %d",
+              index,
               mode->name,
-              mode->vrefresh,
+              mode_vrefresh(mode),
               mode->hdisplay,
               mode->hsync_start,
               mode->hsync_end,
@@ -278,22 +288,24 @@ static const char *modifier_to_string(uint64_t modifier)
                return "VIVANTE_SPLIT_TILED";
        case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
                return "VIVANTE_SPLIT_SUPER_TILED";
-       case NV_FORMAT_MOD_TEGRA_TILED:
-               return "MOD_TEGRA_TILED";
-       case NV_FORMAT_MOD_TEGRA_16BX2_BLOCK(0):
-               return "MOD_TEGRA_16BX2_BLOCK(0)";
-       case NV_FORMAT_MOD_TEGRA_16BX2_BLOCK(1):
-               return "MOD_TEGRA_16BX2_BLOCK(1)";
-       case NV_FORMAT_MOD_TEGRA_16BX2_BLOCK(2):
-               return "MOD_TEGRA_16BX2_BLOCK(2)";
-       case NV_FORMAT_MOD_TEGRA_16BX2_BLOCK(3):
-               return "MOD_TEGRA_16BX2_BLOCK(3)";
-       case NV_FORMAT_MOD_TEGRA_16BX2_BLOCK(4):
-               return "MOD_TEGRA_16BX2_BLOCK(4)";
-       case NV_FORMAT_MOD_TEGRA_16BX2_BLOCK(5):
-               return "MOD_TEGRA_16BX2_BLOCK(5)";
+       case DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED:
+               return "NVIDIA_TEGRA_TILED";
+       case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0):
+               return "NVIDIA_16BX2_BLOCK(0)";
+       case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1):
+               return "NVIDIA_16BX2_BLOCK(1)";
+       case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2):
+               return "NVIDIA_16BX2_BLOCK(2)";
+       case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3):
+               return "NVIDIA_16BX2_BLOCK(3)";
+       case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4):
+               return "NVIDIA_16BX2_BLOCK(4)";
+       case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5):
+               return "NVIDIA_16BX2_BLOCK(5)";
        case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
                return "MOD_BROADCOM_VC4_T_TILED";
+       case DRM_FORMAT_MOD_QCOM_COMPRESSED:
+               return "QCOM_COMPRESSED";
        default:
                return "(UNKNOWN MODIFIER)";
        }
@@ -442,10 +454,10 @@ static void dump_connectors(struct device *dev)
 
                if (connector->count_modes) {
                        printf("  modes:\n");
-                       printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
+                       printf("\tindex name refresh (Hz) hdisp hss hse htot vdisp "
                               "vss vse vtot)\n");
                        for (j = 0; j < connector->count_modes; j++)
-                               dump_mode(&connector->modes[j]);
+                               dump_mode(&connector->modes[j], j);
                }
 
                if (_connector->props) {
@@ -477,7 +489,7 @@ static void dump_crtcs(struct device *dev)
                       crtc->buffer_id,
                       crtc->x, crtc->y,
                       crtc->width, crtc->height);
-               dump_mode(&crtc->mode);
+               dump_mode(&crtc->mode, 0);
 
                if (_crtc->props) {
                        printf("  props:\n");
@@ -656,10 +668,13 @@ static struct resources *get_resources(struct device *dev)
        for (i = 0; i < res->res->count_connectors; i++) {
                struct connector *connector = &res->connectors[i];
                drmModeConnector *conn = connector->connector;
+               int num;
 
-               asprintf(&connector->name, "%s-%u",
+               num = asprintf(&connector->name, "%s-%u",
                         util_lookup_connector_type_name(conn->connector_type),
                         conn->connector_type_id);
+               if (num < 0)
+                       goto error;
        }
 
 #define get_properties(_res, __res, type, Type)                                        \
@@ -788,7 +803,7 @@ struct pipe_arg {
        uint32_t crtc_id;
        char mode_str[64];
        char format_str[5];
-       unsigned int vrefresh;
+       float vrefresh;
        unsigned int fourcc;
        drmModeModeInfo *mode;
        struct crtc *crtc;
@@ -806,14 +821,16 @@ struct plane_arg {
        uint32_t w, h;
        double scale;
        unsigned int fb_id;
+       unsigned int old_fb_id;
        struct bo *bo;
+       struct bo *old_bo;
        char format_str[5]; /* need to leave room for terminating \0 */
        unsigned int fourcc;
 };
 
 static drmModeModeInfo *
 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
-        const unsigned int vrefresh)
+       const float vrefresh)
 {
        drmModeConnector *connector;
        drmModeModeInfo *mode;
@@ -823,16 +840,27 @@ connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
        if (!connector || !connector->count_modes)
                return NULL;
 
+       /* Pick by Index */
+       if (mode_str[0] == '#') {
+               int index = atoi(mode_str + 1);
+
+               if (index >= connector->count_modes || index < 0)
+                       return NULL;
+               return &connector->modes[index];
+       }
+
+       /* Pick by Name */
        for (i = 0; i < connector->count_modes; i++) {
                mode = &connector->modes[i];
                if (!strcmp(mode->name, mode_str)) {
-                       /* If the vertical refresh frequency is not specified then return the
-                        * first mode that match with the name. Else, return the mode that match
-                        * the name and the specified vertical refresh frequency.
+                       /* If the vertical refresh frequency is not specified
+                        * then return the first mode that match with the name.
+                        * Else, return the mode that match the name and
+                        * the specified vertical refresh frequency.
                         */
                        if (vrefresh == 0)
                                return mode;
-                       else if (mode->vrefresh == vrefresh)
+                       else if (fabs(mode_vrefresh(mode) - vrefresh) < 0.005)
                                return mode;
                }
        }
@@ -898,7 +926,13 @@ static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
                mode = connector_find_mode(dev, pipe->con_ids[i],
                                           pipe->mode_str, pipe->vrefresh);
                if (mode == NULL) {
-                       fprintf(stderr,
+                       if (pipe->vrefresh)
+                               fprintf(stderr,
+                               "failed to find mode "
+                               "\"%s-%.2fHz\" for connector %s\n",
+                               pipe->mode_str, pipe->vrefresh, pipe->cons[i]);
+                       else
+                               fprintf(stderr,
                                "failed to find mode \"%s\" for connector %s\n",
                                pipe->mode_str, pipe->cons[i]);
                        return -EINVAL;
@@ -942,9 +976,10 @@ struct property_arg {
        char name[DRM_PROP_NAME_LEN+1];
        uint32_t prop_id;
        uint64_t value;
+       bool optional;
 };
 
-static void set_property(struct device *dev, struct property_arg *p)
+static bool set_property(struct device *dev, struct property_arg *p)
 {
        drmModeObjectProperties *props = NULL;
        drmModePropertyRes **props_info = NULL;
@@ -976,13 +1011,13 @@ static void set_property(struct device *dev, struct property_arg *p)
        if (p->obj_type == 0) {
                fprintf(stderr, "Object %i not found, can't set property\n",
                        p->obj_id);
-                       return;
+               return false;
        }
 
        if (!props) {
                fprintf(stderr, "%s %i has no properties\n",
                        obj_type, p->obj_id);
-               return;
+               return false;
        }
 
        for (i = 0; i < (int)props->count_props; ++i) {
@@ -993,18 +1028,25 @@ static void set_property(struct device *dev, struct property_arg *p)
        }
 
        if (i == (int)props->count_props) {
-               fprintf(stderr, "%s %i has no %s property\n",
-                       obj_type, p->obj_id, p->name);
-               return;
+               if (!p->optional)
+                       fprintf(stderr, "%s %i has no %s property\n",
+                               obj_type, p->obj_id, p->name);
+               return false;
        }
 
        p->prop_id = props->props[i];
 
-       ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
-                                      p->prop_id, p->value);
+       if (!dev->use_atomic)
+               ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
+                                                                          p->prop_id, p->value);
+       else
+               ret = drmModeAtomicAddProperty(dev->req, p->obj_id, p->prop_id, p->value);
+
        if (ret < 0)
                fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
                        obj_type, p->obj_id, p->name, p->value, strerror(errno));
+
+       return true;
 }
 
 /* -------------------------------------------------------------------------- */
@@ -1050,6 +1092,143 @@ static bool format_support(const drmModePlanePtr ovr, uint32_t fmt)
        return false;
 }
 
+static void add_property(struct device *dev, uint32_t obj_id,
+                              const char *name, uint64_t value)
+{
+       struct property_arg p;
+
+       p.obj_id = obj_id;
+       strcpy(p.name, name);
+       p.value = value;
+
+       set_property(dev, &p);
+}
+
+static bool add_property_optional(struct device *dev, uint32_t obj_id,
+                                 const char *name, uint64_t value)
+{
+       struct property_arg p;
+
+       p.obj_id = obj_id;
+       strcpy(p.name, name);
+       p.value = value;
+       p.optional = true;
+
+       return set_property(dev, &p);
+}
+
+static void set_gamma(struct device *dev, unsigned crtc_id, unsigned fourcc)
+{
+       unsigned blob_id = 0;
+       /* TODO: support 1024-sized LUTs, when the use-case arises */
+       struct drm_color_lut gamma_lut[256];
+       int i, ret;
+
+       if (fourcc == DRM_FORMAT_C8) {
+               /* TODO: Add C8 support for more patterns */
+               util_smpte_c8_gamma(256, gamma_lut);
+               drmModeCreatePropertyBlob(dev->fd, gamma_lut, sizeof(gamma_lut), &blob_id);
+       } else {
+               for (i = 0; i < 256; i++) {
+                       gamma_lut[i].red =
+                       gamma_lut[i].green =
+                       gamma_lut[i].blue = i << 8;
+               }
+       }
+
+       add_property_optional(dev, crtc_id, "DEGAMMA_LUT", 0);
+       add_property_optional(dev, crtc_id, "CTM", 0);
+       if (!add_property_optional(dev, crtc_id, "GAMMA_LUT", blob_id)) {
+               uint16_t r[256], g[256], b[256];
+
+               for (i = 0; i < 256; i++) {
+                       r[i] = gamma_lut[i].red;
+                       g[i] = gamma_lut[i].green;
+                       b[i] = gamma_lut[i].blue;
+               }
+
+               ret = drmModeCrtcSetGamma(dev->fd, crtc_id, 256, r, g, b);
+               if (ret)
+                       fprintf(stderr, "failed to set gamma: %s\n", strerror(errno));
+       }
+}
+
+static int atomic_set_plane(struct device *dev, struct plane_arg *p,
+                                                       int pattern, bool update)
+{
+       uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
+       struct bo *plane_bo;
+       int crtc_x, crtc_y, crtc_w, crtc_h;
+       struct crtc *crtc = NULL;
+       unsigned int i;
+       unsigned int old_fb_id;
+
+       /* Find an unused plane which can be connected to our CRTC. Find the
+        * CRTC index first, then iterate over available planes.
+        */
+       for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
+               if (p->crtc_id == dev->resources->res->crtcs[i]) {
+                       crtc = &dev->resources->crtcs[i];
+                       break;
+               }
+       }
+
+       if (!crtc) {
+               fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
+               return -1;
+       }
+
+       if (!update)
+               fprintf(stderr, "testing %dx%d@%s on plane %u, crtc %u\n",
+                       p->w, p->h, p->format_str, p->plane_id, p->crtc_id);
+
+       plane_bo = p->old_bo;
+       p->old_bo = p->bo;
+
+       if (!plane_bo) {
+               plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h,
+                                    handles, pitches, offsets, pattern);
+
+               if (plane_bo == NULL)
+                       return -1;
+
+               if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
+                       handles, pitches, offsets, &p->fb_id, 0)) {
+                       fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
+                       return -1;
+               }
+       }
+
+       p->bo = plane_bo;
+
+       old_fb_id = p->fb_id;
+       p->old_fb_id = old_fb_id;
+
+       crtc_w = p->w * p->scale;
+       crtc_h = p->h * p->scale;
+       if (!p->has_position) {
+               /* Default to the middle of the screen */
+               crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
+               crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
+       } else {
+               crtc_x = p->x;
+               crtc_y = p->y;
+       }
+
+       add_property(dev, p->plane_id, "FB_ID", p->fb_id);
+       add_property(dev, p->plane_id, "CRTC_ID", p->crtc_id);
+       add_property(dev, p->plane_id, "SRC_X", 0);
+       add_property(dev, p->plane_id, "SRC_Y", 0);
+       add_property(dev, p->plane_id, "SRC_W", p->w << 16);
+       add_property(dev, p->plane_id, "SRC_H", p->h << 16);
+       add_property(dev, p->plane_id, "CRTC_X", crtc_x);
+       add_property(dev, p->plane_id, "CRTC_Y", crtc_y);
+       add_property(dev, p->plane_id, "CRTC_W", crtc_w);
+       add_property(dev, p->plane_id, "CRTC_H", crtc_h);
+
+       return 0;
+}
+
 static int set_plane(struct device *dev, struct plane_arg *p)
 {
        drmModePlane *ovr;
@@ -1108,7 +1287,7 @@ static int set_plane(struct device *dev, struct plane_arg *p)
                p->w, p->h, p->format_str, plane_id);
 
        plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h, handles,
-                            pitches, offsets, UTIL_PATTERN_TILES);
+                            pitches, offsets, secondary_fill);
        if (plane_bo == NULL)
                return -1;
 
@@ -1146,6 +1325,66 @@ static int set_plane(struct device *dev, struct plane_arg *p)
        return 0;
 }
 
+static void atomic_set_planes(struct device *dev, struct plane_arg *p,
+                             unsigned int count, bool update)
+{
+       unsigned int i, pattern = primary_fill;
+
+       /* set up planes */
+       for (i = 0; i < count; i++) {
+               if (i > 0)
+                       pattern = secondary_fill;
+               else
+                       set_gamma(dev, p[i].crtc_id, p[i].fourcc);
+
+               if (atomic_set_plane(dev, &p[i], pattern, update))
+                       return;
+       }
+}
+
+static void atomic_clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
+{
+       unsigned int i;
+
+       for (i = 0; i < count; i++) {
+               add_property(dev, p[i].plane_id, "FB_ID", 0);
+               add_property(dev, p[i].plane_id, "CRTC_ID", 0);
+               add_property(dev, p[i].plane_id, "SRC_X", 0);
+               add_property(dev, p[i].plane_id, "SRC_Y", 0);
+               add_property(dev, p[i].plane_id, "SRC_W", 0);
+               add_property(dev, p[i].plane_id, "SRC_H", 0);
+               add_property(dev, p[i].plane_id, "CRTC_X", 0);
+               add_property(dev, p[i].plane_id, "CRTC_Y", 0);
+               add_property(dev, p[i].plane_id, "CRTC_W", 0);
+               add_property(dev, p[i].plane_id, "CRTC_H", 0);
+       }
+}
+
+static void atomic_clear_FB(struct device *dev, struct plane_arg *p, unsigned int count)
+{
+       unsigned int i;
+
+       for (i = 0; i < count; i++) {
+               if (p[i].fb_id) {
+                       drmModeRmFB(dev->fd, p[i].fb_id);
+                       p[i].fb_id = 0;
+               }
+               if (p[i].old_fb_id) {
+                       drmModeRmFB(dev->fd, p[i].old_fb_id);
+                       p[i].old_fb_id = 0;
+               }
+               if (p[i].bo) {
+                       bo_destroy(p[i].bo);
+                       p[i].bo = NULL;
+               }
+               if (p[i].old_bo) {
+                       bo_destroy(p[i].old_bo);
+                       p[i].old_bo = NULL;
+               }
+
+       }
+}
+
 static void clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
 {
        unsigned int i;
@@ -1158,6 +1397,59 @@ static void clear_planes(struct device *dev, struct plane_arg *p, unsigned int c
        }
 }
 
+static void atomic_set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
+{
+       unsigned int i;
+       unsigned int j;
+       int ret;
+
+       for (i = 0; i < count; i++) {
+               struct pipe_arg *pipe = &pipes[i];
+
+               ret = pipe_find_crtc_and_mode(dev, pipe);
+               if (ret < 0)
+                       continue;
+       }
+
+       for (i = 0; i < count; i++) {
+               struct pipe_arg *pipe = &pipes[i];
+               uint32_t blob_id;
+
+               if (pipe->mode == NULL)
+                       continue;
+
+               printf("setting mode %s-%.2fHz on connectors ",
+                      pipe->mode->name, mode_vrefresh(pipe->mode));
+               for (j = 0; j < pipe->num_cons; ++j) {
+                       printf("%s, ", pipe->cons[j]);
+                       add_property(dev, pipe->con_ids[j], "CRTC_ID", pipe->crtc->crtc->crtc_id);
+               }
+               printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
+
+               drmModeCreatePropertyBlob(dev->fd, pipe->mode, sizeof(*pipe->mode), &blob_id);
+               add_property(dev, pipe->crtc->crtc->crtc_id, "MODE_ID", blob_id);
+               add_property(dev, pipe->crtc->crtc->crtc_id, "ACTIVE", 1);
+       }
+}
+
+static void atomic_clear_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
+{
+       unsigned int i;
+       unsigned int j;
+
+       for (i = 0; i < count; i++) {
+               struct pipe_arg *pipe = &pipes[i];
+
+               if (pipe->mode == NULL)
+                       continue;
+
+               for (j = 0; j < pipe->num_cons; ++j)
+                       add_property(dev, pipe->con_ids[j], "CRTC_ID",0);
+
+               add_property(dev, pipe->crtc->crtc->crtc_id, "MODE_ID", 0);
+               add_property(dev, pipe->crtc->crtc->crtc_id, "ACTIVE", 0);
+       }
+}
 
 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
 {
@@ -1186,7 +1478,7 @@ static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int co
 
        bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
                       dev->mode.height, handles, pitches, offsets,
-                      UTIL_PATTERN_SMPTE);
+                      primary_fill);
        if (bo == NULL)
                return;
 
@@ -1209,8 +1501,9 @@ static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int co
                if (pipe->mode == NULL)
                        continue;
 
-               printf("setting mode %s-%dHz@%s on connectors ",
-                      pipe->mode_str, pipe->mode->vrefresh, pipe->format_str);
+               printf("setting mode %s-%.2fHz@%s on connectors ",
+                      pipe->mode->name, mode_vrefresh(pipe->mode),
+                      pipe->format_str);
                for (j = 0; j < pipe->num_cons; ++j)
                        printf("%s, ", pipe->cons[j]);
                printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
@@ -1228,6 +1521,8 @@ static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int co
                        fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
                        return;
                }
+
+               set_gamma(dev, pipe->crtc->crtc->crtc_id, pipe->fourcc);
        }
 }
 
@@ -1426,6 +1721,8 @@ static int parse_connector(struct pipe_arg *pipe, const char *arg)
                return -1;
 
        /* Parse the remaining parameters. */
+       if (!endp)
+               return -1;
        if (*endp == '@') {
                arg = endp + 1;
                pipe->crtc_id = strtoul(arg, &endp, 10);
@@ -1444,7 +1741,7 @@ static int parse_connector(struct pipe_arg *pipe, const char *arg)
        pipe->mode_str[len] = '\0';
 
        if (*p == '-') {
-               pipe->vrefresh = strtoul(p + 1, &endp, 10);
+               pipe->vrefresh = strtof(p + 1, &endp);
                p = endp;
        }
 
@@ -1502,11 +1799,8 @@ static int parse_plane(struct plane_arg *plane, const char *p)
        }
 
        if (*end == '@') {
-               p = end + 1;
-               if (strlen(p) != 4)
-                       return -EINVAL;
-
-               strcpy(plane->format_str, p);
+               strncpy(plane->format_str, end + 1, 4);
+               plane->format_str[4] = '\0';
        } else {
                strcpy(plane->format_str, "XR24");
        }
@@ -1531,9 +1825,21 @@ static int parse_property(struct property_arg *p, const char *arg)
        return 0;
 }
 
+static void parse_fill_patterns(char *arg)
+{
+       char *fill = strtok(arg, ",");
+       if (!fill)
+               return;
+       primary_fill = util_pattern_enum(fill);
+       fill = strtok(NULL, ",");
+       if (!fill)
+               return;
+       secondary_fill = util_pattern_enum(fill);
+}
+
 static void usage(char *name)
 {
-       fprintf(stderr, "usage: %s [-cDdefMPpsCvw]\n", name);
+       fprintf(stderr, "usage: %s [-acDdefMPpsCvw]\n", name);
 
        fprintf(stderr, "\n Query options:\n\n");
        fprintf(stderr, "\t-c\tlist connectors\n");
@@ -1543,10 +1849,12 @@ static void usage(char *name)
 
        fprintf(stderr, "\n Test options:\n\n");
        fprintf(stderr, "\t-P <plane_id>@<crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
-       fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n");
+       fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:[#<mode index>]<mode>[-<vrefresh>][@<format>]\tset a mode\n");
        fprintf(stderr, "\t-C\ttest hw cursor\n");
        fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
        fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
+       fprintf(stderr, "\t-a \tuse atomic API\n");
+       fprintf(stderr, "\t-F pattern1,pattern2\tspecify fill patterns\n");
 
        fprintf(stderr, "\n Generic options:\n\n");
        fprintf(stderr, "\t-d\tdrop master after mode set\n");
@@ -1610,7 +1918,7 @@ static int pipe_resolve_connectors(struct device *dev, struct pipe_arg *pipe)
        return 0;
 }
 
-static char optstr[] = "cdD:efM:P:ps:Cvw:";
+static char optstr[] = "acdD:efF:M:P:ps:Cvw:";
 
 int main(int argc, char **argv)
 {
@@ -1621,6 +1929,7 @@ int main(int argc, char **argv)
        int drop_master = 0;
        int test_vsync = 0;
        int test_cursor = 0;
+       int use_atomic = 0;
        char *device = NULL;
        char *module = NULL;
        unsigned int i;
@@ -1639,6 +1948,9 @@ int main(int argc, char **argv)
                args++;
 
                switch (c) {
+               case 'a':
+                       use_atomic = 1;
+                       break;
                case 'c':
                        connectors = 1;
                        break;
@@ -1655,6 +1967,9 @@ int main(int argc, char **argv)
                case 'f':
                        framebuffers = 1;
                        break;
+               case 'F':
+                       parse_fill_patterns(optarg);
+                       break;
                case 'M':
                        module = optarg;
                        /* Preserve the default behaviour of dumping all information. */
@@ -1718,13 +2033,22 @@ int main(int argc, char **argv)
                }
        }
 
-       if (!args)
+       if (!args || (args == 1 && use_atomic))
                encoders = connectors = crtcs = planes = framebuffers = 1;
 
        dev.fd = util_open(device, module);
        if (dev.fd < 0)
                return -1;
 
+       ret = drmSetClientCap(dev.fd, DRM_CLIENT_CAP_ATOMIC, 1);
+       if (ret && use_atomic) {
+               fprintf(stderr, "no atomic modesetting support: %s\n", strerror(errno));
+               drmClose(dev.fd);
+               return -1;
+       }
+
+       dev.use_atomic = use_atomic;
+
        if (test_vsync && !page_flipping_supported()) {
                fprintf(stderr, "page flipping not supported by drm.\n");
                return -1;
@@ -1765,40 +2089,111 @@ int main(int argc, char **argv)
        for (i = 0; i < prop_count; ++i)
                set_property(&dev, &prop_args[i]);
 
-       if (count || plane_count) {
-               uint64_t cap = 0;
+       if (dev.use_atomic) {
+               dev.req = drmModeAtomicAlloc();
 
-               ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
-               if (ret || cap == 0) {
-                       fprintf(stderr, "driver doesn't support the dumb buffer API\n");
-                       return 1;
+               if (count && plane_count) {
+                       uint64_t cap = 0;
+
+                       ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
+                       if (ret || cap == 0) {
+                               fprintf(stderr, "driver doesn't support the dumb buffer API\n");
+                               return 1;
+                       }
+
+                       atomic_set_mode(&dev, pipe_args, count);
+                       atomic_set_planes(&dev, plane_args, plane_count, false);
+
+                       ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
+                       if (ret) {
+                               fprintf(stderr, "Atomic Commit failed [1]\n");
+                               return 1;
+                       }
+
+                       gettimeofday(&pipe_args->start, NULL);
+                       pipe_args->swap_count = 0;
+
+                       while (test_vsync) {
+                               drmModeAtomicFree(dev.req);
+                               dev.req = drmModeAtomicAlloc();
+                               atomic_set_planes(&dev, plane_args, plane_count, true);
+
+                               ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
+                               if (ret) {
+                                       fprintf(stderr, "Atomic Commit failed [2]\n");
+                                       return 1;
+                               }
+
+                               pipe_args->swap_count++;
+                               if (pipe_args->swap_count == 60) {
+                                       struct timeval end;
+                                       double t;
+
+                                       gettimeofday(&end, NULL);
+                                       t = end.tv_sec + end.tv_usec * 1e-6 -
+                                   (pipe_args->start.tv_sec + pipe_args->start.tv_usec * 1e-6);
+                                       fprintf(stderr, "freq: %.02fHz\n", pipe_args->swap_count / t);
+                                       pipe_args->swap_count = 0;
+                                       pipe_args->start = end;
+                               }
+                       }
+
+                       if (drop_master)
+                               drmDropMaster(dev.fd);
+
+                       getchar();
+
+                       drmModeAtomicFree(dev.req);
+                       dev.req = drmModeAtomicAlloc();
+
+                       atomic_clear_mode(&dev, pipe_args, count);
+                       atomic_clear_planes(&dev, plane_args, plane_count);
+                       ret = drmModeAtomicCommit(dev.fd, dev.req, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
+                       if (ret) {
+                               fprintf(stderr, "Atomic Commit failed\n");
+                               return 1;
+                       }
+
+                       atomic_clear_FB(&dev, plane_args, plane_count);
                }
 
-               if (count)
-                       set_mode(&dev, pipe_args, count);
+               drmModeAtomicFree(dev.req);
+       } else {
+               if (count || plane_count) {
+                       uint64_t cap = 0;
+
+                       ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
+                       if (ret || cap == 0) {
+                               fprintf(stderr, "driver doesn't support the dumb buffer API\n");
+                               return 1;
+                       }
+
+                       if (count)
+                               set_mode(&dev, pipe_args, count);
 
-               if (plane_count)
-                       set_planes(&dev, plane_args, plane_count);
+                       if (plane_count)
+                               set_planes(&dev, plane_args, plane_count);
 
-               if (test_cursor)
-                       set_cursors(&dev, pipe_args, count);
+                       if (test_cursor)
+                               set_cursors(&dev, pipe_args, count);
 
-               if (test_vsync)
-                       test_page_flip(&dev, pipe_args, count);
+                       if (test_vsync)
+                               test_page_flip(&dev, pipe_args, count);
 
-               if (drop_master)
-                       drmDropMaster(dev.fd);
+                       if (drop_master)
+                               drmDropMaster(dev.fd);
 
-               getchar();
+                       getchar();
 
-               if (test_cursor)
-                       clear_cursors(&dev);
+                       if (test_cursor)
+                               clear_cursors(&dev);
 
-               if (plane_count)
-                       clear_planes(&dev, plane_args, plane_count);
+                       if (plane_count)
+                               clear_planes(&dev, plane_args, plane_count);
 
-               if (count)
-                       clear_mode(&dev);
+                       if (count)
+                               clear_mode(&dev);
+               }
        }
 
        free_resources(dev.resources);