blob: 91a3fbde81a59ddb50612fd61aa5bb0e15937c70 [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>
24#include <utils/RefBase.h>
25#include <utils/String8.h>
26#include <utils/String16.h>
27#include <utils/Vector.h>
28
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070029#include "hardware/camera2.h"
30
31namespace android {
32
33class Camera2Device : public virtual RefBase {
34 public:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070035 Camera2Device(int id);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070036
37 ~Camera2Device();
38
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070039 status_t initialize(camera_module_t *module);
40
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070041 status_t dump(int fd, const Vector<String16>& args);
42
43 /**
44 * Get a pointer to the device's static characteristics metadata buffer
45 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070046 camera_metadata_t* info();
47
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070048 /**
49 * Submit request for capture. The Camera2Device takes ownership of the
50 * passed-in buffer.
51 */
52 status_t capture(camera_metadata_t *request);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070053
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070054 /**
55 * Submit request for streaming. The Camera2Device makes a copy of the
56 * passed-in buffer and the caller retains ownership.
57 */
58 status_t setStreamingRequest(camera_metadata_t *request);
59
60 /**
61 * Create an output stream of the requested size and format.
62 *
63 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device selects
64 * an appropriate format; it can be queried with getStreamInfo.
65 *
66 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must be
67 * equal to the size in bytes of the buffers to allocate for the stream. For
68 * other formats, the size parameter is ignored.
69 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070070 status_t createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070071 uint32_t width, uint32_t height, int format, size_t size,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070072 int *id);
73
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070074 /**
75 * Get information about a given stream.
76 */
77 status_t getStreamInfo(int id,
78 uint32_t *width, uint32_t *height, uint32_t *format);
79
80 /**
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -070081 * Set stream gralloc buffer transform
82 */
83 status_t setStreamTransform(int id, int transform);
84
85 /**
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070086 * Delete stream. Must not be called if there are requests in flight which
87 * reference that stream.
88 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070089 status_t deleteStream(int id);
90
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070091 /**
92 * Create a metadata buffer with fields that the HAL device believes are
93 * best for the given use case
94 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070095 status_t createDefaultRequest(int templateId,
96 camera_metadata_t **request);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070097
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070098 /**
99 * Wait until all requests have been processed. Returns INVALID_OPERATION if
100 * the streaming slot is not empty, or TIMED_OUT if the requests haven't
101 * finished processing in 10 seconds.
102 */
103 status_t waitUntilDrained();
104
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700105 /**
106 * Abstract class for HAL notification listeners
107 */
108 class NotificationListener {
109 public:
110 // Refer to the Camera2 HAL definition for notification definitions
111 virtual void notifyError(int errorCode, int arg1, int arg2) = 0;
112 virtual void notifyShutter(int frameNumber, nsecs_t timestamp) = 0;
113 virtual void notifyAutoFocus(uint8_t newState, int triggerId) = 0;
114 virtual void notifyAutoExposure(uint8_t newState, int triggerId) = 0;
115 virtual void notifyAutoWhitebalance(uint8_t newState, int triggerId) = 0;
116 protected:
117 virtual ~NotificationListener();
118 };
119
120 /**
121 * Connect HAL notifications to a listener. Overwrites previous
122 * listener. Set to NULL to stop receiving notifications.
123 */
124 status_t setNotifyCallback(NotificationListener *listener);
125
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700126 private:
127
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700128 const int mId;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700129 camera2_device_t *mDevice;
130
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700131 camera_metadata_t *mDeviceInfo;
132 vendor_tag_query_ops_t *mVendorTagOps;
133
134 /**
135 * Queue class for both sending requests to a camera2 device, and for
136 * receiving frames from a camera2 device.
137 */
138 class MetadataQueue: public camera2_request_queue_src_ops_t,
139 public camera2_frame_queue_dst_ops_t {
140 public:
141 MetadataQueue();
142 ~MetadataQueue();
143
144 // Interface to camera2 HAL device, either for requests (device is
145 // consumer) or for frames (device is producer)
146 const camera2_request_queue_src_ops_t* getToConsumerInterface();
147 void setFromConsumerInterface(camera2_device_t *d);
148
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700149 // Connect queue consumer endpoint to a camera2 device
150 status_t setConsumerDevice(camera2_device_t *d);
151 // Connect queue producer endpoint to a camera2 device
152 status_t setProducerDevice(camera2_device_t *d);
153
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700154 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
155
156 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
157 // On dequeue, user takes ownership of buffer pointer.
158 status_t enqueue(camera_metadata_t *buf);
159 status_t dequeue(camera_metadata_t **buf, bool incrementCount = true);
160 int getBufferCount();
161 status_t waitForBuffer(nsecs_t timeout);
162
163 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
164 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700165 // dequeues the first new entry. The metadata buffers passed in are
166 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700167 status_t setStreamSlot(camera_metadata_t *buf);
168 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
169
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700170 status_t dump(int fd, const Vector<String16>& args);
171
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700172 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700173 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700174 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
175 List<camera_metadata_t*>::iterator end);
176
177 camera2_device_t *mDevice;
178
179 Mutex mMutex;
180 Condition notEmpty;
181
182 int mFrameCount;
183
184 int mCount;
185 List<camera_metadata_t*> mEntries;
186 int mStreamSlotCount;
187 List<camera_metadata_t*> mStreamSlot;
188
189 bool mSignalConsumer;
190
191 static MetadataQueue* getInstance(
192 const camera2_frame_queue_dst_ops_t *q);
193 static MetadataQueue* getInstance(
194 const camera2_request_queue_src_ops_t *q);
195
196 static int consumer_buffer_count(
197 const camera2_request_queue_src_ops_t *q);
198
199 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
200 camera_metadata_t **buffer);
201
202 static int consumer_free(const camera2_request_queue_src_ops_t *q,
203 camera_metadata_t *old_buffer);
204
205 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
206 size_t entries, size_t bytes,
207 camera_metadata_t **buffer);
208
209 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
210 camera_metadata_t *old_buffer);
211
212 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
213 camera_metadata_t *filled_buffer);
214
215 }; // class MetadataQueue
216
217 MetadataQueue mRequestQueue;
218 MetadataQueue mFrameQueue;
219
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700220 /**
221 * Adapter from an ANativeWindow interface to camera2 device stream ops.
222 * Also takes care of allocating/deallocating stream in device interface
223 */
224 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
225 public:
226 StreamAdapter(camera2_device_t *d);
227
228 ~StreamAdapter();
229
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700230 /**
231 * Create a HAL device stream of the requested size and format.
232 *
233 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
234 * selects an appropriate format; it can be queried with getFormat.
235 *
236 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
237 * be equal to the size in bytes of the buffers to allocate for the
238 * stream. For other formats, the size parameter is ignored.
239 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700240 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700241 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700242
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700243 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700244
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700245 status_t setTransform(int transform);
246
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700247 // Get stream parameters.
248 // Only valid after a successful connectToDevice call.
249 int getId() const { return mId; }
250 uint32_t getWidth() const { return mWidth; }
251 uint32_t getHeight() const { return mHeight; }
252 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700253
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700254 // Dump stream information
255 status_t dump(int fd, const Vector<String16>& args);
256
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700257 private:
258 enum {
259 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700260 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700261 ALLOCATED,
262 CONNECTED,
263 ACTIVE
264 } mState;
265
266 sp<ANativeWindow> mConsumerInterface;
267 camera2_device_t *mDevice;
268
269 uint32_t mId;
270 uint32_t mWidth;
271 uint32_t mHeight;
272 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700273 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700274 uint32_t mUsage;
275 uint32_t mMaxProducerBuffers;
276 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700277 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700278 int mFormatRequested;
279
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700280 /** Debugging information */
281 uint32_t mActiveBuffers;
282 uint32_t mFrameCount;
283 int64_t mLastTimestamp;
284
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700285 const camera2_stream_ops *getStreamOps();
286
287 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
288
289 static int dequeue_buffer(const camera2_stream_ops_t *w,
290 buffer_handle_t** buffer);
291
292 static int enqueue_buffer(const camera2_stream_ops_t* w,
293 int64_t timestamp,
294 buffer_handle_t* buffer);
295
296 static int cancel_buffer(const camera2_stream_ops_t* w,
297 buffer_handle_t* buffer);
298
299 static int set_crop(const camera2_stream_ops_t* w,
300 int left, int top, int right, int bottom);
301 }; // class StreamAdapter
302
303 typedef List<sp<StreamAdapter> > StreamList;
304 StreamList mStreams;
305
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700306 // Receives HAL notifications and routes them to the NotificationListener
307 static void notificationCallback(int32_t msg_type,
308 int32_t ext1,
309 int32_t ext2,
310 int32_t ext3,
311 void *user);
312
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700313}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700314
315}; // namespace android
316
317#endif