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