blob: 7f294e6fd5bbb493689de6951b056c510321283a [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
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 Talvalaf76e0272013-02-27 18:02:26 -080017#ifndef ANDROID_SERVERS_CAMERA3DEVICE_H
18#define ANDROID_SERVERS_CAMERA3DEVICE_H
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080019
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 Talvalaf76e0272013-02-27 18:02:26 -080027#include "camera3/Camera3Stream.h"
28#include "camera3/Camera3OutputStream.h"
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080029
30#include "hardware/camera3.h"
31
32/**
33 * Function pointer types with C calling convention to
34 * use for HAL callback functions.
35 */
36extern "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
46namespace android {
47
48/**
49 * CameraDevice for HAL devices with version CAMERA_DEVICE_API_VERSION_3_0
50 */
51class Camera3Device :
52 public CameraDeviceBase,
53 private camera3_callback_ops {
54 public:
55 Camera3Device(int id);
56
57 virtual ~Camera3Device();
58
59 /**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080060 * CameraDeviceBase interface
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080061 */
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080062
Igor Murashkin71381052013-03-04 14:53:08 -080063 virtual int getId() const;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080064
65 // Transitions to idle state on success.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080066 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 Talvalaf76e0272013-02-27 18:02:26 -080070
71 // Capture and setStreamingRequest will configure streams if currently in
72 // idle state
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080073 virtual status_t capture(CameraMetadata &request);
74 virtual status_t setStreamingRequest(const CameraMetadata &request);
75 virtual status_t clearStreamingRequest();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080076
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080077 virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080078
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 Talvala7fa43f32013-02-06 17:20:07 -080082 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 Talvalaf76e0272013-02-27 18:02:26 -080086
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080087 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 Talvalaf76e0272013-02-27 18:02:26 -080090
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080091 virtual status_t deleteStream(int id);
92 virtual status_t deleteReprocessStream(int id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080093
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080094 virtual status_t createDefaultRequest(int templateId, CameraMetadata *request);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080095
96 // Transitions to the idle state on success
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080097 virtual status_t waitUntilDrained();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080098
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080099 virtual status_t setNotifyCallback(NotificationListener *listener);
100 virtual status_t waitForNextFrame(nsecs_t timeout);
101 virtual status_t getNextFrame(CameraMetadata *frame);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800102
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103 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 Talvalaf76e0272013-02-27 18:02:26 -0800106
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800107 virtual status_t pushReprocessBuffer(int reprocessStreamId,
108 buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
109
110 private:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800111 static const nsecs_t kShutdownTimeout = 5000000000; // 5 sec
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700112 struct RequestTrigger;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800113
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 Talvala7fa43f32013-02-06 17:20:07 -0800174
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700175 struct RequestTrigger {
176 // Metadata tag number, e.g. android.control.aePrecaptureTrigger
177 uint32_t metadataTag;
178 // Metadata value, e.g. 'START' or the trigger ID
179 int32_t entryValue;
180
181 // The last part of the fully qualified path, e.g. afTrigger
182 const char *getTagName() const {
183 return get_camera_metadata_tag_name(metadataTag) ?: "NULL";
184 }
185
186 // e.g. TYPE_BYTE, TYPE_INT32, etc.
187 int getTagType() const {
188 return get_camera_metadata_tag_type(metadataTag);
189 }
190 };
191
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192 /**
193 * Thread for managing capture request submission to HAL device.
194 */
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800195 class RequestThread : public Thread {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800196
197 public:
198
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800199 RequestThread(wp<Camera3Device> parent,
200 camera3_device_t *hal3Device);
201
202 /**
203 * Call after stream (re)-configuration is completed.
204 */
205 void configurationComplete();
206
207 /**
208 * Set or clear the list of repeating requests. Does not block
209 * on either. Use waitUntilPaused to wait until request queue
210 * has emptied out.
211 */
212 status_t setRepeatingRequests(const RequestList& requests);
213 status_t clearRepeatingRequests();
214
215 status_t queueRequest(sp<CaptureRequest> request);
216
217 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700218 * Queue a trigger to be dispatched with the next outgoing
219 * process_capture_request. The settings for that request only
220 * will be temporarily rewritten to add the trigger tag/value.
221 * Subsequent requests will not be rewritten (for this tag).
222 */
223 status_t queueTrigger(RequestTrigger trigger[], size_t count);
224
225 /**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800226 * Pause/unpause the capture thread. Doesn't block, so use
227 * waitUntilPaused to wait until the thread is paused.
228 */
229 void setPaused(bool paused);
230
231 /**
232 * Wait until thread is paused, either due to setPaused(true)
233 * or due to lack of input requests. Returns TIMED_OUT in case
234 * the thread does not pause within the timeout.
235 */
236 status_t waitUntilPaused(nsecs_t timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800237
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700238 /**
239 * Wait until thread processes the capture request with settings'
240 * android.request.id == requestId.
241 *
242 * Returns TIMED_OUT in case the thread does not process the request
243 * within the timeout.
244 */
245 status_t waitUntilRequestProcessed(int32_t requestId, nsecs_t timeout);
246
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800247 protected:
248
249 virtual bool threadLoop();
250
251 private:
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700252 status_t queueTriggerLocked(RequestTrigger trigger);
253 // Mix-in queued triggers into this request
254 int32_t insertTriggers(const sp<CaptureRequest> &request);
255 // Purge the queued triggers from this request,
256 // restoring the old field values for those tags.
257 status_t removeTriggers(const sp<CaptureRequest> &request);
258
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800259 static const nsecs_t kRequestTimeout = 50e6; // 50 ms
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800260
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800261 // Waits for a request, or returns NULL if times out.
262 sp<CaptureRequest> waitForNextRequest();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800263
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800264 // Return buffers, etc, for a request that couldn't be fully
265 // constructed. The buffers will be returned in the ERROR state
266 // to mark them as not having valid data.
267 // All arguments will be modified.
268 void cleanUpFailedRequest(camera3_capture_request_t &request,
269 sp<CaptureRequest> &nextRequest,
270 Vector<camera3_stream_buffer_t> &outputBuffers);
271
272 // Pause handling
273 bool waitIfPaused();
274
275 wp<Camera3Device> mParent;
276 camera3_device_t *mHal3Device;
277
278 Mutex mRequestLock;
279 Condition mRequestSignal;
280 RequestList mRequestQueue;
281 RequestList mRepeatingRequests;
282
283 bool mReconfigured;
284
285 // Used by waitIfPaused, waitForNextRequest, and waitUntilPaused
286 Mutex mPauseLock;
287 bool mDoPause;
288 Condition mDoPauseSignal;
289 bool mPaused;
290 Condition mPausedSignal;
291
292 sp<CaptureRequest> mPrevRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700293 int32_t mPrevTriggers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800294
295 int32_t mFrameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700296
297 Mutex mLatestRequestMutex;
298 Condition mLatestRequestSignal;
299 // android.request.id for latest process_capture_request
300 int32_t mLatestRequestId;
301
302 typedef KeyedVector<uint32_t/*tag*/, RequestTrigger> TriggerMap;
303 Mutex mTriggerMutex;
304 TriggerMap mTriggerMap;
305 TriggerMap mTriggerRemovedMap;
306 TriggerMap mTriggerReplacedMap;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800307 };
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800308 sp<RequestThread> mRequestThread;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800309
310 /**
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700311 * Output result queue and current HAL device 3A state
312 */
313
314 // Lock for output side of device
315 Mutex mOutputLock;
316
317 /**** Scope for mOutputLock ****/
318
319 List<CameraMetadata> mResultQueue;
320 Condition mResultSignal;
321 NotificationListener *mListener;
322
323 struct AlgState {
324 camera_metadata_enum_android_control_ae_state aeState;
325 camera_metadata_enum_android_control_af_state afState;
326 camera_metadata_enum_android_control_awb_state awbState;
327
328 AlgState() :
329 aeState(ANDROID_CONTROL_AE_STATE_INACTIVE),
330 afState(ANDROID_CONTROL_AF_STATE_INACTIVE),
331 awbState(ANDROID_CONTROL_AWB_STATE_INACTIVE) {
332 }
333 } m3AState;
334
335 /**** End scope for mOutputLock ****/
336
337 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800338 * Callback functions from HAL device
339 */
340 void processCaptureResult(const camera3_capture_result *result);
341
342 void notify(const camera3_notify_msg *msg);
343
344 /**
345 * Static callback forwarding methods from HAL to instance
346 */
347 static callbacks_process_capture_result_t sProcessCaptureResult;
348
349 static callbacks_notify_t sNotify;
350
351}; // class Camera3Device
352
353}; // namespace android
354
355#endif