render_pictures_main.cpp revision 1125d39de8ec64b26746b07ea0f1360aa473779c
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 8#include "CopyTilesRenderer.h" 9#include "SkBitmap.h" 10#include "SkDevice.h" 11#include "SkCommandLineFlags.h" 12#include "SkGraphics.h" 13#include "SkImageDecoder.h" 14#include "SkImageEncoder.h" 15#include "SkMath.h" 16#include "SkOSFile.h" 17#include "SkPicture.h" 18#include "SkStream.h" 19#include "SkString.h" 20#include "PictureRenderer.h" 21#include "PictureRenderingFlags.h" 22#include "picture_utils.h" 23 24// Flags used by this file, alphabetically: 25DEFINE_int32(clone, 0, "Clone the picture n times before rendering."); 26DECLARE_bool(deferImageDecoding); 27DEFINE_int32(maxComponentDiff, 256, "Maximum diff on a component, 0 - 256. Components that differ " 28 "by more than this amount are considered errors, though all diffs are reported. " 29 "Requires --validate."); 30DECLARE_string(readPath); 31DEFINE_bool(writeEncodedImages, false, "Any time the skp contains an encoded image, write it to a " 32 "file rather than decoding it. Requires writePath to be set. Skips drawing the full " 33 "skp to a file. Not compatible with deferImageDecoding."); 34DEFINE_string2(writePath, w, "", "Directory to write the rendered images."); 35DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a " 36 "file, instead of an image for each tile."); 37DEFINE_bool(validate, false, "Verify that the rendered image contains the same pixels as " 38 "the picture rendered in simple mode. When used in conjunction with --bbh, results " 39 "are validated against the picture rendered in the same mode, but without the bbh."); 40 41static void make_output_filepath(SkString* path, const SkString& dir, 42 const SkString& name) { 43 sk_tools::make_filepath(path, dir, name); 44 // Remove ".skp" 45 path->remove(path->size() - 4, 4); 46} 47 48// Defined in PictureRenderingFlags.cpp 49extern bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap* bitmap); 50 51//////////////////////////////////////////////////////////////////////////////////////////////////// 52 53/** 54 * Table for translating from format of data to a suffix. 55 */ 56struct Format { 57 SkImageDecoder::Format fFormat; 58 const char* fSuffix; 59}; 60static 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 */ 74static 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 */ 86static SkString gInputFileName; 87 88/** 89 * Number to be appended to the image file name so that it is unique. 90 */ 91static 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 */ 100static 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 */ 115static 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 135static bool render_picture(const SkString& inputPath, const SkString* outputDir, 136 sk_tools::PictureRenderer& renderer, 137 SkBitmap** out) { 138 SkString inputFilename; 139 sk_tools::get_basename(&inputFilename, inputPath); 140 141 SkFILEStream inputStream; 142 inputStream.setPath(inputPath.c_str()); 143 if (!inputStream.isValid()) { 144 SkDebugf("Could not open file %s\n", inputPath.c_str()); 145 return false; 146 } 147 148 bool success = false; 149 SkPicture* picture; 150 if (FLAGS_deferImageDecoding) { 151 picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &lazy_decode_bitmap)); 152 } else if (FLAGS_writeEncodedImages) { 153 SkASSERT(!FLAGS_writePath.isEmpty()); 154 reset_image_file_base_name(inputFilename); 155 picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &write_image_to_file)); 156 } else { 157 picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &SkImageDecoder::DecodeMemory)); 158 } 159 160 if (!success) { 161 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str()); 162 return false; 163 } 164 165 for (int i = 0; i < FLAGS_clone; ++i) { 166 SkPicture* clone = picture->clone(); 167 SkDELETE(picture); 168 picture = clone; 169 } 170 171 SkDebugf("drawing... [%i %i] %s\n", picture->width(), picture->height(), 172 inputPath.c_str()); 173 174 renderer.init(picture); 175 renderer.setup(); 176 177 SkString* outputPath = NULL; 178 if (NULL != outputDir && outputDir->size() > 0 && !FLAGS_writeEncodedImages) { 179 outputPath = SkNEW(SkString); 180 make_output_filepath(outputPath, *outputDir, inputFilename); 181 } 182 183 success = renderer.render(outputPath, out); 184 if (outputPath) { 185 if (!success) { 186 SkDebugf("Could not write to file %s\n", outputPath->c_str()); 187 } 188 SkDELETE(outputPath); 189 } 190 191 renderer.end(); 192 193 SkDELETE(picture); 194 return success; 195} 196 197static inline int getByte(uint32_t value, int index) { 198 SkASSERT(0 <= index && index < 4); 199 return (value >> (index * 8)) & 0xFF; 200} 201 202static int MaxByteDiff(uint32_t v1, uint32_t v2) { 203 return SkMax32(SkMax32(abs(getByte(v1, 0) - getByte(v2, 0)), abs(getByte(v1, 1) - getByte(v2, 1))), 204 SkMax32(abs(getByte(v1, 2) - getByte(v2, 2)), abs(getByte(v1, 3) - getByte(v2, 3)))); 205} 206 207namespace { 208class AutoRestoreBbhType { 209public: 210 AutoRestoreBbhType() { 211 fRenderer = NULL; 212 fSavedBbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType; 213 } 214 215 void set(sk_tools::PictureRenderer* renderer, 216 sk_tools::PictureRenderer::BBoxHierarchyType bbhType) { 217 fRenderer = renderer; 218 fSavedBbhType = renderer->getBBoxHierarchyType(); 219 renderer->setBBoxHierarchyType(bbhType); 220 } 221 222 ~AutoRestoreBbhType() { 223 if (NULL != fRenderer) { 224 fRenderer->setBBoxHierarchyType(fSavedBbhType); 225 } 226 } 227 228private: 229 sk_tools::PictureRenderer* fRenderer; 230 sk_tools::PictureRenderer::BBoxHierarchyType fSavedBbhType; 231}; 232} 233 234static bool render_picture(const SkString& inputPath, const SkString* outputDir, 235 sk_tools::PictureRenderer& renderer) { 236 int diffs[256] = {0}; 237 SkBitmap* bitmap = NULL; 238 bool success = render_picture(inputPath, 239 FLAGS_writeWholeImage ? NULL : outputDir, 240 renderer, 241 FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL); 242 243 if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) { 244 SkDebugf("Failed to draw the picture.\n"); 245 SkDELETE(bitmap); 246 return false; 247 } 248 249 if (FLAGS_validate) { 250 SkBitmap* referenceBitmap = NULL; 251 sk_tools::PictureRenderer* referenceRenderer; 252 // If the renderer uses a BBoxHierarchy, then the reference renderer 253 // will be the same renderer, without the bbh. 254 AutoRestoreBbhType arbbh; 255 if (sk_tools::PictureRenderer::kNone_BBoxHierarchyType != 256 renderer.getBBoxHierarchyType()) { 257 referenceRenderer = &renderer; 258 referenceRenderer->ref(); // to match auto unref below 259 arbbh.set(referenceRenderer, sk_tools::PictureRenderer::kNone_BBoxHierarchyType); 260 } else { 261 referenceRenderer = SkNEW(sk_tools::SimplePictureRenderer); 262 } 263 SkAutoTUnref<sk_tools::PictureRenderer> aurReferenceRenderer(referenceRenderer); 264 265 success = render_picture(inputPath, NULL, *referenceRenderer, 266 &referenceBitmap); 267 268 if (!success || NULL == referenceBitmap || NULL == referenceBitmap->getPixels()) { 269 SkDebugf("Failed to draw the reference picture.\n"); 270 SkDELETE(bitmap); 271 SkDELETE(referenceBitmap); 272 return false; 273 } 274 275 if (success && (bitmap->width() != referenceBitmap->width())) { 276 SkDebugf("Expected image width: %i, actual image width %i.\n", 277 referenceBitmap->width(), bitmap->width()); 278 SkDELETE(bitmap); 279 SkDELETE(referenceBitmap); 280 return false; 281 } 282 if (success && (bitmap->height() != referenceBitmap->height())) { 283 SkDebugf("Expected image height: %i, actual image height %i", 284 referenceBitmap->height(), bitmap->height()); 285 SkDELETE(bitmap); 286 SkDELETE(referenceBitmap); 287 return false; 288 } 289 290 for (int y = 0; success && y < bitmap->height(); y++) { 291 for (int x = 0; success && x < bitmap->width(); x++) { 292 int diff = MaxByteDiff(*referenceBitmap->getAddr32(x, y), 293 *bitmap->getAddr32(x, y)); 294 SkASSERT(diff >= 0 && diff <= 255); 295 diffs[diff]++; 296 297 if (diff > FLAGS_maxComponentDiff) { 298 SkDebugf("Expected pixel at (%i %i) exceedds maximum " 299 "component diff of %i: 0x%x, actual 0x%x\n", 300 x, y, FLAGS_maxComponentDiff, 301 *referenceBitmap->getAddr32(x, y), 302 *bitmap->getAddr32(x, y)); 303 SkDELETE(bitmap); 304 SkDELETE(referenceBitmap); 305 return false; 306 } 307 } 308 } 309 SkDELETE(referenceBitmap); 310 311 for (int i = 1; i <= 255; ++i) { 312 if(diffs[i] > 0) { 313 SkDebugf("Number of pixels with max diff of %i is %i\n", i, diffs[i]); 314 } 315 } 316 } 317 318 if (FLAGS_writeWholeImage) { 319 sk_tools::force_all_opaque(*bitmap); 320 if (NULL != outputDir && FLAGS_writeWholeImage) { 321 SkString inputFilename; 322 sk_tools::get_basename(&inputFilename, inputPath); 323 SkString outputPath; 324 make_output_filepath(&outputPath, *outputDir, inputFilename); 325 outputPath.append(".png"); 326 if (!SkImageEncoder::EncodeFile(outputPath.c_str(), *bitmap, 327 SkImageEncoder::kPNG_Type, 100)) { 328 SkDebugf("Failed to draw the picture.\n"); 329 success = false; 330 } 331 } 332 } 333 SkDELETE(bitmap); 334 335 return success; 336} 337 338 339static int process_input(const char* input, const SkString* outputDir, 340 sk_tools::PictureRenderer& renderer) { 341 SkOSFile::Iter iter(input, "skp"); 342 SkString inputFilename; 343 int failures = 0; 344 SkDebugf("process_input, %s\n", input); 345 if (iter.next(&inputFilename)) { 346 do { 347 SkString inputPath; 348 SkString inputAsSkString(input); 349 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename); 350 if (!render_picture(inputPath, outputDir, renderer)) { 351 ++failures; 352 } 353 } while(iter.next(&inputFilename)); 354 } else if (SkStrEndsWith(input, ".skp")) { 355 SkString inputPath(input); 356 if (!render_picture(inputPath, outputDir, renderer)) { 357 ++failures; 358 } 359 } else { 360 SkString warning; 361 warning.printf("Warning: skipping %s\n", input); 362 SkDebugf(warning.c_str()); 363 } 364 return failures; 365} 366 367int tool_main(int argc, char** argv); 368int tool_main(int argc, char** argv) { 369 SkCommandLineFlags::SetUsage("Render .skp files."); 370 SkCommandLineFlags::Parse(argc, argv); 371 372 if (FLAGS_readPath.isEmpty()) { 373 SkDebugf(".skp files or directories are required.\n"); 374 exit(-1); 375 } 376 377 if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) { 378 SkDebugf("--maxComponentDiff must be between 0 and 256\n"); 379 exit(-1); 380 } 381 382 if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) { 383 SkDebugf("--maxComponentDiff requires --validate\n"); 384 exit(-1); 385 } 386 387 if (FLAGS_clone < 0) { 388 SkDebugf("--clone must be >= 0. Was %i\n", FLAGS_clone); 389 exit(-1); 390 } 391 392 if (FLAGS_writeEncodedImages) { 393 if (FLAGS_writePath.isEmpty()) { 394 SkDebugf("--writeEncodedImages requires --writePath\n"); 395 exit(-1); 396 } 397 if (FLAGS_deferImageDecoding) { 398 SkDebugf("--writeEncodedImages is not compatible with --deferImageDecoding\n"); 399 exit(-1); 400 } 401 } 402 403 SkString errorString; 404 SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString, 405 kRender_PictureTool)); 406 if (errorString.size() > 0) { 407 SkDebugf("%s\n", errorString.c_str()); 408 } 409 410 if (renderer.get() == NULL) { 411 exit(-1); 412 } 413 414 SkAutoGraphics ag; 415 416 SkString outputDir; 417 if (FLAGS_writePath.count() == 1) { 418 outputDir.set(FLAGS_writePath[0]); 419 } 420 421 int failures = 0; 422 for (int i = 0; i < FLAGS_readPath.count(); i ++) { 423 failures += process_input(FLAGS_readPath[i], &outputDir, *renderer.get()); 424 } 425 if (failures != 0) { 426 SkDebugf("Failed to render %i pictures.\n", failures); 427 return 1; 428 } 429#if SK_SUPPORT_GPU 430#if GR_CACHE_STATS 431 if (renderer->isUsingGpuDevice()) { 432 GrContext* ctx = renderer->getGrContext(); 433 434 ctx->printCacheStats(); 435 } 436#endif 437#endif 438 return 0; 439} 440 441#if !defined SK_BUILD_FOR_IOS 442int main(int argc, char * const argv[]) { 443 return tool_main(argc, (char**) argv); 444} 445#endif 446