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