blob: faa42b9b65044017d45dfabd7b38c109d1448449 [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"
Igor Murashkin2fba5842013-04-22 14:03:54 -070029#include "camera3/Camera3ZslStream.h"
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080030
31#include "hardware/camera3.h"
32
33/**
34 * Function pointer types with C calling convention to
35 * use for HAL callback functions.
36 */
37extern "C" {
38 typedef void (callbacks_process_capture_result_t)(
39 const struct camera3_callback_ops *,
40 const camera3_capture_result_t *);
41
42 typedef void (callbacks_notify_t)(
43 const struct camera3_callback_ops *,
44 const camera3_notify_msg_t *);
45}
46
47namespace android {
48
49/**
50 * CameraDevice for HAL devices with version CAMERA_DEVICE_API_VERSION_3_0
51 */
52class Camera3Device :
53 public CameraDeviceBase,
54 private camera3_callback_ops {
55 public:
56 Camera3Device(int id);
57
58 virtual ~Camera3Device();
59
60 /**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080061 * CameraDeviceBase interface
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080062 */
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080063
Igor Murashkin71381052013-03-04 14:53:08 -080064 virtual int getId() const;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080065
66 // Transitions to idle state on success.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080067 virtual status_t initialize(camera_module_t *module);
68 virtual status_t disconnect();
69 virtual status_t dump(int fd, const Vector<String16> &args);
70 virtual const CameraMetadata& info() const;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080071
72 // Capture and setStreamingRequest will configure streams if currently in
73 // idle state
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080074 virtual status_t capture(CameraMetadata &request);
75 virtual status_t setStreamingRequest(const CameraMetadata &request);
76 virtual status_t clearStreamingRequest();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080077
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080078 virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080079
80 // Actual stream creation/deletion is delayed until first request is submitted
81 // If adding streams while actively capturing, will pause device before adding
82 // stream, reconfiguring device, and unpausing.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080083 virtual status_t createStream(sp<ANativeWindow> consumer,
84 uint32_t width, uint32_t height, int format, size_t size,
85 int *id);
Igor Murashkin5a269fa2013-04-15 14:59:22 -070086 virtual status_t createInputStream(
87 uint32_t width, uint32_t height, int format,
88 int *id);
Igor Murashkin2fba5842013-04-22 14:03:54 -070089 virtual status_t createZslStream(
90 uint32_t width, uint32_t height,
91 int depth,
92 /*out*/
93 int *id,
94 sp<camera3::Camera3ZslStream>* zslStream);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080095 virtual status_t createReprocessStreamFromStream(int outputId, int *id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080096
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080097 virtual status_t getStreamInfo(int id,
98 uint32_t *width, uint32_t *height, uint32_t *format);
99 virtual status_t setStreamTransform(int id, int transform);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800100
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800101 virtual status_t deleteStream(int id);
102 virtual status_t deleteReprocessStream(int id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800103
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800104 virtual status_t createDefaultRequest(int templateId, CameraMetadata *request);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800105
106 // Transitions to the idle state on success
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800107 virtual status_t waitUntilDrained();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800108
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800109 virtual status_t setNotifyCallback(NotificationListener *listener);
110 virtual status_t waitForNextFrame(nsecs_t timeout);
111 virtual status_t getNextFrame(CameraMetadata *frame);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800112
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800113 virtual status_t triggerAutofocus(uint32_t id);
114 virtual status_t triggerCancelAutofocus(uint32_t id);
115 virtual status_t triggerPrecaptureMetering(uint32_t id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800116
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800117 virtual status_t pushReprocessBuffer(int reprocessStreamId,
118 buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
119
120 private:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700121 static const size_t kInFlightWarnLimit = 20;
122 static const nsecs_t kShutdownTimeout = 5000000000; // 5 sec
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700123 struct RequestTrigger;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800124
125 Mutex mLock;
126
127 /**** Scope for mLock ****/
128
129 const int mId;
130 camera3_device_t *mHal3Device;
131
132 CameraMetadata mDeviceInfo;
133 vendor_tag_query_ops_t mVendorTagOps;
134
135 enum {
136 STATUS_ERROR,
137 STATUS_UNINITIALIZED,
138 STATUS_IDLE,
139 STATUS_ACTIVE
140 } mStatus;
141
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700142 // Tracking cause of fatal errors when in STATUS_ERROR
143 String8 mErrorCause;
144
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800145 // Mapping of stream IDs to stream instances
Igor Murashkin2fba5842013-04-22 14:03:54 -0700146 typedef KeyedVector<int, sp<camera3::Camera3OutputStreamInterface> >
147 StreamSet;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800148
149 StreamSet mOutputStreams;
150 sp<camera3::Camera3Stream> mInputStream;
151 int mNextStreamId;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700152 bool mNeedConfig;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800153
154 // Need to hold on to stream references until configure completes.
Igor Murashkin2fba5842013-04-22 14:03:54 -0700155 Vector<sp<camera3::Camera3StreamInterface> > mDeletedStreams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800156
157 /**** End scope for mLock ****/
158
159 class CaptureRequest : public LightRefBase<CaptureRequest> {
160 public:
161 CameraMetadata mSettings;
162 sp<camera3::Camera3Stream> mInputStream;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700163 Vector<sp<camera3::Camera3OutputStreamInterface> >
164 mOutputStreams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800165 };
166 typedef List<sp<CaptureRequest> > RequestList;
167
168 /**
169 * Lock-held version of waitUntilDrained. Will transition to IDLE on
170 * success.
171 */
172 status_t waitUntilDrainedLocked();
173
174 /**
175 * Do common work for setting up a streaming or single capture request.
176 * On success, will transition to ACTIVE if in IDLE.
177 */
178 sp<CaptureRequest> setUpRequestLocked(const CameraMetadata &request);
179
180 /**
181 * Build a CaptureRequest request from the CameraDeviceBase request
182 * settings.
183 */
184 sp<CaptureRequest> createCaptureRequest(const CameraMetadata &request);
185
186 /**
187 * Take the currently-defined set of streams and configure the HAL to use
188 * them. This is a long-running operation (may be several hundered ms).
189 */
190 status_t configureStreamsLocked();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800191
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700192 /**
193 * Set device into an error state due to some fatal failure, and set an
194 * error message to indicate why. Only the first call's message will be
195 * used. The message is also sent to the log.
196 */
197 void setErrorState(const char *fmt, ...);
198 void setErrorStateV(const char *fmt, va_list args);
199 void setErrorStateLocked(const char *fmt, ...);
200 void setErrorStateLockedV(const char *fmt, va_list args);
201
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700202 struct RequestTrigger {
203 // Metadata tag number, e.g. android.control.aePrecaptureTrigger
204 uint32_t metadataTag;
205 // Metadata value, e.g. 'START' or the trigger ID
206 int32_t entryValue;
207
208 // The last part of the fully qualified path, e.g. afTrigger
209 const char *getTagName() const {
210 return get_camera_metadata_tag_name(metadataTag) ?: "NULL";
211 }
212
213 // e.g. TYPE_BYTE, TYPE_INT32, etc.
214 int getTagType() const {
215 return get_camera_metadata_tag_type(metadataTag);
216 }
217 };
218
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800219 /**
220 * Thread for managing capture request submission to HAL device.
221 */
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800222 class RequestThread : public Thread {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800223
224 public:
225
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800226 RequestThread(wp<Camera3Device> parent,
227 camera3_device_t *hal3Device);
228
229 /**
230 * Call after stream (re)-configuration is completed.
231 */
232 void configurationComplete();
233
234 /**
235 * Set or clear the list of repeating requests. Does not block
236 * on either. Use waitUntilPaused to wait until request queue
237 * has emptied out.
238 */
239 status_t setRepeatingRequests(const RequestList& requests);
240 status_t clearRepeatingRequests();
241
242 status_t queueRequest(sp<CaptureRequest> request);
243
244 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700245 * Queue a trigger to be dispatched with the next outgoing
246 * process_capture_request. The settings for that request only
247 * will be temporarily rewritten to add the trigger tag/value.
248 * Subsequent requests will not be rewritten (for this tag).
249 */
250 status_t queueTrigger(RequestTrigger trigger[], size_t count);
251
252 /**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800253 * Pause/unpause the capture thread. Doesn't block, so use
254 * waitUntilPaused to wait until the thread is paused.
255 */
256 void setPaused(bool paused);
257
258 /**
259 * Wait until thread is paused, either due to setPaused(true)
260 * or due to lack of input requests. Returns TIMED_OUT in case
261 * the thread does not pause within the timeout.
262 */
263 status_t waitUntilPaused(nsecs_t timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800264
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700265 /**
266 * Wait until thread processes the capture request with settings'
267 * android.request.id == requestId.
268 *
269 * Returns TIMED_OUT in case the thread does not process the request
270 * within the timeout.
271 */
272 status_t waitUntilRequestProcessed(int32_t requestId, nsecs_t timeout);
273
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800274 protected:
275
276 virtual bool threadLoop();
277
278 private:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700279 static int getId(const wp<Camera3Device> &device);
280
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700281 status_t queueTriggerLocked(RequestTrigger trigger);
282 // Mix-in queued triggers into this request
283 int32_t insertTriggers(const sp<CaptureRequest> &request);
284 // Purge the queued triggers from this request,
285 // restoring the old field values for those tags.
286 status_t removeTriggers(const sp<CaptureRequest> &request);
287
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800288 static const nsecs_t kRequestTimeout = 50e6; // 50 ms
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800289
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800290 // Waits for a request, or returns NULL if times out.
291 sp<CaptureRequest> waitForNextRequest();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800292
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800293 // Return buffers, etc, for a request that couldn't be fully
294 // constructed. The buffers will be returned in the ERROR state
295 // to mark them as not having valid data.
296 // All arguments will be modified.
297 void cleanUpFailedRequest(camera3_capture_request_t &request,
298 sp<CaptureRequest> &nextRequest,
299 Vector<camera3_stream_buffer_t> &outputBuffers);
300
301 // Pause handling
302 bool waitIfPaused();
303
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700304 // Relay error to parent device object setErrorState
305 void setErrorState(const char *fmt, ...);
306
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800307 wp<Camera3Device> mParent;
308 camera3_device_t *mHal3Device;
309
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700310 const int mId;
311
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800312 Mutex mRequestLock;
313 Condition mRequestSignal;
314 RequestList mRequestQueue;
315 RequestList mRepeatingRequests;
316
317 bool mReconfigured;
318
319 // Used by waitIfPaused, waitForNextRequest, and waitUntilPaused
320 Mutex mPauseLock;
321 bool mDoPause;
322 Condition mDoPauseSignal;
323 bool mPaused;
324 Condition mPausedSignal;
325
326 sp<CaptureRequest> mPrevRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700327 int32_t mPrevTriggers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800328
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700329 uint32_t mFrameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700330
331 Mutex mLatestRequestMutex;
332 Condition mLatestRequestSignal;
333 // android.request.id for latest process_capture_request
334 int32_t mLatestRequestId;
335
336 typedef KeyedVector<uint32_t/*tag*/, RequestTrigger> TriggerMap;
337 Mutex mTriggerMutex;
338 TriggerMap mTriggerMap;
339 TriggerMap mTriggerRemovedMap;
340 TriggerMap mTriggerReplacedMap;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800341 };
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800342 sp<RequestThread> mRequestThread;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800343
344 /**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700345 * In-flight queue for tracking completion of capture requests.
346 */
347
348 struct InFlightRequest {
349 // Set by notify() SHUTTER call.
350 nsecs_t captureTimestamp;
351 // Set by process_capture_result call with valid metadata
352 bool haveResultMetadata;
353 // Decremented by calls to process_capture_result with valid output
354 // buffers
355 int numBuffersLeft;
356
357 InFlightRequest() :
358 captureTimestamp(0),
359 haveResultMetadata(false),
360 numBuffersLeft(0) {
361 }
362
363 explicit InFlightRequest(int numBuffers) :
364 captureTimestamp(0),
365 haveResultMetadata(false),
366 numBuffersLeft(numBuffers) {
367 }
368 };
369 // Map from frame number to the in-flight request state
370 typedef KeyedVector<uint32_t, InFlightRequest> InFlightMap;
371
372 Mutex mInFlightLock; // Protects mInFlightMap
373 InFlightMap mInFlightMap;
374
375 status_t registerInFlight(int32_t frameNumber, int32_t numBuffers);
376
377 /**
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700378 * Output result queue and current HAL device 3A state
379 */
380
381 // Lock for output side of device
382 Mutex mOutputLock;
383
384 /**** Scope for mOutputLock ****/
385
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700386 uint32_t mNextResultFrameNumber;
387 uint32_t mNextShutterFrameNumber;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700388 List<CameraMetadata> mResultQueue;
389 Condition mResultSignal;
390 NotificationListener *mListener;
391
392 struct AlgState {
393 camera_metadata_enum_android_control_ae_state aeState;
394 camera_metadata_enum_android_control_af_state afState;
395 camera_metadata_enum_android_control_awb_state awbState;
396
397 AlgState() :
398 aeState(ANDROID_CONTROL_AE_STATE_INACTIVE),
399 afState(ANDROID_CONTROL_AF_STATE_INACTIVE),
400 awbState(ANDROID_CONTROL_AWB_STATE_INACTIVE) {
401 }
402 } m3AState;
403
404 /**** End scope for mOutputLock ****/
405
406 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800407 * Callback functions from HAL device
408 */
409 void processCaptureResult(const camera3_capture_result *result);
410
411 void notify(const camera3_notify_msg *msg);
412
413 /**
414 * Static callback forwarding methods from HAL to instance
415 */
416 static callbacks_process_capture_result_t sProcessCaptureResult;
417
418 static callbacks_notify_t sNotify;
419
420}; // class Camera3Device
421
422}; // namespace android
423
424#endif