Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 1 | /* |
| 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 Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 24 | /* cliptest: for debugging calculate_edges() function. |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 25 | * 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 Wedgbury | 9cd661e | 2014-04-07 12:40:35 +0100 | [diff] [blame] | 32 | #include "config.h" |
| 33 | |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 34 | #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 Majerech | 06e0892 | 2014-08-19 15:59:44 +0200 | [diff] [blame] | 51 | #include "src/vertex-clipping.h" |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 52 | #include "window.h" |
| 53 | |
| 54 | typedef float GLfloat; |
| 55 | |
| 56 | struct 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 Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 65 | struct weston_view { |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 66 | struct { |
| 67 | int enabled; |
| 68 | } transform; |
| 69 | |
| 70 | struct geometry *geometry; |
| 71 | }; |
| 72 | |
| 73 | static void |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 74 | weston_view_to_global_float(struct weston_view *view, |
| 75 | float sx, float sy, float *x, float *y) |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 76 | { |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 77 | struct geometry *g = view->geometry; |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 78 | |
| 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 Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 84 | /* ---------------------- copied begins -----------------------*/ |
| 85 | /* Keep this in sync with what is in gl-renderer.c! */ |
| 86 | |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 87 | #define max(a, b) (((a) > (b)) ? (a) : (b)) |
| 88 | #define min(a, b) (((a) > (b)) ? (b) : (a)) |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 89 | |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 90 | /* |
| 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 Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 99 | static int |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 100 | calculate_edges(struct weston_view *ev, pixman_box32_t *rect, |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 101 | pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey) |
| 102 | { |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 103 | |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 104 | struct clip_context ctx; |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 105 | int i, n; |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 106 | GLfloat min_x, max_x, min_y, max_y; |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 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 }, |
| 110 | 4 |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 111 | }; |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 112 | |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 113 | ctx.clip.x1 = rect->x1; |
| 114 | ctx.clip.y1 = rect->y1; |
| 115 | ctx.clip.x2 = rect->x2; |
| 116 | ctx.clip.y2 = rect->y2; |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 117 | |
| 118 | /* transform surface to screen space: */ |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 119 | for (i = 0; i < surf.n; i++) |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 120 | weston_view_to_global_float(ev, surf.x[i], surf.y[i], |
| 121 | &surf.x[i], &surf.y[i]); |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 122 | |
| 123 | /* find bounding box: */ |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 124 | min_x = max_x = surf.x[0]; |
| 125 | min_y = max_y = surf.y[0]; |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 126 | |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 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]); |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* First, simple bounding box check to discard early transformed |
| 135 | * surface rects that do not intersect with the clip region: |
| 136 | */ |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 137 | if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) || |
| 138 | (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1)) |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 139 | 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 Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 145 | if (!ev->transform.enabled) |
| 146 | return clip_simple(&ctx, &surf, ex, ey); |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 147 | |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 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. |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 153 | */ |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 154 | n = clip_transformed(&ctx, &surf, ex, ey); |
| 155 | |
| 156 | if (n < 3) |
| 157 | return 0; |
| 158 | |
| 159 | return n; |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 160 | } |
| 161 | |
Pekka Paalanen | 0d64a0f | 2012-09-11 17:02:05 +0300 | [diff] [blame] | 162 | |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 163 | /* ---------------------- copied ends -----------------------*/ |
| 164 | |
| 165 | static void |
| 166 | geometry_set_phi(struct geometry *g, float phi) |
| 167 | { |
| 168 | g->phi = phi; |
| 169 | g->s = sin(phi); |
| 170 | g->c = cos(phi); |
| 171 | } |
| 172 | |
| 173 | static void |
| 174 | geometry_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 | |
| 189 | struct ui_state { |
| 190 | uint32_t button; |
| 191 | int down; |
| 192 | |
| 193 | int down_pos[2]; |
| 194 | struct geometry geometry; |
| 195 | }; |
| 196 | |
| 197 | struct 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 Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 206 | struct weston_view view; |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | static void |
| 210 | draw_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 | |
| 220 | static void |
| 221 | draw_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 | |
| 233 | static void |
| 234 | draw_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 | |
| 248 | static void |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 249 | draw_box(cairo_t *cr, pixman_box32_t *box, struct weston_view *view) |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 250 | { |
| 251 | GLfloat x[4], y[4]; |
| 252 | |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 253 | 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 Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 258 | } 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 | |
| 268 | static void |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 269 | draw_geometry(cairo_t *cr, struct weston_view *view, |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 270 | GLfloat *ex, GLfloat *ey, int n) |
| 271 | { |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 272 | struct geometry *g = view->geometry; |
| 273 | float cx, cy; |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 274 | |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 275 | draw_box(cr, &g->surf, view); |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 276 | cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.4); |
| 277 | cairo_fill(cr); |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 278 | weston_view_to_global_float(view, g->surf.x1 - 4, g->surf.y1 - 4, &cx, &cy); |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 279 | cairo_arc(cr, cx, cy, 1.5, 0.0, 2.0 * M_PI); |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 280 | if (view->transform.enabled == 0) |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 281 | 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 Foreman | a0fae46 | 2014-08-18 16:13:41 -0500 | [diff] [blame] | 288 | 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 Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 292 | |
Derek Foreman | a0fae46 | 2014-08-18 16:13:41 -0500 | [diff] [blame] | 293 | cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.5); |
| 294 | draw_polygon_labels(cr, ex, ey, n); |
| 295 | } |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static void |
| 299 | redraw_handler(struct widget *widget, void *data) |
| 300 | { |
| 301 | struct cliptest *cliptest = data; |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 302 | struct geometry *g = cliptest->view.geometry; |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 303 | 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 Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 310 | n = calculate_edges(&cliptest->view, &g->clip, &g->surf, ex, ey); |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 311 | |
| 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 Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 344 | draw_geometry(cr, &cliptest->view, ex, ey, n); |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 345 | 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 | |
| 359 | static int |
| 360 | motion_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 | |
| 392 | static void |
| 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) |
| 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 | |
| 411 | static void |
| 412 | axis_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 Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 423 | cliptest->view.transform.enabled = 1; |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 424 | |
| 425 | widget_schedule_redraw(cliptest->widget); |
| 426 | } |
| 427 | |
| 428 | static void |
| 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) |
| 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 Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 473 | cliptest->view.transform.enabled = 1; |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 474 | break; |
| 475 | case XKB_KEY_m: |
| 476 | geometry_set_phi(g, g->phi - (M_PI / 24.0)); |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 477 | cliptest->view.transform.enabled = 1; |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 478 | break; |
| 479 | case XKB_KEY_r: |
| 480 | geometry_set_phi(g, 0.0); |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 481 | cliptest->view.transform.enabled = 0; |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 482 | break; |
| 483 | default: |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | widget_schedule_redraw(cliptest->widget); |
| 488 | } |
| 489 | |
| 490 | static void |
| 491 | keyboard_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 | |
| 499 | static void |
| 500 | fullscreen_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 | |
| 508 | static struct cliptest * |
| 509 | cliptest_create(struct display *display) |
| 510 | { |
| 511 | struct cliptest *cliptest; |
| 512 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 513 | cliptest = xzalloc(sizeof *cliptest); |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 514 | cliptest->view.geometry = &cliptest->geometry; |
| 515 | cliptest->view.transform.enabled = 0; |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 516 | geometry_init(&cliptest->geometry); |
| 517 | geometry_init(&cliptest->ui.geometry); |
| 518 | |
| 519 | cliptest->window = window_create(display); |
Jason Ekstrand | ee7fefc | 2013-10-13 19:08:38 -0500 | [diff] [blame] | 520 | cliptest->widget = window_frame_create(cliptest->window, cliptest); |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 521 | 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 | |
| 544 | static struct timespec begin_time; |
| 545 | |
| 546 | static void |
| 547 | reset_timer(void) |
| 548 | { |
| 549 | clock_gettime(CLOCK_MONOTONIC, &begin_time); |
| 550 | } |
| 551 | |
| 552 | static double |
| 553 | read_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 | |
| 562 | static int |
| 563 | benchmark(void) |
| 564 | { |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 565 | struct weston_view view; |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 566 | 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 Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 584 | view.transform.enabled = 1; |
| 585 | view.geometry = &geom; |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 586 | |
| 587 | reset_timer(); |
| 588 | for (i = 0; i < N; i++) { |
| 589 | geometry_set_phi(&geom, (float)i / 360.0f); |
Pekka Paalanen | effef73 | 2014-08-20 10:35:26 +0300 | [diff] [blame^] | 590 | calculate_edges(&view, &geom.clip, &geom.surf, ex, ey); |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 591 | } |
| 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 | |
vivek | 31732f7 | 2014-05-15 18:58:16 +0530 | [diff] [blame] | 599 | static void |
| 600 | cliptest_destroy(struct cliptest *cliptest) |
| 601 | { |
| 602 | widget_destroy(cliptest->widget); |
| 603 | window_destroy(cliptest->window); |
| 604 | free(cliptest); |
| 605 | } |
| 606 | |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 607 | int |
| 608 | main(int argc, char *argv[]) |
| 609 | { |
| 610 | struct display *d; |
| 611 | struct cliptest *cliptest; |
| 612 | |
Bill Spitzak | 36bcf47 | 2014-08-08 12:59:56 -0700 | [diff] [blame] | 613 | 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 Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 619 | |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 620 | d = display_create(&argc, argv); |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 621 | 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 | |
vivek | 31732f7 | 2014-05-15 18:58:16 +0530 | [diff] [blame] | 629 | cliptest_destroy(cliptest); |
| 630 | display_destroy(d); |
Pekka Paalanen | 8c492b1 | 2012-09-11 17:02:04 +0300 | [diff] [blame] | 631 | |
| 632 | return 0; |
| 633 | } |