blob: 9972606d660e3820b9a2e4ccc6168dcdcc2bd482 [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);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070062 virtual status_t createInputStream(
63 uint32_t width, uint32_t height, int format, int *id);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080064 virtual status_t createReprocessStreamFromStream(int outputId, int *id);
65 virtual status_t getStreamInfo(int id,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070066 uint32_t *width, uint32_t *height, uint32_t *format);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080067 virtual status_t setStreamTransform(int id, int transform);
68 virtual status_t deleteStream(int id);
69 virtual status_t deleteReprocessStream(int id);
Igor Murashkine2d167e2014-08-19 16:19:59 -070070 // No-op on HAL2 devices
71 virtual status_t configureStreams();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070072 virtual status_t getInputBufferProducer(
73 sp<IGraphicBufferProducer> *producer);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080074 virtual status_t createDefaultRequest(int templateId, CameraMetadata *request);
75 virtual status_t waitUntilDrained();
76 virtual status_t setNotifyCallback(NotificationListener *listener);
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -070077 virtual bool willNotify3A();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080078 virtual status_t waitForNextFrame(nsecs_t timeout);
Jianing Weicb0652e2014-03-12 18:29:36 -070079 virtual status_t getNextResult(CaptureResult *frame);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080080 virtual status_t triggerAutofocus(uint32_t id);
81 virtual status_t triggerCancelAutofocus(uint32_t id);
82 virtual status_t triggerPrecaptureMetering(uint32_t id);
83 virtual status_t pushReprocessBuffer(int reprocessStreamId,
Eino-Ville Talvala69230df2012-08-29 17:37:16 -070084 buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -070085 // Flush implemented as just a wait
Jianing Weicb0652e2014-03-12 18:29:36 -070086 virtual status_t flush(int64_t *lastFrameNumber = NULL);
Zhijun He204e3292014-07-14 17:09:23 -070087 virtual uint32_t getDeviceVersion();
Zhijun He28c9b6f2014-08-08 12:00:47 -070088 virtual ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const;
Zhijun He204e3292014-07-14 17:09:23 -070089
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070090 private:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070091 const int mId;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080092 camera2_device_t *mHal2Device;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070093
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070094 CameraMetadata mDeviceInfo;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070095
Zhijun He204e3292014-07-14 17:09:23 -070096 uint32_t mDeviceVersion;
97
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070098 /**
99 * Queue class for both sending requests to a camera2 device, and for
100 * receiving frames from a camera2 device.
101 */
102 class MetadataQueue: public camera2_request_queue_src_ops_t,
103 public camera2_frame_queue_dst_ops_t {
104 public:
105 MetadataQueue();
106 ~MetadataQueue();
107
108 // Interface to camera2 HAL device, either for requests (device is
109 // consumer) or for frames (device is producer)
110 const camera2_request_queue_src_ops_t* getToConsumerInterface();
111 void setFromConsumerInterface(camera2_device_t *d);
112
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700113 // Connect queue consumer endpoint to a camera2 device
114 status_t setConsumerDevice(camera2_device_t *d);
115 // Connect queue producer endpoint to a camera2 device
116 status_t setProducerDevice(camera2_device_t *d);
117
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700118 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
119
120 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
121 // On dequeue, user takes ownership of buffer pointer.
122 status_t enqueue(camera_metadata_t *buf);
Eino-Ville Talvalafbd60662012-10-16 10:28:07 -0700123 status_t dequeue(camera_metadata_t **buf, bool incrementCount = false);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700124 int getBufferCount();
125 status_t waitForBuffer(nsecs_t timeout);
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700126 // Wait until a buffer with the given ID is dequeued. Will return
127 // immediately if the latest buffer dequeued has that ID.
128 status_t waitForDequeue(int32_t id, nsecs_t timeout);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700129
130 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
131 // queue copies the contents of the stream slot into the queue, and then
Chien-Yu Chen80de5dc2014-11-07 17:45:00 -0800132 // dequeues the first new entry. The methods take the ownership of the
133 // metadata buffers passed in.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700134 status_t setStreamSlot(camera_metadata_t *buf);
135 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
136
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700137 // Clear the request queue and the streaming slot
138 status_t clear();
139
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700140 status_t dump(int fd, const Vector<String16>& args);
141
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700142 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700143 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700144 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
145 List<camera_metadata_t*>::iterator end);
146
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800147 camera2_device_t *mHal2Device;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700148
149 Mutex mMutex;
150 Condition notEmpty;
151
152 int mFrameCount;
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700153 int32_t mLatestRequestId;
154 Condition mNewRequestId;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700155
156 int mCount;
157 List<camera_metadata_t*> mEntries;
158 int mStreamSlotCount;
159 List<camera_metadata_t*> mStreamSlot;
160
161 bool mSignalConsumer;
162
163 static MetadataQueue* getInstance(
164 const camera2_frame_queue_dst_ops_t *q);
165 static MetadataQueue* getInstance(
166 const camera2_request_queue_src_ops_t *q);
167
168 static int consumer_buffer_count(
169 const camera2_request_queue_src_ops_t *q);
170
171 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
172 camera_metadata_t **buffer);
173
174 static int consumer_free(const camera2_request_queue_src_ops_t *q,
175 camera_metadata_t *old_buffer);
176
177 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
178 size_t entries, size_t bytes,
179 camera_metadata_t **buffer);
180
181 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
182 camera_metadata_t *old_buffer);
183
184 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
185 camera_metadata_t *filled_buffer);
186
187 }; // class MetadataQueue
188
189 MetadataQueue mRequestQueue;
190 MetadataQueue mFrameQueue;
191
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700192 /**
193 * Adapter from an ANativeWindow interface to camera2 device stream ops.
194 * Also takes care of allocating/deallocating stream in device interface
195 */
196 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
197 public:
198 StreamAdapter(camera2_device_t *d);
199
200 ~StreamAdapter();
201
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700202 /**
203 * Create a HAL device stream of the requested size and format.
204 *
205 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
206 * selects an appropriate format; it can be queried with getFormat.
207 *
208 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
209 * be equal to the size in bytes of the buffers to allocate for the
210 * stream. For other formats, the size parameter is ignored.
211 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700212 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700213 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700214
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700215 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700216
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700217 status_t setTransform(int transform);
218
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700219 // Get stream parameters.
220 // Only valid after a successful connectToDevice call.
221 int getId() const { return mId; }
222 uint32_t getWidth() const { return mWidth; }
223 uint32_t getHeight() const { return mHeight; }
224 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700225
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700226 // Dump stream information
227 status_t dump(int fd, const Vector<String16>& args);
228
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700229 private:
230 enum {
231 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700232 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700233 ALLOCATED,
234 CONNECTED,
235 ACTIVE
236 } mState;
237
238 sp<ANativeWindow> mConsumerInterface;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800239 camera2_device_t *mHal2Device;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700240
241 uint32_t mId;
242 uint32_t mWidth;
243 uint32_t mHeight;
244 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700245 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700246 uint32_t mUsage;
247 uint32_t mMaxProducerBuffers;
248 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700249 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700250 int mFormatRequested;
251
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700252 /** Debugging information */
253 uint32_t mActiveBuffers;
254 uint32_t mFrameCount;
255 int64_t mLastTimestamp;
256
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700257 const camera2_stream_ops *getStreamOps();
258
259 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
260
261 static int dequeue_buffer(const camera2_stream_ops_t *w,
262 buffer_handle_t** buffer);
263
264 static int enqueue_buffer(const camera2_stream_ops_t* w,
265 int64_t timestamp,
266 buffer_handle_t* buffer);
267
268 static int cancel_buffer(const camera2_stream_ops_t* w,
269 buffer_handle_t* buffer);
270
271 static int set_crop(const camera2_stream_ops_t* w,
272 int left, int top, int right, int bottom);
273 }; // class StreamAdapter
274
275 typedef List<sp<StreamAdapter> > StreamList;
276 StreamList mStreams;
277
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700278 /**
279 * Adapter from an ANativeWindow interface to camera2 device stream ops.
280 * Also takes care of allocating/deallocating stream in device interface
281 */
282 class ReprocessStreamAdapter: public camera2_stream_in_ops, public virtual RefBase {
283 public:
284 ReprocessStreamAdapter(camera2_device_t *d);
285
286 ~ReprocessStreamAdapter();
287
288 /**
289 * Create a HAL device reprocess stream based on an existing output stream.
290 */
291 status_t connectToDevice(const sp<StreamAdapter> &outputStream);
292
293 status_t release();
294
295 /**
296 * Push buffer into stream for reprocessing. Takes ownership until it notifies
297 * that the buffer has been released
298 */
299 status_t pushIntoStream(buffer_handle_t *handle,
300 const wp<BufferReleasedListener> &releaseListener);
301
302 /**
303 * Get stream parameters.
304 * Only valid after a successful connectToDevice call.
305 */
306 int getId() const { return mId; }
307 uint32_t getWidth() const { return mWidth; }
308 uint32_t getHeight() const { return mHeight; }
309 uint32_t getFormat() const { return mFormat; }
310
311 // Dump stream information
312 status_t dump(int fd, const Vector<String16>& args);
313
314 private:
315 enum {
316 ERROR = -1,
317 RELEASED = 0,
318 ACTIVE
319 } mState;
320
321 sp<ANativeWindow> mConsumerInterface;
322 wp<StreamAdapter> mBaseStream;
323
324 struct QueueEntry {
325 buffer_handle_t *handle;
326 wp<BufferReleasedListener> releaseListener;
327 };
328
329 List<QueueEntry> mQueue;
330
331 List<QueueEntry> mInFlightQueue;
332
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800333 camera2_device_t *mHal2Device;
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700334
335 uint32_t mId;
336 uint32_t mWidth;
337 uint32_t mHeight;
338 uint32_t mFormat;
339
340 /** Debugging information */
341 uint32_t mActiveBuffers;
342 uint32_t mFrameCount;
343 int64_t mLastTimestamp;
344
345 const camera2_stream_in_ops *getStreamOps();
346
347 static int acquire_buffer(const camera2_stream_in_ops_t *w,
348 buffer_handle_t** buffer);
349
350 static int release_buffer(const camera2_stream_in_ops_t* w,
351 buffer_handle_t* buffer);
352
353 }; // class ReprocessStreamAdapter
354
355 typedef List<sp<ReprocessStreamAdapter> > ReprocessStreamList;
356 ReprocessStreamList mReprocessStreams;
357
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700358 // Receives HAL notifications and routes them to the NotificationListener
359 static void notificationCallback(int32_t msg_type,
360 int32_t ext1,
361 int32_t ext2,
362 int32_t ext3,
363 void *user);
364
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700365}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700366
367}; // namespace android
368
369#endif