@@ -29,58 +29,61 @@ TEST_F(BackendTest, FrameScheduledCallback) {
29
29
SKIP_IF (Backend::VULKAN, " Frame callbacks are unsupported in Vulkan, see b/417254479" );
30
30
SKIP_IF (Backend::WEBGPU, " Frame callbacks are unsupported in WebGPU" );
31
31
32
- auto & api = getDriverApi ();
33
- Cleanup cleanup (api);
34
-
35
- // Create a SwapChain.
36
- // In order for the frameScheduledCallback to be called, this must be a real SwapChain (not
37
- // headless) so we obtain a drawable.
38
- auto swapChain = cleanup.add (createSwapChain ());
39
-
40
- Handle<HwRenderTarget> renderTarget = cleanup.add (api.createDefaultRenderTarget ());
41
-
42
32
int callbackCountA = 0 ;
43
- api.setFrameScheduledCallback (swapChain, nullptr , [&callbackCountA](PresentCallable callable) {
44
- callable ();
45
- callbackCountA++;
46
- }, 0 );
47
-
48
- // Render the first frame.
49
- api.makeCurrent (swapChain, swapChain);
50
- api.beginFrame (0 , 0 , 0 );
51
- api.beginRenderPass (renderTarget, {});
52
- api.endRenderPass (0 );
53
- api.commit (swapChain);
54
- api.endFrame (0 );
55
-
56
- // Render the next frame. The same callback should be called.
57
- api.makeCurrent (swapChain, swapChain);
58
- api.beginFrame (0 , 0 , 0 );
59
- api.beginRenderPass (renderTarget, {});
60
- api.endRenderPass (0 );
61
- api.commit (swapChain);
62
- api.endFrame (0 );
63
-
64
- // Now switch out the callback.
65
33
int callbackCountB = 0 ;
66
- api.setFrameScheduledCallback (swapChain, nullptr , [&callbackCountB](PresentCallable callable) {
67
- callable ();
68
- callbackCountB++;
69
- }, 0 );
70
-
71
- // Render one final frame.
72
- api.makeCurrent (swapChain, swapChain);
73
- api.beginFrame (0 , 0 , 0 );
74
- api.beginRenderPass (renderTarget, {});
75
- api.endRenderPass (0 );
76
- api.commit (swapChain);
77
- api.endFrame (0 );
78
-
79
- api.finish ();
80
-
81
- executeCommands ();
82
- getDriver ().purge ();
83
-
34
+ {
35
+ auto & api = getDriverApi ();
36
+ Cleanup cleanup (api);
37
+ cleanup.addPostCall ([&]() { executeCommands (); });
38
+ cleanup.addPostCall ([&]() { getDriver ().purge (); });
39
+
40
+ // Create a SwapChain.
41
+ // In order for the frameScheduledCallback to be called, this must be a real SwapChain (not
42
+ // headless) so we obtain a drawable.
43
+ auto swapChain = cleanup.add (createSwapChain ());
44
+
45
+ Handle<HwRenderTarget> renderTarget = cleanup.add (api.createDefaultRenderTarget ());
46
+
47
+ api.setFrameScheduledCallback (swapChain, nullptr , [&callbackCountA](PresentCallable callable) {
48
+ callable ();
49
+ callbackCountA++;
50
+ }, 0 );
51
+
52
+ // Render the first frame.
53
+ api.makeCurrent (swapChain, swapChain);
54
+ {
55
+ RenderFrame frame (api);
56
+ api.beginRenderPass (renderTarget, {});
57
+ api.endRenderPass (0 );
58
+ api.commit (swapChain);
59
+ }
60
+
61
+ // Render the next frame. The same callback should be called.
62
+ api.makeCurrent (swapChain, swapChain);
63
+ {
64
+ RenderFrame frame (api);
65
+ api.beginRenderPass (renderTarget, {});
66
+ api.endRenderPass (0 );
67
+ api.commit (swapChain);
68
+ }
69
+
70
+ // Now switch out the callback.
71
+ api.setFrameScheduledCallback (swapChain, nullptr , [&callbackCountB](PresentCallable callable) {
72
+ callable ();
73
+ callbackCountB++;
74
+ }, 0 );
75
+
76
+ // Render one final frame.
77
+ api.makeCurrent (swapChain, swapChain);
78
+ {
79
+ RenderFrame frame (api);
80
+ api.beginRenderPass (renderTarget, {});
81
+ api.endRenderPass (0 );
82
+ api.commit (swapChain);
83
+ }
84
+
85
+ api.finish ();
86
+ }
84
87
EXPECT_EQ (callbackCountA, 2 );
85
88
EXPECT_EQ (callbackCountB, 1 );
86
89
}
@@ -90,43 +93,47 @@ TEST_F(BackendTest, FrameCompletedCallback) {
90
93
SKIP_IF (Backend::VULKAN, " Frame callbacks are unsupported in Vulkan, see b/417254479" );
91
94
SKIP_IF (Backend::WEBGPU, " Frame callbacks are unsupported in WebGPU" );
92
95
93
- auto & api = getDriverApi ();
94
- Cleanup cleanup (api);
95
-
96
- // Create a SwapChain.
97
- auto swapChain = cleanup.add (api.createSwapChainHeadless (256 , 256 , 0 ));
98
-
99
96
int callbackCountA = 0 ;
100
- api.setFrameCompletedCallback (swapChain, nullptr ,
101
- [&callbackCountA]() { callbackCountA++; });
102
-
103
- // Render the first frame.
104
- api.makeCurrent (swapChain, swapChain);
105
- api.beginFrame (0 , 0 , 0 );
106
- api.commit (swapChain);
107
- api.endFrame (0 );
108
-
109
- // Render the next frame. The same callback should be called.
110
- api.makeCurrent (swapChain, swapChain);
111
- api.beginFrame (0 , 0 , 0 );
112
- api.commit (swapChain);
113
- api.endFrame (0 );
114
-
115
- // Now switch out the callback.
116
97
int callbackCountB = 0 ;
117
- api.setFrameCompletedCallback (swapChain, nullptr ,
118
- [&callbackCountB]() { callbackCountB++; });
119
-
120
- // Render one final frame.
121
- api.makeCurrent (swapChain, swapChain);
122
- api.beginFrame (0 , 0 , 0 );
123
- api.commit (swapChain);
124
- api.endFrame (0 );
125
-
126
- api.finish ();
127
-
128
- executeCommands ();
129
- getDriver ().purge ();
98
+ {
99
+ auto & api = getDriverApi ();
100
+ Cleanup cleanup (api);
101
+ cleanup.addPostCall ([&]() { executeCommands (); });
102
+ cleanup.addPostCall ([&]() { getDriver ().purge (); });
103
+
104
+ // Create a SwapChain.
105
+ auto swapChain = cleanup.add (api.createSwapChainHeadless (256 , 256 , 0 ));
106
+
107
+ api.setFrameCompletedCallback (swapChain, nullptr ,
108
+ [&callbackCountA]() { callbackCountA++; });
109
+
110
+ // Render the first frame.
111
+ api.makeCurrent (swapChain, swapChain);
112
+ {
113
+ RenderFrame frame (api);
114
+ api.commit (swapChain);
115
+ }
116
+
117
+ // Render the next frame. The same callback should be called.
118
+ api.makeCurrent (swapChain, swapChain);
119
+ {
120
+ RenderFrame frame (api);
121
+ api.commit (swapChain);
122
+ }
123
+
124
+ // Now switch out the callback.
125
+ api.setFrameCompletedCallback (swapChain, nullptr ,
126
+ [&callbackCountB]() { callbackCountB++; });
127
+
128
+ // Render one final frame.
129
+ api.makeCurrent (swapChain, swapChain);
130
+ {
131
+ RenderFrame frame (api);
132
+ api.commit (swapChain);
133
+ }
134
+
135
+ api.finish ();
136
+ }
130
137
131
138
EXPECT_EQ (callbackCountA, 2 );
132
139
EXPECT_EQ (callbackCountB, 1 );
0 commit comments