blob: 5c5faeb4ded3dc7c50546d8dec2746531cf60f4c [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"
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080029
30#include "hardware/camera3.h"
31
32/**
33 * Function pointer types with C calling convention to
34 * use for HAL callback functions.
35 */
36extern "C" {
37 typedef void (callbacks_process_capture_result_t)(
38 const struct camera3_callback_ops *,
39 const camera3_capture_result_t *);
40
41 typedef void (callbacks_notify_t)(
42 const struct camera3_callback_ops *,
43 const camera3_notify_msg_t *);
44}
45
46namespace android {
47
48/**
49 * CameraDevice for HAL devices with version CAMERA_DEVICE_API_VERSION_3_0
50 */
51class Camera3Device :
52 public CameraDeviceBase,
53 private camera3_callback_ops {
54 public:
55 Camera3Device(int id);
56
57 virtual ~Camera3Device();
58
59 /**
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080060 * CameraDeviceBase interface
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080061 */
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080062
Igor Murashkince124da2013-03-04 14:53:08 -080063 virtual int getId() const;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080064
65 // Transitions to idle state on success.
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080066 virtual status_t initialize(camera_module_t *module);
67 virtual status_t disconnect();
68 virtual status_t dump(int fd, const Vector<String16> &args);
69 virtual const CameraMetadata& info() const;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080070
71 // Capture and setStreamingRequest will configure streams if currently in
72 // idle state
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080073 virtual status_t capture(CameraMetadata &request);
74 virtual status_t setStreamingRequest(const CameraMetadata &request);
75 virtual status_t clearStreamingRequest();
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080076
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080077 virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout);
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080078
79 // Actual stream creation/deletion is delayed until first request is submitted
80 // If adding streams while actively capturing, will pause device before adding
81 // stream, reconfiguring device, and unpausing.
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080082 virtual status_t createStream(sp<ANativeWindow> consumer,
83 uint32_t width, uint32_t height, int format, size_t size,
84 int *id);
Igor Murashkin0776a142013-04-15 14:59:22 -070085 virtual status_t createInputStream(
86 uint32_t width, uint32_t height, int format,
87 int *id);
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080088 virtual status_t createReprocessStreamFromStream(int outputId, int *id);
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080089
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080090 virtual status_t getStreamInfo(int id,
91 uint32_t *width, uint32_t *height, uint32_t *format);
92 virtual status_t setStreamTransform(int id, int transform);
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080093
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080094 virtual status_t deleteStream(int id);
95 virtual status_t deleteReprocessStream(int id);
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080096
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080097 virtual status_t createDefaultRequest(int templateId, CameraMetadata *request);
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080098
99 // Transitions to the idle state on success
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800100 virtual status_t waitUntilDrained();
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800101
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800102 virtual status_t setNotifyCallback(NotificationListener *listener);
103 virtual status_t waitForNextFrame(nsecs_t timeout);
104 virtual status_t getNextFrame(CameraMetadata *frame);
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800105
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800106 virtual status_t triggerAutofocus(uint32_t id);
107 virtual status_t triggerCancelAutofocus(uint32_t id);
108 virtual status_t triggerPrecaptureMetering(uint32_t id);
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800109
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800110 virtual status_t pushReprocessBuffer(int reprocessStreamId,
111 buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
112
113 private:
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800114 static const nsecs_t kShutdownTimeout = 5000000000; // 5 sec
Igor Murashkinb3a95a52013-04-01 17:29:07 -0700115 struct RequestTrigger;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800116
117 Mutex mLock;
118
119 /**** Scope for mLock ****/
120
121 const int mId;
122 camera3_device_t *mHal3Device;
123
124 CameraMetadata mDeviceInfo;
125 vendor_tag_query_ops_t mVendorTagOps;
126
127 enum {
128 STATUS_ERROR,
129 STATUS_UNINITIALIZED,
130 STATUS_IDLE,
131 STATUS_ACTIVE
132 } mStatus;
133
134 // Mapping of stream IDs to stream instances
135 typedef KeyedVector<int, sp<camera3::Camera3OutputStream> > StreamSet;
136
137 StreamSet mOutputStreams;
138 sp<camera3::Camera3Stream> mInputStream;
139 int mNextStreamId;
140
141 // Need to hold on to stream references until configure completes.
142 Vector<sp<camera3::Camera3Stream> > mDeletedStreams;
143
144 /**** End scope for mLock ****/
145
146 class CaptureRequest : public LightRefBase<CaptureRequest> {
147 public:
148 CameraMetadata mSettings;
149 sp<camera3::Camera3Stream> mInputStream;
150 Vector<sp<camera3::Camera3Stream> > mOutputStreams;
151 };
152 typedef List<sp<CaptureRequest> > RequestList;
153
154 /**
155 * Lock-held version of waitUntilDrained. Will transition to IDLE on
156 * success.
157 */
158 status_t waitUntilDrainedLocked();
159
160 /**
161 * Do common work for setting up a streaming or single capture request.
162 * On success, will transition to ACTIVE if in IDLE.
163 */
164 sp<CaptureRequest> setUpRequestLocked(const CameraMetadata &request);
165
166 /**
167 * Build a CaptureRequest request from the CameraDeviceBase request
168 * settings.
169 */
170 sp<CaptureRequest> createCaptureRequest(const CameraMetadata &request);
171
172 /**
173 * Take the currently-defined set of streams and configure the HAL to use
174 * them. This is a long-running operation (may be several hundered ms).
175 */
176 status_t configureStreamsLocked();
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800177
Igor Murashkinb3a95a52013-04-01 17:29:07 -0700178 struct RequestTrigger {
179 // Metadata tag number, e.g. android.control.aePrecaptureTrigger
180 uint32_t metadataTag;
181 // Metadata value, e.g. 'START' or the trigger ID
182 int32_t entryValue;
183
184 // The last part of the fully qualified path, e.g. afTrigger
185 const char *getTagName() const {
186 return get_camera_metadata_tag_name(metadataTag) ?: "NULL";
187 }
188
189 // e.g. TYPE_BYTE, TYPE_INT32, etc.
190 int getTagType() const {
191 return get_camera_metadata_tag_type(metadataTag);
192 }
193 };
194
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800195 /**
196 * Thread for managing capture request submission to HAL device.
197 */
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800198 class RequestThread : public Thread {
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800199
200 public:
201
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800202 RequestThread(wp<Camera3Device> parent,
203 camera3_device_t *hal3Device);
204
205 /**
206 * Call after stream (re)-configuration is completed.
207 */
208 void configurationComplete();
209
210 /**
211 * Set or clear the list of repeating requests. Does not block
212 * on either. Use waitUntilPaused to wait until request queue
213 * has emptied out.
214 */
215 status_t setRepeatingRequests(const RequestList& requests);
216 status_t clearRepeatingRequests();
217
218 status_t queueRequest(sp<CaptureRequest> request);
219
220 /**
Igor Murashkinb3a95a52013-04-01 17:29:07 -0700221 * Queue a trigger to be dispatched with the next outgoing
222 * process_capture_request. The settings for that request only
223 * will be temporarily rewritten to add the trigger tag/value.
224 * Subsequent requests will not be rewritten (for this tag).
225 */
226 status_t queueTrigger(RequestTrigger trigger[], size_t count);
227
228 /**
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800229 * Pause/unpause the capture thread. Doesn't block, so use
230 * waitUntilPaused to wait until the thread is paused.
231 */
232 void setPaused(bool paused);
233
234 /**
235 * Wait until thread is paused, either due to setPaused(true)
236 * or due to lack of input requests. Returns TIMED_OUT in case
237 * the thread does not pause within the timeout.
238 */
239 status_t waitUntilPaused(nsecs_t timeout);
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800240
Igor Murashkinb3a95a52013-04-01 17:29:07 -0700241 /**
242 * Wait until thread processes the capture request with settings'
243 * android.request.id == requestId.
244 *
245 * Returns TIMED_OUT in case the thread does not process the request
246 * within the timeout.
247 */
248 status_t waitUntilRequestProcessed(int32_t requestId, nsecs_t timeout);
249
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800250 protected:
251
252 virtual bool threadLoop();
253
254 private:
Igor Murashkinb3a95a52013-04-01 17:29:07 -0700255 status_t queueTriggerLocked(RequestTrigger trigger);
256 // Mix-in queued triggers into this request
257 int32_t insertTriggers(const sp<CaptureRequest> &request);
258 // Purge the queued triggers from this request,
259 // restoring the old field values for those tags.
260 status_t removeTriggers(const sp<CaptureRequest> &request);
261
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800262 static const nsecs_t kRequestTimeout = 50e6; // 50 ms
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800263
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800264 // Waits for a request, or returns NULL if times out.
265 sp<CaptureRequest> waitForNextRequest();
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800266
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800267 // Return buffers, etc, for a request that couldn't be fully
268 // constructed. The buffers will be returned in the ERROR state
269 // to mark them as not having valid data.
270 // All arguments will be modified.
271 void cleanUpFailedRequest(camera3_capture_request_t &request,
272 sp<CaptureRequest> &nextRequest,
273 Vector<camera3_stream_buffer_t> &outputBuffers);
274
275 // Pause handling
276 bool waitIfPaused();
277
278 wp<Camera3Device> mParent;
279 camera3_device_t *mHal3Device;
280
281 Mutex mRequestLock;
282 Condition mRequestSignal;
283 RequestList mRequestQueue;
284 RequestList mRepeatingRequests;
285
286 bool mReconfigured;
287
288 // Used by waitIfPaused, waitForNextRequest, and waitUntilPaused
289 Mutex mPauseLock;
290 bool mDoPause;
291 Condition mDoPauseSignal;
292 bool mPaused;
293 Condition mPausedSignal;
294
295 sp<CaptureRequest> mPrevRequest;
Igor Murashkinb3a95a52013-04-01 17:29:07 -0700296 int32_t mPrevTriggers;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800297
298 int32_t mFrameNumber;
Igor Murashkinb3a95a52013-04-01 17:29:07 -0700299
300 Mutex mLatestRequestMutex;
301 Condition mLatestRequestSignal;
302 // android.request.id for latest process_capture_request
303 int32_t mLatestRequestId;
304
305 typedef KeyedVector<uint32_t/*tag*/, RequestTrigger> TriggerMap;
306 Mutex mTriggerMutex;
307 TriggerMap mTriggerMap;
308 TriggerMap mTriggerRemovedMap;
309 TriggerMap mTriggerReplacedMap;
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800310 };
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800311 sp<RequestThread> mRequestThread;
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800312
313 /**
Eino-Ville Talvalad0158c32013-03-11 14:13:50 -0700314 * Output result queue and current HAL device 3A state
315 */
316
317 // Lock for output side of device
318 Mutex mOutputLock;
319
320 /**** Scope for mOutputLock ****/
321
322 List<CameraMetadata> mResultQueue;
323 Condition mResultSignal;
324 NotificationListener *mListener;
325
326 struct AlgState {
327 camera_metadata_enum_android_control_ae_state aeState;
328 camera_metadata_enum_android_control_af_state afState;
329 camera_metadata_enum_android_control_awb_state awbState;
330
331 AlgState() :
332 aeState(ANDROID_CONTROL_AE_STATE_INACTIVE),
333 afState(ANDROID_CONTROL_AF_STATE_INACTIVE),
334 awbState(ANDROID_CONTROL_AWB_STATE_INACTIVE) {
335 }
336 } m3AState;
337
338 /**** End scope for mOutputLock ****/
339
340 /**
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800341 * Callback functions from HAL device
342 */
343 void processCaptureResult(const camera3_capture_result *result);
344
345 void notify(const camera3_notify_msg *msg);
346
347 /**
348 * Static callback forwarding methods from HAL to instance
349 */
350 static callbacks_process_capture_result_t sProcessCaptureResult;
351
352 static callbacks_notify_t sNotify;
353
354}; // class Camera3Device
355
356}; // namespace android
357
358#endif