Skip to content

Commit

Permalink
[android] Report display refresh rate to server (#3209)
Browse files Browse the repository at this point in the history
b/338464619
  • Loading branch information
jasonzhangxx committed May 15, 2024
1 parent 56d3eaa commit 3725b01
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import android.content.Context;
import dev.cobalt.coat.CobaltService;
import dev.cobalt.util.DisplayUtil;
import dev.cobalt.util.Log;

/** ClientLogInfo to report Android API support on android devices. */
Expand Down Expand Up @@ -32,7 +33,11 @@ public void afterStopped() {}
public ResponseToClient receiveFromClient(byte[] data) {
ResponseToClient response = new ResponseToClient();
response.invalidState = false;
response.data = clientInfo.getBytes(UTF_8);

String responseString =
"displayRefreshRate:" + DisplayUtil.getDefaultDisplayRefreshRate() + ";";
responseString += clientInfo;
response.data = responseString.getBytes(UTF_8);
return response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import android.os.Message;
import android.view.Choreographer;
import android.view.Choreographer.FrameCallback;
import android.view.Display;
import dev.cobalt.util.DisplayUtil;
import dev.cobalt.util.UsedByNative;

Expand All @@ -43,7 +42,6 @@
@UsedByNative
public final class VideoFrameReleaseTimeHelper {

private static final double DISPLAY_REFRESH_RATE_UNKNOWN = -1;
private static final long CHOREOGRAPHER_SAMPLE_DELAY_MILLIS = 500;
private static final long MAX_ALLOWED_DRIFT_NS = 20000000;

Expand Down Expand Up @@ -73,11 +71,11 @@ public final class VideoFrameReleaseTimeHelper {
@SuppressWarnings("unused")
@UsedByNative
public VideoFrameReleaseTimeHelper() {
this(getDefaultDisplayRefreshRate());
this(DisplayUtil.getDefaultDisplayRefreshRate());
}

private VideoFrameReleaseTimeHelper(double defaultDisplayRefreshRate) {
useDefaultDisplayVsync = defaultDisplayRefreshRate != DISPLAY_REFRESH_RATE_UNKNOWN;
useDefaultDisplayVsync = defaultDisplayRefreshRate != DisplayUtil.DISPLAY_REFRESH_RATE_UNKNOWN;
if (useDefaultDisplayVsync) {
vsyncSampler = VSyncSampler.getInstance();
vsyncDurationNs = (long) (NANOS_PER_SECOND / defaultDisplayRefreshRate);
Expand Down Expand Up @@ -220,11 +218,6 @@ private static long closestVsync(long releaseTime, long sampledVsyncTime, long v
return snappedAfterDiff < snappedBeforeDiff ? snappedAfterNs : snappedBeforeNs;
}

private static double getDefaultDisplayRefreshRate() {
Display defaultDisplay = DisplayUtil.getDefaultDisplay();
return defaultDisplay != null ? defaultDisplay.getRefreshRate() : DISPLAY_REFRESH_RATE_UNKNOWN;
}

/**
* Samples display vsync timestamps. A single instance using a single {@link Choreographer} is
* shared by all {@link VideoFrameReleaseTimeHelper} instances. This is done to avoid a resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ private DisplayUtil() {}
private static Display defaultDisplay;
private static DisplayMetrics cachedDisplayMetrics = null;

public static final double DISPLAY_REFRESH_RATE_UNKNOWN = -1;

/** Returns the physical pixels per inch of the screen in the X and Y dimensions. */
public static SizeF getDisplayDpi() {
DisplayMetrics metrics = getDisplayMetrics();
Expand All @@ -52,6 +54,12 @@ public static Display getDefaultDisplay() {
}
}

/** Returns the refresh rate of the default display. */
public static double getDefaultDisplayRefreshRate() {
Display defaultDisplay = getDefaultDisplay();
return defaultDisplay != null ? defaultDisplay.getRefreshRate() : DISPLAY_REFRESH_RATE_UNKNOWN;
}

/**
* Cache the default display and display metrics, this will be triggered when a NativeActivity
* starts.
Expand Down

0 comments on commit 3725b01

Please sign in to comment.