License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / scene-graph-renderer.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/renderers/scene-graph-renderer.h>
20 #include <dali/internal/render/renderers/scene-graph-renderer-declarations.h>
21
22 // INTERNAL INCLUDES
23 #include <dali/internal/render/gl-resources/context.h>
24 #include <dali/internal/render/shaders/shader.h>
25 #include <dali/internal/render/renderers/render-data-provider.h>
26 #include <dali/public-api/actors/blending.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 namespace SceneGraph
35 {
36
37 void Renderer::Initialize( Context& context, TextureCache& textureCache )
38 {
39   mContext = &context;
40   mTextureCache = &textureCache;
41 }
42
43 Renderer::~Renderer()
44 {
45 }
46
47 void Renderer::SetShader( Shader* shader )
48 {
49   mShader = shader;
50 }
51
52 void Renderer::SetUseBlend( bool useBlend )
53 {
54   mUseBlend = useBlend;
55 }
56
57 void Renderer::SetBlendingOptions( unsigned int options )
58 {
59   mBlendingOptions.SetBitmask( options );
60 }
61
62 void Renderer::SetBlendColor( const Vector4& color )
63 {
64   mBlendingOptions.SetBlendColor( color );
65 }
66
67 void Renderer::SetCullFace( CullFaceMode mode )
68 {
69   DALI_ASSERT_DEBUG(mode >= CullNone && mode <= CullFrontAndBack);
70   mCullFaceMode = mode;
71 }
72
73 void Renderer::Render( BufferIndex bufferIndex,
74                        const Matrix& modelViewMatrix,
75                        const Matrix& viewMatrix,
76                        const Matrix& projectionMatrix,
77                        float frametime )
78 {
79   DALI_ASSERT_DEBUG( mContext && "Renderer::Render. Renderer not initialised!! (mContext == NULL)." );
80   DALI_ASSERT_DEBUG( mShader && "Renderer::Render. Shader not set!!" );
81
82   if( !CheckResources() )
83   {
84     // CheckResources() is overriden in derived classes.
85     // Prevents modify the GL state if resources are not ready and nothing is to be rendered.
86     return;
87   }
88
89   // Enables/disables blending mode.
90   mContext->SetBlend( mUseBlend );
91
92   // Set face culling mode
93   mContext->CullFace( mCullFaceMode );
94
95   // Set the blend color
96   const Vector4* const customColor = mBlendingOptions.GetBlendColor();
97   if( customColor )
98   {
99     mContext->SetCustomBlendColor( *customColor );
100   }
101   else
102   {
103     mContext->SetDefaultBlendColor();
104   }
105
106   // Set blend source & destination factors
107   mContext->BlendFuncSeparate( mBlendingOptions.GetBlendSrcFactorRgb(),
108                                mBlendingOptions.GetBlendDestFactorRgb(),
109                                mBlendingOptions.GetBlendSrcFactorAlpha(),
110                                mBlendingOptions.GetBlendDestFactorAlpha() );
111
112   // Set blend equations
113   mContext->BlendEquationSeparate( mBlendingOptions.GetBlendEquationRgb(),
114                                    mBlendingOptions.GetBlendEquationAlpha() );
115
116   mShader->SetFrameTime( frametime );
117
118   const Matrix& modelMatrix = mDataProvider.GetModelMatrix( bufferIndex );
119   const Vector4& color = mDataProvider.GetRenderColor( bufferIndex );
120
121   // Call to over ridden method in the child class
122   // TODO, once MeshRenderer is fixed to render only one mesh, move mShader.Apply here
123   // and we can greatly reduce these parameters. Also then derived renderers can be passed the Program&
124   DoRender( bufferIndex, modelViewMatrix, modelMatrix, viewMatrix, projectionMatrix, color );
125 }
126
127 Renderer::Renderer( RenderDataProvider& dataprovider )
128 : mDataProvider( dataprovider ),
129   mContext( NULL ),
130   mTextureCache( NULL ),
131   mShader( NULL ),
132   mUseBlend( false ),
133   mCullFaceMode( CullNone )
134 {
135 }
136
137 } // namespace SceneGraph
138
139 } // namespace Internal
140
141 } // namespace Dali