HwcBufferCacheTest.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2019 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. #include <compositionengine/impl/HwcBufferCache.h>
  17. #include <gtest/gtest.h>
  18. #include <gui/BufferQueue.h>
  19. #include <ui/GraphicBuffer.h>
  20. namespace android::compositionengine {
  21. namespace {
  22. class TestableHwcBufferCache : public impl::HwcBufferCache {
  23. public:
  24. void getHwcBuffer(int slot, const sp<GraphicBuffer>& buffer, uint32_t* outSlot,
  25. sp<GraphicBuffer>* outBuffer) {
  26. HwcBufferCache::getHwcBuffer(slot, buffer, outSlot, outBuffer);
  27. }
  28. };
  29. class HwcBufferCacheTest : public testing::Test {
  30. public:
  31. ~HwcBufferCacheTest() override = default;
  32. void testSlot(const int inSlot, const uint32_t expectedSlot) {
  33. uint32_t outSlot;
  34. sp<GraphicBuffer> outBuffer;
  35. // The first time, the output is the same as the input
  36. mCache.getHwcBuffer(inSlot, mBuffer1, &outSlot, &outBuffer);
  37. EXPECT_EQ(expectedSlot, outSlot);
  38. EXPECT_EQ(mBuffer1, outBuffer);
  39. // The second time with the same buffer, the outBuffer is nullptr.
  40. mCache.getHwcBuffer(inSlot, mBuffer1, &outSlot, &outBuffer);
  41. EXPECT_EQ(expectedSlot, outSlot);
  42. EXPECT_EQ(nullptr, outBuffer.get());
  43. // With a new buffer, the outBuffer is the input.
  44. mCache.getHwcBuffer(inSlot, mBuffer2, &outSlot, &outBuffer);
  45. EXPECT_EQ(expectedSlot, outSlot);
  46. EXPECT_EQ(mBuffer2, outBuffer);
  47. // Again, the second request with the same buffer sets outBuffer to nullptr.
  48. mCache.getHwcBuffer(inSlot, mBuffer2, &outSlot, &outBuffer);
  49. EXPECT_EQ(expectedSlot, outSlot);
  50. EXPECT_EQ(nullptr, outBuffer.get());
  51. // Setting a slot to use nullptr lookslike works, but note that
  52. // the output values make it look like no new buffer is being set....
  53. mCache.getHwcBuffer(inSlot, sp<GraphicBuffer>(), &outSlot, &outBuffer);
  54. EXPECT_EQ(expectedSlot, outSlot);
  55. EXPECT_EQ(nullptr, outBuffer.get());
  56. }
  57. impl::HwcBufferCache mCache;
  58. sp<GraphicBuffer> mBuffer1{new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888, 1, 0)};
  59. sp<GraphicBuffer> mBuffer2{new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888, 1, 0)};
  60. };
  61. TEST_F(HwcBufferCacheTest, cacheWorksForSlotZero) {
  62. testSlot(0, 0);
  63. }
  64. TEST_F(HwcBufferCacheTest, cacheWorksForMaxSlot) {
  65. testSlot(BufferQueue::NUM_BUFFER_SLOTS - 1, BufferQueue::NUM_BUFFER_SLOTS - 1);
  66. }
  67. TEST_F(HwcBufferCacheTest, cacheMapsNegativeSlotToZero) {
  68. testSlot(-123, 0);
  69. }
  70. } // namespace
  71. } // namespace android::compositionengine