blob: bf2ebea2a06b33fcee3f0823f1ede0d3ff1da329 [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
49 void genTextures(size_t count, uint32_t* names) override;
50 void deleteTextures(size_t count, uint32_t const* names) override;
Ana Krulec9bc9dc62020-02-26 12:16:40 -080051 size_t getMaxTextureSize() const override;
52 size_t getMaxViewportDims() const override;
53
54 bool isProtected() const override;
55 bool supportsProtectedContent() const override;
Derek Sollenberger1ec2fb52021-06-16 15:11:27 -040056 void useProtectedContext(bool useProtectedContext) override;
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040057 void cleanupPostRender() override;
Ana Krulec9bc9dc62020-02-26 12:16:40 -080058
Patrick Williams2e9748f2022-08-09 22:48:18 +000059 ftl::Future<FenceResult> drawLayers(const DisplaySettings& display,
60 const std::vector<LayerSettings>& layers,
61 const std::shared_ptr<ExternalTexture>& buffer,
62 const bool useFramebufferCache,
63 base::unique_fd&& bufferFence) override;
Ana Krulec9bc9dc62020-02-26 12:16:40 -080064
Marin Shalamanov23c31af2020-09-09 15:01:47 +020065 void cleanFramebufferCache() override;
Alec Mourid6f09462020-12-07 11:18:17 -080066 int getContextPriority() override;
Derek Sollenbergerb3998372021-02-16 15:16:56 -050067 bool supportsBackgroundBlur() override;
Ady Abrahamed3290f2021-05-17 15:12:14 -070068 void onActiveDisplaySizeChanged(ui::Size size) override;
Matt Buckleyef51fba2021-10-12 19:30:12 +000069 std::optional<pid_t> getRenderEngineTid() const override;
Leon Scroggins IIIa37ca992022-02-02 18:08:20 -050070 void setEnableTracing(bool tracingEnabled) override;
Marin Shalamanov23c31af2020-09-09 15:01:47 +020071
Alec Mouria90a5702021-04-16 16:36:21 +000072protected:
73 void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, bool isRenderable) override;
74 void unmapExternalTextureBuffer(const sp<GraphicBuffer>& buffer) override;
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040075 bool canSkipPostRenderCleanup() const override;
Patrick Williams2e9748f2022-08-09 22:48:18 +000076 void drawLayersInternal(const std::shared_ptr<std::promise<FenceResult>>&& resultPromise,
Sally Qi4cabdd02021-08-05 16:45:57 -070077 const DisplaySettings& display,
Sally Qi59a9f502021-10-12 18:53:23 +000078 const std::vector<LayerSettings>& layers,
Sally Qi4cabdd02021-08-05 16:45:57 -070079 const std::shared_ptr<ExternalTexture>& buffer,
80 const bool useFramebufferCache, base::unique_fd&& bufferFence) override;
Alec Mouria90a5702021-04-16 16:36:21 +000081
Ana Krulec9bc9dc62020-02-26 12:16:40 -080082private:
Ana Krulec15f7cf32020-05-12 11:57:42 -070083 void threadMain(CreateInstanceFactory factory);
Derek Sollenberger4bea01e2021-04-09 13:59:37 -040084 void waitUntilInitialized() const;
Ady Abrahamfe2a6db2021-06-09 15:41:37 -070085 static status_t setSchedFifo(bool enabled);
Ana Krulec9bc9dc62020-02-26 12:16:40 -080086
87 /* ------------------------------------------------------------------------
88 * Threading
89 */
Ady Abrahamdde984b2021-03-18 12:47:36 -070090 const char* const mThreadName = "RenderEngine";
Ana Krulec9bc9dc62020-02-26 12:16:40 -080091 // Protects the creation and destruction of mThread.
92 mutable std::mutex mThreadMutex;
93 std::thread mThread GUARDED_BY(mThreadMutex);
Alec Mouri39d9cb72021-06-10 10:26:15 -070094 std::atomic<bool> mRunning = true;
95
96 using Work = std::function<void(renderengine::RenderEngine&)>;
97 mutable std::queue<Work> mFunctionCalls GUARDED_BY(mThreadMutex);
Ana Krulec9bc9dc62020-02-26 12:16:40 -080098 mutable std::condition_variable mCondition;
99
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400100 // Used to allow select thread safe methods to be accessed without requiring the
101 // method to be invoked on the RenderEngine thread
102 bool mIsInitialized = false;
103 mutable std::mutex mInitializedMutex;
104 mutable std::condition_variable mInitializedCondition;
105
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800106 /* ------------------------------------------------------------------------
107 * Render Engine
108 */
109 std::unique_ptr<renderengine::RenderEngine> mRenderEngine;
Derek Sollenberger1ec2fb52021-06-16 15:11:27 -0400110 std::atomic<bool> mIsProtected = false;
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800111};
112} // namespace threaded
113} // namespace renderengine
Alec Mouri368e1582020-08-13 10:14:29 -0700114} // namespace android