Skip to content

Commit 17cb12d

Browse files
author
Jonah Williams
authored
[Android] wire up Java Transaction to AHB swapchain. (#162750)
* When the opt in surface control setting is enabled (and the backend is impeller vulkan) - then use the external view embedder 2. * ALways create the SurfaceControl.Transaction in PlatformViewController2 and manage it in Java. This was done for ease of implementation. Rather than switching between SurfaceControl.Transaction objects created in the native heap or created in java, we always go through java. This also means that adding platform views shouldn't change this flow. * We may need to separate this after peformance profiling. One more PR to go to wire up the new message channels and add the integration_test.
1 parent 1fecd2b commit 17cb12d

File tree

10 files changed

+43
-37
lines changed

10 files changed

+43
-37
lines changed

engine/src/flutter/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_impl_vk.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,24 @@ bool AHBFrameSynchronizerVK::WaitForFence(const vk::Device& device) {
6666
std::shared_ptr<AHBSwapchainImplVK> AHBSwapchainImplVK::Create(
6767
const std::weak_ptr<Context>& context,
6868
std::weak_ptr<android::SurfaceControl> surface_control,
69+
const CreateTransactionCB& cb,
6970
const ISize& size,
7071
bool enable_msaa,
7172
size_t swapchain_image_count) {
7273
auto impl = std::shared_ptr<AHBSwapchainImplVK>(
73-
new AHBSwapchainImplVK(context, std::move(surface_control), size,
74+
new AHBSwapchainImplVK(context, std::move(surface_control), cb, size,
7475
enable_msaa, swapchain_image_count));
7576
return impl->IsValid() ? impl : nullptr;
7677
}
7778

7879
AHBSwapchainImplVK::AHBSwapchainImplVK(
7980
const std::weak_ptr<Context>& context,
8081
std::weak_ptr<android::SurfaceControl> surface_control,
82+
const CreateTransactionCB& cb,
8183
const ISize& size,
8284
bool enable_msaa,
8385
size_t swapchain_image_count)
84-
: surface_control_(std::move(surface_control)) {
86+
: surface_control_(std::move(surface_control)), cb_(cb) {
8587
desc_ = android::HardwareBufferDescriptor::MakeForSwapchainImage(size);
8688
pool_ =
8789
std::make_shared<AHBTexturePoolVK>(context, desc_, swapchain_image_count);
@@ -201,7 +203,7 @@ bool AHBSwapchainImplVK::Present(
201203
return false;
202204
}
203205

204-
android::SurfaceTransaction transaction;
206+
android::SurfaceTransaction transaction = cb_();
205207
if (!transaction.SetContents(control.get(), //
206208
texture->GetBackingStore(), //
207209
present_ready->CreateFD() //

engine/src/flutter/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_impl_vk.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
#include "impeller/renderer/surface.h"
1717
#include "impeller/toolkit/android/hardware_buffer.h"
1818
#include "impeller/toolkit/android/surface_control.h"
19+
#include "impeller/toolkit/android/surface_transaction.h"
1920
#include "vulkan/vulkan_handles.hpp"
2021

2122
namespace impeller {
2223

24+
using CreateTransactionCB = std::function<android::SurfaceTransaction()>;
25+
2326
static constexpr const size_t kMaxPendingPresents = 2u;
2427

2528
struct AHBFrameSynchronizerVK {
@@ -68,6 +71,7 @@ class AHBSwapchainImplVK final
6871
static std::shared_ptr<AHBSwapchainImplVK> Create(
6972
const std::weak_ptr<Context>& context,
7073
std::weak_ptr<android::SurfaceControl> surface_control,
74+
const CreateTransactionCB& cb,
7175
const ISize& size,
7276
bool enable_msaa,
7377
size_t swapchain_image_count);
@@ -125,11 +129,13 @@ class AHBSwapchainImplVK final
125129

126130
std::vector<std::unique_ptr<AHBFrameSynchronizerVK>> frame_data_;
127131
size_t frame_index_ = 0;
132+
CreateTransactionCB cb_;
128133
bool is_valid_ = false;
129134

130135
explicit AHBSwapchainImplVK(
131136
const std::weak_ptr<Context>& context,
132137
std::weak_ptr<android::SurfaceControl> surface_control,
138+
const CreateTransactionCB& cb,
133139
const ISize& size,
134140
bool enable_msaa,
135141
size_t swapchain_image_count);

engine/src/flutter/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ bool AHBSwapchainVK::IsAvailableOnPlatform() {
1919

2020
AHBSwapchainVK::AHBSwapchainVK(const std::shared_ptr<Context>& context,
2121
ANativeWindow* window,
22+
const CreateTransactionCB& cb,
2223
const vk::UniqueSurfaceKHR& surface,
2324
const ISize& size,
2425
bool enable_msaa)
2526
: context_(context),
2627
surface_control_(
2728
std::make_shared<android::SurfaceControl>(window, "ImpellerSurface")),
28-
enable_msaa_(enable_msaa) {
29+
enable_msaa_(enable_msaa),
30+
cb_(cb) {
2931
const auto [caps_result, surface_caps] =
3032
ContextVK::Cast(*context).GetPhysicalDevice().getSurfaceCapabilitiesKHR(
3133
*surface);
@@ -80,6 +82,7 @@ void AHBSwapchainVK::UpdateSurfaceSize(const ISize& size) {
8082
TRACE_EVENT0("impeller", __FUNCTION__);
8183
auto impl = AHBSwapchainImplVK::Create(context_, //
8284
surface_control_, //
85+
cb_, //
8386
size, //
8487
enable_msaa_, //
8588
swapchain_image_count_ //

engine/src/flutter/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
#include "impeller/renderer/backend/vulkan/swapchain/swapchain_vk.h"
1010
#include "impeller/toolkit/android/native_window.h"
1111
#include "impeller/toolkit/android/surface_control.h"
12+
#include "impeller/toolkit/android/surface_transaction.h"
1213

1314
namespace impeller {
1415

16+
using CreateTransactionCB = std::function<android::SurfaceTransaction()>;
17+
1518
//------------------------------------------------------------------------------
1619
/// @brief The implementation of a swapchain that uses hardware buffers
1720
/// presented to a given surface control on Android.
@@ -57,10 +60,12 @@ class AHBSwapchainVK final : public SwapchainVK {
5760
std::shared_ptr<android::SurfaceControl> surface_control_;
5861
const bool enable_msaa_;
5962
size_t swapchain_image_count_ = 3u;
63+
CreateTransactionCB cb_;
6064
std::shared_ptr<AHBSwapchainImplVK> impl_;
6165

6266
explicit AHBSwapchainVK(const std::shared_ptr<Context>& context,
6367
ANativeWindow* window,
68+
const CreateTransactionCB& cb,
6469
const vk::UniqueSurfaceKHR& surface,
6570
const ISize& size,
6671
bool enable_msaa);

engine/src/flutter/impeller/renderer/backend/vulkan/swapchain/swapchain_vk.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ std::shared_ptr<SwapchainVK> SwapchainVK::Create(
3434
std::shared_ptr<SwapchainVK> SwapchainVK::Create(
3535
const std::shared_ptr<Context>& context,
3636
ANativeWindow* p_window,
37+
const CreateTransactionCB& cb,
3738
bool enable_msaa) {
3839
TRACE_EVENT0("impeller", "CreateAndroidSwapchain");
3940
if (!context) {
@@ -63,6 +64,7 @@ std::shared_ptr<SwapchainVK> SwapchainVK::Create(
6364
auto ahb_swapchain = std::shared_ptr<AHBSwapchainVK>(new AHBSwapchainVK(
6465
context, //
6566
window.GetHandle(), //
67+
cb, //
6668
surface, //
6769
window.GetSize(), //
6870
enable_msaa //

engine/src/flutter/impeller/renderer/backend/vulkan/swapchain/swapchain_vk.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@
1616

1717
#if FML_OS_ANDROID
1818
#include "impeller/toolkit/android/native_window.h"
19+
#include "impeller/toolkit/android/surface_transaction.h"
1920
#endif // FML_OS_ANDROID
2021

2122
namespace impeller {
2223

24+
#if FML_OS_ANDROID
25+
using CreateTransactionCB = std::function<android::SurfaceTransaction()>;
26+
#endif // FML_OS_ANDROID
27+
2328
//------------------------------------------------------------------------------
2429
/// @brief A swapchain that adapts to the underlying surface going out of
2530
/// date. If the caller cannot acquire the next drawable, it is due
@@ -38,6 +43,7 @@ class SwapchainVK {
3843
static std::shared_ptr<SwapchainVK> Create(
3944
const std::shared_ptr<Context>& context,
4045
ANativeWindow* window,
46+
const CreateTransactionCB& cb,
4147
bool enable_msaa = true);
4248
#endif // FML_OS_ANDROID
4349

engine/src/flutter/shell/platform/android/android_surface_vk_impeller.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "flutter/impeller/renderer/backend/vulkan/swapchain/swapchain_vk.h"
1515
#include "flutter/shell/gpu/gpu_surface_vulkan_impeller.h"
1616
#include "flutter/vulkan/vulkan_native_surface_android.h"
17+
#include "impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.h"
1718

1819
namespace flutter {
1920

@@ -93,10 +94,15 @@ bool AndroidSurfaceVKImpeller::SetNativeWindow(
9394
return false;
9495
}
9596

97+
impeller::CreateTransactionCB cb = [jni_facade]() {
98+
ASurfaceTransaction* tx = jni_facade->createTransaction();
99+
return impeller::android::SurfaceTransaction(tx);
100+
};
101+
96102
auto swapchain = impeller::SwapchainVK::Create(
97103
std::reinterpret_pointer_cast<impeller::Context>(
98104
surface_context_vk_->GetParent()),
99-
window->handle());
105+
window->handle(), cb);
100106

101107
if (surface_context_vk_->SetSwapchain(std::move(swapchain))) {
102108
native_window_ = std::move(window);

engine/src/flutter/shell/platform/android/external_view_embedder/external_view_embedder_2.cc

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include "flutter/shell/platform/android/external_view_embedder/external_view_embedder_2.h"
66
#include "flow/view_slicer.h"
7-
#include "flutter/common/constants.h"
87
#include "flutter/fml/synchronization/waitable_event.h"
98
#include "flutter/fml/trace_event.h"
109
#include "fml/make_copyable.h"
@@ -131,9 +130,7 @@ void AndroidExternalViewEmbedder2::SubmitFlutterView(
131130
for (int64_t view_id : composition_order) {
132131
SkRect view_rect = GetViewRect(view_id, view_params);
133132
const EmbeddedViewParams& params = view_params.at(view_id);
134-
// Display the platform view. If it's already displayed, then it's
135-
// just positioned and sized.
136-
jni_facade->FlutterViewOnDisplayPlatformView(
133+
jni_facade->onDisplayPlatformView2(
137134
view_id, //
138135
view_rect.x(), //
139136
view_rect.y(), //
@@ -148,30 +145,9 @@ void AndroidExternalViewEmbedder2::SubmitFlutterView(
148145
surface_pool_->GetLayer(context, android_context_, jni_facade_,
149146
surface_factory_);
150147
}
151-
jni_facade->FlutterViewEndFrame();
152148
}));
153149
}
154150

155-
// |ExternalViewEmbedder|
156-
std::unique_ptr<SurfaceFrame>
157-
AndroidExternalViewEmbedder2::CreateSurfaceIfNeeded(GrDirectContext* context,
158-
int64_t view_id,
159-
EmbedderViewSlice* slice,
160-
const SkRect& rect) {
161-
std::shared_ptr<OverlayLayer> layer = surface_pool_->GetLayer(
162-
context, android_context_, jni_facade_, surface_factory_);
163-
164-
std::unique_ptr<SurfaceFrame> frame =
165-
layer->surface->AcquireFrame(frame_size_);
166-
167-
DlCanvas* overlay_canvas = frame->Canvas();
168-
overlay_canvas->Clear(DlColor::kTransparent());
169-
// Offset the picture since its absolute position on the scene is determined
170-
// by the position of the overlay view.
171-
slice->render_into(overlay_canvas);
172-
return frame;
173-
}
174-
175151
// |ExternalViewEmbedder|
176152
PostPrerollResult AndroidExternalViewEmbedder2::PostPrerollAction(
177153
const fml::RefPtr<fml::RasterThreadMerger>& raster_thread_merger) {

engine/src/flutter/shell/platform/android/external_view_embedder/external_view_embedder_2.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,6 @@ class AndroidExternalViewEmbedder2 final : public ExternalViewEmbedder {
145145

146146
// Whether the layer tree in the current frame has platform layers.
147147
bool FrameHasPlatformLayers();
148-
149-
// Creates a Surface when needed or recycles an existing one.
150-
// Finally, draws the picture on the frame's canvas.
151-
std::unique_ptr<SurfaceFrame> CreateSurfaceIfNeeded(GrDirectContext* context,
152-
int64_t view_id,
153-
EmbedderViewSlice* slice,
154-
const SkRect& rect);
155148
};
156149

157150
} // namespace flutter

engine/src/flutter/shell/platform/android/platform_view_android.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <memory>
99
#include <utility>
1010

11+
#include "common/settings.h"
1112
#include "flutter/common/graphics/texture.h"
1213
#include "flutter/fml/synchronization/waitable_event.h"
1314
#include "flutter/shell/common/shell_io_manager.h"
@@ -132,6 +133,12 @@ PlatformViewAndroid::PlatformViewAndroid(
132133
delegate.OnPlatformViewGetSettings().enable_impeller //
133134
);
134135
android_surface_ = surface_factory_->CreateSurface();
136+
// TODO(jonahwilliams): we need to expose the runtime check for the
137+
// correct extensions and allowlist for this to work correctly.
138+
android_use_new_platform_view_ =
139+
android_context->RenderingApi() ==
140+
AndroidRenderingAPI::kImpellerVulkan &&
141+
delegate.OnPlatformViewGetSettings().enable_surface_control;
135142
FML_CHECK(android_surface_ && android_surface_->IsValid())
136143
<< "Could not create an OpenGL, Vulkan or Software surface to set up "
137144
"rendering.";

0 commit comments

Comments
 (0)