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()
44 // This class must be created after the Stage; this means it doesn't count the initial objects
45 // that are created by the stage (base layer, default camera actor)
46 mObjectRegistry = Dali::Stage::GetCurrent().GetObjectRegistry();
48 char* profile = getenv("PROFILE_DALI_OBJECTS");
52 int timeInterval = atoi(profile);
53 if( timeInterval > 0 )
55 mTimer = Dali::Timer::New(timeInterval*1000);
56 mTimer.TickSignal().Connect( this, &ObjectProfiler::OnTimeout );
60 mObjectRegistry.ObjectCreatedSignal().Connect( this, &ObjectProfiler::OnObjectCreated );
61 mObjectRegistry.ObjectDestroyedSignal().Connect( this, &ObjectProfiler::OnObjectDestroyed );
65 ObjectProfiler::~ObjectProfiler()
69 void ObjectProfiler::DisplayInstanceCounts()
71 InstanceCountMapIterator iter = mInstanceCountMap.begin();
72 InstanceCountMapIterator end = mInstanceCountMap.end();
74 for( ; iter != end; iter++ )
76 int memorySize = GetMemorySize(iter->first, iter->second);
79 LogMessage( Debug::DebugInfo, "%-30s: % 4d Memory MemorySize: ~% 6.1f kB\n",
80 iter->first.c_str(), iter->second, memorySize / 1024.0f );
84 LogMessage( Debug::DebugInfo, "%-30s: % 4d\n",
85 iter->first.c_str(), iter->second );
88 LogMessage(Debug::DebugInfo, "\n");
92 // Count number of quads:
94 for( InstanceTypes::iterator iter = mInstanceTypes.begin(), end = mInstanceTypes.end(); iter != end; ++iter )
96 if( iter->second.compare("ImageActor") == 0 )
98 BaseHandle handle(iter->first);
99 Dali::ImageActor imageActor = Dali::ImageActor::DownCast(handle);
102 if( imageActor.GetStyle() == Dali::ImageActor::STYLE_QUAD )
110 LogMessage(Debug::DebugInfo, "Number of image actors using Quad style: %d\n", quadCount);
113 bool ObjectProfiler::OnTimeout()
115 DisplayInstanceCounts();
119 void ObjectProfiler::OnObjectCreated(BaseHandle handle)
121 string theType = handle.GetTypeName();
122 if( theType.empty() )
124 DALI_LOG_ERROR("Object created from an unregistered type\n");
125 theType = "<Unregistered>";
128 mInstanceTypes.push_back(InstanceTypePair(&handle.GetBaseObject(), theType));
130 InstanceCountMapIterator iter = mInstanceCountMap.find(theType);
131 if( iter == mInstanceCountMap.end() )
133 InstanceCountPair instanceCount(theType, 1);
134 mInstanceCountMap.insert(instanceCount);
142 void ObjectProfiler::OnObjectDestroyed(const Dali::RefObject* object)
144 const BaseObject* baseObject = static_cast<const BaseObject*>(object);
146 InstanceTypes::iterator end = mInstanceTypes.end();
147 for( InstanceTypes::iterator iter = mInstanceTypes.begin(); iter != end; iter++)
149 if( iter->first == baseObject )
151 const std::string& theType = iter->second;
152 if( !theType.empty() )
154 InstanceCountMapIterator countIter = mInstanceCountMap.find(theType);
155 if( countIter != mInstanceCountMap.end() )
160 mInstanceTypes.erase( iter );
166 int ObjectProfiler::GetMemorySize(const std::string& name, int count)
168 struct MemoryMemorySize
173 MemoryMemorySize memoryMemorySizes[] =
175 { "Animation", ANIMATION_MEMORY_SIZE },
176 { "Constraint", CONSTRAINT_MEMORY_SIZE },
177 { "Actor", ACTOR_MEMORY_SIZE },
178 { "Layer", LAYER_MEMORY_SIZE },
179 { "CameraActor", CAMERA_ACTOR_MEMORY_SIZE },
180 { "ImageActor", IMAGE_ACTOR_MEMORY_SIZE },
181 { "TextActor", TEXT_ACTOR_MEMORY_SIZE },
182 { "Image", IMAGE_MEMORY_SIZE },
185 for( size_t i=0; i<sizeof(memoryMemorySizes)/sizeof(MemoryMemorySize); i++ )
187 if( memoryMemorySizes[i].name.compare(name) == 0 )
189 return count * memoryMemorySizes[i].memorySize;