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