diff --git a/app/src/main/java/com/fpvout/digiview/MainActivity.java b/app/src/main/java/com/fpvout/digiview/MainActivity.java index 7f4c7a4..3e7e4fd 100644 --- a/app/src/main/java/com/fpvout/digiview/MainActivity.java +++ b/app/src/main/java/com/fpvout/digiview/MainActivity.java @@ -25,6 +25,8 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.preference.PreferenceManager; +import com.fpvout.digiview.vr.views.VrView; + import java.util.HashMap; import io.sentry.SentryLevel; @@ -113,7 +115,9 @@ public void onClick(View v) { mUsbMaskConnection = new UsbMaskConnection(); Handler videoReaderEventListener = new Handler(this.getMainLooper(), msg -> onVideoReaderEvent((VideoReaderExoplayer.VideoReaderEventMessageCode) msg.obj)); - mVideoReader = new VideoReaderExoplayer(fpvView, this, videoReaderEventListener); + VrView vrView = findViewById(R.id.vr_view); + + mVideoReader = new VideoReaderExoplayer(fpvView, vrView, this, videoReaderEventListener); if (!usbConnected) { if (searchDevice()) { diff --git a/app/src/main/java/com/fpvout/digiview/VideoReaderExoplayer.java b/app/src/main/java/com/fpvout/digiview/VideoReaderExoplayer.java index 23c795e..2cde883 100644 --- a/app/src/main/java/com/fpvout/digiview/VideoReaderExoplayer.java +++ b/app/src/main/java/com/fpvout/digiview/VideoReaderExoplayer.java @@ -2,16 +2,27 @@ import android.content.Context; import android.content.SharedPreferences; +import android.graphics.SurfaceTexture; import android.net.Uri; +import android.opengl.GLES20; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.util.Log; +import android.view.Surface; +import android.view.SurfaceHolder; import android.view.SurfaceView; +import android.view.View; +import androidx.annotation.NonNull; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.preference.PreferenceManager; +import com.fpvout.digiview.vr.gles.EglCore; +import com.fpvout.digiview.vr.gles.FullFrameRect; +import com.fpvout.digiview.vr.gles.Texture2dProgram; +import com.fpvout.digiview.vr.gles.WindowSurface; +import com.fpvout.digiview.vr.views.VrView; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.DefaultLoadControl; import com.google.android.exoplayer2.ExoPlaybackException; @@ -37,22 +48,30 @@ public class VideoReaderExoplayer { private SimpleExoPlayer mPlayer; static final String VideoPreset = "VideoPreset"; private final SurfaceView surfaceView; + private final VrView vrView; private AndroidUSBInputStream inputStream; - private UsbMaskConnection mUsbMaskConnection; + private UsbMaskConnection mUsbMaskConnection; private boolean zoomedIn; private final Context context; private PerformancePreset performancePreset = PerformancePreset.getPreset(PerformancePreset.PresetType.DEFAULT); static final String VideoZoomedIn = "VideoZoomedIn"; + static final String VREnabled = "VREnabled"; private final SharedPreferences sharedPreferences; - VideoReaderExoplayer(SurfaceView videoSurface, Context c) { + VideoReaderExoplayer(SurfaceView videoSurface, + VrView vrView, + Context c) { surfaceView = videoSurface; + this.vrView = vrView; context = c; sharedPreferences = PreferenceManager.getDefaultSharedPreferences(c); } - VideoReaderExoplayer(SurfaceView videoSurface, Context c, Handler v) { - this(videoSurface, c); + VideoReaderExoplayer(SurfaceView videoSurface, + VrView vrView, + Context c, + Handler v) { + this(videoSurface, vrView, c); videoReaderEventListener = v; } @@ -63,87 +82,97 @@ public void setUsbMaskConnection(UsbMaskConnection connection) { public void start() { zoomedIn = sharedPreferences.getBoolean(VideoZoomedIn, true); + boolean vrEnabled = sharedPreferences.getBoolean(VREnabled, false); performancePreset = PerformancePreset.getPreset(sharedPreferences.getString(VideoPreset, "default")); - DefaultLoadControl loadControl = new DefaultLoadControl.Builder().setBufferDurationsMs(performancePreset.exoPlayerMinBufferMs, performancePreset.exoPlayerMaxBufferMs, performancePreset.exoPlayerBufferForPlaybackMs, performancePreset.exoPlayerBufferForPlaybackAfterRebufferMs).build(); - mPlayer = new SimpleExoPlayer.Builder(context).setLoadControl(loadControl).build(); + DefaultLoadControl loadControl = new DefaultLoadControl.Builder().setBufferDurationsMs(performancePreset.exoPlayerMinBufferMs, performancePreset.exoPlayerMaxBufferMs, performancePreset.exoPlayerBufferForPlaybackMs, performancePreset.exoPlayerBufferForPlaybackAfterRebufferMs).build(); + mPlayer = new SimpleExoPlayer.Builder(context).setLoadControl(loadControl).build(); + if (vrEnabled) { + vrView.setVisibility(View.VISIBLE); + vrView.start(mPlayer); + } else { + vrView.stop(); + vrView.setVisibility(View.GONE); mPlayer.setVideoSurfaceView(surfaceView); - mPlayer.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING); - mPlayer.setWakeMode(C.WAKE_MODE_LOCAL); + } - DataSpec dataSpec = new DataSpec(Uri.EMPTY, 0, C.LENGTH_UNSET); + mPlayer.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING); + mPlayer.setWakeMode(C.WAKE_MODE_LOCAL); - Log.d(TAG, "preset: " + performancePreset); + DataSpec dataSpec = new DataSpec(Uri.EMPTY, 0, C.LENGTH_UNSET); - DataSource.Factory dataSourceFactory = () -> { - switch (performancePreset.dataSourceType){ - case INPUT_STREAM: - return (DataSource) new InputStreamDataSource(context, dataSpec, inputStream); - case BUFFERED_INPUT_STREAM: - default: - return (DataSource) new InputStreamBufferedDataSource(context, dataSpec, inputStream); - } - }; - - ExtractorsFactory extractorsFactory = () ->new Extractor[] {new H264Extractor(performancePreset.h264ReaderMaxSyncFrameSize, performancePreset.h264ReaderSampleTime)}; - MediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory, extractorsFactory).createMediaSource(MediaItem.fromUri(Uri.EMPTY)); - mPlayer.setMediaSource(mediaSource); - - mPlayer.prepare(); - mPlayer.play(); - mPlayer.addListener(new ExoPlayer.EventListener() { - @Override - @NonNullApi - public void onPlayerError(ExoPlaybackException error) { - switch (error.type) { - case ExoPlaybackException.TYPE_SOURCE: - Log.e(TAG, "PLAYER_SOURCE - TYPE_SOURCE: " + error.getSourceException().getMessage()); - (new Handler(Looper.getMainLooper())).postDelayed(() -> restart(), 1000); - break; - case ExoPlaybackException.TYPE_REMOTE: - Log.e(TAG, "PLAYER_SOURCE - TYPE_REMOTE: " + error.getMessage()); - break; - case ExoPlaybackException.TYPE_RENDERER: - Log.e(TAG, "PLAYER_SOURCE - TYPE_RENDERER: " + error.getRendererException().getMessage()); - break; - case ExoPlaybackException.TYPE_UNEXPECTED: - Log.e(TAG, "PLAYER_SOURCE - TYPE_UNEXPECTED: " + error.getUnexpectedException().getMessage()); - break; - } - } + Log.d(TAG, "preset: " + performancePreset); - @Override - public void onPlaybackStateChanged(@NonNullApi int state) { - switch (state) { - case Player.STATE_IDLE: - case Player.STATE_READY: - case Player.STATE_BUFFERING: - break; - case Player.STATE_ENDED: - Log.d(TAG, "PLAYER_STATE - ENDED"); - sendEvent(VideoReaderEventMessageCode.WAITING_FOR_VIDEO); // let MainActivity know so it can hide watermark/show settings button - (new Handler(Looper.getMainLooper())).postDelayed(() -> restart(), 1000); - break; - } + DataSource.Factory dataSourceFactory = () -> { + switch (performancePreset.dataSourceType){ + case INPUT_STREAM: + return (DataSource) new InputStreamDataSource(context, dataSpec, inputStream); + case BUFFERED_INPUT_STREAM: + default: + return (DataSource) new InputStreamBufferedDataSource(context, dataSpec, inputStream); + } + }; + + ExtractorsFactory extractorsFactory = () ->new Extractor[] {new H264Extractor(performancePreset.h264ReaderMaxSyncFrameSize, performancePreset.h264ReaderSampleTime)}; + MediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory, extractorsFactory).createMediaSource(MediaItem.fromUri(Uri.EMPTY)); + mPlayer.setMediaSource(mediaSource); + + mPlayer.prepare(); + mPlayer.play(); + mPlayer.addListener(new ExoPlayer.EventListener() { + @Override + @NonNullApi + public void onPlayerError(ExoPlaybackException error) { + switch (error.type) { + case ExoPlaybackException.TYPE_SOURCE: + Log.e(TAG, "PLAYER_SOURCE - TYPE_SOURCE: " + error.getSourceException().getMessage()); + (new Handler(Looper.getMainLooper())).postDelayed(() -> restart(), 1000); + break; + case ExoPlaybackException.TYPE_REMOTE: + Log.e(TAG, "PLAYER_SOURCE - TYPE_REMOTE: " + error.getSourceException().getMessage()); + break; + case ExoPlaybackException.TYPE_RENDERER: + Log.e(TAG, "PLAYER_SOURCE - TYPE_RENDERER: " + error.getSourceException().getMessage()); + break; + case ExoPlaybackException.TYPE_UNEXPECTED: + Log.e(TAG, "PLAYER_SOURCE - TYPE_UNEXPECTED: " + error.getSourceException().getMessage()); + break; } - }); + } - mPlayer.addVideoListener(new VideoListener() { - @Override - public void onRenderedFirstFrame() { - Log.d(TAG, "PLAYER_RENDER - FIRST FRAME"); - sendEvent(VideoReaderEventMessageCode.VIDEO_PLAYING); // let MainActivity know so it can hide watermark/show settings button + @Override + public void onPlaybackStateChanged(@NonNullApi int state) { + switch (state) { + case Player.STATE_IDLE: + case Player.STATE_READY: + case Player.STATE_BUFFERING: + break; + case Player.STATE_ENDED: + Log.d(TAG, "PLAYER_STATE - ENDED"); + sendEvent(VideoReaderEventMessageCode.WAITING_FOR_VIDEO); // let MainActivity know so it can hide watermark/show settings button + (new Handler(Looper.getMainLooper())).postDelayed(() -> restart(), 1000); + break; } + } + }); + + mPlayer.addVideoListener(new VideoListener() { + @Override + public void onRenderedFirstFrame() { + Log.d(TAG, "PLAYER_RENDER - FIRST FRAME"); + sendEvent(VideoReaderEventMessageCode.VIDEO_PLAYING); // let MainActivity know so it can hide watermark/show settings button + } - @Override - public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) { - if (!zoomedIn) { - ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) surfaceView.getLayoutParams(); - params.dimensionRatio = width + ":" + height; - surfaceView.setLayoutParams(params); - } + @Override + public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) { + if (!zoomedIn) { + ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) surfaceView.getLayoutParams(); + params.dimensionRatio = width + ":" + height; + surfaceView.setLayoutParams(params); + vrView.setAspect(width, height); } - }); + } + }); } private void sendEvent(VideoReaderEventMessageCode eventCode) { @@ -200,6 +229,9 @@ public void restart() { public void stop() { if (mPlayer != null) mPlayer.release(); + + // VR Related teardown + vrView.stop(); } public enum VideoReaderEventMessageCode {WAITING_FOR_VIDEO, VIDEO_PLAYING} diff --git a/app/src/main/java/com/fpvout/digiview/vr/gles/Drawable2d.java b/app/src/main/java/com/fpvout/digiview/vr/gles/Drawable2d.java new file mode 100644 index 0000000..1c8fab3 --- /dev/null +++ b/app/src/main/java/com/fpvout/digiview/vr/gles/Drawable2d.java @@ -0,0 +1,197 @@ +/* + * Copyright 2014 Google Inc. All rights reserved. + * + * 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.fpvout.digiview.vr.gles; + +import java.nio.FloatBuffer; + +/** + * Base class for stuff we like to draw. + */ +public class Drawable2d { + private static final int SIZEOF_FLOAT = 4; + + /** + * Simple equilateral triangle (1.0 per side). Centered on (0,0). + */ + private static final float TRIANGLE_COORDS[] = { + 0.0f, 0.577350269f, // 0 top + -0.5f, -0.288675135f, // 1 bottom left + 0.5f, -0.288675135f // 2 bottom right + }; + private static final float TRIANGLE_TEX_COORDS[] = { + 0.5f, 0.0f, // 0 top center + 0.0f, 1.0f, // 1 bottom left + 1.0f, 1.0f, // 2 bottom right + }; + private static final FloatBuffer TRIANGLE_BUF = + GlUtil.createFloatBuffer(TRIANGLE_COORDS); + private static final FloatBuffer TRIANGLE_TEX_BUF = + GlUtil.createFloatBuffer(TRIANGLE_TEX_COORDS); + + /** + * Simple square, specified as a triangle strip. The square is centered on (0,0) and has + * a size of 1x1. + *

+ * Triangles are 0-1-2 and 2-1-3 (counter-clockwise winding). + */ + private static final float RECTANGLE_COORDS[] = { + -0.5f, -0.5f, // 0 bottom left + 0.5f, -0.5f, // 1 bottom right + -0.5f, 0.5f, // 2 top left + 0.5f, 0.5f, // 3 top right + }; + private static final float RECTANGLE_TEX_COORDS[] = { + 0.0f, 1.0f, // 0 bottom left + 1.0f, 1.0f, // 1 bottom right + 0.0f, 0.0f, // 2 top left + 1.0f, 0.0f // 3 top right + }; + private static final FloatBuffer RECTANGLE_BUF = + GlUtil.createFloatBuffer(RECTANGLE_COORDS); + private static final FloatBuffer RECTANGLE_TEX_BUF = + GlUtil.createFloatBuffer(RECTANGLE_TEX_COORDS); + + /** + * A "full" square, extending from -1 to +1 in both dimensions. When the model/view/projection + * matrix is identity, this will exactly cover the viewport. + *

+ * The texture coordinates are Y-inverted relative to RECTANGLE. (This seems to work out + * right with external textures from SurfaceTexture.) + */ + private static final float FULL_RECTANGLE_COORDS[] = { + -1.0f, -1.0f, // 0 bottom left + 1.0f, -1.0f, // 1 bottom right + -1.0f, 1.0f, // 2 top left + 1.0f, 1.0f, // 3 top right + }; + private static final float FULL_RECTANGLE_TEX_COORDS[] = { + 0.0f, 0.0f, // 0 bottom left + 1.0f, 0.0f, // 1 bottom right + 0.0f, 1.0f, // 2 top left + 1.0f, 1.0f // 3 top right + }; + private static final FloatBuffer FULL_RECTANGLE_BUF = + GlUtil.createFloatBuffer(FULL_RECTANGLE_COORDS); + private static final FloatBuffer FULL_RECTANGLE_TEX_BUF = + GlUtil.createFloatBuffer(FULL_RECTANGLE_TEX_COORDS); + + + private FloatBuffer mVertexArray; + private FloatBuffer mTexCoordArray; + private int mVertexCount; + private int mCoordsPerVertex; + private int mVertexStride; + private int mTexCoordStride; + private Prefab mPrefab; + + /** + * Enum values for constructor. + */ + public enum Prefab { + TRIANGLE, RECTANGLE, FULL_RECTANGLE + } + + /** + * Prepares a drawable from a "pre-fabricated" shape definition. + *

+ * Does no EGL/GL operations, so this can be done at any time. + */ + public Drawable2d(Prefab shape) { + switch (shape) { + case TRIANGLE: + mVertexArray = TRIANGLE_BUF; + mTexCoordArray = TRIANGLE_TEX_BUF; + mCoordsPerVertex = 2; + mVertexStride = mCoordsPerVertex * SIZEOF_FLOAT; + mVertexCount = TRIANGLE_COORDS.length / mCoordsPerVertex; + break; + case RECTANGLE: + mVertexArray = RECTANGLE_BUF; + mTexCoordArray = RECTANGLE_TEX_BUF; + mCoordsPerVertex = 2; + mVertexStride = mCoordsPerVertex * SIZEOF_FLOAT; + mVertexCount = RECTANGLE_COORDS.length / mCoordsPerVertex; + break; + case FULL_RECTANGLE: + mVertexArray = FULL_RECTANGLE_BUF; + mTexCoordArray = FULL_RECTANGLE_TEX_BUF; + mCoordsPerVertex = 2; + mVertexStride = mCoordsPerVertex * SIZEOF_FLOAT; + mVertexCount = FULL_RECTANGLE_COORDS.length / mCoordsPerVertex; + break; + default: + throw new RuntimeException("Unknown shape " + shape); + } + mTexCoordStride = 2 * SIZEOF_FLOAT; + mPrefab = shape; + } + + /** + * Returns the array of vertices. + *

+ * To avoid allocations, this returns internal state. The caller must not modify it. + */ + public FloatBuffer getVertexArray() { + return mVertexArray; + } + + /** + * Returns the array of texture coordinates. + *

+ * To avoid allocations, this returns internal state. The caller must not modify it. + */ + public FloatBuffer getTexCoordArray() { + return mTexCoordArray; + } + + /** + * Returns the number of vertices stored in the vertex array. + */ + public int getVertexCount() { + return mVertexCount; + } + + /** + * Returns the width, in bytes, of the data for each vertex. + */ + public int getVertexStride() { + return mVertexStride; + } + + /** + * Returns the width, in bytes, of the data for each texture coordinate. + */ + public int getTexCoordStride() { + return mTexCoordStride; + } + + /** + * Returns the number of position coordinates per vertex. This will be 2 or 3. + */ + public int getCoordsPerVertex() { + return mCoordsPerVertex; + } + + @Override + public String toString() { + if (mPrefab != null) { + return "[Drawable2d: " + mPrefab + "]"; + } else { + return "[Drawable2d: ...]"; + } + } +} diff --git a/app/src/main/java/com/fpvout/digiview/vr/gles/EglCore.java b/app/src/main/java/com/fpvout/digiview/vr/gles/EglCore.java new file mode 100644 index 0000000..6b06eb2 --- /dev/null +++ b/app/src/main/java/com/fpvout/digiview/vr/gles/EglCore.java @@ -0,0 +1,372 @@ +/* + * Copyright 2013 Google Inc. All rights reserved. + * + * 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.fpvout.digiview.vr.gles; + +import android.graphics.SurfaceTexture; +import android.opengl.EGL14; +import android.opengl.EGLConfig; +import android.opengl.EGLContext; +import android.opengl.EGLDisplay; +import android.opengl.EGLExt; +import android.opengl.EGLSurface; +import android.util.Log; +import android.view.Surface; + +/** + * Core EGL state (display, context, config). + *

+ * The EGLContext must only be attached to one thread at a time. This class is not thread-safe. + */ +public final class EglCore { + private static final String TAG = GlUtil.TAG; + + /** + * Constructor flag: surface must be recordable. This discourages EGL from using a + * pixel format that cannot be converted efficiently to something usable by the video + * encoder. + */ + public static final int FLAG_RECORDABLE = 0x01; + + /** + * Constructor flag: ask for GLES3, fall back to GLES2 if not available. Without this + * flag, GLES2 is used. + */ + public static final int FLAG_TRY_GLES3 = 0x02; + + // Android-specific extension. + private static final int EGL_RECORDABLE_ANDROID = 0x3142; + + private EGLDisplay mEGLDisplay = EGL14.EGL_NO_DISPLAY; + private EGLContext mEGLContext = EGL14.EGL_NO_CONTEXT; + private EGLConfig mEGLConfig = null; + private int mGlVersion = -1; + + + /** + * Prepares EGL display and context. + *

+ * Equivalent to EglCore(null, 0). + */ + public EglCore() { + this(null, 0); + } + + /** + * Prepares EGL display and context. + *

+ * @param sharedContext The context to share, or null if sharing is not desired. + * @param flags Configuration bit flags, e.g. FLAG_RECORDABLE. + */ + public EglCore(EGLContext sharedContext, int flags) { + if (mEGLDisplay != EGL14.EGL_NO_DISPLAY) { + throw new RuntimeException("EGL already set up"); + } + + if (sharedContext == null) { + sharedContext = EGL14.EGL_NO_CONTEXT; + } + + mEGLDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); + if (mEGLDisplay == EGL14.EGL_NO_DISPLAY) { + throw new RuntimeException("unable to get EGL14 display"); + } + int[] version = new int[2]; + if (!EGL14.eglInitialize(mEGLDisplay, version, 0, version, 1)) { + mEGLDisplay = null; + throw new RuntimeException("unable to initialize EGL14"); + } + + // Try to get a GLES3 context, if requested. + if ((flags & FLAG_TRY_GLES3) != 0) { + //Log.d(TAG, "Trying GLES 3"); + EGLConfig config = getConfig(flags, 3); + if (config != null) { + int[] attrib3_list = { + EGL14.EGL_CONTEXT_CLIENT_VERSION, 3, + EGL14.EGL_NONE + }; + EGLContext context = EGL14.eglCreateContext(mEGLDisplay, config, sharedContext, + attrib3_list, 0); + + if (EGL14.eglGetError() == EGL14.EGL_SUCCESS) { + //Log.d(TAG, "Got GLES 3 config"); + mEGLConfig = config; + mEGLContext = context; + mGlVersion = 3; + } + } + } + if (mEGLContext == EGL14.EGL_NO_CONTEXT) { // GLES 2 only, or GLES 3 attempt failed + //Log.d(TAG, "Trying GLES 2"); + EGLConfig config = getConfig(flags, 2); + if (config == null) { + throw new RuntimeException("Unable to find a suitable EGLConfig"); + } + int[] attrib2_list = { + EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, + EGL14.EGL_NONE + }; + EGLContext context = EGL14.eglCreateContext(mEGLDisplay, config, sharedContext, + attrib2_list, 0); + checkEglError("eglCreateContext"); + mEGLConfig = config; + mEGLContext = context; + mGlVersion = 2; + } + + // Confirm with query. + int[] values = new int[1]; + EGL14.eglQueryContext(mEGLDisplay, mEGLContext, EGL14.EGL_CONTEXT_CLIENT_VERSION, + values, 0); + Log.d(TAG, "EGLContext created, client version " + values[0]); + } + + /** + * Finds a suitable EGLConfig. + * + * @param flags Bit flags from constructor. + * @param version Must be 2 or 3. + */ + private EGLConfig getConfig(int flags, int version) { + int renderableType = EGL14.EGL_OPENGL_ES2_BIT; + if (version >= 3) { + renderableType |= EGLExt.EGL_OPENGL_ES3_BIT_KHR; + } + + // The actual surface is generally RGBA or RGBX, so situationally omitting alpha + // doesn't really help. It can also lead to a huge performance hit on glReadPixels() + // when reading into a GL_RGBA buffer. + int[] attribList = { + EGL14.EGL_RED_SIZE, 8, + EGL14.EGL_GREEN_SIZE, 8, + EGL14.EGL_BLUE_SIZE, 8, + EGL14.EGL_ALPHA_SIZE, 8, + //EGL14.EGL_DEPTH_SIZE, 16, + //EGL14.EGL_STENCIL_SIZE, 8, + EGL14.EGL_RENDERABLE_TYPE, renderableType, + EGL14.EGL_NONE, 0, // placeholder for recordable [@-3] + EGL14.EGL_NONE + }; + if ((flags & FLAG_RECORDABLE) != 0) { + attribList[attribList.length - 3] = EGL_RECORDABLE_ANDROID; + attribList[attribList.length - 2] = 1; + } + EGLConfig[] configs = new EGLConfig[1]; + int[] numConfigs = new int[1]; + if (!EGL14.eglChooseConfig(mEGLDisplay, attribList, 0, configs, 0, configs.length, + numConfigs, 0)) { + Log.w(TAG, "unable to find RGB8888 / " + version + " EGLConfig"); + return null; + } + return configs[0]; + } + + /** + * Discards all resources held by this class, notably the EGL context. This must be + * called from the thread where the context was created. + *

+ * On completion, no context will be current. + */ + public void release() { + if (mEGLDisplay != EGL14.EGL_NO_DISPLAY) { + // Android is unusual in that it uses a reference-counted EGLDisplay. So for + // every eglInitialize() we need an eglTerminate(). + EGL14.eglMakeCurrent(mEGLDisplay, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, + EGL14.EGL_NO_CONTEXT); + EGL14.eglDestroyContext(mEGLDisplay, mEGLContext); + EGL14.eglReleaseThread(); + EGL14.eglTerminate(mEGLDisplay); + } + + mEGLDisplay = EGL14.EGL_NO_DISPLAY; + mEGLContext = EGL14.EGL_NO_CONTEXT; + mEGLConfig = null; + } + + @Override + protected void finalize() throws Throwable { + try { + if (mEGLDisplay != EGL14.EGL_NO_DISPLAY) { + // We're limited here -- finalizers don't run on the thread that holds + // the EGL state, so if a surface or context is still current on another + // thread we can't fully release it here. Exceptions thrown from here + // are quietly discarded. Complain in the log file. + Log.w(TAG, "WARNING: EglCore was not explicitly released -- state may be leaked"); + release(); + } + } finally { + super.finalize(); + } + } + + /** + * Destroys the specified surface. Note the EGLSurface won't actually be destroyed if it's + * still current in a context. + */ + public void releaseSurface(EGLSurface eglSurface) { + EGL14.eglDestroySurface(mEGLDisplay, eglSurface); + } + + /** + * Creates an EGL surface associated with a Surface. + *

+ * If this is destined for MediaCodec, the EGLConfig should have the "recordable" attribute. + */ + public EGLSurface createWindowSurface(Object surface) { + if (!(surface instanceof Surface) && !(surface instanceof SurfaceTexture)) { + throw new RuntimeException("invalid surface: " + surface); + } + + // Create a window surface, and attach it to the Surface we received. + int[] surfaceAttribs = { + EGL14.EGL_NONE + }; + EGLSurface eglSurface = EGL14.eglCreateWindowSurface(mEGLDisplay, mEGLConfig, surface, + surfaceAttribs, 0); + checkEglError("eglCreateWindowSurface"); + if (eglSurface == null) { + throw new RuntimeException("surface was null"); + } + return eglSurface; + } + + /** + * Creates an EGL surface associated with an offscreen buffer. + */ + public EGLSurface createOffscreenSurface(int width, int height) { + int[] surfaceAttribs = { + EGL14.EGL_WIDTH, width, + EGL14.EGL_HEIGHT, height, + EGL14.EGL_NONE + }; + EGLSurface eglSurface = EGL14.eglCreatePbufferSurface(mEGLDisplay, mEGLConfig, + surfaceAttribs, 0); + checkEglError("eglCreatePbufferSurface"); + if (eglSurface == null) { + throw new RuntimeException("surface was null"); + } + return eglSurface; + } + + /** + * Makes our EGL context current, using the supplied surface for both "draw" and "read". + */ + public void makeCurrent(EGLSurface eglSurface) { + if (mEGLDisplay == EGL14.EGL_NO_DISPLAY) { + // called makeCurrent() before create? + Log.d(TAG, "NOTE: makeCurrent w/o display"); + } + if (!EGL14.eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext)) { + throw new RuntimeException("eglMakeCurrent failed"); + } + } + + /** + * Makes our EGL context current, using the supplied "draw" and "read" surfaces. + */ + public void makeCurrent(EGLSurface drawSurface, EGLSurface readSurface) { + if (mEGLDisplay == EGL14.EGL_NO_DISPLAY) { + // called makeCurrent() before create? + Log.d(TAG, "NOTE: makeCurrent w/o display"); + } + if (!EGL14.eglMakeCurrent(mEGLDisplay, drawSurface, readSurface, mEGLContext)) { + throw new RuntimeException("eglMakeCurrent(draw,read) failed"); + } + } + + /** + * Makes no context current. + */ + public void makeNothingCurrent() { + if (!EGL14.eglMakeCurrent(mEGLDisplay, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, + EGL14.EGL_NO_CONTEXT)) { + throw new RuntimeException("eglMakeCurrent failed"); + } + } + + /** + * Calls eglSwapBuffers. Use this to "publish" the current frame. + * + * @return false on failure + */ + public boolean swapBuffers(EGLSurface eglSurface) { + return EGL14.eglSwapBuffers(mEGLDisplay, eglSurface); + } + + /** + * Sends the presentation time stamp to EGL. Time is expressed in nanoseconds. + */ + public void setPresentationTime(EGLSurface eglSurface, long nsecs) { + EGLExt.eglPresentationTimeANDROID(mEGLDisplay, eglSurface, nsecs); + } + + /** + * Returns true if our context and the specified surface are current. + */ + public boolean isCurrent(EGLSurface eglSurface) { + return mEGLContext.equals(EGL14.eglGetCurrentContext()) && + eglSurface.equals(EGL14.eglGetCurrentSurface(EGL14.EGL_DRAW)); + } + + /** + * Performs a simple surface query. + */ + public int querySurface(EGLSurface eglSurface, int what) { + int[] value = new int[1]; + EGL14.eglQuerySurface(mEGLDisplay, eglSurface, what, value, 0); + return value[0]; + } + + /** + * Queries a string value. + */ + public String queryString(int what) { + return EGL14.eglQueryString(mEGLDisplay, what); + } + + /** + * Returns the GLES version this context is configured for (currently 2 or 3). + */ + public int getGlVersion() { + return mGlVersion; + } + + /** + * Writes the current display, context, and surface to the log. + */ + public static void logCurrent(String msg) { + EGLDisplay display; + EGLContext context; + EGLSurface surface; + + display = EGL14.eglGetCurrentDisplay(); + context = EGL14.eglGetCurrentContext(); + surface = EGL14.eglGetCurrentSurface(EGL14.EGL_DRAW); + Log.i(TAG, "Current EGL (" + msg + "): display=" + display + ", context=" + context + + ", surface=" + surface); + } + + /** + * Checks for EGL errors. Throws an exception if an error has been raised. + */ + private void checkEglError(String msg) { + int error; + if ((error = EGL14.eglGetError()) != EGL14.EGL_SUCCESS) { + throw new RuntimeException(msg + ": EGL error: 0x" + Integer.toHexString(error)); + } + } +} diff --git a/app/src/main/java/com/fpvout/digiview/vr/gles/EglSurfaceBase.java b/app/src/main/java/com/fpvout/digiview/vr/gles/EglSurfaceBase.java new file mode 100644 index 0000000..c736977 --- /dev/null +++ b/app/src/main/java/com/fpvout/digiview/vr/gles/EglSurfaceBase.java @@ -0,0 +1,197 @@ +/* + * Copyright 2013 Google Inc. All rights reserved. + * + * 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.fpvout.digiview.vr.gles; + +import android.graphics.Bitmap; +import android.opengl.EGL14; +import android.opengl.EGLSurface; +import android.opengl.GLES20; +import android.util.Log; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +/** + * Common base class for EGL surfaces. + *

+ * There can be multiple surfaces associated with a single context. + */ +public class EglSurfaceBase { + protected static final String TAG = GlUtil.TAG; + + // EglCore object we're associated with. It may be associated with multiple surfaces. + protected EglCore mEglCore; + + private EGLSurface mEGLSurface = EGL14.EGL_NO_SURFACE; + private int mWidth = -1; + private int mHeight = -1; + + protected EglSurfaceBase(EglCore eglCore) { + mEglCore = eglCore; + } + + /** + * Creates a window surface. + *

+ * @param surface May be a Surface or SurfaceTexture. + */ + public void createWindowSurface(Object surface) { + if (mEGLSurface != EGL14.EGL_NO_SURFACE) { + throw new IllegalStateException("surface already created"); + } + mEGLSurface = mEglCore.createWindowSurface(surface); + + // Don't cache width/height here, because the size of the underlying surface can change + // out from under us (see e.g. HardwareScalerActivity). + //mWidth = mEglCore.querySurface(mEGLSurface, EGL14.EGL_WIDTH); + //mHeight = mEglCore.querySurface(mEGLSurface, EGL14.EGL_HEIGHT); + } + + /** + * Creates an off-screen surface. + */ + public void createOffscreenSurface(int width, int height) { + if (mEGLSurface != EGL14.EGL_NO_SURFACE) { + throw new IllegalStateException("surface already created"); + } + mEGLSurface = mEglCore.createOffscreenSurface(width, height); + mWidth = width; + mHeight = height; + } + + /** + * Returns the surface's width, in pixels. + *

+ * If this is called on a window surface, and the underlying surface is in the process + * of changing size, we may not see the new size right away (e.g. in the "surfaceChanged" + * callback). The size should match after the next buffer swap. + */ + public int getWidth() { + if (mWidth < 0) { + return mEglCore.querySurface(mEGLSurface, EGL14.EGL_WIDTH); + } else { + return mWidth; + } + } + + /** + * Returns the surface's height, in pixels. + */ + public int getHeight() { + if (mHeight < 0) { + return mEglCore.querySurface(mEGLSurface, EGL14.EGL_HEIGHT); + } else { + return mHeight; + } + } + + /** + * Release the EGL surface. + */ + public void releaseEglSurface() { + mEglCore.releaseSurface(mEGLSurface); + mEGLSurface = EGL14.EGL_NO_SURFACE; + mWidth = mHeight = -1; + } + + /** + * Makes our EGL context and surface current. + */ + public void makeCurrent() { + mEglCore.makeCurrent(mEGLSurface); + } + + /** + * Makes our EGL context and surface current for drawing, using the supplied surface + * for reading. + */ + public void makeCurrentReadFrom(EglSurfaceBase readSurface) { + mEglCore.makeCurrent(mEGLSurface, readSurface.mEGLSurface); + } + + /** + * Calls eglSwapBuffers. Use this to "publish" the current frame. + * + * @return false on failure + */ + public boolean swapBuffers() { + boolean result = mEglCore.swapBuffers(mEGLSurface); + if (!result) { + Log.d(TAG, "WARNING: swapBuffers() failed"); + } + return result; + } + + /** + * Sends the presentation time stamp to EGL. + * + * @param nsecs Timestamp, in nanoseconds. + */ + public void setPresentationTime(long nsecs) { + mEglCore.setPresentationTime(mEGLSurface, nsecs); + } + + /** + * Saves the EGL surface to a file. + *

+ * Expects that this object's EGL surface is current. + */ + public void saveFrame(File file) throws IOException { + if (!mEglCore.isCurrent(mEGLSurface)) { + throw new RuntimeException("Expected EGL context/surface is not current"); + } + + // glReadPixels fills in a "direct" ByteBuffer with what is essentially big-endian RGBA + // data (i.e. a byte of red, followed by a byte of green...). While the Bitmap + // constructor that takes an int[] wants little-endian ARGB (blue/red swapped), the + // Bitmap "copy pixels" method wants the same format GL provides. + // + // Ideally we'd have some way to re-use the ByteBuffer, especially if we're calling + // here often. + // + // Making this even more interesting is the upside-down nature of GL, which means + // our output will look upside down relative to what appears on screen if the + // typical GL conventions are used. + + String filename = file.toString(); + + int width = getWidth(); + int height = getHeight(); + ByteBuffer buf = ByteBuffer.allocateDirect(width * height * 4); + buf.order(ByteOrder.LITTLE_ENDIAN); + GLES20.glReadPixels(0, 0, width, height, + GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf); + GlUtil.checkGlError("glReadPixels"); + buf.rewind(); + + BufferedOutputStream bos = null; + try { + bos = new BufferedOutputStream(new FileOutputStream(filename)); + Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + bmp.copyPixelsFromBuffer(buf); + bmp.compress(Bitmap.CompressFormat.PNG, 90, bos); + bmp.recycle(); + } finally { + if (bos != null) bos.close(); + } + Log.d(TAG, "Saved " + width + "x" + height + " frame as '" + filename + "'"); + } +} diff --git a/app/src/main/java/com/fpvout/digiview/vr/gles/FullFrameRect.java b/app/src/main/java/com/fpvout/digiview/vr/gles/FullFrameRect.java new file mode 100644 index 0000000..d9efa62 --- /dev/null +++ b/app/src/main/java/com/fpvout/digiview/vr/gles/FullFrameRect.java @@ -0,0 +1,89 @@ +/* + * Copyright 2014 Google Inc. All rights reserved. + * + * 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.fpvout.digiview.vr.gles; + +/** + * This class essentially represents a viewport-sized sprite that will be rendered with + * a texture, usually from an external source like the camera or video decoder. + */ +public class FullFrameRect { + private final Drawable2d mRectDrawable = new Drawable2d(Drawable2d.Prefab.FULL_RECTANGLE); + private Texture2dProgram mProgram; + + /** + * Prepares the object. + * + * @param program The program to use. FullFrameRect takes ownership, and will release + * the program when no longer needed. + */ + public FullFrameRect(Texture2dProgram program) { + mProgram = program; + } + + /** + * Releases resources. + *

+ * This must be called with the appropriate EGL context current (i.e. the one that was + * current when the constructor was called). If we're about to destroy the EGL context, + * there's no value in having the caller make it current just to do this cleanup, so you + * can pass a flag that will tell this function to skip any EGL-context-specific cleanup. + */ + public void release(boolean doEglCleanup) { + if (mProgram != null) { + if (doEglCleanup) { + mProgram.release(); + } + mProgram = null; + } + } + + /** + * Returns the program currently in use. + */ + public Texture2dProgram getProgram() { + return mProgram; + } + + /** + * Changes the program. The previous program will be released. + *

+ * The appropriate EGL context must be current. + */ + public void changeProgram(Texture2dProgram program) { + mProgram.release(); + mProgram = program; + } + + /** + * Creates a texture object suitable for use with drawFrame(). + */ + public int createTextureObject() { + return mProgram.createTextureObject(); + } + + /** + * Draws a viewport-filling rect, texturing it with the specified texture object. + */ + public void drawFrame(int textureId, float[] texMatrix) { + // Use the identity matrix for MVP so our 2x2 FULL_RECTANGLE covers the viewport. + mProgram.draw(GlUtil.IDENTITY_MATRIX, mRectDrawable.getVertexArray(), 0, + mRectDrawable.getVertexCount(), mRectDrawable.getCoordsPerVertex(), + mRectDrawable.getVertexStride(), + texMatrix, mRectDrawable.getTexCoordArray(), textureId, + mRectDrawable.getTexCoordStride()); + } +} diff --git a/app/src/main/java/com/fpvout/digiview/vr/gles/GlUtil.java b/app/src/main/java/com/fpvout/digiview/vr/gles/GlUtil.java new file mode 100644 index 0000000..8d10b2e --- /dev/null +++ b/app/src/main/java/com/fpvout/digiview/vr/gles/GlUtil.java @@ -0,0 +1,195 @@ +/* + * Copyright 2014 Google Inc. All rights reserved. + * + * 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.fpvout.digiview.vr.gles; + +import android.opengl.GLES20; +import android.opengl.GLES30; +import android.opengl.Matrix; +import android.util.Log; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.FloatBuffer; + +/** + * Some OpenGL utility functions. + */ +public class GlUtil { + public static final String TAG = "Grafika"; + + /** Identity matrix for general use. Don't modify or life will get weird. */ + public static final float[] IDENTITY_MATRIX; + static { + IDENTITY_MATRIX = new float[16]; + Matrix.setIdentityM(IDENTITY_MATRIX, 0); + } + + private static final int SIZEOF_FLOAT = 4; + + + private GlUtil() {} // do not instantiate + + /** + * Creates a new program from the supplied vertex and fragment shaders. + * + * @return A handle to the program, or 0 on failure. + */ + public static int createProgram(String vertexSource, String fragmentSource) { + int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource); + if (vertexShader == 0) { + return 0; + } + int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource); + if (pixelShader == 0) { + return 0; + } + + int program = GLES20.glCreateProgram(); + checkGlError("glCreateProgram"); + if (program == 0) { + Log.e(TAG, "Could not create program"); + } + GLES20.glAttachShader(program, vertexShader); + checkGlError("glAttachShader"); + GLES20.glAttachShader(program, pixelShader); + checkGlError("glAttachShader"); + GLES20.glLinkProgram(program); + int[] linkStatus = new int[1]; + GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0); + if (linkStatus[0] != GLES20.GL_TRUE) { + Log.e(TAG, "Could not link program: "); + Log.e(TAG, GLES20.glGetProgramInfoLog(program)); + GLES20.glDeleteProgram(program); + program = 0; + } + return program; + } + + /** + * Compiles the provided shader source. + * + * @return A handle to the shader, or 0 on failure. + */ + public static int loadShader(int shaderType, String source) { + int shader = GLES20.glCreateShader(shaderType); + checkGlError("glCreateShader type=" + shaderType); + GLES20.glShaderSource(shader, source); + GLES20.glCompileShader(shader); + int[] compiled = new int[1]; + GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0); + if (compiled[0] == 0) { + Log.e(TAG, "Could not compile shader " + shaderType + ":"); + Log.e(TAG, " " + GLES20.glGetShaderInfoLog(shader)); + GLES20.glDeleteShader(shader); + shader = 0; + } + return shader; + } + + /** + * Checks to see if a GLES error has been raised. + */ + public static void checkGlError(String op) { + int error = GLES20.glGetError(); + if (error != GLES20.GL_NO_ERROR) { + String msg = op + ": glError 0x" + Integer.toHexString(error); + Log.e(TAG, msg); + throw new RuntimeException(msg); + } + } + + /** + * Checks to see if the location we obtained is valid. GLES returns -1 if a label + * could not be found, but does not set the GL error. + *

+ * Throws a RuntimeException if the location is invalid. + */ + public static void checkLocation(int location, String label) { + if (location < 0) { + throw new RuntimeException("Unable to locate '" + label + "' in program"); + } + } + + /** + * Creates a texture from raw data. + * + * @param data Image data, in a "direct" ByteBuffer. + * @param width Texture width, in pixels (not bytes). + * @param height Texture height, in pixels. + * @param format Image data format (use constant appropriate for glTexImage2D(), e.g. GL_RGBA). + * @return Handle to texture. + */ + public static int createImageTexture(ByteBuffer data, int width, int height, int format) { + int[] textureHandles = new int[1]; + int textureHandle; + + GLES20.glGenTextures(1, textureHandles, 0); + textureHandle = textureHandles[0]; + GlUtil.checkGlError("glGenTextures"); + + // Bind the texture handle to the 2D texture target. + GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle); + + // Configure min/mag filtering, i.e. what scaling method do we use if what we're rendering + // is smaller or larger than the source image. + GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, + GLES20.GL_LINEAR); + GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, + GLES20.GL_LINEAR); + GlUtil.checkGlError("loadImageTexture"); + + // Load the data from the buffer into the texture handle. + GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, /*level*/ 0, format, + width, height, /*border*/ 0, format, GLES20.GL_UNSIGNED_BYTE, data); + GlUtil.checkGlError("loadImageTexture"); + + return textureHandle; + } + + /** + * Allocates a direct float buffer, and populates it with the float array data. + */ + public static FloatBuffer createFloatBuffer(float[] coords) { + // Allocate a direct ByteBuffer, using 4 bytes per float, and copy coords into it. + ByteBuffer bb = ByteBuffer.allocateDirect(coords.length * SIZEOF_FLOAT); + bb.order(ByteOrder.nativeOrder()); + FloatBuffer fb = bb.asFloatBuffer(); + fb.put(coords); + fb.position(0); + return fb; + } + + /** + * Writes GL version info to the log. + */ + public static void logVersionInfo() { + Log.i(TAG, "vendor : " + GLES20.glGetString(GLES20.GL_VENDOR)); + Log.i(TAG, "renderer: " + GLES20.glGetString(GLES20.GL_RENDERER)); + Log.i(TAG, "version : " + GLES20.glGetString(GLES20.GL_VERSION)); + + if (false) { + int[] values = new int[1]; + GLES30.glGetIntegerv(GLES30.GL_MAJOR_VERSION, values, 0); + int majorVersion = values[0]; + GLES30.glGetIntegerv(GLES30.GL_MINOR_VERSION, values, 0); + int minorVersion = values[0]; + if (GLES30.glGetError() == GLES30.GL_NO_ERROR) { + Log.i(TAG, "iversion: " + majorVersion + "." + minorVersion); + } + } + } +} diff --git a/app/src/main/java/com/fpvout/digiview/vr/gles/Texture2dProgram.java b/app/src/main/java/com/fpvout/digiview/vr/gles/Texture2dProgram.java new file mode 100644 index 0000000..7cb998b --- /dev/null +++ b/app/src/main/java/com/fpvout/digiview/vr/gles/Texture2dProgram.java @@ -0,0 +1,343 @@ +/* + * Copyright 2014 Google Inc. All rights reserved. + * + * 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.fpvout.digiview.vr.gles; + +import android.opengl.GLES11Ext; +import android.opengl.GLES20; +import android.util.Log; + +import java.nio.FloatBuffer; + +/** + * GL program and supporting functions for textured 2D shapes. + */ +public class Texture2dProgram { + private static final String TAG = GlUtil.TAG; + + public enum ProgramType { + TEXTURE_2D, TEXTURE_EXT, TEXTURE_EXT_BW, TEXTURE_EXT_FILT + } + + // Simple vertex shader, used for all programs. + private static final String VERTEX_SHADER = + "uniform mat4 uMVPMatrix;\n" + + "uniform mat4 uTexMatrix;\n" + + "attribute vec4 aPosition;\n" + + "attribute vec4 aTextureCoord;\n" + + "varying vec2 vTextureCoord;\n" + + "void main() {\n" + + " gl_Position = uMVPMatrix * aPosition;\n" + + " vTextureCoord = (uTexMatrix * aTextureCoord).xy;\n" + + "}\n"; + + // Simple fragment shader for use with "normal" 2D textures. + private static final String FRAGMENT_SHADER_2D = + "precision mediump float;\n" + + "varying vec2 vTextureCoord;\n" + + "uniform sampler2D sTexture;\n" + + "void main() {\n" + + " gl_FragColor = texture2D(sTexture, vTextureCoord);\n" + + "}\n"; + + // Simple fragment shader for use with external 2D textures (e.g. what we get from + // SurfaceTexture). + private static final String FRAGMENT_SHADER_EXT = + "#extension GL_OES_EGL_image_external : require\n" + + "precision mediump float;\n" + + "varying vec2 vTextureCoord;\n" + + "uniform samplerExternalOES sTexture;\n" + + "void main() {\n" + + " gl_FragColor = texture2D(sTexture, vTextureCoord);\n" + + "}\n"; + + // Fragment shader that converts color to black & white with a simple transformation. + private static final String FRAGMENT_SHADER_EXT_BW = + "#extension GL_OES_EGL_image_external : require\n" + + "precision mediump float;\n" + + "varying vec2 vTextureCoord;\n" + + "uniform samplerExternalOES sTexture;\n" + + "void main() {\n" + + " vec4 tc = texture2D(sTexture, vTextureCoord);\n" + + " float color = tc.r * 0.3 + tc.g * 0.59 + tc.b * 0.11;\n" + + " gl_FragColor = vec4(color, color, color, 1.0);\n" + + "}\n"; + + // Fragment shader with a convolution filter. The upper-left half will be drawn normally, + // the lower-right half will have the filter applied, and a thin red line will be drawn + // at the border. + // + // This is not optimized for performance. Some things that might make this faster: + // - Remove the conditionals. They're used to present a half & half view with a red + // stripe across the middle, but that's only useful for a demo. + // - Unroll the loop. Ideally the compiler does this for you when it's beneficial. + // - Bake the filter kernel into the shader, instead of passing it through a uniform + // array. That, combined with loop unrolling, should reduce memory accesses. + public static final int KERNEL_SIZE = 9; + private static final String FRAGMENT_SHADER_EXT_FILT = + "#extension GL_OES_EGL_image_external : require\n" + + "#define KERNEL_SIZE " + KERNEL_SIZE + "\n" + + "precision highp float;\n" + + "varying vec2 vTextureCoord;\n" + + "uniform samplerExternalOES sTexture;\n" + + "uniform float uKernel[KERNEL_SIZE];\n" + + "uniform vec2 uTexOffset[KERNEL_SIZE];\n" + + "uniform float uColorAdjust;\n" + + "void main() {\n" + + " int i = 0;\n" + + " vec4 sum = vec4(0.0);\n" + + " if (vTextureCoord.x < vTextureCoord.y - 0.005) {\n" + + " for (i = 0; i < KERNEL_SIZE; i++) {\n" + + " vec4 texc = texture2D(sTexture, vTextureCoord + uTexOffset[i]);\n" + + " sum += texc * uKernel[i];\n" + + " }\n" + + " sum += uColorAdjust;\n" + + " } else if (vTextureCoord.x > vTextureCoord.y + 0.005) {\n" + + " sum = texture2D(sTexture, vTextureCoord);\n" + + " } else {\n" + + " sum.r = 1.0;\n" + + " }\n" + + " gl_FragColor = sum;\n" + + "}\n"; + + private ProgramType mProgramType; + + // Handles to the GL program and various components of it. + private int mProgramHandle; + private int muMVPMatrixLoc; + private int muTexMatrixLoc; + private int muKernelLoc; + private int muTexOffsetLoc; + private int muColorAdjustLoc; + private int maPositionLoc; + private int maTextureCoordLoc; + + private int mTextureTarget; + + private float[] mKernel = new float[KERNEL_SIZE]; + private float[] mTexOffset; + private float mColorAdjust; + + + /** + * Prepares the program in the current EGL context. + */ + public Texture2dProgram(ProgramType programType) { + mProgramType = programType; + + switch (programType) { + case TEXTURE_2D: + mTextureTarget = GLES20.GL_TEXTURE_2D; + mProgramHandle = GlUtil.createProgram(VERTEX_SHADER, FRAGMENT_SHADER_2D); + break; + case TEXTURE_EXT: + mTextureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES; + mProgramHandle = GlUtil.createProgram(VERTEX_SHADER, FRAGMENT_SHADER_EXT); + break; + case TEXTURE_EXT_BW: + mTextureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES; + mProgramHandle = GlUtil.createProgram(VERTEX_SHADER, FRAGMENT_SHADER_EXT_BW); + break; + case TEXTURE_EXT_FILT: + mTextureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES; + mProgramHandle = GlUtil.createProgram(VERTEX_SHADER, FRAGMENT_SHADER_EXT_FILT); + break; + default: + throw new RuntimeException("Unhandled type " + programType); + } + if (mProgramHandle == 0) { + throw new RuntimeException("Unable to create program"); + } + Log.d(TAG, "Created program " + mProgramHandle + " (" + programType + ")"); + + // get locations of attributes and uniforms + + maPositionLoc = GLES20.glGetAttribLocation(mProgramHandle, "aPosition"); + GlUtil.checkLocation(maPositionLoc, "aPosition"); + maTextureCoordLoc = GLES20.glGetAttribLocation(mProgramHandle, "aTextureCoord"); + GlUtil.checkLocation(maTextureCoordLoc, "aTextureCoord"); + muMVPMatrixLoc = GLES20.glGetUniformLocation(mProgramHandle, "uMVPMatrix"); + GlUtil.checkLocation(muMVPMatrixLoc, "uMVPMatrix"); + muTexMatrixLoc = GLES20.glGetUniformLocation(mProgramHandle, "uTexMatrix"); + GlUtil.checkLocation(muTexMatrixLoc, "uTexMatrix"); + muKernelLoc = GLES20.glGetUniformLocation(mProgramHandle, "uKernel"); + if (muKernelLoc < 0) { + // no kernel in this one + muKernelLoc = -1; + muTexOffsetLoc = -1; + muColorAdjustLoc = -1; + } else { + // has kernel, must also have tex offset and color adj + muTexOffsetLoc = GLES20.glGetUniformLocation(mProgramHandle, "uTexOffset"); + GlUtil.checkLocation(muTexOffsetLoc, "uTexOffset"); + muColorAdjustLoc = GLES20.glGetUniformLocation(mProgramHandle, "uColorAdjust"); + GlUtil.checkLocation(muColorAdjustLoc, "uColorAdjust"); + + // initialize default values + setKernel(new float[] {0f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f}, 0f); + setTexSize(256, 256); + } + } + + /** + * Releases the program. + *

+ * The appropriate EGL context must be current (i.e. the one that was used to create + * the program). + */ + public void release() { + Log.d(TAG, "deleting program " + mProgramHandle); + GLES20.glDeleteProgram(mProgramHandle); + mProgramHandle = -1; + } + + /** + * Returns the program type. + */ + public ProgramType getProgramType() { + return mProgramType; + } + + /** + * Creates a texture object suitable for use with this program. + *

+ * On exit, the texture will be bound. + */ + public int createTextureObject() { + int[] textures = new int[1]; + GLES20.glGenTextures(1, textures, 0); + GlUtil.checkGlError("glGenTextures"); + + int texId = textures[0]; + GLES20.glBindTexture(mTextureTarget, texId); + GlUtil.checkGlError("glBindTexture " + texId); + + GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, + GLES20.GL_NEAREST); + GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, + GLES20.GL_LINEAR); + GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, + GLES20.GL_CLAMP_TO_EDGE); + GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, + GLES20.GL_CLAMP_TO_EDGE); + GlUtil.checkGlError("glTexParameter"); + + return texId; + } + + /** + * Configures the convolution filter values. + * + * @param values Normalized filter values; must be KERNEL_SIZE elements. + */ + public void setKernel(float[] values, float colorAdj) { + if (values.length != KERNEL_SIZE) { + throw new IllegalArgumentException("Kernel size is " + values.length + + " vs. " + KERNEL_SIZE); + } + System.arraycopy(values, 0, mKernel, 0, KERNEL_SIZE); + mColorAdjust = colorAdj; + //Log.d(TAG, "filt kernel: " + Arrays.toString(mKernel) + ", adj=" + colorAdj); + } + + /** + * Sets the size of the texture. This is used to find adjacent texels when filtering. + */ + public void setTexSize(int width, int height) { + float rw = 1.0f / width; + float rh = 1.0f / height; + + // Don't need to create a new array here, but it's syntactically convenient. + mTexOffset = new float[] { + -rw, -rh, 0f, -rh, rw, -rh, + -rw, 0f, 0f, 0f, rw, 0f, + -rw, rh, 0f, rh, rw, rh + }; + //Log.d(TAG, "filt size: " + width + "x" + height + ": " + Arrays.toString(mTexOffset)); + } + + /** + * Issues the draw call. Does the full setup on every call. + * + * @param mvpMatrix The 4x4 projection matrix. + * @param vertexBuffer Buffer with vertex position data. + * @param firstVertex Index of first vertex to use in vertexBuffer. + * @param vertexCount Number of vertices in vertexBuffer. + * @param coordsPerVertex The number of coordinates per vertex (e.g. x,y is 2). + * @param vertexStride Width, in bytes, of the position data for each vertex (often + * vertexCount * sizeof(float)). + * @param texMatrix A 4x4 transformation matrix for texture coords. (Primarily intended + * for use with SurfaceTexture.) + * @param texBuffer Buffer with vertex texture data. + * @param texStride Width, in bytes, of the texture data for each vertex. + */ + public void draw(float[] mvpMatrix, FloatBuffer vertexBuffer, int firstVertex, + int vertexCount, int coordsPerVertex, int vertexStride, + float[] texMatrix, FloatBuffer texBuffer, int textureId, int texStride) { + GlUtil.checkGlError("draw start"); + + // Select the program. + GLES20.glUseProgram(mProgramHandle); + GlUtil.checkGlError("glUseProgram"); + + // Set the texture. + GLES20.glActiveTexture(GLES20.GL_TEXTURE0); + GLES20.glBindTexture(mTextureTarget, textureId); + + // Copy the model / view / projection matrix over. + GLES20.glUniformMatrix4fv(muMVPMatrixLoc, 1, false, mvpMatrix, 0); + GlUtil.checkGlError("glUniformMatrix4fv"); + + // Copy the texture transformation matrix over. + GLES20.glUniformMatrix4fv(muTexMatrixLoc, 1, false, texMatrix, 0); + GlUtil.checkGlError("glUniformMatrix4fv"); + + // Enable the "aPosition" vertex attribute. + GLES20.glEnableVertexAttribArray(maPositionLoc); + GlUtil.checkGlError("glEnableVertexAttribArray"); + + // Connect vertexBuffer to "aPosition". + GLES20.glVertexAttribPointer(maPositionLoc, coordsPerVertex, + GLES20.GL_FLOAT, false, vertexStride, vertexBuffer); + GlUtil.checkGlError("glVertexAttribPointer"); + + // Enable the "aTextureCoord" vertex attribute. + GLES20.glEnableVertexAttribArray(maTextureCoordLoc); + GlUtil.checkGlError("glEnableVertexAttribArray"); + + // Connect texBuffer to "aTextureCoord". + GLES20.glVertexAttribPointer(maTextureCoordLoc, 2, + GLES20.GL_FLOAT, false, texStride, texBuffer); + GlUtil.checkGlError("glVertexAttribPointer"); + + // Populate the convolution kernel, if present. + if (muKernelLoc >= 0) { + GLES20.glUniform1fv(muKernelLoc, KERNEL_SIZE, mKernel, 0); + GLES20.glUniform2fv(muTexOffsetLoc, KERNEL_SIZE, mTexOffset, 0); + GLES20.glUniform1f(muColorAdjustLoc, mColorAdjust); + } + + // Draw the rect. + GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, firstVertex, vertexCount); + GlUtil.checkGlError("glDrawArrays"); + + // Done -- disable vertex array, texture, and program. + GLES20.glDisableVertexAttribArray(maPositionLoc); + GLES20.glDisableVertexAttribArray(maTextureCoordLoc); + GLES20.glBindTexture(mTextureTarget, 0); + GLES20.glUseProgram(0); + } +} diff --git a/app/src/main/java/com/fpvout/digiview/vr/gles/WindowSurface.java b/app/src/main/java/com/fpvout/digiview/vr/gles/WindowSurface.java new file mode 100644 index 0000000..5087e18 --- /dev/null +++ b/app/src/main/java/com/fpvout/digiview/vr/gles/WindowSurface.java @@ -0,0 +1,90 @@ +/* + * Copyright 2013 Google Inc. All rights reserved. + * + * 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.fpvout.digiview.vr.gles; + +import android.graphics.SurfaceTexture; +import android.view.Surface; + +/** + * Recordable EGL window surface. + *

+ * It's good practice to explicitly release() the surface, preferably from a "finally" block. + */ +public class WindowSurface extends EglSurfaceBase { + private Surface mSurface; + private boolean mReleaseSurface; + + /** + * Associates an EGL surface with the native window surface. + *

+ * Set releaseSurface to true if you want the Surface to be released when release() is + * called. This is convenient, but can interfere with framework classes that expect to + * manage the Surface themselves (e.g. if you release a SurfaceView's Surface, the + * surfaceDestroyed() callback won't fire). + */ + public WindowSurface(EglCore eglCore, Surface surface, boolean releaseSurface) { + super(eglCore); + createWindowSurface(surface); + mSurface = surface; + mReleaseSurface = releaseSurface; + } + + /** + * Associates an EGL surface with the SurfaceTexture. + */ + public WindowSurface(EglCore eglCore, SurfaceTexture surfaceTexture) { + super(eglCore); + createWindowSurface(surfaceTexture); + } + + /** + * Releases any resources associated with the EGL surface (and, if configured to do so, + * with the Surface as well). + *

+ * Does not require that the surface's EGL context be current. + */ + public void release() { + releaseEglSurface(); + if (mSurface != null) { + if (mReleaseSurface) { + mSurface.release(); + } + mSurface = null; + } + } + + /** + * Recreate the EGLSurface, using the new EglBase. The caller should have already + * freed the old EGLSurface with releaseEglSurface(). + *

+ * This is useful when we want to update the EGLSurface associated with a Surface. + * For example, if we want to share with a different EGLContext, which can only + * be done by tearing down and recreating the context. (That's handled by the caller; + * this just creates a new EGLSurface for the Surface we were handed earlier.) + *

+ * If the previous EGLSurface isn't fully destroyed, e.g. it's still current on a + * context somewhere, the create call will fail with complaints from the Surface + * about already being connected. + */ + public void recreate(EglCore newEglCore) { + if (mSurface == null) { + throw new RuntimeException("not yet implemented for SurfaceTexture"); + } + mEglCore = newEglCore; // switch to new context + createWindowSurface(mSurface); // create new surface + } +} diff --git a/app/src/main/java/com/fpvout/digiview/vr/views/VrView.java b/app/src/main/java/com/fpvout/digiview/vr/views/VrView.java new file mode 100644 index 0000000..87adfc4 --- /dev/null +++ b/app/src/main/java/com/fpvout/digiview/vr/views/VrView.java @@ -0,0 +1,190 @@ +package com.fpvout.digiview.vr.views; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.SurfaceTexture; +import android.graphics.drawable.Drawable; +import android.opengl.GLES20; +import android.text.TextPaint; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.Surface; +import android.view.SurfaceHolder; +import android.view.SurfaceView; +import android.view.View; +import android.widget.FrameLayout; + +import androidx.annotation.NonNull; +import androidx.constraintlayout.widget.ConstraintLayout; + +import com.fpvout.digiview.R; +import com.fpvout.digiview.VideoReaderExoplayer; +import com.fpvout.digiview.vr.gles.EglCore; +import com.fpvout.digiview.vr.gles.FullFrameRect; +import com.fpvout.digiview.vr.gles.Texture2dProgram; +import com.fpvout.digiview.vr.gles.WindowSurface; +import com.google.android.exoplayer2.SimpleExoPlayer; + +public class VrView extends FrameLayout implements SurfaceTexture.OnFrameAvailableListener { + private SurfaceView surfaceViewLeft; + private SurfaceView surfaceViewRight; + private SimpleExoPlayer mPlayer; + private EglCore eglCore; + private FullFrameRect fullFrameBlit; + private int textureId = 0; + private SurfaceTexture videoSurfaceTexture; + private final float[] transformMatrix = new float[16]; + private WindowSurface mainDisplaySurface; + private WindowSurface secondaryDisplaySurface; + private Surface surface; + + public VrView(Context context) { + super(context); + init(context); + } + + public VrView(Context context, AttributeSet attrs) { + super(context, attrs); + init(context); + } + + public VrView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + init(context); + } + + private void init(Context context) { + LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + inflater.inflate(R.layout.vr_view, this); + surfaceViewLeft = findViewById(R.id.surfaceViewLeft); + surfaceViewRight = findViewById(R.id.surfaceViewRight); + } + + public void setAspect(int dividend, int divisor) { + ConstraintLayout.LayoutParams leftParams = (ConstraintLayout.LayoutParams) surfaceViewLeft.getLayoutParams(); + ConstraintLayout.LayoutParams rightParams = (ConstraintLayout.LayoutParams) surfaceViewRight.getLayoutParams(); + String dimensionString = "H," + dividend + ":" + divisor; + leftParams.dimensionRatio = dimensionString; + rightParams.dimensionRatio = dimensionString; + } + + public void start(SimpleExoPlayer mPlayer) { + this.mPlayer = mPlayer; + surfaceViewLeft.getHolder().addCallback(videoSurfaceCallbackLeft); + surfaceViewRight.getHolder().addCallback(videoSurfaceCallbackRight); + surfaceViewLeft.setVisibility(View.GONE); + surfaceViewRight.setVisibility(View.GONE); + surfaceViewLeft.post(new Runnable() { + @Override + public void run() { + surfaceViewLeft.setVisibility(View.VISIBLE); + surfaceViewRight.setVisibility(View.VISIBLE); + } + }); + } + + public void stop() { + surfaceViewLeft.getHolder().removeCallback(videoSurfaceCallbackLeft); + surfaceViewRight.getHolder().removeCallback(videoSurfaceCallbackRight); + if (surface != null) { + surface.release(); + surface = null; + } + + if (videoSurfaceTexture != null) { + videoSurfaceTexture.release(); + videoSurfaceTexture = null; + } + + if (mainDisplaySurface != null) { + mainDisplaySurface.release(); + mainDisplaySurface = null; + } + + if (secondaryDisplaySurface != null) { + secondaryDisplaySurface.release(); + secondaryDisplaySurface = null; + } + + if (fullFrameBlit != null) { + fullFrameBlit.release(false); + fullFrameBlit = null; + } + + if (eglCore != null) { + eglCore.release(); + eglCore = null; + } + } + + private SurfaceHolder.Callback videoSurfaceCallbackLeft = new SurfaceHolder.Callback() { + @Override + public void surfaceCreated(@NonNull SurfaceHolder holder) { + eglCore = new EglCore(); + + mainDisplaySurface = new WindowSurface(eglCore, holder.getSurface(), false); + mainDisplaySurface.makeCurrent(); + + fullFrameBlit = new FullFrameRect(new Texture2dProgram(Texture2dProgram.ProgramType.TEXTURE_EXT)); + textureId = fullFrameBlit.createTextureObject(); + videoSurfaceTexture = new SurfaceTexture(textureId); + videoSurfaceTexture.setOnFrameAvailableListener(VrView.this); + surface = new Surface(videoSurfaceTexture); + mPlayer.setVideoSurface(surface); + } + + @Override + public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) { + + } + + @Override + public void surfaceDestroyed(@NonNull SurfaceHolder holder) { + + } + }; + + private SurfaceHolder.Callback videoSurfaceCallbackRight = new SurfaceHolder.Callback() { + @Override + public void surfaceCreated(@NonNull SurfaceHolder holder) { + secondaryDisplaySurface = new WindowSurface(eglCore, holder.getSurface(), false); + } + + @Override + public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) { + + } + + @Override + public void surfaceDestroyed(@NonNull SurfaceHolder holder) { + + } + }; + + @Override + public void onFrameAvailable(SurfaceTexture surfaceTexture) { + if (eglCore == null) { + return; + } + + if (mainDisplaySurface != null) { + drawFrame(mainDisplaySurface, surfaceViewLeft.getWidth(), surfaceViewLeft.getHeight()); + } + + if (secondaryDisplaySurface != null) { + drawFrame(secondaryDisplaySurface, secondaryDisplaySurface.getWidth(), secondaryDisplaySurface.getHeight()); + } + } + + private void drawFrame(WindowSurface windowSurface, int viewWidth, int viewHeight) { + windowSurface.makeCurrent(); + videoSurfaceTexture.updateTexImage(); + videoSurfaceTexture.getTransformMatrix(transformMatrix); + GLES20.glViewport(0, 0, viewWidth, viewHeight); + fullFrameBlit.drawFrame(textureId, transformMatrix); + windowSurface.swapBuffers(); + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 3d2145d..11bda30 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -19,6 +19,17 @@ app:layout_constraintTop_toTopOf="parent"> + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dba2270..23a68ff 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -9,7 +9,7 @@ Data Collection Agreement No Thanks Sure! - Report crash data to the DigiView team? + Report crash data to the DigiView team? This app will probably crash less than your drone, but if it does, we\'d like to know! Settings @@ -28,6 +28,8 @@ Video Player Full Screen Can also be toggled by double-tapping or pinching on the video player. + Enable VR Goggle Mode + When enabled, renders split screen video for each eye for use in VR phone holders like Google Cardboard. Show DigiView watermark Links Our Website diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index 828f778..d17a276 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -40,6 +40,12 @@ app:defaultValue="true" app:summary="@string/full_screen_summary" /> + +