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