blob: 41df2e4b72622b59e8ff71b0bc4f744570eb3910 [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);
Eino-Ville Talvala7adb52f2012-09-20 14:44:20 -070043 status_t disconnect();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070044
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070045 status_t dump(int fd, const Vector<String16>& args);
46
47 /**
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070048 * The device's static characteristics metadata buffer
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070049 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070050 const CameraMetadata& info() const;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070051
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070052 /**
53 * Submit request for capture. The Camera2Device takes ownership of the
54 * passed-in buffer.
55 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070056 status_t capture(CameraMetadata &request);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070057
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070058 /**
59 * Submit request for streaming. The Camera2Device makes a copy of the
60 * passed-in buffer and the caller retains ownership.
61 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070062 status_t setStreamingRequest(const CameraMetadata &request);
63
64 /**
65 * Clear the streaming request slot.
66 */
67 status_t clearStreamingRequest();
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070068
69 /**
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -070070 * Wait until a request with the given ID has been dequeued by the
71 * HAL. Returns TIMED_OUT if the timeout duration is reached. Returns
72 * immediately if the latest request received by the HAL has this id.
73 */
74 status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout);
75
76 /**
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070077 * Create an output stream of the requested size and format.
78 *
79 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device selects
80 * an appropriate format; it can be queried with getStreamInfo.
81 *
82 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must be
83 * equal to the size in bytes of the buffers to allocate for the stream. For
84 * other formats, the size parameter is ignored.
85 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070086 status_t createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070087 uint32_t width, uint32_t height, int format, size_t size,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070088 int *id);
89
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070090 /**
Eino-Ville Talvala69230df2012-08-29 17:37:16 -070091 * Create an input reprocess stream that uses buffers from an existing
92 * output stream.
93 */
94 status_t createReprocessStreamFromStream(int outputId, int *id);
95
96 /**
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070097 * Get information about a given stream.
98 */
99 status_t getStreamInfo(int id,
100 uint32_t *width, uint32_t *height, uint32_t *format);
101
102 /**
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700103 * Set stream gralloc buffer transform
104 */
105 status_t setStreamTransform(int id, int transform);
106
107 /**
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700108 * Delete stream. Must not be called if there are requests in flight which
109 * reference that stream.
110 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700111 status_t deleteStream(int id);
112
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700113 /**
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700114 * Delete reprocess stream. Must not be called if there are requests in
115 * flight which reference that stream.
116 */
117 status_t deleteReprocessStream(int id);
118
119 /**
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700120 * Create a metadata buffer with fields that the HAL device believes are
121 * best for the given use case
122 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700123 status_t createDefaultRequest(int templateId, CameraMetadata *request);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700124
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700125 /**
126 * Wait until all requests have been processed. Returns INVALID_OPERATION if
127 * the streaming slot is not empty, or TIMED_OUT if the requests haven't
128 * finished processing in 10 seconds.
129 */
130 status_t waitUntilDrained();
131
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700132 /**
133 * Abstract class for HAL notification listeners
134 */
135 class NotificationListener {
136 public:
137 // Refer to the Camera2 HAL definition for notification definitions
138 virtual void notifyError(int errorCode, int arg1, int arg2) = 0;
139 virtual void notifyShutter(int frameNumber, nsecs_t timestamp) = 0;
140 virtual void notifyAutoFocus(uint8_t newState, int triggerId) = 0;
141 virtual void notifyAutoExposure(uint8_t newState, int triggerId) = 0;
142 virtual void notifyAutoWhitebalance(uint8_t newState, int triggerId) = 0;
143 protected:
144 virtual ~NotificationListener();
145 };
146
147 /**
148 * Connect HAL notifications to a listener. Overwrites previous
149 * listener. Set to NULL to stop receiving notifications.
150 */
151 status_t setNotifyCallback(NotificationListener *listener);
152
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700153 /**
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -0700154 * Wait for a new frame to be produced, with timeout in nanoseconds.
155 * Returns TIMED_OUT when no frame produced within the specified duration
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700156 */
Eino-Ville Talvalac8474b62012-08-24 16:30:44 -0700157 status_t waitForNextFrame(nsecs_t timeout);
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700158
159 /**
160 * Get next metadata frame from the frame queue. Returns NULL if the queue
161 * is empty; caller takes ownership of the metadata buffer.
162 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700163 status_t getNextFrame(CameraMetadata *frame);
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700164
165 /**
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700166 * Trigger auto-focus. The latest ID used in a trigger autofocus or cancel
167 * autofocus call will be returned by the HAL in all subsequent AF
168 * notifications.
169 */
170 status_t triggerAutofocus(uint32_t id);
171
172 /**
173 * Cancel auto-focus. The latest ID used in a trigger autofocus/cancel
174 * autofocus call will be returned by the HAL in all subsequent AF
175 * notifications.
176 */
177 status_t triggerCancelAutofocus(uint32_t id);
178
179 /**
180 * Trigger pre-capture metering. The latest ID used in a trigger pre-capture
181 * call will be returned by the HAL in all subsequent AE and AWB
182 * notifications.
183 */
184 status_t triggerPrecaptureMetering(uint32_t id);
185
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700186 /**
187 * Abstract interface for clients that want to listen to reprocess buffer
188 * release events
189 */
190 struct BufferReleasedListener: public virtual RefBase {
191 virtual void onBufferReleased(buffer_handle_t *handle) = 0;
192 };
193
194 /**
195 * Push a buffer to be reprocessed into a reprocessing stream, and
196 * provide a listener to call once the buffer is returned by the HAL
197 */
198 status_t pushReprocessBuffer(int reprocessStreamId,
199 buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
200
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700201 private:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700202 const int mId;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700203 camera2_device_t *mDevice;
204
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700205 CameraMetadata mDeviceInfo;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700206 vendor_tag_query_ops_t *mVendorTagOps;
207
208 /**
209 * Queue class for both sending requests to a camera2 device, and for
210 * receiving frames from a camera2 device.
211 */
212 class MetadataQueue: public camera2_request_queue_src_ops_t,
213 public camera2_frame_queue_dst_ops_t {
214 public:
215 MetadataQueue();
216 ~MetadataQueue();
217
218 // Interface to camera2 HAL device, either for requests (device is
219 // consumer) or for frames (device is producer)
220 const camera2_request_queue_src_ops_t* getToConsumerInterface();
221 void setFromConsumerInterface(camera2_device_t *d);
222
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700223 // Connect queue consumer endpoint to a camera2 device
224 status_t setConsumerDevice(camera2_device_t *d);
225 // Connect queue producer endpoint to a camera2 device
226 status_t setProducerDevice(camera2_device_t *d);
227
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700228 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
229
230 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
231 // On dequeue, user takes ownership of buffer pointer.
232 status_t enqueue(camera_metadata_t *buf);
Eino-Ville Talvalafbd60662012-10-16 10:28:07 -0700233 status_t dequeue(camera_metadata_t **buf, bool incrementCount = false);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700234 int getBufferCount();
235 status_t waitForBuffer(nsecs_t timeout);
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700236 // Wait until a buffer with the given ID is dequeued. Will return
237 // immediately if the latest buffer dequeued has that ID.
238 status_t waitForDequeue(int32_t id, nsecs_t timeout);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700239
240 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
241 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700242 // dequeues the first new entry. The metadata buffers passed in are
243 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700244 status_t setStreamSlot(camera_metadata_t *buf);
245 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
246
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700247 status_t dump(int fd, const Vector<String16>& args);
248
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700249 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700250 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700251 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
252 List<camera_metadata_t*>::iterator end);
253
254 camera2_device_t *mDevice;
255
256 Mutex mMutex;
257 Condition notEmpty;
258
259 int mFrameCount;
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700260 int32_t mLatestRequestId;
261 Condition mNewRequestId;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700262
263 int mCount;
264 List<camera_metadata_t*> mEntries;
265 int mStreamSlotCount;
266 List<camera_metadata_t*> mStreamSlot;
267
268 bool mSignalConsumer;
269
270 static MetadataQueue* getInstance(
271 const camera2_frame_queue_dst_ops_t *q);
272 static MetadataQueue* getInstance(
273 const camera2_request_queue_src_ops_t *q);
274
275 static int consumer_buffer_count(
276 const camera2_request_queue_src_ops_t *q);
277
278 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
279 camera_metadata_t **buffer);
280
281 static int consumer_free(const camera2_request_queue_src_ops_t *q,
282 camera_metadata_t *old_buffer);
283
284 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
285 size_t entries, size_t bytes,
286 camera_metadata_t **buffer);
287
288 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
289 camera_metadata_t *old_buffer);
290
291 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
292 camera_metadata_t *filled_buffer);
293
294 }; // class MetadataQueue
295
296 MetadataQueue mRequestQueue;
297 MetadataQueue mFrameQueue;
298
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700299 /**
300 * Adapter from an ANativeWindow interface to camera2 device stream ops.
301 * Also takes care of allocating/deallocating stream in device interface
302 */
303 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
304 public:
305 StreamAdapter(camera2_device_t *d);
306
307 ~StreamAdapter();
308
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700309 /**
310 * Create a HAL device stream of the requested size and format.
311 *
312 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
313 * selects an appropriate format; it can be queried with getFormat.
314 *
315 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
316 * be equal to the size in bytes of the buffers to allocate for the
317 * stream. For other formats, the size parameter is ignored.
318 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700319 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700320 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700321
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700322 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700323
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700324 status_t setTransform(int transform);
325
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700326 // Get stream parameters.
327 // Only valid after a successful connectToDevice call.
328 int getId() const { return mId; }
329 uint32_t getWidth() const { return mWidth; }
330 uint32_t getHeight() const { return mHeight; }
331 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700332
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700333 // Dump stream information
334 status_t dump(int fd, const Vector<String16>& args);
335
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700336 private:
337 enum {
338 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700339 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700340 ALLOCATED,
341 CONNECTED,
342 ACTIVE
343 } mState;
344
345 sp<ANativeWindow> mConsumerInterface;
346 camera2_device_t *mDevice;
347
348 uint32_t mId;
349 uint32_t mWidth;
350 uint32_t mHeight;
351 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700352 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700353 uint32_t mUsage;
354 uint32_t mMaxProducerBuffers;
355 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700356 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700357 int mFormatRequested;
358
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700359 /** Debugging information */
360 uint32_t mActiveBuffers;
361 uint32_t mFrameCount;
362 int64_t mLastTimestamp;
363
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700364 const camera2_stream_ops *getStreamOps();
365
366 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
367
368 static int dequeue_buffer(const camera2_stream_ops_t *w,
369 buffer_handle_t** buffer);
370
371 static int enqueue_buffer(const camera2_stream_ops_t* w,
372 int64_t timestamp,
373 buffer_handle_t* buffer);
374
375 static int cancel_buffer(const camera2_stream_ops_t* w,
376 buffer_handle_t* buffer);
377
378 static int set_crop(const camera2_stream_ops_t* w,
379 int left, int top, int right, int bottom);
380 }; // class StreamAdapter
381
382 typedef List<sp<StreamAdapter> > StreamList;
383 StreamList mStreams;
384
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700385 /**
386 * Adapter from an ANativeWindow interface to camera2 device stream ops.
387 * Also takes care of allocating/deallocating stream in device interface
388 */
389 class ReprocessStreamAdapter: public camera2_stream_in_ops, public virtual RefBase {
390 public:
391 ReprocessStreamAdapter(camera2_device_t *d);
392
393 ~ReprocessStreamAdapter();
394
395 /**
396 * Create a HAL device reprocess stream based on an existing output stream.
397 */
398 status_t connectToDevice(const sp<StreamAdapter> &outputStream);
399
400 status_t release();
401
402 /**
403 * Push buffer into stream for reprocessing. Takes ownership until it notifies
404 * that the buffer has been released
405 */
406 status_t pushIntoStream(buffer_handle_t *handle,
407 const wp<BufferReleasedListener> &releaseListener);
408
409 /**
410 * Get stream parameters.
411 * Only valid after a successful connectToDevice call.
412 */
413 int getId() const { return mId; }
414 uint32_t getWidth() const { return mWidth; }
415 uint32_t getHeight() const { return mHeight; }
416 uint32_t getFormat() const { return mFormat; }
417
418 // Dump stream information
419 status_t dump(int fd, const Vector<String16>& args);
420
421 private:
422 enum {
423 ERROR = -1,
424 RELEASED = 0,
425 ACTIVE
426 } mState;
427
428 sp<ANativeWindow> mConsumerInterface;
429 wp<StreamAdapter> mBaseStream;
430
431 struct QueueEntry {
432 buffer_handle_t *handle;
433 wp<BufferReleasedListener> releaseListener;
434 };
435
436 List<QueueEntry> mQueue;
437
438 List<QueueEntry> mInFlightQueue;
439
440 camera2_device_t *mDevice;
441
442 uint32_t mId;
443 uint32_t mWidth;
444 uint32_t mHeight;
445 uint32_t mFormat;
446
447 /** Debugging information */
448 uint32_t mActiveBuffers;
449 uint32_t mFrameCount;
450 int64_t mLastTimestamp;
451
452 const camera2_stream_in_ops *getStreamOps();
453
454 static int acquire_buffer(const camera2_stream_in_ops_t *w,
455 buffer_handle_t** buffer);
456
457 static int release_buffer(const camera2_stream_in_ops_t* w,
458 buffer_handle_t* buffer);
459
460 }; // class ReprocessStreamAdapter
461
462 typedef List<sp<ReprocessStreamAdapter> > ReprocessStreamList;
463 ReprocessStreamList mReprocessStreams;
464
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700465 // Receives HAL notifications and routes them to the NotificationListener
466 static void notificationCallback(int32_t msg_type,
467 int32_t ext1,
468 int32_t ext2,
469 int32_t ext3,
470 void *user);
471
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700472}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700473
474}; // namespace android
475
476#endif