1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef UI_ACCELERATED_WIDGET_MAC_DISPLAY_LINK_MAC_H_
6 #define UI_ACCELERATED_WIDGET_MAC_DISPLAY_LINK_MAC_H_
9 #include <QuartzCore/CVDisplayLink.h>
11 #include "base/lazy_instance.h"
12 #include "base/mac/scoped_typeref.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/synchronization/lock.h"
15 #include "base/time/time.h"
16 #include "base/timer/timer.h"
17 #include "ui/accelerated_widget_mac/accelerated_widget_mac_export.h"
21 class ACCELERATED_WIDGET_MAC_EXPORT DisplayLinkMac
:
22 public base::RefCounted
<DisplayLinkMac
> {
24 static scoped_refptr
<DisplayLinkMac
> GetForDisplay(
25 CGDirectDisplayID display_id
);
27 // Get vsync scheduling parameters.
28 bool GetVSyncParameters(
29 base::TimeTicks
* timebase
,
30 base::TimeDelta
* interval
);
32 // The vsync parameters are cached, because re-computing them is expensive.
33 // The parameters also skew over time (astonishingly quickly -- 0.1 msec per
34 // second), so, use this method to tell the display link the current time.
35 // If too much time has elapsed since the last time the vsync parameters were
36 // calculated, re-calculate them.
37 void NotifyCurrentTime(const base::TimeTicks
& now
);
40 friend class base::RefCounted
<DisplayLinkMac
>;
43 CGDirectDisplayID display_id
,
44 base::ScopedTypeRef
<CVDisplayLinkRef
> display_link
);
45 virtual ~DisplayLinkMac();
47 void StartOrContinueDisplayLink();
48 void StopDisplayLink();
49 void Tick(const CVTimeStamp
& time
);
51 // Called by the system on the display link thread, and posts a call to Tick
53 static CVReturn
DisplayLinkCallback(
54 CVDisplayLinkRef display_link
,
55 const CVTimeStamp
* now
,
56 const CVTimeStamp
* output_time
,
57 CVOptionFlags flags_in
,
58 CVOptionFlags
* flags_out
,
61 // This is called whenever the display is reconfigured, and marks that the
62 // vsync parameters must be recalculated.
63 static void DisplayReconfigurationCallBack(
64 CGDirectDisplayID display
,
65 CGDisplayChangeSummaryFlags flags
,
68 // The task runner to post tasks to from the display link thread.
69 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner_
;
71 // The display that this display link is attached to.
72 CGDirectDisplayID display_id_
;
74 // CVDisplayLink for querying VSync timing info.
75 base::ScopedTypeRef
<CVDisplayLinkRef
> display_link_
;
77 // VSync parameters computed during Tick.
78 bool timebase_and_interval_valid_
;
79 base::TimeTicks timebase_
;
80 base::TimeDelta interval_
;
82 // The time after which we should re-start the display link to get fresh
84 base::TimeTicks recalculate_time_
;
86 // Each display link instance consumes a non-negligible number of cycles, so
87 // make all display links on the same screen share the same object.
88 typedef std::map
<CGDirectDisplayID
, DisplayLinkMac
*> DisplayMap
;
89 static base::LazyInstance
<DisplayMap
> display_map_
;
94 #endif // UI_ACCELERATED_WIDGET_MAC_DISPLAY_LINK_MAC_H_