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 "chrome/browser/android/thumbnail/thumbnail_cache.h" | 5 #include "chrome/browser/android/thumbnail/thumbnail_cache.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 | 198 |
199 Thumbnail* ThumbnailCache::Get(TabId tab_id, | 199 Thumbnail* ThumbnailCache::Get(TabId tab_id, |
200 bool force_disk_read, | 200 bool force_disk_read, |
201 bool allow_approximation) { | 201 bool allow_approximation) { |
202 Thumbnail* thumbnail = cache_.Get(tab_id); | 202 Thumbnail* thumbnail = cache_.Get(tab_id); |
203 if (thumbnail) { | 203 if (thumbnail) { |
204 thumbnail->CreateUIResource(); | 204 thumbnail->CreateUIResource(); |
205 return thumbnail; | 205 return thumbnail; |
206 } | 206 } |
207 | 207 |
208 if (force_disk_read && | 208 if (force_disk_read && base::ContainsValue(visible_ids_, tab_id) && |
209 ContainsValue(visible_ids_, tab_id) && | 209 !base::ContainsValue(read_queue_, tab_id)) { |
210 !ContainsValue(read_queue_, tab_id)) { | |
211 read_queue_.push_back(tab_id); | 210 read_queue_.push_back(tab_id); |
212 ReadNextThumbnail(); | 211 ReadNextThumbnail(); |
213 } | 212 } |
214 | 213 |
215 if (allow_approximation) { | 214 if (allow_approximation) { |
216 thumbnail = approximation_cache_.Get(tab_id); | 215 thumbnail = approximation_cache_.Get(tab_id); |
217 if (thumbnail) { | 216 if (thumbnail) { |
218 thumbnail->CreateUIResource(); | 217 thumbnail->CreateUIResource(); |
219 return thumbnail; | 218 return thumbnail; |
220 } | 219 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 return; | 287 return; |
289 } | 288 } |
290 | 289 |
291 read_queue_.clear(); | 290 read_queue_.clear(); |
292 visible_ids_.clear(); | 291 visible_ids_.clear(); |
293 size_t count = 0; | 292 size_t count = 0; |
294 TabIdList::const_iterator iter = priority.begin(); | 293 TabIdList::const_iterator iter = priority.begin(); |
295 while (iter != priority.end() && count < ids_size) { | 294 while (iter != priority.end() && count < ids_size) { |
296 TabId tab_id = *iter; | 295 TabId tab_id = *iter; |
297 visible_ids_.push_back(tab_id); | 296 visible_ids_.push_back(tab_id); |
298 if (!cache_.Get(tab_id) && | 297 if (!cache_.Get(tab_id) && !base::ContainsValue(read_queue_, tab_id)) |
299 !ContainsValue(read_queue_, tab_id)) | |
300 read_queue_.push_back(tab_id); | 298 read_queue_.push_back(tab_id); |
301 iter++; | 299 iter++; |
302 count++; | 300 count++; |
303 } | 301 } |
304 | 302 |
305 ReadNextThumbnail(); | 303 ReadNextThumbnail(); |
306 } | 304 } |
307 | 305 |
308 void ThumbnailCache::DecompressThumbnailFromFile( | 306 void ThumbnailCache::DecompressThumbnailFromFile( |
309 TabId tab_id, | 307 TabId tab_id, |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 post_read_task = base::Bind( | 394 post_read_task = base::Bind( |
397 &ThumbnailCache::PostReadTask, weak_factory_.GetWeakPtr(), tab_id); | 395 &ThumbnailCache::PostReadTask, weak_factory_.GetWeakPtr(), tab_id); |
398 | 396 |
399 content::BrowserThread::PostTask( | 397 content::BrowserThread::PostTask( |
400 content::BrowserThread::FILE, | 398 content::BrowserThread::FILE, |
401 FROM_HERE, | 399 FROM_HERE, |
402 base::Bind(&ThumbnailCache::ReadTask, false, tab_id, post_read_task)); | 400 base::Bind(&ThumbnailCache::ReadTask, false, tab_id, post_read_task)); |
403 } | 401 } |
404 | 402 |
405 void ThumbnailCache::MakeSpaceForNewItemIfNecessary(TabId tab_id) { | 403 void ThumbnailCache::MakeSpaceForNewItemIfNecessary(TabId tab_id) { |
406 if (cache_.Get(tab_id) || | 404 if (cache_.Get(tab_id) || !base::ContainsValue(visible_ids_, tab_id) || |
407 !ContainsValue(visible_ids_, tab_id) || | |
408 cache_.size() < cache_.MaximumCacheSize()) { | 405 cache_.size() < cache_.MaximumCacheSize()) { |
409 return; | 406 return; |
410 } | 407 } |
411 | 408 |
412 TabId key_to_remove; | 409 TabId key_to_remove; |
413 bool found_key_to_remove = false; | 410 bool found_key_to_remove = false; |
414 | 411 |
415 // 1. Find a cached item not in this list | 412 // 1. Find a cached item not in this list |
416 for (ExpiringThumbnailCache::iterator iter = cache_.begin(); | 413 for (ExpiringThumbnailCache::iterator iter = cache_.begin(); |
417 iter != cache_.end(); | 414 iter != cache_.end(); |
418 iter++) { | 415 iter++) { |
419 if (!ContainsValue(visible_ids_, iter->first)) { | 416 if (!base::ContainsValue(visible_ids_, iter->first)) { |
420 key_to_remove = iter->first; | 417 key_to_remove = iter->first; |
421 found_key_to_remove = true; | 418 found_key_to_remove = true; |
422 break; | 419 break; |
423 } | 420 } |
424 } | 421 } |
425 | 422 |
426 if (!found_key_to_remove) { | 423 if (!found_key_to_remove) { |
427 // 2. Find the least important id we can remove. | 424 // 2. Find the least important id we can remove. |
428 for (TabIdList::reverse_iterator riter = visible_ids_.rbegin(); | 425 for (TabIdList::reverse_iterator riter = visible_ids_.rbegin(); |
429 riter != visible_ids_.rend(); | 426 riter != visible_ids_.rend(); |
(...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
905 dst_bitmap.eraseColor(0); | 902 dst_bitmap.eraseColor(0); |
906 SkAutoLockPixels dst_bitmap_lock(dst_bitmap); | 903 SkAutoLockPixels dst_bitmap_lock(dst_bitmap); |
907 | 904 |
908 SkCanvas canvas(dst_bitmap); | 905 SkCanvas canvas(dst_bitmap); |
909 canvas.scale(new_scale, new_scale); | 906 canvas.scale(new_scale, new_scale); |
910 canvas.drawBitmap(bitmap, 0, 0, NULL); | 907 canvas.drawBitmap(bitmap, 0, 0, NULL); |
911 dst_bitmap.setImmutable(); | 908 dst_bitmap.setImmutable(); |
912 | 909 |
913 return std::make_pair(dst_bitmap, new_scale * scale); | 910 return std::make_pair(dst_bitmap, new_scale * scale); |
914 } | 911 } |
OLD | NEW |