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