OLD | NEW |
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/themes/browser_theme_pack.h" | 5 #include "chrome/browser/themes/browser_theme_pack.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 pack->BuildSourceImagesArray(file_paths); | 429 pack->BuildSourceImagesArray(file_paths); |
430 | 430 |
431 if (!pack->LoadRawBitmapsTo(file_paths, &pack->images_on_ui_thread_)) | 431 if (!pack->LoadRawBitmapsTo(file_paths, &pack->images_on_ui_thread_)) |
432 return NULL; | 432 return NULL; |
433 | 433 |
434 pack->CopyImagesTo(pack->images_on_ui_thread_, &pack->images_on_file_thread_); | 434 pack->CopyImagesTo(pack->images_on_ui_thread_, &pack->images_on_file_thread_); |
435 | 435 |
436 pack->CreateImages(&pack->images_on_ui_thread_); | 436 pack->CreateImages(&pack->images_on_ui_thread_); |
437 pack->CreateImages(&pack->images_on_file_thread_); | 437 pack->CreateImages(&pack->images_on_file_thread_); |
438 | 438 |
439 // For M22, as it is not possible to easily determine which scale factors are | 439 // Make sure the |images_on_file_thread_| has bitmaps for supported |
440 // in use, assume that the 1x scale factor is in use. | 440 // scale factors before passing to FILE thread. |
441 std::vector<ui::ScaleFactor> scale_factors_in_use; | 441 for (ImageCache::iterator it = pack->images_on_file_thread_.begin(); |
442 scale_factors_in_use.push_back(ui::SCALE_FACTOR_100P); | 442 it != pack->images_on_file_thread_.end(); ++it) { |
443 pack->GenerateImageReps(scale_factors_in_use); | 443 gfx::ImageSkia* image_skia = |
| 444 const_cast<gfx::ImageSkia*>(it->second->ToImageSkia()); |
| 445 image_skia->MakeThreadSafe(); |
| 446 } |
444 | 447 |
445 // The BrowserThemePack is now in a consistent state. | 448 // The BrowserThemePack is now in a consistent state. |
446 return pack; | 449 return pack; |
447 } | 450 } |
448 | 451 |
449 // static | 452 // static |
450 scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromDataPack( | 453 scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromDataPack( |
451 const FilePath& path, const std::string& expected_id) { | 454 const FilePath& path, const std::string& expected_id) { |
452 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 455 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
453 // Allow IO on UI thread due to deep-seated theme design issues. | 456 // Allow IO on UI thread due to deep-seated theme design issues. |
(...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1110 void BrowserThemePack::RepackImages(const ImageCache& images, | 1113 void BrowserThemePack::RepackImages(const ImageCache& images, |
1111 RawImages* reencoded_images) const { | 1114 RawImages* reencoded_images) const { |
1112 | 1115 |
1113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 1116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
1114 | 1117 |
1115 typedef std::vector<ui::ScaleFactor> ScaleFactors; | 1118 typedef std::vector<ui::ScaleFactor> ScaleFactors; |
1116 for (ImageCache::const_iterator it = images.begin(); | 1119 for (ImageCache::const_iterator it = images.begin(); |
1117 it != images.end(); ++it) { | 1120 it != images.end(); ++it) { |
1118 gfx::ImageSkia image_skia = *it->second->ToImageSkia(); | 1121 gfx::ImageSkia image_skia = *it->second->ToImageSkia(); |
1119 | 1122 |
1120 // Attempt to generate image reps for all supported scale factors. | |
1121 for (ScaleFactors::const_iterator factor_it = scale_factors_.begin(); | |
1122 factor_it != scale_factors_.end(); ++factor_it) { | |
1123 // Ask for representation to force the representation to be generated | |
1124 // if it wasn't already. | |
1125 image_skia.GetRepresentation(*factor_it); | |
1126 } | |
1127 | |
1128 typedef std::vector<gfx::ImageSkiaRep> ImageSkiaReps; | 1123 typedef std::vector<gfx::ImageSkiaRep> ImageSkiaReps; |
1129 ImageSkiaReps image_reps = image_skia.image_reps(); | 1124 ImageSkiaReps image_reps = image_skia.image_reps(); |
1130 if (image_reps.empty()) { | 1125 if (image_reps.empty()) { |
1131 NOTREACHED() << "No image reps for resource " << it->first << "."; | 1126 NOTREACHED() << "No image reps for resource " << it->first << "."; |
1132 } | 1127 } |
1133 for (ImageSkiaReps::iterator rep_it = image_reps.begin(); | 1128 for (ImageSkiaReps::iterator rep_it = image_reps.begin(); |
1134 rep_it != image_reps.end(); ++rep_it) { | 1129 rep_it != image_reps.end(); ++rep_it) { |
1135 std::vector<unsigned char> bitmap_data; | 1130 std::vector<unsigned char> bitmap_data; |
1136 if (!gfx::PNGCodec::EncodeBGRASkBitmap(rep_it->sk_bitmap(), false, | 1131 if (!gfx::PNGCodec::EncodeBGRASkBitmap(rep_it->sk_bitmap(), false, |
1137 &bitmap_data)) { | 1132 &bitmap_data)) { |
1138 NOTREACHED() << "Image file for resource " << it->first | 1133 NOTREACHED() << "Image file for resource " << it->first |
1139 << " could not be encoded."; | 1134 << " could not be encoded."; |
1140 } | 1135 } |
1141 int raw_id = GetRawIDByPersistentID(it->first, rep_it->scale_factor()); | 1136 int raw_id = GetRawIDByPersistentID(it->first, rep_it->scale_factor()); |
1142 (*reencoded_images)[raw_id] = | 1137 (*reencoded_images)[raw_id] = |
1143 base::RefCountedBytes::TakeVector(&bitmap_data); | 1138 base::RefCountedBytes::TakeVector(&bitmap_data); |
1144 } | 1139 } |
1145 } | 1140 } |
1146 } | 1141 } |
1147 | 1142 |
1148 void BrowserThemePack::GenerateImageReps( | |
1149 const std::vector<ui::ScaleFactor>& scale_factors) { | |
1150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
1151 for (ImageCache::const_iterator it = images_on_ui_thread_.begin(); | |
1152 it != images_on_ui_thread_.end(); | |
1153 ++it) { | |
1154 const gfx::ImageSkia* image1 = it->second->ToImageSkia(); | |
1155 const gfx::ImageSkia* image2 = | |
1156 images_on_file_thread_[it->first]->ToImageSkia(); | |
1157 | |
1158 // Ensure that image reps are generated and cached in |image1| by | |
1159 // calling GetRepresentation(). | |
1160 for (size_t i = 0; i < scale_factors.size(); ++i) | |
1161 image1->GetRepresentation(scale_factors[i]); | |
1162 | |
1163 // |image1| and |image2| have ImageSkiaSources which produce pixel | |
1164 // equivalent output. Instead of regenerating again, copy the image reps | |
1165 // which were generated for |image1| into |image2|. | |
1166 // Don't do a deep copy of the SkBitmaps as SkBitmap is thread safe. | |
1167 std::vector<gfx::ImageSkiaRep> image1_reps = image1->image_reps(); | |
1168 for (size_t i = 0; i < image1_reps.size(); ++i) { | |
1169 gfx::ImageSkiaRep image1_rep = image1_reps[i]; | |
1170 const_cast<gfx::ImageSkia*>(image2)->AddRepresentation(gfx::ImageSkiaRep( | |
1171 image1_rep.sk_bitmap(), image1_rep.scale_factor())); | |
1172 } | |
1173 } | |
1174 } | |
1175 | |
1176 void BrowserThemePack::MergeImageCaches( | 1143 void BrowserThemePack::MergeImageCaches( |
1177 const ImageCache& source, ImageCache* destination) const { | 1144 const ImageCache& source, ImageCache* destination) const { |
1178 for (ImageCache::const_iterator it = source.begin(); it != source.end(); | 1145 for (ImageCache::const_iterator it = source.begin(); it != source.end(); |
1179 ++it) { | 1146 ++it) { |
1180 ImageCache::const_iterator image_it = destination->find(it->first); | 1147 ImageCache::const_iterator image_it = destination->find(it->first); |
1181 if (image_it != destination->end()) | 1148 if (image_it != destination->end()) |
1182 delete image_it->second; | 1149 delete image_it->second; |
1183 | 1150 |
1184 (*destination)[it->first] = it->second; | 1151 (*destination)[it->first] = it->second; |
1185 } | 1152 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1224 ui::ScaleFactor scale_factor) const { | 1191 ui::ScaleFactor scale_factor) const { |
1225 if (prs_id < 0) | 1192 if (prs_id < 0) |
1226 return -1; | 1193 return -1; |
1227 | 1194 |
1228 for (size_t i = 0; i < scale_factors_.size(); ++i) { | 1195 for (size_t i = 0; i < scale_factors_.size(); ++i) { |
1229 if (scale_factors_[i] == scale_factor) | 1196 if (scale_factors_[i] == scale_factor) |
1230 return static_cast<int>(kPersistingImagesLength * i) + prs_id; | 1197 return static_cast<int>(kPersistingImagesLength * i) + prs_id; |
1231 } | 1198 } |
1232 return -1; | 1199 return -1; |
1233 } | 1200 } |
OLD | NEW |