Removed redundant resource loading & rendering code
[platform/core/uifw/dali-core.git] / dali / internal / render / shaders / program-controller.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/shaders/program-controller.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/integration-api/gl-defines.h>
23 #include <dali/internal/common/shader-saver.h>
24 #include <dali/internal/render/gl-resources/gl-call-debug.h>
25 #include <dali/internal/render/shaders/program.h>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 ProgramController::ProgramController( Integration::GlAbstraction& glAbstraction )
34 : mShaderSaver( 0 ),
35   mGlAbstraction( glAbstraction ),
36   mCurrentProgram( NULL ),
37   mProgramBinaryFormat( 0 ),
38   mNumberOfProgramBinaryFormats( 0 )
39 {
40   // we have 17 default programs so make room for those and a few custom ones as well
41   mProgramCache.Reserve( 32 );
42 }
43
44 ProgramController::~ProgramController()
45 {
46 }
47
48 void ProgramController::ResetProgramMatrices()
49 {
50   const ProgramIterator end = mProgramCache.End();
51   for ( ProgramIterator iter = mProgramCache.Begin(); iter != end; ++iter )
52   {
53     Program* program = (*iter)->GetProgram();
54     program->SetProjectionMatrix( NULL );
55     program->SetViewMatrix( NULL );
56   }
57 }
58
59 void ProgramController::GlContextCreated()
60 {
61   // reset any potential previous errors
62   LOG_GL( "GetError()\n" );
63   CHECK_GL( mGlAbstraction, mGlAbstraction.GetError() );
64
65   // find out if program binaries are supported and the format enum as well
66   Dali::Vector<GLint> programBinaryFormats;
67
68   CHECK_GL( mGlAbstraction, mGlAbstraction.GetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS_OES, &mNumberOfProgramBinaryFormats ) );
69   LOG_GL("GetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS_OES) = %d\n", mNumberOfProgramBinaryFormats );
70
71   if( GL_NO_ERROR == mGlAbstraction.GetError() && 0 < mNumberOfProgramBinaryFormats )
72   {
73     programBinaryFormats.Resize( mNumberOfProgramBinaryFormats );
74     CHECK_GL( mGlAbstraction, mGlAbstraction.GetIntegerv(GL_PROGRAM_BINARY_FORMATS_OES, &programBinaryFormats[0] ) );
75     LOG_GL("GetIntegerv(GL_PROGRAM_BINARY_FORMATS_OES) = %d\n", programBinaryFormats[0] );
76     mProgramBinaryFormat = programBinaryFormats[0];
77   }
78 }
79
80 void ProgramController::GlContextDestroyed()
81 {
82   mNumberOfProgramBinaryFormats = 0;
83   mProgramBinaryFormat = 0;
84
85   SetCurrentProgram( NULL );
86   // Inform programs they are no longer valid
87   const ProgramIterator end = mProgramCache.End();
88   for ( ProgramIterator iter = mProgramCache.Begin(); iter != end; ++iter )
89   {
90     (*iter)->GetProgram()->GlContextDestroyed();
91   }
92 }
93
94 Integration::GlAbstraction& ProgramController::GetGlAbstraction()
95 {
96   return mGlAbstraction;
97 }
98
99 Program* ProgramController::GetProgram( size_t shaderHash )
100 {
101   Program* program = NULL;
102   const ProgramIterator end = mProgramCache.End();
103   for ( ProgramIterator iter = mProgramCache.Begin(); iter != end; ++iter )
104   {
105     size_t hash = (*iter)->GetHash();
106     if( shaderHash == hash )
107     {
108       program = (*iter)->GetProgram();
109       break;
110     }
111   }
112   return program;
113 }
114
115 void ProgramController::AddProgram( size_t shaderHash, Program* program )
116 {
117   // we expect unique hash values so its event thread sides job to guarantee that
118   // AddProgram is only called after program checks that GetProgram returns NULL
119   mProgramCache.PushBack( new ProgramPair( program, shaderHash ) );
120 }
121
122 Program* ProgramController::GetCurrentProgram()
123 {
124   return mCurrentProgram;
125 }
126
127 void ProgramController::SetCurrentProgram( Program* program )
128 {
129   mCurrentProgram = program;
130 }
131
132 bool ProgramController::IsBinarySupported()
133 {
134   return mNumberOfProgramBinaryFormats > 0;
135 }
136
137 unsigned int ProgramController::ProgramBinaryFormat()
138 {
139   return mProgramBinaryFormat;
140 }
141
142 void ProgramController::StoreBinary( Internal::ShaderDataPtr programData )
143 {
144   DALI_ASSERT_DEBUG( programData->GetBufferSize() > 0 );
145   DALI_ASSERT_DEBUG( mShaderSaver && "SetShaderSaver() should have been called during startup." );
146
147   if( mShaderSaver != NULL )
148   {
149     mShaderSaver->SaveBinary( programData );
150   }
151 }
152
153 void ProgramController::SetShaderSaver( ShaderSaver& shaderSaver )
154 {
155   mShaderSaver = &shaderSaver;
156 }
157
158 } // namespace Internal
159
160 } // namespace Dali