junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | |
commit-bot@chromium.org | 56799e2 | 2013-07-16 18:21:46 +0000 | [diff] [blame] | 8 | #include "LazyDecodeBitmap.h" |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 9 | #include "CopyTilesRenderer.h" |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 10 | #include "SkBitmap.h" |
keyar@chromium.org | 472b379 | 2012-07-20 22:34:27 +0000 | [diff] [blame] | 11 | #include "SkDevice.h" |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 12 | #include "SkCommandLineFlags.h" |
borenet@google.com | 10ef79e | 2012-09-10 17:19:06 +0000 | [diff] [blame] | 13 | #include "SkGraphics.h" |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 14 | #include "SkImageDecoder.h" |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 15 | #include "SkImageEncoder.h" |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 16 | #include "SkMath.h" |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 17 | #include "SkOSFile.h" |
| 18 | #include "SkPicture.h" |
| 19 | #include "SkStream.h" |
| 20 | #include "SkString.h" |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 21 | #include "PictureRenderer.h" |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 22 | #include "PictureRenderingFlags.h" |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 23 | #include "picture_utils.h" |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 24 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 25 | // Flags used by this file, alphabetically: |
| 26 | DEFINE_int32(clone, 0, "Clone the picture n times before rendering."); |
| 27 | DECLARE_bool(deferImageDecoding); |
| 28 | DEFINE_int32(maxComponentDiff, 256, "Maximum diff on a component, 0 - 256. Components that differ " |
| 29 | "by more than this amount are considered errors, though all diffs are reported. " |
| 30 | "Requires --validate."); |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 31 | DECLARE_string(readPath); |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 32 | DEFINE_bool(writeEncodedImages, false, "Any time the skp contains an encoded image, write it to a " |
| 33 | "file rather than decoding it. Requires writePath to be set. Skips drawing the full " |
| 34 | "skp to a file. Not compatible with deferImageDecoding."); |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 35 | DEFINE_string2(writePath, w, "", "Directory to write the rendered images."); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 36 | DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a " |
| 37 | "file, instead of an image for each tile."); |
| 38 | DEFINE_bool(validate, false, "Verify that the rendered image contains the same pixels as " |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 39 | "the picture rendered in simple mode. When used in conjunction with --bbh, results " |
| 40 | "are validated against the picture rendered in the same mode, but without the bbh."); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 41 | |
commit-bot@chromium.org | ff36a1d | 2013-07-24 20:37:30 +0000 | [diff] [blame] | 42 | DEFINE_bool(bench_record, false, "If true, drop into an infinite loop of recording the picture."); |
| 43 | |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 44 | static void make_output_filepath(SkString* path, const SkString& dir, |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 45 | const SkString& name) { |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 46 | sk_tools::make_filepath(path, dir, name); |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 47 | // Remove ".skp" |
| 48 | path->remove(path->size() - 4, 4); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 49 | } |
| 50 | |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 51 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 52 | |
| 53 | /** |
| 54 | * Table for translating from format of data to a suffix. |
| 55 | */ |
| 56 | struct Format { |
| 57 | SkImageDecoder::Format fFormat; |
| 58 | const char* fSuffix; |
| 59 | }; |
| 60 | static const Format gFormats[] = { |
| 61 | { SkImageDecoder::kBMP_Format, ".bmp" }, |
| 62 | { SkImageDecoder::kGIF_Format, ".gif" }, |
| 63 | { SkImageDecoder::kICO_Format, ".ico" }, |
| 64 | { SkImageDecoder::kJPEG_Format, ".jpg" }, |
| 65 | { SkImageDecoder::kPNG_Format, ".png" }, |
| 66 | { SkImageDecoder::kWBMP_Format, ".wbmp" }, |
| 67 | { SkImageDecoder::kWEBP_Format, ".webp" }, |
| 68 | { SkImageDecoder::kUnknown_Format, "" }, |
| 69 | }; |
| 70 | |
| 71 | /** |
| 72 | * Get an appropriate suffix for an image format. |
| 73 | */ |
| 74 | static const char* get_suffix_from_format(SkImageDecoder::Format format) { |
| 75 | for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) { |
| 76 | if (gFormats[i].fFormat == format) { |
| 77 | return gFormats[i].fSuffix; |
| 78 | } |
| 79 | } |
| 80 | return ""; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Base name for an image file created from the encoded data in an skp. |
| 85 | */ |
| 86 | static SkString gInputFileName; |
| 87 | |
| 88 | /** |
| 89 | * Number to be appended to the image file name so that it is unique. |
| 90 | */ |
| 91 | static uint32_t gImageNo; |
| 92 | |
| 93 | /** |
| 94 | * Set up the name for writing encoded data to a file. |
| 95 | * Sets gInputFileName to name, minus any extension ".*" |
| 96 | * Sets gImageNo to 0, so images from file "X.skp" will |
| 97 | * look like "X_<gImageNo>.<suffix>", beginning with 0 |
| 98 | * for each new skp. |
| 99 | */ |
| 100 | static void reset_image_file_base_name(const SkString& name) { |
| 101 | gImageNo = 0; |
| 102 | // Remove ".skp" |
| 103 | const char* cName = name.c_str(); |
| 104 | const char* dot = strrchr(cName, '.'); |
| 105 | if (dot != NULL) { |
| 106 | gInputFileName.set(cName, dot - cName); |
| 107 | } else { |
| 108 | gInputFileName.set(name); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Write the raw encoded bitmap data to a file. |
| 114 | */ |
| 115 | static bool write_image_to_file(const void* buffer, size_t size, SkBitmap* bitmap) { |
| 116 | SkASSERT(!FLAGS_writePath.isEmpty()); |
| 117 | SkMemoryStream memStream(buffer, size); |
| 118 | SkString outPath; |
| 119 | SkImageDecoder::Format format = SkImageDecoder::GetStreamFormat(&memStream); |
| 120 | SkString name = SkStringPrintf("%s_%d%s", gInputFileName.c_str(), gImageNo++, |
| 121 | get_suffix_from_format(format)); |
| 122 | SkString dir(FLAGS_writePath[0]); |
| 123 | sk_tools::make_filepath(&outPath, dir, name); |
| 124 | SkFILEWStream fileStream(outPath.c_str()); |
| 125 | if (!(fileStream.isValid() && fileStream.write(buffer, size))) { |
| 126 | SkDebugf("Failed to write encoded data to \"%s\"\n", outPath.c_str()); |
| 127 | } |
| 128 | // Put in a dummy bitmap. |
| 129 | return SkImageDecoder::DecodeStream(&memStream, bitmap, SkBitmap::kNo_Config, |
| 130 | SkImageDecoder::kDecodeBounds_Mode); |
| 131 | } |
| 132 | |
| 133 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 134 | |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 135 | static bool render_picture(const SkString& inputPath, const SkString* outputDir, |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 136 | sk_tools::PictureRenderer& renderer, |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 137 | SkBitmap** out) { |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 138 | SkString inputFilename; |
| 139 | sk_tools::get_basename(&inputFilename, inputPath); |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 140 | |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 141 | SkFILEStream inputStream; |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 142 | inputStream.setPath(inputPath.c_str()); |
| 143 | if (!inputStream.isValid()) { |
| 144 | SkDebugf("Could not open file %s\n", inputPath.c_str()); |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 145 | return false; |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 146 | } |
| 147 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 148 | SkPicture::InstallPixelRefProc proc; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 149 | if (FLAGS_deferImageDecoding) { |
commit-bot@chromium.org | 56799e2 | 2013-07-16 18:21:46 +0000 | [diff] [blame] | 150 | proc = &sk_tools::LazyDecodeBitmap; |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 151 | } else if (FLAGS_writeEncodedImages) { |
| 152 | SkASSERT(!FLAGS_writePath.isEmpty()); |
| 153 | reset_image_file_base_name(inputFilename); |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 154 | proc = &write_image_to_file; |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 155 | } else { |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 156 | proc = &SkImageDecoder::DecodeMemory; |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 157 | } |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 158 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 159 | SkDebugf("deserializing... %s\n", inputPath.c_str()); |
| 160 | |
| 161 | SkPicture* picture = SkPicture::CreateFromStream(&inputStream, proc); |
| 162 | |
| 163 | if (NULL == picture) { |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 164 | SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str()); |
| 165 | return false; |
| 166 | } |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 167 | |
commit-bot@chromium.org | ff36a1d | 2013-07-24 20:37:30 +0000 | [diff] [blame] | 168 | while (FLAGS_bench_record) { |
| 169 | const int kRecordFlags = 0; |
| 170 | SkPicture other; |
| 171 | picture->draw(other.beginRecording(picture->width(), picture->height(), kRecordFlags)); |
| 172 | other.endRecording(); |
| 173 | } |
| 174 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 175 | for (int i = 0; i < FLAGS_clone; ++i) { |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 176 | SkPicture* clone = picture->clone(); |
| 177 | SkDELETE(picture); |
| 178 | picture = clone; |
| 179 | } |
| 180 | |
| 181 | SkDebugf("drawing... [%i %i] %s\n", picture->width(), picture->height(), |
borenet@google.com | 03fcee8 | 2012-09-10 18:18:38 +0000 | [diff] [blame] | 182 | inputPath.c_str()); |
skia.committer@gmail.com | 1d225f2 | 2012-09-14 02:01:10 +0000 | [diff] [blame] | 183 | |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 184 | renderer.init(picture); |
scroggo@google.com | b4773b4 | 2012-10-01 20:06:09 +0000 | [diff] [blame] | 185 | renderer.setup(); |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 186 | |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 187 | SkString* outputPath = NULL; |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 188 | if (NULL != outputDir && outputDir->size() > 0 && !FLAGS_writeEncodedImages) { |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 189 | outputPath = SkNEW(SkString); |
| 190 | make_output_filepath(outputPath, *outputDir, inputFilename); |
| 191 | } |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 192 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 193 | bool success = renderer.render(outputPath, out); |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 194 | if (outputPath) { |
| 195 | if (!success) { |
| 196 | SkDebugf("Could not write to file %s\n", outputPath->c_str()); |
| 197 | } |
| 198 | SkDELETE(outputPath); |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 199 | } |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 200 | |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 201 | renderer.end(); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 202 | |
| 203 | SkDELETE(picture); |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 204 | return success; |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 205 | } |
| 206 | |
edisonn@google.com | ca1b3ff | 2013-01-16 18:18:48 +0000 | [diff] [blame] | 207 | static inline int getByte(uint32_t value, int index) { |
| 208 | SkASSERT(0 <= index && index < 4); |
| 209 | return (value >> (index * 8)) & 0xFF; |
| 210 | } |
| 211 | |
| 212 | static int MaxByteDiff(uint32_t v1, uint32_t v2) { |
| 213 | return SkMax32(SkMax32(abs(getByte(v1, 0) - getByte(v2, 0)), abs(getByte(v1, 1) - getByte(v2, 1))), |
| 214 | SkMax32(abs(getByte(v1, 2) - getByte(v2, 2)), abs(getByte(v1, 3) - getByte(v2, 3)))); |
| 215 | } |
| 216 | |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 217 | namespace { |
| 218 | class AutoRestoreBbhType { |
| 219 | public: |
| 220 | AutoRestoreBbhType() { |
| 221 | fRenderer = NULL; |
junov@chromium.org | d34fda1 | 2013-03-13 19:03:26 +0000 | [diff] [blame] | 222 | fSavedBbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType; |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void set(sk_tools::PictureRenderer* renderer, |
| 226 | sk_tools::PictureRenderer::BBoxHierarchyType bbhType) { |
| 227 | fRenderer = renderer; |
| 228 | fSavedBbhType = renderer->getBBoxHierarchyType(); |
| 229 | renderer->setBBoxHierarchyType(bbhType); |
| 230 | } |
| 231 | |
| 232 | ~AutoRestoreBbhType() { |
| 233 | if (NULL != fRenderer) { |
| 234 | fRenderer->setBBoxHierarchyType(fSavedBbhType); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | private: |
| 239 | sk_tools::PictureRenderer* fRenderer; |
| 240 | sk_tools::PictureRenderer::BBoxHierarchyType fSavedBbhType; |
| 241 | }; |
| 242 | } |
| 243 | |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 244 | static bool render_picture(const SkString& inputPath, const SkString* outputDir, |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 245 | sk_tools::PictureRenderer& renderer) { |
edisonn@google.com | ca1b3ff | 2013-01-16 18:18:48 +0000 | [diff] [blame] | 246 | int diffs[256] = {0}; |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 247 | SkBitmap* bitmap = NULL; |
| 248 | bool success = render_picture(inputPath, |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 249 | FLAGS_writeWholeImage ? NULL : outputDir, |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 250 | renderer, |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 251 | FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 252 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 253 | if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) { |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 254 | SkDebugf("Failed to draw the picture.\n"); |
| 255 | SkDELETE(bitmap); |
| 256 | return false; |
| 257 | } |
| 258 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 259 | if (FLAGS_validate) { |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 260 | SkBitmap* referenceBitmap = NULL; |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 261 | sk_tools::PictureRenderer* referenceRenderer; |
| 262 | // If the renderer uses a BBoxHierarchy, then the reference renderer |
skia.committer@gmail.com | 03682be | 2013-03-14 07:02:51 +0000 | [diff] [blame] | 263 | // will be the same renderer, without the bbh. |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 264 | AutoRestoreBbhType arbbh; |
| 265 | if (sk_tools::PictureRenderer::kNone_BBoxHierarchyType != |
| 266 | renderer.getBBoxHierarchyType()) { |
| 267 | referenceRenderer = &renderer; |
| 268 | referenceRenderer->ref(); // to match auto unref below |
| 269 | arbbh.set(referenceRenderer, sk_tools::PictureRenderer::kNone_BBoxHierarchyType); |
| 270 | } else { |
| 271 | referenceRenderer = SkNEW(sk_tools::SimplePictureRenderer); |
| 272 | } |
| 273 | SkAutoTUnref<sk_tools::PictureRenderer> aurReferenceRenderer(referenceRenderer); |
| 274 | |
| 275 | success = render_picture(inputPath, NULL, *referenceRenderer, |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 276 | &referenceBitmap); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 277 | |
junov@chromium.org | c19c191 | 2013-03-12 19:56:49 +0000 | [diff] [blame] | 278 | if (!success || NULL == referenceBitmap || NULL == referenceBitmap->getPixels()) { |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 279 | SkDebugf("Failed to draw the reference picture.\n"); |
| 280 | SkDELETE(bitmap); |
| 281 | SkDELETE(referenceBitmap); |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | if (success && (bitmap->width() != referenceBitmap->width())) { |
| 286 | SkDebugf("Expected image width: %i, actual image width %i.\n", |
| 287 | referenceBitmap->width(), bitmap->width()); |
| 288 | SkDELETE(bitmap); |
| 289 | SkDELETE(referenceBitmap); |
| 290 | return false; |
| 291 | } |
| 292 | if (success && (bitmap->height() != referenceBitmap->height())) { |
| 293 | SkDebugf("Expected image height: %i, actual image height %i", |
| 294 | referenceBitmap->height(), bitmap->height()); |
| 295 | SkDELETE(bitmap); |
| 296 | SkDELETE(referenceBitmap); |
| 297 | return false; |
| 298 | } |
skia.committer@gmail.com | a7d8e3e | 2012-12-19 02:01:38 +0000 | [diff] [blame] | 299 | |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 300 | for (int y = 0; success && y < bitmap->height(); y++) { |
| 301 | for (int x = 0; success && x < bitmap->width(); x++) { |
edisonn@google.com | ca1b3ff | 2013-01-16 18:18:48 +0000 | [diff] [blame] | 302 | int diff = MaxByteDiff(*referenceBitmap->getAddr32(x, y), |
| 303 | *bitmap->getAddr32(x, y)); |
| 304 | SkASSERT(diff >= 0 && diff <= 255); |
| 305 | diffs[diff]++; |
skia.committer@gmail.com | 4d28d98 | 2013-01-17 07:06:06 +0000 | [diff] [blame] | 306 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 307 | if (diff > FLAGS_maxComponentDiff) { |
edisonn@google.com | ca1b3ff | 2013-01-16 18:18:48 +0000 | [diff] [blame] | 308 | SkDebugf("Expected pixel at (%i %i) exceedds maximum " |
| 309 | "component diff of %i: 0x%x, actual 0x%x\n", |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 310 | x, y, FLAGS_maxComponentDiff, |
edisonn@google.com | 01754bf | 2013-01-11 16:08:07 +0000 | [diff] [blame] | 311 | *referenceBitmap->getAddr32(x, y), |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 312 | *bitmap->getAddr32(x, y)); |
| 313 | SkDELETE(bitmap); |
| 314 | SkDELETE(referenceBitmap); |
| 315 | return false; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | SkDELETE(referenceBitmap); |
edisonn@google.com | ca1b3ff | 2013-01-16 18:18:48 +0000 | [diff] [blame] | 320 | |
| 321 | for (int i = 1; i <= 255; ++i) { |
| 322 | if(diffs[i] > 0) { |
| 323 | SkDebugf("Number of pixels with max diff of %i is %i\n", i, diffs[i]); |
| 324 | } |
| 325 | } |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 326 | } |
| 327 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 328 | if (FLAGS_writeWholeImage) { |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 329 | sk_tools::force_all_opaque(*bitmap); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 330 | if (NULL != outputDir && FLAGS_writeWholeImage) { |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 331 | SkString inputFilename; |
| 332 | sk_tools::get_basename(&inputFilename, inputPath); |
| 333 | SkString outputPath; |
| 334 | make_output_filepath(&outputPath, *outputDir, inputFilename); |
| 335 | outputPath.append(".png"); |
| 336 | if (!SkImageEncoder::EncodeFile(outputPath.c_str(), *bitmap, |
| 337 | SkImageEncoder::kPNG_Type, 100)) { |
| 338 | SkDebugf("Failed to draw the picture.\n"); |
| 339 | success = false; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | SkDELETE(bitmap); |
| 344 | |
| 345 | return success; |
| 346 | } |
| 347 | |
| 348 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 349 | static int process_input(const char* input, const SkString* outputDir, |
| 350 | sk_tools::PictureRenderer& renderer) { |
| 351 | SkOSFile::Iter iter(input, "skp"); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 352 | SkString inputFilename; |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 353 | int failures = 0; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 354 | SkDebugf("process_input, %s\n", input); |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 355 | if (iter.next(&inputFilename)) { |
| 356 | do { |
| 357 | SkString inputPath; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 358 | SkString inputAsSkString(input); |
| 359 | sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename); |
| 360 | if (!render_picture(inputPath, outputDir, renderer)) { |
borenet@google.com | 57837bf | 2012-09-19 17:28:29 +0000 | [diff] [blame] | 361 | ++failures; |
| 362 | } |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 363 | } while(iter.next(&inputFilename)); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 364 | } else if (SkStrEndsWith(input, ".skp")) { |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 365 | SkString inputPath(input); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 366 | if (!render_picture(inputPath, outputDir, renderer)) { |
borenet@google.com | 57837bf | 2012-09-19 17:28:29 +0000 | [diff] [blame] | 367 | ++failures; |
| 368 | } |
| 369 | } else { |
| 370 | SkString warning; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 371 | warning.printf("Warning: skipping %s\n", input); |
borenet@google.com | 57837bf | 2012-09-19 17:28:29 +0000 | [diff] [blame] | 372 | SkDebugf(warning.c_str()); |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 373 | } |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 374 | return failures; |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 375 | } |
| 376 | |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 377 | int tool_main(int argc, char** argv); |
| 378 | int tool_main(int argc, char** argv) { |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 379 | SkCommandLineFlags::SetUsage("Render .skp files."); |
| 380 | SkCommandLineFlags::Parse(argc, argv); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 381 | |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 382 | if (FLAGS_readPath.isEmpty()) { |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 383 | SkDebugf(".skp files or directories are required.\n"); |
| 384 | exit(-1); |
| 385 | } |
| 386 | |
| 387 | if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) { |
| 388 | SkDebugf("--maxComponentDiff must be between 0 and 256\n"); |
| 389 | exit(-1); |
| 390 | } |
| 391 | |
| 392 | if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) { |
| 393 | SkDebugf("--maxComponentDiff requires --validate\n"); |
| 394 | exit(-1); |
| 395 | } |
| 396 | |
| 397 | if (FLAGS_clone < 0) { |
| 398 | SkDebugf("--clone must be >= 0. Was %i\n", FLAGS_clone); |
| 399 | exit(-1); |
| 400 | } |
| 401 | |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 402 | if (FLAGS_writeEncodedImages) { |
| 403 | if (FLAGS_writePath.isEmpty()) { |
| 404 | SkDebugf("--writeEncodedImages requires --writePath\n"); |
| 405 | exit(-1); |
| 406 | } |
| 407 | if (FLAGS_deferImageDecoding) { |
| 408 | SkDebugf("--writeEncodedImages is not compatible with --deferImageDecoding\n"); |
| 409 | exit(-1); |
| 410 | } |
| 411 | } |
| 412 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 413 | SkString errorString; |
| 414 | SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString, |
| 415 | kRender_PictureTool)); |
| 416 | if (errorString.size() > 0) { |
| 417 | SkDebugf("%s\n", errorString.c_str()); |
| 418 | } |
| 419 | |
| 420 | if (renderer.get() == NULL) { |
| 421 | exit(-1); |
| 422 | } |
| 423 | |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 424 | SkAutoGraphics ag; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 425 | |
| 426 | SkString outputDir; |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 427 | if (FLAGS_writePath.count() == 1) { |
| 428 | outputDir.set(FLAGS_writePath[0]); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 429 | } |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 430 | |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 431 | int failures = 0; |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 432 | for (int i = 0; i < FLAGS_readPath.count(); i ++) { |
| 433 | failures += process_input(FLAGS_readPath[i], &outputDir, *renderer.get()); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 434 | } |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 435 | if (failures != 0) { |
| 436 | SkDebugf("Failed to render %i pictures.\n", failures); |
| 437 | return 1; |
| 438 | } |
robertphillips@google.com | 163c84b | 2012-09-13 15:40:37 +0000 | [diff] [blame] | 439 | #if SK_SUPPORT_GPU |
| 440 | #if GR_CACHE_STATS |
| 441 | if (renderer->isUsingGpuDevice()) { |
| 442 | GrContext* ctx = renderer->getGrContext(); |
robertphillips@google.com | 163c84b | 2012-09-13 15:40:37 +0000 | [diff] [blame] | 443 | ctx->printCacheStats(); |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 444 | #ifdef SK_DEVELOPER |
| 445 | ctx->dumpFontCache(); |
| 446 | #endif |
robertphillips@google.com | 163c84b | 2012-09-13 15:40:37 +0000 | [diff] [blame] | 447 | } |
| 448 | #endif |
| 449 | #endif |
caryclark@google.com | 868e1f6 | 2012-10-02 20:00:03 +0000 | [diff] [blame] | 450 | return 0; |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 451 | } |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 452 | |
| 453 | #if !defined SK_BUILD_FOR_IOS |
| 454 | int main(int argc, char * const argv[]) { |
| 455 | return tool_main(argc, (char**) argv); |
| 456 | } |
| 457 | #endif |