Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | #include <gtest/gtest.h> |
| 18 | #include <iostream> |
| 19 | |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 20 | #include <binder/IPCThreadState.h> |
| 21 | #include <utils/Thread.h> |
| 22 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 23 | #include "Camera.h" |
| 24 | #include "ProCamera.h" |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 25 | #include <utils/Vector.h> |
| 26 | #include <utils/Mutex.h> |
| 27 | #include <utils/Condition.h> |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 28 | |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 29 | #include <gui/SurfaceComposerClient.h> |
| 30 | #include <gui/Surface.h> |
| 31 | |
| 32 | #include <system/camera_metadata.h> |
| 33 | #include <hardware/camera2.h> // for CAMERA2_TEMPLATE_PREVIEW only |
Igor Murashkin | dcb07d5 | 2013-02-21 14:18:10 -0800 | [diff] [blame] | 34 | #include <camera/CameraMetadata.h> |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 35 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 36 | #include <camera/ICameraServiceListener.h> |
| 37 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 38 | namespace android { |
| 39 | namespace camera2 { |
| 40 | namespace tests { |
| 41 | namespace client { |
| 42 | |
| 43 | #define CAMERA_ID 0 |
| 44 | #define TEST_DEBUGGING 0 |
| 45 | |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 46 | #define TEST_LISTENER_TIMEOUT 1000000000 // 1 second listener timeout |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 47 | #define TEST_FORMAT HAL_PIXEL_FORMAT_Y16 //TODO: YUY2 instead |
| 48 | |
Igor Murashkin | eb72e17 | 2013-02-21 11:43:14 -0800 | [diff] [blame] | 49 | #define TEST_FORMAT_MAIN HAL_PIXEL_FORMAT_Y8 |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 50 | #define TEST_FORMAT_DEPTH HAL_PIXEL_FORMAT_Y16 |
| 51 | |
Igor Murashkin | dcb07d5 | 2013-02-21 14:18:10 -0800 | [diff] [blame] | 52 | // defaults for display "test" |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 53 | #define TEST_DISPLAY_FORMAT HAL_PIXEL_FORMAT_Y8 |
| 54 | #define TEST_DISPLAY_WIDTH 320 |
| 55 | #define TEST_DISPLAY_HEIGHT 240 |
Igor Murashkin | dcb07d5 | 2013-02-21 14:18:10 -0800 | [diff] [blame] | 56 | |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 57 | #define TEST_CPU_FRAME_COUNT 2 |
| 58 | #define TEST_CPU_HEAP_COUNT 5 |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 59 | |
Igor Murashkin | 4bc4a38 | 2013-02-20 13:36:17 -0800 | [diff] [blame] | 60 | #define TEST_FRAME_PROCESSING_DELAY_US 200000 // 200 ms |
| 61 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 62 | #if TEST_DEBUGGING |
| 63 | #define dout std::cerr |
| 64 | #else |
| 65 | #define dout if (0) std::cerr |
| 66 | #endif |
| 67 | |
Igor Murashkin | 39f79f7 | 2013-01-30 10:14:24 -0800 | [diff] [blame] | 68 | #define EXPECT_OK(x) EXPECT_EQ(OK, (x)) |
| 69 | #define ASSERT_OK(x) ASSERT_EQ(OK, (x)) |
| 70 | |
| 71 | class ProCameraTest; |
| 72 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 73 | struct ServiceListener : public BnCameraServiceListener { |
| 74 | |
| 75 | ServiceListener() : |
| 76 | mLatestStatus(STATUS_UNKNOWN), |
| 77 | mPrevStatus(STATUS_UNKNOWN) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | void onStatusChanged(Status status, int32_t cameraId) { |
| 82 | dout << "On status changed: 0x" << std::hex |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 83 | << (unsigned int) status << " cameraId " << cameraId |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 84 | << std::endl; |
| 85 | |
| 86 | Mutex::Autolock al(mMutex); |
| 87 | |
| 88 | mLatestStatus = status; |
| 89 | mCondition.broadcast(); |
| 90 | } |
| 91 | |
| 92 | status_t waitForStatusChange(Status& newStatus) { |
| 93 | Mutex::Autolock al(mMutex); |
| 94 | |
| 95 | if (mLatestStatus != mPrevStatus) { |
| 96 | newStatus = mLatestStatus; |
| 97 | mPrevStatus = mLatestStatus; |
| 98 | return OK; |
| 99 | } |
| 100 | |
| 101 | status_t stat = mCondition.waitRelative(mMutex, |
| 102 | TEST_LISTENER_TIMEOUT); |
| 103 | |
| 104 | if (stat == OK) { |
| 105 | newStatus = mLatestStatus; |
| 106 | mPrevStatus = mLatestStatus; |
| 107 | } |
| 108 | |
| 109 | return stat; |
| 110 | } |
| 111 | |
| 112 | Condition mCondition; |
| 113 | Mutex mMutex; |
| 114 | |
| 115 | Status mLatestStatus; |
| 116 | Status mPrevStatus; |
| 117 | }; |
| 118 | |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 119 | enum ProEvent { |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 120 | UNKNOWN, |
| 121 | ACQUIRED, |
| 122 | RELEASED, |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 123 | STOLEN, |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 124 | FRAME_RECEIVED, |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 125 | RESULT_RECEIVED, |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 128 | inline int ProEvent_Mask(ProEvent e) { |
| 129 | return (1 << static_cast<int>(e)); |
| 130 | } |
| 131 | |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 132 | typedef Vector<ProEvent> EventList; |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 133 | |
| 134 | class ProCameraTestThread : public Thread |
| 135 | { |
| 136 | public: |
| 137 | ProCameraTestThread() { |
| 138 | } |
| 139 | |
| 140 | virtual bool threadLoop() { |
| 141 | mProc = ProcessState::self(); |
| 142 | mProc->startThreadPool(); |
| 143 | |
| 144 | IPCThreadState *ptr = IPCThreadState::self(); |
| 145 | |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 146 | ptr->joinThreadPool(); |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 147 | |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | sp<ProcessState> mProc; |
| 152 | }; |
| 153 | |
| 154 | class ProCameraTestListener : public ProCameraListener { |
| 155 | |
| 156 | public: |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 157 | static const int EVENT_MASK_ALL = 0xFFFFFFFF; |
| 158 | |
| 159 | ProCameraTestListener() { |
| 160 | mEventMask = EVENT_MASK_ALL; |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 161 | mDropFrames = false; |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 164 | status_t WaitForEvent() { |
| 165 | Mutex::Autolock cal(mConditionMutex); |
| 166 | |
| 167 | { |
| 168 | Mutex::Autolock al(mListenerMutex); |
| 169 | |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 170 | if (mProEventList.size() > 0) { |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 171 | return OK; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return mListenerCondition.waitRelative(mConditionMutex, |
| 176 | TEST_LISTENER_TIMEOUT); |
| 177 | } |
| 178 | |
| 179 | /* Read events into out. Existing queue is flushed */ |
| 180 | void ReadEvents(EventList& out) { |
| 181 | Mutex::Autolock al(mListenerMutex); |
| 182 | |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 183 | for (size_t i = 0; i < mProEventList.size(); ++i) { |
| 184 | out.push(mProEventList[i]); |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 187 | mProEventList.clear(); |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Dequeue 1 event from the event queue. |
| 192 | * Returns UNKNOWN if queue is empty |
| 193 | */ |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 194 | ProEvent ReadEvent() { |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 195 | Mutex::Autolock al(mListenerMutex); |
| 196 | |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 197 | if (mProEventList.size() == 0) { |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 198 | return UNKNOWN; |
| 199 | } |
| 200 | |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 201 | ProEvent ev = mProEventList[0]; |
| 202 | mProEventList.removeAt(0); |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 203 | |
| 204 | return ev; |
| 205 | } |
| 206 | |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 207 | void SetEventMask(int eventMask) { |
| 208 | Mutex::Autolock al(mListenerMutex); |
| 209 | mEventMask = eventMask; |
| 210 | } |
| 211 | |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 212 | // Automatically acquire/release frames as they are available |
| 213 | void SetDropFrames(bool dropFrames) { |
| 214 | Mutex::Autolock al(mListenerMutex); |
| 215 | mDropFrames = dropFrames; |
| 216 | } |
| 217 | |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 218 | private: |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 219 | void QueueEvent(ProEvent ev) { |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 220 | bool eventAdded = false; |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 221 | { |
| 222 | Mutex::Autolock al(mListenerMutex); |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 223 | |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 224 | // Drop events not part of mask |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 225 | if (ProEvent_Mask(ev) & mEventMask) { |
| 226 | mProEventList.push(ev); |
| 227 | eventAdded = true; |
| 228 | } |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 231 | if (eventAdded) { |
| 232 | mListenerCondition.broadcast(); |
| 233 | } |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | protected: |
| 237 | |
| 238 | ////////////////////////////////////////////////// |
| 239 | ///////// ProCameraListener ////////////////////// |
| 240 | ////////////////////////////////////////////////// |
| 241 | |
| 242 | |
| 243 | // Lock has been acquired. Write operations now available. |
| 244 | virtual void onLockAcquired() { |
| 245 | QueueEvent(ACQUIRED); |
| 246 | } |
| 247 | // Lock has been released with exclusiveUnlock |
| 248 | virtual void onLockReleased() { |
| 249 | QueueEvent(RELEASED); |
| 250 | } |
| 251 | |
| 252 | // Lock has been stolen by another client. |
| 253 | virtual void onLockStolen() { |
| 254 | QueueEvent(STOLEN); |
| 255 | } |
| 256 | |
| 257 | // Lock free. |
| 258 | virtual void onTriggerNotify(int32_t ext1, int32_t ext2, int32_t ext3) { |
| 259 | |
| 260 | dout << "Trigger notify: " << ext1 << " " << ext2 |
| 261 | << " " << ext3 << std::endl; |
| 262 | } |
| 263 | |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 264 | virtual void onFrameAvailable(int streamId, |
| 265 | const sp<CpuConsumer>& consumer) { |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 266 | |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 267 | QueueEvent(FRAME_RECEIVED); |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 268 | |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 269 | Mutex::Autolock al(mListenerMutex); |
| 270 | if (mDropFrames) { |
| 271 | CpuConsumer::LockedBuffer buf; |
| 272 | status_t ret; |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 273 | |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 274 | EXPECT_OK(ret); |
| 275 | if (OK == (ret = consumer->lockNextBuffer(&buf))) { |
| 276 | |
| 277 | dout << "Frame received on streamId = " << streamId << |
| 278 | ", dataPtr = " << (void*)buf.data << |
| 279 | ", timestamp = " << buf.timestamp << std::endl; |
| 280 | |
| 281 | EXPECT_OK(consumer->unlockBuffer(buf)); |
| 282 | } |
| 283 | } else { |
| 284 | dout << "Frame received on streamId = " << streamId << std::endl; |
| 285 | } |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 286 | } |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 287 | |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 288 | virtual void onResultReceived(int32_t frameId, |
| 289 | camera_metadata* request) { |
| 290 | dout << "Result received frameId = " << frameId |
| 291 | << ", requestPtr = " << (void*)request << std::endl; |
| 292 | QueueEvent(RESULT_RECEIVED); |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 293 | free_camera_metadata(request); |
| 294 | } |
| 295 | |
Igor Murashkin | fa4cf9d | 2013-03-04 16:14:23 -0800 | [diff] [blame] | 296 | virtual void notify(int32_t msg, int32_t ext1, int32_t ext2) { |
| 297 | dout << "Notify received: msg " << std::hex << msg |
| 298 | << ", ext1: " << std::hex << ext1 << ", ext2: " << std::hex << ext2 |
| 299 | << std::endl; |
| 300 | } |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 301 | |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 302 | Vector<ProEvent> mProEventList; |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 303 | Mutex mListenerMutex; |
| 304 | Mutex mConditionMutex; |
| 305 | Condition mListenerCondition; |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 306 | int mEventMask; |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 307 | bool mDropFrames; |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 308 | }; |
| 309 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 310 | class ProCameraTest : public ::testing::Test { |
| 311 | |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 312 | public: |
| 313 | ProCameraTest() { |
Igor Murashkin | dcb07d5 | 2013-02-21 14:18:10 -0800 | [diff] [blame] | 314 | char* displaySecsEnv = getenv("TEST_DISPLAY_SECS"); |
| 315 | if (displaySecsEnv != NULL) { |
| 316 | mDisplaySecs = atoi(displaySecsEnv); |
| 317 | if (mDisplaySecs < 0) { |
| 318 | mDisplaySecs = 0; |
| 319 | } |
| 320 | } else { |
| 321 | mDisplaySecs = 0; |
| 322 | } |
| 323 | |
| 324 | char* displayFmtEnv = getenv("TEST_DISPLAY_FORMAT"); |
| 325 | if (displayFmtEnv != NULL) { |
| 326 | mDisplayFmt = FormatFromString(displayFmtEnv); |
| 327 | } else { |
| 328 | mDisplayFmt = TEST_DISPLAY_FORMAT; |
| 329 | } |
| 330 | |
| 331 | char* displayWidthEnv = getenv("TEST_DISPLAY_WIDTH"); |
| 332 | if (displayWidthEnv != NULL) { |
| 333 | mDisplayW = atoi(displayWidthEnv); |
| 334 | if (mDisplayW < 0) { |
| 335 | mDisplayW = 0; |
| 336 | } |
| 337 | } else { |
| 338 | mDisplayW = TEST_DISPLAY_WIDTH; |
| 339 | } |
| 340 | |
| 341 | char* displayHeightEnv = getenv("TEST_DISPLAY_HEIGHT"); |
| 342 | if (displayHeightEnv != NULL) { |
| 343 | mDisplayH = atoi(displayHeightEnv); |
| 344 | if (mDisplayH < 0) { |
| 345 | mDisplayH = 0; |
| 346 | } |
| 347 | } else { |
| 348 | mDisplayH = TEST_DISPLAY_HEIGHT; |
| 349 | } |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Igor Murashkin | 39f79f7 | 2013-01-30 10:14:24 -0800 | [diff] [blame] | 352 | static void SetUpTestCase() { |
| 353 | // Binder Thread Pool Initialization |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 354 | mTestThread = new ProCameraTestThread(); |
| 355 | mTestThread->run("ProCameraTestThread"); |
Igor Murashkin | 39f79f7 | 2013-01-30 10:14:24 -0800 | [diff] [blame] | 356 | } |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 357 | |
Igor Murashkin | 39f79f7 | 2013-01-30 10:14:24 -0800 | [diff] [blame] | 358 | virtual void SetUp() { |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 359 | mCamera = ProCamera::connect(CAMERA_ID); |
| 360 | ASSERT_NE((void*)NULL, mCamera.get()); |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 361 | |
| 362 | mListener = new ProCameraTestListener(); |
| 363 | mCamera->setListener(mListener); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | virtual void TearDown() { |
| 367 | ASSERT_NE((void*)NULL, mCamera.get()); |
| 368 | mCamera->disconnect(); |
| 369 | } |
| 370 | |
| 371 | protected: |
| 372 | sp<ProCamera> mCamera; |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 373 | sp<ProCameraTestListener> mListener; |
| 374 | |
Igor Murashkin | 39f79f7 | 2013-01-30 10:14:24 -0800 | [diff] [blame] | 375 | static sp<Thread> mTestThread; |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 376 | |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 377 | int mDisplaySecs; |
Igor Murashkin | dcb07d5 | 2013-02-21 14:18:10 -0800 | [diff] [blame] | 378 | int mDisplayFmt; |
| 379 | int mDisplayW; |
| 380 | int mDisplayH; |
| 381 | |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 382 | sp<SurfaceComposerClient> mComposerClient; |
| 383 | sp<SurfaceControl> mSurfaceControl; |
| 384 | |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 385 | sp<SurfaceComposerClient> mDepthComposerClient; |
| 386 | sp<SurfaceControl> mDepthSurfaceControl; |
| 387 | |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 388 | int getSurfaceWidth() { |
| 389 | return 512; |
| 390 | } |
| 391 | int getSurfaceHeight() { |
| 392 | return 512; |
| 393 | } |
| 394 | |
| 395 | void createOnScreenSurface(sp<Surface>& surface) { |
| 396 | mComposerClient = new SurfaceComposerClient; |
| 397 | ASSERT_EQ(NO_ERROR, mComposerClient->initCheck()); |
| 398 | |
| 399 | mSurfaceControl = mComposerClient->createSurface( |
| 400 | String8("ProCameraTest StreamingImage Surface"), |
| 401 | getSurfaceWidth(), getSurfaceHeight(), |
| 402 | PIXEL_FORMAT_RGB_888, 0); |
| 403 | |
Igor Murashkin | dcb07d5 | 2013-02-21 14:18:10 -0800 | [diff] [blame] | 404 | mSurfaceControl->setPosition(0, 0); |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 405 | |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 406 | ASSERT_TRUE(mSurfaceControl != NULL); |
| 407 | ASSERT_TRUE(mSurfaceControl->isValid()); |
| 408 | |
| 409 | SurfaceComposerClient::openGlobalTransaction(); |
| 410 | ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF)); |
| 411 | ASSERT_EQ(NO_ERROR, mSurfaceControl->show()); |
| 412 | SurfaceComposerClient::closeGlobalTransaction(); |
| 413 | |
| 414 | sp<ANativeWindow> window = mSurfaceControl->getSurface(); |
| 415 | surface = mSurfaceControl->getSurface(); |
| 416 | |
| 417 | ASSERT_NE((void*)NULL, surface.get()); |
| 418 | } |
| 419 | |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 420 | void createDepthOnScreenSurface(sp<Surface>& surface) { |
| 421 | mDepthComposerClient = new SurfaceComposerClient; |
| 422 | ASSERT_EQ(NO_ERROR, mDepthComposerClient->initCheck()); |
| 423 | |
| 424 | mDepthSurfaceControl = mDepthComposerClient->createSurface( |
| 425 | String8("ProCameraTest StreamingImage Surface"), |
| 426 | getSurfaceWidth(), getSurfaceHeight(), |
| 427 | PIXEL_FORMAT_RGB_888, 0); |
| 428 | |
| 429 | mDepthSurfaceControl->setPosition(640, 0); |
| 430 | |
| 431 | ASSERT_TRUE(mDepthSurfaceControl != NULL); |
| 432 | ASSERT_TRUE(mDepthSurfaceControl->isValid()); |
| 433 | |
| 434 | SurfaceComposerClient::openGlobalTransaction(); |
| 435 | ASSERT_EQ(NO_ERROR, mDepthSurfaceControl->setLayer(0x7FFFFFFF)); |
| 436 | ASSERT_EQ(NO_ERROR, mDepthSurfaceControl->show()); |
| 437 | SurfaceComposerClient::closeGlobalTransaction(); |
| 438 | |
| 439 | sp<ANativeWindow> window = mDepthSurfaceControl->getSurface(); |
| 440 | surface = mDepthSurfaceControl->getSurface(); |
| 441 | |
| 442 | ASSERT_NE((void*)NULL, surface.get()); |
| 443 | } |
| 444 | |
Igor Murashkin | 7b33a74 | 2013-02-21 13:49:26 -0800 | [diff] [blame] | 445 | template <typename T> |
Igor Murashkin | dcb07d5 | 2013-02-21 14:18:10 -0800 | [diff] [blame] | 446 | static bool ExistsItem(T needle, T* array, size_t count) { |
| 447 | if (!array) { |
| 448 | return false; |
| 449 | } |
| 450 | |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 451 | for (size_t i = 0; i < count; ++i) { |
Igor Murashkin | 7b33a74 | 2013-02-21 13:49:26 -0800 | [diff] [blame] | 452 | if (array[i] == needle) { |
| 453 | return true; |
| 454 | } |
| 455 | } |
| 456 | return false; |
| 457 | } |
| 458 | |
Igor Murashkin | dcb07d5 | 2013-02-21 14:18:10 -0800 | [diff] [blame] | 459 | |
| 460 | static int FormatFromString(const char* str) { |
| 461 | std::string s(str); |
| 462 | |
| 463 | #define CMP_STR(x, y) \ |
| 464 | if (s == #x) return HAL_PIXEL_FORMAT_ ## y; |
| 465 | #define CMP_STR_SAME(x) CMP_STR(x, x) |
| 466 | |
| 467 | CMP_STR_SAME( Y16); |
| 468 | CMP_STR_SAME( Y8); |
| 469 | CMP_STR_SAME( YV12); |
| 470 | CMP_STR(NV16, YCbCr_422_SP); |
| 471 | CMP_STR(NV21, YCrCb_420_SP); |
| 472 | CMP_STR(YUY2, YCbCr_422_I); |
| 473 | CMP_STR(RAW, RAW_SENSOR); |
| 474 | CMP_STR(RGBA, RGBA_8888); |
| 475 | |
| 476 | std::cerr << "Unknown format string " << str << std::endl; |
| 477 | return -1; |
| 478 | |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Creating a streaming request for these output streams from a template, |
| 483 | * and submit it |
| 484 | */ |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 485 | void createSubmitRequestForStreams(uint8_t* streamIds, size_t count, int requestCount=-1) { |
Igor Murashkin | dcb07d5 | 2013-02-21 14:18:10 -0800 | [diff] [blame] | 486 | |
| 487 | ASSERT_NE((void*)NULL, streamIds); |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 488 | ASSERT_LT(0u, count); |
Igor Murashkin | dcb07d5 | 2013-02-21 14:18:10 -0800 | [diff] [blame] | 489 | |
| 490 | camera_metadata_t *requestTmp = NULL; |
| 491 | EXPECT_OK(mCamera->createDefaultRequest(CAMERA2_TEMPLATE_PREVIEW, |
| 492 | /*out*/&requestTmp)); |
| 493 | ASSERT_NE((void*)NULL, requestTmp); |
| 494 | CameraMetadata request(requestTmp); |
| 495 | |
| 496 | // set the output streams. default is empty |
| 497 | |
| 498 | uint32_t tag = static_cast<uint32_t>(ANDROID_REQUEST_OUTPUT_STREAMS); |
| 499 | request.update(tag, streamIds, count); |
| 500 | |
| 501 | requestTmp = request.release(); |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 502 | |
| 503 | if (requestCount < 0) { |
| 504 | EXPECT_OK(mCamera->submitRequest(requestTmp, /*streaming*/true)); |
| 505 | } else { |
| 506 | for (int i = 0; i < requestCount; ++i) { |
| 507 | EXPECT_OK(mCamera->submitRequest(requestTmp, |
| 508 | /*streaming*/false)); |
| 509 | } |
| 510 | } |
Igor Murashkin | dcb07d5 | 2013-02-21 14:18:10 -0800 | [diff] [blame] | 511 | request.acquire(requestTmp); |
| 512 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 513 | }; |
| 514 | |
Igor Murashkin | 39f79f7 | 2013-01-30 10:14:24 -0800 | [diff] [blame] | 515 | sp<Thread> ProCameraTest::mTestThread; |
| 516 | |
Igor Murashkin | 7b33a74 | 2013-02-21 13:49:26 -0800 | [diff] [blame] | 517 | TEST_F(ProCameraTest, AvailableFormats) { |
| 518 | if (HasFatalFailure()) { |
| 519 | return; |
| 520 | } |
| 521 | |
Igor Murashkin | dcb07d5 | 2013-02-21 14:18:10 -0800 | [diff] [blame] | 522 | CameraMetadata staticInfo = mCamera->getCameraInfo(CAMERA_ID); |
| 523 | ASSERT_FALSE(staticInfo.isEmpty()); |
Igor Murashkin | 7b33a74 | 2013-02-21 13:49:26 -0800 | [diff] [blame] | 524 | |
Igor Murashkin | 7b33a74 | 2013-02-21 13:49:26 -0800 | [diff] [blame] | 525 | uint32_t tag = static_cast<uint32_t>(ANDROID_SCALER_AVAILABLE_FORMATS); |
Igor Murashkin | dcb07d5 | 2013-02-21 14:18:10 -0800 | [diff] [blame] | 526 | EXPECT_TRUE(staticInfo.exists(tag)); |
| 527 | camera_metadata_entry_t entry = staticInfo.find(tag); |
Igor Murashkin | 7b33a74 | 2013-02-21 13:49:26 -0800 | [diff] [blame] | 528 | |
Igor Murashkin | dcb07d5 | 2013-02-21 14:18:10 -0800 | [diff] [blame] | 529 | EXPECT_TRUE(ExistsItem<int32_t>(HAL_PIXEL_FORMAT_YV12, |
Igor Murashkin | 7b33a74 | 2013-02-21 13:49:26 -0800 | [diff] [blame] | 530 | entry.data.i32, entry.count)); |
Igor Murashkin | dcb07d5 | 2013-02-21 14:18:10 -0800 | [diff] [blame] | 531 | EXPECT_TRUE(ExistsItem<int32_t>(HAL_PIXEL_FORMAT_YCrCb_420_SP, |
Igor Murashkin | 7b33a74 | 2013-02-21 13:49:26 -0800 | [diff] [blame] | 532 | entry.data.i32, entry.count)); |
Igor Murashkin | 7b33a74 | 2013-02-21 13:49:26 -0800 | [diff] [blame] | 533 | } |
| 534 | |
Igor Murashkin | 39f79f7 | 2013-01-30 10:14:24 -0800 | [diff] [blame] | 535 | // test around exclusiveTryLock (immediate locking) |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 536 | TEST_F(ProCameraTest, LockingImmediate) { |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 537 | |
| 538 | if (HasFatalFailure()) { |
| 539 | return; |
| 540 | } |
| 541 | |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 542 | mListener->SetEventMask(ProEvent_Mask(ACQUIRED) | |
| 543 | ProEvent_Mask(STOLEN) | |
| 544 | ProEvent_Mask(RELEASED)); |
| 545 | |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 546 | EXPECT_FALSE(mCamera->hasExclusiveLock()); |
| 547 | EXPECT_EQ(OK, mCamera->exclusiveTryLock()); |
Igor Murashkin | 39f79f7 | 2013-01-30 10:14:24 -0800 | [diff] [blame] | 548 | // at this point we definitely have the lock |
| 549 | |
| 550 | EXPECT_EQ(OK, mListener->WaitForEvent()); |
| 551 | EXPECT_EQ(ACQUIRED, mListener->ReadEvent()); |
| 552 | |
| 553 | EXPECT_TRUE(mCamera->hasExclusiveLock()); |
| 554 | EXPECT_EQ(OK, mCamera->exclusiveUnlock()); |
| 555 | |
| 556 | EXPECT_EQ(OK, mListener->WaitForEvent()); |
| 557 | EXPECT_EQ(RELEASED, mListener->ReadEvent()); |
| 558 | |
| 559 | EXPECT_FALSE(mCamera->hasExclusiveLock()); |
| 560 | } |
| 561 | |
| 562 | // test around exclusiveLock (locking at some future point in time) |
| 563 | TEST_F(ProCameraTest, LockingAsynchronous) { |
| 564 | |
| 565 | if (HasFatalFailure()) { |
| 566 | return; |
| 567 | } |
| 568 | |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 569 | |
| 570 | mListener->SetEventMask(ProEvent_Mask(ACQUIRED) | |
| 571 | ProEvent_Mask(STOLEN) | |
| 572 | ProEvent_Mask(RELEASED)); |
| 573 | |
Igor Murashkin | 39f79f7 | 2013-01-30 10:14:24 -0800 | [diff] [blame] | 574 | // TODO: Add another procamera that has a lock here. |
| 575 | // then we can be test that the lock wont immediately be acquired |
| 576 | |
| 577 | EXPECT_FALSE(mCamera->hasExclusiveLock()); |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 578 | EXPECT_EQ(OK, mCamera->exclusiveTryLock()); |
| 579 | // at this point we definitely have the lock |
Igor Murashkin | 5376573 | 2013-02-20 17:41:57 -0800 | [diff] [blame] | 580 | |
| 581 | EXPECT_EQ(OK, mListener->WaitForEvent()); |
| 582 | EXPECT_EQ(ACQUIRED, mListener->ReadEvent()); |
| 583 | |
| 584 | EXPECT_TRUE(mCamera->hasExclusiveLock()); |
| 585 | EXPECT_EQ(OK, mCamera->exclusiveUnlock()); |
| 586 | |
| 587 | EXPECT_EQ(OK, mListener->WaitForEvent()); |
| 588 | EXPECT_EQ(RELEASED, mListener->ReadEvent()); |
| 589 | |
| 590 | EXPECT_FALSE(mCamera->hasExclusiveLock()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 591 | } |
| 592 | |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 593 | // Stream directly to the screen. |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 594 | TEST_F(ProCameraTest, DISABLED_StreamingImageSingle) { |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 595 | if (HasFatalFailure()) { |
| 596 | return; |
| 597 | } |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 598 | |
Igor Murashkin | dcb07d5 | 2013-02-21 14:18:10 -0800 | [diff] [blame] | 599 | sp<Surface> surface; |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 600 | if (mDisplaySecs > 0) { |
Igor Murashkin | dcb07d5 | 2013-02-21 14:18:10 -0800 | [diff] [blame] | 601 | createOnScreenSurface(/*out*/surface); |
| 602 | } |
| 603 | else { |
| 604 | dout << "Skipping, will not render to screen" << std::endl; |
| 605 | return; |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 606 | } |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 607 | |
| 608 | int depthStreamId = -1; |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 609 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 610 | sp<ServiceListener> listener = new ServiceListener(); |
| 611 | EXPECT_OK(ProCamera::addServiceListener(listener)); |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 612 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 613 | ServiceListener::Status currentStatus; |
| 614 | |
| 615 | // when subscribing a new listener, |
| 616 | // we immediately get a callback to the current status |
| 617 | while (listener->waitForStatusChange(/*out*/currentStatus) != OK); |
| 618 | EXPECT_EQ(ServiceListener::STATUS_PRESENT, currentStatus); |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 619 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 620 | dout << "Will now stream and resume infinitely..." << std::endl; |
| 621 | while (true) { |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 622 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 623 | if (currentStatus == ServiceListener::STATUS_PRESENT) { |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 624 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 625 | ASSERT_OK(mCamera->createStream(mDisplayW, mDisplayH, mDisplayFmt, |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 626 | surface, |
| 627 | &depthStreamId)); |
| 628 | EXPECT_NE(-1, depthStreamId); |
| 629 | |
| 630 | EXPECT_OK(mCamera->exclusiveTryLock()); |
| 631 | |
| 632 | uint8_t streams[] = { depthStreamId }; |
| 633 | ASSERT_NO_FATAL_FAILURE(createSubmitRequestForStreams( |
| 634 | streams, |
| 635 | /*count*/1)); |
| 636 | } |
| 637 | |
| 638 | ServiceListener::Status stat = ServiceListener::STATUS_UNKNOWN; |
| 639 | |
| 640 | // TODO: maybe check for getch every once in a while? |
| 641 | while (listener->waitForStatusChange(/*out*/stat) != OK); |
| 642 | |
| 643 | if (currentStatus != stat) { |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 644 | if (stat == ServiceListener::STATUS_PRESENT) { |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 645 | dout << "Reconnecting to camera" << std::endl; |
| 646 | mCamera = ProCamera::connect(CAMERA_ID); |
| 647 | } else if (stat == ServiceListener::STATUS_NOT_AVAILABLE) { |
| 648 | dout << "Disconnecting from camera" << std::endl; |
| 649 | mCamera->disconnect(); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 650 | } else if (stat == ServiceListener::STATUS_NOT_PRESENT) { |
| 651 | dout << "Camera unplugged" << std::endl; |
| 652 | mCamera = NULL; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 653 | } else { |
| 654 | dout << "Unknown status change " |
| 655 | << std::hex << stat << std::endl; |
| 656 | } |
| 657 | |
| 658 | currentStatus = stat; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | EXPECT_OK(ProCamera::removeServiceListener(listener)); |
Igor Murashkin | dcb07d5 | 2013-02-21 14:18:10 -0800 | [diff] [blame] | 663 | EXPECT_OK(mCamera->deleteStream(depthStreamId)); |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 664 | EXPECT_OK(mCamera->exclusiveUnlock()); |
| 665 | } |
| 666 | |
Igor Murashkin | eb72e17 | 2013-02-21 11:43:14 -0800 | [diff] [blame] | 667 | // Stream directly to the screen. |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 668 | TEST_F(ProCameraTest, DISABLED_StreamingImageDual) { |
Igor Murashkin | eb72e17 | 2013-02-21 11:43:14 -0800 | [diff] [blame] | 669 | if (HasFatalFailure()) { |
| 670 | return; |
| 671 | } |
Igor Murashkin | eb72e17 | 2013-02-21 11:43:14 -0800 | [diff] [blame] | 672 | sp<Surface> surface; |
| 673 | sp<Surface> depthSurface; |
| 674 | if (mDisplaySecs > 0) { |
| 675 | createOnScreenSurface(/*out*/surface); |
| 676 | createDepthOnScreenSurface(/*out*/depthSurface); |
| 677 | } |
| 678 | |
| 679 | int streamId = -1; |
| 680 | EXPECT_OK(mCamera->createStream(/*width*/1280, /*height*/960, |
| 681 | TEST_FORMAT_MAIN, surface, &streamId)); |
| 682 | EXPECT_NE(-1, streamId); |
| 683 | |
| 684 | int depthStreamId = -1; |
| 685 | EXPECT_OK(mCamera->createStream(/*width*/320, /*height*/240, |
| 686 | TEST_FORMAT_DEPTH, depthSurface, &depthStreamId)); |
| 687 | EXPECT_NE(-1, depthStreamId); |
| 688 | |
| 689 | EXPECT_OK(mCamera->exclusiveTryLock()); |
| 690 | /* |
| 691 | */ |
| 692 | /* iterate in a loop submitting requests every frame. |
| 693 | * what kind of requests doesnt really matter, just whatever. |
| 694 | */ |
| 695 | |
| 696 | // it would probably be better to use CameraMetadata from camera service. |
| 697 | camera_metadata_t *request = NULL; |
| 698 | EXPECT_OK(mCamera->createDefaultRequest(CAMERA2_TEMPLATE_PREVIEW, |
| 699 | /*out*/&request)); |
| 700 | EXPECT_NE((void*)NULL, request); |
| 701 | |
| 702 | /*FIXME: dont need this later, at which point the above should become an |
| 703 | ASSERT_NE*/ |
| 704 | if(request == NULL) request = allocate_camera_metadata(10, 100); |
| 705 | |
| 706 | // set the output streams to just this stream ID |
| 707 | |
| 708 | // wow what a verbose API. |
| 709 | uint8_t allStreams[] = { streamId, depthStreamId }; |
| 710 | // IMPORTANT. bad things will happen if its not a uint8. |
| 711 | size_t streamCount = sizeof(allStreams) / sizeof(allStreams[0]); |
| 712 | camera_metadata_entry_t entry; |
| 713 | uint32_t tag = static_cast<uint32_t>(ANDROID_REQUEST_OUTPUT_STREAMS); |
| 714 | int find = find_camera_metadata_entry(request, tag, &entry); |
| 715 | if (find == -ENOENT) { |
| 716 | if (add_camera_metadata_entry(request, tag, &allStreams, |
| 717 | /*data_count*/streamCount) != OK) { |
| 718 | camera_metadata_t *tmp = allocate_camera_metadata(1000, 10000); |
| 719 | ASSERT_OK(append_camera_metadata(tmp, request)); |
| 720 | free_camera_metadata(request); |
| 721 | request = tmp; |
| 722 | |
| 723 | ASSERT_OK(add_camera_metadata_entry(request, tag, &allStreams, |
| 724 | /*data_count*/streamCount)); |
| 725 | } |
| 726 | } else { |
| 727 | ASSERT_OK(update_camera_metadata_entry(request, entry.index, |
| 728 | &allStreams, /*data_count*/streamCount, &entry)); |
| 729 | } |
| 730 | |
| 731 | EXPECT_OK(mCamera->submitRequest(request, /*streaming*/true)); |
| 732 | |
| 733 | dout << "will sleep now for " << mDisplaySecs << std::endl; |
| 734 | sleep(mDisplaySecs); |
| 735 | |
| 736 | free_camera_metadata(request); |
| 737 | |
| 738 | for (int i = 0; i < streamCount; ++i) { |
| 739 | EXPECT_OK(mCamera->deleteStream(allStreams[i])); |
| 740 | } |
| 741 | EXPECT_OK(mCamera->exclusiveUnlock()); |
| 742 | } |
| 743 | |
| 744 | TEST_F(ProCameraTest, CpuConsumerSingle) { |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 745 | if (HasFatalFailure()) { |
| 746 | return; |
| 747 | } |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 748 | |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 749 | mListener->SetEventMask(ProEvent_Mask(ACQUIRED) | |
| 750 | ProEvent_Mask(STOLEN) | |
| 751 | ProEvent_Mask(RELEASED) | |
| 752 | ProEvent_Mask(FRAME_RECEIVED)); |
| 753 | mListener->SetDropFrames(true); |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 754 | |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 755 | int streamId = -1; |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 756 | sp<CpuConsumer> consumer; |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 757 | EXPECT_OK(mCamera->createStreamCpu(/*width*/320, /*height*/240, |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 758 | TEST_FORMAT_DEPTH, TEST_CPU_HEAP_COUNT, &consumer, &streamId)); |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 759 | EXPECT_NE(-1, streamId); |
| 760 | |
| 761 | EXPECT_OK(mCamera->exclusiveTryLock()); |
| 762 | EXPECT_EQ(OK, mListener->WaitForEvent()); |
| 763 | EXPECT_EQ(ACQUIRED, mListener->ReadEvent()); |
| 764 | /* iterate in a loop submitting requests every frame. |
| 765 | * what kind of requests doesnt really matter, just whatever. |
| 766 | */ |
| 767 | |
| 768 | // it would probably be better to use CameraMetadata from camera service. |
| 769 | camera_metadata_t *request = NULL; |
| 770 | EXPECT_OK(mCamera->createDefaultRequest(CAMERA2_TEMPLATE_PREVIEW, |
| 771 | /*out*/&request)); |
| 772 | EXPECT_NE((void*)NULL, request); |
| 773 | |
| 774 | /*FIXME: dont need this later, at which point the above should become an |
| 775 | ASSERT_NE*/ |
| 776 | if(request == NULL) request = allocate_camera_metadata(10, 100); |
| 777 | |
| 778 | // set the output streams to just this stream ID |
| 779 | |
| 780 | uint8_t allStreams[] = { streamId }; |
| 781 | camera_metadata_entry_t entry; |
| 782 | uint32_t tag = static_cast<uint32_t>(ANDROID_REQUEST_OUTPUT_STREAMS); |
| 783 | int find = find_camera_metadata_entry(request, tag, &entry); |
| 784 | if (find == -ENOENT) { |
| 785 | if (add_camera_metadata_entry(request, tag, &allStreams, |
| 786 | /*data_count*/1) != OK) { |
| 787 | camera_metadata_t *tmp = allocate_camera_metadata(1000, 10000); |
| 788 | ASSERT_OK(append_camera_metadata(tmp, request)); |
| 789 | free_camera_metadata(request); |
| 790 | request = tmp; |
| 791 | |
| 792 | ASSERT_OK(add_camera_metadata_entry(request, tag, &allStreams, |
| 793 | /*data_count*/1)); |
| 794 | } |
| 795 | } else { |
| 796 | ASSERT_OK(update_camera_metadata_entry(request, entry.index, |
| 797 | &allStreams, /*data_count*/1, &entry)); |
| 798 | } |
| 799 | |
| 800 | EXPECT_OK(mCamera->submitRequest(request, /*streaming*/true)); |
| 801 | |
| 802 | // Consume a couple of frames |
| 803 | for (int i = 0; i < TEST_CPU_FRAME_COUNT; ++i) { |
| 804 | EXPECT_EQ(OK, mListener->WaitForEvent()); |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 805 | EXPECT_EQ(FRAME_RECEIVED, mListener->ReadEvent()); |
Igor Murashkin | 5835cc4 | 2013-02-20 19:29:53 -0800 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | // Done: clean up |
| 809 | free_camera_metadata(request); |
| 810 | EXPECT_OK(mCamera->deleteStream(streamId)); |
Igor Murashkin | 68506fd | 2013-02-20 17:57:31 -0800 | [diff] [blame] | 811 | EXPECT_OK(mCamera->exclusiveUnlock()); |
| 812 | } |
| 813 | |
Igor Murashkin | eb72e17 | 2013-02-21 11:43:14 -0800 | [diff] [blame] | 814 | TEST_F(ProCameraTest, CpuConsumerDual) { |
| 815 | if (HasFatalFailure()) { |
| 816 | return; |
| 817 | } |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 818 | |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 819 | mListener->SetEventMask(ProEvent_Mask(FRAME_RECEIVED)); |
| 820 | mListener->SetDropFrames(true); |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 821 | |
Igor Murashkin | eb72e17 | 2013-02-21 11:43:14 -0800 | [diff] [blame] | 822 | int streamId = -1; |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 823 | sp<CpuConsumer> consumer; |
Igor Murashkin | eb72e17 | 2013-02-21 11:43:14 -0800 | [diff] [blame] | 824 | EXPECT_OK(mCamera->createStreamCpu(/*width*/1280, /*height*/960, |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 825 | TEST_FORMAT_MAIN, TEST_CPU_HEAP_COUNT, &consumer, &streamId)); |
Igor Murashkin | eb72e17 | 2013-02-21 11:43:14 -0800 | [diff] [blame] | 826 | EXPECT_NE(-1, streamId); |
| 827 | |
| 828 | int depthStreamId = -1; |
| 829 | EXPECT_OK(mCamera->createStreamCpu(/*width*/320, /*height*/240, |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 830 | TEST_FORMAT_DEPTH, TEST_CPU_HEAP_COUNT, &consumer, &depthStreamId)); |
Igor Murashkin | eb72e17 | 2013-02-21 11:43:14 -0800 | [diff] [blame] | 831 | EXPECT_NE(-1, depthStreamId); |
| 832 | |
| 833 | EXPECT_OK(mCamera->exclusiveTryLock()); |
Igor Murashkin | eb72e17 | 2013-02-21 11:43:14 -0800 | [diff] [blame] | 834 | /* |
| 835 | */ |
| 836 | /* iterate in a loop submitting requests every frame. |
| 837 | * what kind of requests doesnt really matter, just whatever. |
| 838 | */ |
| 839 | |
| 840 | // it would probably be better to use CameraMetadata from camera service. |
| 841 | camera_metadata_t *request = NULL; |
| 842 | EXPECT_OK(mCamera->createDefaultRequest(CAMERA2_TEMPLATE_PREVIEW, |
| 843 | /*out*/&request)); |
| 844 | EXPECT_NE((void*)NULL, request); |
| 845 | |
| 846 | if(request == NULL) request = allocate_camera_metadata(10, 100); |
| 847 | |
| 848 | // set the output streams to just this stream ID |
| 849 | |
| 850 | // wow what a verbose API. |
| 851 | uint8_t allStreams[] = { streamId, depthStreamId }; |
| 852 | size_t streamCount = 2; |
| 853 | camera_metadata_entry_t entry; |
| 854 | uint32_t tag = static_cast<uint32_t>(ANDROID_REQUEST_OUTPUT_STREAMS); |
| 855 | int find = find_camera_metadata_entry(request, tag, &entry); |
| 856 | if (find == -ENOENT) { |
| 857 | if (add_camera_metadata_entry(request, tag, &allStreams, |
| 858 | /*data_count*/streamCount) != OK) { |
| 859 | camera_metadata_t *tmp = allocate_camera_metadata(1000, 10000); |
| 860 | ASSERT_OK(append_camera_metadata(tmp, request)); |
| 861 | free_camera_metadata(request); |
| 862 | request = tmp; |
| 863 | |
| 864 | ASSERT_OK(add_camera_metadata_entry(request, tag, &allStreams, |
| 865 | /*data_count*/streamCount)); |
| 866 | } |
| 867 | } else { |
| 868 | ASSERT_OK(update_camera_metadata_entry(request, entry.index, |
| 869 | &allStreams, /*data_count*/streamCount, &entry)); |
| 870 | } |
| 871 | |
| 872 | EXPECT_OK(mCamera->submitRequest(request, /*streaming*/true)); |
| 873 | |
| 874 | // Consume a couple of frames |
| 875 | for (int i = 0; i < TEST_CPU_FRAME_COUNT; ++i) { |
| 876 | // stream id 1 |
| 877 | EXPECT_EQ(OK, mListener->WaitForEvent()); |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 878 | EXPECT_EQ(FRAME_RECEIVED, mListener->ReadEvent()); |
Igor Murashkin | eb72e17 | 2013-02-21 11:43:14 -0800 | [diff] [blame] | 879 | |
| 880 | // stream id 2 |
| 881 | EXPECT_EQ(OK, mListener->WaitForEvent()); |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 882 | EXPECT_EQ(FRAME_RECEIVED, mListener->ReadEvent()); |
Igor Murashkin | eb72e17 | 2013-02-21 11:43:14 -0800 | [diff] [blame] | 883 | |
| 884 | //TODO: events should be a struct with some data like the stream id |
| 885 | } |
| 886 | |
| 887 | // Done: clean up |
| 888 | free_camera_metadata(request); |
| 889 | EXPECT_OK(mCamera->deleteStream(streamId)); |
| 890 | EXPECT_OK(mCamera->exclusiveUnlock()); |
| 891 | } |
| 892 | |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 893 | TEST_F(ProCameraTest, ResultReceiver) { |
| 894 | if (HasFatalFailure()) { |
| 895 | return; |
| 896 | } |
| 897 | |
| 898 | mListener->SetEventMask(ProEvent_Mask(RESULT_RECEIVED)); |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 899 | mListener->SetDropFrames(true); |
| 900 | //FIXME: if this is run right after the previous test we get FRAME_RECEIVED |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 901 | // need to filter out events at read time |
| 902 | |
| 903 | int streamId = -1; |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 904 | sp<CpuConsumer> consumer; |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 905 | EXPECT_OK(mCamera->createStreamCpu(/*width*/1280, /*height*/960, |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 906 | TEST_FORMAT_MAIN, TEST_CPU_HEAP_COUNT, &consumer, &streamId)); |
Igor Murashkin | a91537e | 2013-02-21 12:02:29 -0800 | [diff] [blame] | 907 | EXPECT_NE(-1, streamId); |
| 908 | |
| 909 | EXPECT_OK(mCamera->exclusiveTryLock()); |
| 910 | /* |
| 911 | */ |
| 912 | /* iterate in a loop submitting requests every frame. |
| 913 | * what kind of requests doesnt really matter, just whatever. |
| 914 | */ |
| 915 | |
| 916 | camera_metadata_t *request = NULL; |
| 917 | EXPECT_OK(mCamera->createDefaultRequest(CAMERA2_TEMPLATE_PREVIEW, |
| 918 | /*out*/&request)); |
| 919 | EXPECT_NE((void*)NULL, request); |
| 920 | |
| 921 | /*FIXME*/ |
| 922 | if(request == NULL) request = allocate_camera_metadata(10, 100); |
| 923 | |
| 924 | // set the output streams to just this stream ID |
| 925 | |
| 926 | uint8_t allStreams[] = { streamId }; |
| 927 | size_t streamCount = 1; |
| 928 | camera_metadata_entry_t entry; |
| 929 | uint32_t tag = static_cast<uint32_t>(ANDROID_REQUEST_OUTPUT_STREAMS); |
| 930 | int find = find_camera_metadata_entry(request, tag, &entry); |
| 931 | if (find == -ENOENT) { |
| 932 | if (add_camera_metadata_entry(request, tag, &allStreams, |
| 933 | /*data_count*/streamCount) != OK) { |
| 934 | camera_metadata_t *tmp = allocate_camera_metadata(1000, 10000); |
| 935 | ASSERT_OK(append_camera_metadata(tmp, request)); |
| 936 | free_camera_metadata(request); |
| 937 | request = tmp; |
| 938 | |
| 939 | ASSERT_OK(add_camera_metadata_entry(request, tag, &allStreams, |
| 940 | /*data_count*/streamCount)); |
| 941 | } |
| 942 | } else { |
| 943 | ASSERT_OK(update_camera_metadata_entry(request, entry.index, |
| 944 | &allStreams, /*data_count*/streamCount, &entry)); |
| 945 | } |
| 946 | |
| 947 | EXPECT_OK(mCamera->submitRequest(request, /*streaming*/true)); |
| 948 | |
| 949 | // Consume a couple of results |
| 950 | for (int i = 0; i < TEST_CPU_FRAME_COUNT; ++i) { |
| 951 | EXPECT_EQ(OK, mListener->WaitForEvent()); |
| 952 | EXPECT_EQ(RESULT_RECEIVED, mListener->ReadEvent()); |
| 953 | } |
| 954 | |
| 955 | // Done: clean up |
| 956 | free_camera_metadata(request); |
| 957 | EXPECT_OK(mCamera->deleteStream(streamId)); |
| 958 | EXPECT_OK(mCamera->exclusiveUnlock()); |
| 959 | } |
| 960 | |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 961 | // FIXME: This is racy and sometimes fails on waitForFrameMetadata |
| 962 | TEST_F(ProCameraTest, DISABLED_WaitForResult) { |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 963 | if (HasFatalFailure()) { |
| 964 | return; |
| 965 | } |
| 966 | |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 967 | mListener->SetDropFrames(true); |
| 968 | |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 969 | int streamId = -1; |
| 970 | sp<CpuConsumer> consumer; |
| 971 | EXPECT_OK(mCamera->createStreamCpu(/*width*/1280, /*height*/960, |
| 972 | TEST_FORMAT_MAIN, TEST_CPU_HEAP_COUNT, &consumer, &streamId)); |
| 973 | EXPECT_NE(-1, streamId); |
| 974 | |
| 975 | EXPECT_OK(mCamera->exclusiveTryLock()); |
| 976 | |
| 977 | uint8_t streams[] = { streamId }; |
| 978 | ASSERT_NO_FATAL_FAILURE(createSubmitRequestForStreams(streams, /*count*/1)); |
| 979 | |
| 980 | // Consume a couple of results |
| 981 | for (int i = 0; i < TEST_CPU_FRAME_COUNT; ++i) { |
| 982 | EXPECT_OK(mCamera->waitForFrameMetadata()); |
| 983 | CameraMetadata meta = mCamera->consumeFrameMetadata(); |
| 984 | EXPECT_FALSE(meta.isEmpty()); |
| 985 | } |
| 986 | |
| 987 | // Done: clean up |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 988 | EXPECT_OK(mCamera->deleteStream(streamId)); |
| 989 | EXPECT_OK(mCamera->exclusiveUnlock()); |
| 990 | } |
| 991 | |
| 992 | TEST_F(ProCameraTest, WaitForSingleStreamBuffer) { |
| 993 | if (HasFatalFailure()) { |
| 994 | return; |
| 995 | } |
| 996 | |
| 997 | int streamId = -1; |
| 998 | sp<CpuConsumer> consumer; |
| 999 | EXPECT_OK(mCamera->createStreamCpu(/*width*/1280, /*height*/960, |
| 1000 | TEST_FORMAT_MAIN, TEST_CPU_HEAP_COUNT, &consumer, &streamId)); |
| 1001 | EXPECT_NE(-1, streamId); |
| 1002 | |
| 1003 | EXPECT_OK(mCamera->exclusiveTryLock()); |
| 1004 | |
| 1005 | uint8_t streams[] = { streamId }; |
| 1006 | ASSERT_NO_FATAL_FAILURE(createSubmitRequestForStreams(streams, /*count*/1, |
| 1007 | /*requests*/TEST_CPU_FRAME_COUNT)); |
| 1008 | |
| 1009 | // Consume a couple of results |
| 1010 | for (int i = 0; i < TEST_CPU_FRAME_COUNT; ++i) { |
Igor Murashkin | 4bc4a38 | 2013-02-20 13:36:17 -0800 | [diff] [blame] | 1011 | EXPECT_EQ(1, mCamera->waitForFrameBuffer(streamId)); |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 1012 | |
| 1013 | CpuConsumer::LockedBuffer buf; |
| 1014 | EXPECT_OK(consumer->lockNextBuffer(&buf)); |
| 1015 | |
| 1016 | dout << "Buffer synchronously received on streamId = " << streamId << |
| 1017 | ", dataPtr = " << (void*)buf.data << |
| 1018 | ", timestamp = " << buf.timestamp << std::endl; |
| 1019 | |
| 1020 | EXPECT_OK(consumer->unlockBuffer(buf)); |
| 1021 | } |
| 1022 | |
| 1023 | // Done: clean up |
| 1024 | EXPECT_OK(mCamera->deleteStream(streamId)); |
| 1025 | EXPECT_OK(mCamera->exclusiveUnlock()); |
| 1026 | } |
| 1027 | |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 1028 | // FIXME: This is racy and sometimes fails on waitForFrameMetadata |
| 1029 | TEST_F(ProCameraTest, DISABLED_WaitForDualStreamBuffer) { |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 1030 | if (HasFatalFailure()) { |
| 1031 | return; |
| 1032 | } |
| 1033 | |
| 1034 | const int REQUEST_COUNT = TEST_CPU_FRAME_COUNT * 10; |
| 1035 | |
| 1036 | // 15 fps |
| 1037 | int streamId = -1; |
| 1038 | sp<CpuConsumer> consumer; |
| 1039 | EXPECT_OK(mCamera->createStreamCpu(/*width*/1280, /*height*/960, |
| 1040 | TEST_FORMAT_MAIN, TEST_CPU_HEAP_COUNT, &consumer, &streamId)); |
| 1041 | EXPECT_NE(-1, streamId); |
| 1042 | |
| 1043 | // 30 fps |
| 1044 | int depthStreamId = -1; |
| 1045 | sp<CpuConsumer> depthConsumer; |
| 1046 | EXPECT_OK(mCamera->createStreamCpu(/*width*/320, /*height*/240, |
| 1047 | TEST_FORMAT_DEPTH, TEST_CPU_HEAP_COUNT, &depthConsumer, &depthStreamId)); |
| 1048 | EXPECT_NE(-1, depthStreamId); |
| 1049 | |
| 1050 | EXPECT_OK(mCamera->exclusiveTryLock()); |
| 1051 | |
| 1052 | uint8_t streams[] = { streamId, depthStreamId }; |
| 1053 | ASSERT_NO_FATAL_FAILURE(createSubmitRequestForStreams(streams, /*count*/2, |
| 1054 | /*requests*/REQUEST_COUNT)); |
| 1055 | |
Igor Murashkin | 4753918 | 2013-03-18 11:10:48 -0700 | [diff] [blame] | 1056 | int depthFrames = 0; |
| 1057 | int greyFrames = 0; |
| 1058 | |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 1059 | // Consume two frames simultaneously. Unsynchronized by timestamps. |
| 1060 | for (int i = 0; i < REQUEST_COUNT; ++i) { |
| 1061 | |
Igor Murashkin | 65d7986 | 2013-03-27 11:07:06 -0700 | [diff] [blame] | 1062 | // Exhaust event queue so it doesn't keep growing |
| 1063 | while (mListener->ReadEvent() != UNKNOWN); |
| 1064 | |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 1065 | // Get the metadata |
| 1066 | EXPECT_OK(mCamera->waitForFrameMetadata()); |
| 1067 | CameraMetadata meta = mCamera->consumeFrameMetadata(); |
| 1068 | EXPECT_FALSE(meta.isEmpty()); |
| 1069 | |
| 1070 | // Get the buffers |
| 1071 | |
Igor Murashkin | 4bc4a38 | 2013-02-20 13:36:17 -0800 | [diff] [blame] | 1072 | EXPECT_EQ(1, mCamera->waitForFrameBuffer(depthStreamId)); |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 1073 | |
| 1074 | /** |
| 1075 | * Guaranteed to be able to consume the depth frame, |
| 1076 | * since we waited on it. |
| 1077 | */ |
| 1078 | CpuConsumer::LockedBuffer depthBuffer; |
| 1079 | EXPECT_OK(depthConsumer->lockNextBuffer(&depthBuffer)); |
| 1080 | |
| 1081 | dout << "Depth Buffer synchronously received on streamId = " << |
| 1082 | streamId << |
| 1083 | ", dataPtr = " << (void*)depthBuffer.data << |
| 1084 | ", timestamp = " << depthBuffer.timestamp << std::endl; |
| 1085 | |
| 1086 | EXPECT_OK(depthConsumer->unlockBuffer(depthBuffer)); |
| 1087 | |
Igor Murashkin | 4753918 | 2013-03-18 11:10:48 -0700 | [diff] [blame] | 1088 | depthFrames++; |
| 1089 | |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 1090 | |
| 1091 | /** Consume Greyscale frames if there are any. |
| 1092 | * There may not be since it runs at half FPS */ |
| 1093 | CpuConsumer::LockedBuffer greyBuffer; |
| 1094 | while (consumer->lockNextBuffer(&greyBuffer) == OK) { |
| 1095 | |
| 1096 | dout << "GRAY Buffer synchronously received on streamId = " << |
| 1097 | streamId << |
| 1098 | ", dataPtr = " << (void*)greyBuffer.data << |
| 1099 | ", timestamp = " << greyBuffer.timestamp << std::endl; |
| 1100 | |
| 1101 | EXPECT_OK(consumer->unlockBuffer(greyBuffer)); |
Igor Murashkin | 4753918 | 2013-03-18 11:10:48 -0700 | [diff] [blame] | 1102 | |
| 1103 | greyFrames++; |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
Igor Murashkin | 4753918 | 2013-03-18 11:10:48 -0700 | [diff] [blame] | 1107 | dout << "Done, summary: depth frames " << std::dec << depthFrames |
| 1108 | << ", grey frames " << std::dec << greyFrames << std::endl; |
| 1109 | |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 1110 | // Done: clean up |
| 1111 | EXPECT_OK(mCamera->deleteStream(streamId)); |
| 1112 | EXPECT_OK(mCamera->exclusiveUnlock()); |
| 1113 | } |
| 1114 | |
Igor Murashkin | ba5ca4e | 2013-02-28 11:21:00 -0800 | [diff] [blame] | 1115 | TEST_F(ProCameraTest, WaitForSingleStreamBufferAndDropFramesSync) { |
Igor Murashkin | 4bc4a38 | 2013-02-20 13:36:17 -0800 | [diff] [blame] | 1116 | if (HasFatalFailure()) { |
| 1117 | return; |
| 1118 | } |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 1119 | |
Igor Murashkin | 4bc4a38 | 2013-02-20 13:36:17 -0800 | [diff] [blame] | 1120 | const int NUM_REQUESTS = 20 * TEST_CPU_FRAME_COUNT; |
| 1121 | |
| 1122 | int streamId = -1; |
| 1123 | sp<CpuConsumer> consumer; |
| 1124 | EXPECT_OK(mCamera->createStreamCpu(/*width*/1280, /*height*/960, |
Igor Murashkin | ba5ca4e | 2013-02-28 11:21:00 -0800 | [diff] [blame] | 1125 | TEST_FORMAT_MAIN, TEST_CPU_HEAP_COUNT, |
| 1126 | /*synchronousMode*/true, &consumer, &streamId)); |
Igor Murashkin | 4bc4a38 | 2013-02-20 13:36:17 -0800 | [diff] [blame] | 1127 | EXPECT_NE(-1, streamId); |
| 1128 | |
| 1129 | EXPECT_OK(mCamera->exclusiveTryLock()); |
| 1130 | |
| 1131 | uint8_t streams[] = { streamId }; |
| 1132 | ASSERT_NO_FATAL_FAILURE(createSubmitRequestForStreams(streams, /*count*/1, |
| 1133 | /*requests*/NUM_REQUESTS)); |
| 1134 | |
| 1135 | // Consume a couple of results |
| 1136 | for (int i = 0; i < NUM_REQUESTS; ++i) { |
Igor Murashkin | 4bc4a38 | 2013-02-20 13:36:17 -0800 | [diff] [blame] | 1137 | int numFrames; |
| 1138 | EXPECT_TRUE((numFrames = mCamera->waitForFrameBuffer(streamId)) > 0); |
| 1139 | |
| 1140 | // Drop all but the newest framebuffer |
| 1141 | EXPECT_EQ(numFrames-1, mCamera->dropFrameBuffer(streamId, numFrames-1)); |
| 1142 | |
| 1143 | dout << "Dropped " << (numFrames - 1) << " frames" << std::endl; |
| 1144 | |
| 1145 | // Skip the counter ahead, don't try to consume these frames again |
| 1146 | i += numFrames-1; |
| 1147 | |
| 1148 | // "Consume" the buffer |
| 1149 | CpuConsumer::LockedBuffer buf; |
| 1150 | EXPECT_OK(consumer->lockNextBuffer(&buf)); |
| 1151 | |
| 1152 | dout << "Buffer synchronously received on streamId = " << streamId << |
| 1153 | ", dataPtr = " << (void*)buf.data << |
| 1154 | ", timestamp = " << buf.timestamp << std::endl; |
| 1155 | |
Igor Murashkin | 3fa4891 | 2013-02-28 10:41:51 -0800 | [diff] [blame] | 1156 | // Process at 10fps, stream is at 15fps. |
| 1157 | // This means we will definitely fill up the buffer queue with |
| 1158 | // extra buffers and need to drop them. |
| 1159 | usleep(TEST_FRAME_PROCESSING_DELAY_US); |
| 1160 | |
Igor Murashkin | 4bc4a38 | 2013-02-20 13:36:17 -0800 | [diff] [blame] | 1161 | EXPECT_OK(consumer->unlockBuffer(buf)); |
| 1162 | } |
| 1163 | |
| 1164 | // Done: clean up |
| 1165 | EXPECT_OK(mCamera->deleteStream(streamId)); |
| 1166 | EXPECT_OK(mCamera->exclusiveUnlock()); |
| 1167 | } |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 1168 | |
Igor Murashkin | ba5ca4e | 2013-02-28 11:21:00 -0800 | [diff] [blame] | 1169 | TEST_F(ProCameraTest, WaitForSingleStreamBufferAndDropFramesAsync) { |
| 1170 | if (HasFatalFailure()) { |
| 1171 | return; |
| 1172 | } |
| 1173 | |
| 1174 | const int NUM_REQUESTS = 20 * TEST_CPU_FRAME_COUNT; |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 1175 | const int CONSECUTIVE_FAILS_ASSUME_TIME_OUT = 5; |
Igor Murashkin | ba5ca4e | 2013-02-28 11:21:00 -0800 | [diff] [blame] | 1176 | |
| 1177 | int streamId = -1; |
| 1178 | sp<CpuConsumer> consumer; |
| 1179 | EXPECT_OK(mCamera->createStreamCpu(/*width*/1280, /*height*/960, |
| 1180 | TEST_FORMAT_MAIN, TEST_CPU_HEAP_COUNT, |
| 1181 | /*synchronousMode*/false, &consumer, &streamId)); |
| 1182 | EXPECT_NE(-1, streamId); |
| 1183 | |
| 1184 | EXPECT_OK(mCamera->exclusiveTryLock()); |
| 1185 | |
| 1186 | uint8_t streams[] = { streamId }; |
| 1187 | ASSERT_NO_FATAL_FAILURE(createSubmitRequestForStreams(streams, /*count*/1, |
| 1188 | /*requests*/NUM_REQUESTS)); |
| 1189 | |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 1190 | uint64_t lastFrameNumber = 0; |
| 1191 | int numFrames; |
| 1192 | |
Igor Murashkin | ba5ca4e | 2013-02-28 11:21:00 -0800 | [diff] [blame] | 1193 | // Consume a couple of results |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 1194 | int i; |
| 1195 | for (i = 0; i < NUM_REQUESTS && lastFrameNumber < NUM_REQUESTS; ++i) { |
| 1196 | EXPECT_LT(0, (numFrames = mCamera->waitForFrameBuffer(streamId))); |
Igor Murashkin | ba5ca4e | 2013-02-28 11:21:00 -0800 | [diff] [blame] | 1197 | |
| 1198 | dout << "Dropped " << (numFrames - 1) << " frames" << std::endl; |
| 1199 | |
| 1200 | // Skip the counter ahead, don't try to consume these frames again |
| 1201 | i += numFrames-1; |
| 1202 | |
| 1203 | // "Consume" the buffer |
| 1204 | CpuConsumer::LockedBuffer buf; |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 1205 | |
| 1206 | EXPECT_EQ(OK, consumer->lockNextBuffer(&buf)); |
| 1207 | |
| 1208 | lastFrameNumber = buf.frameNumber; |
Igor Murashkin | ba5ca4e | 2013-02-28 11:21:00 -0800 | [diff] [blame] | 1209 | |
| 1210 | dout << "Buffer asynchronously received on streamId = " << streamId << |
| 1211 | ", dataPtr = " << (void*)buf.data << |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 1212 | ", timestamp = " << buf.timestamp << |
| 1213 | ", framenumber = " << buf.frameNumber << std::endl; |
Igor Murashkin | ba5ca4e | 2013-02-28 11:21:00 -0800 | [diff] [blame] | 1214 | |
| 1215 | // Process at 10fps, stream is at 15fps. |
| 1216 | // This means we will definitely fill up the buffer queue with |
| 1217 | // extra buffers and need to drop them. |
| 1218 | usleep(TEST_FRAME_PROCESSING_DELAY_US); |
| 1219 | |
| 1220 | EXPECT_OK(consumer->unlockBuffer(buf)); |
| 1221 | } |
| 1222 | |
Igor Murashkin | c6deb68 | 2013-04-18 11:53:35 -0700 | [diff] [blame] | 1223 | dout << "Done after " << i << " iterations " << std::endl; |
| 1224 | |
Igor Murashkin | ba5ca4e | 2013-02-28 11:21:00 -0800 | [diff] [blame] | 1225 | // Done: clean up |
| 1226 | EXPECT_OK(mCamera->deleteStream(streamId)); |
| 1227 | EXPECT_OK(mCamera->exclusiveUnlock()); |
| 1228 | } |
| 1229 | |
Igor Murashkin | a140a6e | 2013-02-21 14:45:03 -0800 | [diff] [blame] | 1230 | |
| 1231 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1232 | //TODO: refactor into separate file |
| 1233 | TEST_F(ProCameraTest, ServiceListenersSubscribe) { |
| 1234 | |
| 1235 | ASSERT_EQ(4u, sizeof(ServiceListener::Status)); |
| 1236 | |
| 1237 | sp<ServiceListener> listener = new ServiceListener(); |
| 1238 | |
| 1239 | EXPECT_EQ(BAD_VALUE, ProCamera::removeServiceListener(listener)); |
| 1240 | EXPECT_OK(ProCamera::addServiceListener(listener)); |
| 1241 | |
| 1242 | EXPECT_EQ(ALREADY_EXISTS, ProCamera::addServiceListener(listener)); |
| 1243 | EXPECT_OK(ProCamera::removeServiceListener(listener)); |
| 1244 | |
| 1245 | EXPECT_EQ(BAD_VALUE, ProCamera::removeServiceListener(listener)); |
| 1246 | } |
| 1247 | |
| 1248 | //TODO: refactor into separate file |
| 1249 | TEST_F(ProCameraTest, ServiceListenersFunctional) { |
| 1250 | |
| 1251 | sp<ServiceListener> listener = new ServiceListener(); |
| 1252 | |
| 1253 | EXPECT_OK(ProCamera::addServiceListener(listener)); |
| 1254 | |
| 1255 | sp<Camera> cam = Camera::connect(CAMERA_ID, |
| 1256 | /*clientPackageName*/String16(), |
| 1257 | -1); |
| 1258 | EXPECT_NE((void*)NULL, cam.get()); |
| 1259 | |
| 1260 | ServiceListener::Status stat = ServiceListener::STATUS_UNKNOWN; |
| 1261 | EXPECT_OK(listener->waitForStatusChange(/*out*/stat)); |
| 1262 | |
| 1263 | EXPECT_EQ(ServiceListener::STATUS_NOT_AVAILABLE, stat); |
| 1264 | |
| 1265 | if (cam.get()) { |
| 1266 | cam->disconnect(); |
| 1267 | } |
| 1268 | |
| 1269 | EXPECT_OK(listener->waitForStatusChange(/*out*/stat)); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1270 | EXPECT_EQ(ServiceListener::STATUS_PRESENT, stat); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1271 | |
| 1272 | EXPECT_OK(ProCamera::removeServiceListener(listener)); |
| 1273 | } |
| 1274 | |
| 1275 | |
| 1276 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | } |
| 1281 | |