xref: /aosp_15_r20/frameworks/native/libs/renderengine/threaded/RenderEngineThreaded.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright 2020 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 <android-base/thread_annotations.h>
20 #include <condition_variable>
21 #include <mutex>
22 #include <queue>
23 #include <thread>
24 
25 #include "renderengine/RenderEngine.h"
26 
27 namespace android {
28 namespace renderengine {
29 namespace threaded {
30 
31 using CreateInstanceFactory = std::function<std::unique_ptr<renderengine::RenderEngine>()>;
32 
33 /**
34  * This class extends a basic RenderEngine class. It contains a thread. Each time a function of
35  * this class is called, we create a lambda function that is put on a queue. The main thread then
36  * executes the functions in order.
37  */
38 class RenderEngineThreaded : public RenderEngine {
39 public:
40     static std::unique_ptr<RenderEngineThreaded> create(CreateInstanceFactory factory);
41 
42     RenderEngineThreaded(CreateInstanceFactory factory);
43     ~RenderEngineThreaded() override;
44     std::future<void> primeCache(PrimeCacheConfig config) override;
45 
46     void dump(std::string& result) override;
47 
48     size_t getMaxTextureSize() const override;
49     size_t getMaxViewportDims() const override;
50 
51     bool supportsProtectedContent() const override;
52     void cleanupPostRender() override;
53 
54     ftl::Future<FenceResult> drawLayers(const DisplaySettings& display,
55                                         const std::vector<LayerSettings>& layers,
56                                         const std::shared_ptr<ExternalTexture>& buffer,
57                                         base::unique_fd&& bufferFence) override;
58     ftl::Future<FenceResult> drawGainmap(const std::shared_ptr<ExternalTexture>& sdr,
59                                          base::borrowed_fd&& sdrFence,
60                                          const std::shared_ptr<ExternalTexture>& hdr,
61                                          base::borrowed_fd&& hdrFence, float hdrSdrRatio,
62                                          ui::Dataspace dataspace,
63                                          const std::shared_ptr<ExternalTexture>& gainmap) override;
64 
65     int getContextPriority() override;
66     bool supportsBackgroundBlur() override;
67     void onActiveDisplaySizeChanged(ui::Size size) override;
68     std::optional<pid_t> getRenderEngineTid() const override;
69     void setEnableTracing(bool tracingEnabled) override;
70 
71 protected:
72     void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, bool isRenderable) override;
73     void unmapExternalTextureBuffer(sp<GraphicBuffer>&& buffer) override;
74     bool canSkipPostRenderCleanup() const override;
75     void drawLayersInternal(const std::shared_ptr<std::promise<FenceResult>>&& resultPromise,
76                             const DisplaySettings& display,
77                             const std::vector<LayerSettings>& layers,
78                             const std::shared_ptr<ExternalTexture>& buffer,
79                             base::unique_fd&& bufferFence) override;
80     void drawGainmapInternal(const std::shared_ptr<std::promise<FenceResult>>&& resultPromise,
81                              const std::shared_ptr<ExternalTexture>& sdr,
82                              base::borrowed_fd&& sdrFence,
83                              const std::shared_ptr<ExternalTexture>& hdr,
84                              base::borrowed_fd&& hdrFence, float hdrSdrRatio,
85                              ui::Dataspace dataspace,
86                              const std::shared_ptr<ExternalTexture>& gainmap) override;
87 
88 private:
89     void threadMain(CreateInstanceFactory factory);
90     void waitUntilInitialized() const;
91     static status_t setSchedFifo(bool enabled);
92 
93     // No-op. This method is only called on leaf implementations of RenderEngine.
useProtectedContext(bool)94     void useProtectedContext(bool) override {}
95 
96     /* ------------------------------------------------------------------------
97      * Threading
98      */
99     const char* const mThreadName = "RenderEngine";
100     // Protects the creation and destruction of mThread.
101     mutable std::mutex mThreadMutex;
102     std::thread mThread GUARDED_BY(mThreadMutex);
103     std::atomic<bool> mRunning = true;
104     std::atomic<bool> mNeedsPostRenderCleanup = false;
105 
106     using Work = std::function<void(renderengine::RenderEngine&)>;
107     mutable std::queue<Work> mFunctionCalls GUARDED_BY(mThreadMutex);
108     mutable std::condition_variable mCondition;
109 
110     // Used to allow select thread safe methods to be accessed without requiring the
111     // method to be invoked on the RenderEngine thread
112     std::atomic_bool mIsInitialized = false;
113     mutable std::mutex mInitializedMutex;
114     mutable std::condition_variable mInitializedCondition;
115 
116     /* ------------------------------------------------------------------------
117      * Render Engine
118      */
119     std::unique_ptr<renderengine::RenderEngine> mRenderEngine;
120 };
121 } // namespace threaded
122 } // namespace renderengine
123 } // namespace android
124