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
41 changes: 41 additions & 0 deletions apps/example/src/imperative.harness.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,47 @@ describe('tgfx canvas handle', () => {
expect(outside.some(hit => hit.node === disc)).toBe(false);
});

test('reports the declared path of an unnamed node regrouped for batching', async () => {
let canvas: React.RefObject<CanvasHandle | null> | null = null;

function Scene() {
const ref = useRef<CanvasHandle>(null);
canvas = ref;
// Unnamed disjoint rects and circles, interleaved: enough siblings that
// the renderer regroups them by kind and draws the circles after every
// rect. A `ref` would slot their geometry and pin them in place.
return (
<Stage canvasRef={ref}>
<Fill color="white" />
{Array.from({ length: 8 }, (_, i) => {
const x = 10 + (i % 4) * 48;
const y = 10 + Math.floor(i / 4) * 48;
return i % 2 === 0 ? (
<Rect
key={i}
x={x}
y={y}
width={32}
height={32}
color="#0A84FF"
/>
) : (
<Circle key={i} cx={x + 16} cy={y + 16} r={16} color="#FF375F" />
);
})}
</Stage>
);
}

await render(<Scene />);
await sleep(300);

// The first circle, declared at index 2: the <Fill> is 0, the first rect 1.
const hits = canvas!.current!.hitTest(74, 26);
expect(hits.length).toBeGreaterThan(0);
expect(hits[0]!.path).toEqual([2]);
});

test('reads the canvas back as encoded png bytes', async () => {
let canvas: React.RefObject<CanvasHandle | null> | null = null;

Expand Down
13 changes: 9 additions & 4 deletions packages/react-native-tgfx/cpp/Scene/TgfxScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,8 @@ bool clusterable(const DrawCmd& cmd, uint64_t* key, tgfx::Rect* bounds) {
/// Reorders `children` so same-class disjoint draws sit together. A node may
/// only hop over clusters whose united bounds it provably does not touch, so
/// every move is between draws that commute; hit-test results cannot change
/// either, since a point can hit at most one of any pair this separates.
/// either, since a point can hit at most one of any pair this separates and
/// a hit reports each node's declared `index`, not where it lands here.
void clusterBatchableChildren(std::vector<DrawCmd>& children) {
// Too few nodes to fragment into batches worth saving.
if (children.size() < 8) {
Expand Down Expand Up @@ -1560,6 +1561,7 @@ bool SceneRenderer::readNodeFields(SceneReader& r, DrawCmd& cmd, int depth) {
if (!readNode(r, child, depth + 1)) {
return false;
}
child.index = i;
cmd.children.push_back(std::move(child));
}
if (cmd.cachePicture && r.dynamic != dynamicBefore) {
Expand Down Expand Up @@ -1593,6 +1595,7 @@ bool SceneRenderer::readNodeFields(SceneReader& r, DrawCmd& cmd, int depth) {
if (!readNode(r, child, depth + 1)) {
return false;
}
child.index = i;
cmd.children.push_back(std::move(child));
}
if (cmd.cachePicture && r.dynamic != dynamicBefore) {
Expand Down Expand Up @@ -1625,6 +1628,7 @@ bool SceneRenderer::readNodeFields(SceneReader& r, DrawCmd& cmd, int depth) {
if (!readNode(r, child, depth + 1)) {
return false;
}
child.index = i;
cmd.children.push_back(std::move(child));
}
if (cmd.cachePicture && r.dynamic != dynamicBefore) {
Expand Down Expand Up @@ -2038,6 +2042,7 @@ void SceneRenderer::setScene(std::span<const double> words,
if (!readNode(reader, cmd, 0)) {
break;
}
cmd.index = i;
program->push_back(std::move(cmd));
}
if (!reader.ok) {
Expand Down Expand Up @@ -2183,7 +2188,7 @@ void hitWalk(const std::vector<DrawCmd>& nodes, const tgfx::Matrix& parent,
continue;
}
if (opensContext(cmd)) {
path.push_back(static_cast<int32_t>(i));
path.push_back(cmd.index);
hitContext(cmd, parent, point, precise, v, strings, pushed, path, out);
path.pop_back();
continue;
Expand All @@ -2207,7 +2212,7 @@ void hitWalk(const std::vector<DrawCmd>& nodes, const tgfx::Matrix& parent,
continue;
}
}
path.push_back(static_cast<int32_t>(i));
path.push_back(cmd.index);
if (!cmd.children.empty()) {
hitWalk(cmd.children, matrix, point, precise, v, strings, pushed, path, out);
}
Expand Down Expand Up @@ -2239,7 +2244,7 @@ void collectHitPlanes(const DrawCmd& cmd, const tgfx::Matrix3D& parent,
if (child.kind != DrawCmd::Group || hidden(child, v)) {
continue;
}
path.push_back(static_cast<int32_t>(i));
path.push_back(child.index);
collectHitPlanes(child, placed, v, path, out);
path.pop_back();
}
Expand Down
2 changes: 2 additions & 0 deletions packages/react-native-tgfx/cpp/Scene/TgfxScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,8 @@ struct DrawCmd {
/// Behind a pointer so a DrawCmd that does not clip costs no Path allocation.
std::shared_ptr<tgfx::Path> clipPath;
Kind kind = Group;
/// Declared position among its siblings, which hit paths report: clustering reorders `children`.
int32_t index = 0;

/// Kind-word bit 64: skip draw and hit-test when it resolves below 0.5.
AnimF visible{1, -1};
Expand Down
47 changes: 47 additions & 0 deletions packages/react-native-tgfx/cpp/tests/SceneTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5673,6 +5673,52 @@ void batchClusteringKeepsOverlapOrderAndPixels() {
CHECK(a != render(flipped.finish(), {}, {}));
}

/// Clustering moves the circles after every rect, but a hit still reports the
/// child index the program declared, which is how an unnamed node is told apart.
void hitTestNamesARegroupedSiblingByItsDeclaredIndex() {
auto pushShapes = [](Writer& w) {
for (int i = 0; i < 8; i++) {
double x = 10 + (i % 4) * 48;
double y = 10 + (i / 4) * 48;
if (i % 2 == 0) {
w.push(1); // Rect
w.pushAnim(x);
w.pushAnim(y);
w.pushAnim(32);
w.pushAnim(32);
w.pushPaint(0xff0a84ff, false);
} else {
w.push(4); // Circle
w.pushAnim(x + 16);
w.pushAnim(y + 16);
w.pushAnim(16);
w.pushPaint(0xffff375f, false);
}
}
};

// 74,26 is the first circle, declared at index 1.
Writer roots;
roots.push(8);
pushShapes(roots);
tgfxrn::SceneRenderer flat;
flat.setScene(roots.finish(), {});
auto hit = flat.hitTest(74, 26, false, {});
CHECK(hit.size() == 2 && hit[0] == 1 && hit[1] == 1);

// A group's children are regrouped the same way.
Writer nested;
nested.push(1);
nested.push(0); // Group
nested.push(0); // no transform ops
nested.push(8);
pushShapes(nested);
tgfxrn::SceneRenderer grouped;
grouped.setScene(nested.finish(), {});
auto inGroup = grouped.hitTest(74, 26, false, {});
CHECK(inGroup.size() == 3 && inGroup[0] == 2 && inGroup[1] == 0 && inGroup[2] == 1);
}

/// setScene-then-hitTest must answer before the worker's first drain.
void anOffscreenSurfaceHitTestsBeforeTheFirstDrain() {
auto surface = std::make_shared<tgfxrn::OffscreenSurface>(8, 8, 1.0f, true, false);
Expand Down Expand Up @@ -6017,6 +6063,7 @@ int main() {
encodingUnpremultipliesAndRejectsLiveFrames();
anOffscreenSurfaceHitTestsBeforeTheFirstDrain();
batchClusteringKeepsOverlapOrderAndPixels();
hitTestNamesARegroupedSiblingByItsDeclaredIndex();
preserve3dKeepsTheGroupsOwnClipAndLayer();
aPlaneOfA3DContextKeepsItsClipAndLayer();
aFillOnA3DPlaneDraws();
Expand Down
Loading