1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.hardware.cts;
18 
19 import android.app.Instrumentation;
20 import android.graphics.SurfaceTexture;
21 import android.hardware.Camera;
22 import android.hardware.Camera.Parameters;
23 import android.os.SystemClock;
24 import android.util.Log;
25 
26 import androidx.test.InstrumentationRegistry;
27 
28 import com.android.compatibility.common.util.DeviceReportLog;
29 import com.android.compatibility.common.util.ResultType;
30 import com.android.compatibility.common.util.ResultUnit;
31 import com.android.compatibility.common.util.Stat;
32 
33 import java.util.Arrays;
34 
35 /**
36  * Measure and report legacy camera device performance.
37  */
38 public class LegacyCameraPerformanceTest extends CameraTestCase {
39     private static final String TAG = "CameraPerformanceTest";
40     private static final String REPORT_LOG_NAME = "CtsCamera1TestCases";
41     private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);
42 
43     private Instrumentation mInstrumentation;
44 
45     @Override
setUp()46     protected void setUp() throws Exception {
47         super.setUp();
48         mInstrumentation = InstrumentationRegistry.getInstrumentation();
49     }
50 
51     @Override
tearDown()52     protected void tearDown() throws Exception {
53         super.tearDown();
54         if (mCamera != null) {
55             mCamera.release();
56             mCamera = null;
57         }
58     }
59 
testLegacyApiPerformance()60     public void testLegacyApiPerformance() throws Exception {
61         final int NUM_TEST_LOOPS = 10;
62 
63         int nCameras = Camera.getNumberOfCameras();
64         double[] avgCameraTakePictureTimes = new double[nCameras];
65 
66         for (int id = 0; id < nCameras; id++) {
67             DeviceReportLog reportLog = new DeviceReportLog(REPORT_LOG_NAME,
68                     "test_camera_takePicture");
69             reportLog.addValue("camera_id", id, ResultType.NEUTRAL, ResultUnit.NONE);
70             double[] cameraOpenTimes = new double[NUM_TEST_LOOPS];
71             double[] startPreviewTimes = new double[NUM_TEST_LOOPS];
72             double[] stopPreviewTimes = new double[NUM_TEST_LOOPS];
73             double[] cameraCloseTimes = new double[NUM_TEST_LOOPS];
74             double[] cameraTakePictureTimes = new double[NUM_TEST_LOOPS];
75             double[] cameraAutoFocusTimes = new double[NUM_TEST_LOOPS];
76             boolean afSupported = false;
77             long openTimeMs, startPreviewTimeMs, stopPreviewTimeMs, closeTimeMs, takePictureTimeMs,
78                  autofocusTimeMs;
79 
80             for (int i = 0; i < NUM_TEST_LOOPS; i++) {
81                 openTimeMs = SystemClock.elapsedRealtime();
82                 initializeMessageLooper(id);
83                 cameraOpenTimes[i] = SystemClock.elapsedRealtime() - openTimeMs;
84 
85                 Parameters parameters = mCamera.getParameters();
86                 if (i == 0) {
87                     for (String focusMode: parameters.getSupportedFocusModes()) {
88                         if (Parameters.FOCUS_MODE_AUTO.equals(focusMode)) {
89                             afSupported = true;
90                             break;
91                         }
92                     }
93                 }
94 
95                 if (afSupported) {
96                     parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
97                     mCamera.setParameters(parameters);
98                 }
99 
100                 SurfaceTexture previewTexture = new SurfaceTexture(/*random int*/ 1);
101                 mCamera.setPreviewTexture(previewTexture);
102                 startPreviewTimeMs = SystemClock.elapsedRealtime();
103                 startPreview();
104                 startPreviewTimes[i] = SystemClock.elapsedRealtime() - startPreviewTimeMs;
105 
106                 if (afSupported) {
107                     autofocusTimeMs = SystemClock.elapsedRealtime();
108                     autoFocus();
109                     cameraAutoFocusTimes[i] = SystemClock.elapsedRealtime() - autofocusTimeMs;
110                 }
111 
112                 //Let preview run for a while
113                 Thread.sleep(1000);
114 
115                 takePictureTimeMs = SystemClock.elapsedRealtime();
116                 takePicture();
117                 cameraTakePictureTimes[i] = SystemClock.elapsedRealtime() - takePictureTimeMs;
118 
119                 //Resume preview after image capture
120                 startPreview();
121 
122                 stopPreviewTimeMs = SystemClock.elapsedRealtime();
123                 mCamera.stopPreview();
124                 closeTimeMs = SystemClock.elapsedRealtime();
125                 stopPreviewTimes[i] = closeTimeMs - stopPreviewTimeMs;
126 
127                 terminateMessageLooper();
128                 cameraCloseTimes[i] = SystemClock.elapsedRealtime() - closeTimeMs;
129                 previewTexture.release();
130             }
131 
132             if (VERBOSE) {
133                 Log.v(TAG, "Camera " + id + " device open times(ms): "
134                         + Arrays.toString(cameraOpenTimes)
135                         + ". Average(ms): " + Stat.getAverage(cameraOpenTimes)
136                         + ". Min(ms): " + Stat.getMin(cameraOpenTimes)
137                         + ". Max(ms): " + Stat.getMax(cameraOpenTimes));
138                 Log.v(TAG, "Camera " + id + " start preview times(ms): "
139                         + Arrays.toString(startPreviewTimes)
140                         + ". Average(ms): " + Stat.getAverage(startPreviewTimes)
141                         + ". Min(ms): " + Stat.getMin(startPreviewTimes)
142                         + ". Max(ms): " + Stat.getMax(startPreviewTimes));
143                 if (afSupported) {
144                     Log.v(TAG, "Camera " + id + " autofocus times(ms): "
145                             + Arrays.toString(cameraAutoFocusTimes)
146                             + ". Average(ms): " + Stat.getAverage(cameraAutoFocusTimes)
147                             + ". Min(ms): " + Stat.getMin(cameraAutoFocusTimes)
148                             + ". Max(ms): " + Stat.getMax(cameraAutoFocusTimes));
149                 }
150                 Log.v(TAG, "Camera " + id + " stop preview times(ms): "
151                         + Arrays.toString(stopPreviewTimes)
152                         + ". Average(ms): " + Stat.getAverage(stopPreviewTimes)
153                         + ". Min(ms): " + Stat.getMin(stopPreviewTimes)
154                         + ". Max(ms): " + Stat.getMax(stopPreviewTimes));
155                 Log.v(TAG, "Camera " + id + " device close times(ms): "
156                         + Arrays.toString(cameraCloseTimes)
157                         + ". Average(ms): " + Stat.getAverage(cameraCloseTimes)
158                         + ". Min(ms): " + Stat.getMin(cameraCloseTimes)
159                         + ". Max(ms): " + Stat.getMax(cameraCloseTimes));
160                 Log.v(TAG, "Camera " + id + " camera takepicture times(ms): "
161                         + Arrays.toString(cameraTakePictureTimes)
162                         + ". Average(ms): " + Stat.getAverage(cameraTakePictureTimes)
163                         + ". Min(ms): " + Stat.getMin(cameraTakePictureTimes)
164                         + ". Max(ms): " + Stat.getMax(cameraTakePictureTimes));
165             }
166 
167             avgCameraTakePictureTimes[id] = Stat.getAverage(cameraTakePictureTimes);
168             reportLog.addValues("camera_open_time", cameraOpenTimes, ResultType.LOWER_BETTER,
169                     ResultUnit.MS);
170             reportLog.addValues("camera_start_preview_time", startPreviewTimes,
171                     ResultType.LOWER_BETTER, ResultUnit.MS);
172             if (afSupported) {
173                 reportLog.addValues("camera_autofocus_time", cameraAutoFocusTimes,
174                         ResultType.LOWER_BETTER, ResultUnit.MS);
175             }
176             reportLog.addValues("camera_stop_preview", stopPreviewTimes,
177                     ResultType.LOWER_BETTER, ResultUnit.MS);
178             reportLog.addValues("camera_close_time", cameraCloseTimes,
179                     ResultType.LOWER_BETTER, ResultUnit.MS);
180             reportLog.addValues("camera_takepicture_time", cameraTakePictureTimes,
181                     ResultType.LOWER_BETTER, ResultUnit.MS);
182 
183             reportLog.submit(mInstrumentation);
184         }
185 
186         if (nCameras != 0) {
187             DeviceReportLog reportLog = new DeviceReportLog(REPORT_LOG_NAME,
188                     "test_camera_takepicture_average");
189             reportLog.setSummary("camera_takepicture_average_time_for_all_cameras",
190                     Stat.getAverage(avgCameraTakePictureTimes), ResultType.LOWER_BETTER,
191                     ResultUnit.MS);
192             reportLog.submit(mInstrumentation);
193         }
194     }
195 }
196