Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "CrashHandler.h" | 8 #include "CrashHandler.h" |
| 9 #include "DMJsonWriter.h" | 9 #include "DMJsonWriter.h" |
| 10 #include "DMSrcSink.h" | 10 #include "DMSrcSink.h" |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 #include "SkMD5.h" | 21 #include "SkMD5.h" |
| 22 #include "SkMutex.h" | 22 #include "SkMutex.h" |
| 23 #include "SkOSFile.h" | 23 #include "SkOSFile.h" |
| 24 #include "SkTHash.h" | 24 #include "SkTHash.h" |
| 25 #include "SkTaskGroup.h" | 25 #include "SkTaskGroup.h" |
| 26 #include "SkThreadUtils.h" | 26 #include "SkThreadUtils.h" |
| 27 #include "Test.h" | 27 #include "Test.h" |
| 28 #include "Timer.h" | 28 #include "Timer.h" |
| 29 #include "sk_tool_utils.h" | 29 #include "sk_tool_utils.h" |
| 30 | 30 |
| 31 #ifdef SKIA_PNG_PREFIXED | |
| 32 // this must proceed png.h | |
| 33 #include "pngprefix.h" | |
| 34 #endif | |
| 35 #include "png.h" | |
| 36 | |
| 31 DEFINE_string(src, "tests gm skp image", "Source types to test."); | 37 DEFINE_string(src, "tests gm skp image", "Source types to test."); |
| 32 DEFINE_bool(nameByHash, false, | 38 DEFINE_bool(nameByHash, false, |
| 33 "If true, write to FLAGS_writePath[0]/<hash>.png instead of " | 39 "If true, write to FLAGS_writePath[0]/<hash>.png instead of " |
| 34 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>. png"); | 40 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>. png"); |
| 35 DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests."); | 41 DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests."); |
| 36 DEFINE_string(matrix, "1 0 0 1", | 42 DEFINE_string(matrix, "1 0 0 1", |
| 37 "2x2 scale+skew matrix to apply or upright when using " | 43 "2x2 scale+skew matrix to apply or upright when using " |
| 38 "'matrix' or 'upright' in config."); | 44 "'matrix' or 'upright' in config."); |
| 39 DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?"); | 45 DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?"); |
| 40 | 46 |
| (...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 459 break; | 465 break; |
| 460 } | 466 } |
| 461 sink = next; | 467 sink = next; |
| 462 } | 468 } |
| 463 if (sink) { | 469 if (sink) { |
| 464 push_sink(config, sink); | 470 push_sink(config, sink); |
| 465 } | 471 } |
| 466 } | 472 } |
| 467 } | 473 } |
| 468 | 474 |
| 475 static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) { | |
| 476 const int w = bitmap.width(), | |
| 477 h = bitmap.height(); | |
| 478 | |
| 479 // First get the bitmap into N32 color format. The next step will work only there. | |
| 480 if (bitmap.colorType() != kN32_SkColorType) { | |
| 481 SkBitmap n32; | |
| 482 if (!bitmap.copyTo(&n32, kN32_SkColorType)) { | |
| 483 return false; | |
| 484 } | |
| 485 bitmap = n32; | |
| 486 } | |
| 487 | |
| 488 // Convert our N32 bitmap into unpremul RGBA for libpng. | |
| 489 SkAutoTMalloc<uint32_t> rgba(w*h); | |
| 490 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpre mul_SkAlphaType), | |
| 491 rgba, 4*w, 0,0)) { | |
| 492 return false; | |
| 493 } | |
| 494 | |
| 495 // We don't need bitmap anymore. Might as well drop our ref. | |
| 496 bitmap = SkBitmap(); | |
|
hal.canary
2015/08/24 20:35:06
bitmap.reset();
| |
| 497 | |
| 498 FILE* f = fopen(path, "w"); | |
| 499 if (!f) { return false; } | |
| 500 | |
| 501 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nu llptr, nullptr); | |
| 502 if (!png) { | |
| 503 fclose(f); | |
| 504 return false; | |
| 505 } | |
| 506 | |
| 507 png_infop info = png_create_info_struct(png); | |
| 508 if (!info) { | |
| 509 png_destroy_write_struct(&png, &info); | |
| 510 fclose(f); | |
| 511 return false; | |
| 512 } | |
| 513 | |
| 514 png_text text[2]; | |
| 515 text[0].key = (png_charp)"Author"; | |
| 516 text[0].text = (png_charp)"DM dump_png()"; | |
| 517 text[0].compression = PNG_TEXT_COMPRESSION_NONE; | |
| 518 text[1].key = (png_charp)"Description"; | |
| 519 text[1].text = (png_charp)md5; | |
| 520 text[1].compression = PNG_TEXT_COMPRESSION_NONE; | |
| 521 png_set_text(png, info, text, 2); | |
| 522 | |
| 523 png_init_io(png, f); | |
| 524 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8, | |
| 525 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, | |
| 526 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); | |
| 527 png_write_info(png, info); | |
| 528 for (int j = 0; j < h; j++) { | |
| 529 png_bytep row = (png_bytep)(rgba.get() + w*j); | |
| 530 png_write_rows(png, &row, 1); | |
| 531 } | |
| 532 png_write_end(png, info); | |
| 533 | |
| 534 png_destroy_write_struct(&png, &info); | |
| 535 fclose(f); | |
| 536 return true; | |
| 537 } | |
| 538 | |
| 469 static bool match(const char* needle, const char* haystack) { | 539 static bool match(const char* needle, const char* haystack) { |
| 470 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle); | 540 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle); |
| 471 } | 541 } |
| 472 | 542 |
| 473 static ImplicitString is_blacklisted(const char* sink, const char* src, | 543 static ImplicitString is_blacklisted(const char* sink, const char* src, |
| 474 const char* srcOptions, const char* name) { | 544 const char* srcOptions, const char* name) { |
| 475 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) { | 545 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) { |
| 476 if (match(FLAGS_blacklist[i+0], sink) && | 546 if (match(FLAGS_blacklist[i+0], sink) && |
| 477 match(FLAGS_blacklist[i+1], src) && | 547 match(FLAGS_blacklist[i+1], src) && |
| 478 match(FLAGS_blacklist[i+2], srcOptions) && | 548 match(FLAGS_blacklist[i+2], srcOptions) && |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 628 sk_mkdir(path.c_str()); | 698 sk_mkdir(path.c_str()); |
| 629 if (strcmp(task.src.options, "") != 0) { | 699 if (strcmp(task.src.options, "") != 0) { |
| 630 path = SkOSPath::Join(path.c_str(), task.src.options); | 700 path = SkOSPath::Join(path.c_str(), task.src.options); |
| 631 sk_mkdir(path.c_str()); | 701 sk_mkdir(path.c_str()); |
| 632 } | 702 } |
| 633 path = SkOSPath::Join(path.c_str(), task.src->name().c_str()); | 703 path = SkOSPath::Join(path.c_str(), task.src->name().c_str()); |
| 634 path.append("."); | 704 path.append("."); |
| 635 path.append(ext); | 705 path.append(ext); |
| 636 } | 706 } |
| 637 | 707 |
| 638 SkFILEWStream file(path.c_str()); | |
| 639 if (!file.isValid()) { | |
| 640 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str())); | |
| 641 return; | |
| 642 } | |
| 643 | |
| 644 if (bitmap) { | 708 if (bitmap) { |
| 645 // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first. | 709 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) { |
| 646 SkBitmap converted; | |
| 647 if (bitmap->info().colorType() == kAlpha_8_SkColorType) { | |
| 648 if (!bitmap->copyTo(&converted, kN32_SkColorType)) { | |
| 649 fail("Can't convert A8 to 8888.\n"); | |
| 650 return; | |
| 651 } | |
| 652 bitmap = &converted; | |
| 653 } | |
| 654 if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kP NG_Type, 100)) { | |
| 655 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str())); | 710 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str())); |
| 656 return; | 711 return; |
| 657 } | 712 } |
| 658 } else { | 713 } else { |
| 714 SkFILEWStream file(path.c_str()); | |
| 715 if (!file.isValid()) { | |
| 716 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str() )); | |
| 717 return; | |
| 718 } | |
| 659 if (!file.writeStream(data, len)) { | 719 if (!file.writeStream(data, len)) { |
| 660 fail(SkStringPrintf("Can't write to %s.\n", path.c_str())); | 720 fail(SkStringPrintf("Can't write to %s.\n", path.c_str())); |
| 661 return; | 721 return; |
| 662 } | 722 } |
| 663 } | 723 } |
| 664 } | 724 } |
| 665 }; | 725 }; |
| 666 | 726 |
| 667 // Run all tasks in the same enclave serially on the same thread. | 727 // Run all tasks in the same enclave serially on the same thread. |
| 668 // They can't possibly run concurrently with each other. | 728 // They can't possibly run concurrently with each other. |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 848 } | 908 } |
| 849 return 0; | 909 return 0; |
| 850 } | 910 } |
| 851 | 911 |
| 852 #if !defined(SK_BUILD_FOR_IOS) | 912 #if !defined(SK_BUILD_FOR_IOS) |
| 853 int main(int argc, char** argv) { | 913 int main(int argc, char** argv) { |
| 854 SkCommandLineFlags::Parse(argc, argv); | 914 SkCommandLineFlags::Parse(argc, argv); |
| 855 return dm_main(); | 915 return dm_main(); |
| 856 } | 916 } |
| 857 #endif | 917 #endif |
| OLD | NEW |