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.
18 #include "object-profiler.h"
20 #include <dali/integration-api/debug.h>
21 #include <dali/integration-api/profiling.h>
22 #include <dali/public-api/actors/image-actor.h>
23 #include <dali/public-api/common/stage.h>
24 #include <dali/public-api/object/ref-object.h>
25 #include <dali/public-api/object/base-object.h>
26 #include <dali/public-api/object/type-registry.h>
29 using namespace Dali::Integration::Profiling;
38 ObjectProfiler::ObjectProfiler()
41 // This class must be created after the Stage; this means it doesn't count the initial objects
42 // that are created by the stage (base layer, default camera actor)
43 mObjectRegistry = Dali::Stage::GetCurrent().GetObjectRegistry();
45 char* profile = getenv("PROFILE_DALI_OBJECTS");
49 int timeInterval = atoi(profile);
50 if( timeInterval > 0 )
52 mTimer = Dali::Timer::New(timeInterval*1000);
53 mTimer.TickSignal().Connect( this, &ObjectProfiler::OnTimeout );
57 mObjectRegistry.ObjectCreatedSignal().Connect( this, &ObjectProfiler::OnObjectCreated );
58 mObjectRegistry.ObjectDestroyedSignal().Connect( this, &ObjectProfiler::OnObjectDestroyed );
62 ObjectProfiler::~ObjectProfiler()
66 void ObjectProfiler::DisplayInstanceCounts()
68 InstanceCountMapIterator iter = mInstanceCountMap.begin();
69 InstanceCountMapIterator end = mInstanceCountMap.end();
71 for( ; iter != end; iter++ )
73 int memorySize = GetMemorySize(iter->first, iter->second);
76 LogMessage( Debug::DebugInfo, "%-30s: % 4d Memory MemorySize: ~% 6.1f kB\n",
77 iter->first.c_str(), iter->second, memorySize / 1024.0f );
81 LogMessage( Debug::DebugInfo, "%-30s: % 4d\n",
82 iter->first.c_str(), iter->second );
85 LogMessage(Debug::DebugInfo, "\n");
89 // Count number of quads:
91 for( InstanceTypes::iterator iter = mInstanceTypes.begin(), end = mInstanceTypes.end(); iter != end; ++iter )
93 if( iter->second.compare("ImageActor") == 0 )
95 BaseHandle handle(iter->first);
96 Dali::ImageActor imageActor = Dali::ImageActor::DownCast(handle);
99 if( imageActor.GetStyle() == Dali::ImageActor::STYLE_QUAD )
107 LogMessage(Debug::DebugInfo, "Number of image actors using Quad style: %d\n", quadCount);
110 bool ObjectProfiler::OnTimeout()
112 DisplayInstanceCounts();
116 void ObjectProfiler::OnObjectCreated(BaseHandle handle)
118 string theType = handle.GetTypeName();
119 if( theType.empty() )
121 DALI_LOG_ERROR("Object created from an unregistered type\n");
122 theType = "<Unregistered>";
125 mInstanceTypes.push_back(InstanceTypePair(&handle.GetBaseObject(), theType));
127 InstanceCountMapIterator iter = mInstanceCountMap.find(theType);
128 if( iter == mInstanceCountMap.end() )
130 InstanceCountPair instanceCount(theType, 1);
131 mInstanceCountMap.insert(instanceCount);
139 void ObjectProfiler::OnObjectDestroyed(const Dali::RefObject* object)
141 const BaseObject* baseObject = static_cast<const BaseObject*>(object);
143 InstanceTypes::iterator end = mInstanceTypes.end();
144 for( InstanceTypes::iterator iter = mInstanceTypes.begin(); iter != end; iter++)
146 if( iter->first == baseObject )
148 const std::string& theType = iter->second;
149 if( !theType.empty() )
151 InstanceCountMapIterator countIter = mInstanceCountMap.find(theType);
152 if( countIter != mInstanceCountMap.end() )
157 mInstanceTypes.erase( iter );
163 int ObjectProfiler::GetMemorySize(const std::string& name, int count)
165 struct MemoryMemorySize
170 MemoryMemorySize memoryMemorySizes[] =
172 { "Animation", ANIMATION_MEMORY_SIZE },
173 { "Constraint", CONSTRAINT_MEMORY_SIZE },
174 { "Actor", ACTOR_MEMORY_SIZE },
175 { "Layer", LAYER_MEMORY_SIZE },
176 { "CameraActor", CAMERA_ACTOR_MEMORY_SIZE },
177 { "ImageActor", IMAGE_ACTOR_MEMORY_SIZE },
178 { "MeshActor", MESH_ACTOR_MEMORY_SIZE },
179 { "Image", IMAGE_MEMORY_SIZE },
180 { "Mesh", MESH_MEMORY_SIZE },
181 { "Material", MATERIAL_MEMORY_SIZE },
184 for( size_t i=0; i<sizeof(memoryMemorySizes)/sizeof(MemoryMemorySize); i++ )
186 if( memoryMemorySizes[i].name.compare(name) == 0 )
188 return count * memoryMemorySizes[i].memorySize;