blob: 79dbaf34d026461dc77f19fdfd93f30c8b556973 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkPicture.h"
9#include "include/core/SkStream.h"
10#include "include/private/SkTo.h"
11#include "src/core/SkFontDescriptor.h"
12#include "src/core/SkPictureCommon.h"
13#include "src/core/SkPictureData.h"
14#include "tools/flags/CommandLineFlags.h"
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000015
Mike Klein84836b72019-03-21 11:31:36 -050016static DEFINE_string2(input, i, "", "skp on which to report");
17static DEFINE_bool2(version, v, true, "version");
18static DEFINE_bool2(cullRect, c, true, "cullRect");
19static DEFINE_bool2(flags, f, true, "flags");
20static DEFINE_bool2(tags, t, true, "tags");
21static DEFINE_bool2(quiet, q, false, "quiet");
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000022
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) {
Mike Klein88544fb2019-03-20 10:50:33 -050035 CommandLineFlags::SetUsage("Prints information about an skp file");
36 CommandLineFlags::Parse(argc, argv);
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000037
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)) {
Mike Reed08d206b2021-07-19 16:46:11 -040057 SkDebugf("Unsupported version %d\n", info.getVersion());
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000058 return kNotAnSKP;
59 }
60
61 if (FLAGS_version && !FLAGS_quiet) {
Mike Reedb3f543d2016-10-04 15:12:01 -040062 SkDebugf("Version: %d\n", info.getVersion());
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000063 }
robertphillipsa8d7f0b2014-08-29 08:03:56 -070064 if (FLAGS_cullRect && !FLAGS_quiet) {
mtklein88fd0fb2014-12-01 06:56:38 -080065 SkDebugf("Cull Rect: %f,%f,%f,%f\n",
66 info.fCullRect.fLeft, info.fCullRect.fTop,
robertphillipsa8d7f0b2014-08-29 08:03:56 -070067 info.fCullRect.fRight, info.fCullRect.fBottom);
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000068 }
69
Ben Wagner255ab8d2016-10-07 15:50:53 -040070 bool hasData;
71 if (!stream.readBool(&hasData)) { return kTruncatedFile; }
72 if (!hasData) {
skia.committer@gmail.comade9a342014-03-04 03:02:32 +000073 // If we read true there's a picture playback object flattened
74 // in the file; if false, there isn't a playback, so we're done
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000075 // reading the file.
76 return kSuccess;
77 }
78
79 for (;;) {
Ben Wagner255ab8d2016-10-07 15:50:53 -040080 uint32_t tag;
81 if (!stream.readU32(&tag)) { return kTruncatedFile; }
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000082 if (SK_PICT_EOF_TAG == tag) {
83 break;
84 }
85
Ben Wagner255ab8d2016-10-07 15:50:53 -040086 uint32_t chunkSize;
87 if (!stream.readU32(&chunkSize)) { return kTruncatedFile; }
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +000088 size_t curPos = stream.getPosition();
89
90 // "move" doesn't error out when seeking beyond the end of file
91 // so we need a preemptive check here.
92 if (curPos+chunkSize > totStreamSize) {
93 if (!FLAGS_quiet) {
94 SkDebugf("truncated file\n");
95 }
96 return kTruncatedFile;
97 }
98
99 // Not all the tags store the chunk size (in bytes). Three
100 // of them store tag-specific size information (e.g., number of
101 // fonts) instead. This forces us to early exit when those
102 // chunks are encountered.
103 switch (tag) {
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000104 case SK_PICT_READER_TAG:
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000105 if (FLAGS_tags && !FLAGS_quiet) {
106 SkDebugf("SK_PICT_READER_TAG %d\n", chunkSize);
107 }
108 break;
109 case SK_PICT_FACTORY_TAG:
110 if (FLAGS_tags && !FLAGS_quiet) {
111 SkDebugf("SK_PICT_FACTORY_TAG %d\n", chunkSize);
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000112 }
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000113 break;
robertphillips3552ba12016-02-25 10:58:49 -0800114 case SK_PICT_TYPEFACE_TAG: {
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000115 if (FLAGS_tags && !FLAGS_quiet) {
116 SkDebugf("SK_PICT_TYPEFACE_TAG %d\n", chunkSize);
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000117 }
robertphillips3552ba12016-02-25 10:58:49 -0800118
119 const int count = SkToInt(chunkSize);
120 for (int i = 0; i < count; i++) {
121 SkFontDescriptor desc;
122 if (!SkFontDescriptor::Deserialize(&stream, &desc)) {
123 if (!FLAGS_quiet) {
124 SkDebugf("File corruption in SkFontDescriptor\n");
125 }
126 return kInvalidTag;
127 }
128 }
129
130 // clear this since we've consumed all the typefaces
131 chunkSize = 0;
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000132 break;
robertphillips3552ba12016-02-25 10:58:49 -0800133 }
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000134 case SK_PICT_PICTURE_TAG:
135 if (FLAGS_tags && !FLAGS_quiet) {
136 SkDebugf("SK_PICT_PICTURE_TAG %d\n", chunkSize);
137 SkDebugf("Exiting early due to format limitations\n");
138 }
139 return kSuccess; // TODO: need to store size in bytes
140 break;
141 case SK_PICT_BUFFER_SIZE_TAG:
142 if (FLAGS_tags && !FLAGS_quiet) {
143 SkDebugf("SK_PICT_BUFFER_SIZE_TAG %d\n", chunkSize);
144 }
145 break;
146 default:
147 if (!FLAGS_quiet) {
148 SkDebugf("Unknown tag %d\n", chunkSize);
149 }
150 return kInvalidTag;
151 }
152
153 if (!stream.move(chunkSize)) {
154 if (!FLAGS_quiet) {
155 SkDebugf("seek error\n");
156 }
157 return kTruncatedFile;
158 }
159 }
160
161 return kSuccess;
162}