2 * Copyright (c) 20207 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.
19 #include <dali/internal/system/common/object-profiler.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/integration-api/profiling.h>
25 #include <dali/public-api/object/ref-object.h>
26 #include <dali/public-api/object/base-object.h>
27 #include <dali/public-api/object/type-registry.h>
30 using namespace Dali::Integration::Profiling;
39 ObjectProfiler::ObjectProfiler( Dali::ObjectRegistry objectRegistry, uint32_t timeInterval )
40 : mObjectRegistry( objectRegistry )
42 // This class must be created after the Stage; this means it doesn't count the initial objects
43 // that are created by the stage (base layer, default camera actor)
45 mTimer = Dali::Timer::New( timeInterval * 1000 );
46 mTimer.TickSignal().Connect( this, &ObjectProfiler::OnTimeout );
49 mObjectRegistry.ObjectCreatedSignal().Connect( this, &ObjectProfiler::OnObjectCreated );
50 mObjectRegistry.ObjectDestroyedSignal().Connect( this, &ObjectProfiler::OnObjectDestroyed );
53 ObjectProfiler::~ObjectProfiler()
57 void ObjectProfiler::DisplayInstanceCounts()
59 for( auto&& element : mInstanceCountContainer )
61 std::size_t memorySize = GetMemorySize( element.first, element.second );
64 LogMessage( Debug::DebugInfo, "%-30s: % 4d Memory MemorySize: ~% 6.1f kB\n",
65 element.first.c_str(), element.second, memorySize / 1024.0f );
69 LogMessage( Debug::DebugInfo, "%-30s: % 4d\n",
70 element.first.c_str(), element.second );
73 LogMessage(Debug::DebugInfo, "\n");
76 bool ObjectProfiler::OnTimeout()
78 DisplayInstanceCounts();
82 void ObjectProfiler::OnObjectCreated(BaseHandle handle)
84 string theType = handle.GetTypeName();
87 DALI_LOG_ERROR("Object created from an unregistered type\n");
88 theType = "<Unregistered>";
91 mInstanceTypes.push_back(InstanceTypePair(&handle.GetBaseObject(), theType));
94 for( auto&& element : mInstanceCountContainer )
96 if( element.first == theType )
104 InstanceCountPair instanceCount( theType, 1 );
105 mInstanceCountContainer.emplace_back( instanceCount );
109 void ObjectProfiler::OnObjectDestroyed(const Dali::RefObject* object)
111 const BaseObject* baseObject = static_cast<const BaseObject*>(object);
113 const auto end = mInstanceTypes.end();
114 for( auto iter = mInstanceTypes.begin(); iter != end; ++iter )
116 if( iter->first == baseObject )
118 const auto& theType = iter->second;
119 if( !theType.empty() )
121 auto&& countIter = std::find_if( mInstanceCountContainer.begin(),
122 mInstanceCountContainer.end(),
123 [theType] ( const InstanceCountPair& instance )
124 { return instance.first == theType; } );
125 if( countIter != mInstanceCountContainer.end() )
127 (*countIter).second--;
130 mInstanceTypes.erase( iter );
136 std::size_t ObjectProfiler::GetMemorySize( const std::string& name, uint32_t count )
138 struct MemoryMemorySize
141 std::size_t memorySize;
143 MemoryMemorySize memoryMemorySizes[] =
145 { "Animation", ANIMATION_MEMORY_SIZE },
146 { "Constraint", CONSTRAINT_MEMORY_SIZE },
147 { "Actor", ACTOR_MEMORY_SIZE },
148 { "Layer", LAYER_MEMORY_SIZE },
149 { "CameraActor", CAMERA_ACTOR_MEMORY_SIZE },
150 { "Renderer", RENDERER_MEMORY_SIZE },
151 { "Geometry", GEOMETRY_MEMORY_SIZE },
152 { "PropertyBuffer", PROPERTY_BUFFER_MEMORY_SIZE },
153 { "TextureSet", TEXTURE_SET_MEMORY_SIZE },
154 { "Sampler", SAMPLER_MEMORY_SIZE },
155 { "Shader", SHADER_MEMORY_SIZE },
158 for( size_t i=0; i<sizeof(memoryMemorySizes)/sizeof(MemoryMemorySize); i++ )
160 if( memoryMemorySizes[i].name.compare(name) == 0 )
162 return count * memoryMemorySizes[i].memorySize;