blob: 65650487f335186b557e891d49a78c4c62fc7dfe [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:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700130 static const size_t kInFlightWarnLimit = 20;
131 static const nsecs_t kShutdownTimeout = 5000000000; // 5 sec
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700132 struct RequestTrigger;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800133
134 Mutex mLock;
135
136 /**** Scope for mLock ****/
137
138 const int mId;
139 camera3_device_t *mHal3Device;
140
141 CameraMetadata mDeviceInfo;
142 vendor_tag_query_ops_t mVendorTagOps;
143
144 enum {
145 STATUS_ERROR,
146 STATUS_UNINITIALIZED,
147 STATUS_IDLE,
148 STATUS_ACTIVE
149 } mStatus;
150
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700151 // Tracking cause of fatal errors when in STATUS_ERROR
152 String8 mErrorCause;
153
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800154 // Mapping of stream IDs to stream instances
Igor Murashkin2fba5842013-04-22 14:03:54 -0700155 typedef KeyedVector<int, sp<camera3::Camera3OutputStreamInterface> >
156 StreamSet;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800157
158 StreamSet mOutputStreams;
159 sp<camera3::Camera3Stream> mInputStream;
160 int mNextStreamId;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700161 bool mNeedConfig;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800162
163 // Need to hold on to stream references until configure completes.
Igor Murashkin2fba5842013-04-22 14:03:54 -0700164 Vector<sp<camera3::Camera3StreamInterface> > mDeletedStreams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800165
166 /**** End scope for mLock ****/
167
168 class CaptureRequest : public LightRefBase<CaptureRequest> {
169 public:
170 CameraMetadata mSettings;
171 sp<camera3::Camera3Stream> mInputStream;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700172 Vector<sp<camera3::Camera3OutputStreamInterface> >
173 mOutputStreams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800174 };
175 typedef List<sp<CaptureRequest> > RequestList;
176
177 /**
178 * Lock-held version of waitUntilDrained. Will transition to IDLE on
179 * success.
180 */
181 status_t waitUntilDrainedLocked();
182
183 /**
184 * Do common work for setting up a streaming or single capture request.
185 * On success, will transition to ACTIVE if in IDLE.
186 */
187 sp<CaptureRequest> setUpRequestLocked(const CameraMetadata &request);
188
189 /**
190 * Build a CaptureRequest request from the CameraDeviceBase request
191 * settings.
192 */
193 sp<CaptureRequest> createCaptureRequest(const CameraMetadata &request);
194
195 /**
196 * Take the currently-defined set of streams and configure the HAL to use
197 * them. This is a long-running operation (may be several hundered ms).
198 */
199 status_t configureStreamsLocked();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800200
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700201 /**
202 * Set device into an error state due to some fatal failure, and set an
203 * error message to indicate why. Only the first call's message will be
204 * used. The message is also sent to the log.
205 */
206 void setErrorState(const char *fmt, ...);
207 void setErrorStateV(const char *fmt, va_list args);
208 void setErrorStateLocked(const char *fmt, ...);
209 void setErrorStateLockedV(const char *fmt, va_list args);
210
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700211 struct RequestTrigger {
212 // Metadata tag number, e.g. android.control.aePrecaptureTrigger
213 uint32_t metadataTag;
214 // Metadata value, e.g. 'START' or the trigger ID
215 int32_t entryValue;
216
217 // The last part of the fully qualified path, e.g. afTrigger
218 const char *getTagName() const {
219 return get_camera_metadata_tag_name(metadataTag) ?: "NULL";
220 }
221
222 // e.g. TYPE_BYTE, TYPE_INT32, etc.
223 int getTagType() const {
224 return get_camera_metadata_tag_type(metadataTag);
225 }
226 };
227
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800228 /**
229 * Thread for managing capture request submission to HAL device.
230 */
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800231 class RequestThread : public Thread {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800232
233 public:
234
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800235 RequestThread(wp<Camera3Device> parent,
236 camera3_device_t *hal3Device);
237
238 /**
239 * Call after stream (re)-configuration is completed.
240 */
241 void configurationComplete();
242
243 /**
244 * Set or clear the list of repeating requests. Does not block
245 * on either. Use waitUntilPaused to wait until request queue
246 * has emptied out.
247 */
248 status_t setRepeatingRequests(const RequestList& requests);
249 status_t clearRepeatingRequests();
250
251 status_t queueRequest(sp<CaptureRequest> request);
252
253 /**
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700254 * Remove all queued and repeating requests, and pending triggers
255 */
256 status_t clear();
257
258 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700259 * Queue a trigger to be dispatched with the next outgoing
260 * process_capture_request. The settings for that request only
261 * will be temporarily rewritten to add the trigger tag/value.
262 * Subsequent requests will not be rewritten (for this tag).
263 */
264 status_t queueTrigger(RequestTrigger trigger[], size_t count);
265
266 /**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800267 * Pause/unpause the capture thread. Doesn't block, so use
268 * waitUntilPaused to wait until the thread is paused.
269 */
270 void setPaused(bool paused);
271
272 /**
273 * Wait until thread is paused, either due to setPaused(true)
274 * or due to lack of input requests. Returns TIMED_OUT in case
275 * the thread does not pause within the timeout.
276 */
277 status_t waitUntilPaused(nsecs_t timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800278
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700279 /**
280 * Wait until thread processes the capture request with settings'
281 * android.request.id == requestId.
282 *
283 * Returns TIMED_OUT in case the thread does not process the request
284 * within the timeout.
285 */
286 status_t waitUntilRequestProcessed(int32_t requestId, nsecs_t timeout);
287
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800288 protected:
289
290 virtual bool threadLoop();
291
292 private:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700293 static int getId(const wp<Camera3Device> &device);
294
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700295 status_t queueTriggerLocked(RequestTrigger trigger);
296 // Mix-in queued triggers into this request
297 int32_t insertTriggers(const sp<CaptureRequest> &request);
298 // Purge the queued triggers from this request,
299 // restoring the old field values for those tags.
300 status_t removeTriggers(const sp<CaptureRequest> &request);
301
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800302 static const nsecs_t kRequestTimeout = 50e6; // 50 ms
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800303
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800304 // Waits for a request, or returns NULL if times out.
305 sp<CaptureRequest> waitForNextRequest();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800306
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800307 // Return buffers, etc, for a request that couldn't be fully
308 // constructed. The buffers will be returned in the ERROR state
309 // to mark them as not having valid data.
310 // All arguments will be modified.
311 void cleanUpFailedRequest(camera3_capture_request_t &request,
312 sp<CaptureRequest> &nextRequest,
313 Vector<camera3_stream_buffer_t> &outputBuffers);
314
315 // Pause handling
316 bool waitIfPaused();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -0700317 void unpauseForNewRequests();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800318
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700319 // Relay error to parent device object setErrorState
320 void setErrorState(const char *fmt, ...);
321
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800322 wp<Camera3Device> mParent;
323 camera3_device_t *mHal3Device;
324
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700325 const int mId;
326
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800327 Mutex mRequestLock;
328 Condition mRequestSignal;
329 RequestList mRequestQueue;
330 RequestList mRepeatingRequests;
331
332 bool mReconfigured;
333
334 // Used by waitIfPaused, waitForNextRequest, and waitUntilPaused
335 Mutex mPauseLock;
336 bool mDoPause;
337 Condition mDoPauseSignal;
338 bool mPaused;
339 Condition mPausedSignal;
340
341 sp<CaptureRequest> mPrevRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700342 int32_t mPrevTriggers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800343
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700344 uint32_t mFrameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700345
346 Mutex mLatestRequestMutex;
347 Condition mLatestRequestSignal;
348 // android.request.id for latest process_capture_request
349 int32_t mLatestRequestId;
350
351 typedef KeyedVector<uint32_t/*tag*/, RequestTrigger> TriggerMap;
352 Mutex mTriggerMutex;
353 TriggerMap mTriggerMap;
354 TriggerMap mTriggerRemovedMap;
355 TriggerMap mTriggerReplacedMap;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800356 };
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800357 sp<RequestThread> mRequestThread;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800358
359 /**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700360 * In-flight queue for tracking completion of capture requests.
361 */
362
363 struct InFlightRequest {
364 // Set by notify() SHUTTER call.
365 nsecs_t captureTimestamp;
366 // Set by process_capture_result call with valid metadata
367 bool haveResultMetadata;
368 // Decremented by calls to process_capture_result with valid output
369 // buffers
370 int numBuffersLeft;
371
372 InFlightRequest() :
373 captureTimestamp(0),
374 haveResultMetadata(false),
375 numBuffersLeft(0) {
376 }
377
378 explicit InFlightRequest(int numBuffers) :
379 captureTimestamp(0),
380 haveResultMetadata(false),
381 numBuffersLeft(numBuffers) {
382 }
383 };
384 // Map from frame number to the in-flight request state
385 typedef KeyedVector<uint32_t, InFlightRequest> InFlightMap;
386
387 Mutex mInFlightLock; // Protects mInFlightMap
388 InFlightMap mInFlightMap;
389
390 status_t registerInFlight(int32_t frameNumber, int32_t numBuffers);
391
392 /**
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700393 * Output result queue and current HAL device 3A state
394 */
395
396 // Lock for output side of device
397 Mutex mOutputLock;
398
399 /**** Scope for mOutputLock ****/
400
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700401 uint32_t mNextResultFrameNumber;
402 uint32_t mNextShutterFrameNumber;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700403 List<CameraMetadata> mResultQueue;
404 Condition mResultSignal;
405 NotificationListener *mListener;
406
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700407 /**** End scope for mOutputLock ****/
408
409 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800410 * Callback functions from HAL device
411 */
412 void processCaptureResult(const camera3_capture_result *result);
413
414 void notify(const camera3_notify_msg *msg);
415
416 /**
417 * Static callback forwarding methods from HAL to instance
418 */
419 static callbacks_process_capture_result_t sProcessCaptureResult;
420
421 static callbacks_notify_t sNotify;
422
423}; // class Camera3Device
424
425}; // namespace android
426
427#endif