|
| 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 |
0 commit comments