1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <sys/types.h> 4 #include <sys/stat.h> 5 #include <sys/select.h> 6 #include <fcntl.h> 7 #include <unistd.h> 8 #include <string.h> 9 #include <signal.h> 10 #include <time.h> 11 #include <errno.h> 12 13 #include <xf86drm.h> 14 15 #include "dev.h" 16 #include "bo.h" 17 #include "modeset.h" 18 19 static int terminate = 0; 20 21 static void sigint_handler(int arg) 22 { 23 terminate = 1; 24 } 25 26 static void 27 page_flip_handler(int fd, unsigned int sequence, unsigned int tv_sec, 28 unsigned int tv_usec, void *user_data) 29 { 30 } 31 32 static void incrementor(int *inc, int *val, int increment, int lower, int upper) 33 { 34 if(*inc > 0) 35 *inc = *val + increment >= upper ? -1 : 1; 36 else 37 *inc = *val - increment <= lower ? 1 : -1; 38 *val += *inc * increment; 39 } 40 41 int main(int argc, char *argv[]) 42 { 43 int ret, i, j, num_test_planes; 44 int x_inc = 1, x = 0, y_inc = 1, y = 0; 45 uint32_t plane_w = 128, plane_h = 128; 46 struct sp_dev *dev; 47 struct sp_plane **plane = NULL; 48 struct sp_crtc *test_crtc; 49 fd_set fds; 50 drmModePropertySetPtr pset; 51 drmEventContext event_context = { 52 .version = DRM_EVENT_CONTEXT_VERSION, 53 .page_flip_handler = page_flip_handler, 54 }; 55 int card = 0, crtc = 0; 56 57 signal(SIGINT, sigint_handler); 58 59 parse_arguments(argc, argv, &card, &crtc); 60 61 dev = create_sp_dev(card); 62 if (!dev) { 63 printf("Failed to create sp_dev\n"); 64 return -1; 65 } 66 67 if (crtc >= dev->num_crtcs) { 68 printf("Invalid crtc %d (num=%d)\n", crtc, dev->num_crtcs); 69 return -1; 70 } 71 72 ret = initialize_screens(dev); 73 if (ret) { 74 printf("Failed to initialize screens\n"); 75 goto out; 76 } 77 test_crtc = &dev->crtcs[crtc]; 78 79 plane = calloc(dev->num_planes, sizeof(*plane)); 80 if (!plane) { 81 printf("Failed to allocate plane array\n"); 82 goto out; 83 } 84 85 /* Create our planes */ 86 num_test_planes = test_crtc->num_planes; 87 for (i = 0; i < num_test_planes; i++) { 88 plane[i] = get_sp_plane(dev, test_crtc); 89 if (!plane[i]) { 90 printf("no unused planes available\n"); 91 goto out; 92 } 93 94 plane[i]->bo = create_sp_bo(dev, plane_w, plane_h, 16, 32, 95 plane[i]->format, 0); 96 if (!plane[i]->bo) { 97 printf("failed to create plane bo\n"); 98 goto out; 99 } 100 101 fill_bo(plane[i]->bo, 0xFF, 0xFF, 0xFF, 0xFF); 102 } 103 104 pset = drmModePropertySetAlloc(); 105 if (!pset) { 106 printf("Failed to allocate the property set\n"); 107 goto out; 108 } 109 110 while (!terminate) { 111 FD_ZERO(&fds); 112 FD_SET(dev->fd, &fds); 113 114 incrementor(&x_inc, &x, 5, 0, 115 test_crtc->crtc->mode.hdisplay - plane_w); 116 incrementor(&y_inc, &y, 5, 0, test_crtc->crtc->mode.vdisplay - 117 plane_h * num_test_planes); 118 119 for (j = 0; j < num_test_planes; j++) { 120 ret = set_sp_plane_pset(dev, plane[j], pset, test_crtc, 121 x, y + j * plane_h); 122 if (ret) { 123 printf("failed to move plane %d\n", ret); 124 goto out; 125 } 126 } 127 128 ret = drmModePropertySetCommit(dev->fd, 129 DRM_MODE_PAGE_FLIP_EVENT, NULL, pset); 130 if (ret) { 131 printf("failed to commit properties ret=%d\n", ret); 132 goto out; 133 } 134 135 do { 136 ret = select(dev->fd + 1, &fds, NULL, NULL, NULL); 137 } while (ret == -1 && errno == EINTR); 138 139 if (FD_ISSET(dev->fd, &fds)) 140 drmHandleEvent(dev->fd, &event_context); 141 } 142 143 drmModePropertySetFree(pset); 144 145 for (i = 0; i < num_test_planes; i++) 146 put_sp_plane(plane[i]); 147 148 out: 149 destroy_sp_dev(dev); 150 free(plane); 151 return ret; 152 } 153