|
| 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 <optional> |
| 7 | + |
| 8 | +#include "flutter/impeller/renderer/backend/gles/gles.h" |
| 9 | +#include "flutter/shell/platform/windows/compositor_opengl.h" |
| 10 | +#include "flutter/shell/platform/windows/egl/manager.h" |
| 11 | +#include "flutter/shell/platform/windows/flutter_window.h" |
| 12 | +#include "flutter/shell/platform/windows/flutter_windows_view.h" |
| 13 | +#include "flutter/shell/platform/windows/testing/engine_modifier.h" |
| 14 | +#include "flutter/shell/platform/windows/testing/flutter_windows_engine_builder.h" |
| 15 | +#include "flutter/shell/platform/windows/testing/windows_test.h" |
| 16 | +#include "gtest/gtest.h" |
| 17 | + |
| 18 | +namespace flutter { |
| 19 | +namespace testing { |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +// Resolves OpenGL procedures through EGL, as the engine does. |
| 24 | +const impeller::ProcTableGLES::Resolver kEGLResolver = |
| 25 | + [](const char* name) -> void* { |
| 26 | + return reinterpret_cast<void*>(::eglGetProcAddress(name)); |
| 27 | +}; |
| 28 | + |
| 29 | +// Verifies that |framebuffer| is complete on the current context. |
| 30 | +void ExpectFramebufferComplete(GLuint framebuffer) { |
| 31 | + impeller::ProcTableGLES gl{kEGLResolver}; |
| 32 | + ASSERT_TRUE(gl.IsValid()); |
| 33 | + |
| 34 | + gl.BindFramebuffer(GL_FRAMEBUFFER, framebuffer); |
| 35 | + EXPECT_EQ(gl.CheckFramebufferStatus(GL_FRAMEBUFFER), |
| 36 | + static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE)); |
| 37 | +} |
| 38 | + |
| 39 | +// Verifies that no GL error is pending on the current context. GL reports |
| 40 | +// failed calls this way rather than through return values. |
| 41 | +void ExpectNoGLError() { |
| 42 | + auto get_error = |
| 43 | + reinterpret_cast<PFNGLGETERRORPROC>(kEGLResolver("glGetError")); |
| 44 | + ASSERT_NE(get_error, nullptr); |
| 45 | + EXPECT_EQ(get_error(), static_cast<GLenum>(GL_NO_ERROR)); |
| 46 | +} |
| 47 | + |
| 48 | +struct TestParams { |
| 49 | + // The test's name suffix. |
| 50 | + const char* name; |
| 51 | + // Whether the compositor is configured for Impeller rather than Skia. |
| 52 | + bool enable_impeller; |
| 53 | + // Caps the D3D11 feature level ANGLE may use. At 10_0 it only creates OpenGL |
| 54 | + // ES 2.0 contexts, like the GPUs where |
| 55 | + // https://github.com/flutter/flutter/issues/191978 left the window black. |
| 56 | + std::optional<egl::D3DFeatureLevel> max_feature_level; |
| 57 | +}; |
| 58 | + |
| 59 | +// Tests the compositor against ANGLE, unlike CompositorOpenGLTest in |
| 60 | +// compositor_opengl_unittests.cc, which uses mocked EGL and OpenGL. |
| 61 | +class CompositorOpenGLANGLETest |
| 62 | + : public WindowsTest, |
| 63 | + public ::testing::WithParamInterface<TestParams> { |
| 64 | + public: |
| 65 | + CompositorOpenGLANGLETest() = default; |
| 66 | + virtual ~CompositorOpenGLANGLETest() = default; |
| 67 | + |
| 68 | + protected: |
| 69 | + void TearDown() override { |
| 70 | + // The compositor leaves the render context current on this thread. |
| 71 | + // Release it before the engine destroys it, or ANGLE crashes the next test |
| 72 | + // that reuses the display. |
| 73 | + if (engine_ && engine_->egl_manager() && |
| 74 | + engine_->egl_manager()->render_context()) { |
| 75 | + engine_->egl_manager()->render_context()->ClearCurrent(); |
| 76 | + } |
| 77 | + WindowsTest::TearDown(); |
| 78 | + } |
| 79 | + |
| 80 | + FlutterWindowsEngine* engine() { return engine_.get(); } |
| 81 | + FlutterWindowsView* view() { return view_.get(); } |
| 82 | + |
| 83 | + void UseHeadlessEngine() { |
| 84 | + auto egl_manager = egl::Manager::Create(egl::GpuPreference::NoPreference, |
| 85 | + /*allow_inverted_surface=*/false, |
| 86 | + GetParam().max_feature_level); |
| 87 | + EXPECT_NE(egl_manager, nullptr); |
| 88 | + |
| 89 | + FlutterWindowsEngineBuilder builder{GetContext()}; |
| 90 | + |
| 91 | + engine_ = builder.Build(); |
| 92 | + EngineModifier modifier{engine_.get()}; |
| 93 | + modifier.SetEGLManager(std::move(egl_manager)); |
| 94 | + } |
| 95 | + |
| 96 | + // Adds a view with a real window and surface to the engine. |
| 97 | + void UseEngineWithView() { |
| 98 | + UseHeadlessEngine(); |
| 99 | + |
| 100 | + auto window = std::make_unique<FlutterWindow>( |
| 101 | + 320, 240, engine_->display_manager(), engine_->windows_proc_table()); |
| 102 | + view_ = std::make_unique<FlutterWindowsView>(kImplicitViewId, engine_.get(), |
| 103 | + std::move(window), false, |
| 104 | + BoxConstraints()); |
| 105 | + view_->CreateRenderSurface(); |
| 106 | + } |
| 107 | + |
| 108 | + private: |
| 109 | + std::unique_ptr<FlutterWindowsEngine> engine_; |
| 110 | + std::unique_ptr<FlutterWindowsView> view_; |
| 111 | + |
| 112 | + FML_DISALLOW_COPY_AND_ASSIGN(CompositorOpenGLANGLETest); |
| 113 | +}; |
| 114 | + |
| 115 | +} // namespace |
| 116 | + |
| 117 | +INSTANTIATE_TEST_SUITE_P( |
| 118 | + RendererAndFeatureLevel, |
| 119 | + CompositorOpenGLANGLETest, |
| 120 | + ::testing::Values( |
| 121 | + TestParams{.name = "SkiaFeatureLevelDefault", |
| 122 | + .enable_impeller = false, |
| 123 | + .max_feature_level = std::nullopt}, |
| 124 | + TestParams{.name = "ImpellerFeatureLevelDefault", |
| 125 | + .enable_impeller = true, |
| 126 | + .max_feature_level = std::nullopt}, |
| 127 | + TestParams{.name = "SkiaFeatureLevel10_0", |
| 128 | + .enable_impeller = false, |
| 129 | + .max_feature_level = egl::D3DFeatureLevel{10, 0}}, |
| 130 | + TestParams{.name = "ImpellerFeatureLevel10_0", |
| 131 | + .enable_impeller = true, |
| 132 | + .max_feature_level = egl::D3DFeatureLevel{10, 0}}), |
| 133 | + [](const ::testing::TestParamInfo<TestParams>& info) { |
| 134 | + return std::string(info.param.name); |
| 135 | + }); |
| 136 | + |
| 137 | +TEST_P(CompositorOpenGLANGLETest, CreateBackingStore) { |
| 138 | + UseHeadlessEngine(); |
| 139 | + |
| 140 | + auto compositor = |
| 141 | + CompositorOpenGL{engine(), kEGLResolver, GetParam().enable_impeller}; |
| 142 | + FlutterBackingStoreConfig config = {}; |
| 143 | + config.size = {320, 240}; |
| 144 | + FlutterBackingStore backing_store = {}; |
| 145 | + |
| 146 | + ASSERT_TRUE(compositor.CreateBackingStore(config, &backing_store)); |
| 147 | + ExpectNoGLError(); |
| 148 | + ExpectFramebufferComplete(backing_store.open_gl.framebuffer.name); |
| 149 | + ASSERT_TRUE(compositor.CollectBackingStore(&backing_store)); |
| 150 | +} |
| 151 | + |
| 152 | +TEST_P(CompositorOpenGLANGLETest, Present) { |
| 153 | + UseEngineWithView(); |
| 154 | + |
| 155 | + auto compositor = |
| 156 | + CompositorOpenGL{engine(), kEGLResolver, GetParam().enable_impeller}; |
| 157 | + FlutterBackingStoreConfig config = {}; |
| 158 | + config.size = {320, 240}; |
| 159 | + FlutterBackingStore backing_store = {}; |
| 160 | + |
| 161 | + ASSERT_TRUE(compositor.CreateBackingStore(config, &backing_store)); |
| 162 | + ExpectNoGLError(); |
| 163 | + |
| 164 | + FlutterLayer layer = {}; |
| 165 | + layer.type = kFlutterLayerContentTypeBackingStore; |
| 166 | + layer.backing_store = &backing_store; |
| 167 | + layer.size = {320, 240}; |
| 168 | + const FlutterLayer* layer_ptr = &layer; |
| 169 | + |
| 170 | + EXPECT_TRUE(compositor.Present(view(), &layer_ptr, 1)); |
| 171 | + ExpectNoGLError(); |
| 172 | + |
| 173 | + ASSERT_TRUE(compositor.CollectBackingStore(&backing_store)); |
| 174 | +} |
| 175 | + |
| 176 | +} // namespace testing |
| 177 | +} // namespace flutter |
0 commit comments