Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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_HWC2_H |
| 18 | #define ANDROID_SF_HWC2_H |
| 19 | |
| 20 | #define HWC2_INCLUDE_STRINGIFICATION |
| 21 | #define HWC2_USE_CPP11 |
| 22 | #include <hardware/hwcomposer2.h> |
| 23 | #undef HWC2_INCLUDE_STRINGIFICATION |
| 24 | #undef HWC2_USE_CPP11 |
| 25 | |
Dan Stoza | 7d7ae73 | 2016-03-16 12:23:40 -0700 | [diff] [blame] | 26 | #include <ui/HdrCapabilities.h> |
Mathias Agopian | 1d77b71 | 2017-02-17 15:46:13 -0800 | [diff] [blame] | 27 | #include <math/mat4.h> |
Dan Stoza | 7d7ae73 | 2016-03-16 12:23:40 -0700 | [diff] [blame] | 28 | |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 29 | #include <utils/Log.h> |
| 30 | #include <utils/StrongPointer.h> |
| 31 | #include <utils/Timers.h> |
| 32 | |
| 33 | #include <functional> |
| 34 | #include <string> |
| 35 | #include <unordered_map> |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 36 | #include <unordered_set> |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 37 | #include <vector> |
Alistair Strachan | c175253 | 2017-06-07 16:34:44 -0700 | [diff] [blame] | 38 | #include <map> |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 39 | |
| 40 | namespace android { |
| 41 | class Fence; |
Dan Stoza | 5a423ea | 2017-02-16 14:10:39 -0800 | [diff] [blame] | 42 | class FloatRect; |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 43 | class GraphicBuffer; |
| 44 | class Rect; |
| 45 | class Region; |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 46 | namespace Hwc2 { |
| 47 | class Composer; |
Dan Stoza | 71bded5 | 2016-10-19 11:10:33 -0700 | [diff] [blame] | 48 | } |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | namespace HWC2 { |
| 52 | |
| 53 | class Display; |
| 54 | class Layer; |
| 55 | |
| 56 | typedef std::function<void(std::shared_ptr<Display>, Connection)> |
| 57 | HotplugCallback; |
| 58 | typedef std::function<void(std::shared_ptr<Display>)> RefreshCallback; |
| 59 | typedef std::function<void(std::shared_ptr<Display>, nsecs_t)> VsyncCallback; |
| 60 | |
Fabien Sanglard | 3396070 | 2016-11-29 17:58:21 -0800 | [diff] [blame] | 61 | // C++ Wrapper around hwc2_device_t. Load all functions pointers |
| 62 | // and handle callback registration. |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 63 | class Device |
| 64 | { |
| 65 | public: |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 66 | // useVrComposer is passed to the composer HAL. When true, the composer HAL |
| 67 | // will use the vr composer service, otherwise it uses the real hardware |
| 68 | // composer. |
| 69 | Device(bool useVrComposer); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 70 | ~Device(); |
| 71 | |
| 72 | friend class HWC2::Display; |
| 73 | friend class HWC2::Layer; |
| 74 | |
| 75 | // Required by HWC2 |
| 76 | |
| 77 | std::string dump() const; |
| 78 | |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 79 | const std::unordered_set<Capability>& getCapabilities() const { |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 80 | return mCapabilities; |
| 81 | }; |
| 82 | |
| 83 | uint32_t getMaxVirtualDisplayCount() const; |
| 84 | Error createVirtualDisplay(uint32_t width, uint32_t height, |
Dan Stoza | 5cf424b | 2016-05-20 14:02:39 -0700 | [diff] [blame] | 85 | android_pixel_format_t* format, |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 86 | std::shared_ptr<Display>* outDisplay); |
| 87 | |
| 88 | void registerHotplugCallback(HotplugCallback hotplug); |
| 89 | void registerRefreshCallback(RefreshCallback refresh); |
| 90 | void registerVsyncCallback(VsyncCallback vsync); |
| 91 | |
| 92 | // For use by callbacks |
| 93 | |
| 94 | void callHotplug(std::shared_ptr<Display> display, Connection connected); |
| 95 | void callRefresh(std::shared_ptr<Display> display); |
| 96 | void callVsync(std::shared_ptr<Display> display, nsecs_t timestamp); |
| 97 | |
| 98 | // Other Device methods |
| 99 | |
| 100 | // This will create a Display if one is not found, but it will not be marked |
Dan Stoza | 3862898 | 2016-07-13 15:48:58 -0700 | [diff] [blame] | 101 | // as connected. This Display may be null if the display has been torn down |
| 102 | // but has not been removed from the map yet. |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 103 | std::shared_ptr<Display> getDisplayById(hwc2_display_t id); |
| 104 | |
Dan Stoza | 09e7a27 | 2016-04-14 12:31:01 -0700 | [diff] [blame] | 105 | bool hasCapability(HWC2::Capability capability) const; |
| 106 | |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 107 | android::Hwc2::Composer* getComposer() { return mComposer.get(); } |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 108 | |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 109 | private: |
| 110 | // Initialization methods |
| 111 | |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 112 | void loadCapabilities(); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 113 | void registerCallbacks(); |
| 114 | |
| 115 | // For use by Display |
| 116 | |
| 117 | void destroyVirtualDisplay(hwc2_display_t display); |
| 118 | |
| 119 | // Member variables |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 120 | std::unique_ptr<android::Hwc2::Composer> mComposer; |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 121 | |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 122 | std::unordered_set<Capability> mCapabilities; |
Dan Stoza | 3862898 | 2016-07-13 15:48:58 -0700 | [diff] [blame] | 123 | std::unordered_map<hwc2_display_t, std::weak_ptr<Display>> mDisplays; |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 124 | |
| 125 | HotplugCallback mHotplug; |
| 126 | std::vector<std::pair<std::shared_ptr<Display>, Connection>> |
| 127 | mPendingHotplugs; |
| 128 | RefreshCallback mRefresh; |
| 129 | std::vector<std::shared_ptr<Display>> mPendingRefreshes; |
| 130 | VsyncCallback mVsync; |
| 131 | std::vector<std::pair<std::shared_ptr<Display>, nsecs_t>> mPendingVsyncs; |
| 132 | }; |
| 133 | |
Fabien Sanglard | 3396070 | 2016-11-29 17:58:21 -0800 | [diff] [blame] | 134 | // Convenience C++ class to access hwc2_device_t Display functions directly. |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 135 | class Display : public std::enable_shared_from_this<Display> |
| 136 | { |
| 137 | public: |
| 138 | Display(Device& device, hwc2_display_t id); |
| 139 | ~Display(); |
| 140 | |
| 141 | friend class HWC2::Device; |
| 142 | friend class HWC2::Layer; |
| 143 | |
| 144 | class Config |
| 145 | { |
| 146 | public: |
| 147 | class Builder |
| 148 | { |
| 149 | public: |
| 150 | Builder(Display& display, hwc2_config_t id); |
| 151 | |
| 152 | std::shared_ptr<const Config> build() { |
| 153 | return std::const_pointer_cast<const Config>( |
| 154 | std::move(mConfig)); |
| 155 | } |
| 156 | |
| 157 | Builder& setWidth(int32_t width) { |
| 158 | mConfig->mWidth = width; |
| 159 | return *this; |
| 160 | } |
| 161 | Builder& setHeight(int32_t height) { |
| 162 | mConfig->mHeight = height; |
| 163 | return *this; |
| 164 | } |
| 165 | Builder& setVsyncPeriod(int32_t vsyncPeriod) { |
| 166 | mConfig->mVsyncPeriod = vsyncPeriod; |
| 167 | return *this; |
| 168 | } |
| 169 | Builder& setDpiX(int32_t dpiX) { |
| 170 | if (dpiX == -1) { |
| 171 | mConfig->mDpiX = getDefaultDensity(); |
| 172 | } else { |
| 173 | mConfig->mDpiX = dpiX / 1000.0f; |
| 174 | } |
| 175 | return *this; |
| 176 | } |
| 177 | Builder& setDpiY(int32_t dpiY) { |
| 178 | if (dpiY == -1) { |
| 179 | mConfig->mDpiY = getDefaultDensity(); |
| 180 | } else { |
| 181 | mConfig->mDpiY = dpiY / 1000.0f; |
| 182 | } |
| 183 | return *this; |
| 184 | } |
| 185 | |
| 186 | private: |
| 187 | float getDefaultDensity(); |
| 188 | std::shared_ptr<Config> mConfig; |
| 189 | }; |
| 190 | |
| 191 | hwc2_display_t getDisplayId() const { return mDisplay.getId(); } |
| 192 | hwc2_config_t getId() const { return mId; } |
| 193 | |
| 194 | int32_t getWidth() const { return mWidth; } |
| 195 | int32_t getHeight() const { return mHeight; } |
| 196 | nsecs_t getVsyncPeriod() const { return mVsyncPeriod; } |
| 197 | float getDpiX() const { return mDpiX; } |
| 198 | float getDpiY() const { return mDpiY; } |
| 199 | |
| 200 | private: |
| 201 | Config(Display& display, hwc2_config_t id); |
| 202 | |
| 203 | Display& mDisplay; |
| 204 | hwc2_config_t mId; |
| 205 | |
| 206 | int32_t mWidth; |
| 207 | int32_t mHeight; |
| 208 | nsecs_t mVsyncPeriod; |
| 209 | float mDpiX; |
| 210 | float mDpiY; |
| 211 | }; |
| 212 | |
| 213 | // Required by HWC2 |
| 214 | |
| 215 | [[clang::warn_unused_result]] Error acceptChanges(); |
| 216 | [[clang::warn_unused_result]] Error createLayer( |
| 217 | std::shared_ptr<Layer>* outLayer); |
| 218 | [[clang::warn_unused_result]] Error getActiveConfig( |
| 219 | std::shared_ptr<const Config>* outConfig) const; |
| 220 | [[clang::warn_unused_result]] Error getChangedCompositionTypes( |
| 221 | std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes); |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 222 | [[clang::warn_unused_result]] Error getColorModes( |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 223 | std::vector<android_color_mode_t>* outModes) const; |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 224 | |
| 225 | // Doesn't call into the HWC2 device, so no errors are possible |
| 226 | std::vector<std::shared_ptr<const Config>> getConfigs() const; |
| 227 | |
| 228 | [[clang::warn_unused_result]] Error getName(std::string* outName) const; |
| 229 | [[clang::warn_unused_result]] Error getRequests( |
| 230 | DisplayRequest* outDisplayRequests, |
| 231 | std::unordered_map<std::shared_ptr<Layer>, LayerRequest>* |
| 232 | outLayerRequests); |
| 233 | [[clang::warn_unused_result]] Error getType(DisplayType* outType) const; |
| 234 | [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const; |
Dan Stoza | 7d7ae73 | 2016-03-16 12:23:40 -0700 | [diff] [blame] | 235 | [[clang::warn_unused_result]] Error getHdrCapabilities( |
| 236 | std::unique_ptr<android::HdrCapabilities>* outCapabilities) const; |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 237 | [[clang::warn_unused_result]] Error getReleaseFences( |
| 238 | std::unordered_map<std::shared_ptr<Layer>, |
| 239 | android::sp<android::Fence>>* outFences) const; |
| 240 | [[clang::warn_unused_result]] Error present( |
Fabien Sanglard | 11d0fc3 | 2016-12-01 15:43:01 -0800 | [diff] [blame] | 241 | android::sp<android::Fence>* outPresentFence); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 242 | [[clang::warn_unused_result]] Error setActiveConfig( |
| 243 | const std::shared_ptr<const Config>& config); |
| 244 | [[clang::warn_unused_result]] Error setClientTarget( |
Daniel Nicoara | 1f42e3a | 2017-04-10 13:27:32 -0400 | [diff] [blame] | 245 | uint32_t slot, const android::sp<android::GraphicBuffer>& target, |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 246 | const android::sp<android::Fence>& acquireFence, |
| 247 | android_dataspace_t dataspace); |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 248 | [[clang::warn_unused_result]] Error setColorMode(android_color_mode_t mode); |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame] | 249 | [[clang::warn_unused_result]] Error setColorTransform( |
| 250 | const android::mat4& matrix, android_color_transform_t hint); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 251 | [[clang::warn_unused_result]] Error setOutputBuffer( |
| 252 | const android::sp<android::GraphicBuffer>& buffer, |
| 253 | const android::sp<android::Fence>& releaseFence); |
| 254 | [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode); |
| 255 | [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled); |
| 256 | [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes, |
| 257 | uint32_t* outNumRequests); |
Fabien Sanglard | 249c0ae | 2017-06-19 19:22:36 -0700 | [diff] [blame^] | 258 | [[clang::warn_unused_result]] Error presentOrValidate(uint32_t* outNumTypes, |
| 259 | uint32_t* outNumRequests, |
| 260 | android::sp<android::Fence>* outPresentFence, uint32_t* state); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 261 | |
| 262 | // Other Display methods |
| 263 | |
| 264 | Device& getDevice() const { return mDevice; } |
| 265 | hwc2_display_t getId() const { return mId; } |
| 266 | bool isConnected() const { return mIsConnected; } |
| 267 | |
| 268 | private: |
| 269 | // For use by Device |
| 270 | |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 271 | void setConnected(bool connected) { mIsConnected = connected; } |
| 272 | int32_t getAttribute(hwc2_config_t configId, Attribute attribute); |
| 273 | void loadConfig(hwc2_config_t configId); |
| 274 | void loadConfigs(); |
| 275 | |
| 276 | // For use by Layer |
| 277 | void destroyLayer(hwc2_layer_t layerId); |
| 278 | |
| 279 | // This may fail (and return a null pointer) if no layer with this ID exists |
| 280 | // on this display |
| 281 | std::shared_ptr<Layer> getLayerById(hwc2_layer_t id) const; |
| 282 | |
| 283 | // Member variables |
| 284 | |
| 285 | Device& mDevice; |
| 286 | hwc2_display_t mId; |
| 287 | bool mIsConnected; |
Chris Forbes | 016d73c | 2017-04-11 10:04:31 -0700 | [diff] [blame] | 288 | DisplayType mType; |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 289 | std::unordered_map<hwc2_layer_t, std::weak_ptr<Layer>> mLayers; |
Alistair Strachan | c175253 | 2017-06-07 16:34:44 -0700 | [diff] [blame] | 290 | // The ordering in this map matters, for getConfigs(), when it is |
| 291 | // converted to a vector |
| 292 | std::map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs; |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 293 | }; |
| 294 | |
Fabien Sanglard | 3396070 | 2016-11-29 17:58:21 -0800 | [diff] [blame] | 295 | // Convenience C++ class to access hwc2_device_t Layer functions directly. |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 296 | class Layer |
| 297 | { |
| 298 | public: |
| 299 | Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id); |
| 300 | ~Layer(); |
| 301 | |
| 302 | bool isAbandoned() const { return mDisplay.expired(); } |
| 303 | hwc2_layer_t getId() const { return mId; } |
| 304 | |
| 305 | [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y); |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 306 | [[clang::warn_unused_result]] Error setBuffer(uint32_t slot, |
Daniel Nicoara | 1f42e3a | 2017-04-10 13:27:32 -0400 | [diff] [blame] | 307 | const android::sp<android::GraphicBuffer>& buffer, |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 308 | const android::sp<android::Fence>& acquireFence); |
| 309 | [[clang::warn_unused_result]] Error setSurfaceDamage( |
| 310 | const android::Region& damage); |
| 311 | |
| 312 | [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode); |
| 313 | [[clang::warn_unused_result]] Error setColor(hwc_color_t color); |
| 314 | [[clang::warn_unused_result]] Error setCompositionType(Composition type); |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame] | 315 | [[clang::warn_unused_result]] Error setDataspace( |
| 316 | android_dataspace_t dataspace); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 317 | [[clang::warn_unused_result]] Error setDisplayFrame( |
| 318 | const android::Rect& frame); |
| 319 | [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha); |
| 320 | [[clang::warn_unused_result]] Error setSidebandStream( |
| 321 | const native_handle_t* stream); |
| 322 | [[clang::warn_unused_result]] Error setSourceCrop( |
Dan Stoza | 5a423ea | 2017-02-16 14:10:39 -0800 | [diff] [blame] | 323 | const android::FloatRect& crop); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 324 | [[clang::warn_unused_result]] Error setTransform(Transform transform); |
| 325 | [[clang::warn_unused_result]] Error setVisibleRegion( |
| 326 | const android::Region& region); |
| 327 | [[clang::warn_unused_result]] Error setZOrder(uint32_t z); |
Daniel Nicoara | 2f5f8a5 | 2016-12-20 16:11:58 -0500 | [diff] [blame] | 328 | [[clang::warn_unused_result]] Error setInfo(uint32_t type, uint32_t appId); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 329 | |
| 330 | private: |
| 331 | std::weak_ptr<Display> mDisplay; |
| 332 | hwc2_display_t mDisplayId; |
| 333 | Device& mDevice; |
| 334 | hwc2_layer_t mId; |
| 335 | }; |
| 336 | |
| 337 | } // namespace HWC2 |
| 338 | |
| 339 | #endif // ANDROID_SF_HWC2_H |