Conversion to Apache 2.0 license
[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  */
51 inline void ProcessRenderList( const RenderList& renderList,
52                                Context& context,
53                                BufferIndex bufferIndex,
54                                float frameTime,
55                                const Matrix& viewMatrix,
56                                const Matrix& projectionMatrix )
57 {
58   DALI_PRINT_RENDER_LIST( renderList );
59
60   // Scissor testing
61   if( renderList.IsClipping() )
62   {
63     context.SetScissorTest( true );
64
65     const Dali::ClippingBox& clip = renderList.GetClippingBox();
66     context.Scissor(clip.x, clip.y, clip.width, clip.height);
67   }
68   else
69   {
70     context.SetScissorTest( false );
71   }
72
73   const unsigned int renderFlags = renderList.GetFlags();
74
75   bool setDepthTest     = ( ( renderFlags & RenderList::DEPTH_TEST ) != 0u );
76   bool depthMask        = ( ( renderFlags & RenderList::DEPTH_WRITE ) != 0u );
77
78   GLbitfield clearMask  = ( renderFlags & RenderList::DEPTH_CLEAR ) ? GL_DEPTH_BUFFER_BIT : 0u;
79
80   context.SetDepthTest( setDepthTest );
81   context.DepthMask( depthMask );
82
83   // Stencil testing, writing, and clearing...
84   const bool enableStencilTest(  renderFlags & RenderList::STENCIL_TEST );
85   const bool enableStencilWrite( renderFlags & RenderList::STENCIL_WRITE );
86
87   context.SetStencilTest( enableStencilTest );
88
89   if( enableStencilTest )
90   {
91     context.StencilFunc( (enableStencilWrite ? GL_ALWAYS : GL_EQUAL), 1, 0xFF );
92     context.StencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE);
93
94     clearMask |= (renderFlags & RenderList::STENCIL_CLEAR) ? GL_STENCIL_BUFFER_BIT : 0u;
95   }
96
97   // Write to stencil buffer or color buffer, but not both
98   context.StencilMask( enableStencilWrite ? 0xFF : 0x00 );
99   context.ColorMask( !enableStencilWrite );
100
101   // Clear depth and/or stencil buffer.
102   if( clearMask )
103   {
104     context.Clear( clearMask );
105   }
106
107   size_t count = renderList.Count();
108   for ( size_t index = 0; index < count; ++index )
109   {
110     const RenderItem& item = renderList.GetItem( index );
111
112     DALI_PRINT_RENDER_ITEM( item );
113
114     SceneGraph::Renderer* renderer = const_cast< SceneGraph::Renderer* >( item.GetRenderer() );
115     const Matrix& modelViewMatrix = item.GetModelViewMatrix();
116
117     renderer->Render( bufferIndex, modelViewMatrix, viewMatrix, projectionMatrix, frameTime );
118   }
119 }
120
121 void ProcessRenderInstruction( const RenderInstruction& instruction,
122                                Context& context,
123                                BufferIndex bufferIndex,
124                                float frameTime )
125 {
126   DALI_PRINT_RENDER_INSTRUCTION( instruction );
127
128   const Matrix* viewMatrix       = instruction.mViewMatrix;
129   const Matrix* projectionMatrix = instruction.mProjectionMatrix;
130
131   DALI_ASSERT_DEBUG( NULL != viewMatrix );
132   DALI_ASSERT_DEBUG( NULL != projectionMatrix );
133
134   if( NULL != viewMatrix &&
135       NULL != projectionMatrix )
136   {
137     const RenderListContainer::SizeType count = instruction.RenderListCount();
138     for( RenderListContainer::SizeType index = 0; index < count; ++index )
139     {
140       const RenderList* renderList = instruction.GetRenderList( index );
141
142       if(  renderList &&
143           !renderList->IsEmpty() )
144       {
145         ProcessRenderList( *renderList, context, bufferIndex, frameTime, *viewMatrix, *projectionMatrix );
146       }
147     }
148   }
149 }
150
151 } // namespace Render
152
153 } // namespace Internal
154
155 } // namespace Dali