Skip to content

Commit

Permalink
Add UI texture scaling (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro authored and bluemarvin committed Apr 9, 2019
1 parent ef9c090 commit 3e47cb1
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 12 deletions.
19 changes: 13 additions & 6 deletions app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java
Expand Up @@ -475,8 +475,10 @@ public void onFrameAvailable(SurfaceTexture surfaceTexture) {
}
widget.setSurfaceTexture(aTexture, aWidth, aHeight);
// Add widget to a virtual display for invalidation
if (((View)widget).getParent() == null) {
mWidgetContainer.addView((View) widget, new FrameLayout.LayoutParams(aWidth, aHeight));
View view = (View) widget;
if (view.getParent() == null) {
float scale = widget.getPlacement().textureScale;
mWidgetContainer.addView(view, new FrameLayout.LayoutParams((int) Math.ceil(aWidth / scale), (int) Math.ceil(aHeight / scale)));
}
});
}
Expand Down Expand Up @@ -507,7 +509,8 @@ void dispatchCreateWidgetLayer(final int aHandle, final Surface aSurface, final
View view = (View) widget;
// Add widget to a virtual display for invalidation
if (aSurface != null && view.getParent() == null) {
mWidgetContainer.addView(view, new FrameLayout.LayoutParams(aWidth, aHeight));
float scale = widget.getPlacement().textureScale;
mWidgetContainer.addView(view, new FrameLayout.LayoutParams((int) Math.ceil(aWidth / scale), (int) Math.ceil(aHeight / scale)));
} else if (aSurface == null && view.getParent() != null) {
mWidgetContainer.removeView(view);
}
Expand All @@ -520,13 +523,17 @@ void dispatchCreateWidgetLayer(final int aHandle, final Surface aSurface, final
void handleMotionEvent(final int aHandle, final int aDevice, final boolean aPressed, final float aX, final float aY) {
runOnUiThread(() -> {
Widget widget = mWidgets.get(aHandle);
float scale = widget != null ? widget.getPlacement().textureScale : 1.0f;
final float x = aX / scale;
final float y = aY / scale;

if (widget == null) {
MotionEventGenerator.dispatch(mRootWidget, aDevice, aPressed, aX, aY);
MotionEventGenerator.dispatch(mRootWidget, aDevice, aPressed, x, y);
} else if (widget == mWindowWidget && mWindowWidget.getBorderWidth() > 0) {
final int border = mWindowWidget.getBorderWidth();
MotionEventGenerator.dispatch(widget, aDevice, aPressed, aX - border, aY - border);
MotionEventGenerator.dispatch(widget, aDevice, aPressed, x - border, y - border);
} else {
MotionEventGenerator.dispatch(widget, aDevice, aPressed, aX, aY);
MotionEventGenerator.dispatch(widget, aDevice, aPressed, x, y);
}
});
}
Expand Down
Expand Up @@ -203,6 +203,7 @@ protected void initializeWidgetPlacement(WidgetPlacement aPlacement) {
aPlacement.rotation = (float)Math.toRadians(-45);
aPlacement.opaque = false;
aPlacement.cylinder = false;
aPlacement.textureScale = 1.0f;
}

@Override
Expand Down
Expand Up @@ -40,6 +40,7 @@ public WidgetPlacement(Context aContext) {
public boolean firstDraw = false;
public boolean layer = true;
public boolean cylinder = true;
public float textureScale = 0.7f;

public WidgetPlacement clone() {
WidgetPlacement w = new WidgetPlacement();
Expand Down Expand Up @@ -70,6 +71,7 @@ public void copyFrom(WidgetPlacement w) {
this.firstDraw = w.firstDraw;
this.layer = w.layer;
this.cylinder = w.cylinder;
this.textureScale = w.textureScale;
}

public int textureWidth() {
Expand Down
Expand Up @@ -96,6 +96,7 @@ protected void initializeWidgetPlacement(WidgetPlacement aPlacement) {
aPlacement.anchorY = 0.0f;
aPlacement.visible = true;
aPlacement.cylinder = true;
aPlacement.textureScale = 1.0f;
}

@Override
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/cpp/BrowserWorld.cpp
Expand Up @@ -713,8 +713,8 @@ BrowserWorld::AddWidget(int32_t aHandle, const WidgetPlacementPtr& aPlacement) {
worldWidth = aPlacement->width * kWorldDPIRatio;
}

int32_t textureWidth = (int32_t)(ceilf(aPlacement->width * aPlacement->density));
int32_t textureHeight = (int32_t)(ceilf(aPlacement->height * aPlacement->density));
const int32_t textureWidth = aPlacement->GetTextureWidth();
const int32_t textureHeight = aPlacement->GetTextureHeight();

const float aspect = (float)textureWidth / (float)textureHeight;
const float worldHeight = worldWidth / aspect;
Expand Down Expand Up @@ -765,8 +765,7 @@ BrowserWorld::UpdateWidget(int32_t aHandle, const WidgetPlacementPtr& aPlacement
widget->SetPlacement(aPlacement);
widget->SetCylinderDensity(m.cylinderDensity);
widget->ToggleWidget(aPlacement->visible);
widget->SetSurfaceTextureSize((int32_t)(ceilf(aPlacement->width * aPlacement->density)),
(int32_t)(ceilf(aPlacement->height * aPlacement->density)));
widget->SetSurfaceTextureSize(aPlacement->GetTextureWidth(), aPlacement->GetTextureHeight());

float worldWidth = 0.0f, worldHeight = 0.0f;
widget->GetWorldSize(worldWidth, worldHeight);
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/cpp/Widget.cpp
Expand Up @@ -130,8 +130,8 @@ struct Widget::State {
cylinder->GetTextureSize(textureWidth, textureHeight);

const float radius = cylinder->GetCylinderRadius();
const float surfaceWidth = (float)textureWidth / placement->density;
const float surfaceHeight = (float)textureHeight / placement->density;
const float surfaceWidth = (float)textureWidth / placement->density * placement->textureScale;
const float surfaceHeight = (float)textureHeight / placement->density * placement->textureScale;
// Cylinder density measures the pixels for a 360 cylinder
// Oculus recommends 4680px density, which is 13 pixels per degree.
const float theta = (float)M_PI * surfaceWidth / (cylinderDensity * 0.5f);
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/cpp/WidgetPlacement.cpp
Expand Up @@ -54,8 +54,19 @@ WidgetPlacement::FromJava(JNIEnv* aEnv, jobject& aObject) {
GET_BOOLEAN_FIELD(firstDraw);
GET_BOOLEAN_FIELD(layer);
GET_BOOLEAN_FIELD(cylinder);
GET_FLOAT_FIELD(textureScale, "textureScale");

return result;
}

int32_t
WidgetPlacement::GetTextureWidth() const{
return (int32_t)ceilf(width * density * textureScale);
}

int32_t
WidgetPlacement::GetTextureHeight() const {
return (int32_t)ceilf(height * density * textureScale);
}

}
4 changes: 4 additions & 0 deletions app/src/main/cpp/WidgetPlacement.h
Expand Up @@ -32,6 +32,10 @@ struct WidgetPlacement {
bool firstDraw;
bool layer;
bool cylinder;
float textureScale;

int32_t GetTextureWidth() const;
int32_t GetTextureHeight() const;

static WidgetPlacementPtr FromJava(JNIEnv* aEnv, jobject& aObject);
private:
Expand Down

0 comments on commit 3e47cb1

Please sign in to comment.