home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / h / vd2 / system / time.h < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  4.3 KB  |  119 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    System library component
  3. //    Copyright (C) 1998-2004 Avery Lee, All Rights Reserved.
  4. //
  5. //    Beginning with 1.6.0, the VirtualDub system library is licensed
  6. //    differently than the remainder of VirtualDub.  This particular file is
  7. //    thus licensed as follows (the "zlib" license):
  8. //
  9. //    This software is provided 'as-is', without any express or implied
  10. //    warranty.  In no event will the authors be held liable for any
  11. //    damages arising from the use of this software.
  12. //
  13. //    Permission is granted to anyone to use this software for any purpose,
  14. //    including commercial applications, and to alter it and redistribute it
  15. //    freely, subject to the following restrictions:
  16. //
  17. //    1.    The origin of this software must not be misrepresented; you must
  18. //        not claim that you wrote the original software. If you use this
  19. //        software in a product, an acknowledgment in the product
  20. //        documentation would be appreciated but is not required.
  21. //    2.    Altered source versions must be plainly marked as such, and must
  22. //        not be misrepresented as being the original software.
  23. //    3.    This notice may not be removed or altered from any source
  24. //        distribution.
  25.  
  26. #ifndef f_VD2_SYSTEM_TIME_H
  27. #define f_VD2_SYSTEM_TIME_H
  28.  
  29. #include <vd2/system/vdtypes.h>
  30. #include <vd2/system/atomic.h>
  31. #include <vd2/system/thread.h>
  32. #include <vd2/system/win32/miniwindows.h>
  33.  
  34. class VDFunctionThunk;
  35.  
  36. // VDGetCurrentTick: Retrieve current process timer, in milliseconds.  Should only
  37. // be used for sparsing updates/checks, and not for precision timing.  Approximate
  38. // resolution is 55ms under Win9x and 10-15ms under WinNT. The advantage of this
  39. // call is that it is usually extremely fast (just reading from the PEB).
  40. uint32 VDGetCurrentTick();
  41.  
  42. // VDGetPreciseTick: Retrieves high-performance timer (QueryPerformanceCounter in
  43. // Win32). This is very precise, often <1us, but often suffers from various bugs.
  44. // that make it undesirable for high-accuracy requirements. On x64 Windows it
  45. // can run at 1/2 speed when CPU throttling is enabled, and on some older buggy
  46. // chipsets it can skip around occasionally.
  47. uint64 VDGetPreciseTick();
  48. uint64 VDGetPreciseTicksPerSecondI();
  49. double VDGetPreciseTicksPerSecond();
  50. double VDGetPreciseSecondsPerTick();
  51.  
  52. // VDGetAccurateTick: Reads a timer with good precision and accuracy, in
  53. // milliseconds. On Win9x, it has 1ms precision; on WinNT, it may have anywhere
  54. // from 1ms to 10-15ms, although 1ms can be forced with timeBeginPeriod().
  55. uint32 VDGetAccurateTick();
  56.  
  57. // VDCallbackTimer is an abstraction of the Windows multimedia timer.  As such, it
  58. // is rather expensive to instantiate, and should only be used for critical timing
  59. // needs... such as multimedia.  Basically, there should only really be one or two
  60. // of these running.  Win32 typically implements these as separate threads
  61. // triggered off a timer, so despite the outdated documentation -- which still hasn't
  62. // been updated from Windows 3.1 -- you can call almost any function from the
  63. // callback.  Execution time in the callback delays other timers, however, so the
  64. // callback should still execute as quickly as possible.
  65.  
  66. class VDINTERFACE IVDTimerCallback {
  67. public:
  68.     virtual void TimerCallback() = 0;
  69. };
  70.  
  71. class VDCallbackTimer : private VDThread {
  72. public:
  73.     VDCallbackTimer();
  74.     ~VDCallbackTimer();
  75.  
  76.     bool Init(IVDTimerCallback *pCB, uint32 period_ms);
  77.     bool Init2(IVDTimerCallback *pCB, uint32 period_100ns);
  78.     bool Init3(IVDTimerCallback *pCB, uint32 period_100ns, uint32 accuracy_100ns, bool precise);
  79.     void Shutdown();
  80.  
  81.     void SetRateDelta(int delta_100ns);
  82.     void AdjustRate(int adjustment_100ns);
  83.  
  84.     bool IsTimerRunning() const;
  85.  
  86. private:
  87.     void ThreadRun();
  88.  
  89.     IVDTimerCallback *mpCB;
  90.     unsigned        mTimerAccuracy;
  91.     uint32            mTimerPeriod;
  92.     VDAtomicInt        mTimerPeriodDelta;
  93.     VDAtomicInt        mTimerPeriodAdjustment;
  94.  
  95.     VDSignal        msigExit;
  96.  
  97.     volatile bool    mbExit;                // this doesn't really need to be atomic -- think about it
  98.     bool            mbPrecise;
  99. };
  100.  
  101.  
  102. class VDLazyTimer {
  103. public:
  104.     VDLazyTimer();
  105.     ~VDLazyTimer();
  106.  
  107.     void SetOneShot(IVDTimerCallback *pCB, uint32 delay);
  108.     void Stop();
  109.  
  110. protected:
  111.     void StaticTimeCallback(VDZHWND hwnd, VDZUINT msg, VDZUINT_PTR id, VDZDWORD time);
  112.  
  113.     uint32                mTimerId;
  114.     VDFunctionThunk        *mpThunk;
  115.     IVDTimerCallback    *mpCB;
  116. };
  117.  
  118. #endif
  119.