|
@@ -3,41 +3,31 @@ package io.legado.app.utils
|
|
|
import android.graphics.Canvas
|
|
import android.graphics.Canvas
|
|
|
import android.graphics.Picture
|
|
import android.graphics.Picture
|
|
|
import android.os.Build
|
|
import android.os.Build
|
|
|
-import android.view.View
|
|
|
|
|
import androidx.core.graphics.record
|
|
import androidx.core.graphics.record
|
|
|
import java.util.concurrent.locks.ReentrantLock
|
|
import java.util.concurrent.locks.ReentrantLock
|
|
|
|
|
|
|
|
class PictureMirror {
|
|
class PictureMirror {
|
|
|
|
|
|
|
|
var picture: Picture? = null
|
|
var picture: Picture? = null
|
|
|
|
|
+
|
|
|
@Volatile
|
|
@Volatile
|
|
|
var isDirty = true
|
|
var isDirty = true
|
|
|
val lock = ReentrantLock()
|
|
val lock = ReentrantLock()
|
|
|
- @Volatile
|
|
|
|
|
- var scheduleInvalidateView: View? = null
|
|
|
|
|
|
|
|
|
|
inline fun drawLocked(
|
|
inline fun drawLocked(
|
|
|
canvas: Canvas?,
|
|
canvas: Canvas?,
|
|
|
width: Int,
|
|
width: Int,
|
|
|
height: Int,
|
|
height: Int,
|
|
|
- view: View? = null,
|
|
|
|
|
block: Canvas.() -> Unit
|
|
block: Canvas.() -> Unit
|
|
|
) {
|
|
) {
|
|
|
if (atLeastApi23) {
|
|
if (atLeastApi23) {
|
|
|
if (picture == null) picture = Picture()
|
|
if (picture == null) picture = Picture()
|
|
|
val picture = picture!!
|
|
val picture = picture!!
|
|
|
if (isDirty) {
|
|
if (isDirty) {
|
|
|
- if (!lock.tryLock()) {
|
|
|
|
|
- if (canvas != null && view != null) {
|
|
|
|
|
- scheduleInvalidateView = view
|
|
|
|
|
- }
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (!lock.tryLock()) return
|
|
|
try {
|
|
try {
|
|
|
picture.record(width, height, block)
|
|
picture.record(width, height, block)
|
|
|
isDirty = false
|
|
isDirty = false
|
|
|
- scheduleInvalidateView?.postInvalidate()
|
|
|
|
|
- scheduleInvalidateView = null
|
|
|
|
|
} finally {
|
|
} finally {
|
|
|
lock.unlock()
|
|
lock.unlock()
|
|
|
}
|
|
}
|