• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)
67           : mRenderEngine(renderEngine), mEnabled(false) {}
68 
69     virtual ~TexturePool() = default;
70 
71     // Sets the display size for the texture pool.
72     // This will trigger a reallocation for all remaining textures in the pool.
73     // setDisplaySize must be called for the texture pool to be used.
74     void setDisplaySize(ui::Size size);
75 
76     // Borrows a new texture from the pool.
77     // If the pool is currently starved of textures, then a new texture is generated.
78     // When the AutoTexture object is destroyed, the scratch texture is automatically returned
79     // to the pool.
80     std::shared_ptr<AutoTexture> borrowTexture();
81 
82     // Enables or disables the pool. When the pool is disabled, no buffers will
83     // be held by the pool. This is useful when the active display changes.
84     void setEnabled(bool enable);
85 
86     void dump(std::string& out) const;
87 
88 protected:
89     // Proteted visibility so that they can be used for testing
90     const static constexpr size_t kMinPoolSize = 3;
91     const static constexpr size_t kMaxPoolSize = 4;
92 
93     struct Entry {
94         std::shared_ptr<renderengine::ExternalTexture> texture;
95         sp<Fence> fence;
96     };
97 
98     std::deque<Entry> mPool;
99 
100 private:
101     std::shared_ptr<renderengine::ExternalTexture> genTexture();
102     // Returns a previously borrowed texture to the pool.
103     void returnTexture(std::shared_ptr<renderengine::ExternalTexture>&& texture,
104                        const sp<Fence>& fence);
105     void allocatePool();
106     renderengine::RenderEngine& mRenderEngine;
107     ui::Size mSize;
108     bool mEnabled;
109 };
110 
111 } // namespace android::compositionengine::impl::planner
112