blob: 3ae76595554241b9d5c03db02af2fb2879e63ae0 [file] [log] [blame]
Emilian Peevb12fb5d2017-01-26 13:04:06 -08001/*
2 * Copyright (C) 2017 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#define LOG_NDEBUG 0
18#define LOG_TAG "CameraZSLTests"
19
20#include <gtest/gtest.h>
21
22#include <binder/ProcessState.h>
23#include <utils/Errors.h>
24#include <utils/Log.h>
25#include <gui/Surface.h>
26#include <gui/SurfaceComposerClient.h>
27#include <camera/CameraParameters.h>
28#include <camera/CameraMetadata.h>
29#include <camera/Camera.h>
Austin Borger1c1bee02023-06-01 16:51:35 -070030#include <camera/StringUtils.h>
Emilian Peevb12fb5d2017-01-26 13:04:06 -080031#include <android/hardware/ICameraService.h>
32
33using namespace android;
34using namespace android::hardware;
35
36class CameraZSLTests : public ::testing::Test,
37 public ::android::hardware::BnCameraClient {
38protected:
39
40 CameraZSLTests() : numCameras(0), mPreviewBufferCount(0),
41 mAutoFocusMessage(false), mSnapshotNotification(false) {}
42
43 //Gtest interface
44 void SetUp() override;
45 void TearDown() override;
46
47 //CameraClient interface
48 void notifyCallback(int32_t msgType, int32_t, int32_t) override;
49 void dataCallback(int32_t msgType, const sp<IMemory>&,
50 camera_frame_metadata_t *) override;
51 void dataCallbackTimestamp(nsecs_t, int32_t,
52 const sp<IMemory>&) override {};
53 void recordingFrameHandleCallbackTimestamp(nsecs_t,
54 native_handle_t*) override {};
Yin-Chia Yehb5df5472017-03-20 19:32:19 -070055 void recordingFrameHandleCallbackTimestampBatch(
56 const std::vector<nsecs_t>&,
57 const std::vector<native_handle_t*>&) override {};
Emilian Peevb12fb5d2017-01-26 13:04:06 -080058
59 status_t waitForPreviewStart();
60 status_t waitForEvent(Mutex &mutex, Condition &condition, bool &flag);
61
62 mutable Mutex mPreviewLock;
63 mutable Condition mPreviewCondition;
64 mutable Mutex mAutoFocusLock;
65 mutable Condition mAutoFocusCondition;
66 mutable Mutex mSnapshotLock;
67 mutable Condition mSnapshotCondition;
68
69 int32_t numCameras;
70 size_t mPreviewBufferCount;
71 sp<ICameraService> mCameraService;
72 sp<SurfaceComposerClient> mComposerClient;
73 bool mAutoFocusMessage;
74 bool mSnapshotNotification;
75 static const int32_t kPreviewThreshold = 8;
76 static const nsecs_t kPreviewTimeout = 5000000000; // 5 [s.]
77 static const nsecs_t kEventTimeout = 10000000000; // 10 [s.]
78};
79
80void CameraZSLTests::SetUp() {
81 ::android::binder::Status rc;
82 ProcessState::self()->startThreadPool();
83 sp<IServiceManager> sm = defaultServiceManager();
84 sp<IBinder> binder = sm->getService(String16("media.camera"));
85 mCameraService = interface_cast<ICameraService>(binder);
86 rc = mCameraService->getNumberOfCameras(
87 hardware::ICameraService::CAMERA_TYPE_ALL, &numCameras);
88 EXPECT_TRUE(rc.isOk());
89
90 mComposerClient = new SurfaceComposerClient;
91 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
92}
93
94void CameraZSLTests::TearDown() {
95 mCameraService.clear();
96 mComposerClient->dispose();
97}
98
99void CameraZSLTests::notifyCallback(int32_t msgType, int32_t,
100 int32_t) {
101 if (CAMERA_MSG_FOCUS == msgType) {
102 Mutex::Autolock l(mAutoFocusLock);
103 mAutoFocusMessage = true;
104 mAutoFocusCondition.broadcast();
105 } else {
106 ALOGV("%s: msgType: %d", __FUNCTION__, msgType);
107 }
108};
109
110void CameraZSLTests::dataCallback(int32_t msgType, const sp<IMemory>& /*data*/,
111 camera_frame_metadata_t *) {
112
113 switch (msgType) {
114 case CAMERA_MSG_PREVIEW_FRAME: {
115 Mutex::Autolock l(mPreviewLock);
116 mPreviewBufferCount++;
117 mPreviewCondition.broadcast();
118 break;
119 }
120 case CAMERA_MSG_COMPRESSED_IMAGE: {
121 Mutex::Autolock l(mSnapshotLock);
122 mSnapshotNotification = true;
123 //TODO: Add checks on incoming Jpeg
124 mSnapshotCondition.broadcast();
125 break;
126 }
127 default:
128 ALOGV("%s: msgType: %d", __FUNCTION__, msgType);
129 }
130};
131
132status_t CameraZSLTests::waitForPreviewStart() {
133 status_t rc = NO_ERROR;
134 Mutex::Autolock l(mPreviewLock);
135 mPreviewBufferCount = 0;
136
137 while (mPreviewBufferCount < kPreviewThreshold) {
138 rc = mPreviewCondition.waitRelative(mPreviewLock,
139 kPreviewTimeout);
140 if (NO_ERROR != rc) {
141 break;
142 }
143 }
144
145 return rc;
146}
147
148status_t CameraZSLTests::waitForEvent(Mutex &mutex,
149 Condition &condition, bool &flag) {
150 status_t rc = NO_ERROR;
151 Mutex::Autolock l(mutex);
152 flag = false;
153
154 while (!flag) {
155 rc = condition.waitRelative(mutex,
156 kEventTimeout);
157 if (NO_ERROR != rc) {
158 break;
159 }
160 }
161
162 return rc;
163}
164
165TEST_F(CameraZSLTests, TestAllPictureSizes) {
166 ::android::binder::Status rc;
167
168 for (int32_t cameraId = 0; cameraId < numCameras; cameraId++) {
169 sp<Surface> previewSurface;
170 sp<SurfaceControl> surfaceControl;
171 sp<ICamera> cameraDevice;
172
Austin Borger1c1bee02023-06-01 16:51:35 -0700173 std::string cameraIdStr = std::to_string(cameraId);
Emilian Peevb12fb5d2017-01-26 13:04:06 -0800174 bool isSupported = false;
175 rc = mCameraService->supportsCameraApi(cameraIdStr,
176 hardware::ICameraService::API_VERSION_1, &isSupported);
177 EXPECT_TRUE(rc.isOk());
178
179 // We only care about camera Camera1 ZSL support.
180 if (!isSupported) {
181 continue;
182 }
183
184 CameraMetadata metadata;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700185 rc = mCameraService->getCameraCharacteristics(cameraIdStr,
Austin Borger18b30a72022-10-27 12:20:29 -0700186 /*targetSdkVersion*/__ANDROID_API_FUTURE__, /*overrideToPortrait*/false,
187 &metadata);
Emilian Peevb12fb5d2017-01-26 13:04:06 -0800188 if (!rc.isOk()) {
189 // The test is relevant only for cameras with Hal 3.x
190 // support.
191 continue;
192 }
193 EXPECT_FALSE(metadata.isEmpty());
194 camera_metadata_entry_t availableCapabilities =
195 metadata.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
196 EXPECT_TRUE(0 < availableCapabilities.count);
197 bool isReprocessSupported = false;
198 const uint8_t *caps = availableCapabilities.data.u8;
199 for (size_t i = 0; i < availableCapabilities.count; i++) {
200 if (ANDROID_REQUEST_AVAILABLE_CAPABILITIES_PRIVATE_REPROCESSING ==
201 caps[i]) {
202 isReprocessSupported = true;
203 break;
204 }
205 }
206 if (!isReprocessSupported) {
207 // ZSL relies on this feature
208 continue;
209 }
210
211 rc = mCameraService->connect(this, cameraId,
Austin Borger1c1bee02023-06-01 16:51:35 -0700212 "ZSLTest", hardware::ICameraService::USE_CALLING_UID,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700213 hardware::ICameraService::USE_CALLING_PID,
Austin Borger18b30a72022-10-27 12:20:29 -0700214 /*targetSdkVersion*/__ANDROID_API_FUTURE__,
Chengfei Taobe683db2023-01-31 18:52:49 +0000215 /*overrideToPortrait*/false, /*forceSlowJpegMode*/false, &cameraDevice);
Emilian Peevb12fb5d2017-01-26 13:04:06 -0800216 EXPECT_TRUE(rc.isOk());
217
218 CameraParameters params(cameraDevice->getParameters());
219
220 String8 focusModes(params.get(
221 CameraParameters::KEY_SUPPORTED_FOCUS_MODES));
222 bool isAFSupported = false;
223 const char *focusMode = nullptr;
224 if (focusModes.contains(CameraParameters::FOCUS_MODE_AUTO)) {
225 // If supported 'auto' should be set by default
226 isAFSupported = true;
227 } else if (focusModes.contains(
228 CameraParameters::FOCUS_MODE_CONTINUOUS_PICTURE)) {
229 isAFSupported = true;
230 focusMode = CameraParameters::FOCUS_MODE_CONTINUOUS_PICTURE;
231 } else if (focusModes.contains(
232 CameraParameters::FOCUS_MODE_CONTINUOUS_VIDEO)) {
233 isAFSupported = true;
234 focusMode = CameraParameters::FOCUS_MODE_CONTINUOUS_VIDEO;
235 } else if (focusModes.contains(CameraParameters::FOCUS_MODE_MACRO)) {
236 isAFSupported = true;
237 focusMode = CameraParameters::FOCUS_MODE_MACRO;
238 }
239
240 if (!isAFSupported) {
241 // AF state is needed
242 continue;
243 }
244
245 if (nullptr != focusMode) {
246 params.set(CameraParameters::KEY_FOCUS_MODE, focusMode);
247 ASSERT_EQ(NO_ERROR, cameraDevice->setParameters(params.flatten()));
248 }
249
250 int previewWidth, previewHeight;
251 params.getPreviewSize(&previewWidth, &previewHeight);
252 ASSERT_TRUE((0 < previewWidth) && (0 < previewHeight));
253
254 surfaceControl = mComposerClient->createSurface(
255 String8("Test Surface"),
256 previewWidth, previewHeight,
257 CameraParameters::previewFormatToEnum(
258 params.getPreviewFormat()),
259 GRALLOC_USAGE_HW_RENDER);
260
261 ASSERT_TRUE(nullptr != surfaceControl.get());
262 ASSERT_TRUE(surfaceControl->isValid());
263
Robert Carr116c7922017-08-31 15:58:36 -0700264 SurfaceComposerClient::Transaction{}
265 .setLayer(surfaceControl, 0x7fffffff)
266 .show(surfaceControl)
267 .apply();
Emilian Peevb12fb5d2017-01-26 13:04:06 -0800268
269 previewSurface = surfaceControl->getSurface();
270 ASSERT_TRUE(previewSurface != NULL);
271 ASSERT_EQ(NO_ERROR, cameraDevice->setPreviewTarget(
272 previewSurface->getIGraphicBufferProducer()));
273
274 cameraDevice->setPreviewCallbackFlag(
275 CAMERA_FRAME_CALLBACK_FLAG_CAMCORDER);
276
277 Vector<Size> pictureSizes;
278 params.getSupportedPictureSizes(pictureSizes);
279 for (size_t i = 0; i < pictureSizes.size(); i++) {
280 params.setPictureSize(pictureSizes[i].width,
281 pictureSizes[i].height);
282 ASSERT_EQ(NO_ERROR, cameraDevice->setParameters(params.flatten()));
283 ASSERT_EQ(NO_ERROR, cameraDevice->startPreview());
284 ASSERT_EQ(NO_ERROR, waitForPreviewStart());
285
286 ASSERT_EQ(NO_ERROR, cameraDevice->autoFocus());
287 ASSERT_EQ(NO_ERROR, waitForEvent(mAutoFocusLock,
288 mAutoFocusCondition, mAutoFocusMessage));
289
290 ASSERT_EQ(NO_ERROR,
291 cameraDevice->takePicture(CAMERA_MSG_COMPRESSED_IMAGE));
292 ASSERT_EQ(NO_ERROR, waitForEvent(mSnapshotLock, mSnapshotCondition,
293 mSnapshotNotification));
294 }
295
296 cameraDevice->stopPreview();
297 rc = cameraDevice->disconnect();
298 EXPECT_TRUE(rc.isOk());
299 }
300}