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