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