@@ -327,5 +327,69 @@ TEST(EntityPassClipStackTest, ClipAndRestoreWithSubpassesNonAA) {
327327 Rect::MakeLTRB (50 , 50 , 55 , 55 ));
328328}
329329
330+ TEST (EntityPassClipStackTest, RestoringChildClipPreservesParentReplayEntities) {
331+ EntityPassClipStack recorder =
332+ EntityPassClipStack (Rect::MakeLTRB (0 , 0 , 100 , 100 ));
333+
334+ // 1. Record parent clip at height 1.
335+ // Non-axis-aligned (modeling e.g. a rounded rect) so RecordClip does not
336+ // elide it against the initial (0, 0, 100, 100) pass coverage and records a
337+ // replay entity at height 1.
338+ recorder.RecordClip (ClipContents (Rect::MakeLTRB (0 , 0 , 100 , 100 ),
339+ /* is_axis_aligned_rect=*/ false ),
340+ Matrix (), {0 , 0 }, 0 , 100 , /* is_aa=*/ true );
341+ ASSERT_EQ (recorder.GetReplayEntities ().size (), 1u );
342+ EXPECT_EQ (recorder.GetReplayEntities ()[0 ].clip_height , 1u );
343+
344+ // 2. Record a redundant child clip at height 2 that skips replay recording.
345+ // Because it is both axis-aligned (no rounded corners cutting inside its
346+ // bounds) and contains the parent's (0, 0, 100, 100) coverage, RecordClip
347+ // pushes coverage at height 2 and returns early without pushing a replay
348+ // entity.
349+ recorder.RecordClip (ClipContents (Rect::MakeLTRB (0 , 0 , 100 , 100 ),
350+ /* is_axis_aligned_rect=*/ true ),
351+ Matrix (), {0 , 0 }, 1 , 100 , /* is_aa=*/ true );
352+ EXPECT_EQ (recorder.GetReplayEntities ().size (), 1u );
353+ // Restoring to height 1 must NOT pop the parent replay entity, even though
354+ // this child clip pushed no replay entity of its own.
355+ recorder.RecordRestore ({0 , 0 }, 1 );
356+ ASSERT_EQ (recorder.GetReplayEntities ().size (), 1u );
357+ EXPECT_EQ (recorder.GetReplayEntities ()[0 ].clip_height , 1u );
358+
359+ // 3. Record a child clip at height 2 that DOES push a replay entity.
360+ // Unlike step 2, this clip is non-axis-aligned and narrower than the parent,
361+ // so RecordClip records a second replay entity at height 2.
362+ recorder.RecordClip (ClipContents (Rect::MakeLTRB (10 , 10 , 50 , 50 ),
363+ /* is_axis_aligned_rect=*/ false ),
364+ Matrix (), {0 , 0 }, 2 , 100 , /* is_aa=*/ true );
365+ ASSERT_EQ (recorder.GetReplayEntities ().size (), 2u );
366+ EXPECT_EQ (recorder.GetReplayEntities ()[1 ].clip_height , 2u );
367+ // Restoring to height 1 must pop the child replay entity while preserving the
368+ // parent replay entity.
369+ recorder.RecordRestore ({0 , 0 }, 1 );
370+ ASSERT_EQ (recorder.GetReplayEntities ().size (), 1u );
371+ EXPECT_EQ (recorder.GetReplayEntities ()[0 ].clip_height , 1u );
372+
373+ // 4. Record two nested rendered clips in a child scope (heights 2 and 3) and
374+ // restore directly to height 1; both child clips must be popped while the
375+ // parent clip remains.
376+ recorder.RecordClip (ClipContents (Rect::MakeLTRB (15 , 15 , 45 , 45 ),
377+ /* is_axis_aligned_rect=*/ false ),
378+ Matrix (), {0 , 0 }, 3 , 100 , /* is_aa=*/ true );
379+ recorder.RecordClip (ClipContents (Rect::MakeLTRB (20 , 20 , 40 , 40 ),
380+ /* is_axis_aligned_rect=*/ false ),
381+ Matrix (), {0 , 0 }, 4 , 100 , /* is_aa=*/ true );
382+ ASSERT_EQ (recorder.GetReplayEntities ().size (), 3u );
383+ EXPECT_EQ (recorder.GetReplayEntities ()[1 ].clip_height , 2u );
384+ EXPECT_EQ (recorder.GetReplayEntities ()[2 ].clip_height , 3u );
385+ recorder.RecordRestore ({0 , 0 }, 1 );
386+ ASSERT_EQ (recorder.GetReplayEntities ().size (), 1u );
387+ EXPECT_EQ (recorder.GetReplayEntities ()[0 ].clip_height , 1u );
388+
389+ // 5. Restore to height 0 (popping parent clip).
390+ recorder.RecordRestore ({0 , 0 }, 0 );
391+ EXPECT_TRUE (recorder.GetReplayEntities ().empty ());
392+ }
393+
330394} // namespace testing
331395} // namespace impeller
0 commit comments