1 /*
2  * Copyright 2022 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 #pragma once
18 
19 #include <gmock/gmock.h>
20 
21 #include "TestableSurfaceFlinger.h"
22 #include "mock/DisplayHardware/MockPowerAdvisor.h"
23 #include "mock/system/window/MockNativeWindow.h"
24 
25 namespace android {
26 
27 using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
28 using android::hardware::graphics::composer::hal::HWDisplayId;
29 using android::Hwc2::mock::PowerAdvisor;
30 
31 struct FakeDisplayInjectorArgs {
32     PhysicalDisplayId displayId = PhysicalDisplayId::fromPort(255u);
33     HWDisplayId hwcDisplayId = 0;
34     bool isPrimary = true;
35 };
36 
37 class FakeDisplayInjector {
38 public:
FakeDisplayInjector(TestableSurfaceFlinger & flinger,Hwc2::mock::PowerAdvisor & powerAdvisor,sp<mock::NativeWindow> nativeWindow)39     FakeDisplayInjector(TestableSurfaceFlinger& flinger, Hwc2::mock::PowerAdvisor& powerAdvisor,
40                         sp<mock::NativeWindow> nativeWindow)
41           : mFlinger(flinger), mPowerAdvisor(powerAdvisor), mNativeWindow(nativeWindow) {}
42 
43     sp<DisplayDevice> injectInternalDisplay(
44             const std::function<void(FakeDisplayDeviceInjector&)>& injectExtra,
45             FakeDisplayInjectorArgs args = {}) {
46         using testing::_;
47         using testing::AnyNumber;
48         using testing::DoAll;
49         using testing::Mock;
50         using testing::Return;
51         using testing::SetArgPointee;
52 
53         constexpr ui::Size kResolution = {1080, 1920};
54 
55         // The DisplayDevice is required to have a framebuffer (behind the
56         // ANativeWindow interface) which uses the actual hardware display
57         // size.
58         EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_WIDTH, _))
59                 .WillRepeatedly(DoAll(SetArgPointee<1>(kResolution.getWidth()), Return(0)));
60         EXPECT_CALL(*mNativeWindow, query(NATIVE_WINDOW_HEIGHT, _))
61                 .WillRepeatedly(DoAll(SetArgPointee<1>(kResolution.getHeight()), Return(0)));
62         EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_BUFFERS_FORMAT));
63         EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_CONNECT));
64         EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_SET_USAGE64));
65         EXPECT_CALL(*mNativeWindow, perform(NATIVE_WINDOW_API_DISCONNECT)).Times(AnyNumber());
66 
67         auto compositionDisplay = compositionengine::impl::
68                 createDisplay(mFlinger.getCompositionEngine(),
69                               compositionengine::DisplayCreationArgsBuilder()
70                                       .setId(args.displayId)
71                                       .setPixels(kResolution)
72                                       .setPowerAdvisor(&mPowerAdvisor)
73                                       .build());
74 
75         auto injector = FakeDisplayDeviceInjector(mFlinger, compositionDisplay,
76                                                   ui::DisplayConnectionType::Internal,
77                                                   args.hwcDisplayId, args.isPrimary);
78 
79         injector.setNativeWindow(mNativeWindow);
80         if (injectExtra) {
81             injectExtra(injector);
82         }
83 
84         auto displayDevice = injector.inject();
85 
86         Mock::VerifyAndClear(mNativeWindow.get());
87 
88         return displayDevice;
89     }
90 
91     TestableSurfaceFlinger& mFlinger;
92     Hwc2::mock::PowerAdvisor& mPowerAdvisor;
93     sp<mock::NativeWindow> mNativeWindow;
94 };
95 
96 } // namespace android
97