d083fa603e325b7a4b2e1b07f3ddb3c5d8229e1d
[platform/upstream/intel-gpu-tools.git] / tests / flip_test.c
1 /*
2  * Copyright 2012 Intel Corporation
3  *   Jesse Barnes <jesse.barnes@intel.com>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23
24 #include "config.h"
25
26 #include <assert.h>
27 #include <cairo.h>
28 #include <errno.h>
29 #include <math.h>
30 #include <stdint.h>
31 #include <unistd.h>
32 #include <sys/poll.h>
33 #include <sys/time.h>
34 #include <sys/mman.h>
35 #include <sys/ioctl.h>
36
37 #include "i915_drm.h"
38 #include "drmtest.h"
39 #include "testdisplay.h"
40 #include "intel_bufmgr.h"
41 #include "intel_batchbuffer.h"
42 #include "intel_gpu_tools.h"
43
44 #define TEST_DPMS               (1 << 0)
45 #define TEST_WITH_DUMMY_LOAD    (1 << 1)
46 #define TEST_PAN                (1 << 2)
47 #define TEST_MODESET            (1 << 3)
48 #define TEST_CHECK_TS           (1 << 4)
49
50 drmModeRes *resources;
51 int drm_fd;
52 static drm_intel_bufmgr *bufmgr;
53 struct intel_batchbuffer *batch;
54 uint32_t devid;
55 int test_time = 3;
56
57 uint32_t *fb_ptr;
58
59 struct type_name {
60         int type;
61         const char *name;
62 };
63
64 struct test_output {
65         uint32_t id;
66         int mode_valid;
67         drmModeModeInfo mode;
68         drmModeEncoder *encoder;
69         drmModeConnector *connector;
70         int crtc;
71         int flags;
72         int count;
73         unsigned int current_fb_id;
74         unsigned int fb_ids[2];
75         struct kmstest_fb fb_info[2];
76         struct timeval last_flip_received;
77         struct timeval last_flip_ts;
78 };
79
80 static void emit_dummy_load(struct test_output *o)
81 {
82         int i, limit;
83         drm_intel_bo *dummy_bo, *target_bo, *tmp_bo;
84         struct kmstest_fb *fb_info = &o->fb_info[o->current_fb_id];
85         unsigned pitch = fb_info->stride;
86
87         limit = intel_gen(devid) < 6 ? 500 : 5000;
88
89         dummy_bo = drm_intel_bo_alloc(bufmgr, "dummy_bo", fb_info->size, 4096);
90         assert(dummy_bo);
91         target_bo = gem_handle_to_libdrm_bo(bufmgr, drm_fd, "imported", fb_info->gem_handle);
92         assert(target_bo);
93
94         for (i = 0; i < limit; i++) {
95                 BEGIN_BATCH(8);
96                 OUT_BATCH(XY_SRC_COPY_BLT_CMD |
97                           XY_SRC_COPY_BLT_WRITE_ALPHA |
98                           XY_SRC_COPY_BLT_WRITE_RGB);
99                 OUT_BATCH((3 << 24) | /* 32 bits */
100                           (0xcc << 16) | /* copy ROP */
101                           pitch);
102                 OUT_BATCH(0 << 16 | 0);
103                 OUT_BATCH((o->mode.vdisplay) << 16 | (o->mode.hdisplay));
104                 OUT_RELOC_FENCED(dummy_bo, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, 0);
105                 OUT_BATCH(0 << 16 | 0);
106                 OUT_BATCH(pitch);
107                 OUT_RELOC_FENCED(target_bo, I915_GEM_DOMAIN_RENDER, 0, 0);
108                 ADVANCE_BATCH();
109
110                 if (IS_GEN6(devid) || IS_GEN7(devid)) {
111                         BEGIN_BATCH(3);
112                         OUT_BATCH(XY_SETUP_CLIP_BLT_CMD);
113                         OUT_BATCH(0);
114                         OUT_BATCH(0);
115                         ADVANCE_BATCH();
116                 }
117
118                 tmp_bo = dummy_bo;
119                 dummy_bo = target_bo;
120                 target_bo = tmp_bo;
121         }
122         intel_batchbuffer_flush(batch);
123
124         drm_intel_bo_unreference(dummy_bo);
125         drm_intel_bo_unreference(target_bo);
126 }
127
128 static int set_dpms(struct test_output *o, int mode)
129 {
130         int i, dpms = 0;
131
132         for (i = 0; i < o->connector->count_props; i++) {
133                 struct drm_mode_get_property prop;
134
135                 prop.prop_id = o->connector->props[i];
136                 prop.count_values = 0;
137                 prop.count_enum_blobs = 0;
138                 if (drmIoctl(drm_fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
139                         continue;
140
141                 if (strcmp(prop.name, "DPMS"))
142                         continue;
143
144                 dpms = prop.prop_id;
145                 break;
146         }
147         if (!dpms) {
148                 fprintf(stderr, "DPMS property not found on %d\n", o->id);
149                 errno = ENOENT;
150                 return -1;
151         }
152
153         return drmModeConnectorSetProperty(drm_fd, o->id, dpms, mode);
154 }
155
156 static bool
157 analog_tv_connector(struct test_output *o)
158 {
159         uint32_t connector_type = o->connector->connector_type;
160
161         return connector_type == DRM_MODE_CONNECTOR_TV ||
162                 connector_type == DRM_MODE_CONNECTOR_9PinDIN ||
163                 connector_type == DRM_MODE_CONNECTOR_SVIDEO ||
164                 connector_type == DRM_MODE_CONNECTOR_Composite;
165 }
166
167 static void page_flip_handler(int fd, unsigned int frame, unsigned int sec,
168                               unsigned int usec, void *data)
169 {
170         struct test_output *o = data;
171         unsigned int new_fb_id;
172         struct timeval now, diff, pageflip_ts;
173         double usec_interflip;
174
175         pageflip_ts.tv_sec = sec;
176         pageflip_ts.tv_usec = usec;
177
178         gettimeofday(&now, NULL);
179
180         timersub(&pageflip_ts, &now, &diff);
181
182         if (diff.tv_sec > 0 || (diff.tv_sec > 0 && diff.tv_usec > 2000)) {
183                 fprintf(stderr, "pageflip timestamp delayed for too long: %is, %iusec\n",
184                         (int) diff.tv_sec, (int) diff.tv_usec);
185                 exit(5);
186         }
187
188         if (!timercmp(&o->last_flip_received, &pageflip_ts, <)) {
189                 fprintf(stderr, "pageflip ts before the pageflip was issued!\n");
190                 exit(6);
191         }
192
193         if (o->count > 1 && o->flags & TEST_CHECK_TS && !analog_tv_connector(o)) {
194                 timersub(&pageflip_ts, &o->last_flip_ts, &diff);
195                 usec_interflip = 1.0 / ((double) o->mode.vrefresh) * 1000.0 * 1000.0;
196
197                 if (fabs((((double) diff.tv_usec) - usec_interflip) / usec_interflip) > 0.005) {
198                         fprintf(stderr, "inter-flip timestamp jitter: %is, %ius\n",
199                                 (int) diff.tv_sec, (int) diff.tv_usec);
200                         /* atm this is way too easy to hit, thanks to the hpd
201                          * poll helper :( hence make it non-fatal for now */
202                         //exit(9);
203                 }
204         }
205
206         o->count++;
207
208         o->current_fb_id = !o->current_fb_id;
209         new_fb_id = o->fb_ids[o->current_fb_id];
210
211         if (o->flags & TEST_WITH_DUMMY_LOAD)
212                 emit_dummy_load(o);
213
214         printf("."); fflush(stdout);
215         if (o->flags & TEST_MODESET) {
216                 if (drmModeSetCrtc(drm_fd, o->crtc,
217                                    o->fb_ids[o->current_fb_id],
218                                    0, 0,
219                                    &o->id, 1, &o->mode)) {
220                         fprintf(stderr, "failed to restore output mode: %s\n",
221                                 strerror(errno));
222                         exit(7);
223                 }
224         }
225
226         if (o->flags & TEST_DPMS)
227                 do_or_die(set_dpms(o, DRM_MODE_DPMS_ON));
228
229         do_or_die(drmModePageFlip(drm_fd, o->crtc, new_fb_id,
230                                   DRM_MODE_PAGE_FLIP_EVENT, o));
231
232         if (o->flags & TEST_DPMS)
233                 do_or_die(set_dpms(o, DRM_MODE_DPMS_OFF));
234
235         if (o->flags & TEST_MODESET) {
236                 if (drmModeSetCrtc(drm_fd, o->crtc,
237                                    0, /* no fb */
238                                    0, 0,
239                                    NULL, 0, NULL)) {
240                         fprintf(stderr, "failed to disable output: %s\n",
241                                 strerror(errno));
242                         exit(7);
243                 }
244         }
245
246         o->last_flip_received = now;
247         o->last_flip_ts = pageflip_ts;
248 }
249
250 static void connector_find_preferred_mode(struct test_output *o, int crtc_id)
251 {
252         drmModeConnector *connector;
253         drmModeEncoder *encoder = NULL;
254         int i, j;
255
256         /* First, find the connector & mode */
257         o->mode_valid = 0;
258         o->crtc = 0;
259         connector = drmModeGetConnector(drm_fd, o->id);
260         assert(connector);
261
262         if (connector->connection != DRM_MODE_CONNECTED) {
263                 drmModeFreeConnector(connector);
264                 return;
265         }
266
267         if (!connector->count_modes) {
268                 fprintf(stderr, "connector %d has no modes\n", o->id);
269                 drmModeFreeConnector(connector);
270                 return;
271         }
272
273         if (connector->connector_id != o->id) {
274                 fprintf(stderr, "connector id doesn't match (%d != %d)\n",
275                         connector->connector_id, o->id);
276                 drmModeFreeConnector(connector);
277                 return;
278         }
279
280         for (j = 0; j < connector->count_modes; j++) {
281                 o->mode = connector->modes[j];
282                 if (o->mode.type & DRM_MODE_TYPE_PREFERRED) {
283                         o->mode_valid = 1;
284                         break;
285                 }
286         }
287
288         if (!o->mode_valid) {
289                 if (connector->count_modes > 0) {
290                         /* use the first mode as test mode */
291                         o->mode = connector->modes[0];
292                         o->mode_valid = 1;
293                 }
294                 else {
295                         fprintf(stderr, "failed to find any modes on connector %d\n",
296                                 o->id);
297                         return;
298                 }
299         }
300
301         /* Now get the encoder */
302         for (i = 0; i < connector->count_encoders; i++) {
303                 encoder = drmModeGetEncoder(drm_fd, connector->encoders[i]);
304
305                 if (!encoder) {
306                         fprintf(stderr, "could not get encoder %i: %s\n",
307                                 resources->encoders[i], strerror(errno));
308                         drmModeFreeEncoder(encoder);
309                         continue;
310                 }
311
312                 break;
313         }
314
315         o->encoder = encoder;
316
317         if (i == resources->count_encoders) {
318                 fprintf(stderr, "failed to find encoder\n");
319                 o->mode_valid = 0;
320                 return;
321         }
322
323         /* Find first CRTC not in use */
324         for (i = 0; i < resources->count_crtcs; i++) {
325                 if (resources->crtcs[i] != crtc_id)
326                         continue;
327                 if (resources->crtcs[i] &&
328                     (o->encoder->possible_crtcs & (1<<i))) {
329                         o->crtc = resources->crtcs[i];
330                         break;
331                 }
332         }
333
334         if (!o->crtc) {
335                 fprintf(stderr, "could not find requested crtc %d\n", crtc_id);
336                 o->mode_valid = 0;
337                 return;
338         }
339
340         o->connector = connector;
341 }
342
343 static void
344 paint_flip_mode(cairo_t *cr, int width, int height, void *priv)
345 {
346         bool odd_frame = (bool) priv;
347
348         if (odd_frame)
349                 cairo_rectangle(cr, width/4, height/2, width/4, height/8);
350         else
351                 cairo_rectangle(cr, width/2, height/2, width/4, height/8);
352
353         cairo_set_source_rgb(cr, 1, 1, 1);
354         cairo_fill(cr);
355 }
356
357 static int
358 fb_is_bound(struct test_output *o, int fb)
359 {
360         struct drm_mode_crtc mode;
361
362         mode.crtc_id = o->crtc;
363         if (drmIoctl(drm_fd, DRM_IOCTL_MODE_GETCRTC, &mode))
364                 return 0;
365
366         return mode.mode_valid && mode.fb_id == fb;
367 }
368
369 static void flip_mode(struct test_output *o, int crtc, int duration)
370 {
371         int ret;
372         int bpp = 32, depth = 24;
373         drmEventContext evctx;
374         int width, height;
375         struct timeval end;
376
377         connector_find_preferred_mode(o, crtc);
378         if (!o->mode_valid)
379                 return;
380
381         fprintf(stdout, "Beginning page flipping on crtc %d, connector %d\n",
382                 crtc, o->id);
383
384         width = o->mode.hdisplay;
385         height = o->mode.vdisplay;
386
387         if (o->flags & TEST_PAN)
388                 width *= 2;
389
390         o->fb_ids[0] = kmstest_create_fb(drm_fd, width, height, bpp, depth,
391                                          false, &o->fb_info[0],
392                                          paint_flip_mode, (void *)false);
393         o->fb_ids[1] = kmstest_create_fb(drm_fd, width, height, bpp, depth,
394                                          false, &o->fb_info[1],
395                                          paint_flip_mode, (void *)true);
396
397         if (!o->fb_ids[0] || !o->fb_ids[1]) {
398                 fprintf(stderr, "failed to create fbs\n");
399                 exit(3);
400         }
401
402         kmstest_dump_mode(&o->mode);
403         if (drmModeSetCrtc(drm_fd, o->crtc, o->fb_ids[0], 0, 0,
404                            &o->id, 1, &o->mode)) {
405                 fprintf(stderr, "failed to set mode (%dx%d@%dHz): %s\n",
406                         width, height, o->mode.vrefresh,
407                         strerror(errno));
408                 exit(3);
409         }
410         assert(fb_is_bound(o, o->fb_ids[0]));
411
412         /* quiescent the hw a bit so ensure we don't miss a single frame */
413         if (o->flags & TEST_CHECK_TS)
414                 sleep(1);
415
416         if (drmModePageFlip(drm_fd, o->crtc, o->fb_ids[1],
417                               DRM_MODE_PAGE_FLIP_EVENT, o)) {
418                 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
419                 exit(4);
420         }
421         o->current_fb_id = 1;
422         o->count = 1; /* for the uncounted tail */
423
424         memset(&evctx, 0, sizeof evctx);
425         evctx.version = DRM_EVENT_CONTEXT_VERSION;
426         evctx.vblank_handler = NULL;
427         evctx.page_flip_handler = page_flip_handler;
428
429         gettimeofday(&end, NULL);
430         gettimeofday(&o->last_flip_received, NULL);
431         end.tv_sec += duration;
432
433         while (1) {
434                 struct timeval now, timeout = { .tv_sec = 3, .tv_usec = 0 };
435                 fd_set fds;
436
437                 /* make timeout lax with the dummy load */
438                 if (o->flags & TEST_WITH_DUMMY_LOAD)
439                         timeout.tv_sec *= 10;
440
441                 FD_ZERO(&fds);
442                 FD_SET(0, &fds);
443                 FD_SET(drm_fd, &fds);
444                 ret = select(drm_fd + 1, &fds, NULL, NULL, &timeout);
445
446                 if (ret <= 0) {
447                         fprintf(stderr, "select timed out or error (ret %d)\n",
448                                 ret);
449                         exit(1);
450                 } else if (FD_ISSET(0, &fds)) {
451                         fprintf(stderr, "no fds active, breaking\n");
452                         exit(2);
453                 }
454
455                 gettimeofday(&now, NULL);
456                 if (now.tv_sec > end.tv_sec ||
457                     (now.tv_sec == end.tv_sec && now.tv_usec >= end.tv_usec)) {
458                         break;
459                 }
460
461                 /* pan before the flip completes */
462                 if (o->flags & TEST_PAN) {
463                         int x_ofs = o->count * 10 > o->mode.hdisplay ? o->mode.hdisplay :
464                                 o->count * 10;
465
466                         if (drmModeSetCrtc(drm_fd, o->crtc, o->fb_ids[o->current_fb_id],
467                                            x_ofs, 0,
468                                            &o->id, 1, &o->mode)) {
469                                 fprintf(stderr, "failed to pan (%dx%d@%dHz): %s\n",
470                                         width, height, o->mode.vrefresh,
471                                         strerror(errno));
472                                 exit(7);
473                         }
474                 }
475
476                 drmHandleEvent(drm_fd, &evctx);
477         }
478
479         /* and drain the event queue */
480         evctx.page_flip_handler = NULL;
481         drmHandleEvent(drm_fd, &evctx);
482
483         /* Verify we drop no frames, but only if it's not a TV encoder, since
484          * those use some funny fake timings behind userspace's back. */
485         if (o->flags & TEST_CHECK_TS && !analog_tv_connector(o)) {
486                 struct timeval now;
487                 long us;
488                 int expected;
489
490                 gettimeofday(&now, NULL);
491
492                 us = duration * 1000 * 1000;
493                 us += (now.tv_sec - end.tv_sec) * 1000 * 1000;
494                 us += now.tv_usec - end.tv_usec;
495
496                 expected = us * o->mode.vrefresh / (1000 * 1000);
497                 if (o->count < expected * 99/100) {
498                         fprintf(stderr, "dropped frames, expected %d, counted %d, encoder type %d\n",
499                                 expected, o->count, o->encoder->encoder_type);
500                         exit(3);
501                 }
502         }
503
504         fprintf(stdout, "\npage flipping on crtc %d, connector %d: PASSED\n",
505                 crtc, o->id);
506
507         drmModeFreeEncoder(o->encoder);
508         drmModeFreeConnector(o->connector);
509 }
510
511 static int run_test(int duration, int flags)
512 {
513         struct test_output o;
514         int c, i;
515
516         resources = drmModeGetResources(drm_fd);
517         if (!resources) {
518                 fprintf(stderr, "drmModeGetResources failed: %s\n",
519                         strerror(errno));
520                 exit(5);
521         }
522
523         /* Find any connected displays */
524         for (c = 0; c < resources->count_connectors; c++) {
525                 memset(&o, 0, sizeof(o));
526                 o.id = resources->connectors[c];
527                 o.flags = flags;
528                 for (i = 0; i < resources->count_crtcs; i++)
529                         flip_mode(&o, resources->crtcs[i], duration);
530         }
531
532         drmModeFreeResources(resources);
533         return 1;
534 }
535
536 int main(int argc, char **argv)
537 {
538         struct {
539                 int duration;
540                 int flags;
541                 const char *name;
542         } tests[] = {
543                 { 15, TEST_CHECK_TS , "plain flip" },
544                 { 30, TEST_DPMS, "flip vs dpms" },
545                 { 30, TEST_DPMS | TEST_WITH_DUMMY_LOAD, "delayed flip vs. dpms" },
546                 { 5, TEST_PAN, "flip vs panning" },
547                 { 30, TEST_PAN | TEST_WITH_DUMMY_LOAD, "delayed flip vs panning" },
548                 { 30, TEST_MODESET, "flip vs modeset" },
549                 { 30, TEST_MODESET | TEST_WITH_DUMMY_LOAD, "delayed flip vs modeset" },
550         };
551         int i;
552
553         drm_fd = drm_open_any();
554
555         bufmgr = drm_intel_bufmgr_gem_init(drm_fd, 4096);
556         devid = intel_get_drm_devid(drm_fd);
557         batch = intel_batchbuffer_alloc(bufmgr, devid);
558
559         for (i = 0; i < sizeof(tests) / sizeof (tests[0]); i++) {
560                 printf("running testcase: %s\n", tests[i].name);
561                 run_test(tests[i].duration, tests[i].flags);
562         }
563
564         close(drm_fd);
565
566         return 0;
567 }