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