hc
2023-02-13 e440ec23c5a540cdd3f7464e8779219be6fd3d95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
From 85894201917e1d1c8d3d4da94a867ee8c8c1a60c Mon Sep 17 00:00:00 2001
From: Jeffy Chen <jeffy.chen@rock-chips.com>
Date: Wed, 28 Nov 2018 17:04:59 +0800
Subject: [PATCH 03/15] qwaylanddisplay: Refactor flushRequests() for multiple
 readers
 
Cancel reading when no events polled, since the other clients, e.g.
waylandsink, might already read the events before us.
 
Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
---
 src/client/qwaylanddisplay.cpp | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
 
diff --git a/src/client/qwaylanddisplay.cpp b/src/client/qwaylanddisplay.cpp
index fe094f6f..ba4ce931 100644
--- a/src/client/qwaylanddisplay.cpp
+++ b/src/client/qwaylanddisplay.cpp
@@ -84,6 +84,7 @@
 #include <QtCore/QDebug>
 
 #include <errno.h>
+#include <poll.h>
 
 QT_BEGIN_NAMESPACE
 
@@ -214,24 +215,38 @@ void QWaylandDisplay::checkError() const
 
 void QWaylandDisplay::flushRequests()
 {
-    if (wl_display_prepare_read(mDisplay) == 0) {
+    struct pollfd fd;
+    int ret;
+
+    // 1. Prepare exclusive reading
+    while (wl_display_prepare_read(mDisplay) != 0)
+        wl_display_dispatch_pending(mDisplay);
+
+    // 2. Nonblock event reading
+    fd.fd = wl_display_get_fd(mDisplay);
+    fd.events = POLLIN | POLLERR | POLLHUP;
+    ret = poll(&fd, 1, 0);
+    if (ret > 0 && fd.revents & POLLIN)
         wl_display_read_events(mDisplay);
-    }
+    else
+        wl_display_cancel_read(mDisplay);
 
-    if (wl_display_dispatch_pending(mDisplay) < 0)
+    // 3. Error check
+    if (wl_display_get_error(mDisplay) != 0)
         checkError();
 
+    // 4. Dispatch events on our queues
+    wl_display_dispatch_pending(mDisplay);
+
     {
         QReadLocker locker(&m_frameQueueLock);
         for (const FrameQueue &q : mExternalQueues) {
             QMutexLocker locker(q.mutex);
-            while (wl_display_prepare_read_queue(mDisplay, q.queue) != 0)
-                wl_display_dispatch_queue_pending(mDisplay, q.queue);
-            wl_display_read_events(mDisplay);
             wl_display_dispatch_queue_pending(mDisplay, q.queue);
         }
     }
 
+    // 5. Final flush
     wl_display_flush(mDisplay);
 }
 
-- 
2.20.1