2 * Copyright (c) 2016 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 <pepper-dali/internal/object-impl.h>
22 #include <pepper-dali/internal/shell-client-impl.h>
23 #include <pepper-dali/internal/object-view-impl.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/public-api/images/buffer-image.h>
28 #include <dali/public-api/events/touch-event.h>
29 #include <dali/public-api/adaptor-framework/native-image-source.h>
30 #include <wayland-tbm-server.h>
44 #if defined(DEBUG_ENABLED)
45 Integration::Log::Filter* gPepperObjectLogging = Integration::Log::Filter::New( Debug::Verbose, false, "LOG_PEPPER_OBJECT" );
48 } // unnamed namespace
50 ObjectPtr Object::New( pepper_surface_t* surface, Pepper::Input input )
52 ObjectPtr impl = new Object();
54 // Second-phase init of the implementation
55 impl->Initialize( surface, input );
68 mSurfaceDestroyListener( NULL ),
69 mBufferDestroyListener( NULL ),
79 pepper_buffer_unreference( mBuffer );
80 pepper_event_listener_remove( mBufferDestroyListener );
83 if( mSurfaceDestroyListener )
85 pepper_event_listener_remove( mSurfaceDestroyListener );
89 void Object::Initialize( pepper_surface_t* surface, Pepper::Input input )
93 mPointer = input.GetPointer();
94 mKeyboard = input.GetKeyboard();
95 mTouch = input.GetTouch();
97 mSurfaceDestroyListener = pepper_object_add_event_listener( (pepper_object_t*)surface, PEPPER_EVENT_OBJECT_DESTROY, 0, Object::OnDestroySurface, this );
99 DALI_LOG_INFO( gPepperObjectLogging, Debug::Verbose, "Object::Initialize: success\n" );
102 bool Object::AttchBuffer( int* width, int* height )
104 pepper_buffer_t* buffer;
105 wl_resource* bufferResource;
106 wl_shm_buffer* shmBuffer;
107 tbm_surface_h tbmSurface;
108 int bufWidth, bufHeight;
110 buffer = pepper_surface_get_buffer( mSurface );
113 DALI_LOG_INFO( gPepperObjectLogging, Debug::General, "Object::AttchBuffer: pepper_surface_get_buffer is failed\n" );
119 pepper_buffer_unreference( mBuffer );
120 pepper_event_listener_remove( mBufferDestroyListener );
123 pepper_buffer_reference( buffer );
126 mBufferDestroyListener = pepper_object_add_event_listener( (pepper_object_t*)buffer, PEPPER_EVENT_OBJECT_DESTROY, 0, Object::OnDestroyBuffer, this );
128 bufferResource = pepper_buffer_get_resource( buffer );
130 shmBuffer = wl_shm_buffer_get( bufferResource );
133 bufWidth = wl_shm_buffer_get_width( shmBuffer );
134 bufHeight = wl_shm_buffer_get_height( shmBuffer );
138 tbmSurface = wayland_tbm_server_get_surface( NULL, bufferResource );
141 bufWidth = tbm_surface_get_width( tbmSurface );
142 bufHeight = tbm_surface_get_height( tbmSurface );
146 DALI_LOG_INFO( gPepperObjectLogging, Debug::General, "Object::AttchBuffer: Failed to get buffer\n" );
153 mObjectView = Pepper::ObjectView::New();
155 mObjectView.TouchedSignal().Connect( this, &Object::OnTouched );
158 Pepper::GetImplementation( mObjectView ).SetSurface( mSurface );
160 // TODO: support multi touch, focus in/out, mouse in/out
162 // TODO: resize callback
164 Pepper::Object handle( this );
165 mObjectViewAddedSignal.Emit( handle, mObjectView );
168 if( shmBuffer != mShmBuffer )
170 // create a new image
171 PixelBuffer* pixelBuffer = static_cast< PixelBuffer* >( wl_shm_buffer_get_data( shmBuffer ) );
172 mBufferImage = BufferImage::New( pixelBuffer, bufWidth, bufHeight );
174 mObjectView.SetImage( mBufferImage );
176 mShmBuffer = shmBuffer;
178 else if ( tbmSurface != mTbmSurface )
180 NativeImageSourcePtr nativeImageSource = NativeImageSource::New( tbmSurface );
181 mNativeImage = NativeImage::New( *nativeImageSource );
183 mObjectView.SetImage( mNativeImage );
185 mTbmSurface = tbmSurface;
188 if( ( mWidth != bufWidth ) || ( mHeight != bufHeight ) )
194 mObjectView.SetSize( mWidth, mHeight );
207 DALI_LOG_INFO( gPepperObjectLogging, Debug::Verbose, "Object::AttchBuffer: this = %p, width = %d, height = %d", this, bufWidth, bufHeight );
212 Pepper::Object::ObjectSignalType& Object::ObjectViewAddedSignal()
214 return mObjectViewAddedSignal;
217 Pepper::Object::ObjectSignalType& Object::ObjectViewDeletedSignal()
219 return mObjectViewDeletedSignal;
222 bool Object::OnTouched( Actor actor, const TouchEvent& touchEvent )
224 const TouchPoint& point = touchEvent.GetPoint(0);
226 Pepper::Internal::ShellClientPtr shellClient = reinterpret_cast< Pepper::Internal::ShellClient* >( pepper_object_get_user_data( reinterpret_cast< pepper_object_t* >( mSurface ), pepper_surface_get_role( mSurface ) ) );
229 DALI_LOG_INFO( gPepperObjectLogging, Debug::General, "Object::OnTouched: shellClient is null. mSurface = %p\n", mSurface );
233 switch( point.state )
235 case TouchPoint::Down:
237 pepper_touch_add_point( mTouch, point.deviceId, point.local.x, point.local.y );
238 pepper_touch_send_down( mTouch, shellClient->GetView(), touchEvent.time, point.deviceId, point.local.x, point.local.y );
240 DALI_LOG_INFO( gPepperObjectLogging, Debug::Verbose, "Object::OnTouched: Touch Down (%.2f, %.2f) device = %d surface = %p\n", point.local.x, point.local.y, point.deviceId, mSurface );
245 pepper_touch_send_up( mTouch, shellClient->GetView(), touchEvent.time, point.deviceId );
246 pepper_touch_remove_point( mTouch, point.deviceId );
248 DALI_LOG_INFO( gPepperObjectLogging, Debug::Verbose, "Object::OnTouched: Touch Up (%.2f, %.2f) surface = %p\n", point.local.x, point.local.y, mSurface );
251 case TouchPoint::Motion:
253 pepper_touch_send_motion( mTouch, shellClient->GetView(), touchEvent.time, point.deviceId, point.local.x, point.local.y );
255 // DALI_LOG_INFO( gPepperObjectLogging, Debug::Verbose, "Object::OnTouched: Touch Motion (%.2f, %.2f)\n", point.local.x, point.local.y );
267 void Object::OnDestroySurface( pepper_event_listener_t* listener, pepper_object_t* pepperObject, uint32_t id, void* info, void* data )
269 Object* object = static_cast< Object* >( data );
271 Pepper::Object handle( object );
272 object->mObjectViewDeletedSignal.Emit( handle, object->mObjectView );
274 object->mSurface = NULL;
276 if( object->mSurfaceDestroyListener )
278 pepper_event_listener_remove( object->mSurfaceDestroyListener );
282 void Object::OnDestroyBuffer( pepper_event_listener_t* listener, pepper_object_t* pepperObject, uint32_t id, void* info, void* data )
284 Object* object = static_cast< Object* >( data );
286 if( object->mBuffer )
288 pepper_buffer_unreference( object->mBuffer );
289 pepper_event_listener_remove( object->mBufferDestroyListener );
292 if( object->mShmBuffer )
294 void* shmData = wl_shm_buffer_get_data( object->mShmBuffer );
296 object->mBufferImage = BufferImage::New( object->mWidth, object->mHeight );
298 PixelBuffer* buffer = object->mBufferImage.GetBuffer();
299 memcpy(buffer, shmData, object->mWidth * object->mHeight * 4);
301 object->mBufferImage.Update();
303 object->mObjectView.SetImage( object->mBufferImage );
305 else if( object->mTbmSurface )
310 } // namespace Internal
312 } // namespace Pepper