blob: 2a43fcfaacf0e2aeacd816a448d800182b75d2aa [file] [log] [blame]
Jonny Lamb92d90f22013-11-26 18:19:48 +01001/*
2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2013 Collabora, Ltd.
4 *
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.
14 *
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
21 * OF THIS SOFTWARE.
22 */
23
24#include <stdio.h>
25#include <string.h>
Pekka Paalanen2d9d6892014-03-14 14:38:18 +020026#include <assert.h>
Jonny Lamb92d90f22013-11-26 18:19:48 +010027#include <cairo.h>
28
29#include <linux/input.h>
30
31#include "window.h"
32#include "scaler-client-protocol.h"
33
34#define BUFFER_SCALE 2
35static const int BUFFER_WIDTH = 421 * BUFFER_SCALE;
36static const int BUFFER_HEIGHT = 337 * BUFFER_SCALE;
37static const int SURFACE_WIDTH = 55 * 4;
38static const int SURFACE_HEIGHT = 77 * 4;
39static const double RECT_X = 21 * BUFFER_SCALE; /* buffer coords */
40static const double RECT_Y = 25 * BUFFER_SCALE;
41static const double RECT_W = 55 * BUFFER_SCALE;
42static const double RECT_H = 77 * BUFFER_SCALE;
43
44struct box {
45 struct display *display;
46 struct window *window;
47 struct widget *widget;
48 int width, height;
49
50 struct wl_scaler *scaler;
Pekka Paalanen2d9d6892014-03-14 14:38:18 +020051 int scaler_version;
Pekka Paalanenb0420ae2014-01-08 15:39:17 +020052 struct wl_viewport *viewport;
Pekka Paalanen2d9d6892014-03-14 14:38:18 +020053
54 enum {
55 MODE_NO_VIEWPORT,
56 MODE_SRC_ONLY,
57 MODE_DST_ONLY,
58 MODE_SRC_DST
59 } mode;
Jonny Lamb92d90f22013-11-26 18:19:48 +010060};
61
62static void
Pekka Paalanen2d9d6892014-03-14 14:38:18 +020063set_my_viewport(struct box *box)
64{
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;
68
69 if (box->mode == MODE_NO_VIEWPORT)
70 return;
71
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.
74 *
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.
78 */
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);
83
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",
87 box->scaler_version);
88 exit(1);
89 }
90
91 switch (box->mode){
92 case MODE_SRC_ONLY:
93 wl_viewport_set_source(box->viewport, src_x, src_y,
94 src_width, src_height);
95 break;
96 case MODE_DST_ONLY:
97 wl_viewport_set_destination(box->viewport,
98 dst_width, dst_height);
99 break;
100 case MODE_SRC_DST:
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);
105 } else {
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);
110 }
111 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
123 /* Dont resize me */
124 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
188 if (strcmp(interface, "wl_scaler") == 0) {
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200189 box->scaler_version = version < 2 ? version : 2;
190
Jonny Lamb92d90f22013-11-26 18:19:48 +0100191 box->scaler = display_bind(display, name,
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200192 &wl_scaler_interface,
193 box->scaler_version);
Jonny Lamb92d90f22013-11-26 18:19:48 +0100194
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200195 box->viewport = wl_scaler_get_viewport(box->scaler,
Jonny Lamb92d90f22013-11-26 18:19:48 +0100196 widget_get_wl_surface(box->widget));
197
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200198 set_my_viewport(box);
Jonny Lamb92d90f22013-11-26 18:19:48 +0100199 }
200}
201
202static void
203button_handler(struct widget *widget,
204 struct input *input, uint32_t time,
205 uint32_t button, enum wl_pointer_button_state state, void *data)
206{
207 struct box *box = data;
208
209 if (button != BTN_LEFT)
210 return;
211
212 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
213 window_move(box->window, input,
214 display_get_serial(box->display));
215 }
216}
217
218static void
219touch_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)
222{
223 struct box *box = data;
224 window_move(box->window, input,
225 display_get_serial(box->display));
226}
227
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200228static void
229usage(const char *progname)
230{
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",
237 progname);
238
239 fprintf(stderr, "Expected output with output_scale=1:\n");
240
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);
248
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);
255
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);
260
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);
265}
266
Jonny Lamb92d90f22013-11-26 18:19:48 +0100267int
268main(int argc, char *argv[])
269{
270 struct box box;
271 struct display *d;
272 struct timeval tv;
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200273 int i;
Jonny Lamb92d90f22013-11-26 18:19:48 +0100274
275 d = display_create(&argc, argv);
276 if (d == NULL) {
277 fprintf(stderr, "failed to create display: %m\n");
278 return -1;
279 }
280
Pekka Paalanen2d9d6892014-03-14 14:38:18 +0200281 box.mode = MODE_SRC_DST;
282
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;
292 else {
293 usage(argv[0]);
294 exit(1);
295 }
296 }
297
Jonny Lamb92d90f22013-11-26 18:19:48 +0100298 gettimeofday(&tv, NULL);
299 srandom(tv.tv_usec);
300
301 box.width = BUFFER_WIDTH / BUFFER_SCALE;
302 box.height = BUFFER_HEIGHT / BUFFER_SCALE;
303 box.display = d;
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);
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
322 window_destroy(box.window);
323 return 0;
324}