From 415a2b7c569457ebf85d7ae066cc19bee196d22e Mon Sep 17 00:00:00 2001
From: emersion <contact@emersion.fr>
Date: Mon, 22 Jan 2018 16:42:22 +0100
Subject: render: add wlr_renderer_clear and wlr_renderer_scissor

---
 examples/output-layout.c       |  1 +
 examples/rotation.c            |  1 +
 examples/tablet.c              |  1 +
 examples/touch.c               |  1 +
 include/wlr/render.h           |  4 ++++
 include/wlr/render/egl.h       |  3 ---
 include/wlr/render/interface.h |  4 ++++
 render/gles2/renderer.c        | 40 +++++++++++++++++++++++++++++-----------
 render/wlr_renderer.c          |  9 +++++++++
 rootston/output.c              | 26 +++++++++++++++++---------
 10 files changed, 67 insertions(+), 23 deletions(-)

diff --git a/examples/output-layout.c b/examples/output-layout.c
index b97d3723..0c85ba7f 100644
--- a/examples/output-layout.c
+++ b/examples/output-layout.c
@@ -102,6 +102,7 @@ static void handle_output_frame(struct output_state *output,
 
 	wlr_output_make_current(wlr_output, NULL);
 	wlr_renderer_begin(sample->renderer, wlr_output);
+	wlr_renderer_clear(sample->renderer, 0.25f, 0.25f, 0.25f, 1);
 
 	animate_cat(sample, output->output);
 
diff --git a/examples/rotation.c b/examples/rotation.c
index 64de73f9..4f7b1567 100644
--- a/examples/rotation.c
+++ b/examples/rotation.c
@@ -44,6 +44,7 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts
 
 	wlr_output_make_current(wlr_output, NULL);
 	wlr_renderer_begin(sample->renderer, wlr_output);
+	wlr_renderer_clear(sample->renderer, 0.25f, 0.25f, 0.25f, 1);
 
 	float matrix[16];
 	for (int y = -128 + (int)odata->y_offs; y < height; y += 128) {
diff --git a/examples/tablet.c b/examples/tablet.c
index 35326664..f12ecbc4 100644
--- a/examples/tablet.c
+++ b/examples/tablet.c
@@ -44,6 +44,7 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts
 
 	wlr_output_make_current(wlr_output, NULL);
 	wlr_renderer_begin(sample->renderer, wlr_output);
+	wlr_renderer_clear(sample->renderer, 0.25f, 0.25f, 0.25f, 1);
 
 	float matrix[16], view[16];
 	float distance = 0.8f * (1 - sample->distance);
diff --git a/examples/touch.c b/examples/touch.c
index 74642b96..2ef2712f 100644
--- a/examples/touch.c
+++ b/examples/touch.c
@@ -43,6 +43,7 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts
 
 	wlr_output_make_current(wlr_output, NULL);
 	wlr_renderer_begin(sample->renderer, wlr_output);
+	wlr_renderer_clear(sample->renderer, 0.25f, 0.25f, 0.25f, 1);
 
 	float matrix[16];
 	struct touch_point *p;
diff --git a/include/wlr/render.h b/include/wlr/render.h
index 5027064d..bfd9e829 100644
--- a/include/wlr/render.h
+++ b/include/wlr/render.h
@@ -5,6 +5,7 @@
 #include <EGL/egl.h>
 #include <EGL/eglext.h>
 #include <wayland-server-protocol.h>
+#include <wlr/types/wlr_box.h>
 #include <wlr/types/wlr_output.h>
 
 struct wlr_texture;
@@ -12,6 +13,9 @@ struct wlr_renderer;
 
 void wlr_renderer_begin(struct wlr_renderer *r, struct wlr_output *output);
 void wlr_renderer_end(struct wlr_renderer *r);
+void wlr_renderer_clear(struct wlr_renderer *r, float red, float green,
+	float blue, float alpha);
+void wlr_renderer_scissor(struct wlr_renderer *r, struct wlr_box *box);
 /**
  * Requests a texture handle from this renderer.
  */
diff --git a/include/wlr/render/egl.h b/include/wlr/render/egl.h
index c292a6f8..6979fd9b 100644
--- a/include/wlr/render/egl.h
+++ b/include/wlr/render/egl.h
@@ -72,7 +72,4 @@ const char *egl_error(void);
 bool wlr_egl_make_current(struct wlr_egl *egl, EGLSurface surface,
 	int *buffer_age);
 
-// TODO: remove
-int wlr_egl_get_buffer_age(struct wlr_egl *egl, EGLSurface surface);
-
 #endif
diff --git a/include/wlr/render/interface.h b/include/wlr/render/interface.h
index bbc5acb4..3927795d 100644
--- a/include/wlr/render/interface.h
+++ b/include/wlr/render/interface.h
@@ -6,6 +6,7 @@
 #include <EGL/eglext.h>
 #include <stdbool.h>
 #include <wlr/render.h>
+#include <wlr/types/wlr_box.h>
 #include <wlr/types/wlr_output.h>
 
 struct wlr_renderer_impl;
@@ -17,6 +18,9 @@ struct wlr_renderer {
 struct wlr_renderer_impl {
 	void (*begin)(struct wlr_renderer *renderer, struct wlr_output *output);
 	void (*end)(struct wlr_renderer *renderer);
+	void (*clear)(struct wlr_renderer *renderer, float red, float green,
+		float blue, float alpha);
+	void (*scissor)(struct wlr_renderer *renderer, struct wlr_box *box);
 	struct wlr_texture *(*texture_create)(struct wlr_renderer *renderer);
 	bool (*render_with_matrix)(struct wlr_renderer *renderer,
 		struct wlr_texture *texture, const float (*matrix)[16]);
diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c
index 32f2eb02..3909fd84 100644
--- a/render/gles2/renderer.c
+++ b/render/gles2/renderer.c
@@ -105,7 +105,7 @@ static void init_globals() {
 	init_default_shaders();
 }
 
-static void wlr_gles2_begin(struct wlr_renderer *_renderer,
+static void wlr_gles2_begin(struct wlr_renderer *wlr_renderer,
 		struct wlr_output *output) {
 	GL_CALL(glViewport(0, 0, output->width, output->height));
 
@@ -117,14 +117,30 @@ static void wlr_gles2_begin(struct wlr_renderer *_renderer,
 	// for users to sling matricies themselves
 }
 
-static void wlr_gles2_end(struct wlr_renderer *renderer) {
+static void wlr_gles2_end(struct wlr_renderer *wlr_renderer) {
 	// no-op
 }
 
+static void wlr_gles2_clear(struct wlr_renderer *wlr_renderer, float red,
+		float green, float blue, float alpha) {
+	glClearColor(red, green, blue, alpha);
+	glClear(GL_COLOR_BUFFER_BIT);
+}
+
+static void wlr_gles2_scissor(struct wlr_renderer *wlr_renderer,
+		struct wlr_box *box) {
+	if (box != NULL) {
+		glScissor(box->x, box->y, box->width, box->height);
+		glEnable(GL_SCISSOR_TEST);
+	} else {
+		glDisable(GL_SCISSOR_TEST);
+	}
+}
+
 static struct wlr_texture *wlr_gles2_texture_create(
-		struct wlr_renderer *_renderer) {
+		struct wlr_renderer *wlr_renderer) {
 	struct wlr_gles2_renderer *renderer =
-		(struct wlr_gles2_renderer *)_renderer;
+		(struct wlr_gles2_renderer *)wlr_renderer;
 	return gles2_texture_create(renderer->egl);
 }
 
@@ -154,7 +170,7 @@ static void draw_quad() {
 	GL_CALL(glDisableVertexAttribArray(1));
 }
 
-static bool wlr_gles2_render_texture(struct wlr_renderer *_renderer,
+static bool wlr_gles2_render_texture(struct wlr_renderer *wlr_renderer,
 		struct wlr_texture *texture, const float (*matrix)[16]) {
 	if (!texture || !texture->valid) {
 		wlr_log(L_ERROR, "attempt to render invalid texture");
@@ -169,7 +185,7 @@ static bool wlr_gles2_render_texture(struct wlr_renderer *_renderer,
 	return true;
 }
 
-static void wlr_gles2_render_quad(struct wlr_renderer *renderer,
+static void wlr_gles2_render_quad(struct wlr_renderer *wlr_renderer,
 		const float (*color)[4], const float (*matrix)[16]) {
 	GL_CALL(glUseProgram(shaders.quad));
 	GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix));
@@ -177,7 +193,7 @@ static void wlr_gles2_render_quad(struct wlr_renderer *renderer,
 	draw_quad();
 }
 
-static void wlr_gles2_render_ellipse(struct wlr_renderer *renderer,
+static void wlr_gles2_render_ellipse(struct wlr_renderer *wlr_renderer,
 		const float (*color)[4], const float (*matrix)[16]) {
 	GL_CALL(glUseProgram(shaders.ellipse));
 	GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix));
@@ -197,10 +213,10 @@ static const enum wl_shm_format *wlr_gles2_formats(
 	return formats;
 }
 
-static bool wlr_gles2_buffer_is_drm(struct wlr_renderer *_renderer,
+static bool wlr_gles2_buffer_is_drm(struct wlr_renderer *wlr_renderer,
 		struct wl_resource *buffer) {
 	struct wlr_gles2_renderer *renderer =
-		(struct wlr_gles2_renderer *)_renderer;
+		(struct wlr_gles2_renderer *)wlr_renderer;
 	EGLint format;
 	return wlr_egl_query_buffer(renderer->egl, buffer,
 		EGL_TEXTURE_FORMAT, &format);
@@ -216,8 +232,8 @@ static void rgba_to_argb(uint32_t *data, size_t height, size_t stride) {
 	}
 }
 
-static void wlr_gles2_read_pixels(struct wlr_renderer *renderer, int x, int y,
-		int width, int height, void *out_data) {
+static void wlr_gles2_read_pixels(struct wlr_renderer *wlr_renderer,
+		int x, int y, int width, int height, void *out_data) {
 	glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, out_data);
 	rgba_to_argb(out_data, height, width*4);
 }
@@ -225,6 +241,8 @@ static void wlr_gles2_read_pixels(struct wlr_renderer *renderer, int x, int y,
 static struct wlr_renderer_impl wlr_renderer_impl = {
 	.begin = wlr_gles2_begin,
 	.end = wlr_gles2_end,
+	.clear = wlr_gles2_clear,
+	.scissor = wlr_gles2_scissor,
 	.texture_create = wlr_gles2_texture_create,
 	.render_with_matrix = wlr_gles2_render_texture,
 	.render_quad = wlr_gles2_render_quad,
diff --git a/render/wlr_renderer.c b/render/wlr_renderer.c
index ef0c31be..ba7d4b74 100644
--- a/render/wlr_renderer.c
+++ b/render/wlr_renderer.c
@@ -23,6 +23,15 @@ void wlr_renderer_end(struct wlr_renderer *r) {
 	r->impl->end(r);
 }
 
+void wlr_renderer_clear(struct wlr_renderer *r, float red, float green,
+		float blue, float alpha) {
+	r->impl->clear(r, red, green, blue, alpha);
+}
+
+void wlr_renderer_scissor(struct wlr_renderer *r, struct wlr_box *box) {
+	r->impl->scissor(r, box);
+}
+
 struct wlr_texture *wlr_render_texture_create(struct wlr_renderer *r) {
 	return r->impl->texture_create(r);
 }
diff --git a/rootston/output.c b/rootston/output.c
index 644fca92..b6879965 100644
--- a/rootston/output.c
+++ b/rootston/output.c
@@ -2,7 +2,6 @@
 #include <time.h>
 #include <stdlib.h>
 #include <stdbool.h>
-#include <GLES2/gl2.h>
 #include <wlr/types/wlr_output_layout.h>
 #include <wlr/types/wlr_compositor.h>
 #include <wlr/types/wlr_wl_shell.h>
@@ -247,8 +246,13 @@ static void render_surface(struct wlr_surface *surface, double lx, double ly,
 	pixman_box32_t *rects =
 		pixman_region32_rectangles(&surface_damage, &nrects);
 	for (int i = 0; i < nrects; ++i) {
-		glScissor(rects[i].x1, output->wlr_output->height - rects[i].y2,
-			rects[i].x2 - rects[i].x1, rects[i].y2 - rects[i].y1);
+		struct wlr_box scissor = {
+			.x = rects[i].x1,
+			.y = output->wlr_output->height - rects[i].y2,
+			.width = rects[i].x2 - rects[i].x1,
+			.height = rects[i].y2 - rects[i].y1,
+		};
+		wlr_renderer_scissor(output->desktop->server->renderer, &scissor);
 		wlr_render_with_matrix(output->desktop->server->renderer,
 			surface->texture, &matrix);
 	}
@@ -353,7 +357,6 @@ static void render_output(struct roots_output *output) {
 	};
 
 	wlr_renderer_begin(server->renderer, wlr_output);
-	glEnable(GL_SCISSOR_TEST);
 
 	if (!pixman_region32_not_empty(&damage)) {
 		// Output isn't damaged but needs buffer swap
@@ -363,10 +366,15 @@ static void render_output(struct roots_output *output) {
 	int nrects;
 	pixman_box32_t *rects = pixman_region32_rectangles(&damage, &nrects);
 	for (int i = 0; i < nrects; ++i) {
-		glScissor(rects[i].x1, wlr_output->height - rects[i].y2,
-			rects[i].x2 - rects[i].x1, rects[i].y2 - rects[i].y1);
-		glClearColor(clear_color[0], clear_color[1], clear_color[2], 1);
-		glClear(GL_COLOR_BUFFER_BIT);
+		struct wlr_box scissor = {
+			.x = rects[i].x1,
+			.y = wlr_output->height - rects[i].y2,
+			.width = rects[i].x2 - rects[i].x1,
+			.height = rects[i].y2 - rects[i].y1,
+		};
+		wlr_renderer_scissor(output->desktop->server->renderer, &scissor);
+		wlr_renderer_clear(output->desktop->server->renderer,
+			clear_color[0], clear_color[1], clear_color[2], 1);
 	}
 
 	// If a view is fullscreen on this output, render it
@@ -425,7 +433,7 @@ static void render_output(struct roots_output *output) {
 	}
 
 renderer_end:
-	glDisable(GL_SCISSOR_TEST);
+	wlr_renderer_scissor(output->desktop->server->renderer, NULL);
 	wlr_renderer_end(server->renderer);
 	wlr_output_swap_buffers(wlr_output, &now, &damage);
 	output->frame_pending = true;
-- 
cgit v1.2.3