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