| 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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 } | 297 } |
| 299 | 298 |
| 300 read_queue_.clear(); | 299 read_queue_.clear(); |
| 301 visible_ids_.clear(); | 300 visible_ids_.clear(); |
| 302 size_t count = 0; | 301 size_t count = 0; |
| 303 TabIdList::const_iterator iter = priority.begin(); | 302 TabIdList::const_iterator iter = priority.begin(); |
| 304 while (iter != priority.end() && count < ids_size) { | 303 while (iter != priority.end() && count < ids_size) { |
| 305 TabId tab_id = *iter; | 304 TabId tab_id = *iter; |
| 306 visible_ids_.push_back(tab_id); | 305 visible_ids_.push_back(tab_id); |
| 307 if (!cache_.Get(tab_id) && | 306 if (!cache_.Get(tab_id) && |
| 308 std::find(read_queue_.begin(), read_queue_.end(), tab_id) == | 307 !ContainsValue(read_queue_, tab_id)) { |
| 309 read_queue_.end()) { | |
| 310 read_queue_.push_back(tab_id); | 308 read_queue_.push_back(tab_id); |
| 311 } | 309 } |
| 312 iter++; | 310 iter++; |
| 313 count++; | 311 count++; |
| 314 } | 312 } |
| 315 | 313 |
| 316 ReadNextThumbnail(); | 314 ReadNextThumbnail(); |
| 317 } | 315 } |
| 318 | 316 |
| 319 void ThumbnailCache::DecompressThumbnailFromFile( | 317 void ThumbnailCache::DecompressThumbnailFromFile( |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 &ThumbnailCache::PostReadTask, weak_factory_.GetWeakPtr(), tab_id); | 421 &ThumbnailCache::PostReadTask, weak_factory_.GetWeakPtr(), tab_id); |
| 424 | 422 |
| 425 content::BrowserThread::PostTask( | 423 content::BrowserThread::PostTask( |
| 426 content::BrowserThread::FILE, | 424 content::BrowserThread::FILE, |
| 427 FROM_HERE, | 425 FROM_HERE, |
| 428 base::Bind(&ThumbnailCache::ReadTask, false, tab_id, post_read_task)); | 426 base::Bind(&ThumbnailCache::ReadTask, false, tab_id, post_read_task)); |
| 429 } | 427 } |
| 430 | 428 |
| 431 void ThumbnailCache::MakeSpaceForNewItemIfNecessary(TabId tab_id) { | 429 void ThumbnailCache::MakeSpaceForNewItemIfNecessary(TabId tab_id) { |
| 432 if (cache_.Get(tab_id) || | 430 if (cache_.Get(tab_id) || |
| 433 std::find(visible_ids_.begin(), visible_ids_.end(), tab_id) == | 431 !ContainsValue(visible_ids_, tab_id) || |
| 434 visible_ids_.end() || | |
| 435 cache_.size() < cache_.MaximumCacheSize()) { | 432 cache_.size() < cache_.MaximumCacheSize()) { |
| 436 return; | 433 return; |
| 437 } | 434 } |
| 438 | 435 |
| 439 TabId key_to_remove; | 436 TabId key_to_remove; |
| 440 bool found_key_to_remove = false; | 437 bool found_key_to_remove = false; |
| 441 | 438 |
| 442 // 1. Find a cached item not in this list | 439 // 1. Find a cached item not in this list |
| 443 for (ExpiringThumbnailCache::iterator iter = cache_.begin(); | 440 for (ExpiringThumbnailCache::iterator iter = cache_.begin(); |
| 444 iter != cache_.end(); | 441 iter != cache_.end(); |
| 445 iter++) { | 442 iter++) { |
| 446 if (std::find(visible_ids_.begin(), visible_ids_.end(), iter->first) == | 443 if (!ContainsValue(visible_ids_, iter->first)) { |
| 447 visible_ids_.end()) { | |
| 448 key_to_remove = iter->first; | 444 key_to_remove = iter->first; |
| 449 found_key_to_remove = true; | 445 found_key_to_remove = true; |
| 450 break; | 446 break; |
| 451 } | 447 } |
| 452 } | 448 } |
| 453 | 449 |
| 454 if (!found_key_to_remove) { | 450 if (!found_key_to_remove) { |
| 455 // 2. Find the least important id we can remove. | 451 // 2. Find the least important id we can remove. |
| 456 for (TabIdList::reverse_iterator riter = visible_ids_.rbegin(); | 452 for (TabIdList::reverse_iterator riter = visible_ids_.rbegin(); |
| 457 riter != visible_ids_.rend(); | 453 riter != visible_ids_.rend(); |
| (...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 931 dst_bitmap.eraseColor(0); | 927 dst_bitmap.eraseColor(0); |
| 932 SkAutoLockPixels dst_bitmap_lock(dst_bitmap); | 928 SkAutoLockPixels dst_bitmap_lock(dst_bitmap); |
| 933 | 929 |
| 934 SkCanvas canvas(dst_bitmap); | 930 SkCanvas canvas(dst_bitmap); |
| 935 canvas.scale(new_scale, new_scale); | 931 canvas.scale(new_scale, new_scale); |
| 936 canvas.drawBitmap(bitmap, 0, 0, NULL); | 932 canvas.drawBitmap(bitmap, 0, 0, NULL); |
| 937 dst_bitmap.setImmutable(); | 933 dst_bitmap.setImmutable(); |
| 938 | 934 |
| 939 return std::make_pair(dst_bitmap, new_scale * scale); | 935 return std::make_pair(dst_bitmap, new_scale * scale); |
| 940 } | 936 } |
| OLD | NEW |