blob: 53ea8f326dccbd2a898500f33c96c52f39874ff6 [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"
robertphillips3552ba12016-02-25 10:58:49 -08009#include "SkFontDescriptor.h"
Hal Canary2a2f6752018-06-11 21:44:01 -040010#include "SkPicture.h"
11#include "SkPictureCommon.h"
12#include "SkPictureData.h"
13#include "SkStream.h"
14#include "SkTo.h"
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000015
16DEFINE_string2(input, i, "", "skp on which to report");
17DEFINE_bool2(version, v, true, "version");
robertphillipsa8d7f0b2014-08-29 08:03:56 -070018DEFINE_bool2(cullRect, c, true, "cullRect");
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +000019DEFINE_bool2(flags, f, true, "flags");
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000020DEFINE_bool2(tags, t, true, "tags");
21DEFINE_bool2(quiet, q, false, "quiet");
22
23// This tool can print simple information about an SKP but its main use
24// is just to check if an SKP has been truncated during the recording
25// process.
26// return codes:
27static const int kSuccess = 0;
28static const int kTruncatedFile = 1;
29static const int kNotAnSKP = 2;
30static const int kInvalidTag = 3;
31static const int kMissingInput = 4;
32static const int kIOError = 5;
33
Mike Kleinbe28ee22017-02-06 12:46:20 -050034int main(int argc, char** argv) {
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000035 SkCommandLineFlags::SetUsage("Prints information about an skp file");
36 SkCommandLineFlags::Parse(argc, argv);
37
38 if (FLAGS_input.count() != 1) {
39 if (!FLAGS_quiet) {
40 SkDebugf("Missing input file\n");
41 }
42 return kMissingInput;
43 }
44
45 SkFILEStream stream(FLAGS_input[0]);
46 if (!stream.isValid()) {
47 if (!FLAGS_quiet) {
48 SkDebugf("Couldn't open file\n");
49 }
50 return kIOError;
51 }
52
53 size_t totStreamSize = stream.getLength();
54
55 SkPictInfo info;
Mike Reede7a58322017-12-20 14:09:20 -050056 if (!SkPicture_StreamIsSKP(&stream, &info)) {
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000057 return kNotAnSKP;
58 }
59
60 if (FLAGS_version && !FLAGS_quiet) {
Mike Reedb3f543d2016-10-04 15:12:01 -040061 SkDebugf("Version: %d\n", info.getVersion());
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000062 }
robertphillipsa8d7f0b2014-08-29 08:03:56 -070063 if (FLAGS_cullRect && !FLAGS_quiet) {
mtklein88fd0fb2014-12-01 06:56:38 -080064 SkDebugf("Cull Rect: %f,%f,%f,%f\n",
65 info.fCullRect.fLeft, info.fCullRect.fTop,
robertphillipsa8d7f0b2014-08-29 08:03:56 -070066 info.fCullRect.fRight, info.fCullRect.fBottom);
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000067 }
68
Ben Wagner255ab8d2016-10-07 15:50:53 -040069 bool hasData;
70 if (!stream.readBool(&hasData)) { return kTruncatedFile; }
71 if (!hasData) {
skia.committer@gmail.comade9a342014-03-04 03:02:32 +000072 // If we read true there's a picture playback object flattened
73 // in the file; if false, there isn't a playback, so we're done
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000074 // reading the file.
75 return kSuccess;
76 }
77
78 for (;;) {
Ben Wagner255ab8d2016-10-07 15:50:53 -040079 uint32_t tag;
80 if (!stream.readU32(&tag)) { return kTruncatedFile; }
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000081 if (SK_PICT_EOF_TAG == tag) {
82 break;
83 }
84
Ben Wagner255ab8d2016-10-07 15:50:53 -040085 uint32_t chunkSize;
86 if (!stream.readU32(&chunkSize)) { return kTruncatedFile; }
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000087 size_t curPos = stream.getPosition();
88
89 // "move" doesn't error out when seeking beyond the end of file
90 // so we need a preemptive check here.
91 if (curPos+chunkSize > totStreamSize) {
92 if (!FLAGS_quiet) {
93 SkDebugf("truncated file\n");
94 }
95 return kTruncatedFile;
96 }
97
98 // Not all the tags store the chunk size (in bytes). Three
99 // of them store tag-specific size information (e.g., number of
100 // fonts) instead. This forces us to early exit when those
101 // chunks are encountered.
102 switch (tag) {
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000103 case SK_PICT_READER_TAG:
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000104 if (FLAGS_tags && !FLAGS_quiet) {
105 SkDebugf("SK_PICT_READER_TAG %d\n", chunkSize);
106 }
107 break;
108 case SK_PICT_FACTORY_TAG:
109 if (FLAGS_tags && !FLAGS_quiet) {
110 SkDebugf("SK_PICT_FACTORY_TAG %d\n", chunkSize);
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000111 }
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000112 break;
robertphillips3552ba12016-02-25 10:58:49 -0800113 case SK_PICT_TYPEFACE_TAG: {
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000114 if (FLAGS_tags && !FLAGS_quiet) {
115 SkDebugf("SK_PICT_TYPEFACE_TAG %d\n", chunkSize);
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000116 }
robertphillips3552ba12016-02-25 10:58:49 -0800117
118 const int count = SkToInt(chunkSize);
119 for (int i = 0; i < count; i++) {
120 SkFontDescriptor desc;
121 if (!SkFontDescriptor::Deserialize(&stream, &desc)) {
122 if (!FLAGS_quiet) {
123 SkDebugf("File corruption in SkFontDescriptor\n");
124 }
125 return kInvalidTag;
126 }
127 }
128
129 // clear this since we've consumed all the typefaces
130 chunkSize = 0;
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000131 break;
robertphillips3552ba12016-02-25 10:58:49 -0800132 }
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000133 case SK_PICT_PICTURE_TAG:
134 if (FLAGS_tags && !FLAGS_quiet) {
135 SkDebugf("SK_PICT_PICTURE_TAG %d\n", chunkSize);
136 SkDebugf("Exiting early due to format limitations\n");
137 }
138 return kSuccess; // TODO: need to store size in bytes
139 break;
140 case SK_PICT_BUFFER_SIZE_TAG:
141 if (FLAGS_tags && !FLAGS_quiet) {
142 SkDebugf("SK_PICT_BUFFER_SIZE_TAG %d\n", chunkSize);
143 }
144 break;
145 default:
146 if (!FLAGS_quiet) {
147 SkDebugf("Unknown tag %d\n", chunkSize);
148 }
149 return kInvalidTag;
150 }
151
152 if (!stream.move(chunkSize)) {
153 if (!FLAGS_quiet) {
154 SkDebugf("seek error\n");
155 }
156 return kTruncatedFile;
157 }
158 }
159
160 return kSuccess;
161}