blob: 0b3ad6e706efc0516a69ec2cc82cc43c63d47c4f [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>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070025#include <utils/KeyedVector.h>
26#include <hardware/camera3.h>
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080027
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070028#include "common/CameraDeviceBase.h"
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080029
30/**
31 * Function pointer types with C calling convention to
32 * use for HAL callback functions.
33 */
34extern "C" {
35 typedef void (callbacks_process_capture_result_t)(
36 const struct camera3_callback_ops *,
37 const camera3_capture_result_t *);
38
39 typedef void (callbacks_notify_t)(
40 const struct camera3_callback_ops *,
41 const camera3_notify_msg_t *);
42}
43
44namespace android {
45
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070046namespace camera3 {
47
48class Camera3Stream;
49class Camera3ZslStream;
50class Camera3OutputStreamInterface;
51class Camera3StreamInterface;
52
53}
54
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080055/**
56 * CameraDevice for HAL devices with version CAMERA_DEVICE_API_VERSION_3_0
57 */
58class Camera3Device :
59 public CameraDeviceBase,
60 private camera3_callback_ops {
61 public:
62 Camera3Device(int id);
63
64 virtual ~Camera3Device();
65
66 /**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080067 * CameraDeviceBase interface
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080068 */
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080069
Igor Murashkin71381052013-03-04 14:53:08 -080070 virtual int getId() const;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080071
72 // Transitions to idle state on success.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080073 virtual status_t initialize(camera_module_t *module);
74 virtual status_t disconnect();
75 virtual status_t dump(int fd, const Vector<String16> &args);
76 virtual const CameraMetadata& info() const;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080077
78 // Capture and setStreamingRequest will configure streams if currently in
79 // idle state
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080080 virtual status_t capture(CameraMetadata &request);
81 virtual status_t setStreamingRequest(const CameraMetadata &request);
82 virtual status_t clearStreamingRequest();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080083
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080084 virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080085
86 // Actual stream creation/deletion is delayed until first request is submitted
87 // If adding streams while actively capturing, will pause device before adding
88 // stream, reconfiguring device, and unpausing.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080089 virtual status_t createStream(sp<ANativeWindow> consumer,
90 uint32_t width, uint32_t height, int format, size_t size,
91 int *id);
Igor Murashkin5a269fa2013-04-15 14:59:22 -070092 virtual status_t createInputStream(
93 uint32_t width, uint32_t height, int format,
94 int *id);
Igor Murashkin2fba5842013-04-22 14:03:54 -070095 virtual status_t createZslStream(
96 uint32_t width, uint32_t height,
97 int depth,
98 /*out*/
99 int *id,
100 sp<camera3::Camera3ZslStream>* zslStream);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800101 virtual status_t createReprocessStreamFromStream(int outputId, int *id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800102
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103 virtual status_t getStreamInfo(int id,
104 uint32_t *width, uint32_t *height, uint32_t *format);
105 virtual status_t setStreamTransform(int id, int transform);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800106
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800107 virtual status_t deleteStream(int id);
108 virtual status_t deleteReprocessStream(int id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800109
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800110 virtual status_t createDefaultRequest(int templateId, CameraMetadata *request);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800111
112 // Transitions to the idle state on success
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800113 virtual status_t waitUntilDrained();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800114
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800115 virtual status_t setNotifyCallback(NotificationListener *listener);
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -0700116 virtual bool willNotify3A();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800117 virtual status_t waitForNextFrame(nsecs_t timeout);
118 virtual status_t getNextFrame(CameraMetadata *frame);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800119
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800120 virtual status_t triggerAutofocus(uint32_t id);
121 virtual status_t triggerCancelAutofocus(uint32_t id);
122 virtual status_t triggerPrecaptureMetering(uint32_t id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800123
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800124 virtual status_t pushReprocessBuffer(int reprocessStreamId,
125 buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
126
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700127 virtual status_t flush();
128
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800129 private:
Igor Murashkin1e479c02013-09-06 16:55:14 -0700130 static const size_t kDumpLockAttempts = 10;
131 static const size_t kDumpSleepDuration = 100000; // 0.10 sec
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700132 static const size_t kInFlightWarnLimit = 20;
133 static const nsecs_t kShutdownTimeout = 5000000000; // 5 sec
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700134 struct RequestTrigger;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800135
136 Mutex mLock;
137
138 /**** Scope for mLock ****/
139
140 const int mId;
141 camera3_device_t *mHal3Device;
142
143 CameraMetadata mDeviceInfo;
144 vendor_tag_query_ops_t mVendorTagOps;
145
146 enum {
147 STATUS_ERROR,
148 STATUS_UNINITIALIZED,
149 STATUS_IDLE,
150 STATUS_ACTIVE
151 } mStatus;
152
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700153 // Tracking cause of fatal errors when in STATUS_ERROR
154 String8 mErrorCause;
155
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800156 // Mapping of stream IDs to stream instances
Igor Murashkin2fba5842013-04-22 14:03:54 -0700157 typedef KeyedVector<int, sp<camera3::Camera3OutputStreamInterface> >
158 StreamSet;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800159
160 StreamSet mOutputStreams;
161 sp<camera3::Camera3Stream> mInputStream;
162 int mNextStreamId;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700163 bool mNeedConfig;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800164
165 // Need to hold on to stream references until configure completes.
Igor Murashkin2fba5842013-04-22 14:03:54 -0700166 Vector<sp<camera3::Camera3StreamInterface> > mDeletedStreams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800167
168 /**** End scope for mLock ****/
169
170 class CaptureRequest : public LightRefBase<CaptureRequest> {
171 public:
172 CameraMetadata mSettings;
173 sp<camera3::Camera3Stream> mInputStream;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700174 Vector<sp<camera3::Camera3OutputStreamInterface> >
175 mOutputStreams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800176 };
177 typedef List<sp<CaptureRequest> > RequestList;
178
179 /**
Igor Murashkin1e479c02013-09-06 16:55:14 -0700180 * Get the last request submitted to the hal by the request thread.
181 *
182 * Takes mLock.
183 */
184 virtual CameraMetadata getLatestRequest();
185
186 /**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800187 * Lock-held version of waitUntilDrained. Will transition to IDLE on
188 * success.
189 */
190 status_t waitUntilDrainedLocked();
191
192 /**
193 * Do common work for setting up a streaming or single capture request.
194 * On success, will transition to ACTIVE if in IDLE.
195 */
196 sp<CaptureRequest> setUpRequestLocked(const CameraMetadata &request);
197
198 /**
199 * Build a CaptureRequest request from the CameraDeviceBase request
200 * settings.
201 */
202 sp<CaptureRequest> createCaptureRequest(const CameraMetadata &request);
203
204 /**
205 * Take the currently-defined set of streams and configure the HAL to use
206 * them. This is a long-running operation (may be several hundered ms).
207 */
208 status_t configureStreamsLocked();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800209
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700210 /**
211 * Set device into an error state due to some fatal failure, and set an
212 * error message to indicate why. Only the first call's message will be
213 * used. The message is also sent to the log.
214 */
215 void setErrorState(const char *fmt, ...);
216 void setErrorStateV(const char *fmt, va_list args);
217 void setErrorStateLocked(const char *fmt, ...);
218 void setErrorStateLockedV(const char *fmt, va_list args);
219
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700220 struct RequestTrigger {
221 // Metadata tag number, e.g. android.control.aePrecaptureTrigger
222 uint32_t metadataTag;
223 // Metadata value, e.g. 'START' or the trigger ID
224 int32_t entryValue;
225
226 // The last part of the fully qualified path, e.g. afTrigger
227 const char *getTagName() const {
228 return get_camera_metadata_tag_name(metadataTag) ?: "NULL";
229 }
230
231 // e.g. TYPE_BYTE, TYPE_INT32, etc.
232 int getTagType() const {
233 return get_camera_metadata_tag_type(metadataTag);
234 }
235 };
236
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800237 /**
238 * Thread for managing capture request submission to HAL device.
239 */
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800240 class RequestThread : public Thread {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800241
242 public:
243
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800244 RequestThread(wp<Camera3Device> parent,
245 camera3_device_t *hal3Device);
246
247 /**
248 * Call after stream (re)-configuration is completed.
249 */
250 void configurationComplete();
251
252 /**
253 * Set or clear the list of repeating requests. Does not block
254 * on either. Use waitUntilPaused to wait until request queue
255 * has emptied out.
256 */
257 status_t setRepeatingRequests(const RequestList& requests);
258 status_t clearRepeatingRequests();
259
260 status_t queueRequest(sp<CaptureRequest> request);
261
262 /**
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700263 * Remove all queued and repeating requests, and pending triggers
264 */
265 status_t clear();
266
267 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700268 * Queue a trigger to be dispatched with the next outgoing
269 * process_capture_request. The settings for that request only
270 * will be temporarily rewritten to add the trigger tag/value.
271 * Subsequent requests will not be rewritten (for this tag).
272 */
273 status_t queueTrigger(RequestTrigger trigger[], size_t count);
274
275 /**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800276 * Pause/unpause the capture thread. Doesn't block, so use
277 * waitUntilPaused to wait until the thread is paused.
278 */
279 void setPaused(bool paused);
280
281 /**
282 * Wait until thread is paused, either due to setPaused(true)
283 * or due to lack of input requests. Returns TIMED_OUT in case
284 * the thread does not pause within the timeout.
285 */
286 status_t waitUntilPaused(nsecs_t timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800287
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700288 /**
289 * Wait until thread processes the capture request with settings'
290 * android.request.id == requestId.
291 *
292 * Returns TIMED_OUT in case the thread does not process the request
293 * within the timeout.
294 */
295 status_t waitUntilRequestProcessed(int32_t requestId, nsecs_t timeout);
296
Igor Murashkin1e479c02013-09-06 16:55:14 -0700297 /**
298 * Get the latest request that was sent to the HAL
299 * with process_capture_request.
300 */
301 CameraMetadata getLatestRequest() const;
302
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800303 protected:
304
305 virtual bool threadLoop();
306
307 private:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700308 static int getId(const wp<Camera3Device> &device);
309
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700310 status_t queueTriggerLocked(RequestTrigger trigger);
311 // Mix-in queued triggers into this request
312 int32_t insertTriggers(const sp<CaptureRequest> &request);
313 // Purge the queued triggers from this request,
314 // restoring the old field values for those tags.
315 status_t removeTriggers(const sp<CaptureRequest> &request);
316
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800317 static const nsecs_t kRequestTimeout = 50e6; // 50 ms
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800318
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800319 // Waits for a request, or returns NULL if times out.
320 sp<CaptureRequest> waitForNextRequest();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800321
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800322 // Return buffers, etc, for a request that couldn't be fully
323 // constructed. The buffers will be returned in the ERROR state
324 // to mark them as not having valid data.
325 // All arguments will be modified.
326 void cleanUpFailedRequest(camera3_capture_request_t &request,
327 sp<CaptureRequest> &nextRequest,
328 Vector<camera3_stream_buffer_t> &outputBuffers);
329
330 // Pause handling
331 bool waitIfPaused();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -0700332 void unpauseForNewRequests();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800333
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700334 // Relay error to parent device object setErrorState
335 void setErrorState(const char *fmt, ...);
336
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800337 wp<Camera3Device> mParent;
338 camera3_device_t *mHal3Device;
339
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700340 const int mId;
341
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800342 Mutex mRequestLock;
343 Condition mRequestSignal;
344 RequestList mRequestQueue;
345 RequestList mRepeatingRequests;
346
347 bool mReconfigured;
348
349 // Used by waitIfPaused, waitForNextRequest, and waitUntilPaused
350 Mutex mPauseLock;
351 bool mDoPause;
352 Condition mDoPauseSignal;
353 bool mPaused;
354 Condition mPausedSignal;
355
356 sp<CaptureRequest> mPrevRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700357 int32_t mPrevTriggers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800358
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700359 uint32_t mFrameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700360
Igor Murashkin1e479c02013-09-06 16:55:14 -0700361 mutable Mutex mLatestRequestMutex;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700362 Condition mLatestRequestSignal;
363 // android.request.id for latest process_capture_request
364 int32_t mLatestRequestId;
Igor Murashkin1e479c02013-09-06 16:55:14 -0700365 CameraMetadata mLatestRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700366
367 typedef KeyedVector<uint32_t/*tag*/, RequestTrigger> TriggerMap;
368 Mutex mTriggerMutex;
369 TriggerMap mTriggerMap;
370 TriggerMap mTriggerRemovedMap;
371 TriggerMap mTriggerReplacedMap;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800372 };
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800373 sp<RequestThread> mRequestThread;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800374
375 /**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700376 * In-flight queue for tracking completion of capture requests.
377 */
378
379 struct InFlightRequest {
380 // Set by notify() SHUTTER call.
381 nsecs_t captureTimestamp;
382 // Set by process_capture_result call with valid metadata
383 bool haveResultMetadata;
384 // Decremented by calls to process_capture_result with valid output
385 // buffers
386 int numBuffersLeft;
387
388 InFlightRequest() :
389 captureTimestamp(0),
390 haveResultMetadata(false),
391 numBuffersLeft(0) {
392 }
393
394 explicit InFlightRequest(int numBuffers) :
395 captureTimestamp(0),
396 haveResultMetadata(false),
397 numBuffersLeft(numBuffers) {
398 }
399 };
400 // Map from frame number to the in-flight request state
401 typedef KeyedVector<uint32_t, InFlightRequest> InFlightMap;
402
403 Mutex mInFlightLock; // Protects mInFlightMap
404 InFlightMap mInFlightMap;
405
406 status_t registerInFlight(int32_t frameNumber, int32_t numBuffers);
407
408 /**
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700409 * Output result queue and current HAL device 3A state
410 */
411
412 // Lock for output side of device
413 Mutex mOutputLock;
414
415 /**** Scope for mOutputLock ****/
416
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700417 uint32_t mNextResultFrameNumber;
418 uint32_t mNextShutterFrameNumber;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700419 List<CameraMetadata> mResultQueue;
420 Condition mResultSignal;
421 NotificationListener *mListener;
422
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700423 /**** End scope for mOutputLock ****/
424
425 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800426 * Callback functions from HAL device
427 */
428 void processCaptureResult(const camera3_capture_result *result);
429
430 void notify(const camera3_notify_msg *msg);
431
432 /**
433 * Static callback forwarding methods from HAL to instance
434 */
435 static callbacks_process_capture_result_t sProcessCaptureResult;
436
437 static callbacks_notify_t sNotify;
438
439}; // class Camera3Device
440
441}; // namespace android
442
443#endif