magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | package org.webrtc; |
| 12 | |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 13 | import android.graphics.Bitmap; |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 14 | import android.graphics.Matrix; |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 15 | import android.graphics.SurfaceTexture; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 16 | import android.opengl.GLES20; |
| 17 | import android.os.Handler; |
| 18 | import android.os.HandlerThread; |
| 19 | import android.os.Looper; |
| 20 | import android.view.Surface; |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 21 | import java.nio.ByteBuffer; |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.Iterator; |
sakal | 037b93a | 2017-01-16 04:57:32 -0800 | [diff] [blame] | 24 | import java.util.Locale; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 25 | import java.util.concurrent.CountDownLatch; |
| 26 | import java.util.concurrent.TimeUnit; |
| 27 | |
| 28 | /** |
| 29 | * Implements org.webrtc.VideoRenderer.Callbacks by displaying the video stream on an EGL Surface. |
| 30 | * This class is intended to be used as a helper class for rendering on SurfaceViews and |
| 31 | * TextureViews. |
| 32 | */ |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 33 | public class EglRenderer implements VideoRenderer.Callbacks, VideoSink { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 34 | private static final String TAG = "EglRenderer"; |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 35 | private static final long LOG_INTERVAL_SEC = 4; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 36 | private static final int MAX_SURFACE_CLEAR_COUNT = 3; |
| 37 | |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 38 | public interface FrameListener { void onFrame(Bitmap frame); } |
| 39 | |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 40 | private static class FrameListenerAndParams { |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 41 | public final FrameListener listener; |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 42 | public final float scale; |
| 43 | public final RendererCommon.GlDrawer drawer; |
sakal | 8fdf957 | 2017-05-31 02:43:10 -0700 | [diff] [blame] | 44 | public final boolean applyFpsReduction; |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 45 | |
sakal | 8fdf957 | 2017-05-31 02:43:10 -0700 | [diff] [blame] | 46 | public FrameListenerAndParams(FrameListener listener, float scale, |
| 47 | RendererCommon.GlDrawer drawer, boolean applyFpsReduction) { |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 48 | this.listener = listener; |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 49 | this.scale = scale; |
| 50 | this.drawer = drawer; |
sakal | 8fdf957 | 2017-05-31 02:43:10 -0700 | [diff] [blame] | 51 | this.applyFpsReduction = applyFpsReduction; |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 55 | private class EglSurfaceCreation implements Runnable { |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 56 | private Object surface; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 57 | |
Mirko Bonadei | 12251b6 | 2017-11-05 19:35:31 -0800 | [diff] [blame^] | 58 | // TODO(bugs.webrtc.org/8491): Remove NoSynchronizedMethodCheck suppression. |
| 59 | @SuppressWarnings("NoSynchronizedMethodCheck") |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 60 | public synchronized void setSurface(Object surface) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 61 | this.surface = surface; |
| 62 | } |
| 63 | |
| 64 | @Override |
Mirko Bonadei | 12251b6 | 2017-11-05 19:35:31 -0800 | [diff] [blame^] | 65 | // TODO(bugs.webrtc.org/8491): Remove NoSynchronizedMethodCheck suppression. |
| 66 | @SuppressWarnings("NoSynchronizedMethodCheck") |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 67 | public synchronized void run() { |
| 68 | if (surface != null && eglBase != null && !eglBase.hasSurface()) { |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 69 | if (surface instanceof Surface) { |
| 70 | eglBase.createSurface((Surface) surface); |
| 71 | } else if (surface instanceof SurfaceTexture) { |
| 72 | eglBase.createSurface((SurfaceTexture) surface); |
| 73 | } else { |
| 74 | throw new IllegalStateException("Invalid surface: " + surface); |
| 75 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 76 | eglBase.makeCurrent(); |
| 77 | // Necessary for YUV frames with odd width. |
| 78 | GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
Xiaolei Yu | 149533a | 2017-11-03 07:55:01 +0800 | [diff] [blame] | 83 | protected final String name; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 84 | |
| 85 | // |renderThreadHandler| is a handler for communicating with |renderThread|, and is synchronized |
| 86 | // on |handlerLock|. |
| 87 | private final Object handlerLock = new Object(); |
| 88 | private Handler renderThreadHandler; |
| 89 | |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 90 | private final ArrayList<FrameListenerAndParams> frameListeners = new ArrayList<>(); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 91 | |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 92 | // Variables for fps reduction. |
| 93 | private final Object fpsReductionLock = new Object(); |
| 94 | // Time for when next frame should be rendered. |
| 95 | private long nextFrameTimeNs; |
| 96 | // Minimum duration between frames when fps reduction is active, or -1 if video is completely |
| 97 | // paused. |
| 98 | private long minRenderPeriodNs; |
| 99 | |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 100 | // EGL and GL resources for drawing YUV/OES textures. After initilization, these are only accessed |
| 101 | // from the render thread. |
| 102 | private EglBase eglBase; |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 103 | private final VideoFrameDrawer frameDrawer = new VideoFrameDrawer(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 104 | private RendererCommon.GlDrawer drawer; |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 105 | private final Matrix drawMatrix = new Matrix(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 106 | |
| 107 | // Pending frame to render. Serves as a queue with size 1. Synchronized on |frameLock|. |
| 108 | private final Object frameLock = new Object(); |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 109 | private VideoFrame pendingFrame; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 110 | |
| 111 | // These variables are synchronized on |layoutLock|. |
| 112 | private final Object layoutLock = new Object(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 113 | private float layoutAspectRatio; |
| 114 | // If true, mirrors the video stream horizontally. |
| 115 | private boolean mirror; |
| 116 | |
| 117 | // These variables are synchronized on |statisticsLock|. |
| 118 | private final Object statisticsLock = new Object(); |
| 119 | // Total number of video frames received in renderFrame() call. |
| 120 | private int framesReceived; |
| 121 | // Number of video frames dropped by renderFrame() because previous frame has not been rendered |
| 122 | // yet. |
| 123 | private int framesDropped; |
| 124 | // Number of rendered video frames. |
| 125 | private int framesRendered; |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 126 | // Start time for counting these statistics, or 0 if we haven't started measuring yet. |
| 127 | private long statisticsStartTimeNs; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 128 | // Time in ns spent in renderFrameOnRenderThread() function. |
| 129 | private long renderTimeNs; |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 130 | // Time in ns spent by the render thread in the swapBuffers() function. |
| 131 | private long renderSwapBufferTimeNs; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 132 | |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 133 | // Used for bitmap capturing. |
| 134 | private GlTextureFrameBuffer bitmapTextureFramebuffer; |
| 135 | |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 136 | private final Runnable logStatisticsRunnable = new Runnable() { |
| 137 | @Override |
| 138 | public void run() { |
| 139 | logStatistics(); |
| 140 | synchronized (handlerLock) { |
| 141 | if (renderThreadHandler != null) { |
| 142 | renderThreadHandler.removeCallbacks(logStatisticsRunnable); |
| 143 | renderThreadHandler.postDelayed( |
| 144 | logStatisticsRunnable, TimeUnit.SECONDS.toMillis(LOG_INTERVAL_SEC)); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | }; |
| 149 | |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 150 | private final EglSurfaceCreation eglSurfaceCreationRunnable = new EglSurfaceCreation(); |
| 151 | |
| 152 | /** |
| 153 | * Standard constructor. The name will be used for the render thread name and included when |
| 154 | * logging. In order to render something, you must first call init() and createEglSurface. |
| 155 | */ |
| 156 | public EglRenderer(String name) { |
| 157 | this.name = name; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Initialize this class, sharing resources with |sharedContext|. The custom |drawer| will be used |
| 162 | * for drawing frames on the EGLSurface. This class is responsible for calling release() on |
| 163 | * |drawer|. It is allowed to call init() to reinitialize the renderer after a previous |
| 164 | * init()/release() cycle. |
| 165 | */ |
| 166 | public void init(final EglBase.Context sharedContext, final int[] configAttributes, |
| 167 | RendererCommon.GlDrawer drawer) { |
| 168 | synchronized (handlerLock) { |
| 169 | if (renderThreadHandler != null) { |
| 170 | throw new IllegalStateException(name + "Already initialized"); |
| 171 | } |
| 172 | logD("Initializing EglRenderer"); |
| 173 | this.drawer = drawer; |
| 174 | |
| 175 | final HandlerThread renderThread = new HandlerThread(name + "EglRenderer"); |
| 176 | renderThread.start(); |
| 177 | renderThreadHandler = new Handler(renderThread.getLooper()); |
| 178 | // Create EGL context on the newly created render thread. It should be possibly to create the |
| 179 | // context on this thread and make it current on the render thread, but this causes failure on |
| 180 | // some Marvel based JB devices. https://bugs.chromium.org/p/webrtc/issues/detail?id=6350. |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 181 | ThreadUtils.invokeAtFrontUninterruptibly(renderThreadHandler, () -> { |
| 182 | // If sharedContext is null, then texture frames are disabled. This is typically for old |
| 183 | // devices that might not be fully spec compliant, so force EGL 1.0 since EGL 1.4 has |
| 184 | // caused trouble on some weird devices. |
| 185 | if (sharedContext == null) { |
| 186 | logD("EglBase10.create context"); |
| 187 | eglBase = EglBase.createEgl10(configAttributes); |
| 188 | } else { |
| 189 | logD("EglBase.create shared context"); |
| 190 | eglBase = EglBase.create(sharedContext, configAttributes); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 191 | } |
| 192 | }); |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 193 | renderThreadHandler.post(eglSurfaceCreationRunnable); |
| 194 | final long currentTimeNs = System.nanoTime(); |
| 195 | resetStatistics(currentTimeNs); |
| 196 | renderThreadHandler.postDelayed( |
| 197 | logStatisticsRunnable, TimeUnit.SECONDS.toMillis(LOG_INTERVAL_SEC)); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | public void createEglSurface(Surface surface) { |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 202 | createEglSurfaceInternal(surface); |
| 203 | } |
| 204 | |
| 205 | public void createEglSurface(SurfaceTexture surfaceTexture) { |
| 206 | createEglSurfaceInternal(surfaceTexture); |
| 207 | } |
| 208 | |
| 209 | private void createEglSurfaceInternal(Object surface) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 210 | eglSurfaceCreationRunnable.setSurface(surface); |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 211 | postToRenderThread(eglSurfaceCreationRunnable); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Block until any pending frame is returned and all GL resources released, even if an interrupt |
| 216 | * occurs. If an interrupt occurs during release(), the interrupt flag will be set. This function |
| 217 | * should be called before the Activity is destroyed and the EGLContext is still valid. If you |
| 218 | * don't call this function, the GL resources might leak. |
| 219 | */ |
| 220 | public void release() { |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 221 | logD("Releasing."); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 222 | final CountDownLatch eglCleanupBarrier = new CountDownLatch(1); |
| 223 | synchronized (handlerLock) { |
| 224 | if (renderThreadHandler == null) { |
| 225 | logD("Already released"); |
| 226 | return; |
| 227 | } |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 228 | renderThreadHandler.removeCallbacks(logStatisticsRunnable); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 229 | // Release EGL and GL resources on render thread. |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 230 | renderThreadHandler.postAtFrontOfQueue(() -> { |
| 231 | if (drawer != null) { |
| 232 | drawer.release(); |
| 233 | drawer = null; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 234 | } |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 235 | frameDrawer.release(); |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 236 | if (bitmapTextureFramebuffer != null) { |
| 237 | bitmapTextureFramebuffer.release(); |
| 238 | bitmapTextureFramebuffer = null; |
| 239 | } |
| 240 | if (eglBase != null) { |
| 241 | logD("eglBase detach and release."); |
| 242 | eglBase.detachCurrent(); |
| 243 | eglBase.release(); |
| 244 | eglBase = null; |
| 245 | } |
| 246 | eglCleanupBarrier.countDown(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 247 | }); |
| 248 | final Looper renderLooper = renderThreadHandler.getLooper(); |
| 249 | // TODO(magjed): Replace this post() with renderLooper.quitSafely() when API support >= 18. |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 250 | renderThreadHandler.post(() -> { |
| 251 | logD("Quitting render thread."); |
| 252 | renderLooper.quit(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 253 | }); |
| 254 | // Don't accept any more frames or messages to the render thread. |
| 255 | renderThreadHandler = null; |
| 256 | } |
| 257 | // Make sure the EGL/GL cleanup posted above is executed. |
| 258 | ThreadUtils.awaitUninterruptibly(eglCleanupBarrier); |
| 259 | synchronized (frameLock) { |
| 260 | if (pendingFrame != null) { |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 261 | pendingFrame.release(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 262 | pendingFrame = null; |
| 263 | } |
| 264 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 265 | logD("Releasing done."); |
| 266 | } |
| 267 | |
| 268 | /** |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 269 | * Reset the statistics logged in logStatistics(). |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 270 | */ |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 271 | private void resetStatistics(long currentTimeNs) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 272 | synchronized (statisticsLock) { |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 273 | statisticsStartTimeNs = currentTimeNs; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 274 | framesReceived = 0; |
| 275 | framesDropped = 0; |
| 276 | framesRendered = 0; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 277 | renderTimeNs = 0; |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 278 | renderSwapBufferTimeNs = 0; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | public void printStackTrace() { |
| 283 | synchronized (handlerLock) { |
| 284 | final Thread renderThread = |
| 285 | (renderThreadHandler == null) ? null : renderThreadHandler.getLooper().getThread(); |
| 286 | if (renderThread != null) { |
| 287 | final StackTraceElement[] renderStackTrace = renderThread.getStackTrace(); |
| 288 | if (renderStackTrace.length > 0) { |
| 289 | logD("EglRenderer stack trace:"); |
| 290 | for (StackTraceElement traceElem : renderStackTrace) { |
| 291 | logD(traceElem.toString()); |
| 292 | } |
| 293 | } |
| 294 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Set if the video stream should be mirrored or not. |
| 300 | */ |
| 301 | public void setMirror(final boolean mirror) { |
| 302 | logD("setMirror: " + mirror); |
| 303 | synchronized (layoutLock) { |
| 304 | this.mirror = mirror; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Set layout aspect ratio. This is used to crop frames when rendering to avoid stretched video. |
| 310 | * Set this to 0 to disable cropping. |
| 311 | */ |
| 312 | public void setLayoutAspectRatio(float layoutAspectRatio) { |
| 313 | logD("setLayoutAspectRatio: " + layoutAspectRatio); |
| 314 | synchronized (layoutLock) { |
| 315 | this.layoutAspectRatio = layoutAspectRatio; |
| 316 | } |
| 317 | } |
| 318 | |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 319 | /** |
| 320 | * Limit render framerate. |
| 321 | * |
| 322 | * @param fps Limit render framerate to this value, or use Float.POSITIVE_INFINITY to disable fps |
| 323 | * reduction. |
| 324 | */ |
| 325 | public void setFpsReduction(float fps) { |
| 326 | logD("setFpsReduction: " + fps); |
| 327 | synchronized (fpsReductionLock) { |
| 328 | final long previousRenderPeriodNs = minRenderPeriodNs; |
| 329 | if (fps <= 0) { |
| 330 | minRenderPeriodNs = Long.MAX_VALUE; |
| 331 | } else { |
| 332 | minRenderPeriodNs = (long) (TimeUnit.SECONDS.toNanos(1) / fps); |
| 333 | } |
| 334 | if (minRenderPeriodNs != previousRenderPeriodNs) { |
| 335 | // Fps reduction changed - reset frame time. |
| 336 | nextFrameTimeNs = System.nanoTime(); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | public void disableFpsReduction() { |
| 342 | setFpsReduction(Float.POSITIVE_INFINITY /* fps */); |
| 343 | } |
| 344 | |
| 345 | public void pauseVideo() { |
| 346 | setFpsReduction(0 /* fps */); |
| 347 | } |
| 348 | |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 349 | /** |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 350 | * Register a callback to be invoked when a new video frame has been received. This version uses |
| 351 | * the drawer of the EglRenderer that was passed in init. |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 352 | * |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 353 | * @param listener The callback to be invoked. The callback will be invoked on the render thread. |
| 354 | * It should be lightweight and must not call removeFrameListener. |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 355 | * @param scale The scale of the Bitmap passed to the callback, or 0 if no Bitmap is |
| 356 | * required. |
| 357 | */ |
sakal | bb58435 | 2016-11-28 08:53:44 -0800 | [diff] [blame] | 358 | public void addFrameListener(final FrameListener listener, final float scale) { |
sakal | 8fdf957 | 2017-05-31 02:43:10 -0700 | [diff] [blame] | 359 | addFrameListener(listener, scale, null, false /* applyFpsReduction */); |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Register a callback to be invoked when a new video frame has been received. |
| 364 | * |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 365 | * @param listener The callback to be invoked. The callback will be invoked on the render thread. |
| 366 | * It should be lightweight and must not call removeFrameListener. |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 367 | * @param scale The scale of the Bitmap passed to the callback, or 0 if no Bitmap is |
| 368 | * required. |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 369 | * @param drawer Custom drawer to use for this frame listener or null to use the default one. |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 370 | */ |
| 371 | public void addFrameListener( |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 372 | final FrameListener listener, final float scale, final RendererCommon.GlDrawer drawerParam) { |
sakal | 8fdf957 | 2017-05-31 02:43:10 -0700 | [diff] [blame] | 373 | addFrameListener(listener, scale, drawerParam, false /* applyFpsReduction */); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Register a callback to be invoked when a new video frame has been received. |
| 378 | * |
| 379 | * @param listener The callback to be invoked. The callback will be invoked on the render thread. |
| 380 | * It should be lightweight and must not call removeFrameListener. |
| 381 | * @param scale The scale of the Bitmap passed to the callback, or 0 if no Bitmap is |
| 382 | * required. |
| 383 | * @param drawer Custom drawer to use for this frame listener or null to use the default one. |
| 384 | * @param applyFpsReduction This callback will not be called for frames that have been dropped by |
| 385 | * FPS reduction. |
| 386 | */ |
| 387 | public void addFrameListener(final FrameListener listener, final float scale, |
| 388 | final RendererCommon.GlDrawer drawerParam, final boolean applyFpsReduction) { |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 389 | postToRenderThread(() -> { |
| 390 | final RendererCommon.GlDrawer listenerDrawer = drawerParam == null ? drawer : drawerParam; |
| 391 | frameListeners.add( |
| 392 | new FrameListenerAndParams(listener, scale, listenerDrawer, applyFpsReduction)); |
sakal | bb58435 | 2016-11-28 08:53:44 -0800 | [diff] [blame] | 393 | }); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Remove any pending callback that was added with addFrameListener. If the callback is not in |
sakal | bb58435 | 2016-11-28 08:53:44 -0800 | [diff] [blame] | 398 | * the queue, nothing happens. It is ensured that callback won't be called after this method |
| 399 | * returns. |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 400 | * |
| 401 | * @param runnable The callback to remove. |
| 402 | */ |
sakal | bb58435 | 2016-11-28 08:53:44 -0800 | [diff] [blame] | 403 | public void removeFrameListener(final FrameListener listener) { |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 404 | if (Thread.currentThread() == renderThreadHandler.getLooper().getThread()) { |
| 405 | throw new RuntimeException("removeFrameListener must not be called on the render thread."); |
| 406 | } |
sakal | bb58435 | 2016-11-28 08:53:44 -0800 | [diff] [blame] | 407 | final CountDownLatch latch = new CountDownLatch(1); |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 408 | postToRenderThread(() -> { |
| 409 | latch.countDown(); |
| 410 | final Iterator<FrameListenerAndParams> iter = frameListeners.iterator(); |
| 411 | while (iter.hasNext()) { |
| 412 | if (iter.next().listener == listener) { |
| 413 | iter.remove(); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
sakal | bb58435 | 2016-11-28 08:53:44 -0800 | [diff] [blame] | 416 | }); |
| 417 | ThreadUtils.awaitUninterruptibly(latch); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 418 | } |
| 419 | |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 420 | // VideoRenderer.Callbacks interface. |
| 421 | @Override |
| 422 | public void renderFrame(VideoRenderer.I420Frame frame) { |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 423 | VideoFrame videoFrame = frame.toVideoFrame(); |
| 424 | onFrame(videoFrame); |
| 425 | videoFrame.release(); |
| 426 | } |
| 427 | |
| 428 | // VideoSink interface. |
| 429 | @Override |
| 430 | public void onFrame(VideoFrame frame) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 431 | synchronized (statisticsLock) { |
| 432 | ++framesReceived; |
| 433 | } |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 434 | final boolean dropOldFrame; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 435 | synchronized (handlerLock) { |
| 436 | if (renderThreadHandler == null) { |
| 437 | logD("Dropping frame - Not initialized or already released."); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 438 | return; |
| 439 | } |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 440 | synchronized (frameLock) { |
| 441 | dropOldFrame = (pendingFrame != null); |
| 442 | if (dropOldFrame) { |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 443 | pendingFrame.release(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 444 | } |
| 445 | pendingFrame = frame; |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 446 | pendingFrame.retain(); |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 447 | renderThreadHandler.post(this ::renderFrameOnRenderThread); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 448 | } |
| 449 | } |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 450 | if (dropOldFrame) { |
| 451 | synchronized (statisticsLock) { |
| 452 | ++framesDropped; |
| 453 | } |
| 454 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Release EGL surface. This function will block until the EGL surface is released. |
| 459 | */ |
sakal | 28ec6bd | 2016-11-09 01:47:12 -0800 | [diff] [blame] | 460 | public void releaseEglSurface(final Runnable completionCallback) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 461 | // Ensure that the render thread is no longer touching the Surface before returning from this |
| 462 | // function. |
| 463 | eglSurfaceCreationRunnable.setSurface(null /* surface */); |
| 464 | synchronized (handlerLock) { |
| 465 | if (renderThreadHandler != null) { |
| 466 | renderThreadHandler.removeCallbacks(eglSurfaceCreationRunnable); |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 467 | renderThreadHandler.postAtFrontOfQueue(() -> { |
| 468 | if (eglBase != null) { |
| 469 | eglBase.detachCurrent(); |
| 470 | eglBase.releaseSurface(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 471 | } |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 472 | completionCallback.run(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 473 | }); |
sakal | 28ec6bd | 2016-11-09 01:47:12 -0800 | [diff] [blame] | 474 | return; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 475 | } |
| 476 | } |
sakal | 28ec6bd | 2016-11-09 01:47:12 -0800 | [diff] [blame] | 477 | completionCallback.run(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /** |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 481 | * Private helper function to post tasks safely. |
| 482 | */ |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 483 | private void postToRenderThread(Runnable runnable) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 484 | synchronized (handlerLock) { |
| 485 | if (renderThreadHandler != null) { |
| 486 | renderThreadHandler.post(runnable); |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
sakal | f25a220 | 2017-05-04 06:06:56 -0700 | [diff] [blame] | 491 | private void clearSurfaceOnRenderThread(float r, float g, float b, float a) { |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 492 | if (eglBase != null && eglBase.hasSurface()) { |
| 493 | logD("clearSurface"); |
sakal | f25a220 | 2017-05-04 06:06:56 -0700 | [diff] [blame] | 494 | GLES20.glClearColor(r, g, b, a); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 495 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); |
| 496 | eglBase.swapBuffers(); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | /** |
sakal | f25a220 | 2017-05-04 06:06:56 -0700 | [diff] [blame] | 501 | * Post a task to clear the surface to a transparent uniform color. |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 502 | */ |
| 503 | public void clearImage() { |
sakal | f25a220 | 2017-05-04 06:06:56 -0700 | [diff] [blame] | 504 | clearImage(0 /* red */, 0 /* green */, 0 /* blue */, 0 /* alpha */); |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Post a task to clear the surface to a specific color. |
| 509 | */ |
| 510 | public void clearImage(final float r, final float g, final float b, final float a) { |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 511 | synchronized (handlerLock) { |
| 512 | if (renderThreadHandler == null) { |
| 513 | return; |
| 514 | } |
sakal | bf08060 | 2017-08-11 01:42:43 -0700 | [diff] [blame] | 515 | renderThreadHandler.postAtFrontOfQueue(() -> clearSurfaceOnRenderThread(r, g, b, a)); |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | /** |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 520 | * Renders and releases |pendingFrame|. |
| 521 | */ |
| 522 | private void renderFrameOnRenderThread() { |
| 523 | // Fetch and render |pendingFrame|. |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 524 | final VideoFrame frame; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 525 | synchronized (frameLock) { |
| 526 | if (pendingFrame == null) { |
| 527 | return; |
| 528 | } |
| 529 | frame = pendingFrame; |
| 530 | pendingFrame = null; |
| 531 | } |
| 532 | if (eglBase == null || !eglBase.hasSurface()) { |
| 533 | logD("Dropping frame - No surface"); |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 534 | frame.release(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 535 | return; |
| 536 | } |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 537 | // Check if fps reduction is active. |
| 538 | final boolean shouldRenderFrame; |
| 539 | synchronized (fpsReductionLock) { |
| 540 | if (minRenderPeriodNs == Long.MAX_VALUE) { |
| 541 | // Rendering is paused. |
| 542 | shouldRenderFrame = false; |
| 543 | } else if (minRenderPeriodNs <= 0) { |
| 544 | // FPS reduction is disabled. |
| 545 | shouldRenderFrame = true; |
| 546 | } else { |
| 547 | final long currentTimeNs = System.nanoTime(); |
| 548 | if (currentTimeNs < nextFrameTimeNs) { |
| 549 | logD("Skipping frame rendering - fps reduction is active."); |
| 550 | shouldRenderFrame = false; |
| 551 | } else { |
| 552 | nextFrameTimeNs += minRenderPeriodNs; |
| 553 | // The time for the next frame should always be in the future. |
| 554 | nextFrameTimeNs = Math.max(nextFrameTimeNs, currentTimeNs); |
| 555 | shouldRenderFrame = true; |
| 556 | } |
| 557 | } |
| 558 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 559 | |
| 560 | final long startTimeNs = System.nanoTime(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 561 | |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 562 | final float frameAspectRatio = frame.getRotatedWidth() / (float) frame.getRotatedHeight(); |
| 563 | final float drawnAspectRatio; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 564 | synchronized (layoutLock) { |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 565 | drawnAspectRatio = layoutAspectRatio != 0f ? layoutAspectRatio : frameAspectRatio; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 566 | } |
| 567 | |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 568 | final float scaleX; |
| 569 | final float scaleY; |
| 570 | |
| 571 | if (frameAspectRatio > drawnAspectRatio) { |
| 572 | scaleX = drawnAspectRatio / frameAspectRatio; |
| 573 | scaleY = 1f; |
| 574 | } else { |
| 575 | scaleX = 1f; |
| 576 | scaleY = frameAspectRatio / drawnAspectRatio; |
| 577 | } |
| 578 | |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 579 | drawMatrix.reset(); |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 580 | drawMatrix.preTranslate(0.5f, 0.5f); |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 581 | if (mirror) |
| 582 | drawMatrix.preScale(-1f, 1f); |
| 583 | drawMatrix.preScale(scaleX, scaleY); |
| 584 | drawMatrix.preTranslate(-0.5f, -0.5f); |
| 585 | |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 586 | if (shouldRenderFrame) { |
| 587 | GLES20.glClearColor(0 /* red */, 0 /* green */, 0 /* blue */, 0 /* alpha */); |
| 588 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 589 | frameDrawer.drawFrame(frame, drawer, drawMatrix, 0 /* viewportX */, 0 /* viewportY */, |
| 590 | eglBase.surfaceWidth(), eglBase.surfaceHeight()); |
sakal | d151652 | 2017-03-13 05:11:48 -0700 | [diff] [blame] | 591 | |
| 592 | final long swapBuffersStartTimeNs = System.nanoTime(); |
| 593 | eglBase.swapBuffers(); |
| 594 | |
| 595 | final long currentTimeNs = System.nanoTime(); |
| 596 | synchronized (statisticsLock) { |
| 597 | ++framesRendered; |
| 598 | renderTimeNs += (currentTimeNs - startTimeNs); |
| 599 | renderSwapBufferTimeNs += (currentTimeNs - swapBuffersStartTimeNs); |
| 600 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 601 | } |
| 602 | |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 603 | notifyCallbacks(frame, shouldRenderFrame); |
| 604 | frame.release(); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 605 | } |
| 606 | |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 607 | private void notifyCallbacks(VideoFrame frame, boolean wasRendered) { |
sakal | bb58435 | 2016-11-28 08:53:44 -0800 | [diff] [blame] | 608 | if (frameListeners.isEmpty()) |
| 609 | return; |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 610 | |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 611 | drawMatrix.reset(); |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 612 | drawMatrix.preTranslate(0.5f, 0.5f); |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 613 | if (mirror) |
| 614 | drawMatrix.preScale(-1f, 1f); |
| 615 | drawMatrix.preScale(1f, -1f); // We want the output to be upside down for Bitmap. |
| 616 | drawMatrix.preTranslate(-0.5f, -0.5f); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 617 | |
sakal | 8fdf957 | 2017-05-31 02:43:10 -0700 | [diff] [blame] | 618 | Iterator<FrameListenerAndParams> it = frameListeners.iterator(); |
| 619 | while (it.hasNext()) { |
| 620 | FrameListenerAndParams listenerAndParams = it.next(); |
| 621 | if (!wasRendered && listenerAndParams.applyFpsReduction) { |
| 622 | continue; |
| 623 | } |
| 624 | it.remove(); |
| 625 | |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 626 | final int scaledWidth = (int) (listenerAndParams.scale * frame.getRotatedWidth()); |
| 627 | final int scaledHeight = (int) (listenerAndParams.scale * frame.getRotatedHeight()); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 628 | |
| 629 | if (scaledWidth == 0 || scaledHeight == 0) { |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 630 | listenerAndParams.listener.onFrame(null); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 631 | continue; |
| 632 | } |
| 633 | |
| 634 | if (bitmapTextureFramebuffer == null) { |
| 635 | bitmapTextureFramebuffer = new GlTextureFrameBuffer(GLES20.GL_RGBA); |
| 636 | } |
| 637 | bitmapTextureFramebuffer.setSize(scaledWidth, scaledHeight); |
| 638 | |
| 639 | GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, bitmapTextureFramebuffer.getFrameBufferId()); |
| 640 | GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, |
| 641 | GLES20.GL_TEXTURE_2D, bitmapTextureFramebuffer.getTextureId(), 0); |
| 642 | |
sakal | 103988d | 2017-02-17 09:59:01 -0800 | [diff] [blame] | 643 | GLES20.glClearColor(0 /* red */, 0 /* green */, 0 /* blue */, 0 /* alpha */); |
| 644 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 645 | frameDrawer.drawFrame(frame, listenerAndParams.drawer, drawMatrix, 0 /* viewportX */, |
| 646 | 0 /* viewportY */, scaledWidth, scaledHeight); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 647 | |
| 648 | final ByteBuffer bitmapBuffer = ByteBuffer.allocateDirect(scaledWidth * scaledHeight * 4); |
| 649 | GLES20.glViewport(0, 0, scaledWidth, scaledHeight); |
| 650 | GLES20.glReadPixels( |
| 651 | 0, 0, scaledWidth, scaledHeight, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, bitmapBuffer); |
| 652 | |
| 653 | GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0); |
| 654 | GlUtil.checkNoGLES2Error("EglRenderer.notifyCallbacks"); |
| 655 | |
| 656 | final Bitmap bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888); |
| 657 | bitmap.copyPixelsFromBuffer(bitmapBuffer); |
sakal | 3a9bc17 | 2016-11-30 08:30:05 -0800 | [diff] [blame] | 658 | listenerAndParams.listener.onFrame(bitmap); |
sakal | fb0c573 | 2016-11-03 09:15:34 -0700 | [diff] [blame] | 659 | } |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 660 | } |
| 661 | |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 662 | private String averageTimeAsString(long sumTimeNs, int count) { |
| 663 | return (count <= 0) ? "NA" : TimeUnit.NANOSECONDS.toMicros(sumTimeNs / count) + " μs"; |
| 664 | } |
| 665 | |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 666 | private void logStatistics() { |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 667 | final long currentTimeNs = System.nanoTime(); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 668 | synchronized (statisticsLock) { |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 669 | final long elapsedTimeNs = currentTimeNs - statisticsStartTimeNs; |
| 670 | if (elapsedTimeNs <= 0) { |
| 671 | return; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 672 | } |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 673 | final float renderFps = framesRendered * TimeUnit.SECONDS.toNanos(1) / (float) elapsedTimeNs; |
| 674 | logD("Duration: " + TimeUnit.NANOSECONDS.toMillis(elapsedTimeNs) + " ms." |
| 675 | + " Frames received: " + framesReceived + "." |
| 676 | + " Dropped: " + framesDropped + "." |
| 677 | + " Rendered: " + framesRendered + "." |
sakal | 037b93a | 2017-01-16 04:57:32 -0800 | [diff] [blame] | 678 | + " Render fps: " + String.format(Locale.US, "%.1f", renderFps) + "." |
magjed | 9ab8a18 | 2016-10-20 03:18:09 -0700 | [diff] [blame] | 679 | + " Average render time: " + averageTimeAsString(renderTimeNs, framesRendered) + "." |
| 680 | + " Average swapBuffer time: " |
| 681 | + averageTimeAsString(renderSwapBufferTimeNs, framesRendered) + "."); |
| 682 | resetStatistics(currentTimeNs); |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
| 686 | private void logD(String string) { |
| 687 | Logging.d(TAG, name + string); |
| 688 | } |
| 689 | } |