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