| /* | |
| * Copyright (C) 2019 The Android Open Source Project | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| // #define LOG_NDEBUG 0 | |
| #include <android/choreographer.h> | |
| #include <android/hardware_buffer.h> | |
| #include <binder/Binder.h> | |
| #include <common/FlagManager.h> | |
| #include <gtest/gtest.h> | |
| #include <utils/Looper.h> | |
| #include "BufferGenerator.h" | |
| #include <gui/BufferItemConsumer.h> | |
| #include <gui/IConsumerListener.h> | |
| #include <gui/Surface.h> | |
| #include <gui/SurfaceComposerClient.h> | |
| #include <private/gui/ComposerServiceAIDL.h> | |
| #include <system/window.h> | |
| #include <ui/GraphicBuffer.h> | |
| #include <ui/LayerStack.h> | |
| #include <ui/PixelFormat.h> | |
| #include <ui/Size.h> | |
| #include <gtest/gtest.h> | |
| #include <gui/AidlUtil.h> | |
| #include <private/gui/ComposerService.h> | |
| #include <private/gui/ComposerServiceAIDL.h> | |
| #include <atomic> | |
| #include <chrono> | |
| #include <condition_variable> | |
| #include <cstdint> | |
| #include <mutex> | |
| namespace android { | |
| namespace { | |
| using namespace std::chrono_literals; | |
| constexpr int32_t kWidth = 100; | |
| constexpr int32_t kHeight = 400; | |
| class FakeSurfaceComposerAIDL : public gui::ISurfaceComposerDefault { | |
| public: | |
| ~FakeSurfaceComposerAIDL() override {} | |
| bool mCreateVirtualDisplayCalled = false; | |
| gui::ISurfaceComposer::ContentAccessPolicy mContentAccessPolicy; | |
| binder::Status createVirtualDisplay( | |
| const std::string& /*displayName*/, bool /*isSecure*/, | |
| gui::ISurfaceComposer::OptimizationPolicy /*optimizationPolicy*/, | |
| gui::ISurfaceComposer::ContentAccessPolicy contentAccessPolicy, | |
| const std::string& /*uniqueId*/, int32_t /*ownerUid*/, float /*requestedRefreshRate*/, | |
| sp<IBinder>* outDisplay) override { | |
| mCreateVirtualDisplayCalled = true; | |
| mContentAccessPolicy = contentAccessPolicy; | |
| *outDisplay = nullptr; | |
| return binder::Status::ok(); | |
| } | |
| }; | |
| struct CallbackHelper { | |
| std::mutex mutex; | |
| std::condition_variable cv; | |
| bool called = false; | |
| static void callback(void* context, nsecs_t, const sp<Fence>&, | |
| const std::vector<SurfaceControlStats>&) { | |
| auto* helper = static_cast<CallbackHelper*>(context); | |
| std::lock_guard<std::mutex> lock(helper->mutex); | |
| helper->called = true; | |
| helper->cv.notify_all(); | |
| } | |
| void wait() { | |
| std::unique_lock<std::mutex> lock(mutex); | |
| cv.wait(lock, [this] { return called; }); | |
| } | |
| }; | |
| class VirtualDisplayTest : public ::testing::Test { | |
| protected: | |
| void SetUp() override { | |
| mConsumer = sp<BufferItemConsumer>::make(GRALLOC_USAGE_HW_COMPOSER); | |
| mConsumer->setName(String8("Virtual disp consumer (VirtualDisplayTest)")); | |
| mConsumer->setDefaultBufferSize(kWidth, kHeight); | |
| } | |
| status_t getBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence) { | |
| return mBufferGenerator.get(outBuffer, outFence); | |
| } | |
| BufferGenerator mBufferGenerator; | |
| sp<BufferItemConsumer> mConsumer; | |
| }; | |
| TEST_F(VirtualDisplayTest, ResizeTransactions_AffectReturnedBuffers) { | |
| if (!FlagManager::getInstance().bugfix_resize_virtual_display_surfaces()) { | |
| GTEST_SKIP(); | |
| } | |
| // This coordinates binder calls of onFrameAvailable with the test itself. | |
| class SizeWatchingFrameListener : public BufferItemConsumer::FrameAvailableListener { | |
| public: | |
| SizeWatchingFrameListener(const sp<BufferItemConsumer>& consumer) : mConsumer(consumer) {} | |
| virtual void onFrameAvailable(const BufferItem&) override { | |
| sp<BufferItemConsumer> consumer = mConsumer.promote(); | |
| if (!consumer) { | |
| return; | |
| } | |
| std::lock_guard<std::mutex> lock(mMutex); | |
| BufferItem item; | |
| ASSERT_EQ(OK, consumer->acquireBuffer(&item, 0)); | |
| mTrackedSizes.push({item.mGraphicBuffer->getWidth(), item.mGraphicBuffer->getHeight()}); | |
| consumer->releaseBuffer(item); | |
| mCondition.notify_all(); | |
| } | |
| ui::Size getNextSize() { | |
| std::unique_lock<std::mutex> lock(mMutex); | |
| if (mTrackedSizes.empty()) { | |
| mCondition.wait(lock); | |
| } | |
| ui::Size size = mTrackedSizes.front(); | |
| mTrackedSizes.pop(); | |
| return size; | |
| } | |
| private: | |
| std::mutex mMutex; | |
| std::condition_variable mCondition; | |
| std::queue<ui::Size> mTrackedSizes; | |
| wp<BufferItemConsumer> mConsumer; | |
| }; | |
| // Set up the virtual display itself to render to the consumer surface | |
| auto sizeListener = sp<SizeWatchingFrameListener>::make(mConsumer); | |
| mConsumer->setFrameAvailableListener(sizeListener); | |
| static const std::string kDisplayName("VirtualDisplay"); | |
| sp<IBinder> virtualDisplay = | |
| SurfaceComposerClient::createVirtualDisplay(kDisplayName, /*isSecure=*/false, | |
| /*optimizeForPower=*/false, | |
| /*uniqueId=*/"", | |
| /*requestedRefreshRate=*/60); | |
| ASSERT_NE(nullptr, virtualDisplay); | |
| // Set up a surface that we'll use to write to the VD. If this surface doesn't get updated, | |
| // there won't be any new frames: | |
| sp<SurfaceControl> sc = | |
| SurfaceComposerClient::getDefault()->createSurface(String8("VDSurface"), 10, 10, | |
| PIXEL_FORMAT_RGBA_8888, 0); | |
| ASSERT_NE(nullptr, sc); | |
| ui::LayerStack layerStack = {123454321}; | |
| SurfaceComposerClient::Transaction t; | |
| t.setDisplaySurface(virtualDisplay, mConsumer->getSurface()); | |
| t.setDisplayLayerStack(virtualDisplay, layerStack); | |
| t.setLayerStack(sc, layerStack); | |
| ASSERT_EQ(OK, t.apply(true)); | |
| sp<Surface> surface = sc->getSurface(); | |
| sp<SurfaceListener> surfaceListener = sp<StubSurfaceListener>::make(); | |
| ASSERT_EQ(OK, surface->connect(NATIVE_WINDOW_API_CPU, surfaceListener)); | |
| auto doFrame = [&]() { | |
| sp<GraphicBuffer> buffer; | |
| sp<Fence> fence; | |
| ASSERT_EQ(OK, surface->dequeueBuffer(&buffer, &fence)); | |
| ASSERT_EQ(OK, surface->queueBuffer(buffer, fence)); | |
| }; | |
| // At first, the frame size should be the default we started with: | |
| doFrame(); | |
| ui::Size size = sizeListener->getNextSize(); | |
| EXPECT_EQ(kWidth, size.width); | |
| EXPECT_EQ(kHeight, size.height); | |
| // But once we update the display size, the frame size should also change: | |
| t.setDisplaySize(virtualDisplay, kWidth / 2, kHeight / 2); | |
| ASSERT_EQ(OK, t.apply(true)); | |
| doFrame(); | |
| size = sizeListener->getNextSize(); | |
| EXPECT_EQ(kWidth / 2, size.width); | |
| EXPECT_EQ(kHeight / 2, size.height); | |
| // Be a good citizen and clean up: | |
| EXPECT_EQ(OK, SurfaceComposerClient::destroyVirtualDisplay(virtualDisplay)); | |
| } | |
| TEST_F(VirtualDisplayTest, VirtualDisplayDestroyedSurfaceReuse) { | |
| static const std::string kDisplayName("VirtualDisplay"); | |
| sp<IBinder> virtualDisplay = | |
| SurfaceComposerClient::createVirtualDisplay(kDisplayName, false /*isSecure*/); | |
| SurfaceComposerClient::Transaction t; | |
| t.setDisplaySurface(virtualDisplay, mConsumer->getSurface()); | |
| t.apply(true); | |
| EXPECT_EQ(NO_ERROR, SurfaceComposerClient::destroyVirtualDisplay(virtualDisplay)); | |
| virtualDisplay.clear(); | |
| // Sync here to ensure the display was completely destroyed in SF | |
| t.apply(true); | |
| // add another sync since we are deferring the display destruction | |
| t.apply(true); | |
| sp<Surface> surface = mConsumer->getSurface(); | |
| sp<ANativeWindow> window(surface); | |
| ASSERT_EQ(NO_ERROR, native_window_api_connect(window.get(), NATIVE_WINDOW_API_EGL)); | |
| } | |
| TEST_F(VirtualDisplayTest, VirtualDisplayModesetFail) { | |
| static const std::string kDisplayName("VirtualDisplay"); | |
| sp<IBinder> virtualDisplay = | |
| SurfaceComposerClient::createVirtualDisplay(kDisplayName, false /*isSecure*/); | |
| SurfaceComposerClient::Transaction t; | |
| t.setDisplaySurface(virtualDisplay, mConsumer->getSurface()); | |
| t.apply(true); | |
| gui::DisplayModeSpecs specs; | |
| specs.displayToken = std::move(virtualDisplay); | |
| gui::ISurfaceComposer::ModeSpecsUpdateResult result; | |
| EXPECT_EQ(SurfaceComposerClient::setDesiredDisplayModeSpecs(sp<BBinder>::make(), {specs}, | |
| &result), | |
| INVALID_OPERATION); | |
| EXPECT_EQ(result, gui::ISurfaceComposer::ModeSpecsUpdateResult::failure); | |
| } | |
| TEST_F(VirtualDisplayTest, CreateVirtualDisplayDefaultIncludeContentFromAllUids) { | |
| // Inject mock and verify argument access logic. | |
| sp<FakeSurfaceComposerAIDL> fakeComposer = sp<FakeSurfaceComposerAIDL>::make(); | |
| ComposerServiceAIDL::setComposerServiceForTesting(fakeComposer); | |
| sp<IBinder> virtualDisplayToken = | |
| SurfaceComposerClient::createVirtualDisplay(std::string("test_virtual_display"), | |
| false /* isSecure */); | |
| // Verify mock was called | |
| EXPECT_TRUE(fakeComposer->mCreateVirtualDisplayCalled); | |
| // Verify contentAccessPolicy defaults to AllUids | |
| EXPECT_EQ(fakeComposer->mContentAccessPolicy, | |
| gui::ISurfaceComposer::ContentAccessPolicy::AllUids); | |
| ComposerServiceAIDL::setComposerServiceForTesting(nullptr); | |
| } | |
| TEST_F(VirtualDisplayTest, VirtualDisplayDoesNotRefreshPhysicalDisplay) { | |
| if (!FlagManager::getInstance().isolate_display_refresh()) { | |
| GTEST_SKIP() << "isolate_display_refresh flag is disabled, skipping test."; | |
| } | |
| auto getPhysicalDisplayPresentCount = [&]() -> int { | |
| const auto ids = SurfaceComposerClient::getPhysicalDisplayIds(); | |
| if (ids.empty()) return -1; | |
| sp<IBinder> physicalToken = SurfaceComposerClient::getPhysicalDisplayToken(ids.front()); | |
| if (physicalToken == nullptr) return -1; | |
| return SurfaceComposerClient::getPhysicalDisplayPresentCount(physicalToken); | |
| }; | |
| static const std::string kDisplayName("VirtualDisplay"); | |
| sp<IBinder> virtualDisplay = | |
| SurfaceComposerClient::createVirtualDisplay(kDisplayName, false /*isSecure*/); | |
| ASSERT_NE(nullptr, virtualDisplay); | |
| sp<SurfaceControl> sc = SurfaceComposerClient::getDefault() | |
| ->createSurface(String8("VirtualDisplayTestLayer"), 10, 10, | |
| PIXEL_FORMAT_RGBA_8888, | |
| ISurfaceComposerClient::eFXSurfaceBufferState); | |
| sp<SurfaceControl> physicalSc = | |
| SurfaceComposerClient::getDefault() | |
| ->createSurface(String8("AODSurface"), 100, 100, PIXEL_FORMAT_RGBA_8888, | |
| ISurfaceComposerClient::eFXSurfaceEffect); | |
| ASSERT_NE(nullptr, physicalSc); | |
| ui::LayerStack layerStack = {123454321}; | |
| SurfaceComposerClient::Transaction t; | |
| t.setDisplaySurface(virtualDisplay, mConsumer->getSurface()); | |
| t.setDisplayLayerStack(virtualDisplay, layerStack); | |
| t.setDisplayProjection(virtualDisplay, ui::ROTATION_0, Rect(10, 10), Rect(10, 10)); | |
| t.setLayerStack(sc, layerStack); | |
| t.setLayer(sc, 1); | |
| t.show(sc); | |
| // Set up the static layer on the physical display (layer stack 99) | |
| t.setLayerStack(physicalSc, {99}); | |
| t.show(physicalSc); | |
| t.setColor(physicalSc, half3(0, 1, 0)); | |
| ASSERT_EQ(OK, t.apply(true)); | |
| SurfaceComposerClient::setDisplayPowerMode(virtualDisplay, 2 /* HWC_POWER_MODE_NORMAL */); | |
| // Set the physical display to NORMAL mode (ON) to ensure it is powered on and susceptible | |
| // to the bug. Tests running via adb might have the screen OFF. | |
| const auto ids = SurfaceComposerClient::getPhysicalDisplayIds(); | |
| sp<IBinder> physicalDisplay = | |
| ids.empty() ? nullptr : SurfaceComposerClient::getPhysicalDisplayToken(ids.front()); | |
| if (physicalDisplay != nullptr) { | |
| SurfaceComposerClient::setDisplayPowerMode(physicalDisplay, 2 /* HWC_POWER_MODE_NORMAL */); | |
| CallbackHelper helper; | |
| SurfaceComposerClient::Transaction t2; | |
| t2.setDisplayLayerStack(physicalDisplay, {99}); | |
| t2.addTransactionCompletedCallback(CallbackHelper::callback, &helper); | |
| t2.apply(); | |
| helper.wait(); | |
| } | |
| int startCount = getPhysicalDisplayPresentCount(); | |
| ASSERT_GE(startCount, 0); | |
| // Animate crop and queue buffers to trigger damage on the virtual display | |
| int numFrames = 0; | |
| auto startTime = std::chrono::steady_clock::now(); | |
| sp<Looper> looper = Looper::prepare(0); | |
| AChoreographer* choreographer = AChoreographer_getInstance(); | |
| ASSERT_NE(nullptr, choreographer); | |
| struct FrameCallbackData { | |
| std::atomic<bool> frameRendered{false}; | |
| }; | |
| FrameCallbackData cbData; | |
| auto callback = [](long /*frameTimeNanos*/, void* data) { | |
| auto* cbData = static_cast<FrameCallbackData*>(data); | |
| cbData->frameRendered = true; | |
| }; | |
| while (std::chrono::steady_clock::now() - startTime < 3s) { | |
| cbData.frameRendered = false; | |
| AChoreographer_postFrameCallback(choreographer, callback, &cbData); | |
| while (!cbData.frameRendered) { | |
| int pollResult = looper->pollOnce(100); | |
| if (pollResult == Looper::POLL_TIMEOUT) { | |
| ALOGW("Choreographer callback timeout"); | |
| break; | |
| } | |
| } | |
| sp<GraphicBuffer> buffer; | |
| sp<Fence> fence; | |
| ASSERT_EQ(OK, getBuffer(&buffer, &fence)); | |
| SurfaceComposerClient::Transaction() | |
| .setBuffer(sc, buffer, fence) | |
| .setCrop(sc, Rect(0, 0, (numFrames % 2 == 0) ? 10 : 9, 10)) | |
| .apply(true); | |
| numFrames++; | |
| } | |
| int endCount = getPhysicalDisplayPresentCount(); | |
| int diffCount = endCount - startCount; | |
| ALOGI("VirtualDisplay test queued %d frames, physical diff: %d", numFrames, diffCount); | |
| EXPECT_EQ(OK, SurfaceComposerClient::destroyVirtualDisplay(virtualDisplay)); | |
| // Restore physical display to NORMAL | |
| if (physicalDisplay != nullptr) { | |
| SurfaceComposerClient::Transaction t3; | |
| t3.setDisplayLayerStack(physicalDisplay, {0}); | |
| t3.apply(true); | |
| SurfaceComposerClient::setDisplayPowerMode(physicalDisplay, 2 /* HWC_POWER_MODE_NORMAL */); | |
| } | |
| EXPECT_LE(diffCount, 36) << "Physical display refreshed " << diffCount | |
| << " times in 3s. Expected < 36"; | |
| } | |
| } // namespace | |
| } // namespace android |