Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 27 | namespace android { |
| 28 | namespace renderengine { |
| 29 | namespace threaded { |
| 30 | |
Ana Krulec | 15f7cf3 | 2020-05-12 11:57:42 -0700 | [diff] [blame] | 31 | using CreateInstanceFactory = std::function<std::unique_ptr<renderengine::RenderEngine>()>; |
| 32 | |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 33 | /** |
| 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 Krulec | 15f7cf3 | 2020-05-12 11:57:42 -0700 | [diff] [blame] | 38 | class RenderEngineThreaded : public RenderEngine { |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 39 | public: |
Ana Krulec | 15f7cf3 | 2020-05-12 11:57:42 -0700 | [diff] [blame] | 40 | static std::unique_ptr<RenderEngineThreaded> create(CreateInstanceFactory factory); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 41 | |
Ana Krulec | 15f7cf3 | 2020-05-12 11:57:42 -0700 | [diff] [blame] | 42 | RenderEngineThreaded(CreateInstanceFactory factory); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 43 | ~RenderEngineThreaded() override; |
| 44 | void primeCache() const override; |
| 45 | |
| 46 | void dump(std::string& result) override; |
| 47 | |
Ana Krulec | 15f7cf3 | 2020-05-12 11:57:42 -0700 | [diff] [blame] | 48 | bool useNativeFenceSync() const override; |
| 49 | bool useWaitSync() const override; |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 50 | void genTextures(size_t count, uint32_t* names) override; |
| 51 | void deleteTextures(size_t count, uint32_t const* names) override; |
| 52 | void bindExternalTextureImage(uint32_t texName, const Image& image) override; |
| 53 | status_t bindExternalTextureBuffer(uint32_t texName, const sp<GraphicBuffer>& buffer, |
| 54 | const sp<Fence>& fence) override; |
| 55 | void cacheExternalTextureBuffer(const sp<GraphicBuffer>& buffer) override; |
| 56 | void unbindExternalTextureBuffer(uint64_t bufferId) override; |
| 57 | status_t bindFrameBuffer(Framebuffer* framebuffer) override; |
| 58 | void unbindFrameBuffer(Framebuffer* framebuffer) override; |
| 59 | size_t getMaxTextureSize() const override; |
| 60 | size_t getMaxViewportDims() const override; |
| 61 | |
| 62 | bool isProtected() const override; |
| 63 | bool supportsProtectedContent() const override; |
| 64 | bool useProtectedContext(bool useProtectedContext) override; |
| 65 | bool cleanupPostRender() override; |
| 66 | |
| 67 | status_t drawLayers(const DisplaySettings& display, |
| 68 | const std::vector<const LayerSettings*>& layers, |
| 69 | const sp<GraphicBuffer>& buffer, const bool useFramebufferCache, |
| 70 | base::unique_fd&& bufferFence, base::unique_fd* drawFence) override; |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 71 | |
| 72 | protected: |
| 73 | Framebuffer* getFramebufferForDrawing() override; |
| 74 | |
| 75 | private: |
Ana Krulec | 15f7cf3 | 2020-05-12 11:57:42 -0700 | [diff] [blame] | 76 | void threadMain(CreateInstanceFactory factory); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 77 | |
| 78 | /* ------------------------------------------------------------------------ |
| 79 | * Threading |
| 80 | */ |
| 81 | const char* const mThreadName = "RenderEngineThread"; |
| 82 | // Protects the creation and destruction of mThread. |
| 83 | mutable std::mutex mThreadMutex; |
| 84 | std::thread mThread GUARDED_BY(mThreadMutex); |
| 85 | bool mRunning GUARDED_BY(mThreadMutex) = true; |
| 86 | mutable std::queue<std::function<void(renderengine::RenderEngine& instance)>> mFunctionCalls |
| 87 | GUARDED_BY(mThreadMutex); |
| 88 | mutable std::condition_variable mCondition; |
| 89 | |
| 90 | /* ------------------------------------------------------------------------ |
| 91 | * Render Engine |
| 92 | */ |
| 93 | std::unique_ptr<renderengine::RenderEngine> mRenderEngine; |
| 94 | }; |
| 95 | } // namespace threaded |
| 96 | } // namespace renderengine |
| 97 | } // namespace android |