| 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 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 count++; | 302 count++; |
| 303 } | 303 } |
| 304 | 304 |
| 305 ReadNextThumbnail(); | 305 ReadNextThumbnail(); |
| 306 } | 306 } |
| 307 | 307 |
| 308 void ThumbnailCache::DecompressThumbnailFromFile( | 308 void ThumbnailCache::DecompressThumbnailFromFile( |
| 309 TabId tab_id, | 309 TabId tab_id, |
| 310 const base::Callback<void(bool, SkBitmap)>& | 310 const base::Callback<void(bool, SkBitmap)>& |
| 311 post_decompress_callback) { | 311 post_decompress_callback) { |
| 312 base::Callback<void(skia::RefPtr<SkPixelRef>, float, const gfx::Size&)> | 312 base::Callback<void(sk_sp<SkPixelRef>, float, const gfx::Size&)> |
| 313 decompress_task = base::Bind( | 313 decompress_task = base::Bind( |
| 314 &ThumbnailCache::DecompressionTask, post_decompress_callback); | 314 &ThumbnailCache::DecompressionTask, post_decompress_callback); |
| 315 | 315 |
| 316 content::BrowserThread::PostTask( | 316 content::BrowserThread::PostTask( |
| 317 content::BrowserThread::FILE, | 317 content::BrowserThread::FILE, |
| 318 FROM_HERE, | 318 FROM_HERE, |
| 319 base::Bind(&ThumbnailCache::ReadTask, true, tab_id, decompress_task)); | 319 base::Bind(&ThumbnailCache::ReadTask, true, tab_id, decompress_task)); |
| 320 } | 320 } |
| 321 | 321 |
| 322 void ThumbnailCache::RemoveFromDisk(TabId tab_id) { | 322 void ThumbnailCache::RemoveFromDisk(TabId tab_id) { |
| 323 base::Closure task = | 323 base::Closure task = |
| 324 base::Bind(&ThumbnailCache::RemoveFromDiskTask, tab_id); | 324 base::Bind(&ThumbnailCache::RemoveFromDiskTask, tab_id); |
| 325 content::BrowserThread::PostTask( | 325 content::BrowserThread::PostTask( |
| 326 content::BrowserThread::FILE, FROM_HERE, task); | 326 content::BrowserThread::FILE, FROM_HERE, task); |
| 327 } | 327 } |
| 328 | 328 |
| 329 void ThumbnailCache::RemoveFromDiskTask(TabId tab_id) { | 329 void ThumbnailCache::RemoveFromDiskTask(TabId tab_id) { |
| 330 base::FilePath file_path = GetFilePath(tab_id); | 330 base::FilePath file_path = GetFilePath(tab_id); |
| 331 if (base::PathExists(file_path)) | 331 if (base::PathExists(file_path)) |
| 332 base::DeleteFile(file_path, false); | 332 base::DeleteFile(file_path, false); |
| 333 } | 333 } |
| 334 | 334 |
| 335 void ThumbnailCache::WriteThumbnailIfNecessary( | 335 void ThumbnailCache::WriteThumbnailIfNecessary( |
| 336 TabId tab_id, | 336 TabId tab_id, |
| 337 skia::RefPtr<SkPixelRef> compressed_data, | 337 sk_sp<SkPixelRef> compressed_data, |
| 338 float scale, | 338 float scale, |
| 339 const gfx::Size& content_size) { | 339 const gfx::Size& content_size) { |
| 340 if (write_tasks_count_ >= write_queue_max_size_) | 340 if (write_tasks_count_ >= write_queue_max_size_) |
| 341 return; | 341 return; |
| 342 | 342 |
| 343 write_tasks_count_++; | 343 write_tasks_count_++; |
| 344 | 344 |
| 345 base::Callback<void()> post_write_task = | 345 base::Callback<void()> post_write_task = |
| 346 base::Bind(&ThumbnailCache::PostWriteTask, weak_factory_.GetWeakPtr()); | 346 base::Bind(&ThumbnailCache::PostWriteTask, weak_factory_.GetWeakPtr()); |
| 347 content::BrowserThread::PostTask(content::BrowserThread::FILE, | 347 content::BrowserThread::PostTask(content::BrowserThread::FILE, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 359 const base::Time& time_stamp, | 359 const base::Time& time_stamp, |
| 360 const SkBitmap& bitmap, | 360 const SkBitmap& bitmap, |
| 361 float scale) { | 361 float scale) { |
| 362 if (compression_tasks_count_ >= compression_queue_max_size_) { | 362 if (compression_tasks_count_ >= compression_queue_max_size_) { |
| 363 RemoveOnMatchedTimeStamp(tab_id, time_stamp); | 363 RemoveOnMatchedTimeStamp(tab_id, time_stamp); |
| 364 return; | 364 return; |
| 365 } | 365 } |
| 366 | 366 |
| 367 compression_tasks_count_++; | 367 compression_tasks_count_++; |
| 368 | 368 |
| 369 base::Callback<void(skia::RefPtr<SkPixelRef>, const gfx::Size&)> | 369 base::Callback<void(sk_sp<SkPixelRef>, const gfx::Size&)> |
| 370 post_compression_task = base::Bind(&ThumbnailCache::PostCompressionTask, | 370 post_compression_task = base::Bind(&ThumbnailCache::PostCompressionTask, |
| 371 weak_factory_.GetWeakPtr(), | 371 weak_factory_.GetWeakPtr(), |
| 372 tab_id, | 372 tab_id, |
| 373 time_stamp, | 373 time_stamp, |
| 374 scale); | 374 scale); |
| 375 | 375 |
| 376 gfx::Size raw_data_size(bitmap.width(), bitmap.height()); | 376 gfx::Size raw_data_size(bitmap.width(), bitmap.height()); |
| 377 gfx::Size encoded_size = GetEncodedSize( | 377 gfx::Size encoded_size = GetEncodedSize( |
| 378 raw_data_size, ui_resource_provider_->SupportsETC1NonPowerOfTwo()); | 378 raw_data_size, ui_resource_provider_->SupportsETC1NonPowerOfTwo()); |
| 379 | 379 |
| 380 base::WorkerPool::PostTask(FROM_HERE, | 380 base::WorkerPool::PostTask(FROM_HERE, |
| 381 base::Bind(&ThumbnailCache::CompressionTask, | 381 base::Bind(&ThumbnailCache::CompressionTask, |
| 382 bitmap, | 382 bitmap, |
| 383 encoded_size, | 383 encoded_size, |
| 384 post_compression_task), | 384 post_compression_task), |
| 385 true); | 385 true); |
| 386 } | 386 } |
| 387 | 387 |
| 388 void ThumbnailCache::ReadNextThumbnail() { | 388 void ThumbnailCache::ReadNextThumbnail() { |
| 389 if (read_queue_.empty() || read_in_progress_) | 389 if (read_queue_.empty() || read_in_progress_) |
| 390 return; | 390 return; |
| 391 | 391 |
| 392 TabId tab_id = read_queue_.front(); | 392 TabId tab_id = read_queue_.front(); |
| 393 read_in_progress_ = true; | 393 read_in_progress_ = true; |
| 394 | 394 |
| 395 base::Callback<void(skia::RefPtr<SkPixelRef>, float, const gfx::Size&)> | 395 base::Callback<void(sk_sp<SkPixelRef>, float, const gfx::Size&)> |
| 396 post_read_task = base::Bind( | 396 post_read_task = base::Bind( |
| 397 &ThumbnailCache::PostReadTask, weak_factory_.GetWeakPtr(), tab_id); | 397 &ThumbnailCache::PostReadTask, weak_factory_.GetWeakPtr(), tab_id); |
| 398 | 398 |
| 399 content::BrowserThread::PostTask( | 399 content::BrowserThread::PostTask( |
| 400 content::BrowserThread::FILE, | 400 content::BrowserThread::FILE, |
| 401 FROM_HERE, | 401 FROM_HERE, |
| 402 base::Bind(&ThumbnailCache::ReadTask, false, tab_id, post_read_task)); | 402 base::Bind(&ThumbnailCache::ReadTask, false, tab_id, post_read_task)); |
| 403 } | 403 } |
| 404 | 404 |
| 405 void ThumbnailCache::MakeSpaceForNewItemIfNecessary(TabId tab_id) { | 405 void ThumbnailCache::MakeSpaceForNewItemIfNecessary(TabId tab_id) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 cached_thumbnail = approximation_cache_.Get(tab_id); | 464 cached_thumbnail = approximation_cache_.Get(tab_id); |
| 465 if (cached_thumbnail && cached_thumbnail->ui_resource_id() == uid) | 465 if (cached_thumbnail && cached_thumbnail->ui_resource_id() == uid) |
| 466 approximation_cache_.Remove(tab_id); | 466 approximation_cache_.Remove(tab_id); |
| 467 } | 467 } |
| 468 | 468 |
| 469 namespace { | 469 namespace { |
| 470 | 470 |
| 471 bool WriteToFile(base::File& file, | 471 bool WriteToFile(base::File& file, |
| 472 const gfx::Size& content_size, | 472 const gfx::Size& content_size, |
| 473 const float scale, | 473 const float scale, |
| 474 skia::RefPtr<SkPixelRef> compressed_data) { | 474 sk_sp<SkPixelRef> compressed_data) { |
| 475 if (!file.IsValid()) | 475 if (!file.IsValid()) |
| 476 return false; | 476 return false; |
| 477 | 477 |
| 478 if (!WriteBigEndianToFile(file, kCompressedKey)) | 478 if (!WriteBigEndianToFile(file, kCompressedKey)) |
| 479 return false; | 479 return false; |
| 480 | 480 |
| 481 if (!WriteBigEndianToFile(file, content_size.width())) | 481 if (!WriteBigEndianToFile(file, content_size.width())) |
| 482 return false; | 482 return false; |
| 483 | 483 |
| 484 if (!WriteBigEndianToFile(file, content_size.height())) | 484 if (!WriteBigEndianToFile(file, content_size.height())) |
| (...skipping 28 matching lines...) Expand all Loading... |
| 513 | 513 |
| 514 if (!WriteBigEndianFloatToFile(file, 1.f / scale)) | 514 if (!WriteBigEndianFloatToFile(file, 1.f / scale)) |
| 515 return false; | 515 return false; |
| 516 | 516 |
| 517 return true; | 517 return true; |
| 518 } | 518 } |
| 519 | 519 |
| 520 } // anonymous namespace | 520 } // anonymous namespace |
| 521 | 521 |
| 522 void ThumbnailCache::WriteTask(TabId tab_id, | 522 void ThumbnailCache::WriteTask(TabId tab_id, |
| 523 skia::RefPtr<SkPixelRef> compressed_data, | 523 sk_sp<SkPixelRef> compressed_data, |
| 524 float scale, | 524 float scale, |
| 525 const gfx::Size& content_size, | 525 const gfx::Size& content_size, |
| 526 const base::Callback<void()>& post_write_task) { | 526 const base::Callback<void()>& post_write_task) { |
| 527 DCHECK(compressed_data); | 527 DCHECK(compressed_data); |
| 528 | 528 |
| 529 base::FilePath file_path = GetFilePath(tab_id); | 529 base::FilePath file_path = GetFilePath(tab_id); |
| 530 | 530 |
| 531 base::File file(file_path, | 531 base::File file(file_path, |
| 532 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); | 532 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
| 533 | 533 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 545 content::BrowserThread::UI, FROM_HERE, post_write_task); | 545 content::BrowserThread::UI, FROM_HERE, post_write_task); |
| 546 } | 546 } |
| 547 | 547 |
| 548 void ThumbnailCache::PostWriteTask() { | 548 void ThumbnailCache::PostWriteTask() { |
| 549 write_tasks_count_--; | 549 write_tasks_count_--; |
| 550 } | 550 } |
| 551 | 551 |
| 552 void ThumbnailCache::CompressionTask( | 552 void ThumbnailCache::CompressionTask( |
| 553 SkBitmap raw_data, | 553 SkBitmap raw_data, |
| 554 gfx::Size encoded_size, | 554 gfx::Size encoded_size, |
| 555 const base::Callback<void(skia::RefPtr<SkPixelRef>, const gfx::Size&)>& | 555 const base::Callback<void(sk_sp<SkPixelRef>, const gfx::Size&)>& |
| 556 post_compression_task) { | 556 post_compression_task) { |
| 557 skia::RefPtr<SkPixelRef> compressed_data; | 557 sk_sp<SkPixelRef> compressed_data; |
| 558 gfx::Size content_size; | 558 gfx::Size content_size; |
| 559 | 559 |
| 560 if (!raw_data.empty()) { | 560 if (!raw_data.empty()) { |
| 561 SkAutoLockPixels raw_data_lock(raw_data); | 561 SkAutoLockPixels raw_data_lock(raw_data); |
| 562 gfx::Size raw_data_size(raw_data.width(), raw_data.height()); | 562 gfx::Size raw_data_size(raw_data.width(), raw_data.height()); |
| 563 size_t pixel_size = 4; // Pixel size is 4 bytes for kARGB_8888_Config. | 563 size_t pixel_size = 4; // Pixel size is 4 bytes for kARGB_8888_Config. |
| 564 size_t stride = pixel_size * raw_data_size.width(); | 564 size_t stride = pixel_size * raw_data_size.width(); |
| 565 | 565 |
| 566 size_t encoded_bytes = | 566 size_t encoded_bytes = |
| 567 etc1_get_encoded_data_size(encoded_size.width(), encoded_size.height()); | 567 etc1_get_encoded_data_size(encoded_size.width(), encoded_size.height()); |
| 568 SkImageInfo info = SkImageInfo::Make(encoded_size.width(), | 568 SkImageInfo info = SkImageInfo::Make(encoded_size.width(), |
| 569 encoded_size.height(), | 569 encoded_size.height(), |
| 570 kUnknown_SkColorType, | 570 kUnknown_SkColorType, |
| 571 kUnpremul_SkAlphaType); | 571 kUnpremul_SkAlphaType); |
| 572 skia::RefPtr<SkData> etc1_pixel_data = skia::AdoptRef( | 572 sk_sp<SkData> etc1_pixel_data(SkData::NewUninitialized(encoded_bytes)); |
| 573 SkData::NewUninitialized(encoded_bytes)); | 573 sk_sp<SkMallocPixelRef> etc1_pixel_ref( |
| 574 skia::RefPtr<SkMallocPixelRef> etc1_pixel_ref = skia::AdoptRef( | |
| 575 SkMallocPixelRef::NewWithData(info, 0, NULL, etc1_pixel_data.get())); | 574 SkMallocPixelRef::NewWithData(info, 0, NULL, etc1_pixel_data.get())); |
| 576 | 575 |
| 577 etc1_pixel_ref->lockPixels(); | 576 etc1_pixel_ref->lockPixels(); |
| 578 bool success = etc1_encode_image( | 577 bool success = etc1_encode_image( |
| 579 reinterpret_cast<unsigned char*>(raw_data.getPixels()), | 578 reinterpret_cast<unsigned char*>(raw_data.getPixels()), |
| 580 raw_data_size.width(), | 579 raw_data_size.width(), |
| 581 raw_data_size.height(), | 580 raw_data_size.height(), |
| 582 pixel_size, | 581 pixel_size, |
| 583 stride, | 582 stride, |
| 584 reinterpret_cast<unsigned char*>(etc1_pixel_ref->pixels()), | 583 reinterpret_cast<unsigned char*>(etc1_pixel_ref->pixels()), |
| 585 encoded_size.width(), | 584 encoded_size.width(), |
| 586 encoded_size.height()); | 585 encoded_size.height()); |
| 587 etc1_pixel_ref->setImmutable(); | 586 etc1_pixel_ref->setImmutable(); |
| 588 etc1_pixel_ref->unlockPixels(); | 587 etc1_pixel_ref->unlockPixels(); |
| 589 | 588 |
| 590 if (success) { | 589 if (success) { |
| 591 compressed_data = etc1_pixel_ref; | 590 compressed_data = std::move(etc1_pixel_ref); |
| 592 content_size = raw_data_size; | 591 content_size = raw_data_size; |
| 593 } | 592 } |
| 594 } | 593 } |
| 595 | 594 |
| 596 content::BrowserThread::PostTask( | 595 content::BrowserThread::PostTask( |
| 597 content::BrowserThread::UI, | 596 content::BrowserThread::UI, |
| 598 FROM_HERE, | 597 FROM_HERE, |
| 599 base::Bind(post_compression_task, compressed_data, content_size)); | 598 base::Bind(post_compression_task, std::move(compressed_data), |
| 599 content_size)); |
| 600 } | 600 } |
| 601 | 601 |
| 602 void ThumbnailCache::PostCompressionTask( | 602 void ThumbnailCache::PostCompressionTask( |
| 603 TabId tab_id, | 603 TabId tab_id, |
| 604 const base::Time& time_stamp, | 604 const base::Time& time_stamp, |
| 605 float scale, | 605 float scale, |
| 606 skia::RefPtr<SkPixelRef> compressed_data, | 606 sk_sp<SkPixelRef> compressed_data, |
| 607 const gfx::Size& content_size) { | 607 const gfx::Size& content_size) { |
| 608 compression_tasks_count_--; | 608 compression_tasks_count_--; |
| 609 if (!compressed_data) { | 609 if (!compressed_data) { |
| 610 RemoveOnMatchedTimeStamp(tab_id, time_stamp); | 610 RemoveOnMatchedTimeStamp(tab_id, time_stamp); |
| 611 return; | 611 return; |
| 612 } | 612 } |
| 613 | 613 |
| 614 Thumbnail* thumbnail = cache_.Get(tab_id); | 614 Thumbnail* thumbnail = cache_.Get(tab_id); |
| 615 if (thumbnail) { | 615 if (thumbnail) { |
| 616 if (thumbnail->time_stamp() != time_stamp) | 616 if (thumbnail->time_stamp() != time_stamp) |
| 617 return; | 617 return; |
| 618 thumbnail->SetCompressedBitmap(compressed_data, content_size); | 618 thumbnail->SetCompressedBitmap(compressed_data, content_size); |
| 619 thumbnail->CreateUIResource(); | 619 thumbnail->CreateUIResource(); |
| 620 } | 620 } |
| 621 WriteThumbnailIfNecessary(tab_id, compressed_data, scale, content_size); | 621 WriteThumbnailIfNecessary(tab_id, std::move(compressed_data), scale, |
| 622 content_size); |
| 622 } | 623 } |
| 623 | 624 |
| 624 namespace { | 625 namespace { |
| 625 | 626 |
| 626 bool ReadFromFile(base::File& file, | 627 bool ReadFromFile(base::File& file, |
| 627 gfx::Size* out_content_size, | 628 gfx::Size* out_content_size, |
| 628 float* out_scale, | 629 float* out_scale, |
| 629 skia::RefPtr<SkPixelRef>* out_pixels) { | 630 sk_sp<SkPixelRef>* out_pixels) { |
| 630 if (!file.IsValid()) | 631 if (!file.IsValid()) |
| 631 return false; | 632 return false; |
| 632 | 633 |
| 633 int key = 0; | 634 int key = 0; |
| 634 if (!ReadBigEndianFromFile(file, &key)) | 635 if (!ReadBigEndianFromFile(file, &key)) |
| 635 return false; | 636 return false; |
| 636 | 637 |
| 637 if (key != kCompressedKey) | 638 if (key != kCompressedKey) |
| 638 return false; | 639 return false; |
| 639 | 640 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 677 display_info.GetDisplayHeight()); | 678 display_info.GetDisplayHeight()); |
| 678 | 679 |
| 679 if (content_width > max_dimension | 680 if (content_width > max_dimension |
| 680 || content_height > max_dimension | 681 || content_height > max_dimension |
| 681 || static_cast<size_t>(raw_width) > NextPowerOfTwo(max_dimension) | 682 || static_cast<size_t>(raw_width) > NextPowerOfTwo(max_dimension) |
| 682 || static_cast<size_t>(raw_height) > NextPowerOfTwo(max_dimension)) { | 683 || static_cast<size_t>(raw_height) > NextPowerOfTwo(max_dimension)) { |
| 683 return false; | 684 return false; |
| 684 } | 685 } |
| 685 | 686 |
| 686 int data_size = etc1_get_encoded_data_size(raw_width, raw_height); | 687 int data_size = etc1_get_encoded_data_size(raw_width, raw_height); |
| 687 skia::RefPtr<SkData> etc1_pixel_data = | 688 sk_sp<SkData> etc1_pixel_data(SkData::NewUninitialized(data_size)); |
| 688 skia::AdoptRef(SkData::NewUninitialized(data_size)); | |
| 689 | 689 |
| 690 int pixel_bytes_read = file.ReadAtCurrentPos( | 690 int pixel_bytes_read = file.ReadAtCurrentPos( |
| 691 reinterpret_cast<char*>(etc1_pixel_data->writable_data()), | 691 reinterpret_cast<char*>(etc1_pixel_data->writable_data()), |
| 692 data_size); | 692 data_size); |
| 693 | 693 |
| 694 if (pixel_bytes_read != data_size) | 694 if (pixel_bytes_read != data_size) |
| 695 return false; | 695 return false; |
| 696 | 696 |
| 697 SkImageInfo info = SkImageInfo::Make(raw_width, | 697 SkImageInfo info = SkImageInfo::Make(raw_width, |
| 698 raw_height, | 698 raw_height, |
| 699 kUnknown_SkColorType, | 699 kUnknown_SkColorType, |
| 700 kUnpremul_SkAlphaType); | 700 kUnpremul_SkAlphaType); |
| 701 | 701 |
| 702 *out_pixels = skia::AdoptRef( | 702 *out_pixels = sk_sp<SkPixelRef>( |
| 703 SkMallocPixelRef::NewWithData(info, | 703 SkMallocPixelRef::NewWithData(info, |
| 704 0, | 704 0, |
| 705 NULL, | 705 NULL, |
| 706 etc1_pixel_data.get())); | 706 etc1_pixel_data.get())); |
| 707 | 707 |
| 708 int extra_data_version = 0; | 708 int extra_data_version = 0; |
| 709 if (!ReadBigEndianFromFile(file, &extra_data_version)) | 709 if (!ReadBigEndianFromFile(file, &extra_data_version)) |
| 710 return false; | 710 return false; |
| 711 | 711 |
| 712 *out_scale = 1.f; | 712 *out_scale = 1.f; |
| 713 if (extra_data_version == 1) { | 713 if (extra_data_version == 1) { |
| 714 if (!ReadBigEndianFloatFromFile(file, out_scale)) | 714 if (!ReadBigEndianFloatFromFile(file, out_scale)) |
| 715 return false; | 715 return false; |
| 716 | 716 |
| 717 if (*out_scale == 0.f) | 717 if (*out_scale == 0.f) |
| 718 return false; | 718 return false; |
| 719 | 719 |
| 720 *out_scale = 1.f / *out_scale; | 720 *out_scale = 1.f / *out_scale; |
| 721 } | 721 } |
| 722 | 722 |
| 723 return true; | 723 return true; |
| 724 } | 724 } |
| 725 | 725 |
| 726 }// anonymous namespace | 726 }// anonymous namespace |
| 727 | 727 |
| 728 void ThumbnailCache::ReadTask( | 728 void ThumbnailCache::ReadTask( |
| 729 bool decompress, | 729 bool decompress, |
| 730 TabId tab_id, | 730 TabId tab_id, |
| 731 const base::Callback< | 731 const base::Callback< |
| 732 void(skia::RefPtr<SkPixelRef>, float, const gfx::Size&)>& | 732 void(sk_sp<SkPixelRef>, float, const gfx::Size&)>& |
| 733 post_read_task) { | 733 post_read_task) { |
| 734 gfx::Size content_size; | 734 gfx::Size content_size; |
| 735 float scale = 0.f; | 735 float scale = 0.f; |
| 736 skia::RefPtr<SkPixelRef> compressed_data; | 736 sk_sp<SkPixelRef> compressed_data; |
| 737 base::FilePath file_path = GetFilePath(tab_id); | 737 base::FilePath file_path = GetFilePath(tab_id); |
| 738 | 738 |
| 739 if (base::PathExists(file_path)) { | 739 if (base::PathExists(file_path)) { |
| 740 base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 740 base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 741 | 741 |
| 742 | 742 |
| 743 bool valid_contents = ReadFromFile(file, | 743 bool valid_contents = ReadFromFile(file, |
| 744 &content_size, | 744 &content_size, |
| 745 &scale, | 745 &scale, |
| 746 &compressed_data); | 746 &compressed_data); |
| 747 file.Close(); | 747 file.Close(); |
| 748 | 748 |
| 749 if (!valid_contents) { | 749 if (!valid_contents) { |
| 750 content_size.SetSize(0, 0); | 750 content_size.SetSize(0, 0); |
| 751 scale = 0.f; | 751 scale = 0.f; |
| 752 compressed_data.clear(); | 752 compressed_data.reset(); |
| 753 base::DeleteFile(file_path, false); | 753 base::DeleteFile(file_path, false); |
| 754 } | 754 } |
| 755 } | 755 } |
| 756 | 756 |
| 757 if (decompress) { | 757 if (decompress) { |
| 758 base::WorkerPool::PostTask( | 758 base::WorkerPool::PostTask( |
| 759 FROM_HERE, | 759 FROM_HERE, |
| 760 base::Bind(post_read_task, compressed_data, scale, content_size), | 760 base::Bind(post_read_task, std::move(compressed_data), scale, |
| 761 content_size), |
| 761 true); | 762 true); |
| 762 } else { | 763 } else { |
| 763 content::BrowserThread::PostTask( | 764 content::BrowserThread::PostTask( |
| 764 content::BrowserThread::UI, | 765 content::BrowserThread::UI, |
| 765 FROM_HERE, | 766 FROM_HERE, |
| 766 base::Bind(post_read_task, compressed_data, scale, content_size)); | 767 base::Bind(post_read_task, std::move(compressed_data), scale, |
| 768 content_size)); |
| 767 } | 769 } |
| 768 } | 770 } |
| 769 | 771 |
| 770 void ThumbnailCache::PostReadTask(TabId tab_id, | 772 void ThumbnailCache::PostReadTask(TabId tab_id, |
| 771 skia::RefPtr<SkPixelRef> compressed_data, | 773 sk_sp<SkPixelRef> compressed_data, |
| 772 float scale, | 774 float scale, |
| 773 const gfx::Size& content_size) { | 775 const gfx::Size& content_size) { |
| 774 read_in_progress_ = false; | 776 read_in_progress_ = false; |
| 775 | 777 |
| 776 TabIdList::iterator iter = | 778 TabIdList::iterator iter = |
| 777 std::find(read_queue_.begin(), read_queue_.end(), tab_id); | 779 std::find(read_queue_.begin(), read_queue_.end(), tab_id); |
| 778 if (iter == read_queue_.end()) { | 780 if (iter == read_queue_.end()) { |
| 779 ReadNextThumbnail(); | 781 ReadNextThumbnail(); |
| 780 return; | 782 return; |
| 781 } | 783 } |
| 782 | 784 |
| 783 read_queue_.erase(iter); | 785 read_queue_.erase(iter); |
| 784 | 786 |
| 785 if (!cache_.Get(tab_id) && compressed_data) { | 787 if (!cache_.Get(tab_id) && compressed_data) { |
| 786 ThumbnailMetaDataMap::iterator meta_iter = | 788 ThumbnailMetaDataMap::iterator meta_iter = |
| 787 thumbnail_meta_data_.find(tab_id); | 789 thumbnail_meta_data_.find(tab_id); |
| 788 base::Time time_stamp = base::Time::Now(); | 790 base::Time time_stamp = base::Time::Now(); |
| 789 if (meta_iter != thumbnail_meta_data_.end()) | 791 if (meta_iter != thumbnail_meta_data_.end()) |
| 790 time_stamp = meta_iter->second.capture_time(); | 792 time_stamp = meta_iter->second.capture_time(); |
| 791 | 793 |
| 792 MakeSpaceForNewItemIfNecessary(tab_id); | 794 MakeSpaceForNewItemIfNecessary(tab_id); |
| 793 std::unique_ptr<Thumbnail> thumbnail = Thumbnail::Create( | 795 std::unique_ptr<Thumbnail> thumbnail = Thumbnail::Create( |
| 794 tab_id, time_stamp, scale, ui_resource_provider_, this); | 796 tab_id, time_stamp, scale, ui_resource_provider_, this); |
| 795 thumbnail->SetCompressedBitmap(compressed_data, | 797 thumbnail->SetCompressedBitmap(std::move(compressed_data), |
| 796 content_size); | 798 content_size); |
| 797 if (kPreferCPUMemory) | 799 if (kPreferCPUMemory) |
| 798 thumbnail->CreateUIResource(); | 800 thumbnail->CreateUIResource(); |
| 799 | 801 |
| 800 cache_.Put(tab_id, std::move(thumbnail)); | 802 cache_.Put(tab_id, std::move(thumbnail)); |
| 801 NotifyObserversOfThumbnailRead(tab_id); | 803 NotifyObserversOfThumbnailRead(tab_id); |
| 802 } | 804 } |
| 803 | 805 |
| 804 ReadNextThumbnail(); | 806 ReadNextThumbnail(); |
| 805 } | 807 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 817 if ((thumbnail && thumbnail->time_stamp() == time_stamp) || | 819 if ((thumbnail && thumbnail->time_stamp() == time_stamp) || |
| 818 (approx_thumbnail && approx_thumbnail->time_stamp() == time_stamp)) { | 820 (approx_thumbnail && approx_thumbnail->time_stamp() == time_stamp)) { |
| 819 Remove(tab_id); | 821 Remove(tab_id); |
| 820 } | 822 } |
| 821 return; | 823 return; |
| 822 } | 824 } |
| 823 | 825 |
| 824 void ThumbnailCache::DecompressionTask( | 826 void ThumbnailCache::DecompressionTask( |
| 825 const base::Callback<void(bool, SkBitmap)>& | 827 const base::Callback<void(bool, SkBitmap)>& |
| 826 post_decompression_callback, | 828 post_decompression_callback, |
| 827 skia::RefPtr<SkPixelRef> compressed_data, | 829 sk_sp<SkPixelRef> compressed_data, |
| 828 float scale, | 830 float scale, |
| 829 const gfx::Size& content_size) { | 831 const gfx::Size& content_size) { |
| 830 SkBitmap raw_data_small; | 832 SkBitmap raw_data_small; |
| 831 bool success = false; | 833 bool success = false; |
| 832 | 834 |
| 833 if (compressed_data.get()) { | 835 if (compressed_data.get()) { |
| 834 gfx::Size buffer_size = gfx::Size(compressed_data->info().width(), | 836 gfx::Size buffer_size = gfx::Size(compressed_data->info().width(), |
| 835 compressed_data->info().height()); | 837 compressed_data->info().height()); |
| 836 | 838 |
| 837 SkBitmap raw_data; | 839 SkBitmap raw_data; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 903 dst_bitmap.eraseColor(0); | 905 dst_bitmap.eraseColor(0); |
| 904 SkAutoLockPixels dst_bitmap_lock(dst_bitmap); | 906 SkAutoLockPixels dst_bitmap_lock(dst_bitmap); |
| 905 | 907 |
| 906 SkCanvas canvas(dst_bitmap); | 908 SkCanvas canvas(dst_bitmap); |
| 907 canvas.scale(new_scale, new_scale); | 909 canvas.scale(new_scale, new_scale); |
| 908 canvas.drawBitmap(bitmap, 0, 0, NULL); | 910 canvas.drawBitmap(bitmap, 0, 0, NULL); |
| 909 dst_bitmap.setImmutable(); | 911 dst_bitmap.setImmutable(); |
| 910 | 912 |
| 911 return std::make_pair(dst_bitmap, new_scale * scale); | 913 return std::make_pair(dst_bitmap, new_scale * scale); |
| 912 } | 914 } |
| OLD | NEW |