Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
02556ae
++
Jul 11, 2024
b3f6670
move all state updates to end of compositing.
Jul 11, 2024
b31aae6
working but unsynchronized.
Jul 11, 2024
75f931a
hey it almost works.
Jul 12, 2024
4525bd8
disable partial repaint.
Jul 12, 2024
6ce3aa0
cleanups.
Jul 12, 2024
be03cf8
set presentsWithTransaction.
Jul 12, 2024
6770376
sanity refactor.
Jul 12, 2024
912cd40
linting.
Jul 12, 2024
1f65cc3
skip post message if there are no platform views.
Jul 12, 2024
53adc81
linting.
Jul 12, 2024
870b950
dispose views in callback.
Jul 15, 2024
f8267bd
++
Jul 15, 2024
80565d5
Merge branch 'main' of github.com:flutter/engine into three_phase_render
Jul 16, 2024
2b9a825
tear down.
Jul 16, 2024
0a7dc5a
reserve.
Jul 17, 2024
2745e88
++
Jul 17, 2024
6002aa0
++
Jul 17, 2024
e7a832f
++
Jul 17, 2024
f099d74
come on big money no whammies.
Jul 17, 2024
e2e6a2a
okay one more.
Jul 17, 2024
44d5b5b
debug printf
Jul 17, 2024
b53063a
++
Jul 19, 2024
9a84539
make test work.
Jul 23, 2024
65a5210
++
Jul 23, 2024
735ce22
testing fixes.
Jul 24, 2024
a4f523b
Merge branch 'main' of github.com:flutter/engine into three_phase_render
Jul 24, 2024
b6c3345
merge in view slicer.
Jul 24, 2024
a4b771e
Merge branch 'main' of github.com:flutter/engine into three_phase_render
Jul 24, 2024
cd8e1de
++
Jul 25, 2024
6603d18
minor cleanups.
Jul 25, 2024
27c0daa
formatting.
Jul 25, 2024
d794211
add back clear
Jul 26, 2024
2f4314b
++
Jul 26, 2024
25a4e06
++
Jul 26, 2024
ea2e66d
merge threads on SIM.
Jul 26, 2024
d220885
++
Jul 26, 2024
9b534cd
Update shell/platform/darwin/ios/ios_external_view_embedder.mm
Jul 26, 2024
970e13a
++
Jul 26, 2024
63ebe66
Merge branch 'three_phase_render' of github.com:jonahwilliams/engine …
Jul 26, 2024
ff4802a
Merge branch 'main' of github.com:flutter/engine into three_phase_render
Jul 26, 2024
e3fc731
does it blend?
Jul 26, 2024
c0fab3b
++
Jul 26, 2024
d73671e
test fixes and clang tidy.
Jul 27, 2024
7285907
simplify and clean up thread access for UIViews.
Jul 27, 2024
bcd8519
reset before clearning composition order.
Jul 27, 2024
b25a742
Merge branch 'main' of github.com:flutter/engine into three_phase_render
Jul 30, 2024
5f6a8d4
switch to SurfaceFrame API.
Jul 30, 2024
be7853d
++
Jul 30, 2024
b19b4d8
Remove extra include.
Jul 30, 2024
ab08739
clang tidy
Jul 30, 2024
6e6e5a2
bracken feedback
Jul 30, 2024
355ab30
more bracken review.
Aug 1, 2024
8cd1c74
++
Aug 1, 2024
30e7b97
review comments and remove dead code.
Aug 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
minor cleanups.
  • Loading branch information
jonahwilliams committed Jul 25, 2024
commit 6603d182e5e548e841279f743eaa888f251befc8
167 changes: 97 additions & 70 deletions shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm
Original file line number Diff line number Diff line change
Expand Up @@ -666,31 +666,7 @@ static bool ClipRRectContainsPlatformViewBoundingRect(const SkRRect& clip_rrect,
// thread, at least until we've refactored iOS surface creation to use IOSurfaces
// instead of CALayers.
if (required_overlay_layers > layer_pool_->size()) {
auto missing_layer_count = required_overlay_layers - layer_pool_->size();
TRACE_EVENT0("flutter", "FlutterPlatformViewsController::CreateMissingLayers");
// Workaround for FLutterPlatformViewsTest
if ([[NSThread currentThread] isMainThread]) {
// Create Missing Layers
for (auto i = 0u; i < missing_layer_count; i++) {
CreateLayer(gr_context, //
ios_context, //
((FlutterView*)flutter_view_.get()).pixelFormat //
);
}
} else {
auto latch = std::make_shared<fml::CountDownLatch>(1u);
platform_task_runner_->PostTask([&]() {
// Create Missing Layers
for (auto i = 0u; i < missing_layer_count; i++) {
CreateLayer(gr_context, //
ios_context, //
((FlutterView*)flutter_view_.get()).pixelFormat //
);
}
latch->CountDown();
});
latch->Wait();
}
CreateMissingOverlays(gr_context, ios_context, required_overlay_layers);
}

int64_t overlay_id = 0;
Expand Down Expand Up @@ -745,66 +721,117 @@ static bool ClipRRectContainsPlatformViewBoundingRect(const SkRRect& clip_rrect,
layer_pool_->RemoveUnusedLayers();
layer_pool_->RecycleLayers();

// Dispose unused Flutter Views.
auto views_to_dispose = DisposeViews();

auto task = [&, platform_view_layers = std::move(platform_view_layers), //
current_composition_params = current_composition_params_, //
views_to_recomposite = views_to_recomposite_, //
callbacks = callbacks, //
callbacks = std::move(callbacks), //
composition_order = composition_order_, //
unused_layers = unused_layers, views_to_dispose]() mutable {
TRACE_EVENT0("flutter", "FlutterPlatformViewsController::SubmitFrame::CATransaction");

[CATransaction begin];

// Configure Flutter overlay views.
for (const auto& [key, layers] : platform_view_layers) {
for (const auto& layer_data : layers) {
layer_data.layer->UpdateViewState(flutter_view_, //
layer_data.rect, //
layer_data.view_id, //
layer_data.overlay_id //
);
}
}
unused_layers = std::move(unused_layers),
views_to_dispose = DisposeViews() //
]() mutable {
PerformSubmit(std::move(platform_view_layers), //
std::move(callbacks), //
std::move(current_composition_params), //
std::move(views_to_recomposite), //
std::move(composition_order), //
std::move(unused_layers), //
std::move(views_to_dispose) //
);
};

// Dispose unused Flutter Views.
for (auto& view : views_to_dispose) {
[view removeFromSuperview];
}
// Workaround for FlutterPlatformViewsTest.mm
if ([[NSThread currentThread] isMainThread]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RunNowOrPostTask instead? Its hard to fathom what the workaround is for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should update the comment, its for iOS simualtors with software backend. The trick to split across threads doesn't work there so I still need to merge threads.

Yeah, will switch to RunNowOrPostTask.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And elsewhere.

task();
} else {
platform_task_runner_->PostTask(task);
}

return did_submit;
}

void FlutterPlatformViewsController::CreateMissingOverlays(
GrDirectContext* gr_context,
const std::shared_ptr<IOSContext>& ios_context,
size_t required_overlay_layers) {
TRACE_EVENT0("flutter", "FlutterPlatformViewsController::CreateMissingLayers");

// Composite Platform Views.
for (int64_t view_id : views_to_recomposite) {
CompositeWithParams(view_id, current_composition_params[view_id]);
auto missing_layer_count = required_overlay_layers - layer_pool_->size();
// Workaround for FLutterPlatformViewsTest
if ([[NSThread currentThread] isMainThread]) {
// Create Missing Layers
for (auto i = 0u; i < missing_layer_count; i++) {
CreateLayer(gr_context, //
ios_context, //
((FlutterView*)flutter_view_.get()).pixelFormat //
);
}
return;
}

// Present callbacks.
for (const auto& cb : callbacks) {
cb();
auto latch = std::make_shared<fml::CountDownLatch>(1u);
platform_task_runner_->PostTask([&]() {
// Create Missing Layers
for (auto i = 0u; i < missing_layer_count; i++) {
CreateLayer(gr_context, //
ios_context, //
((FlutterView*)flutter_view_.get()).pixelFormat //
);
}
latch->CountDown();
});
latch->Wait();
}

// Organize the layers by their z indexes.
auto active_composition_order = BringLayersIntoView(platform_view_layers, composition_order);
/// Update the buffers and mutate the platform views in CATransaction on the platform thread.
void FlutterPlatformViewsController::PerformSubmit(
LayersMap platform_view_layers,
std::vector<SurfaceFrame::DeferredSubmit> callbacks,
std::map<int64_t, EmbeddedViewParams> current_composition_params,
std::unordered_set<int64_t> views_to_recomposite,
std::vector<int64_t> composition_order,
std::vector<std::shared_ptr<FlutterPlatformViewLayer>> unused_layers,
std::vector<UIView*> views_to_dispose) {
TRACE_EVENT0("flutter", "FlutterPlatformViewsController::SubmitFrame::CATransaction");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a handle to the current task runner? Perhaps DCHECK for running on the platform view?


[CATransaction begin];

// Configure Flutter overlay views.
for (const auto& [key, layers] : platform_view_layers) {
for (const auto& layer_data : layers) {
layer_data.layer->UpdateViewState(flutter_view_, //
layer_data.rect, //
layer_data.view_id, //
layer_data.overlay_id //
);
}
}

// If a layer was allocated in the previous frame, but it's not used in the current frame,
// then it can be removed from the scene.
RemoveUnusedLayers(unused_layers, composition_order, active_composition_order);
// Dispose unused Flutter Views.
for (auto& view : views_to_dispose) {
[view removeFromSuperview];
}

// If the frame is submitted with embedded platform views,
// there should be a |[CATransaction begin]| call in this frame prior to all the drawing.
// If that case, we need to commit the transaction.
[CATransaction commit];
};
// Composite Platform Views.
for (int64_t view_id : views_to_recomposite) {
CompositeWithParams(view_id, current_composition_params[view_id]);
}

// Workaround for FlutterPlatformViewsTest.mm
if ([[NSThread currentThread] isMainThread]) {
task();
} else {
platform_task_runner_->PostTask(task);
// Present callbacks.
for (const auto& cb : callbacks) {
cb();
}

return did_submit;
// Organize the layers by their z indexes.
auto active_composition_order = BringLayersIntoView(platform_view_layers, composition_order);

// If a layer was allocated in the previous frame, but it's not used in the current frame,
// then it can be removed from the scene.
RemoveUnusedLayers(unused_layers, composition_order, active_composition_order);

// If the frame is submitted with embedded platform views,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this comment outdated now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep -- I'd be tempted to just delete the whole comment.

// there should be a |[CATransaction begin]| call in this frame prior to all the drawing.
// If that case, we need to commit the transaction.
[CATransaction commit];
}

std::vector<int64_t> FlutterPlatformViewsController::BringLayersIntoView(
Copy link
Contributor

@chinmaygarde chinmaygarde Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oof, this was tough to read and I wonder if this couldn't be made a whole lot simpler and faster.

For one, mutating the view hierarchy can be pretty slow because of layout updated triggered potentially. So you want to minimize them.

And, in cases where the hierarchy changes, you are making addSubview perform a reorder. No matter if there is a reorder or not. And it seems to be response to a performance issue discovered later.

I am assuming that the view hierarchy is the same but you just need to reorder. In that case, you just need two arrays of integers (of z order). You already have one in the composition_order. Build the second one using [superview.subviews indexOfObject:sub]. Now cycle over the two arrays (asserting beforehand that they are the same size) and call -[UIView exchangeSubviewAtIndex:withSubviewAtIndex:] only of the order at the specific index is different. If the order is the same, nothing changes. And you minimize reorders.

for (size_t i = 0; i < composition_order.size(); i++) {
  if (desired_order[i] != composition_order[i]) {
    exchange...
  }
}

That should be it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One complication is that I believe this is also adding the new flutter views, but we can account for that by adding them onto the end first and then moving them maybe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried a few things here, the issue is that the overlay views (not the platform views) need to be sequenced after the flutter views and don't have an ID. Also since the view hierarchy isn't the same I'd need a more complicated diff.

I think we can fix this, but since I'm not really changing this code i'd rather leave it as is.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,22 @@ class FlutterPlatformViewsController {

using LayersMap = std::map<int64_t, std::vector<LayerData>>;

/// Update the buffers and mutate the platform views in CATransaction on the platform thread.
void PerformSubmit(LayersMap platform_view_layers,
std::vector<SurfaceFrame::DeferredSubmit> callbacks,
std::map<int64_t, EmbeddedViewParams> current_composition_params,
std::unordered_set<int64_t> views_to_recomposite,
std::vector<int64_t> composition_order,
std::vector<std::shared_ptr<FlutterPlatformViewLayer>> unused_layers,
std::vector<UIView*> views_to_dispose);

/// @brief Populate any missing overlay layers.
///
/// This requires posting a task to the platform thread and blocking on its completion.
void CreateMissingOverlays(GrDirectContext* gr_context,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the map of layers can contain a nullopt for layers that should go into a specific spot but haven't been constructed yet. Then, in the call that currently reorders, you could inflate them to create the actual view before inserting it in the right spot. Just a suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is another approach we can do, but if we defer creating the overlay layers we also need to defer constructing encoding the raster work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

longer term, we should be able to construct the backing layer on any thread (once we fully move away from CAMetalLayer), and so we could remove this hack

const std::shared_ptr<IOSContext>& ios_context,
size_t required_overlay_layers);

void OnCreate(FlutterMethodCall* call, FlutterResult result) __attribute__((cf_audited_transfer));
void OnDispose(FlutterMethodCall* call, FlutterResult result)
__attribute__((cf_audited_transfer));
Expand Down Expand Up @@ -399,7 +415,7 @@ class FlutterPlatformViewsController {
// A vector of visited platform view IDs.
std::vector<int64_t> visited_platform_views_;

// Only compoiste platform views in this set.
// Only composite platform views in this set.
std::unordered_set<int64_t> views_to_recomposite_;

// The FlutterPlatformViewGestureRecognizersBlockingPolicy for each type of platform view.
Expand Down