2016-05-05 08:00:13 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
2016-07-01 11:15:16 +03:00
|
|
|
#include "WinCompositorWidget.h"
|
2016-08-24 01:18:55 +03:00
|
|
|
#include "gfxPrefs.h"
|
|
|
|
#include "mozilla/gfx/DeviceManagerDx.h"
|
2016-05-05 08:00:14 +03:00
|
|
|
#include "mozilla/gfx/Point.h"
|
2016-08-24 01:18:55 +03:00
|
|
|
#include "mozilla/layers/Compositor.h"
|
2016-07-01 11:15:16 +03:00
|
|
|
#include "mozilla/widget/PlatformWidgetTypes.h"
|
2016-07-19 21:56:07 +03:00
|
|
|
#include "nsWindow.h"
|
|
|
|
#include "VsyncDispatcher.h"
|
2018-03-29 05:21:47 +03:00
|
|
|
#include "WinCompositorWindowThread.h"
|
2016-05-05 08:00:13 +03:00
|
|
|
|
2016-08-24 01:18:55 +03:00
|
|
|
#include <ddraw.h>
|
|
|
|
|
2016-05-05 08:00:13 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace widget {
|
|
|
|
|
2016-05-05 08:00:14 +03:00
|
|
|
using namespace mozilla::gfx;
|
2018-06-21 23:17:48 +03:00
|
|
|
using namespace mozilla;
|
2016-05-05 08:00:14 +03:00
|
|
|
|
Bug 1373739 - Make headless compositing Windows-compatible, in addition to Linux. r=dvander
To make the HeadlessCompositorWidget work under Windows as well as Linux, I had
to change the way that I hooked it into the existing CompositorWidget system.
Under GTK, the CompositorWidgetInitData and CompositorWidgetDelegate types
provided the information needed by the headless compositor widget already (the
widget client size). On Windows, however, the definitions of these types
differ, and the client size information is simply retrieved from the platform
APIs when needed.
After this patch, CompositorWidgetDelegate is renamed to
PlatformCompositorWidgetDelegate, and a new base class called
CompositorWidgetDelegate is added with "AsPlatformSpecificDelegate()" and
"AsHeadlessCompositorWidget()" methods. In non-headless mode, widgets use
AsPlatformSpecificDelegate() to access the Windows- and GTK-specific delegate
APIs. In headless mode, AsHeadlessCompositorWidget() is used to access the
singular CompositorWidget implementation for headless. Meanwhile, the
CompositorWidgetInitData IPDL type is made into a union which always contains a
headless-specific HeadlessCompositorWidgetInitData struct and under GTK and
Windows also contains an {X11,Win}CompositorWidgetInitData struct.
This also includes a small patch to ensure that the GPU process and
hardware-accelerated compositing are always disabled under headless mode. These
features weren't activated by default in the Linux environments I tested in, but
did end up activating (and then promptly crashing Firefox) when I tested on
Windows.
MozReview-Commit-ID: CocPoHBDV7H
--HG--
extra : rebase_source : 4581fa63aa3a9f32a8dc2672015a35b9be01b20f
2017-07-07 03:45:34 +03:00
|
|
|
WinCompositorWidget::WinCompositorWidget(
|
|
|
|
const WinCompositorWidgetInitData& aInitData,
|
2017-01-13 01:29:42 +03:00
|
|
|
const layers::CompositorOptions& aOptions)
|
|
|
|
: CompositorWidget(aOptions),
|
|
|
|
mWidgetKey(aInitData.widgetKey()),
|
2016-07-01 11:15:16 +03:00
|
|
|
mWnd(reinterpret_cast<HWND>(aInitData.hWnd())),
|
2018-03-29 05:21:47 +03:00
|
|
|
mCompositorWnd(nullptr),
|
2018-06-21 23:17:48 +03:00
|
|
|
mTransparentSurfaceLock("mTransparentSurfaceLock"),
|
2018-04-06 06:24:28 +03:00
|
|
|
mTransparencyMode(aInitData.transparencyMode()),
|
2016-05-05 08:00:14 +03:00
|
|
|
mMemoryDC(nullptr),
|
2016-05-12 17:23:11 +03:00
|
|
|
mCompositeDC(nullptr),
|
|
|
|
mLockedBackBufferData(nullptr) {
|
2016-05-05 08:00:13 +03:00
|
|
|
MOZ_ASSERT(mWnd && ::IsWindow(mWnd));
|
2016-08-24 01:18:55 +03:00
|
|
|
|
|
|
|
// mNotDeferEndRemoteDrawing is set on the main thread during init,
|
|
|
|
// but is only accessed after on the compositor thread.
|
|
|
|
mNotDeferEndRemoteDrawing = gfxPrefs::LayersCompositionFrameRate() == 0 ||
|
|
|
|
gfxPlatform::IsInLayoutAsapMode() ||
|
|
|
|
gfxPlatform::ForceSoftwareVsync();
|
2016-05-05 08:00:13 +03:00
|
|
|
}
|
|
|
|
|
2018-03-29 05:21:47 +03:00
|
|
|
WinCompositorWidget::~WinCompositorWidget() { DestroyCompositorWindow(); }
|
|
|
|
|
2016-07-01 11:15:16 +03:00
|
|
|
void WinCompositorWidget::OnDestroyWindow() {
|
2018-06-21 23:17:48 +03:00
|
|
|
MutexAutoLock lock(mTransparentSurfaceLock);
|
2016-05-05 08:00:14 +03:00
|
|
|
mTransparentSurface = nullptr;
|
|
|
|
mMemoryDC = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-08 05:16:52 +03:00
|
|
|
bool WinCompositorWidget::PreRender(WidgetRenderingContext* aContext) {
|
2016-05-05 08:00:13 +03:00
|
|
|
// This can block waiting for WM_SETTEXT to finish
|
|
|
|
// Using PreRender is unnecessarily pessimistic because
|
|
|
|
// we technically only need to block during the present call
|
|
|
|
// not all of compositor rendering
|
|
|
|
mPresentLock.Enter();
|
|
|
|
return true;
|
2016-05-05 08:00:13 +03:00
|
|
|
}
|
|
|
|
|
2016-11-08 05:16:52 +03:00
|
|
|
void WinCompositorWidget::PostRender(WidgetRenderingContext* aContext) {
|
2016-05-05 08:00:13 +03:00
|
|
|
mPresentLock.Leave();
|
2016-05-05 08:00:13 +03:00
|
|
|
}
|
|
|
|
|
2016-07-01 11:15:16 +03:00
|
|
|
LayoutDeviceIntSize WinCompositorWidget::GetClientSize() {
|
2016-05-05 08:00:13 +03:00
|
|
|
RECT r;
|
|
|
|
if (!::GetClientRect(mWnd, &r)) {
|
|
|
|
return LayoutDeviceIntSize();
|
|
|
|
}
|
|
|
|
return LayoutDeviceIntSize(r.right - r.left, r.bottom - r.top);
|
2016-05-05 08:00:13 +03:00
|
|
|
}
|
|
|
|
|
2016-07-01 11:15:16 +03:00
|
|
|
already_AddRefed<gfx::DrawTarget> WinCompositorWidget::StartRemoteDrawing() {
|
2018-06-21 23:17:48 +03:00
|
|
|
MutexAutoLock lock(mTransparentSurfaceLock);
|
|
|
|
|
2016-05-05 08:00:14 +03:00
|
|
|
MOZ_ASSERT(!mCompositeDC);
|
|
|
|
|
|
|
|
RefPtr<gfxASurface> surf;
|
|
|
|
if (mTransparencyMode == eTransparencyTransparent) {
|
|
|
|
surf = EnsureTransparentSurface();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must call this after EnsureTransparentSurface(), since it could update
|
|
|
|
// the DC.
|
|
|
|
HDC dc = GetWindowSurface();
|
|
|
|
if (!surf) {
|
|
|
|
if (!dc) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
uint32_t flags = (mTransparencyMode == eTransparencyOpaque)
|
|
|
|
? 0
|
|
|
|
: gfxWindowsSurface::FLAG_IS_TRANSPARENT;
|
|
|
|
surf = new gfxWindowsSurface(dc, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
IntSize size = surf->GetSize();
|
|
|
|
if (size.width <= 0 || size.height <= 0) {
|
|
|
|
if (dc) {
|
|
|
|
FreeWindowSurface(dc);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-07-04 21:45:18 +03:00
|
|
|
RefPtr<DrawTarget> dt =
|
|
|
|
mozilla::gfx::Factory::CreateDrawTargetForCairoSurface(
|
|
|
|
surf->CairoSurface(), size);
|
|
|
|
if (dt) {
|
|
|
|
mCompositeDC = dc;
|
|
|
|
} else {
|
|
|
|
FreeWindowSurface(dc);
|
|
|
|
}
|
2016-05-05 08:00:14 +03:00
|
|
|
|
2017-07-04 21:45:18 +03:00
|
|
|
return dt.forget();
|
2016-05-05 08:00:14 +03:00
|
|
|
}
|
|
|
|
|
2016-07-01 11:15:16 +03:00
|
|
|
void WinCompositorWidget::EndRemoteDrawing() {
|
2016-05-12 17:23:11 +03:00
|
|
|
MOZ_ASSERT(!mLockedBackBufferData);
|
|
|
|
|
2016-05-05 08:00:14 +03:00
|
|
|
if (mTransparencyMode == eTransparencyTransparent) {
|
|
|
|
MOZ_ASSERT(mTransparentSurface);
|
|
|
|
RedrawTransparentWindow();
|
|
|
|
}
|
|
|
|
if (mCompositeDC) {
|
|
|
|
FreeWindowSurface(mCompositeDC);
|
|
|
|
}
|
|
|
|
mCompositeDC = nullptr;
|
2016-05-05 08:00:13 +03:00
|
|
|
}
|
|
|
|
|
2016-08-24 01:18:55 +03:00
|
|
|
bool WinCompositorWidget::NeedsToDeferEndRemoteDrawing() {
|
|
|
|
if (mNotDeferEndRemoteDrawing) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
IDirectDraw7* ddraw = DeviceManagerDx::Get()->GetDirectDraw();
|
|
|
|
if (!ddraw) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD scanLine = 0;
|
|
|
|
int height = ::GetSystemMetrics(SM_CYSCREEN);
|
|
|
|
HRESULT ret = ddraw->GetScanLine(&scanLine);
|
|
|
|
if (ret == DDERR_VERTICALBLANKINPROGRESS) {
|
|
|
|
scanLine = 0;
|
|
|
|
} else if (ret != DD_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if there is a risk of tearing with GDI.
|
|
|
|
if (static_cast<int>(scanLine) > height / 2) {
|
|
|
|
// No need to defer.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-01 11:15:16 +03:00
|
|
|
already_AddRefed<gfx::DrawTarget> WinCompositorWidget::GetBackBufferDrawTarget(
|
|
|
|
gfx::DrawTarget* aScreenTarget, const LayoutDeviceIntRect& aRect,
|
|
|
|
const LayoutDeviceIntRect& aClearRect) {
|
2016-05-12 17:23:11 +03:00
|
|
|
MOZ_ASSERT(!mLockedBackBufferData);
|
|
|
|
|
2016-07-01 11:15:16 +03:00
|
|
|
RefPtr<gfx::DrawTarget> target = CompositorWidget::GetBackBufferDrawTarget(
|
|
|
|
aScreenTarget, aRect, aClearRect);
|
2016-05-12 17:23:11 +03:00
|
|
|
if (!target) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(target->GetBackendType() == BackendType::CAIRO);
|
|
|
|
|
|
|
|
uint8_t* destData;
|
|
|
|
IntSize destSize;
|
|
|
|
int32_t destStride;
|
|
|
|
SurfaceFormat destFormat;
|
|
|
|
if (!target->LockBits(&destData, &destSize, &destStride, &destFormat)) {
|
|
|
|
// LockBits is not supported. Use original DrawTarget.
|
|
|
|
return target.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<gfx::DrawTarget> dataTarget = Factory::CreateDrawTargetForData(
|
|
|
|
BackendType::CAIRO, destData, destSize, destStride, destFormat);
|
|
|
|
mLockedBackBufferData = destData;
|
|
|
|
|
|
|
|
return dataTarget.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfx::SourceSurface>
|
2016-07-01 11:15:16 +03:00
|
|
|
WinCompositorWidget::EndBackBufferDrawing() {
|
2016-05-12 17:23:11 +03:00
|
|
|
if (mLockedBackBufferData) {
|
|
|
|
MOZ_ASSERT(mLastBackBuffer);
|
|
|
|
mLastBackBuffer->ReleaseBits(mLockedBackBufferData);
|
|
|
|
mLockedBackBufferData = nullptr;
|
|
|
|
}
|
2016-07-01 11:15:16 +03:00
|
|
|
return CompositorWidget::EndBackBufferDrawing();
|
2016-05-12 17:23:11 +03:00
|
|
|
}
|
|
|
|
|
2016-08-24 01:18:55 +03:00
|
|
|
bool WinCompositorWidget::InitCompositor(layers::Compositor* aCompositor) {
|
|
|
|
if (aCompositor->GetBackendType() == layers::LayersBackend::LAYERS_BASIC) {
|
|
|
|
DeviceManagerDx::Get()->InitializeDirectDraw();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-01 11:15:16 +03:00
|
|
|
uintptr_t WinCompositorWidget::GetWidgetKey() { return mWidgetKey; }
|
2016-05-05 08:00:14 +03:00
|
|
|
|
2016-07-01 11:15:16 +03:00
|
|
|
void WinCompositorWidget::EnterPresentLock() { mPresentLock.Enter(); }
|
2016-05-05 08:00:13 +03:00
|
|
|
|
2016-07-01 11:15:16 +03:00
|
|
|
void WinCompositorWidget::LeavePresentLock() { mPresentLock.Leave(); }
|
2016-05-05 08:00:13 +03:00
|
|
|
|
2016-07-01 11:15:16 +03:00
|
|
|
RefPtr<gfxASurface> WinCompositorWidget::EnsureTransparentSurface() {
|
2018-06-21 23:17:48 +03:00
|
|
|
mTransparentSurfaceLock.AssertCurrentThreadOwns();
|
2016-05-05 08:00:14 +03:00
|
|
|
MOZ_ASSERT(mTransparencyMode == eTransparencyTransparent);
|
|
|
|
|
2016-07-26 01:04:33 +03:00
|
|
|
IntSize size = GetClientSize().ToUnknownSize();
|
|
|
|
if (!mTransparentSurface || mTransparentSurface->GetSize() != size) {
|
|
|
|
mTransparentSurface = nullptr;
|
|
|
|
mMemoryDC = nullptr;
|
|
|
|
CreateTransparentSurface(size);
|
2016-05-05 08:00:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<gfxASurface> surface = mTransparentSurface;
|
|
|
|
return surface.forget();
|
|
|
|
}
|
|
|
|
|
2016-07-01 11:15:16 +03:00
|
|
|
void WinCompositorWidget::CreateTransparentSurface(const gfx::IntSize& aSize) {
|
2018-06-21 23:17:48 +03:00
|
|
|
mTransparentSurfaceLock.AssertCurrentThreadOwns();
|
2016-05-05 08:00:14 +03:00
|
|
|
MOZ_ASSERT(!mTransparentSurface && !mMemoryDC);
|
2016-07-01 11:15:16 +03:00
|
|
|
RefPtr<gfxWindowsSurface> surface =
|
|
|
|
new gfxWindowsSurface(aSize, SurfaceFormat::A8R8G8B8_UINT32);
|
2016-05-05 08:00:14 +03:00
|
|
|
mTransparentSurface = surface;
|
|
|
|
mMemoryDC = surface->GetDC();
|
|
|
|
}
|
|
|
|
|
2016-07-01 11:15:16 +03:00
|
|
|
void WinCompositorWidget::UpdateTransparency(nsTransparencyMode aMode) {
|
2018-06-21 23:17:48 +03:00
|
|
|
MutexAutoLock lock(mTransparentSurfaceLock);
|
2016-05-05 08:00:14 +03:00
|
|
|
if (mTransparencyMode == aMode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTransparencyMode = aMode;
|
|
|
|
mTransparentSurface = nullptr;
|
|
|
|
mMemoryDC = nullptr;
|
|
|
|
|
|
|
|
if (mTransparencyMode == eTransparencyTransparent) {
|
|
|
|
EnsureTransparentSurface();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 11:15:16 +03:00
|
|
|
void WinCompositorWidget::ClearTransparentWindow() {
|
2018-06-21 23:17:48 +03:00
|
|
|
MutexAutoLock lock(mTransparentSurfaceLock);
|
2016-05-05 08:00:14 +03:00
|
|
|
if (!mTransparentSurface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-26 01:04:33 +03:00
|
|
|
EnsureTransparentSurface();
|
2016-05-05 08:00:14 +03:00
|
|
|
|
2016-07-26 01:04:33 +03:00
|
|
|
IntSize size = mTransparentSurface->GetSize();
|
|
|
|
if (!size.IsEmpty()) {
|
2016-11-10 07:49:09 +03:00
|
|
|
RefPtr<DrawTarget> drawTarget =
|
|
|
|
gfxPlatform::CreateDrawTargetForSurface(mTransparentSurface, size);
|
2017-02-08 18:06:49 +03:00
|
|
|
if (!drawTarget) {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-26 01:04:33 +03:00
|
|
|
drawTarget->ClearRect(Rect(0, 0, size.width, size.height));
|
|
|
|
RedrawTransparentWindow();
|
2016-05-05 08:00:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 11:15:16 +03:00
|
|
|
bool WinCompositorWidget::RedrawTransparentWindow() {
|
2016-05-05 08:00:14 +03:00
|
|
|
MOZ_ASSERT(mTransparencyMode == eTransparencyTransparent);
|
|
|
|
|
|
|
|
LayoutDeviceIntSize size = GetClientSize();
|
|
|
|
|
|
|
|
::GdiFlush();
|
|
|
|
|
|
|
|
BLENDFUNCTION bf = {AC_SRC_OVER, 0, 255, AC_SRC_ALPHA};
|
|
|
|
SIZE winSize = {size.width, size.height};
|
|
|
|
POINT srcPos = {0, 0};
|
|
|
|
HWND hWnd = WinUtils::GetTopLevelHWND(mWnd, true);
|
|
|
|
RECT winRect;
|
|
|
|
::GetWindowRect(hWnd, &winRect);
|
|
|
|
|
|
|
|
// perform the alpha blend
|
|
|
|
return !!::UpdateLayeredWindow(hWnd, nullptr, (POINT*)&winRect, &winSize,
|
|
|
|
mMemoryDC, &srcPos, 0, &bf, ULW_ALPHA);
|
|
|
|
}
|
|
|
|
|
2016-07-01 11:15:16 +03:00
|
|
|
HDC WinCompositorWidget::GetWindowSurface() {
|
2016-05-05 08:00:14 +03:00
|
|
|
return eTransparencyTransparent == mTransparencyMode ? mMemoryDC
|
|
|
|
: ::GetDC(mWnd);
|
|
|
|
}
|
|
|
|
|
2016-07-01 11:15:16 +03:00
|
|
|
void WinCompositorWidget::FreeWindowSurface(HDC dc) {
|
2016-05-05 08:00:14 +03:00
|
|
|
if (eTransparencyTransparent != mTransparencyMode) ::ReleaseDC(mWnd, dc);
|
|
|
|
}
|
|
|
|
|
2017-06-20 11:17:19 +03:00
|
|
|
bool WinCompositorWidget::IsHidden() const { return ::IsIconic(mWnd); }
|
|
|
|
|
2018-03-29 05:21:47 +03:00
|
|
|
void WinCompositorWidget::EnsureCompositorWindow() {
|
|
|
|
if (mCompositorWnd) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mCompositorWnd = WinCompositorWindowThread::CreateCompositorWindow(mWnd);
|
|
|
|
MOZ_ASSERT(mCompositorWnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WinCompositorWidget::DestroyCompositorWindow() {
|
|
|
|
if (!mCompositorWnd) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
WinCompositorWindowThread::DestroyCompositorWindow(mCompositorWnd);
|
|
|
|
mCompositorWnd = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WinCompositorWidget::UpdateCompositorWndSizeIfNecessary() {
|
|
|
|
if (!mCompositorWnd) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LayoutDeviceIntSize size = GetClientSize();
|
|
|
|
if (mLastCompositorWndSize == size) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force a resize and redraw (but not a move, activate, etc.).
|
|
|
|
if (!::SetWindowPos(mCompositorWnd, nullptr, 0, 0, size.width, size.height,
|
|
|
|
SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOCOPYBITS |
|
|
|
|
SWP_NOOWNERZORDER | SWP_NOZORDER)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mLastCompositorWndSize = size;
|
|
|
|
}
|
|
|
|
|
2016-05-05 08:00:13 +03:00
|
|
|
} // namespace widget
|
|
|
|
} // namespace mozilla
|