From 579e33549dc2293bf16b5571483bac2d36567318 Mon Sep 17 00:00:00 2001 From: Rob Clark Date: Mon, 31 Aug 2015 13:31:15 -0400 Subject: WIP: synchronize gralloc entry points Note entirely sure what synchronization guarantees android provides before calling in to module.. but stuff inside gralloc module is definately not thread-safe so slap a big lock around the outside. --- gralloc.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 15 deletions(-) diff --git a/gralloc.c b/gralloc.c index 92866ca..b338ab9 100644 --- a/gralloc.c +++ b/gralloc.c @@ -32,6 +32,8 @@ #include "gralloc_drm.h" #include "gralloc_drm_priv.h" +static pthread_mutex_t gralloc_lock = PTHREAD_MUTEX_INITIALIZER; + /* * Initialize the DRM device object, optionally with KMS. */ @@ -114,13 +116,23 @@ static int drm_mod_register_buffer(const gralloc_module_t *mod, if (err) return err; - return gralloc_drm_handle_register(handle, dmod->drm); + pthread_mutex_lock(&gralloc_lock); + err = gralloc_drm_handle_register(handle, dmod->drm); + pthread_mutex_unlock(&gralloc_lock); + + return err; } static int drm_mod_unregister_buffer(const gralloc_module_t *mod, buffer_handle_t handle) { - return gralloc_drm_handle_unregister(handle); + int err; + + pthread_mutex_lock(&gralloc_lock); + err = gralloc_drm_handle_unregister(handle); + pthread_mutex_unlock(&gralloc_lock); + + return err; } static int drm_mod_lock(const gralloc_module_t *mod, buffer_handle_t handle, @@ -129,25 +141,40 @@ static int drm_mod_lock(const gralloc_module_t *mod, buffer_handle_t handle, struct gralloc_drm_bo_t *bo; int err; + pthread_mutex_lock(&gralloc_lock); + bo = gralloc_drm_bo_from_handle(handle); - if (!bo) - return -EINVAL; + if (!bo) { + err = -EINVAL; + goto unlock; + } - return gralloc_drm_bo_lock(bo, usage, x, y, w, h, ptr); + err = gralloc_drm_bo_lock(bo, usage, x, y, w, h, ptr); + +unlock: + pthread_mutex_unlock(&gralloc_lock); + return err; } static int drm_mod_unlock(const gralloc_module_t *mod, buffer_handle_t handle) { struct drm_module_t *dmod = (struct drm_module_t *) mod; struct gralloc_drm_bo_t *bo; + int err = 0; + + pthread_mutex_lock(&gralloc_lock); bo = gralloc_drm_bo_from_handle(handle); - if (!bo) - return -EINVAL; + if (!bo) { + err = -EINVAL; + goto unlock; + } gralloc_drm_bo_unlock(bo); - return 0; +unlock: + pthread_mutex_unlock(&gralloc_lock); + return err; } static int drm_mod_close_gpu0(struct hw_device_t *dev) @@ -163,14 +190,21 @@ static int drm_mod_free_gpu0(alloc_device_t *dev, buffer_handle_t handle) { struct drm_module_t *dmod = (struct drm_module_t *) dev->common.module; struct gralloc_drm_bo_t *bo; + int err = 0; + + pthread_mutex_lock(&gralloc_lock); bo = gralloc_drm_bo_from_handle(handle); - if (!bo) - return -EINVAL; + if (!bo) { + err = -EINVAL; + goto unlock; + } gralloc_drm_bo_decref(bo); - return 0; +unlock: + pthread_mutex_unlock(&gralloc_lock); + return err; } static int drm_mod_alloc_gpu0(alloc_device_t *dev, @@ -179,15 +213,19 @@ static int drm_mod_alloc_gpu0(alloc_device_t *dev, { struct drm_module_t *dmod = (struct drm_module_t *) dev->common.module; struct gralloc_drm_bo_t *bo; - int size, bpp, err; + int size, bpp, err = 0; bpp = gralloc_drm_get_bpp(format); if (!bpp) return -EINVAL; + pthread_mutex_lock(&gralloc_lock); + bo = gralloc_drm_bo_create(dmod->drm, w, h, format, usage); - if (!bo) - return -ENOMEM; + if (!bo) { + err = -ENOMEM; + goto unlock; + } if (gralloc_drm_bo_need_fb(bo)) { err = gralloc_drm_bo_add_fb(bo); @@ -202,7 +240,9 @@ static int drm_mod_alloc_gpu0(alloc_device_t *dev, /* in pixels */ *stride /= bpp; - return 0; +unlock: + pthread_mutex_unlock(&gralloc_lock); + return err; } static int drm_mod_open_gpu0(struct drm_module_t *dmod, hw_device_t **dev) -- cgit v1.1