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 #include "SurfaceFlinger.h"
15
16 #include "AndroidWindow.h"
17
18 #include <stdio.h>
19
20 #define E(fmt,...) \
21 fprintf(stderr, "%s: " fmt "\n", __func__, ##__VA_ARGS__);
22
23 using android::base::AutoLock;
24 using android::base::Lock;
25
26 namespace aemu {
27
SurfaceFlinger(int refreshRate,AndroidWindow * composeWindow,std::vector<ANativeWindowBuffer * > appBuffers,SurfaceFlinger::ComposerConstructFunc && composerFunc,Vsync::Callback vsyncFunc)28 SurfaceFlinger::SurfaceFlinger(
29 int refreshRate,
30 AndroidWindow* composeWindow,
31 std::vector<ANativeWindowBuffer*> appBuffers,
32 SurfaceFlinger::ComposerConstructFunc&& composerFunc,
33 Vsync::Callback vsyncFunc)
34 : mComposeWindow(composeWindow),
35 mComposerImpl(composerFunc(mComposeWindow, &mApp2Sf, &mSf2App)),
36 mVsync(refreshRate, std::move(vsyncFunc)) {
37
38 for (auto buffer : appBuffers) {
39 mSf2App.queueBuffer(AndroidBufferQueue::Item(buffer));
40 }
41 }
42
~SurfaceFlinger()43 SurfaceFlinger::~SurfaceFlinger() {
44 join();
45 }
46
start()47 void SurfaceFlinger::start() {
48 mVsync.start();
49 }
50
join()51 void SurfaceFlinger::join() {
52 mVsync.join();
53 }
54
connectWindow(AndroidWindow * window)55 void SurfaceFlinger::connectWindow(AndroidWindow* window) {
56 AutoLock lock(mLock);
57
58 disconnectWindow();
59
60 if (!window) return;
61
62 window->setProducer(&mSf2App, &mApp2Sf);
63
64 // and prime the queue again
65 AndroidBufferQueue::Item item;
66 while (mApp2Sf.try_dequeueBuffer(&item)) {
67 mSf2App.queueBuffer(item);
68 }
69
70 mCurrentWindow = window;
71 }
72
advanceFrame()73 void SurfaceFlinger::advanceFrame() {
74 mComposerImpl->advanceFrame();
75 }
76
disconnectWindow()77 void SurfaceFlinger::disconnectWindow() {
78 if (!mCurrentWindow) return;
79
80 mCurrentWindow->setProducer(nullptr, nullptr);
81
82 mCurrentWindow = nullptr;
83 }
84
85 } // namespace aemu
86