xref: /aosp_15_r20/external/igt-gpu-tools/lib/igt_fb.h (revision d83cc019efdc2edc6c4b16e9034a3ceb8d35d77c)
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 <[email protected]>
25  * 	Damien Lespiau <[email protected]>
26  */
27 
28 #ifndef __IGT_FB_H__
29 #define __IGT_FB_H__
30 
31 #include <cairo.h>
32 #include <stddef.h>
33 #include <stdbool.h>
34 #include <drm_fourcc.h>
35 #include <xf86drmMode.h>
36 
37 #include <i915_drm.h>
38 
39 #include "igt_color_encoding.h"
40 #include "igt_debugfs.h"
41 
42 #if !defined(ANDROID)
43 #define USE_AMD
44 #define USE_INTEL
45 #define USE_VC4
46 #endif
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 /*
53  * Internal format to denote a buffer compatible with pixman's
54  * floating point format. Range [0-1].
55  */
56 #define IGT_FORMAT_FLOAT fourcc_code('I', 'G', 'F', 'x')
57 
58 #define IGT_FORMAT_FMT "%c%c%c%c(0x%08x)"
59 #define IGT_FORMAT_ARGS(f) ((f) >> 0) & 0xff, ((f) >> 8) & 0xff, \
60 		((f) >> 16) & 0xff, ((f) >> 24) & 0xff, (f)
61 
62 /**
63  * igt_fb_t:
64  * @fb_id: KMS ID of the framebuffer
65  * @fd: DRM device fd this framebuffer is created on
66  * @gem_handle: GEM handler of the underlying backing storage
67  * @is_dumb: Whether this framebuffer was allocated using the dumb buffer API
68  * @drm_format: DRM FOURCC code
69  * @width: width in pixels
70  * @height: height in pixels
71  * @modifier: tiling mode as a DRM framebuffer modifier
72  * @size: size in bytes of the underlying backing storage
73  * @cairo_surface: optionally attached cairo drawing surface
74  * @domain: current domain for cache flushing tracking on i915.ko
75  * @num_planes: Amount of planes on this fb. >1 for planar formats.
76  * @strides: line stride for each plane in bytes
77  * @offsets: Offset for each plane in bytes.
78  * @plane_bpp: The bpp for each plane.
79  * @plane_width: The width for each plane.
80  * @plane_height: The height for each plane.
81  *
82  * Tracking structure for KMS framebuffer objects.
83  */
84 typedef struct igt_fb {
85 	uint32_t fb_id;
86 	int fd;
87 	uint32_t gem_handle;
88 	bool is_dumb;
89 	uint32_t drm_format;
90 	int width;
91 	int height;
92 	enum igt_color_encoding color_encoding;
93 	enum igt_color_range color_range;
94 	uint64_t modifier;
95 	uint64_t size;
96 	cairo_surface_t *cairo_surface;
97 	unsigned int domain;
98 	unsigned int num_planes;
99 	uint32_t strides[4];
100 	uint32_t offsets[4];
101 	unsigned int plane_bpp[4];
102 	unsigned int plane_width[4];
103 	unsigned int plane_height[4];
104 } igt_fb_t;
105 
106 /**
107  * igt_text_align:
108  * @align_left: align left
109  * @align_right: align right
110  * @align_bottom: align bottom
111  * @align_top: align top
112  * @align_vcenter: align vcenter
113  * @align_hcenter: align hcenter
114  *
115  * Alignment mode for text drawing using igt_cairo_printf_line().
116  */
117 enum igt_text_align {
118 	align_left,
119 	align_bottom	= align_left,
120 	align_right	= 0x01,
121 	align_top	= 0x02,
122 	align_vcenter	= 0x04,
123 	align_hcenter	= 0x08,
124 };
125 
126 void igt_get_fb_tile_size(int fd, uint64_t modifier, int fb_bpp,
127 			  unsigned *width_ret, unsigned *height_ret);
128 void igt_calc_fb_size(int fd, int width, int height, uint32_t format, uint64_t modifier,
129 		      uint64_t *size_ret, unsigned *stride_ret);
130 void igt_init_fb(struct igt_fb *fb, int fd, int width, int height,
131 		 uint32_t drm_format, uint64_t modifier,
132 		 enum igt_color_encoding color_encoding,
133 		 enum igt_color_range color_range);
134 unsigned int
135 igt_create_fb_with_bo_size(int fd, int width, int height,
136 			   uint32_t format, uint64_t modifier,
137 			   enum igt_color_encoding color_encoding,
138 			   enum igt_color_range color_range,
139 			   struct igt_fb *fb, uint64_t bo_size,
140 			   unsigned bo_stride);
141 unsigned int igt_create_fb(int fd, int width, int height, uint32_t format,
142 			   uint64_t modifier, struct igt_fb *fb);
143 unsigned int igt_create_color_fb(int fd, int width, int height,
144 				 uint32_t format, uint64_t modifier,
145 				 double r, double g, double b,
146 				 struct igt_fb *fb /* out */);
147 unsigned int igt_create_pattern_fb(int fd, int width, int height,
148 				   uint32_t format, uint64_t modifier,
149 				   struct igt_fb *fb /* out */);
150 unsigned int igt_create_color_pattern_fb(int fd, int width, int height,
151 					 uint32_t format, uint64_t modifier,
152 					 double r, double g, double b,
153 					 struct igt_fb *fb /* out */);
154 unsigned int igt_create_image_fb(int drm_fd,  int width, int height,
155 				 uint32_t format, uint64_t modifier,
156 				 const char *filename,
157 				 struct igt_fb *fb /* out */);
158 unsigned int igt_create_stereo_fb(int drm_fd, drmModeModeInfo *mode,
159 				  uint32_t format, uint64_t modifier);
160 unsigned int igt_fb_convert_with_stride(struct igt_fb *dst, struct igt_fb *src,
161 					uint32_t dst_fourcc,
162 					uint64_t dst_modifier,
163 					unsigned int stride);
164 unsigned int igt_fb_convert(struct igt_fb *dst, struct igt_fb *src,
165 			    uint32_t dst_fourcc, uint64_t dst_modifier);
166 void igt_remove_fb(int fd, struct igt_fb *fb);
167 int igt_dirty_fb(int fd, struct igt_fb *fb);
168 void *igt_fb_map_buffer(int fd, struct igt_fb *fb);
169 void igt_fb_unmap_buffer(struct igt_fb *fb, void *buffer);
170 
171 void igt_create_bo_for_fb(int fd, int width, int height,
172 			  uint32_t format, uint64_t modifier,
173 			  struct igt_fb *fb);
174 int igt_create_bo_with_dimensions(int fd, int width, int height, uint32_t format,
175 				  uint64_t modifier, unsigned stride,
176 				  uint64_t *size_ret, unsigned *stride_ret,
177 				  bool *is_dumb);
178 void igt_fb_calc_crc(struct igt_fb *fb, igt_crc_t *crc);
179 
180 uint64_t igt_fb_mod_to_tiling(uint64_t modifier);
181 uint64_t igt_fb_tiling_to_mod(uint64_t tiling);
182 
183 /* cairo-based painting */
184 cairo_surface_t *igt_get_cairo_surface(int fd, struct igt_fb *fb);
185 cairo_surface_t *igt_cairo_image_surface_create_from_png(const char *filename);
186 cairo_t *igt_get_cairo_ctx(int fd, struct igt_fb *fb);
187 void igt_put_cairo_ctx(int fd, struct igt_fb *fb, cairo_t *cr);
188 void igt_paint_color(cairo_t *cr, int x, int y, int w, int h,
189 			 double r, double g, double b);
190 void igt_paint_color_alpha(cairo_t *cr, int x, int y, int w, int h,
191 			       double r, double g, double b, double a);
192 void igt_paint_color_gradient(cairo_t *cr, int x, int y, int w, int h,
193 				  int r, int g, int b);
194 void igt_paint_color_gradient_range(cairo_t *cr, int x, int y, int w, int h,
195 				    double sr, double sg, double sb,
196 				    double er, double eg, double eb);
197 void igt_paint_test_pattern(cairo_t *cr, int width, int height);
198 void igt_paint_image(cairo_t *cr, const char *filename,
199 			 int dst_x, int dst_y, int dst_width, int dst_height);
200 int igt_cairo_printf_line(cairo_t *cr, enum igt_text_align align,
201 			       double yspacing, const char *fmt, ...)
202 			       __attribute__((format (printf, 4, 5)));
203 
204 /* helpers to handle drm fourcc codes */
205 uint32_t igt_bpp_depth_to_drm_format(int bpp, int depth);
206 uint32_t igt_drm_format_to_bpp(uint32_t drm_format);
207 const char *igt_format_str(uint32_t drm_format);
208 bool igt_fb_supported_format(uint32_t drm_format);
209 bool igt_format_is_yuv(uint32_t drm_format);
210 bool igt_format_is_fp16(uint32_t drm_format);
211 int igt_format_plane_bpp(uint32_t drm_format, int plane);
212 void igt_format_array_fill(uint32_t **formats_array, unsigned int *count,
213 			   bool allow_yuv);
214 
215 #ifdef __cplusplus
216 }
217 #endif
218 
219 #endif /* __IGT_FB_H__ */
220 
221