1 /*
2  * Copyright 2018 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 
22 #include <ui/GraphicBuffer.h>
23 
24 namespace android {
25 
26 class SurfaceManager;
27 class EglManager;
28 class Program;
29 
30 class BufferGenerator {
31 public:
32     BufferGenerator();
33     ~BufferGenerator();
34 
35     /* Get a new fence */
36     status_t get(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence);
37 
38     /* Static callback that sets the fence on a particular instance */
39     static void setBuffer(const sp<GraphicBuffer>& buffer, int32_t fence, void* fenceGenerator);
40     ui::Size getSize();
41 
42 private:
43     bool mInitialized = false;
44 
45     std::unique_ptr<SurfaceManager> mSurfaceManager;
46     std::unique_ptr<EglManager> mEglManager;
47     std::unique_ptr<Program> mProgram;
48 
49     std::condition_variable_any mConditionVariable;
50 
51     sp<GraphicBuffer> mGraphicBuffer;
52     int32_t mFence = -1;
53     bool mPending = false;
54 
55     using Epoch = std::chrono::time_point<std::chrono::steady_clock>;
56     Epoch mEpoch = std::chrono::steady_clock::now();
57     ui::Size mBufferSize;
58 };
59 
60 } // namespace android
61