android.view.Choreographer Java Examples

The following examples show how to use android.view.Choreographer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FrameRateTracker.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void doFrame(long frameTimeNanos) {
  long   elapsedNanos = frameTimeNanos - lastFrameTimeNanos;
  double fps          = TimeUnit.SECONDS.toNanos(1) / (double) elapsedNanos;

  if (elapsedNanos > badFrameThresholdNanos) {
    if (consecutiveFrameWarnings < MAX_CONSECUTIVE_FRAME_LOGS) {
      long droppedFrames = elapsedNanos / idealTimePerFrameNanos;
      Log.w(TAG, String.format(Locale.ENGLISH, "Bad frame! Took %d ms (%d dropped frames, or %.2f FPS)", TimeUnit.NANOSECONDS.toMillis(elapsedNanos), droppedFrames, fps));
      consecutiveFrameWarnings++;
    }
  } else {
    consecutiveFrameWarnings = 0;
  }

  fpsData.add(fps);
  runningAverageFps.add(fps);

  lastFrameTimeNanos = frameTimeNanos;
  Choreographer.getInstance().postFrameCallback(this);
}
 
Example #2
Source File: ChorTestActivity.java    From grafika with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    setName("ChorRenderThread");

    Looper.prepare();

    mHandler = new Handler() {
        public void handleMessage(Message msg) {
            Log.d(TAG, "got message, quitting");
            Looper.myLooper().quit();
        }
    };
    Choreographer.getInstance().postFrameCallback(this);

    Looper.loop();
    Log.d(TAG, "looper quit");
    Choreographer.getInstance().removeFrameCallback(this);
}
 
Example #3
Source File: FPSFrameCallback.java    From StickyDecoration with Apache License 2.0 6 votes vote down vote up
@Override
public void doFrame(long frameTimeNanos) {
    if (!enabled) {
        destroy();
        return;
    }
    if (startSampleTimeInNs == 0) {
        startSampleTimeInNs = frameTimeNanos;
    }
    //是否完成一个周期
    boolean aCycleComplete = frameTimeNanos - startSampleTimeInNs > fpsConfig.getSampleTimeInNs();
    if (aCycleComplete) {
        if (fpsConfig.frameDataCallback != null) {
            fpsConfig.frameDataCallback.getFPS(dataSet);
        }
        collectSampleAndSend(frameTimeNanos);
    }
    dataSet.add(frameTimeNanos);
    Choreographer.getInstance().postFrameCallback(this);
}
 
Example #4
Source File: ZhuanlanApplication.java    From ZhuanLan with Apache License 2.0 6 votes vote down vote up
@Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        Tips.init(this);
        DataCenter.init(this, "zhuanlan.db");

//        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
//        .detectDiskReads()
//        .detectDiskWrites()
//        .detectNetwork()
//        .penaltyLog()
//        .build());
//
//        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
//        .detectActivityLeaks()
//        .detectLeakedSqlLiteObjects()
//        .penaltyLog()
//        .penaltyDeath()
//        .build());

        initStetho();

        Choreographer choreographer = Choreographer.getInstance();
        choreographer.postFrameCallback(FRAME_CALLBACK);
    }
 
Example #5
Source File: GLManager.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * GLコンテキスト上で実行されるChoreographer.FrameCallbackをpostする
 * @param callback
 * @param delayMs
 * @throws IllegalStateException
 */
public synchronized void postFrameCallbackDelayed(
	@NonNull final Choreographer.FrameCallback callback,
	final long delayMs) throws IllegalStateException {

	if (DEBUG) Log.v(TAG, "postFrameCallbackDelayed:");
	checkValid();
	if (isGLThread()) {
		// すでにGLスレッド上であれば直接実行
		Choreographer.getInstance().postFrameCallbackDelayed(callback, delayMs);
	} else {
		// 別スレッド上にいるならGLスレッド上へ投げる
		mGLHandler.post(new Runnable() {
			@Override
			public void run() {
				Choreographer.getInstance().postFrameCallbackDelayed(callback, delayMs);
			}
		});
	}
}
 
Example #6
Source File: GLManager.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 未実行のChoreographer.FrameCallbackがあれば取り除く
 * @param callback
 * @throws IllegalStateException
 */
public synchronized void removeFrameCallback(
	@NonNull final Choreographer.FrameCallback callback)
		throws IllegalStateException {

	if (DEBUG) Log.v(TAG, "removeFrameCallback:");
	checkValid();
	if (isGLThread()) {
		// すでにGLスレッド上であれば直接実行
		Choreographer.getInstance().removeFrameCallback(callback);
	} else {
		// 別スレッド上にいるならGLスレッド上へ投げる
		mGLHandler.post(new Runnable() {
			@Override
			public void run() {
				Choreographer.getInstance().removeFrameCallback(callback);
			}
		});
	}
}
 
Example #7
Source File: MainActivity.java    From Android-Tech with Apache License 2.0 6 votes vote down vote up
@Override
    public void doFrame(long frameTimeNanos) {
//        Log.d(TAG, "frameTimeNanos = " + frameTimeNanos);
        if (lastFrameTimeNanos == 0) {
            lastFrameTimeNanos = frameTimeNanos;
            Choreographer.getInstance().postFrameCallback(this);
            return;
        }

        currentFrameTimeNanos = frameTimeNanos;
        long value = (currentFrameTimeNanos - lastFrameTimeNanos) / 1000000;
        // 大于16ms表示发生卡顿
        if (value > 16) {
            Log.d(TAG, "currentFrameTimeNanos - lastFrameTimeNanos = " + (currentFrameTimeNanos - lastFrameTimeNanos) / 1000000);
        }
        lastFrameTimeNanos = currentFrameTimeNanos;
        Choreographer.getInstance().postFrameCallback(this);
    }
 
Example #8
Source File: AbstractDistributeTask.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
	 * ワーカースレッド終了時の処理(ここはまだワーカースレッド上)
	 */
	@WorkerThread
	protected final void handleOnStop() {
		if (DEBUG) Log.v(TAG, "onStop");
		if ((mEnableVSync) && (mChoreographerHandler != null)) {
			mChoreographerHandler.post(new Runnable() {
				@Override
				public void run() {
					Choreographer.getInstance().removeFrameCallback(mFrameCallback);
				}
			});
		}
		notifyParent(false);
		makeCurrent();
		internalOnStop();
		handleRemoveAll();
//		if (DEBUG) Log.v(TAG, "onStop:finished");
	}
 
Example #9
Source File: RecordFBOActivity.java    From grafika with Apache License 2.0 6 votes vote down vote up
@Override
public void surfaceCreated(SurfaceHolder holder) {
    Log.d(TAG, "surfaceCreated holder=" + holder);

    File outputFile = new File(getFilesDir(), "fbo-gl-recording.mp4");
    SurfaceView sv = (SurfaceView) findViewById(R.id.fboActivity_surfaceView);
    mRenderThread = new RenderThread(sv.getHolder(), new ActivityHandler(this), outputFile,
            MiscUtils.getDisplayRefreshNsec(this));
    mRenderThread.setName("RecordFBO GL render");
    mRenderThread.start();
    mRenderThread.waitUntilReady();
    mRenderThread.setRecordMethod(mSelectedRecordMethod);

    RenderHandler rh = mRenderThread.getHandler();
    if (rh != null) {
        rh.sendSurfaceCreated();
    }

    // start the draw events
    Choreographer.getInstance().postFrameCallback(this);
}
 
Example #10
Source File: WanAndroidApp.java    From Awesome-WanAndroid with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void getFPS() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        return;
    }
    Choreographer.getInstance().postFrameCallback(new Choreographer.FrameCallback() {
        @Override
        public void doFrame(long frameTimeNanos) {
            if (mStartFrameTime == 0) {
                mStartFrameTime = frameTimeNanos;
            }
            long interval = frameTimeNanos - mStartFrameTime;
            if (interval > MONITOR_INTERVAL_NANOS) {
                double fps = (((double) (mFrameCount * 1000L * 1000L)) / interval) * MAX_INTERVAL;
                Log.d(TAG, String.valueOf(fps));
                mFrameCount = 0;
                mStartFrameTime = 0;
            } else {
                ++mFrameCount;
            }

            Choreographer.getInstance().postFrameCallback(this);
        }
    });
}
 
Example #11
Source File: FrameRateTracker.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void doFrame(long frameTimeNanos) {
  double averageFps = 0;
  int    size       = fpsData.size();

  for (double fps : fpsData) {
    averageFps += fps / size;
  }

  if (averageFps < badIntervalThresholdFps) {
    if (consecutiveIntervalWarnings < MAX_CONSECUTIVE_INTERVAL_LOGS) {
      Log.w(TAG, String.format(Locale.ENGLISH, "Bad interval! Average of %.2f FPS over the last %d ms", averageFps, TimeUnit.NANOSECONDS.toMillis(frameTimeNanos - lastReportTimeNanos)));
      consecutiveIntervalWarnings++;
    }
  } else {
    consecutiveIntervalWarnings = 0;
  }

  lastReportTimeNanos = frameTimeNanos;
  updateRefreshRate();
  Choreographer.getInstance().postFrameCallbackDelayed(this, REPORTING_INTERVAL);
}
 
Example #12
Source File: RecordFBOActivity.java    From grafika with Apache License 2.0 5 votes vote down vote up
@Override
public void doFrame(long frameTimeNanos) {
    RenderHandler rh = mRenderThread.getHandler();
    if (rh != null) {
        Choreographer.getInstance().postFrameCallback(this);
        rh.sendDoFrame(frameTimeNanos);
    }
}
 
Example #13
Source File: AndroidSpringLooperFactory.java    From StackCardsView with Apache License 2.0 5 votes vote down vote up
public ChoreographerAndroidSpringLooper(Choreographer choreographer) {
  mChoreographer = choreographer;
  mFrameCallback = new Choreographer.FrameCallback() {
    @Override
    public void doFrame(long frameTimeNanos) {
      if (!mStarted || mSpringSystem == null) {
        return;
      }
      long currentTime = SystemClock.uptimeMillis();
      mSpringSystem.loop(currentTime - mLastTime);
      mLastTime = currentTime;
      mChoreographer.postFrameCallback(mFrameCallback);
    }
  };
}
 
Example #14
Source File: ChoreographerCompat.java    From StackCardsView with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Choreographer.FrameCallback getFrameCallback() {
  if (mFrameCallback == null) {
    mFrameCallback = new Choreographer.FrameCallback() {
      @Override
      public void doFrame(long frameTimeNanos) {
        FrameCallback.this.doFrame(frameTimeNanos);
      }
    };
  }
  return mFrameCallback;
}
 
Example #15
Source File: AndroidSpringLooperFactory.java    From KugouLayout with MIT License 5 votes vote down vote up
public ChoreographerAndroidSpringLooper(Choreographer choreographer) {
  mChoreographer = choreographer;
  mFrameCallback = new Choreographer.FrameCallback() {
    @Override
    public void doFrame(long frameTimeNanos) {
      if (!mStarted || mSpringSystem == null) {
        return;
      }
      long currentTime = SystemClock.uptimeMillis();
      mSpringSystem.loop(currentTime - mLastTime);
      mLastTime = currentTime;
      mChoreographer.postFrameCallback(mFrameCallback);
    }
  };
}
 
Example #16
Source File: AbsView.java    From narrate-android with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
protected void init() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        redrawCallback = new Choreographer.FrameCallback() {
            @Override
            public void doFrame(long frameTimeNanos) {
                redrawInternal();
            }
        };
    }
}
 
Example #17
Source File: AndroidSpringLooperFactory.java    From light-novel-library_Wenku8_Android with GNU General Public License v2.0 5 votes vote down vote up
public ChoreographerAndroidSpringLooper(Choreographer choreographer) {
  mChoreographer = choreographer;
  mFrameCallback = new Choreographer.FrameCallback() {
    @Override
    public void doFrame(long frameTimeNanos) {
      if (!mStarted || mSpringSystem == null) {
        return;
      }
      long currentTime = SystemClock.uptimeMillis();
      mSpringSystem.loop(currentTime - mLastTime);
      mLastTime = currentTime;
      mChoreographer.postFrameCallback(mFrameCallback);
    }
  };
}
 
Example #18
Source File: SMUtils.java    From GTTools with MIT License 5 votes vote down vote up
@Override
public void doFrame(long frameTimeNanos)
{
	if (!running) {
		return;
	}
	// 本工具类中只负责帧数的递增计数,需要在调用端控制计数器的清零时机
	i.incrementAndGet();

	Choreographer.getInstance().postFrameCallback(this);
}
 
Example #19
Source File: RecordFBOActivity.java    From pause-resume-video-recording with Apache License 2.0 5 votes vote down vote up
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    Log.d(TAG, "surfaceDestroyed holder=" + holder);

    // We need to wait for the render thread to shut down before continuing because we
    // don't want the Surface to disappear out from under it mid-render.  The frame
    // notifications will have been stopped back in onPause(), but there might have
    // been one in progress.
    //
    // TODO: the RenderThread doesn't currently wait for the encoder / muxer to stop,
    //       so we can't use this as an indication that the .mp4 file is complete.

    RenderHandler rh = mRenderThread.getHandler();
    if (rh != null) {
        rh.sendShutdown();
        try {
            mRenderThread.join();
        } catch (InterruptedException ie) {
            // not expected
            throw new RuntimeException("join was interrupted", ie);
        }
    }
    mRenderThread = null;
    mRecordingEnabled = false;

    // If the callback was posted, remove it.  Without this, we could get one more
    // call on doFrame().
    Choreographer.getInstance().removeFrameCallback(this);
    Log.d(TAG, "surfaceDestroyed complete");
}
 
Example #20
Source File: LottieValueAnimator.java    From lottie-android with Apache License 2.0 5 votes vote down vote up
@MainThread
protected void removeFrameCallback(boolean stopRunning) {
  Choreographer.getInstance().removeFrameCallback(this);
  if (stopRunning) {
    running = false;
  }
}
 
Example #21
Source File: RecordFBOActivity.java    From pause-resume-video-recording with Apache License 2.0 5 votes vote down vote up
@Override
protected void onResume() {
    super.onResume();

    // If we already have a Surface, we just need to resume the frame notifications.
    if (mRenderThread != null) {
        Log.d(TAG, "onResume re-hooking choreographer");
        Choreographer.getInstance().postFrameCallback(this);
    }

    updateControls();
}
 
Example #22
Source File: HardwareScalerActivity.java    From grafika with Apache License 2.0 5 votes vote down vote up
@Override
public void doFrame(long frameTimeNanos) {
    RenderHandler rh = mRenderThread.getHandler();
    if (rh != null) {
        Choreographer.getInstance().postFrameCallback(this);
        rh.sendDoFrame(frameTimeNanos);
    }
}
 
Example #23
Source File: AndroidSpringLooperFactory.java    From Viewer with Apache License 2.0 5 votes vote down vote up
public ChoreographerAndroidSpringLooper(Choreographer choreographer) {
  mChoreographer = choreographer;
  mFrameCallback = new Choreographer.FrameCallback() {
    @Override
    public void doFrame(long frameTimeNanos) {
      if (!mStarted || mSpringSystem == null) {
        return;
      }
      long currentTime = SystemClock.uptimeMillis();
      mSpringSystem.loop(currentTime - mLastTime);
      mLastTime = currentTime;
      mChoreographer.postFrameCallback(mFrameCallback);
    }
  };
}
 
Example #24
Source File: RecordFBOActivity.java    From grafika with Apache License 2.0 5 votes vote down vote up
@Override
protected void onPause() {
    super.onPause();

    // TODO: we might want to stop recording here.  As it is, we continue "recording",
    //       which is pretty boring since we're not outputting any frames (test this
    //       by blanking the screen with the power button).

    // If the callback was posted, remove it.  This stops the notifications.  Ideally we
    // would send a message to the thread letting it know, so when it wakes up it can
    // reset its notion of when the previous Choreographer event arrived.
    Log.d(TAG, "onPause unhooking choreographer");
    Choreographer.getInstance().removeFrameCallback(this);
}
 
Example #25
Source File: ChoreographerCompat.java    From litho with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Choreographer.FrameCallback getFrameCallback() {
  if (mFrameCallback == null) {
    mFrameCallback =
        new Choreographer.FrameCallback() {
          @Override
          public void doFrame(long frameTimeNanos) {
            ChoreographerCompat.FrameCallback.this.doFrameInternal(frameTimeNanos);
          }
        };
  }
  return mFrameCallback;
}
 
Example #26
Source File: FpsCalculator.java    From StaticLayoutView with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void start() {
	Log.d(TAG, "start vsync detect");
	if (mRunning) {
		return;
	}
	
	mRunning = true;

	syncCheckThread = new Thread(new Runnable() {
		@Override
		public void run() {
			for (;;) {
				if (!mRunning) {
					break;
				}
				syncCheckThread();
			}
		}
	});
	syncCheckThread.start();
	
	Choreographer chor = Choreographer.getInstance();
	Field field;
	try {
		field = chor.getClass().getDeclaredField("mFrameIntervalNanos");
		field.setAccessible(true);
		mFrameIntervalNanos = field.getLong(chor);
		Log.d(TAG, "mFrameIntervalNanos " + mFrameIntervalNanos);
	} catch (Exception e) {
		Log.e(TAG, "error: " + e.getMessage());
	}
	chor.postFrameCallback(frameCallback);

}
 
Example #27
Source File: ShapeBackgroundDrawable.java    From DxWaveRefresh with MIT License 5 votes vote down vote up
@Override
public void doFrame(long l) {
    invalidateSelf();
    if (isLoading) {
        Choreographer.getInstance().postFrameCallback(this);
    }
}
 
Example #28
Source File: AndroidSpringLooperFactory.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
public ChoreographerAndroidSpringLooper(Choreographer choreographer) {
  mChoreographer = choreographer;
  mFrameCallback = new Choreographer.FrameCallback() {
    @Override
    public void doFrame(long frameTimeNanos) {
      if (!mStarted || mSpringSystem == null) {
        return;
      }
      mSpringSystem.loop();
      mChoreographer.postFrameCallback(mFrameCallback);
    }
  };
}
 
Example #29
Source File: FpsTask.java    From ArgusAPM with Apache License 2.0 5 votes vote down vote up
@Override
public void doFrame(long frameTimeNanos) {
    mFpsCount++;
    mFrameTimeNanos = frameTimeNanos;
    if (isCanWork()) {
        //注册下一帧回调
        Choreographer.getInstance().postFrameCallback(this);
    } else {
        mCurrentCount = 0;
    }
}
 
Example #30
Source File: HardwareScalerActivity.java    From grafika with Apache License 2.0 5 votes vote down vote up
@Override
protected void onResume() {
    super.onResume();

    // If we already have a Surface, we just need to resume the frame notifications.
    if (mRenderThread != null) {
        Log.d(TAG, "onResume re-hooking choreographer");
        Choreographer.getInstance().postFrameCallback(this);
    }
}