8 #include <gdk-pixbuf/gdk-pixbuf.h>
10 #include "wayland-client.h"
12 static const char gem_device[] = "/dev/dri/card0";
13 static const char socket_name[] = "\0wayland";
15 static uint8_t *convert_to_argb(GdkPixbuf *pixbuf, int *new_stride)
17 uint8_t *data, *row, *src;
19 int width, height, stride, channels, i, j;
21 width = gdk_pixbuf_get_width(pixbuf);
22 height = gdk_pixbuf_get_height(pixbuf);
23 stride = gdk_pixbuf_get_rowstride(pixbuf);
24 row = gdk_pixbuf_get_pixels(pixbuf);
25 channels = gdk_pixbuf_get_n_channels(pixbuf);
27 data = malloc(height * width * 4);
29 fprintf(stderr, "out of memory\n");
33 for (i = 0; i < height; i++) {
34 for (j = 0; j < width; j++) {
35 src = &row[i * stride + j * channels];
36 pixel = (uint32_t *) &data[i * width * 4 + j * 4];
37 *pixel = 0xff000000 | (src[0] << 16) | (src[1] << 8) | src[2];
41 *new_stride = width * 4;
46 static uint32_t name_pixbuf(int fd, GdkPixbuf *pixbuf)
48 struct drm_i915_gem_create create;
49 struct drm_gem_flink flink;
50 struct drm_i915_gem_pwrite pwrite;
51 int32_t width, height, stride;
54 width = gdk_pixbuf_get_width(pixbuf);
55 height = gdk_pixbuf_get_height(pixbuf);
56 data = convert_to_argb(pixbuf, &stride);
58 memset(&create, 0, sizeof(create));
59 create.size = height * stride;
61 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
62 fprintf(stderr, "gem create failed: %m\n");
66 pwrite.handle = create.handle;
68 pwrite.size = height * stride;
69 pwrite.data_ptr = (uint64_t) (uintptr_t) data;
70 if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) {
71 fprintf(stderr, "gem pwrite failed: %m\n");
75 flink.handle = create.handle;
76 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
77 fprintf(stderr, "gem flink failed: %m\n");
82 /* We need to hold on to the handle until the server has received
83 * the attach request... we probably need a confirmation event.
84 * I guess the breadcrumb idea will suffice. */
85 struct drm_gem_close close;
86 close.handle = create.handle;
87 if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) {
88 fprintf(stderr, "gem close failed: %m\n");
97 connection_update(struct wl_connection *connection,
98 uint32_t mask, void *data)
100 struct pollfd *p = data;
103 if (mask & WL_CONNECTION_READABLE)
105 if (mask & WL_CONNECTION_WRITABLE)
106 p->events |= POLLOUT;
111 int main(int argc, char *argv[])
114 GError *error = NULL;
115 struct wl_display *display;
116 struct wl_surface *surface;
117 int fd, width, height, stride;
121 fd = open(gem_device, O_RDWR);
123 fprintf(stderr, "drm open failed: %m\n");
127 display = wl_display_create(socket_name,
128 connection_update, &p[0]);
129 if (display == NULL) {
130 fprintf(stderr, "failed to create display: %m\n");
133 p[0].fd = wl_display_get_fd(display);
135 surface = wl_display_create_surface(display);
138 image = gdk_pixbuf_new_from_file (argv[1], &error);
140 name = name_pixbuf(fd, image);
142 width = gdk_pixbuf_get_width(image);
143 height = gdk_pixbuf_get_height(image);
144 stride = gdk_pixbuf_get_rowstride(image);
146 printf("width %d, height %d\n", width, height);
148 wl_surface_attach(surface, name, width, height, width * 4);
150 wl_surface_map(surface, 0, 0, width, height);
155 if (p[0].revents & POLLIN)
156 mask |= WL_CONNECTION_READABLE;
157 if (p[0].revents & POLLOUT)
158 mask |= WL_CONNECTION_WRITABLE;
159 wl_display_iterate(display, mask);