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