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