blob: 1c7e7eef7626400ee11bc99eb3c94398f4a5dac0 [file] [log] [blame]
John Reck94c40fe2014-10-08 09:28:43 -07001/*
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
Chris Craik27e58b42015-12-07 10:01:38 -080017#include "tests/common/TestContext.h"
John Reck94c40fe2014-10-08 09:28:43 -070018
John Reck84e390c2015-01-05 09:42:52 -080019namespace android {
20namespace uirenderer {
21namespace test {
John Reck94c40fe2014-10-08 09:28:43 -070022
John Reck84e390c2015-01-05 09:42:52 -080023static const int IDENT_DISPLAYEVENT = 1;
John Reck94c40fe2014-10-08 09:28:43 -070024
John Recke702c9c2015-10-07 10:26:02 -070025static 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
John Reck84e390c2015-01-05 09:42:52 -080041 DisplayInfo display;
John Reck94c40fe2014-10-08 09:28:43 -070042 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
John Reck84e390c2015-01-05 09:42:52 -080043 ISurfaceComposer::eDisplayIdMain));
44 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &display);
John Reck94c40fe2014-10-08 09:28:43 -070045 LOG_ALWAYS_FATAL_IF(status, "Failed to get display info\n");
John Reck84e390c2015-01-05 09:42:52 -080046 return display;
John Recke702c9c2015-10-07 10:26:02 -070047#else
48 return DUMMY_DISPLAY;
49#endif
John Reck94c40fe2014-10-08 09:28:43 -070050}
51
John Recke702c9c2015-10-07 10:26:02 -070052// Initialize to a dummy default
53android::DisplayInfo gDisplay = DUMMY_DISPLAY;
John Reck94c40fe2014-10-08 09:28:43 -070054
John Reck84e390c2015-01-05 09:42:52 -080055TestContext::TestContext() {
56 mLooper = new Looper(true);
57 mSurfaceComposerClient = new SurfaceComposerClient();
58 mLooper->addFd(mDisplayEventReceiver.getFd(), IDENT_DISPLAYEVENT,
59 Looper::EVENT_INPUT, nullptr, nullptr);
John Reck94c40fe2014-10-08 09:28:43 -070060}
John Reck84e390c2015-01-05 09:42:52 -080061
62TestContext::~TestContext() {}
63
64sp<Surface> TestContext::surface() {
John Reckf1480762016-07-03 18:28:25 -070065 if (!mSurface.get()) {
66 createSurface();
John Reck84e390c2015-01-05 09:42:52 -080067 }
John Reckf1480762016-07-03 18:28:25 -070068 return mSurface;
69}
John Reck84e390c2015-01-05 09:42:52 -080070
John Reckf1480762016-07-03 18:28:25 -070071void 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);
John Reck84e390c2015-01-05 09:42:52 -080099}
100
101void TestContext::waitForVsync() {
John Reckf1480762016-07-03 18:28:25 -0700102 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 }
John Reckc36df952015-07-29 10:09:36 -0700112#if !HWUI_NULL_GPU
John Reck84e390c2015-01-05 09:42:52 -0800113 // 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) { }
John Reckc36df952015-07-29 10:09:36 -0700122#endif
John Reck84e390c2015-01-05 09:42:52 -0800123}
124
125} // namespace test
126} // namespace uirenderer
127} // namespace android