Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / tools / skpinfo.cpp
1 /*
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"
10 #include "SkPictureData.h"
11 #include "SkStream.h"
12
13 DEFINE_string2(input, i, "", "skp on which to report");
14 DEFINE_bool2(version, v, true, "version");
15 DEFINE_bool2(width, w, true, "width");
16 DEFINE_bool2(height, h, true, "height");
17 DEFINE_bool2(flags, f, true, "flags");
18 DEFINE_bool2(tags, t, true, "tags");
19 DEFINE_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:
25 static const int kSuccess = 0;
26 static const int kTruncatedFile = 1;
27 static const int kNotAnSKP = 2;
28 static const int kInvalidTag = 3;
29 static const int kMissingInput = 4;
30 static const int kIOError = 5;
31
32 int tool_main(int argc, char** argv);
33 int tool_main(int argc, char** argv) {
34     SkCommandLineFlags::SetUsage("Prints information about an skp file");
35     SkCommandLineFlags::Parse(argc, argv);
36
37     if (FLAGS_input.count() != 1) {
38         if (!FLAGS_quiet) {
39             SkDebugf("Missing input file\n");
40         }
41         return kMissingInput;
42     }
43
44     SkFILEStream stream(FLAGS_input[0]);
45     if (!stream.isValid()) {
46         if (!FLAGS_quiet) {
47             SkDebugf("Couldn't open file\n");
48         }
49         return kIOError;
50     }
51
52     size_t totStreamSize = stream.getLength();
53
54     SkPictInfo info;
55     if (!SkPicture::InternalOnly_StreamIsSKP(&stream, &info)) {
56         return kNotAnSKP;
57     }
58
59     if (FLAGS_version && !FLAGS_quiet) {
60         SkDebugf("Version: %d\n", info.fVersion);
61     }
62     if (FLAGS_width && !FLAGS_quiet) {
63         SkDebugf("Width: %d\n", info.fWidth);
64     }
65     if (FLAGS_height && !FLAGS_quiet) {
66         SkDebugf("Height: %d\n", info.fHeight);
67     }
68     if (FLAGS_flags && !FLAGS_quiet) {
69         SkDebugf("Flags: 0x%x\n", info.fFlags);
70     }
71
72     if (!stream.readBool()) {
73         // 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
75         // reading the file.
76         return kSuccess;
77     }
78
79     for (;;) {
80         uint32_t tag = stream.readU32();
81         if (SK_PICT_EOF_TAG == tag) {
82             break;
83         }
84
85         uint32_t chunkSize = stream.readU32();
86         size_t curPos = stream.getPosition();
87
88         // "move" doesn't error out when seeking beyond the end of file
89         // so we need a preemptive check here.
90         if (curPos+chunkSize > totStreamSize) {
91             if (!FLAGS_quiet) {
92                 SkDebugf("truncated file\n");
93             }
94             return kTruncatedFile;
95         }
96
97         // Not all the tags store the chunk size (in bytes). Three
98         // of them store tag-specific size information (e.g., number of
99         // fonts) instead. This forces us to early exit when those
100         // chunks are encountered.
101         switch (tag) {
102         case SK_PICT_READER_TAG:
103             if (FLAGS_tags && !FLAGS_quiet) {
104                 SkDebugf("SK_PICT_READER_TAG %d\n", chunkSize);
105             }
106             break;
107         case SK_PICT_FACTORY_TAG:
108             if (FLAGS_tags && !FLAGS_quiet) {
109                 SkDebugf("SK_PICT_FACTORY_TAG %d\n", chunkSize);
110             }
111             // Remove this code when v21 and below are no longer supported
112 #ifndef DISABLE_V21_COMPATIBILITY_CODE
113             if (info.fVersion < 22) {
114                 if (!FLAGS_quiet) {
115                     SkDebugf("Exiting early due to format limitations\n");
116                 }
117                 return kSuccess;       // TODO: need to store size in bytes
118             }
119 #endif
120             break;
121         case SK_PICT_TYPEFACE_TAG:
122             if (FLAGS_tags && !FLAGS_quiet) {
123                 SkDebugf("SK_PICT_TYPEFACE_TAG %d\n", chunkSize);
124                 SkDebugf("Exiting early due to format limitations\n");
125             }
126             return kSuccess;       // TODO: need to store size in bytes
127             break;
128         case SK_PICT_PICTURE_TAG:
129             if (FLAGS_tags && !FLAGS_quiet) {
130                 SkDebugf("SK_PICT_PICTURE_TAG %d\n", chunkSize);
131                 SkDebugf("Exiting early due to format limitations\n");
132             }
133             return kSuccess;       // TODO: need to store size in bytes
134             break;
135         case SK_PICT_BUFFER_SIZE_TAG:
136             if (FLAGS_tags && !FLAGS_quiet) {
137                 SkDebugf("SK_PICT_BUFFER_SIZE_TAG %d\n", chunkSize);
138             }
139             break;
140         default:
141             if (!FLAGS_quiet) {
142                 SkDebugf("Unknown tag %d\n", chunkSize);
143             }
144             return kInvalidTag;
145         }
146
147         if (!stream.move(chunkSize)) {
148             if (!FLAGS_quiet) {
149                 SkDebugf("seek error\n");
150             }
151             return kTruncatedFile;
152         }
153     }
154
155     return kSuccess;
156 }
157
158 #if !defined SK_BUILD_FOR_IOS
159 int main(int argc, char * const argv[]) {
160     return tool_main(argc, (char**) argv);
161 }
162 #endif