1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  * Copyright © 2013 Collabora, Ltd.
4  *
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:
11  *
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
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.
23  */
24 
25 #include "config.h"
26 
27 #include <stdio.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <cairo.h>
31 
32 #include <linux/input.h>
33 
34 #include "window.h"
35 #include "scaler-client-protocol.h"
36 
37 #define BUFFER_SCALE 2
38 static const int BUFFER_WIDTH = 421 * BUFFER_SCALE;
39 static const int BUFFER_HEIGHT = 337 * BUFFER_SCALE;
40 static const int SURFACE_WIDTH = 55 * 4;
41 static const int SURFACE_HEIGHT = 77 * 4;
42 static const double RECT_X = 21 * BUFFER_SCALE; /* buffer coords */
43 static const double RECT_Y = 25 * BUFFER_SCALE;
44 static const double RECT_W = 55 * BUFFER_SCALE;
45 static const double RECT_H = 77 * BUFFER_SCALE;
46 
47 struct box {
48 	struct display *display;
49 	struct window *window;
50 	struct widget *widget;
51 	int width, height;
52 
53 	struct wl_scaler *scaler;
54 	int scaler_version;
55 	struct wl_viewport *viewport;
56 
57 	enum {
58 		MODE_NO_VIEWPORT,
59 		MODE_SRC_ONLY,
60 		MODE_DST_ONLY,
61 		MODE_SRC_DST
62 	} mode;
63 };
64 
65 static void
set_my_viewport(struct box * box)66 set_my_viewport(struct box *box)
67 {
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;
71 
72 	if (box->mode == MODE_NO_VIEWPORT)
73 		return;
74 
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.
77 	 *
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.
81 	 */
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);
86 
87 	if (box->scaler_version < 2 && box->mode != MODE_SRC_DST) {
88 		fprintf(stderr, "Error: server's wl_scaler interface version "
89 			"%d does not support this mode.\n",
90 			box->scaler_version);
91 		exit(1);
92 	}
93 
94 	switch (box->mode){
95 	case MODE_SRC_ONLY:
96 		wl_viewport_set_source(box->viewport, src_x, src_y,
97 				       src_width, src_height);
98 		break;
99 	case MODE_DST_ONLY:
100 		wl_viewport_set_destination(box->viewport,
101 					    dst_width, dst_height);
102 		break;
103 	case MODE_SRC_DST:
104 		if (box->scaler_version < 2) {
105 			wl_viewport_set(box->viewport,
106 					src_x, src_y, src_width, src_height,
107 					dst_width, dst_height);
108 		} else {
109 			wl_viewport_set_source(box->viewport, src_x, src_y,
110 					       src_width, src_height);
111 			wl_viewport_set_destination(box->viewport,
112 						    dst_width, dst_height);
113 		}
114 		break;
115 	default:
116 		assert(!"not reached");
117 	}
118 }
119 
120 static void
resize_handler(struct widget * widget,int32_t width,int32_t height,void * data)121 resize_handler(struct widget *widget,
122 	       int32_t width, int32_t height, void *data)
123 {
124 	struct box *box = data;
125 
126 	/* Dont resize me */
127 	widget_set_size(box->widget, box->width, box->height);
128 }
129 
130 static void
redraw_handler(struct widget * widget,void * data)131 redraw_handler(struct widget *widget, void *data)
132 {
133 	struct box *box = data;
134 	cairo_surface_t *surface;
135 	cairo_t *cr;
136 
137 	surface = window_get_surface(box->window);
138 	if (surface == NULL ||
139 	    cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
140 		fprintf(stderr, "failed to create cairo egl surface\n");
141 		return;
142 	}
143 
144 	cr = cairo_create(surface);
145 	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
146 	cairo_set_line_width(cr, 1.0);
147 	cairo_translate(cr, RECT_X, RECT_Y);
148 
149 	/* red background */
150 	cairo_set_source_rgba(cr, 255, 0, 0, 255);
151 	cairo_paint(cr);
152 
153 	/* blue box */
154 	cairo_set_source_rgba(cr, 0, 0, 255, 255);
155 	cairo_rectangle(cr, 0, 0, RECT_W, RECT_H);
156 	cairo_fill(cr);
157 
158 	/* black border outside the box */
159 	cairo_set_source_rgb(cr, 0, 0, 0);
160 	cairo_move_to(cr, 0, RECT_H + 0.5);
161 	cairo_line_to(cr, RECT_W, RECT_H + 0.5);
162 	cairo_stroke(cr);
163 
164 	/* white border inside the box */
165 	cairo_set_source_rgb(cr, 1, 1, 1);
166 	cairo_move_to(cr, RECT_W - 0.5, 0);
167 	cairo_line_to(cr, RECT_W - 0.5, RECT_H);
168 	cairo_stroke(cr);
169 
170 	/* the green border on inside the box, to be split half by crop */
171 	cairo_set_source_rgb(cr, 0, 1, 0);
172 	cairo_move_to(cr, 0.5, RECT_H);
173 	cairo_line_to(cr, 0.5, 0);
174 	cairo_move_to(cr, 0, 0.5);
175 	cairo_line_to(cr, RECT_W, 0.5);
176 	cairo_stroke(cr);
177 
178 	cairo_destroy(cr);
179 
180 	/* TODO: buffer_transform */
181 
182 	cairo_surface_destroy(surface);
183 }
184 
185 static void
global_handler(struct display * display,uint32_t name,const char * interface,uint32_t version,void * data)186 global_handler(struct display *display, uint32_t name,
187 	       const char *interface, uint32_t version, void *data)
188 {
189 	struct box *box = data;
190 
191 	if (strcmp(interface, "wl_scaler") == 0) {
192 		box->scaler_version = version < 2 ? version : 2;
193 
194 		box->scaler = display_bind(display, name,
195 					   &wl_scaler_interface,
196 					   box->scaler_version);
197 
198 		box->viewport = wl_scaler_get_viewport(box->scaler,
199 			widget_get_wl_surface(box->widget));
200 
201 		set_my_viewport(box);
202 	}
203 }
204 
205 static void
button_handler(struct widget * widget,struct input * input,uint32_t time,uint32_t button,enum wl_pointer_button_state state,void * data)206 button_handler(struct widget *widget,
207 	       struct input *input, uint32_t time,
208 	       uint32_t button, enum wl_pointer_button_state state, void *data)
209 {
210 	struct box *box = data;
211 
212 	if (button != BTN_LEFT)
213 		return;
214 
215 	if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
216 		window_move(box->window, input,
217 			    display_get_serial(box->display));
218 	}
219 }
220 
221 static void
touch_down_handler(struct widget * widget,struct input * input,uint32_t serial,uint32_t time,int32_t id,float x,float y,void * data)222 touch_down_handler(struct widget *widget, struct input *input,
223 		   uint32_t serial, uint32_t time, int32_t id,
224 		   float x, float y, void *data)
225 {
226 	struct box *box = data;
227 	window_move(box->window, input,
228 		    display_get_serial(box->display));
229 }
230 
231 static void
usage(const char * progname)232 usage(const char *progname)
233 {
234 	fprintf(stderr, "Usage: %s [mode]\n"
235 		"where 'mode' is one of\n"
236 		"  -b\tset both src and dst in viewport (default)\n"
237 		"  -d\tset only dst in viewport\n"
238 		"  -s\tset only src in viewport\n"
239 		"  -n\tdo not set viewport at all\n\n",
240 		progname);
241 
242 	fprintf(stderr, "Expected output with output_scale=1:\n");
243 
244 	fprintf(stderr, "Mode -n:\n"
245 		"  window size %dx%d px\n"
246 		"  Red box with a blue box in the upper left part.\n"
247 		"  The blue box has white right edge, black bottom edge,\n"
248 		"  and thin green left and top edges that can really\n"
249 		"  be seen only when zoomed in.\n\n",
250 		BUFFER_WIDTH / BUFFER_SCALE, BUFFER_HEIGHT / BUFFER_SCALE);
251 
252 	fprintf(stderr, "Mode -b:\n"
253 		"  window size %dx%d px\n"
254 		"  Blue box with green top and left edge,\n"
255 		"  thick white right edge with a hint of red,\n"
256 		"  and a hint of black in bottom edge.\n\n",
257 		SURFACE_WIDTH, SURFACE_HEIGHT);
258 
259 	fprintf(stderr, "Mode -s:\n"
260 		"  window size %.0fx%.0f px\n"
261 		"  The same as mode -b, but scaled a lot smaller.\n\n",
262 		RECT_W / BUFFER_SCALE, RECT_H / BUFFER_SCALE);
263 
264 	fprintf(stderr, "Mode -d:\n"
265 		"  window size %dx%d px\n"
266 		"  This is horizontally squashed version of the -n mode.\n\n",
267 		SURFACE_WIDTH, SURFACE_HEIGHT);
268 }
269 
270 int
main(int argc,char * argv[])271 main(int argc, char *argv[])
272 {
273 	struct box box;
274 	struct display *d;
275 	struct timeval tv;
276 	int i;
277 
278 	box.mode = MODE_SRC_DST;
279 
280 	for (i = 1; i < argc; i++) {
281 		if (strcmp("-s", argv[i]) == 0)
282 			box.mode = MODE_SRC_ONLY;
283 		else if (strcmp("-d", argv[i]) == 0)
284 			box.mode = MODE_DST_ONLY;
285 		else if (strcmp("-b", argv[i]) == 0)
286 			box.mode = MODE_SRC_DST;
287 		else if (strcmp("-n", argv[i]) == 0)
288 			box.mode = MODE_NO_VIEWPORT;
289 		else {
290 			usage(argv[0]);
291 			exit(1);
292 		}
293 	}
294 
295 	d = display_create(&argc, argv);
296 	if (d == NULL) {
297 		fprintf(stderr, "failed to create display: %m\n");
298 		return -1;
299 	}
300 
301 	gettimeofday(&tv, NULL);
302 	srandom(tv.tv_usec);
303 
304 	box.width = BUFFER_WIDTH / BUFFER_SCALE;
305 	box.height = BUFFER_HEIGHT / BUFFER_SCALE;
306 	box.display = d;
307 	box.window = window_create(d);
308 	box.widget = window_add_widget(box.window, &box);
309 	window_set_title(box.window, "Scaler Test Box");
310 	window_set_buffer_scale(box.window, BUFFER_SCALE);
311 
312 	widget_set_resize_handler(box.widget, resize_handler);
313 	widget_set_redraw_handler(box.widget, redraw_handler);
314 	widget_set_button_handler(box.widget, button_handler);
315 	widget_set_default_cursor(box.widget, CURSOR_HAND1);
316 	widget_set_touch_down_handler(box.widget, touch_down_handler);
317 
318 	window_schedule_resize(box.window, box.width, box.height);
319 
320 	display_set_user_data(box.display, &box);
321 	display_set_global_handler(box.display, global_handler);
322 
323 	display_run(d);
324 
325 	widget_destroy(box.widget);
326 	window_destroy(box.window);
327 	display_destroy(d);
328 
329 	return 0;
330 }
331