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