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_
8 #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
);
33 friend class base::RefCounted
<DisplayLinkMac
>;
36 CGDirectDisplayID display_id
,
37 base::ScopedTypeRef
<CVDisplayLinkRef
> display_link
);
38 virtual ~DisplayLinkMac();
40 void StartOrContinueDisplayLink();
41 void StopDisplayLink();
42 void Tick(const CVTimeStamp
& time
);
44 // Called by the system on the display link thread, and posts a call to Tick
46 static CVReturn
DisplayLinkCallback(
47 CVDisplayLinkRef display_link
,
48 const CVTimeStamp
* now
,
49 const CVTimeStamp
* output_time
,
50 CVOptionFlags flags_in
,
51 CVOptionFlags
* flags_out
,
54 // This is called whenever the display is reconfigured, and marks that the
55 // vsync parameters must be recalculated.
56 static void DisplayReconfigurationCallBack(
57 CGDirectDisplayID display
,
58 CGDisplayChangeSummaryFlags flags
,
61 // The task runner to post tasks to from the display link thread.
62 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner_
;
64 // The display that this display link is attached to.
65 CGDirectDisplayID display_id_
;
67 // CVDisplayLink for querying VSync timing info.
68 base::ScopedTypeRef
<CVDisplayLinkRef
> display_link_
;
70 // VSync parameters computed during Tick.
71 bool timebase_and_interval_valid_
;
72 base::TimeTicks timebase_
;
73 base::TimeDelta interval_
;
75 // Each display link instance consumes a non-negligible number of cycles, so
76 // make all display links on the same screen share the same object.
77 typedef std::map
<CGDirectDisplayID
, DisplayLinkMac
*> DisplayMap
;
78 static base::LazyInstance
<DisplayMap
> display_map_
;
83 #endif // UI_ACCELERATED_WIDGET_MAC_DISPLAY_LINK_MAC_H_