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
29 #include <linux/input.h>
32 #include "scaler-client-protocol.h"
34 #define BUFFER_SCALE 2
35 static const int BUFFER_WIDTH = 421 * BUFFER_SCALE;
36 static const int BUFFER_HEIGHT = 337 * BUFFER_SCALE;
37 static const int SURFACE_WIDTH = 55 * 4;
38 static const int SURFACE_HEIGHT = 77 * 4;
39 static const double RECT_X = 21 * BUFFER_SCALE; /* buffer coords */
40 static const double RECT_Y = 25 * BUFFER_SCALE;
41 static const double RECT_W = 55 * BUFFER_SCALE;
42 static const double RECT_H = 77 * BUFFER_SCALE;
45 struct display *display;
46 struct window *window;
47 struct widget *widget;
50 struct wl_scaler *scaler;
52 struct wl_viewport *viewport;
63 set_my_viewport(struct box *box)
65 wl_fixed_t src_x, src_y, src_width, src_height;
66 int32_t dst_width = SURFACE_WIDTH;
67 int32_t dst_height = SURFACE_HEIGHT;
69 if (box->mode == MODE_NO_VIEWPORT)
72 /* Cut the green border in half, take white border fully in,
73 * and black border fully out. The borders are 1px wide in buffer.
75 * The gl-renderer uses linear texture sampling, this means the
76 * top and left edges go to 100% green, bottom goes to 50% blue/black,
77 * right edge has thick white sliding to 50% red.
79 src_x = wl_fixed_from_double((RECT_X + 0.5) / BUFFER_SCALE);
80 src_y = wl_fixed_from_double((RECT_Y + 0.5) / BUFFER_SCALE);
81 src_width = wl_fixed_from_double((RECT_W - 0.5) / BUFFER_SCALE);
82 src_height = wl_fixed_from_double((RECT_H - 0.5) / BUFFER_SCALE);
84 if (box->scaler_version < 2 && box->mode != MODE_SRC_DST) {
85 fprintf(stderr, "Error: server's wl_scaler interface version "
86 "%d does not support this mode.\n",
93 wl_viewport_set_source(box->viewport, src_x, src_y,
94 src_width, src_height);
97 wl_viewport_set_destination(box->viewport,
98 dst_width, dst_height);
101 if (box->scaler_version < 2) {
102 wl_viewport_set(box->viewport,
103 src_x, src_y, src_width, src_height,
104 dst_width, dst_height);
106 wl_viewport_set_source(box->viewport, src_x, src_y,
107 src_width, src_height);
108 wl_viewport_set_destination(box->viewport,
109 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;
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, "wl_scaler") == 0) {
189 box->scaler_version = version < 2 ? version : 2;
191 box->scaler = display_bind(display, name,
192 &wl_scaler_interface,
193 box->scaler_version);
195 box->viewport = wl_scaler_get_viewport(box->scaler,
196 widget_get_wl_surface(box->widget));
198 set_my_viewport(box);
203 button_handler(struct widget *widget,
204 struct input *input, uint32_t time,
205 uint32_t button, enum wl_pointer_button_state state, void *data)
207 struct box *box = data;
209 if (button != BTN_LEFT)
212 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
213 window_move(box->window, input,
214 display_get_serial(box->display));
219 touch_down_handler(struct widget *widget, struct input *input,
220 uint32_t serial, uint32_t time, int32_t id,
221 float x, float y, void *data)
223 struct box *box = data;
224 window_move(box->window, input,
225 display_get_serial(box->display));
229 usage(const char *progname)
231 fprintf(stderr, "Usage: %s [mode]\n"
232 "where 'mode' is one of\n"
233 " -b\tset both src and dst in viewport (default)\n"
234 " -d\tset only dst in viewport\n"
235 " -s\tset only src in viewport\n"
236 " -n\tdo not set viewport at all\n\n",
239 fprintf(stderr, "Expected output with output_scale=1:\n");
241 fprintf(stderr, "Mode -n:\n"
242 " window size %dx%d px\n"
243 " Red box with a blue box in the upper left part.\n"
244 " The blue box has white right edge, black bottom edge,\n"
245 " and thin green left and top edges that can really\n"
246 " be seen only when zoomed in.\n\n",
247 BUFFER_WIDTH / BUFFER_SCALE, BUFFER_HEIGHT / BUFFER_SCALE);
249 fprintf(stderr, "Mode -b:\n"
250 " window size %dx%d px\n"
251 " Blue box with green top and left edge,\n"
252 " thick white right edge with a hint of red,\n"
253 " and a hint of black in bottom edge.\n\n",
254 SURFACE_WIDTH, SURFACE_HEIGHT);
256 fprintf(stderr, "Mode -s:\n"
257 " window size %.0fx%.0f px\n"
258 " The same as mode -b, but scaled a lot smaller.\n\n",
259 RECT_W / BUFFER_SCALE, RECT_H / BUFFER_SCALE);
261 fprintf(stderr, "Mode -d:\n"
262 " window size %dx%d px\n"
263 " This is horizontally squashed version of the -n mode.\n\n",
264 SURFACE_WIDTH, SURFACE_HEIGHT);
268 main(int argc, char *argv[])
275 d = display_create(&argc, argv);
277 fprintf(stderr, "failed to create display: %m\n");
281 box.mode = MODE_SRC_DST;
283 for (i = 1; i < argc; i++) {
284 if (strcmp("-s", argv[i]) == 0)
285 box.mode = MODE_SRC_ONLY;
286 else if (strcmp("-d", argv[i]) == 0)
287 box.mode = MODE_DST_ONLY;
288 else if (strcmp("-b", argv[i]) == 0)
289 box.mode = MODE_SRC_DST;
290 else if (strcmp("-n", argv[i]) == 0)
291 box.mode = MODE_NO_VIEWPORT;
298 gettimeofday(&tv, NULL);
301 box.width = BUFFER_WIDTH / BUFFER_SCALE;
302 box.height = BUFFER_HEIGHT / BUFFER_SCALE;
304 box.window = window_create(d);
305 box.widget = window_add_widget(box.window, &box);
306 window_set_title(box.window, "Scaler Test Box");
307 window_set_buffer_scale(box.window, BUFFER_SCALE);
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);
315 window_schedule_resize(box.window, box.width, box.height);
317 display_set_user_data(box.display, &box);
318 display_set_global_handler(box.display, global_handler);
322 window_destroy(box.window);