blob: 1f53c56b0ac58aea5fb08abafb527547d2ef0df4 [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>
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070024
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070025#include "common/CameraDeviceBase.h"
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070026
27namespace android {
28
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080029/**
30 * CameraDevice for HAL devices with version CAMERA_DEVICE_API_VERSION_2_0
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070031 *
32 * TODO for camera2 API implementation:
33 * Does not produce notifyShutter / notifyIdle callbacks to NotificationListener
34 * Use waitUntilDrained for idle.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080035 */
36class Camera2Device: public CameraDeviceBase {
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070037 public:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070038 Camera2Device(int id);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070039
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080040 virtual ~Camera2Device();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070041
42 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080043 * CameraDevice interface
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070044 */
Igor Murashkin71381052013-03-04 14:53:08 -080045 virtual int getId() const;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080046 virtual status_t initialize(camera_module_t *module);
47 virtual status_t disconnect();
48 virtual status_t dump(int fd, const Vector<String16>& args);
49 virtual const CameraMetadata& info() const;
50 virtual status_t capture(CameraMetadata &request);
51 virtual status_t setStreamingRequest(const CameraMetadata &request);
52 virtual status_t clearStreamingRequest();
53 virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout);
54 virtual status_t createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070055 uint32_t width, uint32_t height, int format, size_t size,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070056 int *id);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080057 virtual status_t createReprocessStreamFromStream(int outputId, int *id);
58 virtual status_t getStreamInfo(int id,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070059 uint32_t *width, uint32_t *height, uint32_t *format);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080060 virtual status_t setStreamTransform(int id, int transform);
61 virtual status_t deleteStream(int id);
62 virtual status_t deleteReprocessStream(int id);
63 virtual status_t createDefaultRequest(int templateId, CameraMetadata *request);
64 virtual status_t waitUntilDrained();
65 virtual status_t setNotifyCallback(NotificationListener *listener);
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -070066 virtual bool willNotify3A();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080067 virtual status_t waitForNextFrame(nsecs_t timeout);
68 virtual status_t getNextFrame(CameraMetadata *frame);
69 virtual status_t triggerAutofocus(uint32_t id);
70 virtual status_t triggerCancelAutofocus(uint32_t id);
71 virtual status_t triggerPrecaptureMetering(uint32_t id);
72 virtual status_t pushReprocessBuffer(int reprocessStreamId,
Eino-Ville Talvala69230df2012-08-29 17:37:16 -070073 buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -070074 // Flush implemented as just a wait
75 virtual status_t flush();
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070076 private:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070077 const int mId;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080078 camera2_device_t *mHal2Device;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070079
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070080 CameraMetadata mDeviceInfo;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070081 vendor_tag_query_ops_t *mVendorTagOps;
82
83 /**
84 * Queue class for both sending requests to a camera2 device, and for
85 * receiving frames from a camera2 device.
86 */
87 class MetadataQueue: public camera2_request_queue_src_ops_t,
88 public camera2_frame_queue_dst_ops_t {
89 public:
90 MetadataQueue();
91 ~MetadataQueue();
92
93 // Interface to camera2 HAL device, either for requests (device is
94 // consumer) or for frames (device is producer)
95 const camera2_request_queue_src_ops_t* getToConsumerInterface();
96 void setFromConsumerInterface(camera2_device_t *d);
97
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070098 // Connect queue consumer endpoint to a camera2 device
99 status_t setConsumerDevice(camera2_device_t *d);
100 // Connect queue producer endpoint to a camera2 device
101 status_t setProducerDevice(camera2_device_t *d);
102
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700103 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
104
105 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
106 // On dequeue, user takes ownership of buffer pointer.
107 status_t enqueue(camera_metadata_t *buf);
Eino-Ville Talvalafbd60662012-10-16 10:28:07 -0700108 status_t dequeue(camera_metadata_t **buf, bool incrementCount = false);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700109 int getBufferCount();
110 status_t waitForBuffer(nsecs_t timeout);
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700111 // Wait until a buffer with the given ID is dequeued. Will return
112 // immediately if the latest buffer dequeued has that ID.
113 status_t waitForDequeue(int32_t id, nsecs_t timeout);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700114
115 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
116 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700117 // dequeues the first new entry. The metadata buffers passed in are
118 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700119 status_t setStreamSlot(camera_metadata_t *buf);
120 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
121
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700122 // Clear the request queue and the streaming slot
123 status_t clear();
124
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700125 status_t dump(int fd, const Vector<String16>& args);
126
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700127 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700128 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700129 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
130 List<camera_metadata_t*>::iterator end);
131
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800132 camera2_device_t *mHal2Device;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700133
134 Mutex mMutex;
135 Condition notEmpty;
136
137 int mFrameCount;
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700138 int32_t mLatestRequestId;
139 Condition mNewRequestId;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700140
141 int mCount;
142 List<camera_metadata_t*> mEntries;
143 int mStreamSlotCount;
144 List<camera_metadata_t*> mStreamSlot;
145
146 bool mSignalConsumer;
147
148 static MetadataQueue* getInstance(
149 const camera2_frame_queue_dst_ops_t *q);
150 static MetadataQueue* getInstance(
151 const camera2_request_queue_src_ops_t *q);
152
153 static int consumer_buffer_count(
154 const camera2_request_queue_src_ops_t *q);
155
156 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
157 camera_metadata_t **buffer);
158
159 static int consumer_free(const camera2_request_queue_src_ops_t *q,
160 camera_metadata_t *old_buffer);
161
162 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
163 size_t entries, size_t bytes,
164 camera_metadata_t **buffer);
165
166 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
167 camera_metadata_t *old_buffer);
168
169 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
170 camera_metadata_t *filled_buffer);
171
172 }; // class MetadataQueue
173
174 MetadataQueue mRequestQueue;
175 MetadataQueue mFrameQueue;
176
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700177 /**
178 * Adapter from an ANativeWindow interface to camera2 device stream ops.
179 * Also takes care of allocating/deallocating stream in device interface
180 */
181 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
182 public:
183 StreamAdapter(camera2_device_t *d);
184
185 ~StreamAdapter();
186
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700187 /**
188 * Create a HAL device stream of the requested size and format.
189 *
190 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
191 * selects an appropriate format; it can be queried with getFormat.
192 *
193 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
194 * be equal to the size in bytes of the buffers to allocate for the
195 * stream. For other formats, the size parameter is ignored.
196 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700197 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700198 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700199
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700200 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700201
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700202 status_t setTransform(int transform);
203
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700204 // Get stream parameters.
205 // Only valid after a successful connectToDevice call.
206 int getId() const { return mId; }
207 uint32_t getWidth() const { return mWidth; }
208 uint32_t getHeight() const { return mHeight; }
209 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700210
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700211 // Dump stream information
212 status_t dump(int fd, const Vector<String16>& args);
213
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700214 private:
215 enum {
216 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700217 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700218 ALLOCATED,
219 CONNECTED,
220 ACTIVE
221 } mState;
222
223 sp<ANativeWindow> mConsumerInterface;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800224 camera2_device_t *mHal2Device;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700225
226 uint32_t mId;
227 uint32_t mWidth;
228 uint32_t mHeight;
229 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700230 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700231 uint32_t mUsage;
232 uint32_t mMaxProducerBuffers;
233 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700234 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700235 int mFormatRequested;
236
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700237 /** Debugging information */
238 uint32_t mActiveBuffers;
239 uint32_t mFrameCount;
240 int64_t mLastTimestamp;
241
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700242 const camera2_stream_ops *getStreamOps();
243
244 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
245
246 static int dequeue_buffer(const camera2_stream_ops_t *w,
247 buffer_handle_t** buffer);
248
249 static int enqueue_buffer(const camera2_stream_ops_t* w,
250 int64_t timestamp,
251 buffer_handle_t* buffer);
252
253 static int cancel_buffer(const camera2_stream_ops_t* w,
254 buffer_handle_t* buffer);
255
256 static int set_crop(const camera2_stream_ops_t* w,
257 int left, int top, int right, int bottom);
258 }; // class StreamAdapter
259
260 typedef List<sp<StreamAdapter> > StreamList;
261 StreamList mStreams;
262
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700263 /**
264 * Adapter from an ANativeWindow interface to camera2 device stream ops.
265 * Also takes care of allocating/deallocating stream in device interface
266 */
267 class ReprocessStreamAdapter: public camera2_stream_in_ops, public virtual RefBase {
268 public:
269 ReprocessStreamAdapter(camera2_device_t *d);
270
271 ~ReprocessStreamAdapter();
272
273 /**
274 * Create a HAL device reprocess stream based on an existing output stream.
275 */
276 status_t connectToDevice(const sp<StreamAdapter> &outputStream);
277
278 status_t release();
279
280 /**
281 * Push buffer into stream for reprocessing. Takes ownership until it notifies
282 * that the buffer has been released
283 */
284 status_t pushIntoStream(buffer_handle_t *handle,
285 const wp<BufferReleasedListener> &releaseListener);
286
287 /**
288 * Get stream parameters.
289 * Only valid after a successful connectToDevice call.
290 */
291 int getId() const { return mId; }
292 uint32_t getWidth() const { return mWidth; }
293 uint32_t getHeight() const { return mHeight; }
294 uint32_t getFormat() const { return mFormat; }
295
296 // Dump stream information
297 status_t dump(int fd, const Vector<String16>& args);
298
299 private:
300 enum {
301 ERROR = -1,
302 RELEASED = 0,
303 ACTIVE
304 } mState;
305
306 sp<ANativeWindow> mConsumerInterface;
307 wp<StreamAdapter> mBaseStream;
308
309 struct QueueEntry {
310 buffer_handle_t *handle;
311 wp<BufferReleasedListener> releaseListener;
312 };
313
314 List<QueueEntry> mQueue;
315
316 List<QueueEntry> mInFlightQueue;
317
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800318 camera2_device_t *mHal2Device;
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700319
320 uint32_t mId;
321 uint32_t mWidth;
322 uint32_t mHeight;
323 uint32_t mFormat;
324
325 /** Debugging information */
326 uint32_t mActiveBuffers;
327 uint32_t mFrameCount;
328 int64_t mLastTimestamp;
329
330 const camera2_stream_in_ops *getStreamOps();
331
332 static int acquire_buffer(const camera2_stream_in_ops_t *w,
333 buffer_handle_t** buffer);
334
335 static int release_buffer(const camera2_stream_in_ops_t* w,
336 buffer_handle_t* buffer);
337
338 }; // class ReprocessStreamAdapter
339
340 typedef List<sp<ReprocessStreamAdapter> > ReprocessStreamList;
341 ReprocessStreamList mReprocessStreams;
342
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700343 // Receives HAL notifications and routes them to the NotificationListener
344 static void notificationCallback(int32_t msg_type,
345 int32_t ext1,
346 int32_t ext2,
347 int32_t ext3,
348 void *user);
349
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700350}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700351
352}; // namespace android
353
354#endif