Lloyd Pique | 76ed703 | 2018-12-04 17:24:28 -0800 | [diff] [blame] | 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 | |
| 17 | #include <compositionengine/impl/HwcBufferCache.h> |
| 18 | #include <gtest/gtest.h> |
| 19 | #include <gui/BufferQueue.h> |
| 20 | #include <ui/GraphicBuffer.h> |
| 21 | |
| 22 | namespace android::compositionengine { |
| 23 | namespace { |
| 24 | |
Valerie Hau | 6f89c37 | 2019-03-02 00:13:33 +0000 | [diff] [blame] | 25 | class TestableHwcBufferCache : public impl::HwcBufferCache { |
| 26 | public: |
Valerie Hau | 13f0d1a | 2019-03-22 10:35:42 -0700 | [diff] [blame] | 27 | void getHwcBuffer(int slot, const sp<GraphicBuffer>& buffer, uint32_t* outSlot, |
Valerie Hau | 6f89c37 | 2019-03-02 00:13:33 +0000 | [diff] [blame] | 28 | sp<GraphicBuffer>* outBuffer) { |
Valerie Hau | 13f0d1a | 2019-03-22 10:35:42 -0700 | [diff] [blame] | 29 | HwcBufferCache::getHwcBuffer(slot, buffer, outSlot, outBuffer); |
Valerie Hau | 6f89c37 | 2019-03-02 00:13:33 +0000 | [diff] [blame] | 30 | } |
Valerie Hau | 6f89c37 | 2019-03-02 00:13:33 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
Lloyd Pique | 76ed703 | 2018-12-04 17:24:28 -0800 | [diff] [blame] | 33 | class HwcBufferCacheTest : public testing::Test { |
| 34 | public: |
| 35 | ~HwcBufferCacheTest() override = default; |
| 36 | |
Valerie Hau | 13f0d1a | 2019-03-22 10:35:42 -0700 | [diff] [blame] | 37 | void testSlot(const int inSlot, const uint32_t expectedSlot) { |
| 38 | uint32_t outSlot; |
| 39 | sp<GraphicBuffer> outBuffer; |
| 40 | |
| 41 | // The first time, the output is the same as the input |
| 42 | mCache.getHwcBuffer(inSlot, mBuffer1, &outSlot, &outBuffer); |
| 43 | EXPECT_EQ(expectedSlot, outSlot); |
| 44 | EXPECT_EQ(mBuffer1, outBuffer); |
| 45 | |
| 46 | // The second time with the same buffer, the outBuffer is nullptr. |
| 47 | mCache.getHwcBuffer(inSlot, mBuffer1, &outSlot, &outBuffer); |
| 48 | EXPECT_EQ(expectedSlot, outSlot); |
| 49 | EXPECT_EQ(nullptr, outBuffer.get()); |
| 50 | |
| 51 | // With a new buffer, the outBuffer is the input. |
| 52 | mCache.getHwcBuffer(inSlot, mBuffer2, &outSlot, &outBuffer); |
| 53 | EXPECT_EQ(expectedSlot, outSlot); |
| 54 | EXPECT_EQ(mBuffer2, outBuffer); |
| 55 | |
| 56 | // Again, the second request with the same buffer sets outBuffer to nullptr. |
| 57 | mCache.getHwcBuffer(inSlot, mBuffer2, &outSlot, &outBuffer); |
| 58 | EXPECT_EQ(expectedSlot, outSlot); |
| 59 | EXPECT_EQ(nullptr, outBuffer.get()); |
| 60 | |
| 61 | // Setting a slot to use nullptr lookslike works, but note that |
| 62 | // the output values make it look like no new buffer is being set.... |
| 63 | mCache.getHwcBuffer(inSlot, sp<GraphicBuffer>(), &outSlot, &outBuffer); |
| 64 | EXPECT_EQ(expectedSlot, outSlot); |
| 65 | EXPECT_EQ(nullptr, outBuffer.get()); |
| 66 | } |
| 67 | |
| 68 | impl::HwcBufferCache mCache; |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 69 | sp<GraphicBuffer> mBuffer1 = |
| 70 | sp<GraphicBuffer>::make(1u, 1u, HAL_PIXEL_FORMAT_RGBA_8888, 1u, 0u); |
| 71 | sp<GraphicBuffer> mBuffer2 = |
| 72 | sp<GraphicBuffer>::make(1u, 1u, HAL_PIXEL_FORMAT_RGBA_8888, 1u, 0u); |
Lloyd Pique | 76ed703 | 2018-12-04 17:24:28 -0800 | [diff] [blame] | 73 | }; |
| 74 | |
Valerie Hau | 13f0d1a | 2019-03-22 10:35:42 -0700 | [diff] [blame] | 75 | TEST_F(HwcBufferCacheTest, cacheWorksForSlotZero) { |
| 76 | testSlot(0, 0); |
| 77 | } |
| 78 | |
| 79 | TEST_F(HwcBufferCacheTest, cacheWorksForMaxSlot) { |
| 80 | testSlot(BufferQueue::NUM_BUFFER_SLOTS - 1, BufferQueue::NUM_BUFFER_SLOTS - 1); |
| 81 | } |
| 82 | |
| 83 | TEST_F(HwcBufferCacheTest, cacheMapsNegativeSlotToZero) { |
| 84 | testSlot(-123, 0); |
| 85 | } |
| 86 | |
Lloyd Pique | 76ed703 | 2018-12-04 17:24:28 -0800 | [diff] [blame] | 87 | } // namespace |
| 88 | } // namespace android::compositionengine |