1 /*
2 * Copyright 2021 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 // #define LOG_NDEBUG 0
18
19 #undef LOG_TAG
20 #define LOG_TAG "Planner"
21
22 #include <compositionengine/impl/planner/TexturePool.h>
23 #include <utils/Log.h>
24
25 namespace android::compositionengine::impl::planner {
26
allocatePool()27 void TexturePool::allocatePool() {
28 mPool.clear();
29 if (mEnabled && mSize.isValid()) {
30 mPool.resize(kMinPoolSize);
31 std::generate_n(mPool.begin(), kMinPoolSize, [&]() {
32 return Entry{genTexture(), nullptr};
33 });
34 }
35 }
36
setDisplaySize(ui::Size size)37 void TexturePool::setDisplaySize(ui::Size size) {
38 if (mSize == size) {
39 return;
40 }
41 mSize = size;
42 allocatePool();
43 }
44
borrowTexture()45 std::shared_ptr<TexturePool::AutoTexture> TexturePool::borrowTexture() {
46 if (mPool.empty()) {
47 return std::make_shared<AutoTexture>(*this, genTexture(), nullptr);
48 }
49
50 const auto entry = mPool.front();
51 mPool.pop_front();
52 return std::make_shared<AutoTexture>(*this, entry.texture, entry.fence);
53 }
54
returnTexture(std::shared_ptr<renderengine::ExternalTexture> && texture,const sp<Fence> & fence)55 void TexturePool::returnTexture(std::shared_ptr<renderengine::ExternalTexture>&& texture,
56 const sp<Fence>& fence) {
57 // Drop the texture on the floor if the pool is not enabled
58 if (!mEnabled) {
59 return;
60 }
61
62 // Or the texture on the floor if the pool is no longer tracking textures of the same size.
63 if (static_cast<int32_t>(texture->getBuffer()->getWidth()) != mSize.getWidth() ||
64 static_cast<int32_t>(texture->getBuffer()->getHeight()) != mSize.getHeight()) {
65 ALOGV("Deallocating texture from Planner's pool - display size changed (previous: (%dx%d), "
66 "current: (%dx%d))",
67 texture->getBuffer()->getWidth(), texture->getBuffer()->getHeight(), mSize.getWidth(),
68 mSize.getHeight());
69 return;
70 }
71
72 // Also ensure the pool does not grow beyond a maximum size.
73 if (mPool.size() == kMaxPoolSize) {
74 ALOGD("Deallocating texture from Planner's pool - max size [%" PRIu64 "] reached",
75 static_cast<uint64_t>(kMaxPoolSize));
76 return;
77 }
78
79 mPool.push_back({std::move(texture), fence});
80 }
81
genTexture()82 std::shared_ptr<renderengine::ExternalTexture> TexturePool::genTexture() {
83 LOG_ALWAYS_FATAL_IF(!mSize.isValid(), "Attempted to generate texture with invalid size");
84 return std::make_shared<
85 renderengine::ExternalTexture>(sp<GraphicBuffer>::
86 make(mSize.getWidth(), mSize.getHeight(),
87 HAL_PIXEL_FORMAT_RGBA_8888, 1,
88 GraphicBuffer::USAGE_HW_RENDER |
89 GraphicBuffer::USAGE_HW_COMPOSER |
90 GraphicBuffer::USAGE_HW_TEXTURE,
91 "Planner"),
92 mRenderEngine,
93 renderengine::ExternalTexture::Usage::READABLE |
94 renderengine::ExternalTexture::Usage::WRITEABLE);
95 }
96
setEnabled(bool enabled)97 void TexturePool::setEnabled(bool enabled) {
98 mEnabled = enabled;
99 allocatePool();
100 }
101
dump(std::string & out) const102 void TexturePool::dump(std::string& out) const {
103 base::StringAppendF(&out,
104 "TexturePool (%s) has %zu buffers of size [%" PRId32 ", %" PRId32 "]\n",
105 mEnabled ? "enabled" : "disabled", mPool.size(), mSize.width, mSize.height);
106 }
107
108 } // namespace android::compositionengine::impl::planner