Skip to content

Commit 24d3c33

Browse files
committed
[Linux] Clear the frame when there is nothing to render
If everything on screen is transparent the engine sends a frame with no layers in it. We treated that as "nothing to do" and kept showing the last frame, so the old contents stayed on screen forever. Now each frame clears before the layers are drawn, so an empty frame clears the window. This also fixes transparent parts of a frame showing the previous frame through them. Two other things stopped an empty frame working: - The Wayland renderer returned before presenting the frame, so a first frame with no layers never showed the window. - The software renderer only drew a layer if it was the only layer, and drew it in the corner instead of at its position. It now draws all the layers like the OpenGL renderer does. Fixes flutter#191870
1 parent 3b192e9 commit 24d3c33

10 files changed

Lines changed: 327 additions & 57 deletions

‎engine/src/flutter/shell/platform/linux/fl_compositor_opengl.cc‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ static void composite_layer(FlCompositorOpenGL* self,
8585
void fl_compositor_opengl_composite_layers(FlCompositorOpenGL* self,
8686
const FlutterLayer** layers,
8787
size_t layers_count) {
88-
if (layers_count == 0) {
89-
return;
90-
}
91-
9288
// Save bindings that are set by this function. All bindings must be restored
9389
// to their original values because Skia expects that its bindings have not
9490
// been altered.
@@ -112,9 +108,11 @@ void fl_compositor_opengl_composite_layers(FlCompositorOpenGL* self,
112108
glGetIntegerv(GL_BLEND_DST_RGB, &saved_dst_rgb);
113109
GLint saved_dst_alpha;
114110
glGetIntegerv(GL_BLEND_DST_ALPHA, &saved_dst_alpha);
111+
GLfloat saved_clear_color[4] = {0.0, 0.0, 0.0, 0.0};
112+
glGetFloatv(GL_COLOR_CLEAR_VALUE, saved_clear_color);
115113

116-
size_t width = layers[0]->size.width;
117-
size_t height = layers[0]->size.height;
114+
size_t width = layers_count > 0 ? layers[0]->size.width : 0;
115+
size_t height = layers_count > 0 ? layers[0]->size.height : 0;
118116

119117
// FIXME(robert-ancell): The vertex array is the same for all views, but
120118
// cannot be shared in OpenGL. Find a way to not generate this every time.
@@ -132,6 +130,9 @@ void fl_compositor_opengl_composite_layers(FlCompositorOpenGL* self,
132130
// See OpenGL specification version 4.6, section 18.3.1.
133131
glDisable(GL_SCISSOR_TEST);
134132

133+
glClearColor(0.0, 0.0, 0.0, 0.0);
134+
glClear(GL_COLOR_BUFFER_BIT);
135+
135136
gboolean first_layer = TRUE;
136137
for (size_t i = 0; i < layers_count; ++i) {
137138
const FlutterLayer* layer = layers[i];
@@ -178,6 +179,8 @@ void fl_compositor_opengl_composite_layers(FlCompositorOpenGL* self,
178179
glDisable(GL_SCISSOR_TEST);
179180
}
180181

182+
glClearColor(saved_clear_color[0], saved_clear_color[1], saved_clear_color[2],
183+
saved_clear_color[3]);
181184
glBindTexture(GL_TEXTURE_2D, saved_texture_binding);
182185
glBindVertexArray(saved_vao_binding);
183186
glBindBuffer(GL_ARRAY_BUFFER, saved_array_buffer_binding);

‎engine/src/flutter/shell/platform/linux/fl_compositor_opengl_test.cc‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ class FlCompositorOpenGLTest : public flutter::testing::LinuxTest {
3939
FlCompositorOpenGL* compositor = nullptr;
4040
};
4141

42+
// The layers replace the target contents, so a frame with nothing to
43+
// rasterize (which has no layers) clears the previous frame.
44+
TEST_F(FlCompositorOpenGLTest, CompositeNoLayers) {
45+
compositor = fl_compositor_opengl_new(opengl_manager);
46+
47+
EXPECT_CALL(epoxy, glClear(GL_COLOR_BUFFER_BIT));
48+
49+
fl_compositor_opengl_composite_layers(compositor, nullptr, 0);
50+
}
51+
4252
TEST_F(FlCompositorOpenGLTest, Composite) {
4353
constexpr size_t width = 100;
4454
constexpr size_t height = 100;

‎engine/src/flutter/shell/platform/linux/fl_compositor_software.cc‎

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,41 @@ gboolean fl_compositor_software_composite_layers(FlCompositorSoftware* self,
2424
cairo_t* cr,
2525
const FlutterLayer** layers,
2626
size_t layers_count) {
27-
if (layers_count == 0) {
28-
return TRUE;
29-
}
27+
cairo_save(cr);
28+
cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
29+
cairo_paint(cr);
30+
cairo_restore(cr);
31+
32+
for (size_t i = 0; i < layers_count; i++) {
33+
const FlutterLayer* layer = layers[i];
34+
switch (layer->type) {
35+
case kFlutterLayerContentTypeBackingStore: {
36+
const FlutterBackingStore* backing_store = layer->backing_store;
37+
g_assert(backing_store->type == kFlutterBackingStoreTypeSoftware);
38+
39+
cairo_surface_t* surface = cairo_image_surface_create_for_data(
40+
static_cast<unsigned char*>(
41+
const_cast<void*>(backing_store->software.allocation)),
42+
CAIRO_FORMAT_ARGB32, backing_store->software.row_bytes / 4,
43+
backing_store->software.height, backing_store->software.row_bytes);
44+
45+
// Layers are placed at their offset in the frame, and only cover the
46+
// area they were rendered for.
47+
cairo_save(cr);
48+
cairo_rectangle(cr, layer->offset.x, layer->offset.y, layer->size.width,
49+
layer->size.height);
50+
cairo_clip(cr);
51+
cairo_set_source_surface(cr, surface, layer->offset.x, layer->offset.y);
52+
cairo_paint(cr);
53+
cairo_restore(cr);
3054

31-
// TODO(robert-ancell): Support multiple layers
32-
if (layers_count == 1) {
33-
const FlutterLayer* layer = layers[0];
34-
g_assert(layer->type == kFlutterLayerContentTypeBackingStore);
35-
g_assert(layer->backing_store->type == kFlutterBackingStoreTypeSoftware);
36-
const FlutterBackingStore* backing_store = layer->backing_store;
37-
38-
cairo_surface_t* surface = cairo_image_surface_create_for_data(
39-
static_cast<unsigned char*>(
40-
const_cast<void*>(backing_store->software.allocation)),
41-
CAIRO_FORMAT_ARGB32, backing_store->software.row_bytes / 4,
42-
backing_store->software.height, backing_store->software.row_bytes);
43-
cairo_set_source_surface(cr, surface, 0.0, 0.0);
44-
cairo_paint(cr);
45-
cairo_surface_destroy(surface);
55+
cairo_surface_destroy(surface);
56+
} break;
57+
case kFlutterLayerContentTypePlatformView: {
58+
// TODO(robert-ancell) Not implemented -
59+
// https://github.com/flutter/flutter/issues/41724
60+
} break;
61+
}
4662
}
4763

4864
return TRUE;

‎engine/src/flutter/shell/platform/linux/fl_compositor_software_test.cc‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,140 @@ TEST_F(FlCompositorSoftwareTest, Render) {
5454
cairo_destroy(cr);
5555
cairo_surface_destroy(surface);
5656
}
57+
58+
// The layers replace the target contents, so a frame with nothing to
59+
// rasterize (which has no layers) clears the previous frame.
60+
TEST_F(FlCompositorSoftwareTest, RenderNoLayers) {
61+
constexpr size_t width = 100;
62+
constexpr size_t height = 100;
63+
64+
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
65+
g_autofree unsigned char* image_data =
66+
static_cast<unsigned char*>(malloc(height * stride));
67+
// Fill the surface with an opaque white, as if a previous frame had been
68+
// rendered into it.
69+
memset(image_data, 0xFF, height * stride);
70+
cairo_surface_t* surface = cairo_image_surface_create_for_data(
71+
image_data, CAIRO_FORMAT_ARGB32, width, height, stride);
72+
cairo_t* cr = cairo_create(surface);
73+
EXPECT_TRUE(
74+
fl_compositor_software_composite_layers(compositor, cr, nullptr, 0));
75+
cairo_surface_flush(surface);
76+
77+
// The previous frame was cleared.
78+
uint32_t* pixels = reinterpret_cast<uint32_t*>(image_data);
79+
EXPECT_EQ(pixels[50 * (stride / 4) + 50], 0x00000000u);
80+
81+
cairo_destroy(cr);
82+
cairo_surface_destroy(surface);
83+
}
84+
85+
TEST_F(FlCompositorSoftwareTest, RenderMultipleLayers) {
86+
constexpr size_t width = 100;
87+
constexpr size_t height = 100;
88+
89+
// The first layer covers the whole frame, the second is a smaller layer
90+
// offset inside it.
91+
size_t layer0_row_bytes = width * 4;
92+
g_autofree unsigned char* layer0_data =
93+
static_cast<unsigned char*>(malloc(height * layer0_row_bytes));
94+
// Fill the layer with an opaque white.
95+
memset(layer0_data, 0xFF, height * layer0_row_bytes);
96+
FlutterBackingStore backing_store0 = {
97+
.type = kFlutterBackingStoreTypeSoftware,
98+
.software = {.allocation = layer0_data,
99+
.row_bytes = layer0_row_bytes,
100+
.height = height}};
101+
FlutterLayer layer0 = {.type = kFlutterLayerContentTypeBackingStore,
102+
.backing_store = &backing_store0,
103+
.offset = {0, 0},
104+
.size = {width, height}};
105+
106+
constexpr size_t layer1_width = 10;
107+
constexpr size_t layer1_height = 10;
108+
size_t layer1_row_bytes = layer1_width * 4;
109+
g_autofree unsigned char* layer1_data =
110+
static_cast<unsigned char*>(malloc(layer1_height * layer1_row_bytes));
111+
// Fill the layer with an opaque red.
112+
uint32_t* layer1_pixels = reinterpret_cast<uint32_t*>(layer1_data);
113+
for (size_t i = 0; i < layer1_width * layer1_height; i++) {
114+
layer1_pixels[i] = 0xFFFF0000;
115+
}
116+
FlutterBackingStore backing_store1 = {
117+
.type = kFlutterBackingStoreTypeSoftware,
118+
.software = {.allocation = layer1_data,
119+
.row_bytes = layer1_row_bytes,
120+
.height = layer1_height}};
121+
FlutterLayer layer1 = {.type = kFlutterLayerContentTypeBackingStore,
122+
.backing_store = &backing_store1,
123+
.offset = {20, 20},
124+
.size = {layer1_width, layer1_height}};
125+
126+
const FlutterLayer* layers[2] = {&layer0, &layer1};
127+
128+
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
129+
g_autofree unsigned char* image_data =
130+
static_cast<unsigned char*>(malloc(height * stride));
131+
memset(image_data, 0, height * stride);
132+
cairo_surface_t* surface = cairo_image_surface_create_for_data(
133+
image_data, CAIRO_FORMAT_ARGB32, width, height, stride);
134+
cairo_t* cr = cairo_create(surface);
135+
EXPECT_TRUE(
136+
fl_compositor_software_composite_layers(compositor, cr, layers, 2));
137+
cairo_surface_flush(surface);
138+
139+
uint32_t* pixels = reinterpret_cast<uint32_t*>(image_data);
140+
// The second layer is drawn at its offset.
141+
EXPECT_EQ(pixels[25 * (stride / 4) + 25], 0xFFFF0000u);
142+
// The first layer is drawn everywhere the second doesn't cover.
143+
EXPECT_EQ(pixels[50 * (stride / 4) + 50], 0xFFFFFFFFu);
144+
EXPECT_EQ(pixels[25 * (stride / 4) + 15], 0xFFFFFFFFu);
145+
EXPECT_EQ(pixels[15 * (stride / 4) + 25], 0xFFFFFFFFu);
146+
147+
cairo_destroy(cr);
148+
cairo_surface_destroy(surface);
149+
}
150+
151+
// Platform views are not implemented, but must not stop the other layers being
152+
// drawn.
153+
TEST_F(FlCompositorSoftwareTest, RenderPlatformViewLayer) {
154+
constexpr size_t width = 100;
155+
constexpr size_t height = 100;
156+
size_t row_bytes = width * 4;
157+
g_autofree unsigned char* layer_data =
158+
static_cast<unsigned char*>(malloc(height * row_bytes));
159+
// Fill the layer with an opaque white.
160+
memset(layer_data, 0xFF, height * row_bytes);
161+
FlutterPlatformView platform_view = {.identifier = 1};
162+
FlutterLayer platform_view_layer = {
163+
.type = kFlutterLayerContentTypePlatformView,
164+
.platform_view = &platform_view,
165+
.offset = {0, 0},
166+
.size = {width, height}};
167+
FlutterBackingStore backing_store = {
168+
.type = kFlutterBackingStoreTypeSoftware,
169+
.software = {
170+
.allocation = layer_data, .row_bytes = row_bytes, .height = height}};
171+
FlutterLayer layer = {.type = kFlutterLayerContentTypeBackingStore,
172+
.backing_store = &backing_store,
173+
.offset = {0, 0},
174+
.size = {width, height}};
175+
const FlutterLayer* layers[2] = {&platform_view_layer, &layer};
176+
177+
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
178+
g_autofree unsigned char* image_data =
179+
static_cast<unsigned char*>(malloc(height * stride));
180+
memset(image_data, 0, height * stride);
181+
cairo_surface_t* surface = cairo_image_surface_create_for_data(
182+
image_data, CAIRO_FORMAT_ARGB32, width, height, stride);
183+
cairo_t* cr = cairo_create(surface);
184+
EXPECT_TRUE(
185+
fl_compositor_software_composite_layers(compositor, cr, layers, 2));
186+
cairo_surface_flush(surface);
187+
188+
uint32_t* pixels = reinterpret_cast<uint32_t*>(image_data);
189+
EXPECT_EQ(pixels[50 * (stride / 4) + 50], 0xFFFFFFFFu);
190+
191+
cairo_destroy(cr);
192+
cairo_surface_destroy(surface);
193+
}

‎engine/src/flutter/shell/platform/linux/fl_opengl_frame.cc‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,23 @@ void fl_opengl_frame_composite(FlOpenGLFrame* self,
5555
size_t layers_count) {
5656
g_return_if_fail(FL_IS_OPENGL_FRAME(self));
5757

58-
if (layers_count == 0) {
58+
size_t width, height;
59+
if (layers_count > 0) {
60+
width = layers[0]->size.width;
61+
height = layers[0]->size.height;
62+
} else if (self->framebuffer != nullptr) {
63+
// A frame with nothing to rasterize, e.g. everything painted was fully
64+
// transparent, has no layers and so no size. Keep the current size so the
65+
// frame still matches the window and is drawn (showing the view
66+
// background) rather than being skipped - the contents are cleared when
67+
// the layers are composited below.
68+
width = fl_framebuffer_get_width(self->framebuffer);
69+
height = fl_framebuffer_get_height(self->framebuffer);
70+
} else {
71+
// Nothing has been rendered yet, so there is nothing to clear.
5972
return;
6073
}
6174

62-
size_t width = layers[0]->size.width;
63-
size_t height = layers[0]->size.height;
64-
6575
if (width == 0 || height == 0) {
6676
// A zero-sized layer has no content to show. Drop any existing frame so
6777
// the renderer reports no frame rather than an empty framebuffer (a 0x0

‎engine/src/flutter/shell/platform/linux/fl_opengl_frame_test.cc‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,57 @@ TEST_F(FlOpenGLFrameTest, ZeroSizeClearsFrame) {
137137
EXPECT_EQ(frame_height, 0u);
138138
}
139139

140+
// A frame with nothing to rasterize (e.g. everything painted is fully
141+
// transparent) has no layers. The previous frame must not be shown again.
142+
TEST_F(FlOpenGLFrameTest, NoLayersClearsFrame) {
143+
constexpr size_t width = 100;
144+
constexpr size_t height = 100;
145+
146+
g_autoptr(FlOpenGLFrame) frame = fl_opengl_frame_new();
147+
g_autoptr(FlFramebuffer) framebuffer =
148+
fl_framebuffer_new(GL_RGBA, width, height);
149+
FlutterBackingStore backing_store = {
150+
.type = kFlutterBackingStoreTypeOpenGL,
151+
.open_gl = {
152+
.type = kFlutterOpenGLTargetTypeFramebuffer,
153+
.framebuffer = {.target = GL_RGBA8, .user_data = framebuffer}}};
154+
FlutterLayer layer = {.type = kFlutterLayerContentTypeBackingStore,
155+
.backing_store = &backing_store,
156+
.offset = {0, 0},
157+
.size = {width, height}};
158+
const FlutterLayer* layers[1] = {&layer};
159+
160+
fl_opengl_frame_composite(frame, compositor, layers, 1);
161+
162+
EXPECT_CALL(epoxy, glClear(GL_COLOR_BUFFER_BIT));
163+
164+
fl_opengl_frame_composite(frame, compositor, nullptr, 0);
165+
166+
// The frame is kept at the same size so it still matches the window and is
167+
// drawn, showing the view background rather than the previous frame.
168+
size_t frame_width = 0;
169+
size_t frame_height = 0;
170+
fl_opengl_frame_get_size(frame, &frame_width, &frame_height);
171+
EXPECT_EQ(frame_width, width);
172+
EXPECT_EQ(frame_height, height);
173+
}
174+
175+
// A frame with no layers received before any content has been rendered has
176+
// nothing to clear.
177+
TEST_F(FlOpenGLFrameTest, NoLayersWithoutFrame) {
178+
g_autoptr(FlOpenGLFrame) frame = fl_opengl_frame_new();
179+
180+
EXPECT_CALL(epoxy, glClear).Times(0);
181+
182+
fl_opengl_frame_composite(frame, compositor, nullptr, 0);
183+
184+
size_t frame_width = 123;
185+
size_t frame_height = 456;
186+
fl_opengl_frame_get_size(frame, &frame_width, &frame_height);
187+
EXPECT_EQ(frame_width, 0u);
188+
EXPECT_EQ(frame_height, 0u);
189+
}
190+
140191
// Frames are copied into CPU memory so they can be used in the OpenGL context
141192
// GTK draws with, which doesn't share objects with the Flutter context.
142193
TEST_F(FlOpenGLFrameTest, CompositeReadsBackPixels) {

‎engine/src/flutter/shell/platform/linux/fl_view_renderer_software.cc‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,22 @@ static void fl_view_renderer_software_present_layers(
171171
}
172172

173173
g_mutex_lock(&self->frame_mutex);
174+
size_t width = 0;
175+
size_t height = 0;
176+
gboolean have_frame = TRUE;
174177
if (layers_count > 0) {
175-
size_t width = layers[0]->size.width;
176-
size_t height = layers[0]->size.height;
178+
width = layers[0]->size.width;
179+
height = layers[0]->size.height;
180+
} else if (self->surface != nullptr) {
181+
// Keep current size if no layers - just clearing.
182+
width = cairo_image_surface_get_width(self->surface);
183+
height = cairo_image_surface_get_height(self->surface);
184+
} else {
185+
// Nothing has been rendered yet, so there is nothing to clear.
186+
have_frame = FALSE;
187+
}
177188

189+
if (have_frame) {
178190
if (width == 0 || height == 0) {
179191
// A zero-sized layer has no content to show. Drop any existing frame so
180192
// the renderer reports no frame rather than an empty surface (a 0x0

0 commit comments

Comments
 (0)