|
| 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/shell/common/rasterizer.h" |
| 6 | + |
| 7 | +#include "flutter/shell/common/thread_host.h" |
| 8 | +#include "flutter/testing/testing.h" |
| 9 | +#include "gmock/gmock.h" |
| 10 | + |
| 11 | +using testing::_; |
| 12 | +using testing::Return; |
| 13 | +using testing::ReturnRef; |
| 14 | + |
| 15 | +namespace flutter { |
| 16 | +namespace { |
| 17 | +class MockDelegate : public Rasterizer::Delegate { |
| 18 | + public: |
| 19 | + MOCK_METHOD1(OnFrameRasterized, void(const FrameTiming& frame_timing)); |
| 20 | + MOCK_METHOD0(GetFrameBudget, fml::Milliseconds()); |
| 21 | + MOCK_CONST_METHOD0(GetLatestFrameTargetTime, fml::TimePoint()); |
| 22 | + MOCK_CONST_METHOD0(GetTaskRunners, const TaskRunners&()); |
| 23 | + MOCK_CONST_METHOD0(GetIsGpuDisabledSyncSwitch, |
| 24 | + std::shared_ptr<fml::SyncSwitch>()); |
| 25 | +}; |
| 26 | + |
| 27 | +class MockSurface : public Surface { |
| 28 | + public: |
| 29 | + MOCK_METHOD0(IsValid, bool()); |
| 30 | + MOCK_METHOD1(AcquireFrame, |
| 31 | + std::unique_ptr<SurfaceFrame>(const SkISize& size)); |
| 32 | + MOCK_CONST_METHOD0(GetRootTransformation, SkMatrix()); |
| 33 | + MOCK_METHOD0(GetContext, GrDirectContext*()); |
| 34 | + MOCK_METHOD0(GetExternalViewEmbedder, ExternalViewEmbedder*()); |
| 35 | + MOCK_METHOD0(MakeRenderContextCurrent, std::unique_ptr<GLContextResult>()); |
| 36 | + MOCK_METHOD0(ClearRenderContext, bool()); |
| 37 | +}; |
| 38 | + |
| 39 | +class MockExternalViewEmbedder : public ExternalViewEmbedder { |
| 40 | + public: |
| 41 | + MOCK_METHOD0(GetRootCanvas, SkCanvas*()); |
| 42 | + MOCK_METHOD0(CancelFrame, void()); |
| 43 | + MOCK_METHOD4(BeginFrame, |
| 44 | + void(SkISize frame_size, |
| 45 | + GrDirectContext* context, |
| 46 | + double device_pixel_ratio, |
| 47 | + fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger)); |
| 48 | + MOCK_METHOD2(PrerollCompositeEmbeddedView, |
| 49 | + void(int view_id, std::unique_ptr<EmbeddedViewParams> params)); |
| 50 | + MOCK_METHOD1(PostPrerollAction, |
| 51 | + PostPrerollResult( |
| 52 | + fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger)); |
| 53 | + MOCK_METHOD0(GetCurrentCanvases, std::vector<SkCanvas*>()); |
| 54 | + MOCK_METHOD1(CompositeEmbeddedView, SkCanvas*(int view_id)); |
| 55 | + MOCK_METHOD2(SubmitFrame, |
| 56 | + void(GrDirectContext* context, |
| 57 | + std::unique_ptr<SurfaceFrame> frame)); |
| 58 | + MOCK_METHOD2(EndFrame, |
| 59 | + void(bool should_resubmit_frame, |
| 60 | + fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger)); |
| 61 | + MOCK_METHOD0(SupportsDynamicThreadMerging, bool()); |
| 62 | +}; |
| 63 | +} // namespace |
| 64 | + |
| 65 | +TEST(RasterizerTest, create) { |
| 66 | + MockDelegate delegate; |
| 67 | + auto rasterizer = std::make_unique<Rasterizer>(delegate); |
| 68 | + EXPECT_TRUE(rasterizer != nullptr); |
| 69 | +} |
| 70 | + |
| 71 | +TEST(RasterizerTest, drawEmptyPipeline) { |
| 72 | + std::string test_name = |
| 73 | + ::testing::UnitTest::GetInstance()->current_test_info()->name(); |
| 74 | + ThreadHost thread_host("io.flutter.test." + test_name + ".", |
| 75 | + ThreadHost::Type::Platform | ThreadHost::Type::GPU | |
| 76 | + ThreadHost::Type::IO | ThreadHost::Type::UI); |
| 77 | + TaskRunners task_runners("test", thread_host.platform_thread->GetTaskRunner(), |
| 78 | + thread_host.raster_thread->GetTaskRunner(), |
| 79 | + thread_host.ui_thread->GetTaskRunner(), |
| 80 | + thread_host.io_thread->GetTaskRunner()); |
| 81 | + MockDelegate delegate; |
| 82 | + ON_CALL(delegate, GetTaskRunners()).WillByDefault(ReturnRef(task_runners)); |
| 83 | + auto rasterizer = std::make_unique<Rasterizer>(delegate); |
| 84 | + auto surface = std::make_unique<MockSurface>(); |
| 85 | + rasterizer->Setup(std::move(surface)); |
| 86 | + fml::AutoResetWaitableEvent latch; |
| 87 | + thread_host.raster_thread->GetTaskRunner()->PostTask([&] { |
| 88 | + auto pipeline = fml::AdoptRef(new Pipeline<LayerTree>(/*depth=*/10)); |
| 89 | + rasterizer->Draw(pipeline, nullptr); |
| 90 | + latch.Signal(); |
| 91 | + }); |
| 92 | + latch.Wait(); |
| 93 | +} |
| 94 | + |
| 95 | +TEST(RasterizerTest, drawWithExternalViewEmbedder) { |
| 96 | + std::string test_name = |
| 97 | + ::testing::UnitTest::GetInstance()->current_test_info()->name(); |
| 98 | + ThreadHost thread_host("io.flutter.test." + test_name + ".", |
| 99 | + ThreadHost::Type::Platform | ThreadHost::Type::GPU | |
| 100 | + ThreadHost::Type::IO | ThreadHost::Type::UI); |
| 101 | + TaskRunners task_runners("test", thread_host.platform_thread->GetTaskRunner(), |
| 102 | + thread_host.raster_thread->GetTaskRunner(), |
| 103 | + thread_host.ui_thread->GetTaskRunner(), |
| 104 | + thread_host.io_thread->GetTaskRunner()); |
| 105 | + MockDelegate delegate; |
| 106 | + EXPECT_CALL(delegate, GetTaskRunners()) |
| 107 | + .WillRepeatedly(ReturnRef(task_runners)); |
| 108 | + EXPECT_CALL(delegate, OnFrameRasterized(_)); |
| 109 | + auto rasterizer = std::make_unique<Rasterizer>(delegate); |
| 110 | + auto surface = std::make_unique<MockSurface>(); |
| 111 | + MockExternalViewEmbedder external_view_embedder; |
| 112 | + EXPECT_CALL(*surface, GetExternalViewEmbedder()) |
| 113 | + .WillRepeatedly(Return(&external_view_embedder)); |
| 114 | + EXPECT_CALL(external_view_embedder, |
| 115 | + BeginFrame(SkISize(), nullptr, 2.0, |
| 116 | + fml::RefPtr<fml::RasterThreadMerger>(nullptr))); |
| 117 | + EXPECT_CALL(external_view_embedder, |
| 118 | + EndFrame(false, fml::RefPtr<fml::RasterThreadMerger>(nullptr))); |
| 119 | + rasterizer->Setup(std::move(surface)); |
| 120 | + fml::AutoResetWaitableEvent latch; |
| 121 | + thread_host.raster_thread->GetTaskRunner()->PostTask([&] { |
| 122 | + auto pipeline = fml::AdoptRef(new Pipeline<LayerTree>(/*depth=*/10)); |
| 123 | + auto layer_tree = std::make_unique<LayerTree>(/*frame_size=*/SkISize(), |
| 124 | + /*device_pixel_ratio=*/2.0f); |
| 125 | + bool result = pipeline->Produce().Complete(std::move(layer_tree)); |
| 126 | + EXPECT_TRUE(result); |
| 127 | + std::function<bool(LayerTree&)> no_discard = [](LayerTree&) { |
| 128 | + return false; |
| 129 | + }; |
| 130 | + rasterizer->Draw(pipeline, no_discard); |
| 131 | + latch.Signal(); |
| 132 | + }); |
| 133 | + latch.Wait(); |
| 134 | +} |
| 135 | +} // namespace flutter |
0 commit comments