Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
android_context_gl_unittests.cc
Go to the documentation of this file.
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 <memory>
13#include "fml/logging.h"
14#include "gmock/gmock.h"
15#include "gtest/gtest.h"
18#include "third_party/skia/include/gpu/ganesh/mock/GrMockTypes.h"
19
20namespace flutter {
21namespace testing {
22namespace android {
23namespace {
24
25TaskRunners MakeTaskRunners(const std::string& thread_label,
26 const ThreadHost& thread_host) {
28 fml::RefPtr<fml::TaskRunner> platform_runner =
30
31 return TaskRunners(thread_label, platform_runner,
32 thread_host.raster_thread->GetTaskRunner(),
33 thread_host.ui_thread->GetTaskRunner(),
34 thread_host.io_thread->GetTaskRunner());
35}
36} // namespace
37
39 public:
41
43
47
48 std::string DescribeGpuModel() const override { return ""; }
49
50 bool IsValid() const override { return true; }
51
52 const std::shared_ptr<const impeller::Capabilities>& GetCapabilities()
53 const override {
55 }
56
60
61 std::shared_ptr<impeller::Allocator> GetResourceAllocator() const override {
63 }
64
65 std::shared_ptr<impeller::ShaderLibrary> GetShaderLibrary() const override {
67 }
68
69 std::shared_ptr<impeller::SamplerLibrary> GetSamplerLibrary() const override {
71 }
72
73 std::shared_ptr<impeller::PipelineLibrary> GetPipelineLibrary()
74 const override {
76 }
77
78 std::shared_ptr<impeller::CommandBuffer> CreateCommandBuffer()
79 const override {
81 }
82
83 std::shared_ptr<impeller::CommandQueue> GetCommandQueue() const override {
85 }
86
87 // A stub returning false is allowed from implementations that are not
88 // planned to be used in benchmarking situations.
89 bool FinishQueue() override { return false; }
90
91 void Shutdown() override { did_shutdown = true; }
92
96
97 bool did_shutdown = false;
98};
99
101 public:
102 TestAndroidContext(const std::shared_ptr<impeller::Context>& impeller_context,
103 AndroidRenderingAPI rendering_api)
104 : AndroidContext(rendering_api), impeller_context_(impeller_context) {
105 SetImpellerContext(impeller_context);
106 }
107
108 private:
109 std::shared_ptr<impeller::Context> impeller_context_;
110};
111
112TEST(AndroidContextGl, Create) {
113 GrMockOptions main_context_options;
114 sk_sp<GrDirectContext> main_context =
115 GrDirectContext::MakeMock(&main_context_options);
116 auto environment = fml::MakeRefCounted<AndroidEnvironmentGL>();
117 std::string thread_label =
118 ::testing::UnitTest::GetInstance()->current_test_info()->name();
119
123 TaskRunners task_runners = MakeTaskRunners(thread_label, thread_host);
124 auto context =
125 std::make_unique<AndroidContextGLSkia>(environment, task_runners);
126 context->SetMainSkiaContext(main_context);
127 EXPECT_NE(context.get(), nullptr);
128 context.reset();
129 EXPECT_TRUE(main_context->abandoned());
130}
131
132TEST(AndroidContextGl, CreateImpeller) {
133 auto impeller_context = std::make_shared<TestImpellerContext>();
134 auto android_context = std::make_unique<TestAndroidContext>(
136 EXPECT_FALSE(impeller_context->did_shutdown);
137
138 android_context.reset();
139
140 EXPECT_TRUE(impeller_context->did_shutdown);
141}
142
143TEST(AndroidContextGl, CreateSingleThread) {
144 GrMockOptions main_context_options;
145 sk_sp<GrDirectContext> main_context =
146 GrDirectContext::MakeMock(&main_context_options);
147 auto environment = fml::MakeRefCounted<AndroidEnvironmentGL>();
148 std::string thread_label =
149 ::testing::UnitTest::GetInstance()->current_test_info()->name();
151 fml::RefPtr<fml::TaskRunner> platform_runner =
153 TaskRunners task_runners =
154 TaskRunners(thread_label, platform_runner, platform_runner,
155 platform_runner, platform_runner);
156 auto context =
157 std::make_unique<AndroidContextGLSkia>(environment, task_runners);
158 context->SetMainSkiaContext(main_context);
159 EXPECT_NE(context.get(), nullptr);
160 context.reset();
161 EXPECT_TRUE(main_context->abandoned());
162}
163
164TEST(AndroidSurfaceGL, CreateSnapshopSurfaceWhenOnscreenSurfaceIsNotNull) {
165 GrMockOptions main_context_options;
166 sk_sp<GrDirectContext> main_context =
167 GrDirectContext::MakeMock(&main_context_options);
168 auto environment = fml::MakeRefCounted<AndroidEnvironmentGL>();
169 std::string thread_label =
170 ::testing::UnitTest::GetInstance()->current_test_info()->name();
174 TaskRunners task_runners = MakeTaskRunners(thread_label, thread_host);
175 auto android_context =
176 std::make_shared<AndroidContextGLSkia>(environment, task_runners);
177 auto android_surface =
178 std::make_unique<AndroidSurfaceGLSkia>(android_context);
179 auto window = fml::MakeRefCounted<AndroidNativeWindow>(
180 nullptr, /*is_fake_window=*/true);
181 android_surface->SetNativeWindow(window, nullptr);
182 auto onscreen_surface = android_surface->GetOnscreenSurface();
183 EXPECT_NE(onscreen_surface, nullptr);
184 android_surface->CreateSnapshotSurface();
185 EXPECT_EQ(onscreen_surface, android_surface->GetOnscreenSurface());
186}
187
188TEST(AndroidSurfaceGL, CreateSnapshopSurfaceWhenOnscreenSurfaceIsNull) {
189 GrMockOptions main_context_options;
190 sk_sp<GrDirectContext> main_context =
191 GrDirectContext::MakeMock(&main_context_options);
192 auto environment = fml::MakeRefCounted<AndroidEnvironmentGL>();
193 std::string thread_label =
194 ::testing::UnitTest::GetInstance()->current_test_info()->name();
195
196 auto mask =
199
200 ThreadHost thread_host(host_config);
201 TaskRunners task_runners = MakeTaskRunners(thread_label, thread_host);
202 auto android_context =
203 std::make_shared<AndroidContextGLSkia>(environment, task_runners);
204 auto android_surface =
205 std::make_unique<AndroidSurfaceGLSkia>(android_context);
206 EXPECT_EQ(android_surface->GetOnscreenSurface(), nullptr);
207 android_surface->CreateSnapshotSurface();
208 EXPECT_NE(android_surface->GetOnscreenSurface(), nullptr);
209}
210
211TEST(AndroidContextGl, EnsureMakeCurrentChecksCurrentContextStatus) {
212 GrMockOptions main_context_options;
213 sk_sp<GrDirectContext> main_context =
214 GrDirectContext::MakeMock(&main_context_options);
215 auto environment = fml::MakeRefCounted<AndroidEnvironmentGL>();
216 std::string thread_label =
217 ::testing::UnitTest::GetInstance()->current_test_info()->name();
218
222 TaskRunners task_runners = MakeTaskRunners(thread_label, thread_host);
223 auto context =
224 std::make_unique<AndroidContextGLSkia>(environment, task_runners);
225
226 auto pbuffer_surface = context->CreatePbufferSurface();
227 auto status = pbuffer_surface->MakeCurrent();
229
230 // context already current, so status must reflect that.
231 status = pbuffer_surface->MakeCurrent();
233}
234} // namespace android
235} // namespace testing
236} // namespace flutter
Holds state that is shared across Android surfaces.
void SetImpellerContext(const std::shared_ptr< impeller::Context > &impeller_context)
TestAndroidContext(const std::shared_ptr< impeller::Context > &impeller_context, AndroidRenderingAPI rendering_api)
bool UpdateOffscreenLayerPixelFormat(impeller::PixelFormat format) override
std::shared_ptr< impeller::CommandBuffer > CreateCommandBuffer() const override
Create a new command buffer. Command buffers can be used to encode graphics, blit,...
bool FinishQueue() override
Wait until all previously submitted command buffers are processed and displayed by the GPU.
const std::shared_ptr< const impeller::Capabilities > & GetCapabilities() const override
Get the capabilities of Impeller context. All optionally supported feature of the platform,...
impeller::Context::BackendType GetBackendType() const override
Get the graphics backend of an Impeller context.
std::shared_ptr< impeller::SamplerLibrary > GetSamplerLibrary() const override
Returns the library of combined image samplers used in shaders.
void Shutdown() override
Force all pending asynchronous work to finish. This is achieved by deleting all owned concurrent mess...
std::shared_ptr< impeller::ShaderLibrary > GetShaderLibrary() const override
Returns the library of shaders used to specify the programmable stages of a pipeline.
impeller::RuntimeStageBackend GetRuntimeStageBackend() const override
Retrieve the runtime stage for this context type.
std::shared_ptr< impeller::PipelineLibrary > GetPipelineLibrary() const override
Returns the library of pipelines used by render or compute commands.
std::shared_ptr< impeller::CommandQueue > GetCommandQueue() const override
Return the graphics queue for submitting command buffers.
std::shared_ptr< impeller::Allocator > GetResourceAllocator() const override
Returns the allocator used to create textures and buffers on the device.
bool IsValid() const override
Determines if a context is valid. If the caller ever receives an invalid context, they must discard i...
static void EnsureInitializedForCurrentThread()
fml::RefPtr< fml::TaskRunner > GetTaskRunner() const
static FML_EMBEDDER_ONLY MessageLoop & GetCurrent()
To do anything rendering related with Impeller, you need a context.
Definition context.h:69
GLFWwindow * window
Definition main.cc:60
#define FML_UNREACHABLE()
Definition logging.h:128
@ kSuccessMadeCurrent
Success, the egl context for the surface made current.
@ kSuccessAlreadyCurrent
Success, the egl context for the surface was already current.
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition formats.h:99
The collection of all the threads used by the engine.
Definition thread_host.h:21
std::unique_ptr< fml::Thread > io_thread
Definition thread_host.h:86
std::unique_ptr< fml::Thread > raster_thread
Definition thread_host.h:85
std::unique_ptr< fml::Thread > ui_thread
Definition thread_host.h:84