| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 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 |  | 
| Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 18 | #pragma clang diagnostic push | 
|  | 19 | #pragma clang diagnostic ignored "-Wconversion" | 
|  | 20 |  | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 21 | #undef LOG_TAG | 
|  | 22 | #define LOG_TAG "LibSurfaceFlingerUnittests" | 
|  | 23 |  | 
|  | 24 | #include <FrameTracer/FrameTracer.h> | 
|  | 25 | #include <gmock/gmock.h> | 
|  | 26 | #include <gtest/gtest.h> | 
|  | 27 | #include <log/log.h> | 
|  | 28 | #include <perfetto/trace/trace.pb.h> | 
|  | 29 |  | 
|  | 30 | #include "libsurfaceflinger_unittest_main.h" | 
|  | 31 |  | 
|  | 32 | using namespace google::protobuf; | 
|  | 33 |  | 
|  | 34 | namespace android { | 
|  | 35 | namespace { | 
|  | 36 |  | 
|  | 37 | class FrameTracerTest : public testing::Test { | 
|  | 38 | public: | 
|  | 39 | FrameTracerTest() { | 
|  | 40 | const ::testing::TestInfo* const test_info = | 
|  | 41 | ::testing::UnitTest::GetInstance()->current_test_info(); | 
|  | 42 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); | 
|  | 43 |  | 
|  | 44 | // Need to initialize tracing in process for testing, and only once per test suite. | 
|  | 45 | static bool wasInitialized = false; | 
|  | 46 | if (!wasInitialized) { | 
|  | 47 | perfetto::TracingInitArgs args; | 
|  | 48 | args.backends = perfetto::kInProcessBackend; | 
|  | 49 | perfetto::Tracing::Initialize(args); | 
|  | 50 | wasInitialized = true; | 
|  | 51 | } | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | ~FrameTracerTest() { | 
|  | 55 | const ::testing::TestInfo* const test_info = | 
|  | 56 | ::testing::UnitTest::GetInstance()->current_test_info(); | 
|  | 57 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | void SetUp() override { | 
|  | 61 | mFrameTracer = std::make_unique<FrameTracer>(); | 
|  | 62 | mFrameTracer->registerDataSource(); | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | void TearDown() override { mFrameTracer.reset(); } | 
|  | 66 |  | 
|  | 67 | // Each tracing session can be used for a single block of Start -> Stop. | 
|  | 68 | static std::unique_ptr<perfetto::TracingSession> getTracingSessionForTest() { | 
|  | 69 | perfetto::TraceConfig cfg; | 
|  | 70 | cfg.set_duration_ms(500); | 
|  | 71 | cfg.add_buffers()->set_size_kb(1024); | 
|  | 72 | auto* ds_cfg = cfg.add_data_sources()->mutable_config(); | 
|  | 73 | ds_cfg->set_name(FrameTracer::kFrameTracerDataSource); | 
|  | 74 |  | 
|  | 75 | auto tracingSession = perfetto::Tracing::NewTrace(perfetto::kInProcessBackend); | 
|  | 76 | tracingSession->Setup(cfg); | 
|  | 77 | return tracingSession; | 
|  | 78 | } | 
|  | 79 |  | 
| Lalit Maganti | a371dfa | 2020-07-08 16:37:01 +0100 | [diff] [blame] | 80 | std::vector<perfetto::protos::TracePacket> readGraphicsFramePacketsBlocking( | 
|  | 81 | perfetto::TracingSession* tracingSession) { | 
|  | 82 | std::vector<char> raw_trace = tracingSession->ReadTraceBlocking(); | 
|  | 83 | perfetto::protos::Trace trace; | 
|  | 84 | EXPECT_TRUE(trace.ParseFromArray(raw_trace.data(), int(raw_trace.size()))); | 
|  | 85 |  | 
|  | 86 | std::vector<perfetto::protos::TracePacket> packets; | 
|  | 87 | for (const auto& packet : trace.packet()) { | 
|  | 88 | if (!packet.has_graphics_frame_event()) { | 
|  | 89 | continue; | 
|  | 90 | } | 
|  | 91 | packets.emplace_back(packet); | 
|  | 92 | } | 
|  | 93 | return packets; | 
|  | 94 | } | 
|  | 95 |  | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 96 | std::unique_ptr<FrameTracer> mFrameTracer; | 
|  | 97 | FenceToFenceTimeMap fenceFactory; | 
|  | 98 | }; | 
|  | 99 |  | 
|  | 100 | TEST_F(FrameTracerTest, traceNewLayerStartsTrackingLayerWhenTracing) { | 
|  | 101 | EXPECT_EQ(mFrameTracer->miniDump(), | 
|  | 102 | "FrameTracer miniDump:\nNumber of layers currently being traced is 0\n"); | 
|  | 103 |  | 
|  | 104 | const std::string layerName = "co.layername#0"; | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 105 | const int32_t layerId = 5; | 
|  | 106 | mFrameTracer->traceNewLayer(layerId, layerName); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 107 |  | 
|  | 108 | EXPECT_EQ(mFrameTracer->miniDump(), | 
|  | 109 | "FrameTracer miniDump:\nNumber of layers currently being traced is 0\n"); | 
|  | 110 |  | 
|  | 111 | auto tracingSession = getTracingSessionForTest(); | 
|  | 112 | tracingSession->StartBlocking(); | 
|  | 113 | EXPECT_EQ(mFrameTracer->miniDump(), | 
|  | 114 | "FrameTracer miniDump:\nNumber of layers currently being traced is 0\n"); | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 115 | mFrameTracer->traceNewLayer(layerId, layerName); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 116 | EXPECT_EQ(mFrameTracer->miniDump(), | 
|  | 117 | "FrameTracer miniDump:\nNumber of layers currently being traced is 1\n"); | 
|  | 118 | tracingSession->StopBlocking(); | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | TEST_F(FrameTracerTest, onDestroyRemovesTheTrackedLayer) { | 
|  | 122 | EXPECT_EQ(mFrameTracer->miniDump(), | 
|  | 123 | "FrameTracer miniDump:\nNumber of layers currently being traced is 0\n"); | 
|  | 124 |  | 
|  | 125 | const std::string layerName = "co.layername#0"; | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 126 | const int32_t layerId = 5; | 
|  | 127 | const int32_t secondlayerId = 6; | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 128 |  | 
|  | 129 | auto tracingSession = getTracingSessionForTest(); | 
|  | 130 | tracingSession->StartBlocking(); | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 131 | mFrameTracer->traceNewLayer(layerId, layerName); | 
|  | 132 | mFrameTracer->traceNewLayer(secondlayerId, layerName); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 133 | EXPECT_EQ(mFrameTracer->miniDump(), | 
|  | 134 | "FrameTracer miniDump:\nNumber of layers currently being traced is 2\n"); | 
|  | 135 | tracingSession->StopBlocking(); | 
|  | 136 |  | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 137 | mFrameTracer->onDestroy(layerId); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 138 | EXPECT_EQ(mFrameTracer->miniDump(), | 
|  | 139 | "FrameTracer miniDump:\nNumber of layers currently being traced is 1\n"); | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 140 | mFrameTracer->onDestroy(layerId); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 141 | EXPECT_EQ(mFrameTracer->miniDump(), | 
|  | 142 | "FrameTracer miniDump:\nNumber of layers currently being traced is 1\n"); | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 143 | mFrameTracer->onDestroy(secondlayerId); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 144 | EXPECT_EQ(mFrameTracer->miniDump(), | 
|  | 145 | "FrameTracer miniDump:\nNumber of layers currently being traced is 0\n"); | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | TEST_F(FrameTracerTest, canTraceAfterAddingLayer) { | 
|  | 149 | const std::string layerName = "co.layername#0"; | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 150 | const int32_t layerId = 1; | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 151 | const uint32_t bufferID = 2; | 
|  | 152 | const uint64_t frameNumber = 3; | 
|  | 153 | const nsecs_t timestamp = 4; | 
|  | 154 | const nsecs_t duration = 5; | 
|  | 155 | const auto type = FrameTracer::FrameEvent::POST; | 
|  | 156 |  | 
|  | 157 | { | 
|  | 158 | auto tracingSession = getTracingSessionForTest(); | 
|  | 159 |  | 
|  | 160 | tracingSession->StartBlocking(); | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 161 | mFrameTracer->traceTimestamp(layerId, bufferID, frameNumber, timestamp, type, duration); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 162 | // Create second trace packet to finalize the previous one. | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 163 | mFrameTracer->traceTimestamp(layerId, 0, 0, 0, FrameTracer::FrameEvent::UNSPECIFIED); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 164 | tracingSession->StopBlocking(); | 
|  | 165 |  | 
| Lalit Maganti | a371dfa | 2020-07-08 16:37:01 +0100 | [diff] [blame] | 166 | auto packets = readGraphicsFramePacketsBlocking(tracingSession.get()); | 
|  | 167 | EXPECT_EQ(packets.size(), 0); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 168 | } | 
|  | 169 |  | 
|  | 170 | { | 
|  | 171 | auto tracingSession = getTracingSessionForTest(); | 
|  | 172 |  | 
|  | 173 | tracingSession->StartBlocking(); | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 174 | mFrameTracer->traceNewLayer(layerId, layerName); | 
|  | 175 | mFrameTracer->traceTimestamp(layerId, bufferID, frameNumber, timestamp, type, duration); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 176 | // Create second trace packet to finalize the previous one. | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 177 | mFrameTracer->traceTimestamp(layerId, 0, 0, 0, FrameTracer::FrameEvent::UNSPECIFIED); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 178 | tracingSession->StopBlocking(); | 
|  | 179 |  | 
| Lalit Maganti | a371dfa | 2020-07-08 16:37:01 +0100 | [diff] [blame] | 180 | auto packets = readGraphicsFramePacketsBlocking(tracingSession.get()); | 
|  | 181 | EXPECT_EQ(packets.size(), 1); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 182 |  | 
| Lalit Maganti | a371dfa | 2020-07-08 16:37:01 +0100 | [diff] [blame] | 183 | const auto& packet = packets[0]; | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 184 | ASSERT_TRUE(packet.has_timestamp()); | 
|  | 185 | EXPECT_EQ(packet.timestamp(), timestamp); | 
|  | 186 | ASSERT_TRUE(packet.has_graphics_frame_event()); | 
|  | 187 | const auto& frame_event = packet.graphics_frame_event(); | 
|  | 188 | ASSERT_TRUE(frame_event.has_buffer_event()); | 
|  | 189 | const auto& buffer_event = frame_event.buffer_event(); | 
|  | 190 | ASSERT_TRUE(buffer_event.has_buffer_id()); | 
|  | 191 | EXPECT_EQ(buffer_event.buffer_id(), bufferID); | 
|  | 192 | ASSERT_TRUE(buffer_event.has_frame_number()); | 
|  | 193 | EXPECT_EQ(buffer_event.frame_number(), frameNumber); | 
|  | 194 | ASSERT_TRUE(buffer_event.has_type()); | 
|  | 195 | EXPECT_EQ(buffer_event.type(), perfetto::protos::GraphicsFrameEvent_BufferEventType(type)); | 
|  | 196 | ASSERT_TRUE(buffer_event.has_duration_ns()); | 
|  | 197 | EXPECT_EQ(buffer_event.duration_ns(), duration); | 
|  | 198 | } | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | TEST_F(FrameTracerTest, traceFenceTriggersOnNextTraceAfterFenceFired) { | 
|  | 202 | const std::string layerName = "co.layername#0"; | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 203 | const int32_t layerId = 5; | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 204 | const uint32_t bufferID = 4; | 
|  | 205 | const uint64_t frameNumber = 3; | 
|  | 206 | const auto type = FrameTracer::FrameEvent::ACQUIRE_FENCE; | 
|  | 207 |  | 
|  | 208 | { | 
|  | 209 | auto fenceTime = fenceFactory.createFenceTimeForTest(Fence::NO_FENCE); | 
|  | 210 | fenceFactory.signalAllForTest(Fence::NO_FENCE, Fence::SIGNAL_TIME_PENDING); | 
|  | 211 | auto tracingSession = getTracingSessionForTest(); | 
|  | 212 | tracingSession->StartBlocking(); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 213 | // Trace. | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 214 | mFrameTracer->traceNewLayer(layerId, layerName); | 
|  | 215 | mFrameTracer->traceFence(layerId, bufferID, frameNumber, fenceTime, type); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 216 | // Create extra trace packet to (hopefully not) trigger and finalize the fence packet. | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 217 | mFrameTracer->traceTimestamp(layerId, bufferID, 0, 0, FrameTracer::FrameEvent::UNSPECIFIED); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 218 | tracingSession->StopBlocking(); | 
| Lalit Maganti | a371dfa | 2020-07-08 16:37:01 +0100 | [diff] [blame] | 219 |  | 
|  | 220 | auto packets = readGraphicsFramePacketsBlocking(tracingSession.get()); | 
|  | 221 | EXPECT_EQ(packets.size(), 0); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 222 | } | 
|  | 223 |  | 
|  | 224 | { | 
|  | 225 | auto fenceTime = fenceFactory.createFenceTimeForTest(Fence::NO_FENCE); | 
|  | 226 | auto tracingSession = getTracingSessionForTest(); | 
|  | 227 | tracingSession->StartBlocking(); | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 228 | mFrameTracer->traceNewLayer(layerId, layerName); | 
|  | 229 | mFrameTracer->traceFence(layerId, bufferID, frameNumber, fenceTime, type); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 230 | const nsecs_t timestamp = systemTime(); | 
|  | 231 | fenceFactory.signalAllForTest(Fence::NO_FENCE, timestamp); | 
|  | 232 | // Create extra trace packet to trigger and finalize fence trace packets. | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 233 | mFrameTracer->traceTimestamp(layerId, bufferID, 0, 0, FrameTracer::FrameEvent::UNSPECIFIED); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 234 | tracingSession->StopBlocking(); | 
|  | 235 |  | 
| Lalit Maganti | a371dfa | 2020-07-08 16:37:01 +0100 | [diff] [blame] | 236 | auto packets = readGraphicsFramePacketsBlocking(tracingSession.get()); | 
|  | 237 | EXPECT_EQ(packets.size(), 2); // Two packets because of the extra trace made above. | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 238 |  | 
| Lalit Maganti | a371dfa | 2020-07-08 16:37:01 +0100 | [diff] [blame] | 239 | const auto& packet = packets[1]; | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 240 | ASSERT_TRUE(packet.has_timestamp()); | 
|  | 241 | EXPECT_EQ(packet.timestamp(), timestamp); | 
|  | 242 | ASSERT_TRUE(packet.has_graphics_frame_event()); | 
|  | 243 | const auto& frame_event = packet.graphics_frame_event(); | 
|  | 244 | ASSERT_TRUE(frame_event.has_buffer_event()); | 
|  | 245 | const auto& buffer_event = frame_event.buffer_event(); | 
|  | 246 | ASSERT_TRUE(buffer_event.has_buffer_id()); | 
|  | 247 | EXPECT_EQ(buffer_event.buffer_id(), bufferID); | 
|  | 248 | ASSERT_TRUE(buffer_event.has_frame_number()); | 
|  | 249 | EXPECT_EQ(buffer_event.frame_number(), frameNumber); | 
|  | 250 | ASSERT_TRUE(buffer_event.has_type()); | 
|  | 251 | EXPECT_EQ(buffer_event.type(), perfetto::protos::GraphicsFrameEvent_BufferEventType(type)); | 
|  | 252 | EXPECT_FALSE(buffer_event.has_duration_ns()); | 
|  | 253 | } | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | TEST_F(FrameTracerTest, traceFenceWithStartTimeAfterSignalTime_ShouldHaveNoDuration) { | 
|  | 257 | const std::string layerName = "co.layername#0"; | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 258 | const int32_t layerId = 5; | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 259 | const uint32_t bufferID = 4; | 
|  | 260 | const uint64_t frameNumber = 3; | 
|  | 261 | const auto type = FrameTracer::FrameEvent::ACQUIRE_FENCE; | 
|  | 262 |  | 
|  | 263 | auto tracingSession = getTracingSessionForTest(); | 
|  | 264 |  | 
|  | 265 | tracingSession->StartBlocking(); | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 266 | mFrameTracer->traceNewLayer(layerId, layerName); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 267 |  | 
|  | 268 | // traceFence called after fence signalled. | 
|  | 269 | const nsecs_t signalTime1 = systemTime(); | 
|  | 270 | const nsecs_t startTime1 = signalTime1 + 100000; | 
|  | 271 | auto fence1 = fenceFactory.createFenceTimeForTest(Fence::NO_FENCE); | 
|  | 272 | fenceFactory.signalAllForTest(Fence::NO_FENCE, signalTime1); | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 273 | mFrameTracer->traceFence(layerId, bufferID, frameNumber, fence1, type, startTime1); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 274 |  | 
|  | 275 | // traceFence called before fence signalled. | 
|  | 276 | const nsecs_t signalTime2 = systemTime(); | 
|  | 277 | const nsecs_t startTime2 = signalTime2 + 100000; | 
|  | 278 | auto fence2 = fenceFactory.createFenceTimeForTest(Fence::NO_FENCE); | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 279 | mFrameTracer->traceFence(layerId, bufferID, frameNumber, fence2, type, startTime2); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 280 | fenceFactory.signalAllForTest(Fence::NO_FENCE, signalTime2); | 
|  | 281 |  | 
|  | 282 | // Create extra trace packet to trigger and finalize fence trace packets. | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 283 | mFrameTracer->traceTimestamp(layerId, bufferID, 0, 0, FrameTracer::FrameEvent::UNSPECIFIED); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 284 | tracingSession->StopBlocking(); | 
|  | 285 |  | 
| Lalit Maganti | a371dfa | 2020-07-08 16:37:01 +0100 | [diff] [blame] | 286 | auto packets = readGraphicsFramePacketsBlocking(tracingSession.get()); | 
|  | 287 | EXPECT_EQ(packets.size(), 2); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 288 |  | 
| Lalit Maganti | a371dfa | 2020-07-08 16:37:01 +0100 | [diff] [blame] | 289 | const auto& packet1 = packets[0]; | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 290 | ASSERT_TRUE(packet1.has_timestamp()); | 
|  | 291 | EXPECT_EQ(packet1.timestamp(), signalTime1); | 
|  | 292 | ASSERT_TRUE(packet1.has_graphics_frame_event()); | 
|  | 293 | ASSERT_TRUE(packet1.graphics_frame_event().has_buffer_event()); | 
|  | 294 | ASSERT_FALSE(packet1.graphics_frame_event().buffer_event().has_duration_ns()); | 
|  | 295 |  | 
| Lalit Maganti | a371dfa | 2020-07-08 16:37:01 +0100 | [diff] [blame] | 296 | const auto& packet2 = packets[1]; | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 297 | ASSERT_TRUE(packet2.has_timestamp()); | 
|  | 298 | EXPECT_EQ(packet2.timestamp(), signalTime2); | 
|  | 299 | ASSERT_TRUE(packet2.has_graphics_frame_event()); | 
|  | 300 | ASSERT_TRUE(packet2.graphics_frame_event().has_buffer_event()); | 
|  | 301 | ASSERT_FALSE(packet2.graphics_frame_event().buffer_event().has_duration_ns()); | 
|  | 302 | } | 
|  | 303 |  | 
|  | 304 | TEST_F(FrameTracerTest, traceFenceOlderThanDeadline_ShouldBeIgnored) { | 
|  | 305 | const std::string layerName = "co.layername#0"; | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 306 | const int32_t layerId = 5; | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 307 | const uint32_t bufferID = 4; | 
|  | 308 | const uint64_t frameNumber = 3; | 
|  | 309 | const auto type = FrameTracer::FrameEvent::ACQUIRE_FENCE; | 
|  | 310 | const nsecs_t signalTime = systemTime() - FrameTracer::kFenceSignallingDeadline; | 
|  | 311 |  | 
|  | 312 | auto tracingSession = getTracingSessionForTest(); | 
|  | 313 | auto fence = fenceFactory.createFenceTimeForTest(Fence::NO_FENCE); | 
|  | 314 |  | 
|  | 315 | tracingSession->StartBlocking(); | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 316 | mFrameTracer->traceNewLayer(layerId, layerName); | 
|  | 317 | mFrameTracer->traceFence(layerId, bufferID, frameNumber, fence, type); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 318 | fenceFactory.signalAllForTest(Fence::NO_FENCE, signalTime); | 
|  | 319 | // Create extra trace packet to trigger and finalize any previous fence packets. | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 320 | mFrameTracer->traceTimestamp(layerId, bufferID, 0, 0, FrameTracer::FrameEvent::UNSPECIFIED); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 321 | tracingSession->StopBlocking(); | 
|  | 322 |  | 
| Lalit Maganti | a371dfa | 2020-07-08 16:37:01 +0100 | [diff] [blame] | 323 | auto packets = readGraphicsFramePacketsBlocking(tracingSession.get()); | 
|  | 324 | EXPECT_EQ(packets.size(), 0); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 325 | } | 
|  | 326 |  | 
|  | 327 | TEST_F(FrameTracerTest, traceFenceWithValidStartTime_ShouldHaveCorrectDuration) { | 
|  | 328 | const std::string layerName = "co.layername#0"; | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 329 | const int32_t layerId = 5; | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 330 | const uint32_t bufferID = 4; | 
|  | 331 | const uint64_t frameNumber = 3; | 
|  | 332 | const auto type = FrameTracer::FrameEvent::ACQUIRE_FENCE; | 
|  | 333 | const nsecs_t duration = 1234; | 
|  | 334 |  | 
|  | 335 | auto tracingSession = getTracingSessionForTest(); | 
|  | 336 |  | 
|  | 337 | tracingSession->StartBlocking(); | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 338 | mFrameTracer->traceNewLayer(layerId, layerName); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 339 |  | 
|  | 340 | // traceFence called after fence signalled. | 
|  | 341 | const nsecs_t signalTime1 = systemTime(); | 
|  | 342 | const nsecs_t startTime1 = signalTime1 - duration; | 
|  | 343 | auto fence1 = fenceFactory.createFenceTimeForTest(Fence::NO_FENCE); | 
|  | 344 | fenceFactory.signalAllForTest(Fence::NO_FENCE, signalTime1); | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 345 | mFrameTracer->traceFence(layerId, bufferID, frameNumber, fence1, type, startTime1); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 346 |  | 
|  | 347 | // traceFence called before fence signalled. | 
|  | 348 | const nsecs_t signalTime2 = systemTime(); | 
|  | 349 | const nsecs_t startTime2 = signalTime2 - duration; | 
|  | 350 | auto fence2 = fenceFactory.createFenceTimeForTest(Fence::NO_FENCE); | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 351 | mFrameTracer->traceFence(layerId, bufferID, frameNumber, fence2, type, startTime2); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 352 | fenceFactory.signalAllForTest(Fence::NO_FENCE, signalTime2); | 
|  | 353 |  | 
|  | 354 | // Create extra trace packet to trigger and finalize fence trace packets. | 
| Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 355 | mFrameTracer->traceTimestamp(layerId, bufferID, 0, 0, FrameTracer::FrameEvent::UNSPECIFIED); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 356 | tracingSession->StopBlocking(); | 
|  | 357 |  | 
| Lalit Maganti | a371dfa | 2020-07-08 16:37:01 +0100 | [diff] [blame] | 358 | auto packets = readGraphicsFramePacketsBlocking(tracingSession.get()); | 
|  | 359 | EXPECT_EQ(packets.size(), 2); | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 360 |  | 
| Lalit Maganti | a371dfa | 2020-07-08 16:37:01 +0100 | [diff] [blame] | 361 | const auto& packet1 = packets[0]; | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 362 | ASSERT_TRUE(packet1.has_timestamp()); | 
|  | 363 | EXPECT_EQ(packet1.timestamp(), startTime1); | 
|  | 364 | ASSERT_TRUE(packet1.has_graphics_frame_event()); | 
|  | 365 | ASSERT_TRUE(packet1.graphics_frame_event().has_buffer_event()); | 
|  | 366 | ASSERT_TRUE(packet1.graphics_frame_event().buffer_event().has_duration_ns()); | 
|  | 367 | const auto& buffer_event1 = packet1.graphics_frame_event().buffer_event(); | 
|  | 368 | EXPECT_EQ(buffer_event1.duration_ns(), duration); | 
|  | 369 |  | 
| Lalit Maganti | a371dfa | 2020-07-08 16:37:01 +0100 | [diff] [blame] | 370 | const auto& packet2 = packets[1]; | 
| Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 371 | ASSERT_TRUE(packet2.has_timestamp()); | 
|  | 372 | EXPECT_EQ(packet2.timestamp(), startTime2); | 
|  | 373 | ASSERT_TRUE(packet2.has_graphics_frame_event()); | 
|  | 374 | ASSERT_TRUE(packet2.graphics_frame_event().has_buffer_event()); | 
|  | 375 | ASSERT_TRUE(packet2.graphics_frame_event().buffer_event().has_duration_ns()); | 
|  | 376 | const auto& buffer_event2 = packet2.graphics_frame_event().buffer_event(); | 
|  | 377 | EXPECT_EQ(buffer_event2.duration_ns(), duration); | 
|  | 378 | } | 
|  | 379 |  | 
|  | 380 | } // namespace | 
|  | 381 | } // namespace android | 
| Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 382 |  | 
|  | 383 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 384 | #pragma clang diagnostic pop // ignored "-Wconversion" |