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 |
11 #include "base/android/path_utils.h" | 11 #include "base/android/path_utils.h" |
12 #include "base/big_endian.h" | 12 #include "base/big_endian.h" |
13 #include "base/files/file.h" | 13 #include "base/files/file.h" |
14 #include "base/files/file_enumerator.h" | 14 #include "base/files/file_enumerator.h" |
15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 17 #include "base/stl_util.h" |
17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
18 #include "base/threading/worker_pool.h" | 19 #include "base/threading/worker_pool.h" |
19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
20 #include "build/build_config.h" | 21 #include "build/build_config.h" |
21 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
22 #include "third_party/android_opengl/etc1/etc1.h" | 23 #include "third_party/android_opengl/etc1/etc1.h" |
23 #include "third_party/skia/include/core/SkBitmap.h" | 24 #include "third_party/skia/include/core/SkBitmap.h" |
24 #include "third_party/skia/include/core/SkCanvas.h" | 25 #include "third_party/skia/include/core/SkCanvas.h" |
25 #include "third_party/skia/include/core/SkData.h" | 26 #include "third_party/skia/include/core/SkData.h" |
26 #include "third_party/skia/include/core/SkMallocPixelRef.h" | 27 #include "third_party/skia/include/core/SkMallocPixelRef.h" |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 Thumbnail* ThumbnailCache::Get(TabId tab_id, | 199 Thumbnail* ThumbnailCache::Get(TabId tab_id, |
199 bool force_disk_read, | 200 bool force_disk_read, |
200 bool allow_approximation) { | 201 bool allow_approximation) { |
201 Thumbnail* thumbnail = cache_.Get(tab_id); | 202 Thumbnail* thumbnail = cache_.Get(tab_id); |
202 if (thumbnail) { | 203 if (thumbnail) { |
203 thumbnail->CreateUIResource(); | 204 thumbnail->CreateUIResource(); |
204 return thumbnail; | 205 return thumbnail; |
205 } | 206 } |
206 | 207 |
207 if (force_disk_read && | 208 if (force_disk_read && |
208 std::find(visible_ids_.begin(), visible_ids_.end(), tab_id) != | 209 ContainsValue(visible_ids_, tab_id) && |
209 visible_ids_.end() && | 210 !ContainsValue(read_queue_, tab_id)) { |
210 std::find(read_queue_.begin(), read_queue_.end(), tab_id) == | |
211 read_queue_.end()) { | |
212 read_queue_.push_back(tab_id); | 211 read_queue_.push_back(tab_id); |
213 ReadNextThumbnail(); | 212 ReadNextThumbnail(); |
214 } | 213 } |
215 | 214 |
216 if (allow_approximation) { | 215 if (allow_approximation) { |
217 thumbnail = approximation_cache_.Get(tab_id); | 216 thumbnail = approximation_cache_.Get(tab_id); |
218 if (thumbnail) { | 217 if (thumbnail) { |
219 thumbnail->CreateUIResource(); | 218 thumbnail->CreateUIResource(); |
220 return thumbnail; | 219 return thumbnail; |
221 } | 220 } |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 } | 289 } |
291 | 290 |
292 read_queue_.clear(); | 291 read_queue_.clear(); |
293 visible_ids_.clear(); | 292 visible_ids_.clear(); |
294 size_t count = 0; | 293 size_t count = 0; |
295 TabIdList::const_iterator iter = priority.begin(); | 294 TabIdList::const_iterator iter = priority.begin(); |
296 while (iter != priority.end() && count < ids_size) { | 295 while (iter != priority.end() && count < ids_size) { |
297 TabId tab_id = *iter; | 296 TabId tab_id = *iter; |
298 visible_ids_.push_back(tab_id); | 297 visible_ids_.push_back(tab_id); |
299 if (!cache_.Get(tab_id) && | 298 if (!cache_.Get(tab_id) && |
300 std::find(read_queue_.begin(), read_queue_.end(), tab_id) == | 299 !ContainsValue(read_queue_, tab_id)) |
301 read_queue_.end()) { | |
302 read_queue_.push_back(tab_id); | 300 read_queue_.push_back(tab_id); |
303 } | |
304 iter++; | 301 iter++; |
305 count++; | 302 count++; |
306 } | 303 } |
307 | 304 |
308 ReadNextThumbnail(); | 305 ReadNextThumbnail(); |
309 } | 306 } |
310 | 307 |
311 void ThumbnailCache::DecompressThumbnailFromFile( | 308 void ThumbnailCache::DecompressThumbnailFromFile( |
312 TabId tab_id, | 309 TabId tab_id, |
313 const base::Callback<void(bool, SkBitmap)>& | 310 const base::Callback<void(bool, SkBitmap)>& |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 &ThumbnailCache::PostReadTask, weak_factory_.GetWeakPtr(), tab_id); | 397 &ThumbnailCache::PostReadTask, weak_factory_.GetWeakPtr(), tab_id); |
401 | 398 |
402 content::BrowserThread::PostTask( | 399 content::BrowserThread::PostTask( |
403 content::BrowserThread::FILE, | 400 content::BrowserThread::FILE, |
404 FROM_HERE, | 401 FROM_HERE, |
405 base::Bind(&ThumbnailCache::ReadTask, false, tab_id, post_read_task)); | 402 base::Bind(&ThumbnailCache::ReadTask, false, tab_id, post_read_task)); |
406 } | 403 } |
407 | 404 |
408 void ThumbnailCache::MakeSpaceForNewItemIfNecessary(TabId tab_id) { | 405 void ThumbnailCache::MakeSpaceForNewItemIfNecessary(TabId tab_id) { |
409 if (cache_.Get(tab_id) || | 406 if (cache_.Get(tab_id) || |
410 std::find(visible_ids_.begin(), visible_ids_.end(), tab_id) == | 407 !ContainsValue(visible_ids_, tab_id) || |
411 visible_ids_.end() || | |
412 cache_.size() < cache_.MaximumCacheSize()) { | 408 cache_.size() < cache_.MaximumCacheSize()) { |
413 return; | 409 return; |
414 } | 410 } |
415 | 411 |
416 TabId key_to_remove; | 412 TabId key_to_remove; |
417 bool found_key_to_remove = false; | 413 bool found_key_to_remove = false; |
418 | 414 |
419 // 1. Find a cached item not in this list | 415 // 1. Find a cached item not in this list |
420 for (ExpiringThumbnailCache::iterator iter = cache_.begin(); | 416 for (ExpiringThumbnailCache::iterator iter = cache_.begin(); |
421 iter != cache_.end(); | 417 iter != cache_.end(); |
422 iter++) { | 418 iter++) { |
423 if (std::find(visible_ids_.begin(), visible_ids_.end(), iter->first) == | 419 if (!ContainsValue(visible_ids_, iter->first)) { |
424 visible_ids_.end()) { | |
425 key_to_remove = iter->first; | 420 key_to_remove = iter->first; |
426 found_key_to_remove = true; | 421 found_key_to_remove = true; |
427 break; | 422 break; |
428 } | 423 } |
429 } | 424 } |
430 | 425 |
431 if (!found_key_to_remove) { | 426 if (!found_key_to_remove) { |
432 // 2. Find the least important id we can remove. | 427 // 2. Find the least important id we can remove. |
433 for (TabIdList::reverse_iterator riter = visible_ids_.rbegin(); | 428 for (TabIdList::reverse_iterator riter = visible_ids_.rbegin(); |
434 riter != visible_ids_.rend(); | 429 riter != visible_ids_.rend(); |
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
908 dst_bitmap.eraseColor(0); | 903 dst_bitmap.eraseColor(0); |
909 SkAutoLockPixels dst_bitmap_lock(dst_bitmap); | 904 SkAutoLockPixels dst_bitmap_lock(dst_bitmap); |
910 | 905 |
911 SkCanvas canvas(dst_bitmap); | 906 SkCanvas canvas(dst_bitmap); |
912 canvas.scale(new_scale, new_scale); | 907 canvas.scale(new_scale, new_scale); |
913 canvas.drawBitmap(bitmap, 0, 0, NULL); | 908 canvas.drawBitmap(bitmap, 0, 0, NULL); |
914 dst_bitmap.setImmutable(); | 909 dst_bitmap.setImmutable(); |
915 | 910 |
916 return std::make_pair(dst_bitmap, new_scale * scale); | 911 return std::make_pair(dst_bitmap, new_scale * scale); |
917 } | 912 } |
OLD | NEW |