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 26 #include <gui/HdrMetadata.h> 27 #include <math/mat4.h> 28 #include <ui/GraphicTypes.h> 29 #include <ui/HdrCapabilities.h> 30 #include <ui/Region.h> 31 #include <utils/Log.h> 32 #include <utils/StrongPointer.h> 33 #include <utils/Timers.h> 34 35 #include <functional> 36 #include <string> 37 #include <unordered_map> 38 #include <unordered_set> 39 #include <vector> 40 41 namespace android { 42 struct DisplayedFrameStats; 43 class Fence; 44 class FloatRect; 45 class GraphicBuffer; 46 namespace Hwc2 { 47 class Composer; 48 } 49 50 class TestableSurfaceFlinger; 51 } 52 53 namespace HWC2 { 54 55 class Display; 56 class Layer; 57 58 // Implement this interface to receive hardware composer events. 59 // 60 // These callback functions will generally be called on a hwbinder thread, but 61 // when first registering the callback the onHotplugReceived() function will 62 // immediately be called on the thread calling registerCallback(). 63 // 64 // All calls receive a sequenceId, which will be the value that was supplied to 65 // HWC2::Device::registerCallback(). It's used to help differentiate callbacks 66 // from different hardware composer instances. 67 class ComposerCallback { 68 public: 69 virtual void onHotplugReceived(int32_t sequenceId, hwc2_display_t display, 70 Connection connection) = 0; 71 virtual void onRefreshReceived(int32_t sequenceId, 72 hwc2_display_t display) = 0; 73 virtual void onVsyncReceived(int32_t sequenceId, hwc2_display_t display, 74 int64_t timestamp) = 0; 75 virtual ~ComposerCallback() = default; 76 }; 77 78 // C++ Wrapper around hwc2_device_t. Load all functions pointers 79 // and handle callback registration. 80 class Device 81 { 82 public: 83 explicit Device(std::unique_ptr<android::Hwc2::Composer> composer); 84 85 void registerCallback(ComposerCallback* callback, int32_t sequenceId); 86 87 // Required by HWC2 88 89 std::string dump() const; 90 getCapabilities()91 const std::unordered_set<Capability>& getCapabilities() const { 92 return mCapabilities; 93 }; 94 95 uint32_t getMaxVirtualDisplayCount() const; 96 Error getDisplayIdentificationData(hwc2_display_t hwcDisplayId, uint8_t* outPort, 97 std::vector<uint8_t>* outData) const; 98 99 Error createVirtualDisplay(uint32_t width, uint32_t height, 100 android::ui::PixelFormat* format, Display** outDisplay); 101 void destroyDisplay(hwc2_display_t displayId); 102 103 void onHotplug(hwc2_display_t displayId, Connection connection); 104 105 // Other Device methods 106 107 Display* getDisplayById(hwc2_display_t id); 108 getComposer()109 android::Hwc2::Composer* getComposer() { return mComposer.get(); } 110 111 // We buffer most state changes and flush them implicitly with 112 // Display::validate, Display::present, and Display::presentOrValidate. 113 // This method provides an explicit way to flush state changes to HWC. 114 Error flushCommands(); 115 116 private: 117 // Initialization methods 118 119 void loadCapabilities(); 120 121 // Member variables 122 std::unique_ptr<android::Hwc2::Composer> mComposer; 123 std::unordered_set<Capability> mCapabilities; 124 std::unordered_map<hwc2_display_t, std::unique_ptr<Display>> mDisplays; 125 bool mRegisteredCallback = false; 126 }; 127 128 // Convenience C++ class to access hwc2_device_t Display functions directly. 129 class Display { 130 public: 131 virtual ~Display(); 132 133 class Config { 134 public: 135 class Builder 136 { 137 public: 138 Builder(Display& display, hwc2_config_t id); 139 build()140 std::shared_ptr<const Config> build() { 141 return std::const_pointer_cast<const Config>( 142 std::move(mConfig)); 143 } 144 setWidth(int32_t width)145 Builder& setWidth(int32_t width) { 146 mConfig->mWidth = width; 147 return *this; 148 } setHeight(int32_t height)149 Builder& setHeight(int32_t height) { 150 mConfig->mHeight = height; 151 return *this; 152 } setVsyncPeriod(int32_t vsyncPeriod)153 Builder& setVsyncPeriod(int32_t vsyncPeriod) { 154 mConfig->mVsyncPeriod = vsyncPeriod; 155 return *this; 156 } setDpiX(int32_t dpiX)157 Builder& setDpiX(int32_t dpiX) { 158 if (dpiX == -1) { 159 mConfig->mDpiX = getDefaultDensity(); 160 } else { 161 mConfig->mDpiX = dpiX / 1000.0f; 162 } 163 return *this; 164 } setDpiY(int32_t dpiY)165 Builder& setDpiY(int32_t dpiY) { 166 if (dpiY == -1) { 167 mConfig->mDpiY = getDefaultDensity(); 168 } else { 169 mConfig->mDpiY = dpiY / 1000.0f; 170 } 171 return *this; 172 } 173 174 private: 175 float getDefaultDensity(); 176 std::shared_ptr<Config> mConfig; 177 }; 178 getDisplayId()179 hwc2_display_t getDisplayId() const { return mDisplay.getId(); } getId()180 hwc2_config_t getId() const { return mId; } 181 getWidth()182 int32_t getWidth() const { return mWidth; } getHeight()183 int32_t getHeight() const { return mHeight; } getVsyncPeriod()184 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; } getDpiX()185 float getDpiX() const { return mDpiX; } getDpiY()186 float getDpiY() const { return mDpiY; } 187 188 private: 189 Config(Display& display, hwc2_config_t id); 190 191 Display& mDisplay; 192 hwc2_config_t mId; 193 194 int32_t mWidth; 195 int32_t mHeight; 196 nsecs_t mVsyncPeriod; 197 float mDpiX; 198 float mDpiY; 199 }; 200 201 virtual hwc2_display_t getId() const = 0; 202 virtual bool isConnected() const = 0; 203 virtual void setConnected(bool connected) = 0; // For use by Device only 204 virtual const std::unordered_set<DisplayCapability>& getCapabilities() const = 0; 205 206 [[clang::warn_unused_result]] virtual Error acceptChanges() = 0; 207 [[clang::warn_unused_result]] virtual Error createLayer(Layer** outLayer) = 0; 208 [[clang::warn_unused_result]] virtual Error destroyLayer(Layer* layer) = 0; 209 [[clang::warn_unused_result]] virtual Error getActiveConfig( 210 std::shared_ptr<const Config>* outConfig) const = 0; 211 [[clang::warn_unused_result]] virtual Error getActiveConfigIndex(int* outIndex) const = 0; 212 [[clang::warn_unused_result]] virtual Error getChangedCompositionTypes( 213 std::unordered_map<Layer*, Composition>* outTypes) = 0; 214 [[clang::warn_unused_result]] virtual Error getColorModes( 215 std::vector<android::ui::ColorMode>* outModes) const = 0; 216 // Returns a bitmask which contains HdrMetadata::Type::*. 217 [[clang::warn_unused_result]] virtual int32_t getSupportedPerFrameMetadata() const = 0; 218 [[clang::warn_unused_result]] virtual Error getRenderIntents( 219 android::ui::ColorMode colorMode, 220 std::vector<android::ui::RenderIntent>* outRenderIntents) const = 0; 221 [[clang::warn_unused_result]] virtual Error getDataspaceSaturationMatrix( 222 android::ui::Dataspace dataspace, android::mat4* outMatrix) = 0; 223 224 // Doesn't call into the HWC2 device, so no Errors are possible 225 virtual std::vector<std::shared_ptr<const Config>> getConfigs() const = 0; 226 227 [[clang::warn_unused_result]] virtual Error getName(std::string* outName) const = 0; 228 [[clang::warn_unused_result]] virtual Error getRequests( 229 DisplayRequest* outDisplayRequests, 230 std::unordered_map<Layer*, LayerRequest>* outLayerRequests) = 0; 231 [[clang::warn_unused_result]] virtual Error getType(DisplayType* outType) const = 0; 232 [[clang::warn_unused_result]] virtual Error supportsDoze(bool* outSupport) const = 0; 233 [[clang::warn_unused_result]] virtual Error getHdrCapabilities( 234 android::HdrCapabilities* outCapabilities) const = 0; 235 [[clang::warn_unused_result]] virtual Error getDisplayedContentSamplingAttributes( 236 android::ui::PixelFormat* outFormat, android::ui::Dataspace* outDataspace, 237 uint8_t* outComponentMask) const = 0; 238 [[clang::warn_unused_result]] virtual Error setDisplayContentSamplingEnabled( 239 bool enabled, uint8_t componentMask, uint64_t maxFrames) const = 0; 240 [[clang::warn_unused_result]] virtual Error getDisplayedContentSample( 241 uint64_t maxFrames, uint64_t timestamp, 242 android::DisplayedFrameStats* outStats) const = 0; 243 [[clang::warn_unused_result]] virtual Error getReleaseFences( 244 std::unordered_map<Layer*, android::sp<android::Fence>>* outFences) const = 0; 245 [[clang::warn_unused_result]] virtual Error present( 246 android::sp<android::Fence>* outPresentFence) = 0; 247 [[clang::warn_unused_result]] virtual Error setActiveConfig( 248 const std::shared_ptr<const Config>& config) = 0; 249 [[clang::warn_unused_result]] virtual Error setClientTarget( 250 uint32_t slot, const android::sp<android::GraphicBuffer>& target, 251 const android::sp<android::Fence>& acquireFence, android::ui::Dataspace dataspace) = 0; 252 [[clang::warn_unused_result]] virtual Error setColorMode( 253 android::ui::ColorMode mode, android::ui::RenderIntent renderIntent) = 0; 254 [[clang::warn_unused_result]] virtual Error setColorTransform( 255 const android::mat4& matrix, android_color_transform_t hint) = 0; 256 [[clang::warn_unused_result]] virtual Error setOutputBuffer( 257 const android::sp<android::GraphicBuffer>& buffer, 258 const android::sp<android::Fence>& releaseFence) = 0; 259 [[clang::warn_unused_result]] virtual Error setPowerMode(PowerMode mode) = 0; 260 [[clang::warn_unused_result]] virtual Error setVsyncEnabled(Vsync enabled) = 0; 261 [[clang::warn_unused_result]] virtual Error validate(uint32_t* outNumTypes, 262 uint32_t* outNumRequests) = 0; 263 [[clang::warn_unused_result]] virtual Error presentOrValidate( 264 uint32_t* outNumTypes, uint32_t* outNumRequests, 265 android::sp<android::Fence>* outPresentFence, uint32_t* state) = 0; 266 [[clang::warn_unused_result]] virtual Error setDisplayBrightness(float brightness) const = 0; 267 }; 268 269 namespace impl { 270 271 class Display : public HWC2::Display { 272 public: 273 Display(android::Hwc2::Composer& composer, const std::unordered_set<Capability>& capabilities, 274 hwc2_display_t id, DisplayType type); 275 ~Display() override; 276 277 // Required by HWC2 278 Error acceptChanges() override; 279 Error createLayer(Layer** outLayer) override; 280 Error destroyLayer(Layer* layer) override; 281 Error getActiveConfig(std::shared_ptr<const Config>* outConfig) const override; 282 Error getActiveConfigIndex(int* outIndex) const override; 283 Error getChangedCompositionTypes(std::unordered_map<Layer*, Composition>* outTypes) override; 284 Error getColorModes(std::vector<android::ui::ColorMode>* outModes) const override; 285 // Returns a bitmask which contains HdrMetadata::Type::*. 286 int32_t getSupportedPerFrameMetadata() const override; 287 Error getRenderIntents(android::ui::ColorMode colorMode, 288 std::vector<android::ui::RenderIntent>* outRenderIntents) const override; 289 Error getDataspaceSaturationMatrix(android::ui::Dataspace dataspace, 290 android::mat4* outMatrix) override; 291 292 // Doesn't call into the HWC2 device, so no errors are possible 293 std::vector<std::shared_ptr<const Config>> getConfigs() const override; 294 295 Error getName(std::string* outName) const override; 296 Error getRequests(DisplayRequest* outDisplayRequests, 297 std::unordered_map<Layer*, LayerRequest>* outLayerRequests) override; 298 Error getType(DisplayType* outType) const override; 299 Error supportsDoze(bool* outSupport) const override; 300 Error getHdrCapabilities(android::HdrCapabilities* outCapabilities) const override; 301 Error getDisplayedContentSamplingAttributes(android::ui::PixelFormat* outFormat, 302 android::ui::Dataspace* outDataspace, 303 uint8_t* outComponentMask) const override; 304 Error setDisplayContentSamplingEnabled(bool enabled, uint8_t componentMask, 305 uint64_t maxFrames) const override; 306 Error getDisplayedContentSample(uint64_t maxFrames, uint64_t timestamp, 307 android::DisplayedFrameStats* outStats) const override; 308 Error getReleaseFences( 309 std::unordered_map<Layer*, android::sp<android::Fence>>* outFences) const override; 310 Error present(android::sp<android::Fence>* outPresentFence) override; 311 Error setActiveConfig(const std::shared_ptr<const HWC2::Display::Config>& config) override; 312 Error setClientTarget(uint32_t slot, const android::sp<android::GraphicBuffer>& target, 313 const android::sp<android::Fence>& acquireFence, 314 android::ui::Dataspace dataspace) override; 315 Error setColorMode(android::ui::ColorMode mode, 316 android::ui::RenderIntent renderIntent) override; 317 Error setColorTransform(const android::mat4& matrix, android_color_transform_t hint) override; 318 Error setOutputBuffer(const android::sp<android::GraphicBuffer>& buffer, 319 const android::sp<android::Fence>& releaseFence) override; 320 Error setPowerMode(PowerMode mode) override; 321 Error setVsyncEnabled(Vsync enabled) override; 322 Error validate(uint32_t* outNumTypes, uint32_t* outNumRequests) override; 323 Error presentOrValidate(uint32_t* outNumTypes, uint32_t* outNumRequests, 324 android::sp<android::Fence>* outPresentFence, uint32_t* state) override; 325 Error setDisplayBrightness(float brightness) const override; 326 327 // Other Display methods getId()328 hwc2_display_t getId() const override { return mId; } isConnected()329 bool isConnected() const override { return mIsConnected; } 330 void setConnected(bool connected) override; // For use by Device only getCapabilities()331 const std::unordered_set<DisplayCapability>& getCapabilities() const override { 332 return mDisplayCapabilities; 333 }; 334 335 private: 336 int32_t getAttribute(hwc2_config_t configId, Attribute attribute); 337 void loadConfig(hwc2_config_t configId); 338 void loadConfigs(); 339 340 // This may fail (and return a null pointer) if no layer with this ID exists 341 // on this display 342 Layer* getLayerById(hwc2_layer_t id) const; 343 344 friend android::TestableSurfaceFlinger; 345 346 // Member variables 347 348 // These are references to data owned by HWC2::Device, which will outlive 349 // this HWC2::Display, so these references are guaranteed to be valid for 350 // the lifetime of this object. 351 android::Hwc2::Composer& mComposer; 352 const std::unordered_set<Capability>& mCapabilities; 353 354 hwc2_display_t mId; 355 bool mIsConnected; 356 DisplayType mType; 357 std::unordered_map<hwc2_layer_t, std::unique_ptr<Layer>> mLayers; 358 std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs; 359 std::once_flag mDisplayCapabilityQueryFlag; 360 std::unordered_set<DisplayCapability> mDisplayCapabilities; 361 }; 362 } // namespace impl 363 364 class Layer { 365 public: 366 virtual ~Layer(); 367 368 virtual hwc2_layer_t getId() const = 0; 369 370 [[clang::warn_unused_result]] virtual Error setCursorPosition(int32_t x, int32_t y) = 0; 371 [[clang::warn_unused_result]] virtual Error setBuffer( 372 uint32_t slot, const android::sp<android::GraphicBuffer>& buffer, 373 const android::sp<android::Fence>& acquireFence) = 0; 374 [[clang::warn_unused_result]] virtual Error setSurfaceDamage(const android::Region& damage) = 0; 375 376 [[clang::warn_unused_result]] virtual Error setBlendMode(BlendMode mode) = 0; 377 [[clang::warn_unused_result]] virtual Error setColor(hwc_color_t color) = 0; 378 [[clang::warn_unused_result]] virtual Error setCompositionType(Composition type) = 0; 379 [[clang::warn_unused_result]] virtual Error setDataspace(android::ui::Dataspace dataspace) = 0; 380 [[clang::warn_unused_result]] virtual Error setPerFrameMetadata( 381 const int32_t supportedPerFrameMetadata, const android::HdrMetadata& metadata) = 0; 382 [[clang::warn_unused_result]] virtual Error setDisplayFrame(const android::Rect& frame) = 0; 383 [[clang::warn_unused_result]] virtual Error setPlaneAlpha(float alpha) = 0; 384 [[clang::warn_unused_result]] virtual Error setSidebandStream( 385 const native_handle_t* stream) = 0; 386 [[clang::warn_unused_result]] virtual Error setSourceCrop(const android::FloatRect& crop) = 0; 387 [[clang::warn_unused_result]] virtual Error setTransform(Transform transform) = 0; 388 [[clang::warn_unused_result]] virtual Error setVisibleRegion(const android::Region& region) = 0; 389 [[clang::warn_unused_result]] virtual Error setZOrder(uint32_t z) = 0; 390 [[clang::warn_unused_result]] virtual Error setInfo(uint32_t type, uint32_t appId) = 0; 391 392 // Composer HAL 2.3 393 [[clang::warn_unused_result]] virtual Error setColorTransform(const android::mat4& matrix) = 0; 394 }; 395 396 namespace impl { 397 398 // Convenience C++ class to access hwc2_device_t Layer functions directly. 399 400 class Layer : public HWC2::Layer { 401 public: 402 Layer(android::Hwc2::Composer& composer, 403 const std::unordered_set<Capability>& capabilities, 404 hwc2_display_t displayId, hwc2_layer_t layerId); 405 ~Layer() override; 406 getId()407 hwc2_layer_t getId() const override { return mId; } 408 409 Error setCursorPosition(int32_t x, int32_t y) override; 410 Error setBuffer(uint32_t slot, const android::sp<android::GraphicBuffer>& buffer, 411 const android::sp<android::Fence>& acquireFence) override; 412 Error setSurfaceDamage(const android::Region& damage) override; 413 414 Error setBlendMode(BlendMode mode) override; 415 Error setColor(hwc_color_t color) override; 416 Error setCompositionType(Composition type) override; 417 Error setDataspace(android::ui::Dataspace dataspace) override; 418 Error setPerFrameMetadata(const int32_t supportedPerFrameMetadata, 419 const android::HdrMetadata& metadata) override; 420 Error setDisplayFrame(const android::Rect& frame) override; 421 Error setPlaneAlpha(float alpha) override; 422 Error setSidebandStream(const native_handle_t* stream) override; 423 Error setSourceCrop(const android::FloatRect& crop) override; 424 Error setTransform(Transform transform) override; 425 Error setVisibleRegion(const android::Region& region) override; 426 Error setZOrder(uint32_t z) override; 427 Error setInfo(uint32_t type, uint32_t appId) override; 428 429 // Composer HAL 2.3 430 Error setColorTransform(const android::mat4& matrix) override; 431 432 private: 433 // These are references to data owned by HWC2::Device, which will outlive 434 // this HWC2::Layer, so these references are guaranteed to be valid for 435 // the lifetime of this object. 436 android::Hwc2::Composer& mComposer; 437 const std::unordered_set<Capability>& mCapabilities; 438 439 hwc2_display_t mDisplayId; 440 hwc2_layer_t mId; 441 442 // Cached HWC2 data, to ensure the same commands aren't sent to the HWC 443 // multiple times. 444 android::Region mVisibleRegion = android::Region::INVALID_REGION; 445 android::Region mDamageRegion = android::Region::INVALID_REGION; 446 android::ui::Dataspace mDataSpace = android::ui::Dataspace::UNKNOWN; 447 android::HdrMetadata mHdrMetadata; 448 android::mat4 mColorMatrix; 449 uint32_t mBufferSlot; 450 }; 451 452 } // namespace impl 453 454 } // namespace HWC2 455 456 #endif // ANDROID_SF_HWC2_H 457