|
1 /* |
|
2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Garbage collector |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 #include "glxgarbagecollector.h" |
|
22 |
|
23 #include <glxassert.h> |
|
24 #include <glxtracer.h> |
|
25 #include <glxlog.h> |
|
26 |
|
27 #include "glxcache.h" |
|
28 #include "glxerrormanager.h" |
|
29 #include "glxmedia.h" |
|
30 #include "glxmedialist.h" |
|
31 #include "mglxmediauser.h" |
|
32 #include "glxthumbnailutility.h" |
|
33 |
|
34 /** |
|
35 * Interval for the periodic timer, in microseconds |
|
36 */ |
|
37 const TInt KPeriodicInterval = 500000; |
|
38 /** |
|
39 * Start Delay for the periodic timer, in microseconds |
|
40 */ |
|
41 const TInt KPeriodicStartDelay = 300000; // It is Changed to 300Micro Second ; To be able to give Others a Chance to Run before it starts |
|
42 /** |
|
43 * Max number of items to scan per periodic callback |
|
44 * @todo Find optimal value for this |
|
45 */ |
|
46 const TInt KMaxScannedMediaCountPerPeriodicCallback = 15; // Changed from 30; Still there is Scope for Nominal Value Or Dynamic Value |
|
47 |
|
48 /** |
|
49 * Max number of attributes that an item may have in use |
|
50 */ |
|
51 const TInt KMaxAttributesInUse = 50; |
|
52 |
|
53 // ----------------------------------------------------------------------------- |
|
54 // NewL |
|
55 // ----------------------------------------------------------------------------- |
|
56 // |
|
57 CGlxGarbageCollector* CGlxGarbageCollector::NewL( |
|
58 const RPointerArray<CGlxCache>& aCaches ) |
|
59 { |
|
60 TRACER( "CGlxGarbageCollector::NewL" ); |
|
61 |
|
62 CGlxGarbageCollector* self = new( ELeave ) CGlxGarbageCollector( aCaches ); |
|
63 CleanupStack::PushL( self ); |
|
64 self->ConstructL(); |
|
65 CleanupStack::Pop(self); |
|
66 return self; |
|
67 } |
|
68 |
|
69 // ----------------------------------------------------------------------------- |
|
70 // Constructor |
|
71 // ----------------------------------------------------------------------------- |
|
72 // |
|
73 CGlxGarbageCollector::CGlxGarbageCollector( |
|
74 const RPointerArray<CGlxCache>& aCaches ) |
|
75 : iCaches( aCaches ) |
|
76 { |
|
77 TRACER("CGlxGarbageCollector::Default Constructor"); |
|
78 } |
|
79 |
|
80 // ----------------------------------------------------------------------------- |
|
81 // ConstructL |
|
82 // ----------------------------------------------------------------------------- |
|
83 // |
|
84 void CGlxGarbageCollector::ConstructL() |
|
85 { |
|
86 TRACER("CGlxGarbageCollector::ConstructL"); |
|
87 |
|
88 // Create callback loop in the lowest priority, so that the garbage |
|
89 // collection does not slow down the application |
|
90 iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle ); |
|
91 iAttributesInUse.ReserveL(KMaxAttributesInUse); |
|
92 } |
|
93 |
|
94 // ----------------------------------------------------------------------------- |
|
95 // Destructor |
|
96 // ----------------------------------------------------------------------------- |
|
97 // |
|
98 CGlxGarbageCollector::~CGlxGarbageCollector() |
|
99 { |
|
100 TRACER("CGlxGarbageCollector::Destructor"); |
|
101 delete iPeriodic; |
|
102 iAttributesInUse.Close(); |
|
103 } |
|
104 |
|
105 // ----------------------------------------------------------------------------- |
|
106 // CleanupL |
|
107 // ----------------------------------------------------------------------------- |
|
108 // |
|
109 void CGlxGarbageCollector::CleanupL() |
|
110 { |
|
111 TRACER("CGlxGarbageCollector::CleanupL"); |
|
112 |
|
113 if ( !iPeriodic->IsActive() ) |
|
114 { |
|
115 iPeriodic->Start( KPeriodicStartDelay, KPeriodicInterval, TCallBack( &PeriodicCallbackL, static_cast<TAny*>( this ) ) ); |
|
116 } |
|
117 else |
|
118 { |
|
119 // a full cleanup round is required to make the cache fully clean, |
|
120 // if cleanup is currently ongoing. we don't know whether the cache |
|
121 // was modified before or after the current (cache or media) index being |
|
122 // cleaned up. |
|
123 iRequiresFullCleanupRound = ETrue; |
|
124 } |
|
125 } |
|
126 |
|
127 // ----------------------------------------------------------------------------- |
|
128 // CancelCleanup |
|
129 // ----------------------------------------------------------------------------- |
|
130 // |
|
131 void CGlxGarbageCollector::CancelCleanup() |
|
132 { |
|
133 TRACER("CGlxGarbageCollector::CancelCleanup"); |
|
134 |
|
135 if (iPeriodic->IsActive()) |
|
136 { |
|
137 iPeriodic->Cancel(); |
|
138 // Consideration of Restarting the timer is there; |
|
139 } |
|
140 } |
|
141 |
|
142 // ----------------------------------------------------------------------------- |
|
143 // Callback from periodic timer |
|
144 // ----------------------------------------------------------------------------- |
|
145 // |
|
146 TInt CGlxGarbageCollector::PeriodicCallbackL( TAny* aPtr ) |
|
147 { |
|
148 TRACER("CGlxGarbageCollector::PeriodicCallback"); |
|
149 |
|
150 GLX_ASSERT_DEBUG( aPtr != NULL, Panic( EGlxPanicLogicError ), |
|
151 "Received null pointer for garbage collector" ); |
|
152 |
|
153 // get "this" pointer |
|
154 static_cast< CGlxGarbageCollector* >( aPtr )->PeriodicCallbackL(); |
|
155 |
|
156 // return value ignored for periodic timers |
|
157 return 0; |
|
158 } |
|
159 |
|
160 // ----------------------------------------------------------------------------- |
|
161 // Flush Count Pages |
|
162 // ----------------------------------------------------------------------------- |
|
163 // |
|
164 void CGlxGarbageCollector::FlushPagesL(TInt aCount) |
|
165 { |
|
166 TBool reachedEnd = CleanupCaches(aCount); |
|
167 |
|
168 if ( reachedEnd ) |
|
169 { |
|
170 // reset to the start of the caches |
|
171 iScanningPosition.iCurrentCacheIndex = 0; |
|
172 iScanningPosition.iNextMediaIndexToCleanup = 0; |
|
173 |
|
174 // next time the full round reaches end, the caches will be fully clean |
|
175 // (unless client calls Cleanup() ) |
|
176 iRequiresFullCleanupRound = EFalse; |
|
177 } |
|
178 } |
|
179 // ----------------------------------------------------------------------------- |
|
180 // Callback from periodic timer |
|
181 // inlined in cpp only => inlines for arm |
|
182 // ----------------------------------------------------------------------------- |
|
183 // |
|
184 inline void CGlxGarbageCollector::PeriodicCallbackL() |
|
185 { |
|
186 TRACER("CGlxGarbageCollector::PeriodicCallbackL"); |
|
187 |
|
188 TBool reachedEnd = CleanupCaches(); |
|
189 |
|
190 // determine whether idle callback should be cancelled (note that check |
|
191 // needs to be done before resetting iRequiresFullCleanupRound below) |
|
192 // do not cancel if reached the end, but cache is dirty |
|
193 if ( reachedEnd && !iRequiresFullCleanupRound ) |
|
194 { |
|
195 iPeriodic->Cancel(); |
|
196 } |
|
197 |
|
198 // if scanned to the end of caches, set up for the next cleanup round |
|
199 if ( reachedEnd ) |
|
200 { |
|
201 // reset to the start of the caches |
|
202 iScanningPosition.iCurrentCacheIndex = 0; |
|
203 iScanningPosition.iNextMediaIndexToCleanup = 0; |
|
204 |
|
205 // next time the full round reaches end, the caches will be fully clean |
|
206 // (unless client calls Cleanup() ) |
|
207 iRequiresFullCleanupRound = EFalse; |
|
208 } |
|
209 } |
|
210 |
|
211 // ----------------------------------------------------------------------------- |
|
212 // Clean up all caches |
|
213 // ----------------------------------------------------------------------------- |
|
214 // |
|
215 TBool CGlxGarbageCollector::CleanupCaches(TInt aCount) |
|
216 { |
|
217 TRACER( "CGlxGarbageCollector::CleanupCaches" ); |
|
218 |
|
219 TInt remainingScanCount = 0; |
|
220 |
|
221 // set the maximum number of items to scan during this call |
|
222 // Count is Needed for Flushing Direct |
|
223 remainingScanCount = aCount*KMaxScannedMediaCountPerPeriodicCallback; |
|
224 |
|
225 // Iterate through all cache until scanned enough items (remainingScanCount) |
|
226 // (unlikely to have many caches, so ok to call RPointerArray::Count() on |
|
227 // each iteration) |
|
228 while ( iScanningPosition.iCurrentCacheIndex < iCaches.Count() ) |
|
229 { |
|
230 // clean up current cache |
|
231 remainingScanCount = CleanupCache( |
|
232 *iCaches[iScanningPosition.iCurrentCacheIndex], remainingScanCount ); |
|
233 |
|
234 // exit the loop if reached full scan count, since the above loop might |
|
235 // have reached the end of the cache. (so don't increment the current |
|
236 // cache index) |
|
237 if ( 0 == remainingScanCount ) |
|
238 { |
|
239 break; |
|
240 } |
|
241 |
|
242 // set indexes to the beginning of the next cache |
|
243 iScanningPosition.iCurrentCacheIndex++; |
|
244 iScanningPosition.iNextMediaIndexToCleanup = 0; |
|
245 } |
|
246 |
|
247 // determine if there is anything more to clean up |
|
248 TBool reachedEndOfAllCaches = |
|
249 ( iScanningPosition.iCurrentCacheIndex == iCaches.Count() ); |
|
250 return reachedEndOfAllCaches; |
|
251 } |
|
252 |
|
253 // ----------------------------------------------------------------------------- |
|
254 // Clean up cache that is currently under cleanup |
|
255 // return number of items yet to scan during this idle callback |
|
256 // ----------------------------------------------------------------------------- |
|
257 // |
|
258 TInt CGlxGarbageCollector::CleanupCache( CGlxCache& aCache, |
|
259 TInt aRemainingScanCount ) |
|
260 { |
|
261 TRACER( "CGlxGarbageCollector::CleanupCache" ); |
|
262 |
|
263 // set the maximum number of items to scan during this call |
|
264 TInt remainingScanCount = aRemainingScanCount; |
|
265 |
|
266 // don't store cache.Count() locally, as the count changes during the loop |
|
267 while ( iScanningPosition.iNextMediaIndexToCleanup < aCache.Count() && |
|
268 remainingScanCount > 0 && ( aCache.IdSpaceId() != KGlxIdSpaceIdRoot ) ) |
|
269 { |
|
270 CGlxMedia& media = aCache.Media( iScanningPosition.iNextMediaIndexToCleanup ); |
|
271 |
|
272 if ( media.UserCount() > 0 ) |
|
273 { |
|
274 // the media object still has users. Check if any attributes |
|
275 // it has can be deleted. |
|
276 TRAPD( err, CleanupMediaL( aCache, media ) ); |
|
277 |
|
278 // skip the media object if cleanup failed. This allows cleanup to |
|
279 // continue also when has no free memory. Skip also if there |
|
280 // are still attributes in use. |
|
281 if ( ( media.Count() == 0 && !err ) || ( err > 0 ) ) |
|
282 { |
|
283 // the media object has no more attributes. It can be deleted. |
|
284 // don't increment iNextMediaIndexToCleanup since the index |
|
285 // of the next item will be decreased by one due to removal |
|
286 aCache.Delete( iScanningPosition.iNextMediaIndexToCleanup ); |
|
287 GLX_LOG_INFO1( "CGlxGarbageCollector - Deleted Media Id=%d since no attributes left", media.Id().Value()); |
|
288 } |
|
289 else if( err < 0 ) |
|
290 { |
|
291 // scan next item |
|
292 iScanningPosition.iNextMediaIndexToCleanup++; |
|
293 } |
|
294 else |
|
295 { |
|
296 // scan next item |
|
297 iScanningPosition.iNextMediaIndexToCleanup++; |
|
298 } |
|
299 } |
|
300 else |
|
301 { |
|
302 // the media object has no more users in present cache. It may be deleted |
|
303 if( !MediaInUse( media ) ) |
|
304 { |
|
305 // Bug Fix @ EDDG-7V3CJA:: Recheck other caches for UserCount |
|
306 aCache.Delete( iScanningPosition.iNextMediaIndexToCleanup ); |
|
307 GLX_LOG_INFO1( "CGlxGarbageCollector - Deleted Media Id=%d since no users left", media.Id().Value()); |
|
308 } |
|
309 } |
|
310 remainingScanCount--; |
|
311 } |
|
312 |
|
313 return remainingScanCount; |
|
314 } |
|
315 |
|
316 // ----------------------------------------------------------------------------- |
|
317 // Perform cleanup on provided media object |
|
318 // ----------------------------------------------------------------------------- |
|
319 // |
|
320 void CGlxGarbageCollector::CleanupMediaL( CGlxCache& aCache, CGlxMedia& aMedia ) |
|
321 { |
|
322 TRACER("CGlxGarbageCollector::CleanupMediaL"); |
|
323 |
|
324 // check which attributes can be deleted. |
|
325 iAttributesInUse.Reset(); |
|
326 |
|
327 GetAttributesInUseL( aMedia, iAttributesInUse ); |
|
328 if ( iAttributesInUse.Count() == 0 ) |
|
329 { |
|
330 User::Leave(1); |
|
331 } |
|
332 GlxErrorManager::ClearExpiredAndUnusedErrorsL( aMedia, iAttributesInUse ); |
|
333 |
|
334 // add the error attribute to the list of attributes that are in use, so |
|
335 // that it won't be deleted by the loop below. |
|
336 // (if there are no errors left, GlxErrorManager::ClearExpiredAndNotInUseErrorsL |
|
337 // would have deleted the error attribute from aMedia, so checking for it |
|
338 // does no harm) |
|
339 iAttributesInUse.AppendL( GlxErrorManager::ErrorAttribute() ); |
|
340 |
|
341 // delete all attributes that are not being used |
|
342 DeleteOtherAttributes( aCache, aMedia, iAttributesInUse ); |
|
343 } |
|
344 |
|
345 // ----------------------------------------------------------------------------- |
|
346 // GetAttributesInUseL |
|
347 // ----------------------------------------------------------------------------- |
|
348 // |
|
349 void CGlxGarbageCollector::GetAttributesInUseL( const CGlxMedia& aMedia, |
|
350 RArray<TMPXAttribute>& aAttributes ) const |
|
351 { |
|
352 TRACER("CGlxGarbageCollector::GetAttributesInUseL"); |
|
353 |
|
354 // get needed attributes from each user of the media object |
|
355 // ( unlikely to have many users, so calling CGlxMedia::UserCount and |
|
356 // CGlxMedia::Id within the loop ) |
|
357 for ( TInt userIndex = 0; userIndex < aMedia.UserCount(); userIndex++ ) |
|
358 { |
|
359 aMedia.User( userIndex ).GetRequiredAttributesL( aMedia.IndexInUser( userIndex ), |
|
360 aAttributes ); |
|
361 } |
|
362 } |
|
363 |
|
364 // ----------------------------------------------------------------------------- |
|
365 // Delete all attributes from a media object except those specified |
|
366 // ----------------------------------------------------------------------------- |
|
367 // |
|
368 void CGlxGarbageCollector::DeleteOtherAttributes( CGlxCache& aCache, CGlxMedia& aMedia, |
|
369 const RArray<TMPXAttribute>& aAttributesToKeep ) const |
|
370 { |
|
371 TRACER("CGlxGarbageCollector::DeleteOtherAttributes"); |
|
372 |
|
373 // loop backwards so can delete attributes during the loop |
|
374 for ( TInt attrIndex = aMedia.Count() - 1; attrIndex >= 0; attrIndex-- ) |
|
375 { |
|
376 // delete the attribute if it is not in use |
|
377 const TMPXAttribute& attrib = aMedia.Attribute(attrIndex); |
|
378 if ( KErrNotFound == aAttributesToKeep.Find( attrib, TMPXAttribute::Match ) ) |
|
379 { |
|
380 GLX_LOG_INFO( "CGlxGarbageCollector::DeleteOtherAttributes() - Deleted attribute" ); |
|
381 aMedia.DeleteAttribute( attrIndex ); |
|
382 if (GlxThumbnailUtility::IsFullThumbnail(attrib)) |
|
383 { |
|
384 GLX_DEBUG2("CGlxGarbageCollector::DeleteOtherAttributes(*** TN ***) aMediaId(%d)", |
|
385 aMedia.Id().Value()); |
|
386 aCache.CleanupMedia(aMedia.Id()); |
|
387 } |
|
388 } |
|
389 } |
|
390 } |
|
391 |
|
392 // ----------------------------------------------------------------------------- |
|
393 // Check for User Count for media to be deleted in the remaining caches |
|
394 // ----------------------------------------------------------------------------- |
|
395 // |
|
396 TBool CGlxGarbageCollector::MediaInUse(const CGlxMedia& aMedia) const |
|
397 { |
|
398 // Bug Fix @ EDDG-7V3CJA :: If media has non zero user count in any of the caches |
|
399 // then deletion of texture is to be avoided. |
|
400 for(TInt cacheCount = 0; cacheCount < iCaches.Count(); cacheCount++ ) |
|
401 { |
|
402 if ( ( iCaches[cacheCount]->Media( aMedia.Id() ) ) |
|
403 && ( iCaches[cacheCount]->IdSpaceId() != KGlxIdSpaceIdRoot ) ) |
|
404 { |
|
405 if ( ( iCaches[cacheCount]->Media(aMedia.Id() ) )->UserCount() > 0 ) |
|
406 { |
|
407 return ETrue; |
|
408 } |
|
409 } |
|
410 } |
|
411 return EFalse; |
|
412 } |