• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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_DISPLAY_H
18 #define ANDROID_HWC_DISPLAY_H
19 
20 #include <utils/Thread.h>
21 
22 #include <optional>
23 #include <set>
24 #include <thread>
25 #include <unordered_map>
26 #include <vector>
27 
28 #include "Common.h"
29 #include "Composer.h"
30 #include "FencedBuffer.h"
31 #include "Layer.h"
32 
33 namespace android {
34 
35 class Composer;
36 class Device;
37 
38 class Display {
39  public:
40   Display(Device& device, Composer* composer, hwc2_display_t id);
41   ~Display();
42 
43   Display(const Display& display) = delete;
44   Display& operator=(const Display& display) = delete;
45 
46   Display(Display&& display) = delete;
47   Display& operator=(Display&& display) = delete;
48 
49   HWC2::Error init(uint32_t width, uint32_t height, uint32_t dpiX,
50                    uint32_t dpiY, uint32_t refreshRateHz,
51                    const std::optional<std::vector<uint8_t>>& edid = std::nullopt);
52 
53   HWC2::Error updateParameters(uint32_t width, uint32_t height, uint32_t dpiX,
54                                uint32_t dpiY, uint32_t refreshRateHz,
55                                const std::optional<std::vector<uint8_t>>& edid
56                                    = std::nullopt);
57 
getId()58   hwc2_display_t getId() const { return mId; }
59 
60   Layer* getLayer(hwc2_layer_t layerHandle);
61 
getClientTarget()62   FencedBuffer& getClientTarget() { return mClientTarget; }
63   buffer_handle_t waitAndGetClientTargetBuffer();
64 
getOrderedLayers()65   const std::vector<Layer*>& getOrderedLayers() { return mOrderedLayers; }
66 
67   HWC2::Error acceptChanges();
68   HWC2::Error createLayer(hwc2_layer_t* outLayerId);
69   HWC2::Error destroyLayer(hwc2_layer_t layerId);
70   HWC2::Error getActiveConfig(hwc2_config_t* outConfigId);
71   HWC2::Error getDisplayAttribute(hwc2_config_t configId, int32_t attribute,
72                                   int32_t* outValue);
73   HWC2::Error getDisplayAttributeEnum(hwc2_config_t configId,
74                                       HWC2::Attribute attribute,
75                                       int32_t* outValue);
76   HWC2::Error getChangedCompositionTypes(uint32_t* outNumElements,
77                                          hwc2_layer_t* outLayers,
78                                          int32_t* outTypes);
79   HWC2::Error getColorModes(uint32_t* outNumModes, int32_t* outModes);
80   HWC2::Error getConfigs(uint32_t* outNumConfigs, hwc2_config_t* outConfigIds);
81   HWC2::Error getDozeSupport(int32_t* outSupport);
82   HWC2::Error getHdrCapabilities(uint32_t* outNumTypes, int32_t* outTypes,
83                                  float* outMaxLuminance,
84                                  float* outMaxAverageLuminance,
85                                  float* outMinLuminance);
86   HWC2::Error getName(uint32_t* outSize, char* outName);
87   HWC2::Error addReleaseFenceLocked(int32_t fence);
88   HWC2::Error addReleaseLayerLocked(hwc2_layer_t layerId);
89   HWC2::Error getReleaseFences(uint32_t* outNumElements,
90                                hwc2_layer_t* outLayers, int32_t* outFences);
91   HWC2::Error clearReleaseFencesAndIdsLocked();
92   HWC2::Error getRequests(int32_t* outDisplayRequests, uint32_t* outNumElements,
93                           hwc2_layer_t* outLayers, int32_t* outLayerRequests);
94   HWC2::Error getType(int32_t* outType);
95   HWC2::Error present(int32_t* outRetireFence);
96   HWC2::Error setActiveConfig(hwc2_config_t configId);
97   HWC2::Error setClientTarget(buffer_handle_t target, int32_t acquireFence,
98                               int32_t dataspace, hwc_region_t damage);
99   HWC2::Error setColorMode(int32_t mode);
100   HWC2::Error setColorTransform(const float* matrix, int32_t hint);
hasColorTransform()101   bool hasColorTransform() const { return mSetColorTransform; }
102   HWC2::Error setOutputBuffer(buffer_handle_t buffer, int32_t releaseFence);
103   HWC2::Error setPowerMode(int32_t mode);
104   HWC2::Error setVsyncEnabled(int32_t enabled);
105   HWC2::Error setVsyncPeriod(uint32_t period);
106   HWC2::Error validate(uint32_t* outNumTypes, uint32_t* outNumRequests);
107   HWC2::Error updateLayerZ(hwc2_layer_t layerId, uint32_t z);
108   HWC2::Error getClientTargetSupport(uint32_t width, uint32_t height,
109                                      int32_t format, int32_t dataspace);
110   HWC2::Error getDisplayIdentificationData(uint8_t* outPort,
111                                            uint32_t* outDataSize,
112                                            uint8_t* outData);
113   HWC2::Error getDisplayCapabilities(uint32_t* outNumCapabilities,
114                                      uint32_t* outCapabilities);
115   HWC2::Error getDisplayBrightnessSupport(bool* out_support);
116   HWC2::Error setDisplayBrightness(float brightness);
lock()117   void lock() { mStateMutex.lock(); }
unlock()118   void unlock() { mStateMutex.unlock(); }
119 
120  private:
121   class Config {
122    public:
Config(hwc2_config_t configId)123     Config(hwc2_config_t configId) : mId(configId) {}
124 
125     Config(const Config& display) = default;
126     Config& operator=(const Config& display) = default;
127 
128     Config(Config&& display) = default;
129     Config& operator=(Config&& display) = default;
130 
getId()131     hwc2_config_t getId() const { return mId; }
setId(hwc2_config_t id)132     void setId(hwc2_config_t id) { mId = id; }
133 
134     int32_t getAttribute(HWC2::Attribute attribute) const;
135     void setAttribute(HWC2::Attribute attribute, int32_t value);
136 
137     std::string toString() const;
138 
139    private:
140     hwc2_config_t mId;
141     std::unordered_map<HWC2::Attribute, int32_t> mAttributes;
142   };
143 
144   // Stores changes requested from the device upon calling prepare().
145   // Handles change request to:
146   //   - Layer composition type.
147   //   - Layer hints.
148   class Changes {
149    public:
getNumTypes()150     uint32_t getNumTypes() const {
151       return static_cast<uint32_t>(mTypeChanges.size());
152     }
153 
getNumLayerRequests()154     uint32_t getNumLayerRequests() const {
155       return static_cast<uint32_t>(mLayerRequests.size());
156     }
157 
getTypeChanges()158     const std::unordered_map<hwc2_layer_t, HWC2::Composition>& getTypeChanges()
159         const {
160       return mTypeChanges;
161     }
162 
163     const std::unordered_map<hwc2_layer_t, HWC2::LayerRequest>&
getLayerRequests()164     getLayerRequests() const {
165       return mLayerRequests;
166     }
167 
addTypeChange(hwc2_layer_t layerId,HWC2::Composition type)168     void addTypeChange(hwc2_layer_t layerId, HWC2::Composition type) {
169       mTypeChanges.insert({layerId, type});
170     }
171 
clearTypeChanges()172     void clearTypeChanges() { mTypeChanges.clear(); }
173 
addLayerRequest(hwc2_layer_t layerId,HWC2::LayerRequest request)174     void addLayerRequest(hwc2_layer_t layerId, HWC2::LayerRequest request) {
175       mLayerRequests.insert({layerId, request});
176     }
177 
178    private:
179     std::unordered_map<hwc2_layer_t, HWC2::Composition> mTypeChanges;
180     std::unordered_map<hwc2_layer_t, HWC2::LayerRequest> mLayerRequests;
181   };
182 
183   // Generate sw vsync signal
184   class VsyncThread : public Thread {
185    public:
VsyncThread(Display & display)186     VsyncThread(Display& display) : mDisplay(display) {}
~VsyncThread()187     virtual ~VsyncThread() {}
188 
189     VsyncThread(const VsyncThread&) = default;
190     VsyncThread& operator=(const VsyncThread&) = default;
191 
192     VsyncThread(VsyncThread&&) = default;
193     VsyncThread& operator=(VsyncThread&&) = default;
194 
195    private:
196     Display& mDisplay;
197     bool threadLoop() final;
198   };
199 
200  private:
201   // The state of this display should only be modified from
202   // SurfaceFlinger's main loop, with the exception of when dump is
203   // called. To prevent a bad state from crashing us during a dump
204   // call, all public calls into Display must acquire this mutex.
205   mutable std::recursive_mutex mStateMutex;
206 
207   Device& mDevice;
208   Composer* mComposer = nullptr;
209   const hwc2_display_t mId;
210   std::string mName;
211   HWC2::DisplayType mType = HWC2::DisplayType::Physical;
212   HWC2::PowerMode mPowerMode = HWC2::PowerMode::Off;
213   HWC2::Vsync mVsyncEnabled = HWC2::Vsync::Invalid;
214   uint32_t mVsyncPeriod;
215   sp<VsyncThread> mVsyncThread;
216   FencedBuffer mClientTarget;
217   // Will only be non-null after the Display has been validated and
218   // before it has been presented
219   std::unique_ptr<Changes> mChanges;
220 
221   std::unordered_map<hwc2_layer_t, std::unique_ptr<Layer>> mLayers;
222   // Ordered layers available after validate().
223   std::vector<Layer*> mOrderedLayers;
224 
225   std::vector<hwc2_display_t> mReleaseLayerIds;
226   std::vector<int32_t> mReleaseFences;
227   std::optional<hwc2_config_t> mActiveConfigId;
228   std::unordered_map<hwc2_config_t, Config> mConfigs;
229   std::set<android_color_mode_t> mColorModes;
230   android_color_mode_t mActiveColorMode;
231   bool mSetColorTransform = false;
232   std::optional<std::vector<uint8_t>> mEdid;
233 };
234 
235 }  // namespace android
236 
237 #endif
238