blob: 104322f08fcf436fd4bb94d0be9fc5b872b06360 [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
Mike Kleinbe28ee22017-02-06 12:46:20 -050032int main(int argc, char** argv) {
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000033 SkCommandLineFlags::SetUsage("Prints information about an skp file");
34 SkCommandLineFlags::Parse(argc, argv);
35
36 if (FLAGS_input.count() != 1) {
37 if (!FLAGS_quiet) {
38 SkDebugf("Missing input file\n");
39 }
40 return kMissingInput;
41 }
42
43 SkFILEStream stream(FLAGS_input[0]);
44 if (!stream.isValid()) {
45 if (!FLAGS_quiet) {
46 SkDebugf("Couldn't open file\n");
47 }
48 return kIOError;
49 }
50
51 size_t totStreamSize = stream.getLength();
52
53 SkPictInfo info;
54 if (!SkPicture::InternalOnly_StreamIsSKP(&stream, &info)) {
55 return kNotAnSKP;
56 }
57
58 if (FLAGS_version && !FLAGS_quiet) {
Mike Reedb3f543d2016-10-04 15:12:01 -040059 SkDebugf("Version: %d\n", info.getVersion());
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000060 }
robertphillipsa8d7f0b2014-08-29 08:03:56 -070061 if (FLAGS_cullRect && !FLAGS_quiet) {
mtklein88fd0fb2014-12-01 06:56:38 -080062 SkDebugf("Cull Rect: %f,%f,%f,%f\n",
63 info.fCullRect.fLeft, info.fCullRect.fTop,
robertphillipsa8d7f0b2014-08-29 08:03:56 -070064 info.fCullRect.fRight, info.fCullRect.fBottom);
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000065 }
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +000066 if (FLAGS_flags && !FLAGS_quiet) {
robertphillips44583122016-04-19 09:29:01 -070067 SkDebugf("Flags: ");
68 bool needsSeparator = false;
69 if (info.fFlags & SkPictInfo::kCrossProcess_Flag) {
70 SkDebugf("kCrossProcess");
71 needsSeparator = true;
72 }
73 if (info.fFlags & SkPictInfo::kScalarIsFloat_Flag) {
74 if (needsSeparator) {
75 SkDebugf("|");
76 }
77 SkDebugf("kScalarIsFloat");
78 needsSeparator = true;
79 }
80 if (info.fFlags & SkPictInfo::kPtrIs64Bit_Flag) {
81 if (needsSeparator) {
82 SkDebugf("|");
83 }
84 SkDebugf("kPtrIs64Bit");
85 }
86 SkDebugf("\n");
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +000087 }
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000088
89 if (!stream.readBool()) {
skia.committer@gmail.comade9a342014-03-04 03:02:32 +000090 // If we read true there's a picture playback object flattened
91 // in the file; if false, there isn't a playback, so we're done
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000092 // reading the file.
93 return kSuccess;
94 }
95
96 for (;;) {
97 uint32_t tag = stream.readU32();
98 if (SK_PICT_EOF_TAG == tag) {
99 break;
100 }
101
102 uint32_t chunkSize = stream.readU32();
103 size_t curPos = stream.getPosition();
104
105 // "move" doesn't error out when seeking beyond the end of file
106 // so we need a preemptive check here.
107 if (curPos+chunkSize > totStreamSize) {
108 if (!FLAGS_quiet) {
109 SkDebugf("truncated file\n");
110 }
111 return kTruncatedFile;
112 }
113
114 // Not all the tags store the chunk size (in bytes). Three
115 // of them store tag-specific size information (e.g., number of
116 // fonts) instead. This forces us to early exit when those
117 // chunks are encountered.
118 switch (tag) {
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000119 case SK_PICT_READER_TAG:
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000120 if (FLAGS_tags && !FLAGS_quiet) {
121 SkDebugf("SK_PICT_READER_TAG %d\n", chunkSize);
122 }
123 break;
124 case SK_PICT_FACTORY_TAG:
125 if (FLAGS_tags && !FLAGS_quiet) {
126 SkDebugf("SK_PICT_FACTORY_TAG %d\n", chunkSize);
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000127 }
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000128 break;
robertphillips3552ba12016-02-25 10:58:49 -0800129 case SK_PICT_TYPEFACE_TAG: {
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000130 if (FLAGS_tags && !FLAGS_quiet) {
131 SkDebugf("SK_PICT_TYPEFACE_TAG %d\n", chunkSize);
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000132 }
robertphillips3552ba12016-02-25 10:58:49 -0800133
134 const int count = SkToInt(chunkSize);
135 for (int i = 0; i < count; i++) {
136 SkFontDescriptor desc;
137 if (!SkFontDescriptor::Deserialize(&stream, &desc)) {
138 if (!FLAGS_quiet) {
139 SkDebugf("File corruption in SkFontDescriptor\n");
140 }
141 return kInvalidTag;
142 }
143 }
144
145 // clear this since we've consumed all the typefaces
146 chunkSize = 0;
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000147 break;
robertphillips3552ba12016-02-25 10:58:49 -0800148 }
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000149 case SK_PICT_PICTURE_TAG:
150 if (FLAGS_tags && !FLAGS_quiet) {
151 SkDebugf("SK_PICT_PICTURE_TAG %d\n", chunkSize);
152 SkDebugf("Exiting early due to format limitations\n");
153 }
154 return kSuccess; // TODO: need to store size in bytes
155 break;
156 case SK_PICT_BUFFER_SIZE_TAG:
157 if (FLAGS_tags && !FLAGS_quiet) {
158 SkDebugf("SK_PICT_BUFFER_SIZE_TAG %d\n", chunkSize);
159 }
160 break;
161 default:
162 if (!FLAGS_quiet) {
163 SkDebugf("Unknown tag %d\n", chunkSize);
164 }
165 return kInvalidTag;
166 }
167
168 if (!stream.move(chunkSize)) {
169 if (!FLAGS_quiet) {
170 SkDebugf("seek error\n");
171 }
172 return kTruncatedFile;
173 }
174 }
175
176 return kSuccess;
177}