Merge "Change PixelData to use the handle/body pattern" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / render / common / render-algorithms.cpp
1 /*
2  * Copyright (c) 2016 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/render-renderer.h>
27 #include <dali/internal/update/nodes/scene-graph-layer.h>
28
29 using Dali::Internal::SceneGraph::RenderItem;
30 using Dali::Internal::SceneGraph::RenderList;
31 using Dali::Internal::SceneGraph::RenderListContainer;
32 using Dali::Internal::SceneGraph::RenderInstruction;
33
34 namespace Dali
35 {
36
37 namespace Internal
38 {
39
40 namespace Render
41 {
42
43 namespace
44 {
45 // Table for fast look-up of Dali::DepthFunction enum to a GL depth function.
46 // Note: These MUST be in the same order as Dali::DepthFunction enum.
47 const short DaliDepthToGLDepthTable[] = { GL_NEVER, GL_ALWAYS, GL_LESS, GL_GREATER, GL_EQUAL, GL_NOTEQUAL, GL_LEQUAL, GL_GEQUAL };
48 } // Unnamed namespace
49
50 /**
51  * Sets up the scissor test if required.
52  * @param[in] renderList The render list from which to get the clipping flag
53  * @param[in] context The context
54  */
55 inline void SetScissorTest( const RenderList& renderList, Context& context )
56 {
57   // Scissor testing
58   if( renderList.IsClipping() )
59   {
60     context.SetScissorTest( true );
61
62     const Dali::ClippingBox& clip = renderList.GetClippingBox();
63     context.Scissor(clip.x, clip.y, clip.width, clip.height);
64   }
65   else
66   {
67     context.SetScissorTest( false );
68   }
69 }
70
71 /**
72  * Sets the render flags for the stencil buffer and clears all required buffers (depth and stencil if required).
73  * @param[in] renderList The render list from which to get the render flags
74  * @param[in] context The context
75  * @param[in] depthTestEnabled True if depth test is enabled for the layer
76  * @param[in] isLayer3D True if the layer is a 3D layer
77  */
78 inline void SetRenderFlags( const RenderList& renderList, Context& context, bool depthTestEnabled, bool isLayer3D )
79 {
80   const unsigned int renderFlags = renderList.GetFlags();
81   GLbitfield clearMask = 0u;
82
83   // Stencil enabled, writing, and clearing...
84   const bool enableStencilBuffer( renderFlags & RenderList::STENCIL_BUFFER_ENABLED );
85   const bool enableStencilWrite( renderFlags & RenderList::STENCIL_WRITE );
86   context.EnableStencilBuffer( enableStencilBuffer );
87   if( enableStencilBuffer )
88   {
89     context.StencilFunc( ( enableStencilWrite ? GL_ALWAYS : GL_EQUAL ), 1, 0xFF );
90     context.StencilOp( GL_KEEP, GL_REPLACE, GL_REPLACE );
91
92     clearMask |= ( renderFlags & RenderList::STENCIL_CLEAR ) ? GL_STENCIL_BUFFER_BIT : 0u;
93   }
94
95   // Write to stencil buffer or color buffer, but not both
96   context.StencilMask( enableStencilWrite ? 0xFF : 0x00 );
97   context.ColorMask( !enableStencilWrite );
98
99   // Enable and Clear the depth buffer if required.
100   // DepthTest must be enabled for the layer, else testing is turned off.
101   if( !depthTestEnabled )
102   {
103     context.EnableDepthBuffer( false );
104   }
105   else if( renderList.HasColorRenderItems() || isLayer3D ) // Also, within the context of this if(), depth test is enabled.
106   {
107
108     clearMask |= GL_DEPTH_BUFFER_BIT;
109     // We need to enable the depth buffer to clear it.
110     // Subsequently it is enabled and disabled on a per-RenderItem basis.
111     // If we do not have color renderers, this is only done for 3D layers.
112     context.EnableDepthBuffer( true );
113   }
114
115   // Clear Depth and/or stencil buffers as required.
116   // Note: The buffers will only be cleared if written to since a previous clear.
117   context.Clear( clearMask, Context::CHECK_CACHED_VALUES );
118 }
119
120 /**
121  * Sets up the depth buffer for reading and writing based on the current render item.
122  * The items read and write mode are used if specified.
123  * If AUTO is selected for reading, the decision will be based on the Layer Behavior.
124  * If AUTO is selected for writing, the decision will be based on the items opacity.
125  * @param item The RenderItem to set up the depth buffer for
126  * @param context The context used to execute GL commands.
127  * @param isLayer3D True if the layer behavior is set to LAYER_3D
128  */
129 inline void SetupDepthBuffer( const RenderItem& item, Context& context, bool isLayer3D )
130 {
131   // Set up whether or not to write to the depth buffer.
132   const DepthWriteMode::Type depthWriteMode = item.mRenderer->GetDepthWriteMode();
133   // Most common mode (AUTO) is tested first.
134   bool enableDepthWrite = ( ( depthWriteMode == DepthWriteMode::AUTO ) && item.mIsOpaque ) ||
135                           ( depthWriteMode == DepthWriteMode::ON );
136   context.DepthMask( enableDepthWrite );
137
138   // Set up whether or not to read from (test) the depth buffer.
139   const DepthTestMode::Type depthTestMode = item.mRenderer->GetDepthTestMode();
140   // Most common mode (AUTO) is tested first.
141   bool enableDepthTest = ( ( depthTestMode == DepthTestMode::AUTO ) && isLayer3D ) ||
142                          ( depthTestMode == DepthTestMode::ON );
143   // Look-up the GL depth function from the Dali::DepthFunction enum, and set it.
144   context.DepthFunc( DaliDepthToGLDepthTable[ item.mRenderer->GetDepthFunction() ] );
145
146   // The depth buffer must be enabled if either reading or writing.
147   context.EnableDepthBuffer( enableDepthWrite || enableDepthTest );
148 }
149
150 /**
151  * Process a render-list.
152  * @param[in] renderList The render-list to process.
153  * @param[in] context The GL context.
154  * @param[in] defaultShader The default shader to use.
155  * @param[in] buffer The current render buffer index (previous update buffer)
156  * @param[in] viewMatrix The view matrix from the appropriate camera.
157  * @param[in] projectionMatrix The projection matrix from the appropriate camera.
158  */
159 inline void ProcessRenderList(
160   const RenderList& renderList,
161   Context& context,
162   SceneGraph::TextureCache& textureCache,
163   SceneGraph::Shader& defaultShader,
164   BufferIndex bufferIndex,
165   const Matrix& viewMatrix,
166   const Matrix& projectionMatrix )
167 {
168   DALI_PRINT_RENDER_LIST( renderList );
169
170   bool depthTestEnabled = !( renderList.GetSourceLayer()->IsDepthTestDisabled() );
171   bool isLayer3D = renderList.GetSourceLayer()->GetBehavior() == Dali::Layer::LAYER_3D;
172
173   SetScissorTest( renderList, context );
174   SetRenderFlags( renderList, context, depthTestEnabled, isLayer3D );
175
176   // The Layers depth enabled flag overrides the per-renderer depth flags.
177   // So if depth test is disabled at the layer level, we ignore per-render flags.
178   // Note: Overlay renderers will not read or write from the depth buffer.
179   if( DALI_LIKELY( !renderList.HasColorRenderItems() || !depthTestEnabled ) )
180   {
181     size_t count = renderList.Count();
182     for ( size_t index = 0; index < count; ++index )
183     {
184       const RenderItem& item = renderList.GetItem( index );
185       DALI_PRINT_RENDER_ITEM( item );
186
187       item.mRenderer->Render( context, textureCache, bufferIndex, *item.mNode, defaultShader,
188                               item.mModelMatrix, item.mModelViewMatrix, viewMatrix, projectionMatrix, item.mSize, !item.mIsOpaque );
189     }
190   }
191   else
192   {
193     size_t count = renderList.Count();
194     for ( size_t index = 0; index < count; ++index )
195     {
196       const RenderItem& item = renderList.GetItem( index );
197       DALI_PRINT_RENDER_ITEM( item );
198
199       // Set up the depth buffer based on per-renderer flags.
200       SetupDepthBuffer( item, context, isLayer3D );
201
202       item.mRenderer->Render( context, textureCache, bufferIndex, *item.mNode, defaultShader,
203                               item.mModelMatrix, item.mModelViewMatrix, viewMatrix, projectionMatrix, item.mSize, !item.mIsOpaque );
204     }
205   }
206 }
207
208 void ProcessRenderInstruction( const RenderInstruction& instruction,
209                                Context& context,
210                                SceneGraph::TextureCache& textureCache,
211                                SceneGraph::Shader& defaultShader,
212                                BufferIndex bufferIndex )
213 {
214   DALI_PRINT_RENDER_INSTRUCTION( instruction, bufferIndex );
215
216   const Matrix* viewMatrix       = instruction.GetViewMatrix( bufferIndex );
217   const Matrix* projectionMatrix = instruction.GetProjectionMatrix( bufferIndex );
218
219   DALI_ASSERT_DEBUG( NULL != viewMatrix );
220   DALI_ASSERT_DEBUG( NULL != projectionMatrix );
221
222   if( NULL != viewMatrix &&
223       NULL != projectionMatrix )
224   {
225     const RenderListContainer::SizeType count = instruction.RenderListCount();
226
227     // Iterate through each render list in order. If a pair of render lists
228     // are marked as interleaved, then process them together.
229     for( RenderListContainer::SizeType index = 0; index < count; ++index )
230     {
231       const RenderList* renderList = instruction.GetRenderList( index );
232
233       if(  renderList &&
234           !renderList->IsEmpty() )
235       {
236         ProcessRenderList( *renderList, context, textureCache, defaultShader, bufferIndex, *viewMatrix, *projectionMatrix );
237       }
238     }
239   }
240 }
241
242 } // namespace Render
243
244 } // namespace Internal
245
246 } // namespace Dali