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