nested: Remove the surface from the surface list when destroyed
[profile/ivi/weston-ivi-shell.git] / clients / smoke.c
1 /*
2  * Copyright © 2010 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 <time.h>
28 #include <math.h>
29 #include <cairo.h>
30
31 #include <wayland-client.h>
32 #include "window.h"
33
34 struct smoke {
35         struct display *display;
36         struct window *window;
37         struct widget *widget;
38         int width, height;
39         int current;
40         uint32_t time;
41         struct { float *d, *u, *v; } b[2];
42 };
43
44 static void diffuse(struct smoke *smoke, uint32_t time,
45                     float *source, float *dest)
46 {
47         float *s, *d;
48         int x, y, k, stride;
49         float t, a = 0.0002;
50
51         stride = smoke->width;
52
53         for (k = 0; k < 5; k++) {
54                 for (y = 1; y < smoke->height - 1; y++) {
55                         s = source + y * stride;
56                         d = dest + y * stride;
57                         for (x = 1; x < smoke->width - 1; x++) {
58                                 t = d[x - 1] + d[x + 1] +
59                                         d[x - stride] + d[x + stride];
60                                 d[x] = (s[x] + a * t) / (1 + 4 * a) * 0.995;
61                         }
62                 }
63         }
64 }
65
66 static void advect(struct smoke *smoke, uint32_t time,
67                    float *uu, float *vv, float *source, float *dest)
68 {
69         float *s, *d;
70         float *u, *v;
71         int x, y, stride;
72         int i, j;
73         float px, py, fx, fy;
74
75         stride = smoke->width;
76
77         for (y = 1; y < smoke->height - 1; y++) {
78                 d = dest + y * stride;
79                 u = uu + y * stride;
80                 v = vv + y * stride;
81
82                 for (x = 1; x < smoke->width - 1; x++) {
83                         px = x - u[x];
84                         py = y - v[x];
85                         if (px < 0.5)
86                                 px = 0.5;
87                         if (py < 0.5)
88                                 py = 0.5;
89                         if (px > smoke->width - 0.5)
90                                 px = smoke->width - 0.5;
91                         if (py > smoke->height - 0.5)
92                                 py = smoke->height - 0.5;
93                         i = (int) px;
94                         j = (int) py;
95                         fx = px - i;
96                         fy = py - j;
97                         s = source + j * stride + i;
98                         d[x] = (s[0] * (1 - fx) + s[1] * fx) * (1 - fy) +
99                                 (s[stride] * (1 - fx) + s[stride + 1] * fx) * fy;
100                 }
101         }
102 }
103
104 static void project(struct smoke *smoke, uint32_t time,
105                     float *u, float *v, float *p, float *div)
106 {
107         int x, y, k, l, s;
108         float h;
109
110         h = 1.0 / smoke->width;
111         s = smoke->width;
112         memset(p, 0, smoke->height * smoke->width);
113         for (y = 1; y < smoke->height - 1; y++) {
114                 l = y * s;
115                 for (x = 1; x < smoke->width - 1; x++) {
116                         div[l + x] = -0.5 * h * (u[l + x + 1] - u[l + x - 1] +
117                                                  v[l + x + s] - v[l + x - s]);
118                         p[l + x] = 0;
119                 }
120         }
121
122         for (k = 0; k < 5; k++) {
123                 for (y = 1; y < smoke->height - 1; y++) {
124                         l = y * s;
125                         for (x = 1; x < smoke->width - 1; x++) {
126                                 p[l + x] = (div[l + x] +
127                                             p[l + x - 1] +
128                                             p[l + x + 1] +
129                                             p[l + x - s] +
130                                             p[l + x + s]) / 4;
131                         }
132                 }
133         }
134
135         for (y = 1; y < smoke->height - 1; y++) {
136                 l = y * s;
137                 for (x = 1; x < smoke->width - 1; x++) {
138                         u[l + x] -= 0.5 * (p[l + x + 1] - p[l + x - 1]) / h;
139                         v[l + x] -= 0.5 * (p[l + x + s] - p[l + x - s]) / h;
140                 }
141         }
142 }
143
144 static void render(struct smoke *smoke, cairo_surface_t *surface)
145 {
146         unsigned char *dest;
147         int x, y, width, height, stride;
148         float *s;
149         uint32_t *d, c, a;
150
151         dest = cairo_image_surface_get_data(surface);
152         width = cairo_image_surface_get_width(surface);
153         height = cairo_image_surface_get_height(surface);
154         stride = cairo_image_surface_get_stride(surface);
155
156         for (y = 1; y < height - 1; y++) {
157                 s = smoke->b[smoke->current].d + y * smoke->height;
158                 d = (uint32_t *) (dest + y * stride);
159                 for (x = 1; x < width - 1; x++) {
160                         c = (int) (s[x] * 800);
161                         if (c > 255)
162                                 c = 255;
163                         a = c;
164                         if (a < 0x33)
165                                 a = 0x33;
166                         d[x] = (a << 24) | (c << 16) | (c << 8) | c;
167                 }
168         }
169 }
170
171 static void
172 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
173 {
174         struct smoke *smoke = data;
175
176         window_schedule_redraw(smoke->window);
177         smoke->time = time;
178
179         if (callback)
180                 wl_callback_destroy(callback);
181 }
182
183 static const struct wl_callback_listener listener = {
184         frame_callback,
185 };
186
187 static void
188 redraw_handler(struct widget *widget, void *data)
189 {
190         struct smoke *smoke = data;
191         uint32_t time = smoke->time;
192         struct wl_callback *callback;
193         cairo_surface_t *surface;
194
195         diffuse(smoke, time / 30, smoke->b[0].u, smoke->b[1].u);
196         diffuse(smoke, time / 30, smoke->b[0].v, smoke->b[1].v);
197         project(smoke, time / 30,
198                 smoke->b[1].u, smoke->b[1].v,
199                 smoke->b[0].u, smoke->b[0].v);
200         advect(smoke, time / 30,
201                smoke->b[1].u, smoke->b[1].v,
202                smoke->b[1].u, smoke->b[0].u);
203         advect(smoke, time / 30,
204                smoke->b[1].u, smoke->b[1].v,
205                smoke->b[1].v, smoke->b[0].v);
206         project(smoke, time / 30,
207                 smoke->b[0].u, smoke->b[0].v,
208                 smoke->b[1].u, smoke->b[1].v);
209
210         diffuse(smoke, time / 30, smoke->b[0].d, smoke->b[1].d);
211         advect(smoke, time / 30,
212                smoke->b[0].u, smoke->b[0].v,
213                smoke->b[1].d, smoke->b[0].d);
214
215         surface = window_get_surface(smoke->window);
216
217         render(smoke, surface);
218
219         window_damage(smoke->window, 0, 0, smoke->width, smoke->height);
220
221         cairo_surface_destroy(surface);
222
223         callback = wl_surface_frame(window_get_wl_surface(smoke->window));
224         wl_callback_add_listener(callback, &listener, smoke);
225         wl_surface_commit(window_get_wl_surface(smoke->window));
226 }
227
228 static void
229 smoke_motion_handler(struct smoke *smoke, float x, float y)
230 {
231         int i, i0, i1, j, j0, j1, k, d = 5;
232
233         if (x - d < 1)
234                 i0 = 1;
235         else
236                 i0 = x - d;
237         if (i0 + 2 * d > smoke->width - 1)
238                 i1 = smoke->width - 1;
239         else
240                 i1 = i0 + 2 * d;
241
242         if (y - d < 1)
243                 j0 = 1;
244         else
245                 j0 = y - d;
246         if (j0 + 2 * d > smoke->height - 1)
247                 j1 = smoke->height - 1;
248         else
249                 j1 = j0 + 2 * d;
250
251         for (i = i0; i < i1; i++)
252                 for (j = j0; j < j1; j++) {
253                         k = j * smoke->width + i;
254                         smoke->b[0].u[k] += 256 - (random() & 512);
255                         smoke->b[0].v[k] += 256 - (random() & 512);
256                         smoke->b[0].d[k] += 1;
257                 }
258 }
259
260 static int
261 mouse_motion_handler(struct widget *widget, struct input *input,
262                      uint32_t time, float x, float y, void *data)
263 {
264         smoke_motion_handler(data, x, y);
265
266         return CURSOR_HAND1;
267 }
268
269 static void
270 touch_motion_handler(struct widget *widget, struct input *input,
271                      uint32_t time, int32_t id, float x, float y, void *data)
272 {
273         smoke_motion_handler(data, x, y);
274 }
275
276 static void
277 resize_handler(struct widget *widget,
278                int32_t width, int32_t height, void *data)
279 {
280         struct smoke *smoke = data;
281
282         /* Dont resize me */
283         widget_set_size(smoke->widget, smoke->width, smoke->height);
284 }
285
286 int main(int argc, char *argv[])
287 {
288         struct timespec ts;
289         struct smoke smoke;
290         struct display *d;
291         int size;
292
293         d = display_create(&argc, argv);
294         if (d == NULL) {
295                 fprintf(stderr, "failed to create display: %m\n");
296                 return -1;
297         }
298
299         smoke.width = 200;
300         smoke.height = 200;
301         smoke.display = d;
302         smoke.window = window_create(d);
303         smoke.widget = window_add_widget(smoke.window, &smoke);
304         window_set_title(smoke.window, "smoke");
305
306         window_set_buffer_type(smoke.window, WINDOW_BUFFER_TYPE_SHM);
307         clock_gettime(CLOCK_MONOTONIC, &ts);
308         srandom(ts.tv_nsec);
309
310         smoke.current = 0;
311         size = smoke.height * smoke.width;
312         smoke.b[0].d = calloc(size, sizeof(float));
313         smoke.b[0].u = calloc(size, sizeof(float));
314         smoke.b[0].v = calloc(size, sizeof(float));
315         smoke.b[1].d = calloc(size, sizeof(float));
316         smoke.b[1].u = calloc(size, sizeof(float));
317         smoke.b[1].v = calloc(size, sizeof(float));
318
319         widget_set_motion_handler(smoke.widget, mouse_motion_handler);
320         widget_set_touch_motion_handler(smoke.widget, touch_motion_handler);
321         widget_set_resize_handler(smoke.widget, resize_handler);
322         widget_set_redraw_handler(smoke.widget, redraw_handler);
323
324         window_set_user_data(smoke.window, &smoke);
325
326         widget_schedule_resize(smoke.widget, smoke.width, smoke.height);
327
328         display_run(d);
329
330         return 0;
331 }