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