blob: 21e2d3a595342f04c3a706cd412ea482ec7c866b [file] [log] [blame]
Gurchetan Singh0d714272017-01-26 11:58:49 -08001/*
2 * Copyright 2017 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
Haixia Shifc476462015-04-22 16:25:04 -07006
Gurchetan Singh0d714272017-01-26 11:58:49 -08007/*
8 * This file performs some sanity checks on the DRM atomic API. To run a test, please run the
9 * following command:
10 *
11 * atomictest <testname>
12 *
13 * To get a list of possible tests, run:
14 *
15 * atomictest
16 */
Haixia Shifc476462015-04-22 16:25:04 -070017
Gurchetan Singh0d714272017-01-26 11:58:49 -080018#include "bs_drm.h"
Haixia Shifc476462015-04-22 16:25:04 -070019
Dongseong Hwang9093afe2017-03-20 19:16:28 -070020#define CHECK(cond) \
21 do { \
22 if (!(cond)) { \
23 bs_debug_error("check %s failed", #cond); \
24 return -1; \
25 } \
Gurchetan Singh0d714272017-01-26 11:58:49 -080026 } while (0)
27
Dongseong Hwang9093afe2017-03-20 19:16:28 -070028#define CHECK_RESULT(ret) \
29 do { \
30 if ((ret) < 0) { \
31 bs_debug_error("failed with error: %d", ret); \
32 return -1; \
33 } \
Gurchetan Singh0d714272017-01-26 11:58:49 -080034 } while (0)
35
36#define CURSOR_SIZE 64
37
38static const uint32_t yuv_formats[] = {
Gurchetan Singh07a45152017-04-27 18:08:18 -070039 DRM_FORMAT_NV12, DRM_FORMAT_UYVY, DRM_FORMAT_YUYV, DRM_FORMAT_YVU420,
Gurchetan Singh0d714272017-01-26 11:58:49 -080040};
41
42static struct gbm_device *gbm = NULL;
43
44static void page_flip_handler(int fd, unsigned int sequence, unsigned int tv_sec,
45 unsigned int tv_usec, void *user_data)
Haixia Shifc476462015-04-22 16:25:04 -070046{
Gurchetan Singh0d714272017-01-26 11:58:49 -080047 // Nothing to do.
Haixia Shifc476462015-04-22 16:25:04 -070048}
49
Gurchetan Singh0d714272017-01-26 11:58:49 -080050struct atomictest_property {
51 uint32_t pid;
52 uint32_t value;
53};
Haixia Shifc476462015-04-22 16:25:04 -070054
Gurchetan Singh0d714272017-01-26 11:58:49 -080055struct atomictest_plane {
56 drmModePlane drm_plane;
57 struct gbm_bo *bo;
Haixia Shifc476462015-04-22 16:25:04 -070058
Gurchetan Singh0d714272017-01-26 11:58:49 -080059 uint32_t format_idx;
60
61 /* Properties. */
62 struct atomictest_property crtc_id;
63 struct atomictest_property crtc_x;
64 struct atomictest_property crtc_y;
65 struct atomictest_property crtc_w;
66 struct atomictest_property crtc_h;
67 struct atomictest_property fb_id;
68 struct atomictest_property src_x;
69 struct atomictest_property src_y;
70 struct atomictest_property src_w;
71 struct atomictest_property src_h;
72 struct atomictest_property type;
73 struct atomictest_property zpos;
74};
75
76struct atomictest_connector {
77 uint32_t connector_id;
78 struct atomictest_property crtc_id;
79 struct atomictest_property edid;
80 struct atomictest_property dpms;
81};
82
83struct atomictest_crtc {
84 uint32_t crtc_id;
85 uint32_t width;
86 uint32_t height;
87 uint32_t *primary_idx;
88 uint32_t *cursor_idx;
89 uint32_t *overlay_idx;
90 uint32_t num_primary;
91 uint32_t num_cursor;
92 uint32_t num_overlay;
93
94 struct atomictest_plane *planes;
95 struct atomictest_property mode_id;
96 struct atomictest_property active;
97};
98
99struct atomictest_mode {
100 uint32_t height;
101 uint32_t width;
102 uint32_t id;
103};
104
105struct atomictest_context {
106 int fd;
107 uint32_t num_crtcs;
108 uint32_t num_connectors;
109 uint32_t num_modes;
110
111 struct atomictest_connector *connectors;
112 struct atomictest_crtc *crtcs;
113 struct atomictest_mode *modes;
Dominik Behre0f1c182016-01-25 14:38:22 -0800114 drmModeAtomicReqPtr pset;
Gurchetan Singh0d714272017-01-26 11:58:49 -0800115 drmEventContext drm_event_ctx;
Dongseong Hwang9093afe2017-03-20 19:16:28 -0700116
117 struct bs_mapper *mapper;
Gurchetan Singh0d714272017-01-26 11:58:49 -0800118};
Haixia Shifc476462015-04-22 16:25:04 -0700119
Gurchetan Singh0d714272017-01-26 11:58:49 -0800120struct atomictest {
121 const char *name;
122 int (*run_test)(struct atomictest_context *ctx, struct atomictest_crtc *crtc);
123};
Haixia Shifc476462015-04-22 16:25:04 -0700124
Gurchetan Singh0d714272017-01-26 11:58:49 -0800125static int32_t get_format_idx(struct atomictest_plane *plane, uint32_t format)
126{
127 for (int32_t i = 0; i < plane->drm_plane.count_formats; i++)
128 if (plane->drm_plane.formats[i] == format)
129 return i;
130 return -1;
131}
132
133static void copy_drm_plane(drmModePlane *dest, drmModePlane *src)
134{
135 memcpy(dest, src, sizeof(drmModePlane));
136 dest->formats = calloc(src->count_formats, sizeof(uint32_t));
137 dest->format_modifiers =
138 calloc(src->count_format_modifiers, sizeof(struct drm_format_modifier));
139 memcpy(dest->formats, src->formats, src->count_formats * sizeof(uint32_t));
140 memcpy(dest->format_modifiers, src->format_modifiers,
141 src->count_format_modifiers * sizeof(struct drm_format_modifier));
142}
143
144static struct atomictest_plane *get_plane(struct atomictest_crtc *crtc, uint32_t idx, uint64_t type)
145{
146 uint32_t index;
147 switch (type) {
148 case DRM_PLANE_TYPE_OVERLAY:
149 index = crtc->overlay_idx[idx];
150 break;
151 case DRM_PLANE_TYPE_PRIMARY:
152 index = crtc->primary_idx[idx];
153 break;
154 case DRM_PLANE_TYPE_CURSOR:
155 index = crtc->cursor_idx[idx];
156 break;
157 default:
158 bs_debug_error("invalid plane type returned");
159 return NULL;
Haixia Shifc476462015-04-22 16:25:04 -0700160 }
161
Gurchetan Singh0d714272017-01-26 11:58:49 -0800162 return &crtc->planes[index];
163}
Haixia Shifc476462015-04-22 16:25:04 -0700164
Dongseong Hwangc19d6a62017-04-11 16:06:25 -0700165static void write_to_buffer(struct bs_mapper *mapper, struct gbm_bo *bo, uint32_t u32, uint16_t u16)
Gurchetan Singh0d714272017-01-26 11:58:49 -0800166{
167 void *map_data;
Gurchetan Singh0d714272017-01-26 11:58:49 -0800168 uint32_t num_ints;
169 uint32_t format = gbm_bo_get_format(bo);
Dongseong Hwangc19d6a62017-04-11 16:06:25 -0700170 void *addr = bs_mapper_map(mapper, bo, 0, &map_data);
Gurchetan Singh0d714272017-01-26 11:58:49 -0800171
172 if (format == GBM_FORMAT_RGB565 || format == GBM_FORMAT_BGR565) {
173 num_ints = gbm_bo_get_plane_size(bo, 0) / sizeof(uint16_t);
174 uint16_t *pixel = (uint16_t *)addr;
175 for (uint32_t i = 0; i < num_ints; i++)
176 pixel[i] = u16;
177 } else {
178 num_ints = gbm_bo_get_plane_size(bo, 0) / sizeof(uint32_t);
179 uint32_t *pixel = (uint32_t *)addr;
180 for (uint32_t i = 0; i < num_ints; i++)
181 pixel[i] = u32;
Haixia Shifc476462015-04-22 16:25:04 -0700182 }
183
Dongseong Hwangc19d6a62017-04-11 16:06:25 -0700184 bs_mapper_unmap(mapper, bo, map_data);
Gurchetan Singh0d714272017-01-26 11:58:49 -0800185}
186
Dongseong Hwangc19d6a62017-04-11 16:06:25 -0700187static void draw_cursor(struct bs_mapper *mapper, struct gbm_bo *bo)
Gurchetan Singh0d714272017-01-26 11:58:49 -0800188{
189 void *map_data;
Dongseong Hwangc19d6a62017-04-11 16:06:25 -0700190 uint32_t *cursor_ptr = bs_mapper_map(mapper, bo, 0, &map_data);
Gurchetan Singh0d714272017-01-26 11:58:49 -0800191 for (size_t y = 0; y < gbm_bo_get_height(bo); y++) {
192 for (size_t x = 0; x < gbm_bo_get_width(bo); x++) {
193 // A white triangle pointing right
194 bool color_white = y > x / 2 && y < (gbm_bo_get_width(bo) - x / 2);
Dongseong Hwang9093afe2017-03-20 19:16:28 -0700195 cursor_ptr[y * gbm_bo_get_height(bo) + x] =
196 (color_white) ? 0xFFFFFFFF : 0x00000000;
Gurchetan Singh0d714272017-01-26 11:58:49 -0800197 }
198 }
199
Dongseong Hwangc19d6a62017-04-11 16:06:25 -0700200 bs_mapper_unmap(mapper, bo, map_data);
Gurchetan Singh0d714272017-01-26 11:58:49 -0800201}
202
203static int get_prop(int fd, drmModeObjectPropertiesPtr props, const char *name,
204 struct atomictest_property *bs_prop)
205{
206 /* Property ID should always be > 0. */
207 bs_prop->pid = 0;
208 drmModePropertyPtr prop;
209 for (uint32_t i = 0; i < props->count_props; i++) {
210 if (bs_prop->pid)
211 break;
212
213 prop = drmModeGetProperty(fd, props->props[i]);
214 if (prop) {
215 if (!strcmp(prop->name, name)) {
216 bs_prop->pid = prop->prop_id;
217 bs_prop->value = props->prop_values[i];
218 }
219 drmModeFreeProperty(prop);
220 }
221 }
222
223 return (bs_prop->pid == 0) ? -1 : 0;
224}
225
226static int get_connector_props(int fd, struct atomictest_connector *connector,
227 drmModeObjectPropertiesPtr props)
228{
229 CHECK_RESULT(get_prop(fd, props, "EDID", &connector->edid));
230 CHECK_RESULT(get_prop(fd, props, "DPMS", &connector->dpms));
231 return 0;
232}
233
234static int get_crtc_props(int fd, struct atomictest_crtc *crtc, drmModeObjectPropertiesPtr props)
235{
236 CHECK_RESULT(get_prop(fd, props, "MODE_ID", &crtc->mode_id));
237 CHECK_RESULT(get_prop(fd, props, "ACTIVE", &crtc->active));
238 return 0;
239}
240
241static int get_plane_props(int fd, struct atomictest_plane *plane, drmModeObjectPropertiesPtr props)
242{
243 CHECK_RESULT(get_prop(fd, props, "CRTC_ID", &plane->crtc_id));
244 CHECK_RESULT(get_prop(fd, props, "FB_ID", &plane->fb_id));
245 CHECK_RESULT(get_prop(fd, props, "CRTC_X", &plane->crtc_x));
246 CHECK_RESULT(get_prop(fd, props, "CRTC_Y", &plane->crtc_y));
247 CHECK_RESULT(get_prop(fd, props, "CRTC_W", &plane->crtc_w));
248 CHECK_RESULT(get_prop(fd, props, "CRTC_H", &plane->crtc_h));
249 CHECK_RESULT(get_prop(fd, props, "SRC_X", &plane->src_x));
250 CHECK_RESULT(get_prop(fd, props, "SRC_Y", &plane->src_y));
251 CHECK_RESULT(get_prop(fd, props, "SRC_W", &plane->src_w));
252 CHECK_RESULT(get_prop(fd, props, "SRC_H", &plane->src_h));
253 CHECK_RESULT(get_prop(fd, props, "type", &plane->type));
254 return 0;
255}
256
257int set_connector_props(struct atomictest_connector *conn, drmModeAtomicReqPtr pset)
258{
259 uint32_t id = conn->connector_id;
260
261 /*
262 * Currently, kernel v4.4 doesn't have CRTC_ID as a property of the connector. It's
263 * required for the modeset to work, so we currently just take it from a plane. Also
264 * setting EDID or DPMS (even w/o modification) makes the kernel return -EINVAL, so
265 * let's keep them unset for now.
266 */
267 CHECK_RESULT(drmModeAtomicAddProperty(pset, id, conn->crtc_id.pid, conn->crtc_id.value));
268 return 0;
269}
270
271int set_crtc_props(struct atomictest_crtc *crtc, drmModeAtomicReqPtr pset)
272{
273 uint32_t id = crtc->crtc_id;
274 CHECK_RESULT(drmModeAtomicAddProperty(pset, id, crtc->mode_id.pid, crtc->mode_id.value));
275 CHECK_RESULT(drmModeAtomicAddProperty(pset, id, crtc->active.pid, crtc->active.value));
276 return 0;
277}
278
279int set_plane_props(struct atomictest_plane *plane, drmModeAtomicReqPtr pset)
280{
281 uint32_t id = plane->drm_plane.plane_id;
282 CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->crtc_id.pid, plane->crtc_id.value));
283 CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->fb_id.pid, plane->fb_id.value));
284 CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->crtc_x.pid, plane->crtc_x.value));
285 CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->crtc_y.pid, plane->crtc_y.value));
286 CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->crtc_w.pid, plane->crtc_w.value));
287 CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->crtc_h.pid, plane->crtc_h.value));
288 CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->src_x.pid, plane->src_x.value));
289 CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->src_y.pid, plane->src_y.value));
290 CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->src_w.pid, plane->src_w.value));
291 CHECK_RESULT(drmModeAtomicAddProperty(pset, id, plane->src_h.pid, plane->src_h.value));
292 return 0;
293}
294
295static int remove_plane_fb(struct atomictest_context *ctx, struct atomictest_plane *plane)
296{
297 if (plane->bo && plane->fb_id.value) {
298 CHECK_RESULT(drmModeRmFB(ctx->fd, plane->fb_id.value));
299 gbm_bo_destroy(plane->bo);
300 plane->bo = NULL;
301 plane->fb_id.value = 0;
302 }
303
304 return 0;
305}
306
307static int add_plane_fb(struct atomictest_context *ctx, struct atomictest_plane *plane)
308{
309 if (plane->format_idx < plane->drm_plane.count_formats) {
310 CHECK_RESULT(remove_plane_fb(ctx, plane));
311 uint32_t flags = (plane->type.value == DRM_PLANE_TYPE_CURSOR) ? GBM_BO_USE_CURSOR
312 : GBM_BO_USE_SCANOUT;
313 /* TODO(gsingh): add create with modifiers option. */
314 plane->bo = gbm_bo_create(gbm, plane->crtc_w.value, plane->crtc_h.value,
315 plane->drm_plane.formats[plane->format_idx], flags);
316
317 CHECK(plane->bo);
318 plane->fb_id.value = bs_drm_fb_create_gbm(plane->bo);
319 CHECK(plane->fb_id.value);
320 CHECK_RESULT(set_plane_props(plane, ctx->pset));
321 }
322
323 return 0;
324}
325
326static int init_plane(struct atomictest_context *ctx, struct atomictest_plane *plane,
327 uint32_t format, uint32_t x, uint32_t y, uint32_t w, uint32_t h,
328 uint32_t zpos, uint32_t crtc_id)
329{
330 int32_t idx = get_format_idx(plane, format);
331 if (idx < 0)
332 return -EINVAL;
333
334 plane->format_idx = idx;
335 plane->crtc_x.value = x;
336 plane->crtc_y.value = y;
337 plane->crtc_w.value = w;
338 plane->crtc_h.value = h;
339 plane->src_w.value = plane->crtc_w.value << 16;
340 plane->src_h.value = plane->crtc_h.value << 16;
341 plane->zpos.value = zpos;
342 plane->crtc_id.value = crtc_id;
343
344 CHECK_RESULT(add_plane_fb(ctx, plane));
345 return 0;
346}
347
348static int disable_plane(struct atomictest_context *ctx, struct atomictest_plane *plane)
349{
350 plane->format_idx = 0;
351 plane->crtc_x.value = 0;
352 plane->crtc_y.value = 0;
353 plane->crtc_w.value = 0;
354 plane->crtc_h.value = 0;
355 plane->src_w.value = 0;
356 plane->src_h.value = 0;
357 plane->zpos.value = 0;
358 plane->crtc_id.value = 0;
359
360 CHECK_RESULT(remove_plane_fb(ctx, plane));
361 CHECK_RESULT(set_plane_props(plane, ctx->pset));
362 return 0;
363}
364
365static int move_plane(struct atomictest_context *ctx, struct atomictest_crtc *crtc,
366 struct atomictest_plane *plane, uint32_t dx, uint32_t dy)
367{
368 if (plane->crtc_x.value < (crtc->width - plane->crtc_w.value) &&
369 plane->crtc_y.value < (crtc->height - plane->crtc_h.value)) {
370 plane->crtc_x.value += dx;
371 plane->crtc_y.value += dy;
372 CHECK_RESULT(set_plane_props(plane, ctx->pset));
373 return 0;
374 }
375
376 return -1;
377}
378
379static int commit(struct atomictest_context *ctx)
380{
381 int ret;
382 fd_set fds;
383 FD_ZERO(&fds);
384 FD_SET(ctx->fd, &fds);
385
386 ret = drmModeAtomicCommit(ctx->fd, ctx->pset,
387 DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
388 CHECK_RESULT(ret);
389 do {
390 ret = select(ctx->fd + 1, &fds, NULL, NULL, NULL);
391 } while (ret == -1 && errno == EINTR);
392
393 CHECK_RESULT(ret);
394 if (FD_ISSET(ctx->fd, &fds))
395 drmHandleEvent(ctx->fd, &ctx->drm_event_ctx);
396
397 return 0;
398}
399
400static int pageflip(struct atomictest_context *ctx, struct atomictest_plane *plane, uint32_t x,
401 uint32_t y, uint32_t w, uint32_t h, uint32_t zpos, uint32_t crtc_id,
402 uint32_t *formats, uint32_t count_formats)
403{
404 /* Check if plane support specified formats. */
405 for (uint32_t i = 0; i < count_formats; i++)
406 CHECK_RESULT(get_format_idx(plane, formats[i]));
407
408 for (uint32_t i = 0; i < count_formats; i++) {
409 CHECK_RESULT(init_plane(ctx, plane, formats[i], x, y, w, h, zpos, crtc_id));
Dongseong Hwangc19d6a62017-04-11 16:06:25 -0700410 write_to_buffer(ctx->mapper, plane->bo, 0x00FF0000, 0xF800);
Gurchetan Singh0d714272017-01-26 11:58:49 -0800411 CHECK_RESULT(commit(ctx));
412 usleep(1e6);
413 }
414
415 return 0;
416}
417
418static int check_mode(struct atomictest_context *ctx, struct atomictest_crtc *crtc)
419{
420 drmModeAtomicSetCursor(ctx->pset, 0);
421
422 for (uint32_t i = 0; i < ctx->num_crtcs; i++) {
423 if (&ctx->crtcs[i] != crtc) {
424 ctx->crtcs[i].mode_id.value = 0;
425 ctx->crtcs[i].active.value = 0;
426 set_crtc_props(&ctx->crtcs[i], ctx->pset);
427 }
428 }
429
430 for (uint32_t i = 0; i < ctx->num_connectors; i++) {
431 ctx->connectors[i].crtc_id.value = crtc->crtc_id;
432 set_connector_props(&ctx->connectors[i], ctx->pset);
433 }
434
435 int ret = -EINVAL;
436 int cursor = drmModeAtomicGetCursor(ctx->pset);
437
438 for (uint32_t i = 0; i < ctx->num_modes; i++) {
439 struct atomictest_mode *mode = &ctx->modes[i];
440 drmModeAtomicSetCursor(ctx->pset, cursor);
441
442 crtc->mode_id.value = mode->id;
443 crtc->active.value = 1;
444 crtc->width = mode->width;
445 crtc->height = mode->height;
446
447 set_crtc_props(crtc, ctx->pset);
448 ret = drmModeAtomicCommit(ctx->fd, ctx->pset,
449 DRM_MODE_ATOMIC_TEST_ONLY | DRM_MODE_ATOMIC_ALLOW_MODESET,
450 NULL);
451 if (!ret)
452 return 0;
453 }
454
455 bs_debug_error("[CRTC:%d]: failed to find mode", crtc->crtc_id);
456 return ret;
457}
458
Gurchetan Singh0d714272017-01-26 11:58:49 -0800459static void free_context(struct atomictest_context *ctx)
460{
461 for (uint32_t i = 0; i < ctx->num_crtcs; i++) {
462 uint32_t num_planes = ctx->crtcs[i].num_primary + ctx->crtcs[i].num_cursor +
463 ctx->crtcs[i].num_overlay;
464
465 for (uint32_t j = 0; j < num_planes; j++) {
466 remove_plane_fb(ctx, &ctx->crtcs[i].planes[j]);
467 free(ctx->crtcs[i].planes[j].drm_plane.formats);
468 free(ctx->crtcs[i].planes[j].drm_plane.format_modifiers);
Haixia Shifc476462015-04-22 16:25:04 -0700469 }
470
Gurchetan Singh0d714272017-01-26 11:58:49 -0800471 free(ctx->crtcs[i].planes);
472 free(ctx->crtcs[i].overlay_idx);
473 free(ctx->crtcs[i].cursor_idx);
474 free(ctx->crtcs[i].primary_idx);
475 }
476
477 drmModeAtomicFree(ctx->pset);
478 free(ctx->modes);
479 free(ctx->crtcs);
480 free(ctx->connectors);
Dongseong Hwang9093afe2017-03-20 19:16:28 -0700481 bs_mapper_destroy(ctx->mapper);
Gurchetan Singh0d714272017-01-26 11:58:49 -0800482 free(ctx);
483}
484
Dongseong Hwang9093afe2017-03-20 19:16:28 -0700485static struct atomictest_context *new_context(uint32_t num_connectors, uint32_t num_crtcs,
486 uint32_t num_planes)
487{
488 struct atomictest_context *ctx = calloc(1, sizeof(*ctx));
489 ctx->connectors = calloc(num_connectors, sizeof(*ctx->connectors));
490 ctx->crtcs = calloc(num_crtcs, sizeof(*ctx->crtcs));
Dongseong Hwang9093afe2017-03-20 19:16:28 -0700491 for (uint32_t i = 0; i < num_crtcs; i++) {
492 ctx->crtcs[i].planes = calloc(num_planes, sizeof(*ctx->crtcs[i].planes));
493 ctx->crtcs[i].overlay_idx = calloc(num_planes, sizeof(uint32_t));
494 ctx->crtcs[i].primary_idx = calloc(num_planes, sizeof(uint32_t));
495 ctx->crtcs[i].cursor_idx = calloc(num_planes, sizeof(uint32_t));
496 }
497
498 ctx->num_connectors = num_connectors;
499 ctx->num_crtcs = num_crtcs;
Kristian H. Kristensen56863512017-04-28 14:21:40 -0700500 ctx->num_modes = 0;
501 ctx->modes = NULL;
Dongseong Hwang9093afe2017-03-20 19:16:28 -0700502 ctx->pset = drmModeAtomicAlloc();
503 ctx->drm_event_ctx.version = DRM_EVENT_CONTEXT_VERSION;
504 ctx->drm_event_ctx.page_flip_handler = page_flip_handler;
505
Dongseong Hwangc19d6a62017-04-11 16:06:25 -0700506 ctx->mapper = bs_mapper_gem_new();
Dongseong Hwang9093afe2017-03-20 19:16:28 -0700507 if (ctx->mapper == NULL) {
508 bs_debug_error("failed to create mapper object");
509 free_context(ctx);
510 return NULL;
511 }
512
513 return ctx;
514}
515
Gurchetan Singh0d714272017-01-26 11:58:49 -0800516static struct atomictest_context *init_atomictest(int fd)
517{
518 drmModeRes *res = drmModeGetResources(fd);
519 if (res == NULL) {
520 bs_debug_error("failed to get drm resources");
521 return false;
522 }
523
524 drmModePlaneRes *plane_res = drmModeGetPlaneResources(fd);
525 if (plane_res == NULL) {
526 bs_debug_error("failed to get plane resources");
527 drmModeFreeResources(res);
528 return NULL;
529 }
530
531 struct atomictest_context *ctx =
532 new_context(res->count_connectors, res->count_crtcs, plane_res->count_planes);
533 if (ctx == NULL) {
534 bs_debug_error("failed to allocate atomic context");
535 drmModeFreePlaneResources(plane_res);
536 drmModeFreeResources(res);
537 return NULL;
538 }
539
Gurchetan Singh0d714272017-01-26 11:58:49 -0800540 ctx->fd = fd;
541 drmModeObjectPropertiesPtr props = NULL;
542
543 for (uint32_t conn_index = 0; conn_index < res->count_connectors; conn_index++) {
544 uint32_t conn_id = res->connectors[conn_index];
545 ctx->connectors[conn_index].connector_id = conn_id;
546 props = drmModeObjectGetProperties(fd, conn_id, DRM_MODE_OBJECT_CONNECTOR);
547 get_connector_props(fd, &ctx->connectors[conn_index], props);
548
549 drmModeConnector *connector = drmModeGetConnector(fd, conn_id);
550 for (uint32_t mode_index = 0; mode_index < connector->count_modes; mode_index++) {
Kristian H. Kristensen56863512017-04-28 14:21:40 -0700551 ctx->modes = realloc(ctx->modes, (ctx->num_modes + 1) * sizeof(*ctx->modes));
Gurchetan Singh0d714272017-01-26 11:58:49 -0800552 drmModeCreatePropertyBlob(fd, &connector->modes[mode_index],
553 sizeof(drmModeModeInfo),
554 &ctx->modes[ctx->num_modes].id);
555 ctx->modes[ctx->num_modes].width = connector->modes[mode_index].hdisplay;
556 ctx->modes[ctx->num_modes].height = connector->modes[mode_index].vdisplay;
557 ctx->num_modes++;
Haixia Shifc476462015-04-22 16:25:04 -0700558 }
559
Gurchetan Singh0d714272017-01-26 11:58:49 -0800560 drmModeFreeConnector(connector);
561 drmModeFreeObjectProperties(props);
562 props = NULL;
Haixia Shifc476462015-04-22 16:25:04 -0700563 }
564
Gurchetan Singh0d714272017-01-26 11:58:49 -0800565 uint32_t crtc_index;
566 for (crtc_index = 0; crtc_index < res->count_crtcs; crtc_index++) {
567 ctx->crtcs[crtc_index].crtc_id = res->crtcs[crtc_index];
568 props =
569 drmModeObjectGetProperties(fd, res->crtcs[crtc_index], DRM_MODE_OBJECT_CRTC);
570 get_crtc_props(fd, &ctx->crtcs[crtc_index], props);
571
572 drmModeFreeObjectProperties(props);
573 props = NULL;
Haixia Shifc476462015-04-22 16:25:04 -0700574 }
575
Gurchetan Singh0d714272017-01-26 11:58:49 -0800576 uint32_t overlay_idx, primary_idx, cursor_idx, idx;
Haixia Shifc476462015-04-22 16:25:04 -0700577
Gurchetan Singh0d714272017-01-26 11:58:49 -0800578 for (uint32_t plane_index = 0; plane_index < plane_res->count_planes; plane_index++) {
Kristian H. Kristensenfe6e6fa2017-05-01 15:12:20 -0700579 drmModePlane *plane = drmModeGetPlane2(fd, plane_res->planes[plane_index]);
Gurchetan Singh0d714272017-01-26 11:58:49 -0800580 if (plane == NULL) {
581 bs_debug_error("failed to get plane id %u", plane_res->planes[plane_index]);
582 continue;
583 }
Haixia Shifc476462015-04-22 16:25:04 -0700584
Gurchetan Singh0d714272017-01-26 11:58:49 -0800585 uint32_t crtc_mask = 0;
586
587 drmModeObjectPropertiesPtr props = drmModeObjectGetProperties(
588 fd, plane_res->planes[plane_index], DRM_MODE_OBJECT_PLANE);
589
590 for (crtc_index = 0; crtc_index < res->count_crtcs; crtc_index++) {
591 crtc_mask = (1 << crtc_index);
592 if (plane->possible_crtcs & crtc_mask) {
593 struct atomictest_crtc *crtc = &ctx->crtcs[crtc_index];
594 cursor_idx = crtc->num_cursor;
595 primary_idx = crtc->num_primary;
596 overlay_idx = crtc->num_overlay;
597 idx = cursor_idx + primary_idx + overlay_idx;
598 copy_drm_plane(&crtc->planes[idx].drm_plane, plane);
599 get_plane_props(fd, &crtc->planes[idx], props);
600 switch (crtc->planes[idx].type.value) {
601 case DRM_PLANE_TYPE_OVERLAY:
602 crtc->overlay_idx[overlay_idx] = idx;
603 crtc->num_overlay++;
604 break;
605 case DRM_PLANE_TYPE_PRIMARY:
606 crtc->primary_idx[primary_idx] = idx;
607 crtc->num_primary++;
608 break;
609 case DRM_PLANE_TYPE_CURSOR:
610 crtc->cursor_idx[cursor_idx] = idx;
611 crtc->num_cursor++;
612 break;
613 default:
614 bs_debug_error("invalid plane type returned");
615 return NULL;
616 }
Haixia Shifc476462015-04-22 16:25:04 -0700617 }
618 }
619
Gurchetan Singh0d714272017-01-26 11:58:49 -0800620 drmModeFreePlane(plane);
621 drmModeFreeObjectProperties(props);
622 props = NULL;
Haixia Shifc476462015-04-22 16:25:04 -0700623 }
624
Gurchetan Singh0d714272017-01-26 11:58:49 -0800625 /* HACK: Set connector CRTC pid to plane CRTC pid. */
626 for (uint32_t i = 0; i < ctx->num_connectors; i++)
627 ctx->connectors[i].crtc_id.pid = ctx->crtcs[0].planes[0].crtc_id.pid;
Haixia Shifc476462015-04-22 16:25:04 -0700628
Gurchetan Singh0d714272017-01-26 11:58:49 -0800629 drmModeFreePlaneResources(plane_res);
630 drmModeFreeResources(res);
631 return ctx;
632}
Haixia Shifc476462015-04-22 16:25:04 -0700633
Gurchetan Singh0d714272017-01-26 11:58:49 -0800634static int run_atomictest(const struct atomictest *test)
635{
636 int ret = 0;
637 int fd = bs_drm_open_main_display();
638 CHECK_RESULT(fd);
639
640 gbm = gbm_create_device(fd);
641 if (!gbm) {
642 bs_debug_error("failed to create gbm device");
643 ret = 1;
644 goto destroy_fd;
645 }
646
647 ret = drmSetClientCap(fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
648 if (ret) {
649 bs_debug_error("failed to enable DRM_CLIENT_CAP_UNIVERSAL_PLANES");
650 ret = 1;
651 goto destroy_gbm_device;
652 }
653
654 ret = drmSetClientCap(fd, DRM_CLIENT_CAP_ATOMIC, 1);
655 if (ret) {
656 bs_debug_error("failed to enable DRM_CLIENT_CAP_ATOMIC");
657 ret = 1;
658 goto destroy_gbm_device;
659 }
660
661 struct atomictest_context *ctx = init_atomictest(fd);
662 if (!ctx) {
663 bs_debug_error("initializing atomictest failed.");
664 ret = 1;
665 goto destroy_gbm_device;
666 }
667
668 struct atomictest_crtc *crtc;
669 for (uint32_t crtc_index = 0; crtc_index < ctx->num_crtcs; crtc_index++) {
670 crtc = &ctx->crtcs[crtc_index];
671 if (!check_mode(ctx, crtc))
672 ret |= test->run_test(ctx, crtc);
673 else
674 ret |= 1;
675 }
676
677 free_context(ctx);
678
679destroy_gbm_device:
680 gbm_device_destroy(gbm);
681destroy_fd:
682 close(fd);
683
Haixia Shifc476462015-04-22 16:25:04 -0700684 return ret;
685}
Gurchetan Singh0d714272017-01-26 11:58:49 -0800686
687static int test_multiple_planes(struct atomictest_context *ctx, struct atomictest_crtc *crtc)
688{
689 struct atomictest_plane *primary, *overlay, *cursor;
690 for (uint32_t i = 0; i < crtc->num_primary; i++) {
691 bool has_video = false;
692 uint32_t x, y;
693 for (uint32_t j = 0; j < crtc->num_overlay; j++) {
694 overlay = get_plane(crtc, j, DRM_PLANE_TYPE_OVERLAY);
695 x = crtc->width >> (j + 2);
696 y = crtc->height >> (j + 2);
697 bool added_video = false;
698 if (!has_video) {
699 uint32_t k = 0;
700 for (k = 0; k < BS_ARRAY_LEN(yuv_formats); k++) {
701 if (!init_plane(ctx, overlay, yuv_formats[k], x, y, x, y, j,
702 crtc->crtc_id)) {
703 has_video = true;
704 added_video = true;
705 break;
706 }
707 }
708
709 if (added_video) {
710 const struct bs_draw_format *draw_format =
711 bs_get_draw_format(yuv_formats[k]);
712 CHECK(draw_format);
Dongseong Hwang9093afe2017-03-20 19:16:28 -0700713 CHECK(
Dongseong Hwang20c494c2016-04-27 11:26:19 +0300714 bs_draw_stripe(ctx->mapper, overlay->bo, draw_format));
Gurchetan Singh0d714272017-01-26 11:58:49 -0800715 }
716 }
717
718 if (!added_video) {
719 added_video = true;
720 CHECK_RESULT(init_plane(ctx, overlay, DRM_FORMAT_XRGB8888, x, y, x,
721 y, i, crtc->crtc_id));
Dongseong Hwangc19d6a62017-04-11 16:06:25 -0700722 write_to_buffer(ctx->mapper, overlay->bo, 0x00FF0000, 0);
Gurchetan Singh0d714272017-01-26 11:58:49 -0800723 }
724 }
725
726 for (uint32_t j = 0; j < crtc->num_cursor; j++) {
727 x = crtc->width >> (j + 2);
728 y = crtc->height >> (j + 2);
729 cursor = get_plane(crtc, j, DRM_PLANE_TYPE_CURSOR);
730 CHECK_RESULT(init_plane(ctx, cursor, DRM_FORMAT_XRGB8888, x, y, CURSOR_SIZE,
731 CURSOR_SIZE, crtc->num_overlay + j, crtc->crtc_id));
Dongseong Hwangc19d6a62017-04-11 16:06:25 -0700732 draw_cursor(ctx->mapper, cursor->bo);
Gurchetan Singh0d714272017-01-26 11:58:49 -0800733 }
734
735 primary = get_plane(crtc, i, DRM_PLANE_TYPE_PRIMARY);
736 CHECK_RESULT(init_plane(ctx, primary, DRM_FORMAT_XRGB8888, 0, 0, crtc->width,
737 crtc->height, 0, crtc->crtc_id));
Dongseong Hwangc19d6a62017-04-11 16:06:25 -0700738 write_to_buffer(ctx->mapper, primary->bo, 0x00000FF, 0);
Gurchetan Singh0d714272017-01-26 11:58:49 -0800739
740 uint32_t num_planes = crtc->num_primary + crtc->num_cursor + crtc->num_overlay;
741 int done = 0;
742 struct atomictest_plane *plane;
743 while (!done) {
744 done = 1;
745 for (uint32_t j = 0; j < num_planes; j++) {
746 plane = &crtc->planes[j];
747 if (plane->type.value != DRM_PLANE_TYPE_PRIMARY)
748 done &= move_plane(ctx, crtc, plane, 20, 20);
749 }
750
751 CHECK_RESULT(commit(ctx));
752 usleep(1e6 / 60);
753 }
754
755 CHECK_RESULT(commit(ctx));
756 usleep(1e6);
757
758 /* Disable primary plane and verify overlays show up. */
759 CHECK_RESULT(disable_plane(ctx, primary));
760 CHECK_RESULT(commit(ctx));
761 usleep(1e6);
762 }
763
764 return 0;
765}
766
767static int test_video_overlay(struct atomictest_context *ctx, struct atomictest_crtc *crtc)
768{
769 struct atomictest_plane *overlay;
770 for (uint32_t i = 0; i < crtc->num_overlay; i++) {
771 overlay = get_plane(crtc, i, DRM_PLANE_TYPE_OVERLAY);
772 for (uint32_t j = 0; j < BS_ARRAY_LEN(yuv_formats); j++) {
773 if (init_plane(ctx, overlay, yuv_formats[j], 0, 0, 800, 800, 0,
774 crtc->crtc_id))
775 continue;
776
777 const struct bs_draw_format *draw_format =
778 bs_get_draw_format(yuv_formats[j]);
779 CHECK(draw_format);
Dongseong Hwang20c494c2016-04-27 11:26:19 +0300780 CHECK(bs_draw_stripe(ctx->mapper, overlay->bo, draw_format));
Gurchetan Singh0d714272017-01-26 11:58:49 -0800781 while (!move_plane(ctx, crtc, overlay, 20, 20)) {
782 CHECK_RESULT(commit(ctx));
783 usleep(1e6 / 60);
784 }
785 }
786 }
787
788 return 0;
789}
790
791static int test_fullscreen_video(struct atomictest_context *ctx, struct atomictest_crtc *crtc)
792{
793 struct atomictest_plane *primary;
794 for (uint32_t i = 0; i < crtc->num_primary; i++) {
795 primary = get_plane(crtc, i, DRM_PLANE_TYPE_PRIMARY);
796 for (uint32_t j = 0; j < BS_ARRAY_LEN(yuv_formats); j++) {
797 if (init_plane(ctx, primary, yuv_formats[j], 0, 0, crtc->width,
798 crtc->height, 0, crtc->crtc_id))
799 continue;
800 const struct bs_draw_format *draw_format =
801 bs_get_draw_format(yuv_formats[j]);
802 CHECK(draw_format);
803
Dongseong Hwang20c494c2016-04-27 11:26:19 +0300804 CHECK(bs_draw_stripe(ctx->mapper, primary->bo, draw_format));
Gurchetan Singh0d714272017-01-26 11:58:49 -0800805 CHECK_RESULT(commit(ctx));
806 usleep(1e6);
807 }
808 }
809
810 return 0;
811}
812
813static int test_disable_primary(struct atomictest_context *ctx, struct atomictest_crtc *crtc)
814{
815 int cursor;
816 struct atomictest_plane *primary, *overlay;
817 for (uint32_t i = 0; i < crtc->num_primary; i++) {
818 for (uint32_t j = 0; j < crtc->num_overlay; j++) {
819 overlay = get_plane(crtc, j, DRM_PLANE_TYPE_OVERLAY);
820 uint32_t x = crtc->width >> (j + 2);
821 uint32_t y = crtc->height >> (j + 2);
822 CHECK_RESULT(init_plane(ctx, overlay, DRM_FORMAT_XRGB8888, x, y, x, y, i,
823 crtc->crtc_id));
Dongseong Hwangc19d6a62017-04-11 16:06:25 -0700824 write_to_buffer(ctx->mapper, overlay->bo, 0x00FF0000, 0);
Gurchetan Singh0d714272017-01-26 11:58:49 -0800825 }
826
827 cursor = drmModeAtomicGetCursor(ctx->pset);
828 primary = get_plane(crtc, i, DRM_PLANE_TYPE_PRIMARY);
829 CHECK_RESULT(init_plane(ctx, primary, DRM_FORMAT_XRGB8888, 0, 0, crtc->width,
830 crtc->height, 0, crtc->crtc_id));
Dongseong Hwangc19d6a62017-04-11 16:06:25 -0700831 write_to_buffer(ctx->mapper, primary->bo, 0x00000FF, 0);
Gurchetan Singh0d714272017-01-26 11:58:49 -0800832 CHECK_RESULT(commit(ctx));
833 usleep(1e6);
834
835 /* Disable primary plane. */
836 disable_plane(ctx, primary);
837 CHECK_RESULT(commit(ctx));
838 usleep(1e6);
839 drmModeAtomicSetCursor(ctx->pset, cursor);
840 }
841
842 return 0;
843}
844
845static int test_overlay_pageflip(struct atomictest_context *ctx, struct atomictest_crtc *crtc)
846{
847 struct atomictest_plane *overlay;
848 uint32_t formats[3] = { DRM_FORMAT_XRGB8888, DRM_FORMAT_XBGR8888, DRM_FORMAT_RGB565 };
849 for (uint32_t i = 0; i < crtc->num_overlay; i++) {
850 overlay = get_plane(crtc, i, DRM_PLANE_TYPE_OVERLAY);
851 uint32_t x = crtc->width >> (i + 1);
852 uint32_t y = crtc->height >> (i + 1);
853
854 CHECK_RESULT(pageflip(ctx, overlay, x, y, x, y, i, crtc->crtc_id, formats,
855 BS_ARRAY_LEN(formats)));
856 }
857
858 return 0;
859}
860
861static int test_primary_pageflip(struct atomictest_context *ctx, struct atomictest_crtc *crtc)
862{
863 int cursor;
864 struct atomictest_plane *primary;
865 uint32_t formats[3] = { DRM_FORMAT_XRGB8888, DRM_FORMAT_XBGR8888, DRM_FORMAT_RGB565 };
866 for (uint32_t i = 0; i < crtc->num_primary; i++) {
867 primary = get_plane(crtc, i, DRM_PLANE_TYPE_PRIMARY);
868 cursor = drmModeAtomicGetCursor(ctx->pset);
869 CHECK_RESULT(pageflip(ctx, primary, 0, 0, crtc->width, crtc->height, 0,
870 crtc->crtc_id, formats, BS_ARRAY_LEN(formats)));
871
872 drmModeAtomicSetCursor(ctx->pset, cursor);
873 }
874
875 return 0;
876}
877
878static const struct atomictest tests[] = {
879 { "disable_primary", test_disable_primary },
880 { "fullscreen_video", test_fullscreen_video },
881 { "multiple_planes", test_multiple_planes },
882 { "overlay_pageflip", test_overlay_pageflip },
883 { "primary_pageflip", test_primary_pageflip },
884 { "video_overlay", test_video_overlay },
885};
886
887static void print_help(const char *argv0)
888{
889 printf("usage: %s <test_name>\n\n", argv0);
890 printf("A valid name test is one the following:\n");
891 for (uint32_t i = 0; i < BS_ARRAY_LEN(tests); i++)
892 printf("%s\n", tests[i].name);
893}
894
895int main(int argc, char **argv)
896{
897 if (argc == 2) {
898 char *name = argv[1];
899 for (uint32_t i = 0; i < BS_ARRAY_LEN(tests); i++) {
900 if (strcmp(tests[i].name, name))
901 continue;
902
903 int ret = run_atomictest(&tests[i]);
904 if (ret) {
905 printf("[ FAILED ] atomictest.%s\n", name);
906 return -1;
907 } else {
908 printf("[ PASSED ] atomictest.%s\n", name);
909 return 0;
910 }
911 }
912 }
913
914 print_help(argv[0]);
915
916 return -1;
917}