Add README for the adventurous, allow evdev override from getenv().
[profile/ivi/wayland.git] / compositor.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include <i915_drm.h>
6 #include <sys/ioctl.h>
7 #include <sys/mman.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <linux/fb.h>
11
12 #include "wayland.h"
13
14 struct lame_compositor {
15         struct wl_compositor base;
16         void *fb;
17         int32_t width, height, stride;
18         int gem_fd;
19 };
20
21 struct surface_data {
22         uint32_t handle;
23         int32_t width, height, stride;
24 };
25
26 void notify_surface_create(struct wl_compositor *compositor,
27                            struct wl_surface *surface)
28 {
29         struct surface_data *sd;
30
31         sd = malloc(sizeof *sd);
32         if (sd == NULL)
33                 return;
34
35         sd->handle = 0;
36         wl_surface_set_data(surface, sd);
37 }
38                                    
39 void notify_surface_destroy(struct wl_compositor *compositor,
40                             struct wl_surface *surface)
41 {
42         struct lame_compositor *lc = (struct lame_compositor *) compositor;
43         struct surface_data *sd;
44         struct drm_gem_close close_arg;
45         int ret;
46
47         sd = wl_surface_get_data(surface);
48         if (sd == NULL || sd->handle == 0)
49                 return;
50         
51         close_arg.handle = sd->handle;
52         ret = ioctl(lc->gem_fd, DRM_IOCTL_GEM_CLOSE, &close_arg);
53         if (ret != 0) {
54                 fprintf(stderr, "failed to gem_close handle %d: %m\n", sd->handle);
55         }
56
57         free(sd);
58 }
59
60 void notify_surface_attach(struct wl_compositor *compositor,
61                            struct wl_surface *surface, uint32_t name, 
62                            uint32_t width, uint32_t height, uint32_t stride)
63 {
64         struct lame_compositor *lc = (struct lame_compositor *) compositor;
65         struct surface_data *sd;
66         struct drm_gem_open open_arg;
67         struct drm_gem_close close_arg;
68         int ret;
69
70         sd = wl_surface_get_data(surface);
71         if (sd == NULL)
72                 return;
73
74         if (sd->handle != 0) {
75                 close_arg.handle = sd->handle;
76                 ret = ioctl(lc->gem_fd, DRM_IOCTL_GEM_CLOSE, &close_arg);
77                 if (ret != 0) {
78                         fprintf(stderr, "failed to gem_close name %d: %m\n", name);
79                 }
80         }
81
82         open_arg.name = name;
83         ret = ioctl(lc->gem_fd, DRM_IOCTL_GEM_OPEN, &open_arg);
84         if (ret != 0) {
85                 fprintf(stderr, "failed to gem_open name %d, fd=%d: %m\n", name, lc->gem_fd);
86                 return;
87         }
88
89         sd->handle = open_arg.handle;
90         sd->width = width;
91         sd->height = height;
92         sd->stride = stride;
93 }
94
95 void notify_surface_map(struct wl_compositor *compositor,
96                         struct wl_surface *surface, struct wl_map *map)
97 {
98         struct lame_compositor *lc = (struct lame_compositor *) compositor;
99         struct surface_data *sd;
100         struct drm_i915_gem_pread pread;
101         char *data, *dst;
102         uint32_t size;
103         int i;
104
105         /* This part is where we actually copy the buffer to screen.
106          * Needs to be part of the repaint loop, not in the notify_map
107          * handler. */
108
109         sd = wl_surface_get_data(surface);
110         if (sd == NULL)
111                 return;
112
113         size = sd->height * sd->stride;
114         data = malloc(size);
115         if (data == NULL) {
116                 fprintf(stderr, "swap buffers malloc failed\n");
117                 return;
118         }
119
120         pread.handle = sd->handle;
121         pread.pad = 0;
122         pread.offset = 0;
123         pread.size = size;
124         pread.data_ptr = (long) data;
125
126         if (ioctl(lc->gem_fd, DRM_IOCTL_I915_GEM_PREAD, &pread)) {
127                 fprintf(stderr, "gem pread failed");
128                 return;
129         }
130
131         dst = lc->fb + lc->stride * map->y + map->x * 4;
132         for (i = 0; i < sd->height; i++)
133                 memcpy(dst + lc->stride * i, data + sd->stride * i, sd->width * 4);
134
135         free(data);
136 }
137
138 struct wl_compositor_interface interface = {
139         notify_surface_create,
140         notify_surface_destroy,
141         notify_surface_attach,
142         notify_surface_map
143 };
144
145 static const char fb_device[] = "/dev/fb";
146 static const char gem_device[] = "/dev/dri/card0";
147
148 struct wl_compositor *
149 wl_compositor_create(void)
150 {
151         struct lame_compositor *lc;
152         struct fb_fix_screeninfo fix;
153         struct fb_var_screeninfo var;
154         int fd;
155
156         lc = malloc(sizeof *lc);
157         if (lc == NULL)
158                 return NULL;
159
160         lc->base.interface = &interface;
161
162         fd = open(fb_device, O_RDWR);
163         if (fd < 0) {
164                 fprintf(stderr, "open %s failed: %m\n", fb_device);
165                 return NULL;
166         }
167
168         if (ioctl(fd, FBIOGET_FSCREENINFO, &fix) < 0) {
169                 fprintf(stderr, "fb get fixed failed\n");
170                 return NULL;
171         }
172
173         if (ioctl(fd, FBIOGET_VSCREENINFO, &var) < 0) {
174                 fprintf(stderr, "fb get fixed failed\n");
175                 return NULL;
176         }
177
178         lc->stride = fix.line_length;
179         lc->width = var.xres;
180         lc->height = var.yres;
181         lc->fb = mmap(NULL, lc->stride * lc->height,
182                       PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
183         close(fd);
184         if (lc->fb == MAP_FAILED) {
185                 fprintf(stderr, "fb map failed\n");
186                 return NULL;
187         }
188
189         lc->gem_fd = open(gem_device, O_RDWR);
190         if (lc->gem_fd < 0) {
191                 fprintf(stderr, "failed to open drm device\n");
192                 return NULL;
193         }
194
195         return &lc->base;
196 }