1/*
2 * Copyright (C) 2014 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#include "TestContext.h"
18
19namespace android {
20namespace uirenderer {
21namespace test {
22
23static const int IDENT_DISPLAYEVENT = 1;
24
25static DisplayInfo getBuiltInDisplay() {
26    DisplayInfo display;
27    sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
28            ISurfaceComposer::eDisplayIdMain));
29    status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &display);
30    LOG_ALWAYS_FATAL_IF(status, "Failed to get display info\n");
31    return display;
32}
33
34android::DisplayInfo gDisplay = getBuiltInDisplay();
35
36TestContext::TestContext() {
37    mLooper = new Looper(true);
38    mSurfaceComposerClient = new SurfaceComposerClient();
39    mLooper->addFd(mDisplayEventReceiver.getFd(), IDENT_DISPLAYEVENT,
40            Looper::EVENT_INPUT, nullptr, nullptr);
41}
42
43TestContext::~TestContext() {}
44
45sp<Surface> TestContext::surface() {
46    if (!mSurfaceControl.get()) {
47        mSurfaceControl = mSurfaceComposerClient->createSurface(String8("HwuiTest"),
48                gDisplay.w, gDisplay.h, PIXEL_FORMAT_RGBX_8888);
49
50        SurfaceComposerClient::openGlobalTransaction();
51        mSurfaceControl->setLayer(0x7FFFFFF);
52        mSurfaceControl->show();
53        SurfaceComposerClient::closeGlobalTransaction();
54    }
55
56    return mSurfaceControl->getSurface();
57}
58
59void TestContext::waitForVsync() {
60#if HWUI_NULL_GPU
61    return;
62#endif
63
64    // Request vsync
65    mDisplayEventReceiver.requestNextVsync();
66
67    // Wait
68    mLooper->pollOnce(-1);
69
70    // Drain it
71    DisplayEventReceiver::Event buf[100];
72    while (mDisplayEventReceiver.getEvents(buf, 100) > 0) { }
73}
74
75} // namespace test
76} // namespace uirenderer
77} // namespace android
78