File: display_link_mac.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 (101 lines) | stat: -rw-r--r-- 3,694 bytes parent folder | download | duplicates (5)
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
// Copyright 2014 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_DISPLAY_MAC_DISPLAY_LINK_MAC_H_
#define UI_DISPLAY_MAC_DISPLAY_LINK_MAC_H_

#include "base/apple/scoped_typeref.h"
#include "base/functional/callback_forward.h"
#include "base/memory/ref_counted.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "ui/display/display_export.h"

namespace ui {

// VSync parameters parsed from CVDisplayLinkOutputCallback's parameters.
struct DISPLAY_EXPORT VSyncParamsMac {
  // The time of the callback.
  bool callback_times_valid = false;
  base::TimeTicks callback_timebase;
  base::TimeDelta callback_interval;

  // The indicated display time.
  bool display_times_valid = false;
  base::TimeTicks display_timebase;
  base::TimeDelta display_interval;
};

// Object used to control the lifetime of callbacks from DisplayLinkMac.
// See notes in DisplayLinkMac::RegisterCallback
class DISPLAY_EXPORT VSyncCallbackMac {
 public:
  using Callback = base::RepeatingCallback<void(VSyncParamsMac)>;
  ~VSyncCallbackMac();

  // To prevent constantly switching VSync on and off, allow this max number of
  // extra CVDisplayLink VSync running before stopping CVDisplayLink.
  static constexpr int kMaxExtraVSyncs = 12;

 private:
  friend class CADisplayLinkMac;
  friend struct ObjCState;
  friend class CVDisplayLinkMac;
  friend class DisplayLinkMacSharedState;
  using UnregisterCallback = base::OnceCallback<void(VSyncCallbackMac*)>;

  explicit VSyncCallbackMac(UnregisterCallback unregister_callback,
                            Callback callback,
                            bool do_callback_on_ctor_thread);

  // The callback to unregister `this` with its DisplayLinkMac.
  UnregisterCallback unregister_callback_;

  Callback callback_for_displaylink_thread_;

  base::WeakPtrFactory<VSyncCallbackMac> weak_factory_{this};
};

class DISPLAY_EXPORT DisplayLinkMac : public base::RefCounted<DisplayLinkMac> {
 public:
  // Create a DisplayLinkMac for the specified display. The returned object may
  // only be accessed on the thread on which it was retrieved.
  static scoped_refptr<DisplayLinkMac> GetForDisplay(int64_t display_id);

  // Register an observer callback.
  // * The specified callback will be called at every VSync tick, until the
  //   returned VSyncCallbackMac object is destroyed.
  // * The resulting VSyncCallbackMac object must be destroyed on the same
  //   thread on which it was created.
  // * The callback is guaranteed to be made on the register thread.
  virtual std::unique_ptr<VSyncCallbackMac> RegisterCallback(
      VSyncCallbackMac::Callback callback) = 0;

  // Get the panel/monitor refresh rate
  virtual double GetRefreshRate() const = 0;
  virtual void GetRefreshIntervalRange(base::TimeDelta& min_interval,
                                       base::TimeDelta& max_interval,
                                       base::TimeDelta& granularity) const = 0;

  virtual void SetPreferredInterval(base::TimeDelta interval) = 0;
  virtual void SetPreferredIntervalRange(
      base::TimeDelta min_interval,
      base::TimeDelta max_interval,
      base::TimeDelta preferred_interval) = 0;

  // Retrieves the current (“now”) time of a given display link. Returns
  // base::TimeTicks() if the current time is not available.
  virtual base::TimeTicks GetCurrentTime() const = 0;

 protected:
  friend class base::RefCounted<DisplayLinkMac>;
  friend class CVDisplayLinkMac;
  friend class CADisplayLinkMac;

  virtual ~DisplayLinkMac() = default;
};

}  // namespace ui

#endif  // UI_DISPLAY_MAC_DISPLAY_LINK_MAC_H_