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