14 struct lame_compositor {
15 struct wl_compositor base;
17 int32_t width, height, stride;
23 int32_t width, height, stride;
26 void notify_surface_create(struct wl_compositor *compositor,
27 struct wl_surface *surface)
29 struct surface_data *sd;
31 sd = malloc(sizeof *sd);
36 wl_surface_set_data(surface, sd);
39 void notify_surface_destroy(struct wl_compositor *compositor,
40 struct wl_surface *surface)
42 struct lame_compositor *lc = (struct lame_compositor *) compositor;
43 struct surface_data *sd;
44 struct drm_gem_close close_arg;
47 sd = wl_surface_get_data(surface);
48 if (sd == NULL || sd->handle == 0)
51 close_arg.handle = sd->handle;
52 ret = ioctl(lc->gem_fd, DRM_IOCTL_GEM_CLOSE, &close_arg);
54 fprintf(stderr, "failed to gem_close handle %d: %m\n", sd->handle);
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)
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;
70 sd = wl_surface_get_data(surface);
74 if (sd->handle != 0) {
75 close_arg.handle = sd->handle;
76 ret = ioctl(lc->gem_fd, DRM_IOCTL_GEM_CLOSE, &close_arg);
78 fprintf(stderr, "failed to gem_close name %d: %m\n", name);
83 ret = ioctl(lc->gem_fd, DRM_IOCTL_GEM_OPEN, &open_arg);
85 fprintf(stderr, "failed to gem_open name %d, fd=%d: %m\n", name, lc->gem_fd);
89 sd->handle = open_arg.handle;
95 void notify_surface_map(struct wl_compositor *compositor,
96 struct wl_surface *surface, struct wl_map *map)
98 struct lame_compositor *lc = (struct lame_compositor *) compositor;
99 struct surface_data *sd;
100 struct drm_i915_gem_pread pread;
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
109 sd = wl_surface_get_data(surface);
113 size = sd->height * sd->stride;
116 fprintf(stderr, "swap buffers malloc failed\n");
120 pread.handle = sd->handle;
124 pread.data_ptr = (long) data;
126 if (ioctl(lc->gem_fd, DRM_IOCTL_I915_GEM_PREAD, &pread)) {
127 fprintf(stderr, "gem pread failed");
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);
138 struct wl_compositor_interface interface = {
139 notify_surface_create,
140 notify_surface_destroy,
141 notify_surface_attach,
145 static const char fb_device[] = "/dev/fb";
146 static const char gem_device[] = "/dev/dri/card0";
148 struct wl_compositor *
149 wl_compositor_create(void)
151 struct lame_compositor *lc;
152 struct fb_fix_screeninfo fix;
153 struct fb_var_screeninfo var;
156 lc = malloc(sizeof *lc);
160 lc->base.interface = &interface;
162 fd = open(fb_device, O_RDWR);
164 fprintf(stderr, "open %s failed: %m\n", fb_device);
168 if (ioctl(fd, FBIOGET_FSCREENINFO, &fix) < 0) {
169 fprintf(stderr, "fb get fixed failed\n");
173 if (ioctl(fd, FBIOGET_VSCREENINFO, &var) < 0) {
174 fprintf(stderr, "fb get fixed failed\n");
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);
184 if (lc->fb == MAP_FAILED) {
185 fprintf(stderr, "fb map failed\n");
189 lc->gem_fd = open(gem_device, O_RDWR);
190 if (lc->gem_fd < 0) {
191 fprintf(stderr, "failed to open drm device\n");