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