Panning now works without modeset
[profile/ivi/libdrm.git] / tests / modedemo / demo.c
1
2 #include <assert.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdint.h>
6 #include <unistd.h>
7 #include <string.h>
8
9 #include "xf86drm.h"
10 #include "xf86drmMode.h"
11
12 #define SIZE_X 2048
13 #define SIZE_Y 2048
14 /* Pitch needs to be power of two */
15 #define PITCH 2048
16
17 static struct drm_mode_modeinfo mode = {
18         .name = "Test mode",
19         .clock = 25200,
20         .hdisplay = 640,
21         .hsync_start = 656,
22         .hsync_end = 752,
23         .htotal = 800,
24         .hskew = 0,
25         .vdisplay = 480,
26         .vsync_start = 490,
27         .vsync_end = 492,
28         .vtotal = 525,
29         .vscan = 0,
30         .vrefresh = 60000, /* vertical refresh * 1000 */
31         .flags = 10,
32 };
33
34 drmModeFBPtr createFB(int fd, drmModeResPtr res);
35 int findConnectedOutputs(int fd, drmModeResPtr res, drmModeOutputPtr *out);
36 drmModeCrtcPtr findFreeCrtc(int fd, drmModeResPtr res);
37 void prettyColors(int fd, unsigned int handle);
38
39 int main(int argc, char **argv)
40 {
41         int fd;
42         const char *driver = "i915"; /* hardcoded for now */
43         drmModeResPtr res;
44         drmModeFBPtr framebuffer;
45         int numOutputs;
46         drmModeOutputPtr out[8];
47         drmModeCrtcPtr crtc;
48
49         printf("Starting test\n");
50
51         fd = drmOpen(driver, NULL);
52
53         if (fd < 0) {
54                 printf("Failed to open the card fb\n");
55                 return 1;
56         }
57
58         res = drmModeGetResources(fd);
59         if (res == 0) {
60                 printf("Failed to get resources from card\n");
61                 drmClose(fd);
62                 return 1;
63         }
64
65         framebuffer = createFB(fd, res);
66         if (framebuffer == NULL) {
67                 printf("Failed to create framebuffer\n");
68                 return 1;
69         }
70
71         numOutputs = findConnectedOutputs(fd, res, out);
72         if (numOutputs < 1) {
73                 printf("Failed to find connected outputs\n");
74                 return 1;
75         }
76
77         crtc = findFreeCrtc(fd, res);
78         if (numOutputs < 1) {
79                 printf("Couldn't find a free crtc\n");
80                 return 1;
81         }
82
83         prettyColors(fd, framebuffer->handle);
84
85         printf("0 0\n");
86         drmModeSetCrtc(fd, crtc->crtc_id, framebuffer->buffer_id, 0, 0, &out[0]->output_id, 1, &mode);
87         sleep(2);
88
89         printf("0 100\n");
90         drmModeSetCrtc(fd, crtc->crtc_id, framebuffer->buffer_id, 0, 100, &out[0]->output_id, 1, &mode);
91         sleep(2);
92
93         printf("100 0\n");
94         drmModeSetCrtc(fd, crtc->crtc_id, framebuffer->buffer_id, 100, 0, &out[0]->output_id, 1, &mode);
95         sleep(2);
96
97         printf("100 100\n");
98         drmModeSetCrtc(fd, crtc->crtc_id, framebuffer->buffer_id, 100, 100, &out[0]->output_id, 1, &mode);
99         sleep(2);
100
101         /* turn the crtc off just in case */
102         drmModeSetCrtc(fd, crtc->crtc_id, 0, 0, 0, 0, 0, 0);
103
104     drmModeFreeResources(res);
105     printf("Ok\n");
106
107     return 0;
108 }
109
110 drmModeFBPtr createFB(int fd, drmModeResPtr res)
111 {
112         drmModeFBPtr frame;
113         unsigned int fb = 0;
114         int ret = 0;
115         drmBO bo;
116
117         ret = drmBOCreate(fd, SIZE_X * SIZE_Y * 4, 0, 0,
118                 DRM_BO_FLAG_READ |
119                 DRM_BO_FLAG_WRITE |
120                 DRM_BO_FLAG_MEM_TT |
121                 DRM_BO_FLAG_MEM_VRAM |
122                 DRM_BO_FLAG_NO_EVICT,
123                 DRM_BO_HINT_DONT_FENCE, &bo);
124
125         if (ret)
126                 goto err;
127
128         ret = drmModeAddFB(fd, SIZE_X, SIZE_Y, 32, 32, PITCH * 4, &bo, &fb);
129
130         if (ret)
131                 goto err_bo;
132
133         frame = drmModeGetFB(fd, fb);
134
135         if (!frame)
136                 goto err_bo;
137
138         return frame;
139
140 err_bo:
141         drmBOUnreference(fd, &bo);
142 err:
143         printf("Something went wrong when creating a fb, using one of the predefined ones\n");
144
145         return drmModeGetFB(fd, res->fbs[0]);
146 }
147
148 int findConnectedOutputs(int fd, drmModeResPtr res, drmModeOutputPtr *out)
149 {
150         int count = 0;
151         int i;
152
153         drmModeOutputPtr output;
154
155         for (i = 0; i < res->count_outputs; i++) {
156                 output = drmModeGetOutput(fd, res->outputs[i]);
157
158                 if (!output || output->connection != DRM_MODE_CONNECTED)
159                         continue;
160
161                 out[count++] = output;
162         }
163
164         return count;
165 }
166
167 drmModeCrtcPtr findFreeCrtc(int fd, drmModeResPtr res)
168 {
169         return drmModeGetCrtc(fd, res->crtcs[0]);
170 }
171
172 void draw(unsigned int x, unsigned int y, unsigned int w, unsigned int h, unsigned int v, unsigned int *ptr)
173 {
174         int i, j;
175
176         for (i = x; i < x + w; i++)
177                 for(j = y; j < y + h; j++)
178                         ptr[(i * PITCH) + j] = v;
179
180 }
181
182 void prettyColors(int fd, unsigned int handle)
183 {
184         drmBO bo;
185         unsigned int *ptr;
186         int i;
187
188         drmBOReference(fd, handle, &bo);
189         drmBOMap(fd, &bo, DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE, 0, (void**)&ptr);
190
191         for (i = 0; i < (SIZE_X*SIZE_Y); i++)
192                 ptr[i] = 0xFFFFFFFF;
193
194         for (i = 0; i < 8; i++)
195                 draw(i * 40, i * 40, 40, 40, 0, ptr);
196
197
198         draw(200, 100, 40, 40, 0xff00ff, ptr);
199         draw(100, 200, 40, 40, 0xff00ff, ptr);
200
201         drmBOUnmap(fd, &bo);
202 }