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 
17 #ifndef ANDROID_HWC_COMPOSERCLIENT_H
18 #define ANDROID_HWC_COMPOSERCLIENT_H
19 
20 #include <aidl/android/hardware/graphics/composer3/BnComposerClient.h>
21 #include <aidl/android/hardware/graphics/composer3/Luts.h>
22 #include <android-base/thread_annotations.h>
23 
24 #include <memory>
25 
26 #include "ComposerResources.h"
27 #include "Display.h"
28 #include "FrameComposer.h"
29 
30 namespace aidl::android::hardware::graphics::composer3::impl {
31 
32 class ComposerClient : public BnComposerClient {
33    public:
34     ComposerClient();
35     virtual ~ComposerClient();
36 
37     HWC3::Error init();
38 
setOnClientDestroyed(std::function<void ()> onClientDestroyed)39     void setOnClientDestroyed(std::function<void()> onClientDestroyed) {
40         mOnClientDestroyed = onClientDestroyed;
41     }
42 
43     // HWC3 interface:
44     ndk::ScopedAStatus createLayer(int64_t displayId, int32_t bufferSlotCount,
45                                    int64_t* layer) override;
46     ndk::ScopedAStatus createVirtualDisplay(int32_t width, int32_t height,
47                                             common::PixelFormat formatHint,
48                                             int32_t outputBufferSlotCount,
49                                             VirtualDisplay* display) override;
50     ndk::ScopedAStatus destroyLayer(int64_t displayId, int64_t layer) override;
51     ndk::ScopedAStatus destroyVirtualDisplay(int64_t displayId) override;
52     ndk::ScopedAStatus executeCommands(const std::vector<DisplayCommand>& commands,
53                                        std::vector<CommandResultPayload>* results) override;
54     ndk::ScopedAStatus getActiveConfig(int64_t displayId, int32_t* config) override;
55     ndk::ScopedAStatus getColorModes(int64_t displayId,
56                                      std::vector<ColorMode>* colorModes) override;
57     ndk::ScopedAStatus getDataspaceSaturationMatrix(common::Dataspace dataspace,
58                                                     std::vector<float>* matrix) override;
59     ndk::ScopedAStatus getDisplayAttribute(int64_t displayId, int32_t config,
60                                            DisplayAttribute attribute, int32_t* value) override;
61     ndk::ScopedAStatus getDisplayCapabilities(int64_t displayId,
62                                               std::vector<DisplayCapability>* caps) override;
63     ndk::ScopedAStatus getDisplayConfigs(int64_t displayId, std::vector<int32_t>* configs) override;
64     ndk::ScopedAStatus getDisplayConnectionType(int64_t displayId,
65                                                 DisplayConnectionType* type) override;
66     ndk::ScopedAStatus getDisplayIdentificationData(int64_t displayId,
67                                                     DisplayIdentification* id) override;
68     ndk::ScopedAStatus getDisplayName(int64_t displayId, std::string* name) override;
69     ndk::ScopedAStatus getDisplayVsyncPeriod(int64_t displayId, int32_t* vsyncPeriod) override;
70     ndk::ScopedAStatus getDisplayedContentSample(int64_t displayId, int64_t maxFrames,
71                                                  int64_t timestamp,
72                                                  DisplayContentSample* samples) override;
73     ndk::ScopedAStatus getDisplayedContentSamplingAttributes(
74         int64_t displayId, DisplayContentSamplingAttributes* attrs) override;
75     ndk::ScopedAStatus getDisplayPhysicalOrientation(int64_t displayId,
76                                                      common::Transform* orientation) override;
77     ndk::ScopedAStatus getHdrCapabilities(int64_t displayId, HdrCapabilities* caps) override;
78     ndk::ScopedAStatus getOverlaySupport(OverlayProperties* properties) override;
79     ndk::ScopedAStatus getMaxVirtualDisplayCount(int32_t* count) override;
80     ndk::ScopedAStatus getPerFrameMetadataKeys(int64_t displayId,
81                                                std::vector<PerFrameMetadataKey>* keys) override;
82     ndk::ScopedAStatus getReadbackBufferAttributes(int64_t displayId,
83                                                    ReadbackBufferAttributes* attrs) override;
84     ndk::ScopedAStatus getReadbackBufferFence(int64_t displayId,
85                                               ndk::ScopedFileDescriptor* acquireFence) override;
86     ndk::ScopedAStatus getRenderIntents(int64_t displayId, ColorMode mode,
87                                         std::vector<RenderIntent>* intents) override;
88     ndk::ScopedAStatus getSupportedContentTypes(int64_t displayId,
89                                                 std::vector<ContentType>* types) override;
90     ndk::ScopedAStatus getDisplayDecorationSupport(
91         int64_t displayId, std::optional<common::DisplayDecorationSupport>* support) override;
92     ndk::ScopedAStatus registerCallback(
93         const std::shared_ptr<IComposerCallback>& callback) override;
94     ndk::ScopedAStatus setActiveConfig(int64_t displayId, int32_t config) override;
95     ndk::ScopedAStatus setActiveConfigWithConstraints(
96         int64_t displayId, int32_t config, const VsyncPeriodChangeConstraints& constraints,
97         VsyncPeriodChangeTimeline* timeline) override;
98     ndk::ScopedAStatus setBootDisplayConfig(int64_t displayId, int32_t config) override;
99     ndk::ScopedAStatus clearBootDisplayConfig(int64_t displayId) override;
100     ndk::ScopedAStatus getPreferredBootDisplayConfig(int64_t displayId, int32_t* config) override;
101     ndk::ScopedAStatus getHdrConversionCapabilities(
102         std::vector<aidl::android::hardware::graphics::common::HdrConversionCapability>*) override;
103     ndk::ScopedAStatus setHdrConversionStrategy(
104         const aidl::android::hardware::graphics::common::HdrConversionStrategy& conversionStrategy,
105         aidl::android::hardware::graphics::common::Hdr* preferredHdrOutputType) override;
106     ndk::ScopedAStatus setAutoLowLatencyMode(int64_t displayId, bool on) override;
107     ndk::ScopedAStatus setClientTargetSlotCount(int64_t displayId, int32_t count) override;
108     ndk::ScopedAStatus setColorMode(int64_t displayId, ColorMode mode,
109                                     RenderIntent intent) override;
110     ndk::ScopedAStatus setContentType(int64_t displayId, ContentType type) override;
111     ndk::ScopedAStatus setDisplayedContentSamplingEnabled(int64_t displayId, bool enable,
112                                                           FormatColorComponent componentMask,
113                                                           int64_t maxFrames) override;
114     ndk::ScopedAStatus setPowerMode(int64_t displayId, PowerMode mode) override;
115     ndk::ScopedAStatus setReadbackBuffer(
116         int64_t displayId, const aidl::android::hardware::common::NativeHandle& buffer,
117         const ndk::ScopedFileDescriptor& releaseFence) override;
118     ndk::ScopedAStatus setVsyncEnabled(int64_t displayId, bool enabled) override;
119     ndk::ScopedAStatus setIdleTimerEnabled(int64_t displayId, int32_t timeoutMs) override;
120     ndk::ScopedAStatus setRefreshRateChangedCallbackDebugEnabled(int64_t displayId,
121                                                                  bool enabled) override;
122     ndk::ScopedAStatus getDisplayConfigurations(int64_t displayId, int32_t maxFrameIntervalNs,
123                                                 std::vector<DisplayConfiguration>*) override;
124     ndk::ScopedAStatus notifyExpectedPresent(int64_t displayId,
125                                              const ClockMonotonicTimestamp& expectedPresentTime,
126                                              int32_t maxFrameIntervalNs) override;
127     ndk::ScopedAStatus getMaxLayerPictureProfiles(int64_t displayId, int32_t* outMaxProfiles)
128                                                   override;
129     ndk::ScopedAStatus startHdcpNegotiation(
130         int64_t displayId, const aidl::android::hardware::drm::HdcpLevels& levels) override;
131     ndk::ScopedAStatus getLuts(int64_t displayId,
132             const std::vector<Buffer>&,
133             std::vector<Luts>*) override;
134 
135    protected:
136     ndk::SpAIBinder createBinder() override;
137 
138    private:
139     class CommandResultWriter;
140 
141     void executeDisplayCommand(CommandResultWriter& commandResults,
142                                const DisplayCommand& displayCommand);
143 
144     void executeLayerCommand(CommandResultWriter& commandResults, Display& display,
145                              const LayerCommand& layerCommand);
146 
147     void executeDisplayCommandSetColorTransform(CommandResultWriter& commandResults,
148                                                 Display& display, const std::vector<float>& matrix);
149     void executeDisplayCommandSetBrightness(CommandResultWriter& commandResults, Display& display,
150                                             const DisplayBrightness& brightness);
151     void executeDisplayCommandSetClientTarget(CommandResultWriter& commandResults, Display& display,
152                                               const ClientTarget& command);
153     void executeDisplayCommandSetOutputBuffer(CommandResultWriter& commandResults, Display& display,
154                                               const Buffer& buffer);
155     void executeDisplayCommandValidateDisplay(
156         CommandResultWriter& commandResults, Display& display,
157         const std::optional<ClockMonotonicTimestamp> expectedPresentTime);
158     void executeDisplayCommandAcceptDisplayChanges(CommandResultWriter& commandResults,
159                                                    Display& display);
160     void executeDisplayCommandPresentOrValidateDisplay(
161         CommandResultWriter& commandResults, Display& display,
162         const std::optional<ClockMonotonicTimestamp> expectedPresentTime);
163     void executeDisplayCommandPresentDisplay(CommandResultWriter& commandResults, Display& display);
164 
165     void executeLayerCommandSetLayerCursorPosition(CommandResultWriter& commandResults,
166                                                    Display& display, Layer* layer,
167                                                    const common::Point& cursorPosition);
168     void executeLayerCommandSetLayerBuffer(CommandResultWriter& commandResults, Display& display,
169                                            Layer* layer, const Buffer& buffer);
170     void executeLayerCommandSetLayerSurfaceDamage(
171         CommandResultWriter& commandResults, Display& display, Layer* layer,
172         const std::vector<std::optional<common::Rect>>& damage);
173     void executeLayerCommandSetLayerBlendMode(CommandResultWriter& commandResults, Display& display,
174                                               Layer* layer, const ParcelableBlendMode& blendMode);
175     void executeLayerCommandSetLayerColor(CommandResultWriter& commandResults, Display& display,
176                                           Layer* layer, const Color& color);
177     void executeLayerCommandSetLayerComposition(CommandResultWriter& commandResults,
178                                                 Display& display, Layer* layer,
179                                                 const ParcelableComposition& composition);
180     void executeLayerCommandSetLayerDataspace(CommandResultWriter& commandResults, Display& display,
181                                               Layer* layer, const ParcelableDataspace& dataspace);
182     void executeLayerCommandSetLayerDisplayFrame(CommandResultWriter& commandResults,
183                                                  Display& display, Layer* layer,
184                                                  const common::Rect& rect);
185     void executeLayerCommandSetLayerPlaneAlpha(CommandResultWriter& commandResults,
186                                                Display& display, Layer* layer,
187                                                const PlaneAlpha& planeAlpha);
188     void executeLayerCommandSetLayerSidebandStream(
189         CommandResultWriter& commandResults, Display& display, Layer* layer,
190         const aidl::android::hardware::common::NativeHandle& sidebandStream);
191     void executeLayerCommandSetLayerSourceCrop(CommandResultWriter& commandResults,
192                                                Display& display, Layer* layer,
193                                                const common::FRect& sourceCrop);
194     void executeLayerCommandSetLayerTransform(CommandResultWriter& commandResults, Display& display,
195                                               Layer* layer, const ParcelableTransform& transform);
196     void executeLayerCommandSetLayerVisibleRegion(
197         CommandResultWriter& commandResults, Display& display, Layer* layer,
198         const std::vector<std::optional<common::Rect>>& visibleRegion);
199     void executeLayerCommandSetLayerZOrder(CommandResultWriter& commandResults, Display& display,
200                                            Layer* layer, const ZOrder& zOrder);
201     void executeLayerCommandSetLayerPerFrameMetadata(
202         CommandResultWriter& commandResults, Display& display, Layer* layer,
203         const std::vector<std::optional<PerFrameMetadata>>& perFrameMetadata);
204     void executeLayerCommandSetLayerColorTransform(CommandResultWriter& commandResults,
205                                                    Display& display, Layer* layer,
206                                                    const std::vector<float>& colorTransform);
207     void executeLayerCommandSetLayerBrightness(CommandResultWriter& commandResults,
208                                                Display& display, Layer* layer,
209                                                const LayerBrightness& brightness);
210     void executeLayerCommandSetLayerPerFrameMetadataBlobs(
211         CommandResultWriter& commandResults, Display& display, Layer* layer,
212         const std::vector<std::optional<PerFrameMetadataBlob>>& perFrameMetadataBlob);
213     void executeLayerCommandSetLayerLuts(
214         CommandResultWriter& commandResults, Display& display, Layer* layer,
215         const Luts& luts);
216 
217     // Returns the display with the given id or nullptr if not found.
218     std::shared_ptr<Display> getDisplay(int64_t displayId);
219 
220     // Finds the Cuttlefish/Goldfish specific configuration and initializes the
221     // displays.
222     HWC3::Error createDisplaysLocked() EXCLUSIVE_LOCKS_REQUIRED(mDisplaysMutex);
223 
224     // Creates a display with the given properties.
225     HWC3::Error createDisplayLocked(int64_t displayId, int32_t activeConfigId,
226                                     const std::vector<DisplayConfig>& configs)
227         EXCLUSIVE_LOCKS_REQUIRED(mDisplaysMutex);
228 
229     HWC3::Error destroyDisplaysLocked() EXCLUSIVE_LOCKS_REQUIRED(mDisplaysMutex);
230 
231     HWC3::Error destroyDisplayLocked(int64_t displayId) EXCLUSIVE_LOCKS_REQUIRED(mDisplaysMutex);
232 
233     HWC3::Error handleHotplug(bool connected,   //
234                               uint32_t id,      //
235                               uint32_t width,   //
236                               uint32_t height,  //
237                               uint32_t dpiX,    //
238                               uint32_t dpiY,    //
239                               uint32_t refreshRate);
240 
241     std::mutex mDisplaysMutex;
242     std::map<int64_t, std::shared_ptr<Display>> mDisplays GUARDED_BY(mDisplaysMutex);
243 
244     // The onHotplug(), onVsync(), etc callbacks registered by SurfaceFlinger.
245     std::shared_ptr<IComposerCallback> mCallbacks;
246 
247     std::function<void()> mOnClientDestroyed;
248 
249     // Underlying interface for composing layers in the guest using libyuv or in
250     // the host using opengl. Owned by Device.
251     FrameComposer* mComposer = nullptr;
252 
253     // Manages importing and caching gralloc buffers for displays and layers.
254     std::unique_ptr<ComposerResources> mResources;
255 };
256 
257 }  // namespace aidl::android::hardware::graphics::composer3::impl
258 
259 #endif
260