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
19#include <cutils/trace.h>
20
21namespace android {
22namespace uirenderer {
23namespace test {
24
25static const int IDENT_DISPLAYEVENT = 1;
26
27static android::DisplayInfo DUMMY_DISPLAY {
28    1080, //w
29    1920, //h
30    320.0, // xdpi
31    320.0, // ydpi
32    60.0, // fps
33    2.0, // density
34    0, // orientation
35    false, // secure?
36    0, // appVsyncOffset
37    0, // presentationDeadline
38};
39
40DisplayInfo getBuiltInDisplay() {
41#if !HWUI_NULL_GPU
42    DisplayInfo display;
43    sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
44            ISurfaceComposer::eDisplayIdMain));
45    status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &display);
46    LOG_ALWAYS_FATAL_IF(status, "Failed to get display info\n");
47    return display;
48#else
49    return DUMMY_DISPLAY;
50#endif
51}
52
53// Initialize to a dummy default
54android::DisplayInfo gDisplay = DUMMY_DISPLAY;
55
56TestContext::TestContext() {
57    mLooper = new Looper(true);
58    mSurfaceComposerClient = new SurfaceComposerClient();
59    mLooper->addFd(mDisplayEventReceiver.getFd(), IDENT_DISPLAYEVENT,
60            Looper::EVENT_INPUT, nullptr, nullptr);
61}
62
63TestContext::~TestContext() {}
64
65sp<Surface> TestContext::surface() {
66    if (!mSurface.get()) {
67        createSurface();
68    }
69    return mSurface;
70}
71
72void TestContext::createSurface() {
73    if (mRenderOffscreen) {
74        createOffscreenSurface();
75    } else {
76        createWindowSurface();
77    }
78}
79
80void TestContext::createWindowSurface() {
81    mSurfaceControl = mSurfaceComposerClient->createSurface(String8("HwuiTest"),
82            gDisplay.w, gDisplay.h, PIXEL_FORMAT_RGBX_8888);
83
84    SurfaceComposerClient::openGlobalTransaction();
85    mSurfaceControl->setLayer(0x7FFFFFF);
86    mSurfaceControl->show();
87    SurfaceComposerClient::closeGlobalTransaction();
88    mSurface = mSurfaceControl->getSurface();
89}
90
91void TestContext::createOffscreenSurface() {
92    sp<IGraphicBufferProducer> producer;
93    sp<IGraphicBufferConsumer> consumer;
94    BufferQueue::createBufferQueue(&producer, &consumer);
95    producer->setMaxDequeuedBufferCount(3);
96    producer->setAsyncMode(true);
97    mConsumer = new BufferItemConsumer(consumer, GRALLOC_USAGE_HW_COMPOSER, 4);
98    mConsumer->setDefaultBufferSize(gDisplay.w, gDisplay.h);
99    mSurface = new Surface(producer);
100}
101
102void TestContext::waitForVsync() {
103    // Hacky fix for not getting sysprop change callbacks
104    // We just poll the sysprop in vsync since it's when the UI thread is
105    // "idle" and shouldn't burn too much time
106    atrace_update_tags();
107
108    if (mConsumer.get()) {
109        BufferItem buffer;
110        if (mConsumer->acquireBuffer(&buffer, 0, false) == OK) {
111            // We assume the producer is internally ordered enough such that
112            // it is unneccessary to set a release fence
113            mConsumer->releaseBuffer(buffer);
114        }
115        // We running free, go go go!
116        return;
117    }
118#if !HWUI_NULL_GPU
119    // Request vsync
120    mDisplayEventReceiver.requestNextVsync();
121
122    // Wait
123    mLooper->pollOnce(-1);
124
125    // Drain it
126    DisplayEventReceiver::Event buf[100];
127    while (mDisplayEventReceiver.getEvents(buf, 100) > 0) { }
128#endif
129}
130
131} // namespace test
132} // namespace uirenderer
133} // namespace android
134