• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                                                         RenderEngineType type);
42 
43     RenderEngineThreaded(CreateInstanceFactory factory, RenderEngineType type);
44     ~RenderEngineThreaded() override;
45     std::future<void> primeCache() override;
46 
47     void dump(std::string& result) override;
48 
49     void genTextures(size_t count, uint32_t* names) override;
50     void deleteTextures(size_t count, uint32_t const* names) override;
51     size_t getMaxTextureSize() const override;
52     size_t getMaxViewportDims() const override;
53 
54     bool isProtected() const override;
55     bool supportsProtectedContent() const override;
56     void useProtectedContext(bool useProtectedContext) override;
57     void cleanupPostRender() override;
58 
59     status_t drawLayers(const DisplaySettings& display,
60                         const std::vector<const LayerSettings*>& layers,
61                         const std::shared_ptr<ExternalTexture>& buffer,
62                         const bool useFramebufferCache, base::unique_fd&& bufferFence,
63                         base::unique_fd* drawFence) override;
64 
65     void cleanFramebufferCache() override;
66     int getContextPriority() override;
67     bool supportsBackgroundBlur() override;
68     void onPrimaryDisplaySizeChanged(ui::Size size) override;
69 
70 protected:
71     void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, bool isRenderable) override;
72     void unmapExternalTextureBuffer(const sp<GraphicBuffer>& buffer) override;
73     bool canSkipPostRenderCleanup() const override;
74 
75 private:
76     void threadMain(CreateInstanceFactory factory);
77     void waitUntilInitialized() const;
78     static status_t setSchedFifo(bool enabled);
79 
80     /* ------------------------------------------------------------------------
81      * Threading
82      */
83     const char* const mThreadName = "RenderEngine";
84     // Protects the creation and destruction of mThread.
85     mutable std::mutex mThreadMutex;
86     std::thread mThread GUARDED_BY(mThreadMutex);
87     std::atomic<bool> mRunning = true;
88 
89     using Work = std::function<void(renderengine::RenderEngine&)>;
90     mutable std::queue<Work> mFunctionCalls GUARDED_BY(mThreadMutex);
91     mutable std::condition_variable mCondition;
92 
93     // Used to allow select thread safe methods to be accessed without requiring the
94     // method to be invoked on the RenderEngine thread
95     bool mIsInitialized = false;
96     mutable std::mutex mInitializedMutex;
97     mutable std::condition_variable mInitializedCondition;
98 
99     /* ------------------------------------------------------------------------
100      * Render Engine
101      */
102     std::unique_ptr<renderengine::RenderEngine> mRenderEngine;
103     std::atomic<bool> mIsProtected = false;
104 };
105 } // namespace threaded
106 } // namespace renderengine
107 } // namespace android
108