65aa91302b5c6913fb30176543d7602cba679a8e
[platform/core/uifw/dali-core.git] / dali / internal / update / rendering / scene-graph-texture-set.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 // CLASS HEADER
18 #include "scene-graph-texture-set.h"
19
20 // INTERNAL HEADERS
21 #include <dali/internal/common/internal-constants.h>
22 #include <dali/internal/common/memory-pool-object-allocator.h>
23 #include <dali/internal/update/rendering/scene-graph-renderer.h>
24
25 namespace //Unnamed namespace
26 {
27 //Memory pool used to allocate new texture sets. Memory used by this pool will be released when shutting down DALi
28 Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::TextureSet> gTextureSetMemoryPool;
29 }
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace SceneGraph
38 {
39
40 TextureSet* TextureSet::New()
41 {
42   return new ( gTextureSetMemoryPool.AllocateRawThreadSafe() ) TextureSet();
43 }
44
45 TextureSet::TextureSet()
46 : mSamplers(),
47   mRenderers(),
48   mHasAlpha( false )
49 {
50 }
51
52 TextureSet::~TextureSet()
53 {
54 }
55
56 void TextureSet::operator delete( void* ptr )
57 {
58   gTextureSetMemoryPool.FreeThreadSafe( static_cast<TextureSet*>( ptr ) );
59 }
60
61 void TextureSet::SetSampler( size_t index, Render::Sampler* sampler )
62 {
63   size_t samplerCount( mSamplers.Size() );
64   if( samplerCount < index + 1 )
65   {
66     mSamplers.Resize( index + 1 );
67     for( size_t i(samplerCount); i<=index; ++i )
68     {
69       mSamplers[i] = NULL;
70     }
71   }
72
73   mSamplers[index] = sampler;
74   NotifyChangeToRenderers();
75 }
76
77 void TextureSet::SetTexture( size_t index, Render::Texture* texture )
78 {
79   const size_t textureCount( mTextures.Size() );
80   if( textureCount < index + 1 )
81   {
82     mTextures.Resize( index + 1 );
83
84     bool samplerExist = true;
85     if( mSamplers.Size() < index + 1 )
86     {
87       mSamplers.Resize( index + 1 );
88       samplerExist = false;
89     }
90
91     for( size_t i(textureCount); i<=index; ++i )
92     {
93       mTextures[i] = 0;
94
95       if( !samplerExist )
96       {
97         mSamplers[i] = 0;
98       }
99     }
100   }
101
102   mTextures[index] = texture;
103   if( texture )
104   {
105     mHasAlpha |= texture->HasAlphaChannel();
106   }
107
108   NotifyChangeToRenderers();
109 }
110
111 bool TextureSet::HasAlpha() const
112 {
113   return mHasAlpha;
114 }
115
116 void TextureSet::AddObserver( Renderer* renderer )
117 {
118   size_t rendererCount( mRenderers.Size() );
119   for( size_t i(0); i<rendererCount; ++i )
120   {
121     if( mRenderers[i] == renderer )
122     {
123       //Renderer already in the list
124       return;
125     }
126   }
127
128   mRenderers.PushBack( renderer );
129 }
130
131 void TextureSet::RemoveObserver( Renderer* renderer )
132 {
133   size_t rendererCount( mRenderers.Size() );
134   for( size_t i(0); i<rendererCount; ++i )
135   {
136     if( mRenderers[i] == renderer )
137     {
138       mRenderers.Remove( mRenderers.Begin() + i );
139       return;
140     }
141   }
142 }
143
144 void TextureSet::NotifyChangeToRenderers()
145 {
146   size_t rendererCount = mRenderers.Size();
147   for( size_t i(0); i<rendererCount; ++i )
148   {
149     mRenderers[i]->TextureSetChanged();
150   }
151 }
152
153 } // namespace SceneGraph
154
155 } // namespace Internal
156
157 } // namespace Dali