pixman-renderer.c 25 KB
Newer Older
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
1 2 3
/*
 * Copyright © 2012 Intel Corporation
 * Copyright © 2013 Vasily Khoruzhick <anarsoul@gmail.com>
4
 * Copyright © 2015 Collabora, Ltd.
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
5
 *
6 7 8 9 10 11 12
 * 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:
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
13
 *
14 15 16 17 18 19 20 21 22 23 24 25
 * 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.
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
26 27
 */

28
#include "config.h"
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
29 30

#include <errno.h>
31
#include <stdint.h>
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
32
#include <stdlib.h>
33
#include <assert.h>
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
34 35

#include "pixman-renderer.h"
36
#include "shared/helpers.h"
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
37

38 39
#include <linux/input.h>

Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
40
struct pixman_output_state {
41 42
	void *shadow_buffer;
	pixman_image_t *shadow_image;
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
43 44 45 46
	pixman_image_t *hw_buffer;
};

struct pixman_surface_state {
47 48
	struct weston_surface *surface;

Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
49 50
	pixman_image_t *image;
	struct weston_buffer_reference buffer_ref;
51

52
	struct wl_listener buffer_destroy_listener;
53
	struct wl_listener surface_destroy_listener;
54
	struct wl_listener renderer_destroy_listener;
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
55 56 57 58
};

struct pixman_renderer {
	struct weston_renderer base;
59

60 61
	int repaint_debug;
	pixman_image_t *debug_color;
62
	struct weston_binding *debug_binding;
63 64

	struct wl_signal destroy_signal;
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
65 66 67 68 69 70 71 72
};

static inline struct pixman_output_state *
get_output_state(struct weston_output *output)
{
	return (struct pixman_output_state *)output->renderer_state;
}

73 74 75
static int
pixman_renderer_create_surface(struct weston_surface *surface);

Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
76 77 78
static inline struct pixman_surface_state *
get_surface_state(struct weston_surface *surface)
{
79 80 81
	if (!surface->renderer_state)
		pixman_renderer_create_surface(surface);

Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
	return (struct pixman_surface_state *)surface->renderer_state;
}

static inline struct pixman_renderer *
get_renderer(struct weston_compositor *ec)
{
	return (struct pixman_renderer *)ec->renderer;
}

static int
pixman_renderer_read_pixels(struct weston_output *output,
			       pixman_format_code_t format, void *pixels,
			       uint32_t x, uint32_t y,
			       uint32_t width, uint32_t height)
{
	struct pixman_output_state *po = get_output_state(output);
98
	pixman_transform_t transform;
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
99 100 101 102 103 104 105 106 107 108 109 110 111
	pixman_image_t *out_buf;

	if (!po->hw_buffer) {
		errno = ENODEV;
		return -1;
	}

	out_buf = pixman_image_create_bits(format,
		width,
		height,
		pixels,
		(PIXMAN_FORMAT_BPP(format) / 8) * width);

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	/* Caller expects vflipped source image */
	pixman_transform_init_translate(&transform,
					pixman_int_to_fixed (x),
					pixman_int_to_fixed (y - pixman_image_get_height (po->hw_buffer)));
	pixman_transform_scale(&transform, NULL,
			       pixman_fixed_1,
			       pixman_fixed_minus_1);
	pixman_image_set_transform(po->hw_buffer, &transform);

	pixman_image_composite32(PIXMAN_OP_SRC,
				 po->hw_buffer, /* src */
				 NULL /* mask */,
				 out_buf, /* dest */
				 0, 0, /* src_x, src_y */
				 0, 0, /* mask_x, mask_y */
				 0, 0, /* dest_x, dest_y */
				 pixman_image_get_width (po->hw_buffer), /* width */
				 pixman_image_get_height (po->hw_buffer) /* height */);
	pixman_image_set_transform(po->hw_buffer, NULL);
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
131 132 133 134 135 136

	pixman_image_unref(out_buf);

	return 0;
}

137 138 139
static void
region_global_to_output(struct weston_output *output, pixman_region32_t *region)
{
140 141 142 143 144 145 146 147 148
	if (output->zoom.active) {
		weston_matrix_transform_region(region, &output->matrix, region);
	} else {
		pixman_region32_translate(region, -output->x, -output->y);
		weston_transformed_region(output->width, output->height,
					  output->transform,
					  output->current_scale,
					  region, region);
	}
149 150
}

151 152
#define D2F(v) pixman_double_to_fixed((double)v)

153
static void
154 155
weston_matrix_to_pixman_transform(pixman_transform_t *pt,
				  const struct weston_matrix *wm)
156
{
157 158 159 160 161 162 163 164 165 166 167
	/* Pixman supports only 2D transform matrix, but Weston uses 3D, *
	 * so we're omitting Z coordinate here. */
	pt->matrix[0][0] = pixman_double_to_fixed(wm->d[0]);
	pt->matrix[0][1] = pixman_double_to_fixed(wm->d[4]);
	pt->matrix[0][2] = pixman_double_to_fixed(wm->d[12]);
	pt->matrix[1][0] = pixman_double_to_fixed(wm->d[1]);
	pt->matrix[1][1] = pixman_double_to_fixed(wm->d[5]);
	pt->matrix[1][2] = pixman_double_to_fixed(wm->d[13]);
	pt->matrix[2][0] = pixman_double_to_fixed(wm->d[3]);
	pt->matrix[2][1] = pixman_double_to_fixed(wm->d[7]);
	pt->matrix[2][2] = pixman_double_to_fixed(wm->d[15]);
168 169
}

170 171 172 173 174 175 176 177 178 179
static void
pixman_renderer_compute_transform(pixman_transform_t *transform_out,
				  struct weston_view *ev,
				  struct weston_output *output)
{
	struct weston_matrix matrix;

	/* Set up the source transformation based on the surface
	   position, the output position/transform/scale and the client
	   specified buffer transform/scale */
180
	matrix = output->inverse_matrix;
181 182 183 184 185 186 187 188 189 190 191 192 193

	if (ev->transform.enabled) {
		weston_matrix_multiply(&matrix, &ev->transform.inverse);
	} else {
		weston_matrix_translate(&matrix,
					-ev->geometry.x, -ev->geometry.y, 0);
	}

	weston_matrix_multiply(&matrix, &ev->surface->surface_to_buffer_matrix);

	weston_matrix_to_pixman_transform(transform_out, &matrix);
}

194 195 196 197 198 199 200 201 202 203 204 205
static bool
view_transformation_is_translation(struct weston_view *view)
{
	if (!view->transform.enabled)
		return true;

	if (view->transform.matrix.type <= WESTON_MATRIX_TRANSFORM_TRANSLATE)
		return true;

	return false;
}

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
static void
region_intersect_only_translation(pixman_region32_t *result_global,
				  pixman_region32_t *global,
				  pixman_region32_t *surf,
				  struct weston_view *view)
{
	float view_x, view_y;

	assert(view_transformation_is_translation(view));

	/* Convert from surface to global coordinates */
	pixman_region32_copy(result_global, surf);
	weston_view_to_global_float(view, 0, 0, &view_x, &view_y);
	pixman_region32_translate(result_global, (int)view_x, (int)view_y);

	pixman_region32_intersect(result_global, result_global, global);
}

224 225 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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
static void
composite_whole(pixman_op_t op,
		pixman_image_t *src,
		pixman_image_t *mask,
		pixman_image_t *dest,
		const pixman_transform_t *transform,
		pixman_filter_t filter)
{
	int32_t dest_width;
	int32_t dest_height;

	dest_width = pixman_image_get_width(dest);
	dest_height = pixman_image_get_height(dest);

	pixman_image_set_transform(src, transform);
	pixman_image_set_filter(src, filter, NULL, 0);

	pixman_image_composite32(op, src, mask, dest,
				 0, 0, /* src_x, src_y */
				 0, 0, /* mask_x, mask_y */
				 0, 0, /* dest_x, dest_y */
				 dest_width, dest_height);
}

static void
composite_clipped(pixman_image_t *src,
		  pixman_image_t *mask,
		  pixman_image_t *dest,
		  const pixman_transform_t *transform,
		  pixman_filter_t filter,
		  pixman_region32_t *src_clip)
{
	int n_box;
	pixman_box32_t *boxes;
	int32_t dest_width;
	int32_t dest_height;
	int src_stride;
	int bitspp;
	pixman_format_code_t src_format;
	void *src_data;
	int i;

	/* Hardcoded to use PIXMAN_OP_OVER, because sampling outside of
	 * a Pixman image produces (0,0,0,0) instead of discarding the
	 * fragment.
	 */

	dest_width = pixman_image_get_width(dest);
	dest_height = pixman_image_get_height(dest);
	src_format = pixman_image_get_format(src);
	src_stride = pixman_image_get_stride(src);
	bitspp = PIXMAN_FORMAT_BPP(src_format);
	src_data = pixman_image_get_data(src);

	assert(src_format);

	/* This would be massive overdraw, except when n_box is 1. */
	boxes = pixman_region32_rectangles(src_clip, &n_box);
	for (i = 0; i < n_box; i++) {
		uint8_t *ptr = src_data;
		pixman_image_t *boximg;
		pixman_transform_t adj = *transform;

		ptr += boxes[i].y1 * src_stride;
		ptr += boxes[i].x1 * bitspp / 8;
		boximg = pixman_image_create_bits_no_clear(src_format,
					boxes[i].x2 - boxes[i].x1,
					boxes[i].y2 - boxes[i].y1,
					(uint32_t *)ptr, src_stride);

		pixman_transform_translate(&adj, NULL,
					   pixman_int_to_fixed(-boxes[i].x1),
					   pixman_int_to_fixed(-boxes[i].y1));
		pixman_image_set_transform(boximg, &adj);

		pixman_image_set_filter(boximg, filter, NULL, 0);
		pixman_image_composite32(PIXMAN_OP_OVER, boximg, mask, dest,
					 0, 0, /* src_x, src_y */
					 0, 0, /* mask_x, mask_y */
					 0, 0, /* dest_x, dest_y */
					 dest_width, dest_height);

		pixman_image_unref(boximg);
	}

	if (n_box > 1) {
		static bool warned = false;

		if (!warned)
			weston_log("Pixman-renderer warning: %dx overdraw\n",
				   n_box);
		warned = true;
	}
}

319 320 321 322 323
/** Paint an intersected region
 *
 * \param ev The view to be painted.
 * \param output The output being painted.
 * \param repaint_output The region to be painted in output coordinates.
324 325
 * \param source_clip The region of the source image to use, in source image
 *                    coordinates. If NULL, use the whole source image.
326 327
 * \param pixman_op Compositing operator, either SRC or OVER.
 */
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
328
static void
329
repaint_region(struct weston_view *ev, struct weston_output *output,
330
	       pixman_region32_t *repaint_output,
331
	       pixman_region32_t *source_clip,
332
	       pixman_op_t pixman_op)
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
333
{
334 335
	struct pixman_renderer *pr =
		(struct pixman_renderer *) output->compositor->renderer;
336
	struct pixman_surface_state *ps = get_surface_state(ev->surface);
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
337
	struct pixman_output_state *po = get_output_state(output);
338
	struct weston_buffer_viewport *vp = &ev->surface->buffer_viewport;
339
	pixman_transform_t transform;
340
	pixman_filter_t filter;
341 342
	pixman_image_t *mask_image;
	pixman_color_t mask = { 0, };
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
343

344 345
	/* Clip rendering to the damaged output region */
	pixman_image_set_clip_region32(po->shadow_image, repaint_output);
346

347
	pixman_renderer_compute_transform(&transform, ev, output);
348

349
	if (ev->transform.enabled || output->current_scale != vp->buffer.scale)
350
		filter = PIXMAN_FILTER_BILINEAR;
351
	else
352
		filter = PIXMAN_FILTER_NEAREST;
353

354 355 356
	if (ps->buffer_ref.buffer)
		wl_shm_buffer_begin_access(ps->buffer_ref.buffer->shm_buffer);

357
	if (ev->alpha < 1.0) {
358
		mask.alpha = 0xffff * ev->alpha;
359
		mask_image = pixman_image_create_solid_fill(&mask);
360
	} else {
361
		mask_image = NULL;
362
	}
363

364 365 366 367 368 369
	if (source_clip)
		composite_clipped(ps->image, mask_image, po->shadow_image,
				  &transform, filter, source_clip);
	else
		composite_whole(pixman_op, ps->image, mask_image,
				po->shadow_image, &transform, filter);
370

371 372 373
	if (mask_image)
		pixman_image_unref(mask_image);

374 375 376
	if (ps->buffer_ref.buffer)
		wl_shm_buffer_end_access(ps->buffer_ref.buffer->shm_buffer);

377 378 379 380 381 382 383 384 385 386 387 388
	if (pr->repaint_debug)
		pixman_image_composite32(PIXMAN_OP_OVER,
					 pr->debug_color, /* src */
					 NULL /* mask */,
					 po->shadow_image, /* dest */
					 0, 0, /* src_x, src_y */
					 0, 0, /* mask_x, mask_y */
					 0, 0, /* dest_x, dest_y */
					 pixman_image_get_width (po->shadow_image), /* width */
					 pixman_image_get_height (po->shadow_image) /* height */);

	pixman_image_set_clip_region32 (po->shadow_image, NULL);
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
389 390
}

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
static void
draw_view_translated(struct weston_view *view, struct weston_output *output,
		     pixman_region32_t *repaint_global)
{
	struct weston_surface *surface = view->surface;
	/* non-opaque region in surface coordinates: */
	pixman_region32_t surface_blend;
	/* region to be painted in output coordinates: */
	pixman_region32_t repaint_output;

	pixman_region32_init(&repaint_output);

	/* Blended region is whole surface minus opaque region,
	 * unless surface alpha forces us to blend all.
	 */
	pixman_region32_init_rect(&surface_blend, 0, 0,
				  surface->width, surface->height);

	if (!(view->alpha < 1.0)) {
		pixman_region32_subtract(&surface_blend, &surface_blend,
					 &surface->opaque);

		if (pixman_region32_not_empty(&surface->opaque)) {
			region_intersect_only_translation(&repaint_output,
							  repaint_global,
							  &surface->opaque,
							  view);
			region_global_to_output(output, &repaint_output);

420
			repaint_region(view, output, &repaint_output, NULL,
421 422 423 424 425 426 427 428 429 430
				       PIXMAN_OP_SRC);
		}
	}

	if (pixman_region32_not_empty(&surface_blend)) {
		region_intersect_only_translation(&repaint_output,
						  repaint_global,
						  &surface_blend, view);
		region_global_to_output(output, &repaint_output);

431
		repaint_region(view, output, &repaint_output, NULL,
432 433 434 435 436 437 438
			       PIXMAN_OP_OVER);
	}

	pixman_region32_fini(&surface_blend);
	pixman_region32_fini(&repaint_output);
}

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
static void
draw_view_source_clipped(struct weston_view *view,
			 struct weston_output *output,
			 pixman_region32_t *repaint_global)
{
	struct weston_surface *surface = view->surface;
	pixman_region32_t surf_region;
	pixman_region32_t buffer_region;
	pixman_region32_t repaint_output;

	/* Do not bother separating the opaque region from non-opaque.
	 * Source clipping requires PIXMAN_OP_OVER in all cases, so painting
	 * opaque separately has no benefit.
	 */

	pixman_region32_init_rect(&surf_region, 0, 0,
				  surface->width, surface->height);
456 457 458 459
	if (view->geometry.scissor_enabled)
		pixman_region32_intersect(&surf_region, &surf_region,
					  &view->geometry.scissor);

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
	pixman_region32_init(&buffer_region);
	weston_surface_to_buffer_region(surface, &surf_region, &buffer_region);

	pixman_region32_init(&repaint_output);
	pixman_region32_copy(&repaint_output, repaint_global);
	region_global_to_output(output, &repaint_output);

	repaint_region(view, output, &repaint_output, &buffer_region,
		       PIXMAN_OP_OVER);

	pixman_region32_fini(&repaint_output);
	pixman_region32_fini(&buffer_region);
	pixman_region32_fini(&surf_region);
}

Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
475
static void
476 477
draw_view(struct weston_view *ev, struct weston_output *output,
	  pixman_region32_t *damage) /* in global coordinates */
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
478
{
479
	struct pixman_surface_state *ps = get_surface_state(ev->surface);
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
480 481 482 483 484 485 486 487 488
	/* repaint bounding region in global coordinates: */
	pixman_region32_t repaint;

	/* No buffer attached */
	if (!ps->image)
		return;

	pixman_region32_init(&repaint);
	pixman_region32_intersect(&repaint,
489
				  &ev->transform.boundingbox, damage);
490
	pixman_region32_subtract(&repaint, &repaint, &ev->clip);
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
491 492 493 494

	if (!pixman_region32_not_empty(&repaint))
		goto out;

495
	if (view_transformation_is_translation(ev)) {
496 497 498 499
		/* The simple case: The surface regions opaque, non-opaque,
		 * etc. are convertible to global coordinate space.
		 * There is no need to use a source clip region.
		 * It is possible to paint opaque region as PIXMAN_OP_SRC.
500 501
		 * Also the boundingbox is accurate rather than an
		 * approximation.
502
		 */
503 504
		draw_view_translated(ev, output, &repaint);
	} else {
505 506 507 508 509 510 511 512
		/* The complex case: the view transformation does not allow
		 * converting opaque etc. regions into global coordinate space.
		 * Therefore we need source clipping to avoid sampling from
		 * unwanted source image areas, unless the source image is
		 * to be used whole. Source clipping does not work with
		 * PIXMAN_OP_SRC.
		 */
		draw_view_source_clipped(ev, output, &repaint);
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
513 514 515 516 517 518 519 520 521
	}

out:
	pixman_region32_fini(&repaint);
}
static void
repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
{
	struct weston_compositor *compositor = output->compositor;
522
	struct weston_view *view;
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
523

524 525 526
	wl_list_for_each_reverse(view, &compositor->view_list, link)
		if (view->plane == &compositor->primary_plane)
			draw_view(view, output, damage);
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
527 528
}

529 530 531 532
static void
copy_to_hw_buffer(struct weston_output *output, pixman_region32_t *region)
{
	struct pixman_output_state *po = get_output_state(output);
533
	pixman_region32_t output_region;
534

535 536
	pixman_region32_init(&output_region);
	pixman_region32_copy(&output_region, region);
537

538
	region_global_to_output(output, &output_region);
539

540
	pixman_image_set_clip_region32 (po->hw_buffer, &output_region);
541
	pixman_region32_fini(&output_region);
542 543 544 545 546 547 548 549 550 551

	pixman_image_composite32(PIXMAN_OP_SRC,
				 po->shadow_image, /* src */
				 NULL /* mask */,
				 po->hw_buffer, /* dest */
				 0, 0, /* src_x, src_y */
				 0, 0, /* mask_x, mask_y */
				 0, 0, /* dest_x, dest_y */
				 pixman_image_get_width (po->hw_buffer), /* width */
				 pixman_image_get_height (po->hw_buffer) /* height */);
552

553
	pixman_image_set_clip_region32 (po->hw_buffer, NULL);
554 555
}

Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
556 557 558 559 560 561 562 563 564 565
static void
pixman_renderer_repaint_output(struct weston_output *output,
			     pixman_region32_t *output_damage)
{
	struct pixman_output_state *po = get_output_state(output);

	if (!po->hw_buffer)
		return;

	repaint_surfaces(output, output_damage);
566
	copy_to_hw_buffer(output, output_damage);
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
567 568 569 570 571 572 573 574 575 576 577 578 579

	pixman_region32_copy(&output->previous_damage, output_damage);
	wl_signal_emit(&output->frame_signal, output);

	/* Actual flip should be done by caller */
}

static void
pixman_renderer_flush_damage(struct weston_surface *surface)
{
	/* No-op for pixman renderer */
}

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
static void
buffer_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
{
	struct pixman_surface_state *ps;

	ps = container_of(listener, struct pixman_surface_state,
			  buffer_destroy_listener);

	if (ps->image) {
		pixman_image_unref(ps->image);
		ps->image = NULL;
	}

	ps->buffer_destroy_listener.notify = NULL;
}

Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
596
static void
597
pixman_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
598 599
{
	struct pixman_surface_state *ps = get_surface_state(es);
600
	struct wl_shm_buffer *shm_buffer;
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
601 602 603 604
	pixman_format_code_t pixman_format;

	weston_buffer_reference(&ps->buffer_ref, buffer);

605 606 607 608 609
	if (ps->buffer_destroy_listener.notify) {
		wl_list_remove(&ps->buffer_destroy_listener.link);
		ps->buffer_destroy_listener.notify = NULL;
	}

Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
610 611 612 613 614 615 616
	if (ps->image) {
		pixman_image_unref(ps->image);
		ps->image = NULL;
	}

	if (!buffer)
		return;
617

618
	shm_buffer = wl_shm_buffer_get(buffer->resource);
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
619

620
	if (! shm_buffer) {
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
621 622 623 624 625
		weston_log("Pixman renderer supports only SHM buffers\n");
		weston_buffer_reference(&ps->buffer_ref, NULL);
		return;
	}

626
	switch (wl_shm_buffer_get_format(shm_buffer)) {
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
627 628 629 630 631 632
	case WL_SHM_FORMAT_XRGB8888:
		pixman_format = PIXMAN_x8r8g8b8;
		break;
	case WL_SHM_FORMAT_ARGB8888:
		pixman_format = PIXMAN_a8r8g8b8;
		break;
633 634 635
	case WL_SHM_FORMAT_RGB565:
		pixman_format = PIXMAN_r5g6b5;
		break;
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
636 637 638 639 640 641
	default:
		weston_log("Unsupported SHM buffer format\n");
		weston_buffer_reference(&ps->buffer_ref, NULL);
		return;
	break;
	}
642 643 644 645 646

	buffer->shm_buffer = shm_buffer;
	buffer->width = wl_shm_buffer_get_width(shm_buffer);
	buffer->height = wl_shm_buffer_get_height(shm_buffer);

Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
647
	ps->image = pixman_image_create_bits(pixman_format,
648 649 650
		buffer->width, buffer->height,
		wl_shm_buffer_get_data(shm_buffer),
		wl_shm_buffer_get_stride(shm_buffer));
651 652 653 654 655

	ps->buffer_destroy_listener.notify =
		buffer_state_handle_buffer_destroy;
	wl_signal_add(&buffer->destroy_signal,
		      &ps->buffer_destroy_listener);
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
656 657
}

658
static void
659
pixman_renderer_surface_state_destroy(struct pixman_surface_state *ps)
660
{
661 662
	wl_list_remove(&ps->surface_destroy_listener.link);
	wl_list_remove(&ps->renderer_destroy_listener.link);
663 664 665 666
	if (ps->buffer_destroy_listener.notify) {
		wl_list_remove(&ps->buffer_destroy_listener.link);
		ps->buffer_destroy_listener.notify = NULL;
	}
667 668 669 670 671 672 673 674 675 676 677

	ps->surface->renderer_state = NULL;

	if (ps->image) {
		pixman_image_unref(ps->image);
		ps->image = NULL;
	}
	weston_buffer_reference(&ps->buffer_ref, NULL);
	free(ps);
}

678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
static void
surface_state_handle_surface_destroy(struct wl_listener *listener, void *data)
{
	struct pixman_surface_state *ps;

	ps = container_of(listener, struct pixman_surface_state,
			  surface_destroy_listener);

	pixman_renderer_surface_state_destroy(ps);
}

static void
surface_state_handle_renderer_destroy(struct wl_listener *listener, void *data)
{
	struct pixman_surface_state *ps;

	ps = container_of(listener, struct pixman_surface_state,
			  renderer_destroy_listener);

	pixman_renderer_surface_state_destroy(ps);
}

Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
700 701 702 703
static int
pixman_renderer_create_surface(struct weston_surface *surface)
{
	struct pixman_surface_state *ps;
704
	struct pixman_renderer *pr = get_renderer(surface->compositor);
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
705

706 707
	ps = zalloc(sizeof *ps);
	if (ps == NULL)
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
708 709 710 711
		return -1;

	surface->renderer_state = ps;

712 713 714 715 716 717 718
	ps->surface = surface;

	ps->surface_destroy_listener.notify =
		surface_state_handle_surface_destroy;
	wl_signal_add(&surface->destroy_signal,
		      &ps->surface_destroy_listener);

719 720 721 722 723
	ps->renderer_destroy_listener.notify =
		surface_state_handle_renderer_destroy;
	wl_signal_add(&pr->destroy_signal,
		      &ps->renderer_destroy_listener);

Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
724 725 726 727 728 729 730 731 732 733 734 735 736 737
	return 0;
}

static void
pixman_renderer_surface_set_color(struct weston_surface *es,
		 float red, float green, float blue, float alpha)
{
	struct pixman_surface_state *ps = get_surface_state(es);
	pixman_color_t color;

	color.red = red * 0xffff;
	color.green = green * 0xffff;
	color.blue = blue * 0xffff;
	color.alpha = alpha * 0xffff;
738

Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
739 740 741 742 743 744 745 746
	if (ps->image) {
		pixman_image_unref(ps->image);
		ps->image = NULL;
	}

	ps->image = pixman_image_create_solid_fill(&color);
}

747
static void
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
748 749
pixman_renderer_destroy(struct weston_compositor *ec)
{
750 751
	struct pixman_renderer *pr = get_renderer(ec);

752
	wl_signal_emit(&pr->destroy_signal, pr);
753 754 755
	weston_binding_destroy(pr->debug_binding);
	free(pr);

Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
756 757 758
	ec->renderer = NULL;
}

759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
static void
pixman_renderer_surface_get_content_size(struct weston_surface *surface,
					 int *width, int *height)
{
	struct pixman_surface_state *ps = get_surface_state(surface);

	if (ps->image) {
		*width = pixman_image_get_width(ps->image);
		*height = pixman_image_get_height(ps->image);
	} else {
		*width = 0;
		*height = 0;
	}
}

static int
pixman_renderer_surface_copy_content(struct weston_surface *surface,
				     void *target, size_t size,
				     int src_x, int src_y,
				     int width, int height)
{
	const pixman_format_code_t format = PIXMAN_a8b8g8r8;
	const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
	struct pixman_surface_state *ps = get_surface_state(surface);
	pixman_image_t *out_buf;

	if (!ps->image)
		return -1;

	out_buf = pixman_image_create_bits(format, width, height,
					   target, width * bytespp);

	pixman_image_set_transform(ps->image, NULL);
	pixman_image_composite32(PIXMAN_OP_SRC,
				 ps->image,    /* src */
				 NULL,         /* mask */
				 out_buf,      /* dest */
				 src_x, src_y, /* src_x, src_y */
				 0, 0,         /* mask_x, mask_y */
				 0, 0,         /* dest_x, dest_y */
				 width, height);

	pixman_image_unref(out_buf);

	return 0;
}

806
static void
807 808
debug_binding(struct weston_keyboard *keyboard, const struct timespec *time,
	      uint32_t key, void *data)
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
{
	struct weston_compositor *ec = data;
	struct pixman_renderer *pr = (struct pixman_renderer *) ec->renderer;

	pr->repaint_debug ^= 1;

	if (pr->repaint_debug) {
		pixman_color_t red = {
			0x3fff, 0x0000, 0x0000, 0x3fff
		};

		pr->debug_color = pixman_image_create_solid_fill(&red);
	} else {
		pixman_image_unref(pr->debug_color);
		weston_compositor_damage_all(ec);
	}
}

Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
827 828 829
WL_EXPORT int
pixman_renderer_init(struct weston_compositor *ec)
{
830
	struct pixman_renderer *renderer;
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
831

832
	renderer = zalloc(sizeof *renderer);
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
833 834 835
	if (renderer == NULL)
		return -1;

836 837
	renderer->repaint_debug = 0;
	renderer->debug_color = NULL;
838 839 840 841 842 843
	renderer->base.read_pixels = pixman_renderer_read_pixels;
	renderer->base.repaint_output = pixman_renderer_repaint_output;
	renderer->base.flush_damage = pixman_renderer_flush_damage;
	renderer->base.attach = pixman_renderer_attach;
	renderer->base.surface_set_color = pixman_renderer_surface_set_color;
	renderer->base.destroy = pixman_renderer_destroy;
844 845 846 847
	renderer->base.surface_get_content_size =
		pixman_renderer_surface_get_content_size;
	renderer->base.surface_copy_content =
		pixman_renderer_surface_copy_content;
848
	ec->renderer = &renderer->base;
849
	ec->capabilities |= WESTON_CAP_ROTATION_ANY;
850
	ec->capabilities |= WESTON_CAP_CAPTURE_YFLIP;
851
	ec->capabilities |= WESTON_CAP_VIEW_CLIP_MASK;
852

853 854 855
	renderer->debug_binding =
		weston_compositor_add_debug_binding(ec, KEY_R,
						    debug_binding, ec);
856 857 858

	wl_display_add_shm_format(ec->wl_display, WL_SHM_FORMAT_RGB565);

859 860
	wl_signal_init(&renderer->destroy_signal);

Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
	return 0;
}

WL_EXPORT void
pixman_renderer_output_set_buffer(struct weston_output *output, pixman_image_t *buffer)
{
	struct pixman_output_state *po = get_output_state(output);

	if (po->hw_buffer)
		pixman_image_unref(po->hw_buffer);
	po->hw_buffer = buffer;

	if (po->hw_buffer) {
		output->compositor->read_format = pixman_image_get_format(po->hw_buffer);
		pixman_image_ref(po->hw_buffer);
	}
}

WL_EXPORT int
pixman_renderer_output_create(struct weston_output *output)
{
882
	struct pixman_output_state *po;
883
	int w, h;
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
884

885 886
	po = zalloc(sizeof *po);
	if (po == NULL)
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
887
		return -1;
888 889

	/* set shadow image transformation */
890 891
	w = output->current_mode->width;
	h = output->current_mode->height;
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909

	po->shadow_buffer = malloc(w * h * 4);

	if (!po->shadow_buffer) {
		free(po);
		return -1;
	}

	po->shadow_image =
		pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
					 po->shadow_buffer, w * 4);

	if (!po->shadow_image) {
		free(po->shadow_buffer);
		free(po);
		return -1;
	}

Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
910 911 912 913 914 915 916 917 918 919
	output->renderer_state = po;

	return 0;
}

WL_EXPORT void
pixman_renderer_output_destroy(struct weston_output *output)
{
	struct pixman_output_state *po = get_output_state(output);

920
	pixman_image_unref(po->shadow_image);
921 922 923

	if (po->hw_buffer)
		pixman_image_unref(po->hw_buffer);
924

925 926 927
	free(po->shadow_buffer);

	po->shadow_buffer = NULL;
928
	po->shadow_image = NULL;
Vasily Khoruzhick's avatar
Vasily Khoruzhick committed
929 930 931 932
	po->hw_buffer = NULL;

	free(po);
}