blob: 790b946db60d9ffd95b45a5956fc3c0669e91980 [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"
30
31namespace android {
32
33class Camera2Device : public virtual RefBase {
34 public:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070035 Camera2Device(int id);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070036
37 ~Camera2Device();
38
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070039 status_t initialize(camera_module_t *module);
40
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070041 status_t dump(int fd, const Vector<String16>& args);
42
43 /**
44 * Get a pointer to the device's static characteristics metadata buffer
45 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070046 camera_metadata_t* info();
47
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070048 /**
49 * Submit request for capture. The Camera2Device takes ownership of the
50 * passed-in buffer.
51 */
52 status_t capture(camera_metadata_t *request);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070053
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070054 /**
55 * Submit request for streaming. The Camera2Device makes a copy of the
56 * passed-in buffer and the caller retains ownership.
57 */
58 status_t setStreamingRequest(camera_metadata_t *request);
59
60 /**
61 * Create an output stream of the requested size and format.
62 *
63 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device selects
64 * an appropriate format; it can be queried with getStreamInfo.
65 *
66 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must be
67 * equal to the size in bytes of the buffers to allocate for the stream. For
68 * other formats, the size parameter is ignored.
69 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070070 status_t createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070071 uint32_t width, uint32_t height, int format, size_t size,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070072 int *id);
73
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070074 /**
75 * Get information about a given stream.
76 */
77 status_t getStreamInfo(int id,
78 uint32_t *width, uint32_t *height, uint32_t *format);
79
80 /**
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -070081 * Set stream gralloc buffer transform
82 */
83 status_t setStreamTransform(int id, int transform);
84
85 /**
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070086 * Delete stream. Must not be called if there are requests in flight which
87 * reference that stream.
88 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070089 status_t deleteStream(int id);
90
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070091 /**
92 * Create a metadata buffer with fields that the HAL device believes are
93 * best for the given use case
94 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070095 status_t createDefaultRequest(int templateId,
96 camera_metadata_t **request);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070097
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070098 /**
99 * Wait until all requests have been processed. Returns INVALID_OPERATION if
100 * the streaming slot is not empty, or TIMED_OUT if the requests haven't
101 * finished processing in 10 seconds.
102 */
103 status_t waitUntilDrained();
104
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700105 /**
106 * Abstract class for HAL notification listeners
107 */
108 class NotificationListener {
109 public:
110 // Refer to the Camera2 HAL definition for notification definitions
111 virtual void notifyError(int errorCode, int arg1, int arg2) = 0;
112 virtual void notifyShutter(int frameNumber, nsecs_t timestamp) = 0;
113 virtual void notifyAutoFocus(uint8_t newState, int triggerId) = 0;
114 virtual void notifyAutoExposure(uint8_t newState, int triggerId) = 0;
115 virtual void notifyAutoWhitebalance(uint8_t newState, int triggerId) = 0;
116 protected:
117 virtual ~NotificationListener();
118 };
119
120 /**
121 * Connect HAL notifications to a listener. Overwrites previous
122 * listener. Set to NULL to stop receiving notifications.
123 */
124 status_t setNotifyCallback(NotificationListener *listener);
125
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700126 /**
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700127 * Abstract class for HAL frame available notifications
128 */
129 class FrameListener {
130 public:
131 virtual void onNewFrameAvailable() = 0;
132 protected:
133 virtual ~FrameListener();
134 };
135
136 /**
137 * Set a frame listener to be notified about new frames.
138 */
139 status_t setFrameListener(FrameListener *listener);
140
141 /**
142 * Get next metadata frame from the frame queue. Returns NULL if the queue
143 * is empty; caller takes ownership of the metadata buffer.
144 */
145 status_t getNextFrame(camera_metadata_t **frame);
146
147 /**
Eino-Ville Talvala174181e2012-08-03 13:53:39 -0700148 * Trigger auto-focus. The latest ID used in a trigger autofocus or cancel
149 * autofocus call will be returned by the HAL in all subsequent AF
150 * notifications.
151 */
152 status_t triggerAutofocus(uint32_t id);
153
154 /**
155 * Cancel auto-focus. The latest ID used in a trigger autofocus/cancel
156 * autofocus call will be returned by the HAL in all subsequent AF
157 * notifications.
158 */
159 status_t triggerCancelAutofocus(uint32_t id);
160
161 /**
162 * Trigger pre-capture metering. The latest ID used in a trigger pre-capture
163 * call will be returned by the HAL in all subsequent AE and AWB
164 * notifications.
165 */
166 status_t triggerPrecaptureMetering(uint32_t id);
167
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700168 private:
169
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700170 const int mId;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700171 camera2_device_t *mDevice;
172
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700173 camera_metadata_t *mDeviceInfo;
174 vendor_tag_query_ops_t *mVendorTagOps;
175
176 /**
177 * Queue class for both sending requests to a camera2 device, and for
178 * receiving frames from a camera2 device.
179 */
180 class MetadataQueue: public camera2_request_queue_src_ops_t,
181 public camera2_frame_queue_dst_ops_t {
182 public:
183 MetadataQueue();
184 ~MetadataQueue();
185
186 // Interface to camera2 HAL device, either for requests (device is
187 // consumer) or for frames (device is producer)
188 const camera2_request_queue_src_ops_t* getToConsumerInterface();
189 void setFromConsumerInterface(camera2_device_t *d);
190
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700191 // Connect queue consumer endpoint to a camera2 device
192 status_t setConsumerDevice(camera2_device_t *d);
193 // Connect queue producer endpoint to a camera2 device
194 status_t setProducerDevice(camera2_device_t *d);
195
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700196 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
197
198 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
199 // On dequeue, user takes ownership of buffer pointer.
200 status_t enqueue(camera_metadata_t *buf);
201 status_t dequeue(camera_metadata_t **buf, bool incrementCount = true);
202 int getBufferCount();
203 status_t waitForBuffer(nsecs_t timeout);
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700204 status_t setListener(FrameListener *listener);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700205
206 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
207 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700208 // dequeues the first new entry. The metadata buffers passed in are
209 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700210 status_t setStreamSlot(camera_metadata_t *buf);
211 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
212
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700213 status_t dump(int fd, const Vector<String16>& args);
214
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700215 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700216 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700217 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
218 List<camera_metadata_t*>::iterator end);
219
220 camera2_device_t *mDevice;
221
222 Mutex mMutex;
223 Condition notEmpty;
224
225 int mFrameCount;
226
227 int mCount;
228 List<camera_metadata_t*> mEntries;
229 int mStreamSlotCount;
230 List<camera_metadata_t*> mStreamSlot;
231
232 bool mSignalConsumer;
Eino-Ville Talvala8ce89d92012-08-10 08:40:26 -0700233 FrameListener *mListener;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700234
235 static MetadataQueue* getInstance(
236 const camera2_frame_queue_dst_ops_t *q);
237 static MetadataQueue* getInstance(
238 const camera2_request_queue_src_ops_t *q);
239
240 static int consumer_buffer_count(
241 const camera2_request_queue_src_ops_t *q);
242
243 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
244 camera_metadata_t **buffer);
245
246 static int consumer_free(const camera2_request_queue_src_ops_t *q,
247 camera_metadata_t *old_buffer);
248
249 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
250 size_t entries, size_t bytes,
251 camera_metadata_t **buffer);
252
253 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
254 camera_metadata_t *old_buffer);
255
256 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
257 camera_metadata_t *filled_buffer);
258
259 }; // class MetadataQueue
260
261 MetadataQueue mRequestQueue;
262 MetadataQueue mFrameQueue;
263
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700264 /**
265 * Adapter from an ANativeWindow interface to camera2 device stream ops.
266 * Also takes care of allocating/deallocating stream in device interface
267 */
268 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
269 public:
270 StreamAdapter(camera2_device_t *d);
271
272 ~StreamAdapter();
273
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700274 /**
275 * Create a HAL device stream of the requested size and format.
276 *
277 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
278 * selects an appropriate format; it can be queried with getFormat.
279 *
280 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
281 * be equal to the size in bytes of the buffers to allocate for the
282 * stream. For other formats, the size parameter is ignored.
283 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700284 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700285 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700286
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700287 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700288
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700289 status_t setTransform(int transform);
290
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700291 // Get stream parameters.
292 // Only valid after a successful connectToDevice call.
293 int getId() const { return mId; }
294 uint32_t getWidth() const { return mWidth; }
295 uint32_t getHeight() const { return mHeight; }
296 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700297
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700298 // Dump stream information
299 status_t dump(int fd, const Vector<String16>& args);
300
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700301 private:
302 enum {
303 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700304 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700305 ALLOCATED,
306 CONNECTED,
307 ACTIVE
308 } mState;
309
310 sp<ANativeWindow> mConsumerInterface;
311 camera2_device_t *mDevice;
312
313 uint32_t mId;
314 uint32_t mWidth;
315 uint32_t mHeight;
316 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700317 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700318 uint32_t mUsage;
319 uint32_t mMaxProducerBuffers;
320 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700321 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700322 int mFormatRequested;
323
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700324 /** Debugging information */
325 uint32_t mActiveBuffers;
326 uint32_t mFrameCount;
327 int64_t mLastTimestamp;
328
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700329 const camera2_stream_ops *getStreamOps();
330
331 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
332
333 static int dequeue_buffer(const camera2_stream_ops_t *w,
334 buffer_handle_t** buffer);
335
336 static int enqueue_buffer(const camera2_stream_ops_t* w,
337 int64_t timestamp,
338 buffer_handle_t* buffer);
339
340 static int cancel_buffer(const camera2_stream_ops_t* w,
341 buffer_handle_t* buffer);
342
343 static int set_crop(const camera2_stream_ops_t* w,
344 int left, int top, int right, int bottom);
345 }; // class StreamAdapter
346
347 typedef List<sp<StreamAdapter> > StreamList;
348 StreamList mStreams;
349
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700350 // Receives HAL notifications and routes them to the NotificationListener
351 static void notificationCallback(int32_t msg_type,
352 int32_t ext1,
353 int32_t ext2,
354 int32_t ext3,
355 void *user);
356
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700357}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700358
359}; // namespace android
360
361#endif