blob: 57aefdab4e01c8f164c57e78dafc231dea26545c [file] [log] [blame]
Pekka Paalanen8c492b12012-09-11 17:02:04 +03001/*
2 * Copyright © 2012 Collabora, Ltd.
3 * Copyright © 2012 Rob Clark
4 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07005 * 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:
Pekka Paalanen8c492b12012-09-11 17:02:04 +030011 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070012 * 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.
Pekka Paalanen8c492b12012-09-11 17:02:04 +030023 */
24
Pekka Paalaneneffef732014-08-20 10:35:26 +030025/* cliptest: for debugging calculate_edges() function.
Pekka Paalanen8c492b12012-09-11 17:02:04 +030026 * 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
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010033#include "config.h"
34
Pekka Paalanen8c492b12012-09-11 17:02:04 +030035#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
Pekka Paalanenb5e3ea22016-06-03 17:12:10 +030052#include "libweston/vertex-clipping.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070053#include "shared/xalloc.h"
Pekka Paalanen8c492b12012-09-11 17:02:04 +030054#include "window.h"
55
56typedef float GLfloat;
57
58struct geometry {
59 pixman_box32_t clip;
60
61 pixman_box32_t surf;
62 float s; /* sin phi */
63 float c; /* cos phi */
64 float phi;
65};
66
Pekka Paalaneneffef732014-08-20 10:35:26 +030067struct weston_view {
Pekka Paalanen8c492b12012-09-11 17:02:04 +030068 struct {
69 int enabled;
70 } transform;
71
72 struct geometry *geometry;
73};
74
75static void
Pekka Paalaneneffef732014-08-20 10:35:26 +030076weston_view_to_global_float(struct weston_view *view,
77 float sx, float sy, float *x, float *y)
Pekka Paalanen8c492b12012-09-11 17:02:04 +030078{
Pekka Paalaneneffef732014-08-20 10:35:26 +030079 struct geometry *g = view->geometry;
Pekka Paalanen8c492b12012-09-11 17:02:04 +030080
81 /* pure rotation around origin by sine and cosine */
82 *x = g->c * sx + g->s * sy;
83 *y = -g->s * sx + g->c * sy;
84}
85
Pekka Paalaneneffef732014-08-20 10:35:26 +030086/* ---------------------- copied begins -----------------------*/
87/* Keep this in sync with what is in gl-renderer.c! */
88
Pekka Paalanen8c492b12012-09-11 17:02:04 +030089#define max(a, b) (((a) > (b)) ? (a) : (b))
90#define min(a, b) (((a) > (b)) ? (b) : (a))
Pekka Paalanen8c492b12012-09-11 17:02:04 +030091
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +030092/*
93 * Compute the boundary vertices of the intersection of the global coordinate
94 * aligned rectangle 'rect', and an arbitrary quadrilateral produced from
95 * 'surf_rect' when transformed from surface coordinates into global coordinates.
96 * The vertices are written to 'ex' and 'ey', and the return value is the
97 * number of vertices. Vertices are produced in clockwise winding order.
98 * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero
99 * polygon area.
100 */
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300101static int
Pekka Paalaneneffef732014-08-20 10:35:26 +0300102calculate_edges(struct weston_view *ev, pixman_box32_t *rect,
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300103 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
104{
Pekka Paalaneneffef732014-08-20 10:35:26 +0300105
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300106 struct clip_context ctx;
Pekka Paalaneneffef732014-08-20 10:35:26 +0300107 int i, n;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300108 GLfloat min_x, max_x, min_y, max_y;
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300109 struct polygon8 surf = {
110 { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
111 { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 },
112 4
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300113 };
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300114
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300115 ctx.clip.x1 = rect->x1;
116 ctx.clip.y1 = rect->y1;
117 ctx.clip.x2 = rect->x2;
118 ctx.clip.y2 = rect->y2;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300119
120 /* transform surface to screen space: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300121 for (i = 0; i < surf.n; i++)
Pekka Paalaneneffef732014-08-20 10:35:26 +0300122 weston_view_to_global_float(ev, surf.x[i], surf.y[i],
123 &surf.x[i], &surf.y[i]);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300124
125 /* find bounding box: */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300126 min_x = max_x = surf.x[0];
127 min_y = max_y = surf.y[0];
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300128
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300129 for (i = 1; i < surf.n; i++) {
130 min_x = min(min_x, surf.x[i]);
131 max_x = max(max_x, surf.x[i]);
132 min_y = min(min_y, surf.y[i]);
133 max_y = max(max_y, surf.y[i]);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300134 }
135
136 /* First, simple bounding box check to discard early transformed
137 * surface rects that do not intersect with the clip region:
138 */
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300139 if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) ||
140 (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1))
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300141 return 0;
142
143 /* Simple case, bounding box edges are parallel to surface edges,
144 * there will be only four edges. We just need to clip the surface
145 * vertices to the clip rect bounds:
146 */
Pekka Paalaneneffef732014-08-20 10:35:26 +0300147 if (!ev->transform.enabled)
148 return clip_simple(&ctx, &surf, ex, ey);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300149
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300150 /* Transformed case: use a general polygon clipping algorithm to
151 * clip the surface rectangle with each side of 'rect'.
152 * The algorithm is Sutherland-Hodgman, as explained in
153 * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
154 * but without looking at any of that code.
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300155 */
Pekka Paalaneneffef732014-08-20 10:35:26 +0300156 n = clip_transformed(&ctx, &surf, ex, ey);
157
158 if (n < 3)
159 return 0;
160
161 return n;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300162}
163
Pekka Paalanen0d64a0f2012-09-11 17:02:05 +0300164
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300165/* ---------------------- copied ends -----------------------*/
166
167static void
168geometry_set_phi(struct geometry *g, float phi)
169{
170 g->phi = phi;
171 g->s = sin(phi);
172 g->c = cos(phi);
173}
174
175static void
176geometry_init(struct geometry *g)
177{
178 g->clip.x1 = -50;
179 g->clip.y1 = -50;
180 g->clip.x2 = -10;
181 g->clip.y2 = -10;
182
183 g->surf.x1 = -20;
184 g->surf.y1 = -20;
185 g->surf.x2 = 20;
186 g->surf.y2 = 20;
187
188 geometry_set_phi(g, 0.0);
189}
190
191struct ui_state {
192 uint32_t button;
193 int down;
194
195 int down_pos[2];
196 struct geometry geometry;
197};
198
199struct cliptest {
200 struct window *window;
201 struct widget *widget;
202 struct display *display;
203 int fullscreen;
204
205 struct ui_state ui;
206
207 struct geometry geometry;
Pekka Paalaneneffef732014-08-20 10:35:26 +0300208 struct weston_view view;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300209};
210
211static void
212draw_polygon_closed(cairo_t *cr, GLfloat *x, GLfloat *y, int n)
213{
214 int i;
215
216 cairo_move_to(cr, x[0], y[0]);
217 for (i = 1; i < n; i++)
218 cairo_line_to(cr, x[i], y[i]);
219 cairo_line_to(cr, x[0], y[0]);
220}
221
222static void
223draw_polygon_labels(cairo_t *cr, GLfloat *x, GLfloat *y, int n)
224{
225 char str[16];
226 int i;
227
228 for (i = 0; i < n; i++) {
229 snprintf(str, 16, "%d", i);
230 cairo_move_to(cr, x[i], y[i]);
231 cairo_show_text(cr, str);
232 }
233}
234
235static void
236draw_coordinates(cairo_t *cr, double ox, double oy, GLfloat *x, GLfloat *y, int n)
237{
238 char str[64];
239 int i;
240 cairo_font_extents_t ext;
241
242 cairo_font_extents(cr, &ext);
243 for (i = 0; i < n; i++) {
244 snprintf(str, 64, "%d: %14.9f, %14.9f", i, x[i], y[i]);
245 cairo_move_to(cr, ox, oy + ext.height * (i + 1));
246 cairo_show_text(cr, str);
247 }
248}
249
250static void
Pekka Paalaneneffef732014-08-20 10:35:26 +0300251draw_box(cairo_t *cr, pixman_box32_t *box, struct weston_view *view)
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300252{
253 GLfloat x[4], y[4];
254
Pekka Paalaneneffef732014-08-20 10:35:26 +0300255 if (view) {
256 weston_view_to_global_float(view, box->x1, box->y1, &x[0], &y[0]);
257 weston_view_to_global_float(view, box->x2, box->y1, &x[1], &y[1]);
258 weston_view_to_global_float(view, box->x2, box->y2, &x[2], &y[2]);
259 weston_view_to_global_float(view, box->x1, box->y2, &x[3], &y[3]);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300260 } else {
261 x[0] = box->x1; y[0] = box->y1;
262 x[1] = box->x2; y[1] = box->y1;
263 x[2] = box->x2; y[2] = box->y2;
264 x[3] = box->x1; y[3] = box->y2;
265 }
266
267 draw_polygon_closed(cr, x, y, 4);
268}
269
270static void
Pekka Paalaneneffef732014-08-20 10:35:26 +0300271draw_geometry(cairo_t *cr, struct weston_view *view,
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300272 GLfloat *ex, GLfloat *ey, int n)
273{
Pekka Paalaneneffef732014-08-20 10:35:26 +0300274 struct geometry *g = view->geometry;
275 float cx, cy;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300276
Pekka Paalaneneffef732014-08-20 10:35:26 +0300277 draw_box(cr, &g->surf, view);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300278 cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.4);
279 cairo_fill(cr);
Pekka Paalaneneffef732014-08-20 10:35:26 +0300280 weston_view_to_global_float(view, g->surf.x1 - 4, g->surf.y1 - 4, &cx, &cy);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300281 cairo_arc(cr, cx, cy, 1.5, 0.0, 2.0 * M_PI);
Pekka Paalaneneffef732014-08-20 10:35:26 +0300282 if (view->transform.enabled == 0)
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300283 cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.8);
284 cairo_fill(cr);
285
286 draw_box(cr, &g->clip, NULL);
287 cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 0.4);
288 cairo_fill(cr);
289
Derek Foremana0fae462014-08-18 16:13:41 -0500290 if (n) {
291 draw_polygon_closed(cr, ex, ey, n);
292 cairo_set_source_rgb(cr, 0.0, 1.0, 0.0);
293 cairo_stroke(cr);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300294
Derek Foremana0fae462014-08-18 16:13:41 -0500295 cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.5);
296 draw_polygon_labels(cr, ex, ey, n);
297 }
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300298}
299
300static void
301redraw_handler(struct widget *widget, void *data)
302{
303 struct cliptest *cliptest = data;
Pekka Paalaneneffef732014-08-20 10:35:26 +0300304 struct geometry *g = cliptest->view.geometry;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300305 struct rectangle allocation;
306 cairo_t *cr;
307 cairo_surface_t *surface;
308 GLfloat ex[8];
309 GLfloat ey[8];
310 int n;
311
Pekka Paalaneneffef732014-08-20 10:35:26 +0300312 n = calculate_edges(&cliptest->view, &g->clip, &g->surf, ex, ey);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300313
314 widget_get_allocation(cliptest->widget, &allocation);
315
316 surface = window_get_surface(cliptest->window);
317 cr = cairo_create(surface);
318 widget_get_allocation(cliptest->widget, &allocation);
319 cairo_rectangle(cr, allocation.x, allocation.y,
320 allocation.width, allocation.height);
321 cairo_clip(cr);
322
323 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
324 cairo_set_source_rgba(cr, 0, 0, 0, 1);
325 cairo_paint(cr);
326
327 cairo_translate(cr, allocation.x, allocation.y);
328 cairo_set_line_width(cr, 1.0);
329 cairo_move_to(cr, allocation.width / 2.0, 0.0);
330 cairo_line_to(cr, allocation.width / 2.0, allocation.height);
331 cairo_move_to(cr, 0.0, allocation.height / 2.0);
332 cairo_line_to(cr, allocation.width, allocation.height / 2.0);
333 cairo_set_source_rgba(cr, 0.5, 0.5, 0.5, 1.0);
334 cairo_stroke(cr);
335
336 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
337 cairo_push_group(cr);
338 cairo_translate(cr, allocation.width / 2.0,
339 allocation.height / 2.0);
340 cairo_scale(cr, 4.0, 4.0);
341 cairo_set_line_width(cr, 0.5);
342 cairo_set_line_join(cr, CAIRO_LINE_JOIN_BEVEL);
343 cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
344 CAIRO_FONT_WEIGHT_BOLD);
345 cairo_set_font_size(cr, 5.0);
Pekka Paalaneneffef732014-08-20 10:35:26 +0300346 draw_geometry(cr, &cliptest->view, ex, ey, n);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300347 cairo_pop_group_to_source(cr);
348 cairo_paint(cr);
349
350 cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 1.0);
351 cairo_select_font_face(cr, "monospace", CAIRO_FONT_SLANT_NORMAL,
352 CAIRO_FONT_WEIGHT_NORMAL);
353 cairo_set_font_size(cr, 12.0);
354 draw_coordinates(cr, 10.0, 10.0, ex, ey, n);
355
356 cairo_destroy(cr);
357
358 cairo_surface_destroy(surface);
359}
360
361static int
362motion_handler(struct widget *widget, struct input *input,
363 uint32_t time, float x, float y, void *data)
364{
365 struct cliptest *cliptest = data;
366 struct ui_state *ui = &cliptest->ui;
367 struct geometry *ref = &ui->geometry;
368 struct geometry *geom = &cliptest->geometry;
369 float dx, dy;
370
371 if (!ui->down)
372 return CURSOR_LEFT_PTR;
373
374 dx = (x - ui->down_pos[0]) * 0.25;
375 dy = (y - ui->down_pos[1]) * 0.25;
376
377 switch (ui->button) {
378 case BTN_LEFT:
379 geom->clip.x1 = ref->clip.x1 + dx;
380 geom->clip.y1 = ref->clip.y1 + dy;
381 /* fall through */
382 case BTN_RIGHT:
383 geom->clip.x2 = ref->clip.x2 + dx;
384 geom->clip.y2 = ref->clip.y2 + dy;
385 break;
386 default:
387 return CURSOR_LEFT_PTR;
388 }
389
390 widget_schedule_redraw(cliptest->widget);
391 return CURSOR_BLANK;
392}
393
394static void
395button_handler(struct widget *widget, struct input *input,
396 uint32_t time, uint32_t button,
397 enum wl_pointer_button_state state, void *data)
398{
399 struct cliptest *cliptest = data;
400 struct ui_state *ui = &cliptest->ui;
401
402 ui->button = button;
403
404 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
405 ui->down = 1;
406 input_get_position(input, &ui->down_pos[0], &ui->down_pos[1]);
407 } else {
408 ui->down = 0;
409 ui->geometry = cliptest->geometry;
410 }
411}
412
413static void
414axis_handler(struct widget *widget, struct input *input, uint32_t time,
415 uint32_t axis, wl_fixed_t value, void *data)
416{
417 struct cliptest *cliptest = data;
418 struct geometry *geom = &cliptest->geometry;
419
420 if (axis != WL_POINTER_AXIS_VERTICAL_SCROLL)
421 return;
422
423 geometry_set_phi(geom, geom->phi +
424 (M_PI / 12.0) * wl_fixed_to_double(value));
Pekka Paalaneneffef732014-08-20 10:35:26 +0300425 cliptest->view.transform.enabled = 1;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300426
427 widget_schedule_redraw(cliptest->widget);
428}
429
430static void
431key_handler(struct window *window, struct input *input, uint32_t time,
432 uint32_t key, uint32_t sym,
433 enum wl_keyboard_key_state state, void *data)
434{
435 struct cliptest *cliptest = data;
436 struct geometry *g = &cliptest->geometry;
437
438 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
439 return;
440
441 switch (sym) {
442 case XKB_KEY_Escape:
443 display_exit(cliptest->display);
444 return;
445 case XKB_KEY_w:
446 g->clip.y1 -= 1;
447 g->clip.y2 -= 1;
448 break;
449 case XKB_KEY_a:
450 g->clip.x1 -= 1;
451 g->clip.x2 -= 1;
452 break;
453 case XKB_KEY_s:
454 g->clip.y1 += 1;
455 g->clip.y2 += 1;
456 break;
457 case XKB_KEY_d:
458 g->clip.x1 += 1;
459 g->clip.x2 += 1;
460 break;
461 case XKB_KEY_i:
462 g->clip.y2 -= 1;
463 break;
464 case XKB_KEY_j:
465 g->clip.x2 -= 1;
466 break;
467 case XKB_KEY_k:
468 g->clip.y2 += 1;
469 break;
470 case XKB_KEY_l:
471 g->clip.x2 += 1;
472 break;
473 case XKB_KEY_n:
474 geometry_set_phi(g, g->phi + (M_PI / 24.0));
Pekka Paalaneneffef732014-08-20 10:35:26 +0300475 cliptest->view.transform.enabled = 1;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300476 break;
477 case XKB_KEY_m:
478 geometry_set_phi(g, g->phi - (M_PI / 24.0));
Pekka Paalaneneffef732014-08-20 10:35:26 +0300479 cliptest->view.transform.enabled = 1;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300480 break;
481 case XKB_KEY_r:
482 geometry_set_phi(g, 0.0);
Pekka Paalaneneffef732014-08-20 10:35:26 +0300483 cliptest->view.transform.enabled = 0;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300484 break;
485 default:
486 return;
487 }
488
489 widget_schedule_redraw(cliptest->widget);
490}
491
492static void
493keyboard_focus_handler(struct window *window,
494 struct input *device, void *data)
495{
496 struct cliptest *cliptest = data;
497
498 window_schedule_redraw(cliptest->window);
499}
500
501static void
502fullscreen_handler(struct window *window, void *data)
503{
504 struct cliptest *cliptest = data;
505
506 cliptest->fullscreen ^= 1;
507 window_set_fullscreen(window, cliptest->fullscreen);
508}
509
510static struct cliptest *
511cliptest_create(struct display *display)
512{
513 struct cliptest *cliptest;
514
Peter Huttererf3d62272013-08-08 11:57:05 +1000515 cliptest = xzalloc(sizeof *cliptest);
Pekka Paalaneneffef732014-08-20 10:35:26 +0300516 cliptest->view.geometry = &cliptest->geometry;
517 cliptest->view.transform.enabled = 0;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300518 geometry_init(&cliptest->geometry);
519 geometry_init(&cliptest->ui.geometry);
520
521 cliptest->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500522 cliptest->widget = window_frame_create(cliptest->window, cliptest);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300523 window_set_title(cliptest->window, "cliptest");
524 cliptest->display = display;
525
526 window_set_user_data(cliptest->window, cliptest);
527 widget_set_redraw_handler(cliptest->widget, redraw_handler);
528 widget_set_button_handler(cliptest->widget, button_handler);
529 widget_set_motion_handler(cliptest->widget, motion_handler);
530 widget_set_axis_handler(cliptest->widget, axis_handler);
531
532 window_set_keyboard_focus_handler(cliptest->window,
533 keyboard_focus_handler);
534 window_set_key_handler(cliptest->window, key_handler);
535 window_set_fullscreen_handler(cliptest->window, fullscreen_handler);
536
537 /* set minimum size */
538 widget_schedule_resize(cliptest->widget, 200, 100);
539
540 /* set current size */
541 widget_schedule_resize(cliptest->widget, 500, 400);
542
543 return cliptest;
544}
545
546static struct timespec begin_time;
547
548static void
549reset_timer(void)
550{
551 clock_gettime(CLOCK_MONOTONIC, &begin_time);
552}
553
554static double
555read_timer(void)
556{
557 struct timespec t;
558
559 clock_gettime(CLOCK_MONOTONIC, &t);
560 return (double)(t.tv_sec - begin_time.tv_sec) +
561 1e-9 * (t.tv_nsec - begin_time.tv_nsec);
562}
563
564static int
565benchmark(void)
566{
Pekka Paalaneneffef732014-08-20 10:35:26 +0300567 struct weston_view view;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300568 struct geometry geom;
569 GLfloat ex[8], ey[8];
570 int i;
571 double t;
572 const int N = 1000000;
573
574 geom.clip.x1 = -19;
575 geom.clip.y1 = -19;
576 geom.clip.x2 = 19;
577 geom.clip.y2 = 19;
578
579 geom.surf.x1 = -20;
580 geom.surf.y1 = -20;
581 geom.surf.x2 = 20;
582 geom.surf.y2 = 20;
583
584 geometry_set_phi(&geom, 0.0);
585
Pekka Paalaneneffef732014-08-20 10:35:26 +0300586 view.transform.enabled = 1;
587 view.geometry = &geom;
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300588
589 reset_timer();
590 for (i = 0; i < N; i++) {
591 geometry_set_phi(&geom, (float)i / 360.0f);
Pekka Paalaneneffef732014-08-20 10:35:26 +0300592 calculate_edges(&view, &geom.clip, &geom.surf, ex, ey);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300593 }
594 t = read_timer();
595
596 printf("%d calls took %g s, average %g us/call\n", N, t, t / N * 1e6);
597
598 return 0;
599}
600
vivek31732f72014-05-15 18:58:16 +0530601static void
602cliptest_destroy(struct cliptest *cliptest)
603{
604 widget_destroy(cliptest->widget);
605 window_destroy(cliptest->window);
606 free(cliptest);
607}
608
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300609int
610main(int argc, char *argv[])
611{
612 struct display *d;
613 struct cliptest *cliptest;
614
Bill Spitzak36bcf472014-08-08 12:59:56 -0700615 if (argc > 1) {
616 if (argc == 2 && !strcmp(argv[1], "-b"))
617 return benchmark();
618 printf("Usage: %s [OPTIONS]\n -b run benchmark\n", argv[0]);
619 return 1;
620 }
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300621
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500622 d = display_create(&argc, argv);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300623 if (d == NULL) {
624 fprintf(stderr, "failed to create display: %m\n");
625 return -1;
626 }
627
628 cliptest = cliptest_create(d);
629 display_run(d);
630
vivek31732f72014-05-15 18:58:16 +0530631 cliptest_destroy(cliptest);
632 display_destroy(d);
Pekka Paalanen8c492b12012-09-11 17:02:04 +0300633
634 return 0;
635}