Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Basic view management for engine classes #42991

Merged
merged 65 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
d36a55d
Rebase
dkwingsmt Jun 30, 2023
3812621
Rename vars and fix fuchsia compile
dkwingsmt Jun 30, 2023
b1b68eb
Revert window metric changes
dkwingsmt Jul 5, 2023
5e036d8
Apply suggestions from code review
dkwingsmt Jul 6, 2023
ba42c07
Merge remote-tracking branch 'origin/main' into mv-window-metrics
dkwingsmt Jul 6, 2023
fba2c7c
Doc enable_implicit_view
dkwingsmt Jul 6, 2023
d7c6510
Better platConfig structure for adding window
dkwingsmt Jul 6, 2023
968c7e9
Merge remote-tracking branch 'origin/main' into mv-window-metrics
dkwingsmt Jul 10, 2023
752a0a4
Merge remote-tracking branch 'origin/main' into mv-window-metrics
dkwingsmt Jul 10, 2023
c912775
Merge remote-tracking branch 'origin/main' into mv-window-metrics
dkwingsmt Jul 20, 2023
2a87664
Fix compile
dkwingsmt Jul 23, 2023
adf5b8f
Doc for implicit view
dkwingsmt Jul 23, 2023
f1a497b
Two tests
dkwingsmt Jul 24, 2023
f31f60c
Remove platform data view metrics
dkwingsmt Jul 24, 2023
9d68f3c
Extract function
dkwingsmt Jul 24, 2023
7af9496
Docs and address comments
dkwingsmt Jul 24, 2023
2e539c6
One more sentence
dkwingsmt Jul 24, 2023
d20363e
Merge remote-tracking branch 'origin/main' into mv-window-metrics
dkwingsmt Jul 24, 2023
3d5a190
AddRemoveView test
dkwingsmt Jul 25, 2023
ba845c7
Merge remote-tracking branch 'origin/main' into mv-window-metrics
dkwingsmt Jul 25, 2023
4fc4922
Remove expected size on removal
dkwingsmt Jul 25, 2023
db9a99c
More docs
dkwingsmt Jul 25, 2023
a098175
Merge remote-tracking branch 'origin/main' into mv-window-metrics
dkwingsmt Jul 25, 2023
2128688
Assign viewId not bool. Move viewId to settings.h.
dkwingsmt Jul 25, 2023
4f049cd
Remove more implicit view ID
dkwingsmt Jul 25, 2023
c795035
Fix compile, prefix view id with flutter
dkwingsmt Jul 25, 2023
03520e2
Address many comments
dkwingsmt Jul 26, 2023
285c0f6
Correct FlushRuntimeStateToIsolate and PlatformData
dkwingsmt Jul 27, 2023
9c92de2
AddView specifies view config
dkwingsmt Jul 27, 2023
86bd921
Merge remote-tracking branch 'origin/main' into mv-window-metrics
dkwingsmt Jul 27, 2023
a898834
Remove ImplicitViewEnabled from more and lint
dkwingsmt Jul 27, 2023
2ff8609
Fix test and lint
dkwingsmt Jul 28, 2023
c61cf67
Fix tests?
dkwingsmt Jul 31, 2023
d9fc2a5
Shell Add/RemoveView blocking
dkwingsmt Aug 1, 2023
e99900a
Merge remote-tracking branch 'origin/main' into mv-window-metrics
dkwingsmt Aug 1, 2023
89755d2
Implement Rasterizer::CollectView
dkwingsmt Aug 1, 2023
42db5c3
Flushing test
dkwingsmt Aug 1, 2023
164ca90
Add TODO doc
dkwingsmt Aug 1, 2023
3e2902b
doc for implicit view ID.
dkwingsmt Aug 1, 2023
26d3cbe
Better doc
dkwingsmt Aug 1, 2023
8db91df
Bring back docs
dkwingsmt Aug 1, 2023
8d718d5
Merge remote-tracking branch 'origin/main' into mv-window-metrics
dkwingsmt Aug 1, 2023
0d3f6fb
Merge remote-tracking branch 'origin/main' into mv-window-metrics
dkwingsmt Aug 1, 2023
5cb56f8
Trivial comments
dkwingsmt Aug 3, 2023
6264164
Use TODO issue; solve lint
dkwingsmt Aug 3, 2023
a1b0740
Simplify implicit view init, add assertion
dkwingsmt Aug 4, 2023
b3284aa
Doc
dkwingsmt Aug 4, 2023
2fb045b
Comments
dkwingsmt Aug 4, 2023
257f7cc
Merge remote-tracking branch 'origin/main' into mv-window-metrics
dkwingsmt Aug 4, 2023
9bb1d75
RemoveView no longer blocks
dkwingsmt Aug 4, 2023
90020f4
Fix compile
dkwingsmt Aug 5, 2023
7c24ab8
Merge remote-tracking branch 'origin/main' into mv-window-metrics
dkwingsmt Aug 7, 2023
9164eb8
Merge remote-tracking branch 'origin/main' into mv-window-metrics
dkwingsmt Aug 8, 2023
352d8b4
Merge remote-tracking branch 'origin/main' into mv-window-metrics
dkwingsmt Aug 15, 2023
d427a09
Merge remote-tracking branch 'origin/main' into mv-window-metrics
dkwingsmt Aug 16, 2023
58f18a8
Remove flag
dkwingsmt Aug 16, 2023
dd832b1
Impl
dkwingsmt Aug 16, 2023
da0d2c9
Merge branch 'pre-metrics' into mv-window-metrics
dkwingsmt Aug 16, 2023
c044725
Remove window
dkwingsmt Aug 17, 2023
6a49f79
return when assert
dkwingsmt Aug 17, 2023
c5885b2
Merge remote-tracking branch 'origin/main' into mv-window-metrics
dkwingsmt Aug 17, 2023
f634fe1
Move to constants
dkwingsmt Aug 17, 2023
7326d22
Merge remote-tracking branch 'origin/main' into mv-window-metrics
dkwingsmt Aug 17, 2023
42f4309
Revert empty line
dkwingsmt Aug 17, 2023
64ecc08
Fix compile
dkwingsmt Aug 17, 2023
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
4 changes: 0 additions & 4 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1904,8 +1904,6 @@ ORIGIN: ../../../flutter/lib/ui/window/pointer_data_packet_converter.cc + ../../
ORIGIN: ../../../flutter/lib/ui/window/pointer_data_packet_converter.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/ui/window/viewport_metrics.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/ui/window/viewport_metrics.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/ui/window/window.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/ui/window/window.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/annotations.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/canvas.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/channel_buffers.dart + ../../../flutter/LICENSE
Expand Down Expand Up @@ -4634,8 +4632,6 @@ FILE: ../../../flutter/lib/ui/window/pointer_data_packet_converter.cc
FILE: ../../../flutter/lib/ui/window/pointer_data_packet_converter.h
FILE: ../../../flutter/lib/ui/window/viewport_metrics.cc
FILE: ../../../flutter/lib/ui/window/viewport_metrics.h
FILE: ../../../flutter/lib/ui/window/window.cc
FILE: ../../../flutter/lib/ui/window/window.h
FILE: ../../../flutter/lib/web_ui/lib/annotations.dart
FILE: ../../../flutter/lib/web_ui/lib/canvas.dart
FILE: ../../../flutter/lib/web_ui/lib/channel_buffers.dart
Expand Down
26 changes: 26 additions & 0 deletions common/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,32 @@

namespace flutter {
constexpr double kMegaByteSizeInBytes = (1 << 20);

// The ID for the implicit view if the implicit view is enabled.
//
// The implicit view is a compatibility mechanism to help the transition from
// the older single-view APIs to the newer multi-view APIs. The two sets of APIs
// use different models for view management. The implicit view mechanism allows
// single-view APIs to operate a special view as if other views don't exist.
//
// In the regular multi-view model, all views should be created by
// `Shell::AddView` before being used, and removed by `Shell::RemoveView` to
// signify that they are gone. If a view is added or removed, the framework
// (`PlatformDispatcher`) will be notified. New view IDs are always unique,
// never reused. Operating a non-existing view is an error.
//
// The implicit view is another special view in addition to the "regular views"
// as above. The shell starts up having the implicit view, which has a fixed
// view ID of `kFlutterImplicitViewId` and is available throughout the lifetime
// of the shell. `Shell::AddView` or `RemoveView` must not be called for this
// view. Even when the window that shows the view is closed, the framework is
// unaware and might continue rendering into or operating this view.
//
// The single-view APIs, which are APIs that do not specify view IDs, operate
// the implicit view. The multi-view APIs can operate all views, including the
// implicit view if the target ID is `kFlutterImplicitViewId`, unless specified
// otherwise.
constexpr int64_t kFlutterImplicitViewId = 0;
} // namespace flutter

#endif // FLUTTER_COMMON_CONSTANTS_H_
2 changes: 0 additions & 2 deletions lib/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ source_set("ui") {
"window/pointer_data_packet_converter.h",
"window/viewport_metrics.cc",
"window/viewport_metrics.h",
"window/window.cc",
"window/window.h",
]

public_configs = [ "//flutter:config" ]
Expand Down
1 change: 0 additions & 1 deletion lib/ui/compositing/scene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "flutter/lib/ui/painting/picture.h"
#include "flutter/lib/ui/ui_dart_state.h"
#include "flutter/lib/ui/window/platform_configuration.h"
#include "flutter/lib/ui/window/window.h"
#if IMPELLER_SUPPORTS_RENDERING
#include "flutter/lib/ui/painting/display_list_deferred_image_gpu_impeller.h"
#endif // IMPELLER_SUPPORTS_RENDERING
Expand Down
8 changes: 7 additions & 1 deletion lib/ui/dart_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <mutex>
#include <string_view>

#include "flutter/common/constants.h"
#include "flutter/common/settings.h"
#include "flutter/fml/build_config.h"
#include "flutter/lib/ui/compositing/scene.h"
Expand Down Expand Up @@ -95,7 +96,6 @@ typedef CanvasPath Path;
V(IsolateNameServerNatives::RemovePortNameMapping, 1) \
V(NativeStringAttribute::initLocaleStringAttribute, 4) \
V(NativeStringAttribute::initSpellOutStringAttribute, 3) \
V(PlatformConfigurationNativeApi::ImplicitViewEnabled, 0) \
V(PlatformConfigurationNativeApi::DefaultRouteName, 0) \
V(PlatformConfigurationNativeApi::ScheduleFrame, 0) \
V(PlatformConfigurationNativeApi::Render, 1) \
Expand Down Expand Up @@ -373,6 +373,12 @@ void DartUI::InitForIsolate(const Settings& settings) {
Dart_PropagateError(result);
}
}

result = Dart_SetField(dart_ui, ToDart("_implicitViewId"),
Dart_NewInteger(kFlutterImplicitViewId));
if (Dart_IsError(result)) {
Dart_PropagateError(result);
}
}

} // namespace flutter
54 changes: 54 additions & 0 deletions lib/ui/hooks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,60 @@

part of dart.ui;

@pragma('vm:entry-point')
void _addView(
int viewId,
double devicePixelRatio,
double width,
double height,
double viewPaddingTop,
double viewPaddingRight,
double viewPaddingBottom,
double viewPaddingLeft,
double viewInsetTop,
double viewInsetRight,
double viewInsetBottom,
double viewInsetLeft,
double systemGestureInsetTop,
double systemGestureInsetRight,
double systemGestureInsetBottom,
double systemGestureInsetLeft,
double physicalTouchSlop,
List<double> displayFeaturesBounds,
List<int> displayFeaturesType,
List<int> displayFeaturesState,
int displayId,
) {
final _ViewConfiguration viewConfiguration = _buildViewConfiguration(
devicePixelRatio,
width,
height,
viewPaddingTop,
viewPaddingRight,
viewPaddingBottom,
viewPaddingLeft,
viewInsetTop,
viewInsetRight,
viewInsetBottom,
viewInsetLeft,
systemGestureInsetTop,
systemGestureInsetRight,
systemGestureInsetBottom,
systemGestureInsetLeft,
physicalTouchSlop,
displayFeaturesBounds,
displayFeaturesType,
displayFeaturesState,
displayId,
);
PlatformDispatcher.instance._addView(viewId, viewConfiguration);
}

@pragma('vm:entry-point')
void _removeView(int viewId) {
PlatformDispatcher.instance._removeView(viewId);
}

@pragma('vm:entry-point')
void _updateDisplays(
List<int> ids,
Expand Down
9 changes: 9 additions & 0 deletions lib/ui/natives.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,12 @@ _ScheduleImmediateClosure _getScheduleMicrotaskClosure() => _scheduleMicrotask;
// rendering.
@pragma('vm:entry-point')
bool _impellerEnabled = false;

// Used internally to indicate whether the embedder enables the implicit view,
// and the implicit view's ID if so.
//
// The exact value of this variable is an implementation detail that may change
// at any time. Apps should always use PlatformDispatcher.implicitView to
// determine the current implicit view, if any.
@pragma('vm:entry-point')
int? _implicitViewId;
16 changes: 10 additions & 6 deletions lib/ui/painting/canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "flutter/lib/ui/painting/paint.h"
#include "flutter/lib/ui/ui_dart_state.h"
#include "flutter/lib/ui/window/platform_configuration.h"
#include "flutter/lib/ui/window/window.h"

using tonic::ToDart;

Expand Down Expand Up @@ -621,11 +620,16 @@ void Canvas::drawShadow(const CanvasPath* path,
}

// Not using SafeNarrow because DPR will always be a relatively small number.
SkScalar dpr = static_cast<float>(UIDartState::Current()
->platform_configuration()
->get_window(0)
->viewport_metrics()
.device_pixel_ratio);
const ViewportMetrics* metrics =
UIDartState::Current()->platform_configuration()->GetMetrics(0);
SkScalar dpr;
// TODO(dkwingsmt): We should support rendering shadows on non-implicit views.
// However, currently this method has no way to get the target view ID.
if (metrics == nullptr) {
dpr = 1.0f;
} else {
dpr = static_cast<float>(metrics->device_pixel_ratio);
}
if (display_list_builder_) {
// The DrawShadow mechanism results in non-public operations to be
// performed on the canvas involving an SkDrawShadowRec. Since we
Expand Down
74 changes: 53 additions & 21 deletions lib/ui/platform_dispatcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,6 @@ typedef ErrorCallback = bool Function(Object exception, StackTrace stackTrace);
// A gesture setting value that indicates it has not been set by the engine.
const double _kUnsetGestureSetting = -1.0;

// The view ID of PlatformDispatcher.implicitView. This is an
// implementation detail that may change at any time. Apps
// should always use PlatformDispatcher.implicitView to determine
// the current implicit view, if any.
//
// Keep this in sync with kImplicitViewId in window/platform_configuration.cc.
const int _kImplicitViewId = 0;

// A message channel to receive KeyData from the platform.
//
// See embedder.cc::kFlutterKeyDataChannel for more information.
Expand Down Expand Up @@ -218,10 +210,29 @@ class PlatformDispatcher {
/// * [View.of], for accessing the current view.
/// * [PlatformDispatcher.views] for a list of all [FlutterView]s provided
/// by the platform.
FlutterView? get implicitView => _implicitViewEnabled() ? _views[_kImplicitViewId] : null;

@Native<Handle Function()>(symbol: 'PlatformConfigurationNativeApi::ImplicitViewEnabled')
external static bool _implicitViewEnabled();
FlutterView? get implicitView {
final FlutterView? result = _views[_implicitViewId];
// Make sure [implicitView] agrees with `_implicitViewId`.
assert((result != null) == (_implicitViewId != null),
(_implicitViewId != null) ?
'The implicit view ID is $_implicitViewId, but the implicit view does not exist.' :
'The implicit view ID is null, but the implicit view exists.');
// Make sure [implicitView] never chages.
assert(() {
if (_debugRecordedLastImplicitView) {
assert(identical(_debugLastImplicitView, result),
'The implicitView has changed:\n'
'Last: $_debugLastImplicitView\nCurrent: $result');
} else {
_debugLastImplicitView = result;
_debugRecordedLastImplicitView = true;
}
return true;
}());
return result;
}
FlutterView? _debugLastImplicitView;
bool _debugRecordedLastImplicitView = false;

/// A callback that is invoked whenever the [ViewConfiguration] of any of the
/// [views] changes.
Expand Down Expand Up @@ -249,6 +260,34 @@ class PlatformDispatcher {
_onMetricsChangedZone = Zone.current;
}

// Called from the engine, via hooks.dart
//
// Adds a new view with the specific view configuration.
//
// The implicit view must be added before [implicitView] is first called,
// which is typically the main function.
void _addView(int id, _ViewConfiguration viewConfiguration) {
assert(!_views.containsKey(id), 'View ID $id already exists.');
_views[id] = FlutterView._(id, this, viewConfiguration);
_invoke(onMetricsChanged, _onMetricsChangedZone);
}

// Called from the engine, via hooks.dart
//
// Removes the specific view.
//
// The target view must must exist. The implicit view must not be removed,
// or an assertion will be triggered.
void _removeView(int id) {
assert(id != _implicitViewId, 'The implicit view #$id can not be removed.');
if (id == _implicitViewId) {
return;
}
assert(_views.containsKey(id), 'View ID $id does not exist.');
_views.remove(id);
_invoke(onMetricsChanged, _onMetricsChangedZone);
}

// Called from the engine, via hooks.dart.
//
// Updates the available displays.
Expand All @@ -264,15 +303,8 @@ class PlatformDispatcher {
//
// Updates the metrics of the window with the given id.
void _updateWindowMetrics(int viewId, _ViewConfiguration viewConfiguration) {
final FlutterView? view = _views[viewId];
if (viewId == _kImplicitViewId && view == null) {
// TODO(goderbauer): Remove the implicit creation of the implicit view
// when we have an addView API and the implicit view is added via that.
_views[viewId] = FlutterView._(viewId, this, viewConfiguration);
} else {
assert(view != null);
view!._viewConfiguration = viewConfiguration;
}
assert(_views.containsKey(viewId), 'View $viewId does not exist.');
_views[viewId]!._viewConfiguration = viewConfiguration;
_invoke(onMetricsChanged, _onMetricsChangedZone);
}

Expand Down
1 change: 1 addition & 0 deletions lib/ui/ui_dart_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "flutter/fml/message_loop.h"
#include "flutter/lib/ui/window/platform_configuration.h"
#include "flutter/lib/ui/window/platform_message.h"
#include "third_party/tonic/converter/dart_converter.h"
#include "third_party/tonic/dart_message_handler.h"

Expand Down
6 changes: 5 additions & 1 deletion lib/ui/window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,11 @@ class SingletonFlutterWindow extends FlutterView {
'This feature was deprecated after v3.7.0-32.0.pre.'
)
SingletonFlutterWindow._() : super._(
_kImplicitViewId,
// TODO(dkwingsmt): This crashes if the implicit view is disabled. We need
// to resolve this by the time embedders are allowed to disable the implicit
// view.
// https://github.com/flutter/flutter/issues/131651
_implicitViewId!,
PlatformDispatcher.instance,
const _ViewConfiguration(),
);
Expand Down
Loading