Chromium Code Reviews| 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_store.h" | 5 #include "chrome/browser/android/thumbnail/thumbnail_store.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 | 9 |
| 10 #include "base/big_endian.h" | 10 #include "base/big_endian.h" |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 31 const float kApproximationScaleFactor = 4.f; | 31 const float kApproximationScaleFactor = 4.f; |
| 32 const base::TimeDelta kCaptureMinRequestTimeMs( | 32 const base::TimeDelta kCaptureMinRequestTimeMs( |
| 33 base::TimeDelta::FromMilliseconds(1000)); | 33 base::TimeDelta::FromMilliseconds(1000)); |
| 34 | 34 |
| 35 const int kCompressedKey = 0xABABABAB; | 35 const int kCompressedKey = 0xABABABAB; |
| 36 const int kCurrentExtraVersion = 1; | 36 const int kCurrentExtraVersion = 1; |
| 37 | 37 |
| 38 // Indicates whether we prefer to have more free CPU memory over GPU memory. | 38 // Indicates whether we prefer to have more free CPU memory over GPU memory. |
| 39 const bool kPreferCPUMemory = true; | 39 const bool kPreferCPUMemory = true; |
| 40 | 40 |
| 41 // TODO(): ETC1 texture sizes should be multiples of four, but some drivers only | |
| 42 // allow power-of-two ETC1 textures. Find better work around. | |
| 43 size_t NextPowerOfTwo(size_t x) { | 41 size_t NextPowerOfTwo(size_t x) { |
| 44 --x; | 42 --x; |
| 45 x |= x >> 1; | 43 x |= x >> 1; |
| 46 x |= x >> 2; | 44 x |= x >> 2; |
| 47 x |= x >> 4; | 45 x |= x >> 4; |
| 48 x |= x >> 8; | 46 x |= x >> 8; |
| 49 x |= x >> 16; | 47 x |= x >> 16; |
| 50 return x + 1; | 48 return x + 1; |
| 51 } | 49 } |
| 52 | 50 |
| 53 gfx::Size GetEncodedSize(const gfx::Size& bitmap_size) { | 51 size_t RoundUpMod4(size_t x) { |
|
David Trainor- moved to gerrit
2014/08/21 22:15:00
(x + 3) & ~3 (is what most ETC1 libs use).
aelias_OOO_until_Jul13
2014/08/21 22:56:12
Done.
| |
| 52 size_t mod = x % 4; | |
| 53 if (mod) { | |
| 54 x += 4 - mod; | |
| 55 } | |
| 56 return x; | |
| 57 } | |
| 58 | |
| 59 gfx::Size GetEncodedSize(const gfx::Size& bitmap_size, bool supports_npot) { | |
| 54 DCHECK(!bitmap_size.IsEmpty()); | 60 DCHECK(!bitmap_size.IsEmpty()); |
| 55 return gfx::Size(NextPowerOfTwo(bitmap_size.width()), | 61 if (!supports_npot) |
| 56 NextPowerOfTwo(bitmap_size.height())); | 62 return gfx::Size(NextPowerOfTwo(bitmap_size.width()), |
| 63 NextPowerOfTwo(bitmap_size.height())); | |
| 64 else | |
| 65 return gfx::Size(RoundUpMod4(bitmap_size.width()), | |
| 66 RoundUpMod4(bitmap_size.height())); | |
| 57 } | 67 } |
| 58 | 68 |
| 59 template<typename T> | 69 template<typename T> |
| 60 bool ReadBigEndianFromFile(base::File& file, T* out) { | 70 bool ReadBigEndianFromFile(base::File& file, T* out) { |
| 61 char buffer[sizeof(T)]; | 71 char buffer[sizeof(T)]; |
| 62 if (file.ReadAtCurrentPos(buffer, sizeof(T)) != sizeof(T)) | 72 if (file.ReadAtCurrentPos(buffer, sizeof(T)) != sizeof(T)) |
| 63 return false; | 73 return false; |
| 64 base::ReadBigEndian(buffer, out); | 74 base::ReadBigEndian(buffer, out); |
| 65 return true; | 75 return true; |
| 66 } | 76 } |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 358 | 368 |
| 359 compression_tasks_count_++; | 369 compression_tasks_count_++; |
| 360 | 370 |
| 361 base::Callback<void(skia::RefPtr<SkPixelRef>, const gfx::Size&)> | 371 base::Callback<void(skia::RefPtr<SkPixelRef>, const gfx::Size&)> |
| 362 post_compression_task = base::Bind(&ThumbnailStore::PostCompressionTask, | 372 post_compression_task = base::Bind(&ThumbnailStore::PostCompressionTask, |
| 363 weak_factory_.GetWeakPtr(), | 373 weak_factory_.GetWeakPtr(), |
| 364 tab_id, | 374 tab_id, |
| 365 time_stamp, | 375 time_stamp, |
| 366 scale); | 376 scale); |
| 367 | 377 |
| 368 base::WorkerPool::PostTask( | 378 gfx::Size raw_data_size(bitmap.width(), bitmap.height()); |
| 369 FROM_HERE, | 379 gfx::Size encoded_size = GetEncodedSize( |
| 370 base::Bind( | 380 raw_data_size, ui_resource_provider_->SupportsETC1NonPowerOfTwo()); |
| 371 &ThumbnailStore::CompressionTask, bitmap, post_compression_task), | 381 |
| 372 true); | 382 base::WorkerPool::PostTask(FROM_HERE, |
| 383 base::Bind(&ThumbnailStore::CompressionTask, | |
| 384 bitmap, | |
| 385 encoded_size, | |
| 386 post_compression_task), | |
| 387 true); | |
| 373 } | 388 } |
| 374 | 389 |
| 375 void ThumbnailStore::ReadNextThumbnail() { | 390 void ThumbnailStore::ReadNextThumbnail() { |
| 376 if (read_queue_.empty() || read_in_progress_) | 391 if (read_queue_.empty() || read_in_progress_) |
| 377 return; | 392 return; |
| 378 | 393 |
| 379 TabId tab_id = read_queue_.front(); | 394 TabId tab_id = read_queue_.front(); |
| 380 read_in_progress_ = true; | 395 read_in_progress_ = true; |
| 381 | 396 |
| 382 base::FilePath file_path = GetFilePath(tab_id); | 397 base::FilePath file_path = GetFilePath(tab_id); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 532 content::BrowserThread::PostTask( | 547 content::BrowserThread::PostTask( |
| 533 content::BrowserThread::UI, FROM_HERE, post_write_task); | 548 content::BrowserThread::UI, FROM_HERE, post_write_task); |
| 534 } | 549 } |
| 535 | 550 |
| 536 void ThumbnailStore::PostWriteTask() { | 551 void ThumbnailStore::PostWriteTask() { |
| 537 write_tasks_count_--; | 552 write_tasks_count_--; |
| 538 } | 553 } |
| 539 | 554 |
| 540 void ThumbnailStore::CompressionTask( | 555 void ThumbnailStore::CompressionTask( |
| 541 SkBitmap raw_data, | 556 SkBitmap raw_data, |
| 557 gfx::Size encoded_size, | |
| 542 const base::Callback<void(skia::RefPtr<SkPixelRef>, const gfx::Size&)>& | 558 const base::Callback<void(skia::RefPtr<SkPixelRef>, const gfx::Size&)>& |
| 543 post_compression_task) { | 559 post_compression_task) { |
| 544 skia::RefPtr<SkPixelRef> compressed_data; | 560 skia::RefPtr<SkPixelRef> compressed_data; |
| 545 gfx::Size content_size; | 561 gfx::Size content_size; |
| 546 | 562 |
| 547 if (!raw_data.empty()) { | 563 if (!raw_data.empty()) { |
| 548 SkAutoLockPixels raw_data_lock(raw_data); | 564 SkAutoLockPixels raw_data_lock(raw_data); |
| 549 gfx::Size raw_data_size(raw_data.width(), raw_data.height()); | 565 gfx::Size raw_data_size(raw_data.width(), raw_data.height()); |
| 550 size_t pixel_size = 4; // Pixel size is 4 bytes for kARGB_8888_Config. | 566 size_t pixel_size = 4; // Pixel size is 4 bytes for kARGB_8888_Config. |
| 551 size_t stride = pixel_size * raw_data_size.width(); | 567 size_t stride = pixel_size * raw_data_size.width(); |
| 552 | 568 |
| 553 gfx::Size encoded_size = GetEncodedSize(raw_data_size); | |
| 554 size_t encoded_bytes = | 569 size_t encoded_bytes = |
| 555 etc1_get_encoded_data_size(encoded_size.width(), encoded_size.height()); | 570 etc1_get_encoded_data_size(encoded_size.width(), encoded_size.height()); |
| 556 SkImageInfo info = {encoded_size.width(), | 571 SkImageInfo info = {encoded_size.width(), |
| 557 encoded_size.height(), | 572 encoded_size.height(), |
| 558 kUnknown_SkColorType, | 573 kUnknown_SkColorType, |
| 559 kUnpremul_SkAlphaType}; | 574 kUnpremul_SkAlphaType}; |
| 560 skia::RefPtr<SkData> etc1_pixel_data = skia::AdoptRef( | 575 skia::RefPtr<SkData> etc1_pixel_data = skia::AdoptRef( |
| 561 SkData::NewFromMalloc(new uint8_t[encoded_bytes], encoded_bytes)); | 576 SkData::NewFromMalloc(new uint8_t[encoded_bytes], encoded_bytes)); |
| 562 skia::RefPtr<SkMallocPixelRef> etc1_pixel_ref = skia::AdoptRef( | 577 skia::RefPtr<SkMallocPixelRef> etc1_pixel_ref = skia::AdoptRef( |
| 563 SkMallocPixelRef::NewWithData(info, 0, NULL, etc1_pixel_data.get())); | 578 SkMallocPixelRef::NewWithData(info, 0, NULL, etc1_pixel_data.get())); |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 830 dst_bitmap.eraseColor(0); | 845 dst_bitmap.eraseColor(0); |
| 831 SkAutoLockPixels dst_bitmap_lock(dst_bitmap); | 846 SkAutoLockPixels dst_bitmap_lock(dst_bitmap); |
| 832 | 847 |
| 833 SkCanvas canvas(dst_bitmap); | 848 SkCanvas canvas(dst_bitmap); |
| 834 canvas.scale(new_scale, new_scale); | 849 canvas.scale(new_scale, new_scale); |
| 835 canvas.drawBitmap(bitmap, 0, 0, NULL); | 850 canvas.drawBitmap(bitmap, 0, 0, NULL); |
| 836 dst_bitmap.setImmutable(); | 851 dst_bitmap.setImmutable(); |
| 837 | 852 |
| 838 return std::make_pair(dst_bitmap, new_scale * scale); | 853 return std::make_pair(dst_bitmap, new_scale * scale); |
| 839 } | 854 } |
| OLD | NEW |