forked from moonlight-stream/moonlight-android
-
-
Notifications
You must be signed in to change notification settings - Fork 86
fix(hdr): select display modes within HDR candidates #502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
70c5112
fix(hdr): select display modes within HDR candidates
qiin2333 26a7f7c
chore: pin common-c to latest mic
qiin2333 c146252
fix(build): include common-c DS5 haptics sources
qiin2333 2dc55b2
fix(hdr): address display mode review feedback
qiin2333 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package com.limelight | ||
|
|
||
| import kotlin.math.roundToInt | ||
|
|
||
| /** Pure display-mode selection policy, independent of Android framework objects. */ | ||
| internal object DisplayModePolicy { | ||
| data class Mode( | ||
| val id: Int, | ||
| val width: Int, | ||
| val height: Int, | ||
| val refreshRate: Float, | ||
| val hdrTypes: List<Int> = emptyList(), | ||
| ) | ||
|
|
||
| data class Request( | ||
| val width: Int, | ||
| val height: Int, | ||
| val fps: Int, | ||
| val usesNativeDisplayMode: Boolean, | ||
| val mayReduceRefreshRate: Boolean, | ||
| val acceptableHdrTypes: List<Int> = emptyList(), | ||
| ) | ||
|
|
||
| data class Result( | ||
| val mode: Mode, | ||
| val hdrFilterApplied: Boolean, | ||
| ) | ||
|
|
||
| fun selectBestMode( | ||
| currentMode: Mode, | ||
| supportedModes: List<Mode>, | ||
| request: Request, | ||
| ): Result { | ||
| val hdrModes = if (request.acceptableHdrTypes.isNotEmpty()) { | ||
| supportedModes.filter { it.supportsAny(request.acceptableHdrTypes) } | ||
| } else { | ||
| emptyList() | ||
| } | ||
| val hdrFilterApplied = hdrModes.isNotEmpty() | ||
| val eligibleModes = if (hdrFilterApplied) hdrModes else supportedModes | ||
|
|
||
| // An HDR request must be optimized entirely within the matching HDR set. Keep the | ||
| // current mode as the baseline only when it belongs to that set; otherwise the first | ||
| // candidate that passes the resolution constraints establishes the HDR baseline. | ||
| var bestMode: Mode? = currentMode.takeIf { !hdrFilterApplied || it.supportsAny(request.acceptableHdrTypes) } | ||
| var refreshRateIsGood = bestMode?.let { isRefreshRateGoodMatch(it.refreshRate, request.fps) } ?: false | ||
| var refreshRateIsEqual = bestMode?.let { isRefreshRateEqualMatch(it.refreshRate, request.fps) } ?: false | ||
|
|
||
| for (candidate in eligibleModes) { | ||
| val comparisonMode = bestMode ?: currentMode | ||
| val resolutionReduced = candidate.width < comparisonMode.width || | ||
| candidate.height < comparisonMode.height | ||
| val resolutionFitsStream = candidate.width >= request.width && | ||
| candidate.height >= request.height | ||
|
|
||
| if (candidate.width > 4096 && request.width <= 4096) { | ||
| continue | ||
| } | ||
|
|
||
| if (request.width < 3840 && request.fps <= 60 && !request.usesNativeDisplayMode && | ||
| (currentMode.width != candidate.width || currentMode.height != candidate.height) | ||
| ) { | ||
| continue | ||
| } | ||
|
|
||
| if (resolutionReduced && !(request.fps > 60 && resolutionFitsStream)) { | ||
| continue | ||
| } | ||
|
|
||
| // There is no valid HDR baseline yet. The first resolution-compatible HDR mode | ||
| // must not be compared against an SDR mode's refresh rate. | ||
| if (bestMode != null) { | ||
| val refreshRateReduced = candidate.refreshRate < bestMode.refreshRate | ||
|
|
||
| if (request.mayReduceRefreshRate && refreshRateIsEqual && | ||
| !isRefreshRateEqualMatch(candidate.refreshRate, request.fps) | ||
| ) { | ||
| continue | ||
| } else if (refreshRateIsGood) { | ||
| if (!isRefreshRateGoodMatch(candidate.refreshRate, request.fps)) { | ||
| continue | ||
| } | ||
|
|
||
| if (request.mayReduceRefreshRate) { | ||
| if (candidate.refreshRate > bestMode.refreshRate) { | ||
| continue | ||
| } | ||
| } else if (refreshRateReduced) { | ||
| continue | ||
| } | ||
| } else if (!isRefreshRateGoodMatch(candidate.refreshRate, request.fps) && refreshRateReduced) { | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| bestMode = candidate | ||
| refreshRateIsGood = isRefreshRateGoodMatch(candidate.refreshRate, request.fps) | ||
| refreshRateIsEqual = isRefreshRateEqualMatch(candidate.refreshRate, request.fps) | ||
| } | ||
|
|
||
| return Result(bestMode ?: currentMode, hdrFilterApplied) | ||
| } | ||
|
|
||
| fun isRefreshRateEqualMatch(refreshRate: Float, targetFps: Int): Boolean { | ||
| return refreshRate >= targetFps && refreshRate <= targetFps + 3 | ||
| } | ||
|
|
||
| fun isRefreshRateGoodMatch(refreshRate: Float, targetFps: Int): Boolean { | ||
| return refreshRate >= targetFps && refreshRate.roundToInt() % targetFps <= 3 | ||
| } | ||
|
|
||
| private fun Mode.supportsAny(acceptableHdrTypes: List<Int>): Boolean { | ||
| return hdrTypes.any(acceptableHdrTypes::contains) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule moonlight-common-c
updated
12 files
| +24 −0 | CMakeLists.txt | |
| +7 −0 | src/Connection.c | |
| +62 −0 | src/ControlStream.c | |
| +87 −0 | src/Ds5HapticsIrStream.c | |
| +14 −0 | src/Ds5HapticsIrStream.h | |
| +69 −0 | src/Ds5HapticsStream.c | |
| +15 −0 | src/Ds5HapticsStream.h | |
| +2 −0 | src/Limelight-internal.h | |
| +51 −0 | src/Limelight.h | |
| +6 −0 | src/SdpGenerator.c | |
| +115 −0 | tests/Ds5HapticsIrStreamGoldenTest.c | |
| +115 −0 | tests/Ds5HapticsStreamGoldenTest.c |
105 changes: 105 additions & 0 deletions
105
app/src/test/java/com/limelight/DisplayModePolicyTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package com.limelight | ||
|
|
||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
|
|
||
| class DisplayModePolicyTest { | ||
| @Test | ||
| fun selectsLowerRefreshHdrModeWhenCurrentModeDoesNotSupportHdr() { | ||
| val currentSdrMode = mode(id = 1, refreshRate = 120f) | ||
| val hdrMode = mode(id = 2, refreshRate = 60f, hdrTypes = listOf(HDR10)) | ||
|
|
||
| val result = DisplayModePolicy.selectBestMode( | ||
| currentMode = currentSdrMode, | ||
| supportedModes = listOf(currentSdrMode, hdrMode), | ||
| request = request(acceptableHdrTypes = listOf(HDR10)), | ||
| ) | ||
|
|
||
| assertEquals(hdrMode, result.mode) | ||
| assertTrue(result.hdrFilterApplied) | ||
| } | ||
|
|
||
| @Test | ||
| fun keepsCurrentModeWhenItSupportsRequestedHdrType() { | ||
| val currentHdrMode = mode(id = 1, refreshRate = 120f, hdrTypes = listOf(HDR10)) | ||
| val lowerRefreshHdrMode = mode(id = 2, refreshRate = 60f, hdrTypes = listOf(HDR10)) | ||
|
|
||
| val result = DisplayModePolicy.selectBestMode( | ||
| currentMode = currentHdrMode, | ||
| supportedModes = listOf(currentHdrMode, lowerRefreshHdrMode), | ||
| request = request(acceptableHdrTypes = listOf(HDR10)), | ||
| ) | ||
|
|
||
| assertEquals(currentHdrMode, result.mode) | ||
| assertTrue(result.hdrFilterApplied) | ||
| } | ||
|
|
||
| @Test | ||
| fun fallsBackToNormalSelectionWhenNoModeSupportsRequestedHdrType() { | ||
| val currentSdrMode = mode(id = 1, refreshRate = 60f) | ||
| val fasterSdrMode = mode(id = 2, refreshRate = 120f) | ||
|
|
||
| val result = DisplayModePolicy.selectBestMode( | ||
| currentMode = currentSdrMode, | ||
| supportedModes = listOf(currentSdrMode, fasterSdrMode), | ||
| request = request(acceptableHdrTypes = listOf(HDR10)), | ||
| ) | ||
|
|
||
| assertEquals(fasterSdrMode, result.mode) | ||
| assertFalse(result.hdrFilterApplied) | ||
| } | ||
|
|
||
| @Test | ||
| fun doesNotRetainHdrCandidateThatViolatesResolutionConstraints() { | ||
| val currentSdrMode = mode(id = 1, width = 3840, height = 2160, refreshRate = 120f) | ||
| val undersizedHdrMode = mode( | ||
| id = 2, | ||
| width = 1920, | ||
| height = 1080, | ||
| refreshRate = 60f, | ||
| hdrTypes = listOf(HDR10), | ||
| ) | ||
|
|
||
| val result = DisplayModePolicy.selectBestMode( | ||
| currentMode = currentSdrMode, | ||
| supportedModes = listOf(currentSdrMode, undersizedHdrMode), | ||
| request = request(acceptableHdrTypes = listOf(HDR10)), | ||
| ) | ||
|
|
||
| assertEquals(currentSdrMode, result.mode) | ||
| assertTrue(result.hdrFilterApplied) | ||
| } | ||
|
|
||
| @Test | ||
| fun modeEqualityUsesHdrTypeValues() { | ||
| assertEquals( | ||
| mode(id = 1, refreshRate = 60f, hdrTypes = listOf(HDR10)), | ||
| mode(id = 1, refreshRate = 60f, hdrTypes = listOf(HDR10)), | ||
| ) | ||
| } | ||
|
|
||
| private fun mode( | ||
| id: Int, | ||
| width: Int = 3840, | ||
| height: Int = 2160, | ||
| refreshRate: Float, | ||
| hdrTypes: List<Int> = emptyList(), | ||
| ) = DisplayModePolicy.Mode(id, width, height, refreshRate, hdrTypes) | ||
|
|
||
| private fun request( | ||
| acceptableHdrTypes: List<Int>, | ||
| ) = DisplayModePolicy.Request( | ||
| width = 3840, | ||
| height = 2160, | ||
| fps = 60, | ||
| usesNativeDisplayMode = true, | ||
| mayReduceRefreshRate = false, | ||
| acceptableHdrTypes = acceptableHdrTypes, | ||
| ) | ||
|
|
||
| private companion object { | ||
| const val HDR10 = 2 | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.