|
| 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 "impeller/entity/contents/content_context.h" |
| 6 | + |
| 7 | +#include <memory> |
| 8 | +#include <string> |
| 9 | +#include <string_view> |
| 10 | +#include <unordered_set> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +#include "gmock/gmock.h" |
| 14 | +#include "gtest/gtest.h" |
| 15 | +#include "impeller/base/comparable.h" |
| 16 | +#include "impeller/base/promise.h" |
| 17 | +#include "impeller/renderer/capabilities.h" |
| 18 | +#include "impeller/renderer/pipeline.h" |
| 19 | +#include "impeller/renderer/pipeline_descriptor.h" |
| 20 | +#include "impeller/renderer/testing/mocks.h" |
| 21 | + |
| 22 | +namespace impeller { |
| 23 | +namespace testing { |
| 24 | + |
| 25 | +namespace { |
| 26 | + |
| 27 | +using ::testing::_; |
| 28 | +using ::testing::AtLeast; |
| 29 | +using ::testing::Contains; |
| 30 | +using ::testing::NiceMock; |
| 31 | +using ::testing::Return; |
| 32 | +using ::testing::ReturnRef; |
| 33 | + |
| 34 | +PipelineFuture<PipelineDescriptor> MakeRealizedPipelineFuture( |
| 35 | + PipelineDescriptor descriptor) { |
| 36 | + return { |
| 37 | + std::move(descriptor), |
| 38 | + RealizedFuture<std::shared_ptr<Pipeline<PipelineDescriptor>>>(nullptr)}; |
| 39 | +} |
| 40 | + |
| 41 | +/// A context with no GPU behind it. It resolves every shader and hands out a |
| 42 | +/// mock pipeline library that records each pipeline requested from it. |
| 43 | +class ContentContextTest : public ::testing::Test { |
| 44 | + public: |
| 45 | + void SetUp() override { |
| 46 | + capabilities_ = |
| 47 | + CapabilitiesBuilder() |
| 48 | + .SetDefaultColorFormat(PixelFormat::kR8G8B8A8UNormInt) |
| 49 | + .SetDefaultDepthStencilFormat(PixelFormat::kD24UnormS8Uint) |
| 50 | + .SetDefaultGlyphAtlasFormat(PixelFormat::kA8UNormInt) |
| 51 | + .Build(); |
| 52 | + ON_CALL(*context_, IsValid()).WillByDefault(Return(true)); |
| 53 | + ON_CALL(*context_, GetBackendType()) |
| 54 | + .WillByDefault(Return(Context::BackendType::kVulkan)); |
| 55 | + ON_CALL(*context_, GetCapabilities()) |
| 56 | + .WillByDefault(ReturnRef(capabilities_)); |
| 57 | + ON_CALL(*context_, GetShaderLibrary()) |
| 58 | + .WillByDefault(Return(shader_library_)); |
| 59 | + ON_CALL(*context_, GetPipelineLibrary()) |
| 60 | + .WillByDefault(Return(pipeline_library_)); |
| 61 | + // Resolve every entrypoint, as if every shader had been compiled in. |
| 62 | + ON_CALL(*shader_library_, GetFunction(_, _)) |
| 63 | + .WillByDefault([](std::string_view name, ShaderStage stage) { |
| 64 | + return std::make_shared<MockShaderFunction>(std::string(name), stage); |
| 65 | + }); |
| 66 | + ON_CALL(*pipeline_library_, GetPipeline(_, _, _)) |
| 67 | + .WillByDefault([this](const PipelineDescriptor& descriptor, |
| 68 | + bool /*async*/, bool /*threadsafe*/) { |
| 69 | + requested_.push_back(descriptor); |
| 70 | + return MakeRealizedPipelineFuture(descriptor); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + protected: |
| 75 | + std::shared_ptr<const Capabilities> capabilities_; |
| 76 | + std::shared_ptr<NiceMock<MockShaderLibrary>> shader_library_ = |
| 77 | + std::make_shared<NiceMock<MockShaderLibrary>>(); |
| 78 | + std::shared_ptr<NiceMock<MockPipelineLibrary>> pipeline_library_ = |
| 79 | + std::make_shared<NiceMock<MockPipelineLibrary>>(); |
| 80 | + std::shared_ptr<NiceMock<MockImpellerContext>> context_ = |
| 81 | + std::make_shared<NiceMock<MockImpellerContext>>(); |
| 82 | + /// Every descriptor requested from `pipeline_library_`, in order. |
| 83 | + std::vector<PipelineDescriptor> requested_; |
| 84 | +}; |
| 85 | + |
| 86 | +} // namespace |
| 87 | + |
| 88 | +TEST_F(ContentContextTest, WarmDefaultPipelinesRequestsDefaultPipelines) { |
| 89 | + ContentContext::WarmDefaultPipelines(*context_); |
| 90 | + |
| 91 | + std::vector<std::string> labels; |
| 92 | + labels.reserve(requested_.size()); |
| 93 | + for (const PipelineDescriptor& descriptor : requested_) { |
| 94 | + labels.emplace_back(descriptor.GetLabel()); |
| 95 | + } |
| 96 | + EXPECT_THAT(labels, Contains("SolidFill Pipeline")); |
| 97 | + EXPECT_THAT(labels, Contains("GlyphAtlas Pipeline")); |
| 98 | + EXPECT_THAT(labels, Contains("TextureFill Pipeline")); |
| 99 | +} |
| 100 | + |
| 101 | +TEST_F(ContentContextTest, WarmDefaultPipelinesRequestsPipelinesAsync) { |
| 102 | + // Compiling synchronously would stall the calling thread, and on OpenGL ES |
| 103 | + // would need a context to be current on it. |
| 104 | + EXPECT_CALL(*pipeline_library_, GetPipeline(_, /*async=*/false, _)).Times(0); |
| 105 | + EXPECT_CALL(*pipeline_library_, GetPipeline(_, /*async=*/true, _)) |
| 106 | + .Times(AtLeast(1)); |
| 107 | + |
| 108 | + ContentContext::WarmDefaultPipelines(*context_); |
| 109 | +} |
| 110 | + |
| 111 | +TEST_F(ContentContextTest, WarmDefaultPipelinesRequestsEachPipelineOnce) { |
| 112 | + ContentContext::WarmDefaultPipelines(*context_); |
| 113 | + |
| 114 | + ASSERT_FALSE(requested_.empty()); |
| 115 | + std::unordered_set<PipelineDescriptor, ComparableHash<PipelineDescriptor>, |
| 116 | + ComparableEqual<PipelineDescriptor>> |
| 117 | + unique(requested_.begin(), requested_.end()); |
| 118 | + EXPECT_EQ(unique.size(), requested_.size()); |
| 119 | +} |
| 120 | + |
| 121 | +TEST_F(ContentContextTest, WarmDefaultPipelinesDoesNoGpuWork) { |
| 122 | + // Embedders call this before there is a surface, when no rendering context |
| 123 | + // may be current. It must only request pipelines. |
| 124 | + EXPECT_CALL(*context_, GetResourceAllocator()).Times(0); |
| 125 | + EXPECT_CALL(*context_, CreateCommandBuffer()).Times(0); |
| 126 | + EXPECT_CALL(*context_, GetCommandQueue()).Times(0); |
| 127 | + |
| 128 | + ContentContext::WarmDefaultPipelines(*context_); |
| 129 | + |
| 130 | + EXPECT_FALSE(requested_.empty()); |
| 131 | +} |
| 132 | + |
| 133 | +TEST_F(ContentContextTest, WarmDefaultPipelinesIgnoresInvalidContext) { |
| 134 | + ON_CALL(*context_, IsValid()).WillByDefault(Return(false)); |
| 135 | + EXPECT_CALL(*pipeline_library_, GetPipeline(_, _, _)).Times(0); |
| 136 | + |
| 137 | + ContentContext::WarmDefaultPipelines(*context_); |
| 138 | +} |
| 139 | + |
| 140 | +} // namespace testing |
| 141 | +} // namespace impeller |
0 commit comments