blob: 223d77aaf42ffda7218cc6b902bbd50b6d781d3a [file] [log] [blame]
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -07001/*
2 * Copyright (C) 2012 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
17#ifndef ANDROID_SERVERS_CAMERA_CAMERA2DEVICE_H
18#define ANDROID_SERVERS_CAMERA_CAMERA2DEVICE_H
19
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070020#include <utils/Condition.h>
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070021#include <utils/Errors.h>
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070022#include <utils/List.h>
23#include <utils/Mutex.h>
24#include <utils/RefBase.h>
25#include <utils/String8.h>
26#include <utils/String16.h>
27#include <utils/Vector.h>
28
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070029#include "hardware/camera2.h"
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070030#include "CameraMetadata.h"
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070031
32namespace android {
33
34class Camera2Device : public virtual RefBase {
35 public:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070036 Camera2Device(int id);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070037
38 ~Camera2Device();
39
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070040 status_t initialize(camera_module_t *module);
41
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070042 status_t dump(int fd, const Vector<String16>& args);
43
44 /**
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070045 * The device's static characteristics metadata buffer
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070046 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070047 const CameraMetadata& info() const;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070048
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070049 /**
50 * Submit request for capture. The Camera2Device takes ownership of the
51 * passed-in buffer.
52 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070053 status_t capture(CameraMetadata &request);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070054
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070055 /**
56 * Submit request for streaming. The Camera2Device makes a copy of the
57 * passed-in buffer and the caller retains ownership.
58 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070059 status_t setStreamingRequest(const CameraMetadata &request);
60
61 /**
62 * Clear the streaming request slot.
63 */
64 status_t clearStreamingRequest();
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070065
66 /**
67 * Create an output stream of the requested size and format.
68 *
69 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device selects
70 * an appropriate format; it can be queried with getStreamInfo.
71 *
72 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must be
73 * equal to the size in bytes of the buffers to allocate for the stream. For
74 * other formats, the size parameter is ignored.
75 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070076 status_t createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070077 uint32_t width, uint32_t height, int format, size_t size,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070078 int *id);
79
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070080 /**
81 * Get information about a given stream.
82 */
83 status_t getStreamInfo(int id,
84 uint32_t *width, uint32_t *height, uint32_t *format);
85
86 /**
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -070087 * Set stream gralloc buffer transform
88 */
89 status_t setStreamTransform(int id, int transform);
90
91 /**
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070092 * Delete stream. Must not be called if there are requests in flight which
93 * reference that stream.
94 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070095 status_t deleteStream(int id);
96
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070097 /**
98 * Create a metadata buffer with fields that the HAL device believes are
99 * best for the given use case
100 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700101 status_t createDefaultRequest(int templateId, CameraMetadata *request);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700102
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700103 /**
104 * Wait until all requests have been processed. Returns INVALID_OPERATION if
105 * the streaming slot is not empty, or TIMED_OUT if the requests haven't
106 * finished processing in 10 seconds.
107 */
108 status_t waitUntilDrained();
109
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700110 /**
111 * Abstract class for HAL notification listeners
112 */
113 class NotificationListener {
114 public:
115 // Refer to the Camera2 HAL definition for notification definitions
116 virtual void notifyError(int errorCode, int arg1, int arg2) = 0;
117 virtual void notifyShutter(int frameNumber, nsecs_t timestamp) = 0;
118 virtual void notifyAutoFocus(uint8_t newState, int triggerId) = 0;
119 virtual void notifyAutoExposure(uint8_t newState, int triggerId) = 0;
120 virtual void notifyAutoWhitebalance(uint8_t newState, int triggerId) = 0;
121 protected:
122 virtual ~NotificationListener();
123 };
124
125 /**
126 * Connect HAL notifications to a listener. Overwrites previous
127 * listener. Set to NULL to stop receiving notifications.
128 */
129 status_t setNotifyCallback(NotificationListener *listener);
130
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700131 /**
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -0700132 * Wait for a new frame to be produced, with timeout in nanoseconds.
133 * Returns TIMED_OUT when no frame produced within the specified duration
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700134 */
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -0700135 status_t waitForNextFrame(nsecs_t timeout);
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700136
137 /**
138 * Get next metadata frame from the frame queue. Returns NULL if the queue
139 * is empty; caller takes ownership of the metadata buffer.
140 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700141 status_t getNextFrame(CameraMetadata *frame);
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700142
143 /**
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700144 * Trigger auto-focus. The latest ID used in a trigger autofocus or cancel
145 * autofocus call will be returned by the HAL in all subsequent AF
146 * notifications.
147 */
148 status_t triggerAutofocus(uint32_t id);
149
150 /**
151 * Cancel auto-focus. The latest ID used in a trigger autofocus/cancel
152 * autofocus call will be returned by the HAL in all subsequent AF
153 * notifications.
154 */
155 status_t triggerCancelAutofocus(uint32_t id);
156
157 /**
158 * Trigger pre-capture metering. The latest ID used in a trigger pre-capture
159 * call will be returned by the HAL in all subsequent AE and AWB
160 * notifications.
161 */
162 status_t triggerPrecaptureMetering(uint32_t id);
163
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700164 private:
165
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700166 const int mId;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700167 camera2_device_t *mDevice;
168
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700169 CameraMetadata mDeviceInfo;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700170 vendor_tag_query_ops_t *mVendorTagOps;
171
172 /**
173 * Queue class for both sending requests to a camera2 device, and for
174 * receiving frames from a camera2 device.
175 */
176 class MetadataQueue: public camera2_request_queue_src_ops_t,
177 public camera2_frame_queue_dst_ops_t {
178 public:
179 MetadataQueue();
180 ~MetadataQueue();
181
182 // Interface to camera2 HAL device, either for requests (device is
183 // consumer) or for frames (device is producer)
184 const camera2_request_queue_src_ops_t* getToConsumerInterface();
185 void setFromConsumerInterface(camera2_device_t *d);
186
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700187 // Connect queue consumer endpoint to a camera2 device
188 status_t setConsumerDevice(camera2_device_t *d);
189 // Connect queue producer endpoint to a camera2 device
190 status_t setProducerDevice(camera2_device_t *d);
191
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700192 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
193
194 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
195 // On dequeue, user takes ownership of buffer pointer.
196 status_t enqueue(camera_metadata_t *buf);
197 status_t dequeue(camera_metadata_t **buf, bool incrementCount = true);
198 int getBufferCount();
199 status_t waitForBuffer(nsecs_t timeout);
200
201 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
202 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700203 // dequeues the first new entry. The metadata buffers passed in are
204 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700205 status_t setStreamSlot(camera_metadata_t *buf);
206 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
207
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700208 status_t dump(int fd, const Vector<String16>& args);
209
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700210 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700211 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700212 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
213 List<camera_metadata_t*>::iterator end);
214
215 camera2_device_t *mDevice;
216
217 Mutex mMutex;
218 Condition notEmpty;
219
220 int mFrameCount;
221
222 int mCount;
223 List<camera_metadata_t*> mEntries;
224 int mStreamSlotCount;
225 List<camera_metadata_t*> mStreamSlot;
226
227 bool mSignalConsumer;
228
229 static MetadataQueue* getInstance(
230 const camera2_frame_queue_dst_ops_t *q);
231 static MetadataQueue* getInstance(
232 const camera2_request_queue_src_ops_t *q);
233
234 static int consumer_buffer_count(
235 const camera2_request_queue_src_ops_t *q);
236
237 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
238 camera_metadata_t **buffer);
239
240 static int consumer_free(const camera2_request_queue_src_ops_t *q,
241 camera_metadata_t *old_buffer);
242
243 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
244 size_t entries, size_t bytes,
245 camera_metadata_t **buffer);
246
247 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
248 camera_metadata_t *old_buffer);
249
250 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
251 camera_metadata_t *filled_buffer);
252
253 }; // class MetadataQueue
254
255 MetadataQueue mRequestQueue;
256 MetadataQueue mFrameQueue;
257
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700258 /**
259 * Adapter from an ANativeWindow interface to camera2 device stream ops.
260 * Also takes care of allocating/deallocating stream in device interface
261 */
262 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
263 public:
264 StreamAdapter(camera2_device_t *d);
265
266 ~StreamAdapter();
267
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700268 /**
269 * Create a HAL device stream of the requested size and format.
270 *
271 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
272 * selects an appropriate format; it can be queried with getFormat.
273 *
274 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
275 * be equal to the size in bytes of the buffers to allocate for the
276 * stream. For other formats, the size parameter is ignored.
277 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700278 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700279 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700280
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700281 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700282
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700283 status_t setTransform(int transform);
284
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700285 // Get stream parameters.
286 // Only valid after a successful connectToDevice call.
287 int getId() const { return mId; }
288 uint32_t getWidth() const { return mWidth; }
289 uint32_t getHeight() const { return mHeight; }
290 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700291
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700292 // Dump stream information
293 status_t dump(int fd, const Vector<String16>& args);
294
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700295 private:
296 enum {
297 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700298 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700299 ALLOCATED,
300 CONNECTED,
301 ACTIVE
302 } mState;
303
304 sp<ANativeWindow> mConsumerInterface;
305 camera2_device_t *mDevice;
306
307 uint32_t mId;
308 uint32_t mWidth;
309 uint32_t mHeight;
310 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700311 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700312 uint32_t mUsage;
313 uint32_t mMaxProducerBuffers;
314 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700315 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700316 int mFormatRequested;
317
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700318 /** Debugging information */
319 uint32_t mActiveBuffers;
320 uint32_t mFrameCount;
321 int64_t mLastTimestamp;
322
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700323 const camera2_stream_ops *getStreamOps();
324
325 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
326
327 static int dequeue_buffer(const camera2_stream_ops_t *w,
328 buffer_handle_t** buffer);
329
330 static int enqueue_buffer(const camera2_stream_ops_t* w,
331 int64_t timestamp,
332 buffer_handle_t* buffer);
333
334 static int cancel_buffer(const camera2_stream_ops_t* w,
335 buffer_handle_t* buffer);
336
337 static int set_crop(const camera2_stream_ops_t* w,
338 int left, int top, int right, int bottom);
339 }; // class StreamAdapter
340
341 typedef List<sp<StreamAdapter> > StreamList;
342 StreamList mStreams;
343
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700344 // Receives HAL notifications and routes them to the NotificationListener
345 static void notificationCallback(int32_t msg_type,
346 int32_t ext1,
347 int32_t ext2,
348 int32_t ext3,
349 void *user);
350
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700351}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700352
353}; // namespace android
354
355#endif