[SRUK] Initial copy from Tizen 2.2 version
[platform/core/uifw/dali-core.git] / dali / internal / render / common / render-algorithms.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/render/common/render-algorithms.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/internal/render/common/render-debug.h>
22 #include <dali/internal/render/common/render-list.h>
23 #include <dali/internal/render/common/render-instruction.h>
24 #include <dali/internal/render/gl-resources/context.h>
25 #include <dali/internal/render/renderers/scene-graph-renderer.h>
26
27 using Dali::Internal::SceneGraph::RenderItem;
28 using Dali::Internal::SceneGraph::RenderList;
29 using Dali::Internal::SceneGraph::RenderListContainer;
30 using Dali::Internal::SceneGraph::RenderInstruction;
31
32 namespace Dali
33 {
34
35 namespace Internal
36 {
37
38 namespace Render
39 {
40
41 /**
42  * Process a render-list.
43  * @param[in] renderList The render-list to process.
44  * @param[in] context The GL context.
45  * @param[in] buffer The current render buffer index (previous update buffer)
46  * @param[in] frameTime The elapsed time between the last two updates.
47  * @param[in] viewMatrix The view matrix from the appropriate camera.
48  * @param[in] projectionMatrix The projection matrix from the appropriate camera.
49  */
50 inline void ProcessRenderList( const RenderList& renderList,
51                                Context& context,
52                                BufferIndex bufferIndex,
53                                float frameTime,
54                                const Matrix& viewMatrix,
55                                const Matrix& projectionMatrix )
56 {
57   DALI_PRINT_RENDER_LIST( renderList );
58
59   // Scissor testing
60   if( renderList.IsClipping() )
61   {
62     context.SetScissorTest( true );
63
64     const Dali::ClippingBox& clip = renderList.GetClippingBox();
65     context.Scissor(clip.x, clip.y, clip.width, clip.height);
66   }
67   else
68   {
69     context.SetScissorTest( false );
70   }
71
72   const unsigned int renderFlags = renderList.GetFlags();
73
74   bool setDepthTest     = ( ( renderFlags & RenderList::DEPTH_TEST ) != 0u );
75   bool depthMask        = ( ( renderFlags & RenderList::DEPTH_WRITE ) != 0u );
76
77   GLbitfield clearMask  = ( renderFlags & RenderList::DEPTH_CLEAR ) ? GL_DEPTH_BUFFER_BIT : 0u;
78
79   context.SetDepthTest( setDepthTest );
80   context.DepthMask( depthMask );
81
82   // Stencil testing, writing, and clearing...
83   const bool enableStencilTest(  renderFlags & RenderList::STENCIL_TEST );
84   const bool enableStencilWrite( renderFlags & RenderList::STENCIL_WRITE );
85
86   context.SetStencilTest( enableStencilTest );
87
88   if( enableStencilTest )
89   {
90     context.StencilFunc( (enableStencilWrite ? GL_ALWAYS : GL_EQUAL), 1, 0xFF );
91     context.StencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE);
92
93     clearMask |= (renderFlags & RenderList::STENCIL_CLEAR) ? GL_STENCIL_BUFFER_BIT : 0u;
94   }
95
96   // Write to stencil buffer or color buffer, but not both
97   context.StencilMask( enableStencilWrite ? 0xFF : 0x00 );
98   context.ColorMask( !enableStencilWrite );
99
100   // Clear depth and/or stencil buffer.
101   if( clearMask )
102   {
103     context.Clear( clearMask );
104   }
105
106   size_t count = renderList.Count();
107   for ( size_t index = 0; index < count; ++index )
108   {
109     const RenderItem& item = renderList.GetItem( index );
110
111     DALI_PRINT_RENDER_ITEM( item );
112
113     SceneGraph::Renderer* renderer = const_cast< SceneGraph::Renderer* >( item.GetRenderer() );
114     const Matrix& modelViewMatrix = item.GetModelViewMatrix();
115
116     renderer->Render( bufferIndex, modelViewMatrix, viewMatrix, projectionMatrix, frameTime );
117   }
118 }
119
120 void ProcessRenderInstruction( const RenderInstruction& instruction,
121                                Context& context,
122                                BufferIndex bufferIndex,
123                                float frameTime )
124 {
125   DALI_PRINT_RENDER_INSTRUCTION( instruction );
126
127   const Matrix* viewMatrix       = instruction.mViewMatrix;
128   const Matrix* projectionMatrix = instruction.mProjectionMatrix;
129
130   DALI_ASSERT_DEBUG( NULL != viewMatrix );
131   DALI_ASSERT_DEBUG( NULL != projectionMatrix );
132
133   if( NULL != viewMatrix &&
134       NULL != projectionMatrix )
135   {
136     const RenderListContainer::SizeType count = instruction.RenderListCount();
137     for( RenderListContainer::SizeType index = 0; index < count; ++index )
138     {
139       const RenderList* renderList = instruction.GetRenderList( index );
140
141       if(  renderList &&
142           !renderList->IsEmpty() )
143       {
144         ProcessRenderList( *renderList, context, bufferIndex, frameTime, *viewMatrix, *projectionMatrix );
145       }
146     }
147   }
148 }
149
150 } // namespace Render
151
152 } // namespace Internal
153
154 } // namespace Dali