Skip to content

Commit 22e321a

Browse files
committed
[Impeller] Log GLES pass encoding failures instead of aborting
A driver can refuse a command when a blit or render pass is encoded in the reactor, for example by reporting an incomplete framebuffer for a texture readback. FML_CHECK then aborted the process, in release builds too. Log a validation error instead, so the pass fails and the app keeps running. Fixes flutter#195319
1 parent 9347202 commit 22e321a

5 files changed

Lines changed: 192 additions & 4 deletions

File tree

‎engine/src/flutter/impeller/renderer/backend/gles/BUILD.gn‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ impeller_component("gles_unittests") {
2727
testonly = true
2828
sources = [
2929
"blit_command_gles_unittests.cc",
30+
"blit_pass_gles_unittests.cc",
3031
"buffer_bindings_gles_unittests.cc",
3132
"command_buffer_gles_unittests.cc",
3233
"device_buffer_gles_unittests.cc",

‎engine/src/flutter/impeller/renderer/backend/gles/blit_pass_gles.cc‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "flutter/fml/trace_event.h"
1010
#include "fml/closure.h"
11+
#include "impeller/base/validation.h"
1112
#include "impeller/core/formats.h"
1213
#include "impeller/renderer/backend/gles/blit_command_gles.h"
1314
#include "impeller/renderer/backend/gles/proc_table_gles.h"
@@ -84,8 +85,13 @@ bool BlitPassGLES::EncodeCommands() const {
8485
std::shared_ptr<const BlitPassGLES> shared_this = shared_from_this();
8586
return reactor_->AddOperation([blit_pass = std::move(shared_this),
8687
label = label_](const auto& reactor) {
87-
auto result = EncodeCommandsInReactor(reactor, blit_pass->commands_, label);
88-
FML_CHECK(result) << "Must be able to encode GL commands without error.";
88+
// A driver can refuse a command here, for example by reporting an
89+
// incomplete framebuffer for a readback. That is a failed pass, not a
90+
// broken engine invariant, so it is logged rather than fatal.
91+
if (!EncodeCommandsInReactor(reactor, blit_pass->commands_, label)) {
92+
VALIDATION_LOG << "Could not encode the commands of blit pass '" << label
93+
<< "'.";
94+
}
8995
});
9096
}
9197

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 <memory>
6+
#include <string>
7+
#include <vector>
8+
9+
#include "flutter/testing/testing.h" // IWYU pragma: keep
10+
#include "gmock/gmock.h"
11+
#include "gtest/gtest.h"
12+
#include "impeller/base/validation.h"
13+
#include "impeller/core/allocator.h"
14+
#include "impeller/core/device_buffer_descriptor.h"
15+
#include "impeller/core/formats.h"
16+
#include "impeller/renderer/backend/gles/context_gles.h"
17+
#include "impeller/renderer/backend/gles/proc_table_gles.h"
18+
#include "impeller/renderer/backend/gles/reactor_gles.h"
19+
#include "impeller/renderer/backend/gles/test/mock_gles.h"
20+
#include "impeller/renderer/backend/gles/texture_gles.h"
21+
#include "impeller/renderer/blit_pass.h"
22+
#include "impeller/renderer/command_buffer.h"
23+
#include "impeller/renderer/context.h"
24+
25+
namespace impeller {
26+
namespace testing {
27+
28+
using ::testing::_;
29+
using ::testing::Contains;
30+
using ::testing::HasSubstr;
31+
using ::testing::NiceMock;
32+
using ::testing::Return;
33+
using ::testing::SetArgPointee;
34+
35+
namespace {
36+
37+
class MockWorker final : public ReactorGLES::Worker {
38+
public:
39+
// |ReactorGLES::Worker|
40+
bool CanReactorReactOnCurrentThreadNow(
41+
const ReactorGLES& reactor) const override {
42+
return true;
43+
}
44+
};
45+
46+
std::shared_ptr<ContextGLES> CreateFakeGLESContext() {
47+
auto gl_procs = std::make_unique<ProcTableGLES>(kMockResolverGLES);
48+
auto shader_library = std::vector<std::shared_ptr<fml::Mapping>>{};
49+
return ContextGLES::Create(Flags{}, std::move(gl_procs), shader_library,
50+
false);
51+
}
52+
53+
} // namespace
54+
55+
// Some drivers report an incomplete framebuffer for the texture a readback
56+
// reads from (https://github.com/flutter/flutter/issues/195319). The pass is
57+
// reported as a validation error and the process keeps running.
58+
TEST(BlitPassGLESTest, ReadbackRefusedByTheDriverDoesNotAbort) {
59+
auto mock_gl_impl = std::make_unique<NiceMock<MockGLESImpl>>();
60+
auto& mock_gl_impl_ref = *mock_gl_impl;
61+
auto mock_gl = MockGLES::Init(std::move(mock_gl_impl));
62+
63+
ON_CALL(mock_gl_impl_ref, GenTextures(1, _))
64+
.WillByDefault(SetArgPointee<1>(1));
65+
ON_CALL(mock_gl_impl_ref, GenFramebuffers(1, _))
66+
.WillByDefault(SetArgPointee<1>(2));
67+
EXPECT_CALL(mock_gl_impl_ref, CheckFramebufferStatus(GL_FRAMEBUFFER))
68+
.WillOnce(Return(GL_FRAMEBUFFER_UNSUPPORTED));
69+
EXPECT_CALL(mock_gl_impl_ref, ReadPixels(_, _, _, _, _, _, _)).Times(0);
70+
71+
auto context_gles = CreateFakeGLESContext();
72+
ASSERT_TRUE(context_gles);
73+
// The reactor only keeps a weak reference to its workers.
74+
auto worker = std::make_shared<MockWorker>();
75+
context_gles->AddReactorWorker(worker);
76+
auto reactor = context_gles->GetReactor();
77+
auto context = std::static_pointer_cast<Context>(context_gles);
78+
79+
auto texture = std::make_shared<TextureGLES>(
80+
reactor, TextureDescriptor{.format = PixelFormat::kR8G8B8A8UNormInt,
81+
.size = {10, 10},
82+
.usage = TextureUsage::kRenderTarget});
83+
auto buffer =
84+
context->GetResourceAllocator()->CreateBuffer(DeviceBufferDescriptor{
85+
.storage_mode = StorageMode::kHostVisible, .size = 10 * 10 * 4});
86+
ASSERT_TRUE(buffer);
87+
ASSERT_TRUE(reactor->React());
88+
89+
auto blit_pass = context->CreateCommandBuffer()->CreateBlitPass();
90+
ASSERT_TRUE(blit_pass);
91+
ASSERT_TRUE(blit_pass->AddCopy(texture, buffer));
92+
93+
// The worker lets the reactor react on this thread, so the pass is encoded
94+
// as soon as it is queued.
95+
std::vector<std::string> validation_errors;
96+
ImpellerValidationErrorsSetCallback(
97+
[&validation_errors](const char* message, const char* file, int line) {
98+
validation_errors.emplace_back(message);
99+
return true;
100+
});
101+
const bool queued = blit_pass->EncodeCommands();
102+
ImpellerValidationErrorsSetCallback(nullptr);
103+
104+
EXPECT_TRUE(queued);
105+
EXPECT_THAT(
106+
validation_errors,
107+
Contains(HasSubstr("Could not encode the commands of blit pass")));
108+
}
109+
110+
} // namespace testing
111+
} // namespace impeller

‎engine/src/flutter/impeller/renderer/backend/gles/render_pass_gles.cc‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,8 +1009,13 @@ bool RenderPassGLES::OnEncodeCommands(const Context& context) const {
10091009
/*bound_buffers=*/render_pass->bound_buffers_, //
10101010
/*tracer=*/tracer, //
10111011
/*impeller_context=*/render_pass->context_);
1012-
FML_CHECK(result)
1013-
<< "Must be able to encode GL commands without error.";
1012+
// A driver can refuse a command here, for example by reporting an
1013+
// incomplete framebuffer. That is a failed pass, not a broken engine
1014+
// invariant, so it is logged rather than fatal.
1015+
if (!result) {
1016+
VALIDATION_LOG << "Could not encode the commands of render pass '"
1017+
<< pass_data->label << "'.";
1018+
}
10141019
},
10151020
/*defer=*/true);
10161021
}

‎engine/src/flutter/impeller/renderer/backend/gles/render_pass_gles_unittests.cc‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
// found in the LICENSE file.
44

55
#include <memory>
6+
#include <string>
7+
#include <vector>
8+
69
#include "flutter/testing/testing.h" // IWYU pragma: keep
710
#include "gmock/gmock.h"
811
#include "gtest/gtest.h"
12+
#include "impeller/base/validation.h"
913
#include "impeller/core/device_buffer.h"
1014
#include "impeller/core/formats.h"
1115
#include "impeller/renderer/backend/gles/command_buffer_gles.h"
@@ -25,7 +29,9 @@ namespace testing {
2529

2630
using ::testing::_;
2731
using ::testing::Args;
32+
using ::testing::Contains;
2833
using ::testing::ElementsAreArray;
34+
using ::testing::HasSubstr;
2935
using ::testing::NiceMock;
3036
using ::testing::Return;
3137
using ::testing::SetArgPointee;
@@ -231,6 +237,65 @@ TEST(RenderPassGLESTest, ResolvingMultisampleTextureCachesResolveFBO) {
231237
}
232238
}
233239

240+
// Some drivers report an incomplete framebuffer for an offscreen render target
241+
// (https://github.com/flutter/flutter/issues/194171). The pass is reported as
242+
// a validation error and the process keeps running.
243+
TEST(RenderPassGLESTest, FramebufferRefusedByTheDriverDoesNotAbort) {
244+
#ifndef IMPELLER_DEBUG
245+
GTEST_SKIP() << "The framebuffer status is only checked in debug builds.";
246+
#endif // IMPELLER_DEBUG
247+
auto mock_gl_impl = std::make_unique<NiceMock<MockGLESImpl>>();
248+
auto& mock_gl_impl_ref = *mock_gl_impl;
249+
auto mock_gl =
250+
MockGLES::Init(std::move(mock_gl_impl), std::nullopt, "OpenGL ES 3.0");
251+
252+
auto context = CreateFakeGLESContext();
253+
auto dummy_worker = std::make_shared<MockWorker>();
254+
context->AddReactorWorker(dummy_worker);
255+
auto reactor = context->GetReactor();
256+
257+
const auto command_buffer =
258+
std::static_pointer_cast<Context>(context)->CreateCommandBuffer();
259+
260+
auto color_texture = std::make_shared<TextureGLES>(
261+
reactor, TextureDescriptor{.storage_mode = StorageMode::kDevicePrivate,
262+
.type = TextureType::kTexture2D,
263+
.format = PixelFormat::kR8G8B8A8UNormInt,
264+
.size = {10, 10},
265+
.usage = TextureUsage::kRenderTarget,
266+
.sample_count = SampleCount::kCount1});
267+
auto render_target = RenderTarget{};
268+
auto color_attachment = ColorAttachment{Attachment{
269+
.texture = color_texture,
270+
.load_action = LoadAction::kClear,
271+
.store_action = StoreAction::kStore,
272+
}};
273+
color_attachment.clear_color = Color::Black();
274+
render_target.SetColorAttachment(color_attachment, 0);
275+
276+
EXPECT_CALL(mock_gl_impl_ref, CheckFramebufferStatus(_))
277+
.WillRepeatedly(Return(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT));
278+
279+
const auto render_pass = command_buffer->CreateRenderPass(render_target);
280+
ASSERT_TRUE(render_pass);
281+
// The render pass defers its encoding to the next reaction.
282+
ASSERT_TRUE(render_pass->EncodeCommands());
283+
284+
std::vector<std::string> validation_errors;
285+
ImpellerValidationErrorsSetCallback(
286+
[&validation_errors](const char* message, const char* file, int line) {
287+
validation_errors.emplace_back(message);
288+
return true;
289+
});
290+
const bool reacted = reactor->React();
291+
ImpellerValidationErrorsSetCallback(nullptr);
292+
293+
EXPECT_TRUE(reacted);
294+
EXPECT_THAT(
295+
validation_errors,
296+
Contains(HasSubstr("Could not encode the commands of render pass")));
297+
}
298+
234299
class RenderPassGLESCommandTest : public ::testing::Test {
235300
protected:
236301
// Builds a mock OpenGL ES context with a render pass and a minimal

0 commit comments

Comments
 (0)