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/. */
6 
7 #include "RenderCompositorEGL.h"
8 
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"
19 
20 #ifdef MOZ_WAYLAND
21 #  include "mozilla/WidgetUtilsGtk.h"
22 #  include "mozilla/widget/GtkCompositorWidget.h"
23 #endif
24 
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
32 
33 namespace mozilla::wr {
34 
35 /* static */
Create(const RefPtr<widget::CompositorWidget> & aWidget,nsACString & aError)36 UniquePtr<RenderCompositor> RenderCompositorEGL::Create(
37     const RefPtr<widget::CompositorWidget>& aWidget, nsACString& aError) {
38   if ((kIsWayland || kIsX11) && !gfx::gfxVars::UseEGL()) {
39     return nullptr;
40   }
41   if (!RenderThread::Get()->SingletonGL()) {
42     gfxCriticalNote << "Failed to get shared GL context";
43     return nullptr;
44   }
45   return MakeUnique<RenderCompositorEGL>(aWidget);
46 }
47 
CreateEGLSurface()48 EGLSurface RenderCompositorEGL::CreateEGLSurface() {
49   EGLSurface surface = EGL_NO_SURFACE;
50   surface = gl::GLContextEGL::CreateEGLSurfaceForCompositorWidget(
51       mWidget, gl::GLContextEGL::Cast(gl())->mConfig);
52   if (surface == EGL_NO_SURFACE) {
53     gfxCriticalNote << "Failed to create EGLSurface";
54   }
55   return surface;
56 }
57 
RenderCompositorEGL(const RefPtr<widget::CompositorWidget> & aWidget)58 RenderCompositorEGL::RenderCompositorEGL(
59     const RefPtr<widget::CompositorWidget>& aWidget)
60     : RenderCompositor(aWidget), mEGLSurface(EGL_NO_SURFACE) {}
61 
~RenderCompositorEGL()62 RenderCompositorEGL::~RenderCompositorEGL() {
63 #ifdef MOZ_WIDGET_ANDROID
64   java::GeckoSurfaceTexture::DestroyUnused((int64_t)gl());
65 #endif
66   DestroyEGLSurface();
67 }
68 
BeginFrame()69 bool RenderCompositorEGL::BeginFrame() {
70   if ((kIsWayland || kIsX11) && mEGLSurface == EGL_NO_SURFACE) {
71     gfxCriticalNote
72         << "We don't have EGLSurface to draw into. Called too early?";
73     return false;
74   }
75 #ifdef MOZ_WAYLAND
76   if (mWidget->AsGTK()) {
77     mWidget->AsGTK()->SetEGLNativeWindowSize(GetBufferSize());
78   }
79 #endif
80   if (!MakeCurrent()) {
81     gfxCriticalNote << "Failed to make render context current, can't draw.";
82     return false;
83   }
84 
85 #ifdef MOZ_WIDGET_ANDROID
86   java::GeckoSurfaceTexture::DestroyUnused((int64_t)gl());
87   gl()->MakeCurrent();  // DestroyUnused can change the current context!
88 #endif
89 
90   return true;
91 }
92 
EndFrame(const nsTArray<DeviceIntRect> & aDirtyRects)93 RenderedFrameId RenderCompositorEGL::EndFrame(
94     const nsTArray<DeviceIntRect>& aDirtyRects) {
95 #ifdef MOZ_WIDGET_ANDROID
96   const auto& gle = gl::GLContextEGL::Cast(gl());
97   const auto& egl = gle->mEgl;
98 
99   EGLSync sync = nullptr;
100   if (layers::AndroidHardwareBufferApi::Get()) {
101     sync = egl->fCreateSync(LOCAL_EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
102   }
103   if (sync) {
104     int fenceFd = egl->fDupNativeFenceFDANDROID(sync);
105     if (fenceFd >= 0) {
106       mReleaseFenceFd = ipc::FileDescriptor(UniqueFileHandle(fenceFd));
107     }
108     egl->fDestroySync(sync);
109     sync = nullptr;
110   }
111 #endif
112 
113   RenderedFrameId frameId = GetNextRenderFrameId();
114   if (mEGLSurface != EGL_NO_SURFACE && aDirtyRects.Length() > 0) {
115     gfx::IntRegion bufferInvalid;
116     const auto bufferSize = GetBufferSize();
117     for (const DeviceIntRect& rect : aDirtyRects) {
118       const auto left = std::max(0, std::min(bufferSize.width, rect.min.x));
119       const auto top = std::max(0, std::min(bufferSize.height, rect.min.y));
120 
121       const auto right = std::min(bufferSize.width, std::max(0, rect.max.x));
122       const auto bottom = std::min(bufferSize.height, std::max(0, rect.max.y));
123 
124       const auto width = right - left;
125       const auto height = bottom - top;
126 
127       bufferInvalid.OrWith(
128           gfx::IntRect(left, (GetBufferSize().height - bottom), width, height));
129     }
130     gl()->SetDamage(bufferInvalid);
131   }
132   gl()->SwapBuffers();
133   return frameId;
134 }
135 
Pause()136 void RenderCompositorEGL::Pause() { DestroyEGLSurface(); }
137 
Resume()138 bool RenderCompositorEGL::Resume() {
139   if (kIsAndroid) {
140     // Destroy EGLSurface if it exists.
141     DestroyEGLSurface();
142 
143 #ifdef MOZ_WIDGET_ANDROID
144     // Query the new surface size as this may have changed. We cannot use
145     // mWidget->GetClientSize() due to a race condition between
146     // nsWindow::Resize() being called and the frame being rendered after the
147     // surface is resized.
148     EGLNativeWindowType window = mWidget->AsAndroid()->GetEGLNativeWindow();
149     JNIEnv* const env = jni::GetEnvForThread();
150     ANativeWindow* const nativeWindow =
151         ANativeWindow_fromSurface(env, reinterpret_cast<jobject>(window));
152     const int32_t width = ANativeWindow_getWidth(nativeWindow);
153     const int32_t height = ANativeWindow_getHeight(nativeWindow);
154 
155     GLint maxTextureSize = 0;
156     gl()->fGetIntegerv(LOCAL_GL_MAX_TEXTURE_SIZE, (GLint*)&maxTextureSize);
157 
158     // When window size is too big, hardware buffer allocation could fail.
159     if (maxTextureSize < width || maxTextureSize < height) {
160       gfxCriticalNote << "Too big ANativeWindow size(" << width << ", "
161                       << height << ") MaxTextureSize " << maxTextureSize;
162       return false;
163     }
164 
165     mEGLSurface = CreateEGLSurface();
166     if (mEGLSurface == EGL_NO_SURFACE) {
167       RenderThread::Get()->HandleWebRenderError(WebRenderError::NEW_SURFACE);
168       return false;
169     }
170     gl::GLContextEGL::Cast(gl())->SetEGLSurfaceOverride(mEGLSurface);
171 
172     mEGLSurfaceSize = LayoutDeviceIntSize(width, height);
173     ANativeWindow_release(nativeWindow);
174 #endif  // MOZ_WIDGET_ANDROID
175   } else if (kIsWayland || kIsX11) {
176     // Destroy EGLSurface if it exists and create a new one. We will set the
177     // swap interval after MakeCurrent() has been called.
178     DestroyEGLSurface();
179     mEGLSurface = CreateEGLSurface();
180     if (mEGLSurface != EGL_NO_SURFACE) {
181       // We have a new EGL surface, which on wayland needs to be configured for
182       // non-blocking buffer swaps. We need MakeCurrent() to set our current EGL
183       // context before we call eglSwapInterval, which is why we do it here
184       // rather than where the surface was created.
185       const auto& gle = gl::GLContextEGL::Cast(gl());
186       const auto& egl = gle->mEgl;
187       MakeCurrent();
188       // Make eglSwapBuffers() non-blocking on wayland.
189       egl->fSwapInterval(0);
190     } else {
191       RenderThread::Get()->HandleWebRenderError(WebRenderError::NEW_SURFACE);
192       return false;
193     }
194   }
195   return true;
196 }
197 
IsPaused()198 bool RenderCompositorEGL::IsPaused() { return mEGLSurface == EGL_NO_SURFACE; }
199 
gl() const200 gl::GLContext* RenderCompositorEGL::gl() const {
201   return RenderThread::Get()->SingletonGL();
202 }
203 
MakeCurrent()204 bool RenderCompositorEGL::MakeCurrent() {
205   const auto& gle = gl::GLContextEGL::Cast(gl());
206 
207   gle->SetEGLSurfaceOverride(mEGLSurface);
208   bool ok = gl()->MakeCurrent();
209   if (!gl()->IsGLES() && ok && mEGLSurface != EGL_NO_SURFACE) {
210     // If we successfully made a surface current, set the draw buffer
211     // appropriately. It's not well-defined by the EGL spec whether
212     // eglMakeCurrent should do this automatically. See bug 1646135.
213     gl()->fDrawBuffer(gl()->IsDoubleBuffered() ? LOCAL_GL_BACK
214                                                : LOCAL_GL_FRONT);
215   }
216   return ok;
217 }
218 
DestroyEGLSurface()219 void RenderCompositorEGL::DestroyEGLSurface() {
220   const auto& gle = gl::GLContextEGL::Cast(gl());
221   const auto& egl = gle->mEgl;
222 
223   // Release EGLSurface of back buffer before calling ResizeBuffers().
224   if (mEGLSurface) {
225     gle->SetEGLSurfaceOverride(EGL_NO_SURFACE);
226     egl->fDestroySurface(mEGLSurface);
227     mEGLSurface = nullptr;
228   }
229 }
230 
GetAndResetReleaseFence()231 ipc::FileDescriptor RenderCompositorEGL::GetAndResetReleaseFence() {
232 #ifdef MOZ_WIDGET_ANDROID
233   MOZ_ASSERT(!layers::AndroidHardwareBufferApi::Get() ||
234              mReleaseFenceFd.IsValid());
235   return std::move(mReleaseFenceFd);
236 #else
237   return ipc::FileDescriptor();
238 #endif
239 }
240 
GetBufferSize()241 LayoutDeviceIntSize RenderCompositorEGL::GetBufferSize() {
242 #ifdef MOZ_WIDGET_ANDROID
243   return mEGLSurfaceSize;
244 #else
245   return mWidget->GetClientSize();
246 #endif
247 }
248 
UsePartialPresent()249 bool RenderCompositorEGL::UsePartialPresent() {
250   return gfx::gfxVars::WebRenderMaxPartialPresentRects() > 0;
251 }
252 
RequestFullRender()253 bool RenderCompositorEGL::RequestFullRender() { return false; }
254 
GetMaxPartialPresentRects()255 uint32_t RenderCompositorEGL::GetMaxPartialPresentRects() {
256   return gfx::gfxVars::WebRenderMaxPartialPresentRects();
257 }
258 
ShouldDrawPreviousPartialPresentRegions()259 bool RenderCompositorEGL::ShouldDrawPreviousPartialPresentRegions() {
260   return true;
261 }
262 
GetBufferAge() const263 size_t RenderCompositorEGL::GetBufferAge() const {
264   if (!StaticPrefs::
265           gfx_webrender_allow_partial_present_buffer_age_AtStartup()) {
266     return 0;
267   }
268   return gl()->GetBufferAge();
269 }
270 
SetBufferDamageRegion(const wr::DeviceIntRect * aRects,size_t aNumRects)271 void RenderCompositorEGL::SetBufferDamageRegion(const wr::DeviceIntRect* aRects,
272                                                 size_t aNumRects) {
273   const auto& gle = gl::GLContextEGL::Cast(gl());
274   const auto& egl = gle->mEgl;
275   if (gle->HasKhrPartialUpdate() &&
276       StaticPrefs::gfx_webrender_allow_partial_present_buffer_age_AtStartup()) {
277     std::vector<EGLint> rects;
278     rects.reserve(4 * aNumRects);
279     const auto bufferSize = GetBufferSize();
280     for (size_t i = 0; i < aNumRects; i++) {
281       const auto left =
282           std::max(0, std::min(bufferSize.width, aRects[i].min.x));
283       const auto top =
284           std::max(0, std::min(bufferSize.height, aRects[i].min.y));
285 
286       const auto right =
287           std::min(bufferSize.width, std::max(0, aRects[i].max.x));
288       const auto bottom =
289           std::min(bufferSize.height, std::max(0, aRects[i].max.y));
290 
291       const auto width = right - left;
292       const auto height = bottom - top;
293 
294       rects.push_back(left);
295       rects.push_back(bufferSize.height - bottom);
296       rects.push_back(width);
297       rects.push_back(height);
298     }
299     const auto ret =
300         egl->fSetDamageRegion(mEGLSurface, rects.data(), rects.size() / 4);
301     if (ret == LOCAL_EGL_FALSE) {
302       const auto err = egl->mLib->fGetError();
303       gfxCriticalError() << "Error in eglSetDamageRegion: " << gfx::hexa(err);
304     }
305   }
306 }
307 
308 }  // namespace mozilla::wr
309