Valerie Hau | 9dab973 | 2019-08-20 09:29:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Hau | 9dab973 | 2019-08-20 09:29:25 -0700 | [diff] [blame] | 17 | #include <binder/IInterface.h> |
| 18 | #include <binder/IPCThreadState.h> |
| 19 | #include <binder/IServiceManager.h> |
| 20 | #include <binder/ProcessState.h> |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 21 | #include <gtest/gtest.h> |
Valerie Hau | 9dab973 | 2019-08-20 09:29:25 -0700 | [diff] [blame] | 22 | #include <gui/ISurfaceComposer.h> |
| 23 | #include <gui/LayerState.h> |
| 24 | #include <gui/Surface.h> |
| 25 | #include <gui/SurfaceComposerClient.h> |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 26 | #include <ui/DisplayMode.h> |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 27 | #include <utils/String8.h> |
Valerie Hau | 9dab973 | 2019-08-20 09:29:25 -0700 | [diff] [blame] | 28 | |
| 29 | #include <limits> |
| 30 | |
Valerie Hau | 9dab973 | 2019-08-20 09:29:25 -0700 | [diff] [blame] | 31 | #include "BufferGenerator.h" |
| 32 | #include "utils/CallbackUtils.h" |
| 33 | #include "utils/ColorUtils.h" |
| 34 | #include "utils/TransactionUtils.h" |
| 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | namespace test { |
| 39 | |
| 40 | using Transaction = SurfaceComposerClient::Transaction; |
| 41 | using CallbackInfo = SurfaceComposerClient::CallbackInfo; |
| 42 | using TCLHash = SurfaceComposerClient::TCLHash; |
| 43 | using android::hardware::graphics::common::V1_1::BufferUsage; |
| 44 | |
| 45 | class TransactionHelper : public Transaction { |
| 46 | public: |
| 47 | size_t getNumListeners() { return mListenerCallbacks.size(); } |
| 48 | |
| 49 | std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash> |
| 50 | getListenerCallbacks() { |
| 51 | return mListenerCallbacks; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | class IPCTestUtils { |
| 56 | public: |
| 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 | |
| 62 | class IIPCTest : public IInterface { |
| 63 | public: |
| 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 | |
| 89 | class BpIPCTest : public SafeBpInterface<IIPCTest> { |
| 90 | public: |
| 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 | |
| 115 | IMPLEMENT_META_INTERFACE(IPCTest, "android.gfx.tests.IIPCTest") |
| 116 | |
| 117 | class onTestDeath : public IBinder::DeathRecipient { |
| 118 | public: |
| 119 | void binderDied(const wp<IBinder>& /*who*/) override { |
| 120 | ALOGE("onTestDeath::binderDied, exiting"); |
| 121 | exit(0); |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | sp<onTestDeath> getDeathToken() { |
| 126 | static sp<onTestDeath> token = new onTestDeath; |
| 127 | return token; |
| 128 | } |
| 129 | |
| 130 | class BnIPCTest : public SafeBnInterface<IIPCTest> { |
| 131 | public: |
| 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 { |
| 139 | mClient = new SurfaceComposerClient; |
| 140 | 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); |
| 162 | transaction->setLayerStack(mSurfaceControl, 0) |
| 163 | .setLayer(mSurfaceControl, std::numeric_limits<int32_t>::max()) |
| 164 | .setFrame(mSurfaceControl, Rect(0, 0, width, height)) |
| 165 | .setBuffer(mSurfaceControl, gb) |
| 166 | .setAcquireFence(mSurfaceControl, fence) |
| 167 | .show(mSurfaceControl) |
| 168 | .addTransactionCompletedCallback(mCallbackHelper.function, |
| 169 | mCallbackHelper.getContext()); |
| 170 | return NO_ERROR; |
| 171 | } |
| 172 | |
| 173 | status_t mergeAndApply(TransactionHelper /*transaction*/) { |
| 174 | // transaction.apply(); |
| 175 | return NO_ERROR; |
| 176 | } |
| 177 | |
| 178 | status_t verifyCallbacks() { |
| 179 | ExpectedResult expected; |
| 180 | expected.addSurface(ExpectedResult::Transaction::PRESENTED, mSurfaceControl); |
| 181 | EXPECT_NO_FATAL_FAILURE(IPCTestUtils::waitForCallback(mCallbackHelper, expected, true)); |
| 182 | return NO_ERROR; |
| 183 | } |
| 184 | |
| 185 | status_t cleanUp() { |
| 186 | if (mClient) mClient->dispose(); |
| 187 | mSurfaceControl = nullptr; |
| 188 | IPCThreadState::self()->stopProcess(); |
| 189 | return NO_ERROR; |
| 190 | } |
| 191 | |
| 192 | status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 193 | uint32_t /*flags*/) override { |
| 194 | EXPECT_GE(code, IBinder::FIRST_CALL_TRANSACTION); |
| 195 | EXPECT_LT(code, static_cast<uint32_t>(IIPCTest::Tag::Last)); |
| 196 | switch (static_cast<IIPCTest::Tag>(code)) { |
| 197 | case IIPCTest::Tag::SetDeathToken: |
| 198 | return callLocal(data, reply, &IIPCTest::setDeathToken); |
| 199 | case IIPCTest::Tag::InitClient: |
| 200 | return callLocal(data, reply, &IIPCTest::initClient); |
| 201 | case IIPCTest::Tag::CreateTransaction: |
| 202 | return callLocal(data, reply, &IIPCTest::createTransaction); |
| 203 | case IIPCTest::Tag::MergeAndApply: |
| 204 | return callLocal(data, reply, &IIPCTest::mergeAndApply); |
| 205 | case IIPCTest::Tag::VerifyCallbacks: |
| 206 | return callLocal(data, reply, &IIPCTest::verifyCallbacks); |
| 207 | case IIPCTest::Tag::CleanUp: |
| 208 | return callLocal(data, reply, &IIPCTest::cleanUp); |
| 209 | default: |
| 210 | return UNKNOWN_ERROR; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | private: |
| 215 | sp<SurfaceComposerClient> mClient; |
| 216 | sp<SurfaceControl> mSurfaceControl; |
| 217 | CallbackHelper mCallbackHelper; |
| 218 | }; |
| 219 | |
| 220 | class IPCTest : public ::testing::Test { |
| 221 | public: |
| 222 | IPCTest() : mDeathRecipient(new BBinder), mRemote(initRemoteService()) { |
| 223 | ProcessState::self()->startThreadPool(); |
| 224 | } |
| 225 | void SetUp() { |
| 226 | mClient = new SurfaceComposerClient; |
| 227 | ASSERT_EQ(NO_ERROR, mClient->initCheck()); |
| 228 | |
| 229 | mPrimaryDisplay = mClient->getInternalDisplayToken(); |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 230 | ui::DisplayMode mode; |
| 231 | mClient->getActiveDisplayMode(mPrimaryDisplay, &mode); |
| 232 | mDisplayWidth = mode.resolution.getWidth(); |
| 233 | mDisplayHeight = mode.resolution.getHeight(); |
Valerie Hau | 9dab973 | 2019-08-20 09:29:25 -0700 | [diff] [blame] | 234 | |
| 235 | Transaction setupTransaction; |
| 236 | setupTransaction.setDisplayLayerStack(mPrimaryDisplay, 0); |
| 237 | setupTransaction.apply(); |
| 238 | } |
| 239 | |
| 240 | protected: |
| 241 | sp<IIPCTest> initRemoteService(); |
| 242 | |
| 243 | sp<IBinder> mDeathRecipient; |
| 244 | sp<IIPCTest> mRemote; |
| 245 | sp<SurfaceComposerClient> mClient; |
| 246 | sp<IBinder> mPrimaryDisplay; |
| 247 | uint32_t mDisplayWidth; |
| 248 | uint32_t mDisplayHeight; |
| 249 | sp<SurfaceControl> sc; |
| 250 | }; |
| 251 | |
| 252 | status_t IPCTestUtils::getBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence) { |
| 253 | static BufferGenerator bufferGenerator; |
| 254 | return bufferGenerator.get(outBuffer, outFence); |
| 255 | } |
| 256 | |
| 257 | void IPCTestUtils::waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult, |
| 258 | bool finalState) { |
| 259 | CallbackData callbackData; |
| 260 | ASSERT_NO_FATAL_FAILURE(helper.getCallbackData(&callbackData)); |
| 261 | EXPECT_NO_FATAL_FAILURE(expectedResult.verifyCallbackData(callbackData)); |
| 262 | |
| 263 | if (finalState) { |
| 264 | ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState()); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | sp<IIPCTest> IPCTest::initRemoteService() { |
| 269 | static std::mutex mMutex; |
| 270 | static sp<IIPCTest> remote; |
| 271 | const String16 serviceName("IPCTest"); |
| 272 | |
| 273 | std::unique_lock<decltype(mMutex)> lock; |
| 274 | if (remote == nullptr) { |
| 275 | pid_t forkPid = fork(); |
| 276 | EXPECT_NE(forkPid, -1); |
| 277 | |
| 278 | if (forkPid == 0) { |
| 279 | sp<IIPCTest> nativeService = new BnIPCTest; |
| 280 | if (!nativeService) { |
| 281 | ALOGE("null service..."); |
| 282 | } |
| 283 | status_t err = defaultServiceManager()->addService(serviceName, |
| 284 | IInterface::asBinder(nativeService)); |
| 285 | if (err != NO_ERROR) { |
| 286 | ALOGE("failed to add service: %d", err); |
| 287 | } |
| 288 | ProcessState::self()->startThreadPool(); |
| 289 | IPCThreadState::self()->joinThreadPool(); |
| 290 | [&]() { exit(0); }(); |
| 291 | } |
| 292 | sp<IBinder> binder = defaultServiceManager()->getService(serviceName); |
| 293 | remote = interface_cast<IIPCTest>(binder); |
| 294 | remote->setDeathToken(mDeathRecipient); |
| 295 | } |
| 296 | return remote; |
| 297 | } |
| 298 | |
| 299 | TEST_F(IPCTest, MergeBasic) { |
| 300 | CallbackHelper helper1; |
| 301 | sc = mClient->createSurface(String8("parentProcessSurface"), 0, 0, PIXEL_FORMAT_RGBA_8888, |
| 302 | ISurfaceComposerClient::eFXSurfaceBufferState, |
| 303 | /*parent*/ nullptr); |
| 304 | sp<GraphicBuffer> gb; |
| 305 | sp<Fence> fence; |
| 306 | int err = IPCTestUtils::getBuffer(&gb, &fence); |
| 307 | ASSERT_EQ(NO_ERROR, err); |
| 308 | TransactionUtils::fillGraphicBufferColor(gb, |
| 309 | {0, 0, static_cast<int32_t>(mDisplayWidth), |
| 310 | static_cast<int32_t>(mDisplayHeight)}, |
| 311 | Color::RED); |
| 312 | |
| 313 | Transaction transaction; |
| 314 | transaction.setLayerStack(sc, 0) |
| 315 | .setLayer(sc, std::numeric_limits<int32_t>::max() - 1) |
| 316 | .setBuffer(sc, gb) |
| 317 | .setAcquireFence(sc, fence) |
| 318 | .show(sc) |
| 319 | .addTransactionCompletedCallback(helper1.function, helper1.getContext()); |
| 320 | |
| 321 | TransactionHelper remote; |
| 322 | mRemote->initClient(); |
| 323 | mRemote->createTransaction(&remote, mDisplayWidth / 2, mDisplayHeight / 2); |
| 324 | ASSERT_EQ(1, remote.getNumListeners()); |
| 325 | auto remoteListenerCallbacks = remote.getListenerCallbacks(); |
| 326 | auto remoteCallback = remoteListenerCallbacks.begin(); |
| 327 | auto remoteCallbackInfo = remoteCallback->second; |
| 328 | auto remoteListenerScs = remoteCallbackInfo.surfaceControls; |
| 329 | ASSERT_EQ(1, remoteCallbackInfo.callbackIds.size()); |
| 330 | ASSERT_EQ(1, remoteListenerScs.size()); |
| 331 | |
| 332 | sp<SurfaceControl> remoteSc = *(remoteListenerScs.begin()); |
| 333 | transaction.merge(std::move(remote)); |
| 334 | transaction.apply(); |
| 335 | |
| 336 | sleep(1); |
| 337 | ExpectedResult expected; |
| 338 | expected.addSurface(ExpectedResult::Transaction::PRESENTED, sc); |
| 339 | expected.addSurface(ExpectedResult::Transaction::PRESENTED, remoteSc); |
| 340 | EXPECT_NO_FATAL_FAILURE(IPCTestUtils::waitForCallback(helper1, expected, true)); |
| 341 | |
| 342 | mRemote->verifyCallbacks(); |
| 343 | mRemote->cleanUp(); |
| 344 | } |
| 345 | |
| 346 | } // namespace test |
| 347 | } // namespace android |