2008-09-30 17:46:10 +04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2008-10-07 18:10:36 +04:00
|
|
|
#include <stdlib.h>
|
2008-09-30 17:46:10 +04:00
|
|
|
#include <string.h>
|
2008-10-07 18:10:36 +04:00
|
|
|
#include <i915_drm.h>
|
|
|
|
#include <sys/ioctl.h>
|
2008-11-02 23:28:22 +03:00
|
|
|
#include <time.h>
|
2008-10-07 18:10:36 +04:00
|
|
|
#include <fcntl.h>
|
2008-09-30 17:46:10 +04:00
|
|
|
#include <unistd.h>
|
2008-10-07 18:10:36 +04:00
|
|
|
#include <math.h>
|
2008-10-12 06:13:51 +04:00
|
|
|
#include <time.h>
|
2008-10-07 18:10:36 +04:00
|
|
|
#include <cairo.h>
|
2008-11-07 22:27:23 +03:00
|
|
|
#include <glib.h>
|
2008-10-07 18:10:36 +04:00
|
|
|
|
|
|
|
#include "wayland-client.h"
|
2008-11-07 22:27:23 +03:00
|
|
|
#include "wayland-glib.h"
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2008-10-07 18:10:36 +04:00
|
|
|
static const char gem_device[] = "/dev/dri/card0";
|
2008-09-30 17:46:10 +04:00
|
|
|
static const char socket_name[] = "\0wayland";
|
|
|
|
|
2008-10-07 18:10:36 +04:00
|
|
|
static uint32_t name_cairo_surface(int fd, cairo_surface_t *surface)
|
2008-09-30 17:46:10 +04:00
|
|
|
{
|
2008-10-07 18:10:36 +04:00
|
|
|
struct drm_i915_gem_create create;
|
|
|
|
struct drm_gem_flink flink;
|
|
|
|
struct drm_i915_gem_pwrite pwrite;
|
|
|
|
int32_t width, height, stride;
|
2008-11-02 23:50:55 +03:00
|
|
|
void *data;
|
2008-10-07 18:10:36 +04:00
|
|
|
|
|
|
|
width = cairo_image_surface_get_width(surface);
|
|
|
|
height = cairo_image_surface_get_height(surface);
|
|
|
|
stride = cairo_image_surface_get_stride(surface);
|
2008-11-02 23:50:55 +03:00
|
|
|
data = cairo_image_surface_get_data(surface);
|
|
|
|
|
2008-10-07 18:10:36 +04:00
|
|
|
memset(&create, 0, sizeof(create));
|
|
|
|
create.size = height * stride;
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2008-10-07 18:10:36 +04:00
|
|
|
if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
|
|
|
|
fprintf(stderr, "gem create failed: %m\n");
|
|
|
|
return 0;
|
2008-09-30 17:46:10 +04:00
|
|
|
}
|
2008-10-07 18:10:36 +04:00
|
|
|
|
|
|
|
pwrite.handle = create.handle;
|
|
|
|
pwrite.offset = 0;
|
|
|
|
pwrite.size = height * stride;
|
2008-11-02 23:50:55 +03:00
|
|
|
pwrite.data_ptr = (uint64_t) (uintptr_t) data;
|
2008-10-07 18:10:36 +04:00
|
|
|
if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) {
|
|
|
|
fprintf(stderr, "gem pwrite failed: %m\n");
|
|
|
|
return 0;
|
2008-09-30 17:46:10 +04:00
|
|
|
}
|
|
|
|
|
2008-10-07 18:10:36 +04:00
|
|
|
flink.handle = create.handle;
|
|
|
|
if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
|
|
|
|
fprintf(stderr, "gem flink failed: %m\n");
|
|
|
|
return 0;
|
2008-09-30 17:46:10 +04:00
|
|
|
}
|
|
|
|
|
2008-10-07 18:10:36 +04:00
|
|
|
#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;
|
2008-09-30 17:46:10 +04:00
|
|
|
}
|
2008-10-07 18:10:36 +04:00
|
|
|
#endif
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2008-10-07 18:10:36 +04:00
|
|
|
return flink.name;
|
|
|
|
}
|
|
|
|
|
2008-10-12 06:13:51 +04:00
|
|
|
static void
|
|
|
|
set_random_color(cairo_t *cr)
|
|
|
|
{
|
|
|
|
cairo_set_source_rgba(cr,
|
2008-10-13 06:58:40 +04:00
|
|
|
0.5 + (random() % 50) / 49.0,
|
|
|
|
0.5 + (random() % 50) / 49.0,
|
|
|
|
0.5 + (random() % 50) / 49.0,
|
|
|
|
0.5 + (random() % 100) / 99.0);
|
2008-10-12 06:13:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-07 18:10:36 +04:00
|
|
|
static void *
|
|
|
|
draw_stuff(int width, int height)
|
|
|
|
{
|
2008-10-12 06:13:51 +04:00
|
|
|
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;
|
|
|
|
|
2008-10-07 18:10:36 +04:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_t *cr;
|
2008-10-12 06:13:51 +04:00
|
|
|
int i;
|
|
|
|
double t, dt = 2 * M_PI / (petal_count * 2);
|
|
|
|
double x1, y1, x2, y2, x3, y3;
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2008-10-07 18:10:36 +04:00
|
|
|
surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
|
|
|
|
width, height);
|
|
|
|
|
|
|
|
cr = cairo_create(surface);
|
2008-10-12 06:13:51 +04:00
|
|
|
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);
|
|
|
|
}
|
2008-10-07 18:10:36 +04:00
|
|
|
|
2008-10-12 06:13:51 +04:00
|
|
|
cairo_close_path(cr);
|
|
|
|
set_random_color(cr);
|
2008-10-07 18:10:36 +04:00
|
|
|
cairo_fill_preserve(cr);
|
2008-10-12 06:13:51 +04:00
|
|
|
set_random_color(cr);
|
2008-10-07 18:10:36 +04:00
|
|
|
cairo_stroke(cr);
|
|
|
|
|
|
|
|
cairo_destroy(cr);
|
|
|
|
|
|
|
|
return surface;
|
2008-09-30 17:46:10 +04:00
|
|
|
}
|
|
|
|
|
2008-11-07 22:27:23 +03:00
|
|
|
struct flower {
|
|
|
|
struct wl_surface *surface;
|
|
|
|
int i;
|
|
|
|
int x, y, width, height;
|
|
|
|
};
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
move_flower(gpointer data)
|
2008-10-08 21:32:07 +04:00
|
|
|
{
|
2008-11-07 22:27:23 +03:00
|
|
|
struct flower *flower = data;
|
2008-10-08 21:32:07 +04:00
|
|
|
|
2008-11-07 22:27:23 +03:00
|
|
|
wl_surface_map(flower->surface,
|
|
|
|
flower->x + cos(flower->i / 31.0) * 400 - flower->width / 2,
|
|
|
|
flower->y + sin(flower->i / 27.0) * 300 - flower->height / 2,
|
|
|
|
flower->width, flower->height);
|
|
|
|
flower->i++;
|
2008-10-08 21:32:07 +04:00
|
|
|
|
2008-11-07 22:27:23 +03:00
|
|
|
return TRUE;
|
2008-10-08 21:32:07 +04:00
|
|
|
}
|
|
|
|
|
2008-09-30 17:46:10 +04:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2008-10-07 18:10:36 +04:00
|
|
|
struct wl_display *display;
|
2008-11-07 22:27:23 +03:00
|
|
|
int fd;
|
|
|
|
uint32_t name;
|
2008-10-07 18:10:36 +04:00
|
|
|
cairo_surface_t *s;
|
2008-11-02 23:28:22 +03:00
|
|
|
struct timespec ts;
|
2008-11-07 22:27:23 +03:00
|
|
|
GMainLoop *loop;
|
|
|
|
GSource *source;
|
|
|
|
struct flower flower;
|
2008-10-12 06:13:51 +04:00
|
|
|
|
2008-10-07 18:10:36 +04:00
|
|
|
fd = open(gem_device, O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "drm open failed: %m\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2008-11-07 22:27:23 +03:00
|
|
|
loop = g_main_loop_new(NULL, FALSE);
|
|
|
|
|
|
|
|
display = wl_display_create(socket_name);
|
2008-10-08 21:32:07 +04:00
|
|
|
if (display == NULL) {
|
|
|
|
fprintf(stderr, "failed to create display: %m\n");
|
2008-09-30 17:46:10 +04:00
|
|
|
return -1;
|
2008-10-07 18:10:36 +04:00
|
|
|
}
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2008-11-07 22:27:23 +03:00
|
|
|
source = wayland_source_new(display);
|
|
|
|
g_source_attach(source, NULL);
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2008-11-07 22:27:23 +03:00
|
|
|
flower.x = 512;
|
|
|
|
flower.y = 384;
|
|
|
|
flower.width = 200;
|
|
|
|
flower.height = 200;
|
|
|
|
flower.surface = wl_display_create_surface(display);
|
2008-11-02 23:28:22 +03:00
|
|
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
srandom(ts.tv_nsec);
|
2008-11-07 22:27:23 +03:00
|
|
|
flower.i = ts.tv_nsec;
|
2008-11-02 23:28:22 +03:00
|
|
|
|
2008-11-07 22:27:23 +03:00
|
|
|
s = draw_stuff(flower.width, flower.height);
|
2008-10-07 18:10:36 +04:00
|
|
|
name = name_cairo_surface(fd, s);
|
2008-09-30 17:46:10 +04:00
|
|
|
|
2008-11-07 22:27:23 +03:00
|
|
|
wl_surface_attach(flower.surface, name, flower.width, flower.height,
|
2008-10-07 18:10:36 +04:00
|
|
|
cairo_image_surface_get_stride(s));
|
|
|
|
|
2008-11-07 22:27:23 +03:00
|
|
|
g_timeout_add(20, move_flower, &flower);
|
|
|
|
|
|
|
|
g_main_loop_run(loop);
|
2008-09-30 17:46:10 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|