blob: 55229650f365297b39a73d3d9de12d56d9323732 [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) {
62 SkDebugf("Cull Rect: %f,%f,%f,%f\n",
63 info.fCullRect.fLeft, info.fCullRect.fTop,
64 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.orgdcb8e542014-03-05 18:25:20 +0000109 // Remove this code when v21 and below are no longer supported
110#ifndef DISABLE_V21_COMPATIBILITY_CODE
111 if (info.fVersion < 22) {
112 if (!FLAGS_quiet) {
113 SkDebugf("Exiting early due to format limitations\n");
114 }
115 return kSuccess; // TODO: need to store size in bytes
116 }
117#endif
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000118 break;
119 case SK_PICT_TYPEFACE_TAG:
120 if (FLAGS_tags && !FLAGS_quiet) {
121 SkDebugf("SK_PICT_TYPEFACE_TAG %d\n", chunkSize);
122 SkDebugf("Exiting early due to format limitations\n");
123 }
124 return kSuccess; // TODO: need to store size in bytes
125 break;
126 case SK_PICT_PICTURE_TAG:
127 if (FLAGS_tags && !FLAGS_quiet) {
128 SkDebugf("SK_PICT_PICTURE_TAG %d\n", chunkSize);
129 SkDebugf("Exiting early due to format limitations\n");
130 }
131 return kSuccess; // TODO: need to store size in bytes
132 break;
133 case SK_PICT_BUFFER_SIZE_TAG:
134 if (FLAGS_tags && !FLAGS_quiet) {
135 SkDebugf("SK_PICT_BUFFER_SIZE_TAG %d\n", chunkSize);
136 }
137 break;
138 default:
139 if (!FLAGS_quiet) {
140 SkDebugf("Unknown tag %d\n", chunkSize);
141 }
142 return kInvalidTag;
143 }
144
145 if (!stream.move(chunkSize)) {
146 if (!FLAGS_quiet) {
147 SkDebugf("seek error\n");
148 }
149 return kTruncatedFile;
150 }
151 }
152
153 return kSuccess;
154}
155
156#if !defined SK_BUILD_FOR_IOS
157int main(int argc, char * const argv[]) {
158 return tool_main(argc, (char**) argv);
159}
160#endif