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