2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2013 Collabora, Ltd.
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.
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
31 #include <linux/input.h>
34 #include "scaler-client-protocol.h"
36 #define BUFFER_SCALE 2
37 static const int BUFFER_WIDTH = 421 * BUFFER_SCALE;
38 static const int BUFFER_HEIGHT = 337 * BUFFER_SCALE;
39 static const int SURFACE_WIDTH = 55 * 4;
40 static const int SURFACE_HEIGHT = 77 * 4;
41 static const double RECT_X = 21 * BUFFER_SCALE; /* buffer coords */
42 static const double RECT_Y = 25 * BUFFER_SCALE;
43 static const double RECT_W = 55 * BUFFER_SCALE;
44 static const double RECT_H = 77 * BUFFER_SCALE;
47 struct display *display;
48 struct window *window;
49 struct widget *widget;
52 struct wl_scaler *scaler;
54 struct wl_viewport *viewport;
65 set_my_viewport(struct box *box)
67 wl_fixed_t src_x, src_y, src_width, src_height;
68 int32_t dst_width = SURFACE_WIDTH;
69 int32_t dst_height = SURFACE_HEIGHT;
71 if (box->mode == MODE_NO_VIEWPORT)
74 /* Cut the green border in half, take white border fully in,
75 * and black border fully out. The borders are 1px wide in buffer.
77 * The gl-renderer uses linear texture sampling, this means the
78 * top and left edges go to 100% green, bottom goes to 50% blue/black,
79 * right edge has thick white sliding to 50% red.
81 src_x = wl_fixed_from_double((RECT_X + 0.5) / BUFFER_SCALE);
82 src_y = wl_fixed_from_double((RECT_Y + 0.5) / BUFFER_SCALE);
83 src_width = wl_fixed_from_double((RECT_W - 0.5) / BUFFER_SCALE);
84 src_height = wl_fixed_from_double((RECT_H - 0.5) / BUFFER_SCALE);
86 if (box->scaler_version < 2 && box->mode != MODE_SRC_DST) {
87 fprintf(stderr, "Error: server's wl_scaler interface version "
88 "%d does not support this mode.\n",
95 wl_viewport_set_source(box->viewport, src_x, src_y,
96 src_width, src_height);
99 wl_viewport_set_destination(box->viewport,
100 dst_width, dst_height);
103 if (box->scaler_version < 2) {
104 wl_viewport_set(box->viewport,
105 src_x, src_y, src_width, src_height,
106 dst_width, dst_height);
108 wl_viewport_set_source(box->viewport, src_x, src_y,
109 src_width, src_height);
110 wl_viewport_set_destination(box->viewport,
111 dst_width, dst_height);
115 assert(!"not reached");
120 resize_handler(struct widget *widget,
121 int32_t width, int32_t height, void *data)
123 struct box *box = data;
126 widget_set_size(box->widget, box->width, box->height);
130 redraw_handler(struct widget *widget, void *data)
132 struct box *box = data;
133 cairo_surface_t *surface;
136 surface = window_get_surface(box->window);
137 if (surface == NULL ||
138 cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
139 fprintf(stderr, "failed to create cairo egl surface\n");
143 cr = cairo_create(surface);
144 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
145 cairo_set_line_width(cr, 1.0);
146 cairo_translate(cr, RECT_X, RECT_Y);
149 cairo_set_source_rgba(cr, 255, 0, 0, 255);
153 cairo_set_source_rgba(cr, 0, 0, 255, 255);
154 cairo_rectangle(cr, 0, 0, RECT_W, RECT_H);
157 /* black border outside the box */
158 cairo_set_source_rgb(cr, 0, 0, 0);
159 cairo_move_to(cr, 0, RECT_H + 0.5);
160 cairo_line_to(cr, RECT_W, RECT_H + 0.5);
163 /* white border inside the box */
164 cairo_set_source_rgb(cr, 1, 1, 1);
165 cairo_move_to(cr, RECT_W - 0.5, 0);
166 cairo_line_to(cr, RECT_W - 0.5, RECT_H);
169 /* the green border on inside the box, to be split half by crop */
170 cairo_set_source_rgb(cr, 0, 1, 0);
171 cairo_move_to(cr, 0.5, RECT_H);
172 cairo_line_to(cr, 0.5, 0);
173 cairo_move_to(cr, 0, 0.5);
174 cairo_line_to(cr, RECT_W, 0.5);
179 /* TODO: buffer_transform */
181 cairo_surface_destroy(surface);
185 global_handler(struct display *display, uint32_t name,
186 const char *interface, uint32_t version, void *data)
188 struct box *box = data;
190 if (strcmp(interface, "wl_scaler") == 0) {
191 box->scaler_version = version < 2 ? version : 2;
193 box->scaler = display_bind(display, name,
194 &wl_scaler_interface,
195 box->scaler_version);
197 box->viewport = wl_scaler_get_viewport(box->scaler,
198 widget_get_wl_surface(box->widget));
200 set_my_viewport(box);
205 button_handler(struct widget *widget,
206 struct input *input, uint32_t time,
207 uint32_t button, enum wl_pointer_button_state state, void *data)
209 struct box *box = data;
211 if (button != BTN_LEFT)
214 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
215 window_move(box->window, input,
216 display_get_serial(box->display));
221 touch_down_handler(struct widget *widget, struct input *input,
222 uint32_t serial, uint32_t time, int32_t id,
223 float x, float y, void *data)
225 struct box *box = data;
226 window_move(box->window, input,
227 display_get_serial(box->display));
231 usage(const char *progname)
233 fprintf(stderr, "Usage: %s [mode]\n"
234 "where 'mode' is one of\n"
235 " -b\tset both src and dst in viewport (default)\n"
236 " -d\tset only dst in viewport\n"
237 " -s\tset only src in viewport\n"
238 " -n\tdo not set viewport at all\n\n",
241 fprintf(stderr, "Expected output with output_scale=1:\n");
243 fprintf(stderr, "Mode -n:\n"
244 " window size %dx%d px\n"
245 " Red box with a blue box in the upper left part.\n"
246 " The blue box has white right edge, black bottom edge,\n"
247 " and thin green left and top edges that can really\n"
248 " be seen only when zoomed in.\n\n",
249 BUFFER_WIDTH / BUFFER_SCALE, BUFFER_HEIGHT / BUFFER_SCALE);
251 fprintf(stderr, "Mode -b:\n"
252 " window size %dx%d px\n"
253 " Blue box with green top and left edge,\n"
254 " thick white right edge with a hint of red,\n"
255 " and a hint of black in bottom edge.\n\n",
256 SURFACE_WIDTH, SURFACE_HEIGHT);
258 fprintf(stderr, "Mode -s:\n"
259 " window size %.0fx%.0f px\n"
260 " The same as mode -b, but scaled a lot smaller.\n\n",
261 RECT_W / BUFFER_SCALE, RECT_H / BUFFER_SCALE);
263 fprintf(stderr, "Mode -d:\n"
264 " window size %dx%d px\n"
265 " This is horizontally squashed version of the -n mode.\n\n",
266 SURFACE_WIDTH, SURFACE_HEIGHT);
270 main(int argc, char *argv[])
277 d = display_create(&argc, argv);
279 fprintf(stderr, "failed to create display: %m\n");
283 box.mode = MODE_SRC_DST;
285 for (i = 1; i < argc; i++) {
286 if (strcmp("-s", argv[i]) == 0)
287 box.mode = MODE_SRC_ONLY;
288 else if (strcmp("-d", argv[i]) == 0)
289 box.mode = MODE_DST_ONLY;
290 else if (strcmp("-b", argv[i]) == 0)
291 box.mode = MODE_SRC_DST;
292 else if (strcmp("-n", argv[i]) == 0)
293 box.mode = MODE_NO_VIEWPORT;
300 gettimeofday(&tv, NULL);
303 box.width = BUFFER_WIDTH / BUFFER_SCALE;
304 box.height = BUFFER_HEIGHT / BUFFER_SCALE;
306 box.window = window_create(d);
307 box.widget = window_add_widget(box.window, &box);
308 window_set_title(box.window, "Scaler Test Box");
309 window_set_buffer_scale(box.window, BUFFER_SCALE);
311 widget_set_resize_handler(box.widget, resize_handler);
312 widget_set_redraw_handler(box.widget, redraw_handler);
313 widget_set_button_handler(box.widget, button_handler);
314 widget_set_default_cursor(box.widget, CURSOR_HAND1);
315 widget_set_touch_down_handler(box.widget, touch_down_handler);
317 window_schedule_resize(box.window, box.width, box.height);
319 display_set_user_data(box.display, &box);
320 display_set_global_handler(box.display, global_handler);
324 widget_destroy(box.widget);
325 window_destroy(box.window);