• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2013 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  */
24 
25 #include <sys/types.h>
26 #include <sys/mman.h>
27 #include <cairo.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdbool.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 
35 #include <drm.h>
36 #include <drm_fourcc.h>
37 #include <xf86drm.h>
38 #include <xf86drmMode.h>
39 #include <i915_drm.h>
40 #include "../overlay.h"
41 //#include "rgb2yuv.h"
42 
43 #ifndef ALIGN
44 #define ALIGN(i,m)	(((i) + (m) - 1) & ~((m) - 1))
45 #endif
46 
47 struct kms_image {
48 	uint32_t handle, name;
49 	uint32_t format;
50 	uint32_t width, height, stride;
51 	uint32_t size;
52 	void *map;
53 };
54 
55 struct kms_overlay {
56 	struct overlay base;
57 	struct kms_image image;
58 	int fd;
59 	int crtc;
60 
61 	int x, y;
62 	int visible;
63 
64 	void *mem;
65 	int size;
66 };
67 
to_kms_overlay(struct overlay * o)68 static inline struct kms_overlay *to_kms_overlay(struct overlay *o)
69 {
70 	return (struct kms_overlay *)o;
71 }
72 
kms_create_fb(int fd,struct kms_image * image)73 static int kms_create_fb(int fd, struct kms_image *image)
74 {
75 	uint32_t offsets[4], pitches[4], handles[4];
76 
77 	handles[0] = image->handle;
78 	pitches[0] = image->stride;
79 	offsets[0] = 0;
80 
81 	return drmModeAddFB2(fd,
82 			     image->width, image->height, image->format,
83 			     handles, pitches, offsets,
84 			     &image->name, 0) == 0;
85 }
86 
attach_to_crtc(int fd,int crtc,int x,int y,struct kms_image * image)87 static int attach_to_crtc(int fd, int crtc, int x, int y, struct kms_image *image)
88 {
89 	struct drm_mode_set_plane s;
90 
91 	s.crtc_id = crtc;
92 	s.fb_id = image->name;
93 	s.flags = 0;
94 	s.crtc_x = x;
95 	s.crtc_y = y;
96 	s.crtc_w = image->width;
97 	s.crtc_h = image->height;
98 	s.src_x = 0;
99 	s.src_y = 0;
100 	s.src_w = image->width << 16;
101 	s.src_h = image->height << 16;
102 
103 	return drmIoctl(fd, DRM_IOCTL_MODE_SETPLANE, &s) == 0;
104 }
105 
detach_from_crtc(int fd,int crtc)106 static int detach_from_crtc(int fd, int crtc)
107 {
108 	struct drm_mode_set_plane s;
109 
110 	memset(&s, 0, sizeof(s));
111 	s.crtc_id = crtc;
112 	return drmIoctl(fd, DRM_IOCTL_MODE_SETPLANE, &s) == 0;
113 }
114 
kms_overlay_show(struct overlay * overlay)115 static void kms_overlay_show(struct overlay *overlay)
116 {
117 	struct kms_overlay *priv = to_kms_overlay(overlay);
118 
119 	memcpy(priv->image.map, priv->mem, priv->size);
120 
121 	if (!priv->visible) {
122 		attach_to_crtc(priv->fd, priv->crtc, priv->x, priv->y, &priv->image);
123 		priv->visible = true;
124 	}
125 }
126 
kms_overlay_hide(struct overlay * overlay)127 static void kms_overlay_hide(struct overlay *overlay)
128 {
129 	struct kms_overlay *priv = to_kms_overlay(overlay);
130 
131 	if (priv->visible) {
132 		detach_from_crtc(priv->fd, priv->crtc);
133 		priv->visible = false;
134 	}
135 }
136 
kms_overlay_destroy(void * data)137 static void kms_overlay_destroy(void *data)
138 {
139 	struct kms_overlay *priv = data;
140 	drmIoctl(priv->fd, DRM_IOCTL_MODE_RMFB, &priv->image.name);
141 	munmap(priv->image.map, priv->image.size);
142 	free(priv->mem);
143 	close(priv->fd);
144 	free(priv);
145 }
146 
is_i915_device(int fd)147 static int is_i915_device(int fd)
148 {
149 	drm_version_t version;
150 	char name[5] = "";
151 
152 	memset(&version, 0, sizeof(version));
153 	version.name_len = 4;
154 	version.name = name;
155 
156 	if (drmIoctl(fd, DRM_IOCTL_VERSION, &version))
157 		return 0;
158 
159 	return strcmp("i915", name) == 0;
160 }
161 
check_device(int fd)162 static int check_device(int fd)
163 {
164 	int ret;
165 
166 	/* Confirm that this is a i915.ko device with GEM/KMS enabled */
167 	ret = is_i915_device(fd);
168 	if (ret) {
169 		struct drm_i915_getparam gp;
170 		gp.param = I915_PARAM_HAS_GEM;
171 		gp.value = &ret;
172 		if (drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp))
173 			ret = 0;
174 	}
175 	if (ret) {
176 		struct drm_mode_card_res res;
177 
178 		memset(&res, 0, sizeof(res));
179 		if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
180 			ret = 0;
181 	}
182 
183 	return ret;
184 }
185 
i915_open(void)186 static int i915_open(void)
187 {
188 	char buf[80];
189 	int fd, n;
190 
191 	for (n = 0; n < 16; n++) {
192 		sprintf(buf, "/dev/dri/card%d", n);
193 		fd = open(buf, O_RDWR);
194 		if (fd == -1)
195 			continue;
196 
197 		if (!check_device(fd)) {
198 			close(fd);
199 			continue;
200 		}
201 		return fd;
202 	}
203 
204 	return -1;
205 }
206 
config_get_pipe(struct config * config)207 static int config_get_pipe(struct config *config)
208 {
209 	const char *str;
210 
211 	str = config_get_value(config, "kms", "pipe");
212 	if (str == NULL)
213 		return 0;
214 
215 	return atoi(str);
216 }
217 
218 cairo_surface_t *
kms_overlay_create(struct config * config,int * width,int * height)219 kms_overlay_create(struct config *config, int *width, int *height)
220 {
221 	struct drm_i915_gem_create create;
222 	struct drm_i915_gem_mmap_gtt map;
223 	struct kms_overlay *priv;
224 	drmModeResPtr kmode;
225 	int i, pipe;
226 
227 	priv = malloc(sizeof(*priv));
228 	if (priv == NULL)
229 		return NULL;
230 
231 	priv->fd = i915_open();
232 	if (priv->fd == -1)
233 		goto err_priv;
234 
235 	kmode = drmModeGetResources(priv->fd);
236 	if (kmode == 0)
237 		goto err_fd;
238 
239 	pipe = config_get_pipe(config);
240 	priv->crtc = 0;
241 
242 	for (i = 0; i < kmode->count_crtcs; i++) {
243 		struct drm_i915_get_pipe_from_crtc_id get_pipe;
244 
245 		get_pipe.pipe = 0;
246 		get_pipe.crtc_id = kmode->crtcs[i];
247 		if (drmIoctl(priv->fd,
248 			     DRM_IOCTL_I915_GET_PIPE_FROM_CRTC_ID,
249 			     &get_pipe)) {
250 			continue;
251 		}
252 
253 		if (get_pipe.pipe != pipe)
254 			continue;
255 
256 		priv->crtc = get_pipe.crtc_id;
257 	}
258 
259 	if (priv->crtc == 0)
260 		goto err_fd;
261 
262 	priv->image.format = DRM_FORMAT_XRGB8888;
263 	priv->image.width = ALIGN(*width, 4);
264 	priv->image.height = ALIGN(*height, 2);
265 	priv->image.stride = ALIGN(4*priv->image.width, 64);
266 	priv->image.size = ALIGN(priv->image.stride * priv->image.height, 4096);
267 
268 	create.handle = 0;
269 	create.size = ALIGN(priv->image.size, 4096);
270 	drmIoctl(priv->fd, DRM_IOCTL_I915_GEM_CREATE, &create);
271 	if (create.handle == 0)
272 		goto err_fd;
273 
274 	priv->image.handle = create.handle;
275 
276 	if (!kms_create_fb(priv->fd, &priv->image))
277 		goto err_create;
278 
279 	/* XXX set color keys */
280 
281 	if (!attach_to_crtc(priv->fd, priv->crtc, 0, 0, &priv->image))
282 		goto err_fb;
283 	detach_from_crtc(priv->fd, priv->crtc);
284 
285 	map.handle = create.handle;
286 	if (drmIoctl(priv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &map))
287 		goto err_fb;
288 
289 	priv->image.map = mmap(0, create.size, PROT_READ | PROT_WRITE, MAP_SHARED, priv->fd, map.offset);
290 	if (priv->image.map == (void *)-1)
291 		goto err_fb;
292 
293 	priv->mem = malloc(create.size);
294 	if (priv->mem == NULL)
295 		goto err_map;
296 
297 	priv->base.surface =
298 		cairo_image_surface_create_for_data(priv->mem,
299 						    CAIRO_FORMAT_RGB24,
300 						    priv->image.width,
301 						    priv->image.height,
302 						    priv->image.stride);
303 	if (cairo_surface_status(priv->base.surface))
304 		goto err_mem;
305 
306 	priv->base.show = kms_overlay_show;
307 	priv->base.hide = kms_overlay_hide;
308 
309 	priv->visible = false;
310 	priv->x = 0;
311 	priv->y = 0;
312 
313 	cairo_surface_set_user_data(priv->base.surface, &overlay_key, priv, kms_overlay_destroy);
314 
315 	*width = priv->image.width;
316 	*height = priv->image.height;
317 
318 	drmIoctl(priv->fd, DRM_IOCTL_GEM_CLOSE, &create.handle);
319 	return priv->base.surface;
320 
321 err_mem:
322 	free(priv->mem);
323 err_map:
324 	munmap(priv->image.map, create.size);
325 err_fb:
326 	drmIoctl(priv->fd, DRM_IOCTL_MODE_RMFB, &priv->image.name);
327 err_create:
328 	drmIoctl(priv->fd, DRM_IOCTL_GEM_CLOSE, &create.handle);
329 err_fd:
330 	close(priv->fd);
331 err_priv:
332 	free(priv);
333 	return NULL;
334 }
335