compositor: introduce weston_buffer_reference

The wl_buffer reference counting API has been inconsistent. You would
manually increment the refcount and register a destroy listener, as
opposed to calling weston_buffer_post_release(), which internally
decremented the refcount, and then removing a list item.

Replace both cases with a single function:
weston_buffer_reference(weston_buffer_reference *ref, wl_buffer *buffer)

Buffer is assigned to ref->buffer, while taking care of all the refcounting
and release posting. You take a reference by passing a non-NULL buffer, and
release a reference by passing NULL as buffer. The function uses an
internal wl_buffer destroy listener, so the pointer gets reset on
destruction automatically.

This is inspired by the pipe_resource_reference() of Mesa, and modified
by krh's suggestion to add struct weston_buffer_reference.

Additionally, when a surface gets destroyed, the associated wl_buffer
will send a release event. Often the buffer is already destroyed on
client side, so the event will be discarded by libwayland-client.

Compositor-drm.c is converted to use weston_buffer_reference.

Signed-off-by: Pekka Paalanen <ppaalanen@gmail.com>
diff --git a/src/compositor.h b/src/compositor.h
index ff7c932..53f9cfb 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -345,6 +345,11 @@
 	struct weston_xkb_info xkb_info;
 };
 
+struct weston_buffer_reference {
+	struct wl_buffer *buffer;
+	struct wl_listener destroy_listener;
+};
+
 struct weston_region {
 	struct wl_resource resource;
 	pixman_region32_t region;
@@ -437,8 +442,7 @@
 
 	struct wl_list frame_callback_list;
 
-	struct wl_buffer *buffer;
-	struct wl_listener buffer_destroy_listener;
+	struct weston_buffer_reference buffer_ref;
 	uint32_t buffer_transform;
 
 	/* All the pending state, that wl_surface.commit will apply. */
@@ -686,6 +690,10 @@
 void
 weston_buffer_post_release(struct wl_buffer *buffer);
 
+void
+weston_buffer_reference(struct weston_buffer_reference *ref,
+			struct wl_buffer *buffer);
+
 uint32_t
 weston_compositor_get_time(void);