| 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 "components/user_manager/user_image/user_image.h" | 5 #include "components/user_manager/user_image/user_image.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" |
| 7 #include "base/trace_event/trace_event.h" | 8 #include "base/trace_event/trace_event.h" |
| 8 #include "third_party/skia/include/core/SkBitmap.h" | 9 #include "third_party/skia/include/core/SkBitmap.h" |
| 9 #include "ui/gfx/codec/jpeg_codec.h" | 10 #include "ui/gfx/codec/jpeg_codec.h" |
| 10 | 11 |
| 11 namespace user_manager { | 12 namespace user_manager { |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 // Default quality for encoding user images. | 16 // Default quality for encoding user images. |
| 16 const int kDefaultEncodingQuality = 90; | 17 const int kDefaultEncodingQuality = 90; |
| 17 | 18 |
| 18 } // namespace | 19 } // namespace |
| 19 | 20 |
| 20 // static | 21 // static |
| 21 scoped_ptr<UserImage::Bytes> UserImage::Encode(const SkBitmap& bitmap) { | 22 std::unique_ptr<UserImage::Bytes> UserImage::Encode(const SkBitmap& bitmap) { |
| 22 TRACE_EVENT2("oobe", "UserImage::Encode", | 23 TRACE_EVENT2("oobe", "UserImage::Encode", |
| 23 "width", bitmap.width(), "height", bitmap.height()); | 24 "width", bitmap.width(), "height", bitmap.height()); |
| 24 SkAutoLockPixels lock_bitmap(bitmap); | 25 SkAutoLockPixels lock_bitmap(bitmap); |
| 25 scoped_ptr<Bytes> output(new Bytes); | 26 std::unique_ptr<Bytes> output(new Bytes); |
| 26 if (gfx::JPEGCodec::Encode( | 27 if (gfx::JPEGCodec::Encode( |
| 27 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | 28 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
| 28 gfx::JPEGCodec::FORMAT_SkBitmap, | 29 gfx::JPEGCodec::FORMAT_SkBitmap, |
| 29 bitmap.width(), | 30 bitmap.width(), |
| 30 bitmap.height(), | 31 bitmap.height(), |
| 31 bitmap.width() * bitmap.bytesPerPixel(), | 32 bitmap.width() * bitmap.bytesPerPixel(), |
| 32 kDefaultEncodingQuality, output.get())) { | 33 kDefaultEncodingQuality, output.get())) { |
| 33 return output; | 34 return output; |
| 34 } else { | 35 } else { |
| 35 return nullptr; | 36 return nullptr; |
| 36 } | 37 } |
| 37 } | 38 } |
| 38 | 39 |
| 39 // static | 40 // static |
| 40 scoped_ptr<UserImage> UserImage::CreateAndEncode(const gfx::ImageSkia& image) { | 41 std::unique_ptr<UserImage> UserImage::CreateAndEncode( |
| 42 const gfx::ImageSkia& image) { |
| 41 if (image.isNull()) | 43 if (image.isNull()) |
| 42 return make_scoped_ptr(new UserImage); | 44 return base::WrapUnique(new UserImage); |
| 43 | 45 |
| 44 scoped_ptr<Bytes> image_bytes = Encode(*image.bitmap()); | 46 std::unique_ptr<Bytes> image_bytes = Encode(*image.bitmap()); |
| 45 if (image_bytes) { | 47 if (image_bytes) { |
| 46 // TODO(crbug.com/593251): Remove the data copy via |image_bytes|. | 48 // TODO(crbug.com/593251): Remove the data copy via |image_bytes|. |
| 47 scoped_ptr<UserImage> result(new UserImage(image, *image_bytes)); | 49 std::unique_ptr<UserImage> result(new UserImage(image, *image_bytes)); |
| 48 result->MarkAsSafe(); | 50 result->MarkAsSafe(); |
| 49 return result; | 51 return result; |
| 50 } | 52 } |
| 51 return make_scoped_ptr(new UserImage(image)); | 53 return base::WrapUnique(new UserImage(image)); |
| 52 } | 54 } |
| 53 | 55 |
| 54 UserImage::UserImage() | 56 UserImage::UserImage() |
| 55 : has_image_bytes_(false), | 57 : has_image_bytes_(false), |
| 56 is_safe_format_(false) { | 58 is_safe_format_(false) { |
| 57 } | 59 } |
| 58 | 60 |
| 59 UserImage::UserImage(const gfx::ImageSkia& image) | 61 UserImage::UserImage(const gfx::ImageSkia& image) |
| 60 : image_(image), | 62 : image_(image), |
| 61 has_image_bytes_(false), | 63 has_image_bytes_(false), |
| 62 is_safe_format_(false) { | 64 is_safe_format_(false) { |
| 63 } | 65 } |
| 64 | 66 |
| 65 UserImage::UserImage(const gfx::ImageSkia& image, | 67 UserImage::UserImage(const gfx::ImageSkia& image, |
| 66 const Bytes& image_bytes) | 68 const Bytes& image_bytes) |
| 67 : image_(image), | 69 : image_(image), |
| 68 has_image_bytes_(false), | 70 has_image_bytes_(false), |
| 69 is_safe_format_(false) { | 71 is_safe_format_(false) { |
| 70 has_image_bytes_ = true; | 72 has_image_bytes_ = true; |
| 71 image_bytes_ = image_bytes; | 73 image_bytes_ = image_bytes; |
| 72 } | 74 } |
| 73 | 75 |
| 74 UserImage::~UserImage() {} | 76 UserImage::~UserImage() {} |
| 75 | 77 |
| 76 void UserImage::MarkAsSafe() { | 78 void UserImage::MarkAsSafe() { |
| 77 is_safe_format_ = true; | 79 is_safe_format_ = true; |
| 78 } | 80 } |
| 79 | 81 |
| 80 } // namespace user_manager | 82 } // namespace user_manager |
| OLD | NEW |