OffscreenBufferPoolTests.cpp revision 38e0c32852e3b9d8ca4a9d3791577f52536419cb
1/*
2 * Copyright (C) 2015 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 <gtest/gtest.h>
18#include <Rect.h>
19#include <renderstate/OffscreenBufferPool.h>
20
21#include <tests/common/TestUtils.h>
22
23using namespace android::uirenderer;
24
25TEST(OffscreenBuffer, computeIdealDimension) {
26    EXPECT_EQ(64u, OffscreenBuffer::computeIdealDimension(1));
27    EXPECT_EQ(64u, OffscreenBuffer::computeIdealDimension(31));
28    EXPECT_EQ(64u, OffscreenBuffer::computeIdealDimension(33));
29    EXPECT_EQ(64u, OffscreenBuffer::computeIdealDimension(64));
30    EXPECT_EQ(1024u, OffscreenBuffer::computeIdealDimension(1000));
31}
32
33TEST(OffscreenBuffer, construct) {
34    TestUtils::runOnRenderThread([] (renderthread::RenderThread& thread) {
35        OffscreenBuffer layer(thread.renderState(), Caches::getInstance(), 49u, 149u);
36        EXPECT_EQ(49u, layer.viewportWidth);
37        EXPECT_EQ(149u, layer.viewportHeight);
38
39        EXPECT_EQ(64u, layer.texture.width());
40        EXPECT_EQ(192u, layer.texture.height());
41
42        EXPECT_EQ(64u * 192u * 4u, layer.getSizeInBytes());
43    });
44}
45
46TEST(OffscreenBuffer, getTextureCoordinates) {
47    TestUtils::runOnRenderThread([] (renderthread::RenderThread& thread) {
48        OffscreenBuffer layerAligned(thread.renderState(), Caches::getInstance(), 256u, 256u);
49        EXPECT_EQ(Rect(0, 1, 1, 0),
50                layerAligned.getTextureCoordinates());
51
52        OffscreenBuffer layerUnaligned(thread.renderState(), Caches::getInstance(), 200u, 225u);
53        EXPECT_EQ(Rect(0, 225.0f / 256.0f, 200.0f / 256.0f, 0),
54                layerUnaligned.getTextureCoordinates());
55    });
56}
57
58TEST(OffscreenBufferPool, construct) {
59    TestUtils::runOnRenderThread([] (renderthread::RenderThread& thread) {
60        OffscreenBufferPool pool;
61        EXPECT_EQ(0u, pool.getCount()) << "pool must be created empty";
62        EXPECT_EQ(0u, pool.getSize()) << "pool must be created empty";
63        EXPECT_EQ((uint32_t) Properties::layerPoolSize, pool.getMaxSize())
64                << "pool must read size from Properties";
65    });
66}
67
68TEST(OffscreenBufferPool, getPutClear) {
69    TestUtils::runOnRenderThread([] (renderthread::RenderThread& thread) {
70        OffscreenBufferPool pool;
71
72        auto layer = pool.get(thread.renderState(), 100u, 200u);
73        EXPECT_EQ(100u, layer->viewportWidth);
74        EXPECT_EQ(200u, layer->viewportHeight);
75
76        ASSERT_LT(layer->getSizeInBytes(), pool.getMaxSize());
77
78        pool.putOrDelete(layer);
79        ASSERT_EQ(layer->getSizeInBytes(), pool.getSize());
80
81        auto layer2 = pool.get(thread.renderState(), 102u, 202u);
82        EXPECT_EQ(layer, layer2) << "layer should be recycled";
83        ASSERT_EQ(0u, pool.getSize()) << "pool should have been emptied by removing only layer";
84
85        pool.putOrDelete(layer);
86        EXPECT_EQ(1u, pool.getCount());
87        pool.clear();
88        EXPECT_EQ(0u, pool.getSize());
89        EXPECT_EQ(0u, pool.getCount());
90    });
91}
92
93TEST(OffscreenBufferPool, resize) {
94    TestUtils::runOnRenderThread([] (renderthread::RenderThread& thread) {
95        OffscreenBufferPool pool;
96
97        auto layer = pool.get(thread.renderState(), 64u, 64u);
98
99        // resize in place
100        ASSERT_EQ(layer, pool.resize(layer, 60u, 55u));
101        EXPECT_EQ(60u, layer->viewportWidth);
102        EXPECT_EQ(55u, layer->viewportHeight);
103        EXPECT_EQ(64u, layer->texture.width());
104        EXPECT_EQ(64u, layer->texture.height());
105
106        // resized to use different object in pool
107        auto layer2 = pool.get(thread.renderState(), 128u, 128u);
108        pool.putOrDelete(layer2);
109        ASSERT_EQ(1u, pool.getCount());
110        ASSERT_EQ(layer2, pool.resize(layer, 120u, 125u));
111        EXPECT_EQ(120u, layer2->viewportWidth);
112        EXPECT_EQ(125u, layer2->viewportHeight);
113        EXPECT_EQ(128u, layer2->texture.width());
114        EXPECT_EQ(128u, layer2->texture.height());
115
116        // original allocation now only thing in pool
117        EXPECT_EQ(1u, pool.getCount());
118        EXPECT_EQ(layer->getSizeInBytes(), pool.getSize());
119    });
120}
121
122TEST(OffscreenBufferPool, putAndDestroy) {
123    TestUtils::runOnRenderThread([] (renderthread::RenderThread& thread) {
124        OffscreenBufferPool pool;
125        // layer too big to return to the pool
126        // Note: this relies on the fact that the pool won't reject based on max texture size
127        auto hugeLayer = pool.get(thread.renderState(), pool.getMaxSize() / 64, 64);
128        EXPECT_GT(hugeLayer->getSizeInBytes(), pool.getMaxSize());
129        pool.putOrDelete(hugeLayer);
130        EXPECT_EQ(0u, pool.getCount()); // failed to put (so was destroyed instead)
131    });
132}
133