Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 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 | |
| 33 | namespace android { |
| 34 | |
| 35 | auto constexpr LAYER_ALPHA = 190; |
| 36 | |
| 37 | struct 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 | |
| 45 | struct 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 | |
| 53 | class BufferQueueScheduler { |
| 54 | public: |
Sahil Dhanju | 01041fe | 2016-08-08 20:27:23 -0700 | [diff] [blame] | 55 | BufferQueueScheduler(const sp<SurfaceControl>& surfaceControl, const HSV& color, int id); |
Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 56 | |
| 57 | void startScheduling(); |
| 58 | void addEvent(const BufferEvent&); |
| 59 | void stopScheduling(); |
| 60 | |
Sahil Dhanju | 01041fe | 2016-08-08 20:27:23 -0700 | [diff] [blame] | 61 | void setSurfaceControl(const sp<SurfaceControl>& surfaceControl, const HSV& color); |
Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 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. |
Sahil Dhanju | 01041fe | 2016-08-08 20:27:23 -0700 | [diff] [blame] | 68 | void fillSurface(const std::shared_ptr<Event>& event); |
Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 69 | |
| 70 | sp<SurfaceControl> mSurfaceControl; |
Sahil Dhanju | 01041fe | 2016-08-08 20:27:23 -0700 | [diff] [blame] | 71 | HSV mColor; |
Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 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 |