Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: chrome/browser/chromeos/login/user_image_loader.cc

Issue 11968044: Fix login visual hitch on chromebook (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/chromeos/login/user_image_loader.h" 5 #include "chrome/browser/chromeos/login/user_image_loader.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_path.h" 8 #include "base/file_path.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/threading/worker_pool.h"
12 #include "chrome/browser/chromeos/login/helper.h" 13 #include "chrome/browser/chromeos/login/helper.h"
13 #include "chrome/browser/chromeos/login/user_image.h" 14 #include "chrome/browser/chromeos/login/user_image.h"
14 #include "content/public/browser/browser_thread.h" 15 #include "content/public/browser/browser_thread.h"
15 #include "skia/ext/image_operations.h" 16 #include "skia/ext/image_operations.h"
16 #include "third_party/skia/include/core/SkBitmap.h" 17 #include "third_party/skia/include/core/SkBitmap.h"
17 #include "ui/gfx/codec/png_codec.h" 18 #include "ui/gfx/codec/png_codec.h"
18 #include "ui/gfx/skbitmap_operations.h" 19 #include "ui/gfx/skbitmap_operations.h"
19 20
20 using content::BrowserThread; 21 using content::BrowserThread;
21 22
(...skipping 15 matching lines...) Expand all
37 38
38 UserImageLoader::~UserImageLoader() { 39 UserImageLoader::~UserImageLoader() {
39 } 40 }
40 41
41 void UserImageLoader::Start(const std::string& filepath, 42 void UserImageLoader::Start(const std::string& filepath,
42 int size, 43 int size,
43 const LoadedCallback& loaded_cb) { 44 const LoadedCallback& loaded_cb) {
44 target_message_loop_ = MessageLoop::current(); 45 target_message_loop_ = MessageLoop::current();
45 46
46 ImageInfo image_info(size, loaded_cb); 47 ImageInfo image_info(size, loaded_cb);
47 BrowserThread::PostTask( 48 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
48 BrowserThread::FILE, FROM_HERE, 49 SequenceToken sequence_token = pool->GetSequenceToken();
49 base::Bind(&UserImageLoader::LoadImage, this, filepath, image_info)); 50 scoped_refptr<base::SequencedTaskRunner> task_runner = pool->
51 GetSequencedTaskRunnerWithShutdownBehavior(sequence_token,
52 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
53 task_runner->PostTask(
54 FROM_HERE,
55 base::Bind(&UserImageLoader::LoadImage, this, filepath, image_info,
56 task_runner));
50 } 57 }
51 58
52 void UserImageLoader::LoadImage(const std::string& filepath, 59 void UserImageLoader::LoadImage(
53 const ImageInfo& image_info) { 60 const std::string& filepath,
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 61 const ImageInfo& image_info,
62 scoped_refptr<base::SequencedTaskRunner> task_runner) {
63 task_runner->RunsTasksOnCurrentThread();
Joao da Silva 2013/01/22 10:04:12 DCHECK(task_runner->RunsTasksOnCurrentThread());
bshe 2013/01/22 15:36:26 Done.
55 64
56 std::string image_data; 65 std::string image_data;
57 file_util::ReadFileToString(FilePath(filepath), &image_data); 66 file_util::ReadFileToString(FilePath(filepath), &image_data);
58 67
59 scoped_refptr<ImageDecoder> image_decoder = 68 scoped_refptr<ImageDecoder> image_decoder =
60 new ImageDecoder(this, image_data, image_codec_); 69 new ImageDecoder(this, image_data, image_codec_);
61 image_info_map_.insert(std::make_pair(image_decoder.get(), image_info)); 70 image_info_map_.insert(std::make_pair(image_decoder.get(), image_info));
Joao da Silva 2013/01/22 10:04:12 Access to image_info_map_ must be locked
bshe 2013/01/22 15:36:26 Dooh. I just focused on the callback functions. Ad
62 image_decoder->Start(); 71 image_decoder->Start(task_runner);
63 } 72 }
64 73
65 void UserImageLoader::OnImageDecoded(const ImageDecoder* decoder, 74 void UserImageLoader::OnImageDecoded(const ImageDecoder* decoder,
66 const SkBitmap& decoded_image) { 75 const SkBitmap& decoded_image) {
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 76 ImageInfoMap::iterator info_it;
68 77 scoped_ptr<ImageInfo> image_info;
69 ImageInfoMap::iterator info_it = image_info_map_.find(decoder); 78 {
70 if (info_it == image_info_map_.end()) { 79 base::AutoLock lock(lock_);
71 NOTREACHED(); 80 info_it = image_info_map_.find(decoder);
72 return; 81 if (info_it == image_info_map_.end()) {
82 NOTREACHED();
83 return;
84 }
85 image_info.reset(
86 new ImageInfo(info_it->second.size, info_it->second.loaded_cb));
87 image_info_map_.erase(info_it);
73 } 88 }
74 89
75 const ImageInfo& image_info = info_it->second;
76 SkBitmap final_image = decoded_image; 90 SkBitmap final_image = decoded_image;
77 91
78 if (image_info.size > 0) { 92 if (image_info->size > 0) {
79 // Auto crop the image, taking the largest square in the center. 93 // Auto crop the image, taking the largest square in the center.
80 int size = std::min(decoded_image.width(), decoded_image.height()); 94 int size = std::min(decoded_image.width(), decoded_image.height());
81 int x = (decoded_image.width() - size) / 2; 95 int x = (decoded_image.width() - size) / 2;
82 int y = (decoded_image.height() - size) / 2; 96 int y = (decoded_image.height() - size) / 2;
83 SkBitmap cropped_image = 97 SkBitmap cropped_image =
84 SkBitmapOperations::CreateTiledBitmap(decoded_image, x, y, size, size); 98 SkBitmapOperations::CreateTiledBitmap(decoded_image, x, y, size, size);
85 if (size > image_info.size) { 99 if (size > image_info->size) {
86 // Also downsize the image to save space and memory. 100 // Also downsize the image to save space and memory.
87 final_image = 101 final_image =
88 skia::ImageOperations::Resize(cropped_image, 102 skia::ImageOperations::Resize(cropped_image,
89 skia::ImageOperations::RESIZE_LANCZOS3, 103 skia::ImageOperations::RESIZE_LANCZOS3,
90 image_info.size, 104 image_info->size,
91 image_info.size); 105 image_info->size);
92 } else { 106 } else {
93 final_image = cropped_image; 107 final_image = cropped_image;
94 } 108 }
95 } 109 }
96 gfx::ImageSkia final_image_skia(final_image); 110 gfx::ImageSkia final_image_skia(final_image);
97 final_image_skia.MakeThreadSafe(); 111 final_image_skia.MakeThreadSafe();
98 UserImage user_image(final_image_skia, decoder->get_image_data()); 112 UserImage user_image(final_image_skia, decoder->get_image_data());
99 if (image_codec_ == ImageDecoder::ROBUST_JPEG_CODEC) 113 if (image_codec_ == ImageDecoder::ROBUST_JPEG_CODEC)
100 user_image.MarkAsSafe(); 114 user_image.MarkAsSafe();
101 target_message_loop_->PostTask( 115 target_message_loop_->PostTask(
102 FROM_HERE, 116 FROM_HERE,
103 base::Bind(image_info.loaded_cb, user_image)); 117 base::Bind(image_info->loaded_cb, user_image));
104
105 image_info_map_.erase(info_it);
106 } 118 }
107 119
108 void UserImageLoader::OnDecodeImageFailed(const ImageDecoder* decoder) { 120 void UserImageLoader::OnDecodeImageFailed(const ImageDecoder* decoder) {
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 121 ImageInfoMap::iterator info_it;
110 122 scoped_ptr<ImageInfo> image_info;
111 ImageInfoMap::iterator info_it = image_info_map_.find(decoder); 123 {
112 if (info_it == image_info_map_.end()) { 124 base::AutoLock lock(lock_);
113 NOTREACHED(); 125 info_it = image_info_map_.find(decoder);
114 return; 126 if (info_it == image_info_map_.end()) {
127 NOTREACHED();
128 return;
129 }
130 image_info.reset(
131 new ImageInfo(info_it->second.size, info_it->second.loaded_cb));
132 image_info_map_.erase(decoder);
115 } 133 }
116 134
117 const ImageInfo& image_info = info_it->second;
118 target_message_loop_->PostTask( 135 target_message_loop_->PostTask(
119 FROM_HERE, 136 FROM_HERE,
120 base::Bind(image_info.loaded_cb, UserImage())); 137 base::Bind(image_info->loaded_cb, UserImage()));
121 image_info_map_.erase(decoder);
122 } 138 }
123 139
124 } // namespace chromeos 140 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698