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