1 /*
2  * Copyright © 2008-2011 Kristian Høgsberg
3  * Copyright © 2011 Intel Corporation
4  * Copyright © 2015 Giulio Camuffo
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial
16  * portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  */
27 
28 #ifndef WESTON_COMPOSITOR_DRM_H
29 #define WESTON_COMPOSITOR_DRM_H
30 
31 #include <libweston/libweston.h>
32 #include <libweston/plugin-registry.h>
33 
34 #ifdef  __cplusplus
35 extern "C" {
36 #endif
37 
38 #define WESTON_DRM_BACKEND_CONFIG_VERSION 3
39 
40 struct libinput_device;
41 
42 enum weston_drm_backend_output_mode {
43 	/** The output is disabled */
44 	WESTON_DRM_BACKEND_OUTPUT_OFF,
45 	/** The output will use the current active mode */
46 	WESTON_DRM_BACKEND_OUTPUT_CURRENT,
47 	/** The output will use the preferred mode. A modeline can be provided
48 	 * by setting weston_backend_output_config::modeline in the form of
49 	 * "WIDTHxHEIGHT" or in the form of an explicit modeline calculated
50 	 * using e.g. the cvt tool. If a valid modeline is supplied it will be
51 	 * used, if invalid or NULL the preferred available mode will be used. */
52 	WESTON_DRM_BACKEND_OUTPUT_PREFERRED,
53 };
54 
55 #define WESTON_DRM_OUTPUT_API_NAME "weston_drm_output_api_v1"
56 
57 struct weston_drm_output_api {
58 	/** The mode to be used by the output. Refer to the documentation
59 	 *  of WESTON_DRM_BACKEND_OUTPUT_PREFERRED for details.
60 	 *
61 	 * Returns 0 on success, -1 on failure.
62 	 */
63 	int (*set_mode)(struct weston_output *output,
64 			enum weston_drm_backend_output_mode mode,
65 			const char *modeline);
66 
67 	/** The pixel format to be used by the output. Valid values are:
68 	 * - NULL - The format set at backend creation time will be used;
69 	 * - "xrgb8888";
70 	 * - "rgb565"
71 	 * - "xrgb2101010"
72 	 */
73 	void (*set_gbm_format)(struct weston_output *output,
74 			       const char *gbm_format);
75 
76 	/** The seat to be used by the output. Set to NULL to use the
77 	 *  default seat.
78 	 */
79 	void (*set_seat)(struct weston_output *output,
80 			 const char *seat);
81 };
82 
83 static inline const struct weston_drm_output_api *
weston_drm_output_get_api(struct weston_compositor * compositor)84 weston_drm_output_get_api(struct weston_compositor *compositor)
85 {
86 	const void *api;
87 	api = weston_plugin_api_get(compositor, WESTON_DRM_OUTPUT_API_NAME,
88 				    sizeof(struct weston_drm_output_api));
89 
90 	return (const struct weston_drm_output_api *)api;
91 }
92 
93 #define WESTON_DRM_VIRTUAL_OUTPUT_API_NAME "weston_drm_virtual_output_api_v1"
94 
95 struct drm_fb;
96 typedef int (*submit_frame_cb)(struct weston_output *output, int fd,
97 			       int stride, struct drm_fb *buffer);
98 
99 struct weston_drm_virtual_output_api {
100 	/** Create virtual output.
101 	 * This is a low-level function, where the caller is expected to wrap
102 	 * the weston_output function pointers as necessary to make the virtual
103 	 * output useful. The caller must set up output make, model, serial,
104 	 * physical size, the mode list and current mode.
105 	 *
106 	 * Returns output on success, NULL on failure.
107 	 */
108 	struct weston_output* (*create_output)(struct weston_compositor *c,
109 					       char *name);
110 
111 	/** Set pixel format same as drm_output set_gbm_format().
112 	 *
113 	 * Returns the set format.
114 	 */
115 	uint32_t (*set_gbm_format)(struct weston_output *output,
116 				   const char *gbm_format);
117 
118 	/** Set a callback to be called when the DRM-backend has drawn a new
119 	 * frame and submits it for display.
120 	 * The callback will deliver a buffer to the virtual output's the
121 	 * owner and assumes the buffer is now reserved for the owner. The
122 	 * callback is called in virtual output repaint function.
123 	 * The caller must call buffer_released() and finish_frame().
124 	 *
125 	 * The callback parameters are output, FD and stride (bytes) of dmabuf,
126 	 * and buffer (drm_fb) pointer.
127 	 * The callback returns 0 on success, -1 on failure.
128 	 *
129 	 * The submit_frame_cb callback hook is responsible for closing the fd
130 	 * if it returns success. One needs to call the buffer release and
131 	 * finish frame functions if and only if this hook returns success.
132 	 */
133 	void (*set_submit_frame_cb)(struct weston_output *output,
134 				    submit_frame_cb cb);
135 
136 	/** Get fd for renderer fence.
137 	 * The returned fence signals when the renderer job has completed and
138 	 * the buffer is fully drawn.
139 	 *
140 	 * Returns fd on success, -1 on failure.
141 	 */
142 	int (*get_fence_sync_fd)(struct weston_output *output);
143 
144 	/** Notify that the caller has finished using buffer */
145 	void (*buffer_released)(struct drm_fb *fb);
146 
147 	/** Notify finish frame
148 	 * This function allows the output repainting mechanism to advance to
149 	 * the next frame.
150 	 */
151 	void (*finish_frame)(struct weston_output *output,
152 			     struct timespec *stamp,
153 			     uint32_t presented_flags);
154 };
155 
156 static inline const struct weston_drm_virtual_output_api *
weston_drm_virtual_output_get_api(struct weston_compositor * compositor)157 weston_drm_virtual_output_get_api(struct weston_compositor *compositor)
158 {
159 	const void *api;
160 	api = weston_plugin_api_get(compositor,
161 				    WESTON_DRM_VIRTUAL_OUTPUT_API_NAME,
162 				    sizeof(struct weston_drm_virtual_output_api));
163 	return (const struct weston_drm_virtual_output_api *)api;
164 }
165 
166 /** The backend configuration struct.
167  *
168  * weston_drm_backend_config contains the configuration used by a DRM
169  * backend.
170  */
171 struct weston_drm_backend_config {
172 	struct weston_backend_config base;
173 
174 	/** The tty to be used. Set to 0 to use the current tty. */
175 	int tty;
176 
177 	/** Whether to use the pixman renderer instead of the OpenGL ES renderer. */
178 	bool use_pixman;
179 
180 	/** The seat to be used for input and output.
181 	 *
182 	 * If seat_id is NULL, the seat is taken from XDG_SEAT environment
183 	 * variable. If neither is set, "seat0" is used. The backend will
184 	 * take ownership of the seat_id pointer and will free it on
185 	 * backend destruction.
186 	 */
187 	char *seat_id;
188 
189 	/** The pixel format of the framebuffer to be used.
190 	 *
191 	 * Valid values are:
192 	 * - NULL - The default format ("xrgb8888") will be used;
193 	 * - "xrgb8888";
194 	 * - "rgb565"
195 	 * - "xrgb2101010"
196 	 * The backend will take ownership of the format pointer and will free
197 	 * it on backend destruction.
198 	 */
199 	char *gbm_format;
200 
201 	/** Callback used to configure input devices.
202 	 *
203 	 * This function will be called by the backend when a new input device
204 	 * needs to be configured.
205 	 * If NULL the device will use the default configuration.
206 	 */
207 	void (*configure_device)(struct weston_compositor *compositor,
208 				 struct libinput_device *device);
209 
210 	/** Maximum duration for a pageflip event to arrive, after which the
211 	 * compositor will consider the DRM driver crashed and will try to exit
212 	 * cleanly.
213 	 *
214 	 * It is exprimed in milliseconds, 0 means disabled. */
215 	uint32_t pageflip_timeout;
216 
217 	/** Specific DRM device to open
218 	 *
219 	 * A DRM device name, like "card0", to open. If NULL, use heuristics
220 	 * based on seat names and boot_vga to find the right device.
221 	 */
222 	char *specific_device;
223 
224 	/** Use shadow buffer if using Pixman-renderer. */
225 	bool use_pixman_shadow;
226 };
227 
228 #ifdef  __cplusplus
229 }
230 #endif
231 
232 #endif /* WESTON_COMPOSITOR_DRM_H */
233