1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5  */
6 
7 #ifndef GFX_SOFTWARE_VSYNC_SOURCE_H
8 #define GFX_SOFTWARE_VSYNC_SOURCE_H
9 
10 #include "mozilla/Monitor.h"
11 #include "mozilla/RefPtr.h"
12 #include "mozilla/TimeStamp.h"
13 #include "base/thread.h"
14 #include "nsISupportsImpl.h"
15 #include "VsyncSource.h"
16 
17 class SoftwareDisplay final : public mozilla::gfx::VsyncSource::Display
18 {
19   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SoftwareDisplay)
20 
21 public:
22   SoftwareDisplay();
23   virtual void EnableVsync() override;
24   virtual void DisableVsync() override;
25   virtual bool IsVsyncEnabled() override;
26   bool IsInSoftwareVsyncThread();
27   virtual void NotifyVsync(mozilla::TimeStamp aVsyncTimestamp) override;
28   virtual mozilla::TimeDuration GetVsyncRate() override;
29   void ScheduleNextVsync(mozilla::TimeStamp aVsyncTimestamp);
30   void Shutdown() override;
31 
32 protected:
33   ~SoftwareDisplay();
34 
35 private:
36   mozilla::TimeDuration mVsyncRate;
37   // Use a chromium thread because nsITimers* fire on the main thread
38   base::Thread* mVsyncThread;
39   RefPtr<mozilla::CancelableRunnable> mCurrentVsyncTask; // only access on vsync thread
40   bool mVsyncEnabled; // Only access on main thread
41 }; // SoftwareDisplay
42 
43 // Fallback option to use a software timer to mimic vsync. Useful for gtests
44 // To mimic a hardware vsync thread, we create a dedicated software timer
45 // vsync thread.
46 class SoftwareVsyncSource : public mozilla::gfx::VsyncSource
47 {
48 public:
49   SoftwareVsyncSource();
50   ~SoftwareVsyncSource();
51 
GetGlobalDisplay()52   virtual Display& GetGlobalDisplay() override
53   {
54     MOZ_ASSERT(mGlobalDisplay != nullptr);
55     return *mGlobalDisplay;
56   }
57 
58 private:
59   RefPtr<SoftwareDisplay> mGlobalDisplay;
60 };
61 
62 #endif /* GFX_SOFTWARE_VSYNC_SOURCE_H */
63