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