compositor: Consolidate output repaint logic

We move the EGL and GLES2 output repaint code into a new gles2-render.c
file.  The eglMakeCurrent, glViewPort, surface loop etc was duplicated
across all backends, but this patch moves it to a new file.
diff --git a/src/Makefile.am b/src/Makefile.am
index 028735e..c04e3bc 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -33,6 +33,7 @@
 	util.c					\
 	matrix.c				\
 	matrix.h				\
+	gles2-renderer.c			\
 	weston-launch.h				\
 	weston-egl-ext.h
 
diff --git a/src/compositor-android.c b/src/compositor-android.c
index bfa2e95..c2f8654 100644
--- a/src/compositor-android.c
+++ b/src/compositor-android.c
@@ -47,7 +47,6 @@
 
 	struct weston_mode mode;
 	struct android_framebuffer *fb;
-	EGLSurface egl_surface;
 };
 
 struct android_seat {
@@ -122,8 +121,10 @@
 	EGLBoolean ret;
 	static int errored;
 
-	ret = eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
-			     output->egl_surface, compositor->base.egl_context);
+	ret = eglMakeCurrent(compositor->base.egl_display,
+			     output->base.egl_surface,
+			     output->base.egl_surface,
+			     compositor->base.egl_context);
 	if (ret == EGL_FALSE) {
 		if (errored)
 			return -1;
@@ -146,33 +147,13 @@
 }
 
 static void
-android_output_repaint(struct weston_output *base, pixman_region32_t *damage,
-		int flip)
+android_output_repaint(struct weston_output *base, pixman_region32_t *damage)
 {
 	struct android_output *output = to_android_output(base);
-	struct android_compositor *compositor = output->compositor;
-	struct weston_surface *surface;
+        struct android_compositor *compositor = output->compositor;
 	struct wl_event_loop *loop;
-	EGLBoolean ret;
-	static int errored;
 
-	if (android_output_make_current(output) < 0)
-		return;
-
-	wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
-		weston_surface_draw(surface, &output->base, damage);
-
-	if (!flip)
-		return;
-
-	wl_signal_emit(&output->base.frame_signal, output);
-
-	ret = eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
-	if (ret == EGL_FALSE && !errored) {
-		errored = 1;
-		weston_log("Failed in eglSwapBuffers.\n");
-		print_egl_error_state();
-	}
+	gles2_renderer_repaint_output(&output->base, damage);
 
 	/* FIXME: does Android have a way to signal page flip done? */
 	loop = wl_display_get_event_loop(compositor->base.wl_display);
@@ -470,12 +451,12 @@
 		return -1;
 	}
 
-	output->egl_surface =
+	output->base.egl_surface =
 		eglCreateWindowSurface(compositor->base.egl_display,
 				       compositor->base.egl_config,
 				       output->fb->native_window,
 				       NULL);
-	if (output->egl_surface == EGL_NO_SURFACE) {
+	if (output->base.egl_surface == EGL_NO_SURFACE) {
 		weston_log("Failed to create FB EGLSurface.\n");
 		print_egl_error_state();
 		return -1;
diff --git a/src/compositor-drm.c b/src/compositor-drm.c
index c6634a0..439ce51 100644
--- a/src/compositor-drm.c
+++ b/src/compositor-drm.c
@@ -140,7 +140,6 @@
 	struct weston_plane fb_plane;
 	struct weston_surface *cursor_surface;
 	int current_cursor;
-	EGLSurface egl_surface;
 	struct drm_fb *current, *next;
 	struct backlight *backlight;
 };
@@ -322,30 +321,12 @@
 }
 
 static void
-drm_output_render(struct drm_output *output, pixman_region32_t *damage, int flip)
+drm_output_render(struct drm_output *output, pixman_region32_t *damage)
 {
-	struct drm_compositor *compositor =
-		(struct drm_compositor *) output->base.compositor;
-	struct weston_surface *surface;
 	struct gbm_bo *bo;
 
-	if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
-			    output->egl_surface,
-			    compositor->base.egl_context)) {
-		weston_log("failed to make current\n");
-		return;
-	}
+	gles2_renderer_repaint_output(&output->base, damage);
 
-	wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
-		if (surface->plane == &compositor->base.primary_plane)
-			weston_surface_draw(surface, &output->base, damage);
-
-	if (!flip)
-		return;
-
-	wl_signal_emit(&output->base.frame_signal, output);
-
-	eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
 	bo = gbm_surface_lock_front_buffer(output->surface);
 	if (!bo) {
 		weston_log("failed to lock front buffer: %m\n");
@@ -362,7 +343,7 @@
 
 static void
 drm_output_repaint(struct weston_output *output_base,
-		   pixman_region32_t *damage, int flip)
+		   pixman_region32_t *damage)
 {
 	struct drm_output *output = (struct drm_output *) output_base;
 	struct drm_compositor *compositor =
@@ -372,11 +353,9 @@
 	int ret = 0;
 
 	if (!output->next)
-		drm_output_render(output, damage, flip);
+		drm_output_render(output, damage);
 	if (!output->next)
 		return;
-	if (!flip)
-		return;
 
 	mode = container_of(output->base.current, struct drm_mode, base);
 	if (!output->current) {
@@ -885,7 +864,7 @@
 	c->crtc_allocator &= ~(1 << output->crtc_id);
 	c->connector_allocator &= ~(1 << output->connector_id);
 
-	eglDestroySurface(c->base.egl_display, output->egl_surface);
+	eglDestroySurface(c->base.egl_display, output->base.egl_surface);
 	gbm_surface_destroy(output->surface);
 
 	weston_plane_release(&output->fb_plane);
@@ -1029,9 +1008,9 @@
 	}
 	output->next = NULL;
 
-	eglDestroySurface(ec->base.egl_display, output->egl_surface);
+	eglDestroySurface(ec->base.egl_display, output->base.egl_surface);
 	gbm_surface_destroy(output->surface);
-	output->egl_surface = egl_surface;
+	output->base.egl_surface = egl_surface;
 	output->surface = surface;
 
 	/*update output*/
@@ -1511,12 +1490,12 @@
 		goto err_free;
 	}
 
-	output->egl_surface =
+	output->base.egl_surface =
 		eglCreateWindowSurface(ec->base.egl_display,
 				       ec->base.egl_config,
 				       output->surface,
 				       NULL);
-	if (output->egl_surface == EGL_NO_SURFACE) {
+	if (output->base.egl_surface == EGL_NO_SURFACE) {
 		weston_log("failed to create egl surface\n");
 		goto err_surface;
 	}
diff --git a/src/compositor-wayland.c b/src/compositor-wayland.c
index 1f45def..21651aa 100644
--- a/src/compositor-wayland.c
+++ b/src/compositor-wayland.c
@@ -74,13 +74,12 @@
 
 struct wayland_output {
 	struct weston_output	base;
-
+	struct wl_listener	frame_listener;
 	struct {
 		struct wl_surface	*surface;
 		struct wl_shell_surface	*shell_surface;
 		struct wl_egl_window	*egl_window;
 	} parent;
-	EGLSurface egl_surface;
 	struct weston_mode	mode;
 };
 
@@ -329,33 +328,24 @@
 };
 
 static void
-wayland_output_repaint(struct weston_output *output_base,
-		       pixman_region32_t *damage, int flip)
+wayland_output_frame_notify(struct wl_listener *listener, void *data)
 {
-	struct wayland_output *output = (struct wayland_output *) output_base;
-	struct wayland_compositor *compositor =
-		(struct wayland_compositor *) output->base.compositor;
-	struct wl_callback *callback;
-	struct weston_surface *surface;
-
-	if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
-			    output->egl_surface,
-			    compositor->base.egl_context)) {
-		weston_log("failed to make current\n");
-		return;
-	}
-
-	wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
-		weston_surface_draw(surface, &output->base, damage);
-
-	if (!flip)
-		return;
+	struct wayland_output *output =
+		container_of(listener,
+			     struct wayland_output, frame_listener);
 
 	draw_border(output);
+}
 
-	wl_signal_emit(&output->base.frame_signal, output);
+static void
+wayland_output_repaint(struct weston_output *output_base,
+		       pixman_region32_t *damage)
+{
+	struct wayland_output *output = (struct wayland_output *) output_base;
+	struct wl_callback *callback;
 
-	eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
+	gles2_renderer_repaint_output(output_base, damage);
+
 	callback = wl_surface_frame(output->parent.surface);
 	wl_callback_add_listener(callback, &frame_listener, output);
 
@@ -368,7 +358,7 @@
 	struct wayland_output *output = (struct wayland_output *) output_base;
 	struct weston_compositor *ec = output->base.compositor;
 
-	eglDestroySurface(ec->egl_display, output->egl_surface);
+	eglDestroySurface(ec->egl_display, output->base.egl_surface);
 	wl_egl_window_destroy(output->parent.egl_window);
 	free(output);
 
@@ -422,16 +412,16 @@
 		goto cleanup_output;
 	}
 
-	output->egl_surface =
+	output->base.egl_surface =
 		eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
 				       output->parent.egl_window, NULL);
-	if (!output->egl_surface) {
+	if (!output->base.egl_surface) {
 		weston_log("failed to create window surface\n");
 		goto cleanup_window;
 	}
 
-	if (!eglMakeCurrent(c->base.egl_display, output->egl_surface,
-			    output->egl_surface, c->base.egl_context)) {
+	if (!eglMakeCurrent(c->base.egl_display, output->base.egl_surface,
+			    output->base.egl_surface, c->base.egl_context)) {
 		weston_log("failed to make surface current\n");
 		goto cleanup_surface;
 		return -1;
@@ -454,10 +444,13 @@
 
 	wl_list_insert(c->base.output_list.prev, &output->base.link);
 
+	output->frame_listener.notify = wayland_output_frame_notify;
+	wl_signal_add(&output->base.frame_signal, &output->frame_listener);
+
 	return 0;
 
 cleanup_surface:
-	eglDestroySurface(c->base.egl_display, output->egl_surface);
+	eglDestroySurface(c->base.egl_display, output->base.egl_surface);
 cleanup_window:
 	wl_egl_window_destroy(output->parent.egl_window);
 cleanup_output:
diff --git a/src/compositor-x11.c b/src/compositor-x11.c
index d5fa0c6..e7c7d6b 100644
--- a/src/compositor-x11.c
+++ b/src/compositor-x11.c
@@ -105,7 +105,6 @@
 	struct weston_output	base;
 
 	xcb_window_t		window;
-	EGLSurface		egl_surface;
 	struct weston_mode	mode;
 	struct wl_event_source *finish_frame_timer;
 };
@@ -322,29 +321,11 @@
 
 static void
 x11_output_repaint(struct weston_output *output_base,
-		   pixman_region32_t *damage, int flip)
+		   pixman_region32_t *damage)
 {
 	struct x11_output *output = (struct x11_output *)output_base;
-	struct x11_compositor *compositor =
-		(struct x11_compositor *)output->base.compositor;
-	struct weston_surface *surface;
 
-	if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
-			    output->egl_surface,
-			    compositor->base.egl_context)) {
-		weston_log("failed to make current\n");
-		return;
-	}
-
-	wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
-		weston_surface_draw(surface, &output->base, damage);
-
-	if (!flip)
-		return;
-
-	wl_signal_emit(&output->base.frame_signal, output);
-
-	eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
+	gles2_renderer_repaint_output(output_base, damage);
 
 	wl_event_source_timer_update(output->finish_frame_timer, 10);
 }
@@ -373,7 +354,8 @@
 	wl_list_remove(&output->base.link);
 	wl_event_source_remove(output->finish_frame_timer);
 
-	eglDestroySurface(compositor->base.egl_display, output->egl_surface);
+	eglDestroySurface(compositor->base.egl_display,
+			  output->base.egl_surface);
 
 	xcb_destroy_window(compositor->conn, output->window);
 
@@ -579,15 +561,15 @@
 		x11_output_change_state(output, 1,
 					c->atom.net_wm_state_fullscreen);
 
-	output->egl_surface = 
+	output->base.egl_surface = 
 		eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
 				       output->window, NULL);
-	if (!output->egl_surface) {
+	if (!output->base.egl_surface) {
 		weston_log("failed to create window surface\n");
 		return NULL;
 	}
-	if (!eglMakeCurrent(c->base.egl_display, output->egl_surface,
-			    output->egl_surface, c->base.egl_context)) {
+	if (!eglMakeCurrent(c->base.egl_display, output->base.egl_surface,
+			    output->base.egl_surface, c->base.egl_context)) {
 		weston_log("failed to make surface current\n");
 		return NULL;
 	}
diff --git a/src/compositor.c b/src/compositor.c
index 2b963f5..c481083 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -1559,17 +1559,9 @@
 	struct weston_frame_callback *cb, *cnext;
 	struct wl_list frame_callback_list;
 	pixman_region32_t opaque, output_damage, new_damage;
-	int32_t width, height;
 
 	weston_compositor_update_drag_surfaces(ec);
 
-	width = output->current->width +
-		output->border.left + output->border.right;
-	height = output->current->height +
-		output->border.top + output->border.bottom;
-
-	glViewport(0, 0, width, height);
-
 	/* Rebuild the surface list and update surface transforms up front. */
 	wl_list_init(&ec->surface_list);
 	wl_list_init(&frame_callback_list);
@@ -1618,21 +1610,7 @@
 	if (output->dirty)
 		weston_output_update_matrix(output);
 
-	/* if debugging, redraw everything outside the damage to clean up
-	 * debug lines from the previous draw on this buffer:
-	 */
-	if (ec->fan_debug) {
-		pixman_region32_t undamaged;
-		pixman_region32_init(&undamaged);
-		pixman_region32_subtract(&undamaged, &output->region,
-				&output_damage);
-		ec->fan_debug = 0;
-		output->repaint(output, &undamaged, 0);
-		ec->fan_debug = 1;
-		pixman_region32_fini(&undamaged);
-	}
-
-	output->repaint(output, &output_damage, 1);
+	output->repaint(output, &output_damage);
 
 	pixman_region32_fini(&output_damage);
 
diff --git a/src/compositor.h b/src/compositor.h
index 38c2657..23eaeb3 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -149,6 +149,7 @@
 struct weston_output {
 	uint32_t id;
 
+	EGLSurface egl_surface;
 	struct wl_list link;
 	struct wl_list resource_list;
 	struct wl_global *global;
@@ -177,7 +178,7 @@
 	struct wl_list mode_list;
 
 	void (*repaint)(struct weston_output *output,
-			pixman_region32_t *damage, int flip);
+			pixman_region32_t *damage);
 	void (*destroy)(struct weston_output *output);
 	void (*assign_planes)(struct weston_output *output);
 	int (*switch_mode)(struct weston_output *output, struct weston_mode *mode);
@@ -794,4 +795,8 @@
 int
 weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode);
 
+void
+gles2_renderer_repaint_output(struct weston_output *output,
+			      pixman_region32_t *output_damage);
+
 #endif
diff --git a/src/gles2-renderer.c b/src/gles2-renderer.c
new file mode 100644
index 0000000..e4598c9
--- /dev/null
+++ b/src/gles2-renderer.c
@@ -0,0 +1,124 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of the copyright holders not be used in
+ * advertising or publicity pertaining to distribution of the software
+ * without specific, written prior permission.  The copyright holders make
+ * no representations about the suitability of this software for any
+ * purpose.  It is provided "as is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "compositor.h"
+
+static const char *
+egl_error_string(EGLint code)
+{
+#define MYERRCODE(x) case x: return #x;
+	switch (code) {
+	MYERRCODE(EGL_SUCCESS)
+	MYERRCODE(EGL_NOT_INITIALIZED)
+	MYERRCODE(EGL_BAD_ACCESS)
+	MYERRCODE(EGL_BAD_ALLOC)
+	MYERRCODE(EGL_BAD_ATTRIBUTE)
+	MYERRCODE(EGL_BAD_CONTEXT)
+	MYERRCODE(EGL_BAD_CONFIG)
+	MYERRCODE(EGL_BAD_CURRENT_SURFACE)
+	MYERRCODE(EGL_BAD_DISPLAY)
+	MYERRCODE(EGL_BAD_SURFACE)
+	MYERRCODE(EGL_BAD_MATCH)
+	MYERRCODE(EGL_BAD_PARAMETER)
+	MYERRCODE(EGL_BAD_NATIVE_PIXMAP)
+	MYERRCODE(EGL_BAD_NATIVE_WINDOW)
+	MYERRCODE(EGL_CONTEXT_LOST)
+	default:
+		return "unknown";
+	}
+#undef MYERRCODE
+}
+
+static void
+print_egl_error_state(void)
+{
+	EGLint code;
+
+	code = eglGetError();
+	weston_log("EGL error state: %s (0x%04lx)\n",
+		egl_error_string(code), (long)code);
+}
+
+static void
+repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
+{
+	struct weston_compositor *compositor = output->compositor;
+	struct weston_surface *surface;
+
+	wl_list_for_each_reverse(surface, &compositor->surface_list, link)
+		if (surface->plane == &compositor->primary_plane)
+			weston_surface_draw(surface, output, damage);
+}
+
+WL_EXPORT void
+gles2_renderer_repaint_output(struct weston_output *output,
+			      pixman_region32_t *output_damage)
+{
+	struct weston_compositor *compositor = output->compositor;
+	EGLBoolean ret;
+	static int errored;
+	int32_t width, height;
+
+	width = output->current->width +
+		output->border.left + output->border.right;
+	height = output->current->height +
+		output->border.top + output->border.bottom;
+
+	glViewport(0, 0, width, height);
+
+	ret = eglMakeCurrent(compositor->egl_display, output->egl_surface,
+			     output->egl_surface, compositor->egl_context);
+	if (ret == EGL_FALSE) {
+		if (errored)
+			return;
+		errored = 1;
+		weston_log("Failed to make EGL context current.\n");
+		print_egl_error_state();
+		return;
+	}
+
+	/* if debugging, redraw everything outside the damage to clean up
+	 * debug lines from the previous draw on this buffer:
+	 */
+	if (compositor->fan_debug) {
+		pixman_region32_t undamaged;
+		pixman_region32_init(&undamaged);
+		pixman_region32_subtract(&undamaged, &output->region,
+					 output_damage);
+		compositor->fan_debug = 0;
+		repaint_surfaces(output, &undamaged);
+		compositor->fan_debug = 1;
+		pixman_region32_fini(&undamaged);
+	}
+
+	repaint_surfaces(output, output_damage);
+
+	wl_signal_emit(&output->frame_signal, output);
+
+	ret = eglSwapBuffers(compositor->egl_display, output->egl_surface);
+	if (ret == EGL_FALSE && !errored) {
+		errored = 1;
+		weston_log("Failed in eglSwapBuffers.\n");
+		print_egl_error_state();
+	}
+
+}