forked from hrydgard/ppsspp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompare.cpp
384 lines (324 loc) · 9.19 KB
/
Compare.cpp
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright (c) 2012- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "headless/Compare.h"
#include "file/file_util.h"
#include "Common/ColorConv.h"
#include "Common/FileUtil.h"
#include "Core/Host.h"
#include "GPU/GPUState.h"
#include "GPU/Common/GPUDebugInterface.h"
#include "GPU/Common/TextureDecoder.h"
#include <algorithm>
#include <cmath>
#include <cstdarg>
#include <iostream>
#include <fstream>
#include <vector>
bool teamCityMode = false;
std::string teamCityName = "";
void TeamCityPrint(const char *fmt, ...)
{
if (!teamCityMode)
return;
const int TEMP_BUFFER_SIZE = 32768;
char temp[TEMP_BUFFER_SIZE];
va_list args;
va_start(args, fmt);
vsnprintf(temp, TEMP_BUFFER_SIZE - 1, fmt, args);
temp[TEMP_BUFFER_SIZE - 1] = '\0';
va_end(args);
printf("%s", temp);
}
struct BufferedLineReader {
const static int MAX_BUFFER = 5;
const static int TEMP_BUFFER_SIZE = 32768;
BufferedLineReader(const std::string &data) : valid_(0), data_(data), pos_(0) {
}
void Fill() {
while (valid_ < MAX_BUFFER && HasMoreLines()) {
buffer_[valid_++] = TrimNewlines(ReadLine());
}
}
const std::string Peek(int pos) {
if (pos >= valid_) {
Fill();
}
if (pos >= valid_) {
return "";
}
return buffer_[pos];
}
void Skip(int count) {
if (count > valid_) {
count = valid_;
}
valid_ -= count;
for (int i = 0; i < valid_; ++i) {
buffer_[i] = buffer_[i + count];
}
Fill();
}
const std::string Consume() {
const std::string result = Peek(0);
Skip(1);
return result;
}
bool HasLines() {
if (HasMoreLines()) {
return true;
}
// Don't say yes if it's a blank line.
for (int i = 0; i < valid_; ++i) {
if (!buffer_[i].empty()) {
return true;
}
}
return false;
}
bool Compare(BufferedLineReader &other) {
if (Peek(0) != other.Peek(0)) {
return false;
}
Skip(1);
other.Skip(1);
return true;
}
protected:
BufferedLineReader() : valid_(0) {
}
virtual bool HasMoreLines() {
return pos_ != data_.npos;
}
virtual std::string ReadLine() {
size_t next = data_.find('\n', pos_);
if (next == data_.npos) {
std::string result = data_.substr(pos_);
pos_ = next;
return result;
} else {
std::string result = data_.substr(pos_, next - pos_);
pos_ = next + 1;
return result;
}
}
static std::string TrimNewlines(const std::string &s) {
size_t p = s.find_last_not_of("\r\n");
if (p == s.npos) {
return "";
}
return s.substr(0, p + 1);
}
int valid_;
std::string buffer_[MAX_BUFFER];
const std::string data_;
size_t pos_;
};
struct BufferedLineReaderFile : public BufferedLineReader {
BufferedLineReaderFile(std::ifstream &in) : BufferedLineReader(), in_(in) {
}
protected:
virtual bool HasMoreLines() {
return !in_.eof();
}
virtual std::string ReadLine() {
char temp[TEMP_BUFFER_SIZE];
in_.getline(temp, TEMP_BUFFER_SIZE);
return temp;
}
std::ifstream &in_;
};
std::string ExpectedFromFilename(const std::string &bootFilename)
{
return bootFilename.substr(0, bootFilename.length() - 4) + ".expected";
}
std::string ExpectedScreenshotFromFilename(const std::string &bootFilename)
{
return bootFilename.substr(0, bootFilename.length() - 4) + ".expected.bmp";
}
static std::string ChopFront(std::string s, std::string front)
{
if (s.size() >= front.size())
{
if (s.substr(0, front.size()) == front)
return s.substr(front.size());
}
return s;
}
static std::string ChopEnd(std::string s, std::string end)
{
if (s.size() >= end.size())
{
size_t endpos = s.size() - end.size();
if (s.substr(endpos) == end)
return s.substr(0, endpos);
}
return s;
}
std::string GetTestName(const std::string &bootFilename)
{
// Kinda ugly, trying to guesstimate the test name from filename...
return ChopEnd(ChopFront(ChopFront(bootFilename, "tests/"), "pspautotests/tests/"), ".prx");
}
bool CompareOutput(const std::string &bootFilename, const std::string &output, bool verbose)
{
std::string expect_filename = ExpectedFromFilename(bootFilename);
std::ifstream expect_f;
expect_f.open(expect_filename.c_str(), std::ios::in);
if (!expect_f.fail())
{
BufferedLineReaderFile expected(expect_f);
BufferedLineReader actual(output);
bool failed = false;
while (expected.HasLines())
{
if (expected.Compare(actual))
continue;
if (!failed)
{
TeamCityPrint("##teamcity[testFailed name='%s' message='Output different from expected file']\n", teamCityName.c_str());
failed = true;
}
// This is a really dirt simple comparing algorithm.
// Perhaps it was an extra line?
if (expected.Peek(0) == actual.Peek(1) || !expected.HasLines())
printf("+ %s\n", actual.Consume().c_str());
// A single missing line?
else if (expected.Peek(1) == actual.Peek(0) || !actual.HasLines())
printf("- %s\n", expected.Consume().c_str());
else
{
printf("O %s\n", actual.Consume().c_str());
printf("E %s\n", expected.Consume().c_str());
}
}
while (actual.HasLines())
{
// If it's a blank line, this will pass.
if (actual.Compare(expected))
continue;
printf("+ %s\n", actual.Consume().c_str());
}
expect_f.close();
if (verbose)
{
if (!failed)
{
printf("++++++++++++++ The Equal Output +++++++++++++\n");
printf("%s", output.c_str());
printf("+++++++++++++++++++++++++++++++++++++++++++++\n");
}
else
{
printf("============== output from failed %s:\n", GetTestName(bootFilename).c_str());
printf("%s", output.c_str());
printf("============== expected output:\n");
std::string fullExpected;
if (readFileToString(true, expect_filename.c_str(), fullExpected))
printf("%s", fullExpected.c_str());
printf("===============================\n");
}
}
return !failed;
}
else
{
fprintf(stderr, "Expectation file %s not found\n", expect_filename.c_str());
TeamCityPrint("##teamcity[testIgnored name='%s' message='Expects file missing']\n", teamCityName.c_str());
return false;
}
}
inline int ComparePixel(u32 pix1, u32 pix2)
{
// For now, if they're different at all except alpha, it's an error.
if ((pix1 & 0xFFFFFF) != (pix2 & 0xFFFFFF))
return 1;
return 0;
}
std::vector<u32> TranslateDebugBufferToCompare(const GPUDebugBuffer *buffer, u32 stride, u32 h)
{
// If the output was small, act like everything outside was 0.
// This can happen depending on viewport parameters.
u32 safeW = std::min(stride, buffer->GetStride());
u32 safeH = std::min(h, buffer->GetHeight());
std::vector<u32> data;
data.resize(stride * h, 0);
const u32 *pixels32 = (const u32 *)buffer->GetData();
const u16 *pixels16 = (const u16 *)buffer->GetData();
int outStride = buffer->GetStride();
if (!buffer->GetFlipped()) {
// Bitmaps are flipped, so we have to compare backwards in this case.
int toLastRow = outStride * (buffer->GetHeight() - 1);
pixels32 += toLastRow;
pixels16 += toLastRow;
outStride = -outStride;
}
u32 errors = 0;
for (u32 y = 0; y < safeH; ++y) {
switch (buffer->GetFormat()) {
case GPU_DBG_FORMAT_8888:
ConvertBGRA8888ToRGBA8888(&data[y * stride], pixels32, safeW);
break;
case GPU_DBG_FORMAT_8888_BGRA:
memcpy(&data[y * stride], pixels32, safeW * sizeof(u32));
break;
case GPU_DBG_FORMAT_565:
ConvertRGB565ToBGRA8888(&data[y * stride], pixels16, safeW);
break;
case GPU_DBG_FORMAT_5551:
ConvertRGBA5551ToBGRA8888(&data[y * stride], pixels16, safeW);
break;
case GPU_DBG_FORMAT_4444:
ConvertRGBA4444ToBGRA8888(&data[y * stride], pixels16, safeW);
break;
default:
data.resize(0);
return data;
}
pixels32 += outStride;
pixels16 += outStride;
}
return data;
}
double CompareScreenshot(const std::vector<u32> &pixels, u32 stride, u32 w, u32 h, const std::string& screenshotFilename, std::string &error)
{
if (pixels.size() < stride * h)
{
error = "Buffer format conversion error";
return -1.0f;
}
// We assume the bitmap is the specified size, not including whatever stride.
u32 *reference = (u32 *) calloc(stride * h, sizeof(u32));
FILE *bmp = File::OpenCFile(screenshotFilename, "rb");
if (bmp)
{
// The bitmap header is 14 + 40 bytes. We could validate it but the test would fail either way.
fseek(bmp, 14 + 40, SEEK_SET);
if (fread(reference, sizeof(u32), stride * h, bmp) != stride * h)
error = "Unable to read screenshot data: " + screenshotFilename;
fclose(bmp);
}
else
{
error = "Unable to read screenshot: " + screenshotFilename;
free(reference);
return -1.0f;
}
u32 errors = 0;
for (u32 y = 0; y < h; ++y)
{
for (u32 x = 0; x < w; ++x)
errors += ComparePixel(pixels[y * stride + x], reference[y * stride + x]);
}
free(reference);
return (double) errors / (double) (w * h);
}