1 /*
2  * Copyright 2017 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 <chrono>
20 
21 #include <composer-hal/2.1/ComposerClient.h>
22 #include <composer-hal/2.2/ComposerClient.h>
23 #include <composer-hal/2.3/ComposerClient.h>
24 #include <composer-hal/2.4/ComposerClient.h>
25 #include <utils/Condition.h>
26 
27 #include "MockComposerHal.h"
28 #include "RenderState.h"
29 
30 using namespace android::hardware::graphics::common;
31 using namespace android::hardware::graphics::composer;
32 using namespace android::hardware::graphics::composer::V2_4;
33 using namespace android::hardware::graphics::composer::V2_4::hal;
34 using namespace android::hardware;
35 using namespace std::chrono_literals;
36 
37 namespace {
38 class LayerImpl;
39 class Frame;
40 class DelayedEventGenerator;
41 } // namespace
42 
43 namespace android {
44 class SurfaceComposerClient;
45 } // namespace android
46 
47 namespace sftest {
48 // NOTE: The ID's need to be exactly these. VR composer and parts of
49 // the SurfaceFlinger assume the display IDs to have these values
50 // despite the enum being documented as a display type.
51 // TODO: Reference to actual documentation
52 constexpr Display PRIMARY_DISPLAY = static_cast<Display>(HWC_DISPLAY_PRIMARY);
53 constexpr Display EXTERNAL_DISPLAY = static_cast<Display>(HWC_DISPLAY_EXTERNAL);
54 
55 class FakeComposerClient : public ComposerHal {
56 public:
57     FakeComposerClient();
58     virtual ~FakeComposerClient();
59 
setMockHal(MockComposerHal * mockHal)60     void setMockHal(MockComposerHal* mockHal) { mMockHal = mockHal; }
61 
62     bool hasCapability(hwc2_capability_t capability) override;
63 
64     std::string dumpDebugInfo() override;
65     void registerEventCallback(EventCallback* callback) override;
66     void unregisterEventCallback() override;
67 
68     uint32_t getMaxVirtualDisplayCount() override;
69     V2_1::Error createVirtualDisplay(uint32_t width, uint32_t height, V1_0::PixelFormat* format,
70                                      Display* outDisplay) override;
71     V2_1::Error destroyVirtualDisplay(Display display) override;
72     V2_1::Error createLayer(Display display, Layer* outLayer) override;
73     V2_1::Error destroyLayer(Display display, Layer layer) override;
74 
75     V2_1::Error getActiveConfig(Display display, Config* outConfig) override;
76     V2_1::Error getClientTargetSupport(Display display, uint32_t width, uint32_t height,
77                                        V1_0::PixelFormat format,
78                                        V1_0::Dataspace dataspace) override;
79     V2_1::Error getColorModes(Display display, hidl_vec<V1_0::ColorMode>* outModes) override;
80     V2_1::Error getDisplayAttribute(Display display, Config config,
81                                     V2_1::IComposerClient::Attribute attribute,
82                                     int32_t* outValue) override;
83     V2_1::Error getDisplayConfigs(Display display, hidl_vec<Config>* outConfigs) override;
84     V2_1::Error getDisplayName(Display display, hidl_string* outName) override;
85     V2_1::Error getDisplayType(Display display, IComposerClient::DisplayType* outType) override;
86     V2_1::Error getDozeSupport(Display display, bool* outSupport) override;
87     V2_1::Error getHdrCapabilities(Display display, hidl_vec<V1_0::Hdr>* outTypes,
88                                    float* outMaxLuminance, float* outMaxAverageLuminance,
89                                    float* outMinLuminance) override;
90 
91     V2_1::Error setActiveConfig(Display display, Config config) override;
92     V2_1::Error setColorMode(Display display, V1_0::ColorMode mode) override;
93     V2_1::Error setPowerMode(Display display, V2_1::IComposerClient::PowerMode mode) override;
94     V2_1::Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled) override;
95 
96     V2_1::Error setColorTransform(Display display, const float* matrix, int32_t hint) override;
97     V2_1::Error setClientTarget(Display display, buffer_handle_t target, int32_t acquireFence,
98                                 int32_t dataspace, const std::vector<hwc_rect_t>& damage) override;
99     V2_1::Error setOutputBuffer(Display display, buffer_handle_t buffer,
100                                 int32_t releaseFence) override;
101     V2_1::Error validateDisplay(Display display, std::vector<Layer>* outChangedLayers,
102                                 std::vector<IComposerClient::Composition>* outCompositionTypes,
103                                 uint32_t* outDisplayRequestMask,
104                                 std::vector<Layer>* outRequestedLayers,
105                                 std::vector<uint32_t>* outRequestMasks) override;
106     V2_1::Error acceptDisplayChanges(Display display) override;
107     V2_1::Error presentDisplay(Display display, int32_t* outPresentFence,
108                                std::vector<Layer>* outLayers,
109                                std::vector<int32_t>* outReleaseFences) override;
110 
111     V2_1::Error setLayerCursorPosition(Display display, Layer layer, int32_t x, int32_t y) override;
112     V2_1::Error setLayerBuffer(Display display, Layer layer, buffer_handle_t buffer,
113                                int32_t acquireFence) override;
114     V2_1::Error setLayerSurfaceDamage(Display display, Layer layer,
115                                       const std::vector<hwc_rect_t>& damage) override;
116     V2_1::Error setLayerBlendMode(Display display, Layer layer, int32_t mode) override;
117     V2_1::Error setLayerColor(Display display, Layer layer, IComposerClient::Color color) override;
118     V2_1::Error setLayerCompositionType(Display display, Layer layer, int32_t type) override;
119     V2_1::Error setLayerDataspace(Display display, Layer layer, int32_t dataspace) override;
120     V2_1::Error setLayerDisplayFrame(Display display, Layer layer,
121                                      const hwc_rect_t& frame) override;
122     V2_1::Error setLayerPlaneAlpha(Display display, Layer layer, float alpha) override;
123     V2_1::Error setLayerSidebandStream(Display display, Layer layer,
124                                        buffer_handle_t stream) override;
125     V2_1::Error setLayerSourceCrop(Display display, Layer layer, const hwc_frect_t& crop) override;
126     V2_1::Error setLayerTransform(Display display, Layer layer, int32_t transform) override;
127     V2_1::Error setLayerVisibleRegion(Display display, Layer layer,
128                                       const std::vector<hwc_rect_t>& visible) override;
129     V2_1::Error setLayerZOrder(Display display, Layer layer, uint32_t z) override;
130 
131     // Composer 2.2
132     V2_1::Error getPerFrameMetadataKeys(
133             Display display,
134             std::vector<V2_2::IComposerClient::PerFrameMetadataKey>* outKeys) override;
135     V2_1::Error setLayerPerFrameMetadata(
136             Display display, Layer layer,
137             const std::vector<V2_2::IComposerClient::PerFrameMetadata>& metadata) override;
138 
139     V2_1::Error getReadbackBufferAttributes(
140             Display display, graphics::common::V1_1::PixelFormat* outFormat,
141             graphics::common::V1_1::Dataspace* outDataspace) override;
142     V2_1::Error setReadbackBuffer(Display display, const native_handle_t* bufferHandle,
143                                   android::base::unique_fd fenceFd) override;
144     V2_1::Error getReadbackBufferFence(Display display,
145                                        android::base::unique_fd* outFenceFd) override;
146     V2_1::Error createVirtualDisplay_2_2(uint32_t width, uint32_t height,
147                                          graphics::common::V1_1::PixelFormat* format,
148                                          Display* outDisplay) override;
149     V2_1::Error getClientTargetSupport_2_2(Display display, uint32_t width, uint32_t height,
150                                            graphics::common::V1_1::PixelFormat format,
151                                            graphics::common::V1_1::Dataspace dataspace) override;
152     V2_1::Error setPowerMode_2_2(Display display, V2_2::IComposerClient::PowerMode mode) override;
153 
154     V2_1::Error setLayerFloatColor(Display display, Layer layer,
155                                    V2_2::IComposerClient::FloatColor color) override;
156 
157     V2_1::Error getColorModes_2_2(Display display,
158                                   hidl_vec<graphics::common::V1_1::ColorMode>* outModes) override;
159     V2_1::Error getRenderIntents(
160             Display display, graphics::common::V1_1::ColorMode mode,
161             std::vector<graphics::common::V1_1::RenderIntent>* outIntents) override;
162     V2_1::Error setColorMode_2_2(Display display, graphics::common::V1_1::ColorMode mode,
163                                  graphics::common::V1_1::RenderIntent intent) override;
164 
165     std::array<float, 16> getDataspaceSaturationMatrix(
166             graphics::common::V1_1::Dataspace dataspace) override;
167 
168     // Composer 2.3
169     V2_1::Error getPerFrameMetadataKeys_2_3(
170             Display display,
171             std::vector<V2_3::IComposerClient::PerFrameMetadataKey>* outKeys) override;
172 
173     V2_1::Error setColorMode_2_3(Display display, graphics::common::V1_2::ColorMode mode,
174                                  graphics::common::V1_1::RenderIntent intent) override;
175 
176     V2_1::Error getRenderIntents_2_3(
177             Display display, graphics::common::V1_2::ColorMode mode,
178             std::vector<graphics::common::V1_1::RenderIntent>* outIntents) override;
179 
180     V2_1::Error getColorModes_2_3(Display display,
181                                   hidl_vec<graphics::common::V1_2::ColorMode>* outModes) override;
182 
183     V2_1::Error getClientTargetSupport_2_3(Display display, uint32_t width, uint32_t height,
184                                            graphics::common::V1_2::PixelFormat format,
185                                            graphics::common::V1_2::Dataspace dataspace) override;
186     V2_1::Error getReadbackBufferAttributes_2_3(
187             Display display, graphics::common::V1_2::PixelFormat* outFormat,
188             graphics::common::V1_2::Dataspace* outDataspace) override;
189     V2_1::Error getHdrCapabilities_2_3(Display display,
190                                        hidl_vec<graphics::common::V1_2::Hdr>* outTypes,
191                                        float* outMaxLuminance, float* outMaxAverageLuminance,
192                                        float* outMinLuminance) override;
193     V2_1::Error setLayerPerFrameMetadata_2_3(
194             Display display, Layer layer,
195             const std::vector<V2_3::IComposerClient::PerFrameMetadata>& metadata) override;
196     V2_1::Error getDisplayIdentificationData(Display display, uint8_t* outPort,
197                                              std::vector<uint8_t>* outData) override;
198     V2_1::Error setLayerColorTransform(Display display, Layer layer, const float* matrix) override;
199     V2_1::Error getDisplayedContentSamplingAttributes(
200             uint64_t display, graphics::common::V1_2::PixelFormat& format,
201             graphics::common::V1_2::Dataspace& dataspace,
202             hidl_bitfield<V2_3::IComposerClient::FormatColorComponent>& componentMask) override;
203     V2_1::Error setDisplayedContentSamplingEnabled(
204             uint64_t display, V2_3::IComposerClient::DisplayedContentSampling enable,
205             hidl_bitfield<V2_3::IComposerClient::FormatColorComponent> componentMask,
206             uint64_t maxFrames) override;
207     V2_1::Error getDisplayedContentSample(uint64_t display, uint64_t maxFrames, uint64_t timestamp,
208                                           uint64_t& frameCount,
209                                           hidl_vec<uint64_t>& sampleComponent0,
210                                           hidl_vec<uint64_t>& sampleComponent1,
211                                           hidl_vec<uint64_t>& sampleComponent2,
212                                           hidl_vec<uint64_t>& sampleComponent3) override;
213     V2_1::Error getDisplayCapabilities(
214             Display display,
215             std::vector<V2_3::IComposerClient::DisplayCapability>* outCapabilities) override;
216     V2_1::Error setLayerPerFrameMetadataBlobs(
217             Display display, Layer layer,
218             std::vector<V2_3::IComposerClient::PerFrameMetadataBlob>& blobs) override;
219     V2_1::Error getDisplayBrightnessSupport(Display display, bool* outSupport) override;
220     V2_1::Error setDisplayBrightness(Display display, float brightness) override;
221 
222     // Composer 2.4
223     void registerEventCallback_2_4(EventCallback_2_4* callback) override;
224 
225     void unregisterEventCallback_2_4() override;
226 
227     V2_4::Error getDisplayCapabilities_2_4(
228             Display display,
229             std::vector<V2_4::IComposerClient::DisplayCapability>* outCapabilities) override;
230     V2_4::Error getDisplayConnectionType(
231             Display display, V2_4::IComposerClient::DisplayConnectionType* outType) override;
232     V2_4::Error getDisplayAttribute_2_4(Display display, Config config,
233                                         IComposerClient::Attribute attribute,
234                                         int32_t* outValue) override;
235     V2_4::Error getDisplayVsyncPeriod(Display display,
236                                       V2_4::VsyncPeriodNanos* outVsyncPeriod) override;
237     V2_4::Error setActiveConfigWithConstraints(
238             Display display, Config config,
239             const V2_4::IComposerClient::VsyncPeriodChangeConstraints& vsyncPeriodChangeConstraints,
240             VsyncPeriodChangeTimeline* outTimeline) override;
241     V2_4::Error setAutoLowLatencyMode(Display display, bool on) override;
242     V2_4::Error getSupportedContentTypes(
243             Display display,
244             std::vector<IComposerClient::ContentType>* outSupportedContentTypes) override;
245     V2_4::Error setContentType(Display display, IComposerClient::ContentType type) override;
246     V2_4::Error validateDisplay_2_4(
247             Display display, std::vector<Layer>* outChangedLayers,
248             std::vector<IComposerClient::Composition>* outCompositionTypes,
249             uint32_t* outDisplayRequestMask, std::vector<Layer>* outRequestedLayers,
250             std::vector<uint32_t>* outRequestMasks,
251             IComposerClient::ClientTargetProperty* outClientTargetProperty) override;
252     V2_4::Error setLayerGenericMetadata(Display display, Layer layer, const std::string& key,
253                                         bool mandatory, const std::vector<uint8_t>& value) override;
254     V2_4::Error getLayerGenericMetadataKeys(
255             std::vector<IComposerClient::LayerGenericMetadataKey>* outKeys) override;
256 
257     void setClient(ComposerClient* client);
258 
259     void requestVSync(uint64_t vsyncTime = 0);
260     // We don't want tests hanging, so always use a timeout. Remember
261     // to always check the number of frames with test ASSERT_!
262     // Wait until next frame is rendered after requesting vsync.
263     void runVSyncAndWait(std::chrono::nanoseconds maxWait = 100ms);
264     void runVSyncAfter(std::chrono::nanoseconds wait);
265 
266     int getFrameCount() const;
267     // We don't want tests hanging, so always use a timeout. Remember
268     // to always check the number of frames with test ASSERT_!
269     void waitUntilFrame(int targetFrame, std::chrono::nanoseconds maxWait = 100ms) const;
270     std::vector<RenderState> getFrameRects(int frame) const;
271     std::vector<RenderState> getLatestFrame() const;
272     void clearFrames();
273 
274     void onSurfaceFlingerStart();
275     void onSurfaceFlingerStop();
276 
277     int getLayerCount() const;
278     Layer getLayer(size_t index) const;
279 
280     void hotplugDisplay(Display display, IComposerCallback::Connection state);
281     void refreshDisplay(Display display);
282 
283 private:
284     LayerImpl& getLayerImpl(Layer handle);
285 
286     EventCallback* mEventCallback;
287     EventCallback_2_4* mEventCallback_2_4;
288     Config mCurrentConfig;
289     bool mVsyncEnabled;
290     std::vector<std::unique_ptr<LayerImpl>> mLayers;
291     std::vector<std::unique_ptr<Frame>> mFrames;
292     // Using a pointer to hide the implementation into the CPP file.
293     std::unique_ptr<DelayedEventGenerator> mDelayedEventGenerator;
294     android::sp<android::SurfaceComposerClient> mSurfaceComposer; // For VSync injections
295     mutable android::Mutex mStateMutex;
296     mutable android::Condition mFramesAvailable;
297 
298     MockComposerHal* mMockHal = nullptr;
299 };
300 
301 } // namespace sftest
302