blob: ba921572fd0927e6a334faa3cf0980e29e53331b [file] [log] [blame]
Chris Craik9fded232015-11-11 16:42:34 -08001/*
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 <renderstate/OffscreenBufferPool.h>
19
20#include <unit_tests/TestUtils.h>
21
22using namespace android;
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(OffscreenBufferPool, construct) {
47 TestUtils::runOnRenderThread([] (renderthread::RenderThread& thread) {
48 OffscreenBufferPool pool;
49 EXPECT_EQ(0u, pool.getCount()) << "pool must be created empty";
50 EXPECT_EQ(0u, pool.getSize()) << "pool must be created empty";
51 EXPECT_EQ((uint32_t) Properties::layerPoolSize, pool.getMaxSize())
52 << "pool must read size from Properties";
53 });
54
55}
56
57TEST(OffscreenBufferPool, getPutClear) {
58 TestUtils::runOnRenderThread([] (renderthread::RenderThread& thread) {
59 OffscreenBufferPool pool;
60
61 auto layer = pool.get(thread.renderState(), 100u, 200u);
62 EXPECT_EQ(100u, layer->viewportWidth);
63 EXPECT_EQ(200u, layer->viewportHeight);
64
65 ASSERT_LT(layer->getSizeInBytes(), pool.getMaxSize());
66
67 pool.putOrDelete(layer);
68 ASSERT_EQ(layer->getSizeInBytes(), pool.getSize());
69
70 auto layer2 = pool.get(thread.renderState(), 102u, 202u);
71 EXPECT_EQ(layer, layer2) << "layer should be recycled";
72 ASSERT_EQ(0u, pool.getSize()) << "pool should have been emptied by removing only layer";
73
74 pool.putOrDelete(layer);
75 EXPECT_EQ(1u, pool.getCount());
76 pool.clear();
77 EXPECT_EQ(0u, pool.getSize());
78 EXPECT_EQ(0u, pool.getCount());
79 });
80}
81
82TEST(OffscreenBufferPool, resize) {
83 TestUtils::runOnRenderThread([] (renderthread::RenderThread& thread) {
84 OffscreenBufferPool pool;
85
86 auto layer = pool.get(thread.renderState(), 64u, 64u);
87
88 // resize in place
89 ASSERT_EQ(layer, pool.resize(layer, 60u, 55u));
90 EXPECT_EQ(60u, layer->viewportWidth);
91 EXPECT_EQ(55u, layer->viewportHeight);
92 EXPECT_EQ(64u, layer->texture.width);
93 EXPECT_EQ(64u, layer->texture.height);
94
95 // resized to use different object in pool
96 auto layer2 = pool.get(thread.renderState(), 128u, 128u);
97 pool.putOrDelete(layer2);
98 ASSERT_EQ(1u, pool.getCount());
99 ASSERT_EQ(layer2, pool.resize(layer, 120u, 125u));
100 EXPECT_EQ(120u, layer2->viewportWidth);
101 EXPECT_EQ(125u, layer2->viewportHeight);
102 EXPECT_EQ(128u, layer2->texture.width);
103 EXPECT_EQ(128u, layer2->texture.height);
104
105 // original allocation now only thing in pool
106 EXPECT_EQ(1u, pool.getCount());
107 EXPECT_EQ(layer->getSizeInBytes(), pool.getSize());
108 });
109}
110
111TEST(OffscreenBufferPool, putAndDestroy) {
112 TestUtils::runOnRenderThread([] (renderthread::RenderThread& thread) {
113 OffscreenBufferPool pool;
114 // layer too big to return to the pool
115 // Note: this relies on the fact that the pool won't reject based on max texture size
116 auto hugeLayer = pool.get(thread.renderState(), pool.getMaxSize() / 64, 64);
117 EXPECT_GT(hugeLayer->getSizeInBytes(), pool.getMaxSize());
118 pool.putOrDelete(hugeLayer);
119 EXPECT_EQ(0u, pool.getCount()); // failed to put (so was destroyed instead)
120 });
121}