b4ee99943a077e9642bc1ed4615c86431fa2b4b4
[platform/core/uifw/dali-adaptor.git] / adaptors / base / render-helper.cpp
1 /*
2  * Copyright (c) 2015 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 "render-helper.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include <base/interfaces/adaptor-internal-services.h>
26 #include <base/display-connection.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 namespace Adaptor
35 {
36
37 RenderHelper::RenderHelper( AdaptorInternalServices& adaptorInterfaces )
38 : mGLES( adaptorInterfaces.GetGlesInterface() ),
39   mEglFactory( &adaptorInterfaces.GetEGLFactoryInterface()),
40   mEGL( NULL ),
41   mSurfaceReplaced( false )
42 {
43   // set the initial values before render thread starts
44   mSurface = adaptorInterfaces.GetRenderSurfaceInterface();
45
46   if( mSurface )
47   {
48     mDisplayConnection = Dali::DisplayConnection::New( mSurface->GetSurfaceType() );
49   }
50   else
51   {
52     mDisplayConnection = Dali::DisplayConnection::New();
53   }
54 }
55
56 RenderHelper::~RenderHelper()
57 {
58   if (mDisplayConnection)
59   {
60     delete mDisplayConnection;
61     mDisplayConnection = NULL;
62   }
63
64   mEglFactory->Destroy();
65 }
66
67 void RenderHelper::Start()
68 {
69   if( mSurface )
70   {
71     mSurface->StartRender();
72   }
73 }
74
75 void RenderHelper::Stop()
76 {
77   if( mSurface )
78   {
79     // Tell surface we have stopped rendering
80     mSurface->StopRender();
81
82     // The surface will be destroyed soon; this pointer will become invalid
83     mSurface = NULL;
84   }
85 }
86
87 void RenderHelper::ConsumeEvents()
88 {
89   mDisplayConnection->ConsumeEvents();
90 }
91
92 void RenderHelper::InitializeEgl()
93 {
94   mEGL = mEglFactory->Create();
95
96   DALI_ASSERT_ALWAYS( mSurface && "NULL surface" );
97
98   // Initialize EGL & OpenGL
99   mDisplayConnection->InitializeEgl( *mEGL );
100   mSurface->InitializeEgl( *mEGL );
101
102   // create the OpenGL context
103   mEGL->CreateContext();
104
105   // create the OpenGL surface
106   mSurface->CreateEglSurface(*mEGL);
107
108   // Make it current
109   mEGL->MakeContextCurrent();
110 }
111
112 void RenderHelper::ReplaceSurface( RenderSurface* newSurface )
113 {
114   mSurface->DestroyEglSurface(*mEGL);
115
116   // This is designed for replacing pixmap surfaces, but should work for window as well
117   // we need to delete the egl surface and renderable (pixmap / window)
118   // Then create a new pixmap/window and new egl surface
119   // If the new surface has a different display connection, then the context will be lost
120   DALI_ASSERT_ALWAYS(newSurface && "NULL surface");
121
122   mDisplayConnection->InitializeEgl(*mEGL);
123
124   newSurface->ReplaceEGLSurface(*mEGL);
125
126   // use the new surface from now on
127   mSurface = newSurface;
128   mSurfaceReplaced = true;
129 }
130
131 void RenderHelper::ShutdownEgl()
132 {
133   if( mSurface )
134   {
135     // give a chance to destroy the OpenGL surface that created externally
136     mSurface->DestroyEglSurface( *mEGL );
137   }
138
139   // delete the GL context / egl surface
140   mEGL->TerminateGles();
141 }
142
143 bool RenderHelper::PreRender()
144 {
145   if( mSurface )
146   {
147     mSurface->PreRender( *mEGL, mGLES );
148   }
149   mGLES.PreRender();
150   return true;
151 }
152
153 void RenderHelper::PostRender()
154 {
155   // Inform the gl implementation that rendering has finished before informing the surface
156   mGLES.PostRender();
157
158   if( mSurface )
159   {
160     // Inform the surface that rendering this frame has finished.
161     mSurface->PostRender( *mEGL, mGLES, mDisplayConnection, mSurfaceReplaced );
162   }
163   mSurfaceReplaced = false;
164 }
165
166 } // namespace Adaptor
167
168 } // namespace Internal
169
170 } // namespace Dali