Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 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 | |
Ana Krulec | 15f7cf3 | 2020-05-12 11:57:42 -0700 | [diff] [blame] | 31 | using CreateInstanceFactory = std::function<std::unique_ptr<renderengine::RenderEngine>()>; |
| 32 | |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 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 | */ |
Ana Krulec | 15f7cf3 | 2020-05-12 11:57:42 -0700 | [diff] [blame] | 38 | class RenderEngineThreaded : public RenderEngine { |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 39 | public: |
Alec Mouri | 0d99510 | 2021-02-24 16:53:38 -0800 | [diff] [blame] | 40 | static std::unique_ptr<RenderEngineThreaded> create(CreateInstanceFactory factory, |
| 41 | RenderEngineType type); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 42 | |
Alec Mouri | 0d99510 | 2021-02-24 16:53:38 -0800 | [diff] [blame] | 43 | RenderEngineThreaded(CreateInstanceFactory factory, RenderEngineType type); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 44 | ~RenderEngineThreaded() override; |
Ady Abraham | fe2a6db | 2021-06-09 15:41:37 -0700 | [diff] [blame] | 45 | std::future<void> primeCache() override; |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 46 | |
| 47 | void dump(std::string& result) override; |
| 48 | |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 49 | size_t getMaxTextureSize() const override; |
| 50 | size_t getMaxViewportDims() const override; |
| 51 | |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 52 | bool supportsProtectedContent() const override; |
Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 53 | void cleanupPostRender() override; |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 54 | |
Patrick Williams | 2e9748f | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 55 | ftl::Future<FenceResult> drawLayers(const DisplaySettings& display, |
| 56 | const std::vector<LayerSettings>& layers, |
| 57 | const std::shared_ptr<ExternalTexture>& buffer, |
Patrick Williams | 2e9748f | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 58 | base::unique_fd&& bufferFence) override; |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 59 | |
Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 60 | int getContextPriority() override; |
Derek Sollenberger | b399837 | 2021-02-16 15:16:56 -0500 | [diff] [blame] | 61 | bool supportsBackgroundBlur() override; |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 62 | void onActiveDisplaySizeChanged(ui::Size size) override; |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 63 | std::optional<pid_t> getRenderEngineTid() const override; |
Leon Scroggins III | a37ca99 | 2022-02-02 18:08:20 -0500 | [diff] [blame] | 64 | void setEnableTracing(bool tracingEnabled) override; |
Marin Shalamanov | 23c31af | 2020-09-09 15:01:47 +0200 | [diff] [blame] | 65 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 66 | protected: |
| 67 | void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, bool isRenderable) override; |
Alec Mouri | 92f89fa | 2023-02-24 00:05:06 +0000 | [diff] [blame] | 68 | void unmapExternalTextureBuffer(sp<GraphicBuffer>&& buffer) override; |
Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 69 | bool canSkipPostRenderCleanup() const override; |
Patrick Williams | 2e9748f | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 70 | void drawLayersInternal(const std::shared_ptr<std::promise<FenceResult>>&& resultPromise, |
Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 71 | const DisplaySettings& display, |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 72 | const std::vector<LayerSettings>& layers, |
Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 73 | const std::shared_ptr<ExternalTexture>& buffer, |
Alec Mouri | f29700f | 2023-08-17 21:53:31 +0000 | [diff] [blame^] | 74 | base::unique_fd&& bufferFence) override; |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 75 | |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 76 | private: |
Ana Krulec | 15f7cf3 | 2020-05-12 11:57:42 -0700 | [diff] [blame] | 77 | void threadMain(CreateInstanceFactory factory); |
Derek Sollenberger | 4bea01e | 2021-04-09 13:59:37 -0400 | [diff] [blame] | 78 | void waitUntilInitialized() const; |
Ady Abraham | fe2a6db | 2021-06-09 15:41:37 -0700 | [diff] [blame] | 79 | static status_t setSchedFifo(bool enabled); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 80 | |
Patrick Williams | 8aed5d2 | 2022-10-31 22:18:10 +0000 | [diff] [blame] | 81 | // No-op. This method is only called on leaf implementations of RenderEngine. |
| 82 | void useProtectedContext(bool) override {} |
| 83 | |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 84 | /* ------------------------------------------------------------------------ |
| 85 | * Threading |
| 86 | */ |
Ady Abraham | dde984b | 2021-03-18 12:47:36 -0700 | [diff] [blame] | 87 | const char* const mThreadName = "RenderEngine"; |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 88 | // Protects the creation and destruction of mThread. |
| 89 | mutable std::mutex mThreadMutex; |
| 90 | std::thread mThread GUARDED_BY(mThreadMutex); |
Alec Mouri | 39d9cb7 | 2021-06-10 10:26:15 -0700 | [diff] [blame] | 91 | std::atomic<bool> mRunning = true; |
| 92 | |
| 93 | using Work = std::function<void(renderengine::RenderEngine&)>; |
| 94 | mutable std::queue<Work> mFunctionCalls GUARDED_BY(mThreadMutex); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 95 | mutable std::condition_variable mCondition; |
| 96 | |
Derek Sollenberger | 4bea01e | 2021-04-09 13:59:37 -0400 | [diff] [blame] | 97 | // Used to allow select thread safe methods to be accessed without requiring the |
| 98 | // method to be invoked on the RenderEngine thread |
| 99 | bool mIsInitialized = false; |
| 100 | mutable std::mutex mInitializedMutex; |
| 101 | mutable std::condition_variable mInitializedCondition; |
| 102 | |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 103 | /* ------------------------------------------------------------------------ |
| 104 | * Render Engine |
| 105 | */ |
| 106 | std::unique_ptr<renderengine::RenderEngine> mRenderEngine; |
| 107 | }; |
| 108 | } // namespace threaded |
| 109 | } // namespace renderengine |
Alec Mouri | 368e158 | 2020-08-13 10:14:29 -0700 | [diff] [blame] | 110 | } // namespace android |