private IDrawTask createTask( boolean useDrwaingCache, DanmakuTimer timer, Context context, int width, int height, IDrawTask.TaskListener taskListener) { mDisp = new AndroidDisplayer(); mDisp.setSize(width, height); DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); mDisp.setDensities( displayMetrics.density, displayMetrics.densityDpi, displayMetrics.scaledDensity); mDisp.resetSlopPixel(DanmakuGlobalConfig.DEFAULT.scaleTextSize); IDrawTask task = useDrwaingCache ? new CacheManagingDrawTask( timer, context, mDisp, taskListener, 1024 * 1024 * AndroidUtils.getMemoryClass(context) / 3) : new DrawTask(timer, context, mDisp, taskListener); task.setParser(mParser); task.prepare(); return task; }
private void waitRendering(long dTime) { mRenderingState.sysTime = SystemClock.uptimeMillis(); mInWaitingState = true; if (mUpdateInNewThread) { if (mThread == null) { return; } try { synchronized (drawTask) { if (dTime == INDEFINITE_TIME) { drawTask.wait(); } else { drawTask.wait(dTime); } sendEmptyMessage(NOTIFY_RENDERING); } } catch (InterruptedException e) { e.printStackTrace(); } } else { if (dTime == INDEFINITE_TIME) { removeMessages(NOTIFY_RENDERING); removeMessages(UPDATE); } else { removeMessages(NOTIFY_RENDERING); removeMessages(UPDATE); sendEmptyMessageDelayed(NOTIFY_RENDERING, dTime); } } }
private IDrawTask createDrawTask( boolean useDrwaingCache, DanmakuTimer timer, Context context, int width, int height, boolean isHardwareAccelerated, IDrawTask.TaskListener taskListener) { mDisp = mContext.getDisplayer(); mDisp.setSize(width, height); DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); mDisp.setDensities( displayMetrics.density, displayMetrics.densityDpi, displayMetrics.scaledDensity); mDisp.resetSlopPixel(mContext.scaleTextSize); mDisp.setHardwareAccelerated(isHardwareAccelerated); IDrawTask task = useDrwaingCache ? new CacheManagingDrawTask( timer, mContext, taskListener, 1024 * 1024 * AndroidUtils.getMemoryClass(context) / 3) : new DrawTask(timer, mContext, taskListener); task.setParser(mParser); task.prepare(); obtainMessage(NOTIFY_DISP_SIZE_CHANGED, false).sendToTarget(); return task; }
public void draw(Canvas canvas) { if (drawTask == null) return; // mDisp.setAverageRenderingTime((Math.max(16, getAverageRenderingTime()) / 8) * 8); mDisp.setExtraData(canvas); drawTask.draw(mDisp); // recordRenderingTime(); }
public IDanmakus getCurrentVisibleDanmakus() { if (drawTask != null) { return drawTask.getVisibleDanmakusOnTime(getCurrentTime()); } return null; }
public RenderingState draw(Canvas canvas) { if (drawTask == null) return mRenderingState; mDisp.setExtraData(canvas); mRenderingState.set(drawTask.draw(mDisp)); recordRenderingTime(); return mRenderingState; }
public void addDanmaku(BaseDanmaku item) { if (drawTask != null) { item.flags = mContext.mGlobalFlagValues; item.setTimer(timer); drawTask.addDanmaku(item); obtainMessage(NOTIFY_RENDERING).sendToTarget(); } }
private void notifyRendering() { if (!mInWaitingState) { return; } if (drawTask != null) { drawTask.requestClear(); } if (mUpdateInNewThread) { synchronized (this) { mDrawTimes.clear(); } synchronized (drawTask) { drawTask.notifyAll(); } } else { mDrawTimes.clear(); removeMessages(UPDATE); sendEmptyMessage(UPDATE); } mInWaitingState = false; }
private void quitUpdateThread() { if (mThread != null) { synchronized (drawTask) { drawTask.notifyAll(); } mThread.quit(); try { mThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } mThread = null; } }
public void addDanmaku(BaseDanmaku item) { if (drawTask != null) { drawTask.addDanmaku(item); } }
@Override public void handleMessage(Message msg) { int what = msg.what; switch (what) { case PREPARE: if (mParser == null || !mDanmakuView.isViewReady()) { sendEmptyMessageDelayed(PREPARE, 100); } else { prepare( new Runnable() { @Override public void run() { mReady = true; if (mCallback != null) { mCallback.prepared(); } } }); } break; case START: Long startTime = (Long) msg.obj; if (startTime != null) { pausedPostion = startTime.longValue(); } else { pausedPostion = 0; } case RESUME: quitFlag = false; if (mReady) { mTimeBase = System.currentTimeMillis() - pausedPostion; timer.update(pausedPostion); removeMessages(RESUME); sendEmptyMessage(UPDATE); drawTask.start(); } else { sendEmptyMessageDelayed(RESUME, 100); } break; case SEEK_POS: Long deltaMs = (Long) msg.obj; mTimeBase -= deltaMs; timer.update(System.currentTimeMillis() - mTimeBase); if (drawTask != null) drawTask.seek(timer.currMillisecond); pausedPostion = timer.currMillisecond; removeMessages(RESUME); sendEmptyMessage(RESUME); break; case UPDATE: if (quitFlag) { break; } long startMS = System.currentTimeMillis(); long d = timer.update(startMS - mTimeBase); if (mCallback != null) { mCallback.updateTimer(timer); } if (d < 0) { removeMessages(UPDATE); sendEmptyMessageDelayed(UPDATE, 60 - d); break; } d = mDanmakuView.drawDanmakus(); removeMessages(UPDATE); if (d == -1) { // reduce refresh rate sendEmptyMessageDelayed(UPDATE, 100); break; } if (d <= 16) { sendEmptyMessage(UPDATE); SystemClock.sleep(16 - d); break; } sendEmptyMessage(UPDATE); break; case SHOW_DANMAKUS: Long start = (Long) msg.obj; if (start == null && drawTask != null) { drawTask.requestClear(); } else if (start != null && drawTask != null) { drawTask.start(); drawTask.seek(start); drawTask.requestClear(); obtainMessage(START, start).sendToTarget(); } mDanmakusVisible = true; break; case HIDE_DANMAKUS: if (mDanmakuView != null) { mDanmakuView.clear(); } Boolean quitDrawTask = (Boolean) msg.obj; if (quitDrawTask.booleanValue() && this.drawTask != null) { this.drawTask.quit(); } mDanmakusVisible = false; if (!quitDrawTask) { break; } case PAUSE: case QUIT: removeCallbacksAndMessages(null); quitFlag = true; mDrawTimes.clear(); pausedPostion = timer.currMillisecond; if (what == QUIT) { if (this.drawTask != null) { this.drawTask.quit(); } if (this.getLooper() != Looper.getMainLooper()) this.getLooper().quit(); if (mParser != null) { mParser.release(); } } break; } }
public void removeAllLiveDanmakus() { if (drawTask != null) { drawTask.removeAllLiveDanmakus(); } }
public void invalidateDanmaku(BaseDanmaku item, boolean remeasure) { if (drawTask != null && item != null) { drawTask.invalidateDanmaku(item, remeasure); } redrawIfNeeded(); }
@Override public void handleMessage(Message msg) { int what = msg.what; switch (what) { case PREPARE: if (mParser == null || !mDanmakuView.isViewReady()) { sendEmptyMessageDelayed(PREPARE, 100); } else { prepare( new Runnable() { @Override public void run() { mReady = true; if (mCallback != null) { mCallback.prepared(); } } }); } break; case SHOW_DANMAKUS: mDanmakusVisible = true; Long start = (Long) msg.obj; boolean resume = false; if (drawTask != null) { if (start == null) { timer.update(getCurrentTime()); drawTask.requestClear(); } else { drawTask.start(); drawTask.seek(start); drawTask.requestClear(); resume = true; } } if (quitFlag && mDanmakuView != null) { mDanmakuView.drawDanmakus(); } notifyRendering(); if (!resume) { break; } case START: Long startTime = (Long) msg.obj; if (startTime != null) { pausedPosition = startTime; } else { pausedPosition = 0; } case RESUME: quitFlag = false; if (mReady) { mRenderingState.reset(); mDrawTimes.clear(); mTimeBase = SystemClock.uptimeMillis() - pausedPosition; timer.update(pausedPosition); removeMessages(RESUME); sendEmptyMessage(UPDATE); drawTask.start(); notifyRendering(); mInSeekingAction = false; } else { sendEmptyMessageDelayed(RESUME, 100); } break; case SEEK_POS: quitFlag = true; quitUpdateThread(); Long position = (Long) msg.obj; long deltaMs = position - timer.currMillisecond; mTimeBase -= deltaMs; timer.update(SystemClock.uptimeMillis() - mTimeBase); mContext.mGlobalFlagValues.updateMeasureFlag(); if (drawTask != null) drawTask.seek(timer.currMillisecond); pausedPosition = timer.currMillisecond; removeMessages(RESUME); sendEmptyMessage(RESUME); break; case UPDATE: if (mUpdateInNewThread) { updateInNewThread(); } else { updateInCurrentThread(); } break; case NOTIFY_DISP_SIZE_CHANGED: mContext.mDanmakuFactory.notifyDispSizeChanged(mContext); Boolean updateFlag = (Boolean) msg.obj; if (updateFlag != null && updateFlag) { mContext.mGlobalFlagValues.updateMeasureFlag(); } break; case HIDE_DANMAKUS: mDanmakusVisible = false; if (mDanmakuView != null) { mDanmakuView.clear(); } if (this.drawTask != null) { this.drawTask.requestClear(); this.drawTask.requestHide(); } Boolean quitDrawTask = (Boolean) msg.obj; if (quitDrawTask && this.drawTask != null) { this.drawTask.quit(); } if (!quitDrawTask) { break; } case PAUSE: removeMessages(UPDATE); case QUIT: if (what == QUIT) { removeCallbacksAndMessages(null); } quitFlag = true; syncTimerIfNeeded(); if (mThread != null) { notifyRendering(); quitUpdateThread(); } pausedPosition = timer.currMillisecond; if (what == QUIT) { if (this.drawTask != null) { this.drawTask.quit(); } if (mParser != null) { mParser.release(); } if (this.getLooper() != Looper.getMainLooper()) this.getLooper().quit(); } break; case NOTIFY_RENDERING: notifyRendering(); break; case UPDATE_WHEN_PAUSED: if (quitFlag && mDanmakuView != null) { drawTask.requestClear(); mDanmakuView.drawDanmakus(); notifyRendering(); } break; case CLEAR_DANMAKUS_ON_SCREEN: if (drawTask != null) { drawTask.clearDanmakusOnScreen(getCurrentTime()); } break; } }