6#include "flutter/shell/platform/android/external_view_embedder/surface_pool.h"
8#include "flutter/fml/make_copyable.h"
9#include "flutter/shell/platform/android/jni/jni_mock.h"
10#include "flutter/shell/platform/android/surface/android_surface_mock.h"
11#include "gmock/gmock.h"
12#include "gtest/gtest.h"
18using ::testing::ByMove;
19using ::testing::Return;
21class TestAndroidSurfaceFactory :
public AndroidSurfaceFactory {
24 std::function<std::unique_ptr<AndroidSurface>(
void)>;
26 surface_producer_ = surface_producer;
32 return surface_producer_();
40 auto pool = std::make_unique<SurfacePool>();
43 auto android_context =
46 auto jni_mock = std::make_shared<JNIMock>();
47 auto window = fml::MakeRefCounted<AndroidNativeWindow>(
nullptr);
48 EXPECT_CALL(*jni_mock, FlutterViewCreateOverlaySurface())
50 ByMove(std::make_unique<PlatformViewAndroidJNI::OverlayMetadata>(
53 auto surface_factory =
54 std::make_shared<TestAndroidSurfaceFactory>([gr_context,
window]() {
55 auto android_surface_mock = std::make_unique<AndroidSurfaceMock>();
56 EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get()));
57 EXPECT_CALL(*android_surface_mock, SetNativeWindow(
window));
58 EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(
true));
59 return android_surface_mock;
61 auto layer =
pool->GetLayer(gr_context.get(), *android_context, jni_mock,
64 ASSERT_TRUE(
pool->HasLayers());
65 ASSERT_NE(
nullptr, layer);
66 ASSERT_EQ(
reinterpret_cast<intptr_t
>(gr_context.get()),
67 layer->gr_context_key);
71 auto pool = std::make_unique<SurfacePool>();
74 auto android_context =
77 auto jni_mock = std::make_shared<JNIMock>();
78 auto window = fml::MakeRefCounted<AndroidNativeWindow>(
nullptr);
79 EXPECT_CALL(*jni_mock, FlutterViewCreateOverlaySurface())
81 ByMove(std::make_unique<PlatformViewAndroidJNI::OverlayMetadata>(
84 auto surface_factory =
85 std::make_shared<TestAndroidSurfaceFactory>([gr_context,
window]() {
86 auto android_surface_mock = std::make_unique<AndroidSurfaceMock>();
87 EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get()));
88 EXPECT_CALL(*android_surface_mock, SetNativeWindow(
window));
89 EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(
true));
90 return android_surface_mock;
92 auto layer =
pool->GetLayer(gr_context.get(), *android_context, jni_mock,
94 ASSERT_EQ(0
UL,
pool->GetUnusedLayers().size());
96 pool->RecycleLayers();
98 ASSERT_TRUE(
pool->HasLayers());
99 ASSERT_EQ(1UL,
pool->GetUnusedLayers().size());
100 ASSERT_EQ(layer,
pool->GetUnusedLayers()[0]);
104 auto pool = std::make_unique<SurfacePool>();
107 auto jni_mock = std::make_shared<JNIMock>();
108 auto window = fml::MakeRefCounted<AndroidNativeWindow>(
nullptr);
109 EXPECT_CALL(*jni_mock, FlutterViewCreateOverlaySurface())
111 ByMove(std::make_unique<PlatformViewAndroidJNI::OverlayMetadata>(
114 auto android_context =
118 auto surface_factory = std::make_shared<TestAndroidSurfaceFactory>(
119 [gr_context_1, gr_context_2,
window]() {
120 auto android_surface_mock = std::make_unique<AndroidSurfaceMock>();
122 EXPECT_CALL(*android_surface_mock,
123 CreateGPUSurface(gr_context_1.get()));
124 EXPECT_CALL(*android_surface_mock,
125 CreateGPUSurface(gr_context_2.get()));
127 EXPECT_CALL(*android_surface_mock, SetNativeWindow(
window));
128 EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(
true));
129 return android_surface_mock;
131 auto layer_1 =
pool->GetLayer(gr_context_1.get(), *android_context, jni_mock,
134 pool->RecycleLayers();
136 auto layer_2 =
pool->GetLayer(gr_context_2.get(), *android_context, jni_mock,
139 ASSERT_TRUE(
pool->HasLayers());
140 ASSERT_NE(
nullptr, layer_1);
141 ASSERT_EQ(layer_1, layer_2);
142 ASSERT_EQ(
reinterpret_cast<intptr_t
>(gr_context_2.get()),
143 layer_1->gr_context_key);
144 ASSERT_EQ(
reinterpret_cast<intptr_t
>(gr_context_2.get()),
145 layer_2->gr_context_key);
149 auto pool = std::make_unique<SurfacePool>();
152 auto android_context =
155 auto jni_mock = std::make_shared<JNIMock>();
156 auto window = fml::MakeRefCounted<AndroidNativeWindow>(
nullptr);
157 EXPECT_CALL(*jni_mock, FlutterViewCreateOverlaySurface())
160 ByMove(std::make_unique<PlatformViewAndroidJNI::OverlayMetadata>(
163 ByMove(std::make_unique<PlatformViewAndroidJNI::OverlayMetadata>(
166 auto surface_factory =
167 std::make_shared<TestAndroidSurfaceFactory>([gr_context,
window]() {
168 auto android_surface_mock = std::make_unique<AndroidSurfaceMock>();
169 EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get()));
170 EXPECT_CALL(*android_surface_mock, SetNativeWindow(
window));
171 EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(
true));
172 return android_surface_mock;
174 auto layer_1 =
pool->GetLayer(gr_context.get(), *android_context, jni_mock,
176 auto layer_2 =
pool->GetLayer(gr_context.get(), *android_context, jni_mock,
179 ASSERT_TRUE(
pool->HasLayers());
180 ASSERT_NE(
nullptr, layer_1);
181 ASSERT_NE(
nullptr, layer_2);
182 ASSERT_NE(layer_1, layer_2);
183 ASSERT_EQ(0, layer_1->id);
184 ASSERT_EQ(1, layer_2->id);
188 auto pool = std::make_unique<SurfacePool>();
189 auto jni_mock = std::make_shared<JNIMock>();
191 EXPECT_CALL(*jni_mock, FlutterViewDestroyOverlaySurfaces()).Times(0);
192 pool->DestroyLayers(jni_mock);
195 auto android_context =
198 auto window = fml::MakeRefCounted<AndroidNativeWindow>(
nullptr);
199 EXPECT_CALL(*jni_mock, FlutterViewCreateOverlaySurface())
202 ByMove(std::make_unique<PlatformViewAndroidJNI::OverlayMetadata>(
205 auto surface_factory =
206 std::make_shared<TestAndroidSurfaceFactory>([gr_context,
window]() {
207 auto android_surface_mock = std::make_unique<AndroidSurfaceMock>();
208 EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get()));
209 EXPECT_CALL(*android_surface_mock, SetNativeWindow(
window));
210 EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(
true));
211 return android_surface_mock;
213 pool->GetLayer(gr_context.get(), *android_context, jni_mock, surface_factory);
215 EXPECT_CALL(*jni_mock, FlutterViewDestroyOverlaySurfaces());
217 ASSERT_TRUE(
pool->HasLayers());
218 pool->DestroyLayers(jni_mock);
220 ASSERT_FALSE(
pool->HasLayers());
221 ASSERT_TRUE(
pool->GetUnusedLayers().empty());
225 auto pool = std::make_unique<SurfacePool>();
226 auto jni_mock = std::make_shared<JNIMock>();
229 auto android_context =
232 auto window = fml::MakeRefCounted<AndroidNativeWindow>(
nullptr);
234 auto surface_factory =
235 std::make_shared<TestAndroidSurfaceFactory>([gr_context,
window]() {
236 auto android_surface_mock = std::make_unique<AndroidSurfaceMock>();
237 EXPECT_CALL(*android_surface_mock, CreateGPUSurface(gr_context.get()));
238 EXPECT_CALL(*android_surface_mock, SetNativeWindow(
window));
239 EXPECT_CALL(*android_surface_mock, IsValid()).WillOnce(Return(
true));
240 return android_surface_mock;
243 EXPECT_CALL(*jni_mock, FlutterViewDestroyOverlaySurfaces()).Times(0);
244 EXPECT_CALL(*jni_mock, FlutterViewCreateOverlaySurface())
247 ByMove(std::make_unique<PlatformViewAndroidJNI::OverlayMetadata>(
250 ASSERT_FALSE(
pool->HasLayers());
252 pool->GetLayer(gr_context.get(), *android_context, jni_mock, surface_factory);
254 ASSERT_TRUE(
pool->HasLayers());
257 EXPECT_CALL(*jni_mock, FlutterViewDestroyOverlaySurfaces()).Times(1);
258 EXPECT_CALL(*jni_mock, FlutterViewCreateOverlaySurface())
261 ByMove(std::make_unique<PlatformViewAndroidJNI::OverlayMetadata>(
263 pool->GetLayer(gr_context.get(), *android_context, jni_mock, surface_factory);
265 ASSERT_TRUE(
pool->GetUnusedLayers().empty());
266 ASSERT_TRUE(
pool->HasLayers());
static sk_sp< GrDirectContext > MakeMock(const GrMockOptions *, const GrContextOptions &)
TestAndroidSurfaceFactory(TestSurfaceProducer &&surface_producer)
std::unique_ptr< AndroidSurface > CreateSurface() override
~TestAndroidSurfaceFactory() override=default
std::function< std::unique_ptr< AndroidSurface >(void)> TestSurfaceProducer
TEST(DisplayListComplexity, EmptyDisplayList)
static constexpr SkISize Make(int32_t w, int32_t h)