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