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 { |
| 36 | int mCameraId; |
| 37 | int mFormat; |
| 38 | int mHeapCount; |
| 39 | }; |
| 40 | |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 41 | inline void PrintTo(const CameraStreamParams& p, ::std::ostream* os) { |
| 42 | *os << "{ "; |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame^] | 43 | *os << "CameraID: " << p.mCameraId << ", "; |
| 44 | *os << "Format: " << p.mFormat << ", "; |
| 45 | *os << "HeapCount: " << p.mHeapCount; |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 46 | *os << " }"; |
| 47 | } |
| 48 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 49 | class CameraStreamFixture |
| 50 | : public CameraModuleFixture</*InfoQuirk*/true> { |
| 51 | |
| 52 | public: |
| 53 | CameraStreamFixture(CameraStreamParams p) |
| 54 | : CameraModuleFixture(p.mCameraId) { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 55 | TEST_EXTENSION_FORKING_CONSTRUCTOR; |
| 56 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 57 | mParam = p; |
| 58 | |
| 59 | SetUp(); |
| 60 | } |
| 61 | |
| 62 | ~CameraStreamFixture() { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 63 | TEST_EXTENSION_FORKING_DESTRUCTOR; |
| 64 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 65 | TearDown(); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | |
| 70 | void SetUp() { |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 71 | TEST_EXTENSION_FORKING_SET_UP; |
| 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 | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | protected: |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame^] | 97 | struct FrameListener : public ConsumerBase::FrameAvailableListener { |
| 98 | |
| 99 | FrameListener() { |
| 100 | mPendingFrames = 0; |
| 101 | } |
| 102 | |
| 103 | // CpuConsumer::FrameAvailableListener implementation |
| 104 | virtual void onFrameAvailable() { |
| 105 | ALOGV("Frame now available (start)"); |
| 106 | |
| 107 | Mutex::Autolock lock(mMutex); |
| 108 | mPendingFrames++; |
| 109 | mCondition.signal(); |
| 110 | |
| 111 | ALOGV("Frame now available (end)"); |
| 112 | } |
| 113 | |
| 114 | status_t waitForFrame(nsecs_t timeout) { |
| 115 | status_t res; |
| 116 | Mutex::Autolock lock(mMutex); |
| 117 | while (mPendingFrames == 0) { |
| 118 | res = mCondition.waitRelative(mMutex, timeout); |
| 119 | if (res != OK) return res; |
| 120 | } |
| 121 | mPendingFrames--; |
| 122 | return OK; |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | Mutex mMutex; |
| 127 | Condition mCondition; |
| 128 | int mPendingFrames; |
| 129 | }; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 130 | |
| 131 | void CreateStream() { |
| 132 | sp<Camera2Device> device = mDevice; |
| 133 | CameraStreamParams p = mParam; |
| 134 | |
| 135 | mCpuConsumer = new CpuConsumer(p.mHeapCount); |
| 136 | mCpuConsumer->setName(String8("CameraStreamTest::mCpuConsumer")); |
| 137 | |
| 138 | mNativeWindow = new SurfaceTextureClient( |
| 139 | mCpuConsumer->getProducerInterface()); |
| 140 | |
| 141 | ASSERT_EQ(OK, |
| 142 | device->createStream(mNativeWindow, |
| 143 | mWidth, mHeight, p.mFormat, /*size (for jpegs)*/0, |
| 144 | &mStreamId)); |
| 145 | |
| 146 | ASSERT_NE(-1, mStreamId); |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame^] | 147 | |
| 148 | // do not make 'this' a FrameListener or the lifetime policy will clash |
| 149 | mFrameListener = new FrameListener(); |
| 150 | mCpuConsumer->setFrameAvailableListener(mFrameListener); |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void DeleteStream() { |
| 154 | ASSERT_EQ(OK, mDevice->deleteStream(mStreamId)); |
| 155 | } |
| 156 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 157 | int mWidth; |
| 158 | int mHeight; |
| 159 | |
| 160 | int mStreamId; |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame^] | 161 | |
| 162 | android::sp<FrameListener> mFrameListener; |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 163 | android::sp<CpuConsumer> mCpuConsumer; |
| 164 | android::sp<ANativeWindow> mNativeWindow; |
| 165 | |
Igor Murashkin | f1b9ae7 | 2012-12-07 15:08:35 -0800 | [diff] [blame^] | 166 | |
Igor Murashkin | e302ee3 | 2012-11-05 11:14:49 -0800 | [diff] [blame] | 167 | private: |
| 168 | CameraStreamParams mParam; |
| 169 | }; |
| 170 | |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
Igor Murashkin | eab33fc | 2012-11-06 17:02:54 -0800 | [diff] [blame] | 175 | #endif |