| 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 |  | 
|  | 25 | class HwcBufferCacheTest : public testing::Test { | 
|  | 26 | public: | 
|  | 27 | ~HwcBufferCacheTest() override = default; | 
|  | 28 |  | 
|  | 29 | void testSlot(const int inSlot, const uint32_t expectedSlot) { | 
|  | 30 | uint32_t outSlot; | 
|  | 31 | sp<GraphicBuffer> outBuffer; | 
|  | 32 |  | 
|  | 33 | // The first time, the output  is the same as the input | 
|  | 34 | mCache.getHwcBuffer(inSlot, mBuffer1, &outSlot, &outBuffer); | 
|  | 35 | EXPECT_EQ(expectedSlot, outSlot); | 
|  | 36 | EXPECT_EQ(mBuffer1, outBuffer); | 
|  | 37 |  | 
|  | 38 | // The second time with the same buffer, the outBuffer is nullptr. | 
|  | 39 | mCache.getHwcBuffer(inSlot, mBuffer1, &outSlot, &outBuffer); | 
|  | 40 | EXPECT_EQ(expectedSlot, outSlot); | 
|  | 41 | EXPECT_EQ(nullptr, outBuffer.get()); | 
|  | 42 |  | 
|  | 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 |  | 
|  | 48 | // Again, the second request with the same buffer sets outBuffer to nullptr. | 
|  | 49 | mCache.getHwcBuffer(inSlot, mBuffer2, &outSlot, &outBuffer); | 
|  | 50 | EXPECT_EQ(expectedSlot, outSlot); | 
|  | 51 | EXPECT_EQ(nullptr, outBuffer.get()); | 
|  | 52 |  | 
|  | 53 | // Setting a slot to use nullptr lookslike works, but note that | 
|  | 54 | // the output values make it look like no new buffer is being set.... | 
|  | 55 | mCache.getHwcBuffer(inSlot, sp<GraphicBuffer>(), &outSlot, &outBuffer); | 
|  | 56 | EXPECT_EQ(expectedSlot, outSlot); | 
|  | 57 | EXPECT_EQ(nullptr, outBuffer.get()); | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | impl::HwcBufferCache mCache; | 
|  | 61 | sp<GraphicBuffer> mBuffer1{new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888, 1, 0)}; | 
|  | 62 | sp<GraphicBuffer> mBuffer2{new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888, 1, 0)}; | 
|  | 63 | }; | 
|  | 64 |  | 
|  | 65 | TEST_F(HwcBufferCacheTest, cacheWorksForSlotZero) { | 
|  | 66 | testSlot(0, 0); | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | TEST_F(HwcBufferCacheTest, cacheWorksForMaxSlot) { | 
|  | 70 | testSlot(BufferQueue::NUM_BUFFER_SLOTS - 1, BufferQueue::NUM_BUFFER_SLOTS - 1); | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | TEST_F(HwcBufferCacheTest, cacheMapsNegativeSlotToZero) { | 
|  | 74 | testSlot(-123, 0); | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | TEST_F(HwcBufferCacheTest, cacheMapsInvalidBufferSlotToZero) { | 
|  | 78 | testSlot(BufferQueue::INVALID_BUFFER_SLOT, 0); | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | } // namespace | 
|  | 82 | } // namespace android::compositionengine |