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 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 17 | #ifndef ANDROID_SERVERS_CAMERA3DEVICE_H |
| 18 | #define ANDROID_SERVERS_CAMERA3DEVICE_H |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 19 | |
| 20 | #include <utils/Condition.h> |
| 21 | #include <utils/Errors.h> |
| 22 | #include <utils/List.h> |
| 23 | #include <utils/Mutex.h> |
| 24 | #include <utils/Thread.h> |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 25 | #include <utils/KeyedVector.h> |
| 26 | #include <hardware/camera3.h> |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 27 | #include <camera/CaptureResult.h> |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 28 | |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 29 | #include "common/CameraDeviceBase.h" |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 30 | #include "device3/StatusTracker.h" |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Function pointer types with C calling convention to |
| 34 | * use for HAL callback functions. |
| 35 | */ |
| 36 | extern "C" { |
| 37 | typedef void (callbacks_process_capture_result_t)( |
| 38 | const struct camera3_callback_ops *, |
| 39 | const camera3_capture_result_t *); |
| 40 | |
| 41 | typedef void (callbacks_notify_t)( |
| 42 | const struct camera3_callback_ops *, |
| 43 | const camera3_notify_msg_t *); |
| 44 | } |
| 45 | |
| 46 | namespace android { |
| 47 | |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 48 | namespace camera3 { |
| 49 | |
| 50 | class Camera3Stream; |
| 51 | class Camera3ZslStream; |
| 52 | class Camera3OutputStreamInterface; |
| 53 | class Camera3StreamInterface; |
| 54 | |
| 55 | } |
| 56 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 57 | /** |
Zhijun He | 95dd5ba | 2014-03-26 18:18:00 -0700 | [diff] [blame] | 58 | * CameraDevice for HAL devices with version CAMERA_DEVICE_API_VERSION_3_0 or higher. |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 59 | */ |
| 60 | class Camera3Device : |
| 61 | public CameraDeviceBase, |
| 62 | private camera3_callback_ops { |
| 63 | public: |
| 64 | Camera3Device(int id); |
| 65 | |
| 66 | virtual ~Camera3Device(); |
| 67 | |
| 68 | /** |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 69 | * CameraDeviceBase interface |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 70 | */ |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 71 | |
Igor Murashkin | 7138105 | 2013-03-04 14:53:08 -0800 | [diff] [blame] | 72 | virtual int getId() const; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 73 | |
| 74 | // Transitions to idle state on success. |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 75 | virtual status_t initialize(camera_module_t *module); |
| 76 | virtual status_t disconnect(); |
| 77 | virtual status_t dump(int fd, const Vector<String16> &args); |
| 78 | virtual const CameraMetadata& info() const; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 79 | |
| 80 | // Capture and setStreamingRequest will configure streams if currently in |
| 81 | // idle state |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 82 | virtual status_t capture(CameraMetadata &request, int64_t *lastFrameNumber = NULL); |
| 83 | virtual status_t captureList(const List<const CameraMetadata> &requests, |
| 84 | int64_t *lastFrameNumber = NULL); |
| 85 | virtual status_t setStreamingRequest(const CameraMetadata &request, |
| 86 | int64_t *lastFrameNumber = NULL); |
| 87 | virtual status_t setStreamingRequestList(const List<const CameraMetadata> &requests, |
| 88 | int64_t *lastFrameNumber = NULL); |
| 89 | virtual status_t clearStreamingRequest(int64_t *lastFrameNumber = NULL); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 90 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 91 | virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 92 | |
| 93 | // Actual stream creation/deletion is delayed until first request is submitted |
| 94 | // If adding streams while actively capturing, will pause device before adding |
| 95 | // stream, reconfiguring device, and unpausing. |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 96 | virtual status_t createStream(sp<ANativeWindow> consumer, |
| 97 | uint32_t width, uint32_t height, int format, size_t size, |
| 98 | int *id); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 99 | virtual status_t createInputStream( |
| 100 | uint32_t width, uint32_t height, int format, |
| 101 | int *id); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 102 | virtual status_t createZslStream( |
| 103 | uint32_t width, uint32_t height, |
| 104 | int depth, |
| 105 | /*out*/ |
| 106 | int *id, |
| 107 | sp<camera3::Camera3ZslStream>* zslStream); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 108 | virtual status_t createReprocessStreamFromStream(int outputId, int *id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 109 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 110 | virtual status_t getStreamInfo(int id, |
| 111 | uint32_t *width, uint32_t *height, uint32_t *format); |
| 112 | virtual status_t setStreamTransform(int id, int transform); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 113 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 114 | virtual status_t deleteStream(int id); |
| 115 | virtual status_t deleteReprocessStream(int id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 116 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 117 | virtual status_t createDefaultRequest(int templateId, CameraMetadata *request); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 118 | |
| 119 | // Transitions to the idle state on success |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 120 | virtual status_t waitUntilDrained(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 121 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 122 | virtual status_t setNotifyCallback(NotificationListener *listener); |
Eino-Ville Talvala | 46910bd | 2013-07-18 19:15:17 -0700 | [diff] [blame] | 123 | virtual bool willNotify3A(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 124 | virtual status_t waitForNextFrame(nsecs_t timeout); |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 125 | virtual status_t getNextResult(CaptureResult *frame); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 126 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 127 | virtual status_t triggerAutofocus(uint32_t id); |
| 128 | virtual status_t triggerCancelAutofocus(uint32_t id); |
| 129 | virtual status_t triggerPrecaptureMetering(uint32_t id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 130 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 131 | virtual status_t pushReprocessBuffer(int reprocessStreamId, |
| 132 | buffer_handle_t *buffer, wp<BufferReleasedListener> listener); |
| 133 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 134 | virtual status_t flush(int64_t *lastFrameNumber = NULL); |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 135 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 136 | // Methods called by subclasses |
| 137 | void notifyStatus(bool idle); // updates from StatusTracker |
| 138 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 139 | private: |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 140 | static const size_t kDumpLockAttempts = 10; |
| 141 | static const size_t kDumpSleepDuration = 100000; // 0.10 sec |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 142 | static const size_t kInFlightWarnLimit = 20; |
| 143 | static const nsecs_t kShutdownTimeout = 5000000000; // 5 sec |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 144 | static const nsecs_t kActiveTimeout = 500000000; // 500 ms |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 145 | struct RequestTrigger; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 146 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 147 | // A lock to enforce serialization on the input/configure side |
| 148 | // of the public interface. |
| 149 | // Only locked by public methods inherited from CameraDeviceBase. |
| 150 | // Not locked by methods guarded by mOutputLock, since they may act |
| 151 | // concurrently to the input/configure side of the interface. |
| 152 | // Must be locked before mLock if both will be locked by a method |
| 153 | Mutex mInterfaceLock; |
| 154 | |
| 155 | // The main lock on internal state |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 156 | Mutex mLock; |
| 157 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 158 | // Camera device ID |
| 159 | const int mId; |
| 160 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 161 | /**** Scope for mLock ****/ |
| 162 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 163 | camera3_device_t *mHal3Device; |
| 164 | |
| 165 | CameraMetadata mDeviceInfo; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 166 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 167 | enum Status { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 168 | STATUS_ERROR, |
| 169 | STATUS_UNINITIALIZED, |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 170 | STATUS_UNCONFIGURED, |
| 171 | STATUS_CONFIGURED, |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 172 | STATUS_ACTIVE |
| 173 | } mStatus; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 174 | Vector<Status> mRecentStatusUpdates; |
| 175 | Condition mStatusChanged; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 176 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 177 | // Tracking cause of fatal errors when in STATUS_ERROR |
| 178 | String8 mErrorCause; |
| 179 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 180 | // Mapping of stream IDs to stream instances |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 181 | typedef KeyedVector<int, sp<camera3::Camera3OutputStreamInterface> > |
| 182 | StreamSet; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 183 | |
| 184 | StreamSet mOutputStreams; |
| 185 | sp<camera3::Camera3Stream> mInputStream; |
| 186 | int mNextStreamId; |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 187 | bool mNeedConfig; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 188 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 189 | // Whether to send state updates upstream |
| 190 | // Pause when doing transparent reconfiguration |
| 191 | bool mPauseStateNotify; |
| 192 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 193 | // Need to hold on to stream references until configure completes. |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 194 | Vector<sp<camera3::Camera3StreamInterface> > mDeletedStreams; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 195 | |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 196 | // Whether quirk ANDROID_QUIRKS_USE_PARTIAL_RESULT is enabled |
| 197 | bool mUsePartialResultQuirk; |
| 198 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 199 | /**** End scope for mLock ****/ |
| 200 | |
| 201 | class CaptureRequest : public LightRefBase<CaptureRequest> { |
| 202 | public: |
| 203 | CameraMetadata mSettings; |
| 204 | sp<camera3::Camera3Stream> mInputStream; |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 205 | Vector<sp<camera3::Camera3OutputStreamInterface> > |
| 206 | mOutputStreams; |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 207 | CaptureResultExtras mResultExtras; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 208 | }; |
| 209 | typedef List<sp<CaptureRequest> > RequestList; |
| 210 | |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 211 | status_t checkStatusOkToCaptureLocked(); |
| 212 | |
| 213 | status_t convertMetadataListToRequestListLocked( |
| 214 | const List<const CameraMetadata> &metadataList, |
| 215 | /*out*/RequestList *requestList); |
| 216 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 217 | status_t submitRequestsHelper(const List<const CameraMetadata> &requests, bool repeating, |
| 218 | int64_t *lastFrameNumber = NULL); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 219 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 220 | /** |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 221 | * Get the last request submitted to the hal by the request thread. |
| 222 | * |
| 223 | * Takes mLock. |
| 224 | */ |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 225 | virtual CameraMetadata getLatestRequestLocked(); |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 226 | |
| 227 | /** |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 228 | * Pause processing and flush everything, but don't tell the clients. |
| 229 | * This is for reconfiguring outputs transparently when according to the |
| 230 | * CameraDeviceBase interface we shouldn't need to. |
| 231 | * Must be called with mLock and mInterfaceLock both held. |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 232 | */ |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 233 | status_t internalPauseAndWaitLocked(); |
| 234 | |
| 235 | /** |
| 236 | * Resume work after internalPauseAndWaitLocked() |
| 237 | * Must be called with mLock and mInterfaceLock both held. |
| 238 | */ |
| 239 | status_t internalResumeLocked(); |
| 240 | |
| 241 | /** |
| 242 | * Wait until status tracker tells us we've transitioned to the target state |
| 243 | * set, which is either ACTIVE when active==true or IDLE (which is any |
| 244 | * non-ACTIVE state) when active==false. |
| 245 | * |
| 246 | * Needs to be called with mLock and mInterfaceLock held. This means there |
| 247 | * can ever only be one waiter at most. |
| 248 | * |
| 249 | * During the wait mLock is released. |
| 250 | * |
| 251 | */ |
| 252 | status_t waitUntilStateThenRelock(bool active, nsecs_t timeout); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 253 | |
| 254 | /** |
Zhijun He | 69a3748 | 2014-03-23 18:44:49 -0700 | [diff] [blame] | 255 | * Implementation of waitUntilDrained. On success, will transition to IDLE state. |
| 256 | * |
| 257 | * Need to be called with mLock and mInterfaceLock held. |
| 258 | */ |
| 259 | status_t waitUntilDrainedLocked(); |
| 260 | |
| 261 | /** |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 262 | * Do common work for setting up a streaming or single capture request. |
| 263 | * On success, will transition to ACTIVE if in IDLE. |
| 264 | */ |
| 265 | sp<CaptureRequest> setUpRequestLocked(const CameraMetadata &request); |
| 266 | |
| 267 | /** |
| 268 | * Build a CaptureRequest request from the CameraDeviceBase request |
| 269 | * settings. |
| 270 | */ |
| 271 | sp<CaptureRequest> createCaptureRequest(const CameraMetadata &request); |
| 272 | |
| 273 | /** |
| 274 | * Take the currently-defined set of streams and configure the HAL to use |
| 275 | * them. This is a long-running operation (may be several hundered ms). |
| 276 | */ |
| 277 | status_t configureStreamsLocked(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 278 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 279 | /** |
| 280 | * Set device into an error state due to some fatal failure, and set an |
| 281 | * error message to indicate why. Only the first call's message will be |
| 282 | * used. The message is also sent to the log. |
| 283 | */ |
| 284 | void setErrorState(const char *fmt, ...); |
| 285 | void setErrorStateV(const char *fmt, va_list args); |
| 286 | void setErrorStateLocked(const char *fmt, ...); |
| 287 | void setErrorStateLockedV(const char *fmt, va_list args); |
| 288 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 289 | /** |
| 290 | * Debugging trylock/spin method |
| 291 | * Try to acquire a lock a few times with sleeps between before giving up. |
| 292 | */ |
| 293 | bool tryLockSpinRightRound(Mutex& lock); |
| 294 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 295 | struct RequestTrigger { |
| 296 | // Metadata tag number, e.g. android.control.aePrecaptureTrigger |
| 297 | uint32_t metadataTag; |
| 298 | // Metadata value, e.g. 'START' or the trigger ID |
| 299 | int32_t entryValue; |
| 300 | |
| 301 | // The last part of the fully qualified path, e.g. afTrigger |
| 302 | const char *getTagName() const { |
| 303 | return get_camera_metadata_tag_name(metadataTag) ?: "NULL"; |
| 304 | } |
| 305 | |
| 306 | // e.g. TYPE_BYTE, TYPE_INT32, etc. |
| 307 | int getTagType() const { |
| 308 | return get_camera_metadata_tag_type(metadataTag); |
| 309 | } |
| 310 | }; |
| 311 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 312 | /** |
| 313 | * Thread for managing capture request submission to HAL device. |
| 314 | */ |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 315 | class RequestThread : public Thread { |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 316 | |
| 317 | public: |
| 318 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 319 | RequestThread(wp<Camera3Device> parent, |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 320 | sp<camera3::StatusTracker> statusTracker, |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 321 | camera3_device_t *hal3Device); |
| 322 | |
| 323 | /** |
| 324 | * Call after stream (re)-configuration is completed. |
| 325 | */ |
| 326 | void configurationComplete(); |
| 327 | |
| 328 | /** |
| 329 | * Set or clear the list of repeating requests. Does not block |
| 330 | * on either. Use waitUntilPaused to wait until request queue |
| 331 | * has emptied out. |
| 332 | */ |
| 333 | status_t setRepeatingRequests(const RequestList& requests); |
| 334 | status_t clearRepeatingRequests(); |
| 335 | |
| 336 | status_t queueRequest(sp<CaptureRequest> request); |
| 337 | |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 338 | status_t queueRequestList(List<sp<CaptureRequest> > &requests); |
| 339 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 340 | /** |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 341 | * Remove all queued and repeating requests, and pending triggers |
| 342 | */ |
| 343 | status_t clear(); |
| 344 | |
| 345 | /** |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 346 | * Queue a trigger to be dispatched with the next outgoing |
| 347 | * process_capture_request. The settings for that request only |
| 348 | * will be temporarily rewritten to add the trigger tag/value. |
| 349 | * Subsequent requests will not be rewritten (for this tag). |
| 350 | */ |
| 351 | status_t queueTrigger(RequestTrigger trigger[], size_t count); |
| 352 | |
| 353 | /** |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 354 | * Pause/unpause the capture thread. Doesn't block, so use |
| 355 | * waitUntilPaused to wait until the thread is paused. |
| 356 | */ |
| 357 | void setPaused(bool paused); |
| 358 | |
| 359 | /** |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 360 | * Wait until thread processes the capture request with settings' |
| 361 | * android.request.id == requestId. |
| 362 | * |
| 363 | * Returns TIMED_OUT in case the thread does not process the request |
| 364 | * within the timeout. |
| 365 | */ |
| 366 | status_t waitUntilRequestProcessed(int32_t requestId, nsecs_t timeout); |
| 367 | |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 368 | /** |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 369 | * Shut down the thread. Shutdown is asynchronous, so thread may |
| 370 | * still be running once this method returns. |
| 371 | */ |
| 372 | virtual void requestExit(); |
| 373 | |
| 374 | /** |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 375 | * Get the latest request that was sent to the HAL |
| 376 | * with process_capture_request. |
| 377 | */ |
| 378 | CameraMetadata getLatestRequest() const; |
| 379 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 380 | int64_t getLastFrameNumber(); |
| 381 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 382 | protected: |
| 383 | |
| 384 | virtual bool threadLoop(); |
| 385 | |
| 386 | private: |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 387 | static int getId(const wp<Camera3Device> &device); |
| 388 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 389 | status_t queueTriggerLocked(RequestTrigger trigger); |
| 390 | // Mix-in queued triggers into this request |
| 391 | int32_t insertTriggers(const sp<CaptureRequest> &request); |
| 392 | // Purge the queued triggers from this request, |
| 393 | // restoring the old field values for those tags. |
| 394 | status_t removeTriggers(const sp<CaptureRequest> &request); |
| 395 | |
Eino-Ville Talvala | 2f876f9 | 2013-09-13 11:39:24 -0700 | [diff] [blame] | 396 | // HAL workaround: Make sure a trigger ID always exists if |
| 397 | // a trigger does |
| 398 | status_t addDummyTriggerIds(const sp<CaptureRequest> &request); |
| 399 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 400 | static const nsecs_t kRequestTimeout = 50e6; // 50 ms |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 401 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 402 | // Waits for a request, or returns NULL if times out. |
| 403 | sp<CaptureRequest> waitForNextRequest(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 404 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 405 | // Return buffers, etc, for a request that couldn't be fully |
| 406 | // constructed. The buffers will be returned in the ERROR state |
| 407 | // to mark them as not having valid data. |
| 408 | // All arguments will be modified. |
| 409 | void cleanUpFailedRequest(camera3_capture_request_t &request, |
| 410 | sp<CaptureRequest> &nextRequest, |
| 411 | Vector<camera3_stream_buffer_t> &outputBuffers); |
| 412 | |
| 413 | // Pause handling |
| 414 | bool waitIfPaused(); |
Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 415 | void unpauseForNewRequests(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 416 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 417 | // Relay error to parent device object setErrorState |
| 418 | void setErrorState(const char *fmt, ...); |
| 419 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 420 | wp<Camera3Device> mParent; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 421 | wp<camera3::StatusTracker> mStatusTracker; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 422 | camera3_device_t *mHal3Device; |
| 423 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 424 | const int mId; // The camera ID |
| 425 | int mStatusId; // The RequestThread's component ID for |
| 426 | // status tracking |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 427 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 428 | Mutex mRequestLock; |
| 429 | Condition mRequestSignal; |
| 430 | RequestList mRequestQueue; |
| 431 | RequestList mRepeatingRequests; |
| 432 | |
| 433 | bool mReconfigured; |
| 434 | |
| 435 | // Used by waitIfPaused, waitForNextRequest, and waitUntilPaused |
| 436 | Mutex mPauseLock; |
| 437 | bool mDoPause; |
| 438 | Condition mDoPauseSignal; |
| 439 | bool mPaused; |
| 440 | Condition mPausedSignal; |
| 441 | |
| 442 | sp<CaptureRequest> mPrevRequest; |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 443 | int32_t mPrevTriggers; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 444 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 445 | uint32_t mFrameNumber; |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 446 | |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 447 | mutable Mutex mLatestRequestMutex; |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 448 | Condition mLatestRequestSignal; |
| 449 | // android.request.id for latest process_capture_request |
| 450 | int32_t mLatestRequestId; |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 451 | CameraMetadata mLatestRequest; |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 452 | |
| 453 | typedef KeyedVector<uint32_t/*tag*/, RequestTrigger> TriggerMap; |
| 454 | Mutex mTriggerMutex; |
| 455 | TriggerMap mTriggerMap; |
| 456 | TriggerMap mTriggerRemovedMap; |
| 457 | TriggerMap mTriggerReplacedMap; |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 458 | |
| 459 | int64_t mLastFrameNumber; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 460 | }; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 461 | sp<RequestThread> mRequestThread; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 462 | |
| 463 | /** |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 464 | * In-flight queue for tracking completion of capture requests. |
| 465 | */ |
| 466 | |
| 467 | struct InFlightRequest { |
| 468 | // Set by notify() SHUTTER call. |
| 469 | nsecs_t captureTimestamp; |
Zhijun He | 1d1f846 | 2013-10-02 16:29:51 -0700 | [diff] [blame] | 470 | int requestStatus; |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 471 | // Set by process_capture_result call with valid metadata |
| 472 | bool haveResultMetadata; |
| 473 | // Decremented by calls to process_capture_result with valid output |
| 474 | // buffers |
| 475 | int numBuffersLeft; |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 476 | CaptureResultExtras resultExtras; |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 477 | |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 478 | // Fields used by the partial result quirk only |
| 479 | struct PartialResultQuirkInFlight { |
| 480 | // Set by process_capture_result once 3A has been sent to clients |
| 481 | bool haveSent3A; |
| 482 | // Result metadata collected so far, when partial results are in use |
| 483 | CameraMetadata collectedResult; |
| 484 | |
| 485 | PartialResultQuirkInFlight(): |
| 486 | haveSent3A(false) { |
| 487 | } |
| 488 | } partialResultQuirk; |
| 489 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 490 | // Default constructor needed by KeyedVector |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 491 | InFlightRequest() : |
| 492 | captureTimestamp(0), |
Zhijun He | 1d1f846 | 2013-10-02 16:29:51 -0700 | [diff] [blame] | 493 | requestStatus(OK), |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 494 | haveResultMetadata(false), |
| 495 | numBuffersLeft(0) { |
| 496 | } |
| 497 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 498 | InFlightRequest(int numBuffers) : |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 499 | captureTimestamp(0), |
Zhijun He | cc27e11 | 2013-10-03 16:12:43 -0700 | [diff] [blame] | 500 | requestStatus(OK), |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 501 | haveResultMetadata(false), |
| 502 | numBuffersLeft(numBuffers) { |
| 503 | } |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 504 | |
| 505 | InFlightRequest(int numBuffers, CaptureResultExtras extras) : |
| 506 | captureTimestamp(0), |
| 507 | requestStatus(OK), |
| 508 | haveResultMetadata(false), |
| 509 | numBuffersLeft(numBuffers), |
| 510 | resultExtras(extras) { |
| 511 | } |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 512 | }; |
| 513 | // Map from frame number to the in-flight request state |
| 514 | typedef KeyedVector<uint32_t, InFlightRequest> InFlightMap; |
| 515 | |
| 516 | Mutex mInFlightLock; // Protects mInFlightMap |
| 517 | InFlightMap mInFlightMap; |
| 518 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 519 | status_t registerInFlight(uint32_t frameNumber, |
| 520 | int32_t numBuffers, CaptureResultExtras resultExtras); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 521 | |
| 522 | /** |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 523 | * For the partial result quirk, check if all 3A state fields are available |
| 524 | * and if so, queue up 3A-only result to the client. Returns true if 3A |
| 525 | * is sent. |
| 526 | */ |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 527 | bool processPartial3AQuirk(uint32_t frameNumber, |
| 528 | const CameraMetadata& partial, const CaptureResultExtras& resultExtras); |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 529 | |
| 530 | // Helpers for reading and writing 3A metadata into to/from partial results |
| 531 | template<typename T> |
| 532 | bool get3AResult(const CameraMetadata& result, int32_t tag, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 533 | T* value, uint32_t frameNumber); |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 534 | |
| 535 | template<typename T> |
| 536 | bool insert3AResult(CameraMetadata &result, int32_t tag, const T* value, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 537 | uint32_t frameNumber); |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 538 | /** |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 539 | * Tracking for idle detection |
| 540 | */ |
| 541 | sp<camera3::StatusTracker> mStatusTracker; |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 542 | |
| 543 | /** |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 544 | * Output result queue and current HAL device 3A state |
| 545 | */ |
| 546 | |
| 547 | // Lock for output side of device |
| 548 | Mutex mOutputLock; |
| 549 | |
| 550 | /**** Scope for mOutputLock ****/ |
| 551 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 552 | uint32_t mNextResultFrameNumber; |
| 553 | uint32_t mNextShutterFrameNumber; |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 554 | List<CaptureResult> mResultQueue; |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 555 | Condition mResultSignal; |
| 556 | NotificationListener *mListener; |
| 557 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 558 | /**** End scope for mOutputLock ****/ |
| 559 | |
| 560 | /** |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 561 | * Callback functions from HAL device |
| 562 | */ |
| 563 | void processCaptureResult(const camera3_capture_result *result); |
| 564 | |
| 565 | void notify(const camera3_notify_msg *msg); |
| 566 | |
| 567 | /** |
| 568 | * Static callback forwarding methods from HAL to instance |
| 569 | */ |
| 570 | static callbacks_process_capture_result_t sProcessCaptureResult; |
| 571 | |
| 572 | static callbacks_notify_t sNotify; |
| 573 | |
| 574 | }; // class Camera3Device |
| 575 | |
| 576 | }; // namespace android |
| 577 | |
| 578 | #endif |