blob: 4ef82d8ad8c4cafbb56026c0b3a8315800409e70 [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
Manoj Guptae8278ac2017-07-18 15:57:14 -070036#include <vector>
Igor Murashkin29e20472013-02-05 17:54:32 -080037#define CPU_CONSUMER_TEST_FORMAT_RAW 0
38#define CPU_CONSUMER_TEST_FORMAT_Y8 0
39#define CPU_CONSUMER_TEST_FORMAT_Y16 0
40#define CPU_CONSUMER_TEST_FORMAT_RGBA_8888 1
41
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070042namespace android {
43
44struct CpuConsumerTestParams {
45 uint32_t width;
46 uint32_t height;
47 int maxLockedBuffers;
48 PixelFormat format;
49};
50
51::std::ostream& operator<<(::std::ostream& os, const CpuConsumerTestParams& p) {
52 return os << "[ (" << p.width << ", " << p.height << "), B:"
53 << p.maxLockedBuffers << ", F:0x"
54 << ::std::hex << p.format << "]";
55}
56
57class CpuConsumerTest : public ::testing::TestWithParam<CpuConsumerTestParams> {
58protected:
59
60 virtual void SetUp() {
61 const ::testing::TestInfo* const test_info =
62 ::testing::UnitTest::GetInstance()->current_test_info();
63 CpuConsumerTestParams params = GetParam();
64 ALOGV("** Starting test %s (%d x %d, %d, 0x%x)",
65 test_info->name(),
66 params.width, params.height,
67 params.maxLockedBuffers, params.format);
Dan Stoza5603a2f2014-04-07 13:41:37 -070068 sp<IGraphicBufferProducer> producer;
69 sp<IGraphicBufferConsumer> consumer;
70 BufferQueue::createBufferQueue(&producer, &consumer);
71 mCC = new CpuConsumer(consumer, params.maxLockedBuffers);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070072 String8 name("CpuConsumer_Under_Test");
73 mCC->setName(name);
Dan Stoza5603a2f2014-04-07 13:41:37 -070074 mSTC = new Surface(producer);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070075 mANW = mSTC;
76 }
77
78 virtual void TearDown() {
79 mANW.clear();
80 mSTC.clear();
81 mCC.clear();
82 }
83
84 class FrameWaiter : public CpuConsumer::FrameAvailableListener {
85 public:
86 FrameWaiter():
87 mPendingFrames(0) {
88 }
89
90 void waitForFrame() {
91 Mutex::Autolock lock(mMutex);
92 while (mPendingFrames == 0) {
93 mCondition.wait(mMutex);
94 }
95 mPendingFrames--;
96 }
97
Dan Stozaf8cebe52015-04-20 12:09:38 -070098 virtual void onFrameAvailable(const BufferItem&) {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070099 Mutex::Autolock lock(mMutex);
100 mPendingFrames++;
101 mCondition.signal();
102 }
103
104 int mPendingFrames;
105 Mutex mMutex;
106 Condition mCondition;
107 };
108
109 // Note that SurfaceTexture will lose the notifications
110 // onBuffersReleased and onFrameAvailable as there is currently
111 // no way to forward the events. This DisconnectWaiter will not let the
112 // disconnect finish until finishDisconnect() is called. It will
113 // also block until a disconnect is called
114 class DisconnectWaiter : public BufferQueue::ConsumerListener {
115 public:
116 DisconnectWaiter () :
117 mWaitForDisconnect(false),
118 mPendingFrames(0) {
119 }
120
121 void waitForFrame() {
122 Mutex::Autolock lock(mMutex);
123 while (mPendingFrames == 0) {
124 mFrameCondition.wait(mMutex);
125 }
126 mPendingFrames--;
127 }
128
Dan Stozaf8cebe52015-04-20 12:09:38 -0700129 virtual void onFrameAvailable(const BufferItem&) {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700130 Mutex::Autolock lock(mMutex);
131 mPendingFrames++;
132 mFrameCondition.signal();
133 }
134
135 virtual void onBuffersReleased() {
136 Mutex::Autolock lock(mMutex);
137 while (!mWaitForDisconnect) {
138 mDisconnectCondition.wait(mMutex);
139 }
140 }
141
142 void finishDisconnect() {
143 Mutex::Autolock lock(mMutex);
144 mWaitForDisconnect = true;
145 mDisconnectCondition.signal();
146 }
147
148 private:
149 Mutex mMutex;
150
151 bool mWaitForDisconnect;
152 Condition mDisconnectCondition;
153
154 int mPendingFrames;
155 Condition mFrameCondition;
156 };
157
158 sp<CpuConsumer> mCC;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800159 sp<Surface> mSTC;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700160 sp<ANativeWindow> mANW;
161};
162
163#define ASSERT_NO_ERROR(err, msg) \
Chih-Hung Hsieh7c5f1092016-05-20 11:46:52 -0700164 ASSERT_EQ(NO_ERROR, err) << (msg) << strerror(-(err))
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700165
166void checkPixel(const CpuConsumer::LockedBuffer &buf,
Igor Murashkin29e20472013-02-05 17:54:32 -0800167 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 -0700168 // Ignores components that don't exist for given pixel
169 switch(buf.format) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800170 case HAL_PIXEL_FORMAT_RAW16: {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700171 String8 msg;
172 uint16_t *bPtr = (uint16_t*)buf.data;
173 bPtr += y * buf.stride + x;
174 // GRBG Bayer mosaic; only check the matching channel
175 switch( ((y & 1) << 1) | (x & 1) ) {
176 case 0: // G
177 case 3: // G
178 EXPECT_EQ(g, *bPtr);
179 break;
180 case 1: // R
181 EXPECT_EQ(r, *bPtr);
182 break;
183 case 2: // B
184 EXPECT_EQ(b, *bPtr);
185 break;
186 }
187 break;
188 }
Igor Murashkin29e20472013-02-05 17:54:32 -0800189 // ignores g,b
190 case HAL_PIXEL_FORMAT_Y8: {
191 uint8_t *bPtr = (uint8_t*)buf.data;
192 bPtr += y * buf.stride + x;
193 EXPECT_EQ(r, *bPtr) << "at x = " << x << " y = " << y;
194 break;
195 }
196 // ignores g,b
197 case HAL_PIXEL_FORMAT_Y16: {
198 // stride is in pixels, not in bytes
199 uint16_t *bPtr = ((uint16_t*)buf.data) + y * buf.stride + x;
200
201 EXPECT_EQ(r, *bPtr) << "at x = " << x << " y = " << y;
202 break;
203 }
204 case HAL_PIXEL_FORMAT_RGBA_8888: {
205 const int bytesPerPixel = 4;
206 uint8_t *bPtr = (uint8_t*)buf.data;
207 bPtr += (y * buf.stride + x) * bytesPerPixel;
208
209 EXPECT_EQ(r, bPtr[0]) << "at x = " << x << " y = " << y;
210 EXPECT_EQ(g, bPtr[1]) << "at x = " << x << " y = " << y;
211 EXPECT_EQ(b, bPtr[2]) << "at x = " << x << " y = " << y;
212 break;
213 }
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700214 default: {
215 ADD_FAILURE() << "Unknown format for check:" << buf.format;
216 break;
217 }
218 }
219}
220
221// Fill a YV12 buffer with a multi-colored checkerboard pattern
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700222void fillYV12Buffer(uint8_t* buf, int w, int h, int stride);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700223
Igor Murashkin29e20472013-02-05 17:54:32 -0800224// Fill a Y8/Y16 buffer with a multi-colored checkerboard pattern
225template <typename T> // T == uint8_t or uint16_t
226void fillGreyscaleBuffer(T* buf, int w, int h, int stride, int bpp) {
227 const int blockWidth = w > 16 ? w / 16 : 1;
228 const int blockHeight = h > 16 ? h / 16 : 1;
229 const int yuvTexOffsetY = 0;
230
231 ASSERT_TRUE(bpp == 8 || bpp == 16);
232 ASSERT_TRUE(sizeof(T)*8 == bpp);
233
234 // stride is in pixels, not in bytes
235 int yuvTexStrideY = stride;
236 for (int x = 0; x < w; x++) {
237 for (int y = 0; y < h; y++) {
238 int parityX = (x / blockWidth) & 1;
239 int parityY = (y / blockHeight) & 1;
240 T intensity = (parityX ^ parityY) ? 63 : 191;
241 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
242 }
243 }
244}
245
246inline uint8_t chooseColorRgba8888(int blockX, int blockY, uint8_t channel) {
247 const int colorVariations = 3;
248 uint8_t color = ((blockX % colorVariations) + (blockY % colorVariations))
249 % (colorVariations) == channel ? 191: 63;
250
251 return color;
252}
253
254// Fill a RGBA8888 buffer with a multi-colored checkerboard pattern
255void fillRgba8888Buffer(uint8_t* buf, int w, int h, int stride)
256{
257 const int blockWidth = w > 16 ? w / 16 : 1;
258 const int blockHeight = h > 16 ? h / 16 : 1;
259 const int bytesPerPixel = 4;
260
261 // stride is in pixels, not in bytes
262 for (int x = 0; x < w; ++x) {
263 for (int y = 0; y < h; ++y) {
264 int blockX = (x / blockWidth);
265 int blockY = (y / blockHeight);
266
267 uint8_t r = chooseColorRgba8888(blockX, blockY, 0);
268 uint8_t g = chooseColorRgba8888(blockX, blockY, 1);
269 uint8_t b = chooseColorRgba8888(blockX, blockY, 2);
270
271 buf[(y*stride + x)*bytesPerPixel + 0] = r;
272 buf[(y*stride + x)*bytesPerPixel + 1] = g;
273 buf[(y*stride + x)*bytesPerPixel + 2] = b;
274 buf[(y*stride + x)*bytesPerPixel + 3] = 255;
275 }
276 }
277}
278
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700279// Fill a RAW sensor buffer with a multi-colored checkerboard pattern.
280// Assumes GRBG mosaic ordering. Result should be a grid in a 2x2 pattern
281// of [ R, B; G, W]
282void fillBayerRawBuffer(uint8_t* buf, int w, int h, int stride) {
283 ALOGVV("fillBayerRawBuffer: %p with %d x %d, stride %d", buf, w, h ,stride);
284 // Blocks need to be even-width/height, aim for 8-wide otherwise
285 const int blockWidth = (w > 16 ? w / 8 : 2) & ~0x1;
286 const int blockHeight = (h > 16 ? h / 8 : 2) & ~0x1;
287 for (int y = 0; y < h; y+=2) {
288 uint16_t *bPtr1 = ((uint16_t*)buf) + stride*y;
289 uint16_t *bPtr2 = bPtr1 + stride;
290 for (int x = 0; x < w; x+=2) {
291 int blockX = (x / blockWidth ) & 1;
292 int blockY = (y / blockHeight) & 1;
293 unsigned short r = (blockX == blockY) ? 1000 : 200;
294 unsigned short g = blockY ? 1000: 200;
295 unsigned short b = blockX ? 1000: 200;
296 // GR row
297 *bPtr1++ = g;
298 *bPtr1++ = r;
299 // BG row
300 *bPtr2++ = b;
301 *bPtr2++ = g;
302 }
303 }
304
305}
306
Igor Murashkin29e20472013-02-05 17:54:32 -0800307template<typename T> // uint8_t or uint16_t
308void checkGreyscaleBuffer(const CpuConsumer::LockedBuffer &buf) {
309 uint32_t w = buf.width;
310 uint32_t h = buf.height;
311 const int blockWidth = w > 16 ? w / 16 : 1;
312 const int blockHeight = h > 16 ? h / 16 : 1;
Igor Murashkin29e20472013-02-05 17:54:32 -0800313
314 // Top-left square is bright
315 checkPixel(buf, 0, 0, 191);
316 checkPixel(buf, 1, 0, 191);
317 checkPixel(buf, 0, 1, 191);
318 checkPixel(buf, 1, 1, 191);
319
320 // One-right square is dark
321 checkPixel(buf, blockWidth, 0, 63);
322 checkPixel(buf, blockWidth + 1, 0, 63);
323 checkPixel(buf, blockWidth, 1, 63);
324 checkPixel(buf, blockWidth + 1, 1, 63);
325
326 // One-down square is dark
327 checkPixel(buf, 0, blockHeight, 63);
328 checkPixel(buf, 1, blockHeight, 63);
329 checkPixel(buf, 0, blockHeight + 1, 63);
330 checkPixel(buf, 1, blockHeight + 1, 63);
331
332 // One-diag square is bright
333 checkPixel(buf, blockWidth, blockHeight, 191);
334 checkPixel(buf, blockWidth + 1, blockHeight, 191);
335 checkPixel(buf, blockWidth, blockHeight + 1, 191);
336 checkPixel(buf, blockWidth + 1, blockHeight + 1, 191);
337
338 // Test bottom-right pixel
339 const int maxBlockX = ((w-1 + (blockWidth-1)) / blockWidth) & 0x1;
340 const int maxBlockY = ((h-1 + (blockHeight-1)) / blockHeight) & 0x1;
341 uint32_t pixelValue = ((maxBlockX % 2) == (maxBlockY % 2)) ? 191 : 63;
342 checkPixel(buf, w-1, h-1, pixelValue);
343}
344
345void checkRgba8888Buffer(const CpuConsumer::LockedBuffer &buf) {
346 uint32_t w = buf.width;
347 uint32_t h = buf.height;
348 const int blockWidth = w > 16 ? w / 16 : 1;
349 const int blockHeight = h > 16 ? h / 16 : 1;
Igor Murashkin29e20472013-02-05 17:54:32 -0800350
351 // Top-left square is bright red
352 checkPixel(buf, 0, 0, 191, 63, 63);
353 checkPixel(buf, 1, 0, 191, 63, 63);
354 checkPixel(buf, 0, 1, 191, 63, 63);
355 checkPixel(buf, 1, 1, 191, 63, 63);
356
357 // One-right square is bright green
358 checkPixel(buf, blockWidth, 0, 63, 191, 63);
359 checkPixel(buf, blockWidth + 1, 0, 63, 191, 63);
360 checkPixel(buf, blockWidth, 1, 63, 191, 63);
361 checkPixel(buf, blockWidth + 1, 1, 63, 191, 63);
362
363 // One-down square is bright green
364 checkPixel(buf, 0, blockHeight, 63, 191, 63);
365 checkPixel(buf, 1, blockHeight, 63, 191, 63);
366 checkPixel(buf, 0, blockHeight + 1, 63, 191, 63);
367 checkPixel(buf, 1, blockHeight + 1, 63, 191, 63);
368
369 // One-diag square is bright blue
370 checkPixel(buf, blockWidth, blockHeight, 63, 63, 191);
371 checkPixel(buf, blockWidth + 1, blockHeight, 63, 63, 191);
372 checkPixel(buf, blockWidth, blockHeight + 1, 63, 63, 191);
373 checkPixel(buf, blockWidth + 1, blockHeight + 1, 63, 63, 191);
374
375 // Test bottom-right pixel
376 {
377 const int maxBlockX = ((w-1) / blockWidth);
378 const int maxBlockY = ((h-1) / blockHeight);
379 uint8_t r = chooseColorRgba8888(maxBlockX, maxBlockY, 0);
380 uint8_t g = chooseColorRgba8888(maxBlockX, maxBlockY, 1);
381 uint8_t b = chooseColorRgba8888(maxBlockX, maxBlockY, 2);
382 checkPixel(buf, w-1, h-1, r, g, b);
383 }
384}
385
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700386void checkBayerRawBuffer(const CpuConsumer::LockedBuffer &buf) {
387 uint32_t w = buf.width;
388 uint32_t h = buf.height;
389 const int blockWidth = (w > 16 ? w / 8 : 2) & ~0x1;
390 const int blockHeight = (h > 16 ? h / 8 : 2) & ~0x1;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700391
392 // Top-left square is red
393 checkPixel(buf, 0, 0, 1000, 200, 200);
394 checkPixel(buf, 1, 0, 1000, 200, 200);
395 checkPixel(buf, 0, 1, 1000, 200, 200);
396 checkPixel(buf, 1, 1, 1000, 200, 200);
397
398 // One-right square is blue
399 checkPixel(buf, blockWidth, 0, 200, 200, 1000);
400 checkPixel(buf, blockWidth + 1, 0, 200, 200, 1000);
401 checkPixel(buf, blockWidth, 1, 200, 200, 1000);
402 checkPixel(buf, blockWidth + 1, 1, 200, 200, 1000);
403
404 // One-down square is green
405 checkPixel(buf, 0, blockHeight, 200, 1000, 200);
406 checkPixel(buf, 1, blockHeight, 200, 1000, 200);
407 checkPixel(buf, 0, blockHeight + 1, 200, 1000, 200);
408 checkPixel(buf, 1, blockHeight + 1, 200, 1000, 200);
409
410 // One-diag square is white
411 checkPixel(buf, blockWidth, blockHeight, 1000, 1000, 1000);
412 checkPixel(buf, blockWidth + 1, blockHeight, 1000, 1000, 1000);
413 checkPixel(buf, blockWidth, blockHeight + 1, 1000, 1000, 1000);
414 checkPixel(buf, blockWidth + 1, blockHeight + 1, 1000, 1000, 1000);
415
416 // Test bottom-right pixel
417 const int maxBlockX = ((w-1) / blockWidth) & 0x1;
418 const int maxBlockY = ((w-1) / blockHeight) & 0x1;
419 unsigned short maxR = (maxBlockX == maxBlockY) ? 1000 : 200;
420 unsigned short maxG = maxBlockY ? 1000: 200;
421 unsigned short maxB = maxBlockX ? 1000: 200;
422 checkPixel(buf, w-1, h-1, maxR, maxG, maxB);
423}
424
Igor Murashkin29e20472013-02-05 17:54:32 -0800425void checkAnyBuffer(const CpuConsumer::LockedBuffer &buf, int format) {
426 switch (format) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800427 case HAL_PIXEL_FORMAT_RAW16:
Igor Murashkin29e20472013-02-05 17:54:32 -0800428 checkBayerRawBuffer(buf);
429 break;
430 case HAL_PIXEL_FORMAT_Y8:
431 checkGreyscaleBuffer<uint8_t>(buf);
432 break;
433 case HAL_PIXEL_FORMAT_Y16:
434 checkGreyscaleBuffer<uint16_t>(buf);
435 break;
436 case HAL_PIXEL_FORMAT_RGBA_8888:
437 checkRgba8888Buffer(buf);
438 break;
439 }
440}
441
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700442// Configures the ANativeWindow producer-side interface based on test parameters
443void configureANW(const sp<ANativeWindow>& anw,
444 const CpuConsumerTestParams& params,
445 int maxBufferSlack) {
446 status_t err;
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700447 err = native_window_api_connect(anw.get(), NATIVE_WINDOW_API_CPU);
448 ASSERT_NO_ERROR(err, "connect error: ");
449
Dan Stozaf8cebe52015-04-20 12:09:38 -0700450 err = native_window_set_buffers_dimensions(anw.get(),
451 params.width, params.height);
452 ASSERT_NO_ERROR(err, "set_buffers_dimensions error: ");
453
454 err = native_window_set_buffers_format(anw.get(), params.format);
455 ASSERT_NO_ERROR(err, "set_buffers_format error: ");
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700456
457 err = native_window_set_usage(anw.get(),
458 GRALLOC_USAGE_SW_WRITE_OFTEN);
459 ASSERT_NO_ERROR(err, "set_usage error: ");
460
461 int minUndequeuedBuffers;
462 err = anw.get()->query(anw.get(),
463 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
464 &minUndequeuedBuffers);
465 ASSERT_NO_ERROR(err, "query error: ");
466
467 ALOGVV("Setting buffer count to %d",
468 maxBufferSlack + 1 + minUndequeuedBuffers);
469 err = native_window_set_buffer_count(anw.get(),
470 maxBufferSlack + 1 + minUndequeuedBuffers);
471 ASSERT_NO_ERROR(err, "set_buffer_count error: ");
472
473}
474
475// Produce one frame of image data; assumes format and resolution configuration
476// is already done.
477void produceOneFrame(const sp<ANativeWindow>& anw,
478 const CpuConsumerTestParams& params,
479 int64_t timestamp, uint32_t *stride) {
480 status_t err;
481 ANativeWindowBuffer* anb;
482 ALOGVV("Dequeue buffer from %p", anw.get());
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700483 err = native_window_dequeue_buffer_and_wait(anw.get(), &anb);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700484 ASSERT_NO_ERROR(err, "dequeueBuffer error: ");
485
486 ASSERT_TRUE(anb != NULL);
487
Mathias Agopianf543e5a2017-04-03 17:16:41 -0700488 sp<GraphicBuffer> buf(GraphicBuffer::from(anb));
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700489
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700490 *stride = buf->getStride();
491 uint8_t* img = NULL;
492
493 ALOGVV("Lock buffer from %p for write", anw.get());
494 err = buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
495 ASSERT_NO_ERROR(err, "lock error: ");
496
497 switch (params.format) {
498 case HAL_PIXEL_FORMAT_YV12:
499 fillYV12Buffer(img, params.width, params.height, *stride);
500 break;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800501 case HAL_PIXEL_FORMAT_RAW16:
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700502 fillBayerRawBuffer(img, params.width, params.height, buf->getStride());
503 break;
Igor Murashkin29e20472013-02-05 17:54:32 -0800504 case HAL_PIXEL_FORMAT_Y8:
505 fillGreyscaleBuffer<uint8_t>(img, params.width, params.height,
506 buf->getStride(), /*bpp*/8);
507 break;
508 case HAL_PIXEL_FORMAT_Y16:
509 fillGreyscaleBuffer<uint16_t>((uint16_t*)img, params.width,
510 params.height, buf->getStride(),
511 /*bpp*/16);
512 break;
513 case HAL_PIXEL_FORMAT_RGBA_8888:
514 fillRgba8888Buffer(img, params.width, params.height, buf->getStride());
515 break;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700516 default:
517 FAIL() << "Unknown pixel format under test!";
518 break;
519 }
520 ALOGVV("Unlock buffer from %p", anw.get());
521 err = buf->unlock();
522 ASSERT_NO_ERROR(err, "unlock error: ");
523
524 ALOGVV("Set timestamp to %p", anw.get());
525 err = native_window_set_buffers_timestamp(anw.get(), timestamp);
526 ASSERT_NO_ERROR(err, "set_buffers_timestamp error: ");
527
528 ALOGVV("Queue buffer to %p", anw.get());
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700529 err = anw->queueBuffer(anw.get(), buf->getNativeBuffer(), -1);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700530 ASSERT_NO_ERROR(err, "queueBuffer error:");
531};
532
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800533// This test is disabled because the HAL_PIXEL_FORMAT_RAW16 format is not
Jamie Gennisfdb6b492012-08-31 14:44:50 -0700534// supported on all devices.
Igor Murashkin29e20472013-02-05 17:54:32 -0800535TEST_P(CpuConsumerTest, FromCpuSingle) {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700536 status_t err;
537 CpuConsumerTestParams params = GetParam();
538
539 // Set up
540
541 ASSERT_NO_FATAL_FAILURE(configureANW(mANW, params, 1));
542
543 // Produce
544
545 const int64_t time = 12345678L;
546 uint32_t stride;
547 ASSERT_NO_FATAL_FAILURE(produceOneFrame(mANW, params, time,
548 &stride));
549
550 // Consume
551
552 CpuConsumer::LockedBuffer b;
553 err = mCC->lockNextBuffer(&b);
554 ASSERT_NO_ERROR(err, "getNextBuffer error: ");
555
556 ASSERT_TRUE(b.data != NULL);
557 EXPECT_EQ(params.width, b.width);
558 EXPECT_EQ(params.height, b.height);
559 EXPECT_EQ(params.format, b.format);
560 EXPECT_EQ(stride, b.stride);
561 EXPECT_EQ(time, b.timestamp);
562
Igor Murashkin29e20472013-02-05 17:54:32 -0800563 checkAnyBuffer(b, GetParam().format);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700564 mCC->unlockBuffer(b);
565}
566
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800567// This test is disabled because the HAL_PIXEL_FORMAT_RAW16 format is not
Jamie Gennisfdb6b492012-08-31 14:44:50 -0700568// supported on all devices.
Igor Murashkin29e20472013-02-05 17:54:32 -0800569TEST_P(CpuConsumerTest, FromCpuManyInQueue) {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700570 status_t err;
571 CpuConsumerTestParams params = GetParam();
572
573 const int numInQueue = 5;
574 // Set up
575
576 ASSERT_NO_FATAL_FAILURE(configureANW(mANW, params, numInQueue));
577
578 // Produce
579
580 const int64_t time[numInQueue] = { 1L, 2L, 3L, 4L, 5L};
581 uint32_t stride[numInQueue];
582
583 for (int i = 0; i < numInQueue; i++) {
584 ALOGV("Producing frame %d", i);
585 ASSERT_NO_FATAL_FAILURE(produceOneFrame(mANW, params, time[i],
586 &stride[i]));
587 }
588
589 // Consume
590
591 for (int i = 0; i < numInQueue; i++) {
592 ALOGV("Consuming frame %d", i);
593 CpuConsumer::LockedBuffer b;
594 err = mCC->lockNextBuffer(&b);
595 ASSERT_NO_ERROR(err, "getNextBuffer error: ");
596
597 ASSERT_TRUE(b.data != NULL);
598 EXPECT_EQ(params.width, b.width);
599 EXPECT_EQ(params.height, b.height);
600 EXPECT_EQ(params.format, b.format);
601 EXPECT_EQ(stride[i], b.stride);
602 EXPECT_EQ(time[i], b.timestamp);
603
Igor Murashkin29e20472013-02-05 17:54:32 -0800604 checkAnyBuffer(b, GetParam().format);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700605
606 mCC->unlockBuffer(b);
607 }
608}
609
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800610// This test is disabled because the HAL_PIXEL_FORMAT_RAW16 format is not
Jamie Gennisfdb6b492012-08-31 14:44:50 -0700611// supported on all devices.
Igor Murashkin29e20472013-02-05 17:54:32 -0800612TEST_P(CpuConsumerTest, FromCpuLockMax) {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700613 status_t err;
614 CpuConsumerTestParams params = GetParam();
615
616 // Set up
617
618 ASSERT_NO_FATAL_FAILURE(configureANW(mANW, params, params.maxLockedBuffers + 1));
619
620 // Produce
621
622 const int64_t time = 1234L;
623 uint32_t stride;
624
625 for (int i = 0; i < params.maxLockedBuffers + 1; i++) {
626 ALOGV("Producing frame %d", i);
627 ASSERT_NO_FATAL_FAILURE(produceOneFrame(mANW, params, time,
628 &stride));
629 }
630
631 // Consume
632
Manoj Guptae8278ac2017-07-18 15:57:14 -0700633 std::vector<CpuConsumer::LockedBuffer> b(params.maxLockedBuffers);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700634 for (int i = 0; i < params.maxLockedBuffers; i++) {
635 ALOGV("Locking frame %d", i);
636 err = mCC->lockNextBuffer(&b[i]);
637 ASSERT_NO_ERROR(err, "getNextBuffer error: ");
638
639 ASSERT_TRUE(b[i].data != NULL);
640 EXPECT_EQ(params.width, b[i].width);
641 EXPECT_EQ(params.height, b[i].height);
642 EXPECT_EQ(params.format, b[i].format);
643 EXPECT_EQ(stride, b[i].stride);
644 EXPECT_EQ(time, b[i].timestamp);
645
Igor Murashkin29e20472013-02-05 17:54:32 -0800646 checkAnyBuffer(b[i], GetParam().format);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700647 }
648
649 ALOGV("Locking frame %d (too many)", params.maxLockedBuffers);
650 CpuConsumer::LockedBuffer bTooMuch;
651 err = mCC->lockNextBuffer(&bTooMuch);
Alistair Strachan8f76b6b2013-12-04 14:55:01 -0800652 ASSERT_TRUE(err == NOT_ENOUGH_DATA) << "Allowing too many locks";
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700653
654 ALOGV("Unlocking frame 0");
655 err = mCC->unlockBuffer(b[0]);
656 ASSERT_NO_ERROR(err, "Could not unlock buffer 0: ");
657
658 ALOGV("Locking frame %d (should work now)", params.maxLockedBuffers);
659 err = mCC->lockNextBuffer(&bTooMuch);
660 ASSERT_NO_ERROR(err, "Did not allow new lock after unlock");
661
662 ASSERT_TRUE(bTooMuch.data != NULL);
663 EXPECT_EQ(params.width, bTooMuch.width);
664 EXPECT_EQ(params.height, bTooMuch.height);
665 EXPECT_EQ(params.format, bTooMuch.format);
666 EXPECT_EQ(stride, bTooMuch.stride);
667 EXPECT_EQ(time, bTooMuch.timestamp);
668
Igor Murashkin29e20472013-02-05 17:54:32 -0800669 checkAnyBuffer(bTooMuch, GetParam().format);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700670
671 ALOGV("Unlocking extra buffer");
672 err = mCC->unlockBuffer(bTooMuch);
673 ASSERT_NO_ERROR(err, "Could not unlock extra buffer: ");
674
675 ALOGV("Locking frame %d (no more available)", params.maxLockedBuffers + 1);
676 err = mCC->lockNextBuffer(&b[0]);
677 ASSERT_EQ(BAD_VALUE, err) << "Not out of buffers somehow";
678
679 for (int i = 1; i < params.maxLockedBuffers; i++) {
680 mCC->unlockBuffer(b[i]);
681 }
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700682}
683
Chia-I Wucacfcc62017-11-16 14:54:07 -0800684TEST_P(CpuConsumerTest, FromCpuInvalid) {
685 status_t err = mCC->lockNextBuffer(nullptr);
686 ASSERT_EQ(BAD_VALUE, err) << "lockNextBuffer did not fail";
687
688 CpuConsumer::LockedBuffer b;
689 err = mCC->unlockBuffer(b);
690 ASSERT_EQ(BAD_VALUE, err) << "unlockBuffer did not fail";
691}
692
Igor Murashkin29e20472013-02-05 17:54:32 -0800693CpuConsumerTestParams y8TestSets[] = {
694 { 512, 512, 1, HAL_PIXEL_FORMAT_Y8},
695 { 512, 512, 3, HAL_PIXEL_FORMAT_Y8},
696 { 2608, 1960, 1, HAL_PIXEL_FORMAT_Y8},
697 { 2608, 1960, 3, HAL_PIXEL_FORMAT_Y8},
698 { 100, 100, 1, HAL_PIXEL_FORMAT_Y8},
699 { 100, 100, 3, HAL_PIXEL_FORMAT_Y8},
700};
701
702CpuConsumerTestParams y16TestSets[] = {
703 { 512, 512, 1, HAL_PIXEL_FORMAT_Y16},
704 { 512, 512, 3, HAL_PIXEL_FORMAT_Y16},
705 { 2608, 1960, 1, HAL_PIXEL_FORMAT_Y16},
706 { 2608, 1960, 3, HAL_PIXEL_FORMAT_Y16},
707 { 100, 100, 1, HAL_PIXEL_FORMAT_Y16},
708 { 100, 100, 3, HAL_PIXEL_FORMAT_Y16},
709};
710
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700711CpuConsumerTestParams rawTestSets[] = {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800712 { 512, 512, 1, HAL_PIXEL_FORMAT_RAW16},
713 { 512, 512, 3, HAL_PIXEL_FORMAT_RAW16},
714 { 2608, 1960, 1, HAL_PIXEL_FORMAT_RAW16},
715 { 2608, 1960, 3, HAL_PIXEL_FORMAT_RAW16},
716 { 100, 100, 1, HAL_PIXEL_FORMAT_RAW16},
717 { 100, 100, 3, HAL_PIXEL_FORMAT_RAW16},
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700718};
719
Igor Murashkin29e20472013-02-05 17:54:32 -0800720CpuConsumerTestParams rgba8888TestSets[] = {
721 { 512, 512, 1, HAL_PIXEL_FORMAT_RGBA_8888},
722 { 512, 512, 3, HAL_PIXEL_FORMAT_RGBA_8888},
723 { 2608, 1960, 1, HAL_PIXEL_FORMAT_RGBA_8888},
724 { 2608, 1960, 3, HAL_PIXEL_FORMAT_RGBA_8888},
725 { 100, 100, 1, HAL_PIXEL_FORMAT_RGBA_8888},
726 { 100, 100, 3, HAL_PIXEL_FORMAT_RGBA_8888},
727};
728
729#if CPU_CONSUMER_TEST_FORMAT_Y8
730INSTANTIATE_TEST_CASE_P(Y8Tests,
731 CpuConsumerTest,
732 ::testing::ValuesIn(y8TestSets));
733#endif
734
735#if CPU_CONSUMER_TEST_FORMAT_Y16
736INSTANTIATE_TEST_CASE_P(Y16Tests,
737 CpuConsumerTest,
738 ::testing::ValuesIn(y16TestSets));
739#endif
740
741#if CPU_CONSUMER_TEST_FORMAT_RAW
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700742INSTANTIATE_TEST_CASE_P(RawTests,
743 CpuConsumerTest,
744 ::testing::ValuesIn(rawTestSets));
Igor Murashkin29e20472013-02-05 17:54:32 -0800745#endif
746
747#if CPU_CONSUMER_TEST_FORMAT_RGBA_8888
748INSTANTIATE_TEST_CASE_P(Rgba8888Tests,
749 CpuConsumerTest,
750 ::testing::ValuesIn(rgba8888TestSets));
751#endif
752
753
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700754
755} // namespace android