OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 package org.chromium.content.browser.shapedetection; | 5 package org.chromium.content.browser.shapedetection; |
6 | 6 |
7 import android.graphics.Bitmap; | 7 import android.graphics.Bitmap; |
8 import android.graphics.PointF; | 8 import android.graphics.PointF; |
9 import android.media.FaceDetector; | 9 import android.media.FaceDetector; |
10 import android.media.FaceDetector.Face; | 10 import android.media.FaceDetector.Face; |
11 | 11 |
12 import org.chromium.base.Log; | 12 import org.chromium.base.Log; |
13 import org.chromium.gfx.mojom.RectF; | 13 import org.chromium.gfx.mojom.RectF; |
14 import org.chromium.mojo.system.MojoException; | 14 import org.chromium.mojo.system.MojoException; |
| 15 import org.chromium.mojo.system.SharedBufferHandle; |
| 16 import org.chromium.mojo.system.SharedBufferHandle.MapFlags; |
15 import org.chromium.shape_detection.mojom.FaceDetection; | 17 import org.chromium.shape_detection.mojom.FaceDetection; |
16 import org.chromium.shape_detection.mojom.FaceDetectionResult; | 18 import org.chromium.shape_detection.mojom.FaceDetectionResult; |
17 import org.chromium.shape_detection.mojom.FaceDetectorOptions; | 19 import org.chromium.shape_detection.mojom.FaceDetectorOptions; |
18 import org.chromium.skia.mojom.ColorType; | |
19 | 20 |
20 import java.nio.ByteBuffer; | 21 import java.nio.ByteBuffer; |
21 | 22 |
22 /** | 23 /** |
23 * Android implementation of the FaceDetection service defined in | 24 * Android implementation of the FaceDetection service defined in |
24 * services/shape_detection/public/interfaces/facedetection.mojom | 25 * services/shape_detection/public/interfaces/facedetection.mojom |
25 */ | 26 */ |
26 public class FaceDetectionImpl implements FaceDetection { | 27 public class FaceDetectionImpl implements FaceDetection { |
27 private static final String TAG = "FaceDetectionImpl"; | 28 private static final String TAG = "FaceDetectionImpl"; |
28 private static final int MAX_FACES = 32; | 29 private static final int MAX_FACES = 32; |
29 private final boolean mFastMode; | 30 private final boolean mFastMode; |
30 private final int mMaxFaces; | 31 private final int mMaxFaces; |
31 | 32 |
32 FaceDetectionImpl(FaceDetectorOptions options) { | 33 FaceDetectionImpl(FaceDetectorOptions options) { |
33 mFastMode = options.fastMode; | 34 mFastMode = options.fastMode; |
34 mMaxFaces = Math.min(options.maxDetectedFaces, MAX_FACES); | 35 mMaxFaces = Math.min(options.maxDetectedFaces, MAX_FACES); |
35 } | 36 } |
36 | 37 |
37 @Override | 38 @Override |
38 public void detect(org.chromium.skia.mojom.Bitmap bitmapData, DetectResponse
callback) { | 39 public void detect( |
39 int width = bitmapData.width; | 40 SharedBufferHandle frameData, int width, int height, DetectResponse
callback) { |
40 int height = bitmapData.height; | |
41 final long numPixels = (long) width * height; | 41 final long numPixels = (long) width * height; |
42 // TODO(xianglu): https://crbug.com/670028 homogeneize overflow checking
. | 42 // TODO(xianglu): https://crbug.com/670028 homogeneize overflow checking
. |
43 if (bitmapData.pixelData == null || width <= 0 || height <= 0 | 43 if (!frameData.isValid() || width <= 0 || height <= 0 || numPixels > (Lo
ng.MAX_VALUE / 4)) { |
44 || numPixels > (Long.MAX_VALUE / 4)) { | |
45 Log.d(TAG, "Invalid argument(s)."); | 44 Log.d(TAG, "Invalid argument(s)."); |
46 callback.call(new FaceDetectionResult()); | 45 callback.call(new FaceDetectionResult()); |
47 return; | 46 return; |
48 } | 47 } |
49 | 48 |
50 // TODO(junwei.fu): Consider supporting other bitmap pixel formats, | 49 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none()
); |
51 // https://crbug.com/684921. | 50 if (imageBuffer.capacity() <= 0) { |
52 if (bitmapData.colorType != ColorType.RGBA_8888 | 51 Log.d(TAG, "Failed to map from SharedBufferHandle."); |
53 && bitmapData.colorType != ColorType.BGRA_8888) { | |
54 Log.e(TAG, "Unsupported bitmap pixel format"); | |
55 callback.call(new FaceDetectionResult()); | 52 callback.call(new FaceDetectionResult()); |
56 return; | 53 return; |
57 } | 54 } |
58 | 55 |
59 ByteBuffer imageBuffer = ByteBuffer.wrap(bitmapData.pixelData); | |
60 if (imageBuffer.capacity() <= 0) { | |
61 Log.d(TAG, "Failed to wrap from Bitmap."); | |
62 callback.call(new FaceDetectionResult()); | |
63 return; | |
64 } | |
65 | |
66 // TODO(junwei.fu): Use |bitmapData| directly for |unPremultipliedBitmap
| to spare a copy | |
67 // if the bitmap pixel format is RGB_565, the ARGB_8888 Bitmap doesn't n
eed to be created | |
68 // in this case, https://crbug.com/684930. | |
69 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88
88); | 56 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88
88); |
70 | 57 |
71 // An int array is needed to construct a Bitmap. However the Bytebuffer | 58 // An int array is needed to construct a Bitmap. However the Bytebuffer |
72 // we get from |bitmapData| is directly allocated and does not have a su
pporting array. | 59 // we get from |sharedBufferHandle| is directly allocated and does not |
73 // Therefore we need to copy from |imageBuffer| to create this intermedi
ate Bitmap. | 60 // have a supporting array. Therefore we need to copy from |imageBuffer| |
| 61 // to create this intermediate Bitmap. |
74 // TODO(xianglu): Consider worker pool as appropriate threads. | 62 // TODO(xianglu): Consider worker pool as appropriate threads. |
75 // http://crbug.com/655814 | 63 // http://crbug.com/655814 |
76 bitmap.copyPixelsFromBuffer(imageBuffer); | 64 bitmap.copyPixelsFromBuffer(imageBuffer); |
77 | 65 |
78 // A Bitmap must be in 565 format for findFaces() to work. See | 66 // A Bitmap must be in 565 format for findFaces() to work. See |
79 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/media/java/andro
id/media/FaceDetector.java#124 | 67 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/media/java/andro
id/media/FaceDetector.java#124 |
80 // | 68 // |
81 // It turns out that FaceDetector is not able to detect correctly if | 69 // It turns out that FaceDetector is not able to detect correctly if |
82 // simply using pixmap.setConfig(). The reason might be that findFaces() | 70 // simply using pixmap.setConfig(). The reason might be that findFaces() |
83 // needs non-premultiplied ARGB arrangement, while the alpha type in the | 71 // needs non-premultiplied ARGB arrangement, while the alpha type in the |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 } | 104 } |
117 | 105 |
118 @Override | 106 @Override |
119 public void close() {} | 107 public void close() {} |
120 | 108 |
121 @Override | 109 @Override |
122 public void onConnectionError(MojoException e) { | 110 public void onConnectionError(MojoException e) { |
123 close(); | 111 close(); |
124 } | 112 } |
125 } | 113 } |
OLD | NEW |