| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "services/shape_detection/face_detection_impl_mac.h" | 5 #include "services/shape_detection/face_detection_impl_mac.h" |
| 6 | 6 |
| 7 #include "base/mac/scoped_cftyperef.h" | 7 #include "base/mac/scoped_cftyperef.h" |
| 8 #include "base/mac/scoped_nsobject.h" | |
| 9 #include "base/memory/shared_memory.h" | |
| 10 #include "media/capture/video/scoped_result_callback.h" | 8 #include "media/capture/video/scoped_result_callback.h" |
| 11 #include "mojo/public/cpp/bindings/strong_binding.h" | 9 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 12 #include "mojo/public/cpp/system/platform_handle.h" | 10 #include "services/shape_detection/detection_utils_mac.h" |
| 13 #include "services/shape_detection/face_detection_provider_impl.h" | 11 #include "services/shape_detection/face_detection_provider_impl.h" |
| 14 | 12 |
| 15 namespace shape_detection { | 13 namespace shape_detection { |
| 16 | 14 |
| 17 namespace { | 15 namespace { |
| 18 | 16 |
| 19 // kCIFormatRGBA8 is not exposed to public until Mac 10.11. So we define the | |
| 20 // same constant to support RGBA8 format in earlier versions. | |
| 21 #if !defined(MAC_OS_X_VERSION_10_11) || \ | |
| 22 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_11 | |
| 23 const int kCIFormatRGBA8 = 24; | |
| 24 #endif | |
| 25 | |
| 26 void RunCallbackWithFaces( | 17 void RunCallbackWithFaces( |
| 27 const shape_detection::mojom::FaceDetection::DetectCallback& callback, | 18 const shape_detection::mojom::FaceDetection::DetectCallback& callback, |
| 28 shape_detection::mojom::FaceDetectionResultPtr faces) { | 19 shape_detection::mojom::FaceDetectionResultPtr faces) { |
| 29 callback.Run(std::move(faces)); | 20 callback.Run(std::move(faces)); |
| 30 } | 21 } |
| 31 | 22 |
| 32 void RunCallbackWithNoFaces( | 23 void RunCallbackWithNoFaces( |
| 33 const shape_detection::mojom::FaceDetection::DetectCallback& callback) { | 24 const shape_detection::mojom::FaceDetection::DetectCallback& callback) { |
| 34 callback.Run(shape_detection::mojom::FaceDetectionResult::New()); | 25 callback.Run(shape_detection::mojom::FaceDetectionResult::New()); |
| 35 } | 26 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 56 FaceDetectionImplMac::~FaceDetectionImplMac() {} | 47 FaceDetectionImplMac::~FaceDetectionImplMac() {} |
| 57 | 48 |
| 58 void FaceDetectionImplMac::Detect(mojo::ScopedSharedBufferHandle frame_data, | 49 void FaceDetectionImplMac::Detect(mojo::ScopedSharedBufferHandle frame_data, |
| 59 uint32_t width, | 50 uint32_t width, |
| 60 uint32_t height, | 51 uint32_t height, |
| 61 const DetectCallback& callback) { | 52 const DetectCallback& callback) { |
| 62 media::ScopedResultCallback<DetectCallback> scoped_callback( | 53 media::ScopedResultCallback<DetectCallback> scoped_callback( |
| 63 base::Bind(&RunCallbackWithFaces, callback), | 54 base::Bind(&RunCallbackWithFaces, callback), |
| 64 base::Bind(&RunCallbackWithNoFaces)); | 55 base::Bind(&RunCallbackWithNoFaces)); |
| 65 | 56 |
| 66 base::CheckedNumeric<uint32_t> num_pixels = | 57 base::scoped_nsobject<CIImage> ci_image = |
| 67 base::CheckedNumeric<uint32_t>(width) * height; | 58 CreateCIImageFromSharedMemory(std::move(frame_data), width, height); |
| 68 base::CheckedNumeric<uint32_t> num_bytes = num_pixels * 4; | 59 if (!ci_image) |
| 69 if (!num_bytes.IsValid()) { | |
| 70 DLOG(ERROR) << "Data overflow"; | |
| 71 return; | 60 return; |
| 72 } | |
| 73 | |
| 74 base::SharedMemoryHandle memory_handle; | |
| 75 size_t memory_size = 0; | |
| 76 bool read_only_flag = false; | |
| 77 const MojoResult result = mojo::UnwrapSharedMemoryHandle( | |
| 78 std::move(frame_data), &memory_handle, &memory_size, &read_only_flag); | |
| 79 DCHECK_EQ(MOJO_RESULT_OK, result) << "Failed to unwrap SharedBufferHandle"; | |
| 80 if (!memory_size || memory_size != num_bytes.ValueOrDie()) { | |
| 81 DLOG(ERROR) << "Invalid image size"; | |
| 82 return; | |
| 83 } | |
| 84 | |
| 85 auto shared_memory = | |
| 86 base::MakeUnique<base::SharedMemory>(memory_handle, true /* read_only */); | |
| 87 if (!shared_memory->Map(memory_size)) { | |
| 88 DLOG(ERROR) << "Failed to map bytes from shared memory"; | |
| 89 return; | |
| 90 } | |
| 91 | |
| 92 NSData* byte_data = [NSData dataWithBytesNoCopy:shared_memory->memory() | |
| 93 length:num_bytes.ValueOrDie() | |
| 94 freeWhenDone:NO]; | |
| 95 | |
| 96 base::ScopedCFTypeRef<CGColorSpaceRef> colorspace( | |
| 97 CGColorSpaceCreateWithName(kCGColorSpaceSRGB)); | |
| 98 | |
| 99 // CIImage will return nil when RGBA8 is not supported in a certain version. | |
| 100 base::scoped_nsobject<CIImage> ci_image([[CIImage alloc] | |
| 101 initWithBitmapData:byte_data | |
| 102 bytesPerRow:width * 4 | |
| 103 size:CGSizeMake(width, height) | |
| 104 format:kCIFormatRGBA8 | |
| 105 colorSpace:colorspace]); | |
| 106 if (!ci_image) { | |
| 107 DLOG(ERROR) << "Failed to create CIImage"; | |
| 108 return; | |
| 109 } | |
| 110 | 61 |
| 111 NSArray* const features = [detector_ featuresInImage:ci_image]; | 62 NSArray* const features = [detector_ featuresInImage:ci_image]; |
| 112 | 63 |
| 113 shape_detection::mojom::FaceDetectionResultPtr faces = | 64 shape_detection::mojom::FaceDetectionResultPtr faces = |
| 114 shape_detection::mojom::FaceDetectionResult::New(); | 65 shape_detection::mojom::FaceDetectionResult::New(); |
| 115 for (CIFaceFeature* const f in features) { | 66 for (CIFaceFeature* const f in features) { |
| 116 // In the default Core Graphics coordinate space, the origin is located | 67 // In the default Core Graphics coordinate space, the origin is located |
| 117 // in the lower-left corner, and thus |ci_image| is flipped vertically. | 68 // in the lower-left corner, and thus |ci_image| is flipped vertically. |
| 118 // We need to adjust |y| coordinate of bounding box before sending it. | 69 // We need to adjust |y| coordinate of bounding box before sending it. |
| 119 gfx::RectF boundingbox(f.bounds.origin.x, | 70 gfx::RectF boundingbox(f.bounds.origin.x, |
| 120 height - f.bounds.origin.y - f.bounds.size.height, | 71 height - f.bounds.origin.y - f.bounds.size.height, |
| 121 f.bounds.size.width, f.bounds.size.height); | 72 f.bounds.size.width, f.bounds.size.height); |
| 122 faces->bounding_boxes.push_back(boundingbox); | 73 faces->bounding_boxes.push_back(boundingbox); |
| 123 } | 74 } |
| 124 scoped_callback.Run(std::move(faces)); | 75 scoped_callback.Run(std::move(faces)); |
| 125 } | 76 } |
| 126 | 77 |
| 127 } // namespace shape_detection | 78 } // namespace shape_detection |
| OLD | NEW |