Skip to content

Commit

Permalink
Remove the GenerateImages target and create baseline images while upd…
Browse files Browse the repository at this point in the history
…ating the local baseline config. (#2656)
  • Loading branch information
domchen authored Jan 9, 2025
1 parent 50267cb commit 944cfd2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ cmake-build-*
node_modules
Build/
test/out/
test/baseline-out/
DerivedData/
xcuserdata
xcshareddata
Expand Down
18 changes: 7 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -583,24 +583,20 @@ if (PAG_BUILD_TESTS)
list(REMOVE_ITEM PAG_TEST_FILES ${PAG_TEST_EXCLUDE_FILES})
endif ()

# used to update the local md5 data for baseline testing.
list(APPEND PAG_BASELINE_DEFINES ${PAG_TEST_DEFINES} UPDATE_BASELINE)
if (NOT PAG_SKIP_GENERATE_BASELINE_IMAGES)
list(APPEND PAG_BASELINE_DEFINES GENERATE_BASELINE_IMAGES)
endif ()
# used to update the local md5 data for baseline testing and generate baseline images to the
# out/ directory. each image has a "_base" suffix.
add_executable(UpdateBaseline ${PAG_TEST_FILES})
add_dependencies(UpdateBaseline test-vendor)
target_include_directories(UpdateBaseline PUBLIC ${PAG_TEST_INCLUDES})
target_compile_definitions(UpdateBaseline PUBLIC ${PAG_TEST_DEFINES} UPDATE_BASELINE)
target_compile_definitions(UpdateBaseline PUBLIC ${PAG_BASELINE_DEFINES})
target_compile_options(UpdateBaseline PUBLIC ${PAG_TEST_COMPILE_OPTIONS})
target_link_options(UpdateBaseline PRIVATE ${PAG_TEST_LINK_OPTIONS})
target_link_libraries(UpdateBaseline ${PAG_TEST_LIBS})

# used to generate baseline images to the out/ directory. each image has a "_base" suffix.
add_executable(GenerateImages ${PAG_TEST_FILES})
add_dependencies(GenerateImages test-vendor)
target_include_directories(GenerateImages PUBLIC ${PAG_TEST_INCLUDES})
target_compile_definitions(GenerateImages PUBLIC ${PAG_TEST_DEFINES} GENERATOR_BASELINE_IMAGES)
target_compile_options(GenerateImages PUBLIC ${PAG_TEST_COMPILE_OPTIONS})
target_link_options(GenerateImages PRIVATE ${PAG_TEST_LINK_OPTIONS})
target_link_libraries(GenerateImages ${PAG_TEST_LIBS})

add_executable(PAGUnitTest ${PAG_TEST_FILES})
add_dependencies(PAGUnitTest test-vendor)
target_include_directories(PAGUnitTest PUBLIC ${PAG_TEST_INCLUDES})
Expand Down
22 changes: 18 additions & 4 deletions test/src/utils/Baseline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@
#include <chrono>
#include <fstream>
#include <iostream>
#include "base/utils/USE.h"
#include "nlohmann/json.hpp"
#include "tgfx/core/ImageCodec.h"
#include "tgfx/core/Surface.h"
#include "tgfx/gpu/opengl/GLDevice.h"
#include "utils/TestDir.h"
#include "utils/TestUtils.h"

namespace pag {
using namespace tgfx;

static std::unique_ptr<Baseline> baseline = nullptr;
static const std::string BASELINE_ROOT = ProjectPath::Absolute("test/baseline");
static const std::string CACHE_ROOT = TestDir::GetRoot() + "/baseline/.cache";
#ifdef GENERATE_BASELINE_IMAGES
static const std::string OUT_ROOT = TestDir::GetRoot() + "/baseline-out";
#else
static const std::string OUT_ROOT = TestDir::GetRoot() + "/out";
static std::unique_ptr<Baseline> baseline = nullptr;
#endif

static void RemoveEmptyFolder(const std::filesystem::path& path) {
if (!std::filesystem::is_directory(path)) {
Expand All @@ -58,7 +62,6 @@ void Baseline::SetUp() {
void Baseline::TearDown() {
baseline->saveData();
baseline = nullptr;
RemoveEmptyFolder(OUT_ROOT);
}

bool Baseline::Compare(std::shared_ptr<tgfx::Surface> surface, const std::string& key) {
Expand Down Expand Up @@ -139,7 +142,7 @@ bool Baseline::compare(const Pixmap& pixmap, const std::string& key) {
} else {
SaveImage(pixmap, key);
}
#ifdef GENERATOR_BASELINE_IMAGES
#ifdef GENERATE_BASELINE_IMAGES
SaveImage(pixmap, key + "_base");
#endif
return result;
Expand Down Expand Up @@ -207,14 +210,24 @@ Baseline::Baseline(const std::string& baselinePath, const std::string& cachePath
void Baseline::saveData() {
#ifdef UPDATE_BASELINE
if (!PAGTest::HasFailure()) {
#ifdef GENERATE_BASELINE_IMAGES
auto outPath = TestDir::GetRoot() + "/out";
std::filesystem::remove_all(outPath);
if (std::filesystem::exists(OUT_ROOT)) {
std::filesystem::rename(OUT_ROOT, outPath);
}
#endif
CreateFolder(cacheMD5Path);
std::ofstream outMD5File(cacheMD5Path);
outMD5File << std::setw(4) << outputMD5 << std::endl;
outMD5File.close();
CreateFolder(cacheVersionPath);
std::filesystem::copy(baselineVersionPath, cacheVersionPath,
std::filesystem::copy_options::overwrite_existing);
} else {
std::filesystem::remove_all(OUT_ROOT);
}
USE(RemoveEmptyFolder);
#else
std::filesystem::remove(outMD5Path);
if (!outputMD5.empty()) {
Expand All @@ -227,6 +240,7 @@ void Baseline::saveData() {
std::ofstream versionFile(outVersionPath);
versionFile << std::setw(4) << outputVersions << std::endl;
versionFile.close();
RemoveEmptyFolder(OUT_ROOT);
#endif
}

Expand Down
4 changes: 4 additions & 0 deletions test/src/utils/TestUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ std::shared_ptr<tgfx::Data> ReadFile(const std::string& path) {
}

std::string GetOutputFile(const std::string& key) {
#ifdef GENERATE_BASELINE_IMAGES
static const std::string OUT_ROOT = TestDir::GetRoot() + "/baseline-out";
#else
static const std::string OUT_ROOT = TestDir::GetRoot() + "/out";
#endif
return OUT_ROOT + "/" + key + ".webp";
}

Expand Down
2 changes: 1 addition & 1 deletion update_baseline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
echo $CMAKE_COMMAND

if [[ $1 == "1" ]]; then
$CMAKE_COMMAND -DCMAKE_CXX_FLAGS="-fprofile-arcs -ftest-coverage -g -O0" -DPAG_USE_SWIFTSHADER=ON -DPAG_BUILD_TESTS=ON -DPAG_SKIP_BASELINE_CHECK=ON -DPAG_ENABLE_PROFILING=OFF -DCMAKE_BUILD_TYPE=Debug ../
$CMAKE_COMMAND -DCMAKE_CXX_FLAGS="-fprofile-arcs -ftest-coverage -g -O0" -DPAG_USE_SWIFTSHADER=ON -DPAG_SKIP_GENERATE_BASELINE_IMAGES=ON -DPAG_BUILD_TESTS=ON -DPAG_SKIP_BASELINE_CHECK=ON -DPAG_ENABLE_PROFILING=OFF -DCMAKE_BUILD_TYPE=Debug ../
else
$CMAKE_COMMAND -DPAG_BUILD_TESTS=ON -DPAG_SKIP_BASELINE_CHECK=ON -DPAG_ENABLE_PROFILING=OFF -DCMAKE_BUILD_TYPE=Debug ../
fi
Expand Down

0 comments on commit 944cfd2

Please sign in to comment.