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