TestContext.cpp revision f1480761c1a83aecd09cdd473ec797a41d1a2f3f
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 (!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"),
81            gDisplay.w, gDisplay.h, PIXEL_FORMAT_RGBX_8888);
82
83    SurfaceComposerClient::openGlobalTransaction();
84    mSurfaceControl->setLayer(0x7FFFFFF);
85    mSurfaceControl->show();
86    SurfaceComposerClient::closeGlobalTransaction();
87    mSurface = mSurfaceControl->getSurface();
88}
89
90void TestContext::createOffscreenSurface() {
91    sp<IGraphicBufferProducer> producer;
92    sp<IGraphicBufferConsumer> consumer;
93    BufferQueue::createBufferQueue(&producer, &consumer);
94    producer->setMaxDequeuedBufferCount(3);
95    producer->setAsyncMode(true);
96    mConsumer = new BufferItemConsumer(consumer, GRALLOC_USAGE_HW_COMPOSER, 4);
97    mConsumer->setDefaultBufferSize(gDisplay.w, gDisplay.h);
98    mSurface = new Surface(producer);
99}
100
101void TestContext::waitForVsync() {
102    if (mConsumer.get()) {
103        BufferItem buffer;
104        if (mConsumer->acquireBuffer(&buffer, 0, false) == OK) {
105            // We assume the producer is internally ordered enough such that
106            // it is unneccessary to set a release fence
107            mConsumer->releaseBuffer(buffer);
108        }
109        // We running free, go go go!
110        return;
111    }
112#if !HWUI_NULL_GPU
113    // Request vsync
114    mDisplayEventReceiver.requestNextVsync();
115
116    // Wait
117    mLooper->pollOnce(-1);
118
119    // Drain it
120    DisplayEventReceiver::Event buf[100];
121    while (mDisplayEventReceiver.getEvents(buf, 100) > 0) { }
122#endif
123}
124
125} // namespace test
126} // namespace uirenderer
127} // namespace android
128