| commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 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" | 
| Hal Canary | 2a2f675 | 2018-06-11 21:44:01 -0400 | [diff] [blame] | 9 | #include "SkPicture.h" | 
| Hal Canary | 2a2f675 | 2018-06-11 21:44:01 -0400 | [diff] [blame] | 10 | #include "SkPictureData.h" | 
| Hal Canary | fdcfb8b | 2018-06-13 09:42:32 -0400 | [diff] [blame] | 11 | #include "SkPictureCommon.h" | 
| Hal Canary | 2a2f675 | 2018-06-11 21:44:01 -0400 | [diff] [blame] | 12 | #include "SkStream.h" | 
| Hal Canary | fdcfb8b | 2018-06-13 09:42:32 -0400 | [diff] [blame] | 13 | #include "SkFontDescriptor.h" | 
| commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 14 |  | 
|  | 15 | DEFINE_string2(input, i, "", "skp on which to report"); | 
|  | 16 | DEFINE_bool2(version, v, true, "version"); | 
| robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 17 | DEFINE_bool2(cullRect, c, true, "cullRect"); | 
| commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 18 | DEFINE_bool2(flags, f, true, "flags"); | 
| commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 19 | DEFINE_bool2(tags, t, true, "tags"); | 
|  | 20 | DEFINE_bool2(quiet, q, false, "quiet"); | 
|  | 21 |  | 
|  | 22 | // This tool can print simple information about an SKP but its main use | 
|  | 23 | // is just to check if an SKP has been truncated during the recording | 
|  | 24 | // process. | 
|  | 25 | // return codes: | 
|  | 26 | static const int kSuccess = 0; | 
|  | 27 | static const int kTruncatedFile = 1; | 
|  | 28 | static const int kNotAnSKP = 2; | 
|  | 29 | static const int kInvalidTag = 3; | 
|  | 30 | static const int kMissingInput = 4; | 
|  | 31 | static const int kIOError = 5; | 
|  | 32 |  | 
| Mike Klein | be28ee2 | 2017-02-06 12:46:20 -0500 | [diff] [blame] | 33 | int main(int argc, char** argv) { | 
| commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 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; | 
| Mike Reed | e7a5832 | 2017-12-20 14:09:20 -0500 | [diff] [blame] | 55 | if (!SkPicture_StreamIsSKP(&stream, &info)) { | 
| commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 56 | return kNotAnSKP; | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | if (FLAGS_version && !FLAGS_quiet) { | 
| Mike Reed | b3f543d | 2016-10-04 15:12:01 -0400 | [diff] [blame] | 60 | SkDebugf("Version: %d\n", info.getVersion()); | 
| commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 61 | } | 
| robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 62 | if (FLAGS_cullRect && !FLAGS_quiet) { | 
| mtklein | 88fd0fb | 2014-12-01 06:56:38 -0800 | [diff] [blame] | 63 | SkDebugf("Cull Rect: %f,%f,%f,%f\n", | 
|  | 64 | info.fCullRect.fLeft, info.fCullRect.fTop, | 
| robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 65 | info.fCullRect.fRight, info.fCullRect.fBottom); | 
| commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 66 | } | 
|  | 67 |  | 
| Ben Wagner | 255ab8d | 2016-10-07 15:50:53 -0400 | [diff] [blame] | 68 | bool hasData; | 
|  | 69 | if (!stream.readBool(&hasData)) { return kTruncatedFile; } | 
|  | 70 | if (!hasData) { | 
| skia.committer@gmail.com | ade9a34 | 2014-03-04 03:02:32 +0000 | [diff] [blame] | 71 | // 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.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 73 | // reading the file. | 
|  | 74 | return kSuccess; | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | for (;;) { | 
| Ben Wagner | 255ab8d | 2016-10-07 15:50:53 -0400 | [diff] [blame] | 78 | uint32_t tag; | 
|  | 79 | if (!stream.readU32(&tag)) { return kTruncatedFile; } | 
| commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 80 | if (SK_PICT_EOF_TAG == tag) { | 
|  | 81 | break; | 
|  | 82 | } | 
|  | 83 |  | 
| Ben Wagner | 255ab8d | 2016-10-07 15:50:53 -0400 | [diff] [blame] | 84 | uint32_t chunkSize; | 
|  | 85 | if (!stream.readU32(&chunkSize)) { return kTruncatedFile; } | 
| commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 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) { | 
| skia.committer@gmail.com | ade9a34 | 2014-03-04 03:02:32 +0000 | [diff] [blame] | 102 | case SK_PICT_READER_TAG: | 
| commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 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); | 
| commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 110 | } | 
| commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 111 | break; | 
| robertphillips | 3552ba1 | 2016-02-25 10:58:49 -0800 | [diff] [blame] | 112 | case SK_PICT_TYPEFACE_TAG: { | 
| commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 113 | if (FLAGS_tags && !FLAGS_quiet) { | 
|  | 114 | SkDebugf("SK_PICT_TYPEFACE_TAG %d\n", chunkSize); | 
| commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 115 | } | 
| robertphillips | 3552ba1 | 2016-02-25 10:58:49 -0800 | [diff] [blame] | 116 |  | 
|  | 117 | const int count = SkToInt(chunkSize); | 
|  | 118 | for (int i = 0; i < count; i++) { | 
|  | 119 | SkFontDescriptor desc; | 
|  | 120 | if (!SkFontDescriptor::Deserialize(&stream, &desc)) { | 
|  | 121 | if (!FLAGS_quiet) { | 
|  | 122 | SkDebugf("File corruption in SkFontDescriptor\n"); | 
|  | 123 | } | 
|  | 124 | return kInvalidTag; | 
|  | 125 | } | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | // clear this since we've consumed all the typefaces | 
|  | 129 | chunkSize = 0; | 
| commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 130 | break; | 
| robertphillips | 3552ba1 | 2016-02-25 10:58:49 -0800 | [diff] [blame] | 131 | } | 
| commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 132 | case SK_PICT_PICTURE_TAG: | 
|  | 133 | if (FLAGS_tags && !FLAGS_quiet) { | 
|  | 134 | SkDebugf("SK_PICT_PICTURE_TAG %d\n", chunkSize); | 
|  | 135 | SkDebugf("Exiting early due to format limitations\n"); | 
|  | 136 | } | 
|  | 137 | return kSuccess;       // TODO: need to store size in bytes | 
|  | 138 | break; | 
|  | 139 | case SK_PICT_BUFFER_SIZE_TAG: | 
|  | 140 | if (FLAGS_tags && !FLAGS_quiet) { | 
|  | 141 | SkDebugf("SK_PICT_BUFFER_SIZE_TAG %d\n", chunkSize); | 
|  | 142 | } | 
|  | 143 | break; | 
|  | 144 | default: | 
|  | 145 | if (!FLAGS_quiet) { | 
|  | 146 | SkDebugf("Unknown tag %d\n", chunkSize); | 
|  | 147 | } | 
|  | 148 | return kInvalidTag; | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | if (!stream.move(chunkSize)) { | 
|  | 152 | if (!FLAGS_quiet) { | 
|  | 153 | SkDebugf("seek error\n"); | 
|  | 154 | } | 
|  | 155 | return kTruncatedFile; | 
|  | 156 | } | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | return kSuccess; | 
|  | 160 | } |