blob: c63d251c31e885f72eabe62540a6c1c74e750cf2 [file] [log] [blame]
Valerie Hau9dab9732019-08-20 09:29:25 -07001/*
2 * Copyright (C) 2019 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
Valerie Hau9dab9732019-08-20 09:29:25 -070017#include <binder/IInterface.h>
18#include <binder/IPCThreadState.h>
19#include <binder/IServiceManager.h>
20#include <binder/ProcessState.h>
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080021#include <gtest/gtest.h>
Valerie Hau9dab9732019-08-20 09:29:25 -070022#include <gui/ISurfaceComposer.h>
23#include <gui/LayerState.h>
24#include <gui/Surface.h>
25#include <gui/SurfaceComposerClient.h>
Marin Shalamanova7fe3042021-01-29 21:02:08 +010026#include <ui/DisplayMode.h>
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080027#include <utils/String8.h>
Valerie Hau9dab9732019-08-20 09:29:25 -070028
29#include <limits>
30
chaviwd7deef72021-10-06 11:53:40 -050031#include <gui/test/CallbackUtils.h>
Valerie Hau9dab9732019-08-20 09:29:25 -070032#include "BufferGenerator.h"
Valerie Hau9dab9732019-08-20 09:29:25 -070033#include "utils/ColorUtils.h"
34#include "utils/TransactionUtils.h"
35
36namespace android {
37
38namespace test {
39
40using Transaction = SurfaceComposerClient::Transaction;
41using CallbackInfo = SurfaceComposerClient::CallbackInfo;
42using TCLHash = SurfaceComposerClient::TCLHash;
43using android::hardware::graphics::common::V1_1::BufferUsage;
44
45class TransactionHelper : public Transaction {
46public:
47 size_t getNumListeners() { return mListenerCallbacks.size(); }
48
49 std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash>
50 getListenerCallbacks() {
51 return mListenerCallbacks;
52 }
53};
54
55class IPCTestUtils {
56public:
57 static void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
58 bool finalState = false);
59 static status_t getBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence);
60};
61
62class IIPCTest : public IInterface {
63public:
64 DECLARE_META_INTERFACE(IPCTest)
65 enum class Tag : uint32_t {
66 SetDeathToken = IBinder::FIRST_CALL_TRANSACTION,
67 InitClient,
68 CreateTransaction,
69 MergeAndApply,
70 VerifyCallbacks,
71 CleanUp,
72 Last,
73 };
74
75 virtual status_t setDeathToken(sp<IBinder>& token) = 0;
76
77 virtual status_t initClient() = 0;
78
79 virtual status_t createTransaction(TransactionHelper* outTransaction, uint32_t width,
80 uint32_t height) = 0;
81
82 virtual status_t mergeAndApply(TransactionHelper transaction) = 0;
83
84 virtual status_t verifyCallbacks() = 0;
85
86 virtual status_t cleanUp() = 0;
87};
88
89class BpIPCTest : public SafeBpInterface<IIPCTest> {
90public:
91 explicit BpIPCTest(const sp<IBinder>& impl) : SafeBpInterface<IIPCTest>(impl, "BpIPCTest") {}
92
93 status_t setDeathToken(sp<IBinder>& token) {
94 return callRemote<decltype(&IIPCTest::setDeathToken)>(Tag::SetDeathToken, token);
95 }
96
97 status_t initClient() { return callRemote<decltype(&IIPCTest::initClient)>(Tag::InitClient); }
98
99 status_t createTransaction(TransactionHelper* transaction, uint32_t width, uint32_t height) {
100 return callRemote<decltype(&IIPCTest::createTransaction)>(Tag::CreateTransaction,
101 transaction, width, height);
102 }
103
104 status_t mergeAndApply(TransactionHelper transaction) {
105 return callRemote<decltype(&IIPCTest::mergeAndApply)>(Tag::MergeAndApply, transaction);
106 }
107
108 status_t verifyCallbacks() {
109 return callRemote<decltype(&IIPCTest::verifyCallbacks)>(Tag::VerifyCallbacks);
110 }
111
112 status_t cleanUp() { return callRemote<decltype(&IIPCTest::cleanUp)>(Tag::CleanUp); }
113};
114
115IMPLEMENT_META_INTERFACE(IPCTest, "android.gfx.tests.IIPCTest")
116
117class onTestDeath : public IBinder::DeathRecipient {
118public:
119 void binderDied(const wp<IBinder>& /*who*/) override {
120 ALOGE("onTestDeath::binderDied, exiting");
121 exit(0);
122 }
123};
124
125sp<onTestDeath> getDeathToken() {
126 static sp<onTestDeath> token = new onTestDeath;
127 return token;
128}
129
130class BnIPCTest : public SafeBnInterface<IIPCTest> {
131public:
132 BnIPCTest() : SafeBnInterface("BnIPCTest") {}
133
134 status_t setDeathToken(sp<IBinder>& token) override {
135 return token->linkToDeath(getDeathToken());
136 }
137
138 status_t initClient() override {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700139 mClient = sp<SurfaceComposerClient>::make();
Valerie Hau9dab9732019-08-20 09:29:25 -0700140 auto err = mClient->initCheck();
141 return err;
142 }
143
144 status_t createTransaction(TransactionHelper* transaction, uint32_t width, uint32_t height) {
145 if (transaction == nullptr) {
146 ALOGE("Error in createTransaction: transaction is nullptr");
147 return BAD_VALUE;
148 }
149 mSurfaceControl = mClient->createSurface(String8("parentProcessSurface"), 0, 0,
150 PIXEL_FORMAT_RGBA_8888,
151 ISurfaceComposerClient::eFXSurfaceBufferState,
152 /*parent*/ nullptr);
153 sp<GraphicBuffer> gb;
154 sp<Fence> fence;
155 int err = IPCTestUtils::getBuffer(&gb, &fence);
156 if (err != NO_ERROR) return err;
157
158 TransactionUtils::fillGraphicBufferColor(gb,
159 {0, 0, static_cast<int32_t>(width),
160 static_cast<int32_t>(height)},
161 Color::RED);
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700162 transaction->setLayerStack(mSurfaceControl, ui::DEFAULT_LAYER_STACK)
Valerie Hau9dab9732019-08-20 09:29:25 -0700163 .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max())
chaviwba4320c2021-09-15 15:20:53 -0500164 .setBuffer(mSurfaceControl, gb, fence)
Valerie Hau9dab9732019-08-20 09:29:25 -0700165 .show(mSurfaceControl)
166 .addTransactionCompletedCallback(mCallbackHelper.function,
167 mCallbackHelper.getContext());
168 return NO_ERROR;
169 }
170
171 status_t mergeAndApply(TransactionHelper /*transaction*/) {
172 // transaction.apply();
173 return NO_ERROR;
174 }
175
176 status_t verifyCallbacks() {
177 ExpectedResult expected;
178 expected.addSurface(ExpectedResult::Transaction::PRESENTED, mSurfaceControl);
179 EXPECT_NO_FATAL_FAILURE(IPCTestUtils::waitForCallback(mCallbackHelper, expected, true));
180 return NO_ERROR;
181 }
182
183 status_t cleanUp() {
184 if (mClient) mClient->dispose();
185 mSurfaceControl = nullptr;
186 IPCThreadState::self()->stopProcess();
187 return NO_ERROR;
188 }
189
190 status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply,
191 uint32_t /*flags*/) override {
192 EXPECT_GE(code, IBinder::FIRST_CALL_TRANSACTION);
193 EXPECT_LT(code, static_cast<uint32_t>(IIPCTest::Tag::Last));
194 switch (static_cast<IIPCTest::Tag>(code)) {
195 case IIPCTest::Tag::SetDeathToken:
196 return callLocal(data, reply, &IIPCTest::setDeathToken);
197 case IIPCTest::Tag::InitClient:
198 return callLocal(data, reply, &IIPCTest::initClient);
199 case IIPCTest::Tag::CreateTransaction:
200 return callLocal(data, reply, &IIPCTest::createTransaction);
201 case IIPCTest::Tag::MergeAndApply:
202 return callLocal(data, reply, &IIPCTest::mergeAndApply);
203 case IIPCTest::Tag::VerifyCallbacks:
204 return callLocal(data, reply, &IIPCTest::verifyCallbacks);
205 case IIPCTest::Tag::CleanUp:
206 return callLocal(data, reply, &IIPCTest::cleanUp);
207 default:
208 return UNKNOWN_ERROR;
209 }
210 }
211
212private:
213 sp<SurfaceComposerClient> mClient;
214 sp<SurfaceControl> mSurfaceControl;
215 CallbackHelper mCallbackHelper;
216};
217
218class IPCTest : public ::testing::Test {
219public:
220 IPCTest() : mDeathRecipient(new BBinder), mRemote(initRemoteService()) {
221 ProcessState::self()->startThreadPool();
222 }
223 void SetUp() {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700224 mClient = sp<SurfaceComposerClient>::make();
Valerie Hau9dab9732019-08-20 09:29:25 -0700225 ASSERT_EQ(NO_ERROR, mClient->initCheck());
226
227 mPrimaryDisplay = mClient->getInternalDisplayToken();
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100228 ui::DisplayMode mode;
229 mClient->getActiveDisplayMode(mPrimaryDisplay, &mode);
230 mDisplayWidth = mode.resolution.getWidth();
231 mDisplayHeight = mode.resolution.getHeight();
Valerie Hau9dab9732019-08-20 09:29:25 -0700232
233 Transaction setupTransaction;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700234 setupTransaction.setDisplayLayerStack(mPrimaryDisplay, ui::DEFAULT_LAYER_STACK);
Valerie Hau9dab9732019-08-20 09:29:25 -0700235 setupTransaction.apply();
236 }
237
238protected:
239 sp<IIPCTest> initRemoteService();
240
241 sp<IBinder> mDeathRecipient;
242 sp<IIPCTest> mRemote;
243 sp<SurfaceComposerClient> mClient;
244 sp<IBinder> mPrimaryDisplay;
245 uint32_t mDisplayWidth;
246 uint32_t mDisplayHeight;
247 sp<SurfaceControl> sc;
248};
249
250status_t IPCTestUtils::getBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence) {
251 static BufferGenerator bufferGenerator;
252 return bufferGenerator.get(outBuffer, outFence);
253}
254
255void IPCTestUtils::waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
256 bool finalState) {
257 CallbackData callbackData;
258 ASSERT_NO_FATAL_FAILURE(helper.getCallbackData(&callbackData));
259 EXPECT_NO_FATAL_FAILURE(expectedResult.verifyCallbackData(callbackData));
260
261 if (finalState) {
262 ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
263 }
264}
265
266sp<IIPCTest> IPCTest::initRemoteService() {
267 static std::mutex mMutex;
268 static sp<IIPCTest> remote;
269 const String16 serviceName("IPCTest");
270
271 std::unique_lock<decltype(mMutex)> lock;
272 if (remote == nullptr) {
273 pid_t forkPid = fork();
274 EXPECT_NE(forkPid, -1);
275
276 if (forkPid == 0) {
277 sp<IIPCTest> nativeService = new BnIPCTest;
278 if (!nativeService) {
279 ALOGE("null service...");
280 }
281 status_t err = defaultServiceManager()->addService(serviceName,
282 IInterface::asBinder(nativeService));
283 if (err != NO_ERROR) {
284 ALOGE("failed to add service: %d", err);
285 }
286 ProcessState::self()->startThreadPool();
287 IPCThreadState::self()->joinThreadPool();
288 [&]() { exit(0); }();
289 }
290 sp<IBinder> binder = defaultServiceManager()->getService(serviceName);
291 remote = interface_cast<IIPCTest>(binder);
292 remote->setDeathToken(mDeathRecipient);
293 }
294 return remote;
295}
296
297TEST_F(IPCTest, MergeBasic) {
298 CallbackHelper helper1;
299 sc = mClient->createSurface(String8("parentProcessSurface"), 0, 0, PIXEL_FORMAT_RGBA_8888,
300 ISurfaceComposerClient::eFXSurfaceBufferState,
301 /*parent*/ nullptr);
302 sp<GraphicBuffer> gb;
303 sp<Fence> fence;
304 int err = IPCTestUtils::getBuffer(&gb, &fence);
305 ASSERT_EQ(NO_ERROR, err);
306 TransactionUtils::fillGraphicBufferColor(gb,
307 {0, 0, static_cast<int32_t>(mDisplayWidth),
308 static_cast<int32_t>(mDisplayHeight)},
309 Color::RED);
310
311 Transaction transaction;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700312 transaction.setLayerStack(sc, ui::DEFAULT_LAYER_STACK)
Valerie Hau9dab9732019-08-20 09:29:25 -0700313 .setLayer(sc, std::numeric_limits<int32_t>::max() - 1)
chaviwba4320c2021-09-15 15:20:53 -0500314 .setBuffer(sc, gb, fence)
Valerie Hau9dab9732019-08-20 09:29:25 -0700315 .show(sc)
316 .addTransactionCompletedCallback(helper1.function, helper1.getContext());
317
318 TransactionHelper remote;
319 mRemote->initClient();
320 mRemote->createTransaction(&remote, mDisplayWidth / 2, mDisplayHeight / 2);
321 ASSERT_EQ(1, remote.getNumListeners());
322 auto remoteListenerCallbacks = remote.getListenerCallbacks();
323 auto remoteCallback = remoteListenerCallbacks.begin();
324 auto remoteCallbackInfo = remoteCallback->second;
325 auto remoteListenerScs = remoteCallbackInfo.surfaceControls;
326 ASSERT_EQ(1, remoteCallbackInfo.callbackIds.size());
327 ASSERT_EQ(1, remoteListenerScs.size());
328
329 sp<SurfaceControl> remoteSc = *(remoteListenerScs.begin());
330 transaction.merge(std::move(remote));
331 transaction.apply();
332
333 sleep(1);
334 ExpectedResult expected;
335 expected.addSurface(ExpectedResult::Transaction::PRESENTED, sc);
336 expected.addSurface(ExpectedResult::Transaction::PRESENTED, remoteSc);
337 EXPECT_NO_FATAL_FAILURE(IPCTestUtils::waitForCallback(helper1, expected, true));
338
339 mRemote->verifyCallbacks();
340 mRemote->cleanUp();
341}
342
343} // namespace test
344} // namespace android