Bug 1719855 - Clean up code whether to fire a pointercancel event. r=botond
[gecko.git] / gfx / webrender_bindings / RenderCompositorEGL.cpp
blob21c2de1634bfe6fd098a3c4805f7c38578f40378
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 "RenderCompositorEGL.h"
9 #include "GLContext.h"
10 #include "GLContextEGL.h"
11 #include "GLContextProvider.h"
12 #include "GLLibraryEGL.h"
13 #include "mozilla/StaticPrefs_gfx.h"
14 #include "mozilla/gfx/Logging.h"
15 #include "mozilla/gfx/gfxVars.h"
16 #include "mozilla/layers/BuildConstants.h"
17 #include "mozilla/webrender/RenderThread.h"
18 #include "mozilla/widget/CompositorWidget.h"
20 #ifdef MOZ_WAYLAND
21 # include "mozilla/WidgetUtilsGtk.h"
22 # include "mozilla/widget/GtkCompositorWidget.h"
23 #endif
25 #ifdef MOZ_WIDGET_ANDROID
26 # include "mozilla/java/GeckoSurfaceTextureWrappers.h"
27 # include "mozilla/layers/AndroidHardwareBuffer.h"
28 # include "mozilla/widget/AndroidCompositorWidget.h"
29 # include <android/native_window.h>
30 # include <android/native_window_jni.h>
31 #endif
33 namespace mozilla::wr {
35 extern LazyLogModule gRenderThreadLog;
36 #define LOG(...) MOZ_LOG(gRenderThreadLog, LogLevel::Debug, (__VA_ARGS__))
38 /* static */
39 UniquePtr<RenderCompositor> RenderCompositorEGL::Create(
40 const RefPtr<widget::CompositorWidget>& aWidget, nsACString& aError) {
41 if ((kIsWayland || kIsX11) && !gfx::gfxVars::UseEGL()) {
42 return nullptr;
44 RefPtr<gl::GLContext> gl = RenderThread::Get()->SingletonGL(aError);
45 if (!gl) {
46 if (aError.IsEmpty()) {
47 aError.Assign("RcANGLE(no shared GL)"_ns);
48 } else {
49 aError.Append("(Create)"_ns);
51 return nullptr;
53 return MakeUnique<RenderCompositorEGL>(aWidget, std::move(gl));
56 EGLSurface RenderCompositorEGL::CreateEGLSurface() {
57 EGLSurface surface = EGL_NO_SURFACE;
58 surface = gl::GLContextEGL::CreateEGLSurfaceForCompositorWidget(
59 mWidget, gl::GLContextEGL::Cast(gl())->mSurfaceConfig);
60 if (surface == EGL_NO_SURFACE) {
61 const auto* renderThread = RenderThread::Get();
62 gfxCriticalNote << "Failed to create EGLSurface. "
63 << renderThread->RendererCount() << " renderers, "
64 << renderThread->ActiveRendererCount() << " active.";
66 return surface;
69 RenderCompositorEGL::RenderCompositorEGL(
70 const RefPtr<widget::CompositorWidget>& aWidget,
71 RefPtr<gl::GLContext>&& aGL)
72 : RenderCompositor(aWidget), mGL(aGL), mEGLSurface(EGL_NO_SURFACE) {
73 MOZ_ASSERT(mGL);
74 LOG("RenderCompositorEGL::RenderCompositorEGL()");
77 RenderCompositorEGL::~RenderCompositorEGL() {
78 LOG("RenderCompositorEGL::~RenderCompositorEGL()");
79 #ifdef MOZ_WIDGET_ANDROID
80 java::GeckoSurfaceTexture::DestroyUnused((int64_t)gl());
81 #endif
82 DestroyEGLSurface();
85 bool RenderCompositorEGL::BeginFrame() {
86 if ((kIsWayland || kIsX11) && mEGLSurface == EGL_NO_SURFACE) {
87 gfxCriticalNote
88 << "We don't have EGLSurface to draw into. Called too early?";
89 return false;
91 #ifdef MOZ_WAYLAND
92 if (mWidget->AsGTK()) {
93 mWidget->AsGTK()->SetEGLNativeWindowSize(GetBufferSize());
95 #endif
96 if (!MakeCurrent()) {
97 gfxCriticalNote << "Failed to make render context current, can't draw.";
98 return false;
101 #ifdef MOZ_WIDGET_ANDROID
102 java::GeckoSurfaceTexture::DestroyUnused((int64_t)gl());
103 gl()->MakeCurrent(); // DestroyUnused can change the current context!
104 #endif
106 return true;
109 RenderedFrameId RenderCompositorEGL::EndFrame(
110 const nsTArray<DeviceIntRect>& aDirtyRects) {
111 #ifdef MOZ_WIDGET_ANDROID
112 const auto& gle = gl::GLContextEGL::Cast(gl());
113 const auto& egl = gle->mEgl;
115 EGLSync sync = nullptr;
116 if (layers::AndroidHardwareBufferApi::Get()) {
117 sync = egl->fCreateSync(LOCAL_EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
119 if (sync) {
120 int fenceFd = egl->fDupNativeFenceFDANDROID(sync);
121 if (fenceFd >= 0) {
122 mReleaseFenceFd = ipc::FileDescriptor(UniqueFileHandle(fenceFd));
124 egl->fDestroySync(sync);
125 sync = nullptr;
127 #endif
129 RenderedFrameId frameId = GetNextRenderFrameId();
130 #ifdef MOZ_WAYLAND
131 if (mWidget->IsHidden()) {
132 return frameId;
134 #endif
135 if (mEGLSurface != EGL_NO_SURFACE && aDirtyRects.Length() > 0) {
136 gfx::IntRegion bufferInvalid;
137 const auto bufferSize = GetBufferSize();
138 for (const DeviceIntRect& rect : aDirtyRects) {
139 const auto left = std::max(0, std::min(bufferSize.width, rect.min.x));
140 const auto top = std::max(0, std::min(bufferSize.height, rect.min.y));
142 const auto right = std::min(bufferSize.width, std::max(0, rect.max.x));
143 const auto bottom = std::min(bufferSize.height, std::max(0, rect.max.y));
145 const auto width = right - left;
146 const auto height = bottom - top;
148 bufferInvalid.OrWith(
149 gfx::IntRect(left, (GetBufferSize().height - bottom), width, height));
151 gl()->SetDamage(bufferInvalid);
153 gl()->SwapBuffers();
154 return frameId;
157 void RenderCompositorEGL::Pause() { DestroyEGLSurface(); }
159 bool RenderCompositorEGL::Resume() {
160 if (kIsAndroid) {
161 // Destroy EGLSurface if it exists.
162 DestroyEGLSurface();
164 auto size = GetBufferSize();
165 GLint maxTextureSize = 0;
166 gl()->fGetIntegerv(LOCAL_GL_MAX_TEXTURE_SIZE, (GLint*)&maxTextureSize);
168 // When window size is too big, hardware buffer allocation could fail.
169 if (maxTextureSize < size.width || maxTextureSize < size.height) {
170 gfxCriticalNote << "Too big ANativeWindow size(" << size.width << ", "
171 << size.height << ") MaxTextureSize " << maxTextureSize;
172 return false;
175 mEGLSurface = CreateEGLSurface();
176 if (mEGLSurface == EGL_NO_SURFACE) {
177 // Often when we fail to create an EGL surface it is because the Java
178 // Surface we have been provided is invalid. Therefore the on the first
179 // occurence we don't raise a WebRenderError and instead just return
180 // failure. This allows the widget a chance to request a new Java
181 // Surface. On subsequent failures, raising the WebRenderError will
182 // result in the compositor being recreated, falling back through
183 // webrender configurations, and eventually crashing if we still do not
184 // succeed.
185 if (!mHandlingNewSurfaceError) {
186 mHandlingNewSurfaceError = true;
187 } else {
188 RenderThread::Get()->HandleWebRenderError(WebRenderError::NEW_SURFACE);
190 return false;
192 mHandlingNewSurfaceError = false;
194 gl::GLContextEGL::Cast(gl())->SetEGLSurfaceOverride(mEGLSurface);
195 } else if (kIsWayland || kIsX11) {
196 // Destroy EGLSurface if it exists and create a new one. We will set the
197 // swap interval after MakeCurrent() has been called.
198 DestroyEGLSurface();
199 mEGLSurface = CreateEGLSurface();
200 if (mEGLSurface != EGL_NO_SURFACE) {
201 // We have a new EGL surface, which on wayland needs to be configured for
202 // non-blocking buffer swaps. We need MakeCurrent() to set our current EGL
203 // context before we call eglSwapInterval, which is why we do it here
204 // rather than where the surface was created.
205 const auto& gle = gl::GLContextEGL::Cast(gl());
206 const auto& egl = gle->mEgl;
207 MakeCurrent();
209 const int interval = gfx::gfxVars::SwapIntervalEGL() ? 1 : 0;
210 egl->fSwapInterval(interval);
211 } else {
212 RenderThread::Get()->HandleWebRenderError(WebRenderError::NEW_SURFACE);
213 return false;
216 return true;
219 bool RenderCompositorEGL::IsPaused() { return mEGLSurface == EGL_NO_SURFACE; }
221 bool RenderCompositorEGL::MakeCurrent() {
222 const auto& gle = gl::GLContextEGL::Cast(gl());
224 gle->SetEGLSurfaceOverride(mEGLSurface);
225 bool ok = gl()->MakeCurrent();
226 if (!gl()->IsGLES() && ok && mEGLSurface != EGL_NO_SURFACE) {
227 // If we successfully made a surface current, set the draw buffer
228 // appropriately. It's not well-defined by the EGL spec whether
229 // eglMakeCurrent should do this automatically. See bug 1646135.
230 gl()->fDrawBuffer(gl()->IsDoubleBuffered() ? LOCAL_GL_BACK
231 : LOCAL_GL_FRONT);
233 return ok;
236 void RenderCompositorEGL::DestroyEGLSurface() {
237 const auto& gle = gl::GLContextEGL::Cast(gl());
238 const auto& egl = gle->mEgl;
240 // Release EGLSurface of back buffer before calling ResizeBuffers().
241 if (mEGLSurface) {
242 gle->SetEGLSurfaceOverride(EGL_NO_SURFACE);
243 if (!egl->fMakeCurrent(EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) {
244 const EGLint err = egl->mLib->fGetError();
245 gfxCriticalNote << "Error in eglMakeCurrent: " << gfx::hexa(err);
247 if (!egl->fDestroySurface(mEGLSurface)) {
248 const EGLint err = egl->mLib->fGetError();
249 gfxCriticalNote << "Error in eglDestroySurface: " << gfx::hexa(err);
251 mEGLSurface = nullptr;
255 ipc::FileDescriptor RenderCompositorEGL::GetAndResetReleaseFence() {
256 #ifdef MOZ_WIDGET_ANDROID
257 MOZ_ASSERT(!layers::AndroidHardwareBufferApi::Get() ||
258 mReleaseFenceFd.IsValid());
259 return std::move(mReleaseFenceFd);
260 #else
261 return ipc::FileDescriptor();
262 #endif
265 LayoutDeviceIntSize RenderCompositorEGL::GetBufferSize() {
266 return mWidget->GetClientSize();
269 bool RenderCompositorEGL::UsePartialPresent() {
270 return gfx::gfxVars::WebRenderMaxPartialPresentRects() > 0;
273 bool RenderCompositorEGL::RequestFullRender() { return false; }
275 uint32_t RenderCompositorEGL::GetMaxPartialPresentRects() {
276 return gfx::gfxVars::WebRenderMaxPartialPresentRects();
279 bool RenderCompositorEGL::ShouldDrawPreviousPartialPresentRegions() {
280 return true;
283 size_t RenderCompositorEGL::GetBufferAge() const {
284 if (!StaticPrefs::
285 gfx_webrender_allow_partial_present_buffer_age_AtStartup()) {
286 return 0;
288 return gl()->GetBufferAge();
291 void RenderCompositorEGL::SetBufferDamageRegion(const wr::DeviceIntRect* aRects,
292 size_t aNumRects) {
293 const auto& gle = gl::GLContextEGL::Cast(gl());
294 const auto& egl = gle->mEgl;
295 if (gle->HasKhrPartialUpdate() &&
296 StaticPrefs::gfx_webrender_allow_partial_present_buffer_age_AtStartup()) {
297 std::vector<EGLint> rects;
298 rects.reserve(4 * aNumRects);
299 const auto bufferSize = GetBufferSize();
300 for (size_t i = 0; i < aNumRects; i++) {
301 const auto left =
302 std::max(0, std::min(bufferSize.width, aRects[i].min.x));
303 const auto top =
304 std::max(0, std::min(bufferSize.height, aRects[i].min.y));
306 const auto right =
307 std::min(bufferSize.width, std::max(0, aRects[i].max.x));
308 const auto bottom =
309 std::min(bufferSize.height, std::max(0, aRects[i].max.y));
311 const auto width = right - left;
312 const auto height = bottom - top;
314 rects.push_back(left);
315 rects.push_back(bufferSize.height - bottom);
316 rects.push_back(width);
317 rects.push_back(height);
319 const auto ret =
320 egl->fSetDamageRegion(mEGLSurface, rects.data(), rects.size() / 4);
321 if (ret == LOCAL_EGL_FALSE) {
322 const auto err = egl->mLib->fGetError();
323 gfxCriticalError() << "Error in eglSetDamageRegion: " << gfx::hexa(err);
328 } // namespace mozilla::wr