Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "CameraBinderTests" |
| 19 | |
| 20 | #include <binder/IInterface.h> |
| 21 | #include <binder/IServiceManager.h> |
| 22 | #include <binder/Parcel.h> |
| 23 | #include <binder/ProcessState.h> |
| 24 | #include <utils/Errors.h> |
| 25 | #include <utils/Log.h> |
| 26 | #include <utils/List.h> |
| 27 | #include <utils/String8.h> |
| 28 | #include <utils/String16.h> |
| 29 | #include <utils/Condition.h> |
| 30 | #include <utils/Mutex.h> |
| 31 | #include <system/graphics.h> |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 32 | #include <hardware/camera3.h> |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 33 | #include <hardware/gralloc.h> |
| 34 | |
| 35 | #include <camera/CameraMetadata.h> |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 36 | #include <android/content/AttributionSourceState.h> |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 37 | #include <android/hardware/ICameraService.h> |
| 38 | #include <android/hardware/ICameraServiceListener.h> |
| 39 | #include <android/hardware/BnCameraServiceListener.h> |
| 40 | #include <android/hardware/camera2/ICameraDeviceUser.h> |
| 41 | #include <android/hardware/camera2/ICameraDeviceCallbacks.h> |
| 42 | #include <android/hardware/camera2/BnCameraDeviceCallbacks.h> |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 43 | #include <camera/camera2/CaptureRequest.h> |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 44 | #include <camera/camera2/OutputConfiguration.h> |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 45 | #include <camera/camera2/SessionConfiguration.h> |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 46 | #include <camera/camera2/SubmitInfo.h> |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 47 | #include <camera/CameraUtils.h> |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 48 | #include <camera/StringUtils.h> |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 49 | |
Jim Shargo | 2e0202f | 2024-07-30 19:54:43 +0000 | [diff] [blame] | 50 | #include <com_android_graphics_libgui_flags.h> |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 51 | #include <gui/BufferItemConsumer.h> |
Carlos Martinez Romero | f99f788 | 2024-12-11 11:42:15 -0800 | [diff] [blame] | 52 | #if not COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 53 | #include <gui/IGraphicBufferProducer.h> |
Carlos Martinez Romero | f99f788 | 2024-12-11 11:42:15 -0800 | [diff] [blame] | 54 | #endif |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 55 | #include <gui/Surface.h> |
| 56 | |
| 57 | #include <gtest/gtest.h> |
| 58 | #include <unistd.h> |
| 59 | #include <stdint.h> |
| 60 | #include <utility> |
| 61 | #include <vector> |
| 62 | #include <map> |
| 63 | #include <algorithm> |
| 64 | |
| 65 | using namespace android; |
Jiyong Park | 5a095ea | 2019-07-09 15:43:57 +0900 | [diff] [blame] | 66 | using ::android::hardware::ICameraService; |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 67 | using ::android::hardware::camera2::ICameraDeviceUser; |
Jayant Chowdhary | dcae796 | 2024-08-20 21:20:10 +0000 | [diff] [blame] | 68 | using ::android::hardware::camera2::CameraMetadataInfo; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 69 | |
| 70 | #define ASSERT_NOT_NULL(x) \ |
| 71 | ASSERT_TRUE((x) != nullptr) |
| 72 | |
| 73 | #define SETUP_TIMEOUT 2000000000 // ns |
| 74 | #define IDLE_TIMEOUT 2000000000 // ns |
| 75 | |
| 76 | // Stub listener implementation |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 77 | class TestCameraServiceListener : public hardware::BnCameraServiceListener { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 78 | std::map<std::string, int32_t> mCameraTorchStatuses; |
| 79 | std::map<std::string, int32_t> mCameraStatuses; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 80 | mutable Mutex mLock; |
| 81 | mutable Condition mCondition; |
| 82 | mutable Condition mTorchCondition; |
| 83 | public: |
| 84 | virtual ~TestCameraServiceListener() {}; |
| 85 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 86 | virtual binder::Status onStatusChanged(int32_t status, const std::string& cameraId, |
| 87 | [[maybe_unused]] int32_t /*deviceId*/) override { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 88 | Mutex::Autolock l(mLock); |
| 89 | mCameraStatuses[cameraId] = status; |
| 90 | mCondition.broadcast(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 91 | return binder::Status::ok(); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 92 | } |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 93 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 94 | virtual binder::Status onPhysicalCameraStatusChanged([[maybe_unused]] int32_t /*status*/, |
| 95 | [[maybe_unused]] const std::string& /*cameraId*/, |
| 96 | [[maybe_unused]] const std::string& /*physicalCameraId*/, |
| 97 | [[maybe_unused]] int32_t /*deviceId*/) override { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 98 | // No op |
| 99 | return binder::Status::ok(); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 100 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 101 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 102 | virtual binder::Status onTorchStatusChanged(int32_t status, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 103 | const std::string& cameraId, [[maybe_unused]] int32_t /*deviceId*/) override { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 104 | Mutex::Autolock l(mLock); |
| 105 | mCameraTorchStatuses[cameraId] = status; |
| 106 | mTorchCondition.broadcast(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 107 | return binder::Status::ok(); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 108 | } |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 109 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 110 | virtual binder::Status onTorchStrengthLevelChanged( |
| 111 | [[maybe_unused]] const std::string& /*cameraId*/, |
| 112 | [[maybe_unused]] int32_t /*torchStrength*/, |
| 113 | [[maybe_unused]] int32_t /*deviceId*/) override { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 114 | // No op |
| 115 | return binder::Status::ok(); |
| 116 | } |
| 117 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 118 | virtual binder::Status onCameraAccessPrioritiesChanged() override { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 119 | // No op |
| 120 | return binder::Status::ok(); |
| 121 | } |
| 122 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 123 | virtual binder::Status onCameraOpened([[maybe_unused]] const std::string& /*cameraId*/, |
| 124 | [[maybe_unused]] const std::string& /*clientPackageName*/, |
| 125 | [[maybe_unused]] int32_t /*deviceId*/) { |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 126 | // No op |
| 127 | return binder::Status::ok(); |
| 128 | } |
| 129 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 130 | virtual binder::Status onCameraClosed([[maybe_unused]] const std::string& /*cameraId*/, |
| 131 | [[maybe_unused]] int32_t /*deviceId*/) override { |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 132 | // No op |
| 133 | return binder::Status::ok(); |
| 134 | } |
| 135 | |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame] | 136 | virtual binder::Status onCameraOpenedInSharedMode( |
| 137 | [[maybe_unused]] const std::string& /*cameraId*/, |
| 138 | [[maybe_unused]] const std::string& /*clientPackageName*/, |
| 139 | [[maybe_unused]] int32_t /*deviceId*/, |
| 140 | [[maybe_unused]] bool /*isPrimaryClient*/) override { |
| 141 | // No op |
| 142 | return binder::Status::ok(); |
| 143 | } |
| 144 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 145 | bool waitForNumCameras(size_t num) const { |
| 146 | Mutex::Autolock l(mLock); |
| 147 | |
| 148 | if (mCameraStatuses.size() == num) { |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | while (mCameraStatuses.size() < num) { |
| 153 | if (mCondition.waitRelative(mLock, SETUP_TIMEOUT) != OK) { |
| 154 | return false; |
| 155 | } |
| 156 | } |
| 157 | return true; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 158 | } |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 159 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 160 | bool waitForTorchState(int32_t status, int32_t cameraId) const { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 161 | Mutex::Autolock l(mLock); |
| 162 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 163 | const auto& iter = mCameraTorchStatuses.find(std::to_string(cameraId)); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 164 | if (iter != mCameraTorchStatuses.end() && iter->second == status) { |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | bool foundStatus = false; |
| 169 | while (!foundStatus) { |
| 170 | if (mTorchCondition.waitRelative(mLock, SETUP_TIMEOUT) != OK) { |
| 171 | return false; |
| 172 | } |
| 173 | const auto& iter = |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 174 | mCameraTorchStatuses.find(std::to_string(cameraId)); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 175 | foundStatus = (iter != mCameraTorchStatuses.end() && iter->second == status); |
| 176 | } |
| 177 | return true; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 178 | } |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 179 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 180 | int32_t getTorchStatus(int32_t cameraId) const { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 181 | Mutex::Autolock l(mLock); |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 182 | const auto& iter = mCameraTorchStatuses.find(std::to_string(cameraId)); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 183 | if (iter == mCameraTorchStatuses.end()) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 184 | return hardware::ICameraServiceListener::TORCH_STATUS_UNKNOWN; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 185 | } |
| 186 | return iter->second; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 187 | } |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 188 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 189 | int32_t getStatus(const std::string& cameraId) const { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 190 | Mutex::Autolock l(mLock); |
| 191 | const auto& iter = mCameraStatuses.find(cameraId); |
| 192 | if (iter == mCameraStatuses.end()) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 193 | return hardware::ICameraServiceListener::STATUS_UNKNOWN; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 194 | } |
| 195 | return iter->second; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 196 | } |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | // Callback implementation |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 200 | class TestCameraDeviceCallbacks : public hardware::camera2::BnCameraDeviceCallbacks { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 201 | public: |
| 202 | enum Status { |
| 203 | IDLE, |
| 204 | ERROR, |
| 205 | PREPARED, |
| 206 | RUNNING, |
| 207 | SENT_RESULT, |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 208 | UNINITIALIZED, |
| 209 | REPEATING_REQUEST_ERROR, |
Shuzhen Wang | 9d06601 | 2016-09-30 11:30:20 -0700 | [diff] [blame] | 210 | REQUEST_QUEUE_EMPTY, |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | protected: |
| 214 | bool mError; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 215 | int32_t mLastStatus; |
| 216 | mutable std::vector<int32_t> mStatusesHit; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 217 | mutable Mutex mLock; |
| 218 | mutable Condition mStatusCondition; |
| 219 | public: |
| 220 | TestCameraDeviceCallbacks() : mError(false), mLastStatus(UNINITIALIZED) {} |
| 221 | |
| 222 | virtual ~TestCameraDeviceCallbacks() {} |
| 223 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 224 | virtual binder::Status onDeviceError(int errorCode, |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 225 | const CaptureResultExtras& resultExtras) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 226 | (void) resultExtras; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 227 | ALOGE("%s: onDeviceError occurred with: %d", __FUNCTION__, static_cast<int>(errorCode)); |
| 228 | Mutex::Autolock l(mLock); |
| 229 | mError = true; |
| 230 | mLastStatus = ERROR; |
| 231 | mStatusesHit.push_back(mLastStatus); |
| 232 | mStatusCondition.broadcast(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 233 | return binder::Status::ok(); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 236 | virtual binder::Status onDeviceIdle() { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 237 | Mutex::Autolock l(mLock); |
| 238 | mLastStatus = IDLE; |
| 239 | mStatusesHit.push_back(mLastStatus); |
| 240 | mStatusCondition.broadcast(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 241 | return binder::Status::ok(); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 244 | virtual binder::Status onCaptureStarted(const CaptureResultExtras& resultExtras, |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 245 | int64_t timestamp) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 246 | (void) resultExtras; |
| 247 | (void) timestamp; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 248 | Mutex::Autolock l(mLock); |
| 249 | mLastStatus = RUNNING; |
| 250 | mStatusesHit.push_back(mLastStatus); |
| 251 | mStatusCondition.broadcast(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 252 | return binder::Status::ok(); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Jayant Chowdhary | dcae796 | 2024-08-20 21:20:10 +0000 | [diff] [blame] | 255 | virtual binder::Status onResultReceived(const CameraMetadataInfo& resultInfo, |
Shuzhen Wang | 5c22c15 | 2017-12-31 17:12:25 -0800 | [diff] [blame] | 256 | const CaptureResultExtras& resultExtras, |
| 257 | const std::vector<PhysicalCaptureResultInfo>& physicalResultInfos) { |
Jayant Chowdhary | dcae796 | 2024-08-20 21:20:10 +0000 | [diff] [blame] | 258 | (void) resultInfo; |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 259 | (void) resultExtras; |
Shuzhen Wang | 5c22c15 | 2017-12-31 17:12:25 -0800 | [diff] [blame] | 260 | (void) physicalResultInfos; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 261 | Mutex::Autolock l(mLock); |
| 262 | mLastStatus = SENT_RESULT; |
| 263 | mStatusesHit.push_back(mLastStatus); |
| 264 | mStatusCondition.broadcast(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 265 | return binder::Status::ok(); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 268 | virtual binder::Status onPrepared(int streamId) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 269 | (void) streamId; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 270 | Mutex::Autolock l(mLock); |
| 271 | mLastStatus = PREPARED; |
| 272 | mStatusesHit.push_back(mLastStatus); |
| 273 | mStatusCondition.broadcast(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 274 | return binder::Status::ok(); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Yin-Chia Yeh | 8ca23dc | 2017-09-05 18:15:56 -0700 | [diff] [blame] | 277 | virtual binder::Status onRepeatingRequestError( |
| 278 | int64_t lastFrameNumber, int32_t stoppedSequenceId) { |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 279 | (void) lastFrameNumber; |
Yin-Chia Yeh | 8ca23dc | 2017-09-05 18:15:56 -0700 | [diff] [blame] | 280 | (void) stoppedSequenceId; |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 281 | Mutex::Autolock l(mLock); |
| 282 | mLastStatus = REPEATING_REQUEST_ERROR; |
| 283 | mStatusesHit.push_back(mLastStatus); |
| 284 | mStatusCondition.broadcast(); |
| 285 | return binder::Status::ok(); |
| 286 | } |
| 287 | |
Shuzhen Wang | 9d06601 | 2016-09-30 11:30:20 -0700 | [diff] [blame] | 288 | virtual binder::Status onRequestQueueEmpty() { |
| 289 | Mutex::Autolock l(mLock); |
| 290 | mLastStatus = REQUEST_QUEUE_EMPTY; |
| 291 | mStatusesHit.push_back(mLastStatus); |
| 292 | mStatusCondition.broadcast(); |
| 293 | return binder::Status::ok(); |
| 294 | } |
| 295 | |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame] | 296 | virtual binder::Status onClientSharedAccessPriorityChanged( |
| 297 | [[maybe_unused]] bool /*isPrimaryClient*/) { |
| 298 | // No-op |
| 299 | return binder::Status::ok(); |
| 300 | } |
| 301 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 302 | // Test helper functions: |
| 303 | |
| 304 | bool hadError() const { |
| 305 | Mutex::Autolock l(mLock); |
| 306 | return mError; |
| 307 | } |
| 308 | |
| 309 | bool waitForStatus(Status status) const { |
| 310 | Mutex::Autolock l(mLock); |
| 311 | if (mLastStatus == status) { |
| 312 | return true; |
| 313 | } |
| 314 | |
| 315 | while (std::find(mStatusesHit.begin(), mStatusesHit.end(), status) |
| 316 | == mStatusesHit.end()) { |
| 317 | |
| 318 | if (mStatusCondition.waitRelative(mLock, IDLE_TIMEOUT) != OK) { |
| 319 | mStatusesHit.clear(); |
| 320 | return false; |
| 321 | } |
| 322 | } |
| 323 | mStatusesHit.clear(); |
| 324 | |
| 325 | return true; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | void clearStatus() const { |
| 329 | Mutex::Autolock l(mLock); |
| 330 | mStatusesHit.clear(); |
| 331 | } |
| 332 | |
| 333 | bool waitForIdle() const { |
| 334 | return waitForStatus(IDLE); |
| 335 | } |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 336 | }; |
| 337 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 338 | namespace { |
| 339 | Mutex gLock; |
| 340 | class DeathNotifier : public IBinder::DeathRecipient |
| 341 | { |
| 342 | public: |
| 343 | DeathNotifier() {} |
| 344 | |
| 345 | virtual void binderDied(const wp<IBinder>& /*who*/) { |
| 346 | ALOGV("binderDied"); |
| 347 | Mutex::Autolock _l(gLock); |
| 348 | ALOGW("Camera service died!"); |
| 349 | } |
| 350 | }; |
| 351 | sp<DeathNotifier> gDeathNotifier; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 352 | } // anonymous namespace |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 353 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 354 | // Exercise basic binder calls for the camera service |
| 355 | TEST(CameraServiceBinderTest, CheckBinderCameraService) { |
| 356 | ProcessState::self()->startThreadPool(); |
| 357 | sp<IServiceManager> sm = defaultServiceManager(); |
| 358 | sp<IBinder> binder = sm->getService(String16("media.camera")); |
| 359 | ASSERT_NOT_NULL(binder); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 360 | if (gDeathNotifier == NULL) { |
| 361 | gDeathNotifier = new DeathNotifier(); |
| 362 | } |
| 363 | binder->linkToDeath(gDeathNotifier); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 364 | sp<hardware::ICameraService> service = |
| 365 | interface_cast<hardware::ICameraService>(binder); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 366 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 367 | binder::Status res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 368 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 369 | int32_t numCameras = 0; |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 370 | AttributionSourceState clientAttribution; |
| 371 | clientAttribution.deviceId = kDefaultDeviceId; |
| 372 | clientAttribution.uid = hardware::ICameraService::USE_CALLING_UID; |
Austin Borger | d1ad6c6 | 2024-07-01 11:28:31 -0700 | [diff] [blame] | 373 | clientAttribution.packageName = "meeeeeeeee!"; |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 374 | res = service->getNumberOfCameras(hardware::ICameraService::CAMERA_TYPE_ALL, clientAttribution, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 375 | /*devicePolicy*/0, &numCameras); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 376 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 377 | EXPECT_LE(0, numCameras); |
| 378 | |
| 379 | // Check listener binder calls |
| 380 | sp<TestCameraServiceListener> listener(new TestCameraServiceListener()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 381 | std::vector<hardware::CameraStatus> statuses; |
| 382 | res = service->addListener(listener, &statuses); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 383 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 384 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 385 | EXPECT_EQ(numCameras, static_cast<const int>(statuses.size())); |
Emilian Peev | aebbe41 | 2018-01-15 13:53:24 +0000 | [diff] [blame] | 386 | for (const auto &it : statuses) { |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 387 | listener->onStatusChanged(it.status, it.cameraId, clientAttribution.deviceId); |
Emilian Peev | aebbe41 | 2018-01-15 13:53:24 +0000 | [diff] [blame] | 388 | } |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 389 | |
| 390 | for (int32_t i = 0; i < numCameras; i++) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 391 | std::string cameraId = std::to_string(i); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 392 | |
| 393 | // Check metadata binder call |
| 394 | CameraMetadata metadata; |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 395 | res = service->getCameraCharacteristics(cameraId, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 396 | /*targetSdkVersion*/__ANDROID_API_FUTURE__, /*overrideToPortrait*/false, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 397 | clientAttribution, /*devicePolicy*/0, &metadata); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 398 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 399 | EXPECT_FALSE(metadata.isEmpty()); |
| 400 | |
| 401 | // Make sure we're available, or skip device tests otherwise |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 402 | int32_t s = listener->getStatus(cameraId); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 403 | EXPECT_EQ(::android::hardware::ICameraServiceListener::STATUS_PRESENT, s); |
| 404 | if (s != ::android::hardware::ICameraServiceListener::STATUS_PRESENT) { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 405 | continue; |
| 406 | } |
| 407 | |
| 408 | // Check connect binder calls |
| 409 | sp<TestCameraDeviceCallbacks> callbacks(new TestCameraDeviceCallbacks()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 410 | sp<hardware::camera2::ICameraDeviceUser> device; |
Austin Borger | d1ad6c6 | 2024-07-01 11:28:31 -0700 | [diff] [blame] | 411 | res = service->connectDevice(callbacks, cameraId, |
| 412 | /*oomScoreOffset*/ 0, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 413 | /*targetSdkVersion*/__ANDROID_API_FUTURE__, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame] | 414 | /*overrideToPortrait*/false, clientAttribution, /*devicePolicy*/0, |
| 415 | /*sharedMode*/false, /*out*/&device); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 416 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 417 | ASSERT_NE(nullptr, device.get()); |
| 418 | device->disconnect(); |
| 419 | EXPECT_FALSE(callbacks->hadError()); |
| 420 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 421 | int32_t torchStatus = listener->getTorchStatus(i); |
| 422 | if (torchStatus == hardware::ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF) { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 423 | // Check torch calls |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 424 | res = service->setTorchMode(cameraId, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 425 | /*enabled*/true, callbacks, clientAttribution, /*devicePolicy*/0); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 426 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 427 | EXPECT_TRUE(listener->waitForTorchState( |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 428 | hardware::ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON, i)); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 429 | res = service->setTorchMode(cameraId, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 430 | /*enabled*/false, callbacks, clientAttribution, /*devicePolicy*/0); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 431 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 432 | EXPECT_TRUE(listener->waitForTorchState( |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 433 | hardware::ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF, i)); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 437 | res = service->removeListener(listener); |
| 438 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | // Test fixture for client focused binder tests |
| 442 | class CameraClientBinderTest : public testing::Test { |
| 443 | protected: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 444 | sp<hardware::ICameraService> service; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 445 | int32_t numCameras; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 446 | std::vector<std::pair<sp<TestCameraDeviceCallbacks>, sp<hardware::camera2::ICameraDeviceUser>>> |
| 447 | openDeviceList; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 448 | sp<TestCameraServiceListener> serviceListener; |
| 449 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 450 | std::pair<sp<TestCameraDeviceCallbacks>, sp<hardware::camera2::ICameraDeviceUser>> |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 451 | openNewDevice(const std::string& deviceId) { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 452 | sp<TestCameraDeviceCallbacks> callbacks(new TestCameraDeviceCallbacks()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 453 | sp<hardware::camera2::ICameraDeviceUser> device; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 454 | { |
| 455 | SCOPED_TRACE("openNewDevice"); |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 456 | AttributionSourceState clientAttribution; |
| 457 | clientAttribution.deviceId = kDefaultDeviceId; |
| 458 | clientAttribution.uid = hardware::ICameraService::USE_CALLING_UID; |
Austin Borger | d1ad6c6 | 2024-07-01 11:28:31 -0700 | [diff] [blame] | 459 | clientAttribution.packageName = "meeeeeeeee!"; |
| 460 | binder::Status res = service->connectDevice(callbacks, deviceId, |
| 461 | /*oomScoreOffset*/ 0, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 462 | /*targetSdkVersion*/__ANDROID_API_FUTURE__, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 463 | /*overrideToPortrait*/false, clientAttribution, /*devicePolicy*/0, |
Jyoti Bhayana | 1f9600b | 2024-10-29 20:25:32 -0700 | [diff] [blame] | 464 | /*sharedMode*/false, /*out*/&device); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 465 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 466 | } |
| 467 | auto p = std::make_pair(callbacks, device); |
| 468 | openDeviceList.push_back(p); |
| 469 | return p; |
| 470 | } |
| 471 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 472 | void closeDevice(std::pair<sp<TestCameraDeviceCallbacks>, |
| 473 | sp<hardware::camera2::ICameraDeviceUser>>& p) { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 474 | if (p.second.get() != nullptr) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 475 | binder::Status res = p.second->disconnect(); |
| 476 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 477 | { |
| 478 | SCOPED_TRACE("closeDevice"); |
| 479 | EXPECT_FALSE(p.first->hadError()); |
| 480 | } |
| 481 | } |
| 482 | auto iter = std::find(openDeviceList.begin(), openDeviceList.end(), p); |
| 483 | if (iter != openDeviceList.end()) { |
| 484 | openDeviceList.erase(iter); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | virtual void SetUp() { |
| 489 | ProcessState::self()->startThreadPool(); |
| 490 | sp<IServiceManager> sm = defaultServiceManager(); |
| 491 | sp<IBinder> binder = sm->getService(String16("media.camera")); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 492 | service = interface_cast<hardware::ICameraService>(binder); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 493 | serviceListener = new TestCameraServiceListener(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 494 | std::vector<hardware::CameraStatus> statuses; |
| 495 | service->addListener(serviceListener, &statuses); |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 496 | AttributionSourceState clientAttribution; |
| 497 | clientAttribution.deviceId = kDefaultDeviceId; |
Emilian Peev | aebbe41 | 2018-01-15 13:53:24 +0000 | [diff] [blame] | 498 | for (const auto &it : statuses) { |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 499 | serviceListener->onStatusChanged(it.status, it.cameraId, clientAttribution.deviceId); |
Emilian Peev | aebbe41 | 2018-01-15 13:53:24 +0000 | [diff] [blame] | 500 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 501 | service->getNumberOfCameras(hardware::ICameraService::CAMERA_TYPE_BACKWARD_COMPATIBLE, |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 502 | clientAttribution, /*devicePolicy*/0, &numCameras); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | virtual void TearDown() { |
| 506 | service = nullptr; |
| 507 | numCameras = 0; |
| 508 | for (auto& p : openDeviceList) { |
| 509 | closeDevice(p); |
| 510 | } |
| 511 | } |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 512 | }; |
| 513 | |
| 514 | TEST_F(CameraClientBinderTest, CheckBinderCameraDeviceUser) { |
| 515 | ASSERT_NOT_NULL(service); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 516 | EXPECT_TRUE(serviceListener->waitForNumCameras(numCameras)); |
| 517 | for (int32_t i = 0; i < numCameras; i++) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 518 | std::string cameraId = std::to_string(i); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 519 | // Make sure we're available, or skip device tests otherwise |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 520 | int32_t s = serviceListener->getStatus(cameraId); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 521 | EXPECT_EQ(hardware::ICameraServiceListener::STATUS_PRESENT, s); |
| 522 | if (s != hardware::ICameraServiceListener::STATUS_PRESENT) { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 523 | continue; |
| 524 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 525 | binder::Status res; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 526 | auto p = openNewDevice(cameraId); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 527 | sp<TestCameraDeviceCallbacks> callbacks = p.first; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 528 | sp<hardware::camera2::ICameraDeviceUser> device = p.second; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 529 | |
| 530 | // Setup a buffer queue; I'm just using the vendor opaque format here as that is |
| 531 | // guaranteed to be present |
Jim Shargo | 2e0202f | 2024-07-30 19:54:43 +0000 | [diff] [blame] | 532 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
| 533 | sp<BufferItemConsumer> opaqueConsumer = new BufferItemConsumer( |
| 534 | GRALLOC_USAGE_SW_READ_NEVER, /*maxImages*/ 2, /*controlledByApp*/ true); |
| 535 | EXPECT_TRUE(opaqueConsumer.get() != nullptr); |
| 536 | opaqueConsumer->setName(String8("nom nom nom")); |
| 537 | |
| 538 | // Set to VGA dimens for default, as that is guaranteed to be present |
| 539 | EXPECT_EQ(OK, opaqueConsumer->setDefaultBufferSize(640, 480)); |
| 540 | EXPECT_EQ(OK, |
| 541 | opaqueConsumer->setDefaultBufferFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)); |
| 542 | |
| 543 | sp<Surface> surface = opaqueConsumer->getSurface(); |
Carlos Martinez Romero | 2349fec | 2024-12-10 17:56:41 -0800 | [diff] [blame] | 544 | ParcelableSurfaceType pSurface = flagtools::surfaceToParcelableSurfaceType(surface); |
Jim Shargo | 2e0202f | 2024-07-30 19:54:43 +0000 | [diff] [blame] | 545 | std::string noPhysicalId; |
Carlos Martinez Romero | 2349fec | 2024-12-10 17:56:41 -0800 | [diff] [blame] | 546 | OutputConfiguration output(pSurface, /*rotation*/ 0, noPhysicalId); |
Jim Shargo | 2e0202f | 2024-07-30 19:54:43 +0000 | [diff] [blame] | 547 | #else |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 548 | sp<IGraphicBufferProducer> gbProducer; |
| 549 | sp<IGraphicBufferConsumer> gbConsumer; |
| 550 | BufferQueue::createBufferQueue(&gbProducer, &gbConsumer); |
| 551 | sp<BufferItemConsumer> opaqueConsumer = new BufferItemConsumer(gbConsumer, |
| 552 | GRALLOC_USAGE_SW_READ_NEVER, /*maxImages*/2, /*controlledByApp*/true); |
| 553 | EXPECT_TRUE(opaqueConsumer.get() != nullptr); |
| 554 | opaqueConsumer->setName(String8("nom nom nom")); |
| 555 | |
| 556 | // Set to VGA dimens for default, as that is guaranteed to be present |
| 557 | EXPECT_EQ(OK, gbConsumer->setDefaultBufferSize(640, 480)); |
| 558 | EXPECT_EQ(OK, gbConsumer->setDefaultBufferFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)); |
| 559 | |
| 560 | sp<Surface> surface(new Surface(gbProducer, /*controlledByApp*/false)); |
| 561 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 562 | std::string noPhysicalId; |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 563 | OutputConfiguration output(gbProducer, /*rotation*/0, noPhysicalId); |
Jim Shargo | 2e0202f | 2024-07-30 19:54:43 +0000 | [diff] [blame] | 564 | #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 565 | |
| 566 | // Can we configure? |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 567 | res = device->beginConfigure(); |
| 568 | EXPECT_TRUE(res.isOk()) << res; |
| 569 | status_t streamId; |
| 570 | res = device->createStream(output, &streamId); |
| 571 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 572 | EXPECT_LE(0, streamId); |
Emilian Peev | 5fbe0ba | 2017-10-20 15:45:45 +0100 | [diff] [blame] | 573 | CameraMetadata sessionParams; |
Emilian Peev | cc0b795 | 2020-01-07 13:54:47 -0800 | [diff] [blame] | 574 | std::vector<int> offlineStreamIds; |
| 575 | res = device->endConfigure(/*isConstrainedHighSpeed*/ false, sessionParams, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 576 | ns2ms(systemTime()), &offlineStreamIds); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 577 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 578 | EXPECT_FALSE(callbacks->hadError()); |
| 579 | |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 580 | // Session configuration must also be supported in this case |
| 581 | SessionConfiguration sessionConfiguration = { /*inputWidth*/ 0, /*inputHeight*/0, |
| 582 | /*inputFormat*/ -1, CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE}; |
| 583 | sessionConfiguration.addOutputConfiguration(output); |
| 584 | bool queryStatus; |
| 585 | res = device->isSessionConfigurationSupported(sessionConfiguration, &queryStatus); |
| 586 | EXPECT_TRUE(res.isOk() || |
Jiyong Park | 5a095ea | 2019-07-09 15:43:57 +0900 | [diff] [blame] | 587 | (res.serviceSpecificErrorCode() == ICameraService::ERROR_INVALID_OPERATION)) |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 588 | << res; |
| 589 | if (res.isOk()) { |
| 590 | EXPECT_TRUE(queryStatus); |
| 591 | } |
| 592 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 593 | // Can we make requests? |
| 594 | CameraMetadata requestTemplate; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 595 | res = device->createDefaultRequest(/*preview template*/1, |
| 596 | /*out*/&requestTemplate); |
| 597 | EXPECT_TRUE(res.isOk()) << res; |
| 598 | |
| 599 | hardware::camera2::CaptureRequest request; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 600 | request.mPhysicalCameraSettings.push_back({cameraId, requestTemplate}); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 601 | request.mSurfaceList.add(surface); |
| 602 | request.mIsReprocess = false; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 603 | int64_t lastFrameNumber = 0; |
| 604 | int64_t lastFrameNumberPrev = 0; |
| 605 | callbacks->clearStatus(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 606 | |
| 607 | hardware::camera2::utils::SubmitInfo info; |
| 608 | res = device->submitRequest(request, /*streaming*/true, /*out*/&info); |
| 609 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 610 | EXPECT_TRUE(callbacks->waitForStatus(TestCameraDeviceCallbacks::SENT_RESULT)); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 611 | EXPECT_LE(0, info.mRequestId); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 612 | |
| 613 | // Can we stop requests? |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 614 | res = device->cancelRequest(info.mRequestId, /*out*/&lastFrameNumber); |
| 615 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 616 | EXPECT_TRUE(callbacks->waitForIdle()); |
| 617 | EXPECT_FALSE(callbacks->hadError()); |
| 618 | |
| 619 | // Can we do it again? |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 620 | lastFrameNumberPrev = info.mLastFrameNumber; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 621 | lastFrameNumber = 0; |
| 622 | requestTemplate.clear(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 623 | res = device->createDefaultRequest(hardware::camera2::ICameraDeviceUser::TEMPLATE_PREVIEW, |
| 624 | /*out*/&requestTemplate); |
| 625 | EXPECT_TRUE(res.isOk()) << res; |
| 626 | hardware::camera2::CaptureRequest request2; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 627 | request2.mPhysicalCameraSettings.push_back({cameraId, requestTemplate}); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 628 | request2.mSurfaceList.add(surface); |
| 629 | request2.mIsReprocess = false; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 630 | callbacks->clearStatus(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 631 | hardware::camera2::utils::SubmitInfo info2; |
| 632 | res = device->submitRequest(request2, /*streaming*/true, |
| 633 | /*out*/&info2); |
| 634 | EXPECT_TRUE(res.isOk()) << res; |
| 635 | EXPECT_EQ(hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES, |
| 636 | info2.mLastFrameNumber); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 637 | lastFrameNumber = 0; |
| 638 | EXPECT_TRUE(callbacks->waitForStatus(TestCameraDeviceCallbacks::SENT_RESULT)); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 639 | EXPECT_LE(0, info2.mRequestId); |
| 640 | res = device->cancelRequest(info2.mRequestId, /*out*/&lastFrameNumber); |
| 641 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 642 | EXPECT_TRUE(callbacks->waitForIdle()); |
| 643 | EXPECT_LE(lastFrameNumberPrev, lastFrameNumber); |
| 644 | sleep(/*second*/1); // allow some time for errors to show up, if any |
| 645 | EXPECT_FALSE(callbacks->hadError()); |
| 646 | |
| 647 | // Can we do it with a request list? |
| 648 | lastFrameNumberPrev = lastFrameNumber; |
| 649 | lastFrameNumber = 0; |
| 650 | requestTemplate.clear(); |
| 651 | CameraMetadata requestTemplate2; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 652 | res = device->createDefaultRequest(hardware::camera2::ICameraDeviceUser::TEMPLATE_PREVIEW, |
| 653 | /*out*/&requestTemplate); |
| 654 | EXPECT_TRUE(res.isOk()) << res; |
| 655 | res = device->createDefaultRequest(hardware::camera2::ICameraDeviceUser::TEMPLATE_PREVIEW, |
| 656 | /*out*/&requestTemplate2); |
| 657 | EXPECT_TRUE(res.isOk()) << res; |
| 658 | android::hardware::camera2::CaptureRequest request3; |
| 659 | android::hardware::camera2::CaptureRequest request4; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 660 | request3.mPhysicalCameraSettings.push_back({cameraId, requestTemplate}); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 661 | request3.mSurfaceList.add(surface); |
| 662 | request3.mIsReprocess = false; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 663 | request4.mPhysicalCameraSettings.push_back({cameraId, requestTemplate2}); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 664 | request4.mSurfaceList.add(surface); |
| 665 | request4.mIsReprocess = false; |
| 666 | std::vector<hardware::camera2::CaptureRequest> requestList; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 667 | requestList.push_back(request3); |
| 668 | requestList.push_back(request4); |
| 669 | |
| 670 | callbacks->clearStatus(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 671 | hardware::camera2::utils::SubmitInfo info3; |
| 672 | res = device->submitRequestList(requestList, /*streaming*/false, |
| 673 | /*out*/&info3); |
| 674 | EXPECT_TRUE(res.isOk()) << res; |
| 675 | EXPECT_LE(0, info3.mRequestId); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 676 | EXPECT_TRUE(callbacks->waitForStatus(TestCameraDeviceCallbacks::SENT_RESULT)); |
| 677 | EXPECT_TRUE(callbacks->waitForIdle()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 678 | EXPECT_LE(lastFrameNumberPrev, info3.mLastFrameNumber); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 679 | sleep(/*second*/1); // allow some time for errors to show up, if any |
| 680 | EXPECT_FALSE(callbacks->hadError()); |
| 681 | |
| 682 | // Can we unconfigure? |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 683 | res = device->beginConfigure(); |
| 684 | EXPECT_TRUE(res.isOk()) << res; |
| 685 | res = device->deleteStream(streamId); |
| 686 | EXPECT_TRUE(res.isOk()) << res; |
Emilian Peev | cc0b795 | 2020-01-07 13:54:47 -0800 | [diff] [blame] | 687 | res = device->endConfigure(/*isConstrainedHighSpeed*/ false, sessionParams, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 688 | ns2ms(systemTime()), &offlineStreamIds); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 689 | EXPECT_TRUE(res.isOk()) << res; |
| 690 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 691 | sleep(/*second*/1); // allow some time for errors to show up, if any |
| 692 | EXPECT_FALSE(callbacks->hadError()); |
| 693 | |
| 694 | closeDevice(p); |
| 695 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 696 | } |
Emilian Peev | aebbe41 | 2018-01-15 13:53:24 +0000 | [diff] [blame] | 697 | |
| 698 | TEST_F(CameraClientBinderTest, CheckBinderCaptureRequest) { |
| 699 | sp<CaptureRequest> requestOriginal, requestParceled; |
Carlos Martinez Romero | f99f788 | 2024-12-11 11:42:15 -0800 | [diff] [blame] | 700 | |
| 701 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
| 702 | sp<BufferItemConsumer> opaqueConsumer = new BufferItemConsumer( |
| 703 | GRALLOC_USAGE_SW_READ_NEVER, /*maxImages*/ 2, /*controlledByApp*/ true); |
| 704 | EXPECT_TRUE(opaqueConsumer.get() != nullptr); |
| 705 | opaqueConsumer->setName(String8("nom nom nom")); |
| 706 | sp<Surface> surface = opaqueConsumer->getSurface(); |
| 707 | #else |
Emilian Peev | aebbe41 | 2018-01-15 13:53:24 +0000 | [diff] [blame] | 708 | sp<IGraphicBufferProducer> gbProducer; |
| 709 | sp<IGraphicBufferConsumer> gbConsumer; |
| 710 | BufferQueue::createBufferQueue(&gbProducer, &gbConsumer); |
| 711 | sp<Surface> surface(new Surface(gbProducer, /*controlledByApp*/false)); |
Carlos Martinez Romero | f99f788 | 2024-12-11 11:42:15 -0800 | [diff] [blame] | 712 | #endif |
| 713 | |
Emilian Peev | aebbe41 | 2018-01-15 13:53:24 +0000 | [diff] [blame] | 714 | Vector<sp<Surface>> surfaceList; |
| 715 | surfaceList.push_back(surface); |
| 716 | std::string physicalDeviceId1 = "0"; |
| 717 | std::string physicalDeviceId2 = "1"; |
| 718 | CameraMetadata physicalDeviceSettings1, physicalDeviceSettings2; |
| 719 | uint8_t intent1 = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW; |
| 720 | uint8_t intent2 = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD; |
| 721 | EXPECT_EQ(OK, physicalDeviceSettings1.update(ANDROID_CONTROL_CAPTURE_INTENT, &intent1, 1)); |
| 722 | EXPECT_EQ(OK, physicalDeviceSettings2.update(ANDROID_CONTROL_CAPTURE_INTENT, &intent2, 1)); |
| 723 | |
| 724 | requestParceled = new CaptureRequest(); |
| 725 | Parcel p; |
| 726 | EXPECT_TRUE(requestParceled->readFromParcel(&p) != OK); |
| 727 | p.writeInt32(0); |
| 728 | p.setDataPosition(0); |
| 729 | EXPECT_TRUE(requestParceled->readFromParcel(&p) != OK); |
| 730 | p.freeData(); |
| 731 | p.writeInt32(-1); |
| 732 | p.setDataPosition(0); |
| 733 | EXPECT_TRUE(requestParceled->readFromParcel(&p) != OK); |
| 734 | p.freeData(); |
| 735 | p.writeInt32(1); |
| 736 | p.setDataPosition(0); |
| 737 | EXPECT_TRUE(requestParceled->readFromParcel(&p) != OK); |
| 738 | |
| 739 | requestOriginal = new CaptureRequest(); |
| 740 | requestOriginal->mPhysicalCameraSettings.push_back({physicalDeviceId1, |
| 741 | physicalDeviceSettings1}); |
| 742 | requestOriginal->mPhysicalCameraSettings.push_back({physicalDeviceId2, |
| 743 | physicalDeviceSettings2}); |
| 744 | requestOriginal->mSurfaceList.push_back(surface); |
| 745 | requestOriginal->mIsReprocess = false; |
| 746 | requestOriginal->mSurfaceConverted = false; |
| 747 | |
| 748 | p.freeData(); |
| 749 | EXPECT_TRUE(requestOriginal->writeToParcel(&p) == OK); |
| 750 | p.setDataPosition(0); |
| 751 | EXPECT_TRUE(requestParceled->readFromParcel(&p) == OK); |
| 752 | EXPECT_EQ(requestParceled->mIsReprocess, false); |
| 753 | EXPECT_FALSE(requestParceled->mSurfaceList.empty()); |
| 754 | EXPECT_EQ(2u, requestParceled->mPhysicalCameraSettings.size()); |
| 755 | auto it = requestParceled->mPhysicalCameraSettings.begin(); |
| 756 | EXPECT_EQ(physicalDeviceId1, it->id); |
| 757 | EXPECT_TRUE(it->settings.exists(ANDROID_CONTROL_CAPTURE_INTENT)); |
| 758 | auto entry = it->settings.find(ANDROID_CONTROL_CAPTURE_INTENT); |
| 759 | EXPECT_EQ(entry.data.u8[0], intent1); |
| 760 | it++; |
| 761 | EXPECT_EQ(physicalDeviceId2, it->id); |
| 762 | EXPECT_TRUE(it->settings.exists(ANDROID_CONTROL_CAPTURE_INTENT)); |
| 763 | entry = it->settings.find(ANDROID_CONTROL_CAPTURE_INTENT); |
| 764 | EXPECT_EQ(entry.data.u8[0], intent2); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 765 | } |