diff --git a/apps/example/src/imperative.harness.tsx b/apps/example/src/imperative.harness.tsx index c28cd2e..b1a7f41 100644 --- a/apps/example/src/imperative.harness.tsx +++ b/apps/example/src/imperative.harness.tsx @@ -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 | null = null; + + function Scene() { + const ref = useRef(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 ( + + + {Array.from({ length: 8 }, (_, i) => { + const x = 10 + (i % 4) * 48; + const y = 10 + Math.floor(i / 4) * 48; + return i % 2 === 0 ? ( + + ) : ( + + ); + })} + + ); + } + + await render(); + await sleep(300); + + // The first circle, declared at index 2: the 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 | null = null; diff --git a/packages/react-native-tgfx/cpp/Scene/TgfxScene.cpp b/packages/react-native-tgfx/cpp/Scene/TgfxScene.cpp index b97217c..a52a471 100644 --- a/packages/react-native-tgfx/cpp/Scene/TgfxScene.cpp +++ b/packages/react-native-tgfx/cpp/Scene/TgfxScene.cpp @@ -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& children) { // Too few nodes to fragment into batches worth saving. if (children.size() < 8) { @@ -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) { @@ -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) { @@ -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) { @@ -2038,6 +2042,7 @@ void SceneRenderer::setScene(std::span words, if (!readNode(reader, cmd, 0)) { break; } + cmd.index = i; program->push_back(std::move(cmd)); } if (!reader.ok) { @@ -2183,7 +2188,7 @@ void hitWalk(const std::vector& nodes, const tgfx::Matrix& parent, continue; } if (opensContext(cmd)) { - path.push_back(static_cast(i)); + path.push_back(cmd.index); hitContext(cmd, parent, point, precise, v, strings, pushed, path, out); path.pop_back(); continue; @@ -2207,7 +2212,7 @@ void hitWalk(const std::vector& nodes, const tgfx::Matrix& parent, continue; } } - path.push_back(static_cast(i)); + path.push_back(cmd.index); if (!cmd.children.empty()) { hitWalk(cmd.children, matrix, point, precise, v, strings, pushed, path, out); } @@ -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(i)); + path.push_back(child.index); collectHitPlanes(child, placed, v, path, out); path.pop_back(); } diff --git a/packages/react-native-tgfx/cpp/Scene/TgfxScene.hpp b/packages/react-native-tgfx/cpp/Scene/TgfxScene.hpp index c749af9..1046f0a 100644 --- a/packages/react-native-tgfx/cpp/Scene/TgfxScene.hpp +++ b/packages/react-native-tgfx/cpp/Scene/TgfxScene.hpp @@ -400,6 +400,8 @@ struct DrawCmd { /// Behind a pointer so a DrawCmd that does not clip costs no Path allocation. std::shared_ptr 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}; diff --git a/packages/react-native-tgfx/cpp/tests/SceneTest.cpp b/packages/react-native-tgfx/cpp/tests/SceneTest.cpp index b1be5d3..9943e4a 100644 --- a/packages/react-native-tgfx/cpp/tests/SceneTest.cpp +++ b/packages/react-native-tgfx/cpp/tests/SceneTest.cpp @@ -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(8, 8, 1.0f, true, false); @@ -6017,6 +6063,7 @@ int main() { encodingUnpremultipliesAndRejectsLiveFrames(); anOffscreenSurfaceHitTestsBeforeTheFirstDrain(); batchClusteringKeepsOverlapOrderAndPixels(); + hitTestNamesARegroupedSiblingByItsDeclaredIndex(); preserve3dKeepsTheGroupsOwnClipAndLayer(); aPlaneOfA3DContextKeepsItsClipAndLayer(); aFillOnA3DPlaneDraws();