Dan Field 0a6301d5ca Listen for display refresh changes and report them correctly (flutter/engine#29800)
This makes sure the frame timings recorder and vsync waiter implementations get
the correct refresh rate if or when it adjusts at runtime. This should be OK
because we'll only query the refresh rate when the display metrics actually
change, which is much less frequent than every frame. I experimented with an NDK
implementation in the previous patch, but that vastly restricts the API levels
we can support, and currently on API 30 and 31 it just calls Java methods
through JNI anyway.

I've refactored the way Display updates are reported so that AndroidDisplay can
just dynamically get the refresh rate correctly. These values are used by a
service protocol extension and by some Stopwatches on the CompositorContext.

See also #29765

Fixes flutter/flutter#93688
Fixes flutter/flutter#93698
2021-11-18 13:09:26 -08:00

61 lines
2.0 KiB
C++

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_SHELL_COMMON_DISPLAY_MANAGER_H_
#define FLUTTER_SHELL_COMMON_DISPLAY_MANAGER_H_
#include <mutex>
#include <vector>
#include "flutter/shell/common/display.h"
namespace flutter {
/// The update type parameter that is passed to
/// `DisplayManager::HandleDisplayUpdates`.
enum class DisplayUpdateType {
/// `flutter::Display`s that were active during start-up. A display is
/// considered active if:
/// 1. The frame buffer hardware is connected.
/// 2. The display is drawable, e.g. it isn't being mirrored from another
/// connected display or sleeping.
kStartup
};
/// Manages lifecycle of the connected displays. This class is thread-safe.
class DisplayManager {
public:
DisplayManager();
~DisplayManager();
/// Returns the display refresh rate of the main display. In cases where there
/// is only one display connected, it will return that. We do not yet support
/// cases where there are multiple displays.
///
/// When there are no registered displays, it returns
/// `kUnknownDisplayRefreshRate`.
double GetMainDisplayRefreshRate() const;
/// Handles the display updates.
void HandleDisplayUpdates(DisplayUpdateType update_type,
std::vector<std::unique_ptr<Display>> displays);
private:
/// Guards `displays_` vector.
mutable std::mutex displays_mutex_;
std::vector<std::unique_ptr<Display>> displays_;
/// Checks that the provided display configuration is valid. Currently this
/// ensures that all the displays have an id in the case there are multiple
/// displays. In case where there is a single display, it is valid for the
/// display to not have an id.
void CheckDisplayConfiguration(
const std::vector<std::unique_ptr<Display>>& displays) const;
};
} // namespace flutter
#endif // FLUTTER_SHELL_COMMON_DISPLAY_MANAGER_H_