10 #include "xf86drmMode.h"
12 /* setting this to 2024 gets the pitch wrong check it */
16 static struct drm_mode_modeinfo mode = {
29 .vrefresh = 60000, /* vertical refresh * 1000 */
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);
38 int main(int argc, char **argv)
41 const char *driver = "i915"; /* hardcoded for now */
43 drmModeFBPtr framebuffer;
45 drmModeOutputPtr out[8];
48 printf("Starting test\n");
50 fd = drmOpen(driver, NULL);
53 printf("Failed to open the card fb\n");
57 res = drmModeGetResources(fd);
59 printf("Failed to get resources from card\n");
64 framebuffer = createFB(fd, res);
65 if (framebuffer == NULL) {
66 printf("Failed to create framebuffer\n");
70 numOutputs = findConnectedOutputs(fd, res, out);
72 printf("Failed to find connected outputs\n");
76 crtc = findFreeCrtc(fd, res);
78 printf("Couldn't find a free crtc\n");
82 prettyColors(fd, framebuffer->handle);
85 drmModeSetCrtc(fd, crtc->crtc_id, framebuffer->buffer_id, 0, 0, &out[1]->output_id, 1, &mode);
89 drmModeSetCrtc(fd, crtc->crtc_id, framebuffer->buffer_id, 0, 100, &out[1]->output_id, 1, &mode);
93 drmModeSetCrtc(fd, crtc->crtc_id, framebuffer->buffer_id, 100, 0, &out[1]->output_id, 1, &mode);
97 drmModeSetCrtc(fd, crtc->crtc_id, framebuffer->buffer_id, 100, 100, &out[1]->output_id, 1, &mode);
100 /* turn the crtc off just in case */
101 drmModeSetCrtc(fd, crtc->crtc_id, 0, 0, 0, 0, 0, 0);
103 drmModeFreeResources(res);
109 drmModeFBPtr createFB(int fd, drmModeResPtr res)
116 ret = drmBOCreate(fd, SIZE_X * SIZE_Y * 4, 0, 0,
120 DRM_BO_FLAG_MEM_VRAM |
121 DRM_BO_FLAG_NO_EVICT,
122 DRM_BO_HINT_DONT_FENCE, &bo);
127 ret = drmModeAddFB(fd, SIZE_X, SIZE_Y, 32, 32, SIZE_X*4, &bo, &fb);
132 frame = drmModeGetFB(fd, fb);
140 drmBOUnreference(fd, &bo);
142 printf("Something went wrong when creating a fb, using one of the predefined ones\n");
144 return drmModeGetFB(fd, res->fbs[0]);
147 int findConnectedOutputs(int fd, drmModeResPtr res, drmModeOutputPtr *out)
152 drmModeOutputPtr output;
154 for (i = 0; i < res->count_outputs; i++) {
155 output = drmModeGetOutput(fd, res->outputs[i]);
157 if (!output || output->connection != DRM_MODE_CONNECTED)
160 out[count++] = output;
166 drmModeCrtcPtr findFreeCrtc(int fd, drmModeResPtr res)
168 return drmModeGetCrtc(fd, res->crtcs[1]);
171 void draw(unsigned int x, unsigned int y, unsigned int w, unsigned int h, unsigned int v, unsigned int *ptr)
175 for (i = x; i < x + w; i++)
176 for(j = y; j < y + h; j++)
177 ptr[(i * SIZE_X) + j] = v;
181 void prettyColors(int fd, unsigned int handle)
187 drmBOReference(fd, handle, &bo);
188 drmBOMap(fd, &bo, DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE, 0, (void**)&ptr);
190 for (i = 0; i < (SIZE_X*SIZE_Y); i++)
193 for (i = 0; i < 8; i++)
194 draw(i*40, i*40, 40, 40, 0, ptr);
197 draw(200, 100, 40, 40, 0xff00ff, ptr);
198 draw(100, 200, 40, 40, 0xff00ff, ptr);