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 59 int getContextPriority() override; 60 bool supportsBackgroundBlur() override; 61 void onActiveDisplaySizeChanged(ui::Size size) override; 62 std::optional<pid_t> getRenderEngineTid() const override; 63 void setEnableTracing(bool tracingEnabled) override; 64 65 protected: 66 void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, bool isRenderable) override; 67 void unmapExternalTextureBuffer(sp<GraphicBuffer>&& buffer) override; 68 bool canSkipPostRenderCleanup() const override; 69 void drawLayersInternal(const std::shared_ptr<std::promise<FenceResult>>&& resultPromise, 70 const DisplaySettings& display, 71 const std::vector<LayerSettings>& layers, 72 const std::shared_ptr<ExternalTexture>& buffer, 73 base::unique_fd&& bufferFence) override; 74 75 private: 76 void threadMain(CreateInstanceFactory factory); 77 void waitUntilInitialized() const; 78 static status_t setSchedFifo(bool enabled); 79 80 // No-op. This method is only called on leaf implementations of RenderEngine. useProtectedContext(bool)81 void useProtectedContext(bool) override {} 82 83 /* ------------------------------------------------------------------------ 84 * Threading 85 */ 86 const char* const mThreadName = "RenderEngine"; 87 // Protects the creation and destruction of mThread. 88 mutable std::mutex mThreadMutex; 89 std::thread mThread GUARDED_BY(mThreadMutex); 90 std::atomic<bool> mRunning = true; 91 std::atomic<bool> mNeedsPostRenderCleanup = false; 92 93 using Work = std::function<void(renderengine::RenderEngine&)>; 94 mutable std::queue<Work> mFunctionCalls GUARDED_BY(mThreadMutex); 95 mutable std::condition_variable mCondition; 96 97 // Used to allow select thread safe methods to be accessed without requiring the 98 // method to be invoked on the RenderEngine thread 99 std::atomic_bool mIsInitialized = false; 100 mutable std::mutex mInitializedMutex; 101 mutable std::condition_variable mInitializedCondition; 102 103 /* ------------------------------------------------------------------------ 104 * Render Engine 105 */ 106 std::unique_ptr<renderengine::RenderEngine> mRenderEngine; 107 }; 108 } // namespace threaded 109 } // namespace renderengine 110 } // namespace android 111