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(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, Looper::EVENT_INPUT, nullptr,
59                   nullptr);
60}
61
62TestContext::~TestContext() {}
63
64sp<Surface> TestContext::surface() {
65    if (!mSurface.get()) {
66        createSurface();
67    }
68    return mSurface;
69}
70
71void TestContext::createSurface() {
72    if (mRenderOffscreen) {
73        createOffscreenSurface();
74    } else {
75        createWindowSurface();
76    }
77}
78
79void TestContext::createWindowSurface() {
80    mSurfaceControl = mSurfaceComposerClient->createSurface(String8("HwuiTest"), gDisplay.w,
81                                                            gDisplay.h, PIXEL_FORMAT_RGBX_8888);
82
83    SurfaceComposerClient::Transaction t;
84    t.setLayer(mSurfaceControl, 0x7FFFFFF).show(mSurfaceControl).apply();
85    mSurface = mSurfaceControl->getSurface();
86}
87
88void TestContext::createOffscreenSurface() {
89    sp<IGraphicBufferProducer> producer;
90    sp<IGraphicBufferConsumer> consumer;
91    BufferQueue::createBufferQueue(&producer, &consumer);
92    producer->setMaxDequeuedBufferCount(3);
93    producer->setAsyncMode(true);
94    mConsumer = new BufferItemConsumer(consumer, GRALLOC_USAGE_HW_COMPOSER, 4);
95    mConsumer->setDefaultBufferSize(gDisplay.w, gDisplay.h);
96    mSurface = new Surface(producer);
97}
98
99void TestContext::waitForVsync() {
100    // Hacky fix for not getting sysprop change callbacks
101    // We just poll the sysprop in vsync since it's when the UI thread is
102    // "idle" and shouldn't burn too much time
103    atrace_update_tags();
104
105    if (mConsumer.get()) {
106        BufferItem buffer;
107        if (mConsumer->acquireBuffer(&buffer, 0, false) == OK) {
108            // We assume the producer is internally ordered enough such that
109            // it is unneccessary to set a release fence
110            mConsumer->releaseBuffer(buffer);
111        }
112        // We running free, go go go!
113        return;
114    }
115#if !HWUI_NULL_GPU
116    // Request vsync
117    mDisplayEventReceiver.requestNextVsync();
118
119    // Wait
120    mLooper->pollOnce(-1);
121
122    // Drain it
123    DisplayEventReceiver::Event buf[100];
124    while (mDisplayEventReceiver.getEvents(buf, 100) > 0) {
125    }
126#endif
127}
128
129}  // namespace test
130}  // namespace uirenderer
131}  // namespace android
132