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