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