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