blob: f4239cb69e75c6e3243a36a10318186028f58de5 [file] [log] [blame]
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -07001/*
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
17#define LOG_TAG "CpuConsumer_test"
18//#define LOG_NDEBUG 0
19//#define LOG_NNDEBUG 0
20
21#ifdef LOG_NNDEBUG
22#define ALOGVV(...) ALOGV(__VA_ARGS__)
23#else
24#define ALOGVV(...) ((void)0)
25#endif
26
27#include <gtest/gtest.h>
28#include <gui/CpuConsumer.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080029#include <gui/Surface.h>
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070030#include <ui/GraphicBuffer.h>
31#include <utils/String8.h>
32#include <utils/Thread.h>
33#include <utils/Mutex.h>
34#include <utils/Condition.h>
35
Chia-I Wu96ad8222017-11-16 15:41:45 -080036#include <thread>
Manoj Guptae8278ac2017-07-18 15:57:14 -070037#include <vector>
Igor Murashkin29e20472013-02-05 17:54:32 -080038#define CPU_CONSUMER_TEST_FORMAT_RAW 0
39#define CPU_CONSUMER_TEST_FORMAT_Y8 0
40#define CPU_CONSUMER_TEST_FORMAT_Y16 0
41#define CPU_CONSUMER_TEST_FORMAT_RGBA_8888 1
42
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070043namespace android {
44
45struct CpuConsumerTestParams {
46 uint32_t width;
47 uint32_t height;
48 int maxLockedBuffers;
49 PixelFormat format;
50};
51
52::std::ostream& operator<<(::std::ostream& os, const CpuConsumerTestParams& p) {
53 return os << "[ (" << p.width << ", " << p.height << "), B:"
54 << p.maxLockedBuffers << ", F:0x"
55 << ::std::hex << p.format << "]";
56}
57
58class CpuConsumerTest : public ::testing::TestWithParam<CpuConsumerTestParams> {
59protected:
60
61 virtual void SetUp() {
62 const ::testing::TestInfo* const test_info =
63 ::testing::UnitTest::GetInstance()->current_test_info();
64 CpuConsumerTestParams params = GetParam();
Nolan Scobiecfa41bf2023-06-14 15:13:18 -040065 ALOGD("** Starting parameterized test %s (%d x %d, %d, 0x%x)",
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070066 test_info->name(),
67 params.width, params.height,
68 params.maxLockedBuffers, params.format);
Jim Shargod30823a2024-07-27 02:49:39 +000069 mCC = new CpuConsumer(params.maxLockedBuffers);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070070 String8 name("CpuConsumer_Under_Test");
71 mCC->setName(name);
Jim Shargod30823a2024-07-27 02:49:39 +000072 mSTC = mCC->getSurface();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070073 mANW = mSTC;
74 }
75
76 virtual void TearDown() {
77 mANW.clear();
78 mSTC.clear();
79 mCC.clear();
80 }
81
82 class FrameWaiter : public CpuConsumer::FrameAvailableListener {
83 public:
84 FrameWaiter():
85 mPendingFrames(0) {
86 }
87
88 void waitForFrame() {
89 Mutex::Autolock lock(mMutex);
90 while (mPendingFrames == 0) {
91 mCondition.wait(mMutex);
92 }
93 mPendingFrames--;
94 }
95
Dan Stozaf8cebe52015-04-20 12:09:38 -070096 virtual void onFrameAvailable(const BufferItem&) {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070097 Mutex::Autolock lock(mMutex);
98 mPendingFrames++;
99 mCondition.signal();
100 }
101
102 int mPendingFrames;
103 Mutex mMutex;
104 Condition mCondition;
105 };
106
107 // Note that SurfaceTexture will lose the notifications
108 // onBuffersReleased and onFrameAvailable as there is currently
109 // no way to forward the events. This DisconnectWaiter will not let the
110 // disconnect finish until finishDisconnect() is called. It will
111 // also block until a disconnect is called
112 class DisconnectWaiter : public BufferQueue::ConsumerListener {
113 public:
114 DisconnectWaiter () :
115 mWaitForDisconnect(false),
116 mPendingFrames(0) {
117 }
118
119 void waitForFrame() {
120 Mutex::Autolock lock(mMutex);
121 while (mPendingFrames == 0) {
122 mFrameCondition.wait(mMutex);
123 }
124 mPendingFrames--;
125 }
126
Dan Stozaf8cebe52015-04-20 12:09:38 -0700127 virtual void onFrameAvailable(const BufferItem&) {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700128 Mutex::Autolock lock(mMutex);
129 mPendingFrames++;
130 mFrameCondition.signal();
131 }
132
133 virtual void onBuffersReleased() {
134 Mutex::Autolock lock(mMutex);
135 while (!mWaitForDisconnect) {
136 mDisconnectCondition.wait(mMutex);
137 }
138 }
139
140 void finishDisconnect() {
141 Mutex::Autolock lock(mMutex);
142 mWaitForDisconnect = true;
143 mDisconnectCondition.signal();
144 }
145
146 private:
147 Mutex mMutex;
148
149 bool mWaitForDisconnect;
150 Condition mDisconnectCondition;
151
152 int mPendingFrames;
153 Condition mFrameCondition;
154 };
155
156 sp<CpuConsumer> mCC;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800157 sp<Surface> mSTC;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700158 sp<ANativeWindow> mANW;
159};
160
161#define ASSERT_NO_ERROR(err, msg) \
Chih-Hung Hsieh7c5f1092016-05-20 11:46:52 -0700162 ASSERT_EQ(NO_ERROR, err) << (msg) << strerror(-(err))
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700163
164void checkPixel(const CpuConsumer::LockedBuffer &buf,
Igor Murashkin29e20472013-02-05 17:54:32 -0800165 uint32_t x, uint32_t y, uint32_t r, uint32_t g=0, uint32_t b=0) {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700166 // Ignores components that don't exist for given pixel
167 switch(buf.format) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800168 case HAL_PIXEL_FORMAT_RAW16: {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700169 String8 msg;
170 uint16_t *bPtr = (uint16_t*)buf.data;
171 bPtr += y * buf.stride + x;
172 // GRBG Bayer mosaic; only check the matching channel
173 switch( ((y & 1) << 1) | (x & 1) ) {
174 case 0: // G
175 case 3: // G
176 EXPECT_EQ(g, *bPtr);
177 break;
178 case 1: // R
179 EXPECT_EQ(r, *bPtr);
180 break;
181 case 2: // B
182 EXPECT_EQ(b, *bPtr);
183 break;
184 }
185 break;
186 }
Igor Murashkin29e20472013-02-05 17:54:32 -0800187 // ignores g,b
188 case HAL_PIXEL_FORMAT_Y8: {
189 uint8_t *bPtr = (uint8_t*)buf.data;
190 bPtr += y * buf.stride + x;
191 EXPECT_EQ(r, *bPtr) << "at x = " << x << " y = " << y;
192 break;
193 }
194 // ignores g,b
195 case HAL_PIXEL_FORMAT_Y16: {
196 // stride is in pixels, not in bytes
197 uint16_t *bPtr = ((uint16_t*)buf.data) + y * buf.stride + x;
198
199 EXPECT_EQ(r, *bPtr) << "at x = " << x << " y = " << y;
200 break;
201 }
202 case HAL_PIXEL_FORMAT_RGBA_8888: {
203 const int bytesPerPixel = 4;
204 uint8_t *bPtr = (uint8_t*)buf.data;
205 bPtr += (y * buf.stride + x) * bytesPerPixel;
206
207 EXPECT_EQ(r, bPtr[0]) << "at x = " << x << " y = " << y;
208 EXPECT_EQ(g, bPtr[1]) << "at x = " << x << " y = " << y;
209 EXPECT_EQ(b, bPtr[2]) << "at x = " << x << " y = " << y;
210 break;
211 }
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700212 default: {
213 ADD_FAILURE() << "Unknown format for check:" << buf.format;
214 break;
215 }
216 }
217}
218
219// Fill a YV12 buffer with a multi-colored checkerboard pattern
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700220void fillYV12Buffer(uint8_t* buf, int w, int h, int stride);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700221
Igor Murashkin29e20472013-02-05 17:54:32 -0800222// Fill a Y8/Y16 buffer with a multi-colored checkerboard pattern
223template <typename T> // T == uint8_t or uint16_t
224void fillGreyscaleBuffer(T* buf, int w, int h, int stride, int bpp) {
225 const int blockWidth = w > 16 ? w / 16 : 1;
226 const int blockHeight = h > 16 ? h / 16 : 1;
227 const int yuvTexOffsetY = 0;
228
229 ASSERT_TRUE(bpp == 8 || bpp == 16);
230 ASSERT_TRUE(sizeof(T)*8 == bpp);
231
232 // stride is in pixels, not in bytes
233 int yuvTexStrideY = stride;
234 for (int x = 0; x < w; x++) {
235 for (int y = 0; y < h; y++) {
236 int parityX = (x / blockWidth) & 1;
237 int parityY = (y / blockHeight) & 1;
238 T intensity = (parityX ^ parityY) ? 63 : 191;
239 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
240 }
241 }
242}
243
244inline uint8_t chooseColorRgba8888(int blockX, int blockY, uint8_t channel) {
245 const int colorVariations = 3;
246 uint8_t color = ((blockX % colorVariations) + (blockY % colorVariations))
247 % (colorVariations) == channel ? 191: 63;
248
249 return color;
250}
251
252// Fill a RGBA8888 buffer with a multi-colored checkerboard pattern
253void fillRgba8888Buffer(uint8_t* buf, int w, int h, int stride)
254{
255 const int blockWidth = w > 16 ? w / 16 : 1;
256 const int blockHeight = h > 16 ? h / 16 : 1;
257 const int bytesPerPixel = 4;
258
259 // stride is in pixels, not in bytes
260 for (int x = 0; x < w; ++x) {
261 for (int y = 0; y < h; ++y) {
262 int blockX = (x / blockWidth);
263 int blockY = (y / blockHeight);
264
265 uint8_t r = chooseColorRgba8888(blockX, blockY, 0);
266 uint8_t g = chooseColorRgba8888(blockX, blockY, 1);
267 uint8_t b = chooseColorRgba8888(blockX, blockY, 2);
268
269 buf[(y*stride + x)*bytesPerPixel + 0] = r;
270 buf[(y*stride + x)*bytesPerPixel + 1] = g;
271 buf[(y*stride + x)*bytesPerPixel + 2] = b;
272 buf[(y*stride + x)*bytesPerPixel + 3] = 255;
273 }
274 }
275}
276
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700277// Fill a RAW sensor buffer with a multi-colored checkerboard pattern.
278// Assumes GRBG mosaic ordering. Result should be a grid in a 2x2 pattern
279// of [ R, B; G, W]
280void fillBayerRawBuffer(uint8_t* buf, int w, int h, int stride) {
281 ALOGVV("fillBayerRawBuffer: %p with %d x %d, stride %d", buf, w, h ,stride);
282 // Blocks need to be even-width/height, aim for 8-wide otherwise
283 const int blockWidth = (w > 16 ? w / 8 : 2) & ~0x1;
284 const int blockHeight = (h > 16 ? h / 8 : 2) & ~0x1;
285 for (int y = 0; y < h; y+=2) {
286 uint16_t *bPtr1 = ((uint16_t*)buf) + stride*y;
287 uint16_t *bPtr2 = bPtr1 + stride;
288 for (int x = 0; x < w; x+=2) {
289 int blockX = (x / blockWidth ) & 1;
290 int blockY = (y / blockHeight) & 1;
291 unsigned short r = (blockX == blockY) ? 1000 : 200;
292 unsigned short g = blockY ? 1000: 200;
293 unsigned short b = blockX ? 1000: 200;
294 // GR row
295 *bPtr1++ = g;
296 *bPtr1++ = r;
297 // BG row
298 *bPtr2++ = b;
299 *bPtr2++ = g;
300 }
301 }
302
303}
304
Igor Murashkin29e20472013-02-05 17:54:32 -0800305template<typename T> // uint8_t or uint16_t
306void checkGreyscaleBuffer(const CpuConsumer::LockedBuffer &buf) {
307 uint32_t w = buf.width;
308 uint32_t h = buf.height;
309 const int blockWidth = w > 16 ? w / 16 : 1;
310 const int blockHeight = h > 16 ? h / 16 : 1;
Igor Murashkin29e20472013-02-05 17:54:32 -0800311
312 // Top-left square is bright
313 checkPixel(buf, 0, 0, 191);
314 checkPixel(buf, 1, 0, 191);
315 checkPixel(buf, 0, 1, 191);
316 checkPixel(buf, 1, 1, 191);
317
318 // One-right square is dark
319 checkPixel(buf, blockWidth, 0, 63);
320 checkPixel(buf, blockWidth + 1, 0, 63);
321 checkPixel(buf, blockWidth, 1, 63);
322 checkPixel(buf, blockWidth + 1, 1, 63);
323
324 // One-down square is dark
325 checkPixel(buf, 0, blockHeight, 63);
326 checkPixel(buf, 1, blockHeight, 63);
327 checkPixel(buf, 0, blockHeight + 1, 63);
328 checkPixel(buf, 1, blockHeight + 1, 63);
329
330 // One-diag square is bright
331 checkPixel(buf, blockWidth, blockHeight, 191);
332 checkPixel(buf, blockWidth + 1, blockHeight, 191);
333 checkPixel(buf, blockWidth, blockHeight + 1, 191);
334 checkPixel(buf, blockWidth + 1, blockHeight + 1, 191);
335
336 // Test bottom-right pixel
337 const int maxBlockX = ((w-1 + (blockWidth-1)) / blockWidth) & 0x1;
338 const int maxBlockY = ((h-1 + (blockHeight-1)) / blockHeight) & 0x1;
339 uint32_t pixelValue = ((maxBlockX % 2) == (maxBlockY % 2)) ? 191 : 63;
340 checkPixel(buf, w-1, h-1, pixelValue);
341}
342
343void checkRgba8888Buffer(const CpuConsumer::LockedBuffer &buf) {
344 uint32_t w = buf.width;
345 uint32_t h = buf.height;
346 const int blockWidth = w > 16 ? w / 16 : 1;
347 const int blockHeight = h > 16 ? h / 16 : 1;
Igor Murashkin29e20472013-02-05 17:54:32 -0800348
349 // Top-left square is bright red
350 checkPixel(buf, 0, 0, 191, 63, 63);
351 checkPixel(buf, 1, 0, 191, 63, 63);
352 checkPixel(buf, 0, 1, 191, 63, 63);
353 checkPixel(buf, 1, 1, 191, 63, 63);
354
355 // One-right square is bright green
356 checkPixel(buf, blockWidth, 0, 63, 191, 63);
357 checkPixel(buf, blockWidth + 1, 0, 63, 191, 63);
358 checkPixel(buf, blockWidth, 1, 63, 191, 63);
359 checkPixel(buf, blockWidth + 1, 1, 63, 191, 63);
360
361 // One-down square is bright green
362 checkPixel(buf, 0, blockHeight, 63, 191, 63);
363 checkPixel(buf, 1, blockHeight, 63, 191, 63);
364 checkPixel(buf, 0, blockHeight + 1, 63, 191, 63);
365 checkPixel(buf, 1, blockHeight + 1, 63, 191, 63);
366
367 // One-diag square is bright blue
368 checkPixel(buf, blockWidth, blockHeight, 63, 63, 191);
369 checkPixel(buf, blockWidth + 1, blockHeight, 63, 63, 191);
370 checkPixel(buf, blockWidth, blockHeight + 1, 63, 63, 191);
371 checkPixel(buf, blockWidth + 1, blockHeight + 1, 63, 63, 191);
372
373 // Test bottom-right pixel
374 {
375 const int maxBlockX = ((w-1) / blockWidth);
376 const int maxBlockY = ((h-1) / blockHeight);
377 uint8_t r = chooseColorRgba8888(maxBlockX, maxBlockY, 0);
378 uint8_t g = chooseColorRgba8888(maxBlockX, maxBlockY, 1);
379 uint8_t b = chooseColorRgba8888(maxBlockX, maxBlockY, 2);
380 checkPixel(buf, w-1, h-1, r, g, b);
381 }
382}
383
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700384void checkBayerRawBuffer(const CpuConsumer::LockedBuffer &buf) {
385 uint32_t w = buf.width;
386 uint32_t h = buf.height;
387 const int blockWidth = (w > 16 ? w / 8 : 2) & ~0x1;
388 const int blockHeight = (h > 16 ? h / 8 : 2) & ~0x1;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700389
390 // Top-left square is red
391 checkPixel(buf, 0, 0, 1000, 200, 200);
392 checkPixel(buf, 1, 0, 1000, 200, 200);
393 checkPixel(buf, 0, 1, 1000, 200, 200);
394 checkPixel(buf, 1, 1, 1000, 200, 200);
395
396 // One-right square is blue
397 checkPixel(buf, blockWidth, 0, 200, 200, 1000);
398 checkPixel(buf, blockWidth + 1, 0, 200, 200, 1000);
399 checkPixel(buf, blockWidth, 1, 200, 200, 1000);
400 checkPixel(buf, blockWidth + 1, 1, 200, 200, 1000);
401
402 // One-down square is green
403 checkPixel(buf, 0, blockHeight, 200, 1000, 200);
404 checkPixel(buf, 1, blockHeight, 200, 1000, 200);
405 checkPixel(buf, 0, blockHeight + 1, 200, 1000, 200);
406 checkPixel(buf, 1, blockHeight + 1, 200, 1000, 200);
407
408 // One-diag square is white
409 checkPixel(buf, blockWidth, blockHeight, 1000, 1000, 1000);
410 checkPixel(buf, blockWidth + 1, blockHeight, 1000, 1000, 1000);
411 checkPixel(buf, blockWidth, blockHeight + 1, 1000, 1000, 1000);
412 checkPixel(buf, blockWidth + 1, blockHeight + 1, 1000, 1000, 1000);
413
414 // Test bottom-right pixel
415 const int maxBlockX = ((w-1) / blockWidth) & 0x1;
416 const int maxBlockY = ((w-1) / blockHeight) & 0x1;
417 unsigned short maxR = (maxBlockX == maxBlockY) ? 1000 : 200;
418 unsigned short maxG = maxBlockY ? 1000: 200;
419 unsigned short maxB = maxBlockX ? 1000: 200;
420 checkPixel(buf, w-1, h-1, maxR, maxG, maxB);
421}
422
Igor Murashkin29e20472013-02-05 17:54:32 -0800423void checkAnyBuffer(const CpuConsumer::LockedBuffer &buf, int format) {
424 switch (format) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800425 case HAL_PIXEL_FORMAT_RAW16:
Igor Murashkin29e20472013-02-05 17:54:32 -0800426 checkBayerRawBuffer(buf);
427 break;
428 case HAL_PIXEL_FORMAT_Y8:
429 checkGreyscaleBuffer<uint8_t>(buf);
430 break;
431 case HAL_PIXEL_FORMAT_Y16:
432 checkGreyscaleBuffer<uint16_t>(buf);
433 break;
434 case HAL_PIXEL_FORMAT_RGBA_8888:
435 checkRgba8888Buffer(buf);
436 break;
437 }
438}
439
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700440// Configures the ANativeWindow producer-side interface based on test parameters
441void configureANW(const sp<ANativeWindow>& anw,
442 const CpuConsumerTestParams& params,
443 int maxBufferSlack) {
444 status_t err;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700445 err = native_window_api_connect(anw.get(), NATIVE_WINDOW_API_CPU);
446 ASSERT_NO_ERROR(err, "connect error: ");
447
Dan Stozaf8cebe52015-04-20 12:09:38 -0700448 err = native_window_set_buffers_dimensions(anw.get(),
449 params.width, params.height);
450 ASSERT_NO_ERROR(err, "set_buffers_dimensions error: ");
451
452 err = native_window_set_buffers_format(anw.get(), params.format);
453 ASSERT_NO_ERROR(err, "set_buffers_format error: ");
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700454
455 err = native_window_set_usage(anw.get(),
456 GRALLOC_USAGE_SW_WRITE_OFTEN);
457 ASSERT_NO_ERROR(err, "set_usage error: ");
458
459 int minUndequeuedBuffers;
460 err = anw.get()->query(anw.get(),
461 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
462 &minUndequeuedBuffers);
463 ASSERT_NO_ERROR(err, "query error: ");
464
465 ALOGVV("Setting buffer count to %d",
466 maxBufferSlack + 1 + minUndequeuedBuffers);
467 err = native_window_set_buffer_count(anw.get(),
468 maxBufferSlack + 1 + minUndequeuedBuffers);
469 ASSERT_NO_ERROR(err, "set_buffer_count error: ");
470
471}
472
473// Produce one frame of image data; assumes format and resolution configuration
474// is already done.
475void produceOneFrame(const sp<ANativeWindow>& anw,
476 const CpuConsumerTestParams& params,
477 int64_t timestamp, uint32_t *stride) {
478 status_t err;
479 ANativeWindowBuffer* anb;
480 ALOGVV("Dequeue buffer from %p", anw.get());
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700481 err = native_window_dequeue_buffer_and_wait(anw.get(), &anb);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700482 ASSERT_NO_ERROR(err, "dequeueBuffer error: ");
483
Yi Konga03e0442018-07-17 11:16:57 -0700484 ASSERT_TRUE(anb != nullptr);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700485
Mathias Agopianf543e5a2017-04-03 17:16:41 -0700486 sp<GraphicBuffer> buf(GraphicBuffer::from(anb));
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700487
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700488 *stride = buf->getStride();
Yi Konga03e0442018-07-17 11:16:57 -0700489 uint8_t* img = nullptr;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700490
491 ALOGVV("Lock buffer from %p for write", anw.get());
492 err = buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
493 ASSERT_NO_ERROR(err, "lock error: ");
494
495 switch (params.format) {
496 case HAL_PIXEL_FORMAT_YV12:
497 fillYV12Buffer(img, params.width, params.height, *stride);
498 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800499 case HAL_PIXEL_FORMAT_RAW16:
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700500 fillBayerRawBuffer(img, params.width, params.height, buf->getStride());
501 break;
Igor Murashkin29e20472013-02-05 17:54:32 -0800502 case HAL_PIXEL_FORMAT_Y8:
503 fillGreyscaleBuffer<uint8_t>(img, params.width, params.height,
504 buf->getStride(), /*bpp*/8);
505 break;
506 case HAL_PIXEL_FORMAT_Y16:
507 fillGreyscaleBuffer<uint16_t>((uint16_t*)img, params.width,
508 params.height, buf->getStride(),
509 /*bpp*/16);
510 break;
511 case HAL_PIXEL_FORMAT_RGBA_8888:
512 fillRgba8888Buffer(img, params.width, params.height, buf->getStride());
513 break;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700514 default:
515 FAIL() << "Unknown pixel format under test!";
516 break;
517 }
518 ALOGVV("Unlock buffer from %p", anw.get());
519 err = buf->unlock();
520 ASSERT_NO_ERROR(err, "unlock error: ");
521
522 ALOGVV("Set timestamp to %p", anw.get());
523 err = native_window_set_buffers_timestamp(anw.get(), timestamp);
524 ASSERT_NO_ERROR(err, "set_buffers_timestamp error: ");
525
526 ALOGVV("Queue buffer to %p", anw.get());
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700527 err = anw->queueBuffer(anw.get(), buf->getNativeBuffer(), -1);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700528 ASSERT_NO_ERROR(err, "queueBuffer error:");
529};
530
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800531// This test is disabled because the HAL_PIXEL_FORMAT_RAW16 format is not
Jamie Gennisfdb6b492012-08-31 14:44:50 -0700532// supported on all devices.
Igor Murashkin29e20472013-02-05 17:54:32 -0800533TEST_P(CpuConsumerTest, FromCpuSingle) {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700534 status_t err;
535 CpuConsumerTestParams params = GetParam();
536
537 // Set up
538
539 ASSERT_NO_FATAL_FAILURE(configureANW(mANW, params, 1));
540
541 // Produce
542
543 const int64_t time = 12345678L;
544 uint32_t stride;
545 ASSERT_NO_FATAL_FAILURE(produceOneFrame(mANW, params, time,
546 &stride));
547
548 // Consume
549
550 CpuConsumer::LockedBuffer b;
551 err = mCC->lockNextBuffer(&b);
552 ASSERT_NO_ERROR(err, "getNextBuffer error: ");
553
Yi Konga03e0442018-07-17 11:16:57 -0700554 ASSERT_TRUE(b.data != nullptr);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700555 EXPECT_EQ(params.width, b.width);
556 EXPECT_EQ(params.height, b.height);
557 EXPECT_EQ(params.format, b.format);
558 EXPECT_EQ(stride, b.stride);
559 EXPECT_EQ(time, b.timestamp);
560
Igor Murashkin29e20472013-02-05 17:54:32 -0800561 checkAnyBuffer(b, GetParam().format);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700562 mCC->unlockBuffer(b);
563}
564
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800565// This test is disabled because the HAL_PIXEL_FORMAT_RAW16 format is not
Jamie Gennisfdb6b492012-08-31 14:44:50 -0700566// supported on all devices.
Igor Murashkin29e20472013-02-05 17:54:32 -0800567TEST_P(CpuConsumerTest, FromCpuManyInQueue) {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700568 status_t err;
569 CpuConsumerTestParams params = GetParam();
570
571 const int numInQueue = 5;
572 // Set up
573
574 ASSERT_NO_FATAL_FAILURE(configureANW(mANW, params, numInQueue));
575
576 // Produce
577
578 const int64_t time[numInQueue] = { 1L, 2L, 3L, 4L, 5L};
579 uint32_t stride[numInQueue];
580
581 for (int i = 0; i < numInQueue; i++) {
Nolan Scobief50aebc2023-04-13 17:49:18 +0000582 ALOGD("Producing frame %d", i);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700583 ASSERT_NO_FATAL_FAILURE(produceOneFrame(mANW, params, time[i],
584 &stride[i]));
585 }
586
587 // Consume
588
589 for (int i = 0; i < numInQueue; i++) {
Nolan Scobief50aebc2023-04-13 17:49:18 +0000590 ALOGD("Consuming frame %d", i);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700591 CpuConsumer::LockedBuffer b;
592 err = mCC->lockNextBuffer(&b);
593 ASSERT_NO_ERROR(err, "getNextBuffer error: ");
594
Yi Konga03e0442018-07-17 11:16:57 -0700595 ASSERT_TRUE(b.data != nullptr);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700596 EXPECT_EQ(params.width, b.width);
597 EXPECT_EQ(params.height, b.height);
598 EXPECT_EQ(params.format, b.format);
599 EXPECT_EQ(stride[i], b.stride);
600 EXPECT_EQ(time[i], b.timestamp);
601
Igor Murashkin29e20472013-02-05 17:54:32 -0800602 checkAnyBuffer(b, GetParam().format);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700603
604 mCC->unlockBuffer(b);
605 }
606}
607
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800608// This test is disabled because the HAL_PIXEL_FORMAT_RAW16 format is not
Jamie Gennisfdb6b492012-08-31 14:44:50 -0700609// supported on all devices.
Igor Murashkin29e20472013-02-05 17:54:32 -0800610TEST_P(CpuConsumerTest, FromCpuLockMax) {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700611 status_t err;
612 CpuConsumerTestParams params = GetParam();
613
614 // Set up
615
616 ASSERT_NO_FATAL_FAILURE(configureANW(mANW, params, params.maxLockedBuffers + 1));
617
618 // Produce
619
620 const int64_t time = 1234L;
621 uint32_t stride;
622
623 for (int i = 0; i < params.maxLockedBuffers + 1; i++) {
Nolan Scobief50aebc2023-04-13 17:49:18 +0000624 ALOGD("Producing frame %d", i);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700625 ASSERT_NO_FATAL_FAILURE(produceOneFrame(mANW, params, time,
626 &stride));
627 }
628
629 // Consume
630
Manoj Guptae8278ac2017-07-18 15:57:14 -0700631 std::vector<CpuConsumer::LockedBuffer> b(params.maxLockedBuffers);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700632 for (int i = 0; i < params.maxLockedBuffers; i++) {
Nolan Scobief50aebc2023-04-13 17:49:18 +0000633 ALOGD("Locking frame %d", i);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700634 err = mCC->lockNextBuffer(&b[i]);
635 ASSERT_NO_ERROR(err, "getNextBuffer error: ");
636
Yi Konga03e0442018-07-17 11:16:57 -0700637 ASSERT_TRUE(b[i].data != nullptr);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700638 EXPECT_EQ(params.width, b[i].width);
639 EXPECT_EQ(params.height, b[i].height);
640 EXPECT_EQ(params.format, b[i].format);
641 EXPECT_EQ(stride, b[i].stride);
642 EXPECT_EQ(time, b[i].timestamp);
643
Igor Murashkin29e20472013-02-05 17:54:32 -0800644 checkAnyBuffer(b[i], GetParam().format);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700645 }
646
Nolan Scobief50aebc2023-04-13 17:49:18 +0000647 ALOGD("Locking frame %d (too many)", params.maxLockedBuffers);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700648 CpuConsumer::LockedBuffer bTooMuch;
649 err = mCC->lockNextBuffer(&bTooMuch);
Alistair Strachan8f76b6b2013-12-04 14:55:01 -0800650 ASSERT_TRUE(err == NOT_ENOUGH_DATA) << "Allowing too many locks";
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700651
Nolan Scobief50aebc2023-04-13 17:49:18 +0000652 ALOGD("Unlocking frame 0");
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700653 err = mCC->unlockBuffer(b[0]);
654 ASSERT_NO_ERROR(err, "Could not unlock buffer 0: ");
655
Nolan Scobief50aebc2023-04-13 17:49:18 +0000656 ALOGD("Locking frame %d (should work now)", params.maxLockedBuffers);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700657 err = mCC->lockNextBuffer(&bTooMuch);
658 ASSERT_NO_ERROR(err, "Did not allow new lock after unlock");
659
Yi Konga03e0442018-07-17 11:16:57 -0700660 ASSERT_TRUE(bTooMuch.data != nullptr);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700661 EXPECT_EQ(params.width, bTooMuch.width);
662 EXPECT_EQ(params.height, bTooMuch.height);
663 EXPECT_EQ(params.format, bTooMuch.format);
664 EXPECT_EQ(stride, bTooMuch.stride);
665 EXPECT_EQ(time, bTooMuch.timestamp);
666
Igor Murashkin29e20472013-02-05 17:54:32 -0800667 checkAnyBuffer(bTooMuch, GetParam().format);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700668
Nolan Scobief50aebc2023-04-13 17:49:18 +0000669 ALOGD("Unlocking extra buffer");
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700670 err = mCC->unlockBuffer(bTooMuch);
671 ASSERT_NO_ERROR(err, "Could not unlock extra buffer: ");
672
Nolan Scobief50aebc2023-04-13 17:49:18 +0000673 ALOGD("Locking frame %d (no more available)", params.maxLockedBuffers + 1);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700674 err = mCC->lockNextBuffer(&b[0]);
675 ASSERT_EQ(BAD_VALUE, err) << "Not out of buffers somehow";
676
677 for (int i = 1; i < params.maxLockedBuffers; i++) {
678 mCC->unlockBuffer(b[i]);
679 }
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700680}
681
Chia-I Wucacfcc62017-11-16 14:54:07 -0800682TEST_P(CpuConsumerTest, FromCpuInvalid) {
683 status_t err = mCC->lockNextBuffer(nullptr);
684 ASSERT_EQ(BAD_VALUE, err) << "lockNextBuffer did not fail";
685
686 CpuConsumer::LockedBuffer b;
687 err = mCC->unlockBuffer(b);
688 ASSERT_EQ(BAD_VALUE, err) << "unlockBuffer did not fail";
689}
690
Chia-I Wu96ad8222017-11-16 15:41:45 -0800691TEST_P(CpuConsumerTest, FromCpuMultiThread) {
692 CpuConsumerTestParams params = GetParam();
693 ASSERT_NO_FATAL_FAILURE(configureANW(mANW, params, params.maxLockedBuffers + 1));
694
695 for (int i = 0; i < 10; i++) {
696 std::atomic<int> threadReadyCount(0);
697 auto lockAndUnlock = [&]() {
698 threadReadyCount++;
699 // busy wait
700 while (threadReadyCount < params.maxLockedBuffers + 1);
701
702 CpuConsumer::LockedBuffer b;
703 status_t err = mCC->lockNextBuffer(&b);
704 if (err == NO_ERROR) {
705 usleep(1000);
706 err = mCC->unlockBuffer(b);
707 ASSERT_NO_ERROR(err, "Could not unlock buffer: ");
708 } else if (err == NOT_ENOUGH_DATA) {
709 // there are params.maxLockedBuffers+1 threads so one of the
710 // threads might get this error
711 } else {
712 FAIL() << "Could not lock buffer";
713 }
714 };
715
716 // produce buffers
717 for (int j = 0; j < params.maxLockedBuffers + 1; j++) {
718 const int64_t time = 1234L;
719 uint32_t stride;
720 ASSERT_NO_FATAL_FAILURE(produceOneFrame(mANW, params, time, &stride));
721 }
722
723 // spawn threads
724 std::vector<std::thread> threads;
725 for (int j = 0; j < params.maxLockedBuffers + 1; j++) {
726 threads.push_back(std::thread(lockAndUnlock));
727 }
728
729 // join threads
730 for (auto& thread : threads) {
731 thread.join();
732 }
733
734 // we produced N+1 buffers, but the threads might only consume N
735 CpuConsumer::LockedBuffer b;
736 if (mCC->lockNextBuffer(&b) == NO_ERROR) {
737 mCC->unlockBuffer(b);
738 }
739
740 if (HasFatalFailure()) {
741 break;
742 }
743 }
744}
745
Igor Murashkin29e20472013-02-05 17:54:32 -0800746CpuConsumerTestParams y8TestSets[] = {
747 { 512, 512, 1, HAL_PIXEL_FORMAT_Y8},
748 { 512, 512, 3, HAL_PIXEL_FORMAT_Y8},
749 { 2608, 1960, 1, HAL_PIXEL_FORMAT_Y8},
750 { 2608, 1960, 3, HAL_PIXEL_FORMAT_Y8},
751 { 100, 100, 1, HAL_PIXEL_FORMAT_Y8},
752 { 100, 100, 3, HAL_PIXEL_FORMAT_Y8},
753};
754
755CpuConsumerTestParams y16TestSets[] = {
756 { 512, 512, 1, HAL_PIXEL_FORMAT_Y16},
757 { 512, 512, 3, HAL_PIXEL_FORMAT_Y16},
758 { 2608, 1960, 1, HAL_PIXEL_FORMAT_Y16},
759 { 2608, 1960, 3, HAL_PIXEL_FORMAT_Y16},
760 { 100, 100, 1, HAL_PIXEL_FORMAT_Y16},
761 { 100, 100, 3, HAL_PIXEL_FORMAT_Y16},
762};
763
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700764CpuConsumerTestParams rawTestSets[] = {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800765 { 512, 512, 1, HAL_PIXEL_FORMAT_RAW16},
766 { 512, 512, 3, HAL_PIXEL_FORMAT_RAW16},
767 { 2608, 1960, 1, HAL_PIXEL_FORMAT_RAW16},
768 { 2608, 1960, 3, HAL_PIXEL_FORMAT_RAW16},
769 { 100, 100, 1, HAL_PIXEL_FORMAT_RAW16},
770 { 100, 100, 3, HAL_PIXEL_FORMAT_RAW16},
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700771};
772
Igor Murashkin29e20472013-02-05 17:54:32 -0800773CpuConsumerTestParams rgba8888TestSets[] = {
774 { 512, 512, 1, HAL_PIXEL_FORMAT_RGBA_8888},
775 { 512, 512, 3, HAL_PIXEL_FORMAT_RGBA_8888},
776 { 2608, 1960, 1, HAL_PIXEL_FORMAT_RGBA_8888},
777 { 2608, 1960, 3, HAL_PIXEL_FORMAT_RGBA_8888},
778 { 100, 100, 1, HAL_PIXEL_FORMAT_RGBA_8888},
779 { 100, 100, 3, HAL_PIXEL_FORMAT_RGBA_8888},
780};
781
782#if CPU_CONSUMER_TEST_FORMAT_Y8
783INSTANTIATE_TEST_CASE_P(Y8Tests,
784 CpuConsumerTest,
785 ::testing::ValuesIn(y8TestSets));
786#endif
787
788#if CPU_CONSUMER_TEST_FORMAT_Y16
789INSTANTIATE_TEST_CASE_P(Y16Tests,
790 CpuConsumerTest,
791 ::testing::ValuesIn(y16TestSets));
792#endif
793
794#if CPU_CONSUMER_TEST_FORMAT_RAW
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700795INSTANTIATE_TEST_CASE_P(RawTests,
796 CpuConsumerTest,
797 ::testing::ValuesIn(rawTestSets));
Igor Murashkin29e20472013-02-05 17:54:32 -0800798#endif
799
800#if CPU_CONSUMER_TEST_FORMAT_RGBA_8888
801INSTANTIATE_TEST_CASE_P(Rgba8888Tests,
802 CpuConsumerTest,
803 ::testing::ValuesIn(rgba8888TestSets));
804#endif
805
806
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700807
808} // namespace android