blob: 23cc3a495c744fabab2e4f1e64e4b6dd645e282e [file] [log] [blame]
Jonny Lamb92d90f22013-11-26 18:19:48 +01001/*
2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2013 Collabora, Ltd.
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:
Jonny Lamb92d90f22013-11-26 18:19:48 +010011 *
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.
Jonny Lamb92d90f22013-11-26 18:19:48 +010023 */
24
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010025#include "config.h"
26
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030027#include <stdint.h>
Jonny Lamb92d90f22013-11-26 18:19:48 +010028#include <stdio.h>
29#include <string.h>
Pekka Paalanen2d9d6892014-03-14 14:38:18 +020030#include <assert.h>
Jonny Lamb92d90f22013-11-26 18:19:48 +010031#include <cairo.h>
32
33#include <linux/input.h>
34
35#include "window.h"
Pekka Paalanen73511002016-04-15 16:53:41 +030036#include "viewporter-client-protocol.h"
Jonny Lamb92d90f22013-11-26 18:19:48 +010037
38#define BUFFER_SCALE 2
39static const int BUFFER_WIDTH = 421 * BUFFER_SCALE;
40static const int BUFFER_HEIGHT = 337 * BUFFER_SCALE;
41static const int SURFACE_WIDTH = 55 * 4;
42static const int SURFACE_HEIGHT = 77 * 4;
43static const double RECT_X = 21 * BUFFER_SCALE; /* buffer coords */
44static const double RECT_Y = 25 * BUFFER_SCALE;
45static const double RECT_W = 55 * BUFFER_SCALE;
46static const double RECT_H = 77 * BUFFER_SCALE;
47
48struct box {
49 struct display *display;
50 struct window *window;
51 struct widget *widget;
52 int width, height;
53
Pekka Paalanen73511002016-04-15 16:53:41 +030054 struct wp_viewporter *viewporter;
55 struct wp_viewport *viewport;
Pekka Paalanen2d9d6892014-03-14 14:38:18 +020056
57 enum {
58 MODE_NO_VIEWPORT,
59 MODE_SRC_ONLY,
60 MODE_DST_ONLY,
61 MODE_SRC_DST
62 } mode;
Jonny Lamb92d90f22013-11-26 18:19:48 +010063};
64
65static void
Pekka Paalanen2d9d6892014-03-14 14:38:18 +020066set_my_viewport(struct box *box)
67{
68 wl_fixed_t src_x, src_y, src_width, src_height;
69 int32_t dst_width = SURFACE_WIDTH;
70 int32_t dst_height = SURFACE_HEIGHT;
71
72 if (box->mode == MODE_NO_VIEWPORT)
73 return;
74
75 /* Cut the green border in half, take white border fully in,
76 * and black border fully out. The borders are 1px wide in buffer.
77 *
78 * The gl-renderer uses linear texture sampling, this means the
79 * top and left edges go to 100% green, bottom goes to 50% blue/black,
80 * right edge has thick white sliding to 50% red.
81 */
82 src_x = wl_fixed_from_double((RECT_X + 0.5) / BUFFER_SCALE);
83 src_y = wl_fixed_from_double((RECT_Y + 0.5) / BUFFER_SCALE);
84 src_width = wl_fixed_from_double((RECT_W - 0.5) / BUFFER_SCALE);
85 src_height = wl_fixed_from_double((RECT_H - 0.5) / BUFFER_SCALE);
86
Pekka Paalanen2d9d6892014-03-14 14:38:18 +020087 switch (box->mode){
88 case MODE_SRC_ONLY:
Derek Foremanc3ded662016-09-30 11:13:23 -050089 /* In SRC_ONLY mode we're just cropping - in order
90 * for the surface size to remain an integer, the
91 * compositor will generate an error if we use a
92 * fractional width or height.
93 *
94 * We use fractional width/height for the other cases
95 * to ensure fractional values are still tested.
96 */
97 src_width = wl_fixed_from_int(RECT_W / BUFFER_SCALE);
98 src_height = wl_fixed_from_int(RECT_H / BUFFER_SCALE);
Pekka Paalanen73511002016-04-15 16:53:41 +030099 wp_viewport_set_source(box->viewport, src_x, src_y,
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200100 src_width, src_height);
101 break;
102 case MODE_DST_ONLY:
Pekka Paalanen73511002016-04-15 16:53:41 +0300103 wp_viewport_set_destination(box->viewport,
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200104 dst_width, dst_height);
105 break;
106 case MODE_SRC_DST:
Pekka Paalanen73511002016-04-15 16:53:41 +0300107 wp_viewport_set_source(box->viewport, src_x, src_y,
108 src_width, src_height);
109 wp_viewport_set_destination(box->viewport,
110 dst_width, dst_height);
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200111 break;
112 default:
113 assert(!"not reached");
114 }
115}
116
117static void
Jonny Lamb92d90f22013-11-26 18:19:48 +0100118resize_handler(struct widget *widget,
119 int32_t width, int32_t height, void *data)
120{
121 struct box *box = data;
122
Eric Engestromd962be12016-04-02 17:03:15 +0100123 /* Don't resize me */
Jonny Lamb92d90f22013-11-26 18:19:48 +0100124 widget_set_size(box->widget, box->width, box->height);
125}
126
127static void
128redraw_handler(struct widget *widget, void *data)
129{
130 struct box *box = data;
131 cairo_surface_t *surface;
132 cairo_t *cr;
133
134 surface = window_get_surface(box->window);
135 if (surface == NULL ||
136 cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
137 fprintf(stderr, "failed to create cairo egl surface\n");
138 return;
139 }
140
141 cr = cairo_create(surface);
142 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
143 cairo_set_line_width(cr, 1.0);
144 cairo_translate(cr, RECT_X, RECT_Y);
145
146 /* red background */
147 cairo_set_source_rgba(cr, 255, 0, 0, 255);
148 cairo_paint(cr);
149
150 /* blue box */
151 cairo_set_source_rgba(cr, 0, 0, 255, 255);
152 cairo_rectangle(cr, 0, 0, RECT_W, RECT_H);
153 cairo_fill(cr);
154
155 /* black border outside the box */
156 cairo_set_source_rgb(cr, 0, 0, 0);
157 cairo_move_to(cr, 0, RECT_H + 0.5);
158 cairo_line_to(cr, RECT_W, RECT_H + 0.5);
159 cairo_stroke(cr);
160
161 /* white border inside the box */
162 cairo_set_source_rgb(cr, 1, 1, 1);
163 cairo_move_to(cr, RECT_W - 0.5, 0);
164 cairo_line_to(cr, RECT_W - 0.5, RECT_H);
165 cairo_stroke(cr);
166
167 /* the green border on inside the box, to be split half by crop */
168 cairo_set_source_rgb(cr, 0, 1, 0);
169 cairo_move_to(cr, 0.5, RECT_H);
170 cairo_line_to(cr, 0.5, 0);
171 cairo_move_to(cr, 0, 0.5);
172 cairo_line_to(cr, RECT_W, 0.5);
173 cairo_stroke(cr);
174
175 cairo_destroy(cr);
176
177 /* TODO: buffer_transform */
178
179 cairo_surface_destroy(surface);
180}
181
182static void
183global_handler(struct display *display, uint32_t name,
184 const char *interface, uint32_t version, void *data)
185{
186 struct box *box = data;
Jonny Lamb92d90f22013-11-26 18:19:48 +0100187
Pekka Paalanen73511002016-04-15 16:53:41 +0300188 if (strcmp(interface, "wp_viewporter") == 0) {
189 box->viewporter = display_bind(display, name,
190 &wp_viewporter_interface, 1);
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200191
Pekka Paalanen73511002016-04-15 16:53:41 +0300192 box->viewport = wp_viewporter_get_viewport(box->viewporter,
Jonny Lamb92d90f22013-11-26 18:19:48 +0100193 widget_get_wl_surface(box->widget));
194
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200195 set_my_viewport(box);
Jonny Lamb92d90f22013-11-26 18:19:48 +0100196 }
197}
198
199static void
200button_handler(struct widget *widget,
201 struct input *input, uint32_t time,
202 uint32_t button, enum wl_pointer_button_state state, void *data)
203{
204 struct box *box = data;
205
206 if (button != BTN_LEFT)
207 return;
208
209 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
210 window_move(box->window, input,
211 display_get_serial(box->display));
212 }
213}
214
215static void
216touch_down_handler(struct widget *widget, struct input *input,
217 uint32_t serial, uint32_t time, int32_t id,
218 float x, float y, void *data)
219{
220 struct box *box = data;
221 window_move(box->window, input,
222 display_get_serial(box->display));
223}
224
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200225static void
226usage(const char *progname)
227{
228 fprintf(stderr, "Usage: %s [mode]\n"
229 "where 'mode' is one of\n"
230 " -b\tset both src and dst in viewport (default)\n"
231 " -d\tset only dst in viewport\n"
232 " -s\tset only src in viewport\n"
233 " -n\tdo not set viewport at all\n\n",
234 progname);
235
236 fprintf(stderr, "Expected output with output_scale=1:\n");
237
238 fprintf(stderr, "Mode -n:\n"
239 " window size %dx%d px\n"
240 " Red box with a blue box in the upper left part.\n"
241 " The blue box has white right edge, black bottom edge,\n"
242 " and thin green left and top edges that can really\n"
243 " be seen only when zoomed in.\n\n",
244 BUFFER_WIDTH / BUFFER_SCALE, BUFFER_HEIGHT / BUFFER_SCALE);
245
246 fprintf(stderr, "Mode -b:\n"
247 " window size %dx%d px\n"
248 " Blue box with green top and left edge,\n"
249 " thick white right edge with a hint of red,\n"
250 " and a hint of black in bottom edge.\n\n",
251 SURFACE_WIDTH, SURFACE_HEIGHT);
252
253 fprintf(stderr, "Mode -s:\n"
254 " window size %.0fx%.0f px\n"
255 " The same as mode -b, but scaled a lot smaller.\n\n",
256 RECT_W / BUFFER_SCALE, RECT_H / BUFFER_SCALE);
257
258 fprintf(stderr, "Mode -d:\n"
259 " window size %dx%d px\n"
260 " This is horizontally squashed version of the -n mode.\n\n",
261 SURFACE_WIDTH, SURFACE_HEIGHT);
262}
263
Jonny Lamb92d90f22013-11-26 18:19:48 +0100264int
265main(int argc, char *argv[])
266{
267 struct box box;
268 struct display *d;
269 struct timeval tv;
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200270 int i;
Jonny Lamb92d90f22013-11-26 18:19:48 +0100271
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200272 box.mode = MODE_SRC_DST;
273
274 for (i = 1; i < argc; i++) {
275 if (strcmp("-s", argv[i]) == 0)
276 box.mode = MODE_SRC_ONLY;
277 else if (strcmp("-d", argv[i]) == 0)
278 box.mode = MODE_DST_ONLY;
279 else if (strcmp("-b", argv[i]) == 0)
280 box.mode = MODE_SRC_DST;
281 else if (strcmp("-n", argv[i]) == 0)
282 box.mode = MODE_NO_VIEWPORT;
283 else {
284 usage(argv[0]);
285 exit(1);
286 }
287 }
288
Bill Spitzak0fc37862014-08-08 13:00:00 -0700289 d = display_create(&argc, argv);
290 if (d == NULL) {
291 fprintf(stderr, "failed to create display: %m\n");
292 return -1;
293 }
294
Jonny Lamb92d90f22013-11-26 18:19:48 +0100295 gettimeofday(&tv, NULL);
296 srandom(tv.tv_usec);
297
298 box.width = BUFFER_WIDTH / BUFFER_SCALE;
299 box.height = BUFFER_HEIGHT / BUFFER_SCALE;
300 box.display = d;
301 box.window = window_create(d);
302 box.widget = window_add_widget(box.window, &box);
303 window_set_title(box.window, "Scaler Test Box");
304 window_set_buffer_scale(box.window, BUFFER_SCALE);
305
306 widget_set_resize_handler(box.widget, resize_handler);
307 widget_set_redraw_handler(box.widget, redraw_handler);
308 widget_set_button_handler(box.widget, button_handler);
309 widget_set_default_cursor(box.widget, CURSOR_HAND1);
310 widget_set_touch_down_handler(box.widget, touch_down_handler);
311
312 window_schedule_resize(box.window, box.width, box.height);
313
314 display_set_user_data(box.display, &box);
315 display_set_global_handler(box.display, global_handler);
316
317 display_run(d);
318
vivek31732f72014-05-15 18:58:16 +0530319 widget_destroy(box.widget);
Jonny Lamb92d90f22013-11-26 18:19:48 +0100320 window_destroy(box.window);
vivek31732f72014-05-15 18:58:16 +0530321 display_destroy(d);
322
Jonny Lamb92d90f22013-11-26 18:19:48 +0100323 return 0;
324}