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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "base/platform_thread.h"
#include "WinCompositorWindowThread.h"
#include "mozilla/gfx/Logging.h"
#include "mozilla/layers/SynchronousTask.h"
#include "mozilla/StaticPtr.h"
#include "transport/runnable_utils.h"
#include "mozilla/StaticPrefs_apz.h"
namespace mozilla {
namespace widget {
static StaticRefPtr<WinCompositorWindowThread> sWinCompositorWindowThread;
/// A window procedure that logs when an input event is received to the gfx
/// error log
///
/// This is done because this window is supposed to be WM_DISABLED, but
/// malfunctioning software may still end up targetting this window. If that
/// happens, it's almost-certainly a bug and should be brought to the attention
/// of the developers that are debugging the issue.
static LRESULT CALLBACK InputEventRejectingWindowProc(HWND window, UINT msg,
WPARAM wparam,
LPARAM lparam) {
switch (msg) {
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
case WM_MOUSEWHEEL:
case WM_MOUSEHWHEEL:
case WM_MOUSEMOVE:
case WM_KEYDOWN:
case WM_KEYUP:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
gfxCriticalNoteOnce
<< "The compositor window received an input event even though it's "
"disabled. There is likely malfunctioning "
"software on the user's machine.";
break;
default:
break;
}
return ::DefWindowProcW(window, msg, wparam, lparam);
}
WinCompositorWindowThread::WinCompositorWindowThread(base::Thread* aThread)
: mThread(aThread), mMonitor("WinCompositorWindowThread") {}
/* static */
WinCompositorWindowThread* WinCompositorWindowThread::Get() {
if (!sWinCompositorWindowThread ||
sWinCompositorWindowThread->mHasAttemptedShutdown) {
return nullptr;
}
return sWinCompositorWindowThread;
}
/* static */
void WinCompositorWindowThread::Start() {
MOZ_ASSERT(NS_IsMainThread());
base::Thread::Options options;
// HWND requests ui thread.
options.message_loop_type = MessageLoop::TYPE_UI;
if (sWinCompositorWindowThread) {
// Try to reuse the thread, which involves stopping and restarting it.
sWinCompositorWindowThread->mThread->Stop();
if (sWinCompositorWindowThread->mThread->StartWithOptions(options)) {
// Success!
sWinCompositorWindowThread->mHasAttemptedShutdown = false;
return;
}
// Restart failed, so null out our sWinCompositorWindowThread and
// try again with a new thread. This will cause the old singleton
// instance to be deallocated, which will destroy its mThread as well.
sWinCompositorWindowThread = nullptr;
}
base::Thread* thread = new base::Thread("WinCompositor");
if (!thread->StartWithOptions(options)) {
delete thread;
return;
}
sWinCompositorWindowThread = new WinCompositorWindowThread(thread);
}
/* static */
void WinCompositorWindowThread::ShutDown() {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(sWinCompositorWindowThread);
sWinCompositorWindowThread->mHasAttemptedShutdown = true;
// Our thread could hang while we're waiting for it to stop.
// Since we're shutting down, that's not a critical problem.
// We set a reasonable amount of time to wait for shutdown,
// and if it succeeds within that time, we correctly stop
// our thread by nulling out the refptr, which will cause it
// to be deallocated and join the thread. If it times out,
// we do nothing, which means that the thread will not be
// joined and sWinCompositorWindowThread memory will leak.
CVStatus status;
{
// It's important to hold the lock before posting the
// runnable. This ensures that the runnable can't begin
// until we've started our Wait, which prevents us from
// Waiting on a monitor that has already been notified.
MonitorAutoLock lock(sWinCompositorWindowThread->mMonitor);
static const TimeDuration TIMEOUT = TimeDuration::FromSeconds(2.0);
RefPtr<Runnable> runnable =
NewRunnableMethod("WinCompositorWindowThread::ShutDownTask",
sWinCompositorWindowThread.get(),
&WinCompositorWindowThread::ShutDownTask);
Loop()->PostTask(runnable.forget());
// Monitor uses SleepConditionVariableSRW, which can have
// spurious wakeups which are reported as timeouts, so we
// check timestamps to ensure that we've waited as long we
// intended to. If we wake early, we don't bother calculating
// a precise amount for the next wait; we just wait the same
// amount of time. This means timeout might happen after as
// much as 2x the TIMEOUT time.
TimeStamp timeStart = TimeStamp::NowLoRes();
do {
status = sWinCompositorWindowThread->mMonitor.Wait(TIMEOUT);
} while ((status == CVStatus::Timeout) &&
((TimeStamp::NowLoRes() - timeStart) < TIMEOUT));
}
if (status == CVStatus::NoTimeout) {
sWinCompositorWindowThread = nullptr;
}
}
void WinCompositorWindowThread::ShutDownTask() {
MonitorAutoLock lock(mMonitor);
MOZ_ASSERT(IsInCompositorWindowThread());
mMonitor.NotifyAll();
}
/* static */
MessageLoop* WinCompositorWindowThread::Loop() {
return sWinCompositorWindowThread
? sWinCompositorWindowThread->mThread->message_loop()
: nullptr;
}
/* static */
bool WinCompositorWindowThread::IsInCompositorWindowThread() {
return sWinCompositorWindowThread &&
sWinCompositorWindowThread->mThread->thread_id() ==
PlatformThread::CurrentId();
}
const wchar_t kClassNameCompositorInitalParent[] =
L"MozillaCompositorInitialParentClass";
const wchar_t kClassNameCompositor[] = L"MozillaCompositorWindowClass";
ATOM g_compositor_inital_parent_window_class;
ATOM g_compositor_window_class;
// This runs on the window owner thread.
void InitializeInitialParentWindowClass() {
if (g_compositor_inital_parent_window_class) {
return;
}
WNDCLASSW wc;
wc.style = 0;
wc.lpfnWndProc = ::DefWindowProcW;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = GetModuleHandle(nullptr);
wc.hIcon = nullptr;
wc.hCursor = nullptr;
wc.hbrBackground = nullptr;
wc.lpszMenuName = nullptr;
wc.lpszClassName = kClassNameCompositorInitalParent;
g_compositor_inital_parent_window_class = ::RegisterClassW(&wc);
}
// This runs on the window owner thread.
void InitializeWindowClass() {
if (g_compositor_window_class) {
return;
}
WNDCLASSW wc;
wc.style = CS_OWNDC;
wc.lpfnWndProc = InputEventRejectingWindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = GetModuleHandle(nullptr);
wc.hIcon = nullptr;
wc.hCursor = nullptr;
wc.hbrBackground = nullptr;
wc.lpszMenuName = nullptr;
wc.lpszClassName = kClassNameCompositor;
g_compositor_window_class = ::RegisterClassW(&wc);
}
/* static */
WinCompositorWnds WinCompositorWindowThread::CreateCompositorWindow() {
MOZ_ASSERT(Loop());
if (!Loop()) {
return WinCompositorWnds(nullptr, nullptr);
}
layers::SynchronousTask task("Create compositor window");
HWND initialParentWnd = nullptr;
HWND compositorWnd = nullptr;
RefPtr<Runnable> runnable = NS_NewRunnableFunction(
"WinCompositorWindowThread::CreateCompositorWindow::Runnable", [&]() {
layers::AutoCompleteTask complete(&task);
InitializeInitialParentWindowClass();
InitializeWindowClass();
// Create initial parent window.
// We could not directly create a compositor window with a main window
// as parent window, so instead create it with a temporary placeholder
// parent. Its parent is set as main window in UI process.
initialParentWnd =
::CreateWindowEx(WS_EX_TOOLWINDOW, kClassNameCompositorInitalParent,
nullptr, WS_POPUP | WS_DISABLED, 0, 0, 1, 1,
nullptr, 0, GetModuleHandle(nullptr), 0);
if (!initialParentWnd) {
gfxCriticalNoteOnce << "Inital parent window failed "
<< ::GetLastError();
return;
}
DWORD extendedStyle = WS_EX_NOPARENTNOTIFY | WS_EX_NOREDIRECTIONBITMAP;
if (!StaticPrefs::apz_windows_force_disable_direct_manipulation()) {
extendedStyle |= WS_EX_LAYERED | WS_EX_TRANSPARENT;
}
compositorWnd = ::CreateWindowEx(
extendedStyle, kClassNameCompositor, nullptr,
WS_CHILDWINDOW | WS_DISABLED | WS_VISIBLE, 0, 0, 1, 1,
initialParentWnd, 0, GetModuleHandle(nullptr), 0);
if (!compositorWnd) {
gfxCriticalNoteOnce << "Compositor window failed "
<< ::GetLastError();
}
});
Loop()->PostTask(runnable.forget());
task.Wait();
return WinCompositorWnds(compositorWnd, initialParentWnd);
}
/* static */
void WinCompositorWindowThread::DestroyCompositorWindow(
WinCompositorWnds aWnds) {
MOZ_ASSERT(aWnds.mCompositorWnd);
MOZ_ASSERT(aWnds.mInitialParentWnd);
MOZ_ASSERT(Loop());
if (!Loop()) {
return;
}
RefPtr<Runnable> runnable = NS_NewRunnableFunction(
"WinCompositorWidget::CreateNativeWindow::Runnable", [aWnds]() {
::DestroyWindow(aWnds.mCompositorWnd);
::DestroyWindow(aWnds.mInitialParentWnd);
});
Loop()->PostTask(runnable.forget());
}
} // namespace widget
} // namespace mozilla
|