blob: 2303caa7ebc30d3a4ff9fe978298f7e355184bbf [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
Sally Qi4cabdd02021-08-05 16:45:57 -070059 std::future<RenderEngineResult> drawLayers(const DisplaySettings& display,
Vladimir Marko21092702021-10-12 12:43:47 +000060 const std::vector<const LayerSettings*>& layers,
Sally Qi4cabdd02021-08-05 16:45:57 -070061 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;
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;
Sally Qi4cabdd02021-08-05 16:45:57 -070074 void drawLayersInternal(const std::shared_ptr<std::promise<RenderEngineResult>>&& resultPromise,
75 const DisplaySettings& display,
Vladimir Marko21092702021-10-12 12:43:47 +000076 const std::vector<const 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
85 /* ------------------------------------------------------------------------
86 * Threading
87 */
Ady Abrahamdde984b2021-03-18 12:47:36 -070088 const char* const mThreadName = "RenderEngine";
Ana Krulec9bc9dc62020-02-26 12:16:40 -080089 // Protects the creation and destruction of mThread.
90 mutable std::mutex mThreadMutex;
91 std::thread mThread GUARDED_BY(mThreadMutex);
Alec Mouri39d9cb72021-06-10 10:26:15 -070092 std::atomic<bool> mRunning = true;
93
94 using Work = std::function<void(renderengine::RenderEngine&)>;
95 mutable std::queue<Work> mFunctionCalls GUARDED_BY(mThreadMutex);
Ana Krulec9bc9dc62020-02-26 12:16:40 -080096 mutable std::condition_variable mCondition;
97
Derek Sollenberger4bea01e2021-04-09 13:59:37 -040098 // Used to allow select thread safe methods to be accessed without requiring the
99 // method to be invoked on the RenderEngine thread
100 bool mIsInitialized = false;
101 mutable std::mutex mInitializedMutex;
102 mutable std::condition_variable mInitializedCondition;
103
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800104 /* ------------------------------------------------------------------------
105 * Render Engine
106 */
107 std::unique_ptr<renderengine::RenderEngine> mRenderEngine;
Derek Sollenberger1ec2fb52021-06-16 15:11:27 -0400108 std::atomic<bool> mIsProtected = false;
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800109};
110} // namespace threaded
111} // namespace renderengine
Alec Mouri368e1582020-08-13 10:14:29 -0700112} // namespace android