2f89812168674a2081f20a9558724f1aa34263b9
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / vsync-monitor-tizen.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 // constants to keep code readability with unsigned int has to be used as boolean (due to multithreading)
43 const unsigned int TRUE = 1u;
44 const unsigned int FALSE = 0u;
45
46 #if defined(DEBUG_ENABLED)
47 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VSYNC_MONITOR");
48 #endif
49
50 const char * const DRM_DEVICE( "/dev/dri/card0" );
51 const int FD_NONE( -1 );
52
53 void ScreenStatusChanged(keynode_t* node, void* data)
54 {
55   VSyncMonitor* vsyncMonitor( static_cast< VSyncMonitor* >( data ) );
56
57   int status = 0;
58   vconf_get_int( VCONFKEY_PM_STATE, &status );
59
60   // status values
61   //  - VCONFKEY_PM_STATE_NORMAL : turn vsync on
62   //  - VCONFKEY_PM_STATE_LCDDIM : turn vsync off
63   //  - VCONFKEY_PM_STATE_LCDOFF : turn vsync off
64   //  - VCONFKEY_PM_STATE_SLEEP : turn vsync off
65   const unsigned int screenOn( VCONFKEY_PM_STATE_NORMAL == status );
66
67   vsyncMonitor->SetHardwareVSyncAvailable( screenOn );
68
69   DALI_LOG_INFO( gLogFilter, Debug::Concise, "%s, Screen %s.\n", __PRETTY_FUNCTION__, screenOn ? "On" : "Off" );
70 }
71
72 } // unnamed namespace
73
74 VSyncMonitor::VSyncMonitor()
75 : mFileDescriptor( FD_NONE ),
76   mUseHardwareVSync( TRUE ),
77   mHardwareVSyncAvailable( FALSE )
78 {
79   vconf_notify_key_changed( VCONFKEY_PM_STATE, ScreenStatusChanged, this );
80 }
81
82 VSyncMonitor::~VSyncMonitor()
83 {
84   Terminate();
85
86   vconf_ignore_key_changed( VCONFKEY_PM_STATE, ScreenStatusChanged );
87 }
88
89 void VSyncMonitor::SetUseHardwareVSync( bool useHardware )
90 {
91   mUseHardwareVSync = useHardware;
92 }
93
94 void VSyncMonitor::SetHardwareVSyncAvailable( bool hardwareVSyncAvailable )
95 {
96   mHardwareVSyncAvailable = hardwareVSyncAvailable;
97 }
98
99 void VSyncMonitor::Initialize()
100 {
101   DALI_ASSERT_DEBUG( mFileDescriptor == FD_NONE && "VSyncMonitor::Initialize() called twice" );
102
103   // Read initial 'use hardware' status
104   ScreenStatusChanged( NULL, this );
105
106   // open /dev node
107   mFileDescriptor = open( DRM_DEVICE, O_RDWR );
108
109   // setup vblank request - block and wait for next vblank
110   mVBlankInfo.request.type = DRM_VBLANK_NEXTONMISS;
111   mVBlankInfo.request.sequence = 0;
112   mVBlankInfo.request.signal = 0;
113
114   // setup vblank reply - block and wait for next vblank
115   mVBlankInfo.reply.type = DRM_VBLANK_NEXTONMISS;
116   mVBlankInfo.reply.sequence = 0;
117   mVBlankInfo.reply.tval_sec = 0;
118   mVBlankInfo.reply.tval_usec = 0;
119 }
120
121 void VSyncMonitor::Terminate()
122 {
123   if( mFileDescriptor != FD_NONE )
124   {
125     close( mFileDescriptor );
126     mFileDescriptor = FD_NONE;
127   }
128 }
129
130 bool VSyncMonitor::UseHardware()
131 {
132   return mUseHardwareVSync && mHardwareVSyncAvailable && (FD_NONE != mFileDescriptor );
133 }
134
135 bool VSyncMonitor::DoSync( unsigned int& frameNumber, unsigned int& seconds, unsigned int& microseconds )
136 {
137   DALI_ASSERT_DEBUG( mFileDescriptor != FD_NONE && "ECoreX::VSyncMonitor is not initialized" );
138
139   if( 0 == drmWaitVBlank( mFileDescriptor, &mVBlankInfo ) )
140   {
141     frameNumber = mVBlankInfo.reply.sequence;
142     seconds = mVBlankInfo.reply.tval_sec;
143     microseconds = mVBlankInfo.reply.tval_usec;
144
145     return true;
146   }
147
148   return false;
149 }
150
151 } // namespace Adaptor
152
153 } // namespace Internal
154
155 } // namespace Dali