use modern construct 'nullptr' instead of 'NULL' or '0'
[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   for( auto&& renderer : mRenderers )
55   {
56     renderer->TextureSetDeleted();
57   }
58 }
59
60 void TextureSet::operator delete( void* ptr )
61 {
62   gTextureSetMemoryPool.FreeThreadSafe( static_cast<TextureSet*>( ptr ) );
63 }
64
65 void TextureSet::SetSampler( uint32_t index, Render::Sampler* sampler )
66 {
67   const uint32_t samplerCount = static_cast<uint32_t>( mSamplers.Size() );
68   if( samplerCount < index + 1 )
69   {
70     mSamplers.Resize( index + 1 );
71     for( uint32_t i(samplerCount); i<=index; ++i )
72     {
73       mSamplers[i] = nullptr;
74     }
75   }
76
77   mSamplers[index] = sampler;
78   NotifyChangeToRenderers();
79 }
80
81 void TextureSet::SetTexture( uint32_t index, Render::Texture* texture )
82 {
83   const uint32_t textureCount = static_cast<uint32_t>( mTextures.Size() );
84   if( textureCount < index + 1 )
85   {
86     mTextures.Resize( index + 1 );
87
88     bool samplerExist = true;
89     if( mSamplers.Size() < index + 1 )
90     {
91       mSamplers.Resize( index + 1 );
92       samplerExist = false;
93     }
94
95     for( uint32_t i(textureCount); i<=index; ++i )
96     {
97       mTextures[i] = nullptr;
98
99       if( !samplerExist )
100       {
101         mSamplers[i] = nullptr;
102       }
103     }
104   }
105
106   mTextures[index] = texture;
107   if( texture )
108   {
109     mHasAlpha |= texture->HasAlphaChannel();
110   }
111
112   NotifyChangeToRenderers();
113 }
114
115 bool TextureSet::HasAlpha() const
116 {
117   return mHasAlpha;
118 }
119
120 void TextureSet::AddObserver( Renderer* renderer )
121 {
122   for( auto&& element : mRenderers )
123   {
124     if( element == renderer )
125     {
126       //Renderer already in the list
127       return;
128     }
129   }
130
131   mRenderers.PushBack( renderer );
132 }
133
134 void TextureSet::RemoveObserver( Renderer* renderer )
135 {
136   for( auto&& iter = mRenderers.Begin(), end = mRenderers.End(); iter != end; ++iter )
137   {
138     if( *iter == renderer )
139     {
140       mRenderers.Remove( iter );
141       return;
142     }
143   }
144 }
145
146 void TextureSet::NotifyChangeToRenderers()
147 {
148   for( auto&& element : mRenderers )
149   {
150     element->TextureSetChanged();
151   }
152 }
153
154 } // namespace SceneGraph
155
156 } // namespace Internal
157
158 } // namespace Dali