Skip to content

Commit

Permalink
IDanmakuView: add invalidateDanmaku(BaseDanmaku,bool) method for re-r…
Browse files Browse the repository at this point in the history
…endering a danmaku
  • Loading branch information
ctiao committed Nov 16, 2015
1 parent 8964d1c commit 2d6e5e2
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ public void addDanmaku(BaseDanmaku danmaku) {
mCacheManager.addDanmaku(danmaku);
}

@Override
public void invalidateDanmaku(BaseDanmaku item, boolean remeasure) {
super.invalidateDanmaku(item, remeasure);
if (mCacheManager == null)
return;
mCacheManager.invalidateDanmaku(item);
}

@Override
protected void onDanmakuRemoved(BaseDanmaku danmaku) {
super.onDanmakuRemoved(danmaku);
Expand Down Expand Up @@ -206,6 +214,12 @@ public void addDanmaku(BaseDanmaku danmaku) {
}
}

public void invalidateDanmaku(BaseDanmaku danmaku) {
if (mHandler != null && danmaku.hasDrawingCache()) {
mHandler.obtainMessage(CacheHandler.REBUILD_CACHE, danmaku).sendToTarget();
}
}

public void begin() {
if (mThread == null) {
mThread = new HandlerThread("DFM Cache-Building Thread");
Expand Down Expand Up @@ -299,16 +313,24 @@ private void evictAllNotInScreen(boolean removeAllReferences) {

protected void entryRemoved(boolean evicted, BaseDanmaku oldValue, BaseDanmaku newValue) {
if (oldValue.cache != null) {
if (oldValue.cache.hasReferences()) {
oldValue.cache.decreaseReference();
oldValue.cache = null;
return;
}
mRealSize -= sizeOf(oldValue);
oldValue.cache.destroy();
mCachePool.release((DrawingCache) oldValue.cache);
IDrawingCache<?> cache = oldValue.cache;
long releasedSize = clearCache(oldValue);
if (releasedSize <= 0) return;
mRealSize -= releasedSize;
mCachePool.release((DrawingCache) cache);
}
}

private long clearCache(BaseDanmaku oldValue) {
if (oldValue.cache.hasReferences()) {
oldValue.cache.decreaseReference();
oldValue.cache = null;
return 0;
}
long size = sizeOf(oldValue);
oldValue.cache.destroy();
oldValue.cache = null;
return size;
}

protected int sizeOf(BaseDanmaku value) {
Expand Down Expand Up @@ -433,6 +455,8 @@ public class CacheHandler extends Handler {

public static final int DISPATCH_ACTIONS = 0x10;

public static final int REBUILD_CACHE = 0x11;

private boolean mPause;

private boolean mSeekedFlag;
Expand Down Expand Up @@ -480,6 +504,16 @@ public void handleMessage(Message msg) {
BaseDanmaku item = (BaseDanmaku) msg.obj;
addDanmakuAndBuildCache(item);
break;
case REBUILD_CACHE:
BaseDanmaku cacheitem = (BaseDanmaku) msg.obj;
if (cacheitem.isLive) {
clearCache(cacheitem);
createCache(cacheitem);
} else {
entryRemoved(true, cacheitem, null);
addDanmakuAndBuildCache(cacheitem);
}
break;
case CLEAR_TIMEOUT_CACHES:
clearTimeOutCaches();
break;
Expand Down Expand Up @@ -712,7 +746,7 @@ public boolean createCache(BaseDanmaku item) {
return true;
}

private byte buildCache(BaseDanmaku item, boolean forcePush) {
private byte buildCache(BaseDanmaku item, boolean forceInsert) {

// measure
if (!item.isMeasured()) {
Expand All @@ -730,7 +764,7 @@ private byte buildCache(BaseDanmaku item, boolean forcePush) {
cache.increaseReference();
item.cache = cache;
//Log.w("cache", danmaku.text + "DrawingCache hit!!:" + item.paintWidth + "," + danmaku.paintWidth);
mCacheManager.push(item, sizeOf(item), forcePush);
mCacheManager.push(item, 0, forceInsert);
return RESULT_SUCCESS;
}

Expand All @@ -744,12 +778,12 @@ private byte buildCache(BaseDanmaku item, boolean forcePush) {
//Log.e("cache", danmaku.text + "DrawingCache hit!!:" + item.paintWidth + "," + danmaku.paintWidth);
cache = DanmakuUtils.buildDanmakuDrawingCache(item, mDisp, cache); //redraw
item.cache = cache;
mCacheManager.push(item, 0, forcePush);
mCacheManager.push(item, 0, forceInsert);
return RESULT_SUCCESS;
}

// guess cache size
if (!forcePush) {
if (!forceInsert) {
int cacheSize = DanmakuUtils.getCacheSize((int) item.paintWidth,
(int) item.paintHeight);
if (mRealSize + cacheSize > mMaxSize) {
Expand All @@ -761,7 +795,7 @@ private byte buildCache(BaseDanmaku item, boolean forcePush) {
cache = mCachePool.acquire();
cache = DanmakuUtils.buildDanmakuDrawingCache(item, mDisp, cache);
item.cache = cache;
boolean pushed = mCacheManager.push(item, sizeOf(item), forcePush);
boolean pushed = mCacheManager.push(item, sizeOf(item), forceInsert);
if (!pushed) {
releaseDanmakuCache(item, cache);
//Log.e("cache", "break at push failed:" + mMaxSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,7 @@ public void onDanmakusDrawingFinished() {

@Override
public void onDanmakuConfigChanged() {
if (quitFlag && mDanmakusVisible) {
obtainMessage(UPDATE_WHEN_PAUSED).sendToTarget();
}
redrawIfNeeded();
}
});
} else {
Expand Down Expand Up @@ -541,6 +539,13 @@ public void addDanmaku(BaseDanmaku item) {
}
}

public void invalidateDanmaku(BaseDanmaku item, boolean remeasure) {
if (drawTask != null) {
drawTask.invalidateDanmaku(item, remeasure);
}
redrawIfNeeded();
}

public void resume() {
sendEmptyMessage(DrawHandler.RESUME);
}
Expand Down Expand Up @@ -584,6 +589,12 @@ public RenderingState draw(Canvas canvas) {
return mRenderingState;
}

private void redrawIfNeeded() {
if (quitFlag && mDanmakusVisible) {
obtainMessage(UPDATE_WHEN_PAUSED).sendToTarget();
}
}

private void notifyRendering() {
if (!mInWaitingState) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ public synchronized void addDanmaku(BaseDanmaku item) {
mLastDanmaku = item;
}
}

@Override
public void invalidateDanmaku(BaseDanmaku item, boolean remeasure) {
if (remeasure) {
item.paintWidth = -1;
item.paintHeight = -1;
}
}

@Override
public synchronized void removeAllDanmakus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public interface IDanmakuView {
* @param item
*/
public void addDanmaku(BaseDanmaku item);

public void invalidateDanmaku(BaseDanmaku item, boolean remeasure);

public void removeAllDanmakus();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public interface IDrawTask {

public void setParser(BaseDanmakuParser parser);

void invalidateDanmaku(BaseDanmaku item, boolean remeasure);

public interface TaskListener {
public void ready();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,14 @@ public void addDanmaku(BaseDanmaku item) {
handler.addDanmaku(item);
}
}


@Override
public void invalidateDanmaku(BaseDanmaku item, boolean remeasure) {
if (handler != null) {
handler.invalidateDanmaku(item, remeasure);
}
}

@Override
public void removeAllDanmakus() {
if (handler != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ public void addDanmaku(BaseDanmaku item) {
handler.addDanmaku(item);
}
}


@Override
public void invalidateDanmaku(BaseDanmaku item, boolean remeasure) {
if (handler != null) {
handler.invalidateDanmaku(item, remeasure);
}
}

@Override
public void removeAllDanmakus() {
if (handler != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ public void addDanmaku(BaseDanmaku item) {
handler.addDanmaku(item);
}
}


@Override
public void invalidateDanmaku(BaseDanmaku item, boolean remeasure) {
if (handler != null) {
handler.invalidateDanmaku(item, remeasure);
}
}

@Override
public void removeAllDanmakus() {
if (handler != null) {
Expand Down

0 comments on commit 2d6e5e2

Please sign in to comment.