OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "athena/resource_manager/public/resource_manager.h" | 5 #include "athena/resource_manager/public/resource_manager.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "athena/activity/public/activity.h" | 10 #include "athena/activity/public/activity.h" |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 // Remember that the activity order has changed. | 169 // Remember that the activity order has changed. |
170 activity_order_changed_ = true; | 170 activity_order_changed_ = true; |
171 } | 171 } |
172 | 172 |
173 void ResourceManagerImpl::OnOverviewModeEnter() { | 173 void ResourceManagerImpl::OnOverviewModeEnter() { |
174 in_overview_mode_ = true; | 174 in_overview_mode_ = true; |
175 } | 175 } |
176 | 176 |
177 void ResourceManagerImpl::OnOverviewModeExit() { | 177 void ResourceManagerImpl::OnOverviewModeExit() { |
178 in_overview_mode_ = false; | 178 in_overview_mode_ = false; |
179 // Reorder the activities. | 179 // Reorder the activities and manage the resources again since an order change |
| 180 // might have caused a visibility change. |
180 UpdateActivityOrder(); | 181 UpdateActivityOrder(); |
| 182 ManageResource(); |
181 } | 183 } |
182 | 184 |
183 void ResourceManagerImpl::OnSplitViewModeEnter() { | 185 void ResourceManagerImpl::OnSplitViewModeEnter() { |
184 // Re-apply the memory pressure to make sure enough items are visible. | 186 // Re-apply the memory pressure to make sure enough items are visible. |
185 in_splitview_mode_ = true; | 187 in_splitview_mode_ = true; |
186 ManageResource(); | 188 ManageResource(); |
187 } | 189 } |
188 | 190 |
189 | 191 |
190 void ResourceManagerImpl::OnSplitViewModeExit() { | 192 void ResourceManagerImpl::OnSplitViewModeExit() { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 case MEMORY_PRESSURE_MODERATE: | 254 case MEMORY_PRESSURE_MODERATE: |
253 max_running_activities = 7; | 255 max_running_activities = 7; |
254 break; | 256 break; |
255 case MEMORY_PRESSURE_LOW: | 257 case MEMORY_PRESSURE_LOW: |
256 // This doesn't really matter. We do not change anything but turning | 258 // This doesn't really matter. We do not change anything but turning |
257 // activities visible. | 259 // activities visible. |
258 max_running_activities = 10000; | 260 max_running_activities = 10000; |
259 break; | 261 break; |
260 } | 262 } |
261 | 263 |
262 // The first n activities should be trated as "visible", means they updated | 264 // The first n activities should be treated as "visible", means they updated |
263 // in overview mode and will keep their layer resources for faster switch | 265 // in overview mode and will keep their layer resources for faster switch |
264 // times. Usually we use |kMaxVisibleActivities| items, but when the memory | 266 // times. Usually we use |kMaxVisibleActivities| items, but when the memory |
265 // pressure gets critical we only hold as many as are really visible. | 267 // pressure gets critical we only hold as many as are really visible. |
266 size_t max_activities = kMaxVisibleActivities; | 268 size_t max_activities = kMaxVisibleActivities; |
267 if (current_memory_pressure_ == MEMORY_PRESSURE_CRITICAL) | 269 if (current_memory_pressure_ == MEMORY_PRESSURE_CRITICAL) |
268 max_activities = 1 + (in_splitview_mode_ ? 1 : 0); | 270 max_activities = 1 + (in_splitview_mode_ ? 1 : 0); |
269 | 271 |
270 // Restart and / or bail if the order of activities changes due to our calls. | 272 // Restart and / or bail if the order of activities changes due to our calls. |
271 activity_order_changed_ = false; | 273 activity_order_changed_ = false; |
272 | 274 |
273 // Change the visibility of our activities in a pre-processing step. This is | 275 // Change the visibility of our activities in a pre-processing step. This is |
274 // required since it might change the order/number of activities. | 276 // required since it might change the order/number of activities. |
275 size_t index = 0; | 277 size_t index = 0; |
276 while (index < activity_list_.size()) { | 278 while (index < activity_list_.size()) { |
277 Activity* activity = activity_list_[index]; | 279 Activity* activity = activity_list_[index]; |
278 Activity::ActivityState state = activity->GetCurrentState(); | 280 Activity::ActivityState state = activity->GetCurrentState(); |
279 | 281 |
280 // The first |kMaxVisibleActivities| entries should be visible, all others | 282 // The first |kMaxVisibleActivities| entries should be visible, all others |
281 // invisible or at a lower activity state. | 283 // invisible or at a lower activity state. |
282 if (index < max_activities || | 284 if (index < max_activities || |
283 (state == Activity::ACTIVITY_INVISIBLE || | 285 (state == Activity::ACTIVITY_INVISIBLE || |
284 state == Activity::ACTIVITY_VISIBLE)) { | 286 state == Activity::ACTIVITY_VISIBLE)) { |
285 Activity::ActivityState visiblity_state = | 287 Activity::ActivityState visiblity_state = |
286 index < max_activities ? Activity::ACTIVITY_VISIBLE : | 288 index < max_activities ? Activity::ACTIVITY_VISIBLE : |
287 Activity::ACTIVITY_INVISIBLE; | 289 Activity::ACTIVITY_INVISIBLE; |
288 // Only change the state when it changes. Note that when the memory | 290 // Only change the state when it changes. Note that when the memory |
289 // pressure is critical, only the primary activities (1 or 2) are made | 291 // pressure is critical, only the primary activities (1 or 2) are made |
290 // visible. Furthermore, in relaxed mode we only want to make visible. | 292 // visible. Furthermore, in relaxed mode we only want to turn visible, |
| 293 // never invisible. |
291 if (visiblity_state != state && | 294 if (visiblity_state != state && |
292 (current_memory_pressure_ != MEMORY_PRESSURE_LOW || | 295 (current_memory_pressure_ != MEMORY_PRESSURE_LOW || |
293 visiblity_state == Activity::ACTIVITY_VISIBLE)) | 296 visiblity_state == Activity::ACTIVITY_VISIBLE)) { |
294 activity->SetCurrentState(visiblity_state); | 297 activity->SetCurrentState(visiblity_state); |
| 298 // If we turned an activity invisible, we should not at the same time |
| 299 // throw an activity out of memory. Thus we grant one more invisible |
| 300 // Activity in that case. |
| 301 if (visiblity_state == Activity::ACTIVITY_INVISIBLE) |
| 302 max_running_activities++; |
| 303 } |
295 } | 304 } |
296 | 305 |
297 // See which index we should handle next. | 306 // See which index we should handle next. |
298 if (activity_order_changed_) { | 307 if (activity_order_changed_) { |
299 activity_order_changed_ = false; | 308 activity_order_changed_ = false; |
300 index = 0; | 309 index = 0; |
301 } else { | 310 } else { |
302 ++index; | 311 ++index; |
303 } | 312 } |
304 } | 313 } |
305 | 314 |
306 // No need to remove anything. | 315 // No need to remove anything. |
307 if (current_memory_pressure_ == MEMORY_PRESSURE_LOW) | 316 if (current_memory_pressure_ == MEMORY_PRESSURE_LOW) |
308 return; | 317 return; |
309 | 318 // TODO(skuhne): Do not release too many activities in short succession. |
| 319 // since it takes time to release resources. As such the time of last call |
| 320 // should be remembered and if called to early we should exit early. |
310 // Check if/which activity we want to unload. | 321 // Check if/which activity we want to unload. |
311 Activity* oldest_media_activity = NULL; | 322 Activity* oldest_media_activity = NULL; |
312 std::vector<Activity*> unloadable_activities; | 323 std::vector<Activity*> unloadable_activities; |
313 for (std::vector<Activity*>::iterator it = activity_list_.begin(); | 324 for (std::vector<Activity*>::iterator it = activity_list_.begin(); |
314 it != activity_list_.end(); ++it) { | 325 it != activity_list_.end(); ++it) { |
315 Activity::ActivityState state = (*it)->GetCurrentState(); | 326 Activity::ActivityState state = (*it)->GetCurrentState(); |
316 // The activity should neither be unloaded nor visible. | 327 // The activity should neither be unloaded nor visible. |
317 if (state != Activity::ACTIVITY_UNLOADED && | 328 if (state != Activity::ACTIVITY_UNLOADED && |
318 state != Activity::ACTIVITY_VISIBLE) { | 329 state != Activity::ACTIVITY_VISIBLE) { |
319 if ((*it)->GetMediaState() == Activity::ACTIVITY_MEDIA_STATE_NONE) { | 330 if ((*it)->GetMediaState() == Activity::ACTIVITY_MEDIA_STATE_NONE) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 } | 410 } |
400 | 411 |
401 ResourceManager::ResourceManager() {} | 412 ResourceManager::ResourceManager() {} |
402 | 413 |
403 ResourceManager::~ResourceManager() { | 414 ResourceManager::~ResourceManager() { |
404 DCHECK(instance); | 415 DCHECK(instance); |
405 instance = NULL; | 416 instance = NULL; |
406 } | 417 } |
407 | 418 |
408 } // namespace athena | 419 } // namespace athena |
OLD | NEW |