1/*
2 * Copyright 2016 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#ifndef ANDROID_SURFACEREPLAYER_BUFFERQUEUESCHEDULER_H
18#define ANDROID_SURFACEREPLAYER_BUFFERQUEUESCHEDULER_H
19
20#include "Color.h"
21#include "Event.h"
22
23#include <gui/SurfaceControl.h>
24
25#include <utils/StrongPointer.h>
26
27#include <atomic>
28#include <condition_variable>
29#include <mutex>
30#include <queue>
31#include <utility>
32
33namespace android {
34
35auto constexpr LAYER_ALPHA = 190;
36
37struct Dimensions {
38    Dimensions() = default;
39    Dimensions(int w, int h) : width(w), height(h) {}
40
41    int width = 0;
42    int height = 0;
43};
44
45struct BufferEvent {
46    BufferEvent() = default;
47    BufferEvent(std::shared_ptr<Event> e, Dimensions d) : event(e), dimensions(d) {}
48
49    std::shared_ptr<Event> event;
50    Dimensions dimensions;
51};
52
53class BufferQueueScheduler {
54  public:
55    BufferQueueScheduler(const sp<SurfaceControl>& surfaceControl, const HSV& color, int id);
56
57    void startScheduling();
58    void addEvent(const BufferEvent&);
59    void stopScheduling();
60
61    void setSurfaceControl(const sp<SurfaceControl>& surfaceControl, const HSV& color);
62
63  private:
64    void bufferUpdate(const Dimensions& dimensions);
65
66    // Lock and fill the surface, block until the event is signaled by the main loop,
67    // then unlock and post the buffer.
68    void fillSurface(const std::shared_ptr<Event>& event);
69
70    sp<SurfaceControl> mSurfaceControl;
71    HSV mColor;
72    const int mSurfaceId;
73
74    bool mContinueScheduling;
75
76    std::queue<BufferEvent> mBufferEvents;
77    std::mutex mMutex;
78    std::condition_variable mCondition;
79};
80
81}  // namespace android
82#endif
83