Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 1 | #include <stdint.h> |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 2 | #include <stdio.h> |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 3 | #include <stdlib.h> |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 4 | #include <string.h> |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 5 | #include <i915_drm.h> |
| 6 | #include <sys/ioctl.h> |
Kristian Høgsberg | 427524a | 2008-10-08 13:32:07 -0400 | [diff] [blame] | 7 | #include <sys/poll.h> |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 8 | #include <fcntl.h> |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 9 | #include <unistd.h> |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 10 | #include <math.h> |
| 11 | #include <cairo.h> |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 12 | |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 13 | #include "wayland-client.h" |
| 14 | |
| 15 | static const char gem_device[] = "/dev/dri/card0"; |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 16 | static const char socket_name[] = "\0wayland"; |
| 17 | |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 18 | static uint32_t name_cairo_surface(int fd, cairo_surface_t *surface) |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 19 | { |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 20 | struct drm_i915_gem_create create; |
| 21 | struct drm_gem_flink flink; |
| 22 | struct drm_i915_gem_pwrite pwrite; |
| 23 | int32_t width, height, stride; |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 24 | |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 25 | width = cairo_image_surface_get_width(surface); |
| 26 | height = cairo_image_surface_get_height(surface); |
| 27 | stride = cairo_image_surface_get_stride(surface); |
| 28 | |
| 29 | memset(&create, 0, sizeof(create)); |
| 30 | create.size = height * stride; |
| 31 | |
| 32 | if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) { |
| 33 | fprintf(stderr, "gem create failed: %m\n"); |
| 34 | return 0; |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 37 | pwrite.handle = create.handle; |
| 38 | pwrite.offset = 0; |
| 39 | pwrite.size = height * stride; |
| 40 | pwrite.data_ptr = (uint64_t) (uintptr_t) |
| 41 | cairo_image_surface_get_data(surface); |
| 42 | if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) { |
| 43 | fprintf(stderr, "gem pwrite failed: %m\n"); |
| 44 | return 0; |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 45 | } |
| 46 | |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 47 | flink.handle = create.handle; |
| 48 | if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) { |
| 49 | fprintf(stderr, "gem flink failed: %m\n"); |
| 50 | return 0; |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 51 | } |
| 52 | |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 53 | #if 0 |
| 54 | /* We need to hold on to the handle until the server has received |
| 55 | * the attach request... we probably need a confirmation event. |
| 56 | * I guess the breadcrumb idea will suffice. */ |
| 57 | struct drm_gem_close close; |
| 58 | close.handle = create.handle; |
| 59 | if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) { |
| 60 | fprintf(stderr, "gem close failed: %m\n"); |
| 61 | return 0; |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 62 | } |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 63 | #endif |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 64 | |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 65 | return flink.name; |
| 66 | } |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 67 | |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 68 | static void * |
| 69 | draw_stuff(int width, int height) |
| 70 | { |
| 71 | cairo_surface_t *surface; |
| 72 | cairo_t *cr; |
| 73 | |
| 74 | surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, |
| 75 | width, height); |
| 76 | |
| 77 | cr = cairo_create(surface); |
| 78 | |
| 79 | cairo_arc(cr, width / 2, height / 2, width / 2 - 10, 0, 2 * M_PI); |
| 80 | cairo_set_source_rgb(cr, 1, 0, 0); |
| 81 | cairo_fill_preserve(cr); |
| 82 | cairo_set_source_rgb(cr, 1, 1, 0); |
| 83 | cairo_stroke(cr); |
| 84 | |
| 85 | cairo_arc(cr, width / 2, height / 2, width / 4 - 10, 0, 2 * M_PI); |
| 86 | cairo_set_source_rgb(cr, 0, 0, 1); |
| 87 | cairo_fill_preserve(cr); |
| 88 | cairo_set_source_rgb(cr, 1, 1, 0); |
| 89 | cairo_stroke(cr); |
| 90 | |
| 91 | cairo_destroy(cr); |
| 92 | |
| 93 | return surface; |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Kristian Høgsberg | 427524a | 2008-10-08 13:32:07 -0400 | [diff] [blame] | 96 | static int |
| 97 | connection_update(struct wl_connection *connection, |
| 98 | uint32_t mask, void *data) |
| 99 | { |
| 100 | struct pollfd *p = data; |
| 101 | |
| 102 | p->events = 0; |
| 103 | if (mask & WL_CONNECTION_READABLE) |
| 104 | p->events |= POLLIN; |
| 105 | if (mask & WL_CONNECTION_WRITABLE) |
| 106 | p->events |= POLLOUT; |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 111 | int main(int argc, char *argv[]) |
| 112 | { |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 113 | struct wl_display *display; |
| 114 | struct wl_surface *surface; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame^] | 115 | const int x = 200, y = 100, width = 300, height = 300; |
Kristian Høgsberg | 427524a | 2008-10-08 13:32:07 -0400 | [diff] [blame] | 116 | int fd, i, ret; |
| 117 | uint32_t name, mask; |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 118 | cairo_surface_t *s; |
Kristian Høgsberg | 427524a | 2008-10-08 13:32:07 -0400 | [diff] [blame] | 119 | struct pollfd p[1]; |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 120 | |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 121 | fd = open(gem_device, O_RDWR); |
| 122 | if (fd < 0) { |
| 123 | fprintf(stderr, "drm open failed: %m\n"); |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 124 | return -1; |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 125 | } |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 126 | |
Kristian Høgsberg | 427524a | 2008-10-08 13:32:07 -0400 | [diff] [blame] | 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"); |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 131 | return -1; |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 132 | } |
Kristian Høgsberg | 427524a | 2008-10-08 13:32:07 -0400 | [diff] [blame] | 133 | p[0].fd = wl_display_get_fd(display); |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 134 | |
| 135 | surface = wl_display_create_surface(display); |
| 136 | |
| 137 | s = draw_stuff(width, height); |
| 138 | name = name_cairo_surface(fd, s); |
| 139 | |
| 140 | wl_surface_attach(surface, name, width, height, |
| 141 | cairo_image_surface_get_stride(s)); |
| 142 | |
Kristian Høgsberg | 427524a | 2008-10-08 13:32:07 -0400 | [diff] [blame] | 143 | i = 0; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame^] | 144 | while (ret = poll(p, 1, 20), ret >= 0) { |
Kristian Høgsberg | 427524a | 2008-10-08 13:32:07 -0400 | [diff] [blame] | 145 | if (ret == 0) { |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame^] | 146 | wl_surface_map(surface, |
| 147 | x + cos(i / 10.0) * 50, |
| 148 | y + sin(i / 10.0) * 50, |
| 149 | width, height); |
Kristian Høgsberg | 427524a | 2008-10-08 13:32:07 -0400 | [diff] [blame] | 150 | i++; |
| 151 | continue; |
| 152 | } |
Kristian Høgsberg | a67a71a | 2008-10-07 10:10:36 -0400 | [diff] [blame] | 153 | |
Kristian Høgsberg | 427524a | 2008-10-08 13:32:07 -0400 | [diff] [blame] | 154 | mask = 0; |
| 155 | if (p[0].revents & POLLIN) |
| 156 | mask |= WL_CONNECTION_READABLE; |
| 157 | if (p[0].revents & POLLOUT) |
| 158 | mask |= WL_CONNECTION_WRITABLE; |
| 159 | if (mask) |
| 160 | wl_display_iterate(display, mask); |
| 161 | } |
Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame] | 162 | |
| 163 | return 0; |
| 164 | } |