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