aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/skqp/gm_knowledge.cpp
blob: bc2ca8201af0705f0716b7970829274e57d04810 (plain)
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
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "gm_knowledge.h"

#include <cfloat>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

#include "../../src/core/SkStreamPriv.h"
#include "SkBitmap.h"
#include "SkCodec.h"
#include "SkOSFile.h"
#include "SkPngEncoder.h"
#include "SkStream.h"

#include "skqp_asset_manager.h"

#define PATH_MAX_PNG "max.png"
#define PATH_MIN_PNG "min.png"
#define PATH_IMG_PNG "image.png"
#define PATH_ERR_PNG "errors.png"
#define PATH_REPORT  "report.html"

////////////////////////////////////////////////////////////////////////////////
inline void path_join_append(std::ostringstream* o) { }

template<class... Types>
void path_join_append(std::ostringstream* o, const char* v, Types... args) {
    constexpr char kPathSeparator[] = "/";
    *o << kPathSeparator << v;
    path_join_append(o, args...);
}

template<class... Types>
std::string path_join(const char* v, Types... args) {
    std::ostringstream o;
    o << v;
    path_join_append(&o, args...);
    return o.str();
}
template<class... Types>
std::string path_join(const std::string& v, Types... args) {
    return path_join(v.c_str(), args...);
}
////////////////////////////////////////////////////////////////////////////////

static int get_error(uint32_t value, uint32_t value_max, uint32_t value_min) {
    int error = 0;
    for (int j : {0, 8, 16, 24}) {
        uint8_t    v = (value     >> j) & 0xFF,
                vmin = (value_min >> j) & 0xFF,
                vmax = (value_max >> j) & 0xFF;
        if (v > vmax) {
            error = std::max(v - vmax, error);
        } else if (v < vmin) {
            error = std::max(vmin - v, error);
        }
    }
    return error;
}

static float set_error_code(gmkb::Error* error_out, gmkb::Error error) {
    SkASSERT(error != gmkb::Error::kNone);
    if (error_out) {
        *error_out = error;
    }
    return FLT_MAX;
}

static SkPixmap to_pixmap(const SkBitmap& bitmap) {
    SkPixmap pixmap;
    SkAssertResult(bitmap.peekPixels(&pixmap));
    return pixmap;
}


static bool WritePixmapToFile(const SkPixmap& pixmap, const char* path) {
    SkFILEWStream wStream(path);
    SkPngEncoder::Options options;
    options.fUnpremulBehavior = SkTransferFunctionBehavior::kIgnore;
    return wStream.isValid() && SkPngEncoder::Encode(&wStream, pixmap, options);
}

constexpr SkColorType kColorType = kRGBA_8888_SkColorType;
constexpr SkAlphaType kAlphaType = kUnpremul_SkAlphaType;

static SkPixmap rgba8888_to_pixmap(const uint32_t* pixels, int width, int height) {
    SkImageInfo info = SkImageInfo::Make(width, height, kColorType, kAlphaType);
    return SkPixmap(info, pixels, width * sizeof(uint32_t));
}

static bool asset_exists(skqp::AssetManager* mgr, const char* path) {
    return mgr && nullptr != mgr->open(path);
}

static bool copy(skqp::AssetManager* mgr, const char* path, const char* dst) {
    if (mgr) {
        if (auto stream = mgr->open(path)) {
            SkFILEWStream wStream(dst);
            return wStream.isValid() && SkStreamCopy(&wStream, stream.get());
        }
    }
    return false;
}

static SkBitmap ReadPngRgba8888FromFile(skqp::AssetManager* assetManager, const char* path) {
    SkBitmap bitmap;
    if (auto codec = SkCodec::MakeFromStream(assetManager->open(path))) {
        SkISize size = codec->getInfo().dimensions();
        SkASSERT(!size.isEmpty());
        SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), kColorType, kAlphaType);
        bitmap.allocPixels(info);
        SkASSERT(bitmap.rowBytes() == (unsigned)bitmap.width() * sizeof(uint32_t));
        if (SkCodec::kSuccess != codec->getPixels(to_pixmap(bitmap))) {
            bitmap.reset();
        }
    }
    return bitmap;
}

namespace gmkb {
// Assumes that for each GM foo, asset_manager has files foo/{max,min}.png
float Check(const uint32_t* pixels,
            int width,
            int height,
            const char* name,
            const char* backend,
            skqp::AssetManager* assetManager,
            const char* report_directory_path,
            Error* error_out) {
    using std::string;
    if (width <= 0 || height <= 0) {
        return set_error_code(error_out, Error::kBadInput);
    }
    size_t N = (unsigned)width * (unsigned)height;
    string max_path = path_join(name, PATH_MAX_PNG);
    string min_path = path_join(name, PATH_MIN_PNG);
    SkBitmap max_image = ReadPngRgba8888FromFile(assetManager, max_path.c_str());
    if (max_image.isNull()) {
        return set_error_code(error_out, Error::kBadData);
    }
    SkBitmap min_image = ReadPngRgba8888FromFile(assetManager, min_path.c_str());
    if (min_image.isNull()) {
        return set_error_code(error_out, Error::kBadData);
    }
    if (max_image.width()  != min_image.width() ||
        max_image.height() != min_image.height())
    {
        return set_error_code(error_out, Error::kBadData);
    }
    if (max_image.width() != width || max_image.height() != height) {
        return set_error_code(error_out, Error::kBadInput);
    }
    int badness = 0;
    int badPixelCount = 0;
    const uint32_t* max_pixels = (uint32_t*)max_image.getPixels();
    const uint32_t* min_pixels = (uint32_t*)min_image.getPixels();

    for (size_t i = 0; i < N; ++i) {
        int error = get_error(pixels[i], max_pixels[i], min_pixels[i]);
        if (error > 0) {
            badness = SkTMax(error, badness);
            ++badPixelCount;
        }
    }
    if (report_directory_path && badness > 0 && report_directory_path[0] != '\0') {
        sk_mkdir(report_directory_path);
        if (!backend) {
            backend = "skia";
        }
        string report_directory = path_join(report_directory_path, backend);
        sk_mkdir(report_directory.c_str());
        string report_subdirectory = path_join(report_directory, name);
        sk_mkdir(report_subdirectory.c_str());
        string error_path = path_join(report_subdirectory, PATH_IMG_PNG);
        SkAssertResult(WritePixmapToFile(rgba8888_to_pixmap(pixels, width, height),
                                         error_path.c_str()));
        SkBitmap errorBitmap;
        errorBitmap.allocPixels(SkImageInfo::Make(width, height, kColorType, kAlphaType));
        uint32_t* errors = (uint32_t*)errorBitmap.getPixels();
        for (size_t i = 0; i < N; ++i) {
            int error = get_error(pixels[i], max_pixels[i], min_pixels[i]);
            errors[i] = error > 0 ? 0xFF000000 + (unsigned)error : 0x00000000;
        }
        error_path = path_join(report_subdirectory, PATH_ERR_PNG);
        SkAssertResult(WritePixmapToFile(to_pixmap(errorBitmap), error_path.c_str()));

        auto report_path = path_join(report_subdirectory, PATH_REPORT);
        auto rdir = path_join("..", "..", backend, name);

        auto max_path_out = path_join(report_subdirectory, PATH_MAX_PNG);
        auto min_path_out = path_join(report_subdirectory, PATH_MIN_PNG);
        (void)copy(assetManager, max_path.c_str(), max_path_out.c_str());
        (void)copy(assetManager, min_path.c_str(), min_path_out.c_str());

        SkString text = SkStringPrintf(
            "backend: %s\n<br>\n"
            "gm name: %s\n<br>\n"
            "maximum error: %d\n<br>\n"
            "bad pixel counts: %d\n<br>\n"
            "<a  href=\"%s/" PATH_IMG_PNG "\">"
            "<img src=\"%s/" PATH_IMG_PNG "\" alt='img'></a>\n"
            "<a  href=\"%s/" PATH_ERR_PNG "\">"
            "<img src=\"%s/" PATH_ERR_PNG "\" alt='err'></a>\n<br>\n"
            "<a  href=\"%s/" PATH_MAX_PNG "\">max</a>\n<br>\n"
            "<a  href=\"%s/" PATH_MIN_PNG "\">min</a>\n<hr>\n",
            backend, name, badness, badPixelCount,
            rdir.c_str(), rdir.c_str(), rdir.c_str(), rdir.c_str(), rdir.c_str(), rdir.c_str());
        SkFILEWStream(report_path.c_str()).write(text.c_str(), text.size());
    }
    if (error_out) {
        *error_out = Error::kNone;
    }
    return (float)badness;
}

bool IsGoodGM(const char* name, skqp::AssetManager* assetManager) {
    std::string max_path = path_join(name, PATH_MAX_PNG);
    std::string min_path = path_join(name, PATH_MIN_PNG);
    return asset_exists(assetManager, max_path.c_str())
        && asset_exists(assetManager, min_path.c_str());
}
}  // namespace gmkb