From bcf38ab56ac6637ea557ea73e39f5cac319e8fbf Mon Sep 17 00:00:00 2001
From: Allan Sandfeld Jensen <allan.jensen@qt.io>
Date: Wed, 10 Apr 2019 15:06:35 +0200
Subject: [PATCH] Avoid context-switches when not switching thread contexts
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When UI and GPU threads are the same we not need to do async scheduling
or calls between them, and doing so comes at a much higher cost than
in a multithreaded environment.

Change-Id: Icddee1a78d87ce08362882da5740471dfef1224a
Reviewed-by: Jüri Valdmann <juri.valdmann@qt.io>
---
 src/core/compositor/compositor_resource_tracker.cpp | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/core/compositor/compositor_resource_tracker.cpp b/src/core/compositor/compositor_resource_tracker.cpp
index ba874053e..73242c693 100644
--- a/src/core/compositor/compositor_resource_tracker.cpp
+++ b/src/core/compositor/compositor_resource_tracker.cpp
@@ -213,6 +213,9 @@ void CompositorResourceTracker::scheduleUpdateMailboxes(std::vector<CompositorRe
 #if QT_CONFIG(opengl)
     scoped_refptr<base::SingleThreadTaskRunner> gpuTaskRunner = gpu_task_runner();
     DCHECK(gpuTaskRunner);
+    thread_local bool currentThreadIsGpu = gpuTaskRunner->BelongsToCurrentThread();
+    if (currentThreadIsGpu)
+        return updateMailboxes(std::move(resources));
     gpuTaskRunner->PostTask(
         FROM_HERE,
         base::BindOnce(&CompositorResourceTracker::updateMailboxes,
@@ -243,6 +246,9 @@ void CompositorResourceTracker::updateMailboxes(std::vector<CompositorResource *
 
 void CompositorResourceTracker::scheduleRunSubmitCallback()
 {
+    thread_local bool currentThreadIsUi = content::BrowserThread::CurrentlyOn(content::BrowserThread::UI);
+    if (currentThreadIsUi)
+        return runSubmitCallback();
     base::PostTaskWithTraits(
         FROM_HERE, { content::BrowserThread::UI, base::TaskPriority::USER_VISIBLE },
         base::BindOnce(&CompositorResourceTracker::runSubmitCallback,
-- 
GitLab