Support a frame rendered / presented callback
[platform/core/uifw/dali-adaptor-legacy.git] / dali / internal / system / android / file-descriptor-monitor-android.cpp
1 /*
2  * Copyright (c) 2017 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 <dali/internal/system/common/file-descriptor-monitor.h>
20
21 // EXTERNAL INCLUDES
22 #include <looper.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/integration-api/debug.h>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 namespace Adaptor
34 {
35
36 /**
37  * Using Impl to hide away Android specific members
38  */
39 struct FileDescriptorMonitor::Impl
40 {
41   // Construction
42   Impl( int fileDescriptor, CallbackBase* callback, int eventBitmask )
43   : mCallback( callback ),
44     mFileDescriptor( fileDescriptor ),
45     mEventsToMonitor( eventBitmask )
46   {
47   }
48
49   ~Impl()
50   {
51     delete mCallback;
52   }
53
54   // Data
55   CallbackBase* mCallback;
56   int mFileDescriptor;
57   int mEventsToMonitor;
58
59   // Static Methods
60
61   /**
62    * Called when the file descriptor receives an event.
63    */
64   static int EventDispatch( int fd, int events, void* data )
65   {
66     Impl* impl = reinterpret_cast<Impl*>(data);
67
68     // if we want read events, check to see if a read event is available
69     int type = FileDescriptorMonitor::FD_NO_EVENT;
70
71     if( impl->mEventsToMonitor & ALOOPER_EVENT_INPUT )
72     {
73
74       type = FileDescriptorMonitor::FD_READABLE;
75
76     }
77     // check if we want write events
78     if( impl->mEventsToMonitor & ALOOPER_EVENT_OUTPUT )
79     {
80
81       type |= FileDescriptorMonitor::FD_WRITABLE;
82
83     }
84
85     // if there is an event, execute the callback
86     if( type != FileDescriptorMonitor::FD_NO_EVENT )
87     {
88       CallbackBase::Execute( *impl->mCallback, static_cast< FileDescriptorMonitor::EventType >( type ), impl->mFileDescriptor );
89     }
90
91     return 1; // Continue receiving callbacks
92   }
93 };
94
95 FileDescriptorMonitor::FileDescriptorMonitor( int fileDescriptor, CallbackBase* callback, int eventBitmask )
96 {
97   mImpl = new Impl(fileDescriptor, callback, eventBitmask);
98
99   if (fileDescriptor >= 0)
100   {
101     int events = 0;
102     if( eventBitmask & FD_READABLE)
103     {
104       events = ALOOPER_EVENT_INPUT;
105     }
106     if( eventBitmask & FD_WRITABLE)
107     {
108       events |= ALOOPER_EVENT_OUTPUT;
109     }
110
111     mImpl->mEventsToMonitor = events;
112
113     ALooper* looper = ALooper_forThread();
114     if( looper )
115     {
116       ALooper_addFd( looper, fileDescriptor, ALOOPER_POLL_CALLBACK, events, &Impl::EventDispatch, mImpl );
117     }
118   }
119 }
120
121 FileDescriptorMonitor::~FileDescriptorMonitor()
122 {
123   if( mImpl->mFileDescriptor )
124   {
125     ALooper* looper = ALooper_forThread();
126     if( looper )
127     {
128       ALooper_removeFd( looper, mImpl->mFileDescriptor );
129     }
130   }
131
132   delete mImpl;
133   mImpl = NULL;
134 }
135
136 } // namespace Adaptor
137
138 } // namespace Internal
139
140 } // namespace Dali
141