[PATCH weston v9 08/62] compositor-drm: Refcount drm_fb

Daniel Stone daniels at collabora.com
Fri Mar 3 23:05:19 UTC 2017


Sometimes we need to duplicate an existing drm_fb, e.g. when
pageflipping to the same buffer to kickstart the repaint loop. To handle
situations like these, and simplify resource management for dumb and
cursor buffers, refcount drm_fb.

drm_fb_get_from_bo has a path where it may reuse a drm_fb, if the BO has
been imported and not released yet. As drm_fb_unref now relies on actual
refcounting (backed up by asserts), we add a balancing drm_fb_ref() to
the path where we return a reused drm_fb.

Differential Revision: https://phabricator.freedesktop.org/D1491

Signed-off-by: Daniel Stone <daniels at collabora.com>
Reviewed-by: Pekka Paalanen <pekka.paalanen at collabora.co.uk>
---
 libweston/compositor-drm.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/libweston/compositor-drm.c b/libweston/compositor-drm.c
index 2e64785..bfb3293 100644
--- a/libweston/compositor-drm.c
+++ b/libweston/compositor-drm.c
@@ -137,6 +137,8 @@ enum drm_fb_type {
 struct drm_fb {
 	enum drm_fb_type type;
 
+	int refcnt;
+
 	uint32_t fb_id, stride, handle, size;
 	const struct pixel_format_info *format;
 	int width, height;
@@ -336,6 +338,8 @@ drm_fb_create_dumb(struct drm_backend *b, int width, int height,
 	if (!fb)
 		return NULL;
 
+	fb->refcnt = 1;
+
 	fb->format = pixel_format_get_info(format);
 	if (!fb->format) {
 		weston_log("failed to look up format 0x%lx\n",
@@ -419,6 +423,13 @@ err_fb:
 }
 
 static struct drm_fb *
+drm_fb_ref(struct drm_fb *fb)
+{
+	fb->refcnt++;
+	return fb;
+}
+
+static struct drm_fb *
 drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
 		   uint32_t format, enum drm_fb_type type)
 {
@@ -428,7 +439,7 @@ drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
 
 	if (fb) {
 		assert(fb->type == type);
-		return fb;
+		return drm_fb_ref(fb);
 	}
 
 	fb = zalloc(sizeof *fb);
@@ -436,6 +447,7 @@ drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
 		return NULL;
 
 	fb->type = type;
+	fb->refcnt = 1;
 	fb->bo = bo;
 
 	fb->width = gbm_bo_get_width(bo);
@@ -510,6 +522,10 @@ drm_fb_unref(struct drm_fb *fb)
 	if (!fb)
 		return;
 
+	assert(fb->refcnt > 0);
+	if (--fb->refcnt > 0)
+		return;
+
 	switch (fb->type) {
 	case BUFFER_PIXMAN_DUMB:
 		/* nothing: pixman buffers are destroyed manually */
-- 
2.9.3



More information about the wayland-devel mailing list