blob: 06047b8816d9d5ea3bb04f3aae78ec42acd23964 [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 Talvala8ce89d92012-08-10 08:40:26 -0700132 * Abstract class for HAL frame available notifications
133 */
134 class FrameListener {
135 public:
136 virtual void onNewFrameAvailable() = 0;
137 protected:
138 virtual ~FrameListener();
139 };
140
141 /**
142 * Set a frame listener to be notified about new frames.
143 */
144 status_t setFrameListener(FrameListener *listener);
145
146 /**
147 * Get next metadata frame from the frame queue. Returns NULL if the queue
148 * is empty; caller takes ownership of the metadata buffer.
149 */
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700150 status_t getNextFrame(CameraMetadata *frame);
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700151
152 /**
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700153 * Trigger auto-focus. The latest ID used in a trigger autofocus or cancel
154 * autofocus call will be returned by the HAL in all subsequent AF
155 * notifications.
156 */
157 status_t triggerAutofocus(uint32_t id);
158
159 /**
160 * Cancel auto-focus. The latest ID used in a trigger autofocus/cancel
161 * autofocus call will be returned by the HAL in all subsequent AF
162 * notifications.
163 */
164 status_t triggerCancelAutofocus(uint32_t id);
165
166 /**
167 * Trigger pre-capture metering. The latest ID used in a trigger pre-capture
168 * call will be returned by the HAL in all subsequent AE and AWB
169 * notifications.
170 */
171 status_t triggerPrecaptureMetering(uint32_t id);
172
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700173 private:
174
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700175 const int mId;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700176 camera2_device_t *mDevice;
177
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700178 CameraMetadata mDeviceInfo;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700179 vendor_tag_query_ops_t *mVendorTagOps;
180
181 /**
182 * Queue class for both sending requests to a camera2 device, and for
183 * receiving frames from a camera2 device.
184 */
185 class MetadataQueue: public camera2_request_queue_src_ops_t,
186 public camera2_frame_queue_dst_ops_t {
187 public:
188 MetadataQueue();
189 ~MetadataQueue();
190
191 // Interface to camera2 HAL device, either for requests (device is
192 // consumer) or for frames (device is producer)
193 const camera2_request_queue_src_ops_t* getToConsumerInterface();
194 void setFromConsumerInterface(camera2_device_t *d);
195
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700196 // Connect queue consumer endpoint to a camera2 device
197 status_t setConsumerDevice(camera2_device_t *d);
198 // Connect queue producer endpoint to a camera2 device
199 status_t setProducerDevice(camera2_device_t *d);
200
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700201 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
202
203 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
204 // On dequeue, user takes ownership of buffer pointer.
205 status_t enqueue(camera_metadata_t *buf);
206 status_t dequeue(camera_metadata_t **buf, bool incrementCount = true);
207 int getBufferCount();
208 status_t waitForBuffer(nsecs_t timeout);
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700209 status_t setListener(FrameListener *listener);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700210
211 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
212 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700213 // dequeues the first new entry. The metadata buffers passed in are
214 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700215 status_t setStreamSlot(camera_metadata_t *buf);
216 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
217
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700218 status_t dump(int fd, const Vector<String16>& args);
219
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700220 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700221 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700222 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
223 List<camera_metadata_t*>::iterator end);
224
225 camera2_device_t *mDevice;
226
227 Mutex mMutex;
228 Condition notEmpty;
229
230 int mFrameCount;
231
232 int mCount;
233 List<camera_metadata_t*> mEntries;
234 int mStreamSlotCount;
235 List<camera_metadata_t*> mStreamSlot;
236
237 bool mSignalConsumer;
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700238 FrameListener *mListener;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700239
240 static MetadataQueue* getInstance(
241 const camera2_frame_queue_dst_ops_t *q);
242 static MetadataQueue* getInstance(
243 const camera2_request_queue_src_ops_t *q);
244
245 static int consumer_buffer_count(
246 const camera2_request_queue_src_ops_t *q);
247
248 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
249 camera_metadata_t **buffer);
250
251 static int consumer_free(const camera2_request_queue_src_ops_t *q,
252 camera_metadata_t *old_buffer);
253
254 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
255 size_t entries, size_t bytes,
256 camera_metadata_t **buffer);
257
258 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
259 camera_metadata_t *old_buffer);
260
261 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
262 camera_metadata_t *filled_buffer);
263
264 }; // class MetadataQueue
265
266 MetadataQueue mRequestQueue;
267 MetadataQueue mFrameQueue;
268
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700269 /**
270 * Adapter from an ANativeWindow interface to camera2 device stream ops.
271 * Also takes care of allocating/deallocating stream in device interface
272 */
273 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
274 public:
275 StreamAdapter(camera2_device_t *d);
276
277 ~StreamAdapter();
278
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700279 /**
280 * Create a HAL device stream of the requested size and format.
281 *
282 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
283 * selects an appropriate format; it can be queried with getFormat.
284 *
285 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
286 * be equal to the size in bytes of the buffers to allocate for the
287 * stream. For other formats, the size parameter is ignored.
288 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700289 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700290 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700291
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700292 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700293
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700294 status_t setTransform(int transform);
295
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700296 // Get stream parameters.
297 // Only valid after a successful connectToDevice call.
298 int getId() const { return mId; }
299 uint32_t getWidth() const { return mWidth; }
300 uint32_t getHeight() const { return mHeight; }
301 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700302
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700303 // Dump stream information
304 status_t dump(int fd, const Vector<String16>& args);
305
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700306 private:
307 enum {
308 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700309 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700310 ALLOCATED,
311 CONNECTED,
312 ACTIVE
313 } mState;
314
315 sp<ANativeWindow> mConsumerInterface;
316 camera2_device_t *mDevice;
317
318 uint32_t mId;
319 uint32_t mWidth;
320 uint32_t mHeight;
321 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700322 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700323 uint32_t mUsage;
324 uint32_t mMaxProducerBuffers;
325 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700326 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700327 int mFormatRequested;
328
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700329 /** Debugging information */
330 uint32_t mActiveBuffers;
331 uint32_t mFrameCount;
332 int64_t mLastTimestamp;
333
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700334 const camera2_stream_ops *getStreamOps();
335
336 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
337
338 static int dequeue_buffer(const camera2_stream_ops_t *w,
339 buffer_handle_t** buffer);
340
341 static int enqueue_buffer(const camera2_stream_ops_t* w,
342 int64_t timestamp,
343 buffer_handle_t* buffer);
344
345 static int cancel_buffer(const camera2_stream_ops_t* w,
346 buffer_handle_t* buffer);
347
348 static int set_crop(const camera2_stream_ops_t* w,
349 int left, int top, int right, int bottom);
350 }; // class StreamAdapter
351
352 typedef List<sp<StreamAdapter> > StreamList;
353 StreamList mStreams;
354
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700355 // Receives HAL notifications and routes them to the NotificationListener
356 static void notificationCallback(int32_t msg_type,
357 int32_t ext1,
358 int32_t ext2,
359 int32_t ext3,
360 void *user);
361
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700362}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700363
364}; // namespace android
365
366#endif