blob: 1116be03acfa6b519db501184ddba54b06c91c0b [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 Talvala61ab9f92012-05-17 10:30:54 -0700105 private:
106
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700107 const int mId;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700108 camera2_device_t *mDevice;
109
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700110 camera_metadata_t *mDeviceInfo;
111 vendor_tag_query_ops_t *mVendorTagOps;
112
113 /**
114 * Queue class for both sending requests to a camera2 device, and for
115 * receiving frames from a camera2 device.
116 */
117 class MetadataQueue: public camera2_request_queue_src_ops_t,
118 public camera2_frame_queue_dst_ops_t {
119 public:
120 MetadataQueue();
121 ~MetadataQueue();
122
123 // Interface to camera2 HAL device, either for requests (device is
124 // consumer) or for frames (device is producer)
125 const camera2_request_queue_src_ops_t* getToConsumerInterface();
126 void setFromConsumerInterface(camera2_device_t *d);
127
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700128 // Connect queue consumer endpoint to a camera2 device
129 status_t setConsumerDevice(camera2_device_t *d);
130 // Connect queue producer endpoint to a camera2 device
131 status_t setProducerDevice(camera2_device_t *d);
132
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700133 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
134
135 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
136 // On dequeue, user takes ownership of buffer pointer.
137 status_t enqueue(camera_metadata_t *buf);
138 status_t dequeue(camera_metadata_t **buf, bool incrementCount = true);
139 int getBufferCount();
140 status_t waitForBuffer(nsecs_t timeout);
141
142 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
143 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700144 // dequeues the first new entry. The metadata buffers passed in are
145 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700146 status_t setStreamSlot(camera_metadata_t *buf);
147 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
148
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700149 status_t dump(int fd, const Vector<String16>& args);
150
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700151 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700152 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700153 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
154 List<camera_metadata_t*>::iterator end);
155
156 camera2_device_t *mDevice;
157
158 Mutex mMutex;
159 Condition notEmpty;
160
161 int mFrameCount;
162
163 int mCount;
164 List<camera_metadata_t*> mEntries;
165 int mStreamSlotCount;
166 List<camera_metadata_t*> mStreamSlot;
167
168 bool mSignalConsumer;
169
170 static MetadataQueue* getInstance(
171 const camera2_frame_queue_dst_ops_t *q);
172 static MetadataQueue* getInstance(
173 const camera2_request_queue_src_ops_t *q);
174
175 static int consumer_buffer_count(
176 const camera2_request_queue_src_ops_t *q);
177
178 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
179 camera_metadata_t **buffer);
180
181 static int consumer_free(const camera2_request_queue_src_ops_t *q,
182 camera_metadata_t *old_buffer);
183
184 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
185 size_t entries, size_t bytes,
186 camera_metadata_t **buffer);
187
188 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
189 camera_metadata_t *old_buffer);
190
191 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
192 camera_metadata_t *filled_buffer);
193
194 }; // class MetadataQueue
195
196 MetadataQueue mRequestQueue;
197 MetadataQueue mFrameQueue;
198
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700199 /**
200 * Adapter from an ANativeWindow interface to camera2 device stream ops.
201 * Also takes care of allocating/deallocating stream in device interface
202 */
203 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
204 public:
205 StreamAdapter(camera2_device_t *d);
206
207 ~StreamAdapter();
208
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700209 /**
210 * Create a HAL device stream of the requested size and format.
211 *
212 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
213 * selects an appropriate format; it can be queried with getFormat.
214 *
215 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
216 * be equal to the size in bytes of the buffers to allocate for the
217 * stream. For other formats, the size parameter is ignored.
218 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700219 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700220 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700221
222 status_t disconnect();
223
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700224 status_t setTransform(int transform);
225
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700226 // Get stream parameters.
227 // Only valid after a successful connectToDevice call.
228 int getId() const { return mId; }
229 uint32_t getWidth() const { return mWidth; }
230 uint32_t getHeight() const { return mHeight; }
231 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700232
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700233 // Dump stream information
234 status_t dump(int fd, const Vector<String16>& args);
235
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700236 private:
237 enum {
238 ERROR = -1,
239 DISCONNECTED = 0,
240 ALLOCATED,
241 CONNECTED,
242 ACTIVE
243 } mState;
244
245 sp<ANativeWindow> mConsumerInterface;
246 camera2_device_t *mDevice;
247
248 uint32_t mId;
249 uint32_t mWidth;
250 uint32_t mHeight;
251 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700252 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700253 uint32_t mUsage;
254 uint32_t mMaxProducerBuffers;
255 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700256 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700257 int mFormatRequested;
258
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700259 /** Debugging information */
260 uint32_t mActiveBuffers;
261 uint32_t mFrameCount;
262 int64_t mLastTimestamp;
263
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700264 const camera2_stream_ops *getStreamOps();
265
266 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
267
268 static int dequeue_buffer(const camera2_stream_ops_t *w,
269 buffer_handle_t** buffer);
270
271 static int enqueue_buffer(const camera2_stream_ops_t* w,
272 int64_t timestamp,
273 buffer_handle_t* buffer);
274
275 static int cancel_buffer(const camera2_stream_ops_t* w,
276 buffer_handle_t* buffer);
277
278 static int set_crop(const camera2_stream_ops_t* w,
279 int left, int top, int right, int bottom);
280 }; // class StreamAdapter
281
282 typedef List<sp<StreamAdapter> > StreamList;
283 StreamList mStreams;
284
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700285}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700286
287}; // namespace android
288
289#endif