blob: 43ec011b734306cdc966f73a1f1282c36b0f98e3 [file] [log] [blame]
Ana Krulec9bc9dc62020-02-26 12:16:40 -08001/*
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
27namespace android {
28namespace renderengine {
29namespace threaded {
30
Ana Krulec15f7cf32020-05-12 11:57:42 -070031using CreateInstanceFactory = std::function<std::unique_ptr<renderengine::RenderEngine>()>;
32
Ana Krulec9bc9dc62020-02-26 12:16:40 -080033/**
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 Krulec15f7cf32020-05-12 11:57:42 -070038class RenderEngineThreaded : public RenderEngine {
Ana Krulec9bc9dc62020-02-26 12:16:40 -080039public:
Alec Mouri0d995102021-02-24 16:53:38 -080040 static std::unique_ptr<RenderEngineThreaded> create(CreateInstanceFactory factory,
41 RenderEngineType type);
Ana Krulec9bc9dc62020-02-26 12:16:40 -080042
Alec Mouri0d995102021-02-24 16:53:38 -080043 RenderEngineThreaded(CreateInstanceFactory factory, RenderEngineType type);
Ana Krulec9bc9dc62020-02-26 12:16:40 -080044 ~RenderEngineThreaded() override;
Ady Abrahamfe2a6db2021-06-09 15:41:37 -070045 std::future<void> primeCache() override;
Ana Krulec9bc9dc62020-02-26 12:16:40 -080046
47 void dump(std::string& result) override;
48
Ana Krulec9bc9dc62020-02-26 12:16:40 -080049 size_t getMaxTextureSize() const override;
50 size_t getMaxViewportDims() const override;
51
Ana Krulec9bc9dc62020-02-26 12:16:40 -080052 bool supportsProtectedContent() const override;
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040053 void cleanupPostRender() override;
Ana Krulec9bc9dc62020-02-26 12:16:40 -080054
Patrick Williams2e9748f2022-08-09 22:48:18 +000055 ftl::Future<FenceResult> drawLayers(const DisplaySettings& display,
56 const std::vector<LayerSettings>& layers,
57 const std::shared_ptr<ExternalTexture>& buffer,
Patrick Williams2e9748f2022-08-09 22:48:18 +000058 base::unique_fd&& bufferFence) override;
Ana Krulec9bc9dc62020-02-26 12:16:40 -080059
Alec Mourid6f09462020-12-07 11:18:17 -080060 int getContextPriority() override;
Derek Sollenbergerb3998372021-02-16 15:16:56 -050061 bool supportsBackgroundBlur() override;
Ady Abrahamed3290f2021-05-17 15:12:14 -070062 void onActiveDisplaySizeChanged(ui::Size size) override;
Matt Buckleyef51fba2021-10-12 19:30:12 +000063 std::optional<pid_t> getRenderEngineTid() const override;
Leon Scroggins IIIa37ca992022-02-02 18:08:20 -050064 void setEnableTracing(bool tracingEnabled) override;
Marin Shalamanov23c31af2020-09-09 15:01:47 +020065
Alec Mouria90a5702021-04-16 16:36:21 +000066protected:
67 void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, bool isRenderable) override;
Alec Mouri92f89fa2023-02-24 00:05:06 +000068 void unmapExternalTextureBuffer(sp<GraphicBuffer>&& buffer) override;
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040069 bool canSkipPostRenderCleanup() const override;
Patrick Williams2e9748f2022-08-09 22:48:18 +000070 void drawLayersInternal(const std::shared_ptr<std::promise<FenceResult>>&& resultPromise,
Sally Qi4cabdd02021-08-05 16:45:57 -070071 const DisplaySettings& display,
Sally Qi59a9f502021-10-12 18:53:23 +000072 const std::vector<LayerSettings>& layers,
Sally Qi4cabdd02021-08-05 16:45:57 -070073 const std::shared_ptr<ExternalTexture>& buffer,
Alec Mourif29700f2023-08-17 21:53:31 +000074 base::unique_fd&& bufferFence) override;
Alec Mouria90a5702021-04-16 16:36:21 +000075
Ana Krulec9bc9dc62020-02-26 12:16:40 -080076private:
Ana Krulec15f7cf32020-05-12 11:57:42 -070077 void threadMain(CreateInstanceFactory factory);
Derek Sollenberger4bea01e2021-04-09 13:59:37 -040078 void waitUntilInitialized() const;
Ady Abrahamfe2a6db2021-06-09 15:41:37 -070079 static status_t setSchedFifo(bool enabled);
Ana Krulec9bc9dc62020-02-26 12:16:40 -080080
Patrick Williams8aed5d22022-10-31 22:18:10 +000081 // No-op. This method is only called on leaf implementations of RenderEngine.
82 void useProtectedContext(bool) override {}
83
Ana Krulec9bc9dc62020-02-26 12:16:40 -080084 /* ------------------------------------------------------------------------
85 * Threading
86 */
Ady Abrahamdde984b2021-03-18 12:47:36 -070087 const char* const mThreadName = "RenderEngine";
Ana Krulec9bc9dc62020-02-26 12:16:40 -080088 // Protects the creation and destruction of mThread.
89 mutable std::mutex mThreadMutex;
90 std::thread mThread GUARDED_BY(mThreadMutex);
Alec Mouri39d9cb72021-06-10 10:26:15 -070091 std::atomic<bool> mRunning = true;
92
93 using Work = std::function<void(renderengine::RenderEngine&)>;
94 mutable std::queue<Work> mFunctionCalls GUARDED_BY(mThreadMutex);
Ana Krulec9bc9dc62020-02-26 12:16:40 -080095 mutable std::condition_variable mCondition;
96
Derek Sollenberger4bea01e2021-04-09 13:59:37 -040097 // 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 Krulec9bc9dc62020-02-26 12:16:40 -0800103 /* ------------------------------------------------------------------------
104 * Render Engine
105 */
106 std::unique_ptr<renderengine::RenderEngine> mRenderEngine;
107};
108} // namespace threaded
109} // namespace renderengine
Alec Mouri368e1582020-08-13 10:14:29 -0700110} // namespace android