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