summaryrefslogtreecommitdiffstats
path: root/WebCore/platform/win/SharedTimerWin.cpp
diff options
context:
space:
mode:
authorThe Android Open Source Project <initial-contribution@android.com>2008-12-17 18:05:15 -0800
committerThe Android Open Source Project <initial-contribution@android.com>2008-12-17 18:05:15 -0800
commit1cbdecfa9fc428ac2d8aca0fa91c9580b3d57353 (patch)
tree4457a7306ea5acb43fe05bfe0973b1f7faf97ba2 /WebCore/platform/win/SharedTimerWin.cpp
parent9364f22aed35e1a1e9d07c121510f80be3ab0502 (diff)
downloadexternal_webkit-1cbdecfa9fc428ac2d8aca0fa91c9580b3d57353.zip
external_webkit-1cbdecfa9fc428ac2d8aca0fa91c9580b3d57353.tar.gz
external_webkit-1cbdecfa9fc428ac2d8aca0fa91c9580b3d57353.tar.bz2
Code drop from //branches/cupcake/...@124589
Diffstat (limited to 'WebCore/platform/win/SharedTimerWin.cpp')
-rw-r--r--WebCore/platform/win/SharedTimerWin.cpp113
1 files changed, 95 insertions, 18 deletions
diff --git a/WebCore/platform/win/SharedTimerWin.cpp b/WebCore/platform/win/SharedTimerWin.cpp
index 8ff7734..b611659 100644
--- a/WebCore/platform/win/SharedTimerWin.cpp
+++ b/WebCore/platform/win/SharedTimerWin.cpp
@@ -39,6 +39,21 @@
#endif
#include <windows.h>
+#include <mmsystem.h>
+
+// These aren't in winuser.h with the MSVS 2003 Platform SDK,
+// so use default values in that case.
+#ifndef USER_TIMER_MINIMUM
+#define USER_TIMER_MINIMUM 0x0000000A
+#endif
+
+#ifndef USER_TIMER_MAXIMUM
+#define USER_TIMER_MAXIMUM 0x7FFFFFFF
+#endif
+
+#ifndef QS_RAWINPUT
+#define QS_RAWINPUT 0x0400
+#endif
#if PLATFORM(WIN)
#include "PluginView.h"
@@ -51,9 +66,22 @@ static void (*sharedTimerFiredFunction)();
static HWND timerWindowHandle = 0;
static UINT timerFiredMessage = 0;
-const LPCWSTR kTimerWindowClassName = L"TimerWindowClass";
+static HANDLE timerQueue;
+static HANDLE timer;
+static Mutex timerMutex;
+static bool highResTimerActive;
static bool processingCustomTimerMessage = false;
-const int sharedTimerID = 1000;
+static LONG pendingTimers;
+
+const LPCWSTR kTimerWindowClassName = L"TimerWindowClass";
+const int timerResolution = 1; // To improve timer resolution, we call timeBeginPeriod/timeEndPeriod with this value to increase timer resolution to 1ms.
+const int highResolutionThresholdMsec = 16; // Only activate high-res timer for sub-16ms timers (Windows can fire timers at 16ms intervals without changing the system resolution).
+const int stopHighResTimerInMsec = 300; // Stop high-res timer after 0.3 seconds to lessen power consumption (we don't use a smaller time since oscillating between high and low resolution breaks timer accuracy on XP).
+
+enum {
+ sharedTimerID = 1000,
+ endHighResTimerID = 1001,
+};
LRESULT CALLBACK TimerWindowWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
@@ -69,13 +97,22 @@ LRESULT CALLBACK TimerWindowWndProc(HWND hWnd, UINT message, WPARAM wParam, LPAR
#endif
if (message == timerFiredMessage) {
+ InterlockedExchange(&pendingTimers, 0);
processingCustomTimerMessage = true;
sharedTimerFiredFunction();
processingCustomTimerMessage = false;
- } else if (message == WM_TIMER && wParam == sharedTimerID)
- sharedTimerFiredFunction();
- else
+ } else if (message == WM_TIMER) {
+ if (wParam == sharedTimerID) {
+ KillTimer(timerWindowHandle, sharedTimerID);
+ sharedTimerFiredFunction();
+ } else if (wParam == endHighResTimerID) {
+ KillTimer(timerWindowHandle, endHighResTimerID);
+ highResTimerActive = false;
+ timeEndPeriod(timerResolution);
+ }
+ } else
return DefWindowProc(hWnd, message, wParam, lParam);
+
return 0;
}
@@ -102,6 +139,21 @@ void setSharedTimerFiredFunction(void (*f)())
sharedTimerFiredFunction = f;
}
+static void clearTimer()
+{
+ MutexLocker locker(timerMutex);
+ if (timerQueue && timer)
+ DeleteTimerQueueTimer(timerQueue, timer, 0);
+ timer = 0;
+}
+
+static void NTAPI queueTimerProc(PVOID, BOOLEAN)
+{
+ clearTimer();
+ if (InterlockedIncrement(&pendingTimers) == 1)
+ PostMessage(timerWindowHandle, timerFiredMessage, 0, 0);
+}
+
void setSharedTimerFireTime(double fireTime)
{
ASSERT(sharedTimerFiredFunction);
@@ -118,29 +170,54 @@ void setSharedTimerFireTime(double fireTime)
intervalInMS = (unsigned)interval;
}
- if (timerID) {
- KillTimer(0, timerID);
- timerID = 0;
+ if (interval < highResolutionThresholdMsec) {
+ if (!highResTimerActive) {
+ highResTimerActive = true;
+ timeBeginPeriod(timerResolution);
+ }
+ SetTimer(timerWindowHandle, endHighResTimerID, stopHighResTimerInMsec, 0);
}
- // We don't allow nested PostMessages, since the custom messages will effectively starve
- // painting and user input. (Win32 has a tri-level queue with application messages >
- // user input > WM_PAINT/WM_TIMER.)
- // In addition, if the queue contains input events that have been there since the last call to
- // GetQueueStatus, PeekMessage or GetMessage we favor timers.
initializeOffScreenTimerWindow();
- if (intervalInMS < USER_TIMER_MINIMUM && !processingCustomTimerMessage &&
- !LOWORD(::GetQueueStatus(QS_ALLINPUT))) {
- // Windows SetTimer does not allow timeouts smaller than 10ms (USER_TIMER_MINIMUM)
- PostMessage(timerWindowHandle, timerFiredMessage, 0, 0);
+ bool timerSet = false;
+ DWORD queueStatus = LOWORD(GetQueueStatus(QS_PAINT | QS_MOUSEBUTTON | QS_KEY | QS_RAWINPUT));
+
+ // Win32 has a tri-level queue with application messages > user input > WM_PAINT/WM_TIMER.
+
+ // If the queue doesn't contains input events, we use a higher priorty timer event posting mechanism.
+ if (!(queueStatus & (QS_MOUSEBUTTON | QS_KEY | QS_RAWINPUT))) {
+ if (intervalInMS < USER_TIMER_MINIMUM && !processingCustomTimerMessage && !(queueStatus & QS_PAINT)) {
+ // Call PostMessage immediately if the timer is already expired, unless a paint is pending.
+ // (we prioritize paints over timers)
+ if (InterlockedIncrement(&pendingTimers) == 1)
+ PostMessage(timerWindowHandle, timerFiredMessage, 0, 0);
+ timerSet = true;
+ } else {
+ // Otherwise, delay the PostMessage via a CreateTimerQueueTimer
+ if (!timerQueue)
+ timerQueue = CreateTimerQueue();
+ MutexLocker locker(timerMutex);
+ if (timer)
+ timerSet = ChangeTimerQueueTimer(timerQueue, timer, intervalInMS, 0);
+ else
+ timerSet = CreateTimerQueueTimer(&timer, timerQueue, queueTimerProc, 0, intervalInMS, 0, WT_EXECUTEINTIMERTHREAD | WT_EXECUTEONLYONCE);
+ }
+ }
+
+ if (timerSet) {
+ if (timerID) {
+ KillTimer(timerWindowHandle, timerID);
+ timerID = 0;
+ }
} else
timerID = SetTimer(timerWindowHandle, sharedTimerID, intervalInMS, 0);
}
void stopSharedTimer()
{
+ clearTimer();
if (timerID) {
- KillTimer(0, timerID);
+ KillTimer(timerWindowHandle, timerID);
timerID = 0;
}
}