blob: f981a20bc2777f51d2441f79d3f153352d1b3d98 [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>
Antonio Borneo39578632019-04-26 23:57:31 +020031#include <errno.h>
Jonny Lamb92d90f22013-11-26 18:19:48 +010032#include <cairo.h>
33
34#include <linux/input.h>
35
36#include "window.h"
Pekka Paalanen73511002016-04-15 16:53:41 +030037#include "viewporter-client-protocol.h"
Jonny Lamb92d90f22013-11-26 18:19:48 +010038
39#define BUFFER_SCALE 2
40static const int BUFFER_WIDTH = 421 * BUFFER_SCALE;
41static const int BUFFER_HEIGHT = 337 * BUFFER_SCALE;
42static const int SURFACE_WIDTH = 55 * 4;
43static const int SURFACE_HEIGHT = 77 * 4;
44static const double RECT_X = 21 * BUFFER_SCALE; /* buffer coords */
45static const double RECT_Y = 25 * BUFFER_SCALE;
46static const double RECT_W = 55 * BUFFER_SCALE;
47static const double RECT_H = 77 * BUFFER_SCALE;
48
49struct box {
50 struct display *display;
51 struct window *window;
52 struct widget *widget;
53 int width, height;
54
Pekka Paalanen73511002016-04-15 16:53:41 +030055 struct wp_viewporter *viewporter;
56 struct wp_viewport *viewport;
Pekka Paalanen2d9d6892014-03-14 14:38:18 +020057
58 enum {
59 MODE_NO_VIEWPORT,
60 MODE_SRC_ONLY,
61 MODE_DST_ONLY,
62 MODE_SRC_DST
63 } mode;
Jonny Lamb92d90f22013-11-26 18:19:48 +010064};
65
66static void
Pekka Paalanen2d9d6892014-03-14 14:38:18 +020067set_my_viewport(struct box *box)
68{
69 wl_fixed_t src_x, src_y, src_width, src_height;
70 int32_t dst_width = SURFACE_WIDTH;
71 int32_t dst_height = SURFACE_HEIGHT;
72
73 if (box->mode == MODE_NO_VIEWPORT)
74 return;
75
76 /* Cut the green border in half, take white border fully in,
77 * and black border fully out. The borders are 1px wide in buffer.
78 *
79 * The gl-renderer uses linear texture sampling, this means the
80 * top and left edges go to 100% green, bottom goes to 50% blue/black,
81 * right edge has thick white sliding to 50% red.
82 */
83 src_x = wl_fixed_from_double((RECT_X + 0.5) / BUFFER_SCALE);
84 src_y = wl_fixed_from_double((RECT_Y + 0.5) / BUFFER_SCALE);
85 src_width = wl_fixed_from_double((RECT_W - 0.5) / BUFFER_SCALE);
86 src_height = wl_fixed_from_double((RECT_H - 0.5) / BUFFER_SCALE);
87
Dongjin Kimba89f002024-09-30 17:49:36 +090088 switch (box->mode) {
Pekka Paalanen2d9d6892014-03-14 14:38:18 +020089 case MODE_SRC_ONLY:
Derek Foremanc3ded662016-09-30 11:13:23 -050090 /* In SRC_ONLY mode we're just cropping - in order
91 * for the surface size to remain an integer, the
92 * compositor will generate an error if we use a
93 * fractional width or height.
94 *
95 * We use fractional width/height for the other cases
96 * to ensure fractional values are still tested.
97 */
98 src_width = wl_fixed_from_int(RECT_W / BUFFER_SCALE);
99 src_height = wl_fixed_from_int(RECT_H / BUFFER_SCALE);
Pekka Paalanen73511002016-04-15 16:53:41 +0300100 wp_viewport_set_source(box->viewport, src_x, src_y,
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200101 src_width, src_height);
102 break;
103 case MODE_DST_ONLY:
Pekka Paalanen73511002016-04-15 16:53:41 +0300104 wp_viewport_set_destination(box->viewport,
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200105 dst_width, dst_height);
106 break;
107 case MODE_SRC_DST:
Pekka Paalanen73511002016-04-15 16:53:41 +0300108 wp_viewport_set_source(box->viewport, src_x, src_y,
109 src_width, src_height);
110 wp_viewport_set_destination(box->viewport,
111 dst_width, dst_height);
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200112 break;
113 default:
114 assert(!"not reached");
115 }
116}
117
118static void
Jonny Lamb92d90f22013-11-26 18:19:48 +0100119resize_handler(struct widget *widget,
120 int32_t width, int32_t height, void *data)
121{
122 struct box *box = data;
123
Eric Engestromd962be12016-04-02 17:03:15 +0100124 /* Don't resize me */
Jonny Lamb92d90f22013-11-26 18:19:48 +0100125 widget_set_size(box->widget, box->width, box->height);
126}
127
128static void
129redraw_handler(struct widget *widget, void *data)
130{
131 struct box *box = data;
132 cairo_surface_t *surface;
133 cairo_t *cr;
134
135 surface = window_get_surface(box->window);
136 if (surface == NULL ||
137 cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
138 fprintf(stderr, "failed to create cairo egl surface\n");
139 return;
140 }
141
142 cr = cairo_create(surface);
143 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
144 cairo_set_line_width(cr, 1.0);
145 cairo_translate(cr, RECT_X, RECT_Y);
146
147 /* red background */
148 cairo_set_source_rgba(cr, 255, 0, 0, 255);
149 cairo_paint(cr);
150
151 /* blue box */
152 cairo_set_source_rgba(cr, 0, 0, 255, 255);
153 cairo_rectangle(cr, 0, 0, RECT_W, RECT_H);
154 cairo_fill(cr);
155
156 /* black border outside the box */
157 cairo_set_source_rgb(cr, 0, 0, 0);
158 cairo_move_to(cr, 0, RECT_H + 0.5);
159 cairo_line_to(cr, RECT_W, RECT_H + 0.5);
160 cairo_stroke(cr);
161
162 /* white border inside the box */
163 cairo_set_source_rgb(cr, 1, 1, 1);
164 cairo_move_to(cr, RECT_W - 0.5, 0);
165 cairo_line_to(cr, RECT_W - 0.5, RECT_H);
166 cairo_stroke(cr);
167
168 /* the green border on inside the box, to be split half by crop */
169 cairo_set_source_rgb(cr, 0, 1, 0);
170 cairo_move_to(cr, 0.5, RECT_H);
171 cairo_line_to(cr, 0.5, 0);
172 cairo_move_to(cr, 0, 0.5);
173 cairo_line_to(cr, RECT_W, 0.5);
174 cairo_stroke(cr);
175
176 cairo_destroy(cr);
177
178 /* TODO: buffer_transform */
179
180 cairo_surface_destroy(surface);
181}
182
183static void
184global_handler(struct display *display, uint32_t name,
185 const char *interface, uint32_t version, void *data)
186{
187 struct box *box = data;
Jonny Lamb92d90f22013-11-26 18:19:48 +0100188
Pekka Paalanen73511002016-04-15 16:53:41 +0300189 if (strcmp(interface, "wp_viewporter") == 0) {
190 box->viewporter = display_bind(display, name,
191 &wp_viewporter_interface, 1);
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200192
Pekka Paalanen73511002016-04-15 16:53:41 +0300193 box->viewport = wp_viewporter_get_viewport(box->viewporter,
Jonny Lamb92d90f22013-11-26 18:19:48 +0100194 widget_get_wl_surface(box->widget));
195
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200196 set_my_viewport(box);
Jonny Lamb92d90f22013-11-26 18:19:48 +0100197 }
198}
199
200static void
201button_handler(struct widget *widget,
202 struct input *input, uint32_t time,
203 uint32_t button, enum wl_pointer_button_state state, void *data)
204{
205 struct box *box = data;
206
207 if (button != BTN_LEFT)
208 return;
209
210 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
211 window_move(box->window, input,
212 display_get_serial(box->display));
213 }
214}
215
216static void
217touch_down_handler(struct widget *widget, struct input *input,
218 uint32_t serial, uint32_t time, int32_t id,
219 float x, float y, void *data)
220{
221 struct box *box = data;
222 window_move(box->window, input,
223 display_get_serial(box->display));
224}
225
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200226static void
227usage(const char *progname)
228{
229 fprintf(stderr, "Usage: %s [mode]\n"
230 "where 'mode' is one of\n"
231 " -b\tset both src and dst in viewport (default)\n"
232 " -d\tset only dst in viewport\n"
233 " -s\tset only src in viewport\n"
234 " -n\tdo not set viewport at all\n\n",
235 progname);
236
237 fprintf(stderr, "Expected output with output_scale=1:\n");
238
239 fprintf(stderr, "Mode -n:\n"
240 " window size %dx%d px\n"
241 " Red box with a blue box in the upper left part.\n"
242 " The blue box has white right edge, black bottom edge,\n"
243 " and thin green left and top edges that can really\n"
244 " be seen only when zoomed in.\n\n",
245 BUFFER_WIDTH / BUFFER_SCALE, BUFFER_HEIGHT / BUFFER_SCALE);
246
247 fprintf(stderr, "Mode -b:\n"
248 " window size %dx%d px\n"
249 " Blue box with green top and left edge,\n"
250 " thick white right edge with a hint of red,\n"
251 " and a hint of black in bottom edge.\n\n",
252 SURFACE_WIDTH, SURFACE_HEIGHT);
253
254 fprintf(stderr, "Mode -s:\n"
255 " window size %.0fx%.0f px\n"
256 " The same as mode -b, but scaled a lot smaller.\n\n",
257 RECT_W / BUFFER_SCALE, RECT_H / BUFFER_SCALE);
258
259 fprintf(stderr, "Mode -d:\n"
260 " window size %dx%d px\n"
261 " This is horizontally squashed version of the -n mode.\n\n",
262 SURFACE_WIDTH, SURFACE_HEIGHT);
263}
264
Jonny Lamb92d90f22013-11-26 18:19:48 +0100265int
266main(int argc, char *argv[])
267{
268 struct box box;
269 struct display *d;
270 struct timeval tv;
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200271 int i;
Jonny Lamb92d90f22013-11-26 18:19:48 +0100272
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200273 box.mode = MODE_SRC_DST;
274
275 for (i = 1; i < argc; i++) {
276 if (strcmp("-s", argv[i]) == 0)
277 box.mode = MODE_SRC_ONLY;
278 else if (strcmp("-d", argv[i]) == 0)
279 box.mode = MODE_DST_ONLY;
280 else if (strcmp("-b", argv[i]) == 0)
281 box.mode = MODE_SRC_DST;
282 else if (strcmp("-n", argv[i]) == 0)
283 box.mode = MODE_NO_VIEWPORT;
284 else {
285 usage(argv[0]);
286 exit(1);
287 }
288 }
289
Bill Spitzak0fc37862014-08-08 13:00:00 -0700290 d = display_create(&argc, argv);
291 if (d == NULL) {
Antonio Borneo39578632019-04-26 23:57:31 +0200292 fprintf(stderr, "failed to create display: %s\n",
293 strerror(errno));
Bill Spitzak0fc37862014-08-08 13:00:00 -0700294 return -1;
295 }
296
Jonny Lamb92d90f22013-11-26 18:19:48 +0100297 gettimeofday(&tv, NULL);
298 srandom(tv.tv_usec);
299
300 box.width = BUFFER_WIDTH / BUFFER_SCALE;
301 box.height = BUFFER_HEIGHT / BUFFER_SCALE;
302 box.display = d;
303 box.window = window_create(d);
304 box.widget = window_add_widget(box.window, &box);
305 window_set_title(box.window, "Scaler Test Box");
Marius Vladb3544c22021-09-09 13:52:18 +0300306 window_set_appid(box.window, "org.freedesktop.weston.scaler-test-box");
Jonny Lamb92d90f22013-11-26 18:19:48 +0100307 window_set_buffer_scale(box.window, BUFFER_SCALE);
308
309 widget_set_resize_handler(box.widget, resize_handler);
310 widget_set_redraw_handler(box.widget, redraw_handler);
311 widget_set_button_handler(box.widget, button_handler);
312 widget_set_default_cursor(box.widget, CURSOR_HAND1);
313 widget_set_touch_down_handler(box.widget, touch_down_handler);
314
315 window_schedule_resize(box.window, box.width, box.height);
316
317 display_set_user_data(box.display, &box);
318 display_set_global_handler(box.display, global_handler);
319
320 display_run(d);
321
vivek31732f72014-05-15 18:58:16 +0530322 widget_destroy(box.widget);
Jonny Lamb92d90f22013-11-26 18:19:48 +0100323 window_destroy(box.window);
vivek31732f72014-05-15 18:58:16 +0530324 display_destroy(d);
325
Jonny Lamb92d90f22013-11-26 18:19:48 +0100326 return 0;
327}