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