blob: a327d8da5c947102fe66c9215136d791185b82f6 [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 /**
Eino-Ville Talvalada6665c2012-08-29 17:37:16 -070083 * Create an input reprocess stream that uses buffers from an existing
84 * output stream.
85 */
86 status_t createReprocessStreamFromStream(int outputId, int *id);
87
88 /**
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070089 * Get information about a given stream.
90 */
91 status_t getStreamInfo(int id,
92 uint32_t *width, uint32_t *height, uint32_t *format);
93
94 /**
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -070095 * Set stream gralloc buffer transform
96 */
97 status_t setStreamTransform(int id, int transform);
98
99 /**
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700100 * Delete stream. Must not be called if there are requests in flight which
101 * reference that stream.
102 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700103 status_t deleteStream(int id);
104
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700105 /**
Eino-Ville Talvalada6665c2012-08-29 17:37:16 -0700106 * Delete reprocess stream. Must not be called if there are requests in
107 * flight which reference that stream.
108 */
109 status_t deleteReprocessStream(int id);
110
111 /**
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700112 * Create a metadata buffer with fields that the HAL device believes are
113 * best for the given use case
114 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700115 status_t createDefaultRequest(int templateId, CameraMetadata *request);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700116
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700117 /**
118 * Wait until all requests have been processed. Returns INVALID_OPERATION if
119 * the streaming slot is not empty, or TIMED_OUT if the requests haven't
120 * finished processing in 10 seconds.
121 */
122 status_t waitUntilDrained();
123
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700124 /**
125 * Abstract class for HAL notification listeners
126 */
127 class NotificationListener {
128 public:
129 // Refer to the Camera2 HAL definition for notification definitions
130 virtual void notifyError(int errorCode, int arg1, int arg2) = 0;
131 virtual void notifyShutter(int frameNumber, nsecs_t timestamp) = 0;
132 virtual void notifyAutoFocus(uint8_t newState, int triggerId) = 0;
133 virtual void notifyAutoExposure(uint8_t newState, int triggerId) = 0;
134 virtual void notifyAutoWhitebalance(uint8_t newState, int triggerId) = 0;
135 protected:
136 virtual ~NotificationListener();
137 };
138
139 /**
140 * Connect HAL notifications to a listener. Overwrites previous
141 * listener. Set to NULL to stop receiving notifications.
142 */
143 status_t setNotifyCallback(NotificationListener *listener);
144
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700145 /**
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -0700146 * Wait for a new frame to be produced, with timeout in nanoseconds.
147 * Returns TIMED_OUT when no frame produced within the specified duration
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700148 */
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -0700149 status_t waitForNextFrame(nsecs_t timeout);
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700150
151 /**
152 * Get next metadata frame from the frame queue. Returns NULL if the queue
153 * is empty; caller takes ownership of the metadata buffer.
154 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700155 status_t getNextFrame(CameraMetadata *frame);
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700156
157 /**
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700158 * Trigger auto-focus. The latest ID used in a trigger autofocus or cancel
159 * autofocus call will be returned by the HAL in all subsequent AF
160 * notifications.
161 */
162 status_t triggerAutofocus(uint32_t id);
163
164 /**
165 * Cancel auto-focus. The latest ID used in a trigger autofocus/cancel
166 * autofocus call will be returned by the HAL in all subsequent AF
167 * notifications.
168 */
169 status_t triggerCancelAutofocus(uint32_t id);
170
171 /**
172 * Trigger pre-capture metering. The latest ID used in a trigger pre-capture
173 * call will be returned by the HAL in all subsequent AE and AWB
174 * notifications.
175 */
176 status_t triggerPrecaptureMetering(uint32_t id);
177
Eino-Ville Talvalada6665c2012-08-29 17:37:16 -0700178 /**
179 * Abstract interface for clients that want to listen to reprocess buffer
180 * release events
181 */
182 struct BufferReleasedListener: public virtual RefBase {
183 virtual void onBufferReleased(buffer_handle_t *handle) = 0;
184 };
185
186 /**
187 * Push a buffer to be reprocessed into a reprocessing stream, and
188 * provide a listener to call once the buffer is returned by the HAL
189 */
190 status_t pushReprocessBuffer(int reprocessStreamId,
191 buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
192
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700193 private:
194
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700195 const int mId;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700196 camera2_device_t *mDevice;
197
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700198 CameraMetadata mDeviceInfo;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700199 vendor_tag_query_ops_t *mVendorTagOps;
200
201 /**
202 * Queue class for both sending requests to a camera2 device, and for
203 * receiving frames from a camera2 device.
204 */
205 class MetadataQueue: public camera2_request_queue_src_ops_t,
206 public camera2_frame_queue_dst_ops_t {
207 public:
208 MetadataQueue();
209 ~MetadataQueue();
210
211 // Interface to camera2 HAL device, either for requests (device is
212 // consumer) or for frames (device is producer)
213 const camera2_request_queue_src_ops_t* getToConsumerInterface();
214 void setFromConsumerInterface(camera2_device_t *d);
215
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700216 // Connect queue consumer endpoint to a camera2 device
217 status_t setConsumerDevice(camera2_device_t *d);
218 // Connect queue producer endpoint to a camera2 device
219 status_t setProducerDevice(camera2_device_t *d);
220
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700221 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
222
223 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
224 // On dequeue, user takes ownership of buffer pointer.
225 status_t enqueue(camera_metadata_t *buf);
226 status_t dequeue(camera_metadata_t **buf, bool incrementCount = true);
227 int getBufferCount();
228 status_t waitForBuffer(nsecs_t timeout);
229
230 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
231 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700232 // dequeues the first new entry. The metadata buffers passed in are
233 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700234 status_t setStreamSlot(camera_metadata_t *buf);
235 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
236
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700237 status_t dump(int fd, const Vector<String16>& args);
238
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700239 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700240 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700241 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
242 List<camera_metadata_t*>::iterator end);
243
244 camera2_device_t *mDevice;
245
246 Mutex mMutex;
247 Condition notEmpty;
248
249 int mFrameCount;
250
251 int mCount;
252 List<camera_metadata_t*> mEntries;
253 int mStreamSlotCount;
254 List<camera_metadata_t*> mStreamSlot;
255
256 bool mSignalConsumer;
257
258 static MetadataQueue* getInstance(
259 const camera2_frame_queue_dst_ops_t *q);
260 static MetadataQueue* getInstance(
261 const camera2_request_queue_src_ops_t *q);
262
263 static int consumer_buffer_count(
264 const camera2_request_queue_src_ops_t *q);
265
266 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
267 camera_metadata_t **buffer);
268
269 static int consumer_free(const camera2_request_queue_src_ops_t *q,
270 camera_metadata_t *old_buffer);
271
272 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
273 size_t entries, size_t bytes,
274 camera_metadata_t **buffer);
275
276 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
277 camera_metadata_t *old_buffer);
278
279 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
280 camera_metadata_t *filled_buffer);
281
282 }; // class MetadataQueue
283
284 MetadataQueue mRequestQueue;
285 MetadataQueue mFrameQueue;
286
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700287 /**
288 * Adapter from an ANativeWindow interface to camera2 device stream ops.
289 * Also takes care of allocating/deallocating stream in device interface
290 */
291 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
292 public:
293 StreamAdapter(camera2_device_t *d);
294
295 ~StreamAdapter();
296
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700297 /**
298 * Create a HAL device stream of the requested size and format.
299 *
300 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
301 * selects an appropriate format; it can be queried with getFormat.
302 *
303 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
304 * be equal to the size in bytes of the buffers to allocate for the
305 * stream. For other formats, the size parameter is ignored.
306 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700307 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700308 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700309
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700310 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700311
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700312 status_t setTransform(int transform);
313
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700314 // Get stream parameters.
315 // Only valid after a successful connectToDevice call.
316 int getId() const { return mId; }
317 uint32_t getWidth() const { return mWidth; }
318 uint32_t getHeight() const { return mHeight; }
319 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700320
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700321 // Dump stream information
322 status_t dump(int fd, const Vector<String16>& args);
323
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700324 private:
325 enum {
326 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700327 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700328 ALLOCATED,
329 CONNECTED,
330 ACTIVE
331 } mState;
332
333 sp<ANativeWindow> mConsumerInterface;
334 camera2_device_t *mDevice;
335
336 uint32_t mId;
337 uint32_t mWidth;
338 uint32_t mHeight;
339 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700340 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700341 uint32_t mUsage;
342 uint32_t mMaxProducerBuffers;
343 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700344 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700345 int mFormatRequested;
346
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700347 /** Debugging information */
348 uint32_t mActiveBuffers;
349 uint32_t mFrameCount;
350 int64_t mLastTimestamp;
351
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700352 const camera2_stream_ops *getStreamOps();
353
354 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
355
356 static int dequeue_buffer(const camera2_stream_ops_t *w,
357 buffer_handle_t** buffer);
358
359 static int enqueue_buffer(const camera2_stream_ops_t* w,
360 int64_t timestamp,
361 buffer_handle_t* buffer);
362
363 static int cancel_buffer(const camera2_stream_ops_t* w,
364 buffer_handle_t* buffer);
365
366 static int set_crop(const camera2_stream_ops_t* w,
367 int left, int top, int right, int bottom);
368 }; // class StreamAdapter
369
370 typedef List<sp<StreamAdapter> > StreamList;
371 StreamList mStreams;
372
Eino-Ville Talvalada6665c2012-08-29 17:37:16 -0700373 /**
374 * Adapter from an ANativeWindow interface to camera2 device stream ops.
375 * Also takes care of allocating/deallocating stream in device interface
376 */
377 class ReprocessStreamAdapter: public camera2_stream_in_ops, public virtual RefBase {
378 public:
379 ReprocessStreamAdapter(camera2_device_t *d);
380
381 ~ReprocessStreamAdapter();
382
383 /**
384 * Create a HAL device reprocess stream based on an existing output stream.
385 */
386 status_t connectToDevice(const sp<StreamAdapter> &outputStream);
387
388 status_t release();
389
390 /**
391 * Push buffer into stream for reprocessing. Takes ownership until it notifies
392 * that the buffer has been released
393 */
394 status_t pushIntoStream(buffer_handle_t *handle,
395 const wp<BufferReleasedListener> &releaseListener);
396
397 /**
398 * Get stream parameters.
399 * Only valid after a successful connectToDevice call.
400 */
401 int getId() const { return mId; }
402 uint32_t getWidth() const { return mWidth; }
403 uint32_t getHeight() const { return mHeight; }
404 uint32_t getFormat() const { return mFormat; }
405
406 // Dump stream information
407 status_t dump(int fd, const Vector<String16>& args);
408
409 private:
410 enum {
411 ERROR = -1,
412 RELEASED = 0,
413 ACTIVE
414 } mState;
415
416 sp<ANativeWindow> mConsumerInterface;
417 wp<StreamAdapter> mBaseStream;
418
419 struct QueueEntry {
420 buffer_handle_t *handle;
421 wp<BufferReleasedListener> releaseListener;
422 };
423
424 List<QueueEntry> mQueue;
425
426 List<QueueEntry> mInFlightQueue;
427
428 camera2_device_t *mDevice;
429
430 uint32_t mId;
431 uint32_t mWidth;
432 uint32_t mHeight;
433 uint32_t mFormat;
434
435 /** Debugging information */
436 uint32_t mActiveBuffers;
437 uint32_t mFrameCount;
438 int64_t mLastTimestamp;
439
440 const camera2_stream_in_ops *getStreamOps();
441
442 static int acquire_buffer(const camera2_stream_in_ops_t *w,
443 buffer_handle_t** buffer);
444
445 static int release_buffer(const camera2_stream_in_ops_t* w,
446 buffer_handle_t* buffer);
447
448 }; // class ReprocessStreamAdapter
449
450 typedef List<sp<ReprocessStreamAdapter> > ReprocessStreamList;
451 ReprocessStreamList mReprocessStreams;
452
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700453 // Receives HAL notifications and routes them to the NotificationListener
454 static void notificationCallback(int32_t msg_type,
455 int32_t ext1,
456 int32_t ext2,
457 int32_t ext3,
458 void *user);
459
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700460}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700461
462}; // namespace android
463
464#endif