This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a Virtual display to host UI Widgets
- Loading branch information
1 parent
3ba2813
commit 261001a
Showing
5 changed files
with
175 additions
and
80 deletions.
There are no files selected for viewing
This file contains 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 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
98 changes: 98 additions & 0 deletions
98
app/src/common/shared/org/mozilla/vrbrowser/ui/OffscreenDisplay.java
This file contains 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,98 @@ | ||
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.vrbrowser.ui; | ||
|
||
import android.app.Presentation; | ||
import android.content.Context; | ||
import android.content.DialogInterface; | ||
import android.graphics.SurfaceTexture; | ||
import android.hardware.display.DisplayManager; | ||
import android.hardware.display.VirtualDisplay; | ||
import android.os.Bundle; | ||
import android.util.DisplayMetrics; | ||
import android.view.Display; | ||
import android.view.LayoutInflater; | ||
import android.view.Surface; | ||
import android.view.View; | ||
|
||
public class OffscreenDisplay { | ||
private Context mContext; | ||
private VirtualDisplay mVirtualDisplay; | ||
private SurfaceTexture mTexture; | ||
private Surface mSurface; | ||
private OffscreenPresentation mPresentation; | ||
|
||
private DisplayMetrics mDefaultMetrics; | ||
|
||
public OffscreenDisplay(Context aContext, SurfaceTexture aTexture, int aWidth, int aHeight) { | ||
mContext = aContext; | ||
aTexture.setDefaultBufferSize(aWidth, aHeight); | ||
mSurface = new Surface(aTexture); | ||
|
||
DisplayManager manager = (DisplayManager) aContext.getSystemService(Context.DISPLAY_SERVICE); | ||
Display defaultDisplay = manager.getDisplay(Display.DEFAULT_DISPLAY); | ||
|
||
mDefaultMetrics = new DisplayMetrics(); | ||
defaultDisplay.getMetrics(mDefaultMetrics); | ||
|
||
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | | ||
DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION | | ||
DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC; | ||
|
||
mVirtualDisplay = manager.createVirtualDisplay("OffscreenViews", aWidth, aHeight, | ||
mDefaultMetrics.densityDpi, mSurface, flags); | ||
|
||
mPresentation = new OffscreenPresentation(mContext, mVirtualDisplay.getDisplay()); | ||
mPresentation.show(); | ||
} | ||
|
||
public void setContentView(View aView) { | ||
if (mPresentation == null) { | ||
throw new IllegalStateException("No presentation!"); | ||
} | ||
|
||
mPresentation.setContentView(aView); | ||
} | ||
|
||
public void resize(int aWidth, int aHeight) { | ||
if (mVirtualDisplay == null) { | ||
throw new IllegalStateException("No virtual display!"); | ||
} | ||
|
||
mVirtualDisplay.resize(aWidth, aHeight, mDefaultMetrics.densityDpi); | ||
} | ||
|
||
public void release() { | ||
if (mPresentation != null) { | ||
mPresentation.dismiss(); | ||
mPresentation = null; | ||
} | ||
|
||
if (mVirtualDisplay != null) { | ||
mVirtualDisplay.release(); | ||
mVirtualDisplay = null; | ||
} | ||
|
||
if (mSurface != null) { | ||
mSurface.release(); | ||
} | ||
|
||
if (mTexture != null) { | ||
mTexture.release(); | ||
} | ||
} | ||
|
||
class OffscreenPresentation extends Presentation { | ||
OffscreenPresentation(Context context, Display display) { | ||
super(context, display); | ||
} | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
} | ||
} | ||
} |
This file contains 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 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