2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2013 Collabora, Ltd.
5 * 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:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
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.
33 #include <linux/input.h>
36 #include "viewporter-client-protocol.h"
38 #define BUFFER_SCALE 2
39 static const int BUFFER_WIDTH = 421 * BUFFER_SCALE;
40 static const int BUFFER_HEIGHT = 337 * BUFFER_SCALE;
41 static const int SURFACE_WIDTH = 55 * 4;
42 static const int SURFACE_HEIGHT = 77 * 4;
43 static const double RECT_X = 21 * BUFFER_SCALE; /* buffer coords */
44 static const double RECT_Y = 25 * BUFFER_SCALE;
45 static const double RECT_W = 55 * BUFFER_SCALE;
46 static const double RECT_H = 77 * BUFFER_SCALE;
49 struct display *display;
50 struct window *window;
51 struct widget *widget;
54 struct wp_viewporter *viewporter;
55 struct wp_viewport *viewport;
66 set_my_viewport(struct box *box)
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;
72 if (box->mode == MODE_NO_VIEWPORT)
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.
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.
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);
89 /* 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.
94 * We use fractional width/height for the other cases
95 * to ensure fractional values are still tested.
97 src_width = wl_fixed_from_int(RECT_W / BUFFER_SCALE);
98 src_height = wl_fixed_from_int(RECT_H / BUFFER_SCALE);
99 wp_viewport_set_source(box->viewport, src_x, src_y,
100 src_width, src_height);
103 wp_viewport_set_destination(box->viewport,
104 dst_width, dst_height);
107 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);
113 assert(!"not reached");
118 resize_handler(struct widget *widget,
119 int32_t width, int32_t height, void *data)
121 struct box *box = data;
123 /* Don't resize me */
124 widget_set_size(box->widget, box->width, box->height);
128 redraw_handler(struct widget *widget, void *data)
130 struct box *box = data;
131 cairo_surface_t *surface;
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");
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);
147 cairo_set_source_rgba(cr, 255, 0, 0, 255);
151 cairo_set_source_rgba(cr, 0, 0, 255, 255);
152 cairo_rectangle(cr, 0, 0, RECT_W, RECT_H);
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);
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);
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);
177 /* TODO: buffer_transform */
179 cairo_surface_destroy(surface);
183 global_handler(struct display *display, uint32_t name,
184 const char *interface, uint32_t version, void *data)
186 struct box *box = data;
188 if (strcmp(interface, "wp_viewporter") == 0) {
189 box->viewporter = display_bind(display, name,
190 &wp_viewporter_interface, 1);
192 box->viewport = wp_viewporter_get_viewport(box->viewporter,
193 widget_get_wl_surface(box->widget));
195 set_my_viewport(box);
200 button_handler(struct widget *widget,
201 struct input *input, uint32_t time,
202 uint32_t button, enum wl_pointer_button_state state, void *data)
204 struct box *box = data;
206 if (button != BTN_LEFT)
209 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
210 window_move(box->window, input,
211 display_get_serial(box->display));
216 touch_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)
220 struct box *box = data;
221 window_move(box->window, input,
222 display_get_serial(box->display));
226 usage(const char *progname)
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",
236 fprintf(stderr, "Expected output with output_scale=1:\n");
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);
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);
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);
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);
265 main(int argc, char *argv[])
272 box.mode = MODE_SRC_DST;
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;
289 d = display_create(&argc, argv);
291 fprintf(stderr, "failed to create display: %m\n");
295 gettimeofday(&tv, NULL);
298 box.width = BUFFER_WIDTH / BUFFER_SCALE;
299 box.height = BUFFER_HEIGHT / BUFFER_SCALE;
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);
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);
312 window_schedule_resize(box.window, box.width, box.height);
314 display_set_user_data(box.display, &box);
315 display_set_global_handler(box.display, global_handler);
319 widget_destroy(box.widget);
320 window_destroy(box.window);