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> |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame^] | 27 | #include <system/camera_metadata.h> |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 28 | |
| 29 | #include "CameraModuleFixture.h" |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 30 | #include "TestExtensions.h" |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | namespace camera2 { |
| 34 | namespace tests { |
| 35 | |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame^] | 36 | // Format specifier for picking the best format for CPU reading the given device |
| 37 | // version |
| 38 | #define CAMERA_STREAM_AUTO_CPU_FORMAT (-1) |
| 39 | |
| 40 | struct CameraStreamParams; |
| 41 | |
| 42 | void PrintTo(const CameraStreamParams& p, ::std::ostream* os); |
| 43 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 44 | struct CameraStreamParams { |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 45 | int mFormat; |
| 46 | int mHeapCount; |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame^] | 47 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 48 | }; |
| 49 | |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame^] | 50 | inline ::std::ostream& operator<<(::std::ostream& os, const CameraStreamParams &p) { |
| 51 | PrintTo(p, &os); |
| 52 | return os; |
| 53 | } |
| 54 | |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 55 | inline void PrintTo(const CameraStreamParams& p, ::std::ostream* os) { |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame^] | 56 | char fmt[100]; |
| 57 | camera_metadata_enum_snprint( |
| 58 | ANDROID_SCALER_AVAILABLE_FORMATS, p.mFormat, fmt, sizeof(fmt)); |
| 59 | |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 60 | *os << "{ "; |
Igor Murashkin | 7a7f357 | 2013-02-11 15:52:03 -0800 | [diff] [blame] | 61 | *os << "Format: 0x" << std::hex << p.mFormat << ", "; |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame^] | 62 | *os << "Format name: " << fmt << ", "; |
Igor Murashkin | 7a7f357 | 2013-02-11 15:52:03 -0800 | [diff] [blame] | 63 | *os << "HeapCount: " << p.mHeapCount; |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 64 | *os << " }"; |
| 65 | } |
| 66 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 67 | class CameraStreamFixture |
| 68 | : public CameraModuleFixture</*InfoQuirk*/true> { |
| 69 | |
| 70 | public: |
| 71 | CameraStreamFixture(CameraStreamParams p) |
Igor Murashkin | 00b597f | 2012-12-11 15:19:25 -0800 | [diff] [blame] | 72 | : CameraModuleFixture(TestSettings::DeviceId()) { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 73 | TEST_EXTENSION_FORKING_CONSTRUCTOR; |
| 74 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 75 | mParam = p; |
| 76 | |
| 77 | SetUp(); |
| 78 | } |
| 79 | |
| 80 | ~CameraStreamFixture() { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 81 | TEST_EXTENSION_FORKING_DESTRUCTOR; |
| 82 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 83 | TearDown(); |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | |
| 88 | void SetUp() { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 89 | TEST_EXTENSION_FORKING_SET_UP; |
| 90 | |
Igor Murashkin | 7acb21a | 2012-12-18 13:38:40 -0800 | [diff] [blame] | 91 | CameraModuleFixture::SetUp(); |
| 92 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 93 | CameraStreamParams p = mParam; |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame^] | 94 | sp<CameraDeviceBase> device = mDevice; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 95 | |
| 96 | /* use an arbitrary w,h */ |
| 97 | { |
| 98 | const int tag = ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES; |
| 99 | |
Igor Murashkin | 7ed10fb | 2013-02-13 17:33:36 -0800 | [diff] [blame] | 100 | const CameraMetadata& staticInfo = device->info(); |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 101 | camera_metadata_ro_entry entry = staticInfo.find(tag); |
| 102 | ASSERT_NE(0u, entry.count) |
| 103 | << "Missing tag android.scaler.availableProcessedSizes"; |
| 104 | |
| 105 | ASSERT_LE(2u, entry.count); |
| 106 | /* this seems like it would always be the smallest w,h |
| 107 | but we actually make no contract that it's sorted asc */; |
| 108 | mWidth = entry.data.i32[0]; |
| 109 | mHeight = entry.data.i32[1]; |
| 110 | } |
| 111 | } |
| 112 | void TearDown() { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 113 | TEST_EXTENSION_FORKING_TEAR_DOWN; |
Igor Murashkin | 7acb21a | 2012-12-18 13:38:40 -0800 | [diff] [blame] | 114 | |
| 115 | // important: shut down HAL before releasing streams |
| 116 | CameraModuleFixture::TearDown(); |
| 117 | |
| 118 | mNativeWindow.clear(); |
| 119 | mCpuConsumer.clear(); |
| 120 | mFrameListener.clear(); |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | protected: |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 124 | struct FrameListener : public ConsumerBase::FrameAvailableListener { |
| 125 | |
| 126 | FrameListener() { |
| 127 | mPendingFrames = 0; |
| 128 | } |
| 129 | |
| 130 | // CpuConsumer::FrameAvailableListener implementation |
| 131 | virtual void onFrameAvailable() { |
| 132 | ALOGV("Frame now available (start)"); |
| 133 | |
| 134 | Mutex::Autolock lock(mMutex); |
| 135 | mPendingFrames++; |
| 136 | mCondition.signal(); |
| 137 | |
| 138 | ALOGV("Frame now available (end)"); |
| 139 | } |
| 140 | |
| 141 | status_t waitForFrame(nsecs_t timeout) { |
| 142 | status_t res; |
| 143 | Mutex::Autolock lock(mMutex); |
| 144 | while (mPendingFrames == 0) { |
| 145 | res = mCondition.waitRelative(mMutex, timeout); |
| 146 | if (res != OK) return res; |
| 147 | } |
| 148 | mPendingFrames--; |
| 149 | return OK; |
| 150 | } |
| 151 | |
| 152 | private: |
| 153 | Mutex mMutex; |
| 154 | Condition mCondition; |
| 155 | int mPendingFrames; |
| 156 | }; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 157 | |
| 158 | void CreateStream() { |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame^] | 159 | sp<CameraDeviceBase> device = mDevice; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 160 | CameraStreamParams p = mParam; |
| 161 | |
| 162 | mCpuConsumer = new CpuConsumer(p.mHeapCount); |
| 163 | mCpuConsumer->setName(String8("CameraStreamTest::mCpuConsumer")); |
| 164 | |
Mathias Agopian | cc50111 | 2013-02-14 17:34:35 -0800 | [diff] [blame] | 165 | mNativeWindow = new Surface( |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 166 | mCpuConsumer->getProducerInterface()); |
| 167 | |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame^] | 168 | int format = MapAutoFormat(p.mFormat); |
| 169 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 170 | ASSERT_EQ(OK, |
| 171 | device->createStream(mNativeWindow, |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame^] | 172 | mWidth, mHeight, format, /*size (for jpegs)*/0, |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 173 | &mStreamId)); |
| 174 | |
| 175 | ASSERT_NE(-1, mStreamId); |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 176 | |
| 177 | // do not make 'this' a FrameListener or the lifetime policy will clash |
| 178 | mFrameListener = new FrameListener(); |
| 179 | mCpuConsumer->setFrameAvailableListener(mFrameListener); |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void DeleteStream() { |
| 183 | ASSERT_EQ(OK, mDevice->deleteStream(mStreamId)); |
| 184 | } |
| 185 | |
Eino-Ville Talvala | 4c543a1 | 2013-06-25 18:12:19 -0700 | [diff] [blame^] | 186 | int MapAutoFormat(int format) { |
| 187 | if (format == CAMERA_STREAM_AUTO_CPU_FORMAT) { |
| 188 | if (getDeviceVersion() >= CAMERA_DEVICE_API_VERSION_3_0) { |
| 189 | format = HAL_PIXEL_FORMAT_YCbCr_420_888; |
| 190 | } else { |
| 191 | format = HAL_PIXEL_FORMAT_YCrCb_420_SP; |
| 192 | } |
| 193 | } |
| 194 | return format; |
| 195 | } |
| 196 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 197 | int mWidth; |
| 198 | int mHeight; |
| 199 | |
| 200 | int mStreamId; |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 201 | |
| 202 | android::sp<FrameListener> mFrameListener; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 203 | android::sp<CpuConsumer> mCpuConsumer; |
| 204 | android::sp<ANativeWindow> mNativeWindow; |
| 205 | |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 206 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 207 | private: |
| 208 | CameraStreamParams mParam; |
| 209 | }; |
| 210 | |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 215 | #endif |