blob: ee30dccbb0af74f7e83a81b1da1f53aad02d6ace [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>
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070027#include <system/camera_metadata.h>
Igor Murashkine302ee32012-11-05 11:14:49 -080028
29#include "CameraModuleFixture.h"
Igor Murashkineab33fc2012-11-06 17:02:54 -080030#include "TestExtensions.h"
Igor Murashkine302ee32012-11-05 11:14:49 -080031
32namespace android {
33namespace camera2 {
34namespace tests {
35
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070036// Format specifier for picking the best format for CPU reading the given device
37// version
38#define CAMERA_STREAM_AUTO_CPU_FORMAT (-1)
39
40struct CameraStreamParams;
41
42void PrintTo(const CameraStreamParams& p, ::std::ostream* os);
43
Igor Murashkine302ee32012-11-05 11:14:49 -080044struct CameraStreamParams {
Igor Murashkine302ee32012-11-05 11:14:49 -080045 int mFormat;
46 int mHeapCount;
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070047
Igor Murashkine302ee32012-11-05 11:14:49 -080048};
49
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070050inline ::std::ostream& operator<<(::std::ostream& os, const CameraStreamParams &p) {
51 PrintTo(p, &os);
52 return os;
53}
54
Igor Murashkineab33fc2012-11-06 17:02:54 -080055inline void PrintTo(const CameraStreamParams& p, ::std::ostream* os) {
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070056 char fmt[100];
57 camera_metadata_enum_snprint(
58 ANDROID_SCALER_AVAILABLE_FORMATS, p.mFormat, fmt, sizeof(fmt));
59
Igor Murashkineab33fc2012-11-06 17:02:54 -080060 *os << "{ ";
Igor Murashkin7a7f3572013-02-11 15:52:03 -080061 *os << "Format: 0x" << std::hex << p.mFormat << ", ";
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070062 *os << "Format name: " << fmt << ", ";
Igor Murashkin7a7f3572013-02-11 15:52:03 -080063 *os << "HeapCount: " << p.mHeapCount;
Igor Murashkineab33fc2012-11-06 17:02:54 -080064 *os << " }";
65}
66
Igor Murashkine302ee32012-11-05 11:14:49 -080067class CameraStreamFixture
68 : public CameraModuleFixture</*InfoQuirk*/true> {
69
70public:
71 CameraStreamFixture(CameraStreamParams p)
Igor Murashkin00b597f2012-12-11 15:19:25 -080072 : CameraModuleFixture(TestSettings::DeviceId()) {
Igor Murashkineab33fc2012-11-06 17:02:54 -080073 TEST_EXTENSION_FORKING_CONSTRUCTOR;
74
Igor Murashkine302ee32012-11-05 11:14:49 -080075 mParam = p;
76
77 SetUp();
78 }
79
80 ~CameraStreamFixture() {
Igor Murashkineab33fc2012-11-06 17:02:54 -080081 TEST_EXTENSION_FORKING_DESTRUCTOR;
82
Igor Murashkine302ee32012-11-05 11:14:49 -080083 TearDown();
84 }
85
86private:
87
88 void SetUp() {
Igor Murashkineab33fc2012-11-06 17:02:54 -080089 TEST_EXTENSION_FORKING_SET_UP;
90
Igor Murashkin7acb21a2012-12-18 13:38:40 -080091 CameraModuleFixture::SetUp();
92
Igor Murashkine302ee32012-11-05 11:14:49 -080093 CameraStreamParams p = mParam;
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070094 sp<CameraDeviceBase> device = mDevice;
Igor Murashkine302ee32012-11-05 11:14:49 -080095
96 /* use an arbitrary w,h */
97 {
98 const int tag = ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES;
99
Igor Murashkin7ed10fb2013-02-13 17:33:36 -0800100 const CameraMetadata& staticInfo = device->info();
Igor Murashkine302ee32012-11-05 11:14:49 -0800101 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 Murashkineab33fc2012-11-06 17:02:54 -0800113 TEST_EXTENSION_FORKING_TEAR_DOWN;
Igor Murashkin7acb21a2012-12-18 13:38:40 -0800114
115 // important: shut down HAL before releasing streams
116 CameraModuleFixture::TearDown();
117
118 mNativeWindow.clear();
119 mCpuConsumer.clear();
120 mFrameListener.clear();
Igor Murashkine302ee32012-11-05 11:14:49 -0800121 }
122
123protected:
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800124 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 Murashkine302ee32012-11-05 11:14:49 -0800157
158 void CreateStream() {
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700159 sp<CameraDeviceBase> device = mDevice;
Igor Murashkine302ee32012-11-05 11:14:49 -0800160 CameraStreamParams p = mParam;
161
Mathias Agopian3ce8f022013-07-12 22:06:45 -0700162 sp<BufferQueue> bq = new BufferQueue();
163 mCpuConsumer = new CpuConsumer(bq, p.mHeapCount);
Igor Murashkine302ee32012-11-05 11:14:49 -0800164 mCpuConsumer->setName(String8("CameraStreamTest::mCpuConsumer"));
165
Mathias Agopiancc501112013-02-14 17:34:35 -0800166 mNativeWindow = new Surface(
Igor Murashkine302ee32012-11-05 11:14:49 -0800167 mCpuConsumer->getProducerInterface());
168
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700169 int format = MapAutoFormat(p.mFormat);
170
Igor Murashkine302ee32012-11-05 11:14:49 -0800171 ASSERT_EQ(OK,
172 device->createStream(mNativeWindow,
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700173 mWidth, mHeight, format, /*size (for jpegs)*/0,
Igor Murashkine302ee32012-11-05 11:14:49 -0800174 &mStreamId));
175
176 ASSERT_NE(-1, mStreamId);
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800177
178 // do not make 'this' a FrameListener or the lifetime policy will clash
179 mFrameListener = new FrameListener();
180 mCpuConsumer->setFrameAvailableListener(mFrameListener);
Igor Murashkine302ee32012-11-05 11:14:49 -0800181 }
182
183 void DeleteStream() {
184 ASSERT_EQ(OK, mDevice->deleteStream(mStreamId));
185 }
186
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700187 int MapAutoFormat(int format) {
188 if (format == CAMERA_STREAM_AUTO_CPU_FORMAT) {
189 if (getDeviceVersion() >= CAMERA_DEVICE_API_VERSION_3_0) {
190 format = HAL_PIXEL_FORMAT_YCbCr_420_888;
191 } else {
192 format = HAL_PIXEL_FORMAT_YCrCb_420_SP;
193 }
194 }
195 return format;
196 }
197
Igor Murashkine302ee32012-11-05 11:14:49 -0800198 int mWidth;
199 int mHeight;
200
201 int mStreamId;
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800202
203 android::sp<FrameListener> mFrameListener;
Igor Murashkine302ee32012-11-05 11:14:49 -0800204 android::sp<CpuConsumer> mCpuConsumer;
205 android::sp<ANativeWindow> mNativeWindow;
206
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800207
Igor Murashkine302ee32012-11-05 11:14:49 -0800208private:
209 CameraStreamParams mParam;
210};
211
212}
213}
214}
215
Igor Murashkineab33fc2012-11-06 17:02:54 -0800216#endif