-
public class UiHelperUiHelper is a simple class that can manage either a SurfaceView, TextureView, or a SurfaceHolder so it can be used to render into with Filament. Here is a simple example with a SurfaceView. The code would be exactly the same with a TextureView:
public class FilamentActivity extends Activity { private UiHelper mUiHelper; private SurfaceView mSurfaceView; // Filament specific APIs private Engine mEngine; private Renderer mRenderer; private View mView; // com.google.android.filament.View, not android.view.View private SwapChain mSwapChain; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create a SurfaceView and add it to the activity mSurfaceView = new SurfaceView(this); setContentView(mSurfaceView); // Create the Filament UI helper mUiHelper = new UiHelper(UiHelper.ContextErrorPolicy.DONT_CHECK); // Attach the SurfaceView to the helper, you could do the same with a TextureView mUiHelper.attachTo(mSurfaceView); // Set a rendering callback that we will use to invoke Filament mUiHelper.setRenderCallback(new UiHelper.RendererCallback() { public void onNativeWindowChanged(Surface surface) { if (mSwapChain != null) mEngine.destroySwapChain(mSwapChain); mSwapChain = mEngine.createSwapChain(surface, mUiHelper.getSwapChainFlags()); } // The native surface went away, we must stop rendering. public void onDetachedFromSurface() { if (mSwapChain != null) { mEngine.destroySwapChain(mSwapChain); // Required to ensure we don't return before Filament is done executing the // destroySwapChain command, otherwise Android might destroy the Surface // too early mEngine.flushAndWait(); mSwapChain = null; } } // The native surface has changed size. This is always called at least once // after the surface is created (after onNativeWindowChanged() is invoked). public void onResized(int width, int height) { // Wait for all pending frames to be processed before returning. This is to // avoid a race between the surface being resized before pending frames are // rendered into it. Fence fence = mEngine.createFence(); fence.wait(Fence.Mode.FLUSH, Fence.WAIT_FOR_EVER); mEngine.destroyFence(fence); // Compute camera projection and set the viewport on the view } }); mEngine = Engine.create(); mRenderer = mEngine.createRenderer(); mView = mEngine.createView(); // Create scene, camera, etc. } public void onDestroy() { super.onDestroy(); // Always detach the surface before destroying the engine mUiHelper.detach(); mEngine.destroy(); } // This is an example of a render function. You will most likely invoke this from // a Choreographer callback to trigger rendering at vsync. public void render() { if (mUiHelper.isReadyToRender) { // If beginFrame() returns false you should skip the frame // This means you are sending frames too quickly to the GPU if (mRenderer.beginFrame(swapChain)) { mRenderer.render(mView); mRenderer.endFrame(); } } } }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public enumUiHelper.ContextErrorPolicyEnum used to decide whether UiHelper should perform extra error checking.
public interfaceUiHelper.RendererCallbackInterface used to know when the native surface is created, destroyed or resized.
-
Constructor Summary
Constructors Constructor Description UiHelper()Creates a UiHelper which will help manage the native surface provided by aSurfaceView or a TextureView. UiHelper(UiHelper.ContextErrorPolicy policy)Creates a UiHelper which will help manage the native surface provided by aSurfaceView or a TextureView.
-
Method Summary
Modifier and Type Method Description voidsetRenderCallback(@Nullable() UiHelper.RendererCallback renderCallback)Sets the renderer callback that will be notified when the native surface iscreated, destroyed or resized. UiHelper.RendererCallbackgetRenderCallback()Returns the current render callback associated with this UiHelper. voiddetach()Free resources associated to the native window specified in attachTo, attachTo, or attachTo. booleanisReadyToRender()Checks whether we are ready to render into the attached surface.Using OpenGL ES when this returns true, will result in drawing commands being lost,HOWEVER, GLES state will be preserved. voidsetDesiredSize(int width, int height)Set the size of the render target buffers of the native surface. intgetDesiredWidth()Returns the requested width for the native surface. intgetDesiredHeight()Returns the requested height for the native surface. booleanisOpaque()Returns true if the render target is opaque. voidsetOpaque(boolean opaque)Controls whether the render target (SurfaceView or TextureView) is opaque or not.The render target is considered opaque by default. booleanisMediaOverlay()Returns true if the SurfaceView used as a render target should be positioned aboveother surfaces but below the activity's surface. voidsetMediaOverlay(boolean overlay)Controls whether the surface of the SurfaceView used as a render target should bepositioned above other surfaces but below the activity's surface. longgetSwapChainFlags()Returns the flags to pass to createSwapChain to honor allthe options set on this UiHelper. voidattachTo(@NonNull() SurfaceView view)Associate UiHelper with a SurfaceView.As soon as SurfaceView is ready (i.e. voidattachTo(@NonNull() TextureView view)Associate UiHelper with a TextureView.As soon as TextureView is ready (i.e. voidattachTo(@NonNull() SurfaceHolder holder)Associate UiHelper with a SurfaceHolder.As soon as a Surface is created, we'll create theEGL resources needed, and call user callbacks if needed. -
-
Constructor Detail
-
UiHelper
UiHelper()
Creates a UiHelper which will help manage the native surface provided by aSurfaceView or a TextureView.
-
UiHelper
UiHelper(UiHelper.ContextErrorPolicy policy)
Creates a UiHelper which will help manage the native surface provided by aSurfaceView or a TextureView.- Parameters:
policy- The error checking policy to use.
-
-
Method Detail
-
setRenderCallback
void setRenderCallback(@Nullable() UiHelper.RendererCallback renderCallback)
Sets the renderer callback that will be notified when the native surface iscreated, destroyed or resized.
- Parameters:
renderCallback- The callback to register.
-
getRenderCallback
@Nullable() UiHelper.RendererCallback getRenderCallback()
Returns the current render callback associated with this UiHelper.
-
detach
void detach()
-
isReadyToRender
boolean isReadyToRender()
Checks whether we are ready to render into the attached surface.Using OpenGL ES when this returns true, will result in drawing commands being lost,HOWEVER, GLES state will be preserved. This is useful to initialize the engine.
-
setDesiredSize
void setDesiredSize(int width, int height)
Set the size of the render target buffers of the native surface.
-
getDesiredWidth
int getDesiredWidth()
Returns the requested width for the native surface.
-
getDesiredHeight
int getDesiredHeight()
Returns the requested height for the native surface.
-
isOpaque
boolean isOpaque()
Returns true if the render target is opaque.
-
setOpaque
void setOpaque(boolean opaque)
Controls whether the render target (SurfaceView or TextureView) is opaque or not.The render target is considered opaque by default.Must be called before calling attachTo, attachTo,or attachTo.
- Parameters:
opaque- Indicates whether the render target should be opaque.
-
isMediaOverlay
boolean isMediaOverlay()
Returns true if the SurfaceView used as a render target should be positioned aboveother surfaces but below the activity's surface. False by default.
-
setMediaOverlay
void setMediaOverlay(boolean overlay)
Controls whether the surface of the SurfaceView used as a render target should bepositioned above other surfaces but below the activity's surface. This propertyonly has an effect when used in combination with setOpaque(false) and does not affect TextureView targets.Must be called before calling attachTo or attachTo.Has no effect when using attachTo.
- Parameters:
overlay- Indicates whether the render target should be rendered below the activity'ssurface when transparent.
-
getSwapChainFlags
long getSwapChainFlags()
Returns the flags to pass to createSwapChain to honor allthe options set on this UiHelper.
-
attachTo
void attachTo(@NonNull() SurfaceView view)
Associate UiHelper with a SurfaceView.As soon as SurfaceView is ready (i.e. has a Surface), we'll create theEGL resources needed, and call user callbacks if needed.
-
attachTo
void attachTo(@NonNull() TextureView view)
Associate UiHelper with a TextureView.As soon as TextureView is ready (i.e. has a buffer), we'll create theEGL resources needed, and call user callbacks if needed.
-
attachTo
void attachTo(@NonNull() SurfaceHolder holder)
Associate UiHelper with a SurfaceHolder.As soon as a Surface is created, we'll create theEGL resources needed, and call user callbacks if needed.
-
-
-
-