Skip to content

Commit 1d6363a

Browse files
AMouriAndroid (Google) Code Review
authored andcommitted
Merge "Add support for restricting HDR headroom for video" into main
2 parents 4238fc9 + 1b0d4e1 commit 1d6363a

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
@@ -611,6 +611,10 @@ void layer_state_t::merge(const layer_state_t& other) {
611611
desiredHdrSdrRatio = other.desiredHdrSdrRatio;
612612
currentHdrSdrRatio = other.currentHdrSdrRatio;
613613
}
614+
if (other.what & eDesiredHdrHeadroomChanged) {
615+
what |= eDesiredHdrHeadroomChanged;
616+
desiredHdrSdrRatio = other.desiredHdrSdrRatio;
617+
}
614618
if (other.what & eCachingHintChanged) {
615619
what |= eCachingHintChanged;
616620
cachingHint = other.cachingHint;
@@ -774,6 +778,7 @@ uint64_t layer_state_t::diff(const layer_state_t& other) const {
774778
CHECK_DIFF(diff, eDataspaceChanged, other, dataspace);
775779
CHECK_DIFF2(diff, eExtendedRangeBrightnessChanged, other, currentHdrSdrRatio,
776780
desiredHdrSdrRatio);
781+
CHECK_DIFF(diff, eDesiredHdrHeadroomChanged, other, desiredHdrSdrRatio);
777782
CHECK_DIFF(diff, eCachingHintChanged, other, cachingHint);
778783
CHECK_DIFF(diff, eHdrMetadataChanged, other, hdrMetadata);
779784
if (other.what & eSurfaceDamageRegionChanged &&

libs/gui/SurfaceComposerClient.cpp

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

1811+
SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setDesiredHdrHeadroom(
1812+
const sp<SurfaceControl>& sc, float desiredRatio) {
1813+
layer_state_t* s = getLayerState(sc);
1814+
if (!s) {
1815+
mStatus = BAD_INDEX;
1816+
return *this;
1817+
}
1818+
s->what |= layer_state_t::eDesiredHdrHeadroomChanged;
1819+
s->desiredHdrSdrRatio = desiredRatio;
1820+
1821+
registerSurfaceControlForCallback(sc);
1822+
return *this;
1823+
}
1824+
18111825
SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setCachingHint(
18121826
const sp<SurfaceControl>& sc, gui::CachingHint cachingHint) {
18131827
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
@@ -209,7 +209,7 @@ struct layer_state_t {
209209
eBackgroundBlurRadiusChanged = 0x80'00000000,
210210
eProducerDisconnect = 0x100'00000000,
211211
eFixedTransformHintChanged = 0x200'00000000,
212-
/* unused 0x400'00000000, */
212+
eDesiredHdrHeadroomChanged = 0x400'00000000,
213213
eBlurRegionsChanged = 0x800'00000000,
214214
eAutoRefreshChanged = 0x1000'00000000,
215215
eStretchChanged = 0x2000'00000000,
@@ -248,7 +248,8 @@ struct layer_state_t {
248248
layer_state_t::eSidebandStreamChanged | layer_state_t::eSurfaceDamageRegionChanged |
249249
layer_state_t::eTransformToDisplayInverseChanged |
250250
layer_state_t::eTransparentRegionChanged |
251-
layer_state_t::eExtendedRangeBrightnessChanged;
251+
layer_state_t::eExtendedRangeBrightnessChanged |
252+
layer_state_t::eDesiredHdrHeadroomChanged;
252253

253254
// Content updates.
254255
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
@@ -381,6 +381,9 @@ void LayerSnapshot::merge(const RequestedLayerState& requested, bool forceUpdate
381381
currentHdrSdrRatio = requested.currentHdrSdrRatio;
382382
desiredHdrSdrRatio = requested.desiredHdrSdrRatio;
383383
}
384+
if (forceUpdate || requested.what & layer_state_t::eDesiredHdrHeadroomChanged) {
385+
desiredHdrSdrRatio = requested.desiredHdrSdrRatio;
386+
}
384387
if (forceUpdate || requested.what & layer_state_t::eCachingHintChanged) {
385388
cachingHint = requested.cachingHint;
386389
}

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;
@@ -606,7 +606,8 @@ bool RequestedLayerState::isSimpleBufferUpdate(const layer_state_t& s) const {
606606
layer_state_t::eShadowRadiusChanged | layer_state_t::eFixedTransformHintChanged |
607607
layer_state_t::eTrustedOverlayChanged | layer_state_t::eStretchChanged |
608608
layer_state_t::eBufferCropChanged | layer_state_t::eDestinationFrameChanged |
609-
layer_state_t::eDimmingEnabledChanged | layer_state_t::eExtendedRangeBrightnessChanged;
609+
layer_state_t::eDimmingEnabledChanged | layer_state_t::eExtendedRangeBrightnessChanged |
610+
layer_state_t::eDesiredHdrHeadroomChanged;
610611
if (changedFlags & deniedChanges) {
611612
ATRACE_FORMAT_INSTANT("%s: false [has denied changes flags 0x%" PRIx64 "]", __func__,
612613
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
@@ -3111,6 +3111,7 @@ void SurfaceFlinger::onCompositionPresented(PhysicalDisplayId pacesetterId,
31113111
for (auto& [compositionDisplay, listener] : hdrInfoListeners) {
31123112
HdrLayerInfoReporter::HdrLayerInfo info;
31133113
int32_t maxArea = 0;
3114+
31143115
auto updateInfoFn =
31153116
[&](const std::shared_ptr<compositionengine::Display>& compositionDisplay,
31163117
const frontend::LayerSnapshot& snapshot, const sp<LayerFE>& layerFe) {
@@ -3121,7 +3122,7 @@ void SurfaceFlinger::onCompositionPresented(PhysicalDisplayId pacesetterId,
31213122
compositionDisplay->getOutputLayerForLayer(layerFe);
31223123
if (outputLayer) {
31233124
const float desiredHdrSdrRatio =
3124-
snapshot.desiredHdrSdrRatio <= 1.f
3125+
snapshot.desiredHdrSdrRatio < 1.f
31253126
? std::numeric_limits<float>::infinity()
31263127
: snapshot.desiredHdrSdrRatio;
31273128
info.mergeDesiredRatio(desiredHdrSdrRatio);
@@ -5559,6 +5560,11 @@ uint32_t SurfaceFlinger::setClientStateLocked(const FrameTimelineInfo& frameTime
55595560
flags |= eTraversalNeeded;
55605561
}
55615562
}
5563+
if (what & layer_state_t::eDesiredHdrHeadroomChanged) {
5564+
if (layer->setDesiredHdrHeadroom(s.desiredHdrSdrRatio)) {
5565+
flags |= eTraversalNeeded;
5566+
}
5567+
}
55625568
if (what & layer_state_t::eCachingHintChanged) {
55635569
if (layer->setCachingHint(s.cachingHint)) {
55645570
flags |= eTraversalNeeded;
@@ -5744,6 +5750,11 @@ uint32_t SurfaceFlinger::updateLayerCallbacksAndStats(const FrameTimelineInfo& f
57445750
flags |= eTraversalNeeded;
57455751
}
57465752
}
5753+
if (what & layer_state_t::eDesiredHdrHeadroomChanged) {
5754+
if (layer->setDesiredHdrHeadroom(s.desiredHdrSdrRatio)) {
5755+
flags |= eTraversalNeeded;
5756+
}
5757+
}
57475758
if (what & layer_state_t::eBufferChanged) {
57485759
std::optional<ui::Transform::RotationFlags> transformHint = std::nullopt;
57495760
frontend::LayerSnapshot* snapshot = mLayerSnapshotBuilder.getSnapshot(layer->sequence);

0 commit comments

Comments
 (0)