Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / effects / inc / FUiEffects_RendererEffectRenderer.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file        FUiEffects_RendererEffectRenderer.h
19  * @brief               This is the header file for the EffectRenderer class
20  */
21
22 #ifndef _FUI_EFFECTS_INTERNAL_RENDERER_EFFECTRENDERER_H_
23 #define _FUI_EFFECTS_INTERNAL_RENDERER_EFFECTRENDERER_H_
24
25 #include <stdint.h>
26 #include <map>
27 #include <list>
28 #include <string>
29 #include <unique_ptr.h>
30
31 #include <FGraphicsOpengl2.h>
32 #include <FGraphics.h>
33 #include <FUiControl.h>
34
35 #include "FUiEffects_RendererViewport.h"
36 #include "FUiEffects_RuntimeRenderDataScene.h"
37 #include "FUiEffects_RendererRendererObject.h"
38 #include "FUiEffects_RendererPointLight.h"
39 #include "FUiEffects_RendererSpotLight.h"
40 #include "FUiEffects_RendererDirectionalLight.h"
41 #include "FUiEffects_RendererLightingParameters.h"
42 #include "FUiEffects_RendererMemoryTexture2DProperty.h"
43
44 #include <renderer/graphics-engine/FUiEffects_RendererGraphicsEngineRender.h>
45 #include <renderer/engine-model/FUiEffects_RendererEngineModelCamera.h>
46 #include <renderer/engine-model/FUiEffects_RendererEngineModelNode.h>
47 #include <renderer/engine-model/FUiEffects_RendererEngineModelGroup.h>
48 #include <renderer/engine-model/FUiEffects_RendererEngineModelStringShaderProperty.h>
49 #include <renderer/engine-model/FUiEffects_RendererEngineModelShaderHolderProgramProperty.h>
50
51 namespace Tizen { namespace Ui { namespace Effects { namespace _Renderer
52 {
53
54 extern const EGLint RENDERER_EGL_DEFAULT_CONFIG_ATTRIBS[];
55
56 /**
57  * @class       EffectRenderer
58  * @brief       This class for model visualization
59  *
60  */
61 class EffectRenderer
62 {
63 public:
64
65         /**
66          * Constructor
67          */
68         EffectRenderer(void);
69
70         /**
71          * Destructor
72          */
73         ~EffectRenderer(void);
74
75         /**
76          * Initializes OpenGL for this device
77          *
78          * @param [in]  config          Specifies attributes required to match by configs
79          * @return              bool            true on success, otherwise false
80          */
81         static bool InitOpenGL(const EGLint* pConfig = RENDERER_EGL_DEFAULT_CONFIG_ATTRIBS);
82
83         /**
84          * Closes OpenGL for this device
85          *
86          * @return              void
87          */
88         static void CloseOpenGL(void);
89
90         /**
91          * OpenGL status
92          *
93          * @return              bool            true on success, otherwise false
94          */
95         static bool CheckOpenGlesInitialized(void);
96
97         /**
98          * Creates OpenGL surface
99          *
100          * @param [in]  wnd                             Specifies the native window
101          * @return              EGLSurface              Result of eglCreateWindowSurface (EGL_NO_SURFACE if failed)
102          */
103         static EGLSurface CreateSurface(EGLNativeWindowType wnd);
104
105         /**
106          * Deletes OpenGL surface
107          *
108          * @param [in]  surface                         Surface to delete
109          * @return              void
110          */
111         static void DeleteSurface(EGLSurface surface);
112
113         /**
114          * Creates OpenGL context
115          *
116          * @param [in]  ShareContext    Sharable context
117          * @return              EGLContext              Result of eglCreateContext (EGL_NO_CONTEXT if failed)
118          */
119         static EGLContext CreateContext(EGLContext shareContext = EGL_NO_CONTEXT);
120
121         /**
122          * Deletes OpenGL context
123          *
124          * @param [in]  context                         Surface to delete
125          * @return              void
126          */
127         static void DeleteContext(EGLContext context);
128
129         /**
130          * Initializes renderer from data: scene graph, objects etc. Clears previous content.
131          *
132          * @param [in]  _renderDataCollection           Array of data pointers
133          * @return              void
134          *
135          * @remark              It saves pointer to EffectRenderer::renderDataCollection for further updates (see EffectRenderer::Update). @n
136          * @remark              So it's assumed that content of _renderDataCollection (pointers values and their order in array) @n
137          * @remark              is constant through all the life cycle of the renderer
138          */
139         void Construct(Tizen::Ui::Effects::_Runtime::RenderDataScene &renderDataCollection);
140
141         /**
142          * Updates renderer objects
143          *
144          * @return              void
145          *
146          * @remark              The source for updates is EffectRenderer::renderDataCollection. See comments for EffectRenderer::Construct
147          */
148         void Update(void);
149
150         /**
151          * Initialize OpenGL context state (Clear color etc)
152          *
153          * @return              void
154          */
155         void InitializeContext(void) const;
156
157         /**
158          * Draw scene
159          *
160          * @return              void
161          */
162         void Draw(void);
163
164         /**
165          * Sets borders of visible scene (in model (world) space) for coordinates calculation
166          *
167          * @param [in]  worldLeft, worldRight, worldBottom, worldTop - coordinates of world sides related to screen
168          * @param [in]   worldZ  - Z level of screen viewport
169          * @param [in]  worldNearDist, worldFarDist     - distances to camera clipping planes in world coordinates
170          * @param [in]  FOV_Y_deg  - Full angle, in degrees, in the vertical direction
171          * @param [in]  perspectiveProjection - true = use perspective projection, false = use orthographic projection
172          *
173          * @return              bool    true on valid arguments and success execution
174          * @see                 SetScreen
175          */
176         bool SetWorld(float worldLeft, float worldRight, float worldBottom, float worldTop, float worldZ, float worldNearDist, float worldFarDist, float FOV_Y_deg, bool perspectiveProjection);
177
178         /**
179          * Transforms window coordinates to model space
180          *
181          * @param [in]  p       point on screen
182          *
183          * @return              Vec3f           Point in model space
184          * @see                 SetScreen, SetWorld
185          */
186         Tizen::Ui::Effects::_Utils::Vec3f TransformScreenToWorld(Tizen::Ui::Effects::_Utils::Vec2f p) const;
187
188         /**
189          * Sets image for texture
190          *
191          * @param [in]  bitmapID        id of image
192          * @param [in]  bitmap          Image bitmap
193          *
194          * @return              bool            True on success, otherwise false
195          */
196         bool SetBitmap(long bitmapID, Tizen::Graphics::Bitmap &bitmap);
197
198         /**
199          * Sets image for texture
200          *
201          * @param [in]  ptr                     pointer to bitmap data
202          * @param [in]  width           width in pixels
203          * @param [in]  height          height in pixels
204          *
205          * @return              bool            True on success, otherwise false
206          *
207          * @remark              If ptr = null empty texture of width*height size will be allocated in memory for faster response on next call to this method
208          */
209         bool SetBitmap(long bitmapID, const void* ptr, size_t width, size_t height);
210
211         /**
212          * Sets borders of native screen (in pixels) for coordinates calculation
213          *
214          * @return              void
215          */
216         void SetScreen(const Tizen::Ui::Control& control);
217
218         /**
219          * Tracks screen size changes
220          *
221          * @return              void
222          */
223         void TrackScreenSizeChanges(const Tizen::Ui::Control& control);
224
225         /**
226          * Prebuild OpenGL data caches for fast response on Draw() method
227          *
228          * @return              void
229          */
230         void BuildCache();
231
232         /**
233          * Frees OpenGL related data
234          *
235          * @return              void
236          */
237         void ReleaseCache();
238
239 private:
240
241         /**
242          * Hidden copy constructor
243          */
244         EffectRenderer(const EffectRenderer &rhs);
245
246         /**
247          * Hidden assignment operator
248          */
249         EffectRenderer& operator=(const EffectRenderer &rhs);
250
251         /**
252          * Initializes renderer lighting data: point, spot and directional lights, ambient parameters, etc.
253          *
254          * @param [in]  _renderDataCollection           Array of data pointers
255          * @return              void
256          *
257          * @remark              It saves pointer to EffectRenderer::renderDataCollection for further updates (see EffectRenderer::Update). @n
258          * @remark              So it's assumed that content of _renderDataCollection (pointers values and their order in array) @n
259          * @remark              is constant through all the life cycle of the renderer
260          */
261         void ConstructLighting(void);
262
263         /**
264          * Updates scene lighting
265          *
266          * @return              void
267          */
268         void UpdateLighting(void);
269
270         /**
271          * Initializes renderer object from given data
272          *
273          * @param [in]  index                   object index in EffectRenderer::objects
274          * @param [in]  rdata                   Data for initialization
275          * @return              void
276          *
277          * @remark              This method is for internal use only
278          */
279         void InitRObjectFromData(Tizen::Ui::Effects::_Runtime::RenderDataSurfaceCollection::size_type index, Tizen::Ui::Effects::_Runtime::RenderDataSurface &rdata);
280
281         /**
282          * Updates renderer object
283          *
284          * @param [in]  index                   object index in EffectRenderer::objects
285          * @param [in]  rdata                   Data for update
286          * @return              void
287          *
288          * @remark              This method is for internal use only
289          */
290         void UpdateRObject(Tizen::Ui::Effects::_Runtime::RenderDataSurfaceCollection::size_type index, Tizen::Ui::Effects::_Runtime::RenderDataSurface &rdata);
291
292         /**
293          * Looks for texture in EffectRenderer::textures by its id.
294          *
295          * @param [in]  bitmapID                        ID of texture
296          * @return              MemoryTexture2DPropertyPtr                      Texture object
297          *
298          * @remark              If ID not found method creates and returns empty texture for further usage filled with some noticeable color (violet [255 0 128])
299          */
300         MemoryTexture2DPropertyPtr FindTexture(long bitmapID);
301
302         /**
303          * Sets borders of native screen (in pixels) for coordinates calculation
304          *
305          * @param [in]  screenLeft, screenRight, screenBottom, screenTop  -  coordinates of screen sides in pixels. @n
306          *                              Used for screen-to-world transformation in touch events handlers. For example commonly  screenTop = 0, screenBottom = window height
307          *
308          * @return              void
309          */
310         void SetScreen(float screenLeft, float screenRight, float screenBottom, float screenTop, int displayWidth, int displayHeight);
311
312         /**
313          * (Re)Creates shader considering scene lighting parameters and count of different light types
314          *
315          * @return              void
316          */
317         void CreateShader();
318
319         /**
320          * Makes this renderer context current
321          *
322          * @return              void
323          */
324         void MakeCurrent(void);
325
326         /**
327          * Restores initial OpenGL context saved in MakeCurrent method
328          *
329          * @return              void
330          */
331         void RestorePreviousContext(void);
332
333 public:
334
335         EGLSurface surface;                             /**< OpenGL surface */
336
337 private:
338         typedef std::map<long, MemoryTexture2DPropertyPtr> TexturesMap;
339         typedef std::map<std::string, PointLightPtr> PointLightsList;
340         typedef std::map<std::string, SpotLightPtr> SpotLightsList;
341         typedef std::map<std::string, DirectionalLightPtr> DirectionalLightsList;
342
343         static EGLDisplay __eglDisplay;
344         static EGLConfig __eglConfig;
345         static EGLint __glMajorVer;
346         static EGLint __glMinorVer;
347         static bool __OpenGLInitialized;
348         EGLContext __context, __previousContext;
349         bool __previousContextSaved;
350         Viewport __viewport;
351         float __FOV_Y_deg;
352         bool __perspectiveProjection;
353
354         TexturesMap __textures;
355         std::vector<RendererObjectPtr> __objects;
356         Tizen::Ui::Effects::_Runtime::RenderDataScene* __renderDataScene;
357         Tizen::Ui::Effects::_Runtime::RenderDataSurfaceCollection* __pRenderDataSurfaceCollection;
358
359         // 3DFx
360         GraphicsEngine::RenderPtr __renderer;
361         EngineModel::GroupPtr __root;
362         EngineModel::CameraPtr __camera;
363         EngineModel::ShaderHolderProgramPropertyPtr __program;
364
365         Tizen::Graphics::Dimension __dimensionPreviousControl;
366
367         // Lighting
368         LightingParameters __lightingParameters;
369         PointLightsList __pointLights;
370         SpotLightsList __spotLights;
371         DirectionalLightsList __directionalLights;
372
373
374 }; // EffectRenderer
375
376 typedef std::unique_ptr<EffectRenderer> EffectRendererUniquePtr;
377
378 } } } } // Tizen::Ui::Effects::_EffectRenderer
379
380 #endif //_FUI_EFFECTS_INTERNAL_RENDERER_EFFECTRENDERER_H_