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