File: flatland_connection.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (103 lines) | stat: -rw-r--r-- 3,973 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_OZONE_PLATFORM_FLATLAND_FLATLAND_CONNECTION_H_
#define UI_OZONE_PLATFORM_FLATLAND_FLATLAND_CONNECTION_H_

#include <fuchsia/ui/composition/cpp/fidl.h>

#include <memory>
#include <string_view>

#include "base/containers/queue.h"
#include "base/functional/callback.h"
#include "base/time/time.h"

namespace ui {

// Helper class used to own fuchsia.ui.composition.Flatland to safely call
// Present. By limiting the number of Present calls, FlatlandConnection
// ensures that the Flatland will not be shut down, thus, users of
// FlatlandConnection should not call Flatland::Present on their own.
class FlatlandConnection final {
 public:
  using OnFramePresentedCallback =
      base::OnceCallback<void(base::TimeTicks actual_presentation_time,
                              base::TimeDelta future_presentation_interval)>;
  using OnErrorCallback =
      base::OnceCallback<void(fuchsia::ui::composition::FlatlandError error)>;

  explicit FlatlandConnection(std::string_view debug_name,
                              OnErrorCallback callback);
  ~FlatlandConnection();

  FlatlandConnection(const FlatlandConnection&) = delete;
  FlatlandConnection& operator=(const FlatlandConnection&) = delete;

  fuchsia::ui::composition::Flatland* flatland() { return flatland_.get(); }

  // Returns an unused, non-zero transform identifier.
  fuchsia::ui::composition::TransformId NextTransformId() {
    return {++next_transform_id_};
  }

  // Returns an unused, non-zero content identifier.
  fuchsia::ui::composition::ContentId NextContentId() {
    return {++next_content_id_};
  }

  void Present();
  void Present(fuchsia::ui::composition::PresentArgs present_args,
               OnFramePresentedCallback callback);

 private:
  // Initial fps value to calculate the future presentation interval, that is
  // used until Flatland's OnNextFrameBegin() callback is received.
  static constexpr int kInitialFramesPerSecondEstimate = 60;

  // Method that is listening to Flatland's OnNextFrameBegin() callback.
  // Returns one or more present credits.
  void OnNextFrameBegin(
      fuchsia::ui::composition::OnNextFrameBeginValues values);
  // Method that is listening to Flatland's OnFramePresented() callback.
  // Returns feedback on the prior frame presentation.
  void OnFramePresented(fuchsia::scenic::scheduling::FramePresentedInfo info);

  fuchsia::ui::composition::FlatlandPtr flatland_;
  uint64_t next_transform_id_ = 0;
  uint64_t next_content_id_ = 0;
  uint32_t present_credits_ = 1;

  // Stores the presentation interval for the future frames.
  base::TimeDelta presentation_interval_ =
      base::Hertz(kInitialFramesPerSecondEstimate);

  struct PendingPresent {
    PendingPresent(fuchsia::ui::composition::PresentArgs present_args,
                   OnFramePresentedCallback callback);
    ~PendingPresent();

    PendingPresent(PendingPresent&& other);
    PendingPresent& operator=(PendingPresent&& other);

    fuchsia::ui::composition::PresentArgs present_args;
    OnFramePresentedCallback callback;
  };

  // Keeps track of pending Presents that cannot be committed in the situation
  // when we don't have enough present credits.
  base::queue<PendingPresent> pending_presents_;
  // Keeps track of release fences for the previous frame, indicating when it
  // is safe to reuse the resources. Ozone defines and sends release fences
  // for the current frame, whereas Flatland expects the release fences for
  // the previous frame resources.
  std::vector<zx::event> previous_present_release_fences_;
  // Keeps track of Presents that are committed, but Flatland hasn't indicated
  // that have taken effect by calling OnFramePresented().
  base::queue<OnFramePresentedCallback> presented_callbacks_;
};

}  // namespace ui

#endif  // UI_OZONE_PLATFORM_FLATLAND_FLATLAND_CONNECTION_H_