blob: 64f460829517f3bec6d013fe3d615e74afb499fb [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 Talvala2e19c3c2012-08-26 09:29:28 -070030#include "camera2/CameraMetadata.h"
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070031
32namespace android {
33
34class Camera2Device : public virtual RefBase {
35 public:
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -070036 typedef camera2::CameraMetadata CameraMetadata;
37
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070038 Camera2Device(int id);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070039
40 ~Camera2Device();
41
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070042 status_t initialize(camera_module_t *module);
43
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070044 status_t dump(int fd, const Vector<String16>& args);
45
46 /**
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070047 * The device's static characteristics metadata buffer
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070048 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070049 const CameraMetadata& info() const;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070050
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070051 /**
52 * Submit request for capture. The Camera2Device takes ownership of the
53 * passed-in buffer.
54 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070055 status_t capture(CameraMetadata &request);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070056
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070057 /**
58 * Submit request for streaming. The Camera2Device makes a copy of the
59 * passed-in buffer and the caller retains ownership.
60 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070061 status_t setStreamingRequest(const CameraMetadata &request);
62
63 /**
64 * Clear the streaming request slot.
65 */
66 status_t clearStreamingRequest();
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070067
68 /**
69 * Create an output stream of the requested size and format.
70 *
71 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device selects
72 * an appropriate format; it can be queried with getStreamInfo.
73 *
74 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must be
75 * equal to the size in bytes of the buffers to allocate for the stream. For
76 * other formats, the size parameter is ignored.
77 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070078 status_t createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070079 uint32_t width, uint32_t height, int format, size_t size,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070080 int *id);
81
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070082 /**
83 * Get information about a given stream.
84 */
85 status_t getStreamInfo(int id,
86 uint32_t *width, uint32_t *height, uint32_t *format);
87
88 /**
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -070089 * Set stream gralloc buffer transform
90 */
91 status_t setStreamTransform(int id, int transform);
92
93 /**
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070094 * Delete stream. Must not be called if there are requests in flight which
95 * reference that stream.
96 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070097 status_t deleteStream(int id);
98
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070099 /**
100 * Create a metadata buffer with fields that the HAL device believes are
101 * best for the given use case
102 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700103 status_t createDefaultRequest(int templateId, CameraMetadata *request);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700104
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700105 /**
106 * Wait until all requests have been processed. Returns INVALID_OPERATION if
107 * the streaming slot is not empty, or TIMED_OUT if the requests haven't
108 * finished processing in 10 seconds.
109 */
110 status_t waitUntilDrained();
111
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700112 /**
113 * Abstract class for HAL notification listeners
114 */
115 class NotificationListener {
116 public:
117 // Refer to the Camera2 HAL definition for notification definitions
118 virtual void notifyError(int errorCode, int arg1, int arg2) = 0;
119 virtual void notifyShutter(int frameNumber, nsecs_t timestamp) = 0;
120 virtual void notifyAutoFocus(uint8_t newState, int triggerId) = 0;
121 virtual void notifyAutoExposure(uint8_t newState, int triggerId) = 0;
122 virtual void notifyAutoWhitebalance(uint8_t newState, int triggerId) = 0;
123 protected:
124 virtual ~NotificationListener();
125 };
126
127 /**
128 * Connect HAL notifications to a listener. Overwrites previous
129 * listener. Set to NULL to stop receiving notifications.
130 */
131 status_t setNotifyCallback(NotificationListener *listener);
132
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700133 /**
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -0700134 * Wait for a new frame to be produced, with timeout in nanoseconds.
135 * Returns TIMED_OUT when no frame produced within the specified duration
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700136 */
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -0700137 status_t waitForNextFrame(nsecs_t timeout);
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700138
139 /**
140 * Get next metadata frame from the frame queue. Returns NULL if the queue
141 * is empty; caller takes ownership of the metadata buffer.
142 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700143 status_t getNextFrame(CameraMetadata *frame);
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700144
145 /**
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700146 * Trigger auto-focus. The latest ID used in a trigger autofocus or cancel
147 * autofocus call will be returned by the HAL in all subsequent AF
148 * notifications.
149 */
150 status_t triggerAutofocus(uint32_t id);
151
152 /**
153 * Cancel auto-focus. The latest ID used in a trigger autofocus/cancel
154 * autofocus call will be returned by the HAL in all subsequent AF
155 * notifications.
156 */
157 status_t triggerCancelAutofocus(uint32_t id);
158
159 /**
160 * Trigger pre-capture metering. The latest ID used in a trigger pre-capture
161 * call will be returned by the HAL in all subsequent AE and AWB
162 * notifications.
163 */
164 status_t triggerPrecaptureMetering(uint32_t id);
165
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700166 private:
167
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700168 const int mId;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700169 camera2_device_t *mDevice;
170
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700171 CameraMetadata mDeviceInfo;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700172 vendor_tag_query_ops_t *mVendorTagOps;
173
174 /**
175 * Queue class for both sending requests to a camera2 device, and for
176 * receiving frames from a camera2 device.
177 */
178 class MetadataQueue: public camera2_request_queue_src_ops_t,
179 public camera2_frame_queue_dst_ops_t {
180 public:
181 MetadataQueue();
182 ~MetadataQueue();
183
184 // Interface to camera2 HAL device, either for requests (device is
185 // consumer) or for frames (device is producer)
186 const camera2_request_queue_src_ops_t* getToConsumerInterface();
187 void setFromConsumerInterface(camera2_device_t *d);
188
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700189 // Connect queue consumer endpoint to a camera2 device
190 status_t setConsumerDevice(camera2_device_t *d);
191 // Connect queue producer endpoint to a camera2 device
192 status_t setProducerDevice(camera2_device_t *d);
193
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700194 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
195
196 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
197 // On dequeue, user takes ownership of buffer pointer.
198 status_t enqueue(camera_metadata_t *buf);
199 status_t dequeue(camera_metadata_t **buf, bool incrementCount = true);
200 int getBufferCount();
201 status_t waitForBuffer(nsecs_t timeout);
202
203 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
204 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700205 // dequeues the first new entry. The metadata buffers passed in are
206 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700207 status_t setStreamSlot(camera_metadata_t *buf);
208 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
209
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700210 status_t dump(int fd, const Vector<String16>& args);
211
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700212 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700213 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700214 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
215 List<camera_metadata_t*>::iterator end);
216
217 camera2_device_t *mDevice;
218
219 Mutex mMutex;
220 Condition notEmpty;
221
222 int mFrameCount;
223
224 int mCount;
225 List<camera_metadata_t*> mEntries;
226 int mStreamSlotCount;
227 List<camera_metadata_t*> mStreamSlot;
228
229 bool mSignalConsumer;
230
231 static MetadataQueue* getInstance(
232 const camera2_frame_queue_dst_ops_t *q);
233 static MetadataQueue* getInstance(
234 const camera2_request_queue_src_ops_t *q);
235
236 static int consumer_buffer_count(
237 const camera2_request_queue_src_ops_t *q);
238
239 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
240 camera_metadata_t **buffer);
241
242 static int consumer_free(const camera2_request_queue_src_ops_t *q,
243 camera_metadata_t *old_buffer);
244
245 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
246 size_t entries, size_t bytes,
247 camera_metadata_t **buffer);
248
249 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
250 camera_metadata_t *old_buffer);
251
252 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
253 camera_metadata_t *filled_buffer);
254
255 }; // class MetadataQueue
256
257 MetadataQueue mRequestQueue;
258 MetadataQueue mFrameQueue;
259
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700260 /**
261 * Adapter from an ANativeWindow interface to camera2 device stream ops.
262 * Also takes care of allocating/deallocating stream in device interface
263 */
264 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
265 public:
266 StreamAdapter(camera2_device_t *d);
267
268 ~StreamAdapter();
269
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700270 /**
271 * Create a HAL device stream of the requested size and format.
272 *
273 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
274 * selects an appropriate format; it can be queried with getFormat.
275 *
276 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
277 * be equal to the size in bytes of the buffers to allocate for the
278 * stream. For other formats, the size parameter is ignored.
279 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700280 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700281 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700282
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700283 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700284
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700285 status_t setTransform(int transform);
286
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700287 // Get stream parameters.
288 // Only valid after a successful connectToDevice call.
289 int getId() const { return mId; }
290 uint32_t getWidth() const { return mWidth; }
291 uint32_t getHeight() const { return mHeight; }
292 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700293
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700294 // Dump stream information
295 status_t dump(int fd, const Vector<String16>& args);
296
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700297 private:
298 enum {
299 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700300 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700301 ALLOCATED,
302 CONNECTED,
303 ACTIVE
304 } mState;
305
306 sp<ANativeWindow> mConsumerInterface;
307 camera2_device_t *mDevice;
308
309 uint32_t mId;
310 uint32_t mWidth;
311 uint32_t mHeight;
312 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700313 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700314 uint32_t mUsage;
315 uint32_t mMaxProducerBuffers;
316 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700317 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700318 int mFormatRequested;
319
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700320 /** Debugging information */
321 uint32_t mActiveBuffers;
322 uint32_t mFrameCount;
323 int64_t mLastTimestamp;
324
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700325 const camera2_stream_ops *getStreamOps();
326
327 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
328
329 static int dequeue_buffer(const camera2_stream_ops_t *w,
330 buffer_handle_t** buffer);
331
332 static int enqueue_buffer(const camera2_stream_ops_t* w,
333 int64_t timestamp,
334 buffer_handle_t* buffer);
335
336 static int cancel_buffer(const camera2_stream_ops_t* w,
337 buffer_handle_t* buffer);
338
339 static int set_crop(const camera2_stream_ops_t* w,
340 int left, int top, int right, int bottom);
341 }; // class StreamAdapter
342
343 typedef List<sp<StreamAdapter> > StreamList;
344 StreamList mStreams;
345
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700346 // Receives HAL notifications and routes them to the NotificationListener
347 static void notificationCallback(int32_t msg_type,
348 int32_t ext1,
349 int32_t ext2,
350 int32_t ext3,
351 void *user);
352
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700353}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700354
355}; // namespace android
356
357#endif