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

Commit a7f95f3

Browse files
committed
Add single_screen option to FlutterEngineDisplay
1 parent b4f386a commit a7f95f3

File tree

7 files changed

+65
-14
lines changed

7 files changed

+65
-14
lines changed

shell/common/display.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@
55
#ifndef FLUTTER_SHELL_COMMON_DISPLAY_H_
66
#define FLUTTER_SHELL_COMMON_DISPLAY_H_
77

8+
#include <optional>
9+
810
namespace flutter {
911

1012
/// Unique ID per display that is stable until the Flutter application restarts.
1113
/// See also: `flutter::Display`
1214
typedef uint64_t DisplayId;
1315

14-
/// Some devices do not support multiple displays, in such cases the embedding
15-
/// might not provide a device identifier. This value is used as the device id
16-
/// in such cases. There must only be one screen in such cases.
17-
static constexpr DisplayId kSingleScreenDeviceId = 0;
18-
1916
/// To be used when the display refresh rate is unknown.
2017
static constexpr double kUnknownDisplayRefreshRate = 0;
2118

@@ -24,20 +21,31 @@ static constexpr double kUnknownDisplayRefreshRate = 0;
2421
/// settings.
2522
class Display {
2623
public:
24+
//------------------------------------------------------------------------------
25+
/// @brief Construct a new Display object in case where the display id of the
26+
/// display is known. In cases where there are more than one displays, every
27+
/// display is expected to have a display id.
28+
///
2729
Display(DisplayId display_id, double refresh_rate)
2830
: display_id_(display_id), refresh_rate_(refresh_rate) {}
2931

32+
//------------------------------------------------------------------------------
33+
/// @brief Construct a new Display object when there is only a single display.
34+
/// When there are multiple displays, every display must have a display id.
35+
///
36+
Display(double refresh_rate) : display_id_({}), refresh_rate_(refresh_rate) {}
37+
3038
~Display() = default;
3139

3240
// Get the display's maximum refresh rate in the unit of frame per second.
3341
// Return `kUnknownDisplayRefreshRate` if the refresh rate is unknown.
3442
double GetRefreshRate() const { return refresh_rate_; }
3543

3644
/// Returns the `DisplayId` of the display.
37-
DisplayId GetDisplayId() const { return display_id_; }
45+
std::optional<DisplayId> GetDisplayId() const { return display_id_; }
3846

3947
private:
40-
DisplayId display_id_;
48+
std::optional<DisplayId> display_id_;
4149
double refresh_rate_;
4250
};
4351

shell/common/display_manager.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ double DisplayManager::GetMainDisplayRefreshRate() {
2424

2525
void DisplayManager::HandleDisplayUpdates(DisplayUpdateType update_type,
2626
std::vector<Display> displays) {
27+
CheckDisplayConfiguration(displays);
2728
std::scoped_lock lock(displays_mutex_);
2829
switch (update_type) {
2930
case DisplayUpdateType::kStartup:
@@ -34,4 +35,13 @@ void DisplayManager::HandleDisplayUpdates(DisplayUpdateType update_type,
3435
}
3536
}
3637

38+
void DisplayManager::CheckDisplayConfiguration(std::vector<Display> displays) {
39+
FML_CHECK(!displays.empty());
40+
if (displays.size() > 1) {
41+
for (auto& display : displays) {
42+
FML_CHECK(display.GetDisplayId().has_value());
43+
}
44+
}
45+
}
46+
3747
} // namespace flutter

shell/common/display_manager.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class DisplayManager {
4646
/// Guards `displays_` vector.
4747
std::mutex displays_mutex_;
4848
std::vector<Display> displays_;
49+
50+
/// Checks that the provided display configuration is valid. Currently this
51+
/// ensures that all the displays have id in case there are multiple. In case
52+
/// there is a single display, it is valid for the display to not have an id.
53+
void CheckDisplayConfiguration(std::vector<Display> displays);
4954
};
5055

5156
} // namespace flutter

shell/platform/android/android_shell_holder.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ AndroidShellHolder::AndroidShellHolder(
7676
}
7777
weak_platform_view = platform_view_android->GetWeakPtr();
7878
shell.OnDisplayUpdates(DisplayUpdateType::kStartup,
79-
{Display(kSingleScreenDeviceId,
80-
jni_facade->GetDisplayRefreshRate())});
79+
{Display(jni_facade->GetDisplayRefreshRate())});
8180
return platform_view_android;
8281
};
8382

shell/platform/darwin/ios/framework/Source/FlutterEngine.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ - (BOOL)createShell:(NSString*)entrypoint
557557

558558
- (void)initializeDisplays {
559559
double refresh_rate = [[[DisplayLinkManager alloc] init] displayRefreshRate];
560-
auto display = flutter::Display(flutter::kSingleScreenDeviceId, refresh_rate);
560+
auto display = flutter::Display(refresh_rate);
561561
_shell->OnDisplayUpdates(flutter::DisplayUpdateType::kStartup, {display});
562562
}
563563

shell/platform/embedder/embedder.cc

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,23 +1956,46 @@ FlutterEngineResult FlutterEnginePostCallbackOnAllNativeThreads(
19561956
"tasks to all threads.");
19571957
}
19581958

1959+
namespace {
1960+
static bool ValidDisplayConfiguration(const FlutterEngineDisplay* displays,
1961+
size_t display_count) {
1962+
for (size_t i = 0; i < display_count; i++) {
1963+
if (displays[i].single_display && display_count != 1) {
1964+
return false;
1965+
}
1966+
}
1967+
return true;
1968+
}
1969+
} // namespace
1970+
19591971
FlutterEngineResult FlutterEngineNotifyDisplayUpdate(
19601972
FLUTTER_API_SYMBOL(FlutterEngine) raw_engine,
19611973
const FlutterEngineDisplaysUpdateType update_type,
1962-
const FlutterEngineDisplay* displays,
1974+
const FlutterEngineDisplay* embedder_displays,
19631975
size_t display_count) {
19641976
if (raw_engine == nullptr) {
19651977
return LOG_EMBEDDER_ERROR(kInvalidArguments, "Invalid engine handle.");
19661978
}
19671979

1980+
if (!ValidDisplayConfiguration(embedder_displays, display_count)) {
1981+
return LOG_EMBEDDER_ERROR(
1982+
kInvalidArguments,
1983+
"Invalid FlutterEngineDisplay configuration specified.");
1984+
}
1985+
19681986
auto engine = reinterpret_cast<flutter::EmbedderEngine*>(raw_engine);
19691987

19701988
switch (update_type) {
19711989
case kFlutterEngineDisplaysUpdateTypeStartup: {
19721990
std::vector<flutter::Display> displays;
19731991
for (size_t i = 0; i < display_count; i++) {
1974-
displays.push_back(flutter::Display(displays[i].GetDisplayId(),
1975-
displays[i].GetRefreshRate()));
1992+
flutter::Display display =
1993+
flutter::Display(embedder_displays[i].refresh_rate);
1994+
if (!embedder_displays[i].single_display) {
1995+
display = flutter::Display(embedder_displays[i].display_id,
1996+
embedder_displays[i].refresh_rate);
1997+
}
1998+
displays.push_back(display);
19761999
}
19772000
engine->GetShell().OnDisplayUpdates(flutter::DisplayUpdateType::kStartup,
19782001
displays);
@@ -1981,6 +2004,6 @@ FlutterEngineResult FlutterEngineNotifyDisplayUpdate(
19812004
default:
19822005
return LOG_EMBEDDER_ERROR(
19832006
kInvalidArguments,
1984-
"Invalid FlutterEngineDartObjectType type specified.");
2007+
"Invalid FlutterEngineDisplaysUpdateType type specified.");
19852008
}
19862009
}

shell/platform/embedder/embedder.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,12 @@ typedef struct {
974974

975975
FlutterEngineDisplayId display_id;
976976

977+
/// This is set to true if the embedder only has one display. In cases where
978+
/// this is set to true, the value of display_id is ignored. In cases where
979+
/// this is not set to true, it is expected that a valid display_id be
980+
/// provided.
981+
bool single_display;
982+
977983
/// This represents the refresh period in frames per second. This value may be
978984
/// zero if the device is not running or unavaliable or unknown.
979985
double refresh_rate;

0 commit comments

Comments
 (0)