blob: 2aa22a2be4b64d15d0bce82c905d2c9903818b49 [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
31 */
32class Camera2Device: public CameraDeviceBase {
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070033 public:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070034 Camera2Device(int id);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070035
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080036 virtual ~Camera2Device();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070037
38 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080039 * CameraDevice interface
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070040 */
Igor Murashkin71381052013-03-04 14:53:08 -080041 virtual int getId() const;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042 virtual status_t initialize(camera_module_t *module);
43 virtual status_t disconnect();
44 virtual status_t dump(int fd, const Vector<String16>& args);
45 virtual const CameraMetadata& info() const;
46 virtual status_t capture(CameraMetadata &request);
47 virtual status_t setStreamingRequest(const CameraMetadata &request);
48 virtual status_t clearStreamingRequest();
49 virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout);
50 virtual status_t createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070051 uint32_t width, uint32_t height, int format, size_t size,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070052 int *id);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080053 virtual status_t createReprocessStreamFromStream(int outputId, int *id);
54 virtual status_t getStreamInfo(int id,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070055 uint32_t *width, uint32_t *height, uint32_t *format);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080056 virtual status_t setStreamTransform(int id, int transform);
57 virtual status_t deleteStream(int id);
58 virtual status_t deleteReprocessStream(int id);
59 virtual status_t createDefaultRequest(int templateId, CameraMetadata *request);
60 virtual status_t waitUntilDrained();
61 virtual status_t setNotifyCallback(NotificationListener *listener);
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -070062 virtual bool willNotify3A();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080063 virtual status_t waitForNextFrame(nsecs_t timeout);
64 virtual status_t getNextFrame(CameraMetadata *frame);
65 virtual status_t triggerAutofocus(uint32_t id);
66 virtual status_t triggerCancelAutofocus(uint32_t id);
67 virtual status_t triggerPrecaptureMetering(uint32_t id);
68 virtual status_t pushReprocessBuffer(int reprocessStreamId,
Eino-Ville Talvala69230df2012-08-29 17:37:16 -070069 buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -070070 // Flush implemented as just a wait
71 virtual status_t flush();
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070072 private:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070073 const int mId;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080074 camera2_device_t *mHal2Device;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070075
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070076 CameraMetadata mDeviceInfo;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070077 vendor_tag_query_ops_t *mVendorTagOps;
78
79 /**
80 * Queue class for both sending requests to a camera2 device, and for
81 * receiving frames from a camera2 device.
82 */
83 class MetadataQueue: public camera2_request_queue_src_ops_t,
84 public camera2_frame_queue_dst_ops_t {
85 public:
86 MetadataQueue();
87 ~MetadataQueue();
88
89 // Interface to camera2 HAL device, either for requests (device is
90 // consumer) or for frames (device is producer)
91 const camera2_request_queue_src_ops_t* getToConsumerInterface();
92 void setFromConsumerInterface(camera2_device_t *d);
93
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070094 // Connect queue consumer endpoint to a camera2 device
95 status_t setConsumerDevice(camera2_device_t *d);
96 // Connect queue producer endpoint to a camera2 device
97 status_t setProducerDevice(camera2_device_t *d);
98
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070099 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
100
101 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
102 // On dequeue, user takes ownership of buffer pointer.
103 status_t enqueue(camera_metadata_t *buf);
Eino-Ville Talvalafbd60662012-10-16 10:28:07 -0700104 status_t dequeue(camera_metadata_t **buf, bool incrementCount = false);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700105 int getBufferCount();
106 status_t waitForBuffer(nsecs_t timeout);
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700107 // Wait until a buffer with the given ID is dequeued. Will return
108 // immediately if the latest buffer dequeued has that ID.
109 status_t waitForDequeue(int32_t id, nsecs_t timeout);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700110
111 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
112 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700113 // dequeues the first new entry. The metadata buffers passed in are
114 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700115 status_t setStreamSlot(camera_metadata_t *buf);
116 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
117
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700118 // Clear the request queue and the streaming slot
119 status_t clear();
120
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700121 status_t dump(int fd, const Vector<String16>& args);
122
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700123 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700124 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700125 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
126 List<camera_metadata_t*>::iterator end);
127
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800128 camera2_device_t *mHal2Device;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700129
130 Mutex mMutex;
131 Condition notEmpty;
132
133 int mFrameCount;
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700134 int32_t mLatestRequestId;
135 Condition mNewRequestId;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700136
137 int mCount;
138 List<camera_metadata_t*> mEntries;
139 int mStreamSlotCount;
140 List<camera_metadata_t*> mStreamSlot;
141
142 bool mSignalConsumer;
143
144 static MetadataQueue* getInstance(
145 const camera2_frame_queue_dst_ops_t *q);
146 static MetadataQueue* getInstance(
147 const camera2_request_queue_src_ops_t *q);
148
149 static int consumer_buffer_count(
150 const camera2_request_queue_src_ops_t *q);
151
152 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
153 camera_metadata_t **buffer);
154
155 static int consumer_free(const camera2_request_queue_src_ops_t *q,
156 camera_metadata_t *old_buffer);
157
158 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
159 size_t entries, size_t bytes,
160 camera_metadata_t **buffer);
161
162 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
163 camera_metadata_t *old_buffer);
164
165 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
166 camera_metadata_t *filled_buffer);
167
168 }; // class MetadataQueue
169
170 MetadataQueue mRequestQueue;
171 MetadataQueue mFrameQueue;
172
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700173 /**
174 * Adapter from an ANativeWindow interface to camera2 device stream ops.
175 * Also takes care of allocating/deallocating stream in device interface
176 */
177 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
178 public:
179 StreamAdapter(camera2_device_t *d);
180
181 ~StreamAdapter();
182
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700183 /**
184 * Create a HAL device stream of the requested size and format.
185 *
186 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
187 * selects an appropriate format; it can be queried with getFormat.
188 *
189 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
190 * be equal to the size in bytes of the buffers to allocate for the
191 * stream. For other formats, the size parameter is ignored.
192 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700193 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700194 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700195
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700196 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700197
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700198 status_t setTransform(int transform);
199
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700200 // Get stream parameters.
201 // Only valid after a successful connectToDevice call.
202 int getId() const { return mId; }
203 uint32_t getWidth() const { return mWidth; }
204 uint32_t getHeight() const { return mHeight; }
205 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700206
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700207 // Dump stream information
208 status_t dump(int fd, const Vector<String16>& args);
209
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700210 private:
211 enum {
212 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700213 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700214 ALLOCATED,
215 CONNECTED,
216 ACTIVE
217 } mState;
218
219 sp<ANativeWindow> mConsumerInterface;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800220 camera2_device_t *mHal2Device;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700221
222 uint32_t mId;
223 uint32_t mWidth;
224 uint32_t mHeight;
225 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700226 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700227 uint32_t mUsage;
228 uint32_t mMaxProducerBuffers;
229 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700230 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700231 int mFormatRequested;
232
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700233 /** Debugging information */
234 uint32_t mActiveBuffers;
235 uint32_t mFrameCount;
236 int64_t mLastTimestamp;
237
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700238 const camera2_stream_ops *getStreamOps();
239
240 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
241
242 static int dequeue_buffer(const camera2_stream_ops_t *w,
243 buffer_handle_t** buffer);
244
245 static int enqueue_buffer(const camera2_stream_ops_t* w,
246 int64_t timestamp,
247 buffer_handle_t* buffer);
248
249 static int cancel_buffer(const camera2_stream_ops_t* w,
250 buffer_handle_t* buffer);
251
252 static int set_crop(const camera2_stream_ops_t* w,
253 int left, int top, int right, int bottom);
254 }; // class StreamAdapter
255
256 typedef List<sp<StreamAdapter> > StreamList;
257 StreamList mStreams;
258
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700259 /**
260 * Adapter from an ANativeWindow interface to camera2 device stream ops.
261 * Also takes care of allocating/deallocating stream in device interface
262 */
263 class ReprocessStreamAdapter: public camera2_stream_in_ops, public virtual RefBase {
264 public:
265 ReprocessStreamAdapter(camera2_device_t *d);
266
267 ~ReprocessStreamAdapter();
268
269 /**
270 * Create a HAL device reprocess stream based on an existing output stream.
271 */
272 status_t connectToDevice(const sp<StreamAdapter> &outputStream);
273
274 status_t release();
275
276 /**
277 * Push buffer into stream for reprocessing. Takes ownership until it notifies
278 * that the buffer has been released
279 */
280 status_t pushIntoStream(buffer_handle_t *handle,
281 const wp<BufferReleasedListener> &releaseListener);
282
283 /**
284 * Get stream parameters.
285 * Only valid after a successful connectToDevice call.
286 */
287 int getId() const { return mId; }
288 uint32_t getWidth() const { return mWidth; }
289 uint32_t getHeight() const { return mHeight; }
290 uint32_t getFormat() const { return mFormat; }
291
292 // Dump stream information
293 status_t dump(int fd, const Vector<String16>& args);
294
295 private:
296 enum {
297 ERROR = -1,
298 RELEASED = 0,
299 ACTIVE
300 } mState;
301
302 sp<ANativeWindow> mConsumerInterface;
303 wp<StreamAdapter> mBaseStream;
304
305 struct QueueEntry {
306 buffer_handle_t *handle;
307 wp<BufferReleasedListener> releaseListener;
308 };
309
310 List<QueueEntry> mQueue;
311
312 List<QueueEntry> mInFlightQueue;
313
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800314 camera2_device_t *mHal2Device;
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700315
316 uint32_t mId;
317 uint32_t mWidth;
318 uint32_t mHeight;
319 uint32_t mFormat;
320
321 /** Debugging information */
322 uint32_t mActiveBuffers;
323 uint32_t mFrameCount;
324 int64_t mLastTimestamp;
325
326 const camera2_stream_in_ops *getStreamOps();
327
328 static int acquire_buffer(const camera2_stream_in_ops_t *w,
329 buffer_handle_t** buffer);
330
331 static int release_buffer(const camera2_stream_in_ops_t* w,
332 buffer_handle_t* buffer);
333
334 }; // class ReprocessStreamAdapter
335
336 typedef List<sp<ReprocessStreamAdapter> > ReprocessStreamList;
337 ReprocessStreamList mReprocessStreams;
338
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700339 // Receives HAL notifications and routes them to the NotificationListener
340 static void notificationCallback(int32_t msg_type,
341 int32_t ext1,
342 int32_t ext2,
343 int32_t ext3,
344 void *user);
345
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700346}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700347
348}; // namespace android
349
350#endif