-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'e2e' of https://github.com/Shopify/react-native-skia in…
…to e2e
- Loading branch information
Showing
7 changed files
with
130 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,105 @@ | ||
import { type SharedValue } from "react-native-reanimated"; | ||
|
||
import Rea from "../external/reanimated/ReanimatedProxy"; | ||
import type { Skia, SkCanvas } from "../skia/types"; | ||
import { HAS_REANIMATED_3 } from "../external/reanimated/renderHelpers"; | ||
|
||
import type { Node } from "./Node"; | ||
import { isSharedValue } from "./utils"; | ||
import type { Recording } from "./Recorder/Recorder"; | ||
import { Recorder } from "./Recorder/Recorder"; | ||
import { visit } from "./Recorder/Visitor"; | ||
import { replay } from "./Recorder/Player"; | ||
import { createDrawingContext } from "./Recorder/DrawingContext"; | ||
import { createRecording, type Recording } from "./Recorder/Recording"; | ||
|
||
const drawOnscreen = (Skia: Skia, nativeId: number, recording: Recording) => { | ||
"worklet"; | ||
|
||
const rec = Skia.PictureRecorder(); | ||
const canvas = rec.beginRecording(); | ||
// const start = performance.now(); | ||
|
||
// TODO: because the pool is not a shared value here, it is copied on every frame | ||
const ctx = createDrawingContext(Skia, recording.paintPool, canvas); | ||
//console.log(recording.commands); | ||
replay(ctx, recording.commands); | ||
const picture = rec.finishRecordingAsPicture(); | ||
//const end = performance.now(); | ||
//console.log("Recording time: ", end - start); | ||
SkiaViewApi.setJsiProperty(nativeId, "picture", picture); | ||
rec.dispose(); | ||
picture.dispose(); | ||
}; | ||
|
||
export class Container { | ||
private _root: Node[] = []; | ||
private _recording: Recording | null = null; | ||
public unmounted = false; | ||
|
||
private values = new Set<SharedValue<unknown>>(); | ||
private mapperId: number | null = null; | ||
|
||
constructor(public Skia: Skia, private nativeId: number) {} | ||
export abstract class Container { | ||
public root: Node[] = []; | ||
protected recording: Recording | null = null; | ||
|
||
get root() { | ||
return this._root; | ||
} | ||
constructor(protected Skia: Skia, protected nativeId: number) {} | ||
|
||
set root(root: Node[]) { | ||
const isOnscreen = this.nativeId !== -1; | ||
if (isOnscreen) { | ||
if (this.mapperId !== null) { | ||
Rea.stopMapper(this.mapperId); | ||
} | ||
const { nativeId, Skia, _recording } = this; | ||
this.mapperId = Rea.startMapper(() => { | ||
"worklet"; | ||
drawOnscreen(Skia, nativeId, _recording!); | ||
}, Array.from(this.values)); | ||
drawOnCanvas(canvas: SkCanvas) { | ||
if (!this.recording) { | ||
throw new Error("No recording to draw"); | ||
} | ||
this._root = root; | ||
const recorder = new Recorder(); | ||
visit(recorder, root); | ||
this._recording = createRecording(recorder.commands); | ||
const ctx = createDrawingContext( | ||
this.Skia, | ||
this.recording.paintPool, | ||
canvas | ||
); | ||
//console.log(this._recording); | ||
replay(ctx, this.recording.commands); | ||
} | ||
|
||
clear() { | ||
console.log("clear container"); | ||
abstract redraw(): void; | ||
} | ||
|
||
class StaticContainer extends Container { | ||
constructor(Skia: Skia, nativeId: number) { | ||
super(Skia, nativeId); | ||
} | ||
|
||
redraw() { | ||
const isOnscreen = this.nativeId !== -1; | ||
if (isOnscreen) { | ||
const { nativeId, Skia, _recording } = this; | ||
Rea.runOnUI(() => { | ||
drawOnscreen(Skia, nativeId, _recording!); | ||
})(); | ||
const recorder = new Recorder(); | ||
visit(recorder, this.root); | ||
this.recording = recorder.getRecording(); | ||
const isOnScreen = this.nativeId !== -1; | ||
if (isOnScreen) { | ||
const rec = this.Skia.PictureRecorder(); | ||
const canvas = rec.beginRecording(); | ||
this.drawOnCanvas(canvas); | ||
const picture = rec.finishRecordingAsPicture(); | ||
SkiaViewApi.setJsiProperty(this.nativeId, "picture", picture); | ||
} | ||
} | ||
} | ||
|
||
getNativeId() { | ||
return this.nativeId; | ||
} | ||
|
||
unregisterValues(values: object) { | ||
Object.values(values) | ||
.filter(isSharedValue) | ||
.forEach((value) => { | ||
this.values.delete(value); | ||
}); | ||
} | ||
class ReanimatedContainer extends Container { | ||
private mapperId: number | null = null; | ||
|
||
registerValues(values: object) { | ||
Object.values(values) | ||
.filter(isSharedValue) | ||
.forEach((value) => { | ||
this.values.add(value); | ||
}); | ||
constructor(Skia: Skia, nativeId: number) { | ||
super(Skia, nativeId); | ||
} | ||
|
||
drawOnCanvas(canvas: SkCanvas) { | ||
if (!this._recording) { | ||
throw new Error("No recording to draw"); | ||
redraw() { | ||
if (this.mapperId !== null) { | ||
Rea.stopMapper(this.mapperId); | ||
} | ||
const recorder = new Recorder(); | ||
visit(recorder, this.root); | ||
const record = recorder.getRecording(); | ||
const { animationValues } = record; | ||
this.recording = { | ||
commands: record.commands, | ||
paintPool: record.paintPool, | ||
}; | ||
if (animationValues.size > 0) { | ||
const { nativeId, Skia, recording } = this; | ||
this.mapperId = Rea.startMapper(() => { | ||
"worklet"; | ||
drawOnscreen(Skia, nativeId, recording!); | ||
}, Array.from(animationValues)); | ||
} | ||
const ctx = createDrawingContext( | ||
this.Skia, | ||
this._recording.paintPool, | ||
canvas | ||
); | ||
//console.log(this._recording); | ||
replay(ctx, this._recording.commands); | ||
} | ||
} | ||
|
||
export const createContainer = (Skia: Skia, nativeId: number) => { | ||
return HAS_REANIMATED_3 && nativeId !== -1 | ||
? new ReanimatedContainer(Skia, nativeId) | ||
: new StaticContainer(Skia, nativeId); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.