blob: c7fd8c870885a6a56ac59a7c4f288b51edaaf4ee [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"
12
13DEFINE_string2(input, i, "", "skp on which to report");
14DEFINE_bool2(version, v, true, "version");
robertphillipsa8d7f0b2014-08-29 08:03:56 -070015DEFINE_bool2(cullRect, c, true, "cullRect");
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +000016DEFINE_bool2(flags, f, true, "flags");
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000017DEFINE_bool2(tags, t, true, "tags");
18DEFINE_bool2(quiet, q, false, "quiet");
19
20// This tool can print simple information about an SKP but its main use
21// is just to check if an SKP has been truncated during the recording
22// process.
23// return codes:
24static const int kSuccess = 0;
25static const int kTruncatedFile = 1;
26static const int kNotAnSKP = 2;
27static const int kInvalidTag = 3;
28static const int kMissingInput = 4;
29static const int kIOError = 5;
30
31int tool_main(int argc, char** argv);
32int tool_main(int argc, char** argv) {
33 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) {
59 SkDebugf("Version: %d\n", info.fVersion);
60 }
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) {
67 SkDebugf("Flags: 0x%x\n", info.fFlags);
68 }
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000069
70 if (!stream.readBool()) {
skia.committer@gmail.comade9a342014-03-04 03:02:32 +000071 // If we read true there's a picture playback object flattened
72 // in the file; if false, there isn't a playback, so we're done
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000073 // reading the file.
74 return kSuccess;
75 }
76
77 for (;;) {
78 uint32_t tag = stream.readU32();
79 if (SK_PICT_EOF_TAG == tag) {
80 break;
81 }
82
83 uint32_t chunkSize = stream.readU32();
84 size_t curPos = stream.getPosition();
85
86 // "move" doesn't error out when seeking beyond the end of file
87 // so we need a preemptive check here.
88 if (curPos+chunkSize > totStreamSize) {
89 if (!FLAGS_quiet) {
90 SkDebugf("truncated file\n");
91 }
92 return kTruncatedFile;
93 }
94
95 // Not all the tags store the chunk size (in bytes). Three
96 // of them store tag-specific size information (e.g., number of
97 // fonts) instead. This forces us to early exit when those
98 // chunks are encountered.
99 switch (tag) {
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000100 case SK_PICT_READER_TAG:
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000101 if (FLAGS_tags && !FLAGS_quiet) {
102 SkDebugf("SK_PICT_READER_TAG %d\n", chunkSize);
103 }
104 break;
105 case SK_PICT_FACTORY_TAG:
106 if (FLAGS_tags && !FLAGS_quiet) {
107 SkDebugf("SK_PICT_FACTORY_TAG %d\n", chunkSize);
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000108 }
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000109 break;
110 case SK_PICT_TYPEFACE_TAG:
111 if (FLAGS_tags && !FLAGS_quiet) {
112 SkDebugf("SK_PICT_TYPEFACE_TAG %d\n", chunkSize);
113 SkDebugf("Exiting early due to format limitations\n");
114 }
115 return kSuccess; // TODO: need to store size in bytes
116 break;
117 case SK_PICT_PICTURE_TAG:
118 if (FLAGS_tags && !FLAGS_quiet) {
119 SkDebugf("SK_PICT_PICTURE_TAG %d\n", chunkSize);
120 SkDebugf("Exiting early due to format limitations\n");
121 }
122 return kSuccess; // TODO: need to store size in bytes
123 break;
124 case SK_PICT_BUFFER_SIZE_TAG:
125 if (FLAGS_tags && !FLAGS_quiet) {
126 SkDebugf("SK_PICT_BUFFER_SIZE_TAG %d\n", chunkSize);
127 }
128 break;
129 default:
130 if (!FLAGS_quiet) {
131 SkDebugf("Unknown tag %d\n", chunkSize);
132 }
133 return kInvalidTag;
134 }
135
136 if (!stream.move(chunkSize)) {
137 if (!FLAGS_quiet) {
138 SkDebugf("seek error\n");
139 }
140 return kTruncatedFile;
141 }
142 }
143
144 return kSuccess;
145}
146
147#if !defined SK_BUILD_FOR_IOS
148int main(int argc, char * const argv[]) {
149 return tool_main(argc, (char**) argv);
150}
151#endif