1 /**
2  * Copyright (c) 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 #pragma once
17 
18 #include <aidl/android/hardware/graphics/common/BlendMode.h>
19 #include <aidl/android/hardware/graphics/common/BufferUsage.h>
20 #include <aidl/android/hardware/graphics/common/FRect.h>
21 #include <aidl/android/hardware/graphics/common/Rect.h>
22 #include <aidl/android/hardware/graphics/composer3/Composition.h>
23 #include <aidl/android/hardware/graphics/composer3/IComposer.h>
24 #include <android-base/properties.h>
25 #include <android/binder_manager.h>
26 #include <android/binder_process.h>
27 #include <android/hardware/graphics/composer3/ComposerClientReader.h>
28 #include <android/hardware/graphics/composer3/ComposerClientWriter.h>
29 #include <binder/ProcessState.h>
30 #include <gtest/gtest.h>
31 #include <ui/Fence.h>
32 #include <ui/GraphicBuffer.h>
33 #include <ui/PixelFormat.h>
34 #include <algorithm>
35 #include <numeric>
36 #include <string>
37 #include <thread>
38 #include <unordered_map>
39 #include <unordered_set>
40 #include "GraphicsComposerCallback.h"
41 
42 using aidl::android::hardware::graphics::common::Dataspace;
43 using aidl::android::hardware::graphics::common::DisplayDecorationSupport;
44 using aidl::android::hardware::graphics::common::FRect;
45 using aidl::android::hardware::graphics::common::PixelFormat;
46 using aidl::android::hardware::graphics::common::Rect;
47 using namespace ::ndk;
48 
49 namespace aidl::android::hardware::graphics::composer3::vts {
50 
51 class VtsDisplay;
52 /**
53  * A wrapper to IComposerClient.
54  * This wrapper manages the IComposerClient instance and manages the resources for
55  * the tests with respect to the IComposerClient calls.
56  */
57 class VtsComposerClient {
58   public:
59     VtsComposerClient(const std::string& name);
60 
61     ScopedAStatus createClient();
62 
63     bool tearDown(ComposerClientWriter*);
64 
65     std::pair<ScopedAStatus, int32_t> getInterfaceVersion() const;
66 
67     std::pair<ScopedAStatus, VirtualDisplay> createVirtualDisplay(int32_t width, int32_t height,
68                                                                   PixelFormat pixelFormat,
69                                                                   int32_t bufferSlotCount);
70 
71     ScopedAStatus destroyVirtualDisplay(int64_t display);
72 
73     std::pair<ScopedAStatus, int64_t> createLayer(int64_t display, int32_t bufferSlotCount,
74                                                   ComposerClientWriter*);
75 
76     ScopedAStatus destroyLayer(int64_t display, int64_t layer, ComposerClientWriter*);
77 
78     std::pair<ScopedAStatus, int32_t> getActiveConfig(int64_t display);
79 
80     ScopedAStatus setActiveConfig(VtsDisplay* vtsDisplay, int32_t config);
81 
82     ScopedAStatus setPeakRefreshRateConfig(VtsDisplay* vtsDisplay);
83 
84     std::pair<ScopedAStatus, int32_t> getDisplayAttribute(int64_t display, int32_t config,
85                                                           DisplayAttribute displayAttribute);
86 
87     ScopedAStatus setPowerMode(int64_t display, PowerMode powerMode);
88 
89     ScopedAStatus setVsync(int64_t display, bool enable);
90 
91     void setVsyncAllowed(bool isAllowed);
92 
93     std::pair<ScopedAStatus, std::vector<float>> getDataspaceSaturationMatrix(Dataspace dataspace);
94 
95     std::pair<ScopedAStatus, std::vector<CommandResultPayload>> executeCommands(
96             const std::vector<DisplayCommand>& commands);
97 
98     std::optional<VsyncPeriodChangeTimeline> takeLastVsyncPeriodChangeTimeline();
99 
100     ScopedAStatus setContentType(int64_t display, ContentType contentType);
101 
102     std::pair<ScopedAStatus, VsyncPeriodChangeTimeline> setActiveConfigWithConstraints(
103             VtsDisplay* vtsDisplay, int32_t config,
104             const VsyncPeriodChangeConstraints& constraints);
105 
106     std::pair<ScopedAStatus, std::vector<DisplayCapability>> getDisplayCapabilities(
107             int64_t display);
108 
109     ScopedAStatus dumpDebugInfo();
110 
111     std::pair<ScopedAStatus, DisplayIdentification> getDisplayIdentificationData(int64_t display);
112 
113     std::pair<ScopedAStatus, HdrCapabilities> getHdrCapabilities(int64_t display);
114 
115     std::pair<ScopedAStatus, std::vector<PerFrameMetadataKey>> getPerFrameMetadataKeys(
116             int64_t display);
117 
118     std::pair<ScopedAStatus, ReadbackBufferAttributes> getReadbackBufferAttributes(int64_t display);
119 
120     ScopedAStatus setReadbackBuffer(int64_t display, const native_handle_t* buffer,
121                                     const ScopedFileDescriptor& releaseFence);
122 
123     std::pair<ScopedAStatus, ScopedFileDescriptor> getReadbackBufferFence(int64_t display);
124 
125     std::pair<ScopedAStatus, std::vector<ColorMode>> getColorModes(int64_t display);
126 
127     std::pair<ScopedAStatus, std::vector<RenderIntent>> getRenderIntents(int64_t display,
128                                                                          ColorMode colorMode);
129 
130     ScopedAStatus setColorMode(int64_t display, ColorMode colorMode, RenderIntent renderIntent);
131 
132     std::pair<ScopedAStatus, DisplayContentSamplingAttributes>
133     getDisplayedContentSamplingAttributes(int64_t display);
134 
135     ScopedAStatus setDisplayedContentSamplingEnabled(int64_t display, bool isEnabled,
136                                                      FormatColorComponent formatColorComponent,
137                                                      int64_t maxFrames);
138 
139     std::pair<ScopedAStatus, DisplayContentSample> getDisplayedContentSample(int64_t display,
140                                                                              int64_t maxFrames,
141                                                                              int64_t timestamp);
142 
143     std::pair<ScopedAStatus, DisplayConnectionType> getDisplayConnectionType(int64_t display);
144 
145     std::pair<ScopedAStatus, std::vector<int32_t>> getDisplayConfigs(int64_t display);
146 
147     std::pair<ScopedAStatus, std::vector<DisplayConfiguration>> getDisplayConfigurations(
148             int64_t display);
149 
150     ScopedAStatus notifyExpectedPresent(int64_t display,
151                                         ClockMonotonicTimestamp expectedPresentTime,
152                                         int frameIntervalNs);
153 
154     std::pair<ScopedAStatus, int32_t> getDisplayVsyncPeriod(int64_t display);
155 
156     ScopedAStatus setAutoLowLatencyMode(int64_t display, bool isEnabled);
157 
158     std::pair<ScopedAStatus, std::vector<ContentType>> getSupportedContentTypes(int64_t display);
159 
160     std::pair<ScopedAStatus, std::optional<DisplayDecorationSupport>> getDisplayDecorationSupport(
161             int64_t display);
162 
163     std::pair<ScopedAStatus, int32_t> getMaxVirtualDisplayCount();
164 
165     std::pair<ScopedAStatus, std::string> getDisplayName(int64_t display);
166 
167     ScopedAStatus setClientTargetSlotCount(int64_t display, int32_t bufferSlotCount);
168 
169     std::pair<ScopedAStatus, std::vector<Capability>> getCapabilities();
170 
171     ScopedAStatus setBootDisplayConfig(int64_t display, int32_t config);
172 
173     ScopedAStatus clearBootDisplayConfig(int64_t display);
174 
175     std::pair<ScopedAStatus, int32_t> getPreferredBootDisplayConfig(int64_t display);
176 
177     std::pair<ScopedAStatus, std::vector<common::HdrConversionCapability>>
178     getHdrConversionCapabilities();
179 
180     std::pair<ScopedAStatus, common::Hdr> setHdrConversionStrategy(
181             const common::HdrConversionStrategy& conversionStrategy);
182 
183     std::pair<ScopedAStatus, common::Transform> getDisplayPhysicalOrientation(int64_t display);
184 
185     ScopedAStatus setIdleTimerEnabled(int64_t display, int32_t timeoutMs);
186 
187     int32_t getVsyncIdleCount();
188 
189     int64_t getVsyncIdleTime();
190 
191     int64_t getInvalidDisplayId();
192 
193     std::pair<ScopedAStatus, std::vector<VtsDisplay>> getDisplays();
194 
195     std::pair<ScopedAStatus, OverlayProperties> getOverlaySupport();
196 
197     ndk::ScopedAStatus setRefreshRateChangedCallbackDebugEnabled(int64_t display, bool enabled);
198 
199     std::vector<RefreshRateChangedDebugData> takeListOfRefreshRateChangedDebugData();
200 
201     static constexpr int32_t kMaxFrameIntervalNs = 50000000;  // 20fps
202     static constexpr int32_t kNoFrameIntervalNs = 0;
203 
204   private:
205     void addDisplayConfigs(VtsDisplay*, const std::vector<DisplayConfiguration>&);
206     ScopedAStatus addDisplayConfigLegacy(VtsDisplay*, int32_t config);
207     bool getDisplayConfigurationSupported() const;
208     ScopedAStatus updateDisplayProperties(VtsDisplay* vtsDisplay, int32_t config);
209 
210     ScopedAStatus addDisplayToDisplayResources(int64_t display, bool isVirtual);
211 
212     ScopedAStatus addLayerToDisplayResources(int64_t display, int64_t layer);
213 
214     void removeLayerFromDisplayResources(int64_t display, int64_t layer);
215 
216     bool destroyAllLayers(ComposerClientWriter*);
217 
218     bool verifyComposerCallbackParams();
219 
220     // Keep track of displays and layers. When a test fails/ends,
221     // the VtsComposerClient::tearDown should be called from the
222     // test tearDown to clean up the resources for the test.
223     struct DisplayResource {
DisplayResourceDisplayResource224         DisplayResource(bool isVirtual_) : isVirtual(isVirtual_) {}
225 
226         bool isVirtual;
227         std::unordered_set<int64_t> layers;
228     };
229 
230     std::shared_ptr<IComposer> mComposer;
231     std::shared_ptr<IComposerClient> mComposerClient;
232     std::shared_ptr<GraphicsComposerCallback> mComposerCallback;
233     std::unordered_map<int64_t, DisplayResource> mDisplayResources;
234     bool mSupportsBatchedCreateLayer = false;
235     std::atomic<int64_t> mNextLayerHandle = 1;
236 };
237 
238 class VtsDisplay {
239   public:
VtsDisplay(int64_t displayId)240     VtsDisplay(int64_t displayId) : mDisplayId(displayId), mDisplayWidth(0), mDisplayHeight(0) {}
241 
getDisplayId()242     int64_t getDisplayId() const { return mDisplayId; }
243 
getCrop()244     FRect getCrop() const {
245         return {0, 0, static_cast<float>(mDisplayWidth), static_cast<float>(mDisplayHeight)};
246     }
247 
getFrameRect()248     Rect getFrameRect() const { return {0, 0, mDisplayWidth, mDisplayHeight}; }
249 
setDimensions(int32_t displayWidth,int32_t displayHeight)250     void setDimensions(int32_t displayWidth, int32_t displayHeight) {
251         mDisplayWidth = displayWidth;
252         mDisplayHeight = displayHeight;
253     }
254 
getDisplayWidth()255     int32_t getDisplayWidth() const { return mDisplayWidth; }
256 
getDisplayHeight()257     int32_t getDisplayHeight() const { return mDisplayHeight; }
258 
259     struct DisplayConfig {
260         DisplayConfig(int32_t vsyncPeriod_, int32_t configGroup_,
261                       std::optional<VrrConfig> vrrConfigOpt_ = {})
vsyncPeriodDisplayConfig262             : vsyncPeriod(vsyncPeriod_),
263               configGroup(configGroup_),
264               vrrConfigOpt(std::move(vrrConfigOpt_)) {}
265         int32_t vsyncPeriod;
266         int32_t configGroup;
267         std::optional<VrrConfig> vrrConfigOpt;
268     };
269 
addDisplayConfig(int32_t config,DisplayConfig displayConfig)270     void addDisplayConfig(int32_t config, DisplayConfig displayConfig) {
271         mDisplayConfigs.insert({config, displayConfig});
272     }
273 
getDisplayConfig(int32_t config)274     DisplayConfig getDisplayConfig(int32_t config) { return mDisplayConfigs.find(config)->second; }
275 
isRateSameBetweenConfigs(int config1,int config2)276     bool isRateSameBetweenConfigs(int config1, int config2) {
277         const auto displayConfig1 = getDisplayConfig(config1);
278         const auto displayConfig2 = getDisplayConfig(config2);
279         const auto vrrConfigOpt1 = displayConfig1.vrrConfigOpt;
280         const auto vrrConfigOpt2 = displayConfig2.vrrConfigOpt;
281 
282         if (vrrConfigOpt1 && vrrConfigOpt2 &&
283             vrrConfigOpt1->minFrameIntervalNs == vrrConfigOpt2->minFrameIntervalNs) {
284             return true;
285         } else if (displayConfig1.vsyncPeriod == displayConfig2.vsyncPeriod) {
286             return true;
287         }
288         return false;
289     }
290 
printConfig(int config)291     std::string printConfig(int config) {
292         const auto displayConfig = getDisplayConfig(config);
293         const auto vrrConfigOpt = displayConfig.vrrConfigOpt;
294         std::stringstream ss;
295         if (displayConfig.vrrConfigOpt) {
296             ss << "{Config " << config << ": vsyncPeriod " << displayConfig.vsyncPeriod
297                 << ", minFrameIntervalNs " << vrrConfigOpt->minFrameIntervalNs << "}";
298         }
299         else {
300             ss << "{Config " << config << ": vsyncPeriod " << displayConfig.vsyncPeriod << "}";
301         }
302         return ss.str();
303     }
304 
getDisplayConfigs()305     std::unordered_map<int32_t, DisplayConfig> getDisplayConfigs() { return mDisplayConfigs; }
306 
307   private:
308     int64_t mDisplayId;
309     int32_t mDisplayWidth;
310     int32_t mDisplayHeight;
311     std::unordered_map<int32_t, DisplayConfig> mDisplayConfigs;
312 };
313 }  // namespace aidl::android::hardware::graphics::composer3::vts
314