blob: a9affa2d18b8146c8738a2f577e5aa3d7e517770 [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
Zhijun He1fa89992015-06-01 15:44:31 -070071 virtual status_t configureStreams(bool isConstrainedHighSpeed = false);
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);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070087 // Prepare is a no-op
88 virtual status_t prepare(int streamId);
89
Zhijun He204e3292014-07-14 17:09:23 -070090 virtual uint32_t getDeviceVersion();
Zhijun He28c9b6f2014-08-08 12:00:47 -070091 virtual ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const;
Zhijun He204e3292014-07-14 17:09:23 -070092
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070093 private:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070094 const int mId;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080095 camera2_device_t *mHal2Device;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070096
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070097 CameraMetadata mDeviceInfo;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070098
Zhijun He204e3292014-07-14 17:09:23 -070099 uint32_t mDeviceVersion;
100
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700101 /**
102 * Queue class for both sending requests to a camera2 device, and for
103 * receiving frames from a camera2 device.
104 */
105 class MetadataQueue: public camera2_request_queue_src_ops_t,
106 public camera2_frame_queue_dst_ops_t {
107 public:
108 MetadataQueue();
109 ~MetadataQueue();
110
111 // Interface to camera2 HAL device, either for requests (device is
112 // consumer) or for frames (device is producer)
113 const camera2_request_queue_src_ops_t* getToConsumerInterface();
114 void setFromConsumerInterface(camera2_device_t *d);
115
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700116 // Connect queue consumer endpoint to a camera2 device
117 status_t setConsumerDevice(camera2_device_t *d);
118 // Connect queue producer endpoint to a camera2 device
119 status_t setProducerDevice(camera2_device_t *d);
120
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700121 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
122
123 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
124 // On dequeue, user takes ownership of buffer pointer.
125 status_t enqueue(camera_metadata_t *buf);
Eino-Ville Talvalafbd60662012-10-16 10:28:07 -0700126 status_t dequeue(camera_metadata_t **buf, bool incrementCount = false);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700127 int getBufferCount();
128 status_t waitForBuffer(nsecs_t timeout);
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700129 // Wait until a buffer with the given ID is dequeued. Will return
130 // immediately if the latest buffer dequeued has that ID.
131 status_t waitForDequeue(int32_t id, nsecs_t timeout);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700132
133 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
134 // queue copies the contents of the stream slot into the queue, and then
Chien-Yu Chen80de5dc2014-11-07 17:45:00 -0800135 // dequeues the first new entry. The methods take the ownership of the
136 // metadata buffers passed in.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700137 status_t setStreamSlot(camera_metadata_t *buf);
138 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
139
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700140 // Clear the request queue and the streaming slot
141 status_t clear();
142
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700143 status_t dump(int fd, const Vector<String16>& args);
144
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700145 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700146 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700147 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
148 List<camera_metadata_t*>::iterator end);
149
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800150 camera2_device_t *mHal2Device;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700151
152 Mutex mMutex;
153 Condition notEmpty;
154
155 int mFrameCount;
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700156 int32_t mLatestRequestId;
157 Condition mNewRequestId;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700158
159 int mCount;
160 List<camera_metadata_t*> mEntries;
161 int mStreamSlotCount;
162 List<camera_metadata_t*> mStreamSlot;
163
164 bool mSignalConsumer;
165
166 static MetadataQueue* getInstance(
167 const camera2_frame_queue_dst_ops_t *q);
168 static MetadataQueue* getInstance(
169 const camera2_request_queue_src_ops_t *q);
170
171 static int consumer_buffer_count(
172 const camera2_request_queue_src_ops_t *q);
173
174 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
175 camera_metadata_t **buffer);
176
177 static int consumer_free(const camera2_request_queue_src_ops_t *q,
178 camera_metadata_t *old_buffer);
179
180 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
181 size_t entries, size_t bytes,
182 camera_metadata_t **buffer);
183
184 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
185 camera_metadata_t *old_buffer);
186
187 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
188 camera_metadata_t *filled_buffer);
189
190 }; // class MetadataQueue
191
192 MetadataQueue mRequestQueue;
193 MetadataQueue mFrameQueue;
194
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700195 /**
196 * Adapter from an ANativeWindow interface to camera2 device stream ops.
197 * Also takes care of allocating/deallocating stream in device interface
198 */
199 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
200 public:
201 StreamAdapter(camera2_device_t *d);
202
203 ~StreamAdapter();
204
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700205 /**
206 * Create a HAL device stream of the requested size and format.
207 *
208 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
209 * selects an appropriate format; it can be queried with getFormat.
210 *
211 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
212 * be equal to the size in bytes of the buffers to allocate for the
213 * stream. For other formats, the size parameter is ignored.
214 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700215 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700216 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700217
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700218 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700219
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700220 status_t setTransform(int transform);
221
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700222 // Get stream parameters.
223 // Only valid after a successful connectToDevice call.
224 int getId() const { return mId; }
225 uint32_t getWidth() const { return mWidth; }
226 uint32_t getHeight() const { return mHeight; }
227 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700228
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700229 // Dump stream information
230 status_t dump(int fd, const Vector<String16>& args);
231
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700232 private:
233 enum {
234 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700235 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700236 ALLOCATED,
237 CONNECTED,
238 ACTIVE
239 } mState;
240
241 sp<ANativeWindow> mConsumerInterface;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800242 camera2_device_t *mHal2Device;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700243
244 uint32_t mId;
245 uint32_t mWidth;
246 uint32_t mHeight;
247 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700248 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700249 uint32_t mUsage;
250 uint32_t mMaxProducerBuffers;
251 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700252 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700253 int mFormatRequested;
254
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700255 /** Debugging information */
256 uint32_t mActiveBuffers;
257 uint32_t mFrameCount;
258 int64_t mLastTimestamp;
259
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700260 const camera2_stream_ops *getStreamOps();
261
262 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
263
264 static int dequeue_buffer(const camera2_stream_ops_t *w,
265 buffer_handle_t** buffer);
266
267 static int enqueue_buffer(const camera2_stream_ops_t* w,
268 int64_t timestamp,
269 buffer_handle_t* buffer);
270
271 static int cancel_buffer(const camera2_stream_ops_t* w,
272 buffer_handle_t* buffer);
273
274 static int set_crop(const camera2_stream_ops_t* w,
275 int left, int top, int right, int bottom);
276 }; // class StreamAdapter
277
278 typedef List<sp<StreamAdapter> > StreamList;
279 StreamList mStreams;
280
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700281 /**
282 * Adapter from an ANativeWindow interface to camera2 device stream ops.
283 * Also takes care of allocating/deallocating stream in device interface
284 */
285 class ReprocessStreamAdapter: public camera2_stream_in_ops, public virtual RefBase {
286 public:
287 ReprocessStreamAdapter(camera2_device_t *d);
288
289 ~ReprocessStreamAdapter();
290
291 /**
292 * Create a HAL device reprocess stream based on an existing output stream.
293 */
294 status_t connectToDevice(const sp<StreamAdapter> &outputStream);
295
296 status_t release();
297
298 /**
299 * Push buffer into stream for reprocessing. Takes ownership until it notifies
300 * that the buffer has been released
301 */
302 status_t pushIntoStream(buffer_handle_t *handle,
303 const wp<BufferReleasedListener> &releaseListener);
304
305 /**
306 * Get stream parameters.
307 * Only valid after a successful connectToDevice call.
308 */
309 int getId() const { return mId; }
310 uint32_t getWidth() const { return mWidth; }
311 uint32_t getHeight() const { return mHeight; }
312 uint32_t getFormat() const { return mFormat; }
313
314 // Dump stream information
315 status_t dump(int fd, const Vector<String16>& args);
316
317 private:
318 enum {
319 ERROR = -1,
320 RELEASED = 0,
321 ACTIVE
322 } mState;
323
324 sp<ANativeWindow> mConsumerInterface;
325 wp<StreamAdapter> mBaseStream;
326
327 struct QueueEntry {
328 buffer_handle_t *handle;
329 wp<BufferReleasedListener> releaseListener;
330 };
331
332 List<QueueEntry> mQueue;
333
334 List<QueueEntry> mInFlightQueue;
335
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800336 camera2_device_t *mHal2Device;
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700337
338 uint32_t mId;
339 uint32_t mWidth;
340 uint32_t mHeight;
341 uint32_t mFormat;
342
343 /** Debugging information */
344 uint32_t mActiveBuffers;
345 uint32_t mFrameCount;
346 int64_t mLastTimestamp;
347
348 const camera2_stream_in_ops *getStreamOps();
349
350 static int acquire_buffer(const camera2_stream_in_ops_t *w,
351 buffer_handle_t** buffer);
352
353 static int release_buffer(const camera2_stream_in_ops_t* w,
354 buffer_handle_t* buffer);
355
356 }; // class ReprocessStreamAdapter
357
358 typedef List<sp<ReprocessStreamAdapter> > ReprocessStreamList;
359 ReprocessStreamList mReprocessStreams;
360
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700361 // Receives HAL notifications and routes them to the NotificationListener
362 static void notificationCallback(int32_t msg_type,
363 int32_t ext1,
364 int32_t ext2,
365 int32_t ext3,
366 void *user);
367
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700368}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700369
370}; // namespace android
371
372#endif