blob: 9be370f8e7084d64a2a1e2aadae463862e3071f0 [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 /**
127 * Trigger auto-focus. The latest ID used in a trigger autofocus or cancel
128 * autofocus call will be returned by the HAL in all subsequent AF
129 * notifications.
130 */
131 status_t triggerAutofocus(uint32_t id);
132
133 /**
134 * Cancel auto-focus. The latest ID used in a trigger autofocus/cancel
135 * autofocus call will be returned by the HAL in all subsequent AF
136 * notifications.
137 */
138 status_t triggerCancelAutofocus(uint32_t id);
139
140 /**
141 * Trigger pre-capture metering. The latest ID used in a trigger pre-capture
142 * call will be returned by the HAL in all subsequent AE and AWB
143 * notifications.
144 */
145 status_t triggerPrecaptureMetering(uint32_t id);
146
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700147 private:
148
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700149 const int mId;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700150 camera2_device_t *mDevice;
151
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700152 camera_metadata_t *mDeviceInfo;
153 vendor_tag_query_ops_t *mVendorTagOps;
154
155 /**
156 * Queue class for both sending requests to a camera2 device, and for
157 * receiving frames from a camera2 device.
158 */
159 class MetadataQueue: public camera2_request_queue_src_ops_t,
160 public camera2_frame_queue_dst_ops_t {
161 public:
162 MetadataQueue();
163 ~MetadataQueue();
164
165 // Interface to camera2 HAL device, either for requests (device is
166 // consumer) or for frames (device is producer)
167 const camera2_request_queue_src_ops_t* getToConsumerInterface();
168 void setFromConsumerInterface(camera2_device_t *d);
169
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700170 // Connect queue consumer endpoint to a camera2 device
171 status_t setConsumerDevice(camera2_device_t *d);
172 // Connect queue producer endpoint to a camera2 device
173 status_t setProducerDevice(camera2_device_t *d);
174
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700175 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
176
177 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
178 // On dequeue, user takes ownership of buffer pointer.
179 status_t enqueue(camera_metadata_t *buf);
180 status_t dequeue(camera_metadata_t **buf, bool incrementCount = true);
181 int getBufferCount();
182 status_t waitForBuffer(nsecs_t timeout);
183
184 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
185 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700186 // dequeues the first new entry. The metadata buffers passed in are
187 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700188 status_t setStreamSlot(camera_metadata_t *buf);
189 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
190
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700191 status_t dump(int fd, const Vector<String16>& args);
192
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700193 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700194 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700195 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
196 List<camera_metadata_t*>::iterator end);
197
198 camera2_device_t *mDevice;
199
200 Mutex mMutex;
201 Condition notEmpty;
202
203 int mFrameCount;
204
205 int mCount;
206 List<camera_metadata_t*> mEntries;
207 int mStreamSlotCount;
208 List<camera_metadata_t*> mStreamSlot;
209
210 bool mSignalConsumer;
211
212 static MetadataQueue* getInstance(
213 const camera2_frame_queue_dst_ops_t *q);
214 static MetadataQueue* getInstance(
215 const camera2_request_queue_src_ops_t *q);
216
217 static int consumer_buffer_count(
218 const camera2_request_queue_src_ops_t *q);
219
220 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
221 camera_metadata_t **buffer);
222
223 static int consumer_free(const camera2_request_queue_src_ops_t *q,
224 camera_metadata_t *old_buffer);
225
226 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
227 size_t entries, size_t bytes,
228 camera_metadata_t **buffer);
229
230 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
231 camera_metadata_t *old_buffer);
232
233 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
234 camera_metadata_t *filled_buffer);
235
236 }; // class MetadataQueue
237
238 MetadataQueue mRequestQueue;
239 MetadataQueue mFrameQueue;
240
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700241 /**
242 * Adapter from an ANativeWindow interface to camera2 device stream ops.
243 * Also takes care of allocating/deallocating stream in device interface
244 */
245 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
246 public:
247 StreamAdapter(camera2_device_t *d);
248
249 ~StreamAdapter();
250
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700251 /**
252 * Create a HAL device stream of the requested size and format.
253 *
254 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
255 * selects an appropriate format; it can be queried with getFormat.
256 *
257 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
258 * be equal to the size in bytes of the buffers to allocate for the
259 * stream. For other formats, the size parameter is ignored.
260 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700261 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700262 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700263
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700264 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700265
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700266 status_t setTransform(int transform);
267
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700268 // Get stream parameters.
269 // Only valid after a successful connectToDevice call.
270 int getId() const { return mId; }
271 uint32_t getWidth() const { return mWidth; }
272 uint32_t getHeight() const { return mHeight; }
273 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700274
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700275 // Dump stream information
276 status_t dump(int fd, const Vector<String16>& args);
277
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700278 private:
279 enum {
280 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700281 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700282 ALLOCATED,
283 CONNECTED,
284 ACTIVE
285 } mState;
286
287 sp<ANativeWindow> mConsumerInterface;
288 camera2_device_t *mDevice;
289
290 uint32_t mId;
291 uint32_t mWidth;
292 uint32_t mHeight;
293 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700294 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700295 uint32_t mUsage;
296 uint32_t mMaxProducerBuffers;
297 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700298 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700299 int mFormatRequested;
300
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700301 /** Debugging information */
302 uint32_t mActiveBuffers;
303 uint32_t mFrameCount;
304 int64_t mLastTimestamp;
305
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700306 const camera2_stream_ops *getStreamOps();
307
308 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
309
310 static int dequeue_buffer(const camera2_stream_ops_t *w,
311 buffer_handle_t** buffer);
312
313 static int enqueue_buffer(const camera2_stream_ops_t* w,
314 int64_t timestamp,
315 buffer_handle_t* buffer);
316
317 static int cancel_buffer(const camera2_stream_ops_t* w,
318 buffer_handle_t* buffer);
319
320 static int set_crop(const camera2_stream_ops_t* w,
321 int left, int top, int right, int bottom);
322 }; // class StreamAdapter
323
324 typedef List<sp<StreamAdapter> > StreamList;
325 StreamList mStreams;
326
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700327 // Receives HAL notifications and routes them to the NotificationListener
328 static void notificationCallback(int32_t msg_type,
329 int32_t ext1,
330 int32_t ext2,
331 int32_t ext3,
332 void *user);
333
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700334}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700335
336}; // namespace android
337
338#endif