Flutter Engine
 
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
19namespace flutter {
20namespace testing {
21namespace android {
22namespace {
23
24TaskRunners MakeTaskRunners(const std::string& thread_label,
25 const ThreadHost& thread_host) {
27 fml::RefPtr<fml::TaskRunner> platform_runner =
29
30 return TaskRunners(thread_label, platform_runner,
31 thread_host.raster_thread->GetTaskRunner(),
32 thread_host.ui_thread->GetTaskRunner(),
33 thread_host.io_thread->GetTaskRunner());
34}
35} // namespace
36
38 public:
40
42
46
47 std::string DescribeGpuModel() const override { return ""; }
48
49 bool IsValid() const override { return true; }
50
51 const std::shared_ptr<const impeller::Capabilities>& GetCapabilities()
52 const override {
54 }
55
59
60 std::shared_ptr<impeller::Allocator> GetResourceAllocator() const override {
62 }
63
64 std::shared_ptr<impeller::ShaderLibrary> GetShaderLibrary() const override {
66 }
67
68 std::shared_ptr<impeller::SamplerLibrary> GetSamplerLibrary() const override {
70 }
71
72 std::shared_ptr<impeller::PipelineLibrary> GetPipelineLibrary()
73 const override {
75 }
76
77 std::shared_ptr<impeller::CommandBuffer> CreateCommandBuffer()
78 const override {
80 }
81
82 std::shared_ptr<impeller::CommandQueue> GetCommandQueue() const override {
84 }
85
86 void Shutdown() override { did_shutdown = true; }
87
91
92 bool did_shutdown = false;
93};
94
96 public:
97 TestAndroidContext(const std::shared_ptr<impeller::Context>& impeller_context,
98 AndroidRenderingAPI rendering_api)
99 : AndroidContext(rendering_api), impeller_context_(impeller_context) {
100 SetImpellerContext(impeller_context);
101 }
102
103 private:
104 std::shared_ptr<impeller::Context> impeller_context_;
105};
106
107TEST(AndroidContextGl, Create) {
108 GrMockOptions main_context_options;
109 sk_sp<GrDirectContext> main_context =
110 GrDirectContext::MakeMock(&main_context_options);
111 auto environment = fml::MakeRefCounted<AndroidEnvironmentGL>();
112 std::string thread_label =
113 ::testing::UnitTest::GetInstance()->current_test_info()->name();
114
118 TaskRunners task_runners = MakeTaskRunners(thread_label, thread_host);
119 auto context =
120 std::make_unique<AndroidContextGLSkia>(environment, task_runners);
121 context->SetMainSkiaContext(main_context);
122 EXPECT_NE(context.get(), nullptr);
123 context.reset();
124 EXPECT_TRUE(main_context->abandoned());
125}
126
127TEST(AndroidContextGl, CreateImpeller) {
128 auto impeller_context = std::make_shared<TestImpellerContext>();
129 auto android_context = std::make_unique<TestAndroidContext>(
131 EXPECT_FALSE(impeller_context->did_shutdown);
132
133 android_context.reset();
134
135 EXPECT_TRUE(impeller_context->did_shutdown);
136}
137
138TEST(AndroidContextGl, CreateSingleThread) {
139 GrMockOptions main_context_options;
140 sk_sp<GrDirectContext> main_context =
141 GrDirectContext::MakeMock(&main_context_options);
142 auto environment = fml::MakeRefCounted<AndroidEnvironmentGL>();
143 std::string thread_label =
144 ::testing::UnitTest::GetInstance()->current_test_info()->name();
146 fml::RefPtr<fml::TaskRunner> platform_runner =
148 TaskRunners task_runners =
149 TaskRunners(thread_label, platform_runner, platform_runner,
150 platform_runner, platform_runner);
151 auto context =
152 std::make_unique<AndroidContextGLSkia>(environment, task_runners);
153 context->SetMainSkiaContext(main_context);
154 EXPECT_NE(context.get(), nullptr);
155 context.reset();
156 EXPECT_TRUE(main_context->abandoned());
157}
158
159TEST(AndroidSurfaceGL, CreateSnapshopSurfaceWhenOnscreenSurfaceIsNotNull) {
160 GrMockOptions main_context_options;
161 sk_sp<GrDirectContext> main_context =
162 GrDirectContext::MakeMock(&main_context_options);
163 auto environment = fml::MakeRefCounted<AndroidEnvironmentGL>();
164 std::string thread_label =
165 ::testing::UnitTest::GetInstance()->current_test_info()->name();
169 TaskRunners task_runners = MakeTaskRunners(thread_label, thread_host);
170 auto android_context =
171 std::make_shared<AndroidContextGLSkia>(environment, task_runners);
172 auto android_surface =
173 std::make_unique<AndroidSurfaceGLSkia>(android_context);
174 auto window = fml::MakeRefCounted<AndroidNativeWindow>(
175 nullptr, /*is_fake_window=*/true);
176 android_surface->SetNativeWindow(window, nullptr);
177 auto onscreen_surface = android_surface->GetOnscreenSurface();
178 EXPECT_NE(onscreen_surface, nullptr);
179 android_surface->CreateSnapshotSurface();
180 EXPECT_EQ(onscreen_surface, android_surface->GetOnscreenSurface());
181}
182
183TEST(AndroidSurfaceGL, CreateSnapshopSurfaceWhenOnscreenSurfaceIsNull) {
184 GrMockOptions main_context_options;
185 sk_sp<GrDirectContext> main_context =
186 GrDirectContext::MakeMock(&main_context_options);
187 auto environment = fml::MakeRefCounted<AndroidEnvironmentGL>();
188 std::string thread_label =
189 ::testing::UnitTest::GetInstance()->current_test_info()->name();
190
191 auto mask =
194
195 ThreadHost thread_host(host_config);
196 TaskRunners task_runners = MakeTaskRunners(thread_label, thread_host);
197 auto android_context =
198 std::make_shared<AndroidContextGLSkia>(environment, task_runners);
199 auto android_surface =
200 std::make_unique<AndroidSurfaceGLSkia>(android_context);
201 EXPECT_EQ(android_surface->GetOnscreenSurface(), nullptr);
202 android_surface->CreateSnapshotSurface();
203 EXPECT_NE(android_surface->GetOnscreenSurface(), nullptr);
204}
205
206TEST(AndroidContextGl, EnsureMakeCurrentChecksCurrentContextStatus) {
207 GrMockOptions main_context_options;
208 sk_sp<GrDirectContext> main_context =
209 GrDirectContext::MakeMock(&main_context_options);
210 auto environment = fml::MakeRefCounted<AndroidEnvironmentGL>();
211 std::string thread_label =
212 ::testing::UnitTest::GetInstance()->current_test_info()->name();
213
217 TaskRunners task_runners = MakeTaskRunners(thread_label, thread_host);
218 auto context =
219 std::make_unique<AndroidContextGLSkia>(environment, task_runners);
220
221 auto pbuffer_surface = context->CreatePbufferSurface();
222 auto status = pbuffer_surface->MakeCurrent();
224
225 // context already current, so status must reflect that.
226 status = pbuffer_surface->MakeCurrent();
228}
229} // namespace android
230} // namespace testing
231} // 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,...
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:65
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