Extract out FPS Tracker & Update Status Logger from UpdateThread class
[platform/core/uifw/dali-adaptor.git] / adaptors / base / render-thread.h
1 #ifndef __DALI_INTERNAL_RENDER_THREAD_H__
2 #define __DALI_INTERNAL_RENDER_THREAD_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <pthread.h>
23
24 // INTERNAL INCLUDES
25 #include <egl-interface.h>
26 #include <render-surface.h> // needed for Dali::RenderSurface
27
28 namespace Dali
29 {
30
31 class RenderSurface;
32 class DisplayConnection;
33
34 namespace Integration
35 {
36 class GlAbstraction;
37 class Core;
38 }
39
40 namespace Internal
41 {
42 namespace Adaptor
43 {
44
45 class AdaptorInternalServices;
46 class ThreadSynchronization;
47 class EglFactoryInterface;
48 class EnvironmentOptions;
49
50 class RenderRequest
51 {
52 public:
53   enum Request
54   {
55     REPLACE_SURFACE, // Request to replace surface
56   };
57
58   /**
59    * Constructor.
60    * @param[in] type The type of the request
61    */
62   RenderRequest( Request type );
63
64   /**
65    * @return the type of the request
66    */
67   Request GetType();
68
69 private:
70   Request mRequestType;
71 };
72
73 class ReplaceSurfaceRequest : public RenderRequest
74 {
75 public:
76
77   /**
78    * Constructor
79    */
80   ReplaceSurfaceRequest();
81
82   /**
83    * Set the new surface
84    * @param[in] newSurface The new surface to use
85    */
86   void SetSurface(RenderSurface* newSurface);
87
88   /**
89    * @return the new surface
90    */
91   RenderSurface* GetSurface();
92
93   /**
94    * Called when the request has been completed to set the result.
95    */
96   void ReplaceCompleted();
97
98   /**
99    * @return true if the replace has completed.
100    */
101   bool GetReplaceCompleted();
102
103 private:
104   RenderSurface* mNewSurface;     ///< The new surface to use.
105   unsigned int mReplaceCompleted; ///< Set to true when the replace has completed.
106 };
107
108
109 /**
110  * The render-thread is responsible for calling Core::Render() after each update.
111  */
112 class RenderThread
113 {
114 public:
115
116   /**
117    * Create the render-thread; this will not do anything until Start() is called.
118    * @param[in] sync thread synchronization object
119    * @param[in] adaptorInterfaces base adaptor interface
120    * @param[in] environmentOptions environment options
121    */
122   RenderThread( ThreadSynchronization& sync,
123                 AdaptorInternalServices& adaptorInterfaces,
124                 const EnvironmentOptions& environmentOptions );
125
126   /**
127    * Destructor
128    */
129   ~RenderThread();
130
131 public:
132
133   /**
134    * Starts the render-thread
135    */
136   void Start();
137
138   /**
139    * Stops the render-thread
140    */
141   void Stop();
142
143 private: // Render thread side helpers
144
145   /**
146    * This method is used by the Render thread for rendering the Core to the screen.
147    * Called from render thread
148    * @return true, if the thread finishes properly.
149    */
150   bool Run();
151
152   /**
153    * Initializes EGL.
154    * Called from render thread
155    */
156   void InitializeEgl();
157
158   /**
159    * Check if main thread made any requests, e.g. ReplaceSurface
160    * Called from render thread
161    */
162   void ProcessRequest( RenderRequest* request );
163
164   /**
165    * Replaces the rendering surface
166    * Used for replacing pixmaps due to resizing
167    * Called from render thread
168    * @param newSurface to use
169    */
170   void ReplaceSurface( RenderSurface* newSurface );
171
172   /**
173    * Shuts down EGL.
174    * Called from render thread
175    */
176   void ShutdownEgl();
177
178   /**
179    * Called before core renders the scene
180    * Called from render thread
181    * @return true if successful and Core::Render should be called.
182    */
183   bool PreRender();
184
185   /**
186    * Called after core has rendered the scene
187    * Called from render thread
188    */
189   void PostRender();
190
191   /**
192    * Helper for the thread calling the entry function.
193    * @param[in] This A pointer to the current RenderThread object
194    */
195   static inline void* InternalThreadEntryFunc( void* This )
196   {
197     ( static_cast<RenderThread*>( This ) )->Run();
198     return NULL;
199   }
200
201 private:
202
203   // Undefined
204   RenderThread( const RenderThread& renderThread );
205
206   // Undefined
207   RenderThread& operator=( const RenderThread& renderThread );
208
209 private: // Data
210
211   ThreadSynchronization&        mThreadSynchronization;  ///< Used to synchronize the all threads
212   Dali::Integration::Core&      mCore;                   ///< Dali core reference
213   Integration::GlAbstraction&   mGLES;                   ///< GL abstraction reference
214   EglFactoryInterface*          mEglFactory;             ///< Factory class to create EGL implementation
215   EglInterface*                 mEGL;                    ///< Interface to EGL implementation
216   pthread_t*                    mThread;                 ///< render thread
217   RenderSurface*                mSurface;                ///< Current surface
218   Dali::DisplayConnection*      mDisplayConnection;      ///< Display connection
219   const EnvironmentOptions&     mEnvironmentOptions;     ///< Environment options
220   bool                          mSurfaceReplaced;        ///< True when new surface has been initialzed.
221 };
222
223 } // namespace Adaptor
224
225 } // namespace Internal
226
227 } // namespace Dali
228
229 #endif // __DALI_INTERNAL_RENDER_THREAD_H__