2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "scene-graph-texture-set.h"
21 #include <dali/public-api/actors/blending.h>
22 #include <dali/public-api/shader-effects/shader-effect.h>
23 #include <dali/devel-api/rendering/texture-set.h>
24 #include <dali/integration-api/resource-declarations.h>
25 #include <dali/internal/common/internal-constants.h>
26 #include <dali/internal/update/resources/texture-metadata.h>
27 #include <dali/internal/update/resources/resource-manager.h>
28 #include <dali/internal/render/shaders/scene-graph-shader.h>
29 #include <dali/internal/common/memory-pool-object-allocator.h>
31 namespace //Unnamed namespace
33 //Memory pool used to allocate new texture sets. Memory used by this pool will be released when shutting down DALi
34 Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::TextureSet> gTextureSetMemoryPool;
46 TextureSet* TextureSet::New()
48 return new ( gTextureSetMemoryPool.AllocateRawThreadSafe() ) TextureSet();
51 TextureSet::TextureSet()
54 mConnectionObservers(),
55 mResourcesReady( false ),
56 mFinishedResourceAcquisition( false ),
60 // Observe own property-owner's uniform map
61 AddUniformMapObserver( *this );
64 TextureSet::~TextureSet()
66 mConnectionObservers.Destroy( *this );
69 void TextureSet::operator delete( void* ptr )
71 gTextureSetMemoryPool.FreeThreadSafe( static_cast<TextureSet*>( ptr ) );
74 void TextureSet::Prepare( const ResourceManager& resourceManager )
78 unsigned int opaqueCount = 0;
79 unsigned int completeCount = 0;
80 unsigned int failedCount = 0;
81 unsigned int frameBufferCount = 0;
82 const std::size_t textureCount( mTextureId.Count() );
83 if( textureCount > 0 )
85 for( unsigned int i(0); i<textureCount; ++i )
87 const ResourceId textureId = mTextureId[ i ];
88 TextureMetadata* metadata = NULL;
89 if( textureId != Integration::InvalidResourceId )
91 // if there is metadata, resource is loaded
92 if( resourceManager.GetTextureMetadata( textureId, metadata ) )
94 DALI_ASSERT_DEBUG( metadata );
95 // metadata is valid pointer from now on
96 if( metadata->IsFullyOpaque() )
101 if( metadata->IsFramebuffer() )
103 if( metadata->HasFrameBufferBeenRenderedTo() )
114 // loaded so will complete this frame
117 // no point checking failure as there is no metadata for failed loads
119 // if no metadata, loading can be failed
120 else if( resourceManager.HasResourceLoadFailed( textureId ) )
127 //If the texture is not valid it is considerer opaque and complete
134 mHasAlpha = ( opaqueCount != textureCount );
136 // ready for rendering when all textures are either successfully loaded or they are FBOs
137 mResourcesReady = (completeCount + frameBufferCount >= textureCount);
139 // Texture set is complete if all resources are either loaded or failed or, if they are FBOs have been rendererd to
140 mFinishedResourceAcquisition = ( completeCount + failedCount == textureCount );
142 if( mFinishedResourceAcquisition )
144 // Texture set is now considered not changed
150 void TextureSet::SetImage( size_t index, ResourceId imageId )
152 size_t textureCount( mTextureId.Size() );
153 if( textureCount < index + 1 )
155 mTextureId.Resize( index + 1 );
156 mSamplers.Resize( index + 1 );
157 for( size_t i(textureCount); i<=index; ++i )
159 mTextureId[i] = Integration::InvalidResourceId;
164 mTextureId[index] = imageId;
166 mConnectionObservers.ConnectionsChanged(*this);
169 void TextureSet::SetSampler( size_t index, Render::Sampler* sampler )
171 size_t samplerCount( mSamplers.Size() );
172 if( samplerCount < index + 1 )
174 mSamplers.Resize( index + 1 );
175 mTextureId.Resize( index + 1 );
176 for( size_t i(samplerCount); i<=index; ++i )
178 mTextureId[i] = Integration::InvalidResourceId;
183 mSamplers[index] = sampler;
186 mConnectionObservers.ConnectionsChanged(*this);
189 bool TextureSet::HasAlpha() const
194 void TextureSet::GetResourcesStatus( bool& resourcesReady, bool& finishedResourceAcquisition )
196 resourcesReady = mResourcesReady;
197 finishedResourceAcquisition = mFinishedResourceAcquisition;
200 void TextureSet::ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex )
204 void TextureSet::DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex )
208 void TextureSet::AddConnectionObserver( ConnectionChangePropagator::Observer& observer )
210 mConnectionObservers.Add(observer);
213 void TextureSet::RemoveConnectionObserver( ConnectionChangePropagator::Observer& observer )
215 mConnectionObservers.Remove(observer);
218 void TextureSet::UniformMappingsChanged( const UniformMap& mappings )
220 // Our uniform map has changed.
221 // Inform connected observers.
222 mConnectionObservers.ConnectedUniformMapChanged();
225 } // namespace SceneGraph
227 } // namespace Internal