Debian Codegraph @

tools/skp_parser.cpp

raw
1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "include/core/SkPicture.h"
9 #include "include/core/SkSerialProcs.h"
10 #include "include/core/SkStream.h"
11 #include "include/utils/SkNullCanvas.h"
12 #include "src/utils/SkJSONWriter.h"
13 #include "tools/DeserialProcsUtils.h"
14 #include "tools/UrlDataManager.h"
15 #include "tools/debugger/DebugCanvas.h"
16
17 #include <iostream>
18
19 #ifdef SK_BUILD_FOR_WIN
20 #include <fcntl.h>
21 #include <io.h>
22 #endif
23
24 /*
25 If you execute skp_parser with one argument, it spits out a json representation
26 of the skp, but that's incomplete since it's missing many binary blobs (these
27 could represent images or typefaces or just anything that doesn't currently
28 have a json representation). Each unique blob is labeled with a string in the
29 form "data/%d". So for example:
30
31 tools/git-sync-deps
32 bin/gn gen out/debug
33 ninja -C out/debug dm skp_parser
34 out/debug/dm -m grayscale -w /tmp/dm --config skp
35 out/debug/skp_parser /tmp/dm/skp/gm/grayscalejpg.skp | less
36 out/debug/skp_parser /tmp/dm/skp/gm/grayscalejpg.skp | grep data
37 out/debug/skp_parser /tmp/dm/skp/gm/grayscalejpg.skp data/0 | file -
38 out/debug/skp_parser /tmp/dm/skp/gm/grayscalejpg.skp data/0 > /tmp/data0.png
39
40 "data/0" is an image that the SKP serializer has encoded as PNG.
41 */
42 int main(int argc, char** argv) {
43 if (argc < 2) {
44 SkDebugf("Usage:\n %s SKP_FILE [DATA_URL]\n", argv[0]);
45 return 1;
46 }
47 SkFILEStream input(argv[1]);
48 if (!input.isValid()) {
49 SkDebugf("Bad file: '%s'\n", argv[1]);
50 return 2;
51 }
52 SkDeserialProcs procs = ToolUtils::get_default_skp_deserial_procs();
53 sk_sp<SkPicture> pic = SkPicture::MakeFromStream(&input, &procs);
54 if (!pic) {
55 SkDebugf("Bad skp: '%s'\n", argv[1]);
56 return 3;
57 }
58 SkISize size = pic->cullRect().roundOut().size();
59 DebugCanvas debugCanvas(size.width(), size.height());
60 pic->playback(&debugCanvas);
61 std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
62 UrlDataManager dataManager(SkString("data"));
63 SkDynamicMemoryWStream stream;
64 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
65 writer.beginObject(); // root
66 debugCanvas.toJSON(writer, dataManager, nullCanvas.get());
67 writer.endObject(); // root
68 writer.flush();
69 if (argc > 2) {
70 if (UrlDataManager::UrlData* data =
71 dataManager.getDataFromUrl(SkString(argv[2]))) {
72 SkData* skdata = data->fData.get();
73 SkASSERT(skdata);
74 #ifdef SK_BUILD_FOR_WIN
75 fflush(stdout);
76 (void)_setmode(_fileno(stdout), _O_BINARY);
77 #endif
78 fwrite(skdata->data(), skdata->size(), 1, stdout);
79 } else {
80 SkDebugf("Bad data url.\n");
81 return 4;
82 }
83 } else {
84 sk_sp<SkData> data = stream.detachAsData();
85 fwrite(data->data(), data->size(), 1, stdout);
86 }
87 return 0;
88 }