blob: d93d26b97de9995d86cb1700fd9d716c97deb29f [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>
24#include <gui/SurfaceTextureClient.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 {
36 int mCameraId;
37 int mFormat;
38 int mHeapCount;
39};
40
Igor Murashkineab33fc2012-11-06 17:02:54 -080041inline void PrintTo(const CameraStreamParams& p, ::std::ostream* os) {
42 *os << "{ ";
Igor Murashkinf1b9ae72012-12-07 15:08:35 -080043 *os << "CameraID: " << p.mCameraId << ", ";
44 *os << "Format: " << p.mFormat << ", ";
45 *os << "HeapCount: " << p.mHeapCount;
Igor Murashkineab33fc2012-11-06 17:02:54 -080046 *os << " }";
47}
48
Igor Murashkine302ee32012-11-05 11:14:49 -080049class CameraStreamFixture
50 : public CameraModuleFixture</*InfoQuirk*/true> {
51
52public:
53 CameraStreamFixture(CameraStreamParams p)
54 : CameraModuleFixture(p.mCameraId) {
Igor Murashkineab33fc2012-11-06 17:02:54 -080055 TEST_EXTENSION_FORKING_CONSTRUCTOR;
56
Igor Murashkine302ee32012-11-05 11:14:49 -080057 mParam = p;
58
59 SetUp();
60 }
61
62 ~CameraStreamFixture() {
Igor Murashkineab33fc2012-11-06 17:02:54 -080063 TEST_EXTENSION_FORKING_DESTRUCTOR;
64
Igor Murashkine302ee32012-11-05 11:14:49 -080065 TearDown();
66 }
67
68private:
69
70 void SetUp() {
Igor Murashkineab33fc2012-11-06 17:02:54 -080071 TEST_EXTENSION_FORKING_SET_UP;
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 Murashkine302ee32012-11-05 11:14:49 -080094 }
95
96protected:
Igor Murashkinf1b9ae72012-12-07 15:08:35 -080097 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 Murashkine302ee32012-11-05 11:14:49 -0800130
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 Murashkinf1b9ae72012-12-07 15:08:35 -0800147
148 // do not make 'this' a FrameListener or the lifetime policy will clash
149 mFrameListener = new FrameListener();
150 mCpuConsumer->setFrameAvailableListener(mFrameListener);
Igor Murashkine302ee32012-11-05 11:14:49 -0800151 }
152
153 void DeleteStream() {
154 ASSERT_EQ(OK, mDevice->deleteStream(mStreamId));
155 }
156
Igor Murashkine302ee32012-11-05 11:14:49 -0800157 int mWidth;
158 int mHeight;
159
160 int mStreamId;
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800161
162 android::sp<FrameListener> mFrameListener;
Igor Murashkine302ee32012-11-05 11:14:49 -0800163 android::sp<CpuConsumer> mCpuConsumer;
164 android::sp<ANativeWindow> mNativeWindow;
165
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800166
Igor Murashkine302ee32012-11-05 11:14:49 -0800167private:
168 CameraStreamParams mParam;
169};
170
171}
172}
173}
174
Igor Murashkineab33fc2012-11-06 17:02:54 -0800175#endif