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 "tests/common/TestContext.h"
18
19namespace android {
20namespace uirenderer {
21namespace test {
22
23static const int IDENT_DISPLAYEVENT = 1;
24
25static android::DisplayInfo DUMMY_DISPLAY {
26    1080, //w
27    1920, //h
28    320.0, // xdpi
29    320.0, // ydpi
30    60.0, // fps
31    2.0, // density
32    0, // orientation
33    false, // secure?
34    0, // appVsyncOffset
35    0, // presentationDeadline
36    0, // colorTransform
37};
38
39DisplayInfo getBuiltInDisplay() {
40#if !HWUI_NULL_GPU
41    DisplayInfo display;
42    sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
43            ISurfaceComposer::eDisplayIdMain));
44    status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &display);
45    LOG_ALWAYS_FATAL_IF(status, "Failed to get display info\n");
46    return display;
47#else
48    return DUMMY_DISPLAY;
49#endif
50}
51
52// Initialize to a dummy default
53android::DisplayInfo gDisplay = DUMMY_DISPLAY;
54
55TestContext::TestContext() {
56    mLooper = new Looper(true);
57    mSurfaceComposerClient = new SurfaceComposerClient();
58    mLooper->addFd(mDisplayEventReceiver.getFd(), IDENT_DISPLAYEVENT,
59            Looper::EVENT_INPUT, nullptr, nullptr);
60}
61
62TestContext::~TestContext() {}
63
64sp<Surface> TestContext::surface() {
65    if (!mSurfaceControl.get()) {
66        mSurfaceControl = mSurfaceComposerClient->createSurface(String8("HwuiTest"),
67                gDisplay.w, gDisplay.h, PIXEL_FORMAT_RGBX_8888);
68
69        SurfaceComposerClient::openGlobalTransaction();
70        mSurfaceControl->setLayer(0x7FFFFFF);
71        mSurfaceControl->show();
72        SurfaceComposerClient::closeGlobalTransaction();
73    }
74
75    return mSurfaceControl->getSurface();
76}
77
78void TestContext::waitForVsync() {
79#if !HWUI_NULL_GPU
80    // Request vsync
81    mDisplayEventReceiver.requestNextVsync();
82
83    // Wait
84    mLooper->pollOnce(-1);
85
86    // Drain it
87    DisplayEventReceiver::Event buf[100];
88    while (mDisplayEventReceiver.getEvents(buf, 100) > 0) { }
89#endif
90}
91
92} // namespace test
93} // namespace uirenderer
94} // namespace android
95