Extract out RenderRequest & common Render functions from RenderThread source files
[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   mDisplayConnection = Dali::DisplayConnection::New();
47 }
48
49 RenderHelper::~RenderHelper()
50 {
51   if (mDisplayConnection)
52   {
53     delete mDisplayConnection;
54     mDisplayConnection = NULL;
55   }
56
57   mEglFactory->Destroy();
58 }
59
60 void RenderHelper::Start()
61 {
62   if( mSurface )
63   {
64     mSurface->StartRender();
65   }
66 }
67
68 void RenderHelper::Stop()
69 {
70   if( mSurface )
71   {
72     // Tell surface we have stopped rendering
73     mSurface->StopRender();
74
75     // The surface will be destroyed soon; this pointer will become invalid
76     mSurface = NULL;
77   }
78 }
79
80 void RenderHelper::ConsumeEvents()
81 {
82   mDisplayConnection->ConsumeEvents();
83 }
84
85 void RenderHelper::InitializeEgl()
86 {
87   mEGL = mEglFactory->Create();
88
89   DALI_ASSERT_ALWAYS( mSurface && "NULL surface" );
90
91   // Initialize EGL & OpenGL
92   mDisplayConnection->InitializeEgl( *mEGL );
93   mSurface->InitializeEgl( *mEGL );
94
95   // create the OpenGL context
96   mEGL->CreateContext();
97
98   // create the OpenGL surface
99   mSurface->CreateEglSurface(*mEGL);
100
101   // Make it current
102   mEGL->MakeContextCurrent();
103 }
104
105 void RenderHelper::ReplaceSurface( RenderSurface* newSurface )
106 {
107   // This is designed for replacing pixmap surfaces, but should work for window as well
108   // we need to delete the egl surface and renderable (pixmap / window)
109   // Then create a new pixmap/window and new egl surface
110   // If the new surface has a different display connection, then the context will be lost
111   DALI_ASSERT_ALWAYS(newSurface && "NULL surface");
112
113   mDisplayConnection->InitializeEgl(*mEGL);
114
115   newSurface->ReplaceEGLSurface(*mEGL);
116
117   // use the new surface from now on
118   mSurface = newSurface;
119   mSurfaceReplaced = true;
120 }
121
122 void RenderHelper::ShutdownEgl()
123 {
124   if( mSurface )
125   {
126     // give a chance to destroy the OpenGL surface that created externally
127     mSurface->DestroyEglSurface( *mEGL );
128   }
129
130   // delete the GL context / egl surface
131   mEGL->TerminateGles();
132 }
133
134 bool RenderHelper::PreRender()
135 {
136   if( mSurface )
137   {
138     mSurface->PreRender( *mEGL, mGLES );
139   }
140   mGLES.PreRender();
141   return true;
142 }
143
144 void RenderHelper::PostRender()
145 {
146   // Inform the gl implementation that rendering has finished before informing the surface
147   mGLES.PostRender();
148
149   if( mSurface )
150   {
151     // Inform the surface that rendering this frame has finished.
152     mSurface->PostRender( *mEGL, mGLES, mDisplayConnection, mSurfaceReplaced );
153   }
154   mSurfaceReplaced = false;
155 }
156
157 } // namespace Adaptor
158
159 } // namespace Internal
160
161 } // namespace Dali