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> |
| 24 | #include <gui/SurfaceTextureClient.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 | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 42 | *os << "Format: " << 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 | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 71 | CameraStreamParams p = mParam; |
| 72 | sp<Camera2Device> device = mDevice; |
| 73 | |
| 74 | /* use an arbitrary w,h */ |
| 75 | { |
| 76 | const int tag = ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES; |
| 77 | |
| 78 | const android::camera2::CameraMetadata& staticInfo = device->info(); |
| 79 | camera_metadata_ro_entry entry = staticInfo.find(tag); |
| 80 | ASSERT_NE(0u, entry.count) |
| 81 | << "Missing tag android.scaler.availableProcessedSizes"; |
| 82 | |
| 83 | ASSERT_LE(2u, entry.count); |
| 84 | /* this seems like it would always be the smallest w,h |
| 85 | but we actually make no contract that it's sorted asc */; |
| 86 | mWidth = entry.data.i32[0]; |
| 87 | mHeight = entry.data.i32[1]; |
| 88 | } |
| 89 | } |
| 90 | void TearDown() { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 91 | TEST_EXTENSION_FORKING_TEAR_DOWN; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | protected: |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 95 | struct FrameListener : public ConsumerBase::FrameAvailableListener { |
| 96 | |
| 97 | FrameListener() { |
| 98 | mPendingFrames = 0; |
| 99 | } |
| 100 | |
| 101 | // CpuConsumer::FrameAvailableListener implementation |
| 102 | virtual void onFrameAvailable() { |
| 103 | ALOGV("Frame now available (start)"); |
| 104 | |
| 105 | Mutex::Autolock lock(mMutex); |
| 106 | mPendingFrames++; |
| 107 | mCondition.signal(); |
| 108 | |
| 109 | ALOGV("Frame now available (end)"); |
| 110 | } |
| 111 | |
| 112 | status_t waitForFrame(nsecs_t timeout) { |
| 113 | status_t res; |
| 114 | Mutex::Autolock lock(mMutex); |
| 115 | while (mPendingFrames == 0) { |
| 116 | res = mCondition.waitRelative(mMutex, timeout); |
| 117 | if (res != OK) return res; |
| 118 | } |
| 119 | mPendingFrames--; |
| 120 | return OK; |
| 121 | } |
| 122 | |
| 123 | private: |
| 124 | Mutex mMutex; |
| 125 | Condition mCondition; |
| 126 | int mPendingFrames; |
| 127 | }; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 128 | |
| 129 | void CreateStream() { |
| 130 | sp<Camera2Device> device = mDevice; |
| 131 | CameraStreamParams p = mParam; |
| 132 | |
| 133 | mCpuConsumer = new CpuConsumer(p.mHeapCount); |
| 134 | mCpuConsumer->setName(String8("CameraStreamTest::mCpuConsumer")); |
| 135 | |
| 136 | mNativeWindow = new SurfaceTextureClient( |
| 137 | mCpuConsumer->getProducerInterface()); |
| 138 | |
| 139 | ASSERT_EQ(OK, |
| 140 | device->createStream(mNativeWindow, |
| 141 | mWidth, mHeight, p.mFormat, /*size (for jpegs)*/0, |
| 142 | &mStreamId)); |
| 143 | |
| 144 | ASSERT_NE(-1, mStreamId); |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 145 | |
| 146 | // do not make 'this' a FrameListener or the lifetime policy will clash |
| 147 | mFrameListener = new FrameListener(); |
| 148 | mCpuConsumer->setFrameAvailableListener(mFrameListener); |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void DeleteStream() { |
| 152 | ASSERT_EQ(OK, mDevice->deleteStream(mStreamId)); |
| 153 | } |
| 154 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 155 | int mWidth; |
| 156 | int mHeight; |
| 157 | |
| 158 | int mStreamId; |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 159 | |
| 160 | android::sp<FrameListener> mFrameListener; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 161 | android::sp<CpuConsumer> mCpuConsumer; |
| 162 | android::sp<ANativeWindow> mNativeWindow; |
| 163 | |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame] | 164 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 165 | private: |
| 166 | CameraStreamParams mParam; |
| 167 | }; |
| 168 | |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 173 | #endif |