You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
202 lines
4.5 KiB
202 lines
4.5 KiB
16 years ago
|
#include <stdint.h>
|
||
|
#include <stdio.h>
|
||
16 years ago
|
#include <stdlib.h>
|
||
16 years ago
|
#include <string.h>
|
||
16 years ago
|
#include <i915_drm.h>
|
||
|
#include <sys/ioctl.h>
|
||
16 years ago
|
#include <sys/poll.h>
|
||
16 years ago
|
#include <fcntl.h>
|
||
16 years ago
|
#include <unistd.h>
|
||
16 years ago
|
#include <math.h>
|
||
16 years ago
|
#include <time.h>
|
||
16 years ago
|
#include <cairo.h>
|
||
|
|
||
|
#include "wayland-client.h"
|
||
16 years ago
|
|
||
16 years ago
|
static const char gem_device[] = "/dev/dri/card0";
|
||
16 years ago
|
static const char socket_name[] = "\0wayland";
|
||
|
|
||
16 years ago
|
static uint32_t name_cairo_surface(int fd, cairo_surface_t *surface)
|
||
16 years ago
|
{
|
||
16 years ago
|
struct drm_i915_gem_create create;
|
||
|
struct drm_gem_flink flink;
|
||
|
struct drm_i915_gem_pwrite pwrite;
|
||
|
int32_t width, height, stride;
|
||
|
|
||
|
width = cairo_image_surface_get_width(surface);
|
||
|
height = cairo_image_surface_get_height(surface);
|
||
|
stride = cairo_image_surface_get_stride(surface);
|
||
16 years ago
|
|
||
16 years ago
|
memset(&create, 0, sizeof(create));
|
||
|
create.size = height * stride;
|
||
16 years ago
|
|
||
16 years ago
|
if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
|
||
|
fprintf(stderr, "gem create failed: %m\n");
|
||
|
return 0;
|
||
16 years ago
|
}
|
||
16 years ago
|
|
||
|
pwrite.handle = create.handle;
|
||
|
pwrite.offset = 0;
|
||
|
pwrite.size = height * stride;
|
||
|
pwrite.data_ptr = (uint64_t) (uintptr_t)
|
||
|
cairo_image_surface_get_data(surface);
|
||
|
if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) {
|
||
|
fprintf(stderr, "gem pwrite failed: %m\n");
|
||
|
return 0;
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
flink.handle = create.handle;
|
||
|
if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
|
||
|
fprintf(stderr, "gem flink failed: %m\n");
|
||
|
return 0;
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
#if 0
|
||
|
/* We need to hold on to the handle until the server has received
|
||
|
* the attach request... we probably need a confirmation event.
|
||
|
* I guess the breadcrumb idea will suffice. */
|
||
|
struct drm_gem_close close;
|
||
|
close.handle = create.handle;
|
||
|
if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) {
|
||
|
fprintf(stderr, "gem close failed: %m\n");
|
||
|
return 0;
|
||
16 years ago
|
}
|
||
16 years ago
|
#endif
|
||
16 years ago
|
|
||
16 years ago
|
return flink.name;
|
||
|
}
|
||
|
|
||
16 years ago
|
static void
|
||
|
set_random_color(cairo_t *cr)
|
||
|
{
|
||
|
cairo_set_source_rgba(cr,
|
||
16 years ago
|
0.5 + (random() % 50) / 49.0,
|
||
|
0.5 + (random() % 50) / 49.0,
|
||
|
0.5 + (random() % 50) / 49.0,
|
||
|
0.5 + (random() % 100) / 99.0);
|
||
16 years ago
|
}
|
||
|
|
||
|
|
||
16 years ago
|
static void *
|
||
|
draw_stuff(int width, int height)
|
||
|
{
|
||
16 years ago
|
const int petal_count = 3 + random() % 5;
|
||
|
const double r1 = 60 + random() % 35;
|
||
|
const double r2 = 20 + random() % 40;
|
||
|
const double u = (10 + random() % 90) / 100.0;
|
||
|
const double v = (random() % 90) / 100.0;
|
||
|
|
||
16 years ago
|
cairo_surface_t *surface;
|
||
|
cairo_t *cr;
|
||
16 years ago
|
int i;
|
||
|
double t, dt = 2 * M_PI / (petal_count * 2);
|
||
|
double x1, y1, x2, y2, x3, y3;
|
||
16 years ago
|
|
||
16 years ago
|
surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
|
||
|
width, height);
|
||
|
|
||
|
cr = cairo_create(surface);
|
||
16 years ago
|
cairo_translate(cr, width / 2, height / 2);
|
||
|
cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
|
||
|
for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
|
||
|
x1 = cos(t) * r1;
|
||
|
y1 = sin(t) * r1;
|
||
|
x2 = cos(t + dt) * r2;
|
||
|
y2 = sin(t + dt) * r2;
|
||
|
x3 = cos(t + 2 * dt) * r1;
|
||
|
y3 = sin(t + 2 * dt) * r1;
|
||
|
|
||
|
cairo_curve_to(cr,
|
||
|
x1 - y1 * u, y1 + x1 * u,
|
||
|
x2 + y2 * v, y2 - x2 * v,
|
||
|
x2, y2);
|
||
|
|
||
|
cairo_curve_to(cr,
|
||
|
x2 - y2 * v, y2 + x2 * v,
|
||
|
x3 + y3 * u, y3 - x3 * u,
|
||
|
x3, y3);
|
||
|
}
|
||
16 years ago
|
|
||
16 years ago
|
cairo_close_path(cr);
|
||
|
set_random_color(cr);
|
||
16 years ago
|
cairo_fill_preserve(cr);
|
||
16 years ago
|
set_random_color(cr);
|
||
16 years ago
|
cairo_stroke(cr);
|
||
|
|
||
|
cairo_destroy(cr);
|
||
|
|
||
|
return surface;
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
static int
|
||
|
connection_update(struct wl_connection *connection,
|
||
|
uint32_t mask, void *data)
|
||
|
{
|
||
|
struct pollfd *p = data;
|
||
|
|
||
|
p->events = 0;
|
||
|
if (mask & WL_CONNECTION_READABLE)
|
||
|
p->events |= POLLIN;
|
||
|
if (mask & WL_CONNECTION_WRITABLE)
|
||
|
p->events |= POLLOUT;
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
16 years ago
|
int main(int argc, char *argv[])
|
||
|
{
|
||
16 years ago
|
struct wl_display *display;
|
||
|
struct wl_surface *surface;
|
||
16 years ago
|
const int x = 200, y = 200, width = 200, height = 200;
|
||
16 years ago
|
int fd, i, ret;
|
||
|
uint32_t name, mask;
|
||
16 years ago
|
cairo_surface_t *s;
|
||
16 years ago
|
struct pollfd p[1];
|
||
16 years ago
|
|
||
16 years ago
|
srandom(time(NULL));
|
||
|
|
||
16 years ago
|
fd = open(gem_device, O_RDWR);
|
||
|
if (fd < 0) {
|
||
|
fprintf(stderr, "drm open failed: %m\n");
|
||
|
return -1;
|
||
|
}
|
||
16 years ago
|
|
||
16 years ago
|
display = wl_display_create(socket_name,
|
||
|
connection_update, &p[0]);
|
||
|
if (display == NULL) {
|
||
|
fprintf(stderr, "failed to create display: %m\n");
|
||
16 years ago
|
return -1;
|
||
16 years ago
|
}
|
||
16 years ago
|
p[0].fd = wl_display_get_fd(display);
|
||
16 years ago
|
|
||
16 years ago
|
surface = wl_display_create_surface(display);
|
||
16 years ago
|
|
||
16 years ago
|
s = draw_stuff(width, height);
|
||
|
name = name_cairo_surface(fd, s);
|
||
16 years ago
|
|
||
16 years ago
|
wl_surface_attach(surface, name, width, height,
|
||
|
cairo_image_surface_get_stride(s));
|
||
|
|
||
16 years ago
|
i = 0;
|
||
16 years ago
|
while (ret = poll(p, 1, 20), ret >= 0) {
|
||
16 years ago
|
if (ret == 0) {
|
||
16 years ago
|
wl_surface_map(surface,
|
||
16 years ago
|
x + cos(i / 30.0) * 200,
|
||
|
y + sin(i / 31.0) * 200,
|
||
16 years ago
|
width, height);
|
||
16 years ago
|
i++;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
mask = 0;
|
||
|
if (p[0].revents & POLLIN)
|
||
|
mask |= WL_CONNECTION_READABLE;
|
||
|
if (p[0].revents & POLLOUT)
|
||
|
mask |= WL_CONNECTION_WRITABLE;
|
||
|
if (mask)
|
||
|
wl_display_iterate(display, mask);
|
||
16 years ago
|
}
|
||
16 years ago
|
|
||
|
return 0;
|
||
|
}
|