1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
|
/*
* Copyright 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <android-base/expected.h>
#include <android-base/thread_annotations.h>
#include <ftl/expected.h>
#include <ftl/future.h>
#include <gui/HdrMetadata.h>
#include <math/mat4.h>
#include <ui/HdrCapabilities.h>
#include <ui/Region.h>
#include <ui/StaticDisplayInfo.h>
#include <utils/Log.h>
#include <utils/StrongPointer.h>
#include <utils/Timers.h>
#include <functional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "ComposerHal.h"
#include "Hal.h"
#include <aidl/android/hardware/graphics/common/DisplayDecorationSupport.h>
#include <aidl/android/hardware/graphics/common/DisplayHotplugEvent.h>
#include <aidl/android/hardware/graphics/composer3/Capability.h>
#include <aidl/android/hardware/graphics/composer3/ClientTargetPropertyWithBrightness.h>
#include <aidl/android/hardware/graphics/composer3/Color.h>
#include <aidl/android/hardware/graphics/composer3/Composition.h>
#include <aidl/android/hardware/graphics/composer3/DisplayCapability.h>
#include <aidl/android/hardware/graphics/composer3/OverlayProperties.h>
#include <aidl/android/hardware/graphics/composer3/RefreshRateChangedDebugData.h>
namespace android {
class Fence;
class FloatRect;
class GraphicBuffer;
class TestableSurfaceFlinger;
struct DisplayedFrameStats;
namespace Hwc2 {
class Composer;
} // namespace Hwc2
namespace HWC2 {
class Layer;
namespace hal = android::hardware::graphics::composer::hal;
using aidl::android::hardware::graphics::common::DisplayHotplugEvent;
using aidl::android::hardware::graphics::composer3::RefreshRateChangedDebugData;
// Implement this interface to receive hardware composer events.
//
// These callback functions will generally be called on a hwbinder thread, but
// when first registering the callback the onComposerHalHotplugEvent() function
// will immediately be called on the thread calling registerCallback().
struct ComposerCallback {
virtual void onComposerHalHotplugEvent(hal::HWDisplayId, DisplayHotplugEvent) = 0;
virtual void onComposerHalRefresh(hal::HWDisplayId) = 0;
virtual void onComposerHalVsync(hal::HWDisplayId, nsecs_t timestamp,
std::optional<hal::VsyncPeriodNanos>) = 0;
virtual void onComposerHalVsyncPeriodTimingChanged(hal::HWDisplayId,
const hal::VsyncPeriodChangeTimeline&) = 0;
virtual void onComposerHalSeamlessPossible(hal::HWDisplayId) = 0;
virtual void onComposerHalVsyncIdle(hal::HWDisplayId) = 0;
virtual void onRefreshRateChangedDebug(const RefreshRateChangedDebugData&) = 0;
protected:
~ComposerCallback() = default;
};
// Convenience C++ class to access per display functions directly.
class Display {
public:
virtual ~Display();
virtual hal::HWDisplayId getId() const = 0;
virtual bool isConnected() const = 0;
virtual void setConnected(bool connected) = 0; // For use by HWComposer only
virtual bool hasCapability(
aidl::android::hardware::graphics::composer3::DisplayCapability) const = 0;
virtual bool isVsyncPeriodSwitchSupported() const = 0;
virtual bool hasDisplayIdleTimerCapability() const = 0;
virtual void onLayerDestroyed(hal::HWLayerId layerId) = 0;
[[nodiscard]] virtual hal::Error acceptChanges() = 0;
[[nodiscard]] virtual base::expected<std::shared_ptr<HWC2::Layer>, hal::Error>
createLayer() = 0;
[[nodiscard]] virtual hal::Error getChangedCompositionTypes(
std::unordered_map<Layer*, aidl::android::hardware::graphics::composer3::Composition>*
outTypes) = 0;
[[nodiscard]] virtual hal::Error getColorModes(std::vector<hal::ColorMode>* outModes) const = 0;
// Returns a bitmask which contains HdrMetadata::Type::*.
[[nodiscard]] virtual int32_t getSupportedPerFrameMetadata() const = 0;
[[nodiscard]] virtual hal::Error getRenderIntents(
hal::ColorMode colorMode, std::vector<hal::RenderIntent>* outRenderIntents) const = 0;
[[nodiscard]] virtual hal::Error getDataspaceSaturationMatrix(hal::Dataspace dataspace,
android::mat4* outMatrix) = 0;
[[nodiscard]] virtual hal::Error getName(std::string* outName) const = 0;
[[nodiscard]] virtual hal::Error getRequests(
hal::DisplayRequest* outDisplayRequests,
std::unordered_map<Layer*, hal::LayerRequest>* outLayerRequests) = 0;
[[nodiscard]] virtual ftl::Expected<ui::DisplayConnectionType, hal::Error> getConnectionType()
const = 0;
[[nodiscard]] virtual hal::Error supportsDoze(bool* outSupport) const = 0;
[[nodiscard]] virtual hal::Error getHdrCapabilities(
android::HdrCapabilities* outCapabilities) const = 0;
[[nodiscard]] virtual hal::Error getOverlaySupport(
aidl::android::hardware::graphics::composer3::OverlayProperties* outProperties)
const = 0;
[[nodiscard]] virtual hal::Error getDisplayedContentSamplingAttributes(
hal::PixelFormat* outFormat, hal::Dataspace* outDataspace,
uint8_t* outComponentMask) const = 0;
[[nodiscard]] virtual hal::Error setDisplayContentSamplingEnabled(bool enabled,
uint8_t componentMask,
uint64_t maxFrames) const = 0;
[[nodiscard]] virtual hal::Error getDisplayedContentSample(
uint64_t maxFrames, uint64_t timestamp,
android::DisplayedFrameStats* outStats) const = 0;
[[nodiscard]] virtual hal::Error getReleaseFences(
std::unordered_map<Layer*, android::sp<android::Fence>>* outFences) const = 0;
[[nodiscard]] virtual hal::Error present(android::sp<android::Fence>* outPresentFence) = 0;
[[nodiscard]] virtual hal::Error setClientTarget(
uint32_t slot, const android::sp<android::GraphicBuffer>& target,
const android::sp<android::Fence>& acquireFence, hal::Dataspace dataspace,
float hdrSdrRatio) = 0;
[[nodiscard]] virtual hal::Error setColorMode(hal::ColorMode mode,
hal::RenderIntent renderIntent) = 0;
[[nodiscard]] virtual hal::Error setColorTransform(const android::mat4& matrix) = 0;
[[nodiscard]] virtual hal::Error setOutputBuffer(
const android::sp<android::GraphicBuffer>& buffer,
const android::sp<android::Fence>& releaseFence) = 0;
[[nodiscard]] virtual hal::Error setPowerMode(hal::PowerMode mode) = 0;
[[nodiscard]] virtual hal::Error setVsyncEnabled(hal::Vsync enabled) = 0;
[[nodiscard]] virtual hal::Error validate(nsecs_t expectedPresentTime, int32_t frameIntervalNs,
uint32_t* outNumTypes, uint32_t* outNumRequests) = 0;
[[nodiscard]] virtual hal::Error presentOrValidate(nsecs_t expectedPresentTime,
int32_t frameIntervalNs,
uint32_t* outNumTypes,
uint32_t* outNumRequests,
android::sp<android::Fence>* outPresentFence,
uint32_t* state) = 0;
[[nodiscard]] virtual ftl::Future<hal::Error> setDisplayBrightness(
float brightness, float brightnessNits,
const Hwc2::Composer::DisplayBrightnessOptions& options) = 0;
[[nodiscard]] virtual hal::Error setActiveConfigWithConstraints(
hal::HWConfigId configId, const hal::VsyncPeriodChangeConstraints& constraints,
hal::VsyncPeriodChangeTimeline* outTimeline) = 0;
[[nodiscard]] virtual hal::Error setBootDisplayConfig(hal::HWConfigId configId) = 0;
[[nodiscard]] virtual hal::Error clearBootDisplayConfig() = 0;
[[nodiscard]] virtual hal::Error getPreferredBootDisplayConfig(
hal::HWConfigId* configId) const = 0;
[[nodiscard]] virtual hal::Error setAutoLowLatencyMode(bool on) = 0;
[[nodiscard]] virtual hal::Error getSupportedContentTypes(
std::vector<hal::ContentType>*) const = 0;
[[nodiscard]] virtual hal::Error setContentType(hal::ContentType) = 0;
[[nodiscard]] virtual hal::Error getClientTargetProperty(
aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness*
outClientTargetProperty) = 0;
[[nodiscard]] virtual hal::Error getDisplayDecorationSupport(
std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
support) = 0;
[[nodiscard]] virtual hal::Error setIdleTimerEnabled(std::chrono::milliseconds timeout) = 0;
[[nodiscard]] virtual hal::Error getPhysicalDisplayOrientation(
Hwc2::AidlTransform* outTransform) const = 0;
};
namespace impl {
class Layer;
class Display : public HWC2::Display {
public:
Display(android::Hwc2::Composer&,
const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&,
hal::HWDisplayId, hal::DisplayType);
~Display() override;
// Required by HWC2
hal::Error acceptChanges() override;
base::expected<std::shared_ptr<HWC2::Layer>, hal::Error> createLayer() override;
hal::Error getChangedCompositionTypes(
std::unordered_map<HWC2::Layer*,
aidl::android::hardware::graphics::composer3::Composition>* outTypes)
override;
hal::Error getColorModes(std::vector<hal::ColorMode>* outModes) const override;
// Returns a bitmask which contains HdrMetadata::Type::*.
int32_t getSupportedPerFrameMetadata() const override;
hal::Error getRenderIntents(hal::ColorMode colorMode,
std::vector<hal::RenderIntent>* outRenderIntents) const override;
hal::Error getDataspaceSaturationMatrix(hal::Dataspace, android::mat4* outMatrix) override;
hal::Error getName(std::string* outName) const override;
hal::Error getRequests(
hal::DisplayRequest* outDisplayRequests,
std::unordered_map<HWC2::Layer*, hal::LayerRequest>* outLayerRequests) override;
ftl::Expected<ui::DisplayConnectionType, hal::Error> getConnectionType() const override;
hal::Error supportsDoze(bool* outSupport) const override EXCLUDES(mDisplayCapabilitiesMutex);
hal::Error getHdrCapabilities(android::HdrCapabilities* outCapabilities) const override;
hal::Error getOverlaySupport(aidl::android::hardware::graphics::composer3::OverlayProperties*
outProperties) const override;
hal::Error getDisplayedContentSamplingAttributes(hal::PixelFormat* outFormat,
hal::Dataspace* outDataspace,
uint8_t* outComponentMask) const override;
hal::Error setDisplayContentSamplingEnabled(bool enabled, uint8_t componentMask,
uint64_t maxFrames) const override;
hal::Error getDisplayedContentSample(uint64_t maxFrames, uint64_t timestamp,
android::DisplayedFrameStats* outStats) const override;
hal::Error getReleaseFences(std::unordered_map<HWC2::Layer*, android::sp<android::Fence>>*
outFences) const override;
hal::Error present(android::sp<android::Fence>* outPresentFence) override;
hal::Error setClientTarget(uint32_t slot, const android::sp<android::GraphicBuffer>& target,
const android::sp<android::Fence>& acquireFence,
hal::Dataspace dataspace, float hdrSdrRatio) override;
hal::Error setColorMode(hal::ColorMode, hal::RenderIntent) override;
hal::Error setColorTransform(const android::mat4& matrix) override;
hal::Error setOutputBuffer(const android::sp<android::GraphicBuffer>&,
const android::sp<android::Fence>& releaseFence) override;
hal::Error setPowerMode(hal::PowerMode) override;
hal::Error setVsyncEnabled(hal::Vsync enabled) override;
hal::Error validate(nsecs_t expectedPresentTime, int32_t frameIntervalNs, uint32_t* outNumTypes,
uint32_t* outNumRequests) override;
hal::Error presentOrValidate(nsecs_t expectedPresentTime, int32_t frameIntervalNs,
uint32_t* outNumTypes, uint32_t* outNumRequests,
android::sp<android::Fence>* outPresentFence,
uint32_t* state) override;
ftl::Future<hal::Error> setDisplayBrightness(
float brightness, float brightnessNits,
const Hwc2::Composer::DisplayBrightnessOptions& options) override;
hal::Error setActiveConfigWithConstraints(hal::HWConfigId configId,
const hal::VsyncPeriodChangeConstraints& constraints,
hal::VsyncPeriodChangeTimeline* outTimeline) override;
hal::Error setBootDisplayConfig(hal::HWConfigId configId) override;
hal::Error clearBootDisplayConfig() override;
hal::Error getPreferredBootDisplayConfig(hal::HWConfigId* configId) const override;
hal::Error setAutoLowLatencyMode(bool on) override;
hal::Error getSupportedContentTypes(
std::vector<hal::ContentType>* outSupportedContentTypes) const override;
hal::Error setContentType(hal::ContentType) override;
hal::Error getClientTargetProperty(
aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness*
outClientTargetProperty) override;
hal::Error getDisplayDecorationSupport(
std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>*
support) override;
hal::Error setIdleTimerEnabled(std::chrono::milliseconds timeout) override;
// Other Display methods
hal::HWDisplayId getId() const override { return mId; }
bool isConnected() const override { return mIsConnected; }
void setConnected(bool connected) override;
bool hasCapability(aidl::android::hardware::graphics::composer3::DisplayCapability)
const override EXCLUDES(mDisplayCapabilitiesMutex);
bool isVsyncPeriodSwitchSupported() const override;
bool hasDisplayIdleTimerCapability() const override;
void onLayerDestroyed(hal::HWLayerId layerId) override;
hal::Error getPhysicalDisplayOrientation(Hwc2::AidlTransform* outTransform) const override;
private:
void loadDisplayCapabilities();
// This may fail (and return a null pointer) if no layer with this ID exists
// on this display
std::shared_ptr<HWC2::Layer> getLayerById(hal::HWLayerId id) const;
friend android::TestableSurfaceFlinger;
// Member variables
// These are references to data owned by HWComposer, which will outlive
// this HWC2::Display, so these references are guaranteed to be valid for
// the lifetime of this object.
android::Hwc2::Composer& mComposer;
const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
mCapabilities;
const hal::HWDisplayId mId;
hal::DisplayType mType;
// Cached on first call to getConnectionType.
mutable std::optional<ftl::Expected<ui::DisplayConnectionType, hal::Error>> mConnectionType;
bool mIsConnected = false;
using Layers = std::unordered_map<hal::HWLayerId, std::weak_ptr<HWC2::impl::Layer>>;
Layers mLayers;
mutable std::mutex mDisplayCapabilitiesMutex;
std::once_flag mDisplayCapabilityQueryFlag;
std::optional<
std::unordered_set<aidl::android::hardware::graphics::composer3::DisplayCapability>>
mDisplayCapabilities GUARDED_BY(mDisplayCapabilitiesMutex);
};
} // namespace impl
class Layer {
public:
virtual ~Layer();
virtual hal::HWLayerId getId() const = 0;
[[nodiscard]] virtual hal::Error setCursorPosition(int32_t x, int32_t y) = 0;
[[nodiscard]] virtual hal::Error setBuffer(uint32_t slot,
const android::sp<android::GraphicBuffer>& buffer,
const android::sp<android::Fence>& acquireFence) = 0;
[[nodiscard]] virtual hal::Error setBufferSlotsToClear(
const std::vector<uint32_t>& slotsToClear, uint32_t activeBufferSlot) = 0;
[[nodiscard]] virtual hal::Error setSurfaceDamage(const android::Region& damage) = 0;
[[nodiscard]] virtual hal::Error setBlendMode(hal::BlendMode mode) = 0;
[[nodiscard]] virtual hal::Error setColor(
aidl::android::hardware::graphics::composer3::Color color) = 0;
[[nodiscard]] virtual hal::Error setCompositionType(
aidl::android::hardware::graphics::composer3::Composition type) = 0;
[[nodiscard]] virtual hal::Error setDataspace(hal::Dataspace dataspace) = 0;
[[nodiscard]] virtual hal::Error setPerFrameMetadata(const int32_t supportedPerFrameMetadata,
const android::HdrMetadata& metadata) = 0;
[[nodiscard]] virtual hal::Error setDisplayFrame(const android::Rect& frame) = 0;
[[nodiscard]] virtual hal::Error setPlaneAlpha(float alpha) = 0;
[[nodiscard]] virtual hal::Error setSidebandStream(const native_handle_t* stream) = 0;
[[nodiscard]] virtual hal::Error setSourceCrop(const android::FloatRect& crop) = 0;
[[nodiscard]] virtual hal::Error setTransform(hal::Transform transform) = 0;
[[nodiscard]] virtual hal::Error setVisibleRegion(const android::Region& region) = 0;
[[nodiscard]] virtual hal::Error setZOrder(uint32_t z) = 0;
// Composer HAL 2.3
[[nodiscard]] virtual hal::Error setColorTransform(const android::mat4& matrix) = 0;
// Composer HAL 2.4
[[nodiscard]] virtual hal::Error setLayerGenericMetadata(const std::string& name,
bool mandatory,
const std::vector<uint8_t>& value) = 0;
// AIDL HAL
[[nodiscard]] virtual hal::Error setBrightness(float brightness) = 0;
[[nodiscard]] virtual hal::Error setBlockingRegion(const android::Region& region) = 0;
};
namespace impl {
// Convenience C++ class to access per layer functions directly.
class Layer : public HWC2::Layer {
public:
Layer(android::Hwc2::Composer& composer,
const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
capabilities,
HWC2::Display& display, hal::HWLayerId layerId);
~Layer() override;
void onOwningDisplayDestroyed();
hal::HWLayerId getId() const override { return mId; }
hal::Error setCursorPosition(int32_t x, int32_t y) override;
hal::Error setBuffer(uint32_t slot, const android::sp<android::GraphicBuffer>& buffer,
const android::sp<android::Fence>& acquireFence) override;
hal::Error setBufferSlotsToClear(const std::vector<uint32_t>& slotsToClear,
uint32_t activeBufferSlot) override;
hal::Error setSurfaceDamage(const android::Region& damage) override;
hal::Error setBlendMode(hal::BlendMode mode) override;
hal::Error setColor(aidl::android::hardware::graphics::composer3::Color color) override;
hal::Error setCompositionType(
aidl::android::hardware::graphics::composer3::Composition type) override;
hal::Error setDataspace(hal::Dataspace dataspace) override;
hal::Error setPerFrameMetadata(const int32_t supportedPerFrameMetadata,
const android::HdrMetadata& metadata) override;
hal::Error setDisplayFrame(const android::Rect& frame) override;
hal::Error setPlaneAlpha(float alpha) override;
hal::Error setSidebandStream(const native_handle_t* stream) override;
hal::Error setSourceCrop(const android::FloatRect& crop) override;
hal::Error setTransform(hal::Transform transform) override;
hal::Error setVisibleRegion(const android::Region& region) override;
hal::Error setZOrder(uint32_t z) override;
// Composer HAL 2.3
hal::Error setColorTransform(const android::mat4& matrix) override;
// Composer HAL 2.4
hal::Error setLayerGenericMetadata(const std::string& name, bool mandatory,
const std::vector<uint8_t>& value) override;
// AIDL HAL
hal::Error setBrightness(float brightness) override;
hal::Error setBlockingRegion(const android::Region& region) override;
private:
// These are references to data owned by HWComposer, which will outlive
// this HWC2::Layer, so these references are guaranteed to be valid for
// the lifetime of this object.
android::Hwc2::Composer& mComposer;
const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
mCapabilities;
HWC2::Display* mDisplay;
hal::HWLayerId mId;
// Cached HWC2 data, to ensure the same commands aren't sent to the HWC
// multiple times.
android::Region mVisibleRegion = android::Region::INVALID_REGION;
android::Region mDamageRegion = android::Region::INVALID_REGION;
android::Region mBlockingRegion = android::Region::INVALID_REGION;
hal::Dataspace mDataSpace = hal::Dataspace::UNKNOWN;
android::HdrMetadata mHdrMetadata;
android::mat4 mColorMatrix;
uint32_t mBufferSlot;
};
} // namespace impl
} // namespace HWC2
} // namespace android
|