Flutter Linux Embedder
fl_subsurface_egl_test.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "gtest/gtest.h"
6 
10 #include "flutter/shell/platform/linux/testing/mock_epoxy.h"
11 #include "flutter/shell/platform/linux/testing/wayland_test.h"
12 
13 #include <epoxy/egl.h>
14 #include <wayland-client.h>
15 
16 using flutter::testing::fl_mock_wayland_free_surface;
17 using flutter::testing::fl_mock_wayland_get_display;
18 using flutter::testing::fl_mock_wayland_get_surface;
19 
20 constexpr size_t kWidth = 100;
21 constexpr size_t kHeight = 200;
22 constexpr gint kScale = 2;
23 
24 // The native window, and so the buffer frames are drawn into, is created in
25 // device pixels.
26 constexpr size_t kBufferWidth = kWidth * kScale;
27 constexpr size_t kBufferHeight = kHeight * kScale;
28 
29 class FlSubsurfaceEGLTest : public flutter::testing::WaylandTest {
30  protected:
31  void SetUp() override {
32  // A driver that supports glBlitFramebuffer, which is the path used when
33  // available.
34  ON_CALL(epoxy, glGetString(GL_VENDOR))
35  .WillByDefault(
36  ::testing::Return(reinterpret_cast<const GLubyte*>("Intel")));
37  ON_CALL(epoxy, epoxy_is_desktop_gl).WillByDefault(::testing::Return(true));
38  ON_CALL(epoxy, epoxy_gl_version).WillByDefault(::testing::Return(30));
39 
41  display = fl_wayland_display_open(fl_mock_wayland_get_display());
42  ASSERT_NE(display, nullptr);
43  parent_surface = fl_mock_wayland_get_surface();
45  ASSERT_NE(subsurface, nullptr);
46  }
47 
48  void TearDown() override {
49  g_clear_object(&subsurface);
50  g_clear_object(&display);
51  g_clear_object(&opengl_manager);
52  fl_mock_wayland_free_surface(parent_surface);
53  }
54 
55  FlSubsurfaceEGL* CreateEGL() {
57  kScale);
58  }
59 
60  ::testing::NiceMock<flutter::testing::MockEpoxy> epoxy;
61  FlOpenGLManager* opengl_manager = nullptr;
62  FlWaylandDisplay* display = nullptr;
63  struct wl_surface* parent_surface = nullptr;
64  FlSubsurface* subsurface = nullptr;
65 };
66 
67 // The native window is created in device pixels, i.e. scaled by the buffer
68 // scale of the surface.
70  EXPECT_CALL(wayland, EGLWindowCreate(kWidth * kScale, kHeight * kScale));
71  EXPECT_CALL(wayland, Request(::testing::StrEq("wl_surface"),
72  WL_SURFACE_SET_BUFFER_SCALE));
73 
74  g_autoptr(FlSubsurfaceEGL) egl = CreateEGL();
75  EXPECT_NE(egl, nullptr);
76  EXPECT_FALSE(HasReceivedLogLevel(G_LOG_LEVEL_WARNING));
77 }
78 
79 // If the EGL setup fails an object is still returned, so the caller doesn't
80 // have to handle a failed construction.
81 TEST_F(FlSubsurfaceEGLTest, CreateFailure) {
82  wayland.egl_window_create_fails = true;
83 
84  g_autoptr(FlSubsurfaceEGL) egl = CreateEGL();
85  EXPECT_NE(egl, nullptr);
86  EXPECT_TRUE(HasReceivedLogLevel(G_LOG_LEVEL_WARNING));
87 }
88 
89 // A subsurface reference is held, so the Wayland surface being drawn to can't
90 // be destroyed while the EGL surface exists.
91 TEST_F(FlSubsurfaceEGLTest, HoldsSubsurface) {
92  g_autoptr(FlSubsurfaceEGL) egl = CreateEGL();
93  ASSERT_NE(egl, nullptr);
94 
95  // Drop the reference held by the test; the EGL object keeps it alive.
96  FlSubsurface* s = subsurface;
97  subsurface = nullptr;
98  gpointer weak_subsurface = s;
99  g_object_add_weak_pointer(G_OBJECT(s), &weak_subsurface);
100  g_object_unref(s);
101  EXPECT_NE(weak_subsurface, nullptr);
102  g_object_remove_weak_pointer(G_OBJECT(s), &weak_subsurface);
103 }
104 
106  g_autoptr(FlSubsurfaceEGL) egl = CreateEGL();
107  ASSERT_NE(egl, nullptr);
108 
109  EXPECT_CALL(wayland, EGLWindowResize(300, 400));
110 
111  fl_subsurface_egl_resize(egl, 300, 400);
112 }
113 
114 // Frames are blitted to the window surface and presented with a buffer swap.
116  g_autoptr(FlSubsurfaceEGL) egl = CreateEGL();
117  ASSERT_NE(egl, nullptr);
118 
119  EXPECT_CALL(epoxy, glBlitFramebuffer);
120 
121  fl_subsurface_egl_present(egl, 1, kWidth, kHeight, nullptr);
122 }
123 
124 // The native window is resized if a frame of a different size arrives, so the
125 // frame isn't scaled by the compositor.
126 TEST_F(FlSubsurfaceEGLTest, PresentResizes) {
127  g_autoptr(FlSubsurfaceEGL) egl = CreateEGL();
128  ASSERT_NE(egl, nullptr);
129 
130  EXPECT_CALL(wayland, EGLWindowResize(kBufferWidth + 50, kBufferHeight + 50));
131 
133  nullptr);
134 }
135 
136 // Drivers without glBlitFramebuffer draw the frame with a shader instead.
137 TEST_F(FlSubsurfaceEGLTest, PresentWithoutBlit) {
138  EXPECT_CALL(epoxy, epoxy_gl_version).WillRepeatedly(::testing::Return(20));
139  EXPECT_CALL(epoxy, epoxy_has_gl_extension(::testing::_))
140  .WillRepeatedly(::testing::Return(false));
141 
142  g_autoptr(FlSubsurfaceEGL) egl = CreateEGL();
143  ASSERT_NE(egl, nullptr);
144 
145  EXPECT_CALL(epoxy, glBlitFramebuffer).Times(0);
146 
147  fl_subsurface_egl_present(egl, 1, kWidth, kHeight, nullptr);
148 }
149 
150 // A frame the same size as the buffer is written to the whole buffer and
151 // presented with a single swap.
152 TEST_F(FlSubsurfaceEGLTest, PresentMatchingBuffer) {
153  g_autoptr(FlSubsurfaceEGL) egl = CreateEGL();
154  ASSERT_NE(egl, nullptr);
155 
156  EXPECT_CALL(epoxy, glBlitFramebuffer(0, 0, kBufferWidth, kBufferHeight, 0, 0,
158  ::testing::_, ::testing::_));
159  EXPECT_CALL(epoxy, eglSwapBuffers).Times(1);
160 
162 }
163 
164 // The buffer a frame is drawn into is acquired when the previous frame is
165 // swapped, so a resize doesn't reach it. A frame of a new size swaps the stale
166 // buffer away unused so the frame is drawn into one of the right size, which
167 // is the only chance a window that is sized to its content gets.
168 TEST_F(FlSubsurfaceEGLTest, PresentDifferentSizeSwapsStaleBuffer) {
169  g_autoptr(FlSubsurfaceEGL) egl = CreateEGL();
170  ASSERT_NE(egl, nullptr);
171 
172  EXPECT_CALL(epoxy, eglSwapBuffers).Times(2);
173 
175  nullptr);
176 }
177 
178 // The whole frame is still written, rather than being aligned against the size
179 // of the buffer that was swapped away.
180 TEST_F(FlSubsurfaceEGLTest, PresentDifferentSizeWritesWholeFrame) {
181  g_autoptr(FlSubsurfaceEGL) egl = CreateEGL();
182  ASSERT_NE(egl, nullptr);
183 
184  EXPECT_CALL(epoxy,
185  glBlitFramebuffer(0, 0, kBufferWidth + 50, kBufferHeight + 50, 0,
186  0, kBufferWidth + 50, kBufferHeight + 50,
187  ::testing::_, ::testing::_));
188 
190  nullptr);
191 }
192 
193 // The shader path writes the whole frame in the same way.
194 TEST_F(FlSubsurfaceEGLTest, PresentDifferentSizeWithoutBlit) {
195  EXPECT_CALL(epoxy, epoxy_gl_version).WillRepeatedly(::testing::Return(20));
196  EXPECT_CALL(epoxy, epoxy_has_gl_extension(::testing::_))
197  .WillRepeatedly(::testing::Return(false));
198 
199  g_autoptr(FlSubsurfaceEGL) egl = CreateEGL();
200  ASSERT_NE(egl, nullptr);
201 
202  EXPECT_CALL(epoxy, glViewport(0, 0, kBufferWidth + 50, kBufferHeight + 50));
203 
205  nullptr);
206 }
207 
208 // A resize from the window leaves the buffer stale in the same way, so the
209 // next frame swaps it away before drawing.
210 TEST_F(FlSubsurfaceEGLTest, PresentAfterResizeSwapsStaleBuffer) {
211  g_autoptr(FlSubsurfaceEGL) egl = CreateEGL();
212  ASSERT_NE(egl, nullptr);
213 
215 
216  EXPECT_CALL(wayland, EGLWindowResize).Times(0);
217  EXPECT_CALL(epoxy, eglSwapBuffers).Times(2);
218 
220  nullptr);
221 }
222 
223 // Once the buffer has caught up with the frame size, frames are presented with
224 // a single swap again.
225 TEST_F(FlSubsurfaceEGLTest, PresentSecondFrameOfNewSize) {
226  g_autoptr(FlSubsurfaceEGL) egl = CreateEGL();
227  ASSERT_NE(egl, nullptr);
228 
230  nullptr);
231 
232  EXPECT_CALL(epoxy, eglSwapBuffers).Times(1);
233 
235  nullptr);
236 }
237 
238 // Wayland requires buffer sizes to be an integer multiple of the buffer scale,
239 // so a frame that isn't a whole number of logical pixels, which a view that
240 // hasn't been allocated yet can produce, is rounded up.
241 TEST_F(FlSubsurfaceEGLTest, PresentRoundsUpToBufferScale) {
242  g_autoptr(FlSubsurfaceEGL) egl = CreateEGL();
243  ASSERT_NE(egl, nullptr);
244 
245  EXPECT_CALL(wayland, EGLWindowResize(kScale, kScale));
246 
247  fl_subsurface_egl_present(egl, 1, 1, 1, nullptr);
248 }
249 
250 // A rounded up frame doesn't fill the buffer, and OpenGL puts the origin at the
251 // bottom left while Wayland puts it at the top left, so the frame is written at
252 // the top of the buffer rather than at the OpenGL origin.
253 TEST_F(FlSubsurfaceEGLTest, PresentRoundedFrameWritesAtTopOfBuffer) {
254  g_autoptr(FlSubsurfaceEGL) egl = CreateEGL();
255  ASSERT_NE(egl, nullptr);
256 
257  EXPECT_CALL(epoxy, glBlitFramebuffer(0, 0, 1, 1, 0, kScale - 1, 1, kScale,
258  ::testing::_, ::testing::_));
259 
260  fl_subsurface_egl_present(egl, 1, 1, 1, nullptr);
261 }
262 
263 // A resize from the window is rounded up in the same way.
264 TEST_F(FlSubsurfaceEGLTest, ResizeRoundsUpToBufferScale) {
265  g_autoptr(FlSubsurfaceEGL) egl = CreateEGL();
266  ASSERT_NE(egl, nullptr);
267 
268  EXPECT_CALL(wayland, EGLWindowResize(kScale, kScale));
269 
270  fl_subsurface_egl_resize(egl, 1, 1);
271 }
272 
273 // The native window is released with the object.
275  FlSubsurfaceEGL* egl = CreateEGL();
276  ASSERT_NE(egl, nullptr);
277 
278  EXPECT_CALL(wayland, EGLWindowDestroy());
279 
280  g_object_unref(egl);
281 }
struct wl_surface * parent_surface
::testing::NiceMock< flutter::testing::MockEpoxy > epoxy
FlSubsurfaceEGL * CreateEGL()
FlOpenGLManager * opengl_manager
FlWaylandDisplay * display
g_autoptr(FlEngine) engine
FlOpenGLManager * fl_opengl_manager_new()
FlSubsurfaceEGL * fl_subsurface_egl_new(FlOpenGLManager *opengl_manager, FlSubsurface *subsurface, size_t width, size_t height, gint scale)
void fl_subsurface_egl_present(FlSubsurfaceEGL *self, GLuint texture_id, size_t width, size_t height, FlGLFence *fence)
void fl_subsurface_egl_resize(FlSubsurfaceEGL *self, size_t width, size_t height)
G_BEGIN_DECLS FlSubsurface * subsurface
constexpr size_t kHeight
constexpr size_t kWidth
constexpr size_t kBufferWidth
constexpr gint kScale
TEST_F(FlSubsurfaceEGLTest, Create)
constexpr size_t kBufferHeight
FlSubsurface * fl_wayland_display_create_subsurface(FlWaylandDisplay *self, struct wl_surface *parent_surface)
FlWaylandDisplay * fl_wayland_display_open(struct wl_display *wl_display)