diff --git a/src/egl/drivers/dri2/platform_android.c b/src/egl/drivers/dri2/platform_android.c index ec60e277014..e0ec9ae795c 100644 --- a/src/egl/drivers/dri2/platform_android.c +++ b/src/egl/drivers/dri2/platform_android.c @@ -41,6 +41,7 @@ #include "util/compiler.h" #include "util/os_file.h" +#include "util/libsync.h" #include "loader.h" #include "egl_dri2.h" @@ -470,6 +471,8 @@ handle_in_fence_fd(struct dri2_egl_surface *dri2_surf, __DRIimage *img) if (dri2_surf->in_fence_fd < 0) return; + validate_fence_fd(dri2_surf->in_fence_fd); + if (dri2_dpy->image->base.version >= 21 && dri2_dpy->image->setInFenceFd != NULL) { dri2_dpy->image->setInFenceFd(img, dri2_surf->in_fence_fd); @@ -481,6 +484,7 @@ handle_in_fence_fd(struct dri2_egl_surface *dri2_surf, __DRIimage *img) static void close_in_fence_fd(struct dri2_egl_surface *dri2_surf) { + validate_fence_fd(dri2_surf->in_fence_fd); if (dri2_surf->in_fence_fd >= 0) close(dri2_surf->in_fence_fd); dri2_surf->in_fence_fd = -1; @@ -497,6 +501,8 @@ droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf) close_in_fence_fd(dri2_surf); + validate_fence_fd(fence_fd); + dri2_surf->in_fence_fd = fence_fd; /* Record all the buffers created by ANativeWindow and update back buffer @@ -1448,6 +1454,7 @@ droid_display_shared_buffer(__DRIdrawable *driDrawable, int fence_fd, } close_in_fence_fd(dri2_surf); + validate_fence_fd(fence_fd); dri2_surf->in_fence_fd = fence_fd; handle_in_fence_fd(dri2_surf, dri2_surf->dri_image_back); } diff --git a/src/gallium/frontends/dri/dri2.c b/src/gallium/frontends/dri/dri2.c index dc87f256c54..8dd8aefe730 100644 --- a/src/gallium/frontends/dri/dri2.c +++ b/src/gallium/frontends/dri/dri2.c @@ -391,6 +391,8 @@ dri2_release_buffer(__DRIscreen *sPriv, __DRIbuffer *bPriv) static void dri2_set_in_fence_fd(__DRIimage *img, int fd) { + validate_fence_fd(fd); + validate_fence_fd(img->in_fence_fd); sync_accumulate("dri", &img->in_fence_fd, fd); } @@ -405,6 +407,8 @@ handle_in_fence(__DRIcontext *context, __DRIimage *img) if (fd == -1) return; + validate_fence_fd(fd); + img->in_fence_fd = -1; pipe->create_fence_fd(pipe, &fence, fd, PIPE_FD_TYPE_NATIVE_SYNC); diff --git a/src/util/libsync.h b/src/util/libsync.h index 0cb895a75d3..248ef96020c 100644 --- a/src/util/libsync.h +++ b/src/util/libsync.h @@ -199,6 +199,19 @@ static inline int sync_accumulate(const char *name, int *fd1, int fd2) return 0; } +/* Helper macro to complain if fd is non-negative and not a valid fence fd. + * Sprinkle this around to help catch fd lifetime issues. + */ +#ifdef DEBUG +# include "util/log.h" +# define validate_fence_fd(fd) do { \ + if (((fd) >= 0) && !sync_valid_fd(fd)) \ + mesa_loge("%s:%d: invalid fence fd: %d", __func__, __LINE__, (fd)); \ + } while (0) +#else +# define validate_fence_fd(fd) do {} while (0) +#endif + #if defined(__cplusplus) } #endif