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 #pragma once 18 19 #include <compositionengine/Output.h> 20 #include <compositionengine/ProjectionSpace.h> 21 #include <compositionengine/impl/planner/LayerState.h> 22 #include <renderengine/RenderEngine.h> 23 24 #include <renderengine/ExternalTexture.h> 25 #include <chrono> 26 #include "android-base/macros.h" 27 28 namespace android::compositionengine::impl::planner { 29 30 // A pool of textures that only manages textures of a single size. 31 // While it is possible to define a texture pool supporting variable-sized textures to save on 32 // memory, it is a simpler implementation to only manage screen-sized textures. The texture pool is 33 // unbounded - there are a minimum number of textures preallocated. Under heavy system load, new 34 // textures may be allocated, but only a maximum number of retained once those textures are no 35 // longer necessary. 36 class TexturePool { 37 public: 38 // RAII class helping with managing textures from the texture pool 39 // Textures once they're no longer used should be returned to the pool instead of outright 40 // deleted. 41 class AutoTexture { 42 public: AutoTexture(TexturePool & texturePool,std::shared_ptr<renderengine::ExternalTexture> texture,const sp<Fence> & fence)43 AutoTexture(TexturePool& texturePool, 44 std::shared_ptr<renderengine::ExternalTexture> texture, const sp<Fence>& fence) 45 : mTexturePool(texturePool), mTexture(texture), mFence(fence) {} 46 ~AutoTexture()47 ~AutoTexture() { mTexturePool.returnTexture(std::move(mTexture), mFence); } 48 getReadyFence()49 sp<Fence> getReadyFence() { return mFence; } 50 setReadyFence(const sp<Fence> & fence)51 void setReadyFence(const sp<Fence>& fence) { mFence = fence; } 52 53 // Disable copying and assigning 54 AutoTexture(const AutoTexture&) = delete; 55 AutoTexture& operator=(const AutoTexture&) = delete; 56 57 // Gets a pointer to the underlying external texture get()58 const std::shared_ptr<renderengine::ExternalTexture>& get() const { return mTexture; } 59 60 private: 61 TexturePool& mTexturePool; 62 std::shared_ptr<renderengine::ExternalTexture> mTexture; 63 sp<Fence> mFence; 64 }; 65 TexturePool(renderengine::RenderEngine & renderEngine)66 TexturePool(renderengine::RenderEngine& renderEngine) : mRenderEngine(renderEngine) {} 67 68 virtual ~TexturePool() = default; 69 70 // Sets the display size for the texture pool. 71 // This will trigger a reallocation for all remaining textures in the pool. 72 // setDisplaySize must be called for the texture pool to be used. 73 void setDisplaySize(ui::Size size); 74 75 // Borrows a new texture from the pool. 76 // If the pool is currently starved of textures, then a new texture is generated. 77 // When the AutoTexture object is destroyed, the scratch texture is automatically returned 78 // to the pool. 79 std::shared_ptr<AutoTexture> borrowTexture(); 80 81 protected: 82 // Proteted visibility so that they can be used for testing 83 const static constexpr size_t kMinPoolSize = 3; 84 const static constexpr size_t kMaxPoolSize = 4; 85 86 struct Entry { 87 std::shared_ptr<renderengine::ExternalTexture> texture; 88 sp<Fence> fence; 89 }; 90 91 std::deque<Entry> mPool; 92 93 private: 94 std::shared_ptr<renderengine::ExternalTexture> genTexture(); 95 // Returns a previously borrowed texture to the pool. 96 void returnTexture(std::shared_ptr<renderengine::ExternalTexture>&& texture, 97 const sp<Fence>& fence); 98 renderengine::RenderEngine& mRenderEngine; 99 ui::Size mSize; 100 }; 101 102 } // namespace android::compositionengine::impl::planner 103