blob: 6b5eded2b38d0228fa4de61fddaba0992ed2fc2e [file] [log] [blame]
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +00001/*
2 * Copyright 2014 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 "SkCommandLineFlags.h"
9#include "SkPicture.h"
10#include "SkPicturePlayback.h"
11#include "SkStream.h"
12
13DEFINE_string2(input, i, "", "skp on which to report");
14DEFINE_bool2(version, v, true, "version");
15DEFINE_bool2(width, w, true, "width");
16DEFINE_bool2(height, h, true, "height");
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +000017DEFINE_bool2(flags, f, true, "flags");
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000018DEFINE_bool2(tags, t, true, "tags");
19DEFINE_bool2(quiet, q, false, "quiet");
20
21// This tool can print simple information about an SKP but its main use
22// is just to check if an SKP has been truncated during the recording
23// process.
24// return codes:
25static const int kSuccess = 0;
26static const int kTruncatedFile = 1;
27static const int kNotAnSKP = 2;
28static const int kInvalidTag = 3;
29static const int kMissingInput = 4;
30static const int kIOError = 5;
31
32int tool_main(int argc, char** argv);
33int tool_main(int argc, char** argv) {
34 SkCommandLineFlags::SetUsage("Prints information about an skp file");
35 SkCommandLineFlags::Parse(argc, argv);
36
37 if (FLAGS_input.count() != 1) {
38 if (!FLAGS_quiet) {
39 SkDebugf("Missing input file\n");
40 }
41 return kMissingInput;
42 }
43
44 SkFILEStream stream(FLAGS_input[0]);
45 if (!stream.isValid()) {
46 if (!FLAGS_quiet) {
47 SkDebugf("Couldn't open file\n");
48 }
49 return kIOError;
50 }
51
52 size_t totStreamSize = stream.getLength();
53
54 SkPictInfo info;
55 if (!SkPicture::InternalOnly_StreamIsSKP(&stream, &info)) {
56 return kNotAnSKP;
57 }
58
59 if (FLAGS_version && !FLAGS_quiet) {
60 SkDebugf("Version: %d\n", info.fVersion);
61 }
62 if (FLAGS_width && !FLAGS_quiet) {
63 SkDebugf("Width: %d\n", info.fWidth);
64 }
65 if (FLAGS_height && !FLAGS_quiet) {
66 SkDebugf("Height: %d\n", info.fHeight);
67 }
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +000068 if (FLAGS_flags && !FLAGS_quiet) {
69 SkDebugf("Flags: 0x%x\n", info.fFlags);
70 }
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000071
72 if (!stream.readBool()) {
skia.committer@gmail.comade9a342014-03-04 03:02:32 +000073 // If we read true there's a picture playback object flattened
74 // in the file; if false, there isn't a playback, so we're done
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000075 // reading the file.
76 return kSuccess;
77 }
78
79 for (;;) {
80 uint32_t tag = stream.readU32();
81 if (SK_PICT_EOF_TAG == tag) {
82 break;
83 }
84
85 uint32_t chunkSize = stream.readU32();
86 size_t curPos = stream.getPosition();
87
88 // "move" doesn't error out when seeking beyond the end of file
89 // so we need a preemptive check here.
90 if (curPos+chunkSize > totStreamSize) {
91 if (!FLAGS_quiet) {
92 SkDebugf("truncated file\n");
93 }
94 return kTruncatedFile;
95 }
96
97 // Not all the tags store the chunk size (in bytes). Three
98 // of them store tag-specific size information (e.g., number of
99 // fonts) instead. This forces us to early exit when those
100 // chunks are encountered.
101 switch (tag) {
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000102 case SK_PICT_READER_TAG:
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000103 if (FLAGS_tags && !FLAGS_quiet) {
104 SkDebugf("SK_PICT_READER_TAG %d\n", chunkSize);
105 }
106 break;
107 case SK_PICT_FACTORY_TAG:
108 if (FLAGS_tags && !FLAGS_quiet) {
109 SkDebugf("SK_PICT_FACTORY_TAG %d\n", chunkSize);
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000110 }
commit-bot@chromium.orgdcb8e542014-03-05 18:25:20 +0000111 // Remove this code when v21 and below are no longer supported
112#ifndef DISABLE_V21_COMPATIBILITY_CODE
113 if (info.fVersion < 22) {
114 if (!FLAGS_quiet) {
115 SkDebugf("Exiting early due to format limitations\n");
116 }
117 return kSuccess; // TODO: need to store size in bytes
118 }
119#endif
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000120 break;
121 case SK_PICT_TYPEFACE_TAG:
122 if (FLAGS_tags && !FLAGS_quiet) {
123 SkDebugf("SK_PICT_TYPEFACE_TAG %d\n", chunkSize);
124 SkDebugf("Exiting early due to format limitations\n");
125 }
126 return kSuccess; // TODO: need to store size in bytes
127 break;
128 case SK_PICT_PICTURE_TAG:
129 if (FLAGS_tags && !FLAGS_quiet) {
130 SkDebugf("SK_PICT_PICTURE_TAG %d\n", chunkSize);
131 SkDebugf("Exiting early due to format limitations\n");
132 }
133 return kSuccess; // TODO: need to store size in bytes
134 break;
135 case SK_PICT_BUFFER_SIZE_TAG:
136 if (FLAGS_tags && !FLAGS_quiet) {
137 SkDebugf("SK_PICT_BUFFER_SIZE_TAG %d\n", chunkSize);
138 }
139 break;
140 default:
141 if (!FLAGS_quiet) {
142 SkDebugf("Unknown tag %d\n", chunkSize);
143 }
144 return kInvalidTag;
145 }
146
147 if (!stream.move(chunkSize)) {
148 if (!FLAGS_quiet) {
149 SkDebugf("seek error\n");
150 }
151 return kTruncatedFile;
152 }
153 }
154
155 return kSuccess;
156}
157
158#if !defined SK_BUILD_FOR_IOS
159int main(int argc, char * const argv[]) {
160 return tool_main(argc, (char**) argv);
161}
162#endif