blob: 8b1e2dec5a65dc777c5112dfaead8f6c1c946e73 [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:
Ana Krulec15f7cf32020-05-12 11:57:42 -070040 static std::unique_ptr<RenderEngineThreaded> create(CreateInstanceFactory factory);
Ana Krulec9bc9dc62020-02-26 12:16:40 -080041
Ana Krulec15f7cf32020-05-12 11:57:42 -070042 RenderEngineThreaded(CreateInstanceFactory factory);
Ana Krulec9bc9dc62020-02-26 12:16:40 -080043 ~RenderEngineThreaded() override;
44 void primeCache() const override;
45
46 void dump(std::string& result) override;
47
48 void genTextures(size_t count, uint32_t* names) override;
49 void deleteTextures(size_t count, uint32_t const* names) override;
Ana Krulec9bc9dc62020-02-26 12:16:40 -080050 void cacheExternalTextureBuffer(const sp<GraphicBuffer>& buffer) override;
51 void unbindExternalTextureBuffer(uint64_t bufferId) override;
Ana Krulec9bc9dc62020-02-26 12:16:40 -080052 size_t getMaxTextureSize() const override;
53 size_t getMaxViewportDims() const override;
54
55 bool isProtected() const override;
56 bool supportsProtectedContent() const override;
57 bool useProtectedContext(bool useProtectedContext) override;
Alec Mouri368e1582020-08-13 10:14:29 -070058 bool cleanupPostRender(CleanupMode mode) override;
Ana Krulec9bc9dc62020-02-26 12:16:40 -080059
60 status_t drawLayers(const DisplaySettings& display,
61 const std::vector<const LayerSettings*>& layers,
62 const sp<GraphicBuffer>& buffer, const bool useFramebufferCache,
63 base::unique_fd&& bufferFence, base::unique_fd* drawFence) 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;
Marin Shalamanov23c31af2020-09-09 15:01:47 +020067
Ana Krulec9bc9dc62020-02-26 12:16:40 -080068private:
Ana Krulec15f7cf32020-05-12 11:57:42 -070069 void threadMain(CreateInstanceFactory factory);
Ana Krulec9bc9dc62020-02-26 12:16:40 -080070
71 /* ------------------------------------------------------------------------
72 * Threading
73 */
74 const char* const mThreadName = "RenderEngineThread";
75 // Protects the creation and destruction of mThread.
76 mutable std::mutex mThreadMutex;
77 std::thread mThread GUARDED_BY(mThreadMutex);
78 bool mRunning GUARDED_BY(mThreadMutex) = true;
79 mutable std::queue<std::function<void(renderengine::RenderEngine& instance)>> mFunctionCalls
80 GUARDED_BY(mThreadMutex);
81 mutable std::condition_variable mCondition;
82
83 /* ------------------------------------------------------------------------
84 * Render Engine
85 */
86 std::unique_ptr<renderengine::RenderEngine> mRenderEngine;
87};
88} // namespace threaded
89} // namespace renderengine
Alec Mouri368e1582020-08-13 10:14:29 -070090} // namespace android