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