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 #undef LOG_TAG
18 #define LOG_TAG "CachingTest"
19 
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <gui/BufferQueue.h>
23 #include "BufferStateLayer.h"
24 
25 namespace android {
26 
27 class SlotGenerationTest : public testing::Test {
28 protected:
29     BufferStateLayer::HwcSlotGenerator mHwcSlotGenerator;
30     sp<GraphicBuffer> mBuffer1{new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888, 1, 0)};
31     sp<GraphicBuffer> mBuffer2{new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888, 1, 0)};
32     sp<GraphicBuffer> mBuffer3{new GraphicBuffer(10, 10, HAL_PIXEL_FORMAT_RGBA_8888, 1, 0)};
33 };
34 
TEST_F(SlotGenerationTest,getHwcCacheSlot_Invalid)35 TEST_F(SlotGenerationTest, getHwcCacheSlot_Invalid) {
36     sp<IBinder> binder = new BBinder();
37     // test getting invalid client_cache_id
38     client_cache_t id;
39     uint32_t slot = mHwcSlotGenerator.getHwcCacheSlot(id);
40     EXPECT_EQ(BufferQueue::INVALID_BUFFER_SLOT, slot);
41 }
42 
TEST_F(SlotGenerationTest,getHwcCacheSlot_Basic)43 TEST_F(SlotGenerationTest, getHwcCacheSlot_Basic) {
44     sp<IBinder> binder = new BBinder();
45     client_cache_t id;
46     id.token = binder;
47     id.id = 0;
48     uint32_t slot = mHwcSlotGenerator.getHwcCacheSlot(id);
49     EXPECT_EQ(BufferQueue::NUM_BUFFER_SLOTS - 1, slot);
50 
51     client_cache_t idB;
52     idB.token = binder;
53     idB.id = 1;
54     slot = mHwcSlotGenerator.getHwcCacheSlot(idB);
55     EXPECT_EQ(BufferQueue::NUM_BUFFER_SLOTS - 2, slot);
56 
57     slot = mHwcSlotGenerator.getHwcCacheSlot(idB);
58     EXPECT_EQ(BufferQueue::NUM_BUFFER_SLOTS - 2, slot);
59 
60     slot = mHwcSlotGenerator.getHwcCacheSlot(id);
61     EXPECT_EQ(BufferQueue::NUM_BUFFER_SLOTS - 1, slot);
62 }
63 
TEST_F(SlotGenerationTest,getHwcCacheSlot_Reuse)64 TEST_F(SlotGenerationTest, getHwcCacheSlot_Reuse) {
65     sp<IBinder> binder = new BBinder();
66     std::vector<client_cache_t> ids;
67     uint32_t cacheId = 0;
68     // fill up cache
69     for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
70         client_cache_t id;
71         id.token = binder;
72         id.id = cacheId;
73         ids.push_back(id);
74 
75         uint32_t slot = mHwcSlotGenerator.getHwcCacheSlot(id);
76         EXPECT_EQ(BufferQueue::NUM_BUFFER_SLOTS - (i + 1), slot);
77         cacheId++;
78     }
79     for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
80         uint32_t slot = mHwcSlotGenerator.getHwcCacheSlot(ids[i]);
81         EXPECT_EQ(BufferQueue::NUM_BUFFER_SLOTS - (i + 1), slot);
82     }
83 
84     for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
85         client_cache_t id;
86         id.token = binder;
87         id.id = cacheId;
88         uint32_t slot = mHwcSlotGenerator.getHwcCacheSlot(id);
89         EXPECT_EQ(BufferQueue::NUM_BUFFER_SLOTS - (i + 1), slot);
90         cacheId++;
91     }
92 }
93 } // namespace android
94