1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#include "vkvg.h"
#include <gtest/gtest.h>
#include <filesystem>
//#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
//#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
namespace fs = std::filesystem;
class BasicDrawTest : public testing::Test {
public:
VkvgDevice dev;
VkvgSurface surf;
fs::path targetDir;
fs::path diffDir;
protected:
BasicDrawTest() {
targetDir = fs::path(GTEST_PNG_ROOT) / ::testing::UnitTest::GetInstance()->current_test_suite()->name();
if (!fs::is_directory(targetDir) || !fs::exists(targetDir))
fs::create_directories(targetDir);
diffDir = fs::path("DiffImages") / ::testing::UnitTest::GetInstance()->current_test_suite()->name();
if (!fs::is_directory(diffDir) || !fs::exists(diffDir))
fs::create_directories(diffDir);
vkvg_device_create_info_t info{};
dev = vkvg_device_create(&info);
}
~BasicDrawTest() override {
vkvg_device_destroy(dev);
}
void SetUp() override {
surf = vkvg_surface_create(dev, 256, 256);
}
void TearDown() override {
vkvg_surface_destroy(surf);
}
void compareWithRefImage() {
fs::path targetPath = targetDir / ::testing::UnitTest::GetInstance()->current_test_info()->name();
targetPath.replace_extension(".png");
if (fs::exists(targetPath)) {
int w = 0, h = 0, channels = 0;
unsigned char *refImg = stbi_load(targetPath.c_str(), &w, &h, &channels, 4); // force 4 components per pixel
EXPECT_TRUE(refImg != nullptr) << "Could not load texture from " << targetPath << stbi_failure_reason();
EXPECT_EQ(vkvg_surface_get_width(surf),w);
EXPECT_EQ(vkvg_surface_get_height(surf),h);
unsigned char *img = (unsigned char*)malloc(w*h*4);
unsigned char *diffImg = (unsigned char*)malloc(w*h*4);
vkvg_status_t result = vkvg_surface_write_to_memory(surf, img);
EXPECT_EQ(result, VKVG_STATUS_SUCCESS);
uint32_t totDiff = 0;
for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
unsigned char* refPix = refImg + (y * w + x) * 4;
unsigned char* pix = img + (y * w + x) * 4;
unsigned char* diffPix = diffImg + (y * w + x) * 4;
if (*refPix < *pix)
*diffPix = *pix - *refPix;
else if (*refPix > *pix)
*diffPix = *refPix - *pix;
else
*diffPix = 0;
totDiff += (uint32_t)*diffPix;
}
}
EXPECT_EQ(totDiff, 0);
if (totDiff > 0) {
fs::path diffPath = diffDir / ::testing::UnitTest::GetInstance()->current_test_info()->name();
targetPath.replace_extension(".png");
stbi_write_png(diffPath.c_str(), (int32_t)w, (int32_t)h, 4, diffImg, (int32_t)(4 * w));
}
free(img);
free(diffImg);
} else {
vkvg_status_t result = vkvg_surface_write_to_png(surf, targetPath.c_str());
EXPECT_EQ(result, VKVG_STATUS_SUCCESS);
GTEST_SKIP() << "Updating reference image: " << targetPath;
}
}
};
TEST_F(BasicDrawTest, CtxSolidPaintRGB) {
VkvgContext ctx = vkvg_create(surf);
vkvg_set_source_rgb(ctx,1,0,0);
vkvg_paint(ctx);
vkvg_destroy(ctx);
compareWithRefImage();
}
TEST_F(BasicDrawTest, CtxSolidPaintRGBA) {
VkvgContext ctx = vkvg_create(surf);
vkvg_set_source_rgba(ctx,1,0,0,1);
vkvg_paint(ctx);
vkvg_destroy(ctx);
compareWithRefImage();
}
TEST_F(BasicDrawTest, CtxSolidPaintWithActiveTransform) {
VkvgContext ctx = vkvg_create(surf);
vkvg_translate(ctx,100,100);
vkvg_rotate(ctx, 2.f);
vkvg_set_source_rgba(ctx,1,0,0,1);
vkvg_paint(ctx);
vkvg_destroy(ctx);
compareWithRefImage();
}
TEST_F(BasicDrawTest, CtxSolidPaintOver) {
VkvgContext ctx = vkvg_create(surf);
vkvg_set_source_rgba(ctx,1,0,0,0.5);
vkvg_paint(ctx);
vkvg_set_source_rgba(ctx,0,0,1,0.5);
vkvg_paint(ctx);
vkvg_destroy(ctx);
compareWithRefImage();
}
TEST_F(BasicDrawTest, CtxSolidPaintRectangle) {
VkvgContext ctx = vkvg_create(surf);
vkvg_set_source_rgb(ctx,1,0,0);
vkvg_rectangle(ctx,50,50,200,150);
vkvg_paint(ctx);
vkvg_destroy(ctx);
compareWithRefImage();
}
TEST_F(BasicDrawTest, CtxSolidPaintRectanglesOver) {
VkvgContext ctx = vkvg_create(surf);
vkvg_set_source_rgba(ctx,1,0,0,0.5f);
vkvg_rectangle(ctx,50,50,100,100);
vkvg_paint(ctx);
vkvg_set_source_rgba(ctx,0,0,1,0.5f);
vkvg_rectangle(ctx,100,100,100,100);
vkvg_paint(ctx);
vkvg_destroy(ctx);
compareWithRefImage();
}
|