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/chromeos/login/users/avatar/user_image_loader.h" | 5 #include "chrome/browser/chromeos/login/users/avatar/user_image_loader.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
59 FROM_HERE, | 59 FROM_HERE, |
60 base::Bind(&UserImageLoader::DecodeImage, | 60 base::Bind(&UserImageLoader::DecodeImage, |
61 this, | 61 this, |
62 base::Passed(&data), | 62 base::Passed(&data), |
63 ImageInfo(std::string(), pixels_per_side, loaded_cb))); | 63 ImageInfo(std::string(), pixels_per_side, loaded_cb))); |
64 } | 64 } |
65 | 65 |
66 void UserImageLoader::ReadAndDecodeImage(const ImageInfo& image_info) { | 66 void UserImageLoader::ReadAndDecodeImage(const ImageInfo& image_info) { |
67 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 67 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
68 | 68 |
69 scoped_ptr<std::string> data(new std::string); | 69 scoped_ptr<std::string> data(new std::string); |
dcheng
2015/02/27 16:47:17
Do we still need to heap-allocate this?
Theresa
2015/03/04 03:10:06
Yes, a bunch of browser tests start failing withou
| |
70 if (!base::ReadFileToString(base::FilePath(image_info.file_path), data.get())) | 70 if (!base::ReadFileToString(base::FilePath(image_info.file_path), data.get())) |
71 LOG(ERROR) << "Failed to read image " << image_info.file_path; | 71 LOG(ERROR) << "Failed to read image " << image_info.file_path; |
72 | 72 |
73 // In case ReadFileToString() fails, |data| is empty and DecodeImage() calls | 73 // In case ReadFileToString() fails, |data| is empty and DecodeImage() calls |
74 // back to OnDecodeImageFailed(). | 74 // back to OnDecodeImageFailed(). |
75 DecodeImage(data.Pass(), image_info); | 75 DecodeImage(data.Pass(), image_info); |
76 } | 76 } |
77 | 77 |
78 void UserImageLoader::DecodeImage(const scoped_ptr<std::string> data, | 78 void UserImageLoader::DecodeImage(const scoped_ptr<std::string> data, |
79 const ImageInfo& image_info) { | 79 const ImageInfo& image_info) { |
80 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 80 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
81 | 81 |
82 scoped_refptr<ImageDecoder> image_decoder = | 82 std::vector<unsigned char> image_data(data->begin(), data->end()); |
83 new ImageDecoder(this, *data, image_codec_); | 83 image_info_queue_.push(image_info); |
84 image_info_map_.insert(std::make_pair(image_decoder.get(), image_info)); | 84 image_data_queue_.push(image_data); |
85 image_decoder->Start(background_task_runner_); | 85 ImageDecoder::GetInstance()->Start(this, *data, image_codec_, |
86 background_task_runner_); | |
86 } | 87 } |
87 | 88 |
88 void UserImageLoader::OnImageDecoded(const ImageDecoder* decoder, | 89 void UserImageLoader::OnImageDecoded(const SkBitmap& decoded_image) { |
89 const SkBitmap& decoded_image) { | |
90 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 90 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
91 | 91 |
92 ImageInfoMap::iterator it = image_info_map_.find(decoder); | 92 ImageInfo image_info = image_info_queue_.front(); |
dcheng
2015/02/27 16:47:17
const ImageInfo& to save a copy?
Theresa
2015/03/04 03:10:06
Done.
| |
93 if (it == image_info_map_.end()) { | 93 const std::string file_path = image_info.file_path; |
94 NOTREACHED(); | 94 const int target_size = image_info.pixels_per_side; |
95 return; | 95 const LoadedCallback loaded_cb = image_info.loaded_cb; |
96 } | 96 image_info_queue_.pop(); |
97 const std::string file_path = it->second.file_path; | |
98 const int target_size = it->second.pixels_per_side; | |
99 const LoadedCallback loaded_cb = it->second.loaded_cb; | |
100 image_info_map_.erase(it); | |
101 | 97 |
102 SkBitmap final_image = decoded_image; | 98 SkBitmap final_image = decoded_image; |
103 | 99 |
104 if (target_size > 0) { | 100 if (target_size > 0) { |
105 // Auto crop the image, taking the largest square in the center. | 101 // Auto crop the image, taking the largest square in the center. |
106 int pixels_per_side = | 102 int pixels_per_side = |
107 std::min(decoded_image.width(), decoded_image.height()); | 103 std::min(decoded_image.width(), decoded_image.height()); |
108 int x = (decoded_image.width() - pixels_per_side) / 2; | 104 int x = (decoded_image.width() - pixels_per_side) / 2; |
109 int y = (decoded_image.height() - pixels_per_side) / 2; | 105 int y = (decoded_image.height() - pixels_per_side) / 2; |
110 SkBitmap cropped_image = SkBitmapOperations::CreateTiledBitmap( | 106 SkBitmap cropped_image = SkBitmapOperations::CreateTiledBitmap( |
111 decoded_image, x, y, pixels_per_side, pixels_per_side); | 107 decoded_image, x, y, pixels_per_side, pixels_per_side); |
112 if (pixels_per_side > target_size) { | 108 if (pixels_per_side > target_size) { |
113 // Also downsize the image to save space and memory. | 109 // Also downsize the image to save space and memory. |
114 final_image = | 110 final_image = |
115 skia::ImageOperations::Resize(cropped_image, | 111 skia::ImageOperations::Resize(cropped_image, |
116 skia::ImageOperations::RESIZE_LANCZOS3, | 112 skia::ImageOperations::RESIZE_LANCZOS3, |
117 target_size, | 113 target_size, |
118 target_size); | 114 target_size); |
119 } else { | 115 } else { |
120 final_image = cropped_image; | 116 final_image = cropped_image; |
121 } | 117 } |
122 } | 118 } |
123 // Make the SkBitmap immutable as we won't modify it. This is important | 119 // Make the SkBitmap immutable as we won't modify it. This is important |
124 // because otherwise it gets duplicated during painting, wasting memory. | 120 // because otherwise it gets duplicated during painting, wasting memory. |
125 final_image.setImmutable(); | 121 final_image.setImmutable(); |
126 gfx::ImageSkia final_image_skia = | 122 gfx::ImageSkia final_image_skia = |
127 gfx::ImageSkia::CreateFrom1xBitmap(final_image); | 123 gfx::ImageSkia::CreateFrom1xBitmap(final_image); |
128 final_image_skia.MakeThreadSafe(); | 124 final_image_skia.MakeThreadSafe(); |
129 user_manager::UserImage user_image(final_image_skia, | 125 user_manager::UserImage user_image(final_image_skia, |
130 decoder->get_image_data()); | 126 image_data_queue_.front()); |
dcheng
2015/02/27 16:47:17
How about adding the original raw image as a membe
Theresa
2015/03/04 03:10:06
Done.
| |
131 user_image.set_file_path(file_path); | 127 user_image.set_file_path(file_path); |
132 if (image_codec_ == ImageDecoder::ROBUST_JPEG_CODEC) | 128 if (image_codec_ == ImageDecoder::ROBUST_JPEG_CODEC) |
133 user_image.MarkAsSafe(); | 129 user_image.MarkAsSafe(); |
134 foreground_task_runner_->PostTask(FROM_HERE, | 130 foreground_task_runner_->PostTask(FROM_HERE, |
135 base::Bind(loaded_cb, user_image)); | 131 base::Bind(loaded_cb, user_image)); |
132 | |
133 image_data_queue_.pop(); | |
136 } | 134 } |
137 | 135 |
138 void UserImageLoader::OnDecodeImageFailed(const ImageDecoder* decoder) { | 136 void UserImageLoader::OnDecodeImageFailed() { |
139 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 137 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
140 | 138 |
141 ImageInfoMap::iterator it = image_info_map_.find(decoder); | 139 ImageInfo image_info = image_info_queue_.front(); |
142 if (it == image_info_map_.end()) { | 140 const LoadedCallback loaded_cb = image_info.loaded_cb; |
143 NOTREACHED(); | 141 image_info_queue_.pop(); |
144 return; | 142 image_data_queue_.pop(); |
145 } | |
146 const LoadedCallback loaded_cb = it->second.loaded_cb; | |
147 image_info_map_.erase(it); | |
148 | 143 |
149 foreground_task_runner_->PostTask( | 144 foreground_task_runner_->PostTask( |
150 FROM_HERE, base::Bind(loaded_cb, user_manager::UserImage())); | 145 FROM_HERE, base::Bind(loaded_cb, user_manager::UserImage())); |
151 } | 146 } |
152 | 147 |
153 } // namespace chromeos | 148 } // namespace chromeos |
OLD | NEW |