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