scaler.c 9.01 KB
Newer Older
Jonny Lamb's avatar
Jonny Lamb committed
1 2 3 4
/*
 * Copyright © 2008 Kristian Høgsberg
 * Copyright © 2013 Collabora, Ltd.
 *
5 6 7 8 9 10
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
Jonny Lamb's avatar
Jonny Lamb committed
11
 *
12 13 14 15 16 17 18 19 20 21 22
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
Jonny Lamb's avatar
Jonny Lamb committed
23 24
 */

25 26
#include "config.h"

27
#include <stdint.h>
Jonny Lamb's avatar
Jonny Lamb committed
28 29
#include <stdio.h>
#include <string.h>
30
#include <assert.h>
31
#include <errno.h>
Jonny Lamb's avatar
Jonny Lamb committed
32 33 34 35 36
#include <cairo.h>

#include <linux/input.h>

#include "window.h"
37
#include "viewporter-client-protocol.h"
Jonny Lamb's avatar
Jonny Lamb committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

#define BUFFER_SCALE 2
static const int BUFFER_WIDTH = 421 * BUFFER_SCALE;
static const int BUFFER_HEIGHT = 337 * BUFFER_SCALE;
static const int SURFACE_WIDTH = 55 * 4;
static const int SURFACE_HEIGHT = 77 * 4;
static const double RECT_X = 21 * BUFFER_SCALE; /* buffer coords */
static const double RECT_Y = 25 * BUFFER_SCALE;
static const double RECT_W = 55 * BUFFER_SCALE;
static const double RECT_H = 77 * BUFFER_SCALE;

struct box {
	struct display *display;
	struct window *window;
	struct widget *widget;
	int width, height;

55 56
	struct wp_viewporter *viewporter;
	struct wp_viewport *viewport;
57 58 59 60 61 62 63

	enum {
		MODE_NO_VIEWPORT,
		MODE_SRC_ONLY,
		MODE_DST_ONLY,
		MODE_SRC_DST
	} mode;
Jonny Lamb's avatar
Jonny Lamb committed
64 65
};

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
static void
set_my_viewport(struct box *box)
{
	wl_fixed_t src_x, src_y, src_width, src_height;
	int32_t dst_width = SURFACE_WIDTH;
	int32_t dst_height = SURFACE_HEIGHT;

	if (box->mode == MODE_NO_VIEWPORT)
		return;

	/* Cut the green border in half, take white border fully in,
	 * and black border fully out. The borders are 1px wide in buffer.
	 *
	 * The gl-renderer uses linear texture sampling, this means the
	 * top and left edges go to 100% green, bottom goes to 50% blue/black,
	 * right edge has thick white sliding to 50% red.
	 */
	src_x = wl_fixed_from_double((RECT_X + 0.5) / BUFFER_SCALE);
	src_y = wl_fixed_from_double((RECT_Y + 0.5) / BUFFER_SCALE);
	src_width = wl_fixed_from_double((RECT_W - 0.5) / BUFFER_SCALE);
	src_height = wl_fixed_from_double((RECT_H - 0.5) / BUFFER_SCALE);

	switch (box->mode){
	case MODE_SRC_ONLY:
90 91 92 93 94 95 96 97 98 99
		/* In SRC_ONLY mode we're just cropping - in order
		 * for the surface size to remain an integer, the
		 * compositor will generate an error if we use a
		 * fractional width or height.
		 *
		 * We use fractional width/height for the other cases
		 * to ensure fractional values are still tested.
		 */
		src_width = wl_fixed_from_int(RECT_W / BUFFER_SCALE);
		src_height = wl_fixed_from_int(RECT_H / BUFFER_SCALE);
100
		wp_viewport_set_source(box->viewport, src_x, src_y,
101 102 103
				       src_width, src_height);
		break;
	case MODE_DST_ONLY:
104
		wp_viewport_set_destination(box->viewport,
105 106 107
					    dst_width, dst_height);
		break;
	case MODE_SRC_DST:
108 109 110 111
		wp_viewport_set_source(box->viewport, src_x, src_y,
				       src_width, src_height);
		wp_viewport_set_destination(box->viewport,
					    dst_width, dst_height);
112 113 114 115 116 117
		break;
	default:
		assert(!"not reached");
	}
}

Jonny Lamb's avatar
Jonny Lamb committed
118 119 120 121 122 123
static void
resize_handler(struct widget *widget,
	       int32_t width, int32_t height, void *data)
{
	struct box *box = data;

124
	/* Don't resize me */
Jonny Lamb's avatar
Jonny Lamb committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	widget_set_size(box->widget, box->width, box->height);
}

static void
redraw_handler(struct widget *widget, void *data)
{
	struct box *box = data;
	cairo_surface_t *surface;
	cairo_t *cr;

	surface = window_get_surface(box->window);
	if (surface == NULL ||
	    cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
		fprintf(stderr, "failed to create cairo egl surface\n");
		return;
	}

	cr = cairo_create(surface);
	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
	cairo_set_line_width(cr, 1.0);
	cairo_translate(cr, RECT_X, RECT_Y);

	/* red background */
	cairo_set_source_rgba(cr, 255, 0, 0, 255);
	cairo_paint(cr);

	/* blue box */
	cairo_set_source_rgba(cr, 0, 0, 255, 255);
	cairo_rectangle(cr, 0, 0, RECT_W, RECT_H);
	cairo_fill(cr);

	/* black border outside the box */
	cairo_set_source_rgb(cr, 0, 0, 0);
	cairo_move_to(cr, 0, RECT_H + 0.5);
	cairo_line_to(cr, RECT_W, RECT_H + 0.5);
	cairo_stroke(cr);

	/* white border inside the box */
	cairo_set_source_rgb(cr, 1, 1, 1);
	cairo_move_to(cr, RECT_W - 0.5, 0);
	cairo_line_to(cr, RECT_W - 0.5, RECT_H);
	cairo_stroke(cr);

	/* the green border on inside the box, to be split half by crop */
	cairo_set_source_rgb(cr, 0, 1, 0);
	cairo_move_to(cr, 0.5, RECT_H);
	cairo_line_to(cr, 0.5, 0);
	cairo_move_to(cr, 0, 0.5);
	cairo_line_to(cr, RECT_W, 0.5);
	cairo_stroke(cr);

	cairo_destroy(cr);

	/* TODO: buffer_transform */

	cairo_surface_destroy(surface);
}

static void
global_handler(struct display *display, uint32_t name,
	       const char *interface, uint32_t version, void *data)
{
	struct box *box = data;

189 190 191
	if (strcmp(interface, "wp_viewporter") == 0) {
		box->viewporter = display_bind(display, name,
					       &wp_viewporter_interface, 1);
Jonny Lamb's avatar
Jonny Lamb committed
192

193
		box->viewport = wp_viewporter_get_viewport(box->viewporter,
Jonny Lamb's avatar
Jonny Lamb committed
194 195
			widget_get_wl_surface(box->widget));

196
		set_my_viewport(box);
Jonny Lamb's avatar
Jonny Lamb committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	}
}

static void
button_handler(struct widget *widget,
	       struct input *input, uint32_t time,
	       uint32_t button, enum wl_pointer_button_state state, void *data)
{
	struct box *box = data;

	if (button != BTN_LEFT)
		return;

	if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
		window_move(box->window, input,
			    display_get_serial(box->display));
	}
}

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)
{
	struct box *box = data;
	window_move(box->window, input,
		    display_get_serial(box->display));
}

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
static void
usage(const char *progname)
{
	fprintf(stderr, "Usage: %s [mode]\n"
		"where 'mode' is one of\n"
		"  -b\tset both src and dst in viewport (default)\n"
		"  -d\tset only dst in viewport\n"
		"  -s\tset only src in viewport\n"
		"  -n\tdo not set viewport at all\n\n",
		progname);

	fprintf(stderr, "Expected output with output_scale=1:\n");

	fprintf(stderr, "Mode -n:\n"
		"  window size %dx%d px\n"
		"  Red box with a blue box in the upper left part.\n"
		"  The blue box has white right edge, black bottom edge,\n"
		"  and thin green left and top edges that can really\n"
		"  be seen only when zoomed in.\n\n",
		BUFFER_WIDTH / BUFFER_SCALE, BUFFER_HEIGHT / BUFFER_SCALE);

	fprintf(stderr, "Mode -b:\n"
		"  window size %dx%d px\n"
		"  Blue box with green top and left edge,\n"
		"  thick white right edge with a hint of red,\n"
		"  and a hint of black in bottom edge.\n\n",
		SURFACE_WIDTH, SURFACE_HEIGHT);

	fprintf(stderr, "Mode -s:\n"
		"  window size %.0fx%.0f px\n"
		"  The same as mode -b, but scaled a lot smaller.\n\n",
		RECT_W / BUFFER_SCALE, RECT_H / BUFFER_SCALE);

	fprintf(stderr, "Mode -d:\n"
		"  window size %dx%d px\n"
		"  This is horizontally squashed version of the -n mode.\n\n",
		SURFACE_WIDTH, SURFACE_HEIGHT);
}

Jonny Lamb's avatar
Jonny Lamb committed
265 266 267 268 269 270
int
main(int argc, char *argv[])
{
	struct box box;
	struct display *d;
	struct timeval tv;
271
	int i;
Jonny Lamb's avatar
Jonny Lamb committed
272

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
	box.mode = MODE_SRC_DST;

	for (i = 1; i < argc; i++) {
		if (strcmp("-s", argv[i]) == 0)
			box.mode = MODE_SRC_ONLY;
		else if (strcmp("-d", argv[i]) == 0)
			box.mode = MODE_DST_ONLY;
		else if (strcmp("-b", argv[i]) == 0)
			box.mode = MODE_SRC_DST;
		else if (strcmp("-n", argv[i]) == 0)
			box.mode = MODE_NO_VIEWPORT;
		else {
			usage(argv[0]);
			exit(1);
		}
	}

290 291
	d = display_create(&argc, argv);
	if (d == NULL) {
292 293
		fprintf(stderr, "failed to create display: %s\n",
			strerror(errno));
294 295 296
		return -1;
	}

Jonny Lamb's avatar
Jonny Lamb committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	gettimeofday(&tv, NULL);
	srandom(tv.tv_usec);

	box.width = BUFFER_WIDTH / BUFFER_SCALE;
	box.height = BUFFER_HEIGHT / BUFFER_SCALE;
	box.display = d;
	box.window = window_create(d);
	box.widget = window_add_widget(box.window, &box);
	window_set_title(box.window, "Scaler Test Box");
	window_set_buffer_scale(box.window, BUFFER_SCALE);

	widget_set_resize_handler(box.widget, resize_handler);
	widget_set_redraw_handler(box.widget, redraw_handler);
	widget_set_button_handler(box.widget, button_handler);
	widget_set_default_cursor(box.widget, CURSOR_HAND1);
	widget_set_touch_down_handler(box.widget, touch_down_handler);

	window_schedule_resize(box.window, box.width, box.height);

	display_set_user_data(box.display, &box);
	display_set_global_handler(box.display, global_handler);

	display_run(d);

321
	widget_destroy(box.widget);
Jonny Lamb's avatar
Jonny Lamb committed
322
	window_destroy(box.window);
323 324
	display_destroy(d);

Jonny Lamb's avatar
Jonny Lamb committed
325 326
	return 0;
}