Skip to content

Commit

Permalink
Refactor Android PlatformView constructors (flutter#25632)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnfield committed Apr 22, 2021
1 parent 6fa0fb0 commit f058860
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 82 deletions.
23 changes: 8 additions & 15 deletions shell/platform/android/android_shell_holder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,14 @@ AndroidShellHolder::AndroidShellHolder(
Shell::CreateCallback<PlatformView> on_create_platform_view =
[is_background_view, &jni_facade, &weak_platform_view](Shell& shell) {
std::unique_ptr<PlatformViewAndroid> platform_view_android;
if (is_background_view) {
platform_view_android = std::make_unique<PlatformViewAndroid>(
shell, // delegate
shell.GetTaskRunners(), // task runners
jni_facade // JNI interop
);
} else {
platform_view_android = std::make_unique<PlatformViewAndroid>(
shell, // delegate
shell.GetTaskRunners(), // task runners
jni_facade, // JNI interop
shell.GetSettings()
.enable_software_rendering // use software rendering
);
}
platform_view_android = std::make_unique<PlatformViewAndroid>(
shell, // delegate
shell.GetTaskRunners(), // task runners
jni_facade, // JNI interop
shell.GetSettings()
.enable_software_rendering, // use software rendering
!is_background_view // create onscreen surface
);
weak_platform_view = platform_view_android->GetWeakPtr();
auto display = Display(jni_facade->GetDisplayRefreshRate());
shell.OnDisplayUpdates(DisplayUpdateType::kStartup, {display});
Expand Down
91 changes: 39 additions & 52 deletions shell/platform/android/platform_view_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,79 +48,66 @@ std::unique_ptr<AndroidSurface> AndroidSurfaceFactoryImpl::CreateSurface() {
jni_facade_);
#endif // SHELL_ENABLE_VULKAN
default:
FML_DCHECK(false);
return nullptr;
}
return nullptr;
}

PlatformViewAndroid::PlatformViewAndroid(
PlatformView::Delegate& delegate,
flutter::TaskRunners task_runners,
std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
bool use_software_rendering)
: PlatformView(delegate, std::move(task_runners)),
jni_facade_(jni_facade),
platform_view_android_delegate_(jni_facade) {
static std::shared_ptr<flutter::AndroidContext> CreateAndroidContext(
bool use_software_rendering,
bool create_onscreen_surface) {
if (!create_onscreen_surface) {
return nullptr;
}
if (use_software_rendering) {
android_context_ =
std::make_shared<AndroidContext>(AndroidRenderingAPI::kSoftware);
} else {
return std::make_shared<AndroidContext>(AndroidRenderingAPI::kSoftware);
}
#if SHELL_ENABLE_VULKAN
android_context_ =
std::make_shared<AndroidContext>(AndroidRenderingAPI::kVulkan);
return std::make_shared<AndroidContext>(AndroidRenderingAPI::kVulkan);
#else // SHELL_ENABLE_VULKAN
android_context_ = std::make_unique<AndroidContextGL>(
AndroidRenderingAPI::kOpenGLES,
fml::MakeRefCounted<AndroidEnvironmentGL>());
return std::make_unique<AndroidContextGL>(
AndroidRenderingAPI::kOpenGLES,
fml::MakeRefCounted<AndroidEnvironmentGL>());
#endif // SHELL_ENABLE_VULKAN
}
surface_factory_ = MakeSurfaceFactory(android_context_, *jni_facade_);
android_surface_ = MakeSurface(surface_factory_);
}

PlatformViewAndroid::PlatformViewAndroid(
PlatformView::Delegate& delegate,
flutter::TaskRunners task_runners,
const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade,
const std::shared_ptr<flutter::AndroidContext>& android_context)
: PlatformView(delegate, std::move(task_runners)),
jni_facade_(jni_facade),
android_context_(android_context),
platform_view_android_delegate_(jni_facade) {
surface_factory_ = MakeSurfaceFactory(android_context_, *jni_facade_);
android_surface_ = MakeSurface(surface_factory_);
}
std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
bool use_software_rendering,
bool create_onscreen_surface)
: PlatformViewAndroid(delegate,
std::move(task_runners),
std::move(jni_facade),
CreateAndroidContext(use_software_rendering,
create_onscreen_surface)) {}

PlatformViewAndroid::PlatformViewAndroid(
PlatformView::Delegate& delegate,
flutter::TaskRunners task_runners,
std::shared_ptr<PlatformViewAndroidJNI> jni_facade)
const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade,
const std::shared_ptr<flutter::AndroidContext>& android_context)
: PlatformView(delegate, std::move(task_runners)),
jni_facade_(jni_facade),
platform_view_android_delegate_(jni_facade) {}

PlatformViewAndroid::~PlatformViewAndroid() = default;

std::shared_ptr<AndroidSurfaceFactoryImpl>
PlatformViewAndroid::MakeSurfaceFactory(
const std::shared_ptr<AndroidContext>& android_context,
const PlatformViewAndroidJNI& jni_facade) {
FML_CHECK(android_context->IsValid())
<< "Could not create surface from invalid Android context.";

return std::make_shared<AndroidSurfaceFactoryImpl>(android_context,
jni_facade_);
android_context_(std::move(android_context)),
platform_view_android_delegate_(jni_facade) {
// TODO(dnfield): always create a pbuffer surface for background use to
// resolve https://github.com/flutter/flutter/issues/73675
if (android_context) {
FML_CHECK(android_context->IsValid())
<< "Could not create surface from invalid Android context.";
surface_factory_ = std::make_shared<AndroidSurfaceFactoryImpl>(
android_context, jni_facade_);
android_surface_ = surface_factory_->CreateSurface();

FML_CHECK(android_surface_ && android_surface_->IsValid())
<< "Could not create an OpenGL, Vulkan or Software surface to set up "
"rendering.";
}
}

std::unique_ptr<AndroidSurface> PlatformViewAndroid::MakeSurface(
const std::shared_ptr<AndroidSurfaceFactoryImpl>& surface_factory) {
auto surface = surface_factory->CreateSurface();

FML_CHECK(surface && surface->IsValid())
<< "Could not create an OpenGL, Vulkan or Software surface to set up "
"rendering.";
return surface;
}
PlatformViewAndroid::~PlatformViewAndroid() = default;

void PlatformViewAndroid::NotifyCreated(
fml::RefPtr<AndroidNativeWindow> native_window) {
Expand Down
17 changes: 2 additions & 15 deletions shell/platform/android/platform_view_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,11 @@ class PlatformViewAndroid final : public PlatformView {
public:
static bool Register(JNIEnv* env);

// Creates a PlatformViewAndroid with no rendering surface for use with
// background execution.
PlatformViewAndroid(PlatformView::Delegate& delegate,
flutter::TaskRunners task_runners,
std::shared_ptr<PlatformViewAndroidJNI> jni_facade);

// Creates a PlatformViewAndroid with a rendering surface.
PlatformViewAndroid(PlatformView::Delegate& delegate,
flutter::TaskRunners task_runners,
std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
bool use_software_rendering);
bool use_software_rendering,
bool create_onscreen_surface);

//----------------------------------------------------------------------------
/// @brief Creates a new PlatformViewAndroid but using an existing
Expand Down Expand Up @@ -170,13 +164,6 @@ class PlatformViewAndroid final : public PlatformView {
// |PlatformView|
void RequestDartDeferredLibrary(intptr_t loading_unit_id) override;

std::shared_ptr<AndroidSurfaceFactoryImpl> MakeSurfaceFactory(
const std::shared_ptr<AndroidContext>& android_context,
const PlatformViewAndroidJNI& jni_facade);

std::unique_ptr<AndroidSurface> MakeSurface(
const std::shared_ptr<AndroidSurfaceFactoryImpl>& surface_factory);

void InstallFirstFrameCallback();

void FireFirstFrameCallback();
Expand Down

0 comments on commit f058860

Please sign in to comment.