• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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__, /*overrideToPortrait*/false,
186                 &metadata);
187         if (!rc.isOk()) {
188             // The test is relevant only for cameras with Hal 3.x
189             // support.
190             continue;
191         }
192         EXPECT_FALSE(metadata.isEmpty());
193         camera_metadata_entry_t availableCapabilities =
194                 metadata.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
195         EXPECT_TRUE(0 < availableCapabilities.count);
196         bool isReprocessSupported = false;
197         const uint8_t *caps = availableCapabilities.data.u8;
198         for (size_t i = 0; i < availableCapabilities.count; i++) {
199             if (ANDROID_REQUEST_AVAILABLE_CAPABILITIES_PRIVATE_REPROCESSING ==
200                 caps[i]) {
201                 isReprocessSupported = true;
202                 break;
203             }
204         }
205         if (!isReprocessSupported) {
206             // ZSL relies on this feature
207             continue;
208         }
209 
210         rc = mCameraService->connect(this, cameraId,
211                 String16("ZSLTest"), hardware::ICameraService::USE_CALLING_UID,
212                 hardware::ICameraService::USE_CALLING_PID,
213                 /*targetSdkVersion*/__ANDROID_API_FUTURE__,
214                 /*overrideToPortrait*/false, /*forceSlowJpegMode*/false, &cameraDevice);
215         EXPECT_TRUE(rc.isOk());
216 
217         CameraParameters params(cameraDevice->getParameters());
218 
219         String8 focusModes(params.get(
220                 CameraParameters::KEY_SUPPORTED_FOCUS_MODES));
221         bool isAFSupported = false;
222         const char *focusMode = nullptr;
223         if (focusModes.contains(CameraParameters::FOCUS_MODE_AUTO)) {
224             // If supported 'auto' should be set by default
225             isAFSupported = true;
226         } else if (focusModes.contains(
227                 CameraParameters::FOCUS_MODE_CONTINUOUS_PICTURE)) {
228             isAFSupported = true;
229             focusMode = CameraParameters::FOCUS_MODE_CONTINUOUS_PICTURE;
230         } else if (focusModes.contains(
231                 CameraParameters::FOCUS_MODE_CONTINUOUS_VIDEO)) {
232             isAFSupported = true;
233             focusMode = CameraParameters::FOCUS_MODE_CONTINUOUS_VIDEO;
234         } else if (focusModes.contains(CameraParameters::FOCUS_MODE_MACRO)) {
235             isAFSupported = true;
236             focusMode = CameraParameters::FOCUS_MODE_MACRO;
237         }
238 
239         if (!isAFSupported) {
240             // AF state is needed
241             continue;
242         }
243 
244         if (nullptr != focusMode) {
245             params.set(CameraParameters::KEY_FOCUS_MODE, focusMode);
246             ASSERT_EQ(NO_ERROR, cameraDevice->setParameters(params.flatten()));
247         }
248 
249         int previewWidth, previewHeight;
250         params.getPreviewSize(&previewWidth, &previewHeight);
251         ASSERT_TRUE((0 < previewWidth) && (0 < previewHeight));
252 
253         surfaceControl = mComposerClient->createSurface(
254                 String8("Test Surface"),
255                 previewWidth, previewHeight,
256                 CameraParameters::previewFormatToEnum(
257                         params.getPreviewFormat()),
258                 GRALLOC_USAGE_HW_RENDER);
259 
260         ASSERT_TRUE(nullptr != surfaceControl.get());
261         ASSERT_TRUE(surfaceControl->isValid());
262 
263         SurfaceComposerClient::Transaction{}
264                 .setLayer(surfaceControl, 0x7fffffff)
265                 .show(surfaceControl)
266                 .apply();
267 
268         previewSurface = surfaceControl->getSurface();
269         ASSERT_TRUE(previewSurface != NULL);
270         ASSERT_EQ(NO_ERROR, cameraDevice->setPreviewTarget(
271                 previewSurface->getIGraphicBufferProducer()));
272 
273         cameraDevice->setPreviewCallbackFlag(
274                 CAMERA_FRAME_CALLBACK_FLAG_CAMCORDER);
275 
276         Vector<Size> pictureSizes;
277         params.getSupportedPictureSizes(pictureSizes);
278         for (size_t i = 0; i < pictureSizes.size(); i++) {
279             params.setPictureSize(pictureSizes[i].width,
280                     pictureSizes[i].height);
281             ASSERT_EQ(NO_ERROR, cameraDevice->setParameters(params.flatten()));
282             ASSERT_EQ(NO_ERROR, cameraDevice->startPreview());
283             ASSERT_EQ(NO_ERROR, waitForPreviewStart());
284 
285             ASSERT_EQ(NO_ERROR, cameraDevice->autoFocus());
286             ASSERT_EQ(NO_ERROR, waitForEvent(mAutoFocusLock,
287                     mAutoFocusCondition, mAutoFocusMessage));
288 
289             ASSERT_EQ(NO_ERROR,
290                     cameraDevice->takePicture(CAMERA_MSG_COMPRESSED_IMAGE));
291             ASSERT_EQ(NO_ERROR, waitForEvent(mSnapshotLock, mSnapshotCondition,
292                     mSnapshotNotification));
293         }
294 
295         cameraDevice->stopPreview();
296         rc = cameraDevice->disconnect();
297         EXPECT_TRUE(rc.isOk());
298     }
299 }
300