Redesigning render sync to handle reduced frequency
[platform/core/uifw/dali-adaptor.git] / adaptors / ubuntu / vsync-monitor-ubuntu.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "vsync-monitor.h"
20
21 // EXTERNAL INCLUDES
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26
27 #include <dali/integration-api/debug.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 namespace Adaptor
36 {
37
38 namespace
39 {
40
41 #if defined(DEBUG_ENABLED)
42 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VSYNC_MONITOR");
43 #endif
44
45 const int FD_NONE( -1 );
46
47 } // unnamed namespace
48
49 VSyncMonitor::VSyncMonitor()
50 : mFileDescriptor( FD_NONE ),
51   mUseHardwareVSync( false ),
52   mHardwareVsyncAvailable( false )
53 {
54 }
55
56 VSyncMonitor::~VSyncMonitor()
57 {
58   Terminate();
59 }
60
61 void VSyncMonitor::SetUseHardwareVsync( bool useHardware )
62 {
63   mUseHardwareVSync = useHardware;
64 }
65
66 void VSyncMonitor::SetHardwareVSyncAvailable( bool hardwareVSyncAvailable )
67 {
68   mHardwareVsyncAvailable = hardwareVSyncAvailable;
69 }
70
71 void VSyncMonitor::Initialize()
72 {
73   DALI_ASSERT_DEBUG( mFileDescriptor == FD_NONE && "VSyncMonitor::Initialize() called twice" );
74
75   // setup vblank request - block and wait for next vblank
76   mVBlankInfo.request.type = DRM_VBLANK_NEXTONMISS;
77   mVBlankInfo.request.sequence = 0;
78   mVBlankInfo.request.signal = 0;
79
80   // setup vblank reply - block and wait for next vblank
81   mVBlankInfo.reply.type = DRM_VBLANK_NEXTONMISS;
82   mVBlankInfo.reply.sequence = 0;
83   mVBlankInfo.reply.tval_sec = 0;
84   mVBlankInfo.reply.tval_usec = 0;
85 }
86
87 void VSyncMonitor::Terminate()
88 {
89 }
90
91 bool VSyncMonitor::UseHardware()
92 {
93   return mUseHardware && (FD_NONE != mFileDescriptor );
94 }
95
96
97 bool VSyncMonitor::DoSync( unsigned int& frameNumber, unsigned int& seconds, unsigned int& microseconds )
98 {
99   DALI_ASSERT_DEBUG( mFileDescriptor != FD_NONE && "ECoreX::VSyncMonitor is not initialized" );
100
101   if( 0 == drmWaitVBlank( mFileDescriptor, &mVBlankInfo ) )
102   {
103     frameNumber = mVBlankInfo.reply.sequence;
104     seconds = mVBlankInfo.reply.tval_sec;
105     microseconds = mVBlankInfo.reply.tval_usec;
106
107     return true;
108   }
109
110   return false;
111 }
112
113 } // namespace Adaptor
114
115 } // namespace Internal
116
117 } // namespace Dali