1 /* 2 * Copyright (C) 2010 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_SF_HWCOMPOSER_H 18 #define ANDROID_SF_HWCOMPOSER_H 19 20 #include "HWC2.h" 21 22 #include <stdint.h> 23 #include <sys/types.h> 24 25 #include <ui/Fence.h> 26 #include <ui/GraphicTypes.h> 27 #include <utils/BitSet.h> 28 #include <utils/Condition.h> 29 #include <utils/Mutex.h> 30 #include <utils/StrongPointer.h> 31 #include <utils/Thread.h> 32 #include <utils/Timers.h> 33 #include <utils/Vector.h> 34 35 #include <memory> 36 #include <optional> 37 #include <set> 38 #include <vector> 39 40 extern "C" int clock_nanosleep(clockid_t clock_id, int flags, 41 const struct timespec *request, 42 struct timespec *remain); 43 44 struct framebuffer_device_t; 45 46 namespace HWC2 { 47 class Device; 48 class Display; 49 } 50 51 namespace android { 52 // --------------------------------------------------------------------------- 53 54 class DisplayDevice; 55 class Fence; 56 class FloatRect; 57 class GraphicBuffer; 58 class NativeHandle; 59 class Region; 60 class String8; 61 class TestableSurfaceFlinger; 62 63 namespace Hwc2 { 64 class Composer; 65 } // namespace Hwc2 66 67 class HWComposer 68 { 69 public: 70 explicit HWComposer(std::unique_ptr<android::Hwc2::Composer> composer); 71 72 ~HWComposer(); 73 74 void registerCallback(HWC2::ComposerCallback* callback, 75 int32_t sequenceId); 76 77 bool hasCapability(HWC2::Capability capability) const; 78 79 // Attempts to allocate a virtual display. If the virtual display is created 80 // on the HWC device, outId will contain its HWC ID. 81 status_t allocateVirtualDisplay(uint32_t width, uint32_t height, 82 ui::PixelFormat* format, int32_t* outId); 83 84 // Attempts to create a new layer on this display 85 HWC2::Layer* createLayer(int32_t displayId); 86 // Destroy a previously created layer 87 void destroyLayer(int32_t displayId, HWC2::Layer* layer); 88 89 // Asks the HAL what it can do 90 status_t prepare(DisplayDevice& displayDevice); 91 92 status_t setClientTarget(int32_t displayId, uint32_t slot, 93 const sp<Fence>& acquireFence, 94 const sp<GraphicBuffer>& target, ui::Dataspace dataspace); 95 96 // Present layers to the display and read releaseFences. 97 status_t presentAndGetReleaseFences(int32_t displayId); 98 99 // set power mode 100 status_t setPowerMode(int32_t displayId, int mode); 101 102 // set active config 103 status_t setActiveConfig(int32_t displayId, size_t configId); 104 105 // Sets a color transform to be applied to the result of composition 106 status_t setColorTransform(int32_t displayId, const mat4& transform); 107 108 // reset state when an external, non-virtual display is disconnected 109 void disconnectDisplay(int32_t displayId); 110 111 // does this display have layers handled by HWC 112 bool hasDeviceComposition(int32_t displayId) const; 113 114 // does this display have pending request to flip client target 115 bool hasFlipClientTargetRequest(int32_t displayId) const; 116 117 // does this display have layers handled by GLES 118 bool hasClientComposition(int32_t displayId) const; 119 120 // get the present fence received from the last call to present. 121 sp<Fence> getPresentFence(int32_t displayId) const; 122 123 // Get last release fence for the given layer 124 sp<Fence> getLayerReleaseFence(int32_t displayId, 125 HWC2::Layer* layer) const; 126 127 // Set the output buffer and acquire fence for a virtual display. 128 // Returns INVALID_OPERATION if displayId is not a virtual display. 129 status_t setOutputBuffer(int32_t displayId, const sp<Fence>& acquireFence, 130 const sp<GraphicBuffer>& buf); 131 132 // After SurfaceFlinger has retrieved the release fences for all the frames, 133 // it can call this to clear the shared pointers in the release fence map 134 void clearReleaseFences(int32_t displayId); 135 136 // Fetches the HDR capabilities of the given display 137 status_t getHdrCapabilities(int32_t displayId, HdrCapabilities* outCapabilities); 138 139 int32_t getSupportedPerFrameMetadata(int32_t displayId) const; 140 141 // Returns the available RenderIntent of the given display. 142 std::vector<ui::RenderIntent> getRenderIntents(int32_t displayId, ui::ColorMode colorMode) const; 143 144 mat4 getDataspaceSaturationMatrix(int32_t displayId, ui::Dataspace dataspace); 145 146 // Events handling --------------------------------------------------------- 147 148 // Returns true if successful, false otherwise. The 149 // DisplayDevice::DisplayType of the display is returned as an output param. 150 bool onVsync(hwc2_display_t displayId, int64_t timestamp, 151 int32_t* outDisplay); 152 void onHotplug(hwc2_display_t displayId, int32_t displayType, HWC2::Connection connection); 153 154 void setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled); 155 156 // Query display parameters. Pass in a display index (e.g. 157 // HWC_DISPLAY_PRIMARY). 158 nsecs_t getRefreshTimestamp(int32_t displayId) const; 159 bool isConnected(int32_t displayId) const; 160 161 // Non-const because it can update configMap inside of mDisplayData 162 std::vector<std::shared_ptr<const HWC2::Display::Config>> 163 getConfigs(int32_t displayId) const; 164 165 std::shared_ptr<const HWC2::Display::Config> 166 getActiveConfig(int32_t displayId) const; 167 int getActiveConfigIndex(int32_t displayId) const; 168 169 std::vector<ui::ColorMode> getColorModes(int32_t displayId) const; 170 171 status_t setActiveColorMode(int32_t displayId, ui::ColorMode mode, 172 ui::RenderIntent renderIntent); 173 174 bool isUsingVrComposer() const; 175 176 // for debugging ---------------------------------------------------------- 177 void dump(String8& out) const; 178 getComposer()179 android::Hwc2::Composer* getComposer() const { return mHwcDevice->getComposer(); } 180 181 std::optional<hwc2_display_t> getHwcDisplayId(int32_t displayId) const; 182 private: 183 // For unit tests 184 friend TestableSurfaceFlinger; 185 186 static const int32_t VIRTUAL_DISPLAY_ID_BASE = 2; 187 188 bool isValidDisplay(int32_t displayId) const; 189 static void validateChange(HWC2::Composition from, HWC2::Composition to); 190 191 struct cb_context; 192 193 struct DisplayData { 194 DisplayData(); 195 ~DisplayData(); 196 void reset(); 197 198 bool hasClientComposition; 199 bool hasDeviceComposition; 200 HWC2::Display* hwcDisplay; 201 HWC2::DisplayRequest displayRequests; 202 sp<Fence> lastPresentFence; // signals when the last set op retires 203 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences; 204 buffer_handle_t outbufHandle; 205 sp<Fence> outbufAcquireFence; 206 mutable std::unordered_map<int32_t, 207 std::shared_ptr<const HWC2::Display::Config>> configMap; 208 209 // protected by mVsyncLock 210 HWC2::Vsync vsyncEnabled; 211 212 bool validateWasSkipped; 213 HWC2::Error presentError; 214 }; 215 216 std::unique_ptr<HWC2::Device> mHwcDevice; 217 std::vector<DisplayData> mDisplayData{HWC_NUM_PHYSICAL_DISPLAY_TYPES}; 218 std::set<size_t> mFreeDisplaySlots; 219 std::unordered_map<hwc2_display_t, int32_t> mHwcDisplaySlots; 220 // protect mDisplayData from races between prepare and dump 221 mutable Mutex mDisplayLock; 222 223 cb_context* mCBContext = nullptr; 224 size_t mVSyncCounts[HWC_NUM_PHYSICAL_DISPLAY_TYPES]{0, 0}; 225 uint32_t mRemainingHwcVirtualDisplays{mHwcDevice->getMaxVirtualDisplayCount()}; 226 227 // protected by mLock 228 mutable Mutex mLock; 229 mutable std::unordered_map<int32_t, nsecs_t> mLastHwVSync{ 230 {{HWC_DISPLAY_PRIMARY, 0}, {HWC_DISPLAY_EXTERNAL, 0}}}; 231 232 // thread-safe 233 mutable Mutex mVsyncLock; 234 }; 235 236 // --------------------------------------------------------------------------- 237 }; // namespace android 238 239 #endif // ANDROID_SF_HWCOMPOSER_H 240