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
setDisplaySize(ui::Size size)27 void TexturePool::setDisplaySize(ui::Size size) {
28 if (mSize == size) {
29 return;
30 }
31 mSize = size;
32 mPool.clear();
33 mPool.resize(kMinPoolSize);
34 std::generate_n(mPool.begin(), kMinPoolSize, [&]() { return Entry{genTexture(), nullptr}; });
35 }
36
borrowTexture()37 std::shared_ptr<TexturePool::AutoTexture> TexturePool::borrowTexture() {
38 if (mPool.empty()) {
39 return std::make_shared<AutoTexture>(*this, genTexture(), nullptr);
40 }
41
42 const auto entry = mPool.front();
43 mPool.pop_front();
44 return std::make_shared<AutoTexture>(*this, entry.texture, entry.fence);
45 }
46
returnTexture(std::shared_ptr<renderengine::ExternalTexture> && texture,const sp<Fence> & fence)47 void TexturePool::returnTexture(std::shared_ptr<renderengine::ExternalTexture>&& texture,
48 const sp<Fence>& fence) {
49 // Drop the texture on the floor if the pool is no longer tracking textures of the same size.
50 if (static_cast<int32_t>(texture->getBuffer()->getWidth()) != mSize.getWidth() ||
51 static_cast<int32_t>(texture->getBuffer()->getHeight()) != mSize.getHeight()) {
52 ALOGV("Deallocating texture from Planner's pool - display size changed (previous: (%dx%d), "
53 "current: (%dx%d))",
54 texture->getBuffer()->getWidth(), texture->getBuffer()->getHeight(), mSize.getWidth(),
55 mSize.getHeight());
56 return;
57 }
58
59 // Also ensure the pool does not grow beyond a maximum size.
60 if (mPool.size() == kMaxPoolSize) {
61 ALOGD("Deallocating texture from Planner's pool - max size [%" PRIu64 "] reached",
62 static_cast<uint64_t>(kMaxPoolSize));
63 return;
64 }
65
66 mPool.push_back({std::move(texture), fence});
67 }
68
genTexture()69 std::shared_ptr<renderengine::ExternalTexture> TexturePool::genTexture() {
70 LOG_ALWAYS_FATAL_IF(!mSize.isValid(), "Attempted to generate texture with invalid size");
71 return std::make_shared<
72 renderengine::ExternalTexture>(sp<GraphicBuffer>::
73 make(mSize.getWidth(), mSize.getHeight(),
74 HAL_PIXEL_FORMAT_RGBA_8888, 1,
75 GraphicBuffer::USAGE_HW_RENDER |
76 GraphicBuffer::USAGE_HW_COMPOSER |
77 GraphicBuffer::USAGE_HW_TEXTURE,
78 "Planner"),
79 mRenderEngine,
80 renderengine::ExternalTexture::Usage::READABLE |
81 renderengine::ExternalTexture::Usage::WRITEABLE);
82 }
83
84 } // namespace android::compositionengine::impl::planner