blob: 117257a90e346c04da34f74b1c3e1e778033a3a7 [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;
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050045 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 void cacheExternalTextureBuffer(const sp<GraphicBuffer>& buffer) override;
52 void unbindExternalTextureBuffer(uint64_t bufferId) override;
Ana Krulec9bc9dc62020-02-26 12:16:40 -080053 size_t getMaxTextureSize() const override;
54 size_t getMaxViewportDims() const override;
55
56 bool isProtected() const override;
57 bool supportsProtectedContent() const override;
58 bool useProtectedContext(bool useProtectedContext) override;
Alec Mouri368e1582020-08-13 10:14:29 -070059 bool cleanupPostRender(CleanupMode mode) override;
Ana Krulec9bc9dc62020-02-26 12:16:40 -080060
61 status_t drawLayers(const DisplaySettings& display,
62 const std::vector<const LayerSettings*>& layers,
63 const sp<GraphicBuffer>& buffer, const bool useFramebufferCache,
64 base::unique_fd&& bufferFence, base::unique_fd* drawFence) override;
Ana Krulec9bc9dc62020-02-26 12:16:40 -080065
Marin Shalamanov23c31af2020-09-09 15:01:47 +020066 void cleanFramebufferCache() override;
Alec Mourid6f09462020-12-07 11:18:17 -080067 int getContextPriority() override;
Derek Sollenbergerb3998372021-02-16 15:16:56 -050068 bool supportsBackgroundBlur() override;
Derek Sollenbergerc4a05e12021-03-24 16:45:20 -040069 void onPrimaryDisplaySizeChanged(ui::Size size) override;
Marin Shalamanov23c31af2020-09-09 15:01:47 +020070
Ana Krulec9bc9dc62020-02-26 12:16:40 -080071private:
Ana Krulec15f7cf32020-05-12 11:57:42 -070072 void threadMain(CreateInstanceFactory factory);
Ana Krulec9bc9dc62020-02-26 12:16:40 -080073
74 /* ------------------------------------------------------------------------
75 * Threading
76 */
Ady Abrahamdde984b2021-03-18 12:47:36 -070077 const char* const mThreadName = "RenderEngine";
Ana Krulec9bc9dc62020-02-26 12:16:40 -080078 // Protects the creation and destruction of mThread.
79 mutable std::mutex mThreadMutex;
80 std::thread mThread GUARDED_BY(mThreadMutex);
81 bool mRunning GUARDED_BY(mThreadMutex) = true;
82 mutable std::queue<std::function<void(renderengine::RenderEngine& instance)>> mFunctionCalls
83 GUARDED_BY(mThreadMutex);
84 mutable std::condition_variable mCondition;
85
86 /* ------------------------------------------------------------------------
87 * Render Engine
88 */
89 std::unique_ptr<renderengine::RenderEngine> mRenderEngine;
90};
91} // namespace threaded
92} // namespace renderengine
Alec Mouri368e1582020-08-13 10:14:29 -070093} // namespace android