-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #530 from Shopify/decouple-reconciller
Decouple Skia from Reconciller
- Loading branch information
Showing
134 changed files
with
714 additions
and
539 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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import path from "path"; | ||
import fs from "fs"; | ||
|
||
import type { Surface } from "canvaskit-wasm"; | ||
|
||
import type { SkSurface } from "../skia"; | ||
import { toValue } from "../skia/web/api/Host"; | ||
|
||
export const processResult = ( | ||
surface: SkSurface, | ||
relPath: string, | ||
overwrite = false | ||
) => { | ||
toValue<Surface>(surface).flush(); | ||
const image = surface.makeImageSnapshot(); | ||
const png = image.encodeToBytes(); | ||
const p = path.resolve(__dirname, relPath); | ||
if (fs.existsSync(p) && !overwrite) { | ||
const ref = fs.readFileSync(p); | ||
expect(ref.equals(png)).toBe(true); | ||
} else { | ||
fs.writeFileSync(p, png); | ||
} | ||
}; |
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
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,2 +1,3 @@ | ||
export { interpolate } from "./interpolate"; | ||
export { interpolateColors, mixColors } from "./interpolateColors"; | ||
export * from "./interpolate"; | ||
export * from "./interpolateColors"; | ||
export * from "./interpolateVector"; |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { Vector } from "../../skia/types"; | ||
|
||
import { interpolate } from "./interpolate"; | ||
|
||
export const interpolateVector = ( | ||
value: number, | ||
inputRange: readonly number[], | ||
outputRange: readonly Vector[], | ||
options?: Parameters<typeof interpolate>[3] | ||
) => ({ | ||
x: interpolate( | ||
value, | ||
inputRange, | ||
outputRange.map((v) => v.x), | ||
options | ||
), | ||
y: interpolate( | ||
value, | ||
inputRange, | ||
outputRange.map((v) => v.y), | ||
options | ||
), | ||
}); | ||
|
||
export const mixVector = (value: number, from: Vector, to: Vector) => | ||
interpolateVector(value, [0, 1], [from, to]); |
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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from "react"; | ||
|
||
import { processResult } from "../../__tests__/setup"; | ||
import { Group, Rect } from "../components"; | ||
import * as SkiaRenderer from "../index"; | ||
|
||
import { center, drawOnNode, width } from "./setup"; | ||
|
||
const size = 128; | ||
|
||
describe("Renderer", () => { | ||
it("Loads renderer without Skia", () => { | ||
expect(SkiaRenderer).toBeDefined(); | ||
}); | ||
it("Light blue rectangle", () => { | ||
const surface = drawOnNode( | ||
<Rect | ||
x={(width - size) / 2} | ||
y={(width - size) / 2} | ||
width={size} | ||
height={size} | ||
color="lightblue" | ||
/> | ||
); | ||
processResult(surface, "snapshots/drawings/lightblue-rect.png"); | ||
}); | ||
it("Scaled light blue rectangle", () => { | ||
const scale = 2; | ||
const scaled = size / scale; | ||
const surface = drawOnNode( | ||
<Group transform={[{ scale }]} origin={center}> | ||
<Rect | ||
x={(width - scaled) / 2} | ||
y={(width - scaled) / 2} | ||
width={scaled} | ||
height={scaled} | ||
color="lightblue" | ||
/> | ||
</Group> | ||
); | ||
processResult(surface, "snapshots/drawings/lightblue-rect.png"); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import CanvasKitInit from "canvaskit-wasm"; | ||
import type { ReactNode } from "react"; | ||
import ReactReconciler from "react-reconciler"; | ||
|
||
import { JsiSkApi } from "../../skia/web"; | ||
import { DependencyManager } from "../DependencyManager"; | ||
import { skHostConfig } from "../HostConfig"; | ||
import { Container } from "../nodes"; | ||
import type { DrawingContext } from "../DrawingContext"; | ||
|
||
let Skia: ReturnType<typeof JsiSkApi>; | ||
|
||
beforeAll(async () => { | ||
const CanvasKit = await CanvasKitInit(); | ||
Skia = JsiSkApi(CanvasKit); | ||
}); | ||
|
||
export const width = 256; | ||
export const height = 256; | ||
export const center = { x: width / 2, y: height / 2 }; | ||
const redraw = () => {}; | ||
const ref = { current: null }; | ||
|
||
const skiaReconciler = ReactReconciler(skHostConfig); | ||
|
||
skiaReconciler.injectIntoDevTools({ | ||
bundleType: 1, | ||
version: "0.0.1", | ||
rendererPackageName: "react-native-skia", | ||
}); | ||
|
||
export const drawOnNode = (element: ReactNode) => { | ||
expect(Skia).toBeDefined(); | ||
const surface = Skia.Surface.Make(width, height)!; | ||
expect(surface).toBeDefined(); | ||
const canvas = surface.getCanvas(); | ||
expect(canvas).toBeDefined(); | ||
expect(element).toBeDefined(); | ||
const container = new Container(new DependencyManager(ref), redraw); | ||
skiaReconciler.createContainer(container, 0, false, null); | ||
const root = skiaReconciler.createContainer(container, 0, false, null); | ||
skiaReconciler.updateContainer(element, root, null, () => {}); | ||
const ctx: DrawingContext = { | ||
width, | ||
height, | ||
timestamp: 0, | ||
canvas, | ||
paint: Skia.Paint(), | ||
opacity: 1, | ||
ref, | ||
center: Skia.Point(width / 2, height / 2), | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
fontMgr: null, | ||
Skia, | ||
}; | ||
container.draw(ctx); | ||
return surface; | ||
}; |
Oops, something went wrong.