blob: 86ff80f9b5d6feb87b3729d987662962638c901c [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"
Igor Murashkinbd02dd12013-02-13 15:53:56 -080030#include "camera/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);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070041 status_t disconnect();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070042
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070043 status_t dump(int fd, const Vector<String16>& args);
44
45 /**
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070046 * The device's static characteristics metadata buffer
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070047 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070048 const CameraMetadata& info() const;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070049
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070050 /**
51 * Submit request for capture. The Camera2Device takes ownership of the
52 * passed-in buffer.
53 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070054 status_t capture(CameraMetadata &request);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070055
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070056 /**
57 * Submit request for streaming. The Camera2Device makes a copy of the
58 * passed-in buffer and the caller retains ownership.
59 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070060 status_t setStreamingRequest(const CameraMetadata &request);
61
62 /**
63 * Clear the streaming request slot.
64 */
65 status_t clearStreamingRequest();
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070066
67 /**
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -070068 * Wait until a request with the given ID has been dequeued by the
69 * HAL. Returns TIMED_OUT if the timeout duration is reached. Returns
70 * immediately if the latest request received by the HAL has this id.
71 */
72 status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout);
73
74 /**
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070075 * Create an output stream of the requested size and format.
76 *
77 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device selects
78 * an appropriate format; it can be queried with getStreamInfo.
79 *
80 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must be
81 * equal to the size in bytes of the buffers to allocate for the stream. For
82 * other formats, the size parameter is ignored.
83 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070084 status_t createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070085 uint32_t width, uint32_t height, int format, size_t size,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070086 int *id);
87
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070088 /**
Eino-Ville Talvala69230df2012-08-29 17:37:16 -070089 * Create an input reprocess stream that uses buffers from an existing
90 * output stream.
91 */
92 status_t createReprocessStreamFromStream(int outputId, int *id);
93
94 /**
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070095 * Get information about a given stream.
96 */
97 status_t getStreamInfo(int id,
98 uint32_t *width, uint32_t *height, uint32_t *format);
99
100 /**
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700101 * Set stream gralloc buffer transform
102 */
103 status_t setStreamTransform(int id, int transform);
104
105 /**
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700106 * Delete stream. Must not be called if there are requests in flight which
107 * reference that stream.
108 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700109 status_t deleteStream(int id);
110
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700111 /**
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700112 * Delete reprocess stream. Must not be called if there are requests in
113 * flight which reference that stream.
114 */
115 status_t deleteReprocessStream(int id);
116
117 /**
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700118 * Create a metadata buffer with fields that the HAL device believes are
119 * best for the given use case
120 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700121 status_t createDefaultRequest(int templateId, CameraMetadata *request);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700122
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700123 /**
124 * Wait until all requests have been processed. Returns INVALID_OPERATION if
125 * the streaming slot is not empty, or TIMED_OUT if the requests haven't
126 * finished processing in 10 seconds.
127 */
128 status_t waitUntilDrained();
129
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700130 /**
131 * Abstract class for HAL notification listeners
132 */
133 class NotificationListener {
134 public:
135 // Refer to the Camera2 HAL definition for notification definitions
136 virtual void notifyError(int errorCode, int arg1, int arg2) = 0;
137 virtual void notifyShutter(int frameNumber, nsecs_t timestamp) = 0;
138 virtual void notifyAutoFocus(uint8_t newState, int triggerId) = 0;
139 virtual void notifyAutoExposure(uint8_t newState, int triggerId) = 0;
140 virtual void notifyAutoWhitebalance(uint8_t newState, int triggerId) = 0;
141 protected:
142 virtual ~NotificationListener();
143 };
144
145 /**
146 * Connect HAL notifications to a listener. Overwrites previous
147 * listener. Set to NULL to stop receiving notifications.
148 */
149 status_t setNotifyCallback(NotificationListener *listener);
150
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700151 /**
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -0700152 * Wait for a new frame to be produced, with timeout in nanoseconds.
153 * Returns TIMED_OUT when no frame produced within the specified duration
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700154 */
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -0700155 status_t waitForNextFrame(nsecs_t timeout);
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700156
157 /**
158 * Get next metadata frame from the frame queue. Returns NULL if the queue
159 * is empty; caller takes ownership of the metadata buffer.
160 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700161 status_t getNextFrame(CameraMetadata *frame);
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700162
163 /**
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700164 * Trigger auto-focus. The latest ID used in a trigger autofocus or cancel
165 * autofocus call will be returned by the HAL in all subsequent AF
166 * notifications.
167 */
168 status_t triggerAutofocus(uint32_t id);
169
170 /**
171 * Cancel auto-focus. The latest ID used in a trigger autofocus/cancel
172 * autofocus call will be returned by the HAL in all subsequent AF
173 * notifications.
174 */
175 status_t triggerCancelAutofocus(uint32_t id);
176
177 /**
178 * Trigger pre-capture metering. The latest ID used in a trigger pre-capture
179 * call will be returned by the HAL in all subsequent AE and AWB
180 * notifications.
181 */
182 status_t triggerPrecaptureMetering(uint32_t id);
183
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700184 /**
185 * Abstract interface for clients that want to listen to reprocess buffer
186 * release events
187 */
188 struct BufferReleasedListener: public virtual RefBase {
189 virtual void onBufferReleased(buffer_handle_t *handle) = 0;
190 };
191
192 /**
193 * Push a buffer to be reprocessed into a reprocessing stream, and
194 * provide a listener to call once the buffer is returned by the HAL
195 */
196 status_t pushReprocessBuffer(int reprocessStreamId,
197 buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
198
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700199 private:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700200 const int mId;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700201 camera2_device_t *mDevice;
202
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700203 CameraMetadata mDeviceInfo;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700204 vendor_tag_query_ops_t *mVendorTagOps;
205
206 /**
207 * Queue class for both sending requests to a camera2 device, and for
208 * receiving frames from a camera2 device.
209 */
210 class MetadataQueue: public camera2_request_queue_src_ops_t,
211 public camera2_frame_queue_dst_ops_t {
212 public:
213 MetadataQueue();
214 ~MetadataQueue();
215
216 // Interface to camera2 HAL device, either for requests (device is
217 // consumer) or for frames (device is producer)
218 const camera2_request_queue_src_ops_t* getToConsumerInterface();
219 void setFromConsumerInterface(camera2_device_t *d);
220
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700221 // Connect queue consumer endpoint to a camera2 device
222 status_t setConsumerDevice(camera2_device_t *d);
223 // Connect queue producer endpoint to a camera2 device
224 status_t setProducerDevice(camera2_device_t *d);
225
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700226 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
227
228 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
229 // On dequeue, user takes ownership of buffer pointer.
230 status_t enqueue(camera_metadata_t *buf);
Eino-Ville Talvalafbd60662012-10-16 10:28:07 -0700231 status_t dequeue(camera_metadata_t **buf, bool incrementCount = false);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700232 int getBufferCount();
233 status_t waitForBuffer(nsecs_t timeout);
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700234 // Wait until a buffer with the given ID is dequeued. Will return
235 // immediately if the latest buffer dequeued has that ID.
236 status_t waitForDequeue(int32_t id, nsecs_t timeout);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700237
238 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
239 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700240 // dequeues the first new entry. The metadata buffers passed in are
241 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700242 status_t setStreamSlot(camera_metadata_t *buf);
243 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
244
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700245 status_t dump(int fd, const Vector<String16>& args);
246
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700247 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700248 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700249 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
250 List<camera_metadata_t*>::iterator end);
251
252 camera2_device_t *mDevice;
253
254 Mutex mMutex;
255 Condition notEmpty;
256
257 int mFrameCount;
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700258 int32_t mLatestRequestId;
259 Condition mNewRequestId;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700260
261 int mCount;
262 List<camera_metadata_t*> mEntries;
263 int mStreamSlotCount;
264 List<camera_metadata_t*> mStreamSlot;
265
266 bool mSignalConsumer;
267
268 static MetadataQueue* getInstance(
269 const camera2_frame_queue_dst_ops_t *q);
270 static MetadataQueue* getInstance(
271 const camera2_request_queue_src_ops_t *q);
272
273 static int consumer_buffer_count(
274 const camera2_request_queue_src_ops_t *q);
275
276 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
277 camera_metadata_t **buffer);
278
279 static int consumer_free(const camera2_request_queue_src_ops_t *q,
280 camera_metadata_t *old_buffer);
281
282 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
283 size_t entries, size_t bytes,
284 camera_metadata_t **buffer);
285
286 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
287 camera_metadata_t *old_buffer);
288
289 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
290 camera_metadata_t *filled_buffer);
291
292 }; // class MetadataQueue
293
294 MetadataQueue mRequestQueue;
295 MetadataQueue mFrameQueue;
296
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700297 /**
298 * Adapter from an ANativeWindow interface to camera2 device stream ops.
299 * Also takes care of allocating/deallocating stream in device interface
300 */
301 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
302 public:
303 StreamAdapter(camera2_device_t *d);
304
305 ~StreamAdapter();
306
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700307 /**
308 * Create a HAL device stream of the requested size and format.
309 *
310 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
311 * selects an appropriate format; it can be queried with getFormat.
312 *
313 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
314 * be equal to the size in bytes of the buffers to allocate for the
315 * stream. For other formats, the size parameter is ignored.
316 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700317 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700318 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700319
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700320 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700321
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700322 status_t setTransform(int transform);
323
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700324 // Get stream parameters.
325 // Only valid after a successful connectToDevice call.
326 int getId() const { return mId; }
327 uint32_t getWidth() const { return mWidth; }
328 uint32_t getHeight() const { return mHeight; }
329 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700330
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700331 // Dump stream information
332 status_t dump(int fd, const Vector<String16>& args);
333
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700334 private:
335 enum {
336 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700337 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700338 ALLOCATED,
339 CONNECTED,
340 ACTIVE
341 } mState;
342
343 sp<ANativeWindow> mConsumerInterface;
344 camera2_device_t *mDevice;
345
346 uint32_t mId;
347 uint32_t mWidth;
348 uint32_t mHeight;
349 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700350 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700351 uint32_t mUsage;
352 uint32_t mMaxProducerBuffers;
353 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700354 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700355 int mFormatRequested;
356
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700357 /** Debugging information */
358 uint32_t mActiveBuffers;
359 uint32_t mFrameCount;
360 int64_t mLastTimestamp;
361
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700362 const camera2_stream_ops *getStreamOps();
363
364 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
365
366 static int dequeue_buffer(const camera2_stream_ops_t *w,
367 buffer_handle_t** buffer);
368
369 static int enqueue_buffer(const camera2_stream_ops_t* w,
370 int64_t timestamp,
371 buffer_handle_t* buffer);
372
373 static int cancel_buffer(const camera2_stream_ops_t* w,
374 buffer_handle_t* buffer);
375
376 static int set_crop(const camera2_stream_ops_t* w,
377 int left, int top, int right, int bottom);
378 }; // class StreamAdapter
379
380 typedef List<sp<StreamAdapter> > StreamList;
381 StreamList mStreams;
382
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700383 /**
384 * Adapter from an ANativeWindow interface to camera2 device stream ops.
385 * Also takes care of allocating/deallocating stream in device interface
386 */
387 class ReprocessStreamAdapter: public camera2_stream_in_ops, public virtual RefBase {
388 public:
389 ReprocessStreamAdapter(camera2_device_t *d);
390
391 ~ReprocessStreamAdapter();
392
393 /**
394 * Create a HAL device reprocess stream based on an existing output stream.
395 */
396 status_t connectToDevice(const sp<StreamAdapter> &outputStream);
397
398 status_t release();
399
400 /**
401 * Push buffer into stream for reprocessing. Takes ownership until it notifies
402 * that the buffer has been released
403 */
404 status_t pushIntoStream(buffer_handle_t *handle,
405 const wp<BufferReleasedListener> &releaseListener);
406
407 /**
408 * Get stream parameters.
409 * Only valid after a successful connectToDevice call.
410 */
411 int getId() const { return mId; }
412 uint32_t getWidth() const { return mWidth; }
413 uint32_t getHeight() const { return mHeight; }
414 uint32_t getFormat() const { return mFormat; }
415
416 // Dump stream information
417 status_t dump(int fd, const Vector<String16>& args);
418
419 private:
420 enum {
421 ERROR = -1,
422 RELEASED = 0,
423 ACTIVE
424 } mState;
425
426 sp<ANativeWindow> mConsumerInterface;
427 wp<StreamAdapter> mBaseStream;
428
429 struct QueueEntry {
430 buffer_handle_t *handle;
431 wp<BufferReleasedListener> releaseListener;
432 };
433
434 List<QueueEntry> mQueue;
435
436 List<QueueEntry> mInFlightQueue;
437
438 camera2_device_t *mDevice;
439
440 uint32_t mId;
441 uint32_t mWidth;
442 uint32_t mHeight;
443 uint32_t mFormat;
444
445 /** Debugging information */
446 uint32_t mActiveBuffers;
447 uint32_t mFrameCount;
448 int64_t mLastTimestamp;
449
450 const camera2_stream_in_ops *getStreamOps();
451
452 static int acquire_buffer(const camera2_stream_in_ops_t *w,
453 buffer_handle_t** buffer);
454
455 static int release_buffer(const camera2_stream_in_ops_t* w,
456 buffer_handle_t* buffer);
457
458 }; // class ReprocessStreamAdapter
459
460 typedef List<sp<ReprocessStreamAdapter> > ReprocessStreamList;
461 ReprocessStreamList mReprocessStreams;
462
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700463 // Receives HAL notifications and routes them to the NotificationListener
464 static void notificationCallback(int32_t msg_type,
465 int32_t ext1,
466 int32_t ext2,
467 int32_t ext3,
468 void *user);
469
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700470}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700471
472}; // namespace android
473
474#endif