Remove valgrind warning of VSyncMonitor
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / internal / common / vsync-monitor.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 // EXTERNAL INCLUDES
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <vconf.h>
24 #include <vconf-keys.h>
25
26 #include <dali/integration-api/debug.h>
27
28 // INTERNAL INCLUDES
29 #include "vsync-monitor.h"
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace Adaptor
38 {
39
40 namespace
41 {
42
43 #if defined(DEBUG_ENABLED)
44 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VSYNC_MONITOR");
45 #endif
46
47 const char * const DRM_DEVICE( "/dev/dri/card0" );
48 const int FD_NONE( -1 );
49
50 void ScreenStatusChanged(keynode_t* node, void* data)
51 {
52   VSyncMonitor* vsyncMonitor( static_cast< VSyncMonitor* >( data ) );
53
54   int status = 0;
55   vconf_get_int( VCONFKEY_PM_STATE, &status );
56
57   // status values
58   //  - VCONFKEY_PM_STATE_NORMAL : turn vsync on
59   //  - VCONFKEY_PM_STATE_LCDDIM : turn vsync off
60   //  - VCONFKEY_PM_STATE_LCDOFF : turn vsync off
61   //  - VCONFKEY_PM_STATE_SLEEP : turn vsync off
62   const bool screenOn( VCONFKEY_PM_STATE_NORMAL == status );
63
64   vsyncMonitor->SetUseHardware( screenOn );
65
66   DALI_LOG_INFO( gLogFilter, Debug::Concise, "%s, Screen %s.\n", __PRETTY_FUNCTION__, screenOn ? "On" : "Off" );
67 }
68
69 } // unnamed namespace
70
71 VSyncMonitor::VSyncMonitor()
72 : mFileDescriptor( FD_NONE ),
73   mUseHardware( true )
74 {
75   vconf_notify_key_changed( VCONFKEY_PM_STATE, ScreenStatusChanged, this );
76 }
77
78 VSyncMonitor::~VSyncMonitor()
79 {
80   Terminate();
81
82   vconf_ignore_key_changed( VCONFKEY_PM_STATE, ScreenStatusChanged );
83 }
84
85 void VSyncMonitor::SetUseHardware( bool useHardware )
86 {
87   mUseHardware = useHardware;
88 }
89
90 void VSyncMonitor::Initialize()
91 {
92   DALI_ASSERT_DEBUG( mFileDescriptor == FD_NONE && "VSyncMonitor::Initialize() called twice" );
93
94   // Read initial 'use hardware' status
95   ScreenStatusChanged( NULL, this );
96
97   // open /dev node
98   mFileDescriptor = open( DRM_DEVICE, O_RDWR );
99
100   // setup vblank request - block and wait for next vblank
101   mVBlankInfo.request.type = DRM_VBLANK_NEXTONMISS;
102   mVBlankInfo.request.sequence = 0;
103   mVBlankInfo.request.signal = 0;
104
105   // setup vblank reply - block and wait for next vblank
106   mVBlankInfo.reply.type = DRM_VBLANK_NEXTONMISS;
107   mVBlankInfo.reply.sequence = 0;
108   mVBlankInfo.reply.tval_sec = 0;
109   mVBlankInfo.reply.tval_usec = 0;
110 }
111
112 void VSyncMonitor::Terminate()
113 {
114   if( mFileDescriptor != FD_NONE )
115   {
116     close( mFileDescriptor );
117     mFileDescriptor = FD_NONE;
118   }
119 }
120
121 bool VSyncMonitor::UseHardware()
122 {
123   return mUseHardware && (FD_NONE != mFileDescriptor );
124 }
125
126
127 bool VSyncMonitor::DoSync( unsigned int& frameNumber, unsigned int& seconds, unsigned int& microseconds )
128 {
129   DALI_ASSERT_DEBUG( mFileDescriptor != FD_NONE && "ECoreX::VSyncMonitor is not initialized" );
130
131   if( 0 == drmWaitVBlank( mFileDescriptor, &mVBlankInfo ) )
132   {
133     frameNumber = mVBlankInfo.reply.sequence;
134     seconds = mVBlankInfo.reply.tval_sec;
135     microseconds = mVBlankInfo.reply.tval_usec;
136
137     return true;
138   }
139
140   return false;
141 }
142
143 } // namespace Adaptor
144
145 } // namespace Internal
146
147 } // namespace Dali