blob: be67de8367c589637f1a037a9d803f3036a33356 [file] [log] [blame]
Alec Mouri16a99402019-07-29 16:37:30 -07001/*
2 * Copyright 2019 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 <condition_variable>
20#include <mutex>
21#include <queue>
22#include <thread>
23
24#include <ui/GraphicBuffer.h>
25
26namespace android {
27namespace renderengine {
28namespace gl {
29
30class GLESRenderEngine;
31
32class ImageManager {
33public:
34 struct Barrier {
35 std::mutex mutex;
36 std::condition_variable_any condition;
37 bool isOpen GUARDED_BY(mutex) = false;
38 status_t result GUARDED_BY(mutex) = NO_ERROR;
39 };
40 ImageManager(GLESRenderEngine* engine);
41 ~ImageManager();
Alec Mouri349fd2d2020-02-27 12:06:10 -080042 // Starts the background thread for the ImageManager
43 // We need this to guarantee that the class is fully-constructed before the
44 // thread begins running.
45 void initThread();
Alec Mouri16a99402019-07-29 16:37:30 -070046 void cacheAsync(const sp<GraphicBuffer>& buffer, const std::shared_ptr<Barrier>& barrier)
47 EXCLUDES(mMutex);
48 status_t cache(const sp<GraphicBuffer>& buffer);
49 void releaseAsync(uint64_t bufferId, const std::shared_ptr<Barrier>& barrier) EXCLUDES(mMutex);
50
51private:
52 struct QueueEntry {
53 enum class Operation { Delete, Insert };
54
55 Operation op = Operation::Delete;
56 sp<GraphicBuffer> buffer = nullptr;
57 uint64_t bufferId = 0;
58 std::shared_ptr<Barrier> barrier = nullptr;
59 };
60
61 void queueOperation(const QueueEntry&& entry);
62 void threadMain();
63 GLESRenderEngine* const mEngine;
Alec Mouri349fd2d2020-02-27 12:06:10 -080064 std::thread mThread;
Alec Mouri16a99402019-07-29 16:37:30 -070065 std::condition_variable_any mCondition;
66 std::mutex mMutex;
67 std::queue<QueueEntry> mQueue GUARDED_BY(mMutex);
68
69 bool mRunning GUARDED_BY(mMutex) = true;
70};
71
72} // namespace gl
73} // namespace renderengine
74} // namespace android