Switch to cairo-drm, drop struct buffer hacks.
[profile/ivi/wayland.git] / flower.c
1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <i915_drm.h>
28 #include <sys/ioctl.h>
29 #include <time.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <math.h>
33 #include <time.h>
34 #include <cairo.h>
35 #include <glib.h>
36 #include <cairo-drm.h>
37
38 #include "wayland-client.h"
39 #include "wayland-glib.h"
40
41 static const char gem_device[] = "/dev/dri/card0";
42 static const char socket_name[] = "\0wayland";
43
44 static void
45 set_random_color(cairo_t *cr)
46 {
47         cairo_set_source_rgba(cr,
48                               0.5 + (random() % 50) / 49.0,
49                               0.5 + (random() % 50) / 49.0,
50                               0.5 + (random() % 50) / 49.0,
51                               0.5 + (random() % 100) / 99.0);
52 }
53
54
55 static void
56 draw_stuff(cairo_surface_t *surface, int width, int height)
57 {
58         const int petal_count = 3 + random() % 5;
59         const double r1 = 60 + random() % 35;
60         const double r2 = 20 + random() % 40;
61         const double u = (10 + random() % 90) / 100.0;
62         const double v = (random() % 90) / 100.0;
63
64         cairo_t *cr;
65         int i;
66         double t, dt = 2 * M_PI / (petal_count * 2);
67         double x1, y1, x2, y2, x3, y3;
68
69         cr = cairo_create(surface);
70         cairo_translate(cr, width / 2, height / 2);
71         cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
72         for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
73                 x1 = cos(t) * r1;
74                 y1 = sin(t) * r1;
75                 x2 = cos(t + dt) * r2;
76                 y2 = sin(t + dt) * r2;
77                 x3 = cos(t + 2 * dt) * r1;
78                 y3 = sin(t + 2 * dt) * r1;
79
80                 cairo_curve_to(cr,
81                                x1 - y1 * u, y1 + x1 * u,
82                                x2 + y2 * v, y2 - x2 * v,
83                                x2, y2);                        
84
85                 cairo_curve_to(cr,
86                                x2 - y2 * v, y2 + x2 * v,
87                                x3 + y3 * u, y3 - x3 * u,
88                                x3, y3);
89         }
90
91         cairo_close_path(cr);
92         set_random_color(cr);
93         cairo_fill_preserve(cr);
94         set_random_color(cr);
95         cairo_stroke(cr);
96
97         cairo_destroy(cr);
98 }
99
100 struct flower {
101         struct wl_compositor *compositor;
102         struct wl_surface *surface;
103         int x, y, width, height;
104         int offset;
105 };
106
107 static void
108 handle_acknowledge(void *data,
109                           struct wl_compositor *compositor,
110                           uint32_t key, uint32_t frame)
111 {
112 }
113
114 static void
115 handle_frame(void *data,
116                     struct wl_compositor *compositor,
117                     uint32_t frame, uint32_t timestamp)
118 {
119         struct flower *flower = data;
120
121         wl_surface_map(flower->surface, 
122                        flower->x + cos((flower->offset + timestamp) / 400.0) * 400 - flower->width / 2,
123                        flower->y + sin((flower->offset + timestamp) / 320.0) * 300 - flower->height / 2,
124                        flower->width, flower->height);
125         wl_compositor_commit(flower->compositor, 0);
126 }
127
128 static const struct wl_compositor_listener compositor_listener = {
129         handle_acknowledge,
130         handle_frame,
131 };
132
133 int main(int argc, char *argv[])
134 {
135         struct wl_display *display;
136         struct wl_visual *visual;
137         int fd;
138         cairo_drm_context_t *ctx;
139         cairo_surface_t *s;
140         struct timespec ts;
141         GMainLoop *loop;
142         GSource *source;
143         struct flower flower;
144
145         fd = open(gem_device, O_RDWR);
146         if (fd < 0) {
147                 fprintf(stderr, "drm open failed: %m\n");
148                 return -1;
149         }
150
151         loop = g_main_loop_new(NULL, FALSE);
152
153         display = wl_display_create(socket_name, sizeof socket_name);
154         if (display == NULL) {
155                 fprintf(stderr, "failed to create display: %m\n");
156                 return -1;
157         }
158
159         source = wl_glib_source_new(display);
160         g_source_attach(source, NULL);
161
162         flower.compositor = wl_display_get_compositor(display);
163         flower.x = 512;
164         flower.y = 384;
165         flower.width = 200;
166         flower.height = 200;
167         flower.surface = wl_compositor_create_surface(flower.compositor);
168
169         clock_gettime(CLOCK_MONOTONIC, &ts);
170         srandom(ts.tv_nsec);
171         flower.offset = random();
172
173         ctx = cairo_drm_context_get_for_fd(fd);
174         s = cairo_drm_surface_create(ctx, CAIRO_CONTENT_COLOR_ALPHA,
175                                      flower.width, flower.height);
176         draw_stuff(s, flower.width, flower.height);
177
178         visual = wl_display_get_premultiplied_argb_visual(display);
179         wl_surface_attach(flower.surface,
180                           cairo_drm_surface_get_name(s),
181                           flower.width, flower.height,
182                           cairo_drm_surface_get_stride(s),
183                           visual);
184
185         wl_compositor_add_listener(flower.compositor,
186                                    &compositor_listener, &flower);
187
188         wl_compositor_commit(flower.compositor, 0);
189
190         g_main_loop_run(loop);
191
192         return 0;
193 }