blob: 9b26f063a12d6ed4768e30bb5e32fce6962f80fe [file] [log] [blame]
Eino-Ville Talvalab99c5b82013-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 Talvala3b53bc92013-02-27 18:02:26 -080017#ifndef ANDROID_SERVERS_CAMERA3DEVICE_H
18#define ANDROID_SERVERS_CAMERA3DEVICE_H
Eino-Ville Talvalab99c5b82013-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 Talvala3b53bc92013-02-27 18:02:26 -080027#include "camera3/Camera3Stream.h"
28#include "camera3/Camera3OutputStream.h"
Igor Murashkinae500e52013-04-22 14:03:54 -070029#include "camera3/Camera3ZslStream.h"
Eino-Ville Talvalab99c5b82013-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 Talvala3b53bc92013-02-27 18:02:26 -080061 * CameraDeviceBase interface
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080062 */
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080063
Igor Murashkince124da2013-03-04 14:53:08 -080064 virtual int getId() const;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080065
66 // Transitions to idle state on success.
Eino-Ville Talvalab99c5b82013-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 Talvala3b53bc92013-02-27 18:02:26 -080071
72 // Capture and setStreamingRequest will configure streams if currently in
73 // idle state
Eino-Ville Talvalab99c5b82013-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 Talvala3b53bc92013-02-27 18:02:26 -080077
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080078 virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout);
Eino-Ville Talvala3b53bc92013-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 Talvalab99c5b82013-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 Murashkin0776a142013-04-15 14:59:22 -070086 virtual status_t createInputStream(
87 uint32_t width, uint32_t height, int format,
88 int *id);
Igor Murashkinae500e52013-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 Talvalab99c5b82013-02-06 17:20:07 -080095 virtual status_t createReprocessStreamFromStream(int outputId, int *id);
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080096
Eino-Ville Talvalab99c5b82013-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 Talvala3b53bc92013-02-27 18:02:26 -0800100
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800101 virtual status_t deleteStream(int id);
102 virtual status_t deleteReprocessStream(int id);
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800103
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800104 virtual status_t createDefaultRequest(int templateId, CameraMetadata *request);
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800105
106 // Transitions to the idle state on success
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800107 virtual status_t waitUntilDrained();
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800108
Eino-Ville Talvalab99c5b82013-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 Talvala3b53bc92013-02-27 18:02:26 -0800112
Eino-Ville Talvalab99c5b82013-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 Talvala3b53bc92013-02-27 18:02:26 -0800116
Eino-Ville Talvalab99c5b82013-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 Talvala3b53bc92013-02-27 18:02:26 -0800121 static const nsecs_t kShutdownTimeout = 5000000000; // 5 sec
Igor Murashkinb3a95a52013-04-01 17:29:07 -0700122 struct RequestTrigger;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800123
124 Mutex mLock;
125
126 /**** Scope for mLock ****/
127
128 const int mId;
129 camera3_device_t *mHal3Device;
130
131 CameraMetadata mDeviceInfo;
132 vendor_tag_query_ops_t mVendorTagOps;
133
134 enum {
135 STATUS_ERROR,
136 STATUS_UNINITIALIZED,
137 STATUS_IDLE,
138 STATUS_ACTIVE
139 } mStatus;
140
141 // Mapping of stream IDs to stream instances
Igor Murashkinae500e52013-04-22 14:03:54 -0700142 typedef KeyedVector<int, sp<camera3::Camera3OutputStreamInterface> >
143 StreamSet;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800144
145 StreamSet mOutputStreams;
146 sp<camera3::Camera3Stream> mInputStream;
147 int mNextStreamId;
148
149 // Need to hold on to stream references until configure completes.
Igor Murashkinae500e52013-04-22 14:03:54 -0700150 Vector<sp<camera3::Camera3StreamInterface> > mDeletedStreams;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800151
152 /**** End scope for mLock ****/
153
154 class CaptureRequest : public LightRefBase<CaptureRequest> {
155 public:
156 CameraMetadata mSettings;
157 sp<camera3::Camera3Stream> mInputStream;
Igor Murashkinae500e52013-04-22 14:03:54 -0700158 Vector<sp<camera3::Camera3OutputStreamInterface> >
159 mOutputStreams;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800160 };
161 typedef List<sp<CaptureRequest> > RequestList;
162
163 /**
164 * Lock-held version of waitUntilDrained. Will transition to IDLE on
165 * success.
166 */
167 status_t waitUntilDrainedLocked();
168
169 /**
170 * Do common work for setting up a streaming or single capture request.
171 * On success, will transition to ACTIVE if in IDLE.
172 */
173 sp<CaptureRequest> setUpRequestLocked(const CameraMetadata &request);
174
175 /**
176 * Build a CaptureRequest request from the CameraDeviceBase request
177 * settings.
178 */
179 sp<CaptureRequest> createCaptureRequest(const CameraMetadata &request);
180
181 /**
182 * Take the currently-defined set of streams and configure the HAL to use
183 * them. This is a long-running operation (may be several hundered ms).
184 */
185 status_t configureStreamsLocked();
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800186
Igor Murashkinb3a95a52013-04-01 17:29:07 -0700187 struct RequestTrigger {
188 // Metadata tag number, e.g. android.control.aePrecaptureTrigger
189 uint32_t metadataTag;
190 // Metadata value, e.g. 'START' or the trigger ID
191 int32_t entryValue;
192
193 // The last part of the fully qualified path, e.g. afTrigger
194 const char *getTagName() const {
195 return get_camera_metadata_tag_name(metadataTag) ?: "NULL";
196 }
197
198 // e.g. TYPE_BYTE, TYPE_INT32, etc.
199 int getTagType() const {
200 return get_camera_metadata_tag_type(metadataTag);
201 }
202 };
203
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800204 /**
205 * Thread for managing capture request submission to HAL device.
206 */
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800207 class RequestThread : public Thread {
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800208
209 public:
210
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800211 RequestThread(wp<Camera3Device> parent,
212 camera3_device_t *hal3Device);
213
214 /**
215 * Call after stream (re)-configuration is completed.
216 */
217 void configurationComplete();
218
219 /**
220 * Set or clear the list of repeating requests. Does not block
221 * on either. Use waitUntilPaused to wait until request queue
222 * has emptied out.
223 */
224 status_t setRepeatingRequests(const RequestList& requests);
225 status_t clearRepeatingRequests();
226
227 status_t queueRequest(sp<CaptureRequest> request);
228
229 /**
Igor Murashkinb3a95a52013-04-01 17:29:07 -0700230 * Queue a trigger to be dispatched with the next outgoing
231 * process_capture_request. The settings for that request only
232 * will be temporarily rewritten to add the trigger tag/value.
233 * Subsequent requests will not be rewritten (for this tag).
234 */
235 status_t queueTrigger(RequestTrigger trigger[], size_t count);
236
237 /**
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800238 * Pause/unpause the capture thread. Doesn't block, so use
239 * waitUntilPaused to wait until the thread is paused.
240 */
241 void setPaused(bool paused);
242
243 /**
244 * Wait until thread is paused, either due to setPaused(true)
245 * or due to lack of input requests. Returns TIMED_OUT in case
246 * the thread does not pause within the timeout.
247 */
248 status_t waitUntilPaused(nsecs_t timeout);
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800249
Igor Murashkinb3a95a52013-04-01 17:29:07 -0700250 /**
251 * Wait until thread processes the capture request with settings'
252 * android.request.id == requestId.
253 *
254 * Returns TIMED_OUT in case the thread does not process the request
255 * within the timeout.
256 */
257 status_t waitUntilRequestProcessed(int32_t requestId, nsecs_t timeout);
258
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800259 protected:
260
261 virtual bool threadLoop();
262
263 private:
Igor Murashkinb3a95a52013-04-01 17:29:07 -0700264 status_t queueTriggerLocked(RequestTrigger trigger);
265 // Mix-in queued triggers into this request
266 int32_t insertTriggers(const sp<CaptureRequest> &request);
267 // Purge the queued triggers from this request,
268 // restoring the old field values for those tags.
269 status_t removeTriggers(const sp<CaptureRequest> &request);
270
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800271 static const nsecs_t kRequestTimeout = 50e6; // 50 ms
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800272
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800273 // Waits for a request, or returns NULL if times out.
274 sp<CaptureRequest> waitForNextRequest();
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800275
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800276 // Return buffers, etc, for a request that couldn't be fully
277 // constructed. The buffers will be returned in the ERROR state
278 // to mark them as not having valid data.
279 // All arguments will be modified.
280 void cleanUpFailedRequest(camera3_capture_request_t &request,
281 sp<CaptureRequest> &nextRequest,
282 Vector<camera3_stream_buffer_t> &outputBuffers);
283
284 // Pause handling
285 bool waitIfPaused();
286
287 wp<Camera3Device> mParent;
288 camera3_device_t *mHal3Device;
289
290 Mutex mRequestLock;
291 Condition mRequestSignal;
292 RequestList mRequestQueue;
293 RequestList mRepeatingRequests;
294
295 bool mReconfigured;
296
297 // Used by waitIfPaused, waitForNextRequest, and waitUntilPaused
298 Mutex mPauseLock;
299 bool mDoPause;
300 Condition mDoPauseSignal;
301 bool mPaused;
302 Condition mPausedSignal;
303
304 sp<CaptureRequest> mPrevRequest;
Igor Murashkinb3a95a52013-04-01 17:29:07 -0700305 int32_t mPrevTriggers;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800306
307 int32_t mFrameNumber;
Igor Murashkinb3a95a52013-04-01 17:29:07 -0700308
309 Mutex mLatestRequestMutex;
310 Condition mLatestRequestSignal;
311 // android.request.id for latest process_capture_request
312 int32_t mLatestRequestId;
313
314 typedef KeyedVector<uint32_t/*tag*/, RequestTrigger> TriggerMap;
315 Mutex mTriggerMutex;
316 TriggerMap mTriggerMap;
317 TriggerMap mTriggerRemovedMap;
318 TriggerMap mTriggerReplacedMap;
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800319 };
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800320 sp<RequestThread> mRequestThread;
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800321
322 /**
Eino-Ville Talvalad0158c32013-03-11 14:13:50 -0700323 * Output result queue and current HAL device 3A state
324 */
325
326 // Lock for output side of device
327 Mutex mOutputLock;
328
329 /**** Scope for mOutputLock ****/
330
331 List<CameraMetadata> mResultQueue;
332 Condition mResultSignal;
333 NotificationListener *mListener;
334
335 struct AlgState {
336 camera_metadata_enum_android_control_ae_state aeState;
337 camera_metadata_enum_android_control_af_state afState;
338 camera_metadata_enum_android_control_awb_state awbState;
339
340 AlgState() :
341 aeState(ANDROID_CONTROL_AE_STATE_INACTIVE),
342 afState(ANDROID_CONTROL_AF_STATE_INACTIVE),
343 awbState(ANDROID_CONTROL_AWB_STATE_INACTIVE) {
344 }
345 } m3AState;
346
347 /**** End scope for mOutputLock ****/
348
349 /**
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800350 * Callback functions from HAL device
351 */
352 void processCaptureResult(const camera3_capture_result *result);
353
354 void notify(const camera3_notify_msg *msg);
355
356 /**
357 * Static callback forwarding methods from HAL to instance
358 */
359 static callbacks_process_capture_result_t sProcessCaptureResult;
360
361 static callbacks_notify_t sNotify;
362
363}; // class Camera3Device
364
365}; // namespace android
366
367#endif