lib: Split the GTT mapping out of get_cairo_surface()
[platform/upstream/intel-gpu-tools.git] / lib / igt_fb.c
1 /*
2  * Copyright © 2013,2014 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *      Daniel Vetter <daniel.vetter@ffwll.ch>
25  *      Damien Lespiau <damien.lespiau@intel.com>
26  */
27
28 #define _GNU_SOURCE
29 #include <stdio.h>
30 #include <math.h>
31
32 #include "drmtest.h"
33 #include "igt_fb.h"
34 #include "ioctl_wrappers.h"
35
36 /**
37  * SECTION:igt_fb
38  * @short_description: Framebuffer handling and drawing library
39  * @title: i-g-t framebuffer
40  * @include: igt_fb.h
41  *
42  * This library contains helper functions for handling kms framebuffer objects
43  * using #igt_fb structures to track all the metadata.  igt_create_fb() creates
44  * a basic framebufffer and igt_remove_fb() cleans everything up again.
45  *
46  * It also supports drawing using the cairo library and provides some simplified
47  * helper functions to easily draw test patterns. The main function to create a
48  * cairo drawing context for a framebuffer object is igt_get_cairo_ctx().
49  *
50  * Finally it also pulls in the drm fourcc headers and provides some helper
51  * functions to work with these pixel format codes.
52  */
53
54 /* drm fourcc/cairo format maps */
55 #define DF(did, cid, _bpp, _depth)      \
56         { DRM_FORMAT_##did, CAIRO_FORMAT_##cid, # did, _bpp, _depth }
57 static struct format_desc_struct {
58         uint32_t drm_id;
59         cairo_format_t cairo_id;
60         const char *name;
61         int bpp;
62         int depth;
63 } format_desc[] = {
64         DF(RGB565,      RGB16_565,      16, 16),
65         DF(RGB888,      INVALID,        24, 24),
66         DF(XRGB8888,    RGB24,          32, 24),
67         DF(XRGB2101010, RGB30,          32, 30),
68         DF(ARGB8888,    ARGB32,         32, 32),
69 };
70 #undef DF
71
72 #define for_each_format(f)      \
73         for (f = format_desc; f - format_desc < ARRAY_SIZE(format_desc); f++)
74
75
76 /* helpers to create nice-looking framebuffers */
77 static int create_bo_for_fb(int fd, int width, int height, int bpp,
78                             bool tiled, uint32_t *gem_handle_ret,
79                             unsigned *size_ret, unsigned *stride_ret,
80                             unsigned bo_size)
81 {
82         uint32_t gem_handle;
83         int size, ret = 0;
84         unsigned stride;
85
86         if (tiled) {
87                 int v;
88
89                 /* Round the tiling up to the next power-of-two and the
90                  * region up to the next pot fence size so that this works
91                  * on all generations.
92                  *
93                  * This can still fail if the framebuffer is too large to
94                  * be tiled. But then that failure is expected.
95                  */
96
97                 v = width * bpp / 8;
98                 for (stride = 512; stride < v; stride *= 2)
99                         ;
100
101                 v = stride * height;
102                 for (size = 1024*1024; size < v; size *= 2)
103                         ;
104         } else {
105                 /* Scan-out has a 64 byte alignment restriction */
106                 stride = (width * (bpp / 8) + 63) & ~63;
107                 size = stride * height;
108         }
109
110         if (bo_size == 0)
111                 bo_size = size;
112         gem_handle = gem_create(fd, bo_size);
113
114         if (tiled)
115                 ret = __gem_set_tiling(fd, gem_handle, I915_TILING_X, stride);
116
117         *stride_ret = stride;
118         *size_ret = size;
119         *gem_handle_ret = gem_handle;
120
121         return ret;
122 }
123
124 /**
125  * igt_paint_color:
126  * @cr: cairo drawing context
127  * @x: pixel x-coordination of the fill rectangle
128  * @y: pixel y-coordination of the fill rectangle
129  * @w: width of the fill rectangle
130  * @h: height of the fill rectangle
131  * @r: red value to use as fill color
132  * @g: gree value to use as fill color
133  * @b: blue value to use as fill color
134  *
135  * This functions draws a solid rectangle with the given color using the drawing
136  * context @cr.
137  */
138 void igt_paint_color(cairo_t *cr, int x, int y, int w, int h,
139                      double r, double g, double b)
140 {
141         cairo_rectangle(cr, x, y, w, h);
142         cairo_set_source_rgb(cr, r, g, b);
143         cairo_fill(cr);
144 }
145
146 /**
147  * igt_paint_color_alpha:
148  * @cr: cairo drawing context
149  * @x: pixel x-coordination of the fill rectangle
150  * @y: pixel y-coordination of the fill rectangle
151  * @w: width of the fill rectangle
152  * @h: height of the fill rectangle
153  * @r: red value to use as fill color
154  * @g: gree value to use as fill color
155  * @b: blue value to use as fill color
156  * @a: alpha value to use as fill color
157  *
158  * This functions draws a rectangle with the given color and alpha values using
159  * the drawing context @cr.
160  */
161 void igt_paint_color_alpha(cairo_t *cr, int x, int y, int w, int h,
162                            double r, double g, double b, double a)
163 {
164         cairo_rectangle(cr, x, y, w, h);
165         cairo_set_source_rgba(cr, r, g, b, a);
166         cairo_fill(cr);
167 }
168
169 /**
170  * igt_paint_color_gradient:
171  * @cr: cairo drawing context
172  * @x: pixel x-coordination of the fill rectangle
173  * @y: pixel y-coordination of the fill rectangle
174  * @w: width of the fill rectangle
175  * @h: height of the fill rectangle
176  * @r: red value to use as fill color
177  * @g: gree value to use as fill color
178  * @b: blue value to use as fill color
179  *
180  * This functions draws a gradient into the rectangle which fades in from black
181  * to the given values using the drawing context @cr.
182  */
183 void
184 igt_paint_color_gradient(cairo_t *cr, int x, int y, int w, int h,
185                          int r, int g, int b)
186 {
187         cairo_pattern_t *pat;
188
189         pat = cairo_pattern_create_linear(x, y, x + w, y + h);
190         cairo_pattern_add_color_stop_rgba(pat, 1, 0, 0, 0, 1);
191         cairo_pattern_add_color_stop_rgba(pat, 0, r, g, b, 1);
192
193         cairo_rectangle(cr, x, y, w, h);
194         cairo_set_source(cr, pat);
195         cairo_fill(cr);
196         cairo_pattern_destroy(pat);
197 }
198
199 static void
200 paint_test_patterns(cairo_t *cr, int width, int height)
201 {
202         double gr_height, gr_width;
203         int x, y;
204
205         y = height * 0.10;
206         gr_width = width * 0.75;
207         gr_height = height * 0.08;
208         x = (width / 2) - (gr_width / 2);
209
210         igt_paint_color_gradient(cr, x, y, gr_width, gr_height, 1, 0, 0);
211
212         y += gr_height;
213         igt_paint_color_gradient(cr, x, y, gr_width, gr_height, 0, 1, 0);
214
215         y += gr_height;
216         igt_paint_color_gradient(cr, x, y, gr_width, gr_height, 0, 0, 1);
217
218         y += gr_height;
219         igt_paint_color_gradient(cr, x, y, gr_width, gr_height, 1, 1, 1);
220 }
221
222 /**
223  * igt_cairo_printf_line:
224  * @cr: cairo drawing context
225  * @align: text alignment
226  * @yspacing: additional y-direction feed after this line
227  * @fmt: format string
228  * @...: optional arguments used in the format string
229  *
230  * This is a little helper to draw text onto framebuffers. All the initial setup
231  * (like setting the font size and the moving to the starting position) still
232  * needs to be done manually with explicit cairo calls on @cr.
233  *
234  * Returns:
235  * The width of the drawn text.
236  */
237 int igt_cairo_printf_line(cairo_t *cr, enum igt_text_align align,
238                                 double yspacing, const char *fmt, ...)
239 {
240         double x, y, xofs, yofs;
241         cairo_text_extents_t extents;
242         char *text;
243         va_list ap;
244         int ret;
245
246         va_start(ap, fmt);
247         ret = vasprintf(&text, fmt, ap);
248         igt_assert(ret >= 0);
249         va_end(ap);
250
251         cairo_text_extents(cr, text, &extents);
252
253         xofs = yofs = 0;
254         if (align & align_right)
255                 xofs = -extents.width;
256         else if (align & align_hcenter)
257                 xofs = -extents.width / 2;
258
259         if (align & align_top)
260                 yofs = extents.height;
261         else if (align & align_vcenter)
262                 yofs = extents.height / 2;
263
264         cairo_get_current_point(cr, &x, &y);
265         if (xofs || yofs)
266                 cairo_rel_move_to(cr, xofs, yofs);
267
268         cairo_text_path(cr, text);
269         cairo_set_source_rgb(cr, 0, 0, 0);
270         cairo_stroke_preserve(cr);
271         cairo_set_source_rgb(cr, 1, 1, 1);
272         cairo_fill(cr);
273
274         cairo_move_to(cr, x, y + extents.height + yspacing);
275
276         free(text);
277
278         return extents.width;
279 }
280
281 static void
282 paint_marker(cairo_t *cr, int x, int y)
283 {
284         enum igt_text_align align;
285         int xoff, yoff;
286
287         cairo_move_to(cr, x, y - 20);
288         cairo_line_to(cr, x, y + 20);
289         cairo_move_to(cr, x - 20, y);
290         cairo_line_to(cr, x + 20, y);
291         cairo_new_sub_path(cr);
292         cairo_arc(cr, x, y, 10, 0, M_PI * 2);
293         cairo_set_line_width(cr, 4);
294         cairo_set_source_rgb(cr, 0, 0, 0);
295         cairo_stroke_preserve(cr);
296         cairo_set_source_rgb(cr, 1, 1, 1);
297         cairo_set_line_width(cr, 2);
298         cairo_stroke(cr);
299
300         xoff = x ? -20 : 20;
301         align = x ? align_right : align_left;
302
303         yoff = y ? -20 : 20;
304         align |= y ? align_bottom : align_top;
305
306         cairo_move_to(cr, x + xoff, y + yoff);
307         cairo_set_font_size(cr, 18);
308         igt_cairo_printf_line(cr, align, 0, "(%d, %d)", x, y);
309 }
310
311 /**
312  * igt_paint_test_pattern:
313  * @cr: cairo drawing context
314  * @width: width of the visible area
315  * @height: height of the visible area
316  *
317  * This functions draws an entire set of test patterns for the given visible
318  * area using the drawing context @cr. This is useful for manual visual
319  * inspection of displayed framebuffers.
320  *
321  * The test patterns include
322  *  - corner markers to check for over/underscan and
323  *  - a set of color and b/w gradients.
324  */
325 void igt_paint_test_pattern(cairo_t *cr, int width, int height)
326 {
327         paint_test_patterns(cr, width, height);
328
329         cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
330
331         /* Paint corner markers */
332         paint_marker(cr, 0, 0);
333         paint_marker(cr, width, 0);
334         paint_marker(cr, 0, height);
335         paint_marker(cr, width, height);
336
337         igt_assert(!cairo_status(cr));
338 }
339
340 /**
341  * igt_paint_image:
342  * @cr: cairo drawing context
343  * @filename: filename of the png image to draw
344  * @dst_x: pixel x-coordination of the destination rectangle
345  * @dst_y: pixel y-coordination of the destination rectangle
346  * @dst_width: width of the destination rectangle
347  * @dst_height: height of the destination rectangle
348  *
349  * This function can be used to draw a scaled version of the supplied png image.
350  * This is currently only used by the CR-code based testing in the "testdisplay"
351  * testcase.
352  */
353 void igt_paint_image(cairo_t *cr, const char *filename,
354                      int dst_x, int dst_y, int dst_width, int dst_height)
355 {
356         cairo_surface_t *image;
357         int img_width, img_height;
358         double scale_x, scale_y;
359
360         image = cairo_image_surface_create_from_png(filename);
361         igt_assert(cairo_surface_status(image) == CAIRO_STATUS_SUCCESS);
362
363         img_width = cairo_image_surface_get_width(image);
364         img_height = cairo_image_surface_get_height(image);
365
366         scale_x = (double)dst_width / img_width;
367         scale_y = (double)dst_height / img_height;
368
369         cairo_save(cr);
370
371         cairo_translate(cr, dst_x, dst_y);
372         cairo_scale(cr, scale_x, scale_y);
373         cairo_set_source_surface(cr, image, 0, 0);
374         cairo_paint(cr);
375
376         cairo_surface_destroy(image);
377
378         cairo_restore(cr);
379 }
380
381 /**
382  * igt_create_fb_with_bo_size:
383  * @fd: open i915 drm file descriptor
384  * @width: width of the framebuffer in pixel
385  * @height: height of the framebuffer in pixel
386  * @format: drm fourcc pixel format code
387  * @tiled: X-tiled or linear framebuffer
388  * @fb: pointer to an #igt_fb structure
389  * @bo_size: size of the backing bo (0 for minimum needed size)
390  *
391  * This function allocates a gem buffer object suitable to back a framebuffer
392  * with the requested properties and then wraps it up in a drm framebuffer
393  * object of the requested size. All metadata is stored in @fb.
394  *
395  * The backing storage of the framebuffer is filled with all zeros, i.e. black
396  * for rgb pixel formats.
397  *
398  * Returns:
399  * The kms id of the created framebuffer on success or a negative error code on
400  * failure.
401  */
402 unsigned int igt_create_fb_with_bo_size(int fd, int width, int height,
403                                         uint32_t format, bool tiled,
404                                         struct igt_fb *fb, unsigned bo_size)
405 {
406         uint32_t handles[4];
407         uint32_t pitches[4];
408         uint32_t offsets[4];
409         uint32_t fb_id;
410         int bpp;
411         int ret;
412
413         memset(fb, 0, sizeof(*fb));
414
415         bpp = igt_drm_format_to_bpp(format);
416         ret = create_bo_for_fb(fd, width, height, bpp, tiled, &fb->gem_handle,
417                               &fb->size, &fb->stride, bo_size);
418         if (ret < 0)
419                 return ret;
420
421         memset(handles, 0, sizeof(handles));
422         handles[0] = fb->gem_handle;
423         memset(pitches, 0, sizeof(pitches));
424         pitches[0] = fb->stride;
425         memset(offsets, 0, sizeof(offsets));
426         if (drmModeAddFB2(fd, width, height, format, handles, pitches,
427                           offsets, &fb_id, 0) < 0) {
428                 gem_close(fd, fb->gem_handle);
429
430                 return 0;
431         }
432
433         fb->width = width;
434         fb->height = height;
435         fb->tiling = tiled;
436         fb->drm_format = format;
437         fb->fb_id = fb_id;
438
439         return fb_id;
440 }
441
442 /**
443  * igt_create_fb:
444  * @fd: open i915 drm file descriptor
445  * @width: width of the framebuffer in pixel
446  * @height: height of the framebuffer in pixel
447  * @format: drm fourcc pixel format code
448  * @tiled: X-tiled or linear framebuffer
449  * @fb: pointer to an #igt_fb structure
450  *
451  * This function allocates a gem buffer object suitable to back a framebuffer
452  * with the requested properties and then wraps it up in a drm framebuffer
453  * object. All metadata is stored in @fb.
454  *
455  * The backing storage of the framebuffer is filled with all zeros, i.e. black
456  * for rgb pixel formats.
457  *
458  * Returns:
459  * The kms id of the created framebuffer on success or a negative error code on
460  * failure.
461  */
462 unsigned int igt_create_fb(int fd, int width, int height, uint32_t format,
463                            bool tiled, struct igt_fb *fb)
464 {
465         return igt_create_fb_with_bo_size(fd, width, height, format, tiled, fb, 0);
466 }
467
468 /**
469  * igt_create_color_fb:
470  * @fd: open i915 drm file descriptor
471  * @width: width of the framebuffer in pixel
472  * @height: height of the framebuffer in pixel
473  * @format: drm fourcc pixel format code
474  * @tiled: X-tiled or linear framebuffer
475  * @r: red value to use as fill color
476  * @g: gree value to use as fill color
477  * @b: blue value to use as fill color
478  * @fb: pointer to an #igt_fb structure
479  *
480  * This function allocates a gem buffer object suitable to back a framebuffer
481  * with the requested properties and then wraps it up in a drm framebuffer
482  * object. All metadata is stored in @fb.
483  *
484  * Compared to igt_create_fb() this function also fills the entire framebuffer
485  * with the given color, which is useful for some simple pipe crc based tests.
486  *
487  * Returns:
488  * The kms id of the created framebuffer on success or a negative error code on
489  * failure.
490  */
491 unsigned int igt_create_color_fb(int fd, int width, int height,
492                                  uint32_t format, bool tiled,
493                                  double r, double g, double b,
494                                  struct igt_fb *fb /* out */)
495 {
496         unsigned int fb_id;
497         cairo_t *cr;
498
499         fb_id = igt_create_fb(fd, width, height, format, tiled, fb);
500         igt_assert(fb_id);
501
502         cr = igt_get_cairo_ctx(fd, fb);
503         igt_paint_color(cr, 0, 0, width, height, r, g, b);
504         igt_assert(cairo_status(cr) == 0);
505         cairo_destroy(cr);
506
507         return fb_id;
508 }
509
510 static cairo_format_t drm_format_to_cairo(uint32_t drm_format)
511 {
512         struct format_desc_struct *f;
513
514         for_each_format(f)
515                 if (f->drm_id == drm_format)
516                         return f->cairo_id;
517
518         igt_fail(101);
519 }
520
521 static void destroy_cairo_surface__gtt(void *arg)
522 {
523         struct igt_fb *fb = arg;
524         munmap(cairo_image_surface_get_data(fb->cairo_surface), fb->size);
525 }
526
527 static void create_cairo_surface__gtt(int fd, struct igt_fb *fb)
528 {
529         fb->cairo_surface =
530                 cairo_image_surface_create_for_data(gem_mmap(fd, fb->gem_handle, fb->size, PROT_READ | PROT_WRITE),
531                                                     drm_format_to_cairo(fb->drm_format),
532                                                     fb->width, fb->height, fb->stride);
533
534         cairo_surface_set_user_data(fb->cairo_surface,
535                                     (cairo_user_data_key_t *)create_cairo_surface__gtt,
536                                     fb, destroy_cairo_surface__gtt);
537 }
538
539 static cairo_surface_t *get_cairo_surface(int fd, struct igt_fb *fb)
540 {
541         if (fb->cairo_surface == NULL)
542                 create_cairo_surface__gtt(fd, fb);
543
544         gem_set_domain(fd, fb->gem_handle,
545                        I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
546
547         igt_assert(cairo_surface_status(fb->cairo_surface) == CAIRO_STATUS_SUCCESS);
548         return cairo_surface_reference(fb->cairo_surface);
549 }
550
551 /**
552  * igt_get_cairo_ctx:
553  * @fd: open i915 drm file descriptor
554  * @fb: pointer to an #igt_fb structure
555  *
556  * This initializes a cairo surface for @fb and then allocates a drawing context
557  * for it. The return cairo drawing context should be released by calling
558  * cairo_destroy(). This also sets a default font for drawing text on
559  * framebuffers.
560  *
561  * Returns:
562  * The created cairo drawing context.
563  */
564 cairo_t *igt_get_cairo_ctx(int fd, struct igt_fb *fb)
565 {
566         cairo_surface_t *surface;
567         cairo_t *cr;
568
569         surface = get_cairo_surface(fd, fb);
570         cr = cairo_create(surface);
571         cairo_surface_destroy(surface);
572         igt_assert(cairo_status(cr) == CAIRO_STATUS_SUCCESS);
573
574         cairo_select_font_face(cr, "Helvetica", CAIRO_FONT_SLANT_NORMAL,
575                                CAIRO_FONT_WEIGHT_NORMAL);
576         igt_assert(cairo_status(cr) == CAIRO_STATUS_SUCCESS);
577
578         return cr;
579 }
580
581 /**
582  * igt_write_fb_to_png:
583  * @fd: open i915 drm file descriptor
584  * @fb: pointer to an #igt_fb structure
585  * @filename: target name for the png image
586  *
587  * This function stores the contents of the supplied framebuffer into a png
588  * image stored at @filename.
589  */
590 void igt_write_fb_to_png(int fd, struct igt_fb *fb, const char *filename)
591 {
592         cairo_surface_t *surface;
593         cairo_status_t status;
594
595         surface = get_cairo_surface(fd, fb);
596         status = cairo_surface_write_to_png(surface, filename);
597         cairo_surface_destroy(surface);
598
599         igt_assert(status == CAIRO_STATUS_SUCCESS);
600 }
601
602 /**
603  * igt_remove_fb:
604  * @fd: open i915 drm file descriptor
605  * @fb: pointer to an #igt_fb structure
606  *
607  * This function releases all resources allocated in igt_create_fb() for @fb.
608  * Note that if this framebuffer is still in use on a primary plane the kernel
609  * will disable the corresponding crtc.
610  */
611 void igt_remove_fb(int fd, struct igt_fb *fb)
612 {
613         cairo_surface_destroy(fb->cairo_surface);
614         do_or_die(drmModeRmFB(fd, fb->fb_id));
615         gem_close(fd, fb->gem_handle);
616 }
617
618 /**
619  * igt_bpp_depth_to_drm_format:
620  * @bpp: desired bits per pixel
621  * @depth: desired depth
622  *
623  * Returns:
624  * The rgb drm fourcc pixel format code corresponding to the given @bpp and
625  * @depth values.  Fails hard if no match was found.
626  */
627 uint32_t igt_bpp_depth_to_drm_format(int bpp, int depth)
628 {
629         struct format_desc_struct *f;
630
631         for_each_format(f)
632                 if (f->bpp == bpp && f->depth == depth)
633                         return f->drm_id;
634
635         igt_fail(101);
636 }
637
638 /**
639  * igt_drm_format_to_bpp:
640  * @drm_format: drm fourcc pixel format code
641  *
642  * Returns:
643  * The bits per pixel for the given drm fourcc pixel format code. Fails hard if
644  * no match was found.
645  */
646 uint32_t igt_drm_format_to_bpp(uint32_t drm_format)
647 {
648         struct format_desc_struct *f;
649
650         for_each_format(f)
651                 if (f->drm_id == drm_format)
652                         return f->bpp;
653
654         igt_fail(101);
655 }
656
657 /**
658  * igt_format_str:
659  * @drm_format: drm fourcc pixel format code
660  *
661  * Returns:
662  * Human-readable fourcc pixel format code for @drm_format or "invalid" no match
663  * was found.
664  */
665 const char *igt_format_str(uint32_t drm_format)
666 {
667         struct format_desc_struct *f;
668
669         for_each_format(f)
670                 if (f->drm_id == drm_format)
671                         return f->name;
672
673         return "invalid";
674 }
675
676 /**
677  * igt_get_all_formats:
678  * @formats: pointer to pointer to store the allocated formats array
679  * @format_count: pointer to integer to store the size of the allocated array
680  *
681  * This functions returns an array of all the drm fourcc codes supported by this
682  * library. The caller must free the allocated array again with free().
683  */
684 void igt_get_all_formats(const uint32_t **formats, int *format_count)
685 {
686         static uint32_t *drm_formats;
687
688         if (!drm_formats) {
689                 struct format_desc_struct *f;
690                 uint32_t *format;
691
692                 drm_formats = calloc(ARRAY_SIZE(format_desc),
693                                      sizeof(*drm_formats));
694                 format = &drm_formats[0];
695                 for_each_format(f)
696                         *format++ = f->drm_id;
697         }
698
699         *formats = drm_formats;
700         *format_count = ARRAY_SIZE(format_desc);
701 }