1 /*
2 * Copyright © 2012 Collabora, Ltd.
3 * Copyright © 2012 Rob Clark
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 /* cliptest: for debugging calculate_edges() function.
26 * controls:
27 * clip box position: mouse left drag, keys: w a s d
28 * clip box size: mouse right drag, keys: i j k l
29 * surface orientation: mouse wheel, keys: n m
30 * surface transform disable key: r
31 */
32
33 #include "config.h"
34
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <fcntl.h>
40 #include <libgen.h>
41 #include <unistd.h>
42 #include <math.h>
43 #include <time.h>
44 #include <pixman.h>
45 #include <cairo.h>
46 #include <float.h>
47 #include <assert.h>
48
49 #include <linux/input.h>
50 #include <wayland-client.h>
51
52 #include "src/vertex-clipping.h"
53 #include "window.h"
54
55 typedef float GLfloat;
56
57 struct geometry {
58 pixman_box32_t clip;
59
60 pixman_box32_t surf;
61 float s; /* sin phi */
62 float c; /* cos phi */
63 float phi;
64 };
65
66 struct weston_view {
67 struct {
68 int enabled;
69 } transform;
70
71 struct geometry *geometry;
72 };
73
74 static void
weston_view_to_global_float(struct weston_view * view,float sx,float sy,float * x,float * y)75 weston_view_to_global_float(struct weston_view *view,
76 float sx, float sy, float *x, float *y)
77 {
78 struct geometry *g = view->geometry;
79
80 /* pure rotation around origin by sine and cosine */
81 *x = g->c * sx + g->s * sy;
82 *y = -g->s * sx + g->c * sy;
83 }
84
85 /* ---------------------- copied begins -----------------------*/
86 /* Keep this in sync with what is in gl-renderer.c! */
87
88 #define max(a, b) (((a) > (b)) ? (a) : (b))
89 #define min(a, b) (((a) > (b)) ? (b) : (a))
90
91 /*
92 * Compute the boundary vertices of the intersection of the global coordinate
93 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
94 * 'surf_rect' when transformed from surface coordinates into global coordinates.
95 * The vertices are written to 'ex' and 'ey', and the return value is the
96 * number of vertices. Vertices are produced in clockwise winding order.
97 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
98 * polygon area.
99 */
100 static int
calculate_edges(struct weston_view * ev,pixman_box32_t * rect,pixman_box32_t * surf_rect,GLfloat * ex,GLfloat * ey)101 calculate_edges(struct weston_view *ev, pixman_box32_t *rect,
102 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
103 {
104
105 struct clip_context ctx;
106 int i, n;
107 GLfloat min_x, max_x, min_y, max_y;
108 struct polygon8 surf = {
109 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
110 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
111 4
112 };
113
114 ctx.clip.x1 = rect->x1;
115 ctx.clip.y1 = rect->y1;
116 ctx.clip.x2 = rect->x2;
117 ctx.clip.y2 = rect->y2;
118
119 /* transform surface to screen space: */
120 for (i = 0; i < surf.n; i++)
121 weston_view_to_global_float(ev, surf.x[i], surf.y[i],
122 &surf.x[i], &surf.y[i]);
123
124 /* find bounding box: */
125 min_x = max_x = surf.x[0];
126 min_y = max_y = surf.y[0];
127
128 for (i = 1; i < surf.n; i++) {
129 min_x = min(min_x, surf.x[i]);
130 max_x = max(max_x, surf.x[i]);
131 min_y = min(min_y, surf.y[i]);
132 max_y = max(max_y, surf.y[i]);
133 }
134
135 /* First, simple bounding box check to discard early transformed
136 * surface rects that do not intersect with the clip region:
137 */
138 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
139 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
140 return 0;
141
142 /* Simple case, bounding box edges are parallel to surface edges,
143 * there will be only four edges. We just need to clip the surface
144 * vertices to the clip rect bounds:
145 */
146 if (!ev->transform.enabled)
147 return clip_simple(&ctx, &surf, ex, ey);
148
149 /* Transformed case: use a general polygon clipping algorithm to
150 * clip the surface rectangle with each side of 'rect'.
151 * The algorithm is Sutherland-Hodgman, as explained in
152 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
153 * but without looking at any of that code.
154 */
155 n = clip_transformed(&ctx, &surf, ex, ey);
156
157 if (n < 3)
158 return 0;
159
160 return n;
161 }
162
163
164 /* ---------------------- copied ends -----------------------*/
165
166 static void
geometry_set_phi(struct geometry * g,float phi)167 geometry_set_phi(struct geometry *g, float phi)
168 {
169 g->phi = phi;
170 g->s = sin(phi);
171 g->c = cos(phi);
172 }
173
174 static void
geometry_init(struct geometry * g)175 geometry_init(struct geometry *g)
176 {
177 g->clip.x1 = -50;
178 g->clip.y1 = -50;
179 g->clip.x2 = -10;
180 g->clip.y2 = -10;
181
182 g->surf.x1 = -20;
183 g->surf.y1 = -20;
184 g->surf.x2 = 20;
185 g->surf.y2 = 20;
186
187 geometry_set_phi(g, 0.0);
188 }
189
190 struct ui_state {
191 uint32_t button;
192 int down;
193
194 int down_pos[2];
195 struct geometry geometry;
196 };
197
198 struct cliptest {
199 struct window *window;
200 struct widget *widget;
201 struct display *display;
202 int fullscreen;
203
204 struct ui_state ui;
205
206 struct geometry geometry;
207 struct weston_view view;
208 };
209
210 static void
draw_polygon_closed(cairo_t * cr,GLfloat * x,GLfloat * y,int n)211 draw_polygon_closed(cairo_t *cr, GLfloat *x, GLfloat *y, int n)
212 {
213 int i;
214
215 cairo_move_to(cr, x[0], y[0]);
216 for (i = 1; i < n; i++)
217 cairo_line_to(cr, x[i], y[i]);
218 cairo_line_to(cr, x[0], y[0]);
219 }
220
221 static void
draw_polygon_labels(cairo_t * cr,GLfloat * x,GLfloat * y,int n)222 draw_polygon_labels(cairo_t *cr, GLfloat *x, GLfloat *y, int n)
223 {
224 char str[16];
225 int i;
226
227 for (i = 0; i < n; i++) {
228 snprintf(str, 16, "%d", i);
229 cairo_move_to(cr, x[i], y[i]);
230 cairo_show_text(cr, str);
231 }
232 }
233
234 static void
draw_coordinates(cairo_t * cr,double ox,double oy,GLfloat * x,GLfloat * y,int n)235 draw_coordinates(cairo_t *cr, double ox, double oy, GLfloat *x, GLfloat *y, int n)
236 {
237 char str[64];
238 int i;
239 cairo_font_extents_t ext;
240
241 cairo_font_extents(cr, &ext);
242 for (i = 0; i < n; i++) {
243 snprintf(str, 64, "%d: %14.9f, %14.9f", i, x[i], y[i]);
244 cairo_move_to(cr, ox, oy + ext.height * (i + 1));
245 cairo_show_text(cr, str);
246 }
247 }
248
249 static void
draw_box(cairo_t * cr,pixman_box32_t * box,struct weston_view * view)250 draw_box(cairo_t *cr, pixman_box32_t *box, struct weston_view *view)
251 {
252 GLfloat x[4], y[4];
253
254 if (view) {
255 weston_view_to_global_float(view, box->x1, box->y1, &x[0], &y[0]);
256 weston_view_to_global_float(view, box->x2, box->y1, &x[1], &y[1]);
257 weston_view_to_global_float(view, box->x2, box->y2, &x[2], &y[2]);
258 weston_view_to_global_float(view, box->x1, box->y2, &x[3], &y[3]);
259 } else {
260 x[0] = box->x1; y[0] = box->y1;
261 x[1] = box->x2; y[1] = box->y1;
262 x[2] = box->x2; y[2] = box->y2;
263 x[3] = box->x1; y[3] = box->y2;
264 }
265
266 draw_polygon_closed(cr, x, y, 4);
267 }
268
269 static void
draw_geometry(cairo_t * cr,struct weston_view * view,GLfloat * ex,GLfloat * ey,int n)270 draw_geometry(cairo_t *cr, struct weston_view *view,
271 GLfloat *ex, GLfloat *ey, int n)
272 {
273 struct geometry *g = view->geometry;
274 float cx, cy;
275
276 draw_box(cr, &g->surf, view);
277 cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.4);
278 cairo_fill(cr);
279 weston_view_to_global_float(view, g->surf.x1 - 4, g->surf.y1 - 4, &cx, &cy);
280 cairo_arc(cr, cx, cy, 1.5, 0.0, 2.0 * M_PI);
281 if (view->transform.enabled == 0)
282 cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.8);
283 cairo_fill(cr);
284
285 draw_box(cr, &g->clip, NULL);
286 cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 0.4);
287 cairo_fill(cr);
288
289 if (n) {
290 draw_polygon_closed(cr, ex, ey, n);
291 cairo_set_source_rgb(cr, 0.0, 1.0, 0.0);
292 cairo_stroke(cr);
293
294 cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.5);
295 draw_polygon_labels(cr, ex, ey, n);
296 }
297 }
298
299 static void
redraw_handler(struct widget * widget,void * data)300 redraw_handler(struct widget *widget, void *data)
301 {
302 struct cliptest *cliptest = data;
303 struct geometry *g = cliptest->view.geometry;
304 struct rectangle allocation;
305 cairo_t *cr;
306 cairo_surface_t *surface;
307 GLfloat ex[8];
308 GLfloat ey[8];
309 int n;
310
311 n = calculate_edges(&cliptest->view, &g->clip, &g->surf, ex, ey);
312
313 widget_get_allocation(cliptest->widget, &allocation);
314
315 surface = window_get_surface(cliptest->window);
316 cr = cairo_create(surface);
317 widget_get_allocation(cliptest->widget, &allocation);
318 cairo_rectangle(cr, allocation.x, allocation.y,
319 allocation.width, allocation.height);
320 cairo_clip(cr);
321
322 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
323 cairo_set_source_rgba(cr, 0, 0, 0, 1);
324 cairo_paint(cr);
325
326 cairo_translate(cr, allocation.x, allocation.y);
327 cairo_set_line_width(cr, 1.0);
328 cairo_move_to(cr, allocation.width / 2.0, 0.0);
329 cairo_line_to(cr, allocation.width / 2.0, allocation.height);
330 cairo_move_to(cr, 0.0, allocation.height / 2.0);
331 cairo_line_to(cr, allocation.width, allocation.height / 2.0);
332 cairo_set_source_rgba(cr, 0.5, 0.5, 0.5, 1.0);
333 cairo_stroke(cr);
334
335 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
336 cairo_push_group(cr);
337 cairo_translate(cr, allocation.width / 2.0,
338 allocation.height / 2.0);
339 cairo_scale(cr, 4.0, 4.0);
340 cairo_set_line_width(cr, 0.5);
341 cairo_set_line_join(cr, CAIRO_LINE_JOIN_BEVEL);
342 cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
343 CAIRO_FONT_WEIGHT_BOLD);
344 cairo_set_font_size(cr, 5.0);
345 draw_geometry(cr, &cliptest->view, ex, ey, n);
346 cairo_pop_group_to_source(cr);
347 cairo_paint(cr);
348
349 cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 1.0);
350 cairo_select_font_face(cr, "monospace", CAIRO_FONT_SLANT_NORMAL,
351 CAIRO_FONT_WEIGHT_NORMAL);
352 cairo_set_font_size(cr, 12.0);
353 draw_coordinates(cr, 10.0, 10.0, ex, ey, n);
354
355 cairo_destroy(cr);
356
357 cairo_surface_destroy(surface);
358 }
359
360 static int
motion_handler(struct widget * widget,struct input * input,uint32_t time,float x,float y,void * data)361 motion_handler(struct widget *widget, struct input *input,
362 uint32_t time, float x, float y, void *data)
363 {
364 struct cliptest *cliptest = data;
365 struct ui_state *ui = &cliptest->ui;
366 struct geometry *ref = &ui->geometry;
367 struct geometry *geom = &cliptest->geometry;
368 float dx, dy;
369
370 if (!ui->down)
371 return CURSOR_LEFT_PTR;
372
373 dx = (x - ui->down_pos[0]) * 0.25;
374 dy = (y - ui->down_pos[1]) * 0.25;
375
376 switch (ui->button) {
377 case BTN_LEFT:
378 geom->clip.x1 = ref->clip.x1 + dx;
379 geom->clip.y1 = ref->clip.y1 + dy;
380 /* fall through */
381 case BTN_RIGHT:
382 geom->clip.x2 = ref->clip.x2 + dx;
383 geom->clip.y2 = ref->clip.y2 + dy;
384 break;
385 default:
386 return CURSOR_LEFT_PTR;
387 }
388
389 widget_schedule_redraw(cliptest->widget);
390 return CURSOR_BLANK;
391 }
392
393 static void
button_handler(struct widget * widget,struct input * input,uint32_t time,uint32_t button,enum wl_pointer_button_state state,void * data)394 button_handler(struct widget *widget, struct input *input,
395 uint32_t time, uint32_t button,
396 enum wl_pointer_button_state state, void *data)
397 {
398 struct cliptest *cliptest = data;
399 struct ui_state *ui = &cliptest->ui;
400
401 ui->button = button;
402
403 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
404 ui->down = 1;
405 input_get_position(input, &ui->down_pos[0], &ui->down_pos[1]);
406 } else {
407 ui->down = 0;
408 ui->geometry = cliptest->geometry;
409 }
410 }
411
412 static void
axis_handler(struct widget * widget,struct input * input,uint32_t time,uint32_t axis,wl_fixed_t value,void * data)413 axis_handler(struct widget *widget, struct input *input, uint32_t time,
414 uint32_t axis, wl_fixed_t value, void *data)
415 {
416 struct cliptest *cliptest = data;
417 struct geometry *geom = &cliptest->geometry;
418
419 if (axis != WL_POINTER_AXIS_VERTICAL_SCROLL)
420 return;
421
422 geometry_set_phi(geom, geom->phi +
423 (M_PI / 12.0) * wl_fixed_to_double(value));
424 cliptest->view.transform.enabled = 1;
425
426 widget_schedule_redraw(cliptest->widget);
427 }
428
429 static void
key_handler(struct window * window,struct input * input,uint32_t time,uint32_t key,uint32_t sym,enum wl_keyboard_key_state state,void * data)430 key_handler(struct window *window, struct input *input, uint32_t time,
431 uint32_t key, uint32_t sym,
432 enum wl_keyboard_key_state state, void *data)
433 {
434 struct cliptest *cliptest = data;
435 struct geometry *g = &cliptest->geometry;
436
437 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
438 return;
439
440 switch (sym) {
441 case XKB_KEY_Escape:
442 display_exit(cliptest->display);
443 return;
444 case XKB_KEY_w:
445 g->clip.y1 -= 1;
446 g->clip.y2 -= 1;
447 break;
448 case XKB_KEY_a:
449 g->clip.x1 -= 1;
450 g->clip.x2 -= 1;
451 break;
452 case XKB_KEY_s:
453 g->clip.y1 += 1;
454 g->clip.y2 += 1;
455 break;
456 case XKB_KEY_d:
457 g->clip.x1 += 1;
458 g->clip.x2 += 1;
459 break;
460 case XKB_KEY_i:
461 g->clip.y2 -= 1;
462 break;
463 case XKB_KEY_j:
464 g->clip.x2 -= 1;
465 break;
466 case XKB_KEY_k:
467 g->clip.y2 += 1;
468 break;
469 case XKB_KEY_l:
470 g->clip.x2 += 1;
471 break;
472 case XKB_KEY_n:
473 geometry_set_phi(g, g->phi + (M_PI / 24.0));
474 cliptest->view.transform.enabled = 1;
475 break;
476 case XKB_KEY_m:
477 geometry_set_phi(g, g->phi - (M_PI / 24.0));
478 cliptest->view.transform.enabled = 1;
479 break;
480 case XKB_KEY_r:
481 geometry_set_phi(g, 0.0);
482 cliptest->view.transform.enabled = 0;
483 break;
484 default:
485 return;
486 }
487
488 widget_schedule_redraw(cliptest->widget);
489 }
490
491 static void
keyboard_focus_handler(struct window * window,struct input * device,void * data)492 keyboard_focus_handler(struct window *window,
493 struct input *device, void *data)
494 {
495 struct cliptest *cliptest = data;
496
497 window_schedule_redraw(cliptest->window);
498 }
499
500 static void
fullscreen_handler(struct window * window,void * data)501 fullscreen_handler(struct window *window, void *data)
502 {
503 struct cliptest *cliptest = data;
504
505 cliptest->fullscreen ^= 1;
506 window_set_fullscreen(window, cliptest->fullscreen);
507 }
508
509 static struct cliptest *
cliptest_create(struct display * display)510 cliptest_create(struct display *display)
511 {
512 struct cliptest *cliptest;
513
514 cliptest = xzalloc(sizeof *cliptest);
515 cliptest->view.geometry = &cliptest->geometry;
516 cliptest->view.transform.enabled = 0;
517 geometry_init(&cliptest->geometry);
518 geometry_init(&cliptest->ui.geometry);
519
520 cliptest->window = window_create(display);
521 cliptest->widget = window_frame_create(cliptest->window, cliptest);
522 window_set_title(cliptest->window, "cliptest");
523 cliptest->display = display;
524
525 window_set_user_data(cliptest->window, cliptest);
526 widget_set_redraw_handler(cliptest->widget, redraw_handler);
527 widget_set_button_handler(cliptest->widget, button_handler);
528 widget_set_motion_handler(cliptest->widget, motion_handler);
529 widget_set_axis_handler(cliptest->widget, axis_handler);
530
531 window_set_keyboard_focus_handler(cliptest->window,
532 keyboard_focus_handler);
533 window_set_key_handler(cliptest->window, key_handler);
534 window_set_fullscreen_handler(cliptest->window, fullscreen_handler);
535
536 /* set minimum size */
537 widget_schedule_resize(cliptest->widget, 200, 100);
538
539 /* set current size */
540 widget_schedule_resize(cliptest->widget, 500, 400);
541
542 return cliptest;
543 }
544
545 static struct timespec begin_time;
546
547 static void
reset_timer(void)548 reset_timer(void)
549 {
550 clock_gettime(CLOCK_MONOTONIC, &begin_time);
551 }
552
553 static double
read_timer(void)554 read_timer(void)
555 {
556 struct timespec t;
557
558 clock_gettime(CLOCK_MONOTONIC, &t);
559 return (double)(t.tv_sec - begin_time.tv_sec) +
560 1e-9 * (t.tv_nsec - begin_time.tv_nsec);
561 }
562
563 static int
benchmark(void)564 benchmark(void)
565 {
566 struct weston_view view;
567 struct geometry geom;
568 GLfloat ex[8], ey[8];
569 int i;
570 double t;
571 const int N = 1000000;
572
573 geom.clip.x1 = -19;
574 geom.clip.y1 = -19;
575 geom.clip.x2 = 19;
576 geom.clip.y2 = 19;
577
578 geom.surf.x1 = -20;
579 geom.surf.y1 = -20;
580 geom.surf.x2 = 20;
581 geom.surf.y2 = 20;
582
583 geometry_set_phi(&geom, 0.0);
584
585 view.transform.enabled = 1;
586 view.geometry = &geom;
587
588 reset_timer();
589 for (i = 0; i < N; i++) {
590 geometry_set_phi(&geom, (float)i / 360.0f);
591 calculate_edges(&view, &geom.clip, &geom.surf, ex, ey);
592 }
593 t = read_timer();
594
595 printf("%d calls took %g s, average %g us/call\n", N, t, t / N * 1e6);
596
597 return 0;
598 }
599
600 static void
cliptest_destroy(struct cliptest * cliptest)601 cliptest_destroy(struct cliptest *cliptest)
602 {
603 widget_destroy(cliptest->widget);
604 window_destroy(cliptest->window);
605 free(cliptest);
606 }
607
608 int
main(int argc,char * argv[])609 main(int argc, char *argv[])
610 {
611 struct display *d;
612 struct cliptest *cliptest;
613
614 if (argc > 1) {
615 if (argc == 2 && !strcmp(argv[1], "-b"))
616 return benchmark();
617 printf("Usage: %s [OPTIONS]\n -b run benchmark\n", argv[0]);
618 return 1;
619 }
620
621 d = display_create(&argc, argv);
622 if (d == NULL) {
623 fprintf(stderr, "failed to create display: %m\n");
624 return -1;
625 }
626
627 cliptest = cliptest_create(d);
628 display_run(d);
629
630 cliptest_destroy(cliptest);
631 display_destroy(d);
632
633 return 0;
634 }
635