blob: d0ca46ec10e81f2add4db690a52129684ee67c9e [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;
Jianing Weicb0652e2014-03-12 18:29:36 -070050 virtual status_t capture(CameraMetadata &request, int64_t *lastFrameNumber = NULL);
51 virtual status_t captureList(const List<const CameraMetadata> &requests,
52 int64_t *lastFrameNumber = NULL);
53 virtual status_t setStreamingRequest(const CameraMetadata &request,
54 int64_t *lastFrameNumber = NULL);
55 virtual status_t setStreamingRequestList(const List<const CameraMetadata> &requests,
56 int64_t *lastFrameNumber = NULL);
57 virtual status_t clearStreamingRequest(int64_t *lastFrameNumber = NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058 virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout);
59 virtual status_t createStream(sp<ANativeWindow> consumer,
Zhijun He28c9b6f2014-08-08 12:00:47 -070060 uint32_t width, uint32_t height, int format, int *id);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080061 virtual status_t createReprocessStreamFromStream(int outputId, int *id);
62 virtual status_t getStreamInfo(int id,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070063 uint32_t *width, uint32_t *height, uint32_t *format);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080064 virtual status_t setStreamTransform(int id, int transform);
65 virtual status_t deleteStream(int id);
66 virtual status_t deleteReprocessStream(int id);
67 virtual status_t createDefaultRequest(int templateId, CameraMetadata *request);
68 virtual status_t waitUntilDrained();
69 virtual status_t setNotifyCallback(NotificationListener *listener);
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -070070 virtual bool willNotify3A();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080071 virtual status_t waitForNextFrame(nsecs_t timeout);
Jianing Weicb0652e2014-03-12 18:29:36 -070072 virtual status_t getNextResult(CaptureResult *frame);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080073 virtual status_t triggerAutofocus(uint32_t id);
74 virtual status_t triggerCancelAutofocus(uint32_t id);
75 virtual status_t triggerPrecaptureMetering(uint32_t id);
76 virtual status_t pushReprocessBuffer(int reprocessStreamId,
Eino-Ville Talvala69230df2012-08-29 17:37:16 -070077 buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -070078 // Flush implemented as just a wait
Jianing Weicb0652e2014-03-12 18:29:36 -070079 virtual status_t flush(int64_t *lastFrameNumber = NULL);
Zhijun He204e3292014-07-14 17:09:23 -070080 virtual uint32_t getDeviceVersion();
Zhijun He28c9b6f2014-08-08 12:00:47 -070081 virtual ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const;
Zhijun He204e3292014-07-14 17:09:23 -070082
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070083 private:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070084 const int mId;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080085 camera2_device_t *mHal2Device;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070086
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070087 CameraMetadata mDeviceInfo;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070088
Zhijun He204e3292014-07-14 17:09:23 -070089 uint32_t mDeviceVersion;
90
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070091 /**
92 * Queue class for both sending requests to a camera2 device, and for
93 * receiving frames from a camera2 device.
94 */
95 class MetadataQueue: public camera2_request_queue_src_ops_t,
96 public camera2_frame_queue_dst_ops_t {
97 public:
98 MetadataQueue();
99 ~MetadataQueue();
100
101 // Interface to camera2 HAL device, either for requests (device is
102 // consumer) or for frames (device is producer)
103 const camera2_request_queue_src_ops_t* getToConsumerInterface();
104 void setFromConsumerInterface(camera2_device_t *d);
105
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700106 // Connect queue consumer endpoint to a camera2 device
107 status_t setConsumerDevice(camera2_device_t *d);
108 // Connect queue producer endpoint to a camera2 device
109 status_t setProducerDevice(camera2_device_t *d);
110
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700111 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
112
113 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
114 // On dequeue, user takes ownership of buffer pointer.
115 status_t enqueue(camera_metadata_t *buf);
Eino-Ville Talvalafbd60662012-10-16 10:28:07 -0700116 status_t dequeue(camera_metadata_t **buf, bool incrementCount = false);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700117 int getBufferCount();
118 status_t waitForBuffer(nsecs_t timeout);
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700119 // Wait until a buffer with the given ID is dequeued. Will return
120 // immediately if the latest buffer dequeued has that ID.
121 status_t waitForDequeue(int32_t id, nsecs_t timeout);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700122
123 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
124 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700125 // dequeues the first new entry. The metadata buffers passed in are
126 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700127 status_t setStreamSlot(camera_metadata_t *buf);
128 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
129
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700130 // Clear the request queue and the streaming slot
131 status_t clear();
132
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700133 status_t dump(int fd, const Vector<String16>& args);
134
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700135 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700136 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700137 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
138 List<camera_metadata_t*>::iterator end);
139
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800140 camera2_device_t *mHal2Device;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700141
142 Mutex mMutex;
143 Condition notEmpty;
144
145 int mFrameCount;
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700146 int32_t mLatestRequestId;
147 Condition mNewRequestId;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700148
149 int mCount;
150 List<camera_metadata_t*> mEntries;
151 int mStreamSlotCount;
152 List<camera_metadata_t*> mStreamSlot;
153
154 bool mSignalConsumer;
155
156 static MetadataQueue* getInstance(
157 const camera2_frame_queue_dst_ops_t *q);
158 static MetadataQueue* getInstance(
159 const camera2_request_queue_src_ops_t *q);
160
161 static int consumer_buffer_count(
162 const camera2_request_queue_src_ops_t *q);
163
164 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
165 camera_metadata_t **buffer);
166
167 static int consumer_free(const camera2_request_queue_src_ops_t *q,
168 camera_metadata_t *old_buffer);
169
170 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
171 size_t entries, size_t bytes,
172 camera_metadata_t **buffer);
173
174 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
175 camera_metadata_t *old_buffer);
176
177 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
178 camera_metadata_t *filled_buffer);
179
180 }; // class MetadataQueue
181
182 MetadataQueue mRequestQueue;
183 MetadataQueue mFrameQueue;
184
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700185 /**
186 * Adapter from an ANativeWindow interface to camera2 device stream ops.
187 * Also takes care of allocating/deallocating stream in device interface
188 */
189 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
190 public:
191 StreamAdapter(camera2_device_t *d);
192
193 ~StreamAdapter();
194
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700195 /**
196 * Create a HAL device stream of the requested size and format.
197 *
198 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
199 * selects an appropriate format; it can be queried with getFormat.
200 *
201 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
202 * be equal to the size in bytes of the buffers to allocate for the
203 * stream. For other formats, the size parameter is ignored.
204 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700205 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700206 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700207
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700208 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700209
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700210 status_t setTransform(int transform);
211
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700212 // Get stream parameters.
213 // Only valid after a successful connectToDevice call.
214 int getId() const { return mId; }
215 uint32_t getWidth() const { return mWidth; }
216 uint32_t getHeight() const { return mHeight; }
217 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700218
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700219 // Dump stream information
220 status_t dump(int fd, const Vector<String16>& args);
221
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700222 private:
223 enum {
224 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700225 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700226 ALLOCATED,
227 CONNECTED,
228 ACTIVE
229 } mState;
230
231 sp<ANativeWindow> mConsumerInterface;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800232 camera2_device_t *mHal2Device;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700233
234 uint32_t mId;
235 uint32_t mWidth;
236 uint32_t mHeight;
237 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700238 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700239 uint32_t mUsage;
240 uint32_t mMaxProducerBuffers;
241 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700242 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700243 int mFormatRequested;
244
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700245 /** Debugging information */
246 uint32_t mActiveBuffers;
247 uint32_t mFrameCount;
248 int64_t mLastTimestamp;
249
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700250 const camera2_stream_ops *getStreamOps();
251
252 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
253
254 static int dequeue_buffer(const camera2_stream_ops_t *w,
255 buffer_handle_t** buffer);
256
257 static int enqueue_buffer(const camera2_stream_ops_t* w,
258 int64_t timestamp,
259 buffer_handle_t* buffer);
260
261 static int cancel_buffer(const camera2_stream_ops_t* w,
262 buffer_handle_t* buffer);
263
264 static int set_crop(const camera2_stream_ops_t* w,
265 int left, int top, int right, int bottom);
266 }; // class StreamAdapter
267
268 typedef List<sp<StreamAdapter> > StreamList;
269 StreamList mStreams;
270
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700271 /**
272 * Adapter from an ANativeWindow interface to camera2 device stream ops.
273 * Also takes care of allocating/deallocating stream in device interface
274 */
275 class ReprocessStreamAdapter: public camera2_stream_in_ops, public virtual RefBase {
276 public:
277 ReprocessStreamAdapter(camera2_device_t *d);
278
279 ~ReprocessStreamAdapter();
280
281 /**
282 * Create a HAL device reprocess stream based on an existing output stream.
283 */
284 status_t connectToDevice(const sp<StreamAdapter> &outputStream);
285
286 status_t release();
287
288 /**
289 * Push buffer into stream for reprocessing. Takes ownership until it notifies
290 * that the buffer has been released
291 */
292 status_t pushIntoStream(buffer_handle_t *handle,
293 const wp<BufferReleasedListener> &releaseListener);
294
295 /**
296 * Get stream parameters.
297 * Only valid after a successful connectToDevice call.
298 */
299 int getId() const { return mId; }
300 uint32_t getWidth() const { return mWidth; }
301 uint32_t getHeight() const { return mHeight; }
302 uint32_t getFormat() const { return mFormat; }
303
304 // Dump stream information
305 status_t dump(int fd, const Vector<String16>& args);
306
307 private:
308 enum {
309 ERROR = -1,
310 RELEASED = 0,
311 ACTIVE
312 } mState;
313
314 sp<ANativeWindow> mConsumerInterface;
315 wp<StreamAdapter> mBaseStream;
316
317 struct QueueEntry {
318 buffer_handle_t *handle;
319 wp<BufferReleasedListener> releaseListener;
320 };
321
322 List<QueueEntry> mQueue;
323
324 List<QueueEntry> mInFlightQueue;
325
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800326 camera2_device_t *mHal2Device;
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700327
328 uint32_t mId;
329 uint32_t mWidth;
330 uint32_t mHeight;
331 uint32_t mFormat;
332
333 /** Debugging information */
334 uint32_t mActiveBuffers;
335 uint32_t mFrameCount;
336 int64_t mLastTimestamp;
337
338 const camera2_stream_in_ops *getStreamOps();
339
340 static int acquire_buffer(const camera2_stream_in_ops_t *w,
341 buffer_handle_t** buffer);
342
343 static int release_buffer(const camera2_stream_in_ops_t* w,
344 buffer_handle_t* buffer);
345
346 }; // class ReprocessStreamAdapter
347
348 typedef List<sp<ReprocessStreamAdapter> > ReprocessStreamList;
349 ReprocessStreamList mReprocessStreams;
350
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700351 // Receives HAL notifications and routes them to the NotificationListener
352 static void notificationCallback(int32_t msg_type,
353 int32_t ext1,
354 int32_t ext2,
355 int32_t ext3,
356 void *user);
357
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700358}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700359
360}; // namespace android
361
362#endif