blob: c846317048b9aaa747a61acee074c0eefd0a8d1e [file] [log] [blame]
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -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
Igor Murashkineab33fc2012-11-06 17:02:54 -080017#ifndef __ANDROID_HAL_CAMERA2_TESTS_UTILS__
18#define __ANDROID_HAL_CAMERA2_TESTS_UTILS__
19
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070020// Utility classes for camera2 HAL testing
21
22#include <system/camera_metadata.h>
23#include <hardware/camera2.h>
24
25#include <gui/SurfaceTextureClient.h>
26#include <gui/CpuConsumer.h>
27
28#include <utils/List.h>
29#include <utils/Mutex.h>
30#include <utils/Condition.h>
31
32namespace android {
Igor Murashkine302ee32012-11-05 11:14:49 -080033namespace camera2 {
34namespace tests {
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070035
36/**
37 * Queue class for both sending requests to a camera2 device, and for receiving
38 * frames from a camera2 device.
39 */
40class MetadataQueue: public camera2_request_queue_src_ops_t,
41 public camera2_frame_queue_dst_ops_t {
42 public:
43 MetadataQueue();
44 ~MetadataQueue();
45
46 // Interface to camera2 HAL device, either for requests (device is consumer)
47 // or for frames (device is producer)
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070048 const camera2_request_queue_src_ops_t* getToConsumerInterface();
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070049 void setFromConsumerInterface(camera2_device_t *d);
50
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070051 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070052
53 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
54 // On dequeue, user takes ownership of buffer pointer.
55 status_t enqueue(camera_metadata_t *buf);
56 status_t dequeue(camera_metadata_t **buf, bool incrementCount = true);
57 int getBufferCount();
58 status_t waitForBuffer(nsecs_t timeout);
59
60 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
61 // queue copies the contents of the stream slot into the queue, and then
62 // dequeues the first new entry.
63 status_t setStreamSlot(camera_metadata_t *buf);
64 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
65
66 private:
67 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
68 List<camera_metadata_t*>::iterator end);
69
70 camera2_device_t *mDevice;
71
72 Mutex mMutex;
73 Condition notEmpty;
74
75 int mFrameCount;
76
77 int mCount;
78 List<camera_metadata_t*> mEntries;
79 int mStreamSlotCount;
80 List<camera_metadata_t*> mStreamSlot;
81
82 bool mSignalConsumer;
83
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070084 static MetadataQueue* getInstance(const camera2_frame_queue_dst_ops_t *q);
85 static MetadataQueue* getInstance(const camera2_request_queue_src_ops_t *q);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070086
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070087 static int consumer_buffer_count(const camera2_request_queue_src_ops_t *q);
88
89 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070090 camera_metadata_t **buffer);
91
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070092 static int consumer_free(const camera2_request_queue_src_ops_t *q,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070093 camera_metadata_t *old_buffer);
94
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070095 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070096 size_t entries, size_t bytes,
97 camera_metadata_t **buffer);
98
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070099 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700100 camera_metadata_t *old_buffer);
101
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700102 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700103 camera_metadata_t *filled_buffer);
104
105};
106
107/**
108 * Basic class to receive and queue up notifications from the camera device
109 */
110
111class NotifierListener {
112 public:
113
114 NotifierListener();
115
116 status_t getNotificationsFrom(camera2_device *dev);
117
118 status_t getNextNotification(int32_t *msg_type, int32_t *ext1,
119 int32_t *ext2, int32_t *ext3);
120
121 status_t waitForNotification(int32_t *msg_type, int32_t *ext1,
122 int32_t *ext2, int32_t *ext3);
123
124 int numNotifications();
125
126 private:
127
128 status_t getNextNotificationLocked(int32_t *msg_type,
129 int32_t *ext1, int32_t *ext2, int32_t *ext3);
130
131 struct Notification {
132 Notification(int32_t type, int32_t e1, int32_t e2, int32_t e3):
133 msg_type(type),
134 ext1(e1),
135 ext2(e2),
136 ext3(e3)
137 {}
138
139 int32_t msg_type;
140 int32_t ext1;
141 int32_t ext2;
142 int32_t ext3;
143 };
144
145 List<Notification> mNotifications;
146
147 Mutex mMutex;
148 Condition mNewNotification;
149
150 void onNotify(int32_t msg_type,
151 int32_t ext1,
152 int32_t ext2,
153 int32_t ext3);
154
155 static void notify_callback_dispatch(int32_t msg_type,
156 int32_t ext1,
157 int32_t ext2,
158 int32_t ext3,
159 void *user);
160
161};
162
163/**
Andy McFaddeneda79df2012-12-18 09:50:24 -0800164 * Adapter from an IGraphicBufferProducer interface to camera2 device stream ops.
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700165 * Also takes care of allocating/deallocating stream in device interface
166 */
167class StreamAdapter: public camera2_stream_ops {
168 public:
Andy McFaddeneda79df2012-12-18 09:50:24 -0800169 StreamAdapter(sp<IGraphicBufferProducer> consumer);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700170
171 ~StreamAdapter();
172
173 status_t connectToDevice(camera2_device_t *d,
174 uint32_t width, uint32_t height, int format);
175
176 status_t disconnect();
177
178 // Get stream ID. Only valid after a successful connectToDevice call.
179 int getId();
180
181 private:
182 enum {
183 ERROR = -1,
184 DISCONNECTED = 0,
185 UNINITIALIZED,
186 ALLOCATED,
187 CONNECTED,
188 ACTIVE
189 } mState;
190
191 sp<ANativeWindow> mConsumerInterface;
192 camera2_device_t *mDevice;
193
194 uint32_t mId;
195 uint32_t mWidth;
196 uint32_t mHeight;
197 uint32_t mFormat;
198 uint32_t mUsage;
199 uint32_t mMaxProducerBuffers;
200 uint32_t mMaxConsumerBuffers;
201
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700202 const camera2_stream_ops *getStreamOps();
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700203
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700204 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700205
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700206 static int dequeue_buffer(const camera2_stream_ops_t *w,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700207 buffer_handle_t** buffer);
208
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700209 static int enqueue_buffer(const camera2_stream_ops_t* w,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700210 int64_t timestamp,
211 buffer_handle_t* buffer);
212
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700213 static int cancel_buffer(const camera2_stream_ops_t* w,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700214 buffer_handle_t* buffer);
215
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700216 static int set_crop(const camera2_stream_ops_t* w,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700217 int left, int top, int right, int bottom);
218
219};
220
221/**
222 * Simple class to wait on the CpuConsumer to have a frame available
223 */
224class FrameWaiter : public CpuConsumer::FrameAvailableListener {
225 public:
226 FrameWaiter();
227
228 /**
229 * Wait for max timeout nanoseconds for a new frame. Returns
230 * OK if a frame is available, TIMED_OUT if the timeout was reached.
231 */
232 status_t waitForFrame(nsecs_t timeout);
233
234 virtual void onFrameAvailable();
235
236 int mPendingFrames;
237 Mutex mMutex;
238 Condition mCondition;
239};
240
Igor Murashkine302ee32012-11-05 11:14:49 -0800241struct HWModuleHelpers {
242 /* attempt to unload the library with dlclose */
243 static int closeModule(hw_module_t* module);
244};
245
246}
247}
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700248}
Igor Murashkineab33fc2012-11-06 17:02:54 -0800249
250#endif