Skip to content

Commit 8ea4cb5

Browse files
authored
Add helpers for backend tests. (#8505)
Both for cleaning up graphics resources at the end of a test and for starting/stopping render loops. BUGS=[402472882]
1 parent 4f8080e commit 8ea4cb5

File tree

4 files changed

+281
-141
lines changed

4 files changed

+281
-141
lines changed

filament/backend/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ if (APPLE OR LINUX)
461461
test/ShaderGenerator.cpp
462462
test/TrianglePrimitive.cpp
463463
test/Arguments.cpp
464+
test/Lifetimes.cpp
464465
test/test_FeedbackLoops.cpp
465466
test/test_Blit.cpp
466467
test/test_MissingRequiredAttributes.cpp

filament/backend/test/Lifetimes.cpp

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (C) 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "Lifetimes.h"
18+
19+
#include "backend/Platform.h"
20+
#include "private/backend/DriverApi.h"
21+
22+
RenderFrame::RenderFrame(filament::backend::DriverApi& api): mApi(api) {
23+
mApi.beginFrame(0, 0, 0);
24+
}
25+
26+
RenderFrame::~RenderFrame() {
27+
mApi.endFrame(0);
28+
}
29+
30+
Cleanup::Cleanup(filament::backend::DriverApi& driverApi) : mDriverApi(driverApi) {}
31+
32+
Cleanup::~Cleanup() {
33+
for (const auto swapChain: mSwapChains) {
34+
mDriverApi.destroySwapChain(swapChain);
35+
}
36+
for (const auto descriptorSet : mDescriptorSets) {
37+
mDriverApi.destroyDescriptorSet(descriptorSet);
38+
}
39+
for (const auto descriptorSetLayout : mDescriptorSetLayouts) {
40+
mDriverApi.destroyDescriptorSetLayout(descriptorSetLayout);
41+
}
42+
for (const auto bufferObject : mBufferObjects) {
43+
mDriverApi.destroyBufferObject(bufferObject);
44+
}
45+
for (const auto program : mPrograms) {
46+
mDriverApi.destroyProgram(program);
47+
}
48+
for (const auto texture : mTextures) {
49+
mDriverApi.destroyTexture(texture);
50+
}
51+
for (const auto renderTarget : mRenderTargets) {
52+
mDriverApi.destroyRenderTarget(renderTarget);
53+
}
54+
55+
for (const auto& callback : mCallbacks) {
56+
callback();
57+
}
58+
}
59+
60+
template <>
61+
void Cleanup::addInternal(filament::backend::SwapChainHandle handle) {
62+
mSwapChains.push_back(handle);
63+
}
64+
template <>
65+
void Cleanup::addInternal(filament::backend::DescriptorSetHandle handle) {
66+
mDescriptorSets.push_back(handle);
67+
}
68+
template <>
69+
void Cleanup::addInternal(
70+
filament::backend::DescriptorSetLayoutHandle handle) {
71+
mDescriptorSetLayouts.push_back(handle);
72+
}
73+
template <>
74+
void Cleanup::addInternal(filament::backend::BufferObjectHandle handle) {
75+
mBufferObjects.push_back(handle);
76+
}
77+
template <>
78+
void Cleanup::addInternal(filament::backend::ProgramHandle handle) {
79+
mPrograms.push_back(handle);
80+
}
81+
template <>
82+
void Cleanup::addInternal(filament::backend::TextureHandle handle) {
83+
mTextures.push_back(handle);
84+
}
85+
template <>
86+
void Cleanup::addInternal(filament::backend::RenderTargetHandle handle) {
87+
mRenderTargets.push_back(handle);
88+
}
89+
90+
void Cleanup::addPostCall(std::function<void()> callback){
91+
mCallbacks.emplace_back(std::move(callback));
92+
}

filament/backend/test/Lifetimes.h

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (C) 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef TNT_LIFETIMES_H
18+
#define TNT_LIFETIMES_H
19+
20+
#include <vector>
21+
#include <functional>
22+
#include <optional>
23+
24+
#include "backend/Handle.h"
25+
#include "backend/DriverApiForward.h"
26+
#include "BackendTest.h"
27+
28+
class RenderFrame {
29+
public:
30+
explicit RenderFrame(filament::backend::DriverApi& api);
31+
~RenderFrame();
32+
33+
RenderFrame(RenderFrame const& rhs) noexcept = delete;
34+
RenderFrame& operator=(RenderFrame const& rhs) noexcept = delete;
35+
RenderFrame(RenderFrame const&& rhs) noexcept = delete;
36+
RenderFrame& operator=(RenderFrame const&& rhs) noexcept = delete;
37+
38+
private:
39+
filament::backend::DriverApi& mApi;
40+
};
41+
42+
class Cleanup {
43+
public:
44+
/**
45+
* @param driverApi Must be valid for the entire lifetime of the Cleanup object.
46+
*/
47+
explicit Cleanup(filament::backend::DriverApi& driverApi);
48+
~Cleanup();
49+
50+
template <typename HandleType>
51+
filament::backend::Handle<HandleType> add(filament::backend::Handle<HandleType> handle);
52+
53+
void addPostCall(std::function<void()> callback);
54+
55+
private:
56+
template <typename HandleType>
57+
void addInternal(filament::backend::Handle<HandleType> handle);
58+
59+
filament::backend::DriverApi & mDriverApi;
60+
61+
std::vector<filament::backend::SwapChainHandle> mSwapChains;
62+
std::vector<filament::backend::DescriptorSetHandle> mDescriptorSets;
63+
std::vector<filament::backend::DescriptorSetLayoutHandle> mDescriptorSetLayouts;
64+
std::vector<filament::backend::BufferObjectHandle> mBufferObjects;
65+
std::vector<filament::backend::ProgramHandle> mPrograms;
66+
std::vector<filament::backend::TextureHandle> mTextures;
67+
std::vector<filament::backend::RenderTargetHandle> mRenderTargets;
68+
69+
std::vector<std::function<void()>> mCallbacks;
70+
};
71+
72+
template <typename HandleType>
73+
filament::backend::Handle<HandleType> Cleanup::add(filament::backend::Handle<HandleType> handle) {
74+
addInternal(handle);
75+
return handle;
76+
}
77+
78+
#endif //TNT_LIFETIMES_H

0 commit comments

Comments
 (0)