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