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