From d947f50363d2320010cfbf79d86b55ebf14839dc Mon Sep 17 00:00:00 2001
From: Esben Haabendal <esben@geanix.com>
Date: Wed, 22 Feb 2023 14:05:23 +0100
Subject: [PATCH] openharmony-standard: Simplified version of drmWaitVBlank
 hack

This simplifies and improves the hack added to libdrm to handle drivers (like
virtio drm) that does not support DRM_IOCTL_WAIT_VBLANK, and tries to return at
60 Hz points in time.

Signed-off-by: Esben Haabendal <esben@geanix.com>
---
 .../xf86drm.c-Add-drmWaitVBlank-hack.patch    | 45 ++++++-------------
 1 file changed, 14 insertions(+), 31 deletions(-)

diff --git a/recipes-openharmony/openharmony/openharmony-standard-3.0/xf86drm.c-Add-drmWaitVBlank-hack.patch b/recipes-openharmony/openharmony/openharmony-standard-3.0/xf86drm.c-Add-drmWaitVBlank-hack.patch
index 9714e3e4..2394595b 100644
--- a/recipes-openharmony/openharmony/openharmony-standard-3.0/xf86drm.c-Add-drmWaitVBlank-hack.patch
+++ b/recipes-openharmony/openharmony/openharmony-standard-3.0/xf86drm.c-Add-drmWaitVBlank-hack.patch
@@ -9,43 +9,26 @@ is essential for the inner workings of the OpenHarmony graphic stack, therefore
 adding a hack that simulates this behaviour until a proper solution is
 provided.
 
-Signed-off-by: Robert Drab <robert.drab@huawei.com>
-Upstream-Status: Inappropriate
+Signed-off-by: Esben Haabendal <esben@geanix.com>
+Upstream-Status: Pending
 
 diff --git a/xf86drm.c b/xf86drm.c
-index b49d42f..0e2805a 100644
+index b49d42f70dbe..ab8bb563c344 100644
 --- a/xf86drm.c
 +++ b/xf86drm.c
-@@ -2171,14 +2171,15 @@ drm_public int drmWaitVBlank(int fd, drmVBlankPtr vbl)
-         fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
-         goto out;
-     }
--    timeout.tv_sec++;
-+    /* HACK: return 0 after 16ms - value observed on the Taurus board */
-+    timeout.tv_nsec += 16000000;
- 
-     do {
-        ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
-        vbl->request.type &= ~DRM_VBLANK_RELATIVE;
--       if (ret && errno == EINTR) {
-+       if (ret && (errno == EINTR || errno == ENOTSUP)) {
-            clock_gettime(CLOCK_MONOTONIC, &cur);
--           /* Timeout after 1s */
-+           /* HACK: return 0 after 16ms - value observed on the Taurus board */
-            if (cur.tv_sec > timeout.tv_sec + 1 ||
-                (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
-                 timeout.tv_nsec)) {
-@@ -2187,9 +2188,12 @@ drm_public int drmWaitVBlank(int fd, drmVBlankPtr vbl)
+@@ -2187,6 +2187,15 @@ drm_public int drmWaitVBlank(int fd, drmVBlankPtr vbl)
                     break;
             }
-+           usleep(10000);
         }
--    } while (ret && errno == EINTR);
-+    } while (ret && (errno == EINTR || errno == ENOTSUP));
++       else if (ret && errno == ENOTSUP) {
++           /* Simulate VBLANK @ 60Hz when DRM driver does not support VBLANK */
++           long delay_us;
++           clock_gettime(CLOCK_MONOTONIC, &cur);
++           delay_us = (cur.tv_nsec % (1000000000L / 60)) / 1000;
++           if (delay_us >= 10) // let's not wait less than 10 us
++               usleep(delay_us);
++           return 0;
++       }
+     } while (ret && errno == EINTR);
  
  out:
-+    if (errno == EBUSY)
-+        return 0;
-     return ret;
- }
- 
-- 
GitLab