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