|
| 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 "flutter/testing/testing.h" // IWYU pragma: keep |
| 6 | +#include "gtest/gtest.h" |
| 7 | +#include "impeller/renderer/backend/gles/blit_command_gles.h" |
| 8 | +#include "impeller/renderer/backend/gles/device_buffer_gles.h" |
| 9 | +#include "impeller/renderer/backend/gles/test/mock_gles.h" |
| 10 | +#include "impeller/renderer/backend/gles/texture_gles.h" |
| 11 | + |
| 12 | +namespace impeller { |
| 13 | +namespace testing { |
| 14 | + |
| 15 | +using ::testing::_; |
| 16 | +using ::testing::Return; |
| 17 | + |
| 18 | +class TestReactorGLES : public ReactorGLES { |
| 19 | + public: |
| 20 | + TestReactorGLES() |
| 21 | + : ReactorGLES(std::make_unique<ProcTableGLES>(kMockResolverGLES)) {} |
| 22 | + |
| 23 | + ~TestReactorGLES() = default; |
| 24 | +}; |
| 25 | + |
| 26 | +class MockWorker final : public ReactorGLES::Worker { |
| 27 | + public: |
| 28 | + MockWorker() = default; |
| 29 | + |
| 30 | + // |ReactorGLES::Worker| |
| 31 | + bool CanReactorReactOnCurrentThreadNow( |
| 32 | + const ReactorGLES& reactor) const override { |
| 33 | + return true; |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +// This test makes sure we bind to GL_FRAMEBUFFER so that it's compatible for |
| 38 | +// OpenGLES 2 and OpenGLES 3. |
| 39 | +TEST(BlitCommandGLESTest, BlitCopyTextureToBufferCommandGLESBindsFramebuffer) { |
| 40 | + auto mock_gles_impl = std::make_unique<MockGLESImpl>(); |
| 41 | + auto& mock_gles_impl_ref = *mock_gles_impl; |
| 42 | + |
| 43 | + EXPECT_CALL(mock_gles_impl_ref, GenFramebuffers(1, _)) |
| 44 | + .WillOnce(::testing::SetArgPointee<1>(3)); |
| 45 | + EXPECT_CALL(mock_gles_impl_ref, GenTextures(1, _)) |
| 46 | + .WillOnce(::testing::SetArgPointee<1>(1)); |
| 47 | + EXPECT_CALL(mock_gles_impl_ref, BindFramebuffer(GL_FRAMEBUFFER, 3)).Times(1); |
| 48 | + EXPECT_CALL(mock_gles_impl_ref, CheckFramebufferStatus(GL_FRAMEBUFFER)) |
| 49 | + .WillOnce(Return(GL_FRAMEBUFFER_COMPLETE)); |
| 50 | + EXPECT_CALL(mock_gles_impl_ref, ReadPixels(_, _, _, _, _, _, _)).Times(1); |
| 51 | + EXPECT_CALL(mock_gles_impl_ref, BindFramebuffer(GL_FRAMEBUFFER, 0)).Times(1); |
| 52 | + |
| 53 | + std::shared_ptr<MockGLES> mock_gl = MockGLES::Init(std::move(mock_gles_impl)); |
| 54 | + auto reactor = std::make_shared<TestReactorGLES>(); |
| 55 | + auto worker = std::make_shared<MockWorker>(); |
| 56 | + reactor->AddWorker(worker); |
| 57 | + |
| 58 | + // Create source texture. |
| 59 | + TextureDescriptor src_tex_desc; |
| 60 | + src_tex_desc.format = PixelFormat::kR8G8B8A8UNormInt; |
| 61 | + src_tex_desc.size = {10, 10}; |
| 62 | + src_tex_desc.usage = |
| 63 | + static_cast<TextureUsageMask>(TextureUsage::kRenderTarget); |
| 64 | + auto source_texture = std::make_shared<TextureGLES>(reactor, src_tex_desc); |
| 65 | + // Avoids the flip which would crash. |
| 66 | + source_texture->SetCoordinateSystem(TextureCoordinateSystem::kUploadFromHost); |
| 67 | + |
| 68 | + // Create destination buffer. |
| 69 | + DeviceBufferDescriptor dest_buffer_desc; |
| 70 | + dest_buffer_desc.size = 10 * 10 * 4; |
| 71 | + dest_buffer_desc.storage_mode = StorageMode::kHostVisible; |
| 72 | + auto allocation = std::make_shared<Allocation>(); |
| 73 | + ASSERT_TRUE(allocation->Truncate(Bytes(dest_buffer_desc.size))); |
| 74 | + auto dest_buffer = |
| 75 | + std::make_shared<DeviceBufferGLES>(dest_buffer_desc, reactor, allocation); |
| 76 | + |
| 77 | + ASSERT_TRUE(reactor->React()); |
| 78 | + |
| 79 | + BlitCopyTextureToBufferCommandGLES command; |
| 80 | + command.source = source_texture; |
| 81 | + command.destination = dest_buffer; |
| 82 | + command.source_region = |
| 83 | + IRect::MakeSize(source_texture->GetTextureDescriptor().size); |
| 84 | + command.label = "TestBlit"; |
| 85 | + |
| 86 | + EXPECT_TRUE(command.Encode(*reactor)); |
| 87 | + |
| 88 | + source_texture.reset(); |
| 89 | + dest_buffer.reset(); |
| 90 | + |
| 91 | + ASSERT_TRUE(reactor->React()); |
| 92 | +} |
| 93 | + |
| 94 | +} // namespace testing |
| 95 | +} // namespace impeller |
0 commit comments