Unverified Commit 730daee3 authored by Nikolay Igotti's avatar Nikolay Igotti Committed by GitHub

Avoid using SkPicture on iOS (#401)

parent 14464cb1
...@@ -11,6 +11,7 @@ import platform.Foundation.NSNotificationCenter ...@@ -11,6 +11,7 @@ import platform.Foundation.NSNotificationCenter
import platform.Foundation.NSSelectorFromString import platform.Foundation.NSSelectorFromString
import platform.UIKit.* import platform.UIKit.*
import platform.darwin.NSObject import platform.darwin.NSObject
import kotlin.system.getTimeNanos
actual open class SkiaLayer { actual open class SkiaLayer {
fun isShowing(): Boolean { fun isShowing(): Boolean {
...@@ -57,7 +58,6 @@ actual open class SkiaLayer { ...@@ -57,7 +58,6 @@ actual open class SkiaLayer {
fun attachTo(view: UIView) { fun attachTo(view: UIView) {
this.view = view this.view = view
contextHandler = MetalContextHandler(this) contextHandler = MetalContextHandler(this)
pictureRecorder = PictureRecorder()
// See https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/using_responders_and_the_responder_chain_to_handle_events?language=objc // See https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/using_responders_and_the_responder_chain_to_handle_events?language=objc
controller = object : NSObject() { controller = object : NSObject() {
@ObjCAction @ObjCAction
...@@ -164,39 +164,23 @@ actual open class SkiaLayer { ...@@ -164,39 +164,23 @@ actual open class SkiaLayer {
redrawer = null redrawer = null
contextHandler?.dispose() contextHandler?.dispose()
contextHandler = null contextHandler = null
picture?.instance?.close()
picture = null
pictureRecorder?.close()
pictureRecorder = null
isDisposed = true isDisposed = true
} }
} }
actual var skikoView: SkikoView? = null actual var skikoView: SkikoView? = null
internal var redrawer: MetalRedrawer? = null internal var redrawer: MetalRedrawer? = null
private var picture: PictureHolder? = null
private var pictureRecorder: PictureRecorder? = null
private var contextHandler: MetalContextHandler? = null private var contextHandler: MetalContextHandler? = null
fun update(nanoTime: Long) { internal fun draw(canvas: Canvas) {
check(!isDisposed) { "SkiaLayer is disposed" }
val (w, h) = view!!.frame.useContents { val (w, h) = view!!.frame.useContents {
size.width to size.height size.width to size.height
} }
val pictureWidth = (w.toFloat() * contentScale).coerceAtLeast(0.0F) val pictureWidth = (w.toFloat() * contentScale).coerceAtLeast(0.0F)
val pictureHeight = (h.toFloat() * contentScale).coerceAtLeast(0.0F) val pictureHeight = (h.toFloat() * contentScale).coerceAtLeast(0.0F)
val bounds = Rect.makeWH(pictureWidth, pictureHeight) skikoView?.onRender(canvas, pictureWidth.toInt(), pictureHeight.toInt(), getTimeNanos())
val canvas = pictureRecorder!!.beginRecording(bounds)
skikoView?.onRender(canvas, pictureWidth.toInt(), pictureHeight.toInt(), nanoTime)
val picture = pictureRecorder!!.finishRecordingAsPicture()
this.picture = PictureHolder(picture, pictureWidth.toInt(), pictureHeight.toInt())
}
internal fun draw(canvas: Canvas) {
check(!isDisposed) { "SkiaLayer is disposed" }
picture?.also {
canvas.drawPicture(it.instance)
}
} }
} }
......
...@@ -40,7 +40,6 @@ internal class MetalRedrawer( ...@@ -40,7 +40,6 @@ internal class MetalRedrawer(
private val frameDispatcher = FrameDispatcher(SkikoDispatchers.Main) { private val frameDispatcher = FrameDispatcher(SkikoDispatchers.Main) {
if (layer.isShowing()) { if (layer.isShowing()) {
layer.update(getTimeNanos())
draw() draw()
} }
} }
...@@ -79,7 +78,6 @@ internal class MetalRedrawer( ...@@ -79,7 +78,6 @@ internal class MetalRedrawer(
override fun redrawImmediately() { override fun redrawImmediately() {
check(!isDisposed) { "MetalRedrawer is disposed" } check(!isDisposed) { "MetalRedrawer is disposed" }
layer.update(getTimeNanos())
draw() draw()
} }
...@@ -144,7 +142,6 @@ internal class MetalLayer : CAMetalLayer { ...@@ -144,7 +142,6 @@ internal class MetalLayer : CAMetalLayer {
} }
override fun drawInContext(ctx: CGContextRef?) { override fun drawInContext(ctx: CGContextRef?) {
skiaLayer.update(getTimeNanos())
contextHandler.draw() contextHandler.draw()
super.drawInContext(ctx) super.drawInContext(ctx)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment