Skip to content

Commit 1b0d4e1

Browse files
committed
Add support for restricting HDR headroom for video
This is needed for allowing apps to vote for HDR headroom restrictions for SurfaceView and SurfaceControl Bug: 323964760 Test: manually poking at test app Test: SurfaceViewTests Test: SurfaceControlTest Test: ASurfaceControlTest Change-Id: Ie886e67879525462d49fdedc535aea659d69321a
1 parent 9892aac commit 1b0d4e1

10 files changed

Lines changed: 106 additions & 10 deletions

File tree

include/android/surface_control.h

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,8 @@ void ASurfaceTransaction_setHdrMetadata_cta861_3(ASurfaceTransaction* transactio
528528

529529
/**
530530
* Sets the desired extended range brightness for the layer. This only applies for layers whose
531-
* dataspace has RANGE_EXTENDED set on it.
532-
*
533-
* Available since API level 34.
531+
* dataspace has RANGE_EXTENDED set on it. See: ASurfaceTransaction_setDesiredHdrHeadroom, prefer
532+
* using this API for formats that encode an HDR/SDR ratio as part of generating the buffer.
534533
*
535534
* @param surface_control The layer whose extended range brightness is being specified
536535
* @param currentBufferRatio The current hdr/sdr ratio of the current buffer as represented as
@@ -564,6 +563,12 @@ void ASurfaceTransaction_setHdrMetadata_cta861_3(ASurfaceTransaction* transactio
564563
* determined entirely by the dataspace being used (ie, typically SDR
565564
* however PQ or HLG transfer functions will still result in HDR)
566565
*
566+
* When called after ASurfaceTransaction_setDesiredHdrHeadroom, the
567+
* desiredRatio will override the desiredHeadroom provided by
568+
* ASurfaceTransaction_setDesiredHdrHeadroom. Conversely, when called before
569+
* ASurfaceTransaction_setDesiredHdrHeadroom, the desiredHeadroom provided by
570+
*. ASurfaceTransaction_setDesiredHdrHeadroom will override the desiredRatio.
571+
*
567572
* Must be finite && >= 1.0f
568573
*
569574
* Available since API level 34.
@@ -573,6 +578,45 @@ void ASurfaceTransaction_setExtendedRangeBrightness(ASurfaceTransaction* transac
573578
float currentBufferRatio,
574579
float desiredRatio) __INTRODUCED_IN(__ANDROID_API_U__);
575580

581+
/**
582+
* Sets the desired hdr headroom for the layer. See: ASurfaceTransaction_setExtendedRangeBrightness,
583+
* prefer using this API for formats that conform to HDR standards like HLG or HDR10, that do not
584+
* communicate a HDR/SDR ratio as part of generating the buffer.
585+
*
586+
* @param surface_control The layer whose desired hdr headroom is being specified
587+
*
588+
* @param desiredHeadroom The desired hdr/sdr ratio as represented as peakHdrBrightnessInNits /
589+
* targetSdrWhitePointInNits. This can be used to communicate the max
590+
* desired brightness range of the panel. The system may not be able to, or
591+
* may choose not to, deliver the requested range.
592+
*
593+
* While requesting a large desired ratio will result in the most
594+
* dynamic range, voluntarily reducing the requested range can help
595+
* improve battery life as well as can improve quality by ensuring
596+
* greater bit depth is allocated to the luminance range in use.
597+
*
598+
* Default value is 0.0f and indicates that the system will choose the best
599+
* headroom for this surface control's content. Typically, this means that
600+
* HLG/PQ encoded content will be displayed with some HDR headroom greater
601+
* than 1.0.
602+
*
603+
* When called after ASurfaceTransaction_setExtendedRangeBrightness, the
604+
* desiredHeadroom will override the desiredRatio provided by
605+
* ASurfaceTransaction_setExtendedRangeBrightness. Conversely, when called
606+
* before ASurfaceTransaction_setExtendedRangeBrightness, the desiredRatio
607+
* provided by ASurfaceTransaction_setExtendedRangeBrightness will override
608+
* the desiredHeadroom.
609+
*
610+
* Must be finite && >= 1.0f or 0.0f to indicate there is no desired
611+
* headroom.
612+
*
613+
* Available since API level 35.
614+
*/
615+
void ASurfaceTransaction_setDesiredHdrHeadroom(ASurfaceTransaction* transaction,
616+
ASurfaceControl* surface_control,
617+
float desiredHeadroom)
618+
__INTRODUCED_IN(__ANDROID_API_V__);
619+
576620
/**
577621
* Same as ASurfaceTransaction_setFrameRateWithChangeStrategy(transaction, surface_control,
578622
* frameRate, compatibility, ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS).

libs/gui/LayerState.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,10 @@ void layer_state_t::merge(const layer_state_t& other) {
605605
desiredHdrSdrRatio = other.desiredHdrSdrRatio;
606606
currentHdrSdrRatio = other.currentHdrSdrRatio;
607607
}
608+
if (other.what & eDesiredHdrHeadroomChanged) {
609+
what |= eDesiredHdrHeadroomChanged;
610+
desiredHdrSdrRatio = other.desiredHdrSdrRatio;
611+
}
608612
if (other.what & eCachingHintChanged) {
609613
what |= eCachingHintChanged;
610614
cachingHint = other.cachingHint;
@@ -768,6 +772,7 @@ uint64_t layer_state_t::diff(const layer_state_t& other) const {
768772
CHECK_DIFF(diff, eDataspaceChanged, other, dataspace);
769773
CHECK_DIFF2(diff, eExtendedRangeBrightnessChanged, other, currentHdrSdrRatio,
770774
desiredHdrSdrRatio);
775+
CHECK_DIFF(diff, eDesiredHdrHeadroomChanged, other, desiredHdrSdrRatio);
771776
CHECK_DIFF(diff, eCachingHintChanged, other, cachingHint);
772777
CHECK_DIFF(diff, eHdrMetadataChanged, other, hdrMetadata);
773778
if (other.what & eSurfaceDamageRegionChanged &&

libs/gui/SurfaceComposerClient.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,20 @@ SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setExten
18071807
return *this;
18081808
}
18091809

1810+
SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDesiredHdrHeadroom(
1811+
const sp<SurfaceControl>& sc, float desiredRatio) {
1812+
layer_state_t* s = getLayerState(sc);
1813+
if (!s) {
1814+
mStatus = BAD_INDEX;
1815+
return *this;
1816+
}
1817+
s->what |= layer_state_t::eDesiredHdrHeadroomChanged;
1818+
s->desiredHdrSdrRatio = desiredRatio;
1819+
1820+
registerSurfaceControlForCallback(sc);
1821+
return *this;
1822+
}
1823+
18101824
SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCachingHint(
18111825
const sp<SurfaceControl>& sc, gui::CachingHint cachingHint) {
18121826
layer_state_t* s = getLayerState(sc);

libs/gui/include/gui/LayerState.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ struct layer_state_t {
206206
eBackgroundBlurRadiusChanged = 0x80'00000000,
207207
eProducerDisconnect = 0x100'00000000,
208208
eFixedTransformHintChanged = 0x200'00000000,
209-
/* unused 0x400'00000000, */
209+
eDesiredHdrHeadroomChanged = 0x400'00000000,
210210
eBlurRegionsChanged = 0x800'00000000,
211211
eAutoRefreshChanged = 0x1000'00000000,
212212
eStretchChanged = 0x2000'00000000,
@@ -245,7 +245,8 @@ struct layer_state_t {
245245
layer_state_t::eSidebandStreamChanged | layer_state_t::eSurfaceDamageRegionChanged |
246246
layer_state_t::eTransformToDisplayInverseChanged |
247247
layer_state_t::eTransparentRegionChanged |
248-
layer_state_t::eExtendedRangeBrightnessChanged;
248+
layer_state_t::eExtendedRangeBrightnessChanged |
249+
layer_state_t::eDesiredHdrHeadroomChanged;
249250

250251
// Content updates.
251252
static constexpr uint64_t CONTENT_CHANGES = layer_state_t::BUFFER_CHANGES |

libs/gui/include/gui/SurfaceComposerClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ class SurfaceComposerClient : public RefBase
594594
Transaction& setDataspace(const sp<SurfaceControl>& sc, ui::Dataspace dataspace);
595595
Transaction& setExtendedRangeBrightness(const sp<SurfaceControl>& sc,
596596
float currentBufferRatio, float desiredRatio);
597+
Transaction& setDesiredHdrHeadroom(const sp<SurfaceControl>& sc, float desiredRatio);
597598
Transaction& setCachingHint(const sp<SurfaceControl>& sc, gui::CachingHint cachingHint);
598599
Transaction& setHdrMetadata(const sp<SurfaceControl>& sc, const HdrMetadata& hdrMetadata);
599600
Transaction& setSurfaceDamageRegion(const sp<SurfaceControl>& sc,

services/surfaceflinger/FrontEnd/LayerSnapshot.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,9 @@ void LayerSnapshot::merge(const RequestedLayerState& requested, bool forceUpdate
380380
currentHdrSdrRatio = requested.currentHdrSdrRatio;
381381
desiredHdrSdrRatio = requested.desiredHdrSdrRatio;
382382
}
383+
if (forceUpdate || requested.what & layer_state_t::eDesiredHdrHeadroomChanged) {
384+
desiredHdrSdrRatio = requested.desiredHdrSdrRatio;
385+
}
383386
if (forceUpdate || requested.what & layer_state_t::eCachingHintChanged) {
384387
cachingHint = requested.cachingHint;
385388
}

services/surfaceflinger/FrontEnd/RequestedLayerState.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ RequestedLayerState::RequestedLayerState(const LayerCreationArgs& args)
9898
z = 0;
9999
layerStack = ui::DEFAULT_LAYER_STACK;
100100
transformToDisplayInverse = false;
101-
desiredHdrSdrRatio = 1.f;
101+
desiredHdrSdrRatio = -1.f;
102102
currentHdrSdrRatio = 1.f;
103103
dataspaceRequested = false;
104104
hdrMetadata.validTypes = 0;
@@ -603,7 +603,8 @@ bool RequestedLayerState::isSimpleBufferUpdate(const layer_state_t& s) const {
603603
layer_state_t::eShadowRadiusChanged | layer_state_t::eFixedTransformHintChanged |
604604
layer_state_t::eTrustedOverlayChanged | layer_state_t::eStretchChanged |
605605
layer_state_t::eBufferCropChanged | layer_state_t::eDestinationFrameChanged |
606-
layer_state_t::eDimmingEnabledChanged | layer_state_t::eExtendedRangeBrightnessChanged;
606+
layer_state_t::eDimmingEnabledChanged | layer_state_t::eExtendedRangeBrightnessChanged |
607+
layer_state_t::eDesiredHdrHeadroomChanged;
607608
if (changedFlags & deniedChanges) {
608609
ATRACE_FORMAT_INSTANT("%s: false [has denied changes flags 0x%" PRIx64 "]", __func__,
609610
s.what & deniedChanges);

services/surfaceflinger/Layer.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,6 +3395,14 @@ bool Layer::setExtendedRangeBrightness(float currentBufferRatio, float desiredRa
33953395
return true;
33963396
}
33973397

3398+
bool Layer::setDesiredHdrHeadroom(float desiredRatio) {
3399+
if (mDrawingState.desiredHdrSdrRatio == desiredRatio) return false;
3400+
mDrawingState.desiredHdrSdrRatio = desiredRatio;
3401+
mDrawingState.modified = true;
3402+
setTransactionFlags(eTransactionNeeded);
3403+
return true;
3404+
}
3405+
33983406
bool Layer::setCachingHint(gui::CachingHint cachingHint) {
33993407
if (mDrawingState.cachingHint == cachingHint) return false;
34003408
mDrawingState.cachingHint = cachingHint;
@@ -3991,6 +3999,13 @@ bool Layer::isSimpleBufferUpdate(const layer_state_t& s) const {
39913999
}
39924000
}
39934001

4002+
if (s.what & layer_state_t::eDesiredHdrHeadroomChanged) {
4003+
if (mDrawingState.desiredHdrSdrRatio != s.desiredHdrSdrRatio) {
4004+
ATRACE_FORMAT_INSTANT("%s: false [eDesiredHdrHeadroomChanged changed]", __func__);
4005+
return false;
4006+
}
4007+
}
4008+
39944009
return true;
39954010
}
39964011

services/surfaceflinger/Layer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ class Layer : public virtual RefBase {
233233
bool autoRefresh = false;
234234
bool dimmingEnabled = true;
235235
float currentHdrSdrRatio = 1.f;
236-
float desiredHdrSdrRatio = 1.f;
236+
float desiredHdrSdrRatio = -1.f;
237237
gui::CachingHint cachingHint = gui::CachingHint::Enabled;
238238
int64_t latchedVsyncId = 0;
239239
bool useVsyncIdForRefreshRateSelection = false;
@@ -317,6 +317,7 @@ class Layer : public virtual RefBase {
317317
void setDesiredPresentTime(nsecs_t /*desiredPresentTime*/, bool /*isAutoTimestamp*/);
318318
bool setDataspace(ui::Dataspace /*dataspace*/);
319319
bool setExtendedRangeBrightness(float currentBufferRatio, float desiredRatio);
320+
bool setDesiredHdrHeadroom(float desiredRatio);
320321
bool setCachingHint(gui::CachingHint cachingHint);
321322
bool setHdrMetadata(const HdrMetadata& /*hdrMetadata*/);
322323
bool setSurfaceDamageRegion(const Region& /*surfaceDamage*/);
@@ -546,7 +547,7 @@ class Layer : public virtual RefBase {
546547
sp<IBinder> mReleaseBufferEndpoint;
547548

548549
bool mFrameLatencyNeeded{false};
549-
float mDesiredHdrSdrRatio = 1.f;
550+
float mDesiredHdrSdrRatio = -1.f;
550551
};
551552

552553
BufferInfo mBufferInfo;

services/surfaceflinger/SurfaceFlinger.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3119,6 +3119,7 @@ void SurfaceFlinger::onCompositionPresented(PhysicalDisplayId pacesetterId,
31193119
for (auto& [compositionDisplay, listener] : hdrInfoListeners) {
31203120
HdrLayerInfoReporter::HdrLayerInfo info;
31213121
int32_t maxArea = 0;
3122+
31223123
auto updateInfoFn =
31233124
[&](const std::shared_ptr<compositionengine::Display>& compositionDisplay,
31243125
const frontend::LayerSnapshot& snapshot, const sp<LayerFE>& layerFe) {
@@ -3129,7 +3130,7 @@ void SurfaceFlinger::onCompositionPresented(PhysicalDisplayId pacesetterId,
31293130
compositionDisplay->getOutputLayerForLayer(layerFe);
31303131
if (outputLayer) {
31313132
const float desiredHdrSdrRatio =
3132-
snapshot.desiredHdrSdrRatio <= 1.f
3133+
snapshot.desiredHdrSdrRatio < 1.f
31333134
? std::numeric_limits<float>::infinity()
31343135
: snapshot.desiredHdrSdrRatio;
31353136
info.mergeDesiredRatio(desiredHdrSdrRatio);
@@ -5415,6 +5416,11 @@ uint32_t SurfaceFlinger::setClientStateLocked(const FrameTimelineInfo& frameTime
54155416
flags |= eTraversalNeeded;
54165417
}
54175418
}
5419+
if (what & layer_state_t::eDesiredHdrHeadroomChanged) {
5420+
if (layer->setDesiredHdrHeadroom(s.desiredHdrSdrRatio)) {
5421+
flags |= eTraversalNeeded;
5422+
}
5423+
}
54185424
if (what & layer_state_t::eCachingHintChanged) {
54195425
if (layer->setCachingHint(s.cachingHint)) {
54205426
flags |= eTraversalNeeded;
@@ -5600,6 +5606,11 @@ uint32_t SurfaceFlinger::updateLayerCallbacksAndStats(const FrameTimelineInfo& f
56005606
flags |= eTraversalNeeded;
56015607
}
56025608
}
5609+
if (what & layer_state_t::eDesiredHdrHeadroomChanged) {
5610+
if (layer->setDesiredHdrHeadroom(s.desiredHdrSdrRatio)) {
5611+
flags |= eTraversalNeeded;
5612+
}
5613+
}
56035614
if (what & layer_state_t::eBufferChanged) {
56045615
std::optional<ui::Transform::RotationFlags> transformHint = std::nullopt;
56055616
frontend::LayerSnapshot* snapshot = mLayerSnapshotBuilder.getSnapshot(layer->sequence);

0 commit comments

Comments
 (0)