blob: 7a8c22a3f494a4162dd3d1cfed6206769ac207be [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;
152
153 // Need to hold on to stream references until configure completes.
Igor Murashkin2fba5842013-04-22 14:03:54 -0700154 Vector<sp<camera3::Camera3StreamInterface> > mDeletedStreams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800155
156 /**** End scope for mLock ****/
157
158 class CaptureRequest : public LightRefBase<CaptureRequest> {
159 public:
160 CameraMetadata mSettings;
161 sp<camera3::Camera3Stream> mInputStream;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700162 Vector<sp<camera3::Camera3OutputStreamInterface> >
163 mOutputStreams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800164 };
165 typedef List<sp<CaptureRequest> > RequestList;
166
167 /**
168 * Lock-held version of waitUntilDrained. Will transition to IDLE on
169 * success.
170 */
171 status_t waitUntilDrainedLocked();
172
173 /**
174 * Do common work for setting up a streaming or single capture request.
175 * On success, will transition to ACTIVE if in IDLE.
176 */
177 sp<CaptureRequest> setUpRequestLocked(const CameraMetadata &request);
178
179 /**
180 * Build a CaptureRequest request from the CameraDeviceBase request
181 * settings.
182 */
183 sp<CaptureRequest> createCaptureRequest(const CameraMetadata &request);
184
185 /**
186 * Take the currently-defined set of streams and configure the HAL to use
187 * them. This is a long-running operation (may be several hundered ms).
188 */
189 status_t configureStreamsLocked();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800190
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700191 /**
192 * Set device into an error state due to some fatal failure, and set an
193 * error message to indicate why. Only the first call's message will be
194 * used. The message is also sent to the log.
195 */
196 void setErrorState(const char *fmt, ...);
197 void setErrorStateV(const char *fmt, va_list args);
198 void setErrorStateLocked(const char *fmt, ...);
199 void setErrorStateLockedV(const char *fmt, va_list args);
200
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700201 struct RequestTrigger {
202 // Metadata tag number, e.g. android.control.aePrecaptureTrigger
203 uint32_t metadataTag;
204 // Metadata value, e.g. 'START' or the trigger ID
205 int32_t entryValue;
206
207 // The last part of the fully qualified path, e.g. afTrigger
208 const char *getTagName() const {
209 return get_camera_metadata_tag_name(metadataTag) ?: "NULL";
210 }
211
212 // e.g. TYPE_BYTE, TYPE_INT32, etc.
213 int getTagType() const {
214 return get_camera_metadata_tag_type(metadataTag);
215 }
216 };
217
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800218 /**
219 * Thread for managing capture request submission to HAL device.
220 */
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800221 class RequestThread : public Thread {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800222
223 public:
224
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800225 RequestThread(wp<Camera3Device> parent,
226 camera3_device_t *hal3Device);
227
228 /**
229 * Call after stream (re)-configuration is completed.
230 */
231 void configurationComplete();
232
233 /**
234 * Set or clear the list of repeating requests. Does not block
235 * on either. Use waitUntilPaused to wait until request queue
236 * has emptied out.
237 */
238 status_t setRepeatingRequests(const RequestList& requests);
239 status_t clearRepeatingRequests();
240
241 status_t queueRequest(sp<CaptureRequest> request);
242
243 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700244 * Queue a trigger to be dispatched with the next outgoing
245 * process_capture_request. The settings for that request only
246 * will be temporarily rewritten to add the trigger tag/value.
247 * Subsequent requests will not be rewritten (for this tag).
248 */
249 status_t queueTrigger(RequestTrigger trigger[], size_t count);
250
251 /**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800252 * Pause/unpause the capture thread. Doesn't block, so use
253 * waitUntilPaused to wait until the thread is paused.
254 */
255 void setPaused(bool paused);
256
257 /**
258 * Wait until thread is paused, either due to setPaused(true)
259 * or due to lack of input requests. Returns TIMED_OUT in case
260 * the thread does not pause within the timeout.
261 */
262 status_t waitUntilPaused(nsecs_t timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800263
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700264 /**
265 * Wait until thread processes the capture request with settings'
266 * android.request.id == requestId.
267 *
268 * Returns TIMED_OUT in case the thread does not process the request
269 * within the timeout.
270 */
271 status_t waitUntilRequestProcessed(int32_t requestId, nsecs_t timeout);
272
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800273 protected:
274
275 virtual bool threadLoop();
276
277 private:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700278 static int getId(const wp<Camera3Device> &device);
279
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700280 status_t queueTriggerLocked(RequestTrigger trigger);
281 // Mix-in queued triggers into this request
282 int32_t insertTriggers(const sp<CaptureRequest> &request);
283 // Purge the queued triggers from this request,
284 // restoring the old field values for those tags.
285 status_t removeTriggers(const sp<CaptureRequest> &request);
286
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800287 static const nsecs_t kRequestTimeout = 50e6; // 50 ms
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800288
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800289 // Waits for a request, or returns NULL if times out.
290 sp<CaptureRequest> waitForNextRequest();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800291
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800292 // Return buffers, etc, for a request that couldn't be fully
293 // constructed. The buffers will be returned in the ERROR state
294 // to mark them as not having valid data.
295 // All arguments will be modified.
296 void cleanUpFailedRequest(camera3_capture_request_t &request,
297 sp<CaptureRequest> &nextRequest,
298 Vector<camera3_stream_buffer_t> &outputBuffers);
299
300 // Pause handling
301 bool waitIfPaused();
302
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700303 // Relay error to parent device object setErrorState
304 void setErrorState(const char *fmt, ...);
305
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800306 wp<Camera3Device> mParent;
307 camera3_device_t *mHal3Device;
308
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700309 const int mId;
310
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800311 Mutex mRequestLock;
312 Condition mRequestSignal;
313 RequestList mRequestQueue;
314 RequestList mRepeatingRequests;
315
316 bool mReconfigured;
317
318 // Used by waitIfPaused, waitForNextRequest, and waitUntilPaused
319 Mutex mPauseLock;
320 bool mDoPause;
321 Condition mDoPauseSignal;
322 bool mPaused;
323 Condition mPausedSignal;
324
325 sp<CaptureRequest> mPrevRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700326 int32_t mPrevTriggers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800327
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700328 uint32_t mFrameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700329
330 Mutex mLatestRequestMutex;
331 Condition mLatestRequestSignal;
332 // android.request.id for latest process_capture_request
333 int32_t mLatestRequestId;
334
335 typedef KeyedVector<uint32_t/*tag*/, RequestTrigger> TriggerMap;
336 Mutex mTriggerMutex;
337 TriggerMap mTriggerMap;
338 TriggerMap mTriggerRemovedMap;
339 TriggerMap mTriggerReplacedMap;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800340 };
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800341 sp<RequestThread> mRequestThread;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800342
343 /**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700344 * In-flight queue for tracking completion of capture requests.
345 */
346
347 struct InFlightRequest {
348 // Set by notify() SHUTTER call.
349 nsecs_t captureTimestamp;
350 // Set by process_capture_result call with valid metadata
351 bool haveResultMetadata;
352 // Decremented by calls to process_capture_result with valid output
353 // buffers
354 int numBuffersLeft;
355
356 InFlightRequest() :
357 captureTimestamp(0),
358 haveResultMetadata(false),
359 numBuffersLeft(0) {
360 }
361
362 explicit InFlightRequest(int numBuffers) :
363 captureTimestamp(0),
364 haveResultMetadata(false),
365 numBuffersLeft(numBuffers) {
366 }
367 };
368 // Map from frame number to the in-flight request state
369 typedef KeyedVector<uint32_t, InFlightRequest> InFlightMap;
370
371 Mutex mInFlightLock; // Protects mInFlightMap
372 InFlightMap mInFlightMap;
373
374 status_t registerInFlight(int32_t frameNumber, int32_t numBuffers);
375
376 /**
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700377 * Output result queue and current HAL device 3A state
378 */
379
380 // Lock for output side of device
381 Mutex mOutputLock;
382
383 /**** Scope for mOutputLock ****/
384
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700385 uint32_t mNextResultFrameNumber;
386 uint32_t mNextShutterFrameNumber;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700387 List<CameraMetadata> mResultQueue;
388 Condition mResultSignal;
389 NotificationListener *mListener;
390
391 struct AlgState {
392 camera_metadata_enum_android_control_ae_state aeState;
393 camera_metadata_enum_android_control_af_state afState;
394 camera_metadata_enum_android_control_awb_state awbState;
395
396 AlgState() :
397 aeState(ANDROID_CONTROL_AE_STATE_INACTIVE),
398 afState(ANDROID_CONTROL_AF_STATE_INACTIVE),
399 awbState(ANDROID_CONTROL_AWB_STATE_INACTIVE) {
400 }
401 } m3AState;
402
403 /**** End scope for mOutputLock ****/
404
405 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800406 * Callback functions from HAL device
407 */
408 void processCaptureResult(const camera3_capture_result *result);
409
410 void notify(const camera3_notify_msg *msg);
411
412 /**
413 * Static callback forwarding methods from HAL to instance
414 */
415 static callbacks_process_capture_result_t sProcessCaptureResult;
416
417 static callbacks_notify_t sNotify;
418
419}; // class Camera3Device
420
421}; // namespace android
422
423#endif