1 // Copyright 2018 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 #pragma once 15 16 #include "AndroidBufferQueue.h" 17 #include "GrallocDispatch.h" 18 #include "Vsync.h" 19 20 #include <system/window.h> 21 22 #include "aemu/base/synchronization/Lock.h" 23 24 #include <functional> 25 #include <memory> 26 #include <vector> 27 28 namespace aemu { 29 30 class AndroidWindow; 31 32 // An abstract class for the composer implementation. 33 class Composer { 34 public: 35 Composer() = default; 36 virtual ~Composer() = default; 37 virtual void advanceFrame() = 0; 38 }; 39 40 class SurfaceFlinger { 41 public: 42 using ComposerConstructFunc = std::function<Composer*(AndroidWindow*, 43 AndroidBufferQueue*, 44 AndroidBufferQueue*)>; 45 46 SurfaceFlinger( 47 int refreshRate, 48 AndroidWindow* composeWindow, 49 std::vector<ANativeWindowBuffer*> appBuffers, 50 ComposerConstructFunc&& composerFunc, 51 Vsync::Callback vsyncFunc); 52 ~SurfaceFlinger(); 53 54 void start(); 55 void join(); 56 57 void connectWindow(AndroidWindow* window); 58 void advanceFrame(); 59 60 private: 61 void disconnectWindow(); 62 63 android::base::Lock mLock; 64 65 AndroidBufferQueue mApp2Sf; 66 AndroidBufferQueue mSf2App; 67 68 AndroidWindow* mComposeWindow = nullptr; 69 std::unique_ptr<Composer> mComposerImpl; 70 Vsync mVsync; 71 72 AndroidWindow* mCurrentWindow = nullptr; 73 }; 74 75 } // namespace aemu 76