Vishnu Nair | d1a0578 | 2024-03-19 04:37:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2024 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 <android/native_window.h> |
| 18 | #include <gtest/gtest.h> |
| 19 | #include <gui/SurfaceComposerClient.h> |
| 20 | #include "android-base/stringprintf.h" |
| 21 | #include "utils/Errors.h" |
| 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | namespace { |
| 26 | status_t runShellCommand(const std::string& cmd, std::string& result) { |
| 27 | FILE* pipe = popen(cmd.c_str(), "r"); |
| 28 | if (!pipe) { |
| 29 | return UNKNOWN_ERROR; |
| 30 | } |
| 31 | |
| 32 | char buffer[1024]; |
| 33 | while (fgets(buffer, sizeof(buffer), pipe) != NULL) { |
| 34 | result += buffer; |
| 35 | } |
| 36 | |
| 37 | pclose(pipe); |
| 38 | return OK; |
| 39 | } |
| 40 | } // namespace |
| 41 | |
| 42 | using android::hardware::graphics::common::V1_1::BufferUsage; |
| 43 | |
| 44 | class WaitForCompletedCallback { |
| 45 | public: |
| 46 | WaitForCompletedCallback() = default; |
| 47 | ~WaitForCompletedCallback() = default; |
| 48 | |
| 49 | static void transactionCompletedCallback(void* callbackContext, nsecs_t /* latchTime */, |
| 50 | const sp<Fence>& /* presentFence */, |
| 51 | const std::vector<SurfaceControlStats>& /* stats */) { |
| 52 | ASSERT_NE(callbackContext, nullptr) << "failed to get callback context"; |
| 53 | WaitForCompletedCallback* context = static_cast<WaitForCompletedCallback*>(callbackContext); |
| 54 | context->notify(); |
| 55 | } |
| 56 | |
| 57 | void wait() { |
| 58 | std::unique_lock lock(mMutex); |
| 59 | cv.wait(lock, [this] { return mCallbackReceived; }); |
| 60 | } |
| 61 | |
| 62 | void notify() { |
| 63 | std::unique_lock lock(mMutex); |
| 64 | mCallbackReceived = true; |
| 65 | cv.notify_one(); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | std::mutex mMutex; |
| 70 | std::condition_variable cv; |
| 71 | bool mCallbackReceived = false; |
| 72 | }; |
| 73 | |
| 74 | TEST(Dumpsys, listLayers) { |
| 75 | sp<SurfaceComposerClient> client = sp<SurfaceComposerClient>::make(); |
| 76 | ASSERT_EQ(NO_ERROR, client->initCheck()); |
| 77 | auto newLayer = |
| 78 | client->createSurface(String8("MY_TEST_LAYER"), 100, 100, PIXEL_FORMAT_RGBA_8888, 0); |
| 79 | std::string layersAsString; |
| 80 | EXPECT_EQ(OK, runShellCommand("dumpsys SurfaceFlinger --list", layersAsString)); |
| 81 | EXPECT_NE(strstr(layersAsString.c_str(), ""), nullptr); |
| 82 | } |
| 83 | |
| 84 | TEST(Dumpsys, stats) { |
| 85 | sp<SurfaceComposerClient> client = sp<SurfaceComposerClient>::make(); |
| 86 | ASSERT_EQ(NO_ERROR, client->initCheck()); |
| 87 | auto newLayer = |
| 88 | client->createSurface(String8("MY_TEST_LAYER"), 100, 100, PIXEL_FORMAT_RGBA_8888, 0); |
| 89 | uint64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN | |
| 90 | BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE; |
| 91 | |
| 92 | sp<GraphicBuffer> buffer = |
| 93 | sp<GraphicBuffer>::make(1u, 1u, PIXEL_FORMAT_RGBA_8888, 1u, usageFlags, "test"); |
| 94 | |
| 95 | WaitForCompletedCallback callback; |
| 96 | SurfaceComposerClient::Transaction() |
| 97 | .setBuffer(newLayer, buffer) |
| 98 | .addTransactionCompletedCallback(WaitForCompletedCallback::transactionCompletedCallback, |
| 99 | &callback) |
| 100 | .apply(); |
| 101 | callback.wait(); |
| 102 | std::string stats; |
| 103 | std::string layerName = base::StringPrintf("MY_TEST_LAYER#%d", newLayer->getLayerId()); |
| 104 | EXPECT_EQ(OK, runShellCommand("dumpsys SurfaceFlinger --latency " + layerName, stats)); |
| 105 | EXPECT_NE(std::string(""), stats); |
| 106 | EXPECT_EQ(OK, runShellCommand("dumpsys SurfaceFlinger --latency-clear " + layerName, stats)); |
| 107 | } |
| 108 | |
| 109 | } // namespace android |