blob: f56daf0bb7ecfa272b2d677e333ba292ebe6c93b [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>
Eino-Ville Talvala7d831712013-07-01 18:47:09 -070022#include <fstream>
Igor Murashkine302ee32012-11-05 11:14:49 -080023
24#include <gui/CpuConsumer.h>
Mathias Agopiancc501112013-02-14 17:34:35 -080025#include <gui/Surface.h>
Igor Murashkinf1b9ae72012-12-07 15:08:35 -080026#include <utils/Condition.h>
27#include <utils/Mutex.h>
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070028#include <system/camera_metadata.h>
Igor Murashkine302ee32012-11-05 11:14:49 -080029
30#include "CameraModuleFixture.h"
Igor Murashkineab33fc2012-11-06 17:02:54 -080031#include "TestExtensions.h"
Igor Murashkine302ee32012-11-05 11:14:49 -080032
Eino-Ville Talvala7d831712013-07-01 18:47:09 -070033#define ALIGN(x, mask) ( ((x) + (mask) - 1) & ~((mask) - 1) )
34
Igor Murashkine302ee32012-11-05 11:14:49 -080035namespace android {
36namespace camera2 {
37namespace tests {
38
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070039// Format specifier for picking the best format for CPU reading the given device
40// version
41#define CAMERA_STREAM_AUTO_CPU_FORMAT (-1)
42
43struct CameraStreamParams;
44
45void PrintTo(const CameraStreamParams& p, ::std::ostream* os);
46
Igor Murashkine302ee32012-11-05 11:14:49 -080047struct CameraStreamParams {
Igor Murashkine302ee32012-11-05 11:14:49 -080048 int mFormat;
49 int mHeapCount;
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070050
Igor Murashkine302ee32012-11-05 11:14:49 -080051};
52
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070053inline ::std::ostream& operator<<(::std::ostream& os, const CameraStreamParams &p) {
54 PrintTo(p, &os);
55 return os;
56}
57
Igor Murashkineab33fc2012-11-06 17:02:54 -080058inline void PrintTo(const CameraStreamParams& p, ::std::ostream* os) {
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070059 char fmt[100];
60 camera_metadata_enum_snprint(
61 ANDROID_SCALER_AVAILABLE_FORMATS, p.mFormat, fmt, sizeof(fmt));
62
Igor Murashkineab33fc2012-11-06 17:02:54 -080063 *os << "{ ";
Igor Murashkin7a7f3572013-02-11 15:52:03 -080064 *os << "Format: 0x" << std::hex << p.mFormat << ", ";
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070065 *os << "Format name: " << fmt << ", ";
Igor Murashkin7a7f3572013-02-11 15:52:03 -080066 *os << "HeapCount: " << p.mHeapCount;
Igor Murashkineab33fc2012-11-06 17:02:54 -080067 *os << " }";
68}
69
Igor Murashkine302ee32012-11-05 11:14:49 -080070class CameraStreamFixture
71 : public CameraModuleFixture</*InfoQuirk*/true> {
72
73public:
74 CameraStreamFixture(CameraStreamParams p)
Igor Murashkin00b597f2012-12-11 15:19:25 -080075 : CameraModuleFixture(TestSettings::DeviceId()) {
Igor Murashkineab33fc2012-11-06 17:02:54 -080076 TEST_EXTENSION_FORKING_CONSTRUCTOR;
77
Igor Murashkine302ee32012-11-05 11:14:49 -080078 mParam = p;
79
80 SetUp();
81 }
82
83 ~CameraStreamFixture() {
Igor Murashkineab33fc2012-11-06 17:02:54 -080084 TEST_EXTENSION_FORKING_DESTRUCTOR;
85
Igor Murashkine302ee32012-11-05 11:14:49 -080086 TearDown();
87 }
88
89private:
90
91 void SetUp() {
Igor Murashkineab33fc2012-11-06 17:02:54 -080092 TEST_EXTENSION_FORKING_SET_UP;
93
Igor Murashkin7acb21a2012-12-18 13:38:40 -080094 CameraModuleFixture::SetUp();
95
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070096 sp<CameraDeviceBase> device = mDevice;
Igor Murashkine302ee32012-11-05 11:14:49 -080097
98 /* use an arbitrary w,h */
99 {
100 const int tag = ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES;
101
Igor Murashkin7ed10fb2013-02-13 17:33:36 -0800102 const CameraMetadata& staticInfo = device->info();
Igor Murashkine302ee32012-11-05 11:14:49 -0800103 camera_metadata_ro_entry entry = staticInfo.find(tag);
104 ASSERT_NE(0u, entry.count)
105 << "Missing tag android.scaler.availableProcessedSizes";
106
107 ASSERT_LE(2u, entry.count);
108 /* this seems like it would always be the smallest w,h
109 but we actually make no contract that it's sorted asc */;
110 mWidth = entry.data.i32[0];
111 mHeight = entry.data.i32[1];
112 }
113 }
114 void TearDown() {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800115 TEST_EXTENSION_FORKING_TEAR_DOWN;
Igor Murashkin7acb21a2012-12-18 13:38:40 -0800116
117 // important: shut down HAL before releasing streams
118 CameraModuleFixture::TearDown();
119
120 mNativeWindow.clear();
121 mCpuConsumer.clear();
122 mFrameListener.clear();
Igor Murashkine302ee32012-11-05 11:14:49 -0800123 }
124
125protected:
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800126 struct FrameListener : public ConsumerBase::FrameAvailableListener {
127
128 FrameListener() {
129 mPendingFrames = 0;
130 }
131
132 // CpuConsumer::FrameAvailableListener implementation
133 virtual void onFrameAvailable() {
134 ALOGV("Frame now available (start)");
135
136 Mutex::Autolock lock(mMutex);
137 mPendingFrames++;
138 mCondition.signal();
139
140 ALOGV("Frame now available (end)");
141 }
142
143 status_t waitForFrame(nsecs_t timeout) {
144 status_t res;
145 Mutex::Autolock lock(mMutex);
146 while (mPendingFrames == 0) {
147 res = mCondition.waitRelative(mMutex, timeout);
148 if (res != OK) return res;
149 }
150 mPendingFrames--;
151 return OK;
152 }
153
154 private:
155 Mutex mMutex;
156 Condition mCondition;
157 int mPendingFrames;
158 };
Igor Murashkine302ee32012-11-05 11:14:49 -0800159
160 void CreateStream() {
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700161 sp<CameraDeviceBase> device = mDevice;
Igor Murashkine302ee32012-11-05 11:14:49 -0800162 CameraStreamParams p = mParam;
163
Dan Stoza5dce9e42014-04-07 13:39:37 -0700164 sp<IGraphicBufferProducer> producer;
165 sp<IGraphicBufferConsumer> consumer;
166 BufferQueue::createBufferQueue(&producer, &consumer);
167 mCpuConsumer = new CpuConsumer(consumer, p.mHeapCount);
Igor Murashkine302ee32012-11-05 11:14:49 -0800168 mCpuConsumer->setName(String8("CameraStreamTest::mCpuConsumer"));
169
Dan Stoza5dce9e42014-04-07 13:39:37 -0700170 mNativeWindow = new Surface(producer);
Igor Murashkine302ee32012-11-05 11:14:49 -0800171
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700172 int format = MapAutoFormat(p.mFormat);
173
Igor Murashkine302ee32012-11-05 11:14:49 -0800174 ASSERT_EQ(OK,
175 device->createStream(mNativeWindow,
Zhijun He1c3e6572014-08-10 21:24:49 -0700176 mWidth, mHeight, format,
Igor Murashkine302ee32012-11-05 11:14:49 -0800177 &mStreamId));
178
179 ASSERT_NE(-1, mStreamId);
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800180
181 // do not make 'this' a FrameListener or the lifetime policy will clash
182 mFrameListener = new FrameListener();
183 mCpuConsumer->setFrameAvailableListener(mFrameListener);
Igor Murashkine302ee32012-11-05 11:14:49 -0800184 }
185
186 void DeleteStream() {
187 ASSERT_EQ(OK, mDevice->deleteStream(mStreamId));
188 }
189
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700190 int MapAutoFormat(int format) {
191 if (format == CAMERA_STREAM_AUTO_CPU_FORMAT) {
192 if (getDeviceVersion() >= CAMERA_DEVICE_API_VERSION_3_0) {
193 format = HAL_PIXEL_FORMAT_YCbCr_420_888;
194 } else {
195 format = HAL_PIXEL_FORMAT_YCrCb_420_SP;
196 }
197 }
198 return format;
199 }
200
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700201 void DumpYuvToFile(const String8 &fileName, const CpuConsumer::LockedBuffer &img) {
202 uint8_t *dataCb, *dataCr;
203 uint32_t stride;
204 uint32_t chromaStride;
205 uint32_t chromaStep;
206
207 switch (img.format) {
208 case HAL_PIXEL_FORMAT_YCbCr_420_888:
209 stride = img.stride;
210 chromaStride = img.chromaStride;
211 chromaStep = img.chromaStep;
212 dataCb = img.dataCb;
213 dataCr = img.dataCr;
214 break;
215 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
216 stride = img.width;
217 chromaStride = img.width;
218 chromaStep = 2;
219 dataCr = img.data + img.width * img.height;
220 dataCb = dataCr + 1;
221 break;
222 case HAL_PIXEL_FORMAT_YV12:
223 stride = img.stride;
224 chromaStride = ALIGN(img.width / 2, 16);
225 chromaStep = 1;
226 dataCr = img.data + img.stride * img.height;
227 dataCb = dataCr + chromaStride * img.height/2;
228 break;
229 default:
230 ALOGE("Unknown format %d, not dumping", img.format);
231 return;
232 }
233
234 // Write Y
235 FILE *yuvFile = fopen(fileName.string(), "w");
236
237 size_t bytes;
238
239 for (size_t y = 0; y < img.height; ++y) {
240 bytes = fwrite(
241 reinterpret_cast<const char*>(img.data + stride * y),
242 1, img.width, yuvFile);
243 if (bytes != img.width) {
244 ALOGE("Unable to write to file %s", fileName.string());
245 fclose(yuvFile);
246 return;
247 }
248 }
249
250 // Write Cb/Cr
251 uint8_t *src = dataCb;
252 for (int c = 0; c < 2; ++c) {
253 for (size_t y = 0; y < img.height / 2; ++y) {
254 uint8_t *px = src + y * chromaStride;
255 if (chromaStep != 1) {
256 for (size_t x = 0; x < img.width / 2; ++x) {
257 fputc(*px, yuvFile);
258 px += chromaStep;
259 }
260 } else {
261 bytes = fwrite(reinterpret_cast<const char*>(px),
262 1, img.width / 2, yuvFile);
263 if (bytes != img.width / 2) {
264 ALOGE("Unable to write to file %s", fileName.string());
265 fclose(yuvFile);
266 return;
267 }
268 }
269 }
270 src = dataCr;
271 }
272 fclose(yuvFile);
273 }
274
Igor Murashkine302ee32012-11-05 11:14:49 -0800275 int mWidth;
276 int mHeight;
277
278 int mStreamId;
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800279
280 android::sp<FrameListener> mFrameListener;
Igor Murashkine302ee32012-11-05 11:14:49 -0800281 android::sp<CpuConsumer> mCpuConsumer;
282 android::sp<ANativeWindow> mNativeWindow;
283
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800284
Igor Murashkine302ee32012-11-05 11:14:49 -0800285private:
286 CameraStreamParams mParam;
287};
288
289}
290}
291}
292
Igor Murashkineab33fc2012-11-06 17:02:54 -0800293#endif