blob: 2328f89462ac0040e16df52a7688cf7592b7aa3c [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);
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -0700110 virtual bool willNotify3A();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800111 virtual status_t waitForNextFrame(nsecs_t timeout);
112 virtual status_t getNextFrame(CameraMetadata *frame);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800113
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800114 virtual status_t triggerAutofocus(uint32_t id);
115 virtual status_t triggerCancelAutofocus(uint32_t id);
116 virtual status_t triggerPrecaptureMetering(uint32_t id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800117
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800118 virtual status_t pushReprocessBuffer(int reprocessStreamId,
119 buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
120
121 private:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700122 static const size_t kInFlightWarnLimit = 20;
123 static const nsecs_t kShutdownTimeout = 5000000000; // 5 sec
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700124 struct RequestTrigger;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800125
126 Mutex mLock;
127
128 /**** Scope for mLock ****/
129
130 const int mId;
131 camera3_device_t *mHal3Device;
132
133 CameraMetadata mDeviceInfo;
134 vendor_tag_query_ops_t mVendorTagOps;
135
136 enum {
137 STATUS_ERROR,
138 STATUS_UNINITIALIZED,
139 STATUS_IDLE,
140 STATUS_ACTIVE
141 } mStatus;
142
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700143 // Tracking cause of fatal errors when in STATUS_ERROR
144 String8 mErrorCause;
145
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800146 // Mapping of stream IDs to stream instances
Igor Murashkin2fba5842013-04-22 14:03:54 -0700147 typedef KeyedVector<int, sp<camera3::Camera3OutputStreamInterface> >
148 StreamSet;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800149
150 StreamSet mOutputStreams;
151 sp<camera3::Camera3Stream> mInputStream;
152 int mNextStreamId;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700153 bool mNeedConfig;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800154
155 // Need to hold on to stream references until configure completes.
Igor Murashkin2fba5842013-04-22 14:03:54 -0700156 Vector<sp<camera3::Camera3StreamInterface> > mDeletedStreams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800157
158 /**** End scope for mLock ****/
159
160 class CaptureRequest : public LightRefBase<CaptureRequest> {
161 public:
162 CameraMetadata mSettings;
163 sp<camera3::Camera3Stream> mInputStream;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700164 Vector<sp<camera3::Camera3OutputStreamInterface> >
165 mOutputStreams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800166 };
167 typedef List<sp<CaptureRequest> > RequestList;
168
169 /**
170 * Lock-held version of waitUntilDrained. Will transition to IDLE on
171 * success.
172 */
173 status_t waitUntilDrainedLocked();
174
175 /**
176 * Do common work for setting up a streaming or single capture request.
177 * On success, will transition to ACTIVE if in IDLE.
178 */
179 sp<CaptureRequest> setUpRequestLocked(const CameraMetadata &request);
180
181 /**
182 * Build a CaptureRequest request from the CameraDeviceBase request
183 * settings.
184 */
185 sp<CaptureRequest> createCaptureRequest(const CameraMetadata &request);
186
187 /**
188 * Take the currently-defined set of streams and configure the HAL to use
189 * them. This is a long-running operation (may be several hundered ms).
190 */
191 status_t configureStreamsLocked();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700193 /**
194 * Set device into an error state due to some fatal failure, and set an
195 * error message to indicate why. Only the first call's message will be
196 * used. The message is also sent to the log.
197 */
198 void setErrorState(const char *fmt, ...);
199 void setErrorStateV(const char *fmt, va_list args);
200 void setErrorStateLocked(const char *fmt, ...);
201 void setErrorStateLockedV(const char *fmt, va_list args);
202
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700203 struct RequestTrigger {
204 // Metadata tag number, e.g. android.control.aePrecaptureTrigger
205 uint32_t metadataTag;
206 // Metadata value, e.g. 'START' or the trigger ID
207 int32_t entryValue;
208
209 // The last part of the fully qualified path, e.g. afTrigger
210 const char *getTagName() const {
211 return get_camera_metadata_tag_name(metadataTag) ?: "NULL";
212 }
213
214 // e.g. TYPE_BYTE, TYPE_INT32, etc.
215 int getTagType() const {
216 return get_camera_metadata_tag_type(metadataTag);
217 }
218 };
219
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800220 /**
221 * Thread for managing capture request submission to HAL device.
222 */
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800223 class RequestThread : public Thread {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800224
225 public:
226
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800227 RequestThread(wp<Camera3Device> parent,
228 camera3_device_t *hal3Device);
229
230 /**
231 * Call after stream (re)-configuration is completed.
232 */
233 void configurationComplete();
234
235 /**
236 * Set or clear the list of repeating requests. Does not block
237 * on either. Use waitUntilPaused to wait until request queue
238 * has emptied out.
239 */
240 status_t setRepeatingRequests(const RequestList& requests);
241 status_t clearRepeatingRequests();
242
243 status_t queueRequest(sp<CaptureRequest> request);
244
245 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700246 * Queue a trigger to be dispatched with the next outgoing
247 * process_capture_request. The settings for that request only
248 * will be temporarily rewritten to add the trigger tag/value.
249 * Subsequent requests will not be rewritten (for this tag).
250 */
251 status_t queueTrigger(RequestTrigger trigger[], size_t count);
252
253 /**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800254 * Pause/unpause the capture thread. Doesn't block, so use
255 * waitUntilPaused to wait until the thread is paused.
256 */
257 void setPaused(bool paused);
258
259 /**
260 * Wait until thread is paused, either due to setPaused(true)
261 * or due to lack of input requests. Returns TIMED_OUT in case
262 * the thread does not pause within the timeout.
263 */
264 status_t waitUntilPaused(nsecs_t timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800265
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700266 /**
267 * Wait until thread processes the capture request with settings'
268 * android.request.id == requestId.
269 *
270 * Returns TIMED_OUT in case the thread does not process the request
271 * within the timeout.
272 */
273 status_t waitUntilRequestProcessed(int32_t requestId, nsecs_t timeout);
274
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800275 protected:
276
277 virtual bool threadLoop();
278
279 private:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700280 static int getId(const wp<Camera3Device> &device);
281
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700282 status_t queueTriggerLocked(RequestTrigger trigger);
283 // Mix-in queued triggers into this request
284 int32_t insertTriggers(const sp<CaptureRequest> &request);
285 // Purge the queued triggers from this request,
286 // restoring the old field values for those tags.
287 status_t removeTriggers(const sp<CaptureRequest> &request);
288
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800289 static const nsecs_t kRequestTimeout = 50e6; // 50 ms
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800290
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800291 // Waits for a request, or returns NULL if times out.
292 sp<CaptureRequest> waitForNextRequest();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800293
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800294 // Return buffers, etc, for a request that couldn't be fully
295 // constructed. The buffers will be returned in the ERROR state
296 // to mark them as not having valid data.
297 // All arguments will be modified.
298 void cleanUpFailedRequest(camera3_capture_request_t &request,
299 sp<CaptureRequest> &nextRequest,
300 Vector<camera3_stream_buffer_t> &outputBuffers);
301
302 // Pause handling
303 bool waitIfPaused();
304
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700305 // Relay error to parent device object setErrorState
306 void setErrorState(const char *fmt, ...);
307
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800308 wp<Camera3Device> mParent;
309 camera3_device_t *mHal3Device;
310
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700311 const int mId;
312
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800313 Mutex mRequestLock;
314 Condition mRequestSignal;
315 RequestList mRequestQueue;
316 RequestList mRepeatingRequests;
317
318 bool mReconfigured;
319
320 // Used by waitIfPaused, waitForNextRequest, and waitUntilPaused
321 Mutex mPauseLock;
322 bool mDoPause;
323 Condition mDoPauseSignal;
324 bool mPaused;
325 Condition mPausedSignal;
326
327 sp<CaptureRequest> mPrevRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700328 int32_t mPrevTriggers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800329
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700330 uint32_t mFrameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700331
332 Mutex mLatestRequestMutex;
333 Condition mLatestRequestSignal;
334 // android.request.id for latest process_capture_request
335 int32_t mLatestRequestId;
336
337 typedef KeyedVector<uint32_t/*tag*/, RequestTrigger> TriggerMap;
338 Mutex mTriggerMutex;
339 TriggerMap mTriggerMap;
340 TriggerMap mTriggerRemovedMap;
341 TriggerMap mTriggerReplacedMap;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800342 };
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800343 sp<RequestThread> mRequestThread;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800344
345 /**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700346 * In-flight queue for tracking completion of capture requests.
347 */
348
349 struct InFlightRequest {
350 // Set by notify() SHUTTER call.
351 nsecs_t captureTimestamp;
352 // Set by process_capture_result call with valid metadata
353 bool haveResultMetadata;
354 // Decremented by calls to process_capture_result with valid output
355 // buffers
356 int numBuffersLeft;
357
358 InFlightRequest() :
359 captureTimestamp(0),
360 haveResultMetadata(false),
361 numBuffersLeft(0) {
362 }
363
364 explicit InFlightRequest(int numBuffers) :
365 captureTimestamp(0),
366 haveResultMetadata(false),
367 numBuffersLeft(numBuffers) {
368 }
369 };
370 // Map from frame number to the in-flight request state
371 typedef KeyedVector<uint32_t, InFlightRequest> InFlightMap;
372
373 Mutex mInFlightLock; // Protects mInFlightMap
374 InFlightMap mInFlightMap;
375
376 status_t registerInFlight(int32_t frameNumber, int32_t numBuffers);
377
378 /**
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700379 * Output result queue and current HAL device 3A state
380 */
381
382 // Lock for output side of device
383 Mutex mOutputLock;
384
385 /**** Scope for mOutputLock ****/
386
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700387 uint32_t mNextResultFrameNumber;
388 uint32_t mNextShutterFrameNumber;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700389 List<CameraMetadata> mResultQueue;
390 Condition mResultSignal;
391 NotificationListener *mListener;
392
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700393 /**** End scope for mOutputLock ****/
394
395 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800396 * Callback functions from HAL device
397 */
398 void processCaptureResult(const camera3_capture_result *result);
399
400 void notify(const camera3_notify_msg *msg);
401
402 /**
403 * Static callback forwarding methods from HAL to instance
404 */
405 static callbacks_process_capture_result_t sProcessCaptureResult;
406
407 static callbacks_notify_t sNotify;
408
409}; // class Camera3Device
410
411}; // namespace android
412
413#endif