Add UniformNameCache to keep track of unique uniform ids to avoid calculating hash...
[platform/core/uifw/dali-core.git] / dali / internal / render / data-providers / uniform-name-cache.cpp
1 /*
2  * Copyright (c) 2015 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 // CLASS HEADER
18 #include <dali/internal/render/data-providers/uniform-name-cache.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/devel-api/common/hash.h>
22
23 namespace Dali
24 {
25 namespace Internal
26 {
27 namespace Render
28 {
29
30 UniformNameCache::UniformNameCache()
31 {}
32
33 UniformNameCache::~UniformNameCache()
34 { // OwnerContainer cleans up
35 }
36
37 int32_t UniformNameCache::GetSamplerUniformUniqueIndex( const std::string& uniformName )
38 {
39   OwnerContainer< UniformEntry* >::SizeType index = 0;
40   const OwnerContainer< UniformEntry* >::SizeType end = mSamplerUniformCache.Size();
41
42   const std::size_t hash = Dali::CalculateHash( uniformName );
43
44   for( ;index < end; ++index )
45   {
46     // check hash first
47     if( hash == mSamplerUniformCache[ index ]->nameHash )
48     {
49       // check full name in case of collision
50       if( mSamplerUniformCache[ index ]->uniformName == uniformName )
51       {
52         // match, return the index
53         return index;
54       }
55     }
56   }
57   // no match found, add new entry to cache
58   mSamplerUniformCache.PushBack( new UniformEntry( uniformName, hash ) );
59
60   return end;
61 }
62
63 } // namespace Render
64
65 } // namespace Internal
66
67 } // namespace Dali