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