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