Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 17 | #ifndef __ANDROID_HAL_CAMERA2_TESTS_STREAM_FIXTURE__ |
| 18 | #define __ANDROID_HAL_CAMERA2_TESTS_STREAM_FIXTURE__ |
| 19 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 21 | #include <iostream> |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 22 | |
| 23 | #include <gui/CpuConsumer.h> |
Mathias Agopian | cc50111 | 2013-02-14 17:34:35 -0800 | [diff] [blame] | 24 | #include <gui/Surface.h> |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 25 | #include <utils/Condition.h> |
| 26 | #include <utils/Mutex.h> |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 27 | |
| 28 | #include "CameraModuleFixture.h" |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 29 | #include "TestExtensions.h" |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace camera2 { |
| 33 | namespace tests { |
| 34 | |
| 35 | struct CameraStreamParams { |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 36 | int mFormat; |
| 37 | int mHeapCount; |
| 38 | }; |
| 39 | |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 40 | inline void PrintTo(const CameraStreamParams& p, ::std::ostream* os) { |
| 41 | *os << "{ "; |
Igor Murashkin | 7a7f357 | 2013-02-11 15:52:03 -0800 | [diff] [blame^] | 42 | *os << "Format: 0x" << std::hex << p.mFormat << ", "; |
| 43 | *os << "HeapCount: " << p.mHeapCount; |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 44 | *os << " }"; |
| 45 | } |
| 46 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 47 | class CameraStreamFixture |
| 48 | : public CameraModuleFixture</*InfoQuirk*/true> { |
| 49 | |
| 50 | public: |
| 51 | CameraStreamFixture(CameraStreamParams p) |
Igor Murashkin | 00b597f | 2012-12-11 15:19:25 -0800 | [diff] [blame] | 52 | : CameraModuleFixture(TestSettings::DeviceId()) { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 53 | TEST_EXTENSION_FORKING_CONSTRUCTOR; |
| 54 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 55 | mParam = p; |
| 56 | |
| 57 | SetUp(); |
| 58 | } |
| 59 | |
| 60 | ~CameraStreamFixture() { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 61 | TEST_EXTENSION_FORKING_DESTRUCTOR; |
| 62 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 63 | TearDown(); |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | |
| 68 | void SetUp() { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 69 | TEST_EXTENSION_FORKING_SET_UP; |
| 70 | |
Igor Murashkin | 7acb21a | 2012-12-18 13:38:40 -0800 | [diff] [blame] | 71 | CameraModuleFixture::SetUp(); |
| 72 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 73 | CameraStreamParams p = mParam; |
| 74 | sp<Camera2Device> device = mDevice; |
| 75 | |
| 76 | /* use an arbitrary w,h */ |
| 77 | { |
| 78 | const int tag = ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES; |
| 79 | |
| 80 | const android::camera2::CameraMetadata& staticInfo = device->info(); |
| 81 | camera_metadata_ro_entry entry = staticInfo.find(tag); |
| 82 | ASSERT_NE(0u, entry.count) |
| 83 | << "Missing tag android.scaler.availableProcessedSizes"; |
| 84 | |
| 85 | ASSERT_LE(2u, entry.count); |
| 86 | /* this seems like it would always be the smallest w,h |
| 87 | but we actually make no contract that it's sorted asc */; |
| 88 | mWidth = entry.data.i32[0]; |
| 89 | mHeight = entry.data.i32[1]; |
| 90 | } |
| 91 | } |
| 92 | void TearDown() { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 93 | TEST_EXTENSION_FORKING_TEAR_DOWN; |
Igor Murashkin | 7acb21a | 2012-12-18 13:38:40 -0800 | [diff] [blame] | 94 | |
| 95 | // important: shut down HAL before releasing streams |
| 96 | CameraModuleFixture::TearDown(); |
| 97 | |
| 98 | mNativeWindow.clear(); |
| 99 | mCpuConsumer.clear(); |
| 100 | mFrameListener.clear(); |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | protected: |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 104 | struct FrameListener : public ConsumerBase::FrameAvailableListener { |
| 105 | |
| 106 | FrameListener() { |
| 107 | mPendingFrames = 0; |
| 108 | } |
| 109 | |
| 110 | // CpuConsumer::FrameAvailableListener implementation |
| 111 | virtual void onFrameAvailable() { |
| 112 | ALOGV("Frame now available (start)"); |
| 113 | |
| 114 | Mutex::Autolock lock(mMutex); |
| 115 | mPendingFrames++; |
| 116 | mCondition.signal(); |
| 117 | |
| 118 | ALOGV("Frame now available (end)"); |
| 119 | } |
| 120 | |
| 121 | status_t waitForFrame(nsecs_t timeout) { |
| 122 | status_t res; |
| 123 | Mutex::Autolock lock(mMutex); |
| 124 | while (mPendingFrames == 0) { |
| 125 | res = mCondition.waitRelative(mMutex, timeout); |
| 126 | if (res != OK) return res; |
| 127 | } |
| 128 | mPendingFrames--; |
| 129 | return OK; |
| 130 | } |
| 131 | |
| 132 | private: |
| 133 | Mutex mMutex; |
| 134 | Condition mCondition; |
| 135 | int mPendingFrames; |
| 136 | }; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 137 | |
| 138 | void CreateStream() { |
| 139 | sp<Camera2Device> device = mDevice; |
| 140 | CameraStreamParams p = mParam; |
| 141 | |
| 142 | mCpuConsumer = new CpuConsumer(p.mHeapCount); |
| 143 | mCpuConsumer->setName(String8("CameraStreamTest::mCpuConsumer")); |
| 144 | |
Mathias Agopian | cc50111 | 2013-02-14 17:34:35 -0800 | [diff] [blame] | 145 | mNativeWindow = new Surface( |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 146 | mCpuConsumer->getProducerInterface()); |
| 147 | |
| 148 | ASSERT_EQ(OK, |
| 149 | device->createStream(mNativeWindow, |
| 150 | mWidth, mHeight, p.mFormat, /*size (for jpegs)*/0, |
| 151 | &mStreamId)); |
| 152 | |
| 153 | ASSERT_NE(-1, mStreamId); |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 154 | |
| 155 | // do not make 'this' a FrameListener or the lifetime policy will clash |
| 156 | mFrameListener = new FrameListener(); |
| 157 | mCpuConsumer->setFrameAvailableListener(mFrameListener); |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void DeleteStream() { |
| 161 | ASSERT_EQ(OK, mDevice->deleteStream(mStreamId)); |
| 162 | } |
| 163 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 164 | int mWidth; |
| 165 | int mHeight; |
| 166 | |
| 167 | int mStreamId; |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 168 | |
| 169 | android::sp<FrameListener> mFrameListener; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 170 | android::sp<CpuConsumer> mCpuConsumer; |
| 171 | android::sp<ANativeWindow> mNativeWindow; |
| 172 | |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 173 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 174 | private: |
| 175 | CameraStreamParams mParam; |
| 176 | }; |
| 177 | |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 182 | #endif |