Add OffscreenApplication
[platform/core/uifw/dali-adaptor.git] / dali / internal / offscreen / common / offscreen-window-impl.cpp
1 /*
2  * Copyright (c) 2020 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/offscreen/common/offscreen-window-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/actors/layer.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/integration-api/adaptor-framework/adaptor.h>
26 #include <dali/integration-api/adaptor-framework/native-render-surface.h>
27 #include <dali/integration-api/adaptor-framework/native-render-surface-factory.h>
28 #include <dali/integration-api/adaptor-framework/trigger-event-factory.h>
29 #include <dali/internal/offscreen/common/offscreen-application-impl.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 OffscreenWindow* OffscreenWindow::New( uint16_t width, uint16_t height, Dali::Any surface, bool isTranslucent )
38 {
39   OffscreenWindow* window = new OffscreenWindow( width, height, surface, isTranslucent );
40   return window;
41 }
42
43 OffscreenWindow::OffscreenWindow( uint16_t width, uint16_t height, Dali::Any surface, bool isTranslucent )
44 : mRenderNotification()
45 {
46   // Create surface
47   mSurface = std::unique_ptr< RenderSurfaceInterface >( CreateNativeSurface( SurfaceSize( width, height ), surface, isTranslucent ) );
48 }
49
50 void OffscreenWindow::Initialize( bool isDefaultWindow )
51 {
52   if( isDefaultWindow )
53   {
54     Initialize();
55     return;
56   }
57
58   Dali::Integration::SceneHolder sceneHolderHandler = Dali::Integration::SceneHolder( this );
59   Dali::Adaptor::Get().AddWindow( sceneHolderHandler );
60
61   Initialize();
62 }
63
64 void OffscreenWindow::Initialize()
65 {
66   // Connect callback to be notified when the surface is rendered
67   TriggerEventFactory triggerEventFactory;
68
69   mRenderNotification = std::unique_ptr< TriggerEventInterface >( triggerEventFactory.CreateTriggerEvent( MakeCallback( this, &OffscreenWindow::OnPostRender ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER ) );
70
71   NativeRenderSurface* surface = GetNativeRenderSurface();
72
73   if( !surface )
74   {
75     return;
76   }
77
78   surface->SetRenderNotification( mRenderNotification.get() );
79 }
80
81 OffscreenWindow::~OffscreenWindow()
82 {
83   NativeRenderSurface* surface = GetNativeRenderSurface();
84
85   if( surface )
86   {
87     // To prevent notification triggering in NativeRenderSurface::PostRender while deleting SceneHolder
88     surface->SetRenderNotification( nullptr );
89   }
90 }
91
92 uint32_t OffscreenWindow::GetLayerCount() const
93 {
94   return mScene.GetLayerCount();
95 }
96
97 Dali::Layer OffscreenWindow::GetLayer( uint32_t depth ) const
98 {
99   return mScene.GetLayer( depth );
100 }
101
102 OffscreenWindow::WindowSize OffscreenWindow::GetSize() const
103 {
104   Size size = mScene.GetSize();
105
106   return OffscreenWindow::WindowSize( static_cast<uint16_t>( size.width ), static_cast<uint16_t>( size.height ) );
107 }
108
109 Dali::Any OffscreenWindow::GetNativeHandle() const
110 {
111   NativeRenderSurface* surface = GetNativeRenderSurface();
112   DALI_ASSERT_ALWAYS( surface && "surface handle is empty" );
113
114   return surface->GetNativeRenderable();
115 }
116
117 NativeRenderSurface* OffscreenWindow::GetNativeRenderSurface() const
118 {
119   return dynamic_cast< NativeRenderSurface* >( mSurface.get() );
120 }
121
122 void OffscreenWindow::OnPostRender()
123 {
124   Dali::OffscreenWindow handle( this );
125   mPostRenderSignal.Emit( handle, GetNativeHandle() );
126 }
127
128 OffscreenWindow::PostRenderSignalType& OffscreenWindow::PostRenderSignal()
129 {
130   return mPostRenderSignal;
131 }
132
133 } // namespace Internal
134
135 } // namespace Dali