blob: 76c08ae787cd321648ef73cf22eb2a3baaeea069 [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
127 private:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700128 static const size_t kInFlightWarnLimit = 20;
129 static const nsecs_t kShutdownTimeout = 5000000000; // 5 sec
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700130 struct RequestTrigger;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800131
132 Mutex mLock;
133
134 /**** Scope for mLock ****/
135
136 const int mId;
137 camera3_device_t *mHal3Device;
138
139 CameraMetadata mDeviceInfo;
140 vendor_tag_query_ops_t mVendorTagOps;
141
142 enum {
143 STATUS_ERROR,
144 STATUS_UNINITIALIZED,
145 STATUS_IDLE,
146 STATUS_ACTIVE
147 } mStatus;
148
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700149 // Tracking cause of fatal errors when in STATUS_ERROR
150 String8 mErrorCause;
151
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800152 // Mapping of stream IDs to stream instances
Igor Murashkin2fba5842013-04-22 14:03:54 -0700153 typedef KeyedVector<int, sp<camera3::Camera3OutputStreamInterface> >
154 StreamSet;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800155
156 StreamSet mOutputStreams;
157 sp<camera3::Camera3Stream> mInputStream;
158 int mNextStreamId;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700159 bool mNeedConfig;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800160
161 // Need to hold on to stream references until configure completes.
Igor Murashkin2fba5842013-04-22 14:03:54 -0700162 Vector<sp<camera3::Camera3StreamInterface> > mDeletedStreams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800163
164 /**** End scope for mLock ****/
165
166 class CaptureRequest : public LightRefBase<CaptureRequest> {
167 public:
168 CameraMetadata mSettings;
169 sp<camera3::Camera3Stream> mInputStream;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700170 Vector<sp<camera3::Camera3OutputStreamInterface> >
171 mOutputStreams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800172 };
173 typedef List<sp<CaptureRequest> > RequestList;
174
175 /**
176 * Lock-held version of waitUntilDrained. Will transition to IDLE on
177 * success.
178 */
179 status_t waitUntilDrainedLocked();
180
181 /**
182 * Do common work for setting up a streaming or single capture request.
183 * On success, will transition to ACTIVE if in IDLE.
184 */
185 sp<CaptureRequest> setUpRequestLocked(const CameraMetadata &request);
186
187 /**
188 * Build a CaptureRequest request from the CameraDeviceBase request
189 * settings.
190 */
191 sp<CaptureRequest> createCaptureRequest(const CameraMetadata &request);
192
193 /**
194 * Take the currently-defined set of streams and configure the HAL to use
195 * them. This is a long-running operation (may be several hundered ms).
196 */
197 status_t configureStreamsLocked();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800198
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700199 /**
200 * Set device into an error state due to some fatal failure, and set an
201 * error message to indicate why. Only the first call's message will be
202 * used. The message is also sent to the log.
203 */
204 void setErrorState(const char *fmt, ...);
205 void setErrorStateV(const char *fmt, va_list args);
206 void setErrorStateLocked(const char *fmt, ...);
207 void setErrorStateLockedV(const char *fmt, va_list args);
208
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700209 struct RequestTrigger {
210 // Metadata tag number, e.g. android.control.aePrecaptureTrigger
211 uint32_t metadataTag;
212 // Metadata value, e.g. 'START' or the trigger ID
213 int32_t entryValue;
214
215 // The last part of the fully qualified path, e.g. afTrigger
216 const char *getTagName() const {
217 return get_camera_metadata_tag_name(metadataTag) ?: "NULL";
218 }
219
220 // e.g. TYPE_BYTE, TYPE_INT32, etc.
221 int getTagType() const {
222 return get_camera_metadata_tag_type(metadataTag);
223 }
224 };
225
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800226 /**
227 * Thread for managing capture request submission to HAL device.
228 */
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800229 class RequestThread : public Thread {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800230
231 public:
232
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800233 RequestThread(wp<Camera3Device> parent,
234 camera3_device_t *hal3Device);
235
236 /**
237 * Call after stream (re)-configuration is completed.
238 */
239 void configurationComplete();
240
241 /**
242 * Set or clear the list of repeating requests. Does not block
243 * on either. Use waitUntilPaused to wait until request queue
244 * has emptied out.
245 */
246 status_t setRepeatingRequests(const RequestList& requests);
247 status_t clearRepeatingRequests();
248
249 status_t queueRequest(sp<CaptureRequest> request);
250
251 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700252 * Queue a trigger to be dispatched with the next outgoing
253 * process_capture_request. The settings for that request only
254 * will be temporarily rewritten to add the trigger tag/value.
255 * Subsequent requests will not be rewritten (for this tag).
256 */
257 status_t queueTrigger(RequestTrigger trigger[], size_t count);
258
259 /**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800260 * Pause/unpause the capture thread. Doesn't block, so use
261 * waitUntilPaused to wait until the thread is paused.
262 */
263 void setPaused(bool paused);
264
265 /**
266 * Wait until thread is paused, either due to setPaused(true)
267 * or due to lack of input requests. Returns TIMED_OUT in case
268 * the thread does not pause within the timeout.
269 */
270 status_t waitUntilPaused(nsecs_t timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800271
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700272 /**
273 * Wait until thread processes the capture request with settings'
274 * android.request.id == requestId.
275 *
276 * Returns TIMED_OUT in case the thread does not process the request
277 * within the timeout.
278 */
279 status_t waitUntilRequestProcessed(int32_t requestId, nsecs_t timeout);
280
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800281 protected:
282
283 virtual bool threadLoop();
284
285 private:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700286 static int getId(const wp<Camera3Device> &device);
287
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700288 status_t queueTriggerLocked(RequestTrigger trigger);
289 // Mix-in queued triggers into this request
290 int32_t insertTriggers(const sp<CaptureRequest> &request);
291 // Purge the queued triggers from this request,
292 // restoring the old field values for those tags.
293 status_t removeTriggers(const sp<CaptureRequest> &request);
294
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800295 static const nsecs_t kRequestTimeout = 50e6; // 50 ms
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800296
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800297 // Waits for a request, or returns NULL if times out.
298 sp<CaptureRequest> waitForNextRequest();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800299
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800300 // Return buffers, etc, for a request that couldn't be fully
301 // constructed. The buffers will be returned in the ERROR state
302 // to mark them as not having valid data.
303 // All arguments will be modified.
304 void cleanUpFailedRequest(camera3_capture_request_t &request,
305 sp<CaptureRequest> &nextRequest,
306 Vector<camera3_stream_buffer_t> &outputBuffers);
307
308 // Pause handling
309 bool waitIfPaused();
310
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700311 // Relay error to parent device object setErrorState
312 void setErrorState(const char *fmt, ...);
313
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800314 wp<Camera3Device> mParent;
315 camera3_device_t *mHal3Device;
316
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700317 const int mId;
318
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800319 Mutex mRequestLock;
320 Condition mRequestSignal;
321 RequestList mRequestQueue;
322 RequestList mRepeatingRequests;
323
324 bool mReconfigured;
325
326 // Used by waitIfPaused, waitForNextRequest, and waitUntilPaused
327 Mutex mPauseLock;
328 bool mDoPause;
329 Condition mDoPauseSignal;
330 bool mPaused;
331 Condition mPausedSignal;
332
333 sp<CaptureRequest> mPrevRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700334 int32_t mPrevTriggers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800335
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700336 uint32_t mFrameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700337
338 Mutex mLatestRequestMutex;
339 Condition mLatestRequestSignal;
340 // android.request.id for latest process_capture_request
341 int32_t mLatestRequestId;
342
343 typedef KeyedVector<uint32_t/*tag*/, RequestTrigger> TriggerMap;
344 Mutex mTriggerMutex;
345 TriggerMap mTriggerMap;
346 TriggerMap mTriggerRemovedMap;
347 TriggerMap mTriggerReplacedMap;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800348 };
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800349 sp<RequestThread> mRequestThread;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800350
351 /**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700352 * In-flight queue for tracking completion of capture requests.
353 */
354
355 struct InFlightRequest {
356 // Set by notify() SHUTTER call.
357 nsecs_t captureTimestamp;
358 // Set by process_capture_result call with valid metadata
359 bool haveResultMetadata;
360 // Decremented by calls to process_capture_result with valid output
361 // buffers
362 int numBuffersLeft;
363
364 InFlightRequest() :
365 captureTimestamp(0),
366 haveResultMetadata(false),
367 numBuffersLeft(0) {
368 }
369
370 explicit InFlightRequest(int numBuffers) :
371 captureTimestamp(0),
372 haveResultMetadata(false),
373 numBuffersLeft(numBuffers) {
374 }
375 };
376 // Map from frame number to the in-flight request state
377 typedef KeyedVector<uint32_t, InFlightRequest> InFlightMap;
378
379 Mutex mInFlightLock; // Protects mInFlightMap
380 InFlightMap mInFlightMap;
381
382 status_t registerInFlight(int32_t frameNumber, int32_t numBuffers);
383
384 /**
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700385 * Output result queue and current HAL device 3A state
386 */
387
388 // Lock for output side of device
389 Mutex mOutputLock;
390
391 /**** Scope for mOutputLock ****/
392
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700393 uint32_t mNextResultFrameNumber;
394 uint32_t mNextShutterFrameNumber;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700395 List<CameraMetadata> mResultQueue;
396 Condition mResultSignal;
397 NotificationListener *mListener;
398
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700399 /**** End scope for mOutputLock ****/
400
401 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800402 * Callback functions from HAL device
403 */
404 void processCaptureResult(const camera3_capture_result *result);
405
406 void notify(const camera3_notify_msg *msg);
407
408 /**
409 * Static callback forwarding methods from HAL to instance
410 */
411 static callbacks_process_capture_result_t sProcessCaptureResult;
412
413 static callbacks_notify_t sNotify;
414
415}; // class Camera3Device
416
417}; // namespace android
418
419#endif