blob: 168e2d2b06208b42824bffec0edc2864ec1ac43b [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
Ana Krulec9bc9dc62020-02-26 12:16:40 -080054 bool supportsProtectedContent() const override;
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040055 void cleanupPostRender() override;
Ana Krulec9bc9dc62020-02-26 12:16:40 -080056
Patrick Williams2e9748f2022-08-09 22:48:18 +000057 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;
Ana Krulec9bc9dc62020-02-26 12:16:40 -080062
Marin Shalamanov23c31af2020-09-09 15:01:47 +020063 void cleanFramebufferCache() override;
Alec Mourid6f09462020-12-07 11:18:17 -080064 int getContextPriority() override;
Derek Sollenbergerb3998372021-02-16 15:16:56 -050065 bool supportsBackgroundBlur() override;
Ady Abrahamed3290f2021-05-17 15:12:14 -070066 void onActiveDisplaySizeChanged(ui::Size size) override;
Matt Buckleyef51fba2021-10-12 19:30:12 +000067 std::optional<pid_t> getRenderEngineTid() const override;
Leon Scroggins IIIa37ca992022-02-02 18:08:20 -050068 void setEnableTracing(bool tracingEnabled) override;
Marin Shalamanov23c31af2020-09-09 15:01:47 +020069
Alec Mouria90a5702021-04-16 16:36:21 +000070protected:
71 void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, bool isRenderable) override;
72 void unmapExternalTextureBuffer(const sp<GraphicBuffer>& buffer) override;
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040073 bool canSkipPostRenderCleanup() const override;
Patrick Williams2e9748f2022-08-09 22:48:18 +000074 void drawLayersInternal(const std::shared_ptr<std::promise<FenceResult>>&& resultPromise,
Sally Qi4cabdd02021-08-05 16:45:57 -070075 const DisplaySettings& display,
Sally Qi59a9f502021-10-12 18:53:23 +000076 const std::vector<LayerSettings>& layers,
Sally Qi4cabdd02021-08-05 16:45:57 -070077 const std::shared_ptr<ExternalTexture>& buffer,
78 const bool useFramebufferCache, base::unique_fd&& bufferFence) override;
Alec Mouria90a5702021-04-16 16:36:21 +000079
Ana Krulec9bc9dc62020-02-26 12:16:40 -080080private:
Ana Krulec15f7cf32020-05-12 11:57:42 -070081 void threadMain(CreateInstanceFactory factory);
Derek Sollenberger4bea01e2021-04-09 13:59:37 -040082 void waitUntilInitialized() const;
Ady Abrahamfe2a6db2021-06-09 15:41:37 -070083 static status_t setSchedFifo(bool enabled);
Ana Krulec9bc9dc62020-02-26 12:16:40 -080084
Patrick Williams8aed5d22022-10-31 22:18:10 +000085 // No-op. This method is only called on leaf implementations of RenderEngine.
86 void useProtectedContext(bool) override {}
87
Ana Krulec9bc9dc62020-02-26 12:16:40 -080088 /* ------------------------------------------------------------------------
89 * Threading
90 */
Ady Abrahamdde984b2021-03-18 12:47:36 -070091 const char* const mThreadName = "RenderEngine";
Ana Krulec9bc9dc62020-02-26 12:16:40 -080092 // Protects the creation and destruction of mThread.
93 mutable std::mutex mThreadMutex;
94 std::thread mThread GUARDED_BY(mThreadMutex);
Alec Mouri39d9cb72021-06-10 10:26:15 -070095 std::atomic<bool> mRunning = true;
96
97 using Work = std::function<void(renderengine::RenderEngine&)>;
98 mutable std::queue<Work> mFunctionCalls GUARDED_BY(mThreadMutex);
Ana Krulec9bc9dc62020-02-26 12:16:40 -080099 mutable std::condition_variable mCondition;
100
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400101 // 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
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800107 /* ------------------------------------------------------------------------
108 * Render Engine
109 */
110 std::unique_ptr<renderengine::RenderEngine> mRenderEngine;
111};
112} // namespace threaded
113} // namespace renderengine
Alec Mouri368e1582020-08-13 10:14:29 -0700114} // namespace android