| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "ui/gfx/image/image_family.h" | 5 #include "ui/gfx/image/image_family.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "skia/ext/image_operations.h" | 9 #include "skia/ext/image_operations.h" |
| 10 #include "ui/gfx/geometry/size.h" | 10 #include "ui/gfx/geometry/size.h" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 | 110 |
| 111 gfx::Image ImageFamily::CreateExact(int width, int height) const { | 111 gfx::Image ImageFamily::CreateExact(int width, int height) const { |
| 112 // Resize crashes if width or height is 0, so just return an empty image. | 112 // Resize crashes if width or height is 0, so just return an empty image. |
| 113 if (width == 0 || height == 0) | 113 if (width == 0 || height == 0) |
| 114 return gfx::Image(); | 114 return gfx::Image(); |
| 115 | 115 |
| 116 const gfx::Image* image = GetBest(width, height); | 116 const gfx::Image* image = GetBest(width, height); |
| 117 if (!image) | 117 if (!image) |
| 118 return gfx::Image(); | 118 return gfx::Image(); |
| 119 | 119 |
| 120 if (image->Width() == width && image->Height() == height) | 120 if (image->Width() == width && image->Height() == height) { |
| 121 return gfx::Image(*image); | 121 // Make a copy at gfx::ImageSkia level, so that resulting image's ref count |
| 122 // is not racy to |image|. Since this function can run on a different thread |
| 123 // than the thread |image| created on, we should not touch the |
| 124 // non-thread-safe ref count in gfx::Image here. |
| 125 std::unique_ptr<gfx::ImageSkia> image_skia(image->CopyImageSkia()); |
| 126 return gfx::Image(*image_skia); |
| 127 } |
| 122 | 128 |
| 123 SkBitmap bitmap = image->AsBitmap(); | 129 SkBitmap bitmap = image->AsBitmap(); |
| 124 SkBitmap resized_bitmap = skia::ImageOperations::Resize( | 130 SkBitmap resized_bitmap = skia::ImageOperations::Resize( |
| 125 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, width, height); | 131 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, width, height); |
| 126 return gfx::Image::CreateFrom1xBitmap(resized_bitmap); | 132 return gfx::Image::CreateFrom1xBitmap(resized_bitmap); |
| 127 } | 133 } |
| 128 | 134 |
| 129 gfx::Image ImageFamily::CreateExact(const gfx::Size& size) const { | 135 gfx::Image ImageFamily::CreateExact(const gfx::Size& size) const { |
| 130 return CreateExact(size.width(), size.height()); | 136 return CreateExact(size.width(), size.height()); |
| 131 } | 137 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 144 DCHECK(greater_or_equal != map_.begin()); | 150 DCHECK(greater_or_equal != map_.begin()); |
| 145 std::map<MapKey, gfx::Image>::const_iterator less_than = greater_or_equal; | 151 std::map<MapKey, gfx::Image>::const_iterator less_than = greater_or_equal; |
| 146 --less_than; | 152 --less_than; |
| 147 // This must be true because there must be at least one image with |aspect|. | 153 // This must be true because there must be at least one image with |aspect|. |
| 148 DCHECK_EQ(less_than->first.aspect(), aspect); | 154 DCHECK_EQ(less_than->first.aspect(), aspect); |
| 149 // We have found the largest image smaller than desired. | 155 // We have found the largest image smaller than desired. |
| 150 return &less_than->second; | 156 return &less_than->second; |
| 151 } | 157 } |
| 152 | 158 |
| 153 } // namespace gfx | 159 } // namespace gfx |
| OLD | NEW |