[Android] HC++ wire up dart platform channel code and integration test. (#162751)

Use the PlatformViewController2 to register a platform view, allow the
dart side platform view logic to opt into this new platform view. Wires
up an integration test with android_engine_test.
This commit is contained in:
Jonah Williams
2025-02-06 19:09:24 -08:00
committed by GitHub
parent 7569fbfce5
commit f0396970e9
23 changed files with 477 additions and 18 deletions

View File

@@ -110,6 +110,7 @@ class MockPlatformViewAndroidJNI : public PlatformViewAndroidJNI {
int32_t viewHeight,
MutatorsStack mutators_stack),
(override));
MOCK_METHOD(void, onEndFrame2, (), (override));
MOCK_METHOD(std::unique_ptr<std::vector<std::string>>,
FlutterViewComputePlatformResolvedLocale,
(std::vector<std::string> supported_locales_data),

View File

@@ -3,10 +3,12 @@
// found in the LICENSE file.
#include "flutter/shell/platform/android/external_view_embedder/external_view_embedder_2.h"
#include "display_list/dl_color.h"
#include "flow/view_slicer.h"
#include "flutter/fml/synchronization/waitable_event.h"
#include "flutter/fml/trace_event.h"
#include "fml/make_copyable.h"
#include "fml/synchronization/count_down_latch.h"
namespace flutter {
@@ -90,8 +92,24 @@ void AndroidExternalViewEmbedder2::SubmitFlutterView(
view_rects //
);
// Create Overlay frame.
surface_pool_->TrimLayers();
// If there is no overlay Surface, initialize one on the platform thread. This
// will only be done once per application launch, as the singular overlay
// surface is never released.
surface_pool_->ResetLayers();
if (!surface_pool_->HasLayers()) {
std::shared_ptr<fml::CountDownLatch> latch =
std::make_shared<fml::CountDownLatch>(1u);
task_runners_.GetPlatformTaskRunner()->PostTask(
fml::MakeCopyable([&, latch]() {
surface_pool_->GetLayer(context, android_context_, jni_facade_,
surface_factory_);
latch->CountDown();
}));
latch->Wait();
}
// Create Overlay frame. If overlay surface creation failed,
// all this work must be skipped.
std::unique_ptr<SurfaceFrame> overlay_frame;
if (surface_pool_->HasLayers()) {
for (int64_t view_id : composition_order_) {
@@ -127,6 +145,7 @@ void AndroidExternalViewEmbedder2::SubmitFlutterView(
jni_facade = jni_facade_, device_pixel_ratio = device_pixel_ratio_,
slices = std::move(slices_)]() -> void {
jni_facade->swapTransaction();
for (int64_t view_id : composition_order) {
SkRect view_rect = GetViewRect(view_id, view_params);
const EmbeddedViewParams& params = view_params.at(view_id);
@@ -141,10 +160,7 @@ void AndroidExternalViewEmbedder2::SubmitFlutterView(
params.mutatorsStack() //
);
}
if (!surface_pool_->HasLayers()) {
surface_pool_->GetLayer(context, android_context_, jni_facade_,
surface_factory_);
}
jni_facade_->onEndFrame2();
}));
}

View File

@@ -122,6 +122,10 @@ void SurfacePool::SetFrameSize(SkISize frame_size) {
requested_frame_size_ = frame_size;
}
void SurfacePool::ResetLayers() {
available_layer_index_ = 0;
}
void SurfacePool::TrimLayers() {
std::lock_guard lock(mutex_);
layers_.erase(layers_.begin() + available_layer_index_, layers_.end());

View File

@@ -76,6 +76,9 @@ class SurfacePool {
// Returns true if the current pool has layers in use.
bool HasLayers();
// Reset the layer index but do not release any layers.
void ResetLayers();
void TrimLayers();
private:

View File

@@ -378,10 +378,7 @@ public class FlutterEngine implements ViewUtils.DisplayUpdater {
attachToJni();
}
// TODO(mattcarroll): FlutterRenderer is temporally coupled to attach(). Remove that coupling if
// possible.
this.renderer = new FlutterRenderer(flutterJNI);
this.platformViewsController = platformViewsController;
this.platformViewsController2 = platformViewsController2;

View File

@@ -341,6 +341,7 @@ import java.util.Set;
flutterEngine
.getPlatformViewsController()
.attach(activity, flutterEngine.getRenderer(), flutterEngine.getDartExecutor());
flutterEngine.getPlatformViewsController2().attach(activity, flutterEngine.getDartExecutor());
// Notify all ActivityAware plugins that they are now attached to a new Activity.
for (ActivityAware activityAware : activityAwarePlugins.values()) {
@@ -391,6 +392,7 @@ import java.util.Set;
private void detachFromActivityInternal() {
// Deactivate PlatformViewsController.
flutterEngine.getPlatformViewsController().detach();
flutterEngine.getPlatformViewsController2().detach();
exclusiveActivity = null;
activityPluginBinding = null;

View File

@@ -1301,6 +1301,16 @@ public class FlutterJNI {
platformViewsController2.applyTransactions();
}
@SuppressWarnings("unused")
@SuppressLint("NewApi")
@UiThread
public void endFrame2() {
if (platformViewsController2 == null) {
throw new RuntimeException("");
}
platformViewsController2.onEndFrame();
}
@SuppressWarnings("unused")
@SuppressLint("NewApi")
@UiThread

View File

@@ -560,6 +560,7 @@ public class PlatformViewsController2 implements PlatformViewsAccessibilityDeleg
surfaceControlBuilder.setFormat(PixelFormat.RGBA_8888);
surfaceControlBuilder.setName("Flutter Overlay Surface");
surfaceControlBuilder.setOpaque(false);
surfaceControlBuilder.setHidden(false);
final SurfaceControl surfaceControl = surfaceControlBuilder.build();
final SurfaceControl.Transaction tx =
flutterView.getRootSurfaceControl().buildReparentTransaction(surfaceControl);

View File

@@ -139,6 +139,8 @@ class JNIMock final : public PlatformViewAndroidJNI {
MutatorsStack mutators_stack),
(override));
MOCK_METHOD(void, onEndFrame2, (), (override));
MOCK_METHOD(std::unique_ptr<std::vector<std::string>>,
FlutterViewComputePlatformResolvedLocale,
(std::vector<std::string> supported_locales_data),

View File

@@ -228,6 +228,8 @@ class PlatformViewAndroidJNI {
virtual void destroyOverlaySurface2() = 0;
virtual void onEndFrame2() = 0;
virtual void onDisplayPlatformView2(int32_t view_id,
int32_t x,
int32_t y,

View File

@@ -158,6 +158,8 @@ static jmethodID g_destroy_overlay_surface2_method = nullptr;
static jmethodID g_on_display_platform_view2_method = nullptr;
static jmethodID g_on_end_frame2_method = nullptr;
// Mutators
static fml::jni::ScopedJavaGlobalRef<jclass>* g_mutators_stack_class = nullptr;
static jmethodID g_mutators_stack_init_method = nullptr;
@@ -1032,6 +1034,13 @@ bool RegisterApi(JNIEnv* env) {
FML_LOG(ERROR) << "Could not locate onDisplayPlatformView2 method";
return false;
}
g_on_end_frame2_method =
env->GetMethodID(g_flutter_jni_class->obj(), "endFrame2", "()V");
if (g_on_end_frame2_method == nullptr) {
FML_LOG(ERROR) << "Could not locate onEndFrame2 method";
return false;
}
//
fml::jni::ScopedJavaLocalRef<jclass> overlay_surface_class(
@@ -2151,4 +2160,17 @@ void PlatformViewAndroidJNIImpl::onDisplayPlatformView2(
FML_CHECK(fml::jni::CheckException(env));
}
void PlatformViewAndroidJNIImpl::onEndFrame2() {
JNIEnv* env = fml::jni::AttachCurrentThread();
auto java_object = java_object_.get(env);
if (java_object.is_null()) {
return;
}
env->CallVoidMethod(java_object.obj(), g_on_end_frame2_method);
FML_CHECK(fml::jni::CheckException(env));
}
} // namespace flutter

View File

@@ -124,6 +124,8 @@ class PlatformViewAndroidJNIImpl final : public PlatformViewAndroidJNI {
int32_t viewHeight,
MutatorsStack mutators_stack) override;
void onEndFrame2() override;
private:
// Reference to FlutterJNI object.
const fml::jni::JavaObjectWeakGlobalRef java_object_;

View File

@@ -21,6 +21,7 @@ import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.platform.PlatformViewsController;
import io.flutter.plugin.platform.PlatformViewsController2;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -37,6 +38,8 @@ public class FlutterEngineConnectionRegistryTest {
FlutterEngine flutterEngine = mock(FlutterEngine.class);
PlatformViewsController platformViewsController = mock(PlatformViewsController.class);
when(flutterEngine.getPlatformViewsController()).thenReturn(platformViewsController);
PlatformViewsController2 platformViewsController2 = mock(PlatformViewsController2.class);
when(flutterEngine.getPlatformViewsController2()).thenReturn(platformViewsController2);
FlutterLoader flutterLoader = mock(FlutterLoader.class);
@@ -75,6 +78,8 @@ public class FlutterEngineConnectionRegistryTest {
FlutterEngine flutterEngine = mock(FlutterEngine.class);
PlatformViewsController platformViewsController = mock(PlatformViewsController.class);
when(flutterEngine.getPlatformViewsController()).thenReturn(platformViewsController);
PlatformViewsController2 platformViewsController2 = mock(PlatformViewsController2.class);
when(flutterEngine.getPlatformViewsController2()).thenReturn(platformViewsController2);
FlutterLoader flutterLoader = mock(FlutterLoader.class);
@@ -124,6 +129,8 @@ public class FlutterEngineConnectionRegistryTest {
FlutterEngine flutterEngine = mock(FlutterEngine.class);
PlatformViewsController platformViewsController = mock(PlatformViewsController.class);
when(flutterEngine.getPlatformViewsController()).thenReturn(platformViewsController);
PlatformViewsController2 platformViewsController2 = mock(PlatformViewsController2.class);
when(flutterEngine.getPlatformViewsController2()).thenReturn(platformViewsController2);
FlutterLoader flutterLoader = mock(FlutterLoader.class);