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"
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;
46 #if defined(DEBUG_ENABLED)
47 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VSYNC_MONITOR");
50 const char * const DRM_DEVICE( "/dev/dri/card0" );
51 const int FD_NONE( -1 );
53 void ScreenStatusChanged(keynode_t* node, void* data)
55 VSyncMonitor* vsyncMonitor( static_cast< VSyncMonitor* >( data ) );
58 vconf_get_int( VCONFKEY_PM_STATE, &status );
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 );
67 vsyncMonitor->SetHardwareVSyncAvailable( screenOn );
69 DALI_LOG_INFO( gLogFilter, Debug::Concise, "%s, Screen %s.\n", __PRETTY_FUNCTION__, screenOn ? "On" : "Off" );
72 } // unnamed namespace
74 VSyncMonitor::VSyncMonitor()
75 : mFileDescriptor( FD_NONE ),
76 mUseHardwareVSync( TRUE ),
77 mHardwareVSyncAvailable( FALSE )
79 vconf_notify_key_changed( VCONFKEY_PM_STATE, ScreenStatusChanged, this );
82 VSyncMonitor::~VSyncMonitor()
86 vconf_ignore_key_changed( VCONFKEY_PM_STATE, ScreenStatusChanged );
89 void VSyncMonitor::SetUseHardwareVSync( bool useHardware )
91 mUseHardwareVSync = useHardware;
94 void VSyncMonitor::SetHardwareVSyncAvailable( bool hardwareVSyncAvailable )
96 mHardwareVSyncAvailable = hardwareVSyncAvailable;
99 void VSyncMonitor::Initialize()
101 DALI_ASSERT_DEBUG( mFileDescriptor == FD_NONE && "VSyncMonitor::Initialize() called twice" );
103 // Read initial 'use hardware' status
104 ScreenStatusChanged( NULL, this );
107 mFileDescriptor = open( DRM_DEVICE, O_RDWR );
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;
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;
121 void VSyncMonitor::Terminate()
123 if( mFileDescriptor != FD_NONE )
125 close( mFileDescriptor );
126 mFileDescriptor = FD_NONE;
130 bool VSyncMonitor::UseHardware()
132 return mUseHardwareVSync && mHardwareVSyncAvailable && (FD_NONE != mFileDescriptor );
135 bool VSyncMonitor::DoSync( unsigned int& frameNumber, unsigned int& seconds, unsigned int& microseconds )
137 DALI_ASSERT_DEBUG( mFileDescriptor != FD_NONE && "ECoreX::VSyncMonitor is not initialized" );
139 if( 0 == drmWaitVBlank( mFileDescriptor, &mVBlankInfo ) )
141 frameNumber = mVBlankInfo.reply.sequence;
142 seconds = mVBlankInfo.reply.tval_sec;
143 microseconds = mVBlankInfo.reply.tval_usec;
151 } // namespace Adaptor
153 } // namespace Internal