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) { |
| 52 return (x + 3) & ~3; |
| 53 } |
| 54 |
| 55 gfx::Size GetEncodedSize(const gfx::Size& bitmap_size, bool supports_npot) { |
54 DCHECK(!bitmap_size.IsEmpty()); | 56 DCHECK(!bitmap_size.IsEmpty()); |
55 return gfx::Size(NextPowerOfTwo(bitmap_size.width()), | 57 if (!supports_npot) |
56 NextPowerOfTwo(bitmap_size.height())); | 58 return gfx::Size(NextPowerOfTwo(bitmap_size.width()), |
| 59 NextPowerOfTwo(bitmap_size.height())); |
| 60 else |
| 61 return gfx::Size(RoundUpMod4(bitmap_size.width()), |
| 62 RoundUpMod4(bitmap_size.height())); |
57 } | 63 } |
58 | 64 |
59 template<typename T> | 65 template<typename T> |
60 bool ReadBigEndianFromFile(base::File& file, T* out) { | 66 bool ReadBigEndianFromFile(base::File& file, T* out) { |
61 char buffer[sizeof(T)]; | 67 char buffer[sizeof(T)]; |
62 if (file.ReadAtCurrentPos(buffer, sizeof(T)) != sizeof(T)) | 68 if (file.ReadAtCurrentPos(buffer, sizeof(T)) != sizeof(T)) |
63 return false; | 69 return false; |
64 base::ReadBigEndian(buffer, out); | 70 base::ReadBigEndian(buffer, out); |
65 return true; | 71 return true; |
66 } | 72 } |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 | 364 |
359 compression_tasks_count_++; | 365 compression_tasks_count_++; |
360 | 366 |
361 base::Callback<void(skia::RefPtr<SkPixelRef>, const gfx::Size&)> | 367 base::Callback<void(skia::RefPtr<SkPixelRef>, const gfx::Size&)> |
362 post_compression_task = base::Bind(&ThumbnailStore::PostCompressionTask, | 368 post_compression_task = base::Bind(&ThumbnailStore::PostCompressionTask, |
363 weak_factory_.GetWeakPtr(), | 369 weak_factory_.GetWeakPtr(), |
364 tab_id, | 370 tab_id, |
365 time_stamp, | 371 time_stamp, |
366 scale); | 372 scale); |
367 | 373 |
368 base::WorkerPool::PostTask( | 374 gfx::Size raw_data_size(bitmap.width(), bitmap.height()); |
369 FROM_HERE, | 375 gfx::Size encoded_size = GetEncodedSize( |
370 base::Bind( | 376 raw_data_size, ui_resource_provider_->SupportsETC1NonPowerOfTwo()); |
371 &ThumbnailStore::CompressionTask, bitmap, post_compression_task), | 377 |
372 true); | 378 base::WorkerPool::PostTask(FROM_HERE, |
| 379 base::Bind(&ThumbnailStore::CompressionTask, |
| 380 bitmap, |
| 381 encoded_size, |
| 382 post_compression_task), |
| 383 true); |
373 } | 384 } |
374 | 385 |
375 void ThumbnailStore::ReadNextThumbnail() { | 386 void ThumbnailStore::ReadNextThumbnail() { |
376 if (read_queue_.empty() || read_in_progress_) | 387 if (read_queue_.empty() || read_in_progress_) |
377 return; | 388 return; |
378 | 389 |
379 TabId tab_id = read_queue_.front(); | 390 TabId tab_id = read_queue_.front(); |
380 read_in_progress_ = true; | 391 read_in_progress_ = true; |
381 | 392 |
382 base::FilePath file_path = GetFilePath(tab_id); | 393 base::FilePath file_path = GetFilePath(tab_id); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 content::BrowserThread::PostTask( | 543 content::BrowserThread::PostTask( |
533 content::BrowserThread::UI, FROM_HERE, post_write_task); | 544 content::BrowserThread::UI, FROM_HERE, post_write_task); |
534 } | 545 } |
535 | 546 |
536 void ThumbnailStore::PostWriteTask() { | 547 void ThumbnailStore::PostWriteTask() { |
537 write_tasks_count_--; | 548 write_tasks_count_--; |
538 } | 549 } |
539 | 550 |
540 void ThumbnailStore::CompressionTask( | 551 void ThumbnailStore::CompressionTask( |
541 SkBitmap raw_data, | 552 SkBitmap raw_data, |
| 553 gfx::Size encoded_size, |
542 const base::Callback<void(skia::RefPtr<SkPixelRef>, const gfx::Size&)>& | 554 const base::Callback<void(skia::RefPtr<SkPixelRef>, const gfx::Size&)>& |
543 post_compression_task) { | 555 post_compression_task) { |
544 skia::RefPtr<SkPixelRef> compressed_data; | 556 skia::RefPtr<SkPixelRef> compressed_data; |
545 gfx::Size content_size; | 557 gfx::Size content_size; |
546 | 558 |
547 if (!raw_data.empty()) { | 559 if (!raw_data.empty()) { |
548 SkAutoLockPixels raw_data_lock(raw_data); | 560 SkAutoLockPixels raw_data_lock(raw_data); |
549 gfx::Size raw_data_size(raw_data.width(), raw_data.height()); | 561 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. | 562 size_t pixel_size = 4; // Pixel size is 4 bytes for kARGB_8888_Config. |
551 size_t stride = pixel_size * raw_data_size.width(); | 563 size_t stride = pixel_size * raw_data_size.width(); |
552 | 564 |
553 gfx::Size encoded_size = GetEncodedSize(raw_data_size); | |
554 size_t encoded_bytes = | 565 size_t encoded_bytes = |
555 etc1_get_encoded_data_size(encoded_size.width(), encoded_size.height()); | 566 etc1_get_encoded_data_size(encoded_size.width(), encoded_size.height()); |
556 SkImageInfo info = {encoded_size.width(), | 567 SkImageInfo info = {encoded_size.width(), |
557 encoded_size.height(), | 568 encoded_size.height(), |
558 kUnknown_SkColorType, | 569 kUnknown_SkColorType, |
559 kUnpremul_SkAlphaType}; | 570 kUnpremul_SkAlphaType}; |
560 skia::RefPtr<SkData> etc1_pixel_data = skia::AdoptRef( | 571 skia::RefPtr<SkData> etc1_pixel_data = skia::AdoptRef( |
561 SkData::NewFromMalloc(new uint8_t[encoded_bytes], encoded_bytes)); | 572 SkData::NewFromMalloc(new uint8_t[encoded_bytes], encoded_bytes)); |
562 skia::RefPtr<SkMallocPixelRef> etc1_pixel_ref = skia::AdoptRef( | 573 skia::RefPtr<SkMallocPixelRef> etc1_pixel_ref = skia::AdoptRef( |
563 SkMallocPixelRef::NewWithData(info, 0, NULL, etc1_pixel_data.get())); | 574 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); | 841 dst_bitmap.eraseColor(0); |
831 SkAutoLockPixels dst_bitmap_lock(dst_bitmap); | 842 SkAutoLockPixels dst_bitmap_lock(dst_bitmap); |
832 | 843 |
833 SkCanvas canvas(dst_bitmap); | 844 SkCanvas canvas(dst_bitmap); |
834 canvas.scale(new_scale, new_scale); | 845 canvas.scale(new_scale, new_scale); |
835 canvas.drawBitmap(bitmap, 0, 0, NULL); | 846 canvas.drawBitmap(bitmap, 0, 0, NULL); |
836 dst_bitmap.setImmutable(); | 847 dst_bitmap.setImmutable(); |
837 | 848 |
838 return std::make_pair(dst_bitmap, new_scale * scale); | 849 return std::make_pair(dst_bitmap, new_scale * scale); |
839 } | 850 } |
OLD | NEW |