1 /*
2  * Copyright (C) 2021 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 #undef LOG_TAG
18 #define LOG_TAG "LibSurfaceFlingerUnittests"
19 
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <gui/SurfaceComposerClient.h>
23 #include <log/log.h>
24 #include <renderengine/ExternalTexture.h>
25 #include <renderengine/mock/FakeExternalTexture.h>
26 #include <renderengine/mock/RenderEngine.h>
27 #include <utils/String8.h>
28 
29 #include "TestableSurfaceFlinger.h"
30 #include "mock/DisplayHardware/MockComposer.h"
31 
32 namespace android {
33 
34 using testing::_;
35 using testing::Mock;
36 using testing::Return;
37 
38 using PresentState = frametimeline::SurfaceFrame::PresentState;
39 
40 class TransactionFrameTracerTest : public testing::Test {
41 public:
TransactionFrameTracerTest()42     TransactionFrameTracerTest() {
43         const ::testing::TestInfo* const test_info =
44                 ::testing::UnitTest::GetInstance()->current_test_info();
45         ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
46         mFlinger.setupMockScheduler();
47         mFlinger.setupComposer(std::make_unique<Hwc2::mock::Composer>());
48         mFlinger.setupRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
49     }
50 
~TransactionFrameTracerTest()51     ~TransactionFrameTracerTest() {
52         const ::testing::TestInfo* const test_info =
53                 ::testing::UnitTest::GetInstance()->current_test_info();
54         ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
55     }
56 
createLayer()57     sp<Layer> createLayer() {
58         sp<Client> client;
59         LayerCreationArgs args(mFlinger.flinger(), client, "buffer-state-layer", 0,
60                                LayerMetadata());
61         return sp<Layer>::make(args);
62     }
63 
commitTransaction(Layer * layer)64     void commitTransaction(Layer* layer) { layer->commitTransaction(); }
65 
66     TestableSurfaceFlinger mFlinger;
67     renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine();
68 
69     FenceToFenceTimeMap fenceFactory;
70 
BLASTTransactionSendsFrameTracerEvents()71     void BLASTTransactionSendsFrameTracerEvents() {
72         sp<Layer> layer = createLayer();
73 
74         sp<Fence> fence(sp<Fence>::make());
75         int32_t layerId = layer->getSequence();
76         uint64_t bufferId = 42;
77         uint64_t frameNumber = 5;
78         nsecs_t dequeueTime = 10;
79         nsecs_t postTime = 20;
80         EXPECT_CALL(*mFlinger.getFrameTracer(), traceNewLayer(layerId, "buffer-state-layer"));
81         EXPECT_CALL(*mFlinger.getFrameTracer(),
82                     traceTimestamp(layerId, bufferId, frameNumber, dequeueTime,
83                                    FrameTracer::FrameEvent::DEQUEUE, /*duration*/ 0));
84         EXPECT_CALL(*mFlinger.getFrameTracer(),
85                     traceTimestamp(layerId, bufferId, frameNumber, postTime,
86                                    FrameTracer::FrameEvent::QUEUE, /*duration*/ 0));
87         BufferData bufferData;
88         bufferData.acquireFence = fence;
89         bufferData.frameNumber = frameNumber;
90         bufferData.flags |= BufferData::BufferDataChange::fenceChanged;
91         bufferData.flags |= BufferData::BufferDataChange::frameNumberChanged;
92         std::shared_ptr<renderengine::ExternalTexture> externalTexture = std::make_shared<
93                 renderengine::mock::FakeExternalTexture>(1U /*width*/, 1U /*height*/, bufferId,
94                                                          HAL_PIXEL_FORMAT_RGBA_8888,
95                                                          0ULL /*usage*/);
96         layer->setBuffer(externalTexture, bufferData, postTime, /*desiredPresentTime*/ 30, false,
97                          FrameTimelineInfo{});
98 
99         commitTransaction(layer.get());
100         nsecs_t latchTime = 25;
101         EXPECT_CALL(*mFlinger.getFrameTracer(),
102                     traceFence(layerId, bufferId, frameNumber, _,
103                                FrameTracer::FrameEvent::ACQUIRE_FENCE, /*startTime*/ 0));
104         EXPECT_CALL(*mFlinger.getFrameTracer(),
105                     traceTimestamp(layerId, bufferId, frameNumber, latchTime,
106                                    FrameTracer::FrameEvent::LATCH, /*duration*/ 0));
107         layer->updateTexImage(latchTime);
108 
109         auto glDoneFence = fenceFactory.createFenceTimeForTest(fence);
110         auto presentFence = fenceFactory.createFenceTimeForTest(fence);
111         CompositorTiming compositorTiming;
112         EXPECT_CALL(*mFlinger.getFrameTracer(),
113                     traceFence(layerId, bufferId, frameNumber, presentFence,
114                                FrameTracer::FrameEvent::PRESENT_FENCE, /*startTime*/ 0));
115         layer->onCompositionPresented(nullptr, glDoneFence, presentFence, compositorTiming);
116     }
117 };
118 
TEST_F(TransactionFrameTracerTest,BLASTTransactionSendsFrameTracerEvents)119 TEST_F(TransactionFrameTracerTest, BLASTTransactionSendsFrameTracerEvents) {
120     BLASTTransactionSendsFrameTracerEvents();
121 }
122 
123 } // namespace android
124