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. 4 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_DISPLAY_LINK_MAC_H_ 6 #define CONTENT_BROWSER_RENDERER_HOST_DISPLAY_LINK_MAC_H_ 7 8 #include <QuartzCore/CVDisplayLink.h> 9 #include <map> 10 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 18 namespace content { 19 20 class DisplayLinkMac : public base::RefCounted<DisplayLinkMac> { 21 public: 22 static scoped_refptr<DisplayLinkMac> GetForDisplay( 23 CGDirectDisplayID display_id); 24 25 // Get vsync scheduling parameters. 26 bool GetVSyncParameters( 27 base::TimeTicks* timebase, 28 base::TimeDelta* interval); 29 30 private: 31 friend class base::RefCounted<DisplayLinkMac>; 32 33 DisplayLinkMac( 34 CGDirectDisplayID display_id, 35 base::ScopedTypeRef<CVDisplayLinkRef> display_link); 36 virtual ~DisplayLinkMac(); 37 38 void StartOrContinueDisplayLink(); 39 void StopDisplayLink(); 40 void Tick(const CVTimeStamp* time); 41 42 static CVReturn DisplayLinkCallback( 43 CVDisplayLinkRef display_link, 44 const CVTimeStamp* now, 45 const CVTimeStamp* output_time, 46 CVOptionFlags flags_in, 47 CVOptionFlags* flags_out, 48 void* context); 49 50 // The display that this display link is attached to. 51 CGDirectDisplayID display_id_; 52 53 // CVDisplayLink for querying VSync timing info. 54 base::ScopedTypeRef<CVDisplayLinkRef> display_link_; 55 56 // Timer for stopping the display link if it has not been queried in 57 // the last second. 58 base::DelayTimer<DisplayLinkMac> stop_timer_; 59 60 // VSync parameters computed during Tick. 61 bool timebase_and_interval_valid_; 62 base::TimeTicks timebase_; 63 base::TimeDelta interval_; 64 65 // Lock for sharing data between UI thread and display-link thread. 66 base::Lock lock_; 67 68 // Each display link instance consumes a non-negligible number of cycles, so 69 // make all display links on the same screen share the same object. 70 typedef std::map<CGDirectDisplayID, DisplayLinkMac*> DisplayMap; 71 static base::LazyInstance<DisplayMap> display_map_; 72 }; 73 74 } // content 75 76 #endif // CONTENT_BROWSER_RENDERER_HOST_DISPLAY_LINK_MAC_H_ 77