2 * DRM based mode setting test program
3 * Copyright 2008 Tungsten Graphics
4 * Jakob Bornecrantz <jakob@tungstengraphics.com>
5 * Copyright 2008 Intel Corporation
6 * Jesse Barnes <jesse.barnes@intel.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33 #include <sys/ioctl.h>
36 #include "drm_fourcc.h"
38 #include "libdrm_macros.h"
40 #include "xf86drmMode.h"
53 /* -----------------------------------------------------------------------------
58 bo_create_dumb(int fd, unsigned int width, unsigned int height, unsigned int bpp)
63 bo = calloc(1, sizeof(*bo));
65 fprintf(stderr, "failed to allocate buffer object\n");
69 ret = drmModeCreateDumbBuffer(fd, width, height, bpp, 0, &bo->handle,
70 &bo->pitch, &bo->size);
72 fprintf(stderr, "failed to create dumb buffer: %s\n",
83 static int bo_map(struct bo *bo, void **out)
89 ret = drmModeMapDumbBuffer(bo->fd, bo->handle, &offset);
93 map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
95 if (map == MAP_FAILED)
104 static void bo_unmap(struct bo *bo)
109 drm_munmap(bo->ptr, bo->size);
114 bo_create(int fd, unsigned int format,
115 unsigned int width, unsigned int height,
116 unsigned int handles[4], unsigned int pitches[4],
117 unsigned int offsets[4], enum util_fill_pattern pattern)
119 unsigned int virtual_height;
122 void *planes[3] = { 0, };
128 case DRM_FORMAT_NV12:
129 case DRM_FORMAT_NV21:
130 case DRM_FORMAT_NV16:
131 case DRM_FORMAT_NV61:
132 case DRM_FORMAT_NV24:
133 case DRM_FORMAT_NV42:
134 case DRM_FORMAT_YUV420:
135 case DRM_FORMAT_YVU420:
139 case DRM_FORMAT_ARGB4444:
140 case DRM_FORMAT_XRGB4444:
141 case DRM_FORMAT_ABGR4444:
142 case DRM_FORMAT_XBGR4444:
143 case DRM_FORMAT_RGBA4444:
144 case DRM_FORMAT_RGBX4444:
145 case DRM_FORMAT_BGRA4444:
146 case DRM_FORMAT_BGRX4444:
147 case DRM_FORMAT_ARGB1555:
148 case DRM_FORMAT_XRGB1555:
149 case DRM_FORMAT_ABGR1555:
150 case DRM_FORMAT_XBGR1555:
151 case DRM_FORMAT_RGBA5551:
152 case DRM_FORMAT_RGBX5551:
153 case DRM_FORMAT_BGRA5551:
154 case DRM_FORMAT_BGRX5551:
155 case DRM_FORMAT_RGB565:
156 case DRM_FORMAT_BGR565:
157 case DRM_FORMAT_UYVY:
158 case DRM_FORMAT_VYUY:
159 case DRM_FORMAT_YUYV:
160 case DRM_FORMAT_YVYU:
164 case DRM_FORMAT_BGR888:
165 case DRM_FORMAT_RGB888:
169 case DRM_FORMAT_ARGB8888:
170 case DRM_FORMAT_XRGB8888:
171 case DRM_FORMAT_ABGR8888:
172 case DRM_FORMAT_XBGR8888:
173 case DRM_FORMAT_RGBA8888:
174 case DRM_FORMAT_RGBX8888:
175 case DRM_FORMAT_BGRA8888:
176 case DRM_FORMAT_BGRX8888:
177 case DRM_FORMAT_ARGB2101010:
178 case DRM_FORMAT_XRGB2101010:
179 case DRM_FORMAT_ABGR2101010:
180 case DRM_FORMAT_XBGR2101010:
181 case DRM_FORMAT_RGBA1010102:
182 case DRM_FORMAT_RGBX1010102:
183 case DRM_FORMAT_BGRA1010102:
184 case DRM_FORMAT_BGRX1010102:
188 case DRM_FORMAT_XRGB16161616F:
189 case DRM_FORMAT_XBGR16161616F:
190 case DRM_FORMAT_ARGB16161616F:
191 case DRM_FORMAT_ABGR16161616F:
196 fprintf(stderr, "unsupported format 0x%08x\n", format);
201 case DRM_FORMAT_NV12:
202 case DRM_FORMAT_NV21:
203 case DRM_FORMAT_YUV420:
204 case DRM_FORMAT_YVU420:
205 virtual_height = height * 3 / 2;
208 case DRM_FORMAT_NV16:
209 case DRM_FORMAT_NV61:
210 virtual_height = height * 2;
213 case DRM_FORMAT_NV24:
214 case DRM_FORMAT_NV42:
215 virtual_height = height * 3;
219 virtual_height = height;
223 bo = bo_create_dumb(fd, width, virtual_height, bpp);
227 ret = bo_map(bo, &virtual);
229 fprintf(stderr, "failed to map buffer: %s\n",
235 /* just testing a limited # of formats to test single
236 * and multi-planar path.. would be nice to add more..
239 case DRM_FORMAT_UYVY:
240 case DRM_FORMAT_VYUY:
241 case DRM_FORMAT_YUYV:
242 case DRM_FORMAT_YVYU:
244 handles[0] = bo->handle;
245 pitches[0] = bo->pitch;
250 case DRM_FORMAT_NV12:
251 case DRM_FORMAT_NV21:
252 case DRM_FORMAT_NV16:
253 case DRM_FORMAT_NV61:
255 handles[0] = bo->handle;
256 pitches[0] = bo->pitch;
257 pitches[1] = pitches[0];
258 offsets[1] = pitches[0] * height;
259 handles[1] = bo->handle;
262 planes[1] = virtual + offsets[1];
265 case DRM_FORMAT_NV24:
266 case DRM_FORMAT_NV42:
268 handles[0] = bo->handle;
269 pitches[0] = bo->pitch;
270 pitches[1] = pitches[0] * 2;
271 offsets[1] = pitches[0] * height;
272 handles[1] = bo->handle;
275 planes[1] = virtual + offsets[1];
278 case DRM_FORMAT_YUV420:
279 case DRM_FORMAT_YVU420:
281 handles[0] = bo->handle;
282 pitches[0] = bo->pitch;
283 pitches[1] = pitches[0] / 2;
284 offsets[1] = pitches[0] * height;
285 handles[1] = bo->handle;
286 pitches[2] = pitches[1];
287 offsets[2] = offsets[1] + pitches[1] * height / 2;
288 handles[2] = bo->handle;
291 planes[1] = virtual + offsets[1];
292 planes[2] = virtual + offsets[2];
296 case DRM_FORMAT_ARGB4444:
297 case DRM_FORMAT_XRGB4444:
298 case DRM_FORMAT_ABGR4444:
299 case DRM_FORMAT_XBGR4444:
300 case DRM_FORMAT_RGBA4444:
301 case DRM_FORMAT_RGBX4444:
302 case DRM_FORMAT_BGRA4444:
303 case DRM_FORMAT_BGRX4444:
304 case DRM_FORMAT_ARGB1555:
305 case DRM_FORMAT_XRGB1555:
306 case DRM_FORMAT_ABGR1555:
307 case DRM_FORMAT_XBGR1555:
308 case DRM_FORMAT_RGBA5551:
309 case DRM_FORMAT_RGBX5551:
310 case DRM_FORMAT_BGRA5551:
311 case DRM_FORMAT_BGRX5551:
312 case DRM_FORMAT_RGB565:
313 case DRM_FORMAT_BGR565:
314 case DRM_FORMAT_BGR888:
315 case DRM_FORMAT_RGB888:
316 case DRM_FORMAT_ARGB8888:
317 case DRM_FORMAT_XRGB8888:
318 case DRM_FORMAT_ABGR8888:
319 case DRM_FORMAT_XBGR8888:
320 case DRM_FORMAT_RGBA8888:
321 case DRM_FORMAT_RGBX8888:
322 case DRM_FORMAT_BGRA8888:
323 case DRM_FORMAT_BGRX8888:
324 case DRM_FORMAT_ARGB2101010:
325 case DRM_FORMAT_XRGB2101010:
326 case DRM_FORMAT_ABGR2101010:
327 case DRM_FORMAT_XBGR2101010:
328 case DRM_FORMAT_RGBA1010102:
329 case DRM_FORMAT_RGBX1010102:
330 case DRM_FORMAT_BGRA1010102:
331 case DRM_FORMAT_BGRX1010102:
332 case DRM_FORMAT_XRGB16161616F:
333 case DRM_FORMAT_XBGR16161616F:
334 case DRM_FORMAT_ARGB16161616F:
335 case DRM_FORMAT_ABGR16161616F:
337 handles[0] = bo->handle;
338 pitches[0] = bo->pitch;
344 util_fill_pattern(format, pattern, planes, width, height, pitches[0]);
350 void bo_destroy(struct bo *bo)
354 ret = drmModeDestroyDumbBuffer(bo->fd, bo->handle);
356 fprintf(stderr, "failed to destroy dumb buffer: %s\n",
362 void bo_dump(struct bo *bo, const char *filename)
366 if (!bo || !filename)
369 fp = fopen(filename, "wb");
374 printf("Dumping buffer %p to file %s.\n", bo->ptr, filename);
375 fwrite(bo->ptr, 1, bo->size, fp);