| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2013 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_TAG "Camera3-Device" | 
|  | 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA | 
|  | 19 | //#define LOG_NDEBUG 0 | 
|  | 20 | //#define LOG_NNDEBUG 0  // Per-frame verbose logging | 
|  | 21 |  | 
|  | 22 | #ifdef LOG_NNDEBUG | 
|  | 23 | #define ALOGVV(...) ALOGV(__VA_ARGS__) | 
|  | 24 | #else | 
|  | 25 | #define ALOGVV(...) ((void)0) | 
|  | 26 | #endif | 
|  | 27 |  | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 28 | // Convenience macro for transient errors | 
|  | 29 | #define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \ | 
|  | 30 | ##__VA_ARGS__) | 
|  | 31 |  | 
|  | 32 | // Convenience macros for transitioning to the error state | 
|  | 33 | #define SET_ERR(fmt, ...) setErrorState(   \ | 
|  | 34 | "%s: " fmt, __FUNCTION__,              \ | 
|  | 35 | ##__VA_ARGS__) | 
|  | 36 | #define SET_ERR_L(fmt, ...) setErrorStateLocked( \ | 
|  | 37 | "%s: " fmt, __FUNCTION__,                    \ | 
|  | 38 | ##__VA_ARGS__) | 
|  | 39 |  | 
| Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 40 | #include <inttypes.h> | 
|  | 41 |  | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 42 | #include <utils/Log.h> | 
|  | 43 | #include <utils/Trace.h> | 
|  | 44 | #include <utils/Timers.h> | 
| Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 45 |  | 
| Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 46 | #include "utils/CameraTraces.h" | 
| Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 47 | #include "device3/Camera3Device.h" | 
|  | 48 | #include "device3/Camera3OutputStream.h" | 
|  | 49 | #include "device3/Camera3InputStream.h" | 
|  | 50 | #include "device3/Camera3ZslStream.h" | 
| Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 51 | #include "device3/Camera3DummyStream.h" | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 52 | #include "CameraService.h" | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 53 |  | 
|  | 54 | using namespace android::camera3; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 55 |  | 
|  | 56 | namespace android { | 
|  | 57 |  | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 58 | Camera3Device::Camera3Device(int id): | 
|  | 59 | mId(id), | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 60 | mHal3Device(NULL), | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 61 | mStatus(STATUS_UNINITIALIZED), | 
| Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 62 | mUsePartialResult(false), | 
|  | 63 | mNumPartialResults(1), | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 64 | mNextResultFrameNumber(0), | 
| Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 65 | mNextReprocessResultFrameNumber(0), | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 66 | mNextShutterFrameNumber(0), | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 67 | mListener(NULL) | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 68 | { | 
|  | 69 | ATRACE_CALL(); | 
|  | 70 | camera3_callback_ops::notify = &sNotify; | 
|  | 71 | camera3_callback_ops::process_capture_result = &sProcessCaptureResult; | 
|  | 72 | ALOGV("%s: Created device for camera %d", __FUNCTION__, id); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | Camera3Device::~Camera3Device() | 
|  | 76 | { | 
|  | 77 | ATRACE_CALL(); | 
|  | 78 | ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId); | 
|  | 79 | disconnect(); | 
|  | 80 | } | 
|  | 81 |  | 
| Igor Murashkin | 7138105 | 2013-03-04 14:53:08 -0800 | [diff] [blame] | 82 | int Camera3Device::getId() const { | 
|  | 83 | return mId; | 
|  | 84 | } | 
|  | 85 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 86 | /** | 
|  | 87 | * CameraDeviceBase interface | 
|  | 88 | */ | 
|  | 89 |  | 
| Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 90 | status_t Camera3Device::initialize(CameraModule *module) | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 91 | { | 
|  | 92 | ATRACE_CALL(); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 93 | Mutex::Autolock il(mInterfaceLock); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 94 | Mutex::Autolock l(mLock); | 
|  | 95 |  | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 96 | ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 97 | if (mStatus != STATUS_UNINITIALIZED) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 98 | CLOGE("Already initialized!"); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 99 | return INVALID_OPERATION; | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | /** Open HAL device */ | 
|  | 103 |  | 
|  | 104 | status_t res; | 
|  | 105 | String8 deviceName = String8::format("%d", mId); | 
|  | 106 |  | 
|  | 107 | camera3_device_t *device; | 
|  | 108 |  | 
| Zhijun He | 213ce79 | 2013-11-19 08:45:15 -0800 | [diff] [blame] | 109 | ATRACE_BEGIN("camera3->open"); | 
| Chien-Yu Chen | d231fd6 | 2015-02-25 16:04:22 -0800 | [diff] [blame] | 110 | res = module->open(deviceName.string(), | 
|  | 111 | reinterpret_cast<hw_device_t**>(&device)); | 
| Zhijun He | 213ce79 | 2013-11-19 08:45:15 -0800 | [diff] [blame] | 112 | ATRACE_END(); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 113 |  | 
|  | 114 | if (res != OK) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 115 | SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 116 | return res; | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | /** Cross-check device version */ | 
| Zhijun He | 95dd5ba | 2014-03-26 18:18:00 -0700 | [diff] [blame] | 120 | if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 121 | SET_ERR_L("Could not open camera: " | 
| Zhijun He | 95dd5ba | 2014-03-26 18:18:00 -0700 | [diff] [blame] | 122 | "Camera device should be at least %x, reports %x instead", | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 123 | CAMERA_DEVICE_API_VERSION_3_0, | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 124 | device->common.version); | 
|  | 125 | device->common.close(&device->common); | 
|  | 126 | return BAD_VALUE; | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | camera_info info; | 
| Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 130 | res = CameraService::filterGetInfoErrorCode(module->getCameraInfo( | 
| Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 131 | mId, &info)); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 132 | if (res != OK) return res; | 
|  | 133 |  | 
|  | 134 | if (info.device_version != device->common.version) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 135 | SET_ERR_L("HAL reporting mismatched camera_info version (%x)" | 
|  | 136 | " and device version (%x).", | 
| Zhijun He | 95dd5ba | 2014-03-26 18:18:00 -0700 | [diff] [blame] | 137 | info.device_version, device->common.version); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 138 | device->common.close(&device->common); | 
|  | 139 | return BAD_VALUE; | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | /** Initialize device with callback functions */ | 
|  | 143 |  | 
| Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 144 | ATRACE_BEGIN("camera3->initialize"); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 145 | res = device->ops->initialize(device, this); | 
| Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 146 | ATRACE_END(); | 
|  | 147 |  | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 148 | if (res != OK) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 149 | SET_ERR_L("Unable to initialize HAL device: %s (%d)", | 
|  | 150 | strerror(-res), res); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 151 | device->common.close(&device->common); | 
|  | 152 | return BAD_VALUE; | 
|  | 153 | } | 
|  | 154 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 155 | /** Start up status tracker thread */ | 
|  | 156 | mStatusTracker = new StatusTracker(this); | 
|  | 157 | res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string()); | 
|  | 158 | if (res != OK) { | 
|  | 159 | SET_ERR_L("Unable to start status tracking thread: %s (%d)", | 
|  | 160 | strerror(-res), res); | 
|  | 161 | device->common.close(&device->common); | 
|  | 162 | mStatusTracker.clear(); | 
|  | 163 | return res; | 
|  | 164 | } | 
|  | 165 |  | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 166 | /** Start up request queue thread */ | 
|  | 167 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 168 | mRequestThread = new RequestThread(this, mStatusTracker, device); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 169 | res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string()); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 170 | if (res != OK) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 171 | SET_ERR_L("Unable to start request queue thread: %s (%d)", | 
|  | 172 | strerror(-res), res); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 173 | device->common.close(&device->common); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 174 | mRequestThread.clear(); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 175 | return res; | 
|  | 176 | } | 
|  | 177 |  | 
| Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 178 | mPreparerThread = new PreparerThread(); | 
|  | 179 |  | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 180 | /** Everything is good to go */ | 
|  | 181 |  | 
| Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 182 | mDeviceVersion = device->common.version; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 183 | mDeviceInfo = info.static_camera_characteristics; | 
|  | 184 | mHal3Device = device; | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 185 | mStatus = STATUS_UNCONFIGURED; | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 186 | mNextStreamId = 0; | 
| Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 187 | mDummyStreamId = NO_STREAM; | 
| Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 188 | mNeedConfig = true; | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 189 | mPauseStateNotify = false; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 190 |  | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 191 | // Will the HAL be sending in early partial result metadata? | 
| Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 192 | if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) { | 
|  | 193 | camera_metadata_entry partialResultsCount = | 
|  | 194 | mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT); | 
|  | 195 | if (partialResultsCount.count > 0) { | 
|  | 196 | mNumPartialResults = partialResultsCount.data.i32[0]; | 
|  | 197 | mUsePartialResult = (mNumPartialResults > 1); | 
|  | 198 | } | 
|  | 199 | } else { | 
|  | 200 | camera_metadata_entry partialResultsQuirk = | 
|  | 201 | mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT); | 
|  | 202 | if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) { | 
|  | 203 | mUsePartialResult = true; | 
|  | 204 | } | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 205 | } | 
|  | 206 |  | 
| Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 207 | camera_metadata_entry configs = | 
|  | 208 | mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS); | 
|  | 209 | for (uint32_t i = 0; i < configs.count; i += 4) { | 
|  | 210 | if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED && | 
|  | 211 | configs.data.i32[i + 3] == | 
|  | 212 | ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) { | 
|  | 213 | mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1], | 
|  | 214 | configs.data.i32[i + 2])); | 
|  | 215 | } | 
|  | 216 | } | 
|  | 217 |  | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 218 | return OK; | 
|  | 219 | } | 
|  | 220 |  | 
|  | 221 | status_t Camera3Device::disconnect() { | 
|  | 222 | ATRACE_CALL(); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 223 | Mutex::Autolock il(mInterfaceLock); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 224 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 225 | ALOGV("%s: E", __FUNCTION__); | 
|  | 226 |  | 
| Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 227 | status_t res = OK; | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 228 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 229 | { | 
|  | 230 | Mutex::Autolock l(mLock); | 
|  | 231 | if (mStatus == STATUS_UNINITIALIZED) return res; | 
|  | 232 |  | 
|  | 233 | if (mStatus == STATUS_ACTIVE || | 
|  | 234 | (mStatus == STATUS_ERROR && mRequestThread != NULL)) { | 
|  | 235 | res = mRequestThread->clearRepeatingRequests(); | 
| Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 236 | if (res != OK) { | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 237 | SET_ERR_L("Can't stop streaming"); | 
| Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 238 | // Continue to close device even in case of error | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 239 | } else { | 
|  | 240 | res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout); | 
|  | 241 | if (res != OK) { | 
|  | 242 | SET_ERR_L("Timeout waiting for HAL to drain"); | 
|  | 243 | // Continue to close device even in case of error | 
|  | 244 | } | 
| Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 245 | } | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 246 | } | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 247 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 248 | if (mStatus == STATUS_ERROR) { | 
|  | 249 | CLOGE("Shutting down in an error state"); | 
| Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 250 | } | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 251 |  | 
|  | 252 | if (mStatusTracker != NULL) { | 
|  | 253 | mStatusTracker->requestExit(); | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | if (mRequestThread != NULL) { | 
|  | 257 | mRequestThread->requestExit(); | 
|  | 258 | } | 
|  | 259 |  | 
|  | 260 | mOutputStreams.clear(); | 
|  | 261 | mInputStream.clear(); | 
|  | 262 | } | 
|  | 263 |  | 
|  | 264 | // Joining done without holding mLock, otherwise deadlocks may ensue | 
|  | 265 | // as the threads try to access parent state | 
|  | 266 | if (mRequestThread != NULL && mStatus != STATUS_ERROR) { | 
|  | 267 | // HAL may be in a bad state, so waiting for request thread | 
|  | 268 | // (which may be stuck in the HAL processCaptureRequest call) | 
|  | 269 | // could be dangerous. | 
|  | 270 | mRequestThread->join(); | 
|  | 271 | } | 
|  | 272 |  | 
|  | 273 | if (mStatusTracker != NULL) { | 
|  | 274 | mStatusTracker->join(); | 
|  | 275 | } | 
|  | 276 |  | 
|  | 277 | { | 
|  | 278 | Mutex::Autolock l(mLock); | 
|  | 279 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 280 | mRequestThread.clear(); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 281 | mStatusTracker.clear(); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 282 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 283 | if (mHal3Device != NULL) { | 
| Zhijun He | 213ce79 | 2013-11-19 08:45:15 -0800 | [diff] [blame] | 284 | ATRACE_BEGIN("camera3->close"); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 285 | mHal3Device->common.close(&mHal3Device->common); | 
| Zhijun He | 213ce79 | 2013-11-19 08:45:15 -0800 | [diff] [blame] | 286 | ATRACE_END(); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 287 | mHal3Device = NULL; | 
|  | 288 | } | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 289 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 290 | mStatus = STATUS_UNINITIALIZED; | 
|  | 291 | } | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 292 |  | 
|  | 293 | ALOGV("%s: X", __FUNCTION__); | 
| Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 294 | return res; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 295 | } | 
|  | 296 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 297 | // For dumping/debugging only - | 
|  | 298 | // try to acquire a lock a few times, eventually give up to proceed with | 
|  | 299 | // debug/dump operations | 
|  | 300 | bool Camera3Device::tryLockSpinRightRound(Mutex& lock) { | 
|  | 301 | bool gotLock = false; | 
|  | 302 | for (size_t i = 0; i < kDumpLockAttempts; ++i) { | 
|  | 303 | if (lock.tryLock() == NO_ERROR) { | 
|  | 304 | gotLock = true; | 
|  | 305 | break; | 
|  | 306 | } else { | 
|  | 307 | usleep(kDumpSleepDuration); | 
|  | 308 | } | 
|  | 309 | } | 
|  | 310 | return gotLock; | 
|  | 311 | } | 
|  | 312 |  | 
| Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 313 | Camera3Device::Size Camera3Device::getMaxJpegResolution() const { | 
|  | 314 | int32_t maxJpegWidth = 0, maxJpegHeight = 0; | 
|  | 315 | if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) { | 
|  | 316 | const int STREAM_CONFIGURATION_SIZE = 4; | 
|  | 317 | const int STREAM_FORMAT_OFFSET = 0; | 
|  | 318 | const int STREAM_WIDTH_OFFSET = 1; | 
|  | 319 | const int STREAM_HEIGHT_OFFSET = 2; | 
|  | 320 | const int STREAM_IS_INPUT_OFFSET = 3; | 
|  | 321 | camera_metadata_ro_entry_t availableStreamConfigs = | 
|  | 322 | mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS); | 
|  | 323 | if (availableStreamConfigs.count == 0 || | 
|  | 324 | availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) { | 
|  | 325 | return Size(0, 0); | 
|  | 326 | } | 
|  | 327 |  | 
|  | 328 | // Get max jpeg size (area-wise). | 
|  | 329 | for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) { | 
|  | 330 | int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET]; | 
|  | 331 | int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET]; | 
|  | 332 | int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET]; | 
|  | 333 | int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET]; | 
|  | 334 | if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT | 
|  | 335 | && format == HAL_PIXEL_FORMAT_BLOB && | 
|  | 336 | (width * height > maxJpegWidth * maxJpegHeight)) { | 
|  | 337 | maxJpegWidth = width; | 
|  | 338 | maxJpegHeight = height; | 
|  | 339 | } | 
|  | 340 | } | 
|  | 341 | } else { | 
|  | 342 | camera_metadata_ro_entry availableJpegSizes = | 
|  | 343 | mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES); | 
|  | 344 | if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) { | 
|  | 345 | return Size(0, 0); | 
|  | 346 | } | 
|  | 347 |  | 
|  | 348 | // Get max jpeg size (area-wise). | 
|  | 349 | for (size_t i = 0; i < availableJpegSizes.count; i += 2) { | 
|  | 350 | if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1]) | 
|  | 351 | > (maxJpegWidth * maxJpegHeight)) { | 
|  | 352 | maxJpegWidth = availableJpegSizes.data.i32[i]; | 
|  | 353 | maxJpegHeight = availableJpegSizes.data.i32[i + 1]; | 
|  | 354 | } | 
|  | 355 | } | 
|  | 356 | } | 
|  | 357 | return Size(maxJpegWidth, maxJpegHeight); | 
|  | 358 | } | 
|  | 359 |  | 
| Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 360 | ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const { | 
| Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 361 | // Get max jpeg size (area-wise). | 
|  | 362 | Size maxJpegResolution = getMaxJpegResolution(); | 
|  | 363 | if (maxJpegResolution.width == 0) { | 
| Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 364 | ALOGE("%s: Camera %d: Can't find find valid available jpeg sizes in static metadata!", | 
|  | 365 | __FUNCTION__, mId); | 
|  | 366 | return BAD_VALUE; | 
|  | 367 | } | 
|  | 368 |  | 
| Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 369 | // Get max jpeg buffer size | 
|  | 370 | ssize_t maxJpegBufferSize = 0; | 
| Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 371 | camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE); | 
|  | 372 | if (jpegBufMaxSize.count == 0) { | 
| Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 373 | ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId); | 
|  | 374 | return BAD_VALUE; | 
|  | 375 | } | 
| Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 376 | maxJpegBufferSize = jpegBufMaxSize.data.i32[0]; | 
| Yin-Chia Yeh | 0c4e56d | 2015-01-09 15:21:27 -0800 | [diff] [blame] | 377 | assert(kMinJpegBufferSize < maxJpegBufferSize); | 
| Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 378 |  | 
|  | 379 | // Calculate final jpeg buffer size for the given resolution. | 
| Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 380 | float scaleFactor = ((float) (width * height)) / | 
|  | 381 | (maxJpegResolution.width * maxJpegResolution.height); | 
| Yin-Chia Yeh | 0c4e56d | 2015-01-09 15:21:27 -0800 | [diff] [blame] | 382 | ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) + | 
|  | 383 | kMinJpegBufferSize; | 
| Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 384 | if (jpegBufferSize > maxJpegBufferSize) { | 
|  | 385 | jpegBufferSize = maxJpegBufferSize; | 
| Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 386 | } | 
|  | 387 |  | 
|  | 388 | return jpegBufferSize; | 
|  | 389 | } | 
|  | 390 |  | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 391 | status_t Camera3Device::dump(int fd, const Vector<String16> &args) { | 
|  | 392 | ATRACE_CALL(); | 
|  | 393 | (void)args; | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 394 |  | 
|  | 395 | // Try to lock, but continue in case of failure (to avoid blocking in | 
|  | 396 | // deadlocks) | 
|  | 397 | bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock); | 
|  | 398 | bool gotLock = tryLockSpinRightRound(mLock); | 
|  | 399 |  | 
|  | 400 | ALOGW_IF(!gotInterfaceLock, | 
|  | 401 | "Camera %d: %s: Unable to lock interface lock, proceeding anyway", | 
|  | 402 | mId, __FUNCTION__); | 
|  | 403 | ALOGW_IF(!gotLock, | 
|  | 404 | "Camera %d: %s: Unable to lock main lock, proceeding anyway", | 
|  | 405 | mId, __FUNCTION__); | 
|  | 406 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 407 | String8 lines; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 408 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 409 | const char *status = | 
|  | 410 | mStatus == STATUS_ERROR         ? "ERROR" : | 
|  | 411 | mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" : | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 412 | mStatus == STATUS_UNCONFIGURED  ? "UNCONFIGURED" : | 
|  | 413 | mStatus == STATUS_CONFIGURED    ? "CONFIGURED" : | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 414 | mStatus == STATUS_ACTIVE        ? "ACTIVE" : | 
|  | 415 | "Unknown"; | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 416 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 417 | lines.appendFormat("    Device status: %s\n", status); | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 418 | if (mStatus == STATUS_ERROR) { | 
|  | 419 | lines.appendFormat("    Error cause: %s\n", mErrorCause.string()); | 
|  | 420 | } | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 421 | lines.appendFormat("    Stream configuration:\n"); | 
| Zhijun He | 1fa8999 | 2015-06-01 15:44:31 -0700 | [diff] [blame^] | 422 | lines.appendFormat("    Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ? | 
|  | 423 | "CONSTAINED HIGH SPEED VIDEO" : "NORMAL"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 424 |  | 
|  | 425 | if (mInputStream != NULL) { | 
|  | 426 | write(fd, lines.string(), lines.size()); | 
|  | 427 | mInputStream->dump(fd, args); | 
|  | 428 | } else { | 
|  | 429 | lines.appendFormat("      No input stream.\n"); | 
|  | 430 | write(fd, lines.string(), lines.size()); | 
|  | 431 | } | 
|  | 432 | for (size_t i = 0; i < mOutputStreams.size(); i++) { | 
|  | 433 | mOutputStreams[i]->dump(fd,args); | 
|  | 434 | } | 
|  | 435 |  | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 436 | lines = String8("    In-flight requests:\n"); | 
|  | 437 | if (mInFlightMap.size() == 0) { | 
|  | 438 | lines.append("      None\n"); | 
|  | 439 | } else { | 
|  | 440 | for (size_t i = 0; i < mInFlightMap.size(); i++) { | 
|  | 441 | InFlightRequest r = mInFlightMap.valueAt(i); | 
| Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 442 | lines.appendFormat("      Frame %d |  Timestamp: %" PRId64 ", metadata" | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 443 | " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i), | 
| Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 444 | r.shutterTimestamp, r.haveResultMetadata ? "true" : "false", | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 445 | r.numBuffersLeft); | 
|  | 446 | } | 
|  | 447 | } | 
|  | 448 | write(fd, lines.string(), lines.size()); | 
|  | 449 |  | 
| Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 450 | { | 
|  | 451 | lines = String8("    Last request sent:\n"); | 
|  | 452 | write(fd, lines.string(), lines.size()); | 
|  | 453 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 454 | CameraMetadata lastRequest = getLatestRequestLocked(); | 
| Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 455 | lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6); | 
|  | 456 | } | 
|  | 457 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 458 | if (mHal3Device != NULL) { | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 459 | lines = String8("    HAL device dump:\n"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 460 | write(fd, lines.string(), lines.size()); | 
|  | 461 | mHal3Device->ops->dump(mHal3Device, fd); | 
|  | 462 | } | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 463 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 464 | if (gotLock) mLock.unlock(); | 
|  | 465 | if (gotInterfaceLock) mInterfaceLock.unlock(); | 
|  | 466 |  | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 467 | return OK; | 
|  | 468 | } | 
|  | 469 |  | 
|  | 470 | const CameraMetadata& Camera3Device::info() const { | 
|  | 471 | ALOGVV("%s: E", __FUNCTION__); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 472 | if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED || | 
|  | 473 | mStatus == STATUS_ERROR)) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 474 | ALOGW("%s: Access to static info %s!", __FUNCTION__, | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 475 | mStatus == STATUS_ERROR ? | 
|  | 476 | "when in error state" : "before init"); | 
|  | 477 | } | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 478 | return mDeviceInfo; | 
|  | 479 | } | 
|  | 480 |  | 
| Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 481 | status_t Camera3Device::checkStatusOkToCaptureLocked() { | 
|  | 482 | switch (mStatus) { | 
|  | 483 | case STATUS_ERROR: | 
|  | 484 | CLOGE("Device has encountered a serious error"); | 
|  | 485 | return INVALID_OPERATION; | 
|  | 486 | case STATUS_UNINITIALIZED: | 
|  | 487 | CLOGE("Device not initialized"); | 
|  | 488 | return INVALID_OPERATION; | 
|  | 489 | case STATUS_UNCONFIGURED: | 
|  | 490 | case STATUS_CONFIGURED: | 
|  | 491 | case STATUS_ACTIVE: | 
|  | 492 | // OK | 
|  | 493 | break; | 
|  | 494 | default: | 
|  | 495 | SET_ERR_L("Unexpected status: %d", mStatus); | 
|  | 496 | return INVALID_OPERATION; | 
|  | 497 | } | 
|  | 498 | return OK; | 
|  | 499 | } | 
|  | 500 |  | 
|  | 501 | status_t Camera3Device::convertMetadataListToRequestListLocked( | 
|  | 502 | const List<const CameraMetadata> &metadataList, RequestList *requestList) { | 
|  | 503 | if (requestList == NULL) { | 
|  | 504 | CLOGE("requestList cannot be NULL."); | 
|  | 505 | return BAD_VALUE; | 
|  | 506 | } | 
|  | 507 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 508 | int32_t burstId = 0; | 
| Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 509 | for (List<const CameraMetadata>::const_iterator it = metadataList.begin(); | 
|  | 510 | it != metadataList.end(); ++it) { | 
|  | 511 | sp<CaptureRequest> newRequest = setUpRequestLocked(*it); | 
|  | 512 | if (newRequest == 0) { | 
|  | 513 | CLOGE("Can't create capture request"); | 
|  | 514 | return BAD_VALUE; | 
|  | 515 | } | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 516 |  | 
|  | 517 | // Setup burst Id and request Id | 
|  | 518 | newRequest->mResultExtras.burstId = burstId++; | 
|  | 519 | if (it->exists(ANDROID_REQUEST_ID)) { | 
|  | 520 | if (it->find(ANDROID_REQUEST_ID).count == 0) { | 
|  | 521 | CLOGE("RequestID entry exists; but must not be empty in metadata"); | 
|  | 522 | return BAD_VALUE; | 
|  | 523 | } | 
|  | 524 | newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0]; | 
|  | 525 | } else { | 
|  | 526 | CLOGE("RequestID does not exist in metadata"); | 
|  | 527 | return BAD_VALUE; | 
|  | 528 | } | 
|  | 529 |  | 
| Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 530 | requestList->push_back(newRequest); | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 531 |  | 
|  | 532 | ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId); | 
| Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 533 | } | 
|  | 534 | return OK; | 
|  | 535 | } | 
|  | 536 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 537 | status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) { | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 538 | ATRACE_CALL(); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 539 |  | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 540 | List<const CameraMetadata> requests; | 
|  | 541 | requests.push_back(request); | 
|  | 542 | return captureList(requests, /*lastFrameNumber*/NULL); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 543 | } | 
|  | 544 |  | 
| Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 545 | status_t Camera3Device::submitRequestsHelper( | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 546 | const List<const CameraMetadata> &requests, bool repeating, | 
|  | 547 | /*out*/ | 
|  | 548 | int64_t *lastFrameNumber) { | 
| Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 549 | ATRACE_CALL(); | 
|  | 550 | Mutex::Autolock il(mInterfaceLock); | 
|  | 551 | Mutex::Autolock l(mLock); | 
|  | 552 |  | 
|  | 553 | status_t res = checkStatusOkToCaptureLocked(); | 
|  | 554 | if (res != OK) { | 
|  | 555 | // error logged by previous call | 
|  | 556 | return res; | 
|  | 557 | } | 
|  | 558 |  | 
|  | 559 | RequestList requestList; | 
|  | 560 |  | 
|  | 561 | res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList); | 
|  | 562 | if (res != OK) { | 
|  | 563 | // error logged by previous call | 
|  | 564 | return res; | 
|  | 565 | } | 
|  | 566 |  | 
|  | 567 | if (repeating) { | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 568 | res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber); | 
| Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 569 | } else { | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 570 | res = mRequestThread->queueRequestList(requestList, lastFrameNumber); | 
| Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 571 | } | 
|  | 572 |  | 
|  | 573 | if (res == OK) { | 
|  | 574 | waitUntilStateThenRelock(/*active*/true, kActiveTimeout); | 
|  | 575 | if (res != OK) { | 
|  | 576 | SET_ERR_L("Can't transition to active in %f seconds!", | 
|  | 577 | kActiveTimeout/1e9); | 
|  | 578 | } | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 579 | ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId, | 
|  | 580 | (*(requestList.begin()))->mResultExtras.requestId); | 
| Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 581 | } else { | 
|  | 582 | CLOGE("Cannot queue request. Impossible."); | 
|  | 583 | return BAD_VALUE; | 
|  | 584 | } | 
|  | 585 |  | 
|  | 586 | return res; | 
|  | 587 | } | 
|  | 588 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 589 | status_t Camera3Device::captureList(const List<const CameraMetadata> &requests, | 
|  | 590 | int64_t *lastFrameNumber) { | 
| Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 591 | ATRACE_CALL(); | 
|  | 592 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 593 | return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber); | 
| Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 594 | } | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 595 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 596 | status_t Camera3Device::setStreamingRequest(const CameraMetadata &request, | 
|  | 597 | int64_t* /*lastFrameNumber*/) { | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 598 | ATRACE_CALL(); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 599 |  | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 600 | List<const CameraMetadata> requests; | 
|  | 601 | requests.push_back(request); | 
|  | 602 | return setStreamingRequestList(requests, /*lastFrameNumber*/NULL); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 603 | } | 
|  | 604 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 605 | status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests, | 
|  | 606 | int64_t *lastFrameNumber) { | 
| Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 607 | ATRACE_CALL(); | 
|  | 608 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 609 | return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber); | 
| Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 610 | } | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 611 |  | 
|  | 612 | sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked( | 
|  | 613 | const CameraMetadata &request) { | 
|  | 614 | status_t res; | 
|  | 615 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 616 | if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) { | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 617 | res = configureStreamsLocked(); | 
| Yin-Chia Yeh | 3ea3fcd | 2014-09-05 14:14:44 -0700 | [diff] [blame] | 618 | // Stream configuration failed due to unsupported configuration. | 
|  | 619 | // Device back to unconfigured state. Client might try other configuraitons | 
|  | 620 | if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) { | 
|  | 621 | CLOGE("No streams configured"); | 
|  | 622 | return NULL; | 
|  | 623 | } | 
|  | 624 | // Stream configuration failed for other reason. Fatal. | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 625 | if (res != OK) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 626 | SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 627 | return NULL; | 
|  | 628 | } | 
| Yin-Chia Yeh | 3ea3fcd | 2014-09-05 14:14:44 -0700 | [diff] [blame] | 629 | // Stream configuration successfully configure to empty stream configuration. | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 630 | if (mStatus == STATUS_UNCONFIGURED) { | 
|  | 631 | CLOGE("No streams configured"); | 
|  | 632 | return NULL; | 
|  | 633 | } | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 634 | } | 
|  | 635 |  | 
|  | 636 | sp<CaptureRequest> newRequest = createCaptureRequest(request); | 
|  | 637 | return newRequest; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 638 | } | 
|  | 639 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 640 | status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) { | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 641 | ATRACE_CALL(); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 642 | Mutex::Autolock il(mInterfaceLock); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 643 | Mutex::Autolock l(mLock); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 644 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 645 | switch (mStatus) { | 
|  | 646 | case STATUS_ERROR: | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 647 | CLOGE("Device has encountered a serious error"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 648 | return INVALID_OPERATION; | 
|  | 649 | case STATUS_UNINITIALIZED: | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 650 | CLOGE("Device not initialized"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 651 | return INVALID_OPERATION; | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 652 | case STATUS_UNCONFIGURED: | 
|  | 653 | case STATUS_CONFIGURED: | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 654 | case STATUS_ACTIVE: | 
|  | 655 | // OK | 
|  | 656 | break; | 
|  | 657 | default: | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 658 | SET_ERR_L("Unexpected status: %d", mStatus); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 659 | return INVALID_OPERATION; | 
|  | 660 | } | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 661 | ALOGV("Camera %d: Clearing repeating request", mId); | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 662 |  | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 663 | return mRequestThread->clearRepeatingRequests(lastFrameNumber); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 664 | } | 
|  | 665 |  | 
|  | 666 | status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) { | 
|  | 667 | ATRACE_CALL(); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 668 | Mutex::Autolock il(mInterfaceLock); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 669 |  | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 670 | return mRequestThread->waitUntilRequestProcessed(requestId, timeout); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 671 | } | 
|  | 672 |  | 
| Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 673 | status_t Camera3Device::createInputStream( | 
|  | 674 | uint32_t width, uint32_t height, int format, int *id) { | 
|  | 675 | ATRACE_CALL(); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 676 | Mutex::Autolock il(mInterfaceLock); | 
| Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 677 | Mutex::Autolock l(mLock); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 678 | ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d", | 
|  | 679 | mId, mNextStreamId, width, height, format); | 
| Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 680 |  | 
|  | 681 | status_t res; | 
|  | 682 | bool wasActive = false; | 
|  | 683 |  | 
|  | 684 | switch (mStatus) { | 
|  | 685 | case STATUS_ERROR: | 
|  | 686 | ALOGE("%s: Device has encountered a serious error", __FUNCTION__); | 
|  | 687 | return INVALID_OPERATION; | 
|  | 688 | case STATUS_UNINITIALIZED: | 
|  | 689 | ALOGE("%s: Device not initialized", __FUNCTION__); | 
|  | 690 | return INVALID_OPERATION; | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 691 | case STATUS_UNCONFIGURED: | 
|  | 692 | case STATUS_CONFIGURED: | 
| Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 693 | // OK | 
|  | 694 | break; | 
|  | 695 | case STATUS_ACTIVE: | 
|  | 696 | ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 697 | res = internalPauseAndWaitLocked(); | 
| Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 698 | if (res != OK) { | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 699 | SET_ERR_L("Can't pause captures to reconfigure streams!"); | 
| Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 700 | return res; | 
|  | 701 | } | 
|  | 702 | wasActive = true; | 
|  | 703 | break; | 
|  | 704 | default: | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 705 | SET_ERR_L("%s: Unexpected status: %d", mStatus); | 
| Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 706 | return INVALID_OPERATION; | 
|  | 707 | } | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 708 | assert(mStatus != STATUS_ACTIVE); | 
| Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 709 |  | 
|  | 710 | if (mInputStream != 0) { | 
|  | 711 | ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__); | 
|  | 712 | return INVALID_OPERATION; | 
|  | 713 | } | 
|  | 714 |  | 
|  | 715 | sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId, | 
|  | 716 | width, height, format); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 717 | newStream->setStatusTracker(mStatusTracker); | 
| Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 718 |  | 
|  | 719 | mInputStream = newStream; | 
|  | 720 |  | 
|  | 721 | *id = mNextStreamId++; | 
|  | 722 |  | 
|  | 723 | // Continue captures if active at start | 
|  | 724 | if (wasActive) { | 
|  | 725 | ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); | 
|  | 726 | res = configureStreamsLocked(); | 
|  | 727 | if (res != OK) { | 
|  | 728 | ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)", | 
|  | 729 | __FUNCTION__, mNextStreamId, strerror(-res), res); | 
|  | 730 | return res; | 
|  | 731 | } | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 732 | internalResumeLocked(); | 
| Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 733 | } | 
|  | 734 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 735 | ALOGV("Camera %d: Created input stream", mId); | 
| Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 736 | return OK; | 
|  | 737 | } | 
|  | 738 |  | 
| Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 739 |  | 
|  | 740 | status_t Camera3Device::createZslStream( | 
|  | 741 | uint32_t width, uint32_t height, | 
|  | 742 | int depth, | 
|  | 743 | /*out*/ | 
|  | 744 | int *id, | 
|  | 745 | sp<Camera3ZslStream>* zslStream) { | 
|  | 746 | ATRACE_CALL(); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 747 | Mutex::Autolock il(mInterfaceLock); | 
| Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 748 | Mutex::Autolock l(mLock); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 749 | ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d", | 
|  | 750 | mId, mNextStreamId, width, height, depth); | 
| Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 751 |  | 
|  | 752 | status_t res; | 
|  | 753 | bool wasActive = false; | 
|  | 754 |  | 
|  | 755 | switch (mStatus) { | 
|  | 756 | case STATUS_ERROR: | 
|  | 757 | ALOGE("%s: Device has encountered a serious error", __FUNCTION__); | 
|  | 758 | return INVALID_OPERATION; | 
|  | 759 | case STATUS_UNINITIALIZED: | 
|  | 760 | ALOGE("%s: Device not initialized", __FUNCTION__); | 
|  | 761 | return INVALID_OPERATION; | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 762 | case STATUS_UNCONFIGURED: | 
|  | 763 | case STATUS_CONFIGURED: | 
| Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 764 | // OK | 
|  | 765 | break; | 
|  | 766 | case STATUS_ACTIVE: | 
|  | 767 | ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 768 | res = internalPauseAndWaitLocked(); | 
| Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 769 | if (res != OK) { | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 770 | SET_ERR_L("Can't pause captures to reconfigure streams!"); | 
| Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 771 | return res; | 
|  | 772 | } | 
|  | 773 | wasActive = true; | 
|  | 774 | break; | 
|  | 775 | default: | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 776 | SET_ERR_L("Unexpected status: %d", mStatus); | 
| Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 777 | return INVALID_OPERATION; | 
|  | 778 | } | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 779 | assert(mStatus != STATUS_ACTIVE); | 
| Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 780 |  | 
|  | 781 | if (mInputStream != 0) { | 
|  | 782 | ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__); | 
|  | 783 | return INVALID_OPERATION; | 
|  | 784 | } | 
|  | 785 |  | 
|  | 786 | sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId, | 
|  | 787 | width, height, depth); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 788 | newStream->setStatusTracker(mStatusTracker); | 
| Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 789 |  | 
|  | 790 | res = mOutputStreams.add(mNextStreamId, newStream); | 
|  | 791 | if (res < 0) { | 
|  | 792 | ALOGE("%s: Can't add new stream to set: %s (%d)", | 
|  | 793 | __FUNCTION__, strerror(-res), res); | 
|  | 794 | return res; | 
|  | 795 | } | 
|  | 796 | mInputStream = newStream; | 
|  | 797 |  | 
| Yuvraj Pasi | e5e3d08 | 2014-04-15 18:37:45 +0530 | [diff] [blame] | 798 | mNeedConfig = true; | 
|  | 799 |  | 
| Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 800 | *id = mNextStreamId++; | 
|  | 801 | *zslStream = newStream; | 
|  | 802 |  | 
|  | 803 | // Continue captures if active at start | 
|  | 804 | if (wasActive) { | 
|  | 805 | ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); | 
|  | 806 | res = configureStreamsLocked(); | 
|  | 807 | if (res != OK) { | 
|  | 808 | ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)", | 
|  | 809 | __FUNCTION__, mNextStreamId, strerror(-res), res); | 
|  | 810 | return res; | 
|  | 811 | } | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 812 | internalResumeLocked(); | 
| Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 813 | } | 
|  | 814 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 815 | ALOGV("Camera %d: Created ZSL stream", mId); | 
| Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 816 | return OK; | 
|  | 817 | } | 
|  | 818 |  | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 819 | status_t Camera3Device::createStream(sp<ANativeWindow> consumer, | 
| Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 820 | uint32_t width, uint32_t height, int format, android_dataspace dataSpace, | 
| Yin-Chia Yeh | b97babb | 2015-03-12 13:42:44 -0700 | [diff] [blame] | 821 | camera3_stream_rotation_t rotation, int *id) { | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 822 | ATRACE_CALL(); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 823 | Mutex::Autolock il(mInterfaceLock); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 824 | Mutex::Autolock l(mLock); | 
| Yin-Chia Yeh | b97babb | 2015-03-12 13:42:44 -0700 | [diff] [blame] | 825 | ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d", | 
|  | 826 | mId, mNextStreamId, width, height, format, dataSpace, rotation); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 827 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 828 | status_t res; | 
|  | 829 | bool wasActive = false; | 
|  | 830 |  | 
|  | 831 | switch (mStatus) { | 
|  | 832 | case STATUS_ERROR: | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 833 | CLOGE("Device has encountered a serious error"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 834 | return INVALID_OPERATION; | 
|  | 835 | case STATUS_UNINITIALIZED: | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 836 | CLOGE("Device not initialized"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 837 | return INVALID_OPERATION; | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 838 | case STATUS_UNCONFIGURED: | 
|  | 839 | case STATUS_CONFIGURED: | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 840 | // OK | 
|  | 841 | break; | 
|  | 842 | case STATUS_ACTIVE: | 
|  | 843 | ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 844 | res = internalPauseAndWaitLocked(); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 845 | if (res != OK) { | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 846 | SET_ERR_L("Can't pause captures to reconfigure streams!"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 847 | return res; | 
|  | 848 | } | 
|  | 849 | wasActive = true; | 
|  | 850 | break; | 
|  | 851 | default: | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 852 | SET_ERR_L("Unexpected status: %d", mStatus); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 853 | return INVALID_OPERATION; | 
|  | 854 | } | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 855 | assert(mStatus != STATUS_ACTIVE); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 856 |  | 
|  | 857 | sp<Camera3OutputStream> newStream; | 
|  | 858 | if (format == HAL_PIXEL_FORMAT_BLOB) { | 
| Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 859 | ssize_t jpegBufferSize = getJpegBufferSize(width, height); | 
| Zhijun He | 28c9b6f | 2014-08-08 12:00:47 -0700 | [diff] [blame] | 860 | if (jpegBufferSize <= 0) { | 
| Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 861 | SET_ERR_L("Invalid jpeg buffer size %zd", jpegBufferSize); | 
|  | 862 | return BAD_VALUE; | 
|  | 863 | } | 
|  | 864 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 865 | newStream = new Camera3OutputStream(mNextStreamId, consumer, | 
| Yin-Chia Yeh | b97babb | 2015-03-12 13:42:44 -0700 | [diff] [blame] | 866 | width, height, jpegBufferSize, format, dataSpace, rotation); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 867 | } else { | 
|  | 868 | newStream = new Camera3OutputStream(mNextStreamId, consumer, | 
| Yin-Chia Yeh | b97babb | 2015-03-12 13:42:44 -0700 | [diff] [blame] | 869 | width, height, format, dataSpace, rotation); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 870 | } | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 871 | newStream->setStatusTracker(mStatusTracker); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 872 |  | 
|  | 873 | res = mOutputStreams.add(mNextStreamId, newStream); | 
|  | 874 | if (res < 0) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 875 | SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 876 | return res; | 
|  | 877 | } | 
|  | 878 |  | 
|  | 879 | *id = mNextStreamId++; | 
| Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 880 | mNeedConfig = true; | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 881 |  | 
|  | 882 | // Continue captures if active at start | 
|  | 883 | if (wasActive) { | 
|  | 884 | ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); | 
|  | 885 | res = configureStreamsLocked(); | 
|  | 886 | if (res != OK) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 887 | CLOGE("Can't reconfigure device for new stream %d: %s (%d)", | 
|  | 888 | mNextStreamId, strerror(-res), res); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 889 | return res; | 
|  | 890 | } | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 891 | internalResumeLocked(); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 892 | } | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 893 | ALOGV("Camera %d: Created new stream", mId); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 894 | return OK; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 895 | } | 
|  | 896 |  | 
|  | 897 | status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) { | 
|  | 898 | ATRACE_CALL(); | 
|  | 899 | (void)outputId; (void)id; | 
|  | 900 |  | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 901 | CLOGE("Unimplemented"); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 902 | return INVALID_OPERATION; | 
|  | 903 | } | 
|  | 904 |  | 
|  | 905 |  | 
|  | 906 | status_t Camera3Device::getStreamInfo(int id, | 
|  | 907 | uint32_t *width, uint32_t *height, uint32_t *format) { | 
|  | 908 | ATRACE_CALL(); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 909 | Mutex::Autolock il(mInterfaceLock); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 910 | Mutex::Autolock l(mLock); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 911 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 912 | switch (mStatus) { | 
|  | 913 | case STATUS_ERROR: | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 914 | CLOGE("Device has encountered a serious error"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 915 | return INVALID_OPERATION; | 
|  | 916 | case STATUS_UNINITIALIZED: | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 917 | CLOGE("Device not initialized!"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 918 | return INVALID_OPERATION; | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 919 | case STATUS_UNCONFIGURED: | 
|  | 920 | case STATUS_CONFIGURED: | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 921 | case STATUS_ACTIVE: | 
|  | 922 | // OK | 
|  | 923 | break; | 
|  | 924 | default: | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 925 | SET_ERR_L("Unexpected status: %d", mStatus); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 926 | return INVALID_OPERATION; | 
|  | 927 | } | 
|  | 928 |  | 
|  | 929 | ssize_t idx = mOutputStreams.indexOfKey(id); | 
|  | 930 | if (idx == NAME_NOT_FOUND) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 931 | CLOGE("Stream %d is unknown", id); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 932 | return idx; | 
|  | 933 | } | 
|  | 934 |  | 
|  | 935 | if (width) *width  = mOutputStreams[idx]->getWidth(); | 
|  | 936 | if (height) *height = mOutputStreams[idx]->getHeight(); | 
|  | 937 | if (format) *format = mOutputStreams[idx]->getFormat(); | 
|  | 938 |  | 
|  | 939 | return OK; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 940 | } | 
|  | 941 |  | 
|  | 942 | status_t Camera3Device::setStreamTransform(int id, | 
|  | 943 | int transform) { | 
|  | 944 | ATRACE_CALL(); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 945 | Mutex::Autolock il(mInterfaceLock); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 946 | Mutex::Autolock l(mLock); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 947 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 948 | switch (mStatus) { | 
|  | 949 | case STATUS_ERROR: | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 950 | CLOGE("Device has encountered a serious error"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 951 | return INVALID_OPERATION; | 
|  | 952 | case STATUS_UNINITIALIZED: | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 953 | CLOGE("Device not initialized"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 954 | return INVALID_OPERATION; | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 955 | case STATUS_UNCONFIGURED: | 
|  | 956 | case STATUS_CONFIGURED: | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 957 | case STATUS_ACTIVE: | 
|  | 958 | // OK | 
|  | 959 | break; | 
|  | 960 | default: | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 961 | SET_ERR_L("Unexpected status: %d", mStatus); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 962 | return INVALID_OPERATION; | 
|  | 963 | } | 
|  | 964 |  | 
|  | 965 | ssize_t idx = mOutputStreams.indexOfKey(id); | 
|  | 966 | if (idx == NAME_NOT_FOUND) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 967 | CLOGE("Stream %d does not exist", | 
|  | 968 | id); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 969 | return BAD_VALUE; | 
|  | 970 | } | 
|  | 971 |  | 
|  | 972 | return mOutputStreams.editValueAt(idx)->setTransform(transform); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 973 | } | 
|  | 974 |  | 
|  | 975 | status_t Camera3Device::deleteStream(int id) { | 
|  | 976 | ATRACE_CALL(); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 977 | Mutex::Autolock il(mInterfaceLock); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 978 | Mutex::Autolock l(mLock); | 
|  | 979 | status_t res; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 980 |  | 
| Igor Murashkin | e2172be | 2013-05-28 15:31:39 -0700 | [diff] [blame] | 981 | ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id); | 
|  | 982 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 983 | // CameraDevice semantics require device to already be idle before | 
|  | 984 | // deleteStream is called, unlike for createStream. | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 985 | if (mStatus == STATUS_ACTIVE) { | 
| Igor Murashkin | 5282713 | 2013-05-13 14:53:44 -0700 | [diff] [blame] | 986 | ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId); | 
|  | 987 | return -EBUSY; | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 988 | } | 
|  | 989 |  | 
| Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 990 | sp<Camera3StreamInterface> deletedStream; | 
| Zhijun He | 5f44635 | 2014-01-22 09:49:33 -0800 | [diff] [blame] | 991 | ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 992 | if (mInputStream != NULL && id == mInputStream->getId()) { | 
|  | 993 | deletedStream = mInputStream; | 
|  | 994 | mInputStream.clear(); | 
|  | 995 | } else { | 
| Zhijun He | 5f44635 | 2014-01-22 09:49:33 -0800 | [diff] [blame] | 996 | if (outputStreamIdx == NAME_NOT_FOUND) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 997 | CLOGE("Stream %d does not exist", id); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 998 | return BAD_VALUE; | 
|  | 999 | } | 
| Zhijun He | 5f44635 | 2014-01-22 09:49:33 -0800 | [diff] [blame] | 1000 | } | 
|  | 1001 |  | 
|  | 1002 | // Delete output stream or the output part of a bi-directional stream. | 
|  | 1003 | if (outputStreamIdx != NAME_NOT_FOUND) { | 
|  | 1004 | deletedStream = mOutputStreams.editValueAt(outputStreamIdx); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1005 | mOutputStreams.removeItem(id); | 
|  | 1006 | } | 
|  | 1007 |  | 
|  | 1008 | // Free up the stream endpoint so that it can be used by some other stream | 
|  | 1009 | res = deletedStream->disconnect(); | 
|  | 1010 | if (res != OK) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1011 | SET_ERR_L("Can't disconnect deleted stream %d", id); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1012 | // fall through since we want to still list the stream as deleted. | 
|  | 1013 | } | 
|  | 1014 | mDeletedStreams.add(deletedStream); | 
| Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 1015 | mNeedConfig = true; | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1016 |  | 
|  | 1017 | return res; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1018 | } | 
|  | 1019 |  | 
|  | 1020 | status_t Camera3Device::deleteReprocessStream(int id) { | 
|  | 1021 | ATRACE_CALL(); | 
|  | 1022 | (void)id; | 
|  | 1023 |  | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1024 | CLOGE("Unimplemented"); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1025 | return INVALID_OPERATION; | 
|  | 1026 | } | 
|  | 1027 |  | 
| Zhijun He | 1fa8999 | 2015-06-01 15:44:31 -0700 | [diff] [blame^] | 1028 | status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) { | 
| Igor Murashkin | e2d167e | 2014-08-19 16:19:59 -0700 | [diff] [blame] | 1029 | ATRACE_CALL(); | 
|  | 1030 | ALOGV("%s: E", __FUNCTION__); | 
|  | 1031 |  | 
|  | 1032 | Mutex::Autolock il(mInterfaceLock); | 
|  | 1033 | Mutex::Autolock l(mLock); | 
| Zhijun He | 1fa8999 | 2015-06-01 15:44:31 -0700 | [diff] [blame^] | 1034 | mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed; | 
| Igor Murashkin | e2d167e | 2014-08-19 16:19:59 -0700 | [diff] [blame] | 1035 |  | 
|  | 1036 | return configureStreamsLocked(); | 
|  | 1037 | } | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1038 |  | 
| Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 1039 | status_t Camera3Device::getInputBufferProducer( | 
|  | 1040 | sp<IGraphicBufferProducer> *producer) { | 
|  | 1041 | Mutex::Autolock il(mInterfaceLock); | 
|  | 1042 | Mutex::Autolock l(mLock); | 
|  | 1043 |  | 
|  | 1044 | if (producer == NULL) { | 
|  | 1045 | return BAD_VALUE; | 
|  | 1046 | } else if (mInputStream == NULL) { | 
|  | 1047 | return INVALID_OPERATION; | 
|  | 1048 | } | 
|  | 1049 |  | 
|  | 1050 | return mInputStream->getInputBufferProducer(producer); | 
|  | 1051 | } | 
|  | 1052 |  | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1053 | status_t Camera3Device::createDefaultRequest(int templateId, | 
|  | 1054 | CameraMetadata *request) { | 
|  | 1055 | ATRACE_CALL(); | 
| Alex Ray | fe7e0c6 | 2013-05-30 00:12:13 -0700 | [diff] [blame] | 1056 | ALOGV("%s: for template %d", __FUNCTION__, templateId); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1057 | Mutex::Autolock il(mInterfaceLock); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1058 | Mutex::Autolock l(mLock); | 
|  | 1059 |  | 
|  | 1060 | switch (mStatus) { | 
|  | 1061 | case STATUS_ERROR: | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1062 | CLOGE("Device has encountered a serious error"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1063 | return INVALID_OPERATION; | 
|  | 1064 | case STATUS_UNINITIALIZED: | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1065 | CLOGE("Device is not initialized!"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1066 | return INVALID_OPERATION; | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1067 | case STATUS_UNCONFIGURED: | 
|  | 1068 | case STATUS_CONFIGURED: | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1069 | case STATUS_ACTIVE: | 
|  | 1070 | // OK | 
|  | 1071 | break; | 
|  | 1072 | default: | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1073 | SET_ERR_L("Unexpected status: %d", mStatus); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1074 | return INVALID_OPERATION; | 
|  | 1075 | } | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1076 |  | 
| Zhijun He | a1530f1 | 2014-09-14 12:44:20 -0700 | [diff] [blame] | 1077 | if (!mRequestTemplateCache[templateId].isEmpty()) { | 
|  | 1078 | *request = mRequestTemplateCache[templateId]; | 
|  | 1079 | return OK; | 
|  | 1080 | } | 
|  | 1081 |  | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1082 | const camera_metadata_t *rawRequest; | 
| Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1083 | ATRACE_BEGIN("camera3->construct_default_request_settings"); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1084 | rawRequest = mHal3Device->ops->construct_default_request_settings( | 
|  | 1085 | mHal3Device, templateId); | 
| Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1086 | ATRACE_END(); | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1087 | if (rawRequest == NULL) { | 
| Yin-Chia Yeh | 0336d36 | 2015-04-14 12:34:22 -0700 | [diff] [blame] | 1088 | ALOGI("%s: template %d is not supported on this camera device", | 
|  | 1089 | __FUNCTION__, templateId); | 
|  | 1090 | return BAD_VALUE; | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1091 | } | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1092 | *request = rawRequest; | 
| Zhijun He | a1530f1 | 2014-09-14 12:44:20 -0700 | [diff] [blame] | 1093 | mRequestTemplateCache[templateId] = rawRequest; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1094 |  | 
|  | 1095 | return OK; | 
|  | 1096 | } | 
|  | 1097 |  | 
|  | 1098 | status_t Camera3Device::waitUntilDrained() { | 
|  | 1099 | ATRACE_CALL(); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1100 | Mutex::Autolock il(mInterfaceLock); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1101 | Mutex::Autolock l(mLock); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1102 |  | 
| Zhijun He | 69a3748 | 2014-03-23 18:44:49 -0700 | [diff] [blame] | 1103 | return waitUntilDrainedLocked(); | 
|  | 1104 | } | 
|  | 1105 |  | 
|  | 1106 | status_t Camera3Device::waitUntilDrainedLocked() { | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1107 | switch (mStatus) { | 
|  | 1108 | case STATUS_UNINITIALIZED: | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1109 | case STATUS_UNCONFIGURED: | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1110 | ALOGV("%s: Already idle", __FUNCTION__); | 
|  | 1111 | return OK; | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1112 | case STATUS_CONFIGURED: | 
|  | 1113 | // To avoid race conditions, check with tracker to be sure | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1114 | case STATUS_ERROR: | 
|  | 1115 | case STATUS_ACTIVE: | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1116 | // Need to verify shut down | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1117 | break; | 
|  | 1118 | default: | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1119 | SET_ERR_L("Unexpected status: %d",mStatus); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1120 | return INVALID_OPERATION; | 
|  | 1121 | } | 
|  | 1122 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1123 | ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId); | 
|  | 1124 | status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout); | 
| Eino-Ville Talvala | 9c8a091 | 2014-09-14 14:52:19 -0700 | [diff] [blame] | 1125 | if (res != OK) { | 
|  | 1126 | SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res), | 
|  | 1127 | res); | 
|  | 1128 | } | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1129 | return res; | 
|  | 1130 | } | 
|  | 1131 |  | 
|  | 1132 | // Pause to reconfigure | 
|  | 1133 | status_t Camera3Device::internalPauseAndWaitLocked() { | 
|  | 1134 | mRequestThread->setPaused(true); | 
|  | 1135 | mPauseStateNotify = true; | 
|  | 1136 |  | 
|  | 1137 | ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId); | 
|  | 1138 | status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout); | 
|  | 1139 | if (res != OK) { | 
|  | 1140 | SET_ERR_L("Can't idle device in %f seconds!", | 
|  | 1141 | kShutdownTimeout/1e9); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1142 | } | 
|  | 1143 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1144 | return res; | 
|  | 1145 | } | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1146 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1147 | // Resume after internalPauseAndWaitLocked | 
|  | 1148 | status_t Camera3Device::internalResumeLocked() { | 
|  | 1149 | status_t res; | 
|  | 1150 |  | 
|  | 1151 | mRequestThread->setPaused(false); | 
|  | 1152 |  | 
|  | 1153 | res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout); | 
|  | 1154 | if (res != OK) { | 
|  | 1155 | SET_ERR_L("Can't transition to active in %f seconds!", | 
|  | 1156 | kActiveTimeout/1e9); | 
|  | 1157 | } | 
|  | 1158 | mPauseStateNotify = false; | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1159 | return OK; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1160 | } | 
|  | 1161 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1162 | status_t Camera3Device::waitUntilStateThenRelock(bool active, | 
|  | 1163 | nsecs_t timeout) { | 
|  | 1164 | status_t res = OK; | 
|  | 1165 | if (active == (mStatus == STATUS_ACTIVE)) { | 
|  | 1166 | // Desired state already reached | 
|  | 1167 | return res; | 
|  | 1168 | } | 
|  | 1169 |  | 
|  | 1170 | bool stateSeen = false; | 
|  | 1171 | do { | 
|  | 1172 | mRecentStatusUpdates.clear(); | 
|  | 1173 |  | 
|  | 1174 | res = mStatusChanged.waitRelative(mLock, timeout); | 
|  | 1175 | if (res != OK) break; | 
|  | 1176 |  | 
|  | 1177 | // Check state change history during wait | 
|  | 1178 | for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) { | 
|  | 1179 | if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) { | 
|  | 1180 | stateSeen = true; | 
|  | 1181 | break; | 
|  | 1182 | } | 
|  | 1183 | } | 
|  | 1184 | } while (!stateSeen); | 
|  | 1185 |  | 
|  | 1186 | return res; | 
|  | 1187 | } | 
|  | 1188 |  | 
|  | 1189 |  | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1190 | status_t Camera3Device::setNotifyCallback(NotificationListener *listener) { | 
|  | 1191 | ATRACE_CALL(); | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1192 | Mutex::Autolock l(mOutputLock); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1193 |  | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1194 | if (listener != NULL && mListener != NULL) { | 
|  | 1195 | ALOGW("%s: Replacing old callback listener", __FUNCTION__); | 
|  | 1196 | } | 
|  | 1197 | mListener = listener; | 
| Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1198 | mRequestThread->setNotificationListener(listener); | 
|  | 1199 | mPreparerThread->setNotificationListener(listener); | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1200 |  | 
|  | 1201 | return OK; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1202 | } | 
|  | 1203 |  | 
| Eino-Ville Talvala | 46910bd | 2013-07-18 19:15:17 -0700 | [diff] [blame] | 1204 | bool Camera3Device::willNotify3A() { | 
|  | 1205 | return false; | 
|  | 1206 | } | 
|  | 1207 |  | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1208 | status_t Camera3Device::waitForNextFrame(nsecs_t timeout) { | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1209 | status_t res; | 
|  | 1210 | Mutex::Autolock l(mOutputLock); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1211 |  | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1212 | while (mResultQueue.empty()) { | 
|  | 1213 | res = mResultSignal.waitRelative(mOutputLock, timeout); | 
|  | 1214 | if (res == TIMED_OUT) { | 
|  | 1215 | return res; | 
|  | 1216 | } else if (res != OK) { | 
| Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 1217 | ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)", | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1218 | __FUNCTION__, mId, timeout, strerror(-res), res); | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1219 | return res; | 
|  | 1220 | } | 
|  | 1221 | } | 
|  | 1222 | return OK; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1223 | } | 
|  | 1224 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1225 | status_t Camera3Device::getNextResult(CaptureResult *frame) { | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1226 | ATRACE_CALL(); | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1227 | Mutex::Autolock l(mOutputLock); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1228 |  | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1229 | if (mResultQueue.empty()) { | 
|  | 1230 | return NOT_ENOUGH_DATA; | 
|  | 1231 | } | 
|  | 1232 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1233 | if (frame == NULL) { | 
|  | 1234 | ALOGE("%s: argument cannot be NULL", __FUNCTION__); | 
|  | 1235 | return BAD_VALUE; | 
|  | 1236 | } | 
|  | 1237 |  | 
|  | 1238 | CaptureResult &result = *(mResultQueue.begin()); | 
|  | 1239 | frame->mResultExtras = result.mResultExtras; | 
|  | 1240 | frame->mMetadata.acquire(result.mMetadata); | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1241 | mResultQueue.erase(mResultQueue.begin()); | 
|  | 1242 |  | 
|  | 1243 | return OK; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1244 | } | 
|  | 1245 |  | 
|  | 1246 | status_t Camera3Device::triggerAutofocus(uint32_t id) { | 
|  | 1247 | ATRACE_CALL(); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1248 | Mutex::Autolock il(mInterfaceLock); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1249 |  | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1250 | ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id); | 
|  | 1251 | // Mix-in this trigger into the next request and only the next request. | 
|  | 1252 | RequestTrigger trigger[] = { | 
|  | 1253 | { | 
|  | 1254 | ANDROID_CONTROL_AF_TRIGGER, | 
|  | 1255 | ANDROID_CONTROL_AF_TRIGGER_START | 
|  | 1256 | }, | 
|  | 1257 | { | 
|  | 1258 | ANDROID_CONTROL_AF_TRIGGER_ID, | 
|  | 1259 | static_cast<int32_t>(id) | 
| Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1260 | } | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1261 | }; | 
|  | 1262 |  | 
|  | 1263 | return mRequestThread->queueTrigger(trigger, | 
|  | 1264 | sizeof(trigger)/sizeof(trigger[0])); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1265 | } | 
|  | 1266 |  | 
|  | 1267 | status_t Camera3Device::triggerCancelAutofocus(uint32_t id) { | 
|  | 1268 | ATRACE_CALL(); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1269 | Mutex::Autolock il(mInterfaceLock); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1270 |  | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1271 | ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id); | 
|  | 1272 | // Mix-in this trigger into the next request and only the next request. | 
|  | 1273 | RequestTrigger trigger[] = { | 
|  | 1274 | { | 
|  | 1275 | ANDROID_CONTROL_AF_TRIGGER, | 
|  | 1276 | ANDROID_CONTROL_AF_TRIGGER_CANCEL | 
|  | 1277 | }, | 
|  | 1278 | { | 
|  | 1279 | ANDROID_CONTROL_AF_TRIGGER_ID, | 
|  | 1280 | static_cast<int32_t>(id) | 
| Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1281 | } | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1282 | }; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1283 |  | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1284 | return mRequestThread->queueTrigger(trigger, | 
|  | 1285 | sizeof(trigger)/sizeof(trigger[0])); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1286 | } | 
|  | 1287 |  | 
|  | 1288 | status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) { | 
|  | 1289 | ATRACE_CALL(); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1290 | Mutex::Autolock il(mInterfaceLock); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1291 |  | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1292 | ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id); | 
|  | 1293 | // Mix-in this trigger into the next request and only the next request. | 
|  | 1294 | RequestTrigger trigger[] = { | 
|  | 1295 | { | 
|  | 1296 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, | 
|  | 1297 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START | 
|  | 1298 | }, | 
|  | 1299 | { | 
|  | 1300 | ANDROID_CONTROL_AE_PRECAPTURE_ID, | 
|  | 1301 | static_cast<int32_t>(id) | 
| Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1302 | } | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1303 | }; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1304 |  | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1305 | return mRequestThread->queueTrigger(trigger, | 
|  | 1306 | sizeof(trigger)/sizeof(trigger[0])); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1307 | } | 
|  | 1308 |  | 
|  | 1309 | status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId, | 
|  | 1310 | buffer_handle_t *buffer, wp<BufferReleasedListener> listener) { | 
|  | 1311 | ATRACE_CALL(); | 
|  | 1312 | (void)reprocessStreamId; (void)buffer; (void)listener; | 
|  | 1313 |  | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1314 | CLOGE("Unimplemented"); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1315 | return INVALID_OPERATION; | 
|  | 1316 | } | 
|  | 1317 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1318 | status_t Camera3Device::flush(int64_t *frameNumber) { | 
| Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 1319 | ATRACE_CALL(); | 
|  | 1320 | ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1321 | Mutex::Autolock il(mInterfaceLock); | 
| Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 1322 |  | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 1323 | NotificationListener* listener; | 
|  | 1324 | { | 
|  | 1325 | Mutex::Autolock l(mOutputLock); | 
|  | 1326 | listener = mListener; | 
|  | 1327 | } | 
|  | 1328 |  | 
| Zhijun He | 7ef2039 | 2014-04-21 16:04:17 -0700 | [diff] [blame] | 1329 | { | 
|  | 1330 | Mutex::Autolock l(mLock); | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 1331 | mRequestThread->clear(listener, /*out*/frameNumber); | 
| Zhijun He | 7ef2039 | 2014-04-21 16:04:17 -0700 | [diff] [blame] | 1332 | } | 
|  | 1333 |  | 
| Zhijun He | 491e341 | 2013-12-27 10:57:44 -0800 | [diff] [blame] | 1334 | status_t res; | 
|  | 1335 | if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) { | 
|  | 1336 | res = mHal3Device->ops->flush(mHal3Device); | 
|  | 1337 | } else { | 
| Zhijun He | 7ef2039 | 2014-04-21 16:04:17 -0700 | [diff] [blame] | 1338 | Mutex::Autolock l(mLock); | 
| Zhijun He | 69a3748 | 2014-03-23 18:44:49 -0700 | [diff] [blame] | 1339 | res = waitUntilDrainedLocked(); | 
| Zhijun He | 491e341 | 2013-12-27 10:57:44 -0800 | [diff] [blame] | 1340 | } | 
|  | 1341 |  | 
|  | 1342 | return res; | 
| Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 1343 | } | 
|  | 1344 |  | 
| Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1345 | status_t Camera3Device::prepare(int streamId) { | 
|  | 1346 | ATRACE_CALL(); | 
|  | 1347 | ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId); | 
| Eino-Ville Talvala | 261394e | 2015-05-13 14:28:38 -0700 | [diff] [blame] | 1348 | Mutex::Autolock il(mInterfaceLock); | 
|  | 1349 | Mutex::Autolock l(mLock); | 
| Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1350 |  | 
|  | 1351 | sp<Camera3StreamInterface> stream; | 
|  | 1352 | ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId); | 
|  | 1353 | if (outputStreamIdx == NAME_NOT_FOUND) { | 
|  | 1354 | CLOGE("Stream %d does not exist", streamId); | 
|  | 1355 | return BAD_VALUE; | 
|  | 1356 | } | 
|  | 1357 |  | 
|  | 1358 | stream = mOutputStreams.editValueAt(outputStreamIdx); | 
|  | 1359 |  | 
|  | 1360 | if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) { | 
| Eino-Ville Talvala | 261394e | 2015-05-13 14:28:38 -0700 | [diff] [blame] | 1361 | CLOGE("Stream %d has already been a request target", streamId); | 
| Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1362 | return BAD_VALUE; | 
|  | 1363 | } | 
|  | 1364 |  | 
|  | 1365 | if (mRequestThread->isStreamPending(stream)) { | 
| Eino-Ville Talvala | 261394e | 2015-05-13 14:28:38 -0700 | [diff] [blame] | 1366 | CLOGE("Stream %d is already a target in a pending request", streamId); | 
| Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1367 | return BAD_VALUE; | 
|  | 1368 | } | 
|  | 1369 |  | 
|  | 1370 | return mPreparerThread->prepare(stream); | 
|  | 1371 | } | 
|  | 1372 |  | 
| Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 1373 | uint32_t Camera3Device::getDeviceVersion() { | 
|  | 1374 | ATRACE_CALL(); | 
|  | 1375 | Mutex::Autolock il(mInterfaceLock); | 
|  | 1376 | return mDeviceVersion; | 
|  | 1377 | } | 
|  | 1378 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1379 | /** | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1380 | * Methods called by subclasses | 
|  | 1381 | */ | 
|  | 1382 |  | 
|  | 1383 | void Camera3Device::notifyStatus(bool idle) { | 
|  | 1384 | { | 
|  | 1385 | // Need mLock to safely update state and synchronize to current | 
|  | 1386 | // state of methods in flight. | 
|  | 1387 | Mutex::Autolock l(mLock); | 
|  | 1388 | // We can get various system-idle notices from the status tracker | 
|  | 1389 | // while starting up. Only care about them if we've actually sent | 
|  | 1390 | // in some requests recently. | 
|  | 1391 | if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) { | 
|  | 1392 | return; | 
|  | 1393 | } | 
|  | 1394 | ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId, | 
|  | 1395 | idle ? "idle" : "active"); | 
|  | 1396 | mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE; | 
|  | 1397 | mRecentStatusUpdates.add(mStatus); | 
|  | 1398 | mStatusChanged.signal(); | 
|  | 1399 |  | 
|  | 1400 | // Skip notifying listener if we're doing some user-transparent | 
|  | 1401 | // state changes | 
|  | 1402 | if (mPauseStateNotify) return; | 
|  | 1403 | } | 
|  | 1404 | NotificationListener *listener; | 
|  | 1405 | { | 
|  | 1406 | Mutex::Autolock l(mOutputLock); | 
|  | 1407 | listener = mListener; | 
|  | 1408 | } | 
|  | 1409 | if (idle && listener != NULL) { | 
|  | 1410 | listener->notifyIdle(); | 
|  | 1411 | } | 
|  | 1412 | } | 
|  | 1413 |  | 
|  | 1414 | /** | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1415 | * Camera3Device private methods | 
|  | 1416 | */ | 
|  | 1417 |  | 
|  | 1418 | sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest( | 
|  | 1419 | const CameraMetadata &request) { | 
|  | 1420 | ATRACE_CALL(); | 
|  | 1421 | status_t res; | 
|  | 1422 |  | 
|  | 1423 | sp<CaptureRequest> newRequest = new CaptureRequest; | 
|  | 1424 | newRequest->mSettings = request; | 
|  | 1425 |  | 
|  | 1426 | camera_metadata_entry_t inputStreams = | 
|  | 1427 | newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS); | 
|  | 1428 | if (inputStreams.count > 0) { | 
|  | 1429 | if (mInputStream == NULL || | 
| Zhijun He | d1d6467 | 2013-09-06 15:00:01 -0700 | [diff] [blame] | 1430 | mInputStream->getId() != inputStreams.data.i32[0]) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1431 | CLOGE("Request references unknown input stream %d", | 
|  | 1432 | inputStreams.data.u8[0]); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1433 | return NULL; | 
|  | 1434 | } | 
|  | 1435 | // Lazy completion of stream configuration (allocation/registration) | 
|  | 1436 | // on first use | 
|  | 1437 | if (mInputStream->isConfiguring()) { | 
|  | 1438 | res = mInputStream->finishConfiguration(mHal3Device); | 
|  | 1439 | if (res != OK) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1440 | SET_ERR_L("Unable to finish configuring input stream %d:" | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1441 | " %s (%d)", | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1442 | mInputStream->getId(), strerror(-res), res); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1443 | return NULL; | 
|  | 1444 | } | 
|  | 1445 | } | 
| Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1446 | // Check if stream is being prepared | 
|  | 1447 | if (mInputStream->isPreparing()) { | 
|  | 1448 | CLOGE("Request references an input stream that's being prepared!"); | 
|  | 1449 | return NULL; | 
|  | 1450 | } | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1451 |  | 
|  | 1452 | newRequest->mInputStream = mInputStream; | 
|  | 1453 | newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS); | 
|  | 1454 | } | 
|  | 1455 |  | 
|  | 1456 | camera_metadata_entry_t streams = | 
|  | 1457 | newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS); | 
|  | 1458 | if (streams.count == 0) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1459 | CLOGE("Zero output streams specified!"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1460 | return NULL; | 
|  | 1461 | } | 
|  | 1462 |  | 
|  | 1463 | for (size_t i = 0; i < streams.count; i++) { | 
| Zhijun He | d1d6467 | 2013-09-06 15:00:01 -0700 | [diff] [blame] | 1464 | int idx = mOutputStreams.indexOfKey(streams.data.i32[i]); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1465 | if (idx == NAME_NOT_FOUND) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1466 | CLOGE("Request references unknown stream %d", | 
|  | 1467 | streams.data.u8[i]); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1468 | return NULL; | 
|  | 1469 | } | 
| Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 1470 | sp<Camera3OutputStreamInterface> stream = | 
|  | 1471 | mOutputStreams.editValueAt(idx); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1472 |  | 
|  | 1473 | // Lazy completion of stream configuration (allocation/registration) | 
|  | 1474 | // on first use | 
|  | 1475 | if (stream->isConfiguring()) { | 
|  | 1476 | res = stream->finishConfiguration(mHal3Device); | 
|  | 1477 | if (res != OK) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1478 | SET_ERR_L("Unable to finish configuring stream %d: %s (%d)", | 
|  | 1479 | stream->getId(), strerror(-res), res); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1480 | return NULL; | 
|  | 1481 | } | 
|  | 1482 | } | 
| Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1483 | // Check if stream is being prepared | 
|  | 1484 | if (stream->isPreparing()) { | 
|  | 1485 | CLOGE("Request references an output stream that's being prepared!"); | 
|  | 1486 | return NULL; | 
|  | 1487 | } | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1488 |  | 
|  | 1489 | newRequest->mOutputStreams.push(stream); | 
|  | 1490 | } | 
|  | 1491 | newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS); | 
|  | 1492 |  | 
|  | 1493 | return newRequest; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1494 | } | 
|  | 1495 |  | 
| Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 1496 | bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) { | 
|  | 1497 | for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) { | 
|  | 1498 | Size size = mSupportedOpaqueInputSizes[i]; | 
|  | 1499 | if (size.width == width && size.height == height) { | 
|  | 1500 | return true; | 
|  | 1501 | } | 
|  | 1502 | } | 
|  | 1503 |  | 
|  | 1504 | return false; | 
|  | 1505 | } | 
|  | 1506 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1507 | status_t Camera3Device::configureStreamsLocked() { | 
|  | 1508 | ATRACE_CALL(); | 
|  | 1509 | status_t res; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1510 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1511 | if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1512 | CLOGE("Not idle"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1513 | return INVALID_OPERATION; | 
|  | 1514 | } | 
|  | 1515 |  | 
| Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 1516 | if (!mNeedConfig) { | 
|  | 1517 | ALOGV("%s: Skipping config, no stream changes", __FUNCTION__); | 
|  | 1518 | return OK; | 
|  | 1519 | } | 
|  | 1520 |  | 
| Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 1521 | // Workaround for device HALv3.2 or older spec bug - zero streams requires | 
|  | 1522 | // adding a dummy stream instead. | 
|  | 1523 | // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround. | 
|  | 1524 | if (mOutputStreams.size() == 0) { | 
|  | 1525 | addDummyStreamLocked(); | 
|  | 1526 | } else { | 
|  | 1527 | tryRemoveDummyStreamLocked(); | 
|  | 1528 | } | 
|  | 1529 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1530 | // Start configuring the streams | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1531 | ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1532 |  | 
|  | 1533 | camera3_stream_configuration config; | 
| Zhijun He | 1fa8999 | 2015-06-01 15:44:31 -0700 | [diff] [blame^] | 1534 | config.operation_mode = mIsConstrainedHighSpeedConfiguration ? | 
|  | 1535 | CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE : | 
|  | 1536 | CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE; | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1537 | config.num_streams = (mInputStream != NULL) + mOutputStreams.size(); | 
|  | 1538 |  | 
|  | 1539 | Vector<camera3_stream_t*> streams; | 
|  | 1540 | streams.setCapacity(config.num_streams); | 
|  | 1541 |  | 
|  | 1542 | if (mInputStream != NULL) { | 
|  | 1543 | camera3_stream_t *inputStream; | 
|  | 1544 | inputStream = mInputStream->startConfiguration(); | 
|  | 1545 | if (inputStream == NULL) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1546 | SET_ERR_L("Can't start input stream configuration"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1547 | return INVALID_OPERATION; | 
|  | 1548 | } | 
|  | 1549 | streams.add(inputStream); | 
|  | 1550 | } | 
|  | 1551 |  | 
|  | 1552 | for (size_t i = 0; i < mOutputStreams.size(); i++) { | 
| Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 1553 |  | 
|  | 1554 | // Don't configure bidi streams twice, nor add them twice to the list | 
|  | 1555 | if (mOutputStreams[i].get() == | 
|  | 1556 | static_cast<Camera3StreamInterface*>(mInputStream.get())) { | 
|  | 1557 |  | 
|  | 1558 | config.num_streams--; | 
|  | 1559 | continue; | 
|  | 1560 | } | 
|  | 1561 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1562 | camera3_stream_t *outputStream; | 
|  | 1563 | outputStream = mOutputStreams.editValueAt(i)->startConfiguration(); | 
|  | 1564 | if (outputStream == NULL) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1565 | SET_ERR_L("Can't start output stream configuration"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1566 | return INVALID_OPERATION; | 
|  | 1567 | } | 
|  | 1568 | streams.add(outputStream); | 
|  | 1569 | } | 
|  | 1570 |  | 
|  | 1571 | config.streams = streams.editArray(); | 
|  | 1572 |  | 
|  | 1573 | // Do the HAL configuration; will potentially touch stream | 
|  | 1574 | // max_buffers, usage, priv fields. | 
| Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1575 | ATRACE_BEGIN("camera3->configure_streams"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1576 | res = mHal3Device->ops->configure_streams(mHal3Device, &config); | 
| Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1577 | ATRACE_END(); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1578 |  | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 1579 | if (res == BAD_VALUE) { | 
|  | 1580 | // HAL rejected this set of streams as unsupported, clean up config | 
|  | 1581 | // attempt and return to unconfigured state | 
|  | 1582 | if (mInputStream != NULL && mInputStream->isConfiguring()) { | 
|  | 1583 | res = mInputStream->cancelConfiguration(); | 
|  | 1584 | if (res != OK) { | 
|  | 1585 | SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)", | 
|  | 1586 | mInputStream->getId(), strerror(-res), res); | 
|  | 1587 | return res; | 
|  | 1588 | } | 
|  | 1589 | } | 
|  | 1590 |  | 
|  | 1591 | for (size_t i = 0; i < mOutputStreams.size(); i++) { | 
|  | 1592 | sp<Camera3OutputStreamInterface> outputStream = | 
|  | 1593 | mOutputStreams.editValueAt(i); | 
|  | 1594 | if (outputStream->isConfiguring()) { | 
|  | 1595 | res = outputStream->cancelConfiguration(); | 
|  | 1596 | if (res != OK) { | 
|  | 1597 | SET_ERR_L( | 
|  | 1598 | "Can't cancel configuring output stream %d: %s (%d)", | 
|  | 1599 | outputStream->getId(), strerror(-res), res); | 
|  | 1600 | return res; | 
|  | 1601 | } | 
|  | 1602 | } | 
|  | 1603 | } | 
|  | 1604 |  | 
|  | 1605 | // Return state to that at start of call, so that future configures | 
|  | 1606 | // properly clean things up | 
|  | 1607 | mStatus = STATUS_UNCONFIGURED; | 
|  | 1608 | mNeedConfig = true; | 
|  | 1609 |  | 
|  | 1610 | ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId); | 
|  | 1611 | return BAD_VALUE; | 
|  | 1612 | } else if (res != OK) { | 
|  | 1613 | // Some other kind of error from configure_streams - this is not | 
|  | 1614 | // expected | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1615 | SET_ERR_L("Unable to configure streams with HAL: %s (%d)", | 
|  | 1616 | strerror(-res), res); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1617 | return res; | 
|  | 1618 | } | 
|  | 1619 |  | 
| Eino-Ville Talvala | 4c95676 | 2013-04-19 17:26:13 -0700 | [diff] [blame] | 1620 | // Finish all stream configuration immediately. | 
|  | 1621 | // TODO: Try to relax this later back to lazy completion, which should be | 
|  | 1622 | // faster | 
|  | 1623 |  | 
| Igor Murashkin | 073f857 | 2013-05-02 14:59:28 -0700 | [diff] [blame] | 1624 | if (mInputStream != NULL && mInputStream->isConfiguring()) { | 
| Eino-Ville Talvala | 4c95676 | 2013-04-19 17:26:13 -0700 | [diff] [blame] | 1625 | res = mInputStream->finishConfiguration(mHal3Device); | 
|  | 1626 | if (res != OK) { | 
|  | 1627 | SET_ERR_L("Can't finish configuring input stream %d: %s (%d)", | 
|  | 1628 | mInputStream->getId(), strerror(-res), res); | 
|  | 1629 | return res; | 
|  | 1630 | } | 
|  | 1631 | } | 
|  | 1632 |  | 
|  | 1633 | for (size_t i = 0; i < mOutputStreams.size(); i++) { | 
| Igor Murashkin | 073f857 | 2013-05-02 14:59:28 -0700 | [diff] [blame] | 1634 | sp<Camera3OutputStreamInterface> outputStream = | 
|  | 1635 | mOutputStreams.editValueAt(i); | 
|  | 1636 | if (outputStream->isConfiguring()) { | 
|  | 1637 | res = outputStream->finishConfiguration(mHal3Device); | 
|  | 1638 | if (res != OK) { | 
|  | 1639 | SET_ERR_L("Can't finish configuring output stream %d: %s (%d)", | 
|  | 1640 | outputStream->getId(), strerror(-res), res); | 
|  | 1641 | return res; | 
|  | 1642 | } | 
| Eino-Ville Talvala | 4c95676 | 2013-04-19 17:26:13 -0700 | [diff] [blame] | 1643 | } | 
|  | 1644 | } | 
|  | 1645 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1646 | // Request thread needs to know to avoid using repeat-last-settings protocol | 
|  | 1647 | // across configure_streams() calls | 
|  | 1648 | mRequestThread->configurationComplete(); | 
|  | 1649 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1650 | // Update device state | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1651 |  | 
| Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 1652 | mNeedConfig = false; | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1653 |  | 
| Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 1654 | if (mDummyStreamId == NO_STREAM) { | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1655 | mStatus = STATUS_CONFIGURED; | 
|  | 1656 | } else { | 
|  | 1657 | mStatus = STATUS_UNCONFIGURED; | 
|  | 1658 | } | 
|  | 1659 |  | 
|  | 1660 | ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId); | 
|  | 1661 |  | 
| Zhijun He | 0a21051 | 2014-07-24 13:45:15 -0700 | [diff] [blame] | 1662 | // tear down the deleted streams after configure streams. | 
|  | 1663 | mDeletedStreams.clear(); | 
|  | 1664 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1665 | return OK; | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1666 | } | 
|  | 1667 |  | 
| Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 1668 | status_t Camera3Device::addDummyStreamLocked() { | 
|  | 1669 | ATRACE_CALL(); | 
|  | 1670 | status_t res; | 
|  | 1671 |  | 
|  | 1672 | if (mDummyStreamId != NO_STREAM) { | 
|  | 1673 | // Should never be adding a second dummy stream when one is already | 
|  | 1674 | // active | 
|  | 1675 | SET_ERR_L("%s: Camera %d: A dummy stream already exists!", | 
|  | 1676 | __FUNCTION__, mId); | 
|  | 1677 | return INVALID_OPERATION; | 
|  | 1678 | } | 
|  | 1679 |  | 
|  | 1680 | ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId); | 
|  | 1681 |  | 
|  | 1682 | sp<Camera3OutputStreamInterface> dummyStream = | 
|  | 1683 | new Camera3DummyStream(mNextStreamId); | 
|  | 1684 |  | 
|  | 1685 | res = mOutputStreams.add(mNextStreamId, dummyStream); | 
|  | 1686 | if (res < 0) { | 
|  | 1687 | SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res); | 
|  | 1688 | return res; | 
|  | 1689 | } | 
|  | 1690 |  | 
|  | 1691 | mDummyStreamId = mNextStreamId; | 
|  | 1692 | mNextStreamId++; | 
|  | 1693 |  | 
|  | 1694 | return OK; | 
|  | 1695 | } | 
|  | 1696 |  | 
|  | 1697 | status_t Camera3Device::tryRemoveDummyStreamLocked() { | 
|  | 1698 | ATRACE_CALL(); | 
|  | 1699 | status_t res; | 
|  | 1700 |  | 
|  | 1701 | if (mDummyStreamId == NO_STREAM) return OK; | 
|  | 1702 | if (mOutputStreams.size() == 1) return OK; | 
|  | 1703 |  | 
|  | 1704 | ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId); | 
|  | 1705 |  | 
|  | 1706 | // Ok, have a dummy stream and there's at least one other output stream, | 
|  | 1707 | // so remove the dummy | 
|  | 1708 |  | 
|  | 1709 | sp<Camera3StreamInterface> deletedStream; | 
|  | 1710 | ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId); | 
|  | 1711 | if (outputStreamIdx == NAME_NOT_FOUND) { | 
|  | 1712 | SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId); | 
|  | 1713 | return INVALID_OPERATION; | 
|  | 1714 | } | 
|  | 1715 |  | 
|  | 1716 | deletedStream = mOutputStreams.editValueAt(outputStreamIdx); | 
|  | 1717 | mOutputStreams.removeItemsAt(outputStreamIdx); | 
|  | 1718 |  | 
|  | 1719 | // Free up the stream endpoint so that it can be used by some other stream | 
|  | 1720 | res = deletedStream->disconnect(); | 
|  | 1721 | if (res != OK) { | 
|  | 1722 | SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId); | 
|  | 1723 | // fall through since we want to still list the stream as deleted. | 
|  | 1724 | } | 
|  | 1725 | mDeletedStreams.add(deletedStream); | 
|  | 1726 | mDummyStreamId = NO_STREAM; | 
|  | 1727 |  | 
|  | 1728 | return res; | 
|  | 1729 | } | 
|  | 1730 |  | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1731 | void Camera3Device::setErrorState(const char *fmt, ...) { | 
|  | 1732 | Mutex::Autolock l(mLock); | 
|  | 1733 | va_list args; | 
|  | 1734 | va_start(args, fmt); | 
|  | 1735 |  | 
|  | 1736 | setErrorStateLockedV(fmt, args); | 
|  | 1737 |  | 
|  | 1738 | va_end(args); | 
|  | 1739 | } | 
|  | 1740 |  | 
|  | 1741 | void Camera3Device::setErrorStateV(const char *fmt, va_list args) { | 
|  | 1742 | Mutex::Autolock l(mLock); | 
|  | 1743 | setErrorStateLockedV(fmt, args); | 
|  | 1744 | } | 
|  | 1745 |  | 
|  | 1746 | void Camera3Device::setErrorStateLocked(const char *fmt, ...) { | 
|  | 1747 | va_list args; | 
|  | 1748 | va_start(args, fmt); | 
|  | 1749 |  | 
|  | 1750 | setErrorStateLockedV(fmt, args); | 
|  | 1751 |  | 
|  | 1752 | va_end(args); | 
|  | 1753 | } | 
|  | 1754 |  | 
|  | 1755 | void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) { | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1756 | // Print out all error messages to log | 
|  | 1757 | String8 errorCause = String8::formatV(fmt, args); | 
|  | 1758 | ALOGE("Camera %d: %s", mId, errorCause.string()); | 
|  | 1759 |  | 
|  | 1760 | // But only do error state transition steps for the first error | 
| Zhijun He | b05eeae | 2013-06-06 13:51:22 -0700 | [diff] [blame] | 1761 | if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return; | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1762 |  | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1763 | mErrorCause = errorCause; | 
|  | 1764 |  | 
|  | 1765 | mRequestThread->setPaused(true); | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1766 | mStatus = STATUS_ERROR; | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 1767 |  | 
|  | 1768 | // Notify upstream about a device error | 
|  | 1769 | if (mListener != NULL) { | 
|  | 1770 | mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE, | 
|  | 1771 | CaptureResultExtras()); | 
|  | 1772 | } | 
|  | 1773 |  | 
|  | 1774 | // Save stack trace. View by dumping it later. | 
|  | 1775 | CameraTraces::saveTrace(); | 
|  | 1776 | // TODO: consider adding errorCause and client pid/procname | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1777 | } | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1778 |  | 
|  | 1779 | /** | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1780 | * In-flight request management | 
|  | 1781 | */ | 
|  | 1782 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1783 | status_t Camera3Device::registerInFlight(uint32_t frameNumber, | 
| Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 1784 | int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput) { | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1785 | ATRACE_CALL(); | 
|  | 1786 | Mutex::Autolock l(mInFlightLock); | 
|  | 1787 |  | 
|  | 1788 | ssize_t res; | 
| Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 1789 | res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput)); | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1790 | if (res < 0) return res; | 
|  | 1791 |  | 
|  | 1792 | return OK; | 
|  | 1793 | } | 
|  | 1794 |  | 
|  | 1795 | /** | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1796 | * Check if all 3A fields are ready, and send off a partial 3A-only result | 
|  | 1797 | * to the output frame queue | 
|  | 1798 | */ | 
| Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 1799 | bool Camera3Device::processPartial3AResult( | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1800 | uint32_t frameNumber, | 
|  | 1801 | const CameraMetadata& partial, const CaptureResultExtras& resultExtras) { | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1802 |  | 
|  | 1803 | // Check if all 3A states are present | 
|  | 1804 | // The full list of fields is | 
|  | 1805 | //   android.control.afMode | 
|  | 1806 | //   android.control.awbMode | 
|  | 1807 | //   android.control.aeState | 
|  | 1808 | //   android.control.awbState | 
|  | 1809 | //   android.control.afState | 
|  | 1810 | //   android.control.afTriggerID | 
|  | 1811 | //   android.control.aePrecaptureID | 
|  | 1812 | // TODO: Add android.control.aeMode | 
|  | 1813 |  | 
|  | 1814 | bool gotAllStates = true; | 
|  | 1815 |  | 
|  | 1816 | uint8_t afMode; | 
|  | 1817 | uint8_t awbMode; | 
|  | 1818 | uint8_t aeState; | 
|  | 1819 | uint8_t afState; | 
|  | 1820 | uint8_t awbState; | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1821 |  | 
|  | 1822 | gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE, | 
|  | 1823 | &afMode, frameNumber); | 
|  | 1824 |  | 
|  | 1825 | gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE, | 
|  | 1826 | &awbMode, frameNumber); | 
|  | 1827 |  | 
|  | 1828 | gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE, | 
|  | 1829 | &aeState, frameNumber); | 
|  | 1830 |  | 
|  | 1831 | gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE, | 
|  | 1832 | &afState, frameNumber); | 
|  | 1833 |  | 
|  | 1834 | gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE, | 
|  | 1835 | &awbState, frameNumber); | 
|  | 1836 |  | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1837 | if (!gotAllStates) return false; | 
|  | 1838 |  | 
| Eino-Ville Talvala | 184dfe4 | 2013-11-07 15:13:16 -0800 | [diff] [blame] | 1839 | ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, " | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1840 | "AF state %d, AE state %d, AWB state %d, " | 
|  | 1841 | "AF trigger %d, AE precapture trigger %d", | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 1842 | __FUNCTION__, mId, frameNumber, resultExtras.requestId, | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1843 | afMode, awbMode, | 
|  | 1844 | afState, aeState, awbState, | 
| Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1845 | resultExtras.afTriggerId, resultExtras.precaptureTriggerId); | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1846 |  | 
|  | 1847 | // Got all states, so construct a minimal result to send | 
|  | 1848 | // In addition to the above fields, this means adding in | 
|  | 1849 | //   android.request.frameCount | 
| Eino-Ville Talvala | 184dfe4 | 2013-11-07 15:13:16 -0800 | [diff] [blame] | 1850 | //   android.request.requestId | 
| Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 1851 | //   android.quirks.partialResult (for HAL version below HAL3.2) | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1852 |  | 
| Eino-Ville Talvala | 184dfe4 | 2013-11-07 15:13:16 -0800 | [diff] [blame] | 1853 | const size_t kMinimal3AResultEntries = 10; | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1854 |  | 
|  | 1855 | Mutex::Autolock l(mOutputLock); | 
|  | 1856 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1857 | CaptureResult captureResult; | 
|  | 1858 | captureResult.mResultExtras = resultExtras; | 
|  | 1859 | captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0); | 
|  | 1860 | // TODO: change this to sp<CaptureResult>. This will need other changes, including, | 
|  | 1861 | // but not limited to CameraDeviceBase::getNextResult | 
|  | 1862 | CaptureResult& min3AResult = | 
|  | 1863 | *mResultQueue.insert(mResultQueue.end(), captureResult); | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1864 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1865 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT, | 
|  | 1866 | // TODO: This is problematic casting. Need to fix CameraMetadata. | 
|  | 1867 | reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) { | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1868 | return false; | 
|  | 1869 | } | 
|  | 1870 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1871 | int32_t requestId = resultExtras.requestId; | 
|  | 1872 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID, | 
| Eino-Ville Talvala | 184dfe4 | 2013-11-07 15:13:16 -0800 | [diff] [blame] | 1873 | &requestId, frameNumber)) { | 
|  | 1874 | return false; | 
|  | 1875 | } | 
|  | 1876 |  | 
| Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 1877 | if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) { | 
|  | 1878 | static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL; | 
|  | 1879 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT, | 
|  | 1880 | &partialResult, frameNumber)) { | 
|  | 1881 | return false; | 
|  | 1882 | } | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1883 | } | 
|  | 1884 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1885 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE, | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1886 | &afMode, frameNumber)) { | 
|  | 1887 | return false; | 
|  | 1888 | } | 
|  | 1889 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1890 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE, | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1891 | &awbMode, frameNumber)) { | 
|  | 1892 | return false; | 
|  | 1893 | } | 
|  | 1894 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1895 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE, | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1896 | &aeState, frameNumber)) { | 
|  | 1897 | return false; | 
|  | 1898 | } | 
|  | 1899 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1900 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE, | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1901 | &afState, frameNumber)) { | 
|  | 1902 | return false; | 
|  | 1903 | } | 
|  | 1904 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1905 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE, | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1906 | &awbState, frameNumber)) { | 
|  | 1907 | return false; | 
|  | 1908 | } | 
|  | 1909 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1910 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID, | 
| Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1911 | &resultExtras.afTriggerId, frameNumber)) { | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1912 | return false; | 
|  | 1913 | } | 
|  | 1914 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1915 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID, | 
| Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1916 | &resultExtras.precaptureTriggerId, frameNumber)) { | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1917 | return false; | 
|  | 1918 | } | 
|  | 1919 |  | 
| Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 1920 | // We only send the aggregated partial when all 3A related metadata are available | 
|  | 1921 | // For both API1 and API2. | 
|  | 1922 | // TODO: we probably should pass through all partials to API2 unconditionally. | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1923 | mResultSignal.signal(); | 
|  | 1924 |  | 
|  | 1925 | return true; | 
|  | 1926 | } | 
|  | 1927 |  | 
|  | 1928 | template<typename T> | 
|  | 1929 | bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag, | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1930 | T* value, uint32_t frameNumber) { | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1931 | (void) frameNumber; | 
|  | 1932 |  | 
|  | 1933 | camera_metadata_ro_entry_t entry; | 
|  | 1934 |  | 
|  | 1935 | entry = result.find(tag); | 
|  | 1936 | if (entry.count == 0) { | 
|  | 1937 | ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__, | 
|  | 1938 | mId, frameNumber, get_camera_metadata_tag_name(tag)); | 
|  | 1939 | return false; | 
|  | 1940 | } | 
|  | 1941 |  | 
|  | 1942 | if (sizeof(T) == sizeof(uint8_t)) { | 
|  | 1943 | *value = entry.data.u8[0]; | 
|  | 1944 | } else if (sizeof(T) == sizeof(int32_t)) { | 
|  | 1945 | *value = entry.data.i32[0]; | 
|  | 1946 | } else { | 
|  | 1947 | ALOGE("%s: Unexpected type", __FUNCTION__); | 
|  | 1948 | return false; | 
|  | 1949 | } | 
|  | 1950 | return true; | 
|  | 1951 | } | 
|  | 1952 |  | 
|  | 1953 | template<typename T> | 
|  | 1954 | bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag, | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1955 | const T* value, uint32_t frameNumber) { | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1956 | if (result.update(tag, value, 1) != NO_ERROR) { | 
|  | 1957 | mResultQueue.erase(--mResultQueue.end(), mResultQueue.end()); | 
|  | 1958 | SET_ERR("Frame %d: Failed to set %s in partial metadata", | 
|  | 1959 | frameNumber, get_camera_metadata_tag_name(tag)); | 
|  | 1960 | return false; | 
|  | 1961 | } | 
|  | 1962 | return true; | 
|  | 1963 | } | 
|  | 1964 |  | 
| Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 1965 | void Camera3Device::returnOutputBuffers( | 
|  | 1966 | const camera3_stream_buffer_t *outputBuffers, size_t numBuffers, | 
|  | 1967 | nsecs_t timestamp) { | 
|  | 1968 | for (size_t i = 0; i < numBuffers; i++) | 
|  | 1969 | { | 
|  | 1970 | Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream); | 
|  | 1971 | status_t res = stream->returnBuffer(outputBuffers[i], timestamp); | 
|  | 1972 | // Note: stream may be deallocated at this point, if this buffer was | 
|  | 1973 | // the last reference to it. | 
|  | 1974 | if (res != OK) { | 
|  | 1975 | ALOGE("Can't return buffer to its stream: %s (%d)", | 
|  | 1976 | strerror(-res), res); | 
|  | 1977 | } | 
|  | 1978 | } | 
|  | 1979 | } | 
|  | 1980 |  | 
|  | 1981 |  | 
|  | 1982 | void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) { | 
|  | 1983 |  | 
|  | 1984 | const InFlightRequest &request = mInFlightMap.valueAt(idx); | 
|  | 1985 | const uint32_t frameNumber = mInFlightMap.keyAt(idx); | 
|  | 1986 |  | 
|  | 1987 | nsecs_t sensorTimestamp = request.sensorTimestamp; | 
|  | 1988 | nsecs_t shutterTimestamp = request.shutterTimestamp; | 
|  | 1989 |  | 
|  | 1990 | // Check if it's okay to remove the request from InFlightMap: | 
|  | 1991 | // In the case of a successful request: | 
|  | 1992 | //      all input and output buffers, all result metadata, shutter callback | 
|  | 1993 | //      arrived. | 
|  | 1994 | // In the case of a unsuccessful request: | 
|  | 1995 | //      all input and output buffers arrived. | 
|  | 1996 | if (request.numBuffersLeft == 0 && | 
|  | 1997 | (request.requestStatus != OK || | 
|  | 1998 | (request.haveResultMetadata && shutterTimestamp != 0))) { | 
|  | 1999 | ATRACE_ASYNC_END("frame capture", frameNumber); | 
|  | 2000 |  | 
|  | 2001 | // Sanity check - if sensor timestamp matches shutter timestamp | 
|  | 2002 | if (request.requestStatus == OK && | 
|  | 2003 | sensorTimestamp != shutterTimestamp) { | 
|  | 2004 | SET_ERR("sensor timestamp (%" PRId64 | 
|  | 2005 | ") for frame %d doesn't match shutter timestamp (%" PRId64 ")", | 
|  | 2006 | sensorTimestamp, frameNumber, shutterTimestamp); | 
|  | 2007 | } | 
|  | 2008 |  | 
|  | 2009 | // for an unsuccessful request, it may have pending output buffers to | 
|  | 2010 | // return. | 
|  | 2011 | assert(request.requestStatus != OK || | 
|  | 2012 | request.pendingOutputBuffers.size() == 0); | 
|  | 2013 | returnOutputBuffers(request.pendingOutputBuffers.array(), | 
|  | 2014 | request.pendingOutputBuffers.size(), 0); | 
|  | 2015 |  | 
|  | 2016 | mInFlightMap.removeItemsAt(idx, 1); | 
|  | 2017 |  | 
|  | 2018 | ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber); | 
|  | 2019 | } | 
|  | 2020 |  | 
|  | 2021 | // Sanity check - if we have too many in-flight frames, something has | 
|  | 2022 | // likely gone wrong | 
|  | 2023 | if (mInFlightMap.size() > kInFlightWarnLimit) { | 
|  | 2024 | CLOGE("In-flight list too large: %zu", mInFlightMap.size()); | 
|  | 2025 | } | 
|  | 2026 | } | 
|  | 2027 |  | 
|  | 2028 |  | 
|  | 2029 | void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata, | 
|  | 2030 | CaptureResultExtras &resultExtras, | 
|  | 2031 | CameraMetadata &collectedPartialResult, | 
| Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 2032 | uint32_t frameNumber, | 
|  | 2033 | bool reprocess) { | 
| Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2034 | if (pendingMetadata.isEmpty()) | 
|  | 2035 | return; | 
|  | 2036 |  | 
|  | 2037 | Mutex::Autolock l(mOutputLock); | 
|  | 2038 |  | 
|  | 2039 | // TODO: need to track errors for tighter bounds on expected frame number | 
| Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 2040 | if (reprocess) { | 
|  | 2041 | if (frameNumber < mNextReprocessResultFrameNumber) { | 
|  | 2042 | SET_ERR("Out-of-order reprocess capture result metadata submitted! " | 
| Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2043 | "(got frame number %d, expecting %d)", | 
| Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 2044 | frameNumber, mNextReprocessResultFrameNumber); | 
|  | 2045 | return; | 
|  | 2046 | } | 
|  | 2047 | mNextReprocessResultFrameNumber = frameNumber + 1; | 
|  | 2048 | } else { | 
|  | 2049 | if (frameNumber < mNextResultFrameNumber) { | 
|  | 2050 | SET_ERR("Out-of-order capture result metadata submitted! " | 
|  | 2051 | "(got frame number %d, expecting %d)", | 
|  | 2052 | frameNumber, mNextResultFrameNumber); | 
|  | 2053 | return; | 
|  | 2054 | } | 
|  | 2055 | mNextResultFrameNumber = frameNumber + 1; | 
| Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2056 | } | 
| Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2057 |  | 
|  | 2058 | CaptureResult captureResult; | 
|  | 2059 | captureResult.mResultExtras = resultExtras; | 
|  | 2060 | captureResult.mMetadata = pendingMetadata; | 
|  | 2061 |  | 
|  | 2062 | if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT, | 
|  | 2063 | (int32_t*)&frameNumber, 1) != OK) { | 
|  | 2064 | SET_ERR("Failed to set frame# in metadata (%d)", | 
|  | 2065 | frameNumber); | 
|  | 2066 | return; | 
|  | 2067 | } else { | 
|  | 2068 | ALOGVV("%s: Camera %d: Set frame# in metadata (%d)", | 
|  | 2069 | __FUNCTION__, mId, frameNumber); | 
|  | 2070 | } | 
|  | 2071 |  | 
|  | 2072 | // Append any previous partials to form a complete result | 
|  | 2073 | if (mUsePartialResult && !collectedPartialResult.isEmpty()) { | 
|  | 2074 | captureResult.mMetadata.append(collectedPartialResult); | 
|  | 2075 | } | 
|  | 2076 |  | 
|  | 2077 | captureResult.mMetadata.sort(); | 
|  | 2078 |  | 
|  | 2079 | // Check that there's a timestamp in the result metadata | 
|  | 2080 | camera_metadata_entry entry = | 
|  | 2081 | captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP); | 
|  | 2082 | if (entry.count == 0) { | 
|  | 2083 | SET_ERR("No timestamp provided by HAL for frame %d!", | 
|  | 2084 | frameNumber); | 
|  | 2085 | return; | 
|  | 2086 | } | 
|  | 2087 |  | 
|  | 2088 | // Valid result, insert into queue | 
|  | 2089 | List<CaptureResult>::iterator queuedResult = | 
|  | 2090 | mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult)); | 
|  | 2091 | ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64 | 
|  | 2092 | ", burstId = %" PRId32, __FUNCTION__, | 
|  | 2093 | queuedResult->mResultExtras.requestId, | 
|  | 2094 | queuedResult->mResultExtras.frameNumber, | 
|  | 2095 | queuedResult->mResultExtras.burstId); | 
|  | 2096 |  | 
|  | 2097 | mResultSignal.signal(); | 
|  | 2098 | } | 
|  | 2099 |  | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2100 | /** | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2101 | * Camera HAL device callback methods | 
|  | 2102 | */ | 
|  | 2103 |  | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2104 | void Camera3Device::processCaptureResult(const camera3_capture_result *result) { | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2105 | ATRACE_CALL(); | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2106 |  | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2107 | status_t res; | 
|  | 2108 |  | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2109 | uint32_t frameNumber = result->frame_number; | 
| Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2110 | if (result->result == NULL && result->num_output_buffers == 0 && | 
|  | 2111 | result->input_buffer == NULL) { | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2112 | SET_ERR("No result data provided by HAL for frame %d", | 
|  | 2113 | frameNumber); | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2114 | return; | 
|  | 2115 | } | 
| Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2116 |  | 
|  | 2117 | // For HAL3.2 or above, If HAL doesn't support partial, it must always set | 
|  | 2118 | // partial_result to 1 when metadata is included in this result. | 
|  | 2119 | if (!mUsePartialResult && | 
|  | 2120 | mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 && | 
|  | 2121 | result->result != NULL && | 
|  | 2122 | result->partial_result != 1) { | 
|  | 2123 | SET_ERR("Result is malformed for frame %d: partial_result %u must be 1" | 
|  | 2124 | " if partial result is not supported", | 
|  | 2125 | frameNumber, result->partial_result); | 
|  | 2126 | return; | 
|  | 2127 | } | 
|  | 2128 |  | 
|  | 2129 | bool isPartialResult = false; | 
|  | 2130 | CameraMetadata collectedPartialResult; | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2131 | CaptureResultExtras resultExtras; | 
| Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2132 | bool hasInputBufferInRequest = false; | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2133 |  | 
| Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2134 | // Get shutter timestamp and resultExtras from list of in-flight requests, | 
|  | 2135 | // where it was added by the shutter notification for this frame. If the | 
|  | 2136 | // shutter timestamp isn't received yet, append the output buffers to the | 
|  | 2137 | // in-flight request and they will be returned when the shutter timestamp | 
|  | 2138 | // arrives. Update the in-flight status and remove the in-flight entry if | 
|  | 2139 | // all result data and shutter timestamp have been received. | 
|  | 2140 | nsecs_t shutterTimestamp = 0; | 
|  | 2141 |  | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2142 | { | 
|  | 2143 | Mutex::Autolock l(mInFlightLock); | 
|  | 2144 | ssize_t idx = mInFlightMap.indexOfKey(frameNumber); | 
|  | 2145 | if (idx == NAME_NOT_FOUND) { | 
|  | 2146 | SET_ERR("Unknown frame number for capture result: %d", | 
|  | 2147 | frameNumber); | 
|  | 2148 | return; | 
|  | 2149 | } | 
|  | 2150 | InFlightRequest &request = mInFlightMap.editValueAt(idx); | 
| Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2151 | ALOGVV("%s: got InFlightRequest requestId = %" PRId32 | 
|  | 2152 | ", frameNumber = %" PRId64 ", burstId = %" PRId32 | 
|  | 2153 | ", partialResultCount = %d", | 
|  | 2154 | __FUNCTION__, request.resultExtras.requestId, | 
|  | 2155 | request.resultExtras.frameNumber, request.resultExtras.burstId, | 
|  | 2156 | result->partial_result); | 
|  | 2157 | // Always update the partial count to the latest one if it's not 0 | 
|  | 2158 | // (buffers only). When framework aggregates adjacent partial results | 
|  | 2159 | // into one, the latest partial count will be used. | 
|  | 2160 | if (result->partial_result != 0) | 
|  | 2161 | request.resultExtras.partialResultCount = result->partial_result; | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2162 |  | 
|  | 2163 | // Check if this result carries only partial metadata | 
| Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2164 | if (mUsePartialResult && result->result != NULL) { | 
|  | 2165 | if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) { | 
|  | 2166 | if (result->partial_result > mNumPartialResults || result->partial_result < 1) { | 
|  | 2167 | SET_ERR("Result is malformed for frame %d: partial_result %u must be  in" | 
|  | 2168 | " the range of [1, %d] when metadata is included in the result", | 
|  | 2169 | frameNumber, result->partial_result, mNumPartialResults); | 
|  | 2170 | return; | 
|  | 2171 | } | 
|  | 2172 | isPartialResult = (result->partial_result < mNumPartialResults); | 
| Zhijun He | 5d76e1a | 2014-07-22 16:08:13 -0700 | [diff] [blame] | 2173 | if (isPartialResult) { | 
|  | 2174 | request.partialResult.collectedResult.append(result->result); | 
|  | 2175 | } | 
| Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2176 | } else { | 
|  | 2177 | camera_metadata_ro_entry_t partialResultEntry; | 
|  | 2178 | res = find_camera_metadata_ro_entry(result->result, | 
|  | 2179 | ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry); | 
|  | 2180 | if (res != NAME_NOT_FOUND && | 
|  | 2181 | partialResultEntry.count > 0 && | 
|  | 2182 | partialResultEntry.data.u8[0] == | 
|  | 2183 | ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) { | 
|  | 2184 | // A partial result. Flag this as such, and collect this | 
|  | 2185 | // set of metadata into the in-flight entry. | 
|  | 2186 | isPartialResult = true; | 
|  | 2187 | request.partialResult.collectedResult.append( | 
|  | 2188 | result->result); | 
|  | 2189 | request.partialResult.collectedResult.erase( | 
|  | 2190 | ANDROID_QUIRKS_PARTIAL_RESULT); | 
|  | 2191 | } | 
|  | 2192 | } | 
|  | 2193 |  | 
|  | 2194 | if (isPartialResult) { | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2195 | // Fire off a 3A-only result if possible | 
| Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2196 | if (!request.partialResult.haveSent3A) { | 
|  | 2197 | request.partialResult.haveSent3A = | 
|  | 2198 | processPartial3AResult(frameNumber, | 
|  | 2199 | request.partialResult.collectedResult, | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2200 | request.resultExtras); | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2201 | } | 
|  | 2202 | } | 
|  | 2203 | } | 
|  | 2204 |  | 
| Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2205 | shutterTimestamp = request.shutterTimestamp; | 
| Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2206 | hasInputBufferInRequest = request.hasInputBuffer; | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2207 |  | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2208 | // Did we get the (final) result metadata for this capture? | 
| Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2209 | if (result->result != NULL && !isPartialResult) { | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2210 | if (request.haveResultMetadata) { | 
|  | 2211 | SET_ERR("Called multiple times with metadata for frame %d", | 
|  | 2212 | frameNumber); | 
|  | 2213 | return; | 
|  | 2214 | } | 
| Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2215 | if (mUsePartialResult && | 
|  | 2216 | !request.partialResult.collectedResult.isEmpty()) { | 
|  | 2217 | collectedPartialResult.acquire( | 
|  | 2218 | request.partialResult.collectedResult); | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2219 | } | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2220 | request.haveResultMetadata = true; | 
|  | 2221 | } | 
|  | 2222 |  | 
| Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2223 | uint32_t numBuffersReturned = result->num_output_buffers; | 
|  | 2224 | if (result->input_buffer != NULL) { | 
|  | 2225 | if (hasInputBufferInRequest) { | 
|  | 2226 | numBuffersReturned += 1; | 
|  | 2227 | } else { | 
|  | 2228 | ALOGW("%s: Input buffer should be NULL if there is no input" | 
|  | 2229 | " buffer sent in the request", | 
|  | 2230 | __FUNCTION__); | 
|  | 2231 | } | 
|  | 2232 | } | 
|  | 2233 | request.numBuffersLeft -= numBuffersReturned; | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2234 | if (request.numBuffersLeft < 0) { | 
|  | 2235 | SET_ERR("Too many buffers returned for frame %d", | 
|  | 2236 | frameNumber); | 
|  | 2237 | return; | 
|  | 2238 | } | 
|  | 2239 |  | 
| Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2240 | camera_metadata_ro_entry_t entry; | 
|  | 2241 | res = find_camera_metadata_ro_entry(result->result, | 
|  | 2242 | ANDROID_SENSOR_TIMESTAMP, &entry); | 
|  | 2243 | if (res == OK && entry.count == 1) { | 
|  | 2244 | request.sensorTimestamp = entry.data.i64[0]; | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2245 | } | 
|  | 2246 |  | 
| Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2247 | // If shutter event isn't received yet, append the output buffers to | 
|  | 2248 | // the in-flight request. Otherwise, return the output buffers to | 
|  | 2249 | // streams. | 
|  | 2250 | if (shutterTimestamp == 0) { | 
|  | 2251 | request.pendingOutputBuffers.appendArray(result->output_buffers, | 
|  | 2252 | result->num_output_buffers); | 
| Igor Murashkin | d2c9069 | 2013-04-02 12:32:32 -0700 | [diff] [blame] | 2253 | } else { | 
| Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2254 | returnOutputBuffers(result->output_buffers, | 
|  | 2255 | result->num_output_buffers, shutterTimestamp); | 
| Igor Murashkin | d2c9069 | 2013-04-02 12:32:32 -0700 | [diff] [blame] | 2256 | } | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2257 |  | 
| Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2258 | if (result->result != NULL && !isPartialResult) { | 
|  | 2259 | if (shutterTimestamp == 0) { | 
|  | 2260 | request.pendingMetadata = result->result; | 
|  | 2261 | request.partialResult.collectedResult = collectedPartialResult; | 
|  | 2262 | } else { | 
|  | 2263 | CameraMetadata metadata; | 
|  | 2264 | metadata = result->result; | 
|  | 2265 | sendCaptureResult(metadata, request.resultExtras, | 
| Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 2266 | collectedPartialResult, frameNumber, hasInputBufferInRequest); | 
| Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2267 | } | 
| Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2268 | } | 
|  | 2269 |  | 
| Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2270 | removeInFlightRequestIfReadyLocked(idx); | 
|  | 2271 | } // scope for mInFlightLock | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2272 |  | 
| Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2273 | if (result->input_buffer != NULL) { | 
| Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2274 | if (hasInputBufferInRequest) { | 
|  | 2275 | Camera3Stream *stream = | 
|  | 2276 | Camera3Stream::cast(result->input_buffer->stream); | 
|  | 2277 | res = stream->returnInputBuffer(*(result->input_buffer)); | 
|  | 2278 | // Note: stream may be deallocated at this point, if this buffer was the | 
|  | 2279 | // last reference to it. | 
|  | 2280 | if (res != OK) { | 
|  | 2281 | ALOGE("%s: RequestThread: Can't return input buffer for frame %d to" | 
|  | 2282 | "  its stream:%s (%d)",  __FUNCTION__, | 
|  | 2283 | frameNumber, strerror(-res), res); | 
| Zhijun He | 0ea8fa4 | 2014-07-07 17:05:38 -0700 | [diff] [blame] | 2284 | } | 
|  | 2285 | } else { | 
|  | 2286 | ALOGW("%s: Input buffer should be NULL if there is no input" | 
|  | 2287 | " buffer sent in the request, skipping input buffer return.", | 
|  | 2288 | __FUNCTION__); | 
| Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2289 | } | 
|  | 2290 | } | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2291 | } | 
|  | 2292 |  | 
|  | 2293 | void Camera3Device::notify(const camera3_notify_msg *msg) { | 
| Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 2294 | ATRACE_CALL(); | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2295 | NotificationListener *listener; | 
|  | 2296 | { | 
|  | 2297 | Mutex::Autolock l(mOutputLock); | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2298 | listener = mListener; | 
|  | 2299 | } | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2300 |  | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2301 | if (msg == NULL) { | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2302 | SET_ERR("HAL sent NULL notify message!"); | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2303 | return; | 
|  | 2304 | } | 
|  | 2305 |  | 
|  | 2306 | switch (msg->type) { | 
|  | 2307 | case CAMERA3_MSG_ERROR: { | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2308 | notifyError(msg->message.error, listener); | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2309 | break; | 
|  | 2310 | } | 
|  | 2311 | case CAMERA3_MSG_SHUTTER: { | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2312 | notifyShutter(msg->message.shutter, listener); | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2313 | break; | 
|  | 2314 | } | 
|  | 2315 | default: | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2316 | SET_ERR("Unknown notify message from HAL: %d", | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 2317 | msg->type); | 
| Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2318 | } | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2319 | } | 
|  | 2320 |  | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2321 | void Camera3Device::notifyError(const camera3_error_msg_t &msg, | 
|  | 2322 | NotificationListener *listener) { | 
|  | 2323 |  | 
|  | 2324 | // Map camera HAL error codes to ICameraDeviceCallback error codes | 
|  | 2325 | // Index into this with the HAL error code | 
|  | 2326 | static const ICameraDeviceCallbacks::CameraErrorCode | 
|  | 2327 | halErrorMap[CAMERA3_MSG_NUM_ERRORS] = { | 
|  | 2328 | // 0 = Unused error code | 
|  | 2329 | ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR, | 
|  | 2330 | // 1 = CAMERA3_MSG_ERROR_DEVICE | 
|  | 2331 | ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE, | 
|  | 2332 | // 2 = CAMERA3_MSG_ERROR_REQUEST | 
|  | 2333 | ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, | 
|  | 2334 | // 3 = CAMERA3_MSG_ERROR_RESULT | 
|  | 2335 | ICameraDeviceCallbacks::ERROR_CAMERA_RESULT, | 
|  | 2336 | // 4 = CAMERA3_MSG_ERROR_BUFFER | 
|  | 2337 | ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER | 
|  | 2338 | }; | 
|  | 2339 |  | 
|  | 2340 | ICameraDeviceCallbacks::CameraErrorCode errorCode = | 
|  | 2341 | ((msg.error_code >= 0) && | 
|  | 2342 | (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ? | 
|  | 2343 | halErrorMap[msg.error_code] : | 
|  | 2344 | ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR; | 
|  | 2345 |  | 
|  | 2346 | int streamId = 0; | 
|  | 2347 | if (msg.error_stream != NULL) { | 
|  | 2348 | Camera3Stream *stream = | 
|  | 2349 | Camera3Stream::cast(msg.error_stream); | 
|  | 2350 | streamId = stream->getId(); | 
|  | 2351 | } | 
|  | 2352 | ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d", | 
|  | 2353 | mId, __FUNCTION__, msg.frame_number, | 
|  | 2354 | streamId, msg.error_code); | 
|  | 2355 |  | 
|  | 2356 | CaptureResultExtras resultExtras; | 
|  | 2357 | switch (errorCode) { | 
|  | 2358 | case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE: | 
|  | 2359 | // SET_ERR calls notifyError | 
|  | 2360 | SET_ERR("Camera HAL reported serious device error"); | 
|  | 2361 | break; | 
|  | 2362 | case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST: | 
|  | 2363 | case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT: | 
|  | 2364 | case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER: | 
|  | 2365 | { | 
|  | 2366 | Mutex::Autolock l(mInFlightLock); | 
|  | 2367 | ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number); | 
|  | 2368 | if (idx >= 0) { | 
|  | 2369 | InFlightRequest &r = mInFlightMap.editValueAt(idx); | 
|  | 2370 | r.requestStatus = msg.error_code; | 
|  | 2371 | resultExtras = r.resultExtras; | 
|  | 2372 | } else { | 
|  | 2373 | resultExtras.frameNumber = msg.frame_number; | 
|  | 2374 | ALOGE("Camera %d: %s: cannot find in-flight request on " | 
|  | 2375 | "frame %" PRId64 " error", mId, __FUNCTION__, | 
|  | 2376 | resultExtras.frameNumber); | 
|  | 2377 | } | 
|  | 2378 | } | 
|  | 2379 | if (listener != NULL) { | 
|  | 2380 | listener->notifyError(errorCode, resultExtras); | 
|  | 2381 | } else { | 
|  | 2382 | ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__); | 
|  | 2383 | } | 
|  | 2384 | break; | 
|  | 2385 | default: | 
|  | 2386 | // SET_ERR calls notifyError | 
|  | 2387 | SET_ERR("Unknown error message from HAL: %d", msg.error_code); | 
|  | 2388 | break; | 
|  | 2389 | } | 
|  | 2390 | } | 
|  | 2391 |  | 
|  | 2392 | void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg, | 
|  | 2393 | NotificationListener *listener) { | 
|  | 2394 | ssize_t idx; | 
|  | 2395 | // Verify ordering of shutter notifications | 
|  | 2396 | { | 
|  | 2397 | Mutex::Autolock l(mOutputLock); | 
|  | 2398 | // TODO: need to track errors for tighter bounds on expected frame number. | 
|  | 2399 | if (msg.frame_number < mNextShutterFrameNumber) { | 
|  | 2400 | SET_ERR("Shutter notification out-of-order. Expected " | 
|  | 2401 | "notification for frame %d, got frame %d", | 
|  | 2402 | mNextShutterFrameNumber, msg.frame_number); | 
|  | 2403 | return; | 
|  | 2404 | } | 
|  | 2405 | mNextShutterFrameNumber = msg.frame_number + 1; | 
|  | 2406 | } | 
|  | 2407 |  | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2408 | // Set timestamp for the request in the in-flight tracking | 
|  | 2409 | // and get the request ID to send upstream | 
|  | 2410 | { | 
|  | 2411 | Mutex::Autolock l(mInFlightLock); | 
|  | 2412 | idx = mInFlightMap.indexOfKey(msg.frame_number); | 
|  | 2413 | if (idx >= 0) { | 
|  | 2414 | InFlightRequest &r = mInFlightMap.editValueAt(idx); | 
| Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2415 |  | 
|  | 2416 | ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64, | 
|  | 2417 | mId, __FUNCTION__, | 
|  | 2418 | msg.frame_number, r.resultExtras.requestId, msg.timestamp); | 
|  | 2419 | // Call listener, if any | 
|  | 2420 | if (listener != NULL) { | 
|  | 2421 | listener->notifyShutter(r.resultExtras, msg.timestamp); | 
|  | 2422 | } | 
|  | 2423 |  | 
|  | 2424 | r.shutterTimestamp = msg.timestamp; | 
|  | 2425 |  | 
|  | 2426 | // send pending result and buffers | 
|  | 2427 | sendCaptureResult(r.pendingMetadata, r.resultExtras, | 
| Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 2428 | r.partialResult.collectedResult, msg.frame_number, | 
|  | 2429 | r.hasInputBuffer); | 
| Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2430 | returnOutputBuffers(r.pendingOutputBuffers.array(), | 
|  | 2431 | r.pendingOutputBuffers.size(), r.shutterTimestamp); | 
|  | 2432 | r.pendingOutputBuffers.clear(); | 
|  | 2433 |  | 
|  | 2434 | removeInFlightRequestIfReadyLocked(idx); | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2435 | } | 
|  | 2436 | } | 
|  | 2437 | if (idx < 0) { | 
|  | 2438 | SET_ERR("Shutter notification for non-existent frame number %d", | 
|  | 2439 | msg.frame_number); | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2440 | } | 
|  | 2441 | } | 
|  | 2442 |  | 
|  | 2443 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2444 | CameraMetadata Camera3Device::getLatestRequestLocked() { | 
| Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 2445 | ALOGV("%s", __FUNCTION__); | 
|  | 2446 |  | 
| Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 2447 | CameraMetadata retVal; | 
|  | 2448 |  | 
|  | 2449 | if (mRequestThread != NULL) { | 
|  | 2450 | retVal = mRequestThread->getLatestRequest(); | 
|  | 2451 | } | 
|  | 2452 |  | 
| Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 2453 | return retVal; | 
|  | 2454 | } | 
|  | 2455 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2456 |  | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2457 | /** | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2458 | * RequestThread inner class methods | 
|  | 2459 | */ | 
|  | 2460 |  | 
|  | 2461 | Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent, | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2462 | sp<StatusTracker> statusTracker, | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2463 | camera3_device_t *hal3Device) : | 
| Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 2464 | Thread(/*canCallJava*/false), | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2465 | mParent(parent), | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2466 | mStatusTracker(statusTracker), | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2467 | mHal3Device(hal3Device), | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2468 | mId(getId(parent)), | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2469 | mReconfigured(false), | 
|  | 2470 | mDoPause(false), | 
|  | 2471 | mPaused(true), | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2472 | mFrameNumber(0), | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2473 | mLatestRequestId(NAME_NOT_FOUND), | 
| Yin-Chia Yeh | c00a25c | 2014-08-21 14:27:44 -0700 | [diff] [blame] | 2474 | mCurrentAfTriggerId(0), | 
|  | 2475 | mCurrentPreCaptureTriggerId(0), | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2476 | mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) { | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2477 | mStatusId = statusTracker->addComponent(); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2478 | } | 
|  | 2479 |  | 
| Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 2480 | void Camera3Device::RequestThread::setNotificationListener( | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2481 | NotificationListener *listener) { | 
|  | 2482 | Mutex::Autolock l(mRequestLock); | 
|  | 2483 | mListener = listener; | 
|  | 2484 | } | 
|  | 2485 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2486 | void Camera3Device::RequestThread::configurationComplete() { | 
|  | 2487 | Mutex::Autolock l(mRequestLock); | 
|  | 2488 | mReconfigured = true; | 
|  | 2489 | } | 
|  | 2490 |  | 
| Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2491 | status_t Camera3Device::RequestThread::queueRequestList( | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2492 | List<sp<CaptureRequest> > &requests, | 
|  | 2493 | /*out*/ | 
|  | 2494 | int64_t *lastFrameNumber) { | 
| Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2495 | Mutex::Autolock l(mRequestLock); | 
|  | 2496 | for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end(); | 
|  | 2497 | ++it) { | 
|  | 2498 | mRequestQueue.push_back(*it); | 
|  | 2499 | } | 
|  | 2500 |  | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2501 | if (lastFrameNumber != NULL) { | 
|  | 2502 | *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1; | 
|  | 2503 | ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".", | 
|  | 2504 | __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber, | 
|  | 2505 | *lastFrameNumber); | 
|  | 2506 | } | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2507 |  | 
| Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2508 | unpauseForNewRequests(); | 
|  | 2509 |  | 
|  | 2510 | return OK; | 
|  | 2511 | } | 
|  | 2512 |  | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2513 |  | 
|  | 2514 | status_t Camera3Device::RequestThread::queueTrigger( | 
|  | 2515 | RequestTrigger trigger[], | 
|  | 2516 | size_t count) { | 
|  | 2517 |  | 
|  | 2518 | Mutex::Autolock l(mTriggerMutex); | 
|  | 2519 | status_t ret; | 
|  | 2520 |  | 
|  | 2521 | for (size_t i = 0; i < count; ++i) { | 
|  | 2522 | ret = queueTriggerLocked(trigger[i]); | 
|  | 2523 |  | 
|  | 2524 | if (ret != OK) { | 
|  | 2525 | return ret; | 
|  | 2526 | } | 
|  | 2527 | } | 
|  | 2528 |  | 
|  | 2529 | return OK; | 
|  | 2530 | } | 
|  | 2531 |  | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2532 | int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) { | 
|  | 2533 | sp<Camera3Device> d = device.promote(); | 
|  | 2534 | if (d != NULL) return d->mId; | 
|  | 2535 | return 0; | 
|  | 2536 | } | 
|  | 2537 |  | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2538 | status_t Camera3Device::RequestThread::queueTriggerLocked( | 
|  | 2539 | RequestTrigger trigger) { | 
|  | 2540 |  | 
|  | 2541 | uint32_t tag = trigger.metadataTag; | 
|  | 2542 | ssize_t index = mTriggerMap.indexOfKey(tag); | 
|  | 2543 |  | 
|  | 2544 | switch (trigger.getTagType()) { | 
|  | 2545 | case TYPE_BYTE: | 
|  | 2546 | // fall-through | 
|  | 2547 | case TYPE_INT32: | 
|  | 2548 | break; | 
|  | 2549 | default: | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2550 | ALOGE("%s: Type not supported: 0x%x", __FUNCTION__, | 
|  | 2551 | trigger.getTagType()); | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2552 | return INVALID_OPERATION; | 
|  | 2553 | } | 
|  | 2554 |  | 
|  | 2555 | /** | 
|  | 2556 | * Collect only the latest trigger, since we only have 1 field | 
|  | 2557 | * in the request settings per trigger tag, and can't send more than 1 | 
|  | 2558 | * trigger per request. | 
|  | 2559 | */ | 
|  | 2560 | if (index != NAME_NOT_FOUND) { | 
|  | 2561 | mTriggerMap.editValueAt(index) = trigger; | 
|  | 2562 | } else { | 
|  | 2563 | mTriggerMap.add(tag, trigger); | 
|  | 2564 | } | 
|  | 2565 |  | 
|  | 2566 | return OK; | 
|  | 2567 | } | 
|  | 2568 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2569 | status_t Camera3Device::RequestThread::setRepeatingRequests( | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2570 | const RequestList &requests, | 
|  | 2571 | /*out*/ | 
|  | 2572 | int64_t *lastFrameNumber) { | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2573 | Mutex::Autolock l(mRequestLock); | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2574 | if (lastFrameNumber != NULL) { | 
|  | 2575 | *lastFrameNumber = mRepeatingLastFrameNumber; | 
|  | 2576 | } | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2577 | mRepeatingRequests.clear(); | 
|  | 2578 | mRepeatingRequests.insert(mRepeatingRequests.begin(), | 
|  | 2579 | requests.begin(), requests.end()); | 
| Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 2580 |  | 
|  | 2581 | unpauseForNewRequests(); | 
|  | 2582 |  | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2583 | mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES; | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2584 | return OK; | 
|  | 2585 | } | 
|  | 2586 |  | 
| Yin-Chia Yeh | 8684b7f | 2014-06-13 14:53:05 -0700 | [diff] [blame] | 2587 | bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) { | 
|  | 2588 | if (mRepeatingRequests.empty()) { | 
|  | 2589 | return false; | 
|  | 2590 | } | 
|  | 2591 | int32_t requestId = requestIn->mResultExtras.requestId; | 
|  | 2592 | const RequestList &repeatRequests = mRepeatingRequests; | 
|  | 2593 | // All repeating requests are guaranteed to have same id so only check first quest | 
|  | 2594 | const sp<CaptureRequest> firstRequest = *repeatRequests.begin(); | 
|  | 2595 | return (firstRequest->mResultExtras.requestId == requestId); | 
|  | 2596 | } | 
|  | 2597 |  | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2598 | status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) { | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2599 | Mutex::Autolock l(mRequestLock); | 
|  | 2600 | mRepeatingRequests.clear(); | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2601 | if (lastFrameNumber != NULL) { | 
|  | 2602 | *lastFrameNumber = mRepeatingLastFrameNumber; | 
|  | 2603 | } | 
|  | 2604 | mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES; | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2605 | return OK; | 
|  | 2606 | } | 
|  | 2607 |  | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2608 | status_t Camera3Device::RequestThread::clear( | 
|  | 2609 | NotificationListener *listener, | 
|  | 2610 | /*out*/int64_t *lastFrameNumber) { | 
| Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 2611 | Mutex::Autolock l(mRequestLock); | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2612 | ALOGV("RequestThread::%s:", __FUNCTION__); | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2613 |  | 
| Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 2614 | mRepeatingRequests.clear(); | 
| Yin-Chia Yeh | 8684b7f | 2014-06-13 14:53:05 -0700 | [diff] [blame] | 2615 |  | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2616 | // Send errors for all requests pending in the request queue, including | 
|  | 2617 | // pending repeating requests | 
|  | 2618 | if (listener != NULL) { | 
|  | 2619 | for (RequestList::iterator it = mRequestQueue.begin(); | 
|  | 2620 | it != mRequestQueue.end(); ++it) { | 
| Chien-Yu Chen | c2adf48 | 2015-05-27 14:27:49 -0700 | [diff] [blame] | 2621 | // Abort the input buffers for reprocess requests. | 
|  | 2622 | if ((*it)->mInputStream != NULL) { | 
|  | 2623 | camera3_stream_buffer_t inputBuffer; | 
|  | 2624 | status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer); | 
|  | 2625 | if (res != OK) { | 
|  | 2626 | ALOGW("%s: %d: couldn't get input buffer while clearing the request " | 
|  | 2627 | "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res); | 
|  | 2628 | } else { | 
|  | 2629 | res = (*it)->mInputStream->returnInputBuffer(inputBuffer); | 
|  | 2630 | if (res != OK) { | 
|  | 2631 | ALOGE("%s: %d: couldn't return input buffer while clearing the request " | 
|  | 2632 | "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res); | 
|  | 2633 | } | 
|  | 2634 | } | 
|  | 2635 | } | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2636 | // Set the frame number this request would have had, if it | 
|  | 2637 | // had been submitted; this frame number will not be reused. | 
|  | 2638 | // The requestId and burstId fields were set when the request was | 
|  | 2639 | // submitted originally (in convertMetadataListToRequestListLocked) | 
|  | 2640 | (*it)->mResultExtras.frameNumber = mFrameNumber++; | 
|  | 2641 | listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, | 
|  | 2642 | (*it)->mResultExtras); | 
| Yin-Chia Yeh | 8684b7f | 2014-06-13 14:53:05 -0700 | [diff] [blame] | 2643 | } | 
|  | 2644 | } | 
| Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 2645 | mRequestQueue.clear(); | 
|  | 2646 | mTriggerMap.clear(); | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2647 | if (lastFrameNumber != NULL) { | 
|  | 2648 | *lastFrameNumber = mRepeatingLastFrameNumber; | 
|  | 2649 | } | 
|  | 2650 | mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES; | 
| Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 2651 | return OK; | 
|  | 2652 | } | 
|  | 2653 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2654 | void Camera3Device::RequestThread::setPaused(bool paused) { | 
|  | 2655 | Mutex::Autolock l(mPauseLock); | 
|  | 2656 | mDoPause = paused; | 
|  | 2657 | mDoPauseSignal.signal(); | 
|  | 2658 | } | 
|  | 2659 |  | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2660 | status_t Camera3Device::RequestThread::waitUntilRequestProcessed( | 
|  | 2661 | int32_t requestId, nsecs_t timeout) { | 
|  | 2662 | Mutex::Autolock l(mLatestRequestMutex); | 
|  | 2663 | status_t res; | 
|  | 2664 | while (mLatestRequestId != requestId) { | 
|  | 2665 | nsecs_t startTime = systemTime(); | 
|  | 2666 |  | 
|  | 2667 | res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout); | 
|  | 2668 | if (res != OK) return res; | 
|  | 2669 |  | 
|  | 2670 | timeout -= (systemTime() - startTime); | 
|  | 2671 | } | 
|  | 2672 |  | 
|  | 2673 | return OK; | 
|  | 2674 | } | 
|  | 2675 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2676 | void Camera3Device::RequestThread::requestExit() { | 
|  | 2677 | // Call parent to set up shutdown | 
|  | 2678 | Thread::requestExit(); | 
|  | 2679 | // The exit from any possible waits | 
|  | 2680 | mDoPauseSignal.signal(); | 
|  | 2681 | mRequestSignal.signal(); | 
|  | 2682 | } | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2683 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2684 | bool Camera3Device::RequestThread::threadLoop() { | 
|  | 2685 |  | 
|  | 2686 | status_t res; | 
|  | 2687 |  | 
|  | 2688 | // Handle paused state. | 
|  | 2689 | if (waitIfPaused()) { | 
|  | 2690 | return true; | 
|  | 2691 | } | 
|  | 2692 |  | 
|  | 2693 | // Get work to do | 
|  | 2694 |  | 
|  | 2695 | sp<CaptureRequest> nextRequest = waitForNextRequest(); | 
|  | 2696 | if (nextRequest == NULL) { | 
|  | 2697 | return true; | 
|  | 2698 | } | 
|  | 2699 |  | 
|  | 2700 | // Create request to HAL | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2701 | camera3_capture_request_t request = camera3_capture_request_t(); | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2702 | request.frame_number = nextRequest->mResultExtras.frameNumber; | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2703 | Vector<camera3_stream_buffer_t> outputBuffers; | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2704 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2705 | // Get the request ID, if any | 
|  | 2706 | int requestId; | 
|  | 2707 | camera_metadata_entry_t requestIdEntry = | 
|  | 2708 | nextRequest->mSettings.find(ANDROID_REQUEST_ID); | 
|  | 2709 | if (requestIdEntry.count > 0) { | 
|  | 2710 | requestId = requestIdEntry.data.i32[0]; | 
|  | 2711 | } else { | 
|  | 2712 | ALOGW("%s: Did not have android.request.id set in the request", | 
|  | 2713 | __FUNCTION__); | 
|  | 2714 | requestId = NAME_NOT_FOUND; | 
|  | 2715 | } | 
|  | 2716 |  | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2717 | // Insert any queued triggers (before metadata is locked) | 
|  | 2718 | int32_t triggerCount; | 
|  | 2719 | res = insertTriggers(nextRequest); | 
|  | 2720 | if (res < 0) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 2721 | SET_ERR("RequestThread: Unable to insert triggers " | 
|  | 2722 | "(capture request %d, HAL device: %s (%d)", | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2723 | request.frame_number, strerror(-res), res); | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2724 | cleanUpFailedRequest(request, nextRequest, outputBuffers); | 
|  | 2725 | return false; | 
|  | 2726 | } | 
|  | 2727 | triggerCount = res; | 
|  | 2728 |  | 
|  | 2729 | bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0); | 
|  | 2730 |  | 
|  | 2731 | // If the request is the same as last, or we had triggers last time | 
|  | 2732 | if (mPrevRequest != nextRequest || triggersMixedIn) { | 
|  | 2733 | /** | 
| Eino-Ville Talvala | 2f876f9 | 2013-09-13 11:39:24 -0700 | [diff] [blame] | 2734 | * HAL workaround: | 
|  | 2735 | * Insert a dummy trigger ID if a trigger is set but no trigger ID is | 
|  | 2736 | */ | 
|  | 2737 | res = addDummyTriggerIds(nextRequest); | 
|  | 2738 | if (res != OK) { | 
|  | 2739 | SET_ERR("RequestThread: Unable to insert dummy trigger IDs " | 
|  | 2740 | "(capture request %d, HAL device: %s (%d)", | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2741 | request.frame_number, strerror(-res), res); | 
| Eino-Ville Talvala | 2f876f9 | 2013-09-13 11:39:24 -0700 | [diff] [blame] | 2742 | cleanUpFailedRequest(request, nextRequest, outputBuffers); | 
|  | 2743 | return false; | 
|  | 2744 | } | 
|  | 2745 |  | 
|  | 2746 | /** | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2747 | * The request should be presorted so accesses in HAL | 
|  | 2748 | *   are O(logn). Sidenote, sorting a sorted metadata is nop. | 
|  | 2749 | */ | 
|  | 2750 | nextRequest->mSettings.sort(); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2751 | request.settings = nextRequest->mSettings.getAndLock(); | 
|  | 2752 | mPrevRequest = nextRequest; | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2753 | ALOGVV("%s: Request settings are NEW", __FUNCTION__); | 
|  | 2754 |  | 
|  | 2755 | IF_ALOGV() { | 
|  | 2756 | camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t(); | 
|  | 2757 | find_camera_metadata_ro_entry( | 
|  | 2758 | request.settings, | 
|  | 2759 | ANDROID_CONTROL_AF_TRIGGER, | 
|  | 2760 | &e | 
|  | 2761 | ); | 
|  | 2762 | if (e.count > 0) { | 
|  | 2763 | ALOGV("%s: Request (frame num %d) had AF trigger 0x%x", | 
|  | 2764 | __FUNCTION__, | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2765 | request.frame_number, | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2766 | e.data.u8[0]); | 
|  | 2767 | } | 
|  | 2768 | } | 
|  | 2769 | } else { | 
|  | 2770 | // leave request.settings NULL to indicate 'reuse latest given' | 
|  | 2771 | ALOGVV("%s: Request settings are REUSED", | 
|  | 2772 | __FUNCTION__); | 
|  | 2773 | } | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2774 |  | 
| Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2775 | uint32_t totalNumBuffers = 0; | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2776 |  | 
|  | 2777 | // Fill in buffers | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2778 | if (nextRequest->mInputStream != NULL) { | 
| Chien-Yu Chen | c2adf48 | 2015-05-27 14:27:49 -0700 | [diff] [blame] | 2779 | request.input_buffer = &nextRequest->mInputBuffer; | 
| Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2780 | totalNumBuffers += 1; | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2781 | } else { | 
|  | 2782 | request.input_buffer = NULL; | 
|  | 2783 | } | 
|  | 2784 |  | 
|  | 2785 | outputBuffers.insertAt(camera3_stream_buffer_t(), 0, | 
|  | 2786 | nextRequest->mOutputStreams.size()); | 
|  | 2787 | request.output_buffers = outputBuffers.array(); | 
|  | 2788 | for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) { | 
|  | 2789 | res = nextRequest->mOutputStreams.editItemAt(i)-> | 
|  | 2790 | getBuffer(&outputBuffers.editItemAt(i)); | 
|  | 2791 | if (res != OK) { | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2792 | // Can't get output buffer from gralloc queue - this could be due to | 
|  | 2793 | // abandoned queue or other consumer misbehavior, so not a fatal | 
|  | 2794 | // error | 
| Eino-Ville Talvala | 07d2169 | 2013-09-24 18:04:19 -0700 | [diff] [blame] | 2795 | ALOGE("RequestThread: Can't get output buffer, skipping request:" | 
|  | 2796 | " %s (%d)", strerror(-res), res); | 
| Eino-Ville Talvala | 0ec23d3 | 2015-05-28 15:15:54 -0700 | [diff] [blame] | 2797 | { | 
|  | 2798 | Mutex::Autolock l(mRequestLock); | 
|  | 2799 | if (mListener != NULL) { | 
|  | 2800 | mListener->notifyError( | 
|  | 2801 | ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, | 
|  | 2802 | nextRequest->mResultExtras); | 
|  | 2803 | } | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2804 | } | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2805 | cleanUpFailedRequest(request, nextRequest, outputBuffers); | 
|  | 2806 | return true; | 
|  | 2807 | } | 
|  | 2808 | request.num_output_buffers++; | 
|  | 2809 | } | 
| Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2810 | totalNumBuffers += request.num_output_buffers; | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2811 |  | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2812 | // Log request in the in-flight queue | 
|  | 2813 | sp<Camera3Device> parent = mParent.promote(); | 
|  | 2814 | if (parent == NULL) { | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2815 | // Should not happen, and nowhere to send errors to, so just log it | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2816 | CLOGE("RequestThread: Parent is gone"); | 
|  | 2817 | cleanUpFailedRequest(request, nextRequest, outputBuffers); | 
|  | 2818 | return false; | 
|  | 2819 | } | 
|  | 2820 |  | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2821 | res = parent->registerInFlight(request.frame_number, | 
| Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2822 | totalNumBuffers, nextRequest->mResultExtras, | 
|  | 2823 | /*hasInput*/request.input_buffer != NULL); | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2824 | ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64 | 
|  | 2825 | ", burstId = %" PRId32 ".", | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2826 | __FUNCTION__, | 
|  | 2827 | nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber, | 
|  | 2828 | nextRequest->mResultExtras.burstId); | 
| Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2829 | if (res != OK) { | 
|  | 2830 | SET_ERR("RequestThread: Unable to register new in-flight request:" | 
|  | 2831 | " %s (%d)", strerror(-res), res); | 
|  | 2832 | cleanUpFailedRequest(request, nextRequest, outputBuffers); | 
|  | 2833 | return false; | 
|  | 2834 | } | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2835 |  | 
| Zhijun He | cc27e11 | 2013-10-03 16:12:43 -0700 | [diff] [blame] | 2836 | // Inform waitUntilRequestProcessed thread of a new request ID | 
|  | 2837 | { | 
|  | 2838 | Mutex::Autolock al(mLatestRequestMutex); | 
|  | 2839 |  | 
|  | 2840 | mLatestRequestId = requestId; | 
|  | 2841 | mLatestRequestSignal.signal(); | 
|  | 2842 | } | 
|  | 2843 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2844 | // Submit request and block until ready for next one | 
| Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 2845 | ATRACE_ASYNC_BEGIN("frame capture", request.frame_number); | 
|  | 2846 | ATRACE_BEGIN("camera3->process_capture_request"); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2847 | res = mHal3Device->ops->process_capture_request(mHal3Device, &request); | 
| Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 2848 | ATRACE_END(); | 
|  | 2849 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2850 | if (res != OK) { | 
| Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2851 | // Should only get a failure here for malformed requests or device-level | 
|  | 2852 | // errors, so consider all errors fatal.  Bad metadata failures should | 
|  | 2853 | // come through notify. | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 2854 | SET_ERR("RequestThread: Unable to submit capture request %d to HAL" | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2855 | " device: %s (%d)", request.frame_number, strerror(-res), res); | 
|  | 2856 | cleanUpFailedRequest(request, nextRequest, outputBuffers); | 
|  | 2857 | return false; | 
|  | 2858 | } | 
|  | 2859 |  | 
| Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 2860 | // Update the latest request sent to HAL | 
|  | 2861 | if (request.settings != NULL) { // Don't update them if they were unchanged | 
|  | 2862 | Mutex::Autolock al(mLatestRequestMutex); | 
|  | 2863 |  | 
|  | 2864 | camera_metadata_t* cloned = clone_camera_metadata(request.settings); | 
|  | 2865 | mLatestRequest.acquire(cloned); | 
|  | 2866 | } | 
|  | 2867 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2868 | if (request.settings != NULL) { | 
|  | 2869 | nextRequest->mSettings.unlock(request.settings); | 
|  | 2870 | } | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2871 |  | 
| Eino-Ville Talvala | e74c228 | 2015-05-27 14:46:23 -0700 | [diff] [blame] | 2872 | // Unset as current request | 
|  | 2873 | { | 
|  | 2874 | Mutex::Autolock l(mRequestLock); | 
|  | 2875 | mNextRequest.clear(); | 
|  | 2876 | } | 
|  | 2877 |  | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2878 | // Remove any previously queued triggers (after unlock) | 
|  | 2879 | res = removeTriggers(mPrevRequest); | 
|  | 2880 | if (res != OK) { | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 2881 | SET_ERR("RequestThread: Unable to remove triggers " | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2882 | "(capture request %d, HAL device: %s (%d)", | 
|  | 2883 | request.frame_number, strerror(-res), res); | 
|  | 2884 | return false; | 
|  | 2885 | } | 
|  | 2886 | mPrevTriggers = triggerCount; | 
|  | 2887 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2888 | return true; | 
|  | 2889 | } | 
|  | 2890 |  | 
| Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 2891 | CameraMetadata Camera3Device::RequestThread::getLatestRequest() const { | 
|  | 2892 | Mutex::Autolock al(mLatestRequestMutex); | 
|  | 2893 |  | 
|  | 2894 | ALOGV("RequestThread::%s", __FUNCTION__); | 
|  | 2895 |  | 
|  | 2896 | return mLatestRequest; | 
|  | 2897 | } | 
|  | 2898 |  | 
| Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 2899 | bool Camera3Device::RequestThread::isStreamPending( | 
|  | 2900 | sp<Camera3StreamInterface>& stream) { | 
|  | 2901 | Mutex::Autolock l(mRequestLock); | 
|  | 2902 |  | 
| Eino-Ville Talvala | e74c228 | 2015-05-27 14:46:23 -0700 | [diff] [blame] | 2903 | if (mNextRequest != nullptr) { | 
|  | 2904 | for (const auto& s : mNextRequest->mOutputStreams) { | 
|  | 2905 | if (stream == s) return true; | 
|  | 2906 | } | 
|  | 2907 | if (stream == mNextRequest->mInputStream) return true; | 
|  | 2908 | } | 
|  | 2909 |  | 
| Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 2910 | for (const auto& request : mRequestQueue) { | 
|  | 2911 | for (const auto& s : request->mOutputStreams) { | 
|  | 2912 | if (stream == s) return true; | 
|  | 2913 | } | 
|  | 2914 | if (stream == request->mInputStream) return true; | 
|  | 2915 | } | 
|  | 2916 |  | 
|  | 2917 | for (const auto& request : mRepeatingRequests) { | 
|  | 2918 | for (const auto& s : request->mOutputStreams) { | 
|  | 2919 | if (stream == s) return true; | 
|  | 2920 | } | 
|  | 2921 | if (stream == request->mInputStream) return true; | 
|  | 2922 | } | 
|  | 2923 |  | 
|  | 2924 | return false; | 
|  | 2925 | } | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2926 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2927 | void Camera3Device::RequestThread::cleanUpFailedRequest( | 
|  | 2928 | camera3_capture_request_t &request, | 
|  | 2929 | sp<CaptureRequest> &nextRequest, | 
|  | 2930 | Vector<camera3_stream_buffer_t> &outputBuffers) { | 
|  | 2931 |  | 
|  | 2932 | if (request.settings != NULL) { | 
|  | 2933 | nextRequest->mSettings.unlock(request.settings); | 
|  | 2934 | } | 
| Chien-Yu Chen | c2adf48 | 2015-05-27 14:27:49 -0700 | [diff] [blame] | 2935 | if (nextRequest->mInputStream != NULL) { | 
|  | 2936 | nextRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR; | 
|  | 2937 | nextRequest->mInputStream->returnInputBuffer(nextRequest->mInputBuffer); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2938 | } | 
|  | 2939 | for (size_t i = 0; i < request.num_output_buffers; i++) { | 
|  | 2940 | outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR; | 
|  | 2941 | nextRequest->mOutputStreams.editItemAt(i)->returnBuffer( | 
|  | 2942 | outputBuffers[i], 0); | 
|  | 2943 | } | 
| Eino-Ville Talvala | e74c228 | 2015-05-27 14:46:23 -0700 | [diff] [blame] | 2944 |  | 
|  | 2945 | Mutex::Autolock l(mRequestLock); | 
|  | 2946 | mNextRequest.clear(); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2947 | } | 
|  | 2948 |  | 
|  | 2949 | sp<Camera3Device::CaptureRequest> | 
|  | 2950 | Camera3Device::RequestThread::waitForNextRequest() { | 
|  | 2951 | status_t res; | 
|  | 2952 | sp<CaptureRequest> nextRequest; | 
|  | 2953 |  | 
|  | 2954 | // Optimized a bit for the simple steady-state case (single repeating | 
|  | 2955 | // request), to avoid putting that request in the queue temporarily. | 
|  | 2956 | Mutex::Autolock l(mRequestLock); | 
|  | 2957 |  | 
|  | 2958 | while (mRequestQueue.empty()) { | 
|  | 2959 | if (!mRepeatingRequests.empty()) { | 
|  | 2960 | // Always atomically enqueue all requests in a repeating request | 
|  | 2961 | // list. Guarantees a complete in-sequence set of captures to | 
|  | 2962 | // application. | 
|  | 2963 | const RequestList &requests = mRepeatingRequests; | 
|  | 2964 | RequestList::const_iterator firstRequest = | 
|  | 2965 | requests.begin(); | 
|  | 2966 | nextRequest = *firstRequest; | 
|  | 2967 | mRequestQueue.insert(mRequestQueue.end(), | 
|  | 2968 | ++firstRequest, | 
|  | 2969 | requests.end()); | 
|  | 2970 | // No need to wait any longer | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2971 |  | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2972 | mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1; | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2973 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2974 | break; | 
|  | 2975 | } | 
|  | 2976 |  | 
|  | 2977 | res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout); | 
|  | 2978 |  | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2979 | if ((mRequestQueue.empty() && mRepeatingRequests.empty()) || | 
|  | 2980 | exitPending()) { | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2981 | Mutex::Autolock pl(mPauseLock); | 
|  | 2982 | if (mPaused == false) { | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2983 | ALOGV("%s: RequestThread: Going idle", __FUNCTION__); | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2984 | mPaused = true; | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2985 | // Let the tracker know | 
|  | 2986 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); | 
|  | 2987 | if (statusTracker != 0) { | 
|  | 2988 | statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE); | 
|  | 2989 | } | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2990 | } | 
|  | 2991 | // Stop waiting for now and let thread management happen | 
|  | 2992 | return NULL; | 
|  | 2993 | } | 
|  | 2994 | } | 
|  | 2995 |  | 
|  | 2996 | if (nextRequest == NULL) { | 
|  | 2997 | // Don't have a repeating request already in hand, so queue | 
|  | 2998 | // must have an entry now. | 
|  | 2999 | RequestList::iterator firstRequest = | 
|  | 3000 | mRequestQueue.begin(); | 
|  | 3001 | nextRequest = *firstRequest; | 
|  | 3002 | mRequestQueue.erase(firstRequest); | 
|  | 3003 | } | 
|  | 3004 |  | 
| Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 3005 | // In case we've been unpaused by setPaused clearing mDoPause, need to | 
|  | 3006 | // update internal pause state (capture/setRepeatingRequest unpause | 
|  | 3007 | // directly). | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3008 | Mutex::Autolock pl(mPauseLock); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3009 | if (mPaused) { | 
|  | 3010 | ALOGV("%s: RequestThread: Unpaused", __FUNCTION__); | 
|  | 3011 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); | 
|  | 3012 | if (statusTracker != 0) { | 
|  | 3013 | statusTracker->markComponentActive(mStatusId); | 
|  | 3014 | } | 
|  | 3015 | } | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3016 | mPaused = false; | 
|  | 3017 |  | 
|  | 3018 | // Check if we've reconfigured since last time, and reset the preview | 
|  | 3019 | // request if so. Can't use 'NULL request == repeat' across configure calls. | 
|  | 3020 | if (mReconfigured) { | 
|  | 3021 | mPrevRequest.clear(); | 
|  | 3022 | mReconfigured = false; | 
|  | 3023 | } | 
|  | 3024 |  | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 3025 | if (nextRequest != NULL) { | 
|  | 3026 | nextRequest->mResultExtras.frameNumber = mFrameNumber++; | 
| Yin-Chia Yeh | c00a25c | 2014-08-21 14:27:44 -0700 | [diff] [blame] | 3027 | nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId; | 
|  | 3028 | nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId; | 
| Chien-Yu Chen | c2adf48 | 2015-05-27 14:27:49 -0700 | [diff] [blame] | 3029 |  | 
|  | 3030 | // Since RequestThread::clear() removes buffers from the input stream, | 
|  | 3031 | // get the right buffer here before unlocking mRequestLock | 
|  | 3032 | if (nextRequest->mInputStream != NULL) { | 
|  | 3033 | res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer); | 
|  | 3034 | if (res != OK) { | 
|  | 3035 | // Can't get input buffer from gralloc queue - this could be due to | 
|  | 3036 | // disconnected queue or other producer misbehavior, so not a fatal | 
|  | 3037 | // error | 
|  | 3038 | ALOGE("%s: Can't get input buffer, skipping request:" | 
|  | 3039 | " %s (%d)", __FUNCTION__, strerror(-res), res); | 
|  | 3040 | if (mListener != NULL) { | 
|  | 3041 | mListener->notifyError( | 
|  | 3042 | ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, | 
|  | 3043 | nextRequest->mResultExtras); | 
|  | 3044 | } | 
|  | 3045 | return NULL; | 
|  | 3046 | } | 
|  | 3047 | } | 
| Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 3048 | } | 
| Eino-Ville Talvala | e74c228 | 2015-05-27 14:46:23 -0700 | [diff] [blame] | 3049 | mNextRequest = nextRequest; | 
|  | 3050 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3051 | return nextRequest; | 
|  | 3052 | } | 
|  | 3053 |  | 
|  | 3054 | bool Camera3Device::RequestThread::waitIfPaused() { | 
|  | 3055 | status_t res; | 
|  | 3056 | Mutex::Autolock l(mPauseLock); | 
|  | 3057 | while (mDoPause) { | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3058 | if (mPaused == false) { | 
|  | 3059 | mPaused = true; | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3060 | ALOGV("%s: RequestThread: Paused", __FUNCTION__); | 
|  | 3061 | // Let the tracker know | 
|  | 3062 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); | 
|  | 3063 | if (statusTracker != 0) { | 
|  | 3064 | statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE); | 
|  | 3065 | } | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3066 | } | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3067 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3068 | res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout); | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3069 | if (res == TIMED_OUT || exitPending()) { | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3070 | return true; | 
|  | 3071 | } | 
|  | 3072 | } | 
|  | 3073 | // We don't set mPaused to false here, because waitForNextRequest needs | 
|  | 3074 | // to further manage the paused state in case of starvation. | 
|  | 3075 | return false; | 
|  | 3076 | } | 
|  | 3077 |  | 
| Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 3078 | void Camera3Device::RequestThread::unpauseForNewRequests() { | 
|  | 3079 | // With work to do, mark thread as unpaused. | 
|  | 3080 | // If paused by request (setPaused), don't resume, to avoid | 
|  | 3081 | // extra signaling/waiting overhead to waitUntilPaused | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3082 | mRequestSignal.signal(); | 
| Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 3083 | Mutex::Autolock p(mPauseLock); | 
|  | 3084 | if (!mDoPause) { | 
| Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3085 | ALOGV("%s: RequestThread: Going active", __FUNCTION__); | 
|  | 3086 | if (mPaused) { | 
|  | 3087 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); | 
|  | 3088 | if (statusTracker != 0) { | 
|  | 3089 | statusTracker->markComponentActive(mStatusId); | 
|  | 3090 | } | 
|  | 3091 | } | 
| Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 3092 | mPaused = false; | 
|  | 3093 | } | 
|  | 3094 | } | 
|  | 3095 |  | 
| Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 3096 | void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) { | 
|  | 3097 | sp<Camera3Device> parent = mParent.promote(); | 
|  | 3098 | if (parent != NULL) { | 
|  | 3099 | va_list args; | 
|  | 3100 | va_start(args, fmt); | 
|  | 3101 |  | 
|  | 3102 | parent->setErrorStateV(fmt, args); | 
|  | 3103 |  | 
|  | 3104 | va_end(args); | 
|  | 3105 | } | 
|  | 3106 | } | 
|  | 3107 |  | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3108 | status_t Camera3Device::RequestThread::insertTriggers( | 
|  | 3109 | const sp<CaptureRequest> &request) { | 
|  | 3110 |  | 
|  | 3111 | Mutex::Autolock al(mTriggerMutex); | 
|  | 3112 |  | 
| Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 3113 | sp<Camera3Device> parent = mParent.promote(); | 
|  | 3114 | if (parent == NULL) { | 
|  | 3115 | CLOGE("RequestThread: Parent is gone"); | 
|  | 3116 | return DEAD_OBJECT; | 
|  | 3117 | } | 
|  | 3118 |  | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3119 | CameraMetadata &metadata = request->mSettings; | 
|  | 3120 | size_t count = mTriggerMap.size(); | 
|  | 3121 |  | 
|  | 3122 | for (size_t i = 0; i < count; ++i) { | 
|  | 3123 | RequestTrigger trigger = mTriggerMap.valueAt(i); | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3124 | uint32_t tag = trigger.metadataTag; | 
| Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 3125 |  | 
|  | 3126 | if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) { | 
|  | 3127 | bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID); | 
|  | 3128 | uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue); | 
| Yin-Chia Yeh | c00a25c | 2014-08-21 14:27:44 -0700 | [diff] [blame] | 3129 | if (isAeTrigger) { | 
|  | 3130 | request->mResultExtras.precaptureTriggerId = triggerId; | 
|  | 3131 | mCurrentPreCaptureTriggerId = triggerId; | 
|  | 3132 | } else { | 
|  | 3133 | request->mResultExtras.afTriggerId = triggerId; | 
|  | 3134 | mCurrentAfTriggerId = triggerId; | 
|  | 3135 | } | 
| Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 3136 | if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) { | 
|  | 3137 | continue; // Trigger ID tag is deprecated since device HAL 3.2 | 
|  | 3138 | } | 
|  | 3139 | } | 
|  | 3140 |  | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3141 | camera_metadata_entry entry = metadata.find(tag); | 
|  | 3142 |  | 
|  | 3143 | if (entry.count > 0) { | 
|  | 3144 | /** | 
|  | 3145 | * Already has an entry for this trigger in the request. | 
|  | 3146 | * Rewrite it with our requested trigger value. | 
|  | 3147 | */ | 
|  | 3148 | RequestTrigger oldTrigger = trigger; | 
|  | 3149 |  | 
|  | 3150 | oldTrigger.entryValue = entry.data.u8[0]; | 
|  | 3151 |  | 
|  | 3152 | mTriggerReplacedMap.add(tag, oldTrigger); | 
|  | 3153 | } else { | 
|  | 3154 | /** | 
|  | 3155 | * More typical, no trigger entry, so we just add it | 
|  | 3156 | */ | 
|  | 3157 | mTriggerRemovedMap.add(tag, trigger); | 
|  | 3158 | } | 
|  | 3159 |  | 
|  | 3160 | status_t res; | 
|  | 3161 |  | 
|  | 3162 | switch (trigger.getTagType()) { | 
|  | 3163 | case TYPE_BYTE: { | 
|  | 3164 | uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue); | 
|  | 3165 | res = metadata.update(tag, | 
|  | 3166 | &entryValue, | 
|  | 3167 | /*count*/1); | 
|  | 3168 | break; | 
|  | 3169 | } | 
|  | 3170 | case TYPE_INT32: | 
|  | 3171 | res = metadata.update(tag, | 
|  | 3172 | &trigger.entryValue, | 
|  | 3173 | /*count*/1); | 
|  | 3174 | break; | 
|  | 3175 | default: | 
|  | 3176 | ALOGE("%s: Type not supported: 0x%x", | 
|  | 3177 | __FUNCTION__, | 
|  | 3178 | trigger.getTagType()); | 
|  | 3179 | return INVALID_OPERATION; | 
|  | 3180 | } | 
|  | 3181 |  | 
|  | 3182 | if (res != OK) { | 
|  | 3183 | ALOGE("%s: Failed to update request metadata with trigger tag %s" | 
|  | 3184 | ", value %d", __FUNCTION__, trigger.getTagName(), | 
|  | 3185 | trigger.entryValue); | 
|  | 3186 | return res; | 
|  | 3187 | } | 
|  | 3188 |  | 
|  | 3189 | ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__, | 
|  | 3190 | trigger.getTagName(), | 
|  | 3191 | trigger.entryValue); | 
|  | 3192 | } | 
|  | 3193 |  | 
|  | 3194 | mTriggerMap.clear(); | 
|  | 3195 |  | 
|  | 3196 | return count; | 
|  | 3197 | } | 
|  | 3198 |  | 
|  | 3199 | status_t Camera3Device::RequestThread::removeTriggers( | 
|  | 3200 | const sp<CaptureRequest> &request) { | 
|  | 3201 | Mutex::Autolock al(mTriggerMutex); | 
|  | 3202 |  | 
|  | 3203 | CameraMetadata &metadata = request->mSettings; | 
|  | 3204 |  | 
|  | 3205 | /** | 
|  | 3206 | * Replace all old entries with their old values. | 
|  | 3207 | */ | 
|  | 3208 | for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) { | 
|  | 3209 | RequestTrigger trigger = mTriggerReplacedMap.valueAt(i); | 
|  | 3210 |  | 
|  | 3211 | status_t res; | 
|  | 3212 |  | 
|  | 3213 | uint32_t tag = trigger.metadataTag; | 
|  | 3214 | switch (trigger.getTagType()) { | 
|  | 3215 | case TYPE_BYTE: { | 
|  | 3216 | uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue); | 
|  | 3217 | res = metadata.update(tag, | 
|  | 3218 | &entryValue, | 
|  | 3219 | /*count*/1); | 
|  | 3220 | break; | 
|  | 3221 | } | 
|  | 3222 | case TYPE_INT32: | 
|  | 3223 | res = metadata.update(tag, | 
|  | 3224 | &trigger.entryValue, | 
|  | 3225 | /*count*/1); | 
|  | 3226 | break; | 
|  | 3227 | default: | 
|  | 3228 | ALOGE("%s: Type not supported: 0x%x", | 
|  | 3229 | __FUNCTION__, | 
|  | 3230 | trigger.getTagType()); | 
|  | 3231 | return INVALID_OPERATION; | 
|  | 3232 | } | 
|  | 3233 |  | 
|  | 3234 | if (res != OK) { | 
|  | 3235 | ALOGE("%s: Failed to restore request metadata with trigger tag %s" | 
|  | 3236 | ", trigger value %d", __FUNCTION__, | 
|  | 3237 | trigger.getTagName(), trigger.entryValue); | 
|  | 3238 | return res; | 
|  | 3239 | } | 
|  | 3240 | } | 
|  | 3241 | mTriggerReplacedMap.clear(); | 
|  | 3242 |  | 
|  | 3243 | /** | 
|  | 3244 | * Remove all new entries. | 
|  | 3245 | */ | 
|  | 3246 | for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) { | 
|  | 3247 | RequestTrigger trigger = mTriggerRemovedMap.valueAt(i); | 
|  | 3248 | status_t res = metadata.erase(trigger.metadataTag); | 
|  | 3249 |  | 
|  | 3250 | if (res != OK) { | 
|  | 3251 | ALOGE("%s: Failed to erase metadata with trigger tag %s" | 
|  | 3252 | ", trigger value %d", __FUNCTION__, | 
|  | 3253 | trigger.getTagName(), trigger.entryValue); | 
|  | 3254 | return res; | 
|  | 3255 | } | 
|  | 3256 | } | 
|  | 3257 | mTriggerRemovedMap.clear(); | 
|  | 3258 |  | 
|  | 3259 | return OK; | 
|  | 3260 | } | 
|  | 3261 |  | 
| Eino-Ville Talvala | 2f876f9 | 2013-09-13 11:39:24 -0700 | [diff] [blame] | 3262 | status_t Camera3Device::RequestThread::addDummyTriggerIds( | 
|  | 3263 | const sp<CaptureRequest> &request) { | 
|  | 3264 | // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here | 
|  | 3265 | static const int32_t dummyTriggerId = 1; | 
|  | 3266 | status_t res; | 
|  | 3267 |  | 
|  | 3268 | CameraMetadata &metadata = request->mSettings; | 
|  | 3269 |  | 
|  | 3270 | // If AF trigger is active, insert a dummy AF trigger ID if none already | 
|  | 3271 | // exists | 
|  | 3272 | camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER); | 
|  | 3273 | camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID); | 
|  | 3274 | if (afTrigger.count > 0 && | 
|  | 3275 | afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE && | 
|  | 3276 | afId.count == 0) { | 
|  | 3277 | res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1); | 
|  | 3278 | if (res != OK) return res; | 
|  | 3279 | } | 
|  | 3280 |  | 
|  | 3281 | // If AE precapture trigger is active, insert a dummy precapture trigger ID | 
|  | 3282 | // if none already exists | 
|  | 3283 | camera_metadata_entry pcTrigger = | 
|  | 3284 | metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER); | 
|  | 3285 | camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID); | 
|  | 3286 | if (pcTrigger.count > 0 && | 
|  | 3287 | pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE && | 
|  | 3288 | pcId.count == 0) { | 
|  | 3289 | res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID, | 
|  | 3290 | &dummyTriggerId, 1); | 
|  | 3291 | if (res != OK) return res; | 
|  | 3292 | } | 
|  | 3293 |  | 
|  | 3294 | return OK; | 
|  | 3295 | } | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3296 |  | 
| Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 3297 | /** | 
|  | 3298 | * PreparerThread inner class methods | 
|  | 3299 | */ | 
|  | 3300 |  | 
|  | 3301 | Camera3Device::PreparerThread::PreparerThread() : | 
|  | 3302 | Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) { | 
|  | 3303 | } | 
|  | 3304 |  | 
|  | 3305 | Camera3Device::PreparerThread::~PreparerThread() { | 
|  | 3306 | Thread::requestExitAndWait(); | 
|  | 3307 | if (mCurrentStream != nullptr) { | 
|  | 3308 | mCurrentStream->cancelPrepare(); | 
|  | 3309 | ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId()); | 
|  | 3310 | mCurrentStream.clear(); | 
|  | 3311 | } | 
|  | 3312 | clear(); | 
|  | 3313 | } | 
|  | 3314 |  | 
|  | 3315 | status_t Camera3Device::PreparerThread::prepare(sp<Camera3StreamInterface>& stream) { | 
|  | 3316 | status_t res; | 
|  | 3317 |  | 
|  | 3318 | Mutex::Autolock l(mLock); | 
|  | 3319 |  | 
|  | 3320 | res = stream->startPrepare(); | 
|  | 3321 | if (res == OK) { | 
|  | 3322 | // No preparation needed, fire listener right off | 
|  | 3323 | ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId()); | 
|  | 3324 | if (mListener) { | 
|  | 3325 | mListener->notifyPrepared(stream->getId()); | 
|  | 3326 | } | 
|  | 3327 | return OK; | 
|  | 3328 | } else if (res != NOT_ENOUGH_DATA) { | 
|  | 3329 | return res; | 
|  | 3330 | } | 
|  | 3331 |  | 
|  | 3332 | // Need to prepare, start up thread if necessary | 
|  | 3333 | if (!mActive) { | 
|  | 3334 | // mRunning will change to false before the thread fully shuts down, so wait to be sure it | 
|  | 3335 | // isn't running | 
|  | 3336 | Thread::requestExitAndWait(); | 
|  | 3337 | res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND); | 
|  | 3338 | if (res != OK) { | 
|  | 3339 | ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res)); | 
|  | 3340 | if (mListener) { | 
|  | 3341 | mListener->notifyPrepared(stream->getId()); | 
|  | 3342 | } | 
|  | 3343 | return res; | 
|  | 3344 | } | 
|  | 3345 | mCancelNow = false; | 
|  | 3346 | mActive = true; | 
|  | 3347 | ALOGV("%s: Preparer stream started", __FUNCTION__); | 
|  | 3348 | } | 
|  | 3349 |  | 
|  | 3350 | // queue up the work | 
|  | 3351 | mPendingStreams.push_back(stream); | 
|  | 3352 | ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId()); | 
|  | 3353 |  | 
|  | 3354 | return OK; | 
|  | 3355 | } | 
|  | 3356 |  | 
|  | 3357 | status_t Camera3Device::PreparerThread::clear() { | 
|  | 3358 | status_t res; | 
|  | 3359 |  | 
|  | 3360 | Mutex::Autolock l(mLock); | 
|  | 3361 |  | 
|  | 3362 | for (const auto& stream : mPendingStreams) { | 
|  | 3363 | stream->cancelPrepare(); | 
|  | 3364 | } | 
|  | 3365 | mPendingStreams.clear(); | 
|  | 3366 | mCancelNow = true; | 
|  | 3367 |  | 
|  | 3368 | return OK; | 
|  | 3369 | } | 
|  | 3370 |  | 
|  | 3371 | void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) { | 
|  | 3372 | Mutex::Autolock l(mLock); | 
|  | 3373 | mListener = listener; | 
|  | 3374 | } | 
|  | 3375 |  | 
|  | 3376 | bool Camera3Device::PreparerThread::threadLoop() { | 
|  | 3377 | status_t res; | 
|  | 3378 | { | 
|  | 3379 | Mutex::Autolock l(mLock); | 
|  | 3380 | if (mCurrentStream == nullptr) { | 
|  | 3381 | // End thread if done with work | 
|  | 3382 | if (mPendingStreams.empty()) { | 
|  | 3383 | ALOGV("%s: Preparer stream out of work", __FUNCTION__); | 
|  | 3384 | // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would | 
|  | 3385 | // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive. | 
|  | 3386 | mActive = false; | 
|  | 3387 | return false; | 
|  | 3388 | } | 
|  | 3389 |  | 
|  | 3390 | // Get next stream to prepare | 
|  | 3391 | auto it = mPendingStreams.begin(); | 
|  | 3392 | mCurrentStream = *it; | 
|  | 3393 | mPendingStreams.erase(it); | 
|  | 3394 | ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId()); | 
|  | 3395 | ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId()); | 
|  | 3396 | } else if (mCancelNow) { | 
|  | 3397 | mCurrentStream->cancelPrepare(); | 
|  | 3398 | ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId()); | 
|  | 3399 | ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId()); | 
|  | 3400 | mCurrentStream.clear(); | 
|  | 3401 | mCancelNow = false; | 
|  | 3402 | return true; | 
|  | 3403 | } | 
|  | 3404 | } | 
|  | 3405 |  | 
|  | 3406 | res = mCurrentStream->prepareNextBuffer(); | 
|  | 3407 | if (res == NOT_ENOUGH_DATA) return true; | 
|  | 3408 | if (res != OK) { | 
|  | 3409 | // Something bad happened; try to recover by cancelling prepare and | 
|  | 3410 | // signalling listener anyway | 
|  | 3411 | ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__, | 
|  | 3412 | mCurrentStream->getId(), res, strerror(-res)); | 
|  | 3413 | mCurrentStream->cancelPrepare(); | 
|  | 3414 | } | 
|  | 3415 |  | 
|  | 3416 | // This stream has finished, notify listener | 
|  | 3417 | Mutex::Autolock l(mLock); | 
|  | 3418 | if (mListener) { | 
|  | 3419 | ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__, | 
|  | 3420 | mCurrentStream->getId()); | 
|  | 3421 | mListener->notifyPrepared(mCurrentStream->getId()); | 
|  | 3422 | } | 
|  | 3423 |  | 
|  | 3424 | ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId()); | 
|  | 3425 | mCurrentStream.clear(); | 
|  | 3426 |  | 
|  | 3427 | return true; | 
|  | 3428 | } | 
| Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3429 |  | 
| Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3430 | /** | 
| Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 3431 | * Static callback forwarding methods from HAL to instance | 
|  | 3432 | */ | 
|  | 3433 |  | 
|  | 3434 | void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb, | 
|  | 3435 | const camera3_capture_result *result) { | 
|  | 3436 | Camera3Device *d = | 
|  | 3437 | const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb)); | 
|  | 3438 | d->processCaptureResult(result); | 
|  | 3439 | } | 
|  | 3440 |  | 
|  | 3441 | void Camera3Device::sNotify(const camera3_callback_ops *cb, | 
|  | 3442 | const camera3_notify_msg *msg) { | 
|  | 3443 | Camera3Device *d = | 
|  | 3444 | const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb)); | 
|  | 3445 | d->notify(msg); | 
|  | 3446 | } | 
|  | 3447 |  | 
|  | 3448 | }; // namespace android |