gl-renderer: Track the buffer type in gl_surface_state

Checking for gs->num_images for determining the previous buffer type
when attaching is not reliable. The number of images is never cleared
in the SHM path, so after a switch from an EGL buffer to SHM, every
following attach of an SHM buffer will happen with gs->num_images > 0,
and the code will assume the previous buffer was an EGL one.

Fix this by adding a buffer_type field to gl_surface_state.
diff --git a/src/gl-renderer.c b/src/gl-renderer.c
index 94ed5b5..9f2d483 100644
--- a/src/gl-renderer.c
+++ b/src/gl-renderer.c
@@ -54,6 +54,12 @@
 	pixman_region32_t buffer_damage[BUFFER_DAMAGE_COUNT];
 };
 
+enum buffer_type {
+	BUFFER_TYPE_NULL,
+	BUFFER_TYPE_SHM,
+	BUFFER_TYPE_EGL
+};
+
 struct gl_surface_state {
 	GLfloat color[4];
 	struct gl_shader *shader;
@@ -67,6 +73,7 @@
 	int num_images;
 
 	struct weston_buffer_reference buffer_ref;
+	enum buffer_type buffer_type;
 	int pitch; /* in pixels */
 	int height; /* in pixels */
 };
@@ -1201,6 +1208,7 @@
 		gs->num_images = 0;
 		glDeleteTextures(gs->num_textures, gs->textures);
 		gs->num_textures = 0;
+		gs->buffer_type = BUFFER_TYPE_NULL;
 		return;
 	}
 
@@ -1211,15 +1219,15 @@
 		buffer->height = wl_shm_buffer_get_height(shm_buffer);
 
 		/* Only allocate a texture if it doesn't match existing one.
-		 * If gs->num_images is not 0, then a switch from DRM allocated
-		 * buffer to a SHM buffer is happening, and we need to allocate
-		 * a new texture buffer. */
+		 * If a switch from DRM allocated buffer to a SHM buffer is
+		 * happening, we need to allocate a new texture buffer. */
 		if (wl_shm_buffer_get_stride(shm_buffer) / 4 != gs->pitch ||
 		    buffer->height != gs->height ||
-		    gs->num_images > 0) {
+		    gs->buffer_type != BUFFER_TYPE_SHM) {
 			gs->pitch =  wl_shm_buffer_get_stride(shm_buffer) / 4;
 			gs->height = buffer->height;
 			gs->target = GL_TEXTURE_2D;
+			gs->buffer_type = BUFFER_TYPE_SHM;
 
 			ensure_textures(gs, 1);
 			glBindTexture(GL_TEXTURE_2D, gs->textures[0]);
@@ -1300,9 +1308,11 @@
 
 		gs->pitch = buffer->width;
 		gs->height = buffer->height;
+		gs->buffer_type = BUFFER_TYPE_EGL;
 	} else {
 		weston_log("unhandled buffer type!\n");
 		weston_buffer_reference(&gs->buffer_ref, NULL);
+		gs->buffer_type = BUFFER_TYPE_NULL;
 	}
 }