| 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/extensions/image_loader.h" | 5 #include "chrome/browser/extensions/image_loader.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 image_info.desired_size)) { | 64 image_info.desired_size)) { |
| 65 return skia::ImageOperations::Resize( | 65 return skia::ImageOperations::Resize( |
| 66 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, | 66 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, |
| 67 image_info.desired_size.width(), image_info.desired_size.height()); | 67 image_info.desired_size.width(), image_info.desired_size.height()); |
| 68 } | 68 } |
| 69 | 69 |
| 70 return bitmap; | 70 return bitmap; |
| 71 } | 71 } |
| 72 | 72 |
| 73 void LoadResourceOnUIThread(int resource_id, SkBitmap* bitmap) { | 73 void LoadResourceOnUIThread(int resource_id, SkBitmap* bitmap) { |
| 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 74 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 75 | 75 |
| 76 gfx::ImageSkia image( | 76 gfx::ImageSkia image( |
| 77 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id)); | 77 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id)); |
| 78 image.MakeThreadSafe(); | 78 image.MakeThreadSafe(); |
| 79 *bitmap = *image.bitmap(); | 79 *bitmap = *image.bitmap(); |
| 80 } | 80 } |
| 81 | 81 |
| 82 void LoadImageOnBlockingPool(const ImageLoader::ImageRepresentation& image_info, | 82 void LoadImageOnBlockingPool(const ImageLoader::ImageRepresentation& image_info, |
| 83 SkBitmap* bitmap) { | 83 SkBitmap* bitmap) { |
| 84 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 84 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 ImageRepresentation::RESIZE_WHEN_LARGER, | 313 ImageRepresentation::RESIZE_WHEN_LARGER, |
| 314 max_size, | 314 max_size, |
| 315 ui::SCALE_FACTOR_100P)); | 315 ui::SCALE_FACTOR_100P)); |
| 316 LoadImagesAsync(extension, info_list, callback); | 316 LoadImagesAsync(extension, info_list, callback); |
| 317 } | 317 } |
| 318 | 318 |
| 319 void ImageLoader::LoadImagesAsync( | 319 void ImageLoader::LoadImagesAsync( |
| 320 const Extension* extension, | 320 const Extension* extension, |
| 321 const std::vector<ImageRepresentation>& info_list, | 321 const std::vector<ImageRepresentation>& info_list, |
| 322 const ImageLoaderImageCallback& callback) { | 322 const ImageLoaderImageCallback& callback) { |
| 323 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 323 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 324 DCHECK(!BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 324 DCHECK(!BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 325 base::PostTaskAndReplyWithResult( | 325 base::PostTaskAndReplyWithResult( |
| 326 BrowserThread::GetBlockingPool(), | 326 BrowserThread::GetBlockingPool(), |
| 327 FROM_HERE, | 327 FROM_HERE, |
| 328 base::Bind(LoadImagesOnBlockingPool, | 328 base::Bind(LoadImagesOnBlockingPool, |
| 329 info_list, | 329 info_list, |
| 330 LoadResourceBitmaps(extension, info_list)), | 330 LoadResourceBitmaps(extension, info_list)), |
| 331 base::Bind( | 331 base::Bind( |
| 332 &ImageLoader::ReplyBack, weak_ptr_factory_.GetWeakPtr(), callback)); | 332 &ImageLoader::ReplyBack, weak_ptr_factory_.GetWeakPtr(), callback)); |
| 333 } | 333 } |
| 334 | 334 |
| 335 void ImageLoader::LoadImageFamilyAsync( | 335 void ImageLoader::LoadImageFamilyAsync( |
| 336 const extensions::Extension* extension, | 336 const extensions::Extension* extension, |
| 337 const std::vector<ImageRepresentation>& info_list, | 337 const std::vector<ImageRepresentation>& info_list, |
| 338 const ImageLoaderImageFamilyCallback& callback) { | 338 const ImageLoaderImageFamilyCallback& callback) { |
| 339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 339 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 340 DCHECK(!BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 340 DCHECK(!BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 341 base::PostTaskAndReplyWithResult( | 341 base::PostTaskAndReplyWithResult( |
| 342 BrowserThread::GetBlockingPool(), | 342 BrowserThread::GetBlockingPool(), |
| 343 FROM_HERE, | 343 FROM_HERE, |
| 344 base::Bind(LoadImagesOnBlockingPool, | 344 base::Bind(LoadImagesOnBlockingPool, |
| 345 info_list, | 345 info_list, |
| 346 LoadResourceBitmaps(extension, info_list)), | 346 LoadResourceBitmaps(extension, info_list)), |
| 347 base::Bind(&ImageLoader::ReplyBackWithImageFamily, | 347 base::Bind(&ImageLoader::ReplyBackWithImageFamily, |
| 348 weak_ptr_factory_.GetWeakPtr(), | 348 weak_ptr_factory_.GetWeakPtr(), |
| 349 callback)); | 349 callback)); |
| 350 } | 350 } |
| 351 | 351 |
| 352 void ImageLoader::ReplyBack(const ImageLoaderImageCallback& callback, | 352 void ImageLoader::ReplyBack(const ImageLoaderImageCallback& callback, |
| 353 const std::vector<LoadResult>& load_result) { | 353 const std::vector<LoadResult>& load_result) { |
| 354 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 354 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 355 | 355 |
| 356 gfx::ImageSkia image_skia; | 356 gfx::ImageSkia image_skia; |
| 357 | 357 |
| 358 for (std::vector<LoadResult>::const_iterator it = load_result.begin(); | 358 for (std::vector<LoadResult>::const_iterator it = load_result.begin(); |
| 359 it != load_result.end(); ++it) { | 359 it != load_result.end(); ++it) { |
| 360 const SkBitmap& bitmap = it->bitmap; | 360 const SkBitmap& bitmap = it->bitmap; |
| 361 const ImageRepresentation& image_rep = it->image_representation; | 361 const ImageRepresentation& image_rep = it->image_representation; |
| 362 | 362 |
| 363 image_skia.AddRepresentation(gfx::ImageSkiaRep( | 363 image_skia.AddRepresentation(gfx::ImageSkiaRep( |
| 364 bitmap, | 364 bitmap, |
| 365 ui::GetImageScale(image_rep.scale_factor))); | 365 ui::GetImageScale(image_rep.scale_factor))); |
| 366 } | 366 } |
| 367 | 367 |
| 368 gfx::Image image; | 368 gfx::Image image; |
| 369 if (!image_skia.isNull()) { | 369 if (!image_skia.isNull()) { |
| 370 image_skia.MakeThreadSafe(); | 370 image_skia.MakeThreadSafe(); |
| 371 image = gfx::Image(image_skia); | 371 image = gfx::Image(image_skia); |
| 372 } | 372 } |
| 373 | 373 |
| 374 callback.Run(image); | 374 callback.Run(image); |
| 375 } | 375 } |
| 376 | 376 |
| 377 void ImageLoader::ReplyBackWithImageFamily( | 377 void ImageLoader::ReplyBackWithImageFamily( |
| 378 const ImageLoaderImageFamilyCallback& callback, | 378 const ImageLoaderImageFamilyCallback& callback, |
| 379 const std::vector<LoadResult>& load_result) { | 379 const std::vector<LoadResult>& load_result) { |
| 380 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 380 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 381 | 381 |
| 382 std::map<std::pair<int, int>, gfx::ImageSkia> image_skia_map; | 382 std::map<std::pair<int, int>, gfx::ImageSkia> image_skia_map; |
| 383 gfx::ImageFamily image_family; | 383 gfx::ImageFamily image_family; |
| 384 | 384 |
| 385 for (std::vector<LoadResult>::const_iterator it = load_result.begin(); | 385 for (std::vector<LoadResult>::const_iterator it = load_result.begin(); |
| 386 it != load_result.end(); | 386 it != load_result.end(); |
| 387 ++it) { | 387 ++it) { |
| 388 const SkBitmap& bitmap = it->bitmap; | 388 const SkBitmap& bitmap = it->bitmap; |
| 389 const ImageRepresentation& image_rep = it->image_representation; | 389 const ImageRepresentation& image_rep = it->image_representation; |
| 390 const std::pair<int, int> key = std::make_pair( | 390 const std::pair<int, int> key = std::make_pair( |
| 391 image_rep.desired_size.width(), image_rep.desired_size.height()); | 391 image_rep.desired_size.width(), image_rep.desired_size.height()); |
| 392 // Create a new ImageSkia for this width/height, or add a representation to | 392 // Create a new ImageSkia for this width/height, or add a representation to |
| 393 // an existing ImageSkia with the same width/height. | 393 // an existing ImageSkia with the same width/height. |
| 394 image_skia_map[key].AddRepresentation( | 394 image_skia_map[key].AddRepresentation( |
| 395 gfx::ImageSkiaRep(bitmap, ui::GetImageScale(image_rep.scale_factor))); | 395 gfx::ImageSkiaRep(bitmap, ui::GetImageScale(image_rep.scale_factor))); |
| 396 } | 396 } |
| 397 | 397 |
| 398 for (std::map<std::pair<int, int>, gfx::ImageSkia>::iterator it = | 398 for (std::map<std::pair<int, int>, gfx::ImageSkia>::iterator it = |
| 399 image_skia_map.begin(); | 399 image_skia_map.begin(); |
| 400 it != image_skia_map.end(); | 400 it != image_skia_map.end(); |
| 401 ++it) { | 401 ++it) { |
| 402 it->second.MakeThreadSafe(); | 402 it->second.MakeThreadSafe(); |
| 403 image_family.Add(it->second); | 403 image_family.Add(it->second); |
| 404 } | 404 } |
| 405 | 405 |
| 406 callback.Run(image_family); | 406 callback.Run(image_family); |
| 407 } | 407 } |
| 408 | 408 |
| 409 } // namespace extensions | 409 } // namespace extensions |
| OLD | NEW |