Skip to content

Commit

Permalink
Fix Android samples so they handle surface resizes properly
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelflinger committed Apr 21, 2023
1 parent 7b90a8f commit c24cc72
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.filament.android;

import com.google.android.filament.Engine;
import com.google.android.filament.Fence;

public class FilamentHelper {

/**
* 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. This is typically called from {@link UiHelper.RendererCallback#onResized},
* {@link android.view.SurfaceHolder.Callback#surfaceChanged} or
* {@link android.view.TextureView.SurfaceTextureListener#onSurfaceTextureSizeChanged}.
*
* @param engine Filament engine to synchronize
*
* @see UiHelper.RendererCallback#onResized
* @see android.view.SurfaceHolder.Callback#surfaceChanged
* @see android.view.TextureView.SurfaceTextureListener#onSurfaceTextureSizeChanged
*/
static public void synchronizePendingFrames(Engine engine) {
Fence fence = engine.createFence();
fence.wait(Fence.Mode.FLUSH, Fence.WAIT_FOR_EVER);
engine.destroyFence(fence);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@
* // 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
* }
* });
Expand Down Expand Up @@ -175,7 +183,7 @@ private interface RenderSurface {
}

private static class SurfaceViewHandler implements RenderSurface {
private SurfaceView mSurfaceView;
private final SurfaceView mSurfaceView;

SurfaceViewHandler(SurfaceView surface) {
mSurfaceView = surface;
Expand All @@ -192,7 +200,7 @@ public void detach() {
}

private static class SurfaceHolderHandler implements RenderSurface {
private SurfaceHolder mSurfaceHolder;
private final SurfaceHolder mSurfaceHolder;

SurfaceHolderHandler(SurfaceHolder surface) {
mSurfaceHolder = surface;
Expand All @@ -209,7 +217,7 @@ public void detach() {
}

private class TextureViewHandler implements RenderSurface {
private TextureView mTextureView;
private final TextureView mTextureView;
private Surface mSurface;

TextureViewHandler(TextureView surface) { mTextureView = surface; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,19 @@ class ModelViewer(
view.viewport = Viewport(0, 0, width, height)
cameraManipulator.setViewport(width, height)
updateCameraProjection()
synchronizePendingFrames(engine)
}
}

private fun synchronizePendingFrames(engine: Engine) {
// 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.
val fence = engine.createFence()
fence.wait(Fence.Mode.FLUSH, Fence.WAIT_FOR_EVER)
engine.destroyFence(fence)
}

companion object {
private val kDefaultObjectPosition = Float3(0.0f, 0.0f, -4.0f)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.google.android.filament.*
import com.google.android.filament.RenderableManager.*
import com.google.android.filament.VertexBuffer.*
import com.google.android.filament.android.DisplayHelper
import com.google.android.filament.android.FilamentHelper
import com.google.android.filament.android.UiHelper

import java.nio.ByteBuffer
Expand Down Expand Up @@ -401,6 +402,8 @@ class MainActivity : Activity(), ActivityCompat.OnRequestPermissionsResultCallba
camera.setProjection(45.0, aspect, 0.1, 20.0, Camera.Fov.VERTICAL)

view.viewport = Viewport(0, 0, width, height)

FilamentHelper.synchronizePendingFrames(engine)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.google.android.filament.RenderableManager.PrimitiveType
import com.google.android.filament.VertexBuffer.AttributeType
import com.google.android.filament.VertexBuffer.VertexAttribute
import com.google.android.filament.android.DisplayHelper
import com.google.android.filament.android.FilamentHelper
import com.google.android.filament.android.UiHelper
import java.nio.ByteBuffer
import java.nio.ByteOrder
Expand Down Expand Up @@ -327,6 +328,8 @@ class MainActivity : Activity() {
-aspect * zoom, aspect * zoom, -zoom, zoom, 0.0, 10.0)

view.viewport = Viewport(0, 0, width, height)

FilamentHelper.synchronizePendingFrames(engine)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import android.view.animation.LinearInterpolator

import com.google.android.filament.*
import com.google.android.filament.android.DisplayHelper
import com.google.android.filament.android.FilamentHelper
import com.google.android.filament.android.UiHelper

import java.nio.ByteBuffer
Expand Down Expand Up @@ -303,6 +304,8 @@ class MainActivity : Activity() {
camera.setProjection(45.0, aspect, 0.1, 20.0, Camera.Fov.VERTICAL)

view.viewport = Viewport(0, 0, width, height)

FilamentHelper.synchronizePendingFrames(engine)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.google.android.filament.*
import com.google.android.filament.RenderableManager.*
import com.google.android.filament.VertexBuffer.*
import com.google.android.filament.android.DisplayHelper
import com.google.android.filament.android.FilamentHelper
import com.google.android.filament.android.UiHelper

import java.nio.ByteBuffer
Expand Down Expand Up @@ -413,6 +414,8 @@ class MainActivity : Activity() {
camera.setProjection(45.0, aspect, 0.1, 20.0, Camera.Fov.VERTICAL)

view.viewport = Viewport(0, 0, width, height)

FilamentHelper.synchronizePendingFrames(engine)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import android.view.animation.LinearInterpolator
import androidx.annotation.RequiresApi
import com.google.android.filament.*
import com.google.android.filament.android.DisplayHelper
import com.google.android.filament.android.FilamentHelper
import com.google.android.filament.android.UiHelper

class FilamentLiveWallpaper : WallpaperService() {
Expand Down Expand Up @@ -226,6 +227,8 @@ class FilamentLiveWallpaper : WallpaperService() {
camera.setProjection(45.0, aspect, 0.1, 20.0, Camera.Fov.VERTICAL)

view.viewport = Viewport(0, 0, width, height)

FilamentHelper.synchronizePendingFrames(engine)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import android.view.animation.LinearInterpolator

import com.google.android.filament.*
import com.google.android.filament.android.DisplayHelper
import com.google.android.filament.android.FilamentHelper
import com.google.android.filament.android.UiHelper
import com.google.android.filament.filamat.MaterialBuilder

Expand Down Expand Up @@ -343,6 +344,8 @@ class MainActivity : Activity() {
camera.setProjection(45.0, aspect, 0.1, 20.0, Camera.Fov.VERTICAL)

view.viewport = Viewport(0, 0, width, height)

FilamentHelper.synchronizePendingFrames(engine)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.google.android.filament.RenderableManager.*
import com.google.android.filament.Renderer.ClearOptions
import com.google.android.filament.VertexBuffer.*
import com.google.android.filament.android.DisplayHelper
import com.google.android.filament.android.FilamentHelper
import com.google.android.filament.android.UiHelper

import java.nio.ByteBuffer
Expand Down Expand Up @@ -458,6 +459,8 @@ class MainActivity : Activity() {
view1.viewport = Viewport(width / 2, 0, width / 2, height / 2)
view2.viewport = Viewport(0, height / 2, width / 2, height / 2)
view4.viewport = Viewport(width / 4, height / 4, width / 2, height / 2)

FilamentHelper.synchronizePendingFrames(engine)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.google.android.filament.Engine;
import com.google.android.filament.Entity;
import com.google.android.filament.EntityManager;
import com.google.android.filament.Fence;
import com.google.android.filament.Filament;
import com.google.android.filament.IndirectLight;
import com.google.android.filament.LightManager;
Expand All @@ -49,6 +50,7 @@
import com.google.android.filament.Viewport;

import com.google.android.filament.android.DisplayHelper;
import com.google.android.filament.android.FilamentHelper;
import com.google.android.filament.android.TextureHelper;
import com.google.android.filament.android.UiHelper;

Expand Down Expand Up @@ -107,6 +109,7 @@ public void onResized(int width, int height) {
mCamera.setProjection(60.0, aspect, 1.0, 2000.0, Camera.Fov.HORIZONTAL);
}
mView.setViewport(new Viewport(0, 0, width, height));
FilamentHelper.synchronizePendingFrames(mEngine);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import android.os.Build
import android.view.MotionEvent
import androidx.annotation.RequiresApi
import com.google.android.filament.android.DisplayHelper
import com.google.android.filament.android.FilamentHelper


class MainActivity : Activity(), ActivityCompat.OnRequestPermissionsResultCallback {
Expand Down Expand Up @@ -401,6 +402,8 @@ class MainActivity : Activity(), ActivityCompat.OnRequestPermissionsResultCallba
camera.setProjection(45.0, aspect, 0.1, 20.0, Camera.Fov.VERTICAL)

view.viewport = Viewport(0, 0, width, height)

FilamentHelper.synchronizePendingFrames(engine)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.google.android.filament.RenderableManager.PrimitiveType
import com.google.android.filament.VertexBuffer.AttributeType
import com.google.android.filament.VertexBuffer.VertexAttribute
import com.google.android.filament.android.DisplayHelper
import com.google.android.filament.android.FilamentHelper
import com.google.android.filament.android.UiHelper
import java.nio.ByteBuffer
import java.nio.ByteOrder
Expand Down Expand Up @@ -327,6 +328,8 @@ class MainActivity : Activity() {
-aspect * zoom, aspect * zoom, -zoom, zoom, 0.0, 10.0)

view.viewport = Viewport(0, 0, width, height)

FilamentHelper.synchronizePendingFrames(engine)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import android.view.animation.LinearInterpolator

import com.google.android.filament.*
import com.google.android.filament.android.DisplayHelper
import com.google.android.filament.android.FilamentHelper
import com.google.android.filament.utils.*
import com.google.android.filament.android.UiHelper

Expand Down Expand Up @@ -323,6 +324,8 @@ class MainActivity : Activity() {
camera.setProjection(45.0, aspect, 0.1, 20.0, Camera.Fov.VERTICAL)

view.viewport = Viewport(0, 0, width, height)

FilamentHelper.synchronizePendingFrames(engine)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.google.android.filament.*
import com.google.android.filament.RenderableManager.*
import com.google.android.filament.VertexBuffer.*
import com.google.android.filament.android.DisplayHelper
import com.google.android.filament.android.FilamentHelper
import com.google.android.filament.android.UiHelper

import java.nio.ByteBuffer
Expand Down Expand Up @@ -350,6 +351,8 @@ class MainActivity : Activity() {
-aspect * zoom, aspect * zoom, -zoom, zoom, 0.0, 10.0)

view.viewport = Viewport(0, 0, width, height)

FilamentHelper.synchronizePendingFrames(engine)
}
}

Expand Down

0 comments on commit c24cc72

Please sign in to comment.