Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 81 additions & 5 deletions apps/benchmark/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// whole matrix by itself and prints one `[bench] tick ...` line per second to
// logcat; bench/run.mjs pairs those with host-side CPU and PSS samples.
import React, { useEffect, useMemo, useRef, useState } from 'react';
import { Linking, StyleSheet, Text as RNText, View } from 'react-native';
import { Linking, PixelRatio, StyleSheet, Text as RNText, View } from 'react-native';
import {
Easing,
cancelAnimation,
Expand All @@ -22,11 +22,13 @@ import {
Rect as TgfxRect,
Shader as TgfxShader,
Text as TgfxText,
Vertices as TgfxVertices,
TgfxImage,
renderToImage,
} from 'react-native-tgfx';
import {
Canvas as SkCanvas,
Circle as SkCircle,
Fill as SkFill,
Group as SkGroup,
Image as SkImageView,
Expand All @@ -47,7 +49,10 @@ import { BG, CX, CY, H, PALETTE, SHAPES, W } from './layout';

// react-native-skia 3.x has one canvas, on Graphite.
const ENGINES = ['tgfx', 'skia'] as const;
const SCENES = ['shapes', 'images', 'shader', 'pattern', 'paths', 'text', ...ANIM_SCENES] as const;
// `shapes` pits each engine's fastest static path against the other: tgfx nodes,
// skia's ShapeBlob. `shapes-nodes` and `shapes-atlas` hold the technique equal:
// 20k declarative nodes on both, or one sprite atlas and one tinted mesh on both.
const SCENES = ['shapes', 'shapes-nodes', 'shapes-atlas', 'images', 'shader', 'pattern', 'paths', 'text', ...ANIM_SCENES] as const;
// surface: tgfx composite="layer", skia backingView SurfaceView.
// texture: tgfx composite="texture", skia backingView TextureView (its default).
const VIEWS = ['surface', 'texture'] as const;
Expand Down Expand Up @@ -198,21 +203,75 @@ function tgfxTextureScene(i: number, size: number) {
);
}

// `shapes-atlas` for tgfx: ShapeBlob's technique through the public API. One
// white circle and one white rect, rasterized once at the device scale, side by
// side in a two-cell atlas; every shape is a quad tinted by its vertex colour.
const ATLAS_PX = SHAPES[0].size * PixelRatio.get();
const ATLAS_CELL = Math.ceil(ATLAS_PX) + 2;

function tgfxAtlasScene() {
const r = ATLAS_PX / 2;
return (
<>
<TgfxCircle cx={1 + r} cy={1 + r} r={r} color="#ffffff" />
<TgfxRect x={ATLAS_CELL + 1} y={1} width={ATLAS_PX} height={ATLAS_PX} color="#ffffff" />
</>
);
}

// Two triangles per shape, padded by one device pixel on each side so the
// atlas cell's antialiased edge is inside the quad.
let atlasMesh: { vertices: { x: number; y: number }[]; textures: { x: number; y: number }[]; colors: string[] } | null = null;
function tgfxAtlasMesh() {
if (atlasMesh != null) return atlasMesh;
const pad = 1 / PixelRatio.get();
const vertices: { x: number; y: number }[] = [];
const textures: { x: number; y: number }[] = [];
const colors: string[] = [];
for (const s of SHAPES) {
const x0 = s.x - pad;
const y0 = s.y - pad;
const x1 = s.x + s.size + pad;
const y1 = s.y + s.size + pad;
const u0 = s.circle ? 0 : ATLAS_CELL;
const u1 = u0 + ATLAS_PX + 2;
const v1 = ATLAS_PX + 2;
vertices.push({ x: x0, y: y0 }, { x: x1, y: y0 }, { x: x1, y: y1 }, { x: x0, y: y0 }, { x: x1, y: y1 }, { x: x0, y: y1 });
textures.push({ x: u0, y: 0 }, { x: u1, y: 0 }, { x: u1, y: v1 }, { x: u0, y: 0 }, { x: u1, y: v1 }, { x: u0, y: v1 });
for (let k = 0; k < 6; k += 1) colors.push(s.color);
}
atlasMesh = { vertices, textures, colors };
return atlasMesh;
}

// ---- scenes ----------------------------------------------------------------

function TgfxScene({
scene,
t,
images,
tile,
atlas,
}: {
scene: string;
t: ReturnType<typeof useSharedValue<number>>;
images: TgfxImage[];
tile: TgfxImage | null;
atlas: TgfxImage | null;
}) {
const origin = { x: CX, y: CY };
if (scene === 'shapes') {
if (scene === 'shapes-atlas') {
if (atlas == null) return null;
const mesh = tgfxAtlasMesh();
return (
<TgfxGroup transform={[{ rotate: t }]} origin={origin}>
<TgfxVertices vertices={mesh.vertices} textures={mesh.textures} colors={mesh.colors}>
<TgfxImageShader image={atlas} />
</TgfxVertices>
</TgfxGroup>
);
}
if (scene === 'shapes' || scene === 'shapes-nodes') {
return (
<TgfxGroup transform={[{ rotate: t }]} origin={origin}>
{SHAPES.map((s, i) =>
Expand Down Expand Up @@ -291,6 +350,19 @@ function SkiaScene({
const patternTransform = useDerivedValue(() => [{ rotate: t.value }, { scale: 0.03 }]);
const uniforms = useDerivedValue(() => ({ uTime: t.value, uSize: [W, H] }));
const origin = vec(CX, CY);
if (scene === 'shapes-nodes') {
return (
<SkGroup transform={transform} origin={origin}>
{SHAPES.map((s, i) =>
s.circle ? (
<SkCircle key={i} cx={s.x + s.size / 2} cy={s.y + s.size / 2} r={s.size / 2} color={s.color} />
) : (
<SkRect key={i} x={s.x} y={s.y} width={s.size} height={s.size} color={s.color} />
)
)}
</SkGroup>
);
}
if (scene === 'images') {
if (images.length < 8) return null;
return (
Expand Down Expand Up @@ -358,6 +430,7 @@ export default function App() {
const [combo, setCombo] = useState(0);
const [tgfxImages, setTgfxImages] = useState<TgfxImage[]>([]);
const [tgfxTile, setTgfxTile] = useState<TgfxImage | null>(null);
const [tgfxAtlas, setTgfxAtlas] = useState<TgfxImage | null>(null);
const [plan, setPlan] = useState<Combo[] | null>(null);
const [gapMs, setGapMs] = useState(0);
// anim-imp for tgfx: which thread writes the 20k handles (bench://run?...&imp=js).
Expand Down Expand Up @@ -396,9 +469,12 @@ export default function App() {
built.push(await renderToImage(tgfxTextureScene(i, 128), { width: 128, height: 128 }));
}
const builtTile = await renderToImage(tgfxTextureScene(2, 64), { width: 64, height: 64 });
// Here, not in the scene: renderToImage needs no canvas mounted around it.
const builtAtlas = await renderToImage(tgfxAtlasScene(), { width: ATLAS_CELL * 2, height: ATLAS_CELL });
if (!alive) return;
setTgfxImages(built);
setTgfxTile(builtTile);
setTgfxAtlas(builtAtlas);
})();
return () => {
alive = false;
Expand Down Expand Up @@ -519,7 +595,7 @@ export default function App() {
{isAnim(scene) ? (
<TgfxAnimScene scene={scene} t={t} />
) : (
<TgfxScene scene={scene} t={t} images={tgfxImages} tile={tgfxTile} />
<TgfxScene scene={scene} t={t} images={tgfxImages} tile={tgfxTile} atlas={tgfxAtlas} />
)}
</TgfxCanvas>
)
Expand All @@ -533,7 +609,7 @@ export default function App() {
deviceIndependentFonts={scene === 'text' && dif}
>
<SkFill color={BG} />
{scene === 'shapes' ? (
{scene === 'shapes' || scene === 'shapes-atlas' ? (
<SkBlobShapes t={t} />
) : isAnim(scene) ? (
<SkiaAnimScene scene={scene} t={t} />
Expand Down
15 changes: 12 additions & 3 deletions apps/benchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ Static scenes: 20,000 shapes, 3,600 images, a 40-octave full-screen shader, a
repeating image pattern, 10,000 cubic paths (half stroked) and 6,000 texts, each
under one animated rotation driven by a shared value. `paths<N>` runs any other
path count (`paths900` is the scene the tables before 2026-11-10 used; both
engines sit at 120 fps on it on a current flagship).
engines sit at 120 fps on it on a current flagship). The default matrix runs
`paths900`, `paths2500`, `paths5000` and `paths` (10,000), so a table shows
where each engine falls off.

Skia draws the static shapes through `Skia.ShapeBlob` + `drawShapes` (3.10),
recorded once into a picture: its explicit batched-shape API, the counterpart of
the batching tgfx does internally for plain `<Rect>`/`<Circle>` nodes. The
unbatched Skia path is the `shapes-picture` row.
unbatched Skia path is the `shapes-picture` row. Two more rows hold the
technique equal:

| scene | tgfx | skia |
|---|---|---|
| `shapes` | 20,000 `<Rect>`/`<Circle>` nodes | one ShapeBlob |
| `shapes-nodes` | 20,000 `<Rect>`/`<Circle>` nodes | 20,000 `<Rect>`/`<Circle>` nodes |
| `shapes-atlas` | a white circle and rect rasterized once into an atlas, 20,000 tinted quads in one `<Vertices>` | one ShapeBlob (the same technique, built in) |

Per-shape scenes (`AnimScenes.tsx`): every shape orbits its own home on its own
phase and speed, so no transform can be shared and tgfx's draw reordering
Expand Down Expand Up @@ -56,7 +65,7 @@ app and the rows stop.
| flag | default | |
|---|---|---|
| `--engines` | `tgfx,skia` | engine order; `skia,tgfx` runs skia first |
| `--scenes` | all nine | subset, e.g. `paths,anim-imp`, or `paths2500` for another path count |
| `--scenes` | all fourteen | subset, e.g. `paths,anim-imp`, or `paths7500` for another path count |
| `--views` | `surface,texture` | outer loop of the matrix |
| `--skip` | none | `engine:scene` pairs to leave out, e.g. `tgfx:anim-imp` |
| `--order` | `sequential` | `alternate` runs every engine per scene |
Expand Down
Loading
Loading