Skip to content

Commit fab1982

Browse files
Only creates 'onscreen_surface_' when it's not available in 'AndroidSurfaceGL::CreateSnapshotSurface' (#30590)
* Only creates 'onscreen_surface_' when it's not available in 'AndroidSurfaceGL::CreatePbufferSurface' * Rename the function and add some unit tests * Update android_surface_gl.h Co-authored-by: Dan Field <dfield@gmail.com>
1 parent bf7cd79 commit fab1982

File tree

6 files changed

+112
-6
lines changed

6 files changed

+112
-6
lines changed

shell/platform/android/android_context_gl_unittests.cc

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,62 @@
44
#include "flutter/shell/common/thread_host.h"
55
#include "flutter/shell/platform/android/android_context_gl.h"
66
#include "flutter/shell/platform/android/android_environment_gl.h"
7+
#include "flutter/shell/platform/android/android_surface_gl.h"
8+
#include "flutter/shell/platform/android/jni/platform_view_android_jni.h"
9+
#include "gmock/gmock.h"
710
#include "gtest/gtest.h"
811

912
namespace flutter {
1013
namespace testing {
1114
namespace android {
1215
namespace {
16+
class MockPlatformViewAndroidJNI : public PlatformViewAndroidJNI {
17+
public:
18+
MOCK_METHOD2(FlutterViewHandlePlatformMessage,
19+
void(std::unique_ptr<flutter::PlatformMessage> message,
20+
int responseId));
21+
MOCK_METHOD2(FlutterViewHandlePlatformMessageResponse,
22+
void(int responseId, std::unique_ptr<fml::Mapping> data));
23+
MOCK_METHOD3(FlutterViewUpdateSemantics,
24+
void(std::vector<uint8_t> buffer,
25+
std::vector<std::string> strings,
26+
std::vector<std::vector<uint8_t>> string_attribute_args));
27+
MOCK_METHOD2(FlutterViewUpdateCustomAccessibilityActions,
28+
void(std::vector<uint8_t> actions_buffer,
29+
std::vector<std::string> strings));
30+
MOCK_METHOD0(FlutterViewOnFirstFrame, void());
31+
MOCK_METHOD0(FlutterViewOnPreEngineRestart, void());
32+
MOCK_METHOD2(SurfaceTextureAttachToGLContext,
33+
void(JavaLocalRef surface_texture, int textureId));
34+
MOCK_METHOD1(SurfaceTextureUpdateTexImage,
35+
void(JavaLocalRef surface_texture));
36+
MOCK_METHOD2(SurfaceTextureGetTransformMatrix,
37+
void(JavaLocalRef surface_texture, SkMatrix& transform));
38+
MOCK_METHOD1(SurfaceTextureDetachFromGLContext,
39+
void(JavaLocalRef surface_texture));
40+
MOCK_METHOD8(FlutterViewOnDisplayPlatformView,
41+
void(int view_id,
42+
int x,
43+
int y,
44+
int width,
45+
int height,
46+
int viewWidth,
47+
int viewHeight,
48+
MutatorsStack mutators_stack));
49+
MOCK_METHOD5(FlutterViewDisplayOverlaySurface,
50+
void(int surface_id, int x, int y, int width, int height));
51+
MOCK_METHOD0(FlutterViewBeginFrame, void());
52+
MOCK_METHOD0(FlutterViewEndFrame, void());
53+
MOCK_METHOD0(FlutterViewCreateOverlaySurface,
54+
std::unique_ptr<PlatformViewAndroidJNI::OverlayMetadata>());
55+
MOCK_METHOD0(FlutterViewDestroyOverlaySurfaces, void());
56+
MOCK_METHOD1(FlutterViewComputePlatformResolvedLocale,
57+
std::unique_ptr<std::vector<std::string>>(
58+
std::vector<std::string> supported_locales_data));
59+
MOCK_METHOD0(GetDisplayRefreshRate, double());
60+
MOCK_METHOD1(RequestDartDeferredLibrary, bool(int loading_unit_id));
61+
};
62+
1363
TaskRunners MakeTaskRunners(const std::string& thread_label,
1464
const ThreadHost& thread_host) {
1565
fml::MessageLoop::EnsureInitializedForCurrentThread();
@@ -62,6 +112,52 @@ TEST(AndroidContextGl, CreateSingleThread) {
62112
context.reset();
63113
EXPECT_TRUE(main_context->abandoned());
64114
}
115+
116+
TEST(AndroidSurfaceGL, CreateSnapshopSurfaceWhenOnscreenSurfaceIsNotNull) {
117+
GrMockOptions main_context_options;
118+
sk_sp<GrDirectContext> main_context =
119+
GrDirectContext::MakeMock(&main_context_options);
120+
auto environment = fml::MakeRefCounted<AndroidEnvironmentGL>();
121+
std::string thread_label =
122+
::testing::UnitTest::GetInstance()->current_test_info()->name();
123+
ThreadHost thread_host(thread_label, ThreadHost::Type::UI |
124+
ThreadHost::Type::RASTER |
125+
ThreadHost::Type::IO);
126+
TaskRunners task_runners = MakeTaskRunners(thread_label, thread_host);
127+
auto android_context = std::make_shared<AndroidContextGL>(
128+
AndroidRenderingAPI::kOpenGLES, environment, task_runners);
129+
auto jni = std::make_shared<MockPlatformViewAndroidJNI>();
130+
auto android_surface =
131+
std::make_unique<AndroidSurfaceGL>(android_context, jni);
132+
auto window = fml::MakeRefCounted<AndroidNativeWindow>(
133+
nullptr, /*is_fake_window=*/true);
134+
android_surface->SetNativeWindow(window);
135+
auto onscreen_surface = android_surface->GetOnscreenSurface();
136+
EXPECT_NE(onscreen_surface, nullptr);
137+
android_surface->CreateSnapshotSurface();
138+
EXPECT_EQ(onscreen_surface, android_surface->GetOnscreenSurface());
139+
}
140+
141+
TEST(AndroidSurfaceGL, CreateSnapshopSurfaceWhenOnscreenSurfaceIsNull) {
142+
GrMockOptions main_context_options;
143+
sk_sp<GrDirectContext> main_context =
144+
GrDirectContext::MakeMock(&main_context_options);
145+
auto environment = fml::MakeRefCounted<AndroidEnvironmentGL>();
146+
std::string thread_label =
147+
::testing::UnitTest::GetInstance()->current_test_info()->name();
148+
ThreadHost thread_host(thread_label, ThreadHost::Type::UI |
149+
ThreadHost::Type::RASTER |
150+
ThreadHost::Type::IO);
151+
TaskRunners task_runners = MakeTaskRunners(thread_label, thread_host);
152+
auto android_context = std::make_shared<AndroidContextGL>(
153+
AndroidRenderingAPI::kOpenGLES, environment, task_runners);
154+
auto jni = std::make_shared<MockPlatformViewAndroidJNI>();
155+
auto android_surface =
156+
std::make_unique<AndroidSurfaceGL>(android_context, jni);
157+
EXPECT_EQ(android_surface->GetOnscreenSurface(), nullptr);
158+
android_surface->CreateSnapshotSurface();
159+
EXPECT_NE(android_surface->GetOnscreenSurface(), nullptr);
160+
}
65161
} // namespace android
66162
} // namespace testing
67163
} // namespace flutter

shell/platform/android/android_surface_gl.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,10 @@ AndroidContextGL* AndroidSurfaceGL::GLContextPtr() const {
175175
return reinterpret_cast<AndroidContextGL*>(android_context_.get());
176176
}
177177

178-
std::unique_ptr<Surface> AndroidSurfaceGL::CreatePbufferSurface() {
179-
onscreen_surface_ = GLContextPtr()->CreatePbufferSurface();
178+
std::unique_ptr<Surface> AndroidSurfaceGL::CreateSnapshotSurface() {
179+
if (!onscreen_surface_ || !onscreen_surface_->IsValid()) {
180+
onscreen_surface_ = GLContextPtr()->CreatePbufferSurface();
181+
}
180182
sk_sp<GrDirectContext> main_skia_context =
181183
GLContextPtr()->GetMainSkiaContext();
182184
if (!main_skia_context) {

shell/platform/android/android_surface_gl.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class AndroidSurfaceGL final : public GPUSurfaceGLDelegate,
4949
bool SetNativeWindow(fml::RefPtr<AndroidNativeWindow> window) override;
5050

5151
// |AndroidSurface|
52-
virtual std::unique_ptr<Surface> CreatePbufferSurface() override;
52+
virtual std::unique_ptr<Surface> CreateSnapshotSurface() override;
5353

5454
// |GPUSurfaceGLDelegate|
5555
std::unique_ptr<GLContextResult> GLContextMakeCurrent() override;
@@ -66,6 +66,14 @@ class AndroidSurfaceGL final : public GPUSurfaceGLDelegate,
6666
// |GPUSurfaceGLDelegate|
6767
sk_sp<const GrGLInterface> GetGLInterface() const override;
6868

69+
// Obtain a raw pointer to the on-screen AndroidEGLSurface.
70+
//
71+
// This method is intended for use in tests. Callers must not
72+
// delete the returned pointer.
73+
AndroidEGLSurface* GetOnscreenSurface() const {
74+
return onscreen_surface_.get();
75+
}
76+
6977
private:
7078
fml::RefPtr<AndroidNativeWindow> native_window_;
7179
std::unique_ptr<AndroidEGLSurface> onscreen_surface_;

shell/platform/android/surface/android_surface.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ AndroidSurface::AndroidSurface(
1515

1616
AndroidSurface::~AndroidSurface() = default;
1717

18-
std::unique_ptr<Surface> AndroidSurface::CreatePbufferSurface() {
18+
std::unique_ptr<Surface> AndroidSurface::CreateSnapshotSurface() {
1919
return nullptr;
2020
}
2121

shell/platform/android/surface/android_surface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class AndroidSurface {
3737

3838
virtual bool SetNativeWindow(fml::RefPtr<AndroidNativeWindow> window) = 0;
3939

40-
virtual std::unique_ptr<Surface> CreatePbufferSurface();
40+
virtual std::unique_ptr<Surface> CreateSnapshotSurface();
4141

4242
protected:
4343
explicit AndroidSurface(

shell/platform/android/surface/snapshot_surface_producer.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ AndroidSnapshotSurfaceProducer::AndroidSnapshotSurfaceProducer(
1212

1313
std::unique_ptr<Surface>
1414
AndroidSnapshotSurfaceProducer::CreateSnapshotSurface() {
15-
return android_surface_.CreatePbufferSurface();
15+
return android_surface_.CreateSnapshotSurface();
1616
}
1717

1818
} // namespace flutter

0 commit comments

Comments
 (0)