blob: dd5fd75f192a3448b9d515f43144c45b168f25eb [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"
robertphillipsdb539902014-07-01 08:47:04 -070010#include "SkPictureData.h"
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000011#include "SkStream.h"
robertphillips3552ba12016-02-25 10:58:49 -080012#include "SkFontDescriptor.h"
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000013
14DEFINE_string2(input, i, "", "skp on which to report");
15DEFINE_bool2(version, v, true, "version");
robertphillipsa8d7f0b2014-08-29 08:03:56 -070016DEFINE_bool2(cullRect, c, true, "cullRect");
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 }
robertphillipsa8d7f0b2014-08-29 08:03:56 -070062 if (FLAGS_cullRect && !FLAGS_quiet) {
mtklein88fd0fb2014-12-01 06:56:38 -080063 SkDebugf("Cull Rect: %f,%f,%f,%f\n",
64 info.fCullRect.fLeft, info.fCullRect.fTop,
robertphillipsa8d7f0b2014-08-29 08:03:56 -070065 info.fCullRect.fRight, info.fCullRect.fBottom);
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000066 }
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +000067 if (FLAGS_flags && !FLAGS_quiet) {
robertphillips44583122016-04-19 09:29:01 -070068 SkDebugf("Flags: ");
69 bool needsSeparator = false;
70 if (info.fFlags & SkPictInfo::kCrossProcess_Flag) {
71 SkDebugf("kCrossProcess");
72 needsSeparator = true;
73 }
74 if (info.fFlags & SkPictInfo::kScalarIsFloat_Flag) {
75 if (needsSeparator) {
76 SkDebugf("|");
77 }
78 SkDebugf("kScalarIsFloat");
79 needsSeparator = true;
80 }
81 if (info.fFlags & SkPictInfo::kPtrIs64Bit_Flag) {
82 if (needsSeparator) {
83 SkDebugf("|");
84 }
85 SkDebugf("kPtrIs64Bit");
86 }
87 SkDebugf("\n");
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +000088 }
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000089
90 if (!stream.readBool()) {
skia.committer@gmail.comade9a342014-03-04 03:02:32 +000091 // If we read true there's a picture playback object flattened
92 // in the file; if false, there isn't a playback, so we're done
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000093 // reading the file.
94 return kSuccess;
95 }
96
97 for (;;) {
98 uint32_t tag = stream.readU32();
99 if (SK_PICT_EOF_TAG == tag) {
100 break;
101 }
102
103 uint32_t chunkSize = stream.readU32();
104 size_t curPos = stream.getPosition();
105
106 // "move" doesn't error out when seeking beyond the end of file
107 // so we need a preemptive check here.
108 if (curPos+chunkSize > totStreamSize) {
109 if (!FLAGS_quiet) {
110 SkDebugf("truncated file\n");
111 }
112 return kTruncatedFile;
113 }
114
115 // Not all the tags store the chunk size (in bytes). Three
116 // of them store tag-specific size information (e.g., number of
117 // fonts) instead. This forces us to early exit when those
118 // chunks are encountered.
119 switch (tag) {
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000120 case SK_PICT_READER_TAG:
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000121 if (FLAGS_tags && !FLAGS_quiet) {
122 SkDebugf("SK_PICT_READER_TAG %d\n", chunkSize);
123 }
124 break;
125 case SK_PICT_FACTORY_TAG:
126 if (FLAGS_tags && !FLAGS_quiet) {
127 SkDebugf("SK_PICT_FACTORY_TAG %d\n", chunkSize);
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000128 }
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000129 break;
robertphillips3552ba12016-02-25 10:58:49 -0800130 case SK_PICT_TYPEFACE_TAG: {
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000131 if (FLAGS_tags && !FLAGS_quiet) {
132 SkDebugf("SK_PICT_TYPEFACE_TAG %d\n", chunkSize);
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000133 }
robertphillips3552ba12016-02-25 10:58:49 -0800134
135 const int count = SkToInt(chunkSize);
136 for (int i = 0; i < count; i++) {
137 SkFontDescriptor desc;
138 if (!SkFontDescriptor::Deserialize(&stream, &desc)) {
139 if (!FLAGS_quiet) {
140 SkDebugf("File corruption in SkFontDescriptor\n");
141 }
142 return kInvalidTag;
143 }
144 }
145
146 // clear this since we've consumed all the typefaces
147 chunkSize = 0;
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000148 break;
robertphillips3552ba12016-02-25 10:58:49 -0800149 }
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000150 case SK_PICT_PICTURE_TAG:
151 if (FLAGS_tags && !FLAGS_quiet) {
152 SkDebugf("SK_PICT_PICTURE_TAG %d\n", chunkSize);
153 SkDebugf("Exiting early due to format limitations\n");
154 }
155 return kSuccess; // TODO: need to store size in bytes
156 break;
157 case SK_PICT_BUFFER_SIZE_TAG:
158 if (FLAGS_tags && !FLAGS_quiet) {
159 SkDebugf("SK_PICT_BUFFER_SIZE_TAG %d\n", chunkSize);
160 }
161 break;
162 default:
163 if (!FLAGS_quiet) {
164 SkDebugf("Unknown tag %d\n", chunkSize);
165 }
166 return kInvalidTag;
167 }
168
169 if (!stream.move(chunkSize)) {
170 if (!FLAGS_quiet) {
171 SkDebugf("seek error\n");
172 }
173 return kTruncatedFile;
174 }
175 }
176
177 return kSuccess;
178}
179
180#if !defined SK_BUILD_FOR_IOS
181int main(int argc, char * const argv[]) {
182 return tool_main(argc, (char**) argv);
183}
184#endif