blob: 9b32fa6511ebd4f5204a5d33bdc0de3bb6e2d66a [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;
Yin-Chia Yehe074a932015-01-30 10:29:02 -080046 virtual status_t initialize(CameraModule *module);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080047 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,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080060 uint32_t width, uint32_t height, int format,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070061 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080062 virtual status_t createReprocessStreamFromStream(int outputId, int *id);
63 virtual status_t getStreamInfo(int id,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070064 uint32_t *width, uint32_t *height, uint32_t *format);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080065 virtual status_t setStreamTransform(int id, int transform);
66 virtual status_t deleteStream(int id);
67 virtual status_t deleteReprocessStream(int id);
Igor Murashkine2d167e2014-08-19 16:19:59 -070068 // No-op on HAL2 devices
69 virtual status_t configureStreams();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080070 virtual status_t createDefaultRequest(int templateId, CameraMetadata *request);
71 virtual status_t waitUntilDrained();
72 virtual status_t setNotifyCallback(NotificationListener *listener);
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -070073 virtual bool willNotify3A();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080074 virtual status_t waitForNextFrame(nsecs_t timeout);
Jianing Weicb0652e2014-03-12 18:29:36 -070075 virtual status_t getNextResult(CaptureResult *frame);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080076 virtual status_t triggerAutofocus(uint32_t id);
77 virtual status_t triggerCancelAutofocus(uint32_t id);
78 virtual status_t triggerPrecaptureMetering(uint32_t id);
79 virtual status_t pushReprocessBuffer(int reprocessStreamId,
Eino-Ville Talvala69230df2012-08-29 17:37:16 -070080 buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -070081 // Flush implemented as just a wait
Jianing Weicb0652e2014-03-12 18:29:36 -070082 virtual status_t flush(int64_t *lastFrameNumber = NULL);
Zhijun He204e3292014-07-14 17:09:23 -070083 virtual uint32_t getDeviceVersion();
Zhijun He28c9b6f2014-08-08 12:00:47 -070084 virtual ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const;
Zhijun He204e3292014-07-14 17:09:23 -070085
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070086 private:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070087 const int mId;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080088 camera2_device_t *mHal2Device;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070089
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070090 CameraMetadata mDeviceInfo;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070091
Zhijun He204e3292014-07-14 17:09:23 -070092 uint32_t mDeviceVersion;
93
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070094 /**
95 * Queue class for both sending requests to a camera2 device, and for
96 * receiving frames from a camera2 device.
97 */
98 class MetadataQueue: public camera2_request_queue_src_ops_t,
99 public camera2_frame_queue_dst_ops_t {
100 public:
101 MetadataQueue();
102 ~MetadataQueue();
103
104 // Interface to camera2 HAL device, either for requests (device is
105 // consumer) or for frames (device is producer)
106 const camera2_request_queue_src_ops_t* getToConsumerInterface();
107 void setFromConsumerInterface(camera2_device_t *d);
108
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700109 // Connect queue consumer endpoint to a camera2 device
110 status_t setConsumerDevice(camera2_device_t *d);
111 // Connect queue producer endpoint to a camera2 device
112 status_t setProducerDevice(camera2_device_t *d);
113
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700114 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
115
116 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
117 // On dequeue, user takes ownership of buffer pointer.
118 status_t enqueue(camera_metadata_t *buf);
Eino-Ville Talvalafbd60662012-10-16 10:28:07 -0700119 status_t dequeue(camera_metadata_t **buf, bool incrementCount = false);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700120 int getBufferCount();
121 status_t waitForBuffer(nsecs_t timeout);
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700122 // Wait until a buffer with the given ID is dequeued. Will return
123 // immediately if the latest buffer dequeued has that ID.
124 status_t waitForDequeue(int32_t id, nsecs_t timeout);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700125
126 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
127 // queue copies the contents of the stream slot into the queue, and then
Chien-Yu Chen80de5dc2014-11-07 17:45:00 -0800128 // dequeues the first new entry. The methods take the ownership of the
129 // metadata buffers passed in.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700130 status_t setStreamSlot(camera_metadata_t *buf);
131 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
132
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700133 // Clear the request queue and the streaming slot
134 status_t clear();
135
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700136 status_t dump(int fd, const Vector<String16>& args);
137
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700138 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700139 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700140 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
141 List<camera_metadata_t*>::iterator end);
142
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800143 camera2_device_t *mHal2Device;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700144
145 Mutex mMutex;
146 Condition notEmpty;
147
148 int mFrameCount;
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700149 int32_t mLatestRequestId;
150 Condition mNewRequestId;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700151
152 int mCount;
153 List<camera_metadata_t*> mEntries;
154 int mStreamSlotCount;
155 List<camera_metadata_t*> mStreamSlot;
156
157 bool mSignalConsumer;
158
159 static MetadataQueue* getInstance(
160 const camera2_frame_queue_dst_ops_t *q);
161 static MetadataQueue* getInstance(
162 const camera2_request_queue_src_ops_t *q);
163
164 static int consumer_buffer_count(
165 const camera2_request_queue_src_ops_t *q);
166
167 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
168 camera_metadata_t **buffer);
169
170 static int consumer_free(const camera2_request_queue_src_ops_t *q,
171 camera_metadata_t *old_buffer);
172
173 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
174 size_t entries, size_t bytes,
175 camera_metadata_t **buffer);
176
177 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
178 camera_metadata_t *old_buffer);
179
180 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
181 camera_metadata_t *filled_buffer);
182
183 }; // class MetadataQueue
184
185 MetadataQueue mRequestQueue;
186 MetadataQueue mFrameQueue;
187
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700188 /**
189 * Adapter from an ANativeWindow interface to camera2 device stream ops.
190 * Also takes care of allocating/deallocating stream in device interface
191 */
192 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
193 public:
194 StreamAdapter(camera2_device_t *d);
195
196 ~StreamAdapter();
197
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700198 /**
199 * Create a HAL device stream of the requested size and format.
200 *
201 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
202 * selects an appropriate format; it can be queried with getFormat.
203 *
204 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
205 * be equal to the size in bytes of the buffers to allocate for the
206 * stream. For other formats, the size parameter is ignored.
207 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700208 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700209 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700210
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700211 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700212
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700213 status_t setTransform(int transform);
214
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700215 // Get stream parameters.
216 // Only valid after a successful connectToDevice call.
217 int getId() const { return mId; }
218 uint32_t getWidth() const { return mWidth; }
219 uint32_t getHeight() const { return mHeight; }
220 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700221
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700222 // Dump stream information
223 status_t dump(int fd, const Vector<String16>& args);
224
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700225 private:
226 enum {
227 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700228 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700229 ALLOCATED,
230 CONNECTED,
231 ACTIVE
232 } mState;
233
234 sp<ANativeWindow> mConsumerInterface;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800235 camera2_device_t *mHal2Device;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700236
237 uint32_t mId;
238 uint32_t mWidth;
239 uint32_t mHeight;
240 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700241 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700242 uint32_t mUsage;
243 uint32_t mMaxProducerBuffers;
244 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700245 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700246 int mFormatRequested;
247
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700248 /** Debugging information */
249 uint32_t mActiveBuffers;
250 uint32_t mFrameCount;
251 int64_t mLastTimestamp;
252
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700253 const camera2_stream_ops *getStreamOps();
254
255 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
256
257 static int dequeue_buffer(const camera2_stream_ops_t *w,
258 buffer_handle_t** buffer);
259
260 static int enqueue_buffer(const camera2_stream_ops_t* w,
261 int64_t timestamp,
262 buffer_handle_t* buffer);
263
264 static int cancel_buffer(const camera2_stream_ops_t* w,
265 buffer_handle_t* buffer);
266
267 static int set_crop(const camera2_stream_ops_t* w,
268 int left, int top, int right, int bottom);
269 }; // class StreamAdapter
270
271 typedef List<sp<StreamAdapter> > StreamList;
272 StreamList mStreams;
273
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700274 /**
275 * Adapter from an ANativeWindow interface to camera2 device stream ops.
276 * Also takes care of allocating/deallocating stream in device interface
277 */
278 class ReprocessStreamAdapter: public camera2_stream_in_ops, public virtual RefBase {
279 public:
280 ReprocessStreamAdapter(camera2_device_t *d);
281
282 ~ReprocessStreamAdapter();
283
284 /**
285 * Create a HAL device reprocess stream based on an existing output stream.
286 */
287 status_t connectToDevice(const sp<StreamAdapter> &outputStream);
288
289 status_t release();
290
291 /**
292 * Push buffer into stream for reprocessing. Takes ownership until it notifies
293 * that the buffer has been released
294 */
295 status_t pushIntoStream(buffer_handle_t *handle,
296 const wp<BufferReleasedListener> &releaseListener);
297
298 /**
299 * Get stream parameters.
300 * Only valid after a successful connectToDevice call.
301 */
302 int getId() const { return mId; }
303 uint32_t getWidth() const { return mWidth; }
304 uint32_t getHeight() const { return mHeight; }
305 uint32_t getFormat() const { return mFormat; }
306
307 // Dump stream information
308 status_t dump(int fd, const Vector<String16>& args);
309
310 private:
311 enum {
312 ERROR = -1,
313 RELEASED = 0,
314 ACTIVE
315 } mState;
316
317 sp<ANativeWindow> mConsumerInterface;
318 wp<StreamAdapter> mBaseStream;
319
320 struct QueueEntry {
321 buffer_handle_t *handle;
322 wp<BufferReleasedListener> releaseListener;
323 };
324
325 List<QueueEntry> mQueue;
326
327 List<QueueEntry> mInFlightQueue;
328
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800329 camera2_device_t *mHal2Device;
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700330
331 uint32_t mId;
332 uint32_t mWidth;
333 uint32_t mHeight;
334 uint32_t mFormat;
335
336 /** Debugging information */
337 uint32_t mActiveBuffers;
338 uint32_t mFrameCount;
339 int64_t mLastTimestamp;
340
341 const camera2_stream_in_ops *getStreamOps();
342
343 static int acquire_buffer(const camera2_stream_in_ops_t *w,
344 buffer_handle_t** buffer);
345
346 static int release_buffer(const camera2_stream_in_ops_t* w,
347 buffer_handle_t* buffer);
348
349 }; // class ReprocessStreamAdapter
350
351 typedef List<sp<ReprocessStreamAdapter> > ReprocessStreamList;
352 ReprocessStreamList mReprocessStreams;
353
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700354 // Receives HAL notifications and routes them to the NotificationListener
355 static void notificationCallback(int32_t msg_type,
356 int32_t ext1,
357 int32_t ext2,
358 int32_t ext3,
359 void *user);
360
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700361}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700362
363}; // namespace android
364
365#endif