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, };
140 case DRM_FORMAT_NV12:
141 case DRM_FORMAT_NV21:
142 case DRM_FORMAT_NV16:
143 case DRM_FORMAT_NV61:
144 case DRM_FORMAT_NV24:
145 case DRM_FORMAT_NV42:
146 case DRM_FORMAT_YUV420:
147 case DRM_FORMAT_YVU420:
151 case DRM_FORMAT_ARGB4444:
152 case DRM_FORMAT_XRGB4444:
153 case DRM_FORMAT_ABGR4444:
154 case DRM_FORMAT_XBGR4444:
155 case DRM_FORMAT_RGBA4444:
156 case DRM_FORMAT_RGBX4444:
157 case DRM_FORMAT_BGRA4444:
158 case DRM_FORMAT_BGRX4444:
159 case DRM_FORMAT_ARGB1555:
160 case DRM_FORMAT_XRGB1555:
161 case DRM_FORMAT_ABGR1555:
162 case DRM_FORMAT_XBGR1555:
163 case DRM_FORMAT_RGBA5551:
164 case DRM_FORMAT_RGBX5551:
165 case DRM_FORMAT_BGRA5551:
166 case DRM_FORMAT_BGRX5551:
167 case DRM_FORMAT_RGB565:
168 case DRM_FORMAT_BGR565:
169 case DRM_FORMAT_UYVY:
170 case DRM_FORMAT_VYUY:
171 case DRM_FORMAT_YUYV:
172 case DRM_FORMAT_YVYU:
176 case DRM_FORMAT_BGR888:
177 case DRM_FORMAT_RGB888:
181 case DRM_FORMAT_ARGB8888:
182 case DRM_FORMAT_XRGB8888:
183 case DRM_FORMAT_ABGR8888:
184 case DRM_FORMAT_XBGR8888:
185 case DRM_FORMAT_RGBA8888:
186 case DRM_FORMAT_RGBX8888:
187 case DRM_FORMAT_BGRA8888:
188 case DRM_FORMAT_BGRX8888:
189 case DRM_FORMAT_ARGB2101010:
190 case DRM_FORMAT_XRGB2101010:
191 case DRM_FORMAT_ABGR2101010:
192 case DRM_FORMAT_XBGR2101010:
193 case DRM_FORMAT_RGBA1010102:
194 case DRM_FORMAT_RGBX1010102:
195 case DRM_FORMAT_BGRA1010102:
196 case DRM_FORMAT_BGRX1010102:
200 case DRM_FORMAT_XRGB16161616F:
201 case DRM_FORMAT_XBGR16161616F:
202 case DRM_FORMAT_ARGB16161616F:
203 case DRM_FORMAT_ABGR16161616F:
208 fprintf(stderr, "unsupported format 0x%08x\n", format);
213 case DRM_FORMAT_NV12:
214 case DRM_FORMAT_NV21:
215 case DRM_FORMAT_YUV420:
216 case DRM_FORMAT_YVU420:
217 virtual_height = height * 3 / 2;
220 case DRM_FORMAT_NV16:
221 case DRM_FORMAT_NV61:
222 virtual_height = height * 2;
225 case DRM_FORMAT_NV24:
226 case DRM_FORMAT_NV42:
227 virtual_height = height * 3;
231 virtual_height = height;
235 bo = bo_create_dumb(fd, width, virtual_height, bpp);
239 ret = bo_map(bo, &virtual);
241 fprintf(stderr, "failed to map buffer: %s\n",
247 /* just testing a limited # of formats to test single
248 * and multi-planar path.. would be nice to add more..
251 case DRM_FORMAT_UYVY:
252 case DRM_FORMAT_VYUY:
253 case DRM_FORMAT_YUYV:
254 case DRM_FORMAT_YVYU:
256 handles[0] = bo->handle;
257 pitches[0] = bo->pitch;
262 case DRM_FORMAT_NV12:
263 case DRM_FORMAT_NV21:
264 case DRM_FORMAT_NV16:
265 case DRM_FORMAT_NV61:
267 handles[0] = bo->handle;
268 pitches[0] = bo->pitch;
269 pitches[1] = pitches[0];
270 offsets[1] = pitches[0] * height;
271 handles[1] = bo->handle;
274 planes[1] = virtual + offsets[1];
277 case DRM_FORMAT_NV24:
278 case DRM_FORMAT_NV42:
280 handles[0] = bo->handle;
281 pitches[0] = bo->pitch;
282 pitches[1] = pitches[0] * 2;
283 offsets[1] = pitches[0] * height;
284 handles[1] = bo->handle;
287 planes[1] = virtual + offsets[1];
290 case DRM_FORMAT_YUV420:
291 case DRM_FORMAT_YVU420:
293 handles[0] = bo->handle;
294 pitches[0] = bo->pitch;
295 pitches[1] = pitches[0] / 2;
296 offsets[1] = pitches[0] * height;
297 handles[1] = bo->handle;
298 pitches[2] = pitches[1];
299 offsets[2] = offsets[1] + pitches[1] * height / 2;
300 handles[2] = bo->handle;
303 planes[1] = virtual + offsets[1];
304 planes[2] = virtual + offsets[2];
311 case DRM_FORMAT_ARGB4444:
312 case DRM_FORMAT_XRGB4444:
313 case DRM_FORMAT_ABGR4444:
314 case DRM_FORMAT_XBGR4444:
315 case DRM_FORMAT_RGBA4444:
316 case DRM_FORMAT_RGBX4444:
317 case DRM_FORMAT_BGRA4444:
318 case DRM_FORMAT_BGRX4444:
319 case DRM_FORMAT_ARGB1555:
320 case DRM_FORMAT_XRGB1555:
321 case DRM_FORMAT_ABGR1555:
322 case DRM_FORMAT_XBGR1555:
323 case DRM_FORMAT_RGBA5551:
324 case DRM_FORMAT_RGBX5551:
325 case DRM_FORMAT_BGRA5551:
326 case DRM_FORMAT_BGRX5551:
327 case DRM_FORMAT_RGB565:
328 case DRM_FORMAT_BGR565:
329 case DRM_FORMAT_BGR888:
330 case DRM_FORMAT_RGB888:
331 case DRM_FORMAT_ARGB8888:
332 case DRM_FORMAT_XRGB8888:
333 case DRM_FORMAT_ABGR8888:
334 case DRM_FORMAT_XBGR8888:
335 case DRM_FORMAT_RGBA8888:
336 case DRM_FORMAT_RGBX8888:
337 case DRM_FORMAT_BGRA8888:
338 case DRM_FORMAT_BGRX8888:
339 case DRM_FORMAT_ARGB2101010:
340 case DRM_FORMAT_XRGB2101010:
341 case DRM_FORMAT_ABGR2101010:
342 case DRM_FORMAT_XBGR2101010:
343 case DRM_FORMAT_RGBA1010102:
344 case DRM_FORMAT_RGBX1010102:
345 case DRM_FORMAT_BGRA1010102:
346 case DRM_FORMAT_BGRX1010102:
347 case DRM_FORMAT_XRGB16161616F:
348 case DRM_FORMAT_XBGR16161616F:
349 case DRM_FORMAT_ARGB16161616F:
350 case DRM_FORMAT_ABGR16161616F:
352 handles[0] = bo->handle;
353 pitches[0] = bo->pitch;
359 util_fill_pattern(format, pattern, planes, width, height, pitches[0]);
365 void bo_destroy(struct bo *bo)
369 ret = drmModeDestroyDumbBuffer(bo->fd, bo->handle);
371 fprintf(stderr, "failed to destroy dumb buffer: %s\n",
377 void bo_dump(struct bo *bo, const char *filename)
381 if (!bo || !filename)
384 fp = fopen(filename, "wb");
389 printf("Dumping buffer %p to file %s.\n", bo->ptr, filename);
390 fwrite(bo->ptr, 1, bo->size, fp);