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" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 19 | #include "SkPictureRecorder.h" |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 20 | #include "SkStream.h" |
| 21 | #include "SkString.h" |
commit-bot@chromium.org | 90c0fbd | 2014-05-09 03:18:41 +0000 | [diff] [blame] | 22 | |
| 23 | #include "image_expectations.h" |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 24 | #include "PictureRenderer.h" |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 25 | #include "PictureRenderingFlags.h" |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 26 | #include "picture_utils.h" |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 27 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 28 | // Flags used by this file, alphabetically: |
epoger | b492c6f | 2014-08-14 07:32:49 -0700 | [diff] [blame] | 29 | DEFINE_bool(bench_record, false, "If true, drop into an infinite loop of recording the picture."); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 30 | DECLARE_bool(deferImageDecoding); |
epoger | b492c6f | 2014-08-14 07:32:49 -0700 | [diff] [blame] | 31 | DEFINE_string(descriptions, "", "one or more key=value pairs to add to the descriptions section " |
| 32 | "of the JSON summary."); |
rmistry | 2529f2e | 2014-08-22 04:46:30 -0700 | [diff] [blame] | 33 | DEFINE_string(imageBaseGSUrl, "", "The Google Storage image base URL the images are stored in."); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 34 | DEFINE_int32(maxComponentDiff, 256, "Maximum diff on a component, 0 - 256. Components that differ " |
| 35 | "by more than this amount are considered errors, though all diffs are reported. " |
| 36 | "Requires --validate."); |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 37 | DEFINE_string(mismatchPath, "", "Write images for tests that failed due to " |
| 38 | "pixel mismatches into this directory."); |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 39 | #if GR_GPU_STATS |
| 40 | DEFINE_bool(gpuStats, false, "Only meaningful with gpu configurations. " |
| 41 | "Report some GPU call statistics."); |
| 42 | #endif |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 43 | DEFINE_bool(mpd, false, "If true, use MultiPictureDraw for rendering."); |
commit-bot@chromium.org | 205ce48 | 2014-05-12 15:37:20 +0000 | [diff] [blame] | 44 | DEFINE_string(readJsonSummaryPath, "", "JSON file to read image expectations from."); |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 45 | DECLARE_string(readPath); |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 46 | DEFINE_bool(writeChecksumBasedFilenames, false, |
| 47 | "When writing out images, use checksum-based filenames."); |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 48 | DEFINE_bool(writeEncodedImages, false, "Any time the skp contains an encoded image, write it to a " |
| 49 | "file rather than decoding it. Requires writePath to be set. Skips drawing the full " |
| 50 | "skp to a file. Not compatible with deferImageDecoding."); |
commit-bot@chromium.org | 4610a46 | 2014-04-29 19:39:22 +0000 | [diff] [blame] | 51 | DEFINE_string(writeJsonSummaryPath, "", "File to write a JSON summary of image results to."); |
| 52 | DEFINE_string2(writePath, w, "", "Directory to write the rendered images into."); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 53 | DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a " |
| 54 | "file, instead of an image for each tile."); |
| 55 | 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] | 56 | "the picture rendered in simple mode. When used in conjunction with --bbh, results " |
| 57 | "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] | 58 | |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 59 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 60 | |
| 61 | /** |
| 62 | * Table for translating from format of data to a suffix. |
| 63 | */ |
| 64 | struct Format { |
| 65 | SkImageDecoder::Format fFormat; |
| 66 | const char* fSuffix; |
| 67 | }; |
| 68 | static const Format gFormats[] = { |
| 69 | { SkImageDecoder::kBMP_Format, ".bmp" }, |
| 70 | { SkImageDecoder::kGIF_Format, ".gif" }, |
| 71 | { SkImageDecoder::kICO_Format, ".ico" }, |
| 72 | { SkImageDecoder::kJPEG_Format, ".jpg" }, |
| 73 | { SkImageDecoder::kPNG_Format, ".png" }, |
| 74 | { SkImageDecoder::kWBMP_Format, ".wbmp" }, |
| 75 | { SkImageDecoder::kWEBP_Format, ".webp" }, |
| 76 | { SkImageDecoder::kUnknown_Format, "" }, |
| 77 | }; |
| 78 | |
| 79 | /** |
| 80 | * Get an appropriate suffix for an image format. |
| 81 | */ |
| 82 | static const char* get_suffix_from_format(SkImageDecoder::Format format) { |
| 83 | for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) { |
| 84 | if (gFormats[i].fFormat == format) { |
| 85 | return gFormats[i].fSuffix; |
| 86 | } |
| 87 | } |
| 88 | return ""; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Base name for an image file created from the encoded data in an skp. |
| 93 | */ |
| 94 | static SkString gInputFileName; |
| 95 | |
| 96 | /** |
| 97 | * Number to be appended to the image file name so that it is unique. |
| 98 | */ |
| 99 | static uint32_t gImageNo; |
| 100 | |
| 101 | /** |
| 102 | * Set up the name for writing encoded data to a file. |
| 103 | * Sets gInputFileName to name, minus any extension ".*" |
| 104 | * Sets gImageNo to 0, so images from file "X.skp" will |
| 105 | * look like "X_<gImageNo>.<suffix>", beginning with 0 |
| 106 | * for each new skp. |
| 107 | */ |
| 108 | static void reset_image_file_base_name(const SkString& name) { |
| 109 | gImageNo = 0; |
| 110 | // Remove ".skp" |
| 111 | const char* cName = name.c_str(); |
| 112 | const char* dot = strrchr(cName, '.'); |
| 113 | if (dot != NULL) { |
| 114 | gInputFileName.set(cName, dot - cName); |
| 115 | } else { |
| 116 | gInputFileName.set(name); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Write the raw encoded bitmap data to a file. |
| 122 | */ |
| 123 | static bool write_image_to_file(const void* buffer, size_t size, SkBitmap* bitmap) { |
| 124 | SkASSERT(!FLAGS_writePath.isEmpty()); |
| 125 | SkMemoryStream memStream(buffer, size); |
| 126 | SkString outPath; |
| 127 | SkImageDecoder::Format format = SkImageDecoder::GetStreamFormat(&memStream); |
| 128 | SkString name = SkStringPrintf("%s_%d%s", gInputFileName.c_str(), gImageNo++, |
| 129 | get_suffix_from_format(format)); |
| 130 | SkString dir(FLAGS_writePath[0]); |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 131 | outPath = SkOSPath::Join(dir.c_str(), name.c_str()); |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 132 | SkFILEWStream fileStream(outPath.c_str()); |
| 133 | if (!(fileStream.isValid() && fileStream.write(buffer, size))) { |
| 134 | SkDebugf("Failed to write encoded data to \"%s\"\n", outPath.c_str()); |
| 135 | } |
| 136 | // Put in a dummy bitmap. |
reed | bfefc7c | 2014-06-12 17:40:00 -0700 | [diff] [blame] | 137 | return SkImageDecoder::DecodeStream(&memStream, bitmap, kUnknown_SkColorType, |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 138 | SkImageDecoder::kDecodeBounds_Mode); |
| 139 | } |
| 140 | |
| 141 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 142 | |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 143 | /** |
| 144 | * Called only by render_picture(). |
| 145 | */ |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 146 | static bool render_picture_internal(const SkString& inputPath, const SkString* writePath, |
| 147 | const SkString* mismatchPath, |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 148 | sk_tools::PictureRenderer& renderer, |
| 149 | SkBitmap** out) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 150 | SkString inputFilename = SkOSPath::Basename(inputPath.c_str()); |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 151 | SkString writePathString; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 152 | if (writePath && writePath->size() > 0 && !FLAGS_writeEncodedImages) { |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 153 | writePathString.set(*writePath); |
| 154 | } |
| 155 | SkString mismatchPathString; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 156 | if (mismatchPath && mismatchPath->size() > 0) { |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 157 | mismatchPathString.set(*mismatchPath); |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 158 | } |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 159 | |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 160 | SkFILEStream inputStream; |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 161 | inputStream.setPath(inputPath.c_str()); |
| 162 | if (!inputStream.isValid()) { |
| 163 | SkDebugf("Could not open file %s\n", inputPath.c_str()); |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 164 | return false; |
twiz@google.com | a31b8bb | 2012-06-22 18:24:56 +0000 | [diff] [blame] | 165 | } |
| 166 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 167 | SkPicture::InstallPixelRefProc proc; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 168 | if (FLAGS_deferImageDecoding) { |
commit-bot@chromium.org | 56799e2 | 2013-07-16 18:21:46 +0000 | [diff] [blame] | 169 | proc = &sk_tools::LazyDecodeBitmap; |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 170 | } else if (FLAGS_writeEncodedImages) { |
| 171 | SkASSERT(!FLAGS_writePath.isEmpty()); |
| 172 | reset_image_file_base_name(inputFilename); |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 173 | proc = &write_image_to_file; |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 174 | } else { |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 175 | proc = &SkImageDecoder::DecodeMemory; |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 176 | } |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 177 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 178 | SkDebugf("deserializing... %s\n", inputPath.c_str()); |
| 179 | |
robertphillips | e947075 | 2014-08-12 10:12:40 -0700 | [diff] [blame] | 180 | SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream, proc)); |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 181 | |
| 182 | if (NULL == picture) { |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 183 | SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str()); |
| 184 | return false; |
| 185 | } |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 186 | |
commit-bot@chromium.org | ff36a1d | 2013-07-24 20:37:30 +0000 | [diff] [blame] | 187 | while (FLAGS_bench_record) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 188 | SkPictureRecorder recorder; |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 189 | picture->playback(recorder.beginRecording(picture->cullRect().width(), |
| 190 | picture->cullRect().height(), |
| 191 | NULL, 0)); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 192 | SkAutoTUnref<SkPicture> other(recorder.endRecording()); |
commit-bot@chromium.org | ff36a1d | 2013-07-24 20:37:30 +0000 | [diff] [blame] | 193 | } |
| 194 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 195 | SkDebugf("drawing... [%f %f %f %f] %s\n", |
| 196 | picture->cullRect().fLeft, picture->cullRect().fTop, |
| 197 | picture->cullRect().fRight, picture->cullRect().fBottom, |
borenet@google.com | 03fcee8 | 2012-09-10 18:18:38 +0000 | [diff] [blame] | 198 | inputPath.c_str()); |
skia.committer@gmail.com | 1d225f2 | 2012-09-14 02:01:10 +0000 | [diff] [blame] | 199 | |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 200 | renderer.init(picture, &writePathString, &mismatchPathString, &inputFilename, |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 201 | FLAGS_writeChecksumBasedFilenames, FLAGS_mpd); |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 202 | |
scroggo@google.com | b4773b4 | 2012-10-01 20:06:09 +0000 | [diff] [blame] | 203 | renderer.setup(); |
commit-bot@chromium.org | 8991c67 | 2014-05-22 00:36:05 +0000 | [diff] [blame] | 204 | renderer.enableWrites(); |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 205 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 206 | bool success = renderer.render(out); |
| 207 | if (!success) { |
| 208 | SkDebugf("Failed to render %s\n", inputFilename.c_str()); |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 209 | } |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 210 | |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 211 | renderer.end(); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 212 | |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 213 | return success; |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 214 | } |
| 215 | |
edisonn@google.com | ca1b3ff | 2013-01-16 18:18:48 +0000 | [diff] [blame] | 216 | static inline int getByte(uint32_t value, int index) { |
| 217 | SkASSERT(0 <= index && index < 4); |
| 218 | return (value >> (index * 8)) & 0xFF; |
| 219 | } |
| 220 | |
| 221 | static int MaxByteDiff(uint32_t v1, uint32_t v2) { |
| 222 | return SkMax32(SkMax32(abs(getByte(v1, 0) - getByte(v2, 0)), abs(getByte(v1, 1) - getByte(v2, 1))), |
| 223 | SkMax32(abs(getByte(v1, 2) - getByte(v2, 2)), abs(getByte(v1, 3) - getByte(v2, 3)))); |
| 224 | } |
| 225 | |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 226 | class AutoRestoreBbhType { |
| 227 | public: |
| 228 | AutoRestoreBbhType() { |
| 229 | fRenderer = NULL; |
junov@chromium.org | d34fda1 | 2013-03-13 19:03:26 +0000 | [diff] [blame] | 230 | fSavedBbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType; |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void set(sk_tools::PictureRenderer* renderer, |
| 234 | sk_tools::PictureRenderer::BBoxHierarchyType bbhType) { |
| 235 | fRenderer = renderer; |
| 236 | fSavedBbhType = renderer->getBBoxHierarchyType(); |
| 237 | renderer->setBBoxHierarchyType(bbhType); |
| 238 | } |
| 239 | |
| 240 | ~AutoRestoreBbhType() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 241 | if (fRenderer) { |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 242 | fRenderer->setBBoxHierarchyType(fSavedBbhType); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | private: |
| 247 | sk_tools::PictureRenderer* fRenderer; |
| 248 | sk_tools::PictureRenderer::BBoxHierarchyType fSavedBbhType; |
| 249 | }; |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 250 | |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 251 | /** |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 252 | * Render the SKP file(s) within inputPath. |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 253 | * |
| 254 | * @param inputPath path to an individual SKP file, or a directory of SKP files |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 255 | * @param writePath if not NULL, write all image(s) generated into this directory |
| 256 | * @param mismatchPath if not NULL, write any image(s) not matching expectations into this directory |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 257 | * @param renderer PictureRenderer to use to render the SKPs |
| 258 | * @param jsonSummaryPtr if not NULL, add the image(s) generated to this summary |
| 259 | */ |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 260 | static bool render_picture(const SkString& inputPath, const SkString* writePath, |
| 261 | const SkString* mismatchPath, sk_tools::PictureRenderer& renderer, |
commit-bot@chromium.org | 205ce48 | 2014-05-12 15:37:20 +0000 | [diff] [blame] | 262 | sk_tools::ImageResultsAndExpectations *jsonSummaryPtr) { |
edisonn@google.com | ca1b3ff | 2013-01-16 18:18:48 +0000 | [diff] [blame] | 263 | int diffs[256] = {0}; |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 264 | SkBitmap* bitmap = NULL; |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 265 | renderer.setJsonSummaryPtr(jsonSummaryPtr); |
| 266 | bool success = render_picture_internal(inputPath, |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 267 | FLAGS_writeWholeImage ? NULL : writePath, |
| 268 | FLAGS_writeWholeImage ? NULL : mismatchPath, |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 269 | renderer, |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 270 | FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 271 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 272 | if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) { |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 273 | SkDebugf("Failed to draw the picture.\n"); |
| 274 | SkDELETE(bitmap); |
| 275 | return false; |
| 276 | } |
| 277 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 278 | if (FLAGS_validate) { |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 279 | SkBitmap* referenceBitmap = NULL; |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 280 | sk_tools::PictureRenderer* referenceRenderer; |
| 281 | // If the renderer uses a BBoxHierarchy, then the reference renderer |
skia.committer@gmail.com | 03682be | 2013-03-14 07:02:51 +0000 | [diff] [blame] | 282 | // will be the same renderer, without the bbh. |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 283 | AutoRestoreBbhType arbbh; |
| 284 | if (sk_tools::PictureRenderer::kNone_BBoxHierarchyType != |
| 285 | renderer.getBBoxHierarchyType()) { |
| 286 | referenceRenderer = &renderer; |
| 287 | referenceRenderer->ref(); // to match auto unref below |
| 288 | arbbh.set(referenceRenderer, sk_tools::PictureRenderer::kNone_BBoxHierarchyType); |
| 289 | } else { |
krajcevski | b1aded8 | 2014-08-18 07:52:17 -0700 | [diff] [blame] | 290 | #if SK_SUPPORT_GPU |
| 291 | referenceRenderer = SkNEW_ARGS(sk_tools::SimplePictureRenderer, |
| 292 | (renderer.getGrContextOptions())); |
| 293 | #else |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 294 | referenceRenderer = SkNEW(sk_tools::SimplePictureRenderer); |
krajcevski | b1aded8 | 2014-08-18 07:52:17 -0700 | [diff] [blame] | 295 | #endif |
junov@chromium.org | e286e84 | 2013-03-13 17:27:16 +0000 | [diff] [blame] | 296 | } |
| 297 | SkAutoTUnref<sk_tools::PictureRenderer> aurReferenceRenderer(referenceRenderer); |
| 298 | |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 299 | success = render_picture_internal(inputPath, NULL, NULL, *referenceRenderer, |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 300 | &referenceBitmap); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 301 | |
junov@chromium.org | c19c191 | 2013-03-12 19:56:49 +0000 | [diff] [blame] | 302 | if (!success || NULL == referenceBitmap || NULL == referenceBitmap->getPixels()) { |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 303 | SkDebugf("Failed to draw the reference picture.\n"); |
| 304 | SkDELETE(bitmap); |
| 305 | SkDELETE(referenceBitmap); |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | if (success && (bitmap->width() != referenceBitmap->width())) { |
| 310 | SkDebugf("Expected image width: %i, actual image width %i.\n", |
| 311 | referenceBitmap->width(), bitmap->width()); |
| 312 | SkDELETE(bitmap); |
| 313 | SkDELETE(referenceBitmap); |
| 314 | return false; |
| 315 | } |
| 316 | if (success && (bitmap->height() != referenceBitmap->height())) { |
| 317 | SkDebugf("Expected image height: %i, actual image height %i", |
| 318 | referenceBitmap->height(), bitmap->height()); |
| 319 | SkDELETE(bitmap); |
| 320 | SkDELETE(referenceBitmap); |
| 321 | return false; |
| 322 | } |
skia.committer@gmail.com | a7d8e3e | 2012-12-19 02:01:38 +0000 | [diff] [blame] | 323 | |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 324 | for (int y = 0; success && y < bitmap->height(); y++) { |
| 325 | for (int x = 0; success && x < bitmap->width(); x++) { |
edisonn@google.com | ca1b3ff | 2013-01-16 18:18:48 +0000 | [diff] [blame] | 326 | int diff = MaxByteDiff(*referenceBitmap->getAddr32(x, y), |
| 327 | *bitmap->getAddr32(x, y)); |
| 328 | SkASSERT(diff >= 0 && diff <= 255); |
| 329 | diffs[diff]++; |
skia.committer@gmail.com | 4d28d98 | 2013-01-17 07:06:06 +0000 | [diff] [blame] | 330 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 331 | if (diff > FLAGS_maxComponentDiff) { |
edisonn@google.com | ca1b3ff | 2013-01-16 18:18:48 +0000 | [diff] [blame] | 332 | SkDebugf("Expected pixel at (%i %i) exceedds maximum " |
| 333 | "component diff of %i: 0x%x, actual 0x%x\n", |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 334 | x, y, FLAGS_maxComponentDiff, |
edisonn@google.com | 01754bf | 2013-01-11 16:08:07 +0000 | [diff] [blame] | 335 | *referenceBitmap->getAddr32(x, y), |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 336 | *bitmap->getAddr32(x, y)); |
| 337 | SkDELETE(bitmap); |
| 338 | SkDELETE(referenceBitmap); |
| 339 | return false; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | SkDELETE(referenceBitmap); |
edisonn@google.com | ca1b3ff | 2013-01-16 18:18:48 +0000 | [diff] [blame] | 344 | |
| 345 | for (int i = 1; i <= 255; ++i) { |
| 346 | if(diffs[i] > 0) { |
| 347 | SkDebugf("Number of pixels with max diff of %i is %i\n", i, diffs[i]); |
| 348 | } |
| 349 | } |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 350 | } |
| 351 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 352 | if (FLAGS_writeWholeImage) { |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 353 | sk_tools::force_all_opaque(*bitmap); |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 354 | |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 355 | SkString inputFilename = SkOSPath::Basename(inputPath.c_str()); |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 356 | SkString outputFilename(inputFilename); |
| 357 | sk_tools::replace_char(&outputFilename, '.', '_'); |
| 358 | outputFilename.append(".png"); |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 359 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 360 | if (jsonSummaryPtr) { |
commit-bot@chromium.org | 205ce48 | 2014-05-12 15:37:20 +0000 | [diff] [blame] | 361 | sk_tools::ImageDigest imageDigest(*bitmap); |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 362 | jsonSummaryPtr->add(inputFilename.c_str(), outputFilename.c_str(), imageDigest); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 363 | if ((mismatchPath) && !mismatchPath->isEmpty() && |
epoger | 85b438d | 2014-08-21 23:21:32 -0700 | [diff] [blame] | 364 | !jsonSummaryPtr->getExpectation(inputFilename.c_str()).matches(imageDigest)) { |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 365 | success &= sk_tools::write_bitmap_to_disk(*bitmap, *mismatchPath, NULL, |
| 366 | outputFilename); |
| 367 | } |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 368 | } |
| 369 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 370 | if ((writePath) && !writePath->isEmpty()) { |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 371 | success &= sk_tools::write_bitmap_to_disk(*bitmap, *writePath, NULL, outputFilename); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | SkDELETE(bitmap); |
| 375 | |
| 376 | return success; |
| 377 | } |
| 378 | |
| 379 | |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 380 | static int process_input(const char* input, const SkString* writePath, |
| 381 | const SkString* mismatchPath, sk_tools::PictureRenderer& renderer, |
commit-bot@chromium.org | 205ce48 | 2014-05-12 15:37:20 +0000 | [diff] [blame] | 382 | sk_tools::ImageResultsAndExpectations *jsonSummaryPtr) { |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 383 | SkOSFile::Iter iter(input, "skp"); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 384 | SkString inputFilename; |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 385 | int failures = 0; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 386 | SkDebugf("process_input, %s\n", input); |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 387 | if (iter.next(&inputFilename)) { |
| 388 | do { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 389 | SkString inputPath = SkOSPath::Join(input, inputFilename.c_str()); |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 390 | if (!render_picture(inputPath, writePath, mismatchPath, renderer, jsonSummaryPtr)) { |
borenet@google.com | 57837bf | 2012-09-19 17:28:29 +0000 | [diff] [blame] | 391 | ++failures; |
| 392 | } |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 393 | } while(iter.next(&inputFilename)); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 394 | } else if (SkStrEndsWith(input, ".skp")) { |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 395 | SkString inputPath(input); |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 396 | if (!render_picture(inputPath, writePath, mismatchPath, renderer, jsonSummaryPtr)) { |
borenet@google.com | 57837bf | 2012-09-19 17:28:29 +0000 | [diff] [blame] | 397 | ++failures; |
| 398 | } |
| 399 | } else { |
| 400 | SkString warning; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 401 | warning.printf("Warning: skipping %s\n", input); |
kkinnunen | 297aaf9 | 2015-02-19 06:32:12 -0800 | [diff] [blame] | 402 | SkDebugf("%s", warning.c_str()); |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 403 | } |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 404 | return failures; |
keyar@chromium.org | 1cbd47c | 2012-07-13 18:22:59 +0000 | [diff] [blame] | 405 | } |
| 406 | |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 407 | int tool_main(int argc, char** argv); |
| 408 | int tool_main(int argc, char** argv) { |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 409 | SkCommandLineFlags::SetUsage("Render .skp files."); |
| 410 | SkCommandLineFlags::Parse(argc, argv); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 411 | |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 412 | if (FLAGS_readPath.isEmpty()) { |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 413 | SkDebugf(".skp files or directories are required.\n"); |
| 414 | exit(-1); |
| 415 | } |
| 416 | |
| 417 | if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) { |
| 418 | SkDebugf("--maxComponentDiff must be between 0 and 256\n"); |
| 419 | exit(-1); |
| 420 | } |
| 421 | |
| 422 | if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) { |
| 423 | SkDebugf("--maxComponentDiff requires --validate\n"); |
| 424 | exit(-1); |
| 425 | } |
| 426 | |
scroggo@google.com | 1125d39 | 2013-05-03 20:43:37 +0000 | [diff] [blame] | 427 | if (FLAGS_writeEncodedImages) { |
| 428 | if (FLAGS_writePath.isEmpty()) { |
| 429 | SkDebugf("--writeEncodedImages requires --writePath\n"); |
| 430 | exit(-1); |
| 431 | } |
| 432 | if (FLAGS_deferImageDecoding) { |
| 433 | SkDebugf("--writeEncodedImages is not compatible with --deferImageDecoding\n"); |
| 434 | exit(-1); |
| 435 | } |
| 436 | } |
| 437 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 438 | SkString errorString; |
| 439 | SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString, |
| 440 | kRender_PictureTool)); |
| 441 | if (errorString.size() > 0) { |
| 442 | SkDebugf("%s\n", errorString.c_str()); |
| 443 | } |
| 444 | |
| 445 | if (renderer.get() == NULL) { |
| 446 | exit(-1); |
| 447 | } |
| 448 | |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 449 | SkAutoGraphics ag; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 450 | |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 451 | SkString writePath; |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 452 | if (FLAGS_writePath.count() == 1) { |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 453 | writePath.set(FLAGS_writePath[0]); |
| 454 | } |
| 455 | SkString mismatchPath; |
| 456 | if (FLAGS_mismatchPath.count() == 1) { |
| 457 | mismatchPath.set(FLAGS_mismatchPath[0]); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 458 | } |
commit-bot@chromium.org | 205ce48 | 2014-05-12 15:37:20 +0000 | [diff] [blame] | 459 | sk_tools::ImageResultsAndExpectations jsonSummary; |
| 460 | sk_tools::ImageResultsAndExpectations* jsonSummaryPtr = NULL; |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 461 | if (FLAGS_writeJsonSummaryPath.count() == 1) { |
| 462 | jsonSummaryPtr = &jsonSummary; |
commit-bot@chromium.org | 205ce48 | 2014-05-12 15:37:20 +0000 | [diff] [blame] | 463 | if (FLAGS_readJsonSummaryPath.count() == 1) { |
| 464 | SkASSERT(jsonSummary.readExpectationsFile(FLAGS_readJsonSummaryPath[0])); |
| 465 | } |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 466 | } |
keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 467 | |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 468 | int failures = 0; |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 469 | for (int i = 0; i < FLAGS_readPath.count(); i ++) { |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 470 | failures += process_input(FLAGS_readPath[i], &writePath, &mismatchPath, *renderer.get(), |
| 471 | jsonSummaryPtr); |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 472 | } |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 473 | if (failures != 0) { |
| 474 | SkDebugf("Failed to render %i pictures.\n", failures); |
| 475 | return 1; |
| 476 | } |
bsalomon | 24234fe | 2014-10-24 09:34:41 -0700 | [diff] [blame] | 477 | #if GR_CACHE_STATS && SK_SUPPORT_GPU |
robertphillips@google.com | 163c84b | 2012-09-13 15:40:37 +0000 | [diff] [blame] | 478 | if (renderer->isUsingGpuDevice()) { |
| 479 | GrContext* ctx = renderer->getGrContext(); |
robertphillips@google.com | 163c84b | 2012-09-13 15:40:37 +0000 | [diff] [blame] | 480 | ctx->printCacheStats(); |
| 481 | } |
| 482 | #endif |
bsalomon | 24234fe | 2014-10-24 09:34:41 -0700 | [diff] [blame] | 483 | |
| 484 | #if GR_GPU_STATS && SK_SUPPORT_GPU |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 485 | if (FLAGS_gpuStats && renderer->isUsingGpuDevice()) { |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 486 | renderer->getGrContext()->printGpuStats(); |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 487 | } |
| 488 | #endif |
bsalomon | 24234fe | 2014-10-24 09:34:41 -0700 | [diff] [blame] | 489 | |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 490 | if (FLAGS_writeJsonSummaryPath.count() == 1) { |
epoger | b492c6f | 2014-08-14 07:32:49 -0700 | [diff] [blame] | 491 | // If there were any descriptions on the command line, insert them now. |
| 492 | for (int i=0; i<FLAGS_descriptions.count(); i++) { |
| 493 | SkTArray<SkString> tokens; |
| 494 | SkStrSplit(FLAGS_descriptions[i], "=", &tokens); |
| 495 | SkASSERT(tokens.count() == 2); |
| 496 | jsonSummary.addDescription(tokens[0].c_str(), tokens[1].c_str()); |
| 497 | } |
rmistry | 2529f2e | 2014-08-22 04:46:30 -0700 | [diff] [blame] | 498 | if (FLAGS_imageBaseGSUrl.count() == 1) { |
| 499 | jsonSummary.setImageBaseGSUrl(FLAGS_imageBaseGSUrl[0]); |
| 500 | } |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 501 | jsonSummary.writeToFile(FLAGS_writeJsonSummaryPath[0]); |
| 502 | } |
caryclark@google.com | 868e1f6 | 2012-10-02 20:00:03 +0000 | [diff] [blame] | 503 | return 0; |
junov@chromium.org | 777442d | 2012-06-12 14:56:36 +0000 | [diff] [blame] | 504 | } |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 505 | |
| 506 | #if !defined SK_BUILD_FOR_IOS |
| 507 | int main(int argc, char * const argv[]) { |
| 508 | return tool_main(argc, (char**) argv); |
| 509 | } |
| 510 | #endif |