2 * Copyright © 2012 Collabora, Ltd.
3 * Copyright © 2012 Rob Clark
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 /* cliptest: for debugging calculate_edges() function.
26 * clip box position: mouse left drag, keys: w a s d
27 * clip box size: mouse right drag, keys: i j k l
28 * surface orientation: mouse wheel, keys: n m
29 * surface transform disable key: r
48 #include <linux/input.h>
49 #include <wayland-client.h>
51 #include "src/vertex-clipping.h"
54 typedef float GLfloat;
60 float s; /* sin phi */
61 float c; /* cos phi */
70 struct geometry *geometry;
74 weston_view_to_global_float(struct weston_view *view,
75 float sx, float sy, float *x, float *y)
77 struct geometry *g = view->geometry;
79 /* pure rotation around origin by sine and cosine */
80 *x = g->c * sx + g->s * sy;
81 *y = -g->s * sx + g->c * sy;
84 /* ---------------------- copied begins -----------------------*/
85 /* Keep this in sync with what is in gl-renderer.c! */
87 #define max(a, b) (((a) > (b)) ? (a) : (b))
88 #define min(a, b) (((a) > (b)) ? (b) : (a))
91 * Compute the boundary vertices of the intersection of the global coordinate
92 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
93 * 'surf_rect' when transformed from surface coordinates into global coordinates.
94 * The vertices are written to 'ex' and 'ey', and the return value is the
95 * number of vertices. Vertices are produced in clockwise winding order.
96 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
100 calculate_edges(struct weston_view *ev, pixman_box32_t *rect,
101 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
104 struct clip_context ctx;
106 GLfloat min_x, max_x, min_y, max_y;
107 struct polygon8 surf = {
108 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
109 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
113 ctx.clip.x1 = rect->x1;
114 ctx.clip.y1 = rect->y1;
115 ctx.clip.x2 = rect->x2;
116 ctx.clip.y2 = rect->y2;
118 /* transform surface to screen space: */
119 for (i = 0; i < surf.n; i++)
120 weston_view_to_global_float(ev, surf.x[i], surf.y[i],
121 &surf.x[i], &surf.y[i]);
123 /* find bounding box: */
124 min_x = max_x = surf.x[0];
125 min_y = max_y = surf.y[0];
127 for (i = 1; i < surf.n; i++) {
128 min_x = min(min_x, surf.x[i]);
129 max_x = max(max_x, surf.x[i]);
130 min_y = min(min_y, surf.y[i]);
131 max_y = max(max_y, surf.y[i]);
134 /* First, simple bounding box check to discard early transformed
135 * surface rects that do not intersect with the clip region:
137 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
138 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
141 /* Simple case, bounding box edges are parallel to surface edges,
142 * there will be only four edges. We just need to clip the surface
143 * vertices to the clip rect bounds:
145 if (!ev->transform.enabled)
146 return clip_simple(&ctx, &surf, ex, ey);
148 /* Transformed case: use a general polygon clipping algorithm to
149 * clip the surface rectangle with each side of 'rect'.
150 * The algorithm is Sutherland-Hodgman, as explained in
151 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
152 * but without looking at any of that code.
154 n = clip_transformed(&ctx, &surf, ex, ey);
163 /* ---------------------- copied ends -----------------------*/
166 geometry_set_phi(struct geometry *g, float phi)
174 geometry_init(struct geometry *g)
186 geometry_set_phi(g, 0.0);
194 struct geometry geometry;
198 struct window *window;
199 struct widget *widget;
200 struct display *display;
205 struct geometry geometry;
206 struct weston_view view;
210 draw_polygon_closed(cairo_t *cr, GLfloat *x, GLfloat *y, int n)
214 cairo_move_to(cr, x[0], y[0]);
215 for (i = 1; i < n; i++)
216 cairo_line_to(cr, x[i], y[i]);
217 cairo_line_to(cr, x[0], y[0]);
221 draw_polygon_labels(cairo_t *cr, GLfloat *x, GLfloat *y, int n)
226 for (i = 0; i < n; i++) {
227 snprintf(str, 16, "%d", i);
228 cairo_move_to(cr, x[i], y[i]);
229 cairo_show_text(cr, str);
234 draw_coordinates(cairo_t *cr, double ox, double oy, GLfloat *x, GLfloat *y, int n)
238 cairo_font_extents_t ext;
240 cairo_font_extents(cr, &ext);
241 for (i = 0; i < n; i++) {
242 snprintf(str, 64, "%d: %14.9f, %14.9f", i, x[i], y[i]);
243 cairo_move_to(cr, ox, oy + ext.height * (i + 1));
244 cairo_show_text(cr, str);
249 draw_box(cairo_t *cr, pixman_box32_t *box, struct weston_view *view)
254 weston_view_to_global_float(view, box->x1, box->y1, &x[0], &y[0]);
255 weston_view_to_global_float(view, box->x2, box->y1, &x[1], &y[1]);
256 weston_view_to_global_float(view, box->x2, box->y2, &x[2], &y[2]);
257 weston_view_to_global_float(view, box->x1, box->y2, &x[3], &y[3]);
259 x[0] = box->x1; y[0] = box->y1;
260 x[1] = box->x2; y[1] = box->y1;
261 x[2] = box->x2; y[2] = box->y2;
262 x[3] = box->x1; y[3] = box->y2;
265 draw_polygon_closed(cr, x, y, 4);
269 draw_geometry(cairo_t *cr, struct weston_view *view,
270 GLfloat *ex, GLfloat *ey, int n)
272 struct geometry *g = view->geometry;
275 draw_box(cr, &g->surf, view);
276 cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.4);
278 weston_view_to_global_float(view, g->surf.x1 - 4, g->surf.y1 - 4, &cx, &cy);
279 cairo_arc(cr, cx, cy, 1.5, 0.0, 2.0 * M_PI);
280 if (view->transform.enabled == 0)
281 cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.8);
284 draw_box(cr, &g->clip, NULL);
285 cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 0.4);
289 draw_polygon_closed(cr, ex, ey, n);
290 cairo_set_source_rgb(cr, 0.0, 1.0, 0.0);
293 cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.5);
294 draw_polygon_labels(cr, ex, ey, n);
299 redraw_handler(struct widget *widget, void *data)
301 struct cliptest *cliptest = data;
302 struct geometry *g = cliptest->view.geometry;
303 struct rectangle allocation;
305 cairo_surface_t *surface;
310 n = calculate_edges(&cliptest->view, &g->clip, &g->surf, ex, ey);
312 widget_get_allocation(cliptest->widget, &allocation);
314 surface = window_get_surface(cliptest->window);
315 cr = cairo_create(surface);
316 widget_get_allocation(cliptest->widget, &allocation);
317 cairo_rectangle(cr, allocation.x, allocation.y,
318 allocation.width, allocation.height);
321 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
322 cairo_set_source_rgba(cr, 0, 0, 0, 1);
325 cairo_translate(cr, allocation.x, allocation.y);
326 cairo_set_line_width(cr, 1.0);
327 cairo_move_to(cr, allocation.width / 2.0, 0.0);
328 cairo_line_to(cr, allocation.width / 2.0, allocation.height);
329 cairo_move_to(cr, 0.0, allocation.height / 2.0);
330 cairo_line_to(cr, allocation.width, allocation.height / 2.0);
331 cairo_set_source_rgba(cr, 0.5, 0.5, 0.5, 1.0);
334 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
335 cairo_push_group(cr);
336 cairo_translate(cr, allocation.width / 2.0,
337 allocation.height / 2.0);
338 cairo_scale(cr, 4.0, 4.0);
339 cairo_set_line_width(cr, 0.5);
340 cairo_set_line_join(cr, CAIRO_LINE_JOIN_BEVEL);
341 cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
342 CAIRO_FONT_WEIGHT_BOLD);
343 cairo_set_font_size(cr, 5.0);
344 draw_geometry(cr, &cliptest->view, ex, ey, n);
345 cairo_pop_group_to_source(cr);
348 cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 1.0);
349 cairo_select_font_face(cr, "monospace", CAIRO_FONT_SLANT_NORMAL,
350 CAIRO_FONT_WEIGHT_NORMAL);
351 cairo_set_font_size(cr, 12.0);
352 draw_coordinates(cr, 10.0, 10.0, ex, ey, n);
356 cairo_surface_destroy(surface);
360 motion_handler(struct widget *widget, struct input *input,
361 uint32_t time, float x, float y, void *data)
363 struct cliptest *cliptest = data;
364 struct ui_state *ui = &cliptest->ui;
365 struct geometry *ref = &ui->geometry;
366 struct geometry *geom = &cliptest->geometry;
370 return CURSOR_LEFT_PTR;
372 dx = (x - ui->down_pos[0]) * 0.25;
373 dy = (y - ui->down_pos[1]) * 0.25;
375 switch (ui->button) {
377 geom->clip.x1 = ref->clip.x1 + dx;
378 geom->clip.y1 = ref->clip.y1 + dy;
381 geom->clip.x2 = ref->clip.x2 + dx;
382 geom->clip.y2 = ref->clip.y2 + dy;
385 return CURSOR_LEFT_PTR;
388 widget_schedule_redraw(cliptest->widget);
393 button_handler(struct widget *widget, struct input *input,
394 uint32_t time, uint32_t button,
395 enum wl_pointer_button_state state, void *data)
397 struct cliptest *cliptest = data;
398 struct ui_state *ui = &cliptest->ui;
402 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
404 input_get_position(input, &ui->down_pos[0], &ui->down_pos[1]);
407 ui->geometry = cliptest->geometry;
412 axis_handler(struct widget *widget, struct input *input, uint32_t time,
413 uint32_t axis, wl_fixed_t value, void *data)
415 struct cliptest *cliptest = data;
416 struct geometry *geom = &cliptest->geometry;
418 if (axis != WL_POINTER_AXIS_VERTICAL_SCROLL)
421 geometry_set_phi(geom, geom->phi +
422 (M_PI / 12.0) * wl_fixed_to_double(value));
423 cliptest->view.transform.enabled = 1;
425 widget_schedule_redraw(cliptest->widget);
429 key_handler(struct window *window, struct input *input, uint32_t time,
430 uint32_t key, uint32_t sym,
431 enum wl_keyboard_key_state state, void *data)
433 struct cliptest *cliptest = data;
434 struct geometry *g = &cliptest->geometry;
436 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
441 display_exit(cliptest->display);
472 geometry_set_phi(g, g->phi + (M_PI / 24.0));
473 cliptest->view.transform.enabled = 1;
476 geometry_set_phi(g, g->phi - (M_PI / 24.0));
477 cliptest->view.transform.enabled = 1;
480 geometry_set_phi(g, 0.0);
481 cliptest->view.transform.enabled = 0;
487 widget_schedule_redraw(cliptest->widget);
491 keyboard_focus_handler(struct window *window,
492 struct input *device, void *data)
494 struct cliptest *cliptest = data;
496 window_schedule_redraw(cliptest->window);
500 fullscreen_handler(struct window *window, void *data)
502 struct cliptest *cliptest = data;
504 cliptest->fullscreen ^= 1;
505 window_set_fullscreen(window, cliptest->fullscreen);
508 static struct cliptest *
509 cliptest_create(struct display *display)
511 struct cliptest *cliptest;
513 cliptest = xzalloc(sizeof *cliptest);
514 cliptest->view.geometry = &cliptest->geometry;
515 cliptest->view.transform.enabled = 0;
516 geometry_init(&cliptest->geometry);
517 geometry_init(&cliptest->ui.geometry);
519 cliptest->window = window_create(display);
520 cliptest->widget = window_frame_create(cliptest->window, cliptest);
521 window_set_title(cliptest->window, "cliptest");
522 cliptest->display = display;
524 window_set_user_data(cliptest->window, cliptest);
525 widget_set_redraw_handler(cliptest->widget, redraw_handler);
526 widget_set_button_handler(cliptest->widget, button_handler);
527 widget_set_motion_handler(cliptest->widget, motion_handler);
528 widget_set_axis_handler(cliptest->widget, axis_handler);
530 window_set_keyboard_focus_handler(cliptest->window,
531 keyboard_focus_handler);
532 window_set_key_handler(cliptest->window, key_handler);
533 window_set_fullscreen_handler(cliptest->window, fullscreen_handler);
535 /* set minimum size */
536 widget_schedule_resize(cliptest->widget, 200, 100);
538 /* set current size */
539 widget_schedule_resize(cliptest->widget, 500, 400);
544 static struct timespec begin_time;
549 clock_gettime(CLOCK_MONOTONIC, &begin_time);
557 clock_gettime(CLOCK_MONOTONIC, &t);
558 return (double)(t.tv_sec - begin_time.tv_sec) +
559 1e-9 * (t.tv_nsec - begin_time.tv_nsec);
565 struct weston_view view;
566 struct geometry geom;
567 GLfloat ex[8], ey[8];
570 const int N = 1000000;
582 geometry_set_phi(&geom, 0.0);
584 view.transform.enabled = 1;
585 view.geometry = &geom;
588 for (i = 0; i < N; i++) {
589 geometry_set_phi(&geom, (float)i / 360.0f);
590 calculate_edges(&view, &geom.clip, &geom.surf, ex, ey);
594 printf("%d calls took %g s, average %g us/call\n", N, t, t / N * 1e6);
600 cliptest_destroy(struct cliptest *cliptest)
602 widget_destroy(cliptest->widget);
603 window_destroy(cliptest->window);
608 main(int argc, char *argv[])
611 struct cliptest *cliptest;
614 if (argc == 2 && !strcmp(argv[1], "-b"))
616 printf("Usage: %s [OPTIONS]\n -b run benchmark\n", argv[0]);
620 d = display_create(&argc, argv);
622 fprintf(stderr, "failed to create display: %m\n");
626 cliptest = cliptest_create(d);
629 cliptest_destroy(cliptest);