10 #include "xf86drmMode.h"
14 /* Pitch needs to be power of two */
17 static struct drm_mode_modeinfo mode = {
30 .vrefresh = 60000, /* vertical refresh * 1000 */
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);
39 int main(int argc, char **argv)
42 const char *driver = "i915"; /* hardcoded for now */
44 drmModeFBPtr framebuffer;
46 drmModeOutputPtr out[8];
49 printf("Starting test\n");
51 fd = drmOpen(driver, NULL);
54 printf("Failed to open the card fb\n");
58 res = drmModeGetResources(fd);
60 printf("Failed to get resources from card\n");
65 framebuffer = createFB(fd, res);
66 if (framebuffer == NULL) {
67 printf("Failed to create framebuffer\n");
71 numOutputs = findConnectedOutputs(fd, res, out);
73 printf("Failed to find connected outputs\n");
77 crtc = findFreeCrtc(fd, res);
79 printf("Couldn't find a free crtc\n");
83 prettyColors(fd, framebuffer->handle);
86 drmModeSetCrtc(fd, crtc->crtc_id, framebuffer->buffer_id, 0, 0, &out[0]->output_id, 1, &mode);
90 drmModeSetCrtc(fd, crtc->crtc_id, framebuffer->buffer_id, 0, 100, &out[0]->output_id, 1, &mode);
94 drmModeSetCrtc(fd, crtc->crtc_id, framebuffer->buffer_id, 100, 0, &out[0]->output_id, 1, &mode);
98 drmModeSetCrtc(fd, crtc->crtc_id, framebuffer->buffer_id, 100, 100, &out[0]->output_id, 1, &mode);
101 /* turn the crtc off just in case */
102 drmModeSetCrtc(fd, crtc->crtc_id, 0, 0, 0, 0, 0, 0);
104 drmModeFreeResources(res);
110 drmModeFBPtr createFB(int fd, drmModeResPtr res)
117 ret = drmBOCreate(fd, SIZE_X * SIZE_Y * 4, 0, 0,
121 DRM_BO_FLAG_MEM_VRAM |
122 DRM_BO_FLAG_NO_EVICT,
123 DRM_BO_HINT_DONT_FENCE, &bo);
128 ret = drmModeAddFB(fd, SIZE_X, SIZE_Y, 32, 32, PITCH * 4, &bo, &fb);
133 frame = drmModeGetFB(fd, fb);
141 drmBOUnreference(fd, &bo);
143 printf("Something went wrong when creating a fb, using one of the predefined ones\n");
145 return drmModeGetFB(fd, res->fbs[0]);
148 int findConnectedOutputs(int fd, drmModeResPtr res, drmModeOutputPtr *out)
153 drmModeOutputPtr output;
155 for (i = 0; i < res->count_outputs; i++) {
156 output = drmModeGetOutput(fd, res->outputs[i]);
158 if (!output || output->connection != DRM_MODE_CONNECTED)
161 out[count++] = output;
167 drmModeCrtcPtr findFreeCrtc(int fd, drmModeResPtr res)
169 return drmModeGetCrtc(fd, res->crtcs[0]);
172 void draw(unsigned int x, unsigned int y, unsigned int w, unsigned int h, unsigned int v, unsigned int *ptr)
176 for (i = x; i < x + w; i++)
177 for(j = y; j < y + h; j++)
178 ptr[(i * PITCH) + j] = v;
182 void prettyColors(int fd, unsigned int handle)
188 drmBOReference(fd, handle, &bo);
189 drmBOMap(fd, &bo, DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE, 0, (void**)&ptr);
191 for (i = 0; i < (SIZE_X*SIZE_Y); i++)
194 for (i = 0; i < 8; i++)
195 draw(i * 40, i * 40, 40, 40, 0, ptr);
198 draw(200, 100, 40, 40, 0xff00ff, ptr);
199 draw(100, 200, 40, 40, 0xff00ff, ptr);