compositor: Generalize output previous damage into per buffer damage

This is a more generic fix for the issue solved in 4f521731 where
damage obscured by overlays could be lost in one of the output buffers
due to rapid move of a surface in an overlay plane.

This changes the renderer so it keeps track of the damage in each
buffer. Every time a new frame is drawn, the damage of the frame is
added to all the buffers and the rendered regions are cleared from
the current buffer's damage.
diff --git a/src/screenshooter.c b/src/screenshooter.c
index ba80ce5..ffcc970 100644
--- a/src/screenshooter.c
+++ b/src/screenshooter.c
@@ -261,7 +261,7 @@
 	struct weston_output *output = data;
 	uint32_t msecs = output->frame_time;
 	pixman_box32_t *r;
-	pixman_region32_t damage;
+	pixman_region32_t damage, *previous_damage;
 	int i, j, k, n, width, height, run, stride;
 	uint32_t delta, prev, *d, *s, *p, next;
 	struct {
@@ -270,9 +270,18 @@
 	} header;
 	struct iovec v[2];
 
+	/* When recording, this will be exactly the region that was repainted
+	 * in this frame. Since overlays are disabled, the whole primary plane
+	 * damage is rendered. For the first frame, the whole output will be
+	 * damaged and that damage will be added to both buffers causing the
+	 * non-current buffer damage to be while output. Rendering will clear
+	 * all the damage in the current buffer so in the next frame (when
+	 * that is non-current) the only damage left will be the one added
+	 * from the primary plane. */
+	previous_damage = &output->buffer_damage[output->current_buffer ^ 1];
+
 	pixman_region32_init(&damage);
-	pixman_region32_intersect(&damage, &output->region,
-				  &output->previous_damage);
+	pixman_region32_intersect(&damage, &output->region, previous_damage);
 
 	r = pixman_region32_rectangles(&damage, &n);
 	if (n == 0)