blob: d4bbf6ddaaebdf9cd6700879bd76f4006ac89428 [file] [log] [blame]
Lloyd Pique76ed7032018-12-04 17:24:28 -08001/*
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
22namespace android::compositionengine {
23namespace {
24
Valerie Haub28f0072019-02-20 10:36:29 -080025class TestableHwcBufferCache : public impl::HwcBufferCache {
26public:
27 void getHwcBuffer(const sp<GraphicBuffer>& buffer, uint32_t* outSlot,
28 sp<GraphicBuffer>* outBuffer) {
29 HwcBufferCache::getHwcBuffer(buffer, outSlot, outBuffer);
30 }
31 int getSlot(const sp<GraphicBuffer>& buffer) { return HwcBufferCache::getSlot(buffer); }
32 int getLeastRecentlyUsedSlot() { return HwcBufferCache::getLeastRecentlyUsedSlot(); }
33};
34
Lloyd Pique76ed7032018-12-04 17:24:28 -080035class HwcBufferCacheTest : public testing::Test {
36public:
37 ~HwcBufferCacheTest() override = default;
38
Valerie Haub28f0072019-02-20 10:36:29 -080039 TestableHwcBufferCache mCache;
Lloyd Pique76ed7032018-12-04 17:24:28 -080040 sp<GraphicBuffer> mBuffer1{new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888, 1, 0)};
41 sp<GraphicBuffer> mBuffer2{new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888, 1, 0)};
42};
43
Valerie Haub28f0072019-02-20 10:36:29 -080044TEST_F(HwcBufferCacheTest, testSlot) {
45 uint32_t outSlot;
46 sp<GraphicBuffer> outBuffer;
47
48 // The first time, the output is the same as the input
49 mCache.getHwcBuffer(mBuffer1, &outSlot, &outBuffer);
50 EXPECT_EQ(0, outSlot);
51 EXPECT_EQ(mBuffer1, outBuffer);
52
53 // The second time with the same buffer, the outBuffer is nullptr.
54 mCache.getHwcBuffer(mBuffer1, &outSlot, &outBuffer);
55 EXPECT_EQ(0, outSlot);
56 EXPECT_EQ(nullptr, outBuffer.get());
57
58 // With a new buffer, the outBuffer is the input.
59 mCache.getHwcBuffer(mBuffer2, &outSlot, &outBuffer);
60 EXPECT_EQ(1, outSlot);
61 EXPECT_EQ(mBuffer2, outBuffer);
62
63 // Again, the second request with the same buffer sets outBuffer to nullptr.
64 mCache.getHwcBuffer(mBuffer2, &outSlot, &outBuffer);
65 EXPECT_EQ(1, outSlot);
66 EXPECT_EQ(nullptr, outBuffer.get());
67
68 // Setting a slot to use nullptr lookslike works, but note that
69 // the output values make it look like no new buffer is being set....
70 mCache.getHwcBuffer(sp<GraphicBuffer>(), &outSlot, &outBuffer);
71 EXPECT_EQ(2, outSlot);
72 EXPECT_EQ(nullptr, outBuffer.get());
Lloyd Pique76ed7032018-12-04 17:24:28 -080073}
74
Valerie Haub28f0072019-02-20 10:36:29 -080075TEST_F(HwcBufferCacheTest, testGetLeastRecentlyUsedSlot) {
76 int slot;
77 uint32_t outSlot;
78 sp<GraphicBuffer> outBuffer;
Lloyd Pique76ed7032018-12-04 17:24:28 -080079
Valerie Haub28f0072019-02-20 10:36:29 -080080 // fill up cache
81 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
82 sp<GraphicBuffer> buf{new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888, 1, 0)};
83 mCache.getHwcBuffer(buf, &outSlot, &outBuffer);
84 EXPECT_EQ(buf, outBuffer);
85 EXPECT_EQ(i, outSlot);
86 }
Lloyd Pique76ed7032018-12-04 17:24:28 -080087
Valerie Haub28f0072019-02-20 10:36:29 -080088 slot = mCache.getLeastRecentlyUsedSlot();
89 EXPECT_EQ(0, slot);
90
91 mCache.getHwcBuffer(mBuffer1, &outSlot, &outBuffer);
92 EXPECT_EQ(0, outSlot);
93 EXPECT_EQ(mBuffer1, outBuffer);
94
95 slot = mCache.getLeastRecentlyUsedSlot();
96 EXPECT_EQ(1, slot);
Lloyd Pique76ed7032018-12-04 17:24:28 -080097}
98
99} // namespace
100} // namespace android::compositionengine