Bug 1734943 [wpt PR 31170] - Correct scrolling contents cull rect, a=testonly
[gecko.git] / widget / gtk / WindowSurfaceProvider.cpp
blob1351f8b3c163c80c5423c8dff969586bf1d223b1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "WindowSurfaceProvider.h"
9 #include "gfxPlatformGtk.h"
10 #include "mozilla/gfx/Logging.h"
11 #include "mozilla/layers/LayersTypes.h"
12 #include "nsWindow.h"
14 #ifdef MOZ_WAYLAND
15 # include "mozilla/StaticPrefs_widget.h"
16 # include "WindowSurfaceWaylandMultiBuffer.h"
17 #endif
18 #ifdef MOZ_X11
19 # include "mozilla/X11Util.h"
20 # include "WindowSurfaceX11Image.h"
21 # include "WindowSurfaceX11SHM.h"
22 #endif
24 #undef LOG
25 #ifdef MOZ_LOGGING
26 # include "mozilla/Logging.h"
27 # include "nsTArray.h"
28 # include "Units.h"
29 extern mozilla::LazyLogModule gWidgetLog;
30 # define LOG(args) MOZ_LOG(gWidgetLog, mozilla::LogLevel::Debug, args)
31 #else
32 # define LOG(args)
33 #endif /* MOZ_LOGGING */
35 namespace mozilla {
36 namespace widget {
38 using namespace mozilla::layers;
40 WindowSurfaceProvider::WindowSurfaceProvider()
41 : mWindowSurface(nullptr)
42 #ifdef MOZ_WAYLAND
44 mWidget(nullptr)
45 #endif
46 #ifdef MOZ_X11
48 mIsShaped(false),
49 mXDepth(0),
50 mXWindow(0),
51 mXVisual(nullptr)
52 #endif
56 #ifdef MOZ_WAYLAND
57 void WindowSurfaceProvider::Initialize(RefPtr<nsWindow> aWidget) {
58 mWidget = std::move(aWidget);
60 #endif
61 #ifdef MOZ_X11
62 void WindowSurfaceProvider::Initialize(Window aWindow, Visual* aVisual,
63 int aDepth, bool aIsShaped) {
64 mXWindow = aWindow;
65 mXVisual = aVisual;
66 mXDepth = aDepth;
67 mIsShaped = aIsShaped;
69 #endif
71 void WindowSurfaceProvider::CleanupResources() { mWindowSurface = nullptr; }
73 RefPtr<WindowSurface> WindowSurfaceProvider::CreateWindowSurface() {
74 #ifdef MOZ_WAYLAND
75 if (GdkIsWaylandDisplay()) {
76 return MakeRefPtr<WindowSurfaceWaylandMB>(mWidget);
78 #endif
79 #ifdef MOZ_X11
80 if (GdkIsX11Display()) {
81 // Blit to the window with the following priority:
82 // 1. MIT-SHM
83 // 2. XPutImage
84 # ifdef MOZ_HAVE_SHMIMAGE
85 if (!mIsShaped && nsShmImage::UseShm()) {
86 LOG(("Drawing to Window 0x%lx will use MIT-SHM\n", mXWindow));
87 return MakeRefPtr<WindowSurfaceX11SHM>(DefaultXDisplay(), mXWindow,
88 mXVisual, mXDepth);
90 # endif // MOZ_HAVE_SHMIMAGE
92 LOG(("Drawing to Window 0x%lx will use XPutImage\n", mXWindow));
93 return MakeRefPtr<WindowSurfaceX11Image>(DefaultXDisplay(), mXWindow,
94 mXVisual, mXDepth, mIsShaped);
96 #endif
97 MOZ_RELEASE_ASSERT(false);
100 already_AddRefed<gfx::DrawTarget>
101 WindowSurfaceProvider::StartRemoteDrawingInRegion(
102 const LayoutDeviceIntRegion& aInvalidRegion,
103 layers::BufferMode* aBufferMode) {
104 if (aInvalidRegion.IsEmpty()) return nullptr;
106 if (!mWindowSurface) {
107 mWindowSurface = CreateWindowSurface();
108 if (!mWindowSurface) return nullptr;
111 *aBufferMode = BufferMode::BUFFER_NONE;
112 RefPtr<gfx::DrawTarget> dt = mWindowSurface->Lock(aInvalidRegion);
113 #ifdef MOZ_X11
114 if (!dt && GdkIsX11Display() && !mWindowSurface->IsFallback()) {
115 // We can't use WindowSurfaceX11Image fallback on Wayland but
116 // Lock() call on WindowSurfaceWayland should never fail.
117 gfxWarningOnce()
118 << "Failed to lock WindowSurface, falling back to XPutImage backend.";
119 mWindowSurface = MakeRefPtr<WindowSurfaceX11Image>(
120 DefaultXDisplay(), mXWindow, mXVisual, mXDepth, mIsShaped);
121 dt = mWindowSurface->Lock(aInvalidRegion);
123 #endif
124 return dt.forget();
127 void WindowSurfaceProvider::EndRemoteDrawingInRegion(
128 gfx::DrawTarget* aDrawTarget, const LayoutDeviceIntRegion& aInvalidRegion) {
129 if (mWindowSurface) mWindowSurface->Commit(aInvalidRegion);
132 } // namespace widget
133 } // namespace mozilla