2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <sys/types.h>
24 #include <vconf-keys.h>
26 #include <dali/integration-api/debug.h>
29 #include "vsync-monitor.h"
43 #if defined(DEBUG_ENABLED)
44 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VSYNC_MONITOR");
47 const char * const DRM_DEVICE( "/dev/dri/card0" );
48 const int FD_NONE( -1 );
50 void ScreenStatusChanged(keynode_t* node, void* data)
52 VSyncMonitor* vsyncMonitor( static_cast< VSyncMonitor* >( data ) );
55 vconf_get_int( VCONFKEY_PM_STATE, &status );
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 );
64 vsyncMonitor->SetHardwareVSyncAvailable( screenOn );
66 DALI_LOG_INFO( gLogFilter, Debug::Concise, "%s, Screen %s.\n", __PRETTY_FUNCTION__, screenOn ? "On" : "Off" );
69 } // unnamed namespace
71 VSyncMonitor::VSyncMonitor()
72 : mFileDescriptor( FD_NONE ),
73 mUseHardwareVSync( true ),
74 mHardwareVSyncAvailable( false )
76 vconf_notify_key_changed( VCONFKEY_PM_STATE, ScreenStatusChanged, this );
79 VSyncMonitor::~VSyncMonitor()
83 vconf_ignore_key_changed( VCONFKEY_PM_STATE, ScreenStatusChanged );
86 void VSyncMonitor::SetUseHardwareVSync( bool useHardware )
88 mUseHardwareVSync = useHardware;
91 void VSyncMonitor::SetHardwareVSyncAvailable( bool hardwareVSyncAvailable )
93 mHardwareVSyncAvailable = hardwareVSyncAvailable;
96 void VSyncMonitor::Initialize()
98 DALI_ASSERT_DEBUG( mFileDescriptor == FD_NONE && "VSyncMonitor::Initialize() called twice" );
100 // Read initial 'use hardware' status
101 ScreenStatusChanged( NULL, this );
104 mFileDescriptor = open( DRM_DEVICE, O_RDWR );
106 // setup vblank request - block and wait for next vblank
107 mVBlankInfo.request.type = DRM_VBLANK_NEXTONMISS;
108 mVBlankInfo.request.sequence = 0;
109 mVBlankInfo.request.signal = 0;
111 // setup vblank reply - block and wait for next vblank
112 mVBlankInfo.reply.type = DRM_VBLANK_NEXTONMISS;
113 mVBlankInfo.reply.sequence = 0;
114 mVBlankInfo.reply.tval_sec = 0;
115 mVBlankInfo.reply.tval_usec = 0;
118 void VSyncMonitor::Terminate()
120 if( mFileDescriptor != FD_NONE )
122 close( mFileDescriptor );
123 mFileDescriptor = FD_NONE;
127 bool VSyncMonitor::UseHardware()
129 return mUseHardwareVSync && mHardwareVSyncAvailable && (FD_NONE != mFileDescriptor );
132 bool VSyncMonitor::DoSync( unsigned int& frameNumber, unsigned int& seconds, unsigned int& microseconds )
134 DALI_ASSERT_DEBUG( mFileDescriptor != FD_NONE && "ECoreX::VSyncMonitor is not initialized" );
136 if( 0 == drmWaitVBlank( mFileDescriptor, &mVBlankInfo ) )
138 frameNumber = mVBlankInfo.reply.sequence;
139 seconds = mVBlankInfo.reply.tval_sec;
140 microseconds = mVBlankInfo.reply.tval_usec;
148 } // namespace Adaptor
150 } // namespace Internal