From ca3f93e58a3cc3545b3d9fa5596eb7283ab3fc37 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Samuel=20R=C3=B8dal?= <samuel.rodal@nokia.com>
Date: Thu, 1 Mar 2012 18:06:29 +0100
Subject: [PATCH] Fixed Windows performance regression introduced by ac2818bef95f134.

Only create a temporary DC if we're not on the main thread.

Change-Id: Ia52cf7f44d8e55d537b0e520ee9d6b9633f4f3f3
---
 src/gui/kernel/qwidget_win.cpp |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/gui/kernel/qwidget_win.cpp b/src/gui/kernel/qwidget_win.cpp
index 1881a4a..49dbe9d 100644
--- a/src/gui/kernel/qwidget_win.cpp
+++ b/src/gui/kernel/qwidget_win.cpp
@@ -49,6 +49,7 @@
 #include "qlayout.h"
 #include "qpainter.h"
 #include "qstack.h"
+#include "qthread.h"
 #include "qt_windows.h"
 #include "qwidget.h"
 #include "qwidget_p.h"
@@ -1636,6 +1637,8 @@ void QWidgetPrivate::scroll_sys(int dx, int dy, const QRect &r)
     }
 }
 
+extern Q_GUI_EXPORT HDC qt_win_display_dc();
+
 int QWidget::metric(PaintDeviceMetric m) const
 {
     Q_D(const QWidget);
@@ -1645,7 +1648,8 @@ int QWidget::metric(PaintDeviceMetric m) const
     } else if (m == PdmHeight) {
         val = data->crect.height();
     } else {
-        HDC gdc = GetDC(0);
+        bool ownDC = QThread::currentThread() != qApp->thread();
+        HDC gdc = ownDC ? GetDC(0) : qt_win_display_dc();
         switch (m) {
         case PdmDpiX:
         case PdmPhysicalDpiX:
@@ -1696,7 +1700,8 @@ int QWidget::metric(PaintDeviceMetric m) const
             val = 0;
             qWarning("QWidget::metric: Invalid metric command");
         }
-        ReleaseDC(0, gdc);
+        if (ownDC)
+            ReleaseDC(0, gdc);
     }
     return val;
 }
-- 
1.7.1

