Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
User dynamic clock levels on Oculus (#1062)
Browse files Browse the repository at this point in the history
* User dynamic clock levels on Oculus
* Use enum instead of boolean for CPULevel
  • Loading branch information
MortimerGoro authored Apr 10, 2019
1 parent 3e47cb1 commit 8b05b8d
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@
import java.util.HashMap;
import java.util.LinkedList;

import androidx.annotation.IntDef;
import androidx.annotation.Keep;
import androidx.annotation.NonNull;

public class VRBrowserActivity extends PlatformActivity implements WidgetManagerDelegate {
public class VRBrowserActivity extends PlatformActivity implements WidgetManagerDelegate, SessionStore.VideoAvailabilityListener {

private BroadcastReceiver mCrashReceiver = new BroadcastReceiver() {
@Override
Expand Down Expand Up @@ -148,6 +149,7 @@ protected void onCreate(Bundle savedInstanceState) {
Bundle extras = getIntent() != null ? getIntent().getExtras() : null;
SessionStore.get().setContext(this, extras);
SessionStore.get().registerListeners();
SessionStore.get().addVideoAvailabilityListener(this);

// Create broadcast receiver for getting crash messages from crash process
IntentFilter intentFilter = new IntentFilter();
Expand Down Expand Up @@ -305,6 +307,7 @@ protected void onDestroy() {
// Remove all widget listeners
mTray.removeAllListeners();
mBookmarksView.removeAllListeners();
SessionStore.get().removeVideoAvailabilityListener(this);

SessionStore.get().unregisterListeners();
super.onDestroy();
Expand Down Expand Up @@ -770,6 +773,12 @@ public void addWidgets(final Iterable<Widget> aWidgets) {
});
}

// VideoAvailabilityListener
@Override
public void onVideoAvailabilityChanged(boolean aVideosAvailable) {
queueRunnable(() -> setCPULevelNative(aVideosAvailable ? CPU_LEVEL_HIGH : CPU_LEVEL_NORMAL));
}

// WidgetManagerDelegate
@Override
public void addWidget(final Widget aWidget) {
Expand Down Expand Up @@ -1043,6 +1052,12 @@ public void setCylinderDensity(final float aDensity) {
private native void setControllersVisibleNative(boolean aVisible);
private native void runCallbackNative(long aCallback);
private native void setCylinderDensityNative(float aDensity);
private native void setCPULevelNative(@CPULevelFlags int aCPULevel);
private native void setIsServo(boolean aIsServo);
private native void updateFoveatedLevelNative(int appLevel, int webVRLevel);

@IntDef(value = { CPU_LEVEL_NORMAL, CPU_LEVEL_HIGH})
private @interface CPULevelFlags {}
private static final int CPU_LEVEL_NORMAL = 0;
private static final int CPU_LEVEL_HIGH = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public static SessionStore get() {
private LinkedList<SessionChangeListener> mSessionChangeListeners;
private LinkedList<GeckoSession.TextInputDelegate> mTextInputListeners;
private LinkedList<GeckoSession.PromptDelegate> mPromptListeners;
private LinkedList<VideoAvailabilityListener> mVideoAvailabilityListeners;
private UserAgentOverride mUserAgentOverride;

public interface SessionChangeListener {
Expand All @@ -89,6 +90,10 @@ public interface SessionChangeListener {
void onCurrentSessionChange(GeckoSession aSession, int aId);
}

public interface VideoAvailabilityListener {
void onVideoAvailabilityChanged(boolean aVideosAvailable);
}

class SessionSettings {
boolean multiprocess = SettingsStore.getInstance(mContext).isMultiprocessEnabled();
boolean privateMode = false;
Expand Down Expand Up @@ -139,6 +144,7 @@ public void registerListeners() {
mSessionChangeListeners = new LinkedList<>();
mTextInputListeners = new LinkedList<>();
mPromptListeners = new LinkedList<>();
mVideoAvailabilityListeners = new LinkedList<>();

if (mPrefs != null) {
mPrefs.registerOnSharedPreferenceChangeListener(this);
Expand All @@ -151,6 +157,7 @@ public void unregisterListeners() {
mContentListeners.clear();
mSessionChangeListeners.clear();
mTextInputListeners.clear();
mVideoAvailabilityListeners.clear();

if (mPrefs != null) {
mPrefs.unregisterOnSharedPreferenceChangeListener(this);
Expand Down Expand Up @@ -321,6 +328,14 @@ public void removePromptListener(GeckoSession.PromptDelegate aListener) {
mPromptListeners.remove(aListener);
}

public void addVideoAvailabilityListener(VideoAvailabilityListener aListener) {
mVideoAvailabilityListeners.add(aListener);
}

public void removeVideoAvailabilityListener(VideoAvailabilityListener aListener) {
mVideoAvailabilityListeners.remove(aListener);
}

public int createSession() {
return createSession(false);
}
Expand Down Expand Up @@ -1424,6 +1439,12 @@ public void onMediaAdd(@NonNull GeckoSession session, @NonNull MediaElement elem
}
Media media = new Media(element);
state.mMediaElements.add(media);

if (state.mMediaElements.size() == 1) {
for (VideoAvailabilityListener listener: mVideoAvailabilityListeners) {
listener.onVideoAvailabilityChanged(true);
}
}
}

@Override
Expand All @@ -1437,6 +1458,11 @@ public void onMediaRemove(@NonNull GeckoSession session, @NonNull MediaElement e
if (media.getMediaElement() == element) {
media.unload();
state.mMediaElements.remove(i);
if (state.mMediaElements.size() == 0) {
for (VideoAvailabilityListener listener: mVideoAvailabilityListeners) {
listener.onVideoAvailabilityChanged(false);
}
}
return;
}
}
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/cpp/BrowserWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,11 @@ BrowserWorld::SetCylinderDensity(const float aDensity) {
}
}

void
BrowserWorld::SetCPULevel(const device::CPULevel aLevel) {
m.device->SetCPULevel(aLevel);
}

void
BrowserWorld::SetIsServo(const bool aIsServo) {
m.externalVR->SetSourceBrowser(aIsServo ? ExternalVR::VRBrowserType::Servo : ExternalVR::VRBrowserType::Gecko);
Expand Down Expand Up @@ -1347,6 +1352,11 @@ JNI_METHOD(void, runCallbackNative)
}
}

JNI_METHOD(void, setCPULevelNative)
(JNIEnv* aEnv, jobject, int aCPULevel) {
crow::BrowserWorld::Instance().SetCPULevel(static_cast<crow::device::CPULevel>(aCPULevel));
}

JNI_METHOD(void, setIsServo)
(JNIEnv* aEnv, jobject, jboolean aIsServo) {
crow::BrowserWorld::Instance().SetIsServo(aIsServo);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/cpp/BrowserWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class BrowserWorld {
void ResetUIYaw();
void SetCylinderDensity(const float aDensity);
void SetIsServo(const bool aIsServo);
void SetCPULevel(const device::CPULevel aLevel);
JNIEnv* GetJNIEnv() const;
protected:
struct State;
Expand Down
1 change: 1 addition & 0 deletions app/src/main/cpp/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const CapabilityFlags MountDetection = 1 << 8;
const CapabilityFlags PositionEmulated = 1 << 9;
enum class Eye { Left, Right };
enum class RenderMode { StandAlone, Immersive };
enum class CPULevel { Normal = 0, High };
const int32_t EyeCount = 2;
inline int32_t EyeIndex(const Eye aEye) { return aEye == Eye::Left ? 0 : 1; }

Expand Down
1 change: 1 addition & 0 deletions app/src/main/cpp/DeviceDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class DeviceDelegate {
virtual void ReleaseControllerDelegate() = 0;
virtual int32_t GetControllerModelCount() const = 0;
virtual const std::string GetControllerModelName(const int32_t aModelIndex) const = 0;
virtual void SetCPULevel(const device::CPULevel aLevel) {};
virtual void ProcessEvents() = 0;
virtual void StartFrame() = 0;
virtual void BindEye(const device::Eye aWhich) = 0;
Expand Down
27 changes: 26 additions & 1 deletion app/src/oculusvr/cpp/DeviceDelegateOculusVR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ struct DeviceDelegateOculusVR::State {
ImmersiveDisplayPtr immersiveDisplay;
int reorientCount = -1;
vrb::Matrix reorientMatrix = vrb::Matrix::Identity();
device::CPULevel minCPULevel = device::CPULevel::Normal;


void UpdatePerspective() {
Expand Down Expand Up @@ -701,6 +702,18 @@ struct DeviceDelegateOculusVR::State {
}
}

void UpdateClockLevels() {
if (!ovr) {
return;
}

if (renderMode == device::RenderMode::StandAlone && minCPULevel == device::CPULevel::Normal) {
vrapi_SetClockLevels(ovr, 2, 2);
} else {
vrapi_SetClockLevels(ovr, 4, 4);
}
}

void AddUILayer(const OculusLayerPtr& aLayer, VRLayerSurface::SurfaceType aSurfaceType) {
if (ovr) {
vrb::RenderContextPtr ctx = context.lock();
Expand Down Expand Up @@ -734,6 +747,10 @@ struct DeviceDelegateOculusVR::State {
return deviceType >= VRAPI_DEVICE_TYPE_OCULUSQUEST_START && deviceType <= VRAPI_DEVICE_TYPE_OCULUSQUEST_END;
}

bool IsOculusGo() const {
return deviceType >= VRAPI_DEVICE_TYPE_OCULUSGO_START && deviceType <= VRAPI_DEVICE_TYPE_OCULUSGO_END;
}

bool Is6DOF() const {
// ovrInputHeadsetCapabilities is unavailable in Oculus mobile SDK,
// the current workaround is checking if it is Quest.
Expand Down Expand Up @@ -986,6 +1003,7 @@ DeviceDelegateOculusVR::SetRenderMode(const device::RenderMode aMode) {

m.UpdateTrackingMode();
m.UpdateFoveatedLevel();
m.UpdateClockLevels();

// Reset reorient when exiting or entering immersive
m.reorientMatrix = vrb::Matrix::Identity();
Expand Down Expand Up @@ -1102,6 +1120,13 @@ DeviceDelegateOculusVR::GetControllerModelName(const int32_t aModelIndex) const
return name;
}


void
DeviceDelegateOculusVR::SetCPULevel(const device::CPULevel aLevel) {
m.minCPULevel = aLevel;
m.UpdateClockLevels();
};

void
DeviceDelegateOculusVR::ProcessEvents() {

Expand Down Expand Up @@ -1442,9 +1467,9 @@ DeviceDelegateOculusVR::EnterVR(const crow::BrowserEGLContext& aEGLContext) {
if (!m.ovr) {
VRB_LOG("Entering VR mode failed");
} else {
vrapi_SetClockLevels(m.ovr, 4, 4);
vrapi_SetPerfThread(m.ovr, VRAPI_PERF_THREAD_TYPE_MAIN, gettid());
vrapi_SetPerfThread(m.ovr, VRAPI_PERF_THREAD_TYPE_RENDERER, gettid());
m.UpdateClockLevels();
m.UpdateTrackingMode();
m.UpdateFoveatedLevel();
}
Expand Down
1 change: 1 addition & 0 deletions app/src/oculusvr/cpp/DeviceDelegateOculusVR.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class DeviceDelegateOculusVR : public DeviceDelegate {
void SetFoveatedLevel(const int32_t aAppLevel, const int32_t aWebVRLevel) override;
int32_t GetControllerModelCount() const override;
const std::string GetControllerModelName(const int32_t aModelIndex) const override;
void SetCPULevel(const device::CPULevel aLevel) override;
void ProcessEvents() override;
void StartFrame() override;
void BindEye(const device::Eye aWhich) override;
Expand Down

0 comments on commit 8b05b8d

Please sign in to comment.