blob: c7bcc548432a33b740f8a559e500a9c927e52b8e [file] [log] [blame]
Igor Murashkine302ee32012-11-05 11:14:49 -08001/*
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 Murashkineab33fc2012-11-06 17:02:54 -080017#ifndef __ANDROID_HAL_CAMERA2_TESTS_STREAM_FIXTURE__
18#define __ANDROID_HAL_CAMERA2_TESTS_STREAM_FIXTURE__
19
Igor Murashkine302ee32012-11-05 11:14:49 -080020#include <gtest/gtest.h>
Igor Murashkineab33fc2012-11-06 17:02:54 -080021#include <iostream>
Igor Murashkine302ee32012-11-05 11:14:49 -080022
23#include <gui/CpuConsumer.h>
Mathias Agopiancc501112013-02-14 17:34:35 -080024#include <gui/Surface.h>
Igor Murashkinf1b9ae72012-12-07 15:08:35 -080025#include <utils/Condition.h>
26#include <utils/Mutex.h>
Igor Murashkine302ee32012-11-05 11:14:49 -080027
28#include "CameraModuleFixture.h"
Igor Murashkineab33fc2012-11-06 17:02:54 -080029#include "TestExtensions.h"
Igor Murashkine302ee32012-11-05 11:14:49 -080030
31namespace android {
32namespace camera2 {
33namespace tests {
34
35struct CameraStreamParams {
Igor Murashkine302ee32012-11-05 11:14:49 -080036 int mFormat;
37 int mHeapCount;
38};
39
Igor Murashkineab33fc2012-11-06 17:02:54 -080040inline void PrintTo(const CameraStreamParams& p, ::std::ostream* os) {
41 *os << "{ ";
Igor Murashkin7a7f3572013-02-11 15:52:03 -080042 *os << "Format: 0x" << std::hex << p.mFormat << ", ";
43 *os << "HeapCount: " << p.mHeapCount;
Igor Murashkineab33fc2012-11-06 17:02:54 -080044 *os << " }";
45}
46
Igor Murashkine302ee32012-11-05 11:14:49 -080047class CameraStreamFixture
48 : public CameraModuleFixture</*InfoQuirk*/true> {
49
50public:
51 CameraStreamFixture(CameraStreamParams p)
Igor Murashkin00b597f2012-12-11 15:19:25 -080052 : CameraModuleFixture(TestSettings::DeviceId()) {
Igor Murashkineab33fc2012-11-06 17:02:54 -080053 TEST_EXTENSION_FORKING_CONSTRUCTOR;
54
Igor Murashkine302ee32012-11-05 11:14:49 -080055 mParam = p;
56
57 SetUp();
58 }
59
60 ~CameraStreamFixture() {
Igor Murashkineab33fc2012-11-06 17:02:54 -080061 TEST_EXTENSION_FORKING_DESTRUCTOR;
62
Igor Murashkine302ee32012-11-05 11:14:49 -080063 TearDown();
64 }
65
66private:
67
68 void SetUp() {
Igor Murashkineab33fc2012-11-06 17:02:54 -080069 TEST_EXTENSION_FORKING_SET_UP;
70
Igor Murashkin7acb21a2012-12-18 13:38:40 -080071 CameraModuleFixture::SetUp();
72
Igor Murashkine302ee32012-11-05 11:14:49 -080073 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 Murashkineab33fc2012-11-06 17:02:54 -080093 TEST_EXTENSION_FORKING_TEAR_DOWN;
Igor Murashkin7acb21a2012-12-18 13:38:40 -080094
95 // important: shut down HAL before releasing streams
96 CameraModuleFixture::TearDown();
97
98 mNativeWindow.clear();
99 mCpuConsumer.clear();
100 mFrameListener.clear();
Igor Murashkine302ee32012-11-05 11:14:49 -0800101 }
102
103protected:
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800104 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 Murashkine302ee32012-11-05 11:14:49 -0800137
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 Agopiancc501112013-02-14 17:34:35 -0800145 mNativeWindow = new Surface(
Igor Murashkine302ee32012-11-05 11:14:49 -0800146 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 Murashkinf1b9ae72012-12-07 15:08:35 -0800154
155 // do not make 'this' a FrameListener or the lifetime policy will clash
156 mFrameListener = new FrameListener();
157 mCpuConsumer->setFrameAvailableListener(mFrameListener);
Igor Murashkine302ee32012-11-05 11:14:49 -0800158 }
159
160 void DeleteStream() {
161 ASSERT_EQ(OK, mDevice->deleteStream(mStreamId));
162 }
163
Igor Murashkine302ee32012-11-05 11:14:49 -0800164 int mWidth;
165 int mHeight;
166
167 int mStreamId;
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800168
169 android::sp<FrameListener> mFrameListener;
Igor Murashkine302ee32012-11-05 11:14:49 -0800170 android::sp<CpuConsumer> mCpuConsumer;
171 android::sp<ANativeWindow> mNativeWindow;
172
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800173
Igor Murashkine302ee32012-11-05 11:14:49 -0800174private:
175 CameraStreamParams mParam;
176};
177
178}
179}
180}
181
Igor Murashkineab33fc2012-11-06 17:02:54 -0800182#endif