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> |
| 25 | |
| 26 | #include "CameraDeviceBase.h" |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 27 | #include "camera3/Camera3Stream.h" |
| 28 | #include "camera3/Camera3OutputStream.h" |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 29 | |
| 30 | #include "hardware/camera3.h" |
| 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 | |
| 48 | /** |
| 49 | * CameraDevice for HAL devices with version CAMERA_DEVICE_API_VERSION_3_0 |
| 50 | */ |
| 51 | class Camera3Device : |
| 52 | public CameraDeviceBase, |
| 53 | private camera3_callback_ops { |
| 54 | public: |
| 55 | Camera3Device(int id); |
| 56 | |
| 57 | virtual ~Camera3Device(); |
| 58 | |
| 59 | /** |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 60 | * CameraDeviceBase interface |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 61 | */ |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 62 | |
Igor Murashkin | 7138105 | 2013-03-04 14:53:08 -0800 | [diff] [blame] | 63 | virtual int getId() const; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 64 | |
| 65 | // Transitions to idle state on success. |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 66 | virtual status_t initialize(camera_module_t *module); |
| 67 | virtual status_t disconnect(); |
| 68 | virtual status_t dump(int fd, const Vector<String16> &args); |
| 69 | virtual const CameraMetadata& info() const; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 70 | |
| 71 | // Capture and setStreamingRequest will configure streams if currently in |
| 72 | // idle state |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 73 | virtual status_t capture(CameraMetadata &request); |
| 74 | virtual status_t setStreamingRequest(const CameraMetadata &request); |
| 75 | virtual status_t clearStreamingRequest(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 76 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 77 | virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 78 | |
| 79 | // Actual stream creation/deletion is delayed until first request is submitted |
| 80 | // If adding streams while actively capturing, will pause device before adding |
| 81 | // stream, reconfiguring device, and unpausing. |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 82 | virtual status_t createStream(sp<ANativeWindow> consumer, |
| 83 | uint32_t width, uint32_t height, int format, size_t size, |
| 84 | int *id); |
| 85 | virtual status_t createReprocessStreamFromStream(int outputId, int *id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 86 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 87 | virtual status_t getStreamInfo(int id, |
| 88 | uint32_t *width, uint32_t *height, uint32_t *format); |
| 89 | virtual status_t setStreamTransform(int id, int transform); |
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 deleteStream(int id); |
| 92 | virtual status_t deleteReprocessStream(int id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 93 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 94 | virtual status_t createDefaultRequest(int templateId, CameraMetadata *request); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 95 | |
| 96 | // Transitions to the idle state on success |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 97 | virtual status_t waitUntilDrained(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 98 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 99 | virtual status_t setNotifyCallback(NotificationListener *listener); |
| 100 | virtual status_t waitForNextFrame(nsecs_t timeout); |
| 101 | virtual status_t getNextFrame(CameraMetadata *frame); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 102 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 103 | virtual status_t triggerAutofocus(uint32_t id); |
| 104 | virtual status_t triggerCancelAutofocus(uint32_t id); |
| 105 | virtual status_t triggerPrecaptureMetering(uint32_t id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 106 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 107 | virtual status_t pushReprocessBuffer(int reprocessStreamId, |
| 108 | buffer_handle_t *buffer, wp<BufferReleasedListener> listener); |
| 109 | |
| 110 | private: |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 111 | static const nsecs_t kShutdownTimeout = 5000000000; // 5 sec |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 112 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 113 | |
| 114 | Mutex mLock; |
| 115 | |
| 116 | /**** Scope for mLock ****/ |
| 117 | |
| 118 | const int mId; |
| 119 | camera3_device_t *mHal3Device; |
| 120 | |
| 121 | CameraMetadata mDeviceInfo; |
| 122 | vendor_tag_query_ops_t mVendorTagOps; |
| 123 | |
| 124 | enum { |
| 125 | STATUS_ERROR, |
| 126 | STATUS_UNINITIALIZED, |
| 127 | STATUS_IDLE, |
| 128 | STATUS_ACTIVE |
| 129 | } mStatus; |
| 130 | |
| 131 | // Mapping of stream IDs to stream instances |
| 132 | typedef KeyedVector<int, sp<camera3::Camera3OutputStream> > StreamSet; |
| 133 | |
| 134 | StreamSet mOutputStreams; |
| 135 | sp<camera3::Camera3Stream> mInputStream; |
| 136 | int mNextStreamId; |
| 137 | |
| 138 | // Need to hold on to stream references until configure completes. |
| 139 | Vector<sp<camera3::Camera3Stream> > mDeletedStreams; |
| 140 | |
| 141 | /**** End scope for mLock ****/ |
| 142 | |
| 143 | class CaptureRequest : public LightRefBase<CaptureRequest> { |
| 144 | public: |
| 145 | CameraMetadata mSettings; |
| 146 | sp<camera3::Camera3Stream> mInputStream; |
| 147 | Vector<sp<camera3::Camera3Stream> > mOutputStreams; |
| 148 | }; |
| 149 | typedef List<sp<CaptureRequest> > RequestList; |
| 150 | |
| 151 | /** |
| 152 | * Lock-held version of waitUntilDrained. Will transition to IDLE on |
| 153 | * success. |
| 154 | */ |
| 155 | status_t waitUntilDrainedLocked(); |
| 156 | |
| 157 | /** |
| 158 | * Do common work for setting up a streaming or single capture request. |
| 159 | * On success, will transition to ACTIVE if in IDLE. |
| 160 | */ |
| 161 | sp<CaptureRequest> setUpRequestLocked(const CameraMetadata &request); |
| 162 | |
| 163 | /** |
| 164 | * Build a CaptureRequest request from the CameraDeviceBase request |
| 165 | * settings. |
| 166 | */ |
| 167 | sp<CaptureRequest> createCaptureRequest(const CameraMetadata &request); |
| 168 | |
| 169 | /** |
| 170 | * Take the currently-defined set of streams and configure the HAL to use |
| 171 | * them. This is a long-running operation (may be several hundered ms). |
| 172 | */ |
| 173 | status_t configureStreamsLocked(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * Thread for managing capture request submission to HAL device. |
| 177 | */ |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 178 | class RequestThread : public Thread { |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 179 | |
| 180 | public: |
| 181 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 182 | RequestThread(wp<Camera3Device> parent, |
| 183 | camera3_device_t *hal3Device); |
| 184 | |
| 185 | /** |
| 186 | * Call after stream (re)-configuration is completed. |
| 187 | */ |
| 188 | void configurationComplete(); |
| 189 | |
| 190 | /** |
| 191 | * Set or clear the list of repeating requests. Does not block |
| 192 | * on either. Use waitUntilPaused to wait until request queue |
| 193 | * has emptied out. |
| 194 | */ |
| 195 | status_t setRepeatingRequests(const RequestList& requests); |
| 196 | status_t clearRepeatingRequests(); |
| 197 | |
| 198 | status_t queueRequest(sp<CaptureRequest> request); |
| 199 | |
| 200 | /** |
| 201 | * Pause/unpause the capture thread. Doesn't block, so use |
| 202 | * waitUntilPaused to wait until the thread is paused. |
| 203 | */ |
| 204 | void setPaused(bool paused); |
| 205 | |
| 206 | /** |
| 207 | * Wait until thread is paused, either due to setPaused(true) |
| 208 | * or due to lack of input requests. Returns TIMED_OUT in case |
| 209 | * the thread does not pause within the timeout. |
| 210 | */ |
| 211 | status_t waitUntilPaused(nsecs_t timeout); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 212 | |
| 213 | protected: |
| 214 | |
| 215 | virtual bool threadLoop(); |
| 216 | |
| 217 | private: |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 218 | static const nsecs_t kRequestTimeout = 50e6; // 50 ms |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 219 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 220 | // Waits for a request, or returns NULL if times out. |
| 221 | sp<CaptureRequest> waitForNextRequest(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 222 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 223 | // Return buffers, etc, for a request that couldn't be fully |
| 224 | // constructed. The buffers will be returned in the ERROR state |
| 225 | // to mark them as not having valid data. |
| 226 | // All arguments will be modified. |
| 227 | void cleanUpFailedRequest(camera3_capture_request_t &request, |
| 228 | sp<CaptureRequest> &nextRequest, |
| 229 | Vector<camera3_stream_buffer_t> &outputBuffers); |
| 230 | |
| 231 | // Pause handling |
| 232 | bool waitIfPaused(); |
| 233 | |
| 234 | wp<Camera3Device> mParent; |
| 235 | camera3_device_t *mHal3Device; |
| 236 | |
| 237 | Mutex mRequestLock; |
| 238 | Condition mRequestSignal; |
| 239 | RequestList mRequestQueue; |
| 240 | RequestList mRepeatingRequests; |
| 241 | |
| 242 | bool mReconfigured; |
| 243 | |
| 244 | // Used by waitIfPaused, waitForNextRequest, and waitUntilPaused |
| 245 | Mutex mPauseLock; |
| 246 | bool mDoPause; |
| 247 | Condition mDoPauseSignal; |
| 248 | bool mPaused; |
| 249 | Condition mPausedSignal; |
| 250 | |
| 251 | sp<CaptureRequest> mPrevRequest; |
| 252 | |
| 253 | int32_t mFrameNumber; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 254 | }; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame^] | 255 | sp<RequestThread> mRequestThread; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 256 | |
| 257 | /** |
| 258 | * Callback functions from HAL device |
| 259 | */ |
| 260 | void processCaptureResult(const camera3_capture_result *result); |
| 261 | |
| 262 | void notify(const camera3_notify_msg *msg); |
| 263 | |
| 264 | /** |
| 265 | * Static callback forwarding methods from HAL to instance |
| 266 | */ |
| 267 | static callbacks_process_capture_result_t sProcessCaptureResult; |
| 268 | |
| 269 | static callbacks_notify_t sNotify; |
| 270 | |
| 271 | }; // class Camera3Device |
| 272 | |
| 273 | }; // namespace android |
| 274 | |
| 275 | #endif |