2 * Copyright (c) 2014 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 "object-profiler.h"
23 #include <dali/integration-api/debug.h>
24 #include <dali/integration-api/profiling.h>
25 #include <dali/public-api/actors/image-actor.h>
26 #include <dali/public-api/common/stage.h>
27 #include <dali/public-api/object/ref-object.h>
28 #include <dali/public-api/object/base-object.h>
29 #include <dali/public-api/object/type-registry.h>
32 using namespace Dali::Integration::Profiling;
41 ObjectProfiler::ObjectProfiler( unsigned int timeInterval )
43 // This class must be created after the Stage; this means it doesn't count the initial objects
44 // that are created by the stage (base layer, default camera actor)
45 mObjectRegistry = Dali::Stage::GetCurrent().GetObjectRegistry();
47 mTimer = Dali::Timer::New( timeInterval * 1000 );
48 mTimer.TickSignal().Connect( this, &ObjectProfiler::OnTimeout );
51 mObjectRegistry.ObjectCreatedSignal().Connect( this, &ObjectProfiler::OnObjectCreated );
52 mObjectRegistry.ObjectDestroyedSignal().Connect( this, &ObjectProfiler::OnObjectDestroyed );
55 ObjectProfiler::~ObjectProfiler()
59 void ObjectProfiler::DisplayInstanceCounts()
61 InstanceCountMapIterator iter = mInstanceCountMap.begin();
62 InstanceCountMapIterator end = mInstanceCountMap.end();
64 for( ; iter != end; iter++ )
66 int memorySize = GetMemorySize(iter->first, iter->second);
69 LogMessage( Debug::DebugInfo, "%-30s: % 4d Memory MemorySize: ~% 6.1f kB\n",
70 iter->first.c_str(), iter->second, memorySize / 1024.0f );
74 LogMessage( Debug::DebugInfo, "%-30s: % 4d\n",
75 iter->first.c_str(), iter->second );
78 LogMessage(Debug::DebugInfo, "\n");
81 bool ObjectProfiler::OnTimeout()
83 DisplayInstanceCounts();
87 void ObjectProfiler::OnObjectCreated(BaseHandle handle)
89 string theType = handle.GetTypeName();
92 DALI_LOG_ERROR("Object created from an unregistered type\n");
93 theType = "<Unregistered>";
96 mInstanceTypes.push_back(InstanceTypePair(&handle.GetBaseObject(), theType));
98 InstanceCountMapIterator iter = mInstanceCountMap.find(theType);
99 if( iter == mInstanceCountMap.end() )
101 InstanceCountPair instanceCount(theType, 1);
102 mInstanceCountMap.insert(instanceCount);
110 void ObjectProfiler::OnObjectDestroyed(const Dali::RefObject* object)
112 const BaseObject* baseObject = static_cast<const BaseObject*>(object);
114 InstanceTypes::iterator end = mInstanceTypes.end();
115 for( InstanceTypes::iterator iter = mInstanceTypes.begin(); iter != end; iter++)
117 if( iter->first == baseObject )
119 const std::string& theType = iter->second;
120 if( !theType.empty() )
122 InstanceCountMapIterator countIter = mInstanceCountMap.find(theType);
123 if( countIter != mInstanceCountMap.end() )
128 mInstanceTypes.erase( iter );
134 int ObjectProfiler::GetMemorySize(const std::string& name, int count)
136 struct MemoryMemorySize
141 MemoryMemorySize memoryMemorySizes[] =
143 { "Animation", ANIMATION_MEMORY_SIZE },
144 { "Constraint", CONSTRAINT_MEMORY_SIZE },
145 { "Actor", ACTOR_MEMORY_SIZE },
146 { "Layer", LAYER_MEMORY_SIZE },
147 { "CameraActor", CAMERA_ACTOR_MEMORY_SIZE },
148 { "Image", IMAGE_MEMORY_SIZE },
149 { "Renderer", RENDERER_MEMORY_SIZE },
150 { "Geometry", GEOMETRY_MEMORY_SIZE },
151 { "PropertyBuffer", PROPERTY_BUFFER_MEMORY_SIZE },
152 { "TextureSet", TEXTURE_SET_MEMORY_SIZE },
153 { "Sampler", SAMPLER_MEMORY_SIZE },
154 { "Shader", SHADER_MEMORY_SIZE },
157 for( size_t i=0; i<sizeof(memoryMemorySizes)/sizeof(MemoryMemorySize); i++ )
159 if( memoryMemorySizes[i].name.compare(name) == 0 )
161 return count * memoryMemorySizes[i].memorySize;