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