blob: 6cad08e51b520305eb6357aac8d2ff17909ff7e9 [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 Talvala42368d92013-04-09 14:13:50 -0700111 static const size_t kInFlightWarnLimit = 20;
112 static const nsecs_t kShutdownTimeout = 5000000000; // 5 sec
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700113 struct RequestTrigger;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800114
115 Mutex mLock;
116
117 /**** Scope for mLock ****/
118
119 const int mId;
120 camera3_device_t *mHal3Device;
121
122 CameraMetadata mDeviceInfo;
123 vendor_tag_query_ops_t mVendorTagOps;
124
125 enum {
126 STATUS_ERROR,
127 STATUS_UNINITIALIZED,
128 STATUS_IDLE,
129 STATUS_ACTIVE
130 } mStatus;
131
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700132 // Tracking cause of fatal errors when in STATUS_ERROR
133 String8 mErrorCause;
134
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800135 // Mapping of stream IDs to stream instances
136 typedef KeyedVector<int, sp<camera3::Camera3OutputStream> > StreamSet;
137
138 StreamSet mOutputStreams;
139 sp<camera3::Camera3Stream> mInputStream;
140 int mNextStreamId;
141
142 // Need to hold on to stream references until configure completes.
143 Vector<sp<camera3::Camera3Stream> > mDeletedStreams;
144
145 /**** End scope for mLock ****/
146
147 class CaptureRequest : public LightRefBase<CaptureRequest> {
148 public:
149 CameraMetadata mSettings;
150 sp<camera3::Camera3Stream> mInputStream;
151 Vector<sp<camera3::Camera3Stream> > mOutputStreams;
152 };
153 typedef List<sp<CaptureRequest> > RequestList;
154
155 /**
156 * Lock-held version of waitUntilDrained. Will transition to IDLE on
157 * success.
158 */
159 status_t waitUntilDrainedLocked();
160
161 /**
162 * Do common work for setting up a streaming or single capture request.
163 * On success, will transition to ACTIVE if in IDLE.
164 */
165 sp<CaptureRequest> setUpRequestLocked(const CameraMetadata &request);
166
167 /**
168 * Build a CaptureRequest request from the CameraDeviceBase request
169 * settings.
170 */
171 sp<CaptureRequest> createCaptureRequest(const CameraMetadata &request);
172
173 /**
174 * Take the currently-defined set of streams and configure the HAL to use
175 * them. This is a long-running operation (may be several hundered ms).
176 */
177 status_t configureStreamsLocked();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800178
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700179 /**
180 * Set device into an error state due to some fatal failure, and set an
181 * error message to indicate why. Only the first call's message will be
182 * used. The message is also sent to the log.
183 */
184 void setErrorState(const char *fmt, ...);
185 void setErrorStateV(const char *fmt, va_list args);
186 void setErrorStateLocked(const char *fmt, ...);
187 void setErrorStateLockedV(const char *fmt, va_list args);
188
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700189 struct RequestTrigger {
190 // Metadata tag number, e.g. android.control.aePrecaptureTrigger
191 uint32_t metadataTag;
192 // Metadata value, e.g. 'START' or the trigger ID
193 int32_t entryValue;
194
195 // The last part of the fully qualified path, e.g. afTrigger
196 const char *getTagName() const {
197 return get_camera_metadata_tag_name(metadataTag) ?: "NULL";
198 }
199
200 // e.g. TYPE_BYTE, TYPE_INT32, etc.
201 int getTagType() const {
202 return get_camera_metadata_tag_type(metadataTag);
203 }
204 };
205
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800206 /**
207 * Thread for managing capture request submission to HAL device.
208 */
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800209 class RequestThread : public Thread {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800210
211 public:
212
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800213 RequestThread(wp<Camera3Device> parent,
214 camera3_device_t *hal3Device);
215
216 /**
217 * Call after stream (re)-configuration is completed.
218 */
219 void configurationComplete();
220
221 /**
222 * Set or clear the list of repeating requests. Does not block
223 * on either. Use waitUntilPaused to wait until request queue
224 * has emptied out.
225 */
226 status_t setRepeatingRequests(const RequestList& requests);
227 status_t clearRepeatingRequests();
228
229 status_t queueRequest(sp<CaptureRequest> request);
230
231 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700232 * Queue a trigger to be dispatched with the next outgoing
233 * process_capture_request. The settings for that request only
234 * will be temporarily rewritten to add the trigger tag/value.
235 * Subsequent requests will not be rewritten (for this tag).
236 */
237 status_t queueTrigger(RequestTrigger trigger[], size_t count);
238
239 /**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800240 * Pause/unpause the capture thread. Doesn't block, so use
241 * waitUntilPaused to wait until the thread is paused.
242 */
243 void setPaused(bool paused);
244
245 /**
246 * Wait until thread is paused, either due to setPaused(true)
247 * or due to lack of input requests. Returns TIMED_OUT in case
248 * the thread does not pause within the timeout.
249 */
250 status_t waitUntilPaused(nsecs_t timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800251
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700252 /**
253 * Wait until thread processes the capture request with settings'
254 * android.request.id == requestId.
255 *
256 * Returns TIMED_OUT in case the thread does not process the request
257 * within the timeout.
258 */
259 status_t waitUntilRequestProcessed(int32_t requestId, nsecs_t timeout);
260
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800261 protected:
262
263 virtual bool threadLoop();
264
265 private:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700266 static int getId(const wp<Camera3Device> &device);
267
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700268 status_t queueTriggerLocked(RequestTrigger trigger);
269 // Mix-in queued triggers into this request
270 int32_t insertTriggers(const sp<CaptureRequest> &request);
271 // Purge the queued triggers from this request,
272 // restoring the old field values for those tags.
273 status_t removeTriggers(const sp<CaptureRequest> &request);
274
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800275 static const nsecs_t kRequestTimeout = 50e6; // 50 ms
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800276
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800277 // Waits for a request, or returns NULL if times out.
278 sp<CaptureRequest> waitForNextRequest();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800279
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800280 // Return buffers, etc, for a request that couldn't be fully
281 // constructed. The buffers will be returned in the ERROR state
282 // to mark them as not having valid data.
283 // All arguments will be modified.
284 void cleanUpFailedRequest(camera3_capture_request_t &request,
285 sp<CaptureRequest> &nextRequest,
286 Vector<camera3_stream_buffer_t> &outputBuffers);
287
288 // Pause handling
289 bool waitIfPaused();
290
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700291 // Relay error to parent device object setErrorState
292 void setErrorState(const char *fmt, ...);
293
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800294 wp<Camera3Device> mParent;
295 camera3_device_t *mHal3Device;
296
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700297 const int mId;
298
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800299 Mutex mRequestLock;
300 Condition mRequestSignal;
301 RequestList mRequestQueue;
302 RequestList mRepeatingRequests;
303
304 bool mReconfigured;
305
306 // Used by waitIfPaused, waitForNextRequest, and waitUntilPaused
307 Mutex mPauseLock;
308 bool mDoPause;
309 Condition mDoPauseSignal;
310 bool mPaused;
311 Condition mPausedSignal;
312
313 sp<CaptureRequest> mPrevRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700314 int32_t mPrevTriggers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800315
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700316 uint32_t mFrameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700317
318 Mutex mLatestRequestMutex;
319 Condition mLatestRequestSignal;
320 // android.request.id for latest process_capture_request
321 int32_t mLatestRequestId;
322
323 typedef KeyedVector<uint32_t/*tag*/, RequestTrigger> TriggerMap;
324 Mutex mTriggerMutex;
325 TriggerMap mTriggerMap;
326 TriggerMap mTriggerRemovedMap;
327 TriggerMap mTriggerReplacedMap;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800328 };
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800329 sp<RequestThread> mRequestThread;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800330
331 /**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700332 * In-flight queue for tracking completion of capture requests.
333 */
334
335 struct InFlightRequest {
336 // Set by notify() SHUTTER call.
337 nsecs_t captureTimestamp;
338 // Set by process_capture_result call with valid metadata
339 bool haveResultMetadata;
340 // Decremented by calls to process_capture_result with valid output
341 // buffers
342 int numBuffersLeft;
343
344 InFlightRequest() :
345 captureTimestamp(0),
346 haveResultMetadata(false),
347 numBuffersLeft(0) {
348 }
349
350 explicit InFlightRequest(int numBuffers) :
351 captureTimestamp(0),
352 haveResultMetadata(false),
353 numBuffersLeft(numBuffers) {
354 }
355 };
356 // Map from frame number to the in-flight request state
357 typedef KeyedVector<uint32_t, InFlightRequest> InFlightMap;
358
359 Mutex mInFlightLock; // Protects mInFlightMap
360 InFlightMap mInFlightMap;
361
362 status_t registerInFlight(int32_t frameNumber, int32_t numBuffers);
363
364 /**
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700365 * Output result queue and current HAL device 3A state
366 */
367
368 // Lock for output side of device
369 Mutex mOutputLock;
370
371 /**** Scope for mOutputLock ****/
372
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700373 uint32_t mNextResultFrameNumber;
374 uint32_t mNextShutterFrameNumber;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700375 List<CameraMetadata> mResultQueue;
376 Condition mResultSignal;
377 NotificationListener *mListener;
378
379 struct AlgState {
380 camera_metadata_enum_android_control_ae_state aeState;
381 camera_metadata_enum_android_control_af_state afState;
382 camera_metadata_enum_android_control_awb_state awbState;
383
384 AlgState() :
385 aeState(ANDROID_CONTROL_AE_STATE_INACTIVE),
386 afState(ANDROID_CONTROL_AF_STATE_INACTIVE),
387 awbState(ANDROID_CONTROL_AWB_STATE_INACTIVE) {
388 }
389 } m3AState;
390
391 /**** End scope for mOutputLock ****/
392
393 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800394 * Callback functions from HAL device
395 */
396 void processCaptureResult(const camera3_capture_result *result);
397
398 void notify(const camera3_notify_msg *msg);
399
400 /**
401 * Static callback forwarding methods from HAL to instance
402 */
403 static callbacks_process_capture_result_t sProcessCaptureResult;
404
405 static callbacks_notify_t sNotify;
406
407}; // class Camera3Device
408
409}; // namespace android
410
411#endif