Skip to content

Commit a25c061

Browse files
committed
[canvaskit] Make gm test wrapper not omit known pngs
We just make a global set of known png digests and expect that the test harness will load this with the appropriate data. Bug: skia:10812 Change-Id: I8e1fc987d36cc57386167410514803f8c3b90a69 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/328379 Reviewed-by: Chris Dalton <csmartdalton@google.com>
1 parent 38688fc commit a25c061

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

modules/canvaskit/gm_bindings.cpp

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* found in the LICENSE file.
66
*/
77

8+
#include <set>
89
#include <string>
910
#include <emscripten.h>
1011
#include <emscripten/bind.h>
@@ -71,6 +72,12 @@ static sk_sp<GrDirectContext> MakeGrContext(EMSCRIPTEN_WEBGL_CONTEXT_HANDLE cont
7172
return dContext;
7273
}
7374

75+
static std::set<std::string> gKnownDigests;
76+
77+
static void LoadKnownDigest(std::string md5) {
78+
gKnownDigests.insert(md5);
79+
}
80+
7481
/**
7582
* Runs the given GM and returns a JS object. If the GM was successful, the object will have the
7683
* following properties:
@@ -140,31 +147,36 @@ static JSObject RunGM(sk_sp<GrDirectContext> ctx, std::string name) {
140147
md5.appendf("%02x", digest.data[i]);
141148
}
142149

143-
// We do not need to include the keys because they are optional - they are not read by Gold.
144-
CommandLineFlags::StringArray empty;
145-
SkDynamicMemoryWStream stream;
146-
// TODO(kjlubick) make emission of PNGs optional and make it so we can check the hash against
147-
// the list of known digests to not emit it. This will hopefully speed tests up.
148-
hashAndEncode->encodePNG(&stream, md5.c_str(), empty, empty);
149-
150-
auto data = stream.detachAsData();
151-
152-
// This is the cleanest way to create a new Uint8Array with a copy of the data that is not
153-
// in the WASM heap. kjlubick tried returning a pointer inside an SkData, but that lead to some
154-
// use after free issues. By making the copy using the JS transliteration, we don't risk the
155-
// SkData object being cleaned up before we make the copy.
156-
Uint8Array pngData = emscripten::val(
157-
// https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html#memory-views
158-
typed_memory_view(data->size(), data->bytes())
159-
).call<Uint8Array>("slice"); // slice with no args makes a copy of the memory view.
160-
161-
result.set("png", pngData);
150+
auto ok = gKnownDigests.find(md5.c_str());
151+
if (ok == gKnownDigests.end()) {
152+
// We only need to decode the image if it is "interesting", that is, we have not written it
153+
// before to disk and uploaded it to gold.
154+
SkDynamicMemoryWStream stream;
155+
// We do not need to include the keys because they are optional - they are not read by Gold.
156+
CommandLineFlags::StringArray empty;
157+
hashAndEncode->encodePNG(&stream, md5.c_str(), empty, empty);
158+
159+
auto data = stream.detachAsData();
160+
161+
// This is the cleanest way to create a new Uint8Array with a copy of the data that is not
162+
// in the WASM heap. kjlubick tried returning a pointer inside an SkData, but that lead to
163+
// some use after free issues. By making the copy using the JS transliteration, we don't
164+
// risk the SkData object being cleaned up before we make the copy.
165+
Uint8Array pngData = emscripten::val(
166+
// https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html#memory-views
167+
typed_memory_view(data->size(), data->bytes())
168+
).call<Uint8Array>("slice"); // slice with no args makes a copy of the memory view.
169+
170+
result.set("png", pngData);
171+
gKnownDigests.emplace(md5.c_str());
172+
}
162173
result.set("hash", md5.c_str());
163174
return result;
164175
}
165176

166177
EMSCRIPTEN_BINDINGS(GMs) {
167178
function("ListGMs", &ListGMs);
179+
function("LoadKnownDigest", &LoadKnownDigest);
168180
function("MakeGrContext", &MakeGrContext);
169181
function("RunGM", &RunGM);
170182

0 commit comments

Comments
 (0)