blob: f8e5dd15939b0955f71f848db9124862b4d9bb1e [file] [log] [blame]
Pekka Paalanen8c492b12012-09-11 17:02:04 +03001/*
2 * Copyright © 2012 Collabora, Ltd.
3 * Copyright © 2012 Rob Clark
4 *
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.
14 *
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
21 * OF THIS SOFTWARE.
22 */
23
Pekka Paalaneneffef732014-08-20 10:35:26 +030024/* cliptest: for debugging calculate_edges() function.
Pekka Paalanen8c492b12012-09-11 17:02:04 +030025 * controls:
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
30 */
31
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010032#include "config.h"
33
Pekka Paalanen8c492b12012-09-11 17:02:04 +030034#include <stdint.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <fcntl.h>
39#include <libgen.h>
40#include <unistd.h>
41#include <math.h>
42#include <time.h>
43#include <pixman.h>
44#include <cairo.h>
45#include <float.h>
46#include <assert.h>
47
48#include <linux/input.h>
49#include <wayland-client.h>
50
Ondřej Majerech06e08922014-08-19 15:59:44 +020051#include "src/vertex-clipping.h"
Pekka Paalanen8c492b12012-09-11 17:02:04 +030052#include "window.h"
53
54typedef float GLfloat;
55
56struct geometry {
57 pixman_box32_t clip;
58
59 pixman_box32_t surf;
60 float s; /* sin phi */
61 float c; /* cos phi */
62 float phi;
63};
64
Pekka Paalaneneffef732014-08-20 10:35:26 +030065struct weston_view {
Pekka Paalanen8c492b12012-09-11 17:02:04 +030066 struct {
67 int enabled;
68 } transform;
69
70 struct geometry *geometry;
71};
72
73static void
Pekka Paalaneneffef732014-08-20 10:35:26 +030074weston_view_to_global_float(struct weston_view *view,
75 float sx, float sy, float *x, float *y)
Pekka Paalanen8c492b12012-09-11 17:02:04 +030076{
Pekka Paalaneneffef732014-08-20 10:35:26 +030077 struct geometry *g = view->geometry;
Pekka Paalanen8c492b12012-09-11 17:02:04 +030078
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;
82}
83
Pekka Paalaneneffef732014-08-20 10:35:26 +030084/* ---------------------- copied begins -----------------------*/
85/* Keep this in sync with what is in gl-renderer.c! */
86
Pekka Paalanen8c492b12012-09-11 17:02:04 +030087#define max(a, b) (((a) > (b)) ? (a) : (b))
88#define min(a, b) (((a) > (b)) ? (b) : (a))
Pekka Paalanen8c492b12012-09-11 17:02:04 +030089
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +030090/*
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
97 * polygon area.
98 */
Pekka Paalanen8c492b12012-09-11 17:02:04 +030099static int
Pekka Paalaneneffef732014-08-20 10:35:26 +0300100calculate_edges(struct weston_view *ev, pixman_box32_t *rect,
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300101 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
102{
Pekka Paalaneneffef732014-08-20 10:35:26 +0300103
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300104 struct clip_context ctx;
Pekka Paalaneneffef732014-08-20 10:35:26 +0300105 int i, n;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300106 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300107 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 },
110 4
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300111 };
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300112
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300113 ctx.clip.x1 = rect->x1;
114 ctx.clip.y1 = rect->y1;
115 ctx.clip.x2 = rect->x2;
116 ctx.clip.y2 = rect->y2;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300117
118 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300119 for (i = 0; i < surf.n; i++)
Pekka Paalaneneffef732014-08-20 10:35:26 +0300120 weston_view_to_global_float(ev, surf.x[i], surf.y[i],
121 &surf.x[i], &surf.y[i]);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300122
123 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300124 min_x = max_x = surf.x[0];
125 min_y = max_y = surf.y[0];
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300126
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300127 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]);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300132 }
133
134 /* First, simple bounding box check to discard early transformed
135 * surface rects that do not intersect with the clip region:
136 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300137 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
138 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300139 return 0;
140
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:
144 */
Pekka Paalaneneffef732014-08-20 10:35:26 +0300145 if (!ev->transform.enabled)
146 return clip_simple(&ctx, &surf, ex, ey);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300147
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300148 /* 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.
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300153 */
Pekka Paalaneneffef732014-08-20 10:35:26 +0300154 n = clip_transformed(&ctx, &surf, ex, ey);
155
156 if (n < 3)
157 return 0;
158
159 return n;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300160}
161
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300162
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300163/* ---------------------- copied ends -----------------------*/
164
165static void
166geometry_set_phi(struct geometry *g, float phi)
167{
168 g->phi = phi;
169 g->s = sin(phi);
170 g->c = cos(phi);
171}
172
173static void
174geometry_init(struct geometry *g)
175{
176 g->clip.x1 = -50;
177 g->clip.y1 = -50;
178 g->clip.x2 = -10;
179 g->clip.y2 = -10;
180
181 g->surf.x1 = -20;
182 g->surf.y1 = -20;
183 g->surf.x2 = 20;
184 g->surf.y2 = 20;
185
186 geometry_set_phi(g, 0.0);
187}
188
189struct ui_state {
190 uint32_t button;
191 int down;
192
193 int down_pos[2];
194 struct geometry geometry;
195};
196
197struct cliptest {
198 struct window *window;
199 struct widget *widget;
200 struct display *display;
201 int fullscreen;
202
203 struct ui_state ui;
204
205 struct geometry geometry;
Pekka Paalaneneffef732014-08-20 10:35:26 +0300206 struct weston_view view;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300207};
208
209static void
210draw_polygon_closed(cairo_t *cr, GLfloat *x, GLfloat *y, int n)
211{
212 int i;
213
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]);
218}
219
220static void
221draw_polygon_labels(cairo_t *cr, GLfloat *x, GLfloat *y, int n)
222{
223 char str[16];
224 int i;
225
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);
230 }
231}
232
233static void
234draw_coordinates(cairo_t *cr, double ox, double oy, GLfloat *x, GLfloat *y, int n)
235{
236 char str[64];
237 int i;
238 cairo_font_extents_t ext;
239
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);
245 }
246}
247
248static void
Pekka Paalaneneffef732014-08-20 10:35:26 +0300249draw_box(cairo_t *cr, pixman_box32_t *box, struct weston_view *view)
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300250{
251 GLfloat x[4], y[4];
252
Pekka Paalaneneffef732014-08-20 10:35:26 +0300253 if (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]);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300258 } else {
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;
263 }
264
265 draw_polygon_closed(cr, x, y, 4);
266}
267
268static void
Pekka Paalaneneffef732014-08-20 10:35:26 +0300269draw_geometry(cairo_t *cr, struct weston_view *view,
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300270 GLfloat *ex, GLfloat *ey, int n)
271{
Pekka Paalaneneffef732014-08-20 10:35:26 +0300272 struct geometry *g = view->geometry;
273 float cx, cy;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300274
Pekka Paalaneneffef732014-08-20 10:35:26 +0300275 draw_box(cr, &g->surf, view);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300276 cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.4);
277 cairo_fill(cr);
Pekka Paalaneneffef732014-08-20 10:35:26 +0300278 weston_view_to_global_float(view, g->surf.x1 - 4, g->surf.y1 - 4, &cx, &cy);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300279 cairo_arc(cr, cx, cy, 1.5, 0.0, 2.0 * M_PI);
Pekka Paalaneneffef732014-08-20 10:35:26 +0300280 if (view->transform.enabled == 0)
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300281 cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.8);
282 cairo_fill(cr);
283
284 draw_box(cr, &g->clip, NULL);
285 cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 0.4);
286 cairo_fill(cr);
287
Derek Foremana0fae462014-08-18 16:13:41 -0500288 if (n) {
289 draw_polygon_closed(cr, ex, ey, n);
290 cairo_set_source_rgb(cr, 0.0, 1.0, 0.0);
291 cairo_stroke(cr);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300292
Derek Foremana0fae462014-08-18 16:13:41 -0500293 cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.5);
294 draw_polygon_labels(cr, ex, ey, n);
295 }
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300296}
297
298static void
299redraw_handler(struct widget *widget, void *data)
300{
301 struct cliptest *cliptest = data;
Pekka Paalaneneffef732014-08-20 10:35:26 +0300302 struct geometry *g = cliptest->view.geometry;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300303 struct rectangle allocation;
304 cairo_t *cr;
305 cairo_surface_t *surface;
306 GLfloat ex[8];
307 GLfloat ey[8];
308 int n;
309
Pekka Paalaneneffef732014-08-20 10:35:26 +0300310 n = calculate_edges(&cliptest->view, &g->clip, &g->surf, ex, ey);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300311
312 widget_get_allocation(cliptest->widget, &allocation);
313
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);
319 cairo_clip(cr);
320
321 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
322 cairo_set_source_rgba(cr, 0, 0, 0, 1);
323 cairo_paint(cr);
324
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);
332 cairo_stroke(cr);
333
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);
Pekka Paalaneneffef732014-08-20 10:35:26 +0300344 draw_geometry(cr, &cliptest->view, ex, ey, n);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300345 cairo_pop_group_to_source(cr);
346 cairo_paint(cr);
347
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);
353
354 cairo_destroy(cr);
355
356 cairo_surface_destroy(surface);
357}
358
359static int
360motion_handler(struct widget *widget, struct input *input,
361 uint32_t time, float x, float y, void *data)
362{
363 struct cliptest *cliptest = data;
364 struct ui_state *ui = &cliptest->ui;
365 struct geometry *ref = &ui->geometry;
366 struct geometry *geom = &cliptest->geometry;
367 float dx, dy;
368
369 if (!ui->down)
370 return CURSOR_LEFT_PTR;
371
372 dx = (x - ui->down_pos[0]) * 0.25;
373 dy = (y - ui->down_pos[1]) * 0.25;
374
375 switch (ui->button) {
376 case BTN_LEFT:
377 geom->clip.x1 = ref->clip.x1 + dx;
378 geom->clip.y1 = ref->clip.y1 + dy;
379 /* fall through */
380 case BTN_RIGHT:
381 geom->clip.x2 = ref->clip.x2 + dx;
382 geom->clip.y2 = ref->clip.y2 + dy;
383 break;
384 default:
385 return CURSOR_LEFT_PTR;
386 }
387
388 widget_schedule_redraw(cliptest->widget);
389 return CURSOR_BLANK;
390}
391
392static void
393button_handler(struct widget *widget, struct input *input,
394 uint32_t time, uint32_t button,
395 enum wl_pointer_button_state state, void *data)
396{
397 struct cliptest *cliptest = data;
398 struct ui_state *ui = &cliptest->ui;
399
400 ui->button = button;
401
402 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
403 ui->down = 1;
404 input_get_position(input, &ui->down_pos[0], &ui->down_pos[1]);
405 } else {
406 ui->down = 0;
407 ui->geometry = cliptest->geometry;
408 }
409}
410
411static void
412axis_handler(struct widget *widget, struct input *input, uint32_t time,
413 uint32_t axis, wl_fixed_t value, void *data)
414{
415 struct cliptest *cliptest = data;
416 struct geometry *geom = &cliptest->geometry;
417
418 if (axis != WL_POINTER_AXIS_VERTICAL_SCROLL)
419 return;
420
421 geometry_set_phi(geom, geom->phi +
422 (M_PI / 12.0) * wl_fixed_to_double(value));
Pekka Paalaneneffef732014-08-20 10:35:26 +0300423 cliptest->view.transform.enabled = 1;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300424
425 widget_schedule_redraw(cliptest->widget);
426}
427
428static void
429key_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)
432{
433 struct cliptest *cliptest = data;
434 struct geometry *g = &cliptest->geometry;
435
436 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
437 return;
438
439 switch (sym) {
440 case XKB_KEY_Escape:
441 display_exit(cliptest->display);
442 return;
443 case XKB_KEY_w:
444 g->clip.y1 -= 1;
445 g->clip.y2 -= 1;
446 break;
447 case XKB_KEY_a:
448 g->clip.x1 -= 1;
449 g->clip.x2 -= 1;
450 break;
451 case XKB_KEY_s:
452 g->clip.y1 += 1;
453 g->clip.y2 += 1;
454 break;
455 case XKB_KEY_d:
456 g->clip.x1 += 1;
457 g->clip.x2 += 1;
458 break;
459 case XKB_KEY_i:
460 g->clip.y2 -= 1;
461 break;
462 case XKB_KEY_j:
463 g->clip.x2 -= 1;
464 break;
465 case XKB_KEY_k:
466 g->clip.y2 += 1;
467 break;
468 case XKB_KEY_l:
469 g->clip.x2 += 1;
470 break;
471 case XKB_KEY_n:
472 geometry_set_phi(g, g->phi + (M_PI / 24.0));
Pekka Paalaneneffef732014-08-20 10:35:26 +0300473 cliptest->view.transform.enabled = 1;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300474 break;
475 case XKB_KEY_m:
476 geometry_set_phi(g, g->phi - (M_PI / 24.0));
Pekka Paalaneneffef732014-08-20 10:35:26 +0300477 cliptest->view.transform.enabled = 1;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300478 break;
479 case XKB_KEY_r:
480 geometry_set_phi(g, 0.0);
Pekka Paalaneneffef732014-08-20 10:35:26 +0300481 cliptest->view.transform.enabled = 0;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300482 break;
483 default:
484 return;
485 }
486
487 widget_schedule_redraw(cliptest->widget);
488}
489
490static void
491keyboard_focus_handler(struct window *window,
492 struct input *device, void *data)
493{
494 struct cliptest *cliptest = data;
495
496 window_schedule_redraw(cliptest->window);
497}
498
499static void
500fullscreen_handler(struct window *window, void *data)
501{
502 struct cliptest *cliptest = data;
503
504 cliptest->fullscreen ^= 1;
505 window_set_fullscreen(window, cliptest->fullscreen);
506}
507
508static struct cliptest *
509cliptest_create(struct display *display)
510{
511 struct cliptest *cliptest;
512
Peter Huttererf3d62272013-08-08 11:57:05 +1000513 cliptest = xzalloc(sizeof *cliptest);
Pekka Paalaneneffef732014-08-20 10:35:26 +0300514 cliptest->view.geometry = &cliptest->geometry;
515 cliptest->view.transform.enabled = 0;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300516 geometry_init(&cliptest->geometry);
517 geometry_init(&cliptest->ui.geometry);
518
519 cliptest->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500520 cliptest->widget = window_frame_create(cliptest->window, cliptest);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300521 window_set_title(cliptest->window, "cliptest");
522 cliptest->display = display;
523
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);
529
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);
534
535 /* set minimum size */
536 widget_schedule_resize(cliptest->widget, 200, 100);
537
538 /* set current size */
539 widget_schedule_resize(cliptest->widget, 500, 400);
540
541 return cliptest;
542}
543
544static struct timespec begin_time;
545
546static void
547reset_timer(void)
548{
549 clock_gettime(CLOCK_MONOTONIC, &begin_time);
550}
551
552static double
553read_timer(void)
554{
555 struct timespec t;
556
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);
560}
561
562static int
563benchmark(void)
564{
Pekka Paalaneneffef732014-08-20 10:35:26 +0300565 struct weston_view view;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300566 struct geometry geom;
567 GLfloat ex[8], ey[8];
568 int i;
569 double t;
570 const int N = 1000000;
571
572 geom.clip.x1 = -19;
573 geom.clip.y1 = -19;
574 geom.clip.x2 = 19;
575 geom.clip.y2 = 19;
576
577 geom.surf.x1 = -20;
578 geom.surf.y1 = -20;
579 geom.surf.x2 = 20;
580 geom.surf.y2 = 20;
581
582 geometry_set_phi(&geom, 0.0);
583
Pekka Paalaneneffef732014-08-20 10:35:26 +0300584 view.transform.enabled = 1;
585 view.geometry = &geom;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300586
587 reset_timer();
588 for (i = 0; i < N; i++) {
589 geometry_set_phi(&geom, (float)i / 360.0f);
Pekka Paalaneneffef732014-08-20 10:35:26 +0300590 calculate_edges(&view, &geom.clip, &geom.surf, ex, ey);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300591 }
592 t = read_timer();
593
594 printf("%d calls took %g s, average %g us/call\n", N, t, t / N * 1e6);
595
596 return 0;
597}
598
vivek31732f72014-05-15 18:58:16 +0530599static void
600cliptest_destroy(struct cliptest *cliptest)
601{
602 widget_destroy(cliptest->widget);
603 window_destroy(cliptest->window);
604 free(cliptest);
605}
606
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300607int
608main(int argc, char *argv[])
609{
610 struct display *d;
611 struct cliptest *cliptest;
612
Bill Spitzak36bcf472014-08-08 12:59:56 -0700613 if (argc > 1) {
614 if (argc == 2 && !strcmp(argv[1], "-b"))
615 return benchmark();
616 printf("Usage: %s [OPTIONS]\n -b run benchmark\n", argv[0]);
617 return 1;
618 }
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300619
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500620 d = display_create(&argc, argv);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300621 if (d == NULL) {
622 fprintf(stderr, "failed to create display: %m\n");
623 return -1;
624 }
625
626 cliptest = cliptest_create(d);
627 display_run(d);
628
vivek31732f72014-05-15 18:58:16 +0530629 cliptest_destroy(cliptest);
630 display_destroy(d);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300631
632 return 0;
633}