|
| 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 | +#define FML_USED_ON_EMBEDDER |
| 6 | + |
| 7 | +#include "flutter/shell/common/rasterizer.h" |
| 8 | + |
| 9 | +#include "flutter/shell/common/thread_host.h" |
| 10 | +#include "flutter/testing/testing.h" |
| 11 | +#include "gmock/gmock.h" |
| 12 | + |
| 13 | +using testing::_; |
| 14 | +using testing::ByMove; |
| 15 | +using testing::Return; |
| 16 | +using testing::ReturnRef; |
| 17 | + |
| 18 | +namespace flutter { |
| 19 | +namespace { |
| 20 | +class MockDelegate : public Rasterizer::Delegate { |
| 21 | + public: |
| 22 | + MOCK_METHOD1(OnFrameRasterized, void(const FrameTiming& frame_timing)); |
| 23 | + MOCK_METHOD0(GetFrameBudget, fml::Milliseconds()); |
| 24 | + MOCK_CONST_METHOD0(GetLatestFrameTargetTime, fml::TimePoint()); |
| 25 | + MOCK_CONST_METHOD0(GetTaskRunners, const TaskRunners&()); |
| 26 | + MOCK_CONST_METHOD0(GetIsGpuDisabledSyncSwitch, |
| 27 | + std::shared_ptr<fml::SyncSwitch>()); |
| 28 | +}; |
| 29 | + |
| 30 | +class MockSurface : public Surface { |
| 31 | + public: |
| 32 | + MOCK_METHOD0(IsValid, bool()); |
| 33 | + MOCK_METHOD1(AcquireFrame, |
| 34 | + std::unique_ptr<SurfaceFrame>(const SkISize& size)); |
| 35 | + MOCK_CONST_METHOD0(GetRootTransformation, SkMatrix()); |
| 36 | + MOCK_METHOD0(GetContext, GrDirectContext*()); |
| 37 | + MOCK_METHOD0(GetExternalViewEmbedder, ExternalViewEmbedder*()); |
| 38 | + MOCK_METHOD0(MakeRenderContextCurrent, std::unique_ptr<GLContextResult>()); |
| 39 | + MOCK_METHOD0(ClearRenderContext, bool()); |
| 40 | +}; |
| 41 | + |
| 42 | +class MockExternalViewEmbedder : public ExternalViewEmbedder { |
| 43 | + public: |
| 44 | + MOCK_METHOD0(GetRootCanvas, SkCanvas*()); |
| 45 | + MOCK_METHOD0(CancelFrame, void()); |
| 46 | + MOCK_METHOD4(BeginFrame, |
| 47 | + void(SkISize frame_size, |
| 48 | + GrDirectContext* context, |
| 49 | + double device_pixel_ratio, |
| 50 | + fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger)); |
| 51 | + MOCK_METHOD2(PrerollCompositeEmbeddedView, |
| 52 | + void(int view_id, std::unique_ptr<EmbeddedViewParams> params)); |
| 53 | + MOCK_METHOD1(PostPrerollAction, |
| 54 | + PostPrerollResult( |
| 55 | + fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger)); |
| 56 | + MOCK_METHOD0(GetCurrentCanvases, std::vector<SkCanvas*>()); |
| 57 | + MOCK_METHOD1(CompositeEmbeddedView, SkCanvas*(int view_id)); |
| 58 | + MOCK_METHOD2(SubmitFrame, |
| 59 | + void(GrDirectContext* context, |
| 60 | + std::unique_ptr<SurfaceFrame> frame)); |
| 61 | + MOCK_METHOD2(EndFrame, |
| 62 | + void(bool should_resubmit_frame, |
| 63 | + fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger)); |
| 64 | + MOCK_METHOD0(SupportsDynamicThreadMerging, bool()); |
| 65 | +}; |
| 66 | +} // namespace |
| 67 | + |
| 68 | +TEST(RasterizerTest, create) { |
| 69 | + MockDelegate delegate; |
| 70 | + auto rasterizer = std::make_unique<Rasterizer>(delegate); |
| 71 | + EXPECT_TRUE(rasterizer != nullptr); |
| 72 | +} |
| 73 | + |
| 74 | +TEST(RasterizerTest, drawEmptyPipeline) { |
| 75 | + std::string test_name = |
| 76 | + ::testing::UnitTest::GetInstance()->current_test_info()->name(); |
| 77 | + ThreadHost thread_host("io.flutter.test." + test_name + ".", |
| 78 | + ThreadHost::Type::Platform | ThreadHost::Type::GPU | |
| 79 | + ThreadHost::Type::IO | ThreadHost::Type::UI); |
| 80 | + TaskRunners task_runners("test", thread_host.platform_thread->GetTaskRunner(), |
| 81 | + thread_host.raster_thread->GetTaskRunner(), |
| 82 | + thread_host.ui_thread->GetTaskRunner(), |
| 83 | + thread_host.io_thread->GetTaskRunner()); |
| 84 | + MockDelegate delegate; |
| 85 | + ON_CALL(delegate, GetTaskRunners()).WillByDefault(ReturnRef(task_runners)); |
| 86 | + auto rasterizer = std::make_unique<Rasterizer>(delegate); |
| 87 | + auto surface = std::make_unique<MockSurface>(); |
| 88 | + rasterizer->Setup(std::move(surface)); |
| 89 | + fml::AutoResetWaitableEvent latch; |
| 90 | + thread_host.raster_thread->GetTaskRunner()->PostTask([&] { |
| 91 | + auto pipeline = fml::AdoptRef(new Pipeline<LayerTree>(/*depth=*/10)); |
| 92 | + rasterizer->Draw(pipeline); |
| 93 | + latch.Signal(); |
| 94 | + }); |
| 95 | + latch.Wait(); |
| 96 | +} |
| 97 | + |
| 98 | +TEST(RasterizerTest, |
| 99 | + drawWithExternalViewEmbedderExternalViewEmbedderSubmitFrameCalled) { |
| 100 | + std::string test_name = |
| 101 | + ::testing::UnitTest::GetInstance()->current_test_info()->name(); |
| 102 | + ThreadHost thread_host("io.flutter.test." + test_name + ".", |
| 103 | + ThreadHost::Type::Platform | ThreadHost::Type::GPU | |
| 104 | + ThreadHost::Type::IO | ThreadHost::Type::UI); |
| 105 | + TaskRunners task_runners("test", thread_host.platform_thread->GetTaskRunner(), |
| 106 | + thread_host.raster_thread->GetTaskRunner(), |
| 107 | + thread_host.ui_thread->GetTaskRunner(), |
| 108 | + thread_host.io_thread->GetTaskRunner()); |
| 109 | + MockDelegate delegate; |
| 110 | + EXPECT_CALL(delegate, GetTaskRunners()) |
| 111 | + .WillRepeatedly(ReturnRef(task_runners)); |
| 112 | + EXPECT_CALL(delegate, OnFrameRasterized(_)); |
| 113 | + auto rasterizer = std::make_unique<Rasterizer>(delegate); |
| 114 | + auto surface = std::make_unique<MockSurface>(); |
| 115 | + |
| 116 | + MockExternalViewEmbedder external_view_embedder; |
| 117 | + EXPECT_CALL(*surface, GetExternalViewEmbedder()) |
| 118 | + .WillRepeatedly(Return(&external_view_embedder)); |
| 119 | + |
| 120 | + auto surface_frame = std::make_unique<SurfaceFrame>( |
| 121 | + /*surface=*/nullptr, /*supports_readback=*/true, |
| 122 | + /*submit_callback=*/[](const SurfaceFrame&, SkCanvas*) { return true; }); |
| 123 | + EXPECT_CALL(*surface, AcquireFrame(SkISize())) |
| 124 | + .WillOnce(Return(ByMove(std::move(surface_frame)))); |
| 125 | + |
| 126 | + EXPECT_CALL(external_view_embedder, |
| 127 | + BeginFrame(/*frame_size=*/SkISize(), /*context=*/nullptr, |
| 128 | + /*device_pixel_ratio=*/2.0, |
| 129 | + /*raster_thread_merger=*/ |
| 130 | + fml::RefPtr<fml::RasterThreadMerger>(nullptr))) |
| 131 | + .Times(1); |
| 132 | + EXPECT_CALL(external_view_embedder, SubmitFrame).Times(1); |
| 133 | + EXPECT_CALL( |
| 134 | + external_view_embedder, |
| 135 | + EndFrame(/*should_resubmit_frame=*/false, |
| 136 | + /*raster_thread_merger=*/fml::RefPtr<fml::RasterThreadMerger>( |
| 137 | + nullptr))) |
| 138 | + .Times(1); |
| 139 | + |
| 140 | + rasterizer->Setup(std::move(surface)); |
| 141 | + fml::AutoResetWaitableEvent latch; |
| 142 | + thread_host.raster_thread->GetTaskRunner()->PostTask([&] { |
| 143 | + auto pipeline = fml::AdoptRef(new Pipeline<LayerTree>(/*depth=*/10)); |
| 144 | + auto layer_tree = std::make_unique<LayerTree>(/*frame_size=*/SkISize(), |
| 145 | + /*device_pixel_ratio=*/2.0f); |
| 146 | + bool result = pipeline->Produce().Complete(std::move(layer_tree)); |
| 147 | + EXPECT_TRUE(result); |
| 148 | + rasterizer->Draw(pipeline); |
| 149 | + latch.Signal(); |
| 150 | + }); |
| 151 | + latch.Wait(); |
| 152 | +} |
| 153 | + |
| 154 | +TEST( |
| 155 | + RasterizerTest, |
| 156 | + drawWithExternalViewEmbedderAndThreadMergerNotMergedExternalViewEmbedderSubmitFrameNotCalled) { |
| 157 | + std::string test_name = |
| 158 | + ::testing::UnitTest::GetInstance()->current_test_info()->name(); |
| 159 | + ThreadHost thread_host("io.flutter.test." + test_name + ".", |
| 160 | + ThreadHost::Type::Platform | ThreadHost::Type::GPU | |
| 161 | + ThreadHost::Type::IO | ThreadHost::Type::UI); |
| 162 | + TaskRunners task_runners("test", thread_host.platform_thread->GetTaskRunner(), |
| 163 | + thread_host.raster_thread->GetTaskRunner(), |
| 164 | + thread_host.ui_thread->GetTaskRunner(), |
| 165 | + thread_host.io_thread->GetTaskRunner()); |
| 166 | + MockDelegate delegate; |
| 167 | + EXPECT_CALL(delegate, GetTaskRunners()) |
| 168 | + .WillRepeatedly(ReturnRef(task_runners)); |
| 169 | + EXPECT_CALL(delegate, OnFrameRasterized(_)); |
| 170 | + auto rasterizer = std::make_unique<Rasterizer>(delegate); |
| 171 | + auto surface = std::make_unique<MockSurface>(); |
| 172 | + MockExternalViewEmbedder external_view_embedder; |
| 173 | + EXPECT_CALL(*surface, GetExternalViewEmbedder()) |
| 174 | + .WillRepeatedly(Return(&external_view_embedder)); |
| 175 | + EXPECT_CALL(external_view_embedder, SupportsDynamicThreadMerging) |
| 176 | + .WillRepeatedly(Return(true)); |
| 177 | + auto surface_frame = std::make_unique<SurfaceFrame>( |
| 178 | + /*surface=*/nullptr, /*supports_readback=*/true, |
| 179 | + /*submit_callback=*/[](const SurfaceFrame&, SkCanvas*) { return true; }); |
| 180 | + EXPECT_CALL(*surface, AcquireFrame(SkISize())) |
| 181 | + .WillOnce(Return(ByMove(std::move(surface_frame)))); |
| 182 | + |
| 183 | + EXPECT_CALL(external_view_embedder, |
| 184 | + BeginFrame(/*frame_size=*/SkISize(), /*context=*/nullptr, |
| 185 | + /*device_pixel_ratio=*/2.0, |
| 186 | + /*raster_thread_merger=*/_)) |
| 187 | + .Times(1); |
| 188 | + EXPECT_CALL(external_view_embedder, SubmitFrame).Times(0); |
| 189 | + EXPECT_CALL(external_view_embedder, EndFrame(/*should_resubmit_frame=*/false, |
| 190 | + /*raster_thread_merger=*/_)) |
| 191 | + .Times(1); |
| 192 | + |
| 193 | + rasterizer->Setup(std::move(surface)); |
| 194 | + fml::AutoResetWaitableEvent latch; |
| 195 | + thread_host.raster_thread->GetTaskRunner()->PostTask([&] { |
| 196 | + auto pipeline = fml::AdoptRef(new Pipeline<LayerTree>(/*depth=*/10)); |
| 197 | + auto layer_tree = std::make_unique<LayerTree>(/*frame_size=*/SkISize(), |
| 198 | + /*device_pixel_ratio=*/2.0f); |
| 199 | + bool result = pipeline->Produce().Complete(std::move(layer_tree)); |
| 200 | + EXPECT_TRUE(result); |
| 201 | + rasterizer->Draw(pipeline); |
| 202 | + latch.Signal(); |
| 203 | + }); |
| 204 | + latch.Wait(); |
| 205 | +} |
| 206 | + |
| 207 | +TEST( |
| 208 | + RasterizerTest, |
| 209 | + drawWithExternalViewEmbedderAndThreadsMergedExternalViewEmbedderSubmitFrameCalled) { |
| 210 | + std::string test_name = |
| 211 | + ::testing::UnitTest::GetInstance()->current_test_info()->name(); |
| 212 | + ThreadHost thread_host("io.flutter.test." + test_name + ".", |
| 213 | + ThreadHost::Type::Platform | ThreadHost::Type::GPU | |
| 214 | + ThreadHost::Type::IO | ThreadHost::Type::UI); |
| 215 | + fml::MessageLoop::EnsureInitializedForCurrentThread(); |
| 216 | + TaskRunners task_runners("test", |
| 217 | + fml::MessageLoop::GetCurrent().GetTaskRunner(), |
| 218 | + fml::MessageLoop::GetCurrent().GetTaskRunner(), |
| 219 | + thread_host.ui_thread->GetTaskRunner(), |
| 220 | + thread_host.io_thread->GetTaskRunner()); |
| 221 | + |
| 222 | + MockDelegate delegate; |
| 223 | + EXPECT_CALL(delegate, GetTaskRunners()) |
| 224 | + .WillRepeatedly(ReturnRef(task_runners)); |
| 225 | + EXPECT_CALL(delegate, OnFrameRasterized(_)); |
| 226 | + |
| 227 | + auto rasterizer = std::make_unique<Rasterizer>(delegate); |
| 228 | + auto surface = std::make_unique<MockSurface>(); |
| 229 | + |
| 230 | + MockExternalViewEmbedder external_view_embedder; |
| 231 | + EXPECT_CALL(*surface, GetExternalViewEmbedder()) |
| 232 | + .WillRepeatedly(Return(&external_view_embedder)); |
| 233 | + |
| 234 | + auto surface_frame = std::make_unique<SurfaceFrame>( |
| 235 | + /*surface=*/nullptr, /*supports_readback=*/true, |
| 236 | + /*submit_callback=*/[](const SurfaceFrame&, SkCanvas*) { return true; }); |
| 237 | + EXPECT_CALL(*surface, AcquireFrame(SkISize())) |
| 238 | + .WillOnce(Return(ByMove(std::move(surface_frame)))); |
| 239 | + EXPECT_CALL(external_view_embedder, SupportsDynamicThreadMerging) |
| 240 | + .WillRepeatedly(Return(true)); |
| 241 | + |
| 242 | + EXPECT_CALL(external_view_embedder, |
| 243 | + BeginFrame(/*frame_size=*/SkISize(), /*context=*/nullptr, |
| 244 | + /*device_pixel_ratio=*/2.0, |
| 245 | + /*raster_thread_merger=*/_)) |
| 246 | + .Times(1); |
| 247 | + EXPECT_CALL(external_view_embedder, SubmitFrame).Times(1); |
| 248 | + EXPECT_CALL(external_view_embedder, EndFrame(/*should_resubmit_frame=*/false, |
| 249 | + /*raster_thread_merger=*/_)) |
| 250 | + .Times(1); |
| 251 | + |
| 252 | + rasterizer->Setup(std::move(surface)); |
| 253 | + |
| 254 | + auto pipeline = fml::AdoptRef(new Pipeline<LayerTree>(/*depth=*/10)); |
| 255 | + auto layer_tree = std::make_unique<LayerTree>(/*frame_size=*/SkISize(), |
| 256 | + /*device_pixel_ratio=*/2.0f); |
| 257 | + bool result = pipeline->Produce().Complete(std::move(layer_tree)); |
| 258 | + EXPECT_TRUE(result); |
| 259 | + rasterizer->Draw(pipeline); |
| 260 | +} |
| 261 | +} // namespace flutter |
0 commit comments