reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 1 | #include "gm.h" |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 2 | #include "SkGraphics.h" |
| 3 | #include "SkImageDecoder.h" |
| 4 | #include "SkImageEncoder.h" |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 5 | |
| 6 | using namespace skiagm; |
| 7 | |
| 8 | // need to explicitly declare this, or we get some weird infinite loop llist |
| 9 | template GMRegistry* GMRegistry::gHead; |
| 10 | |
| 11 | class Iter { |
| 12 | public: |
| 13 | Iter() { |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 14 | fReg = GMRegistry::Head(); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 15 | } |
| 16 | |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 17 | GM* next() { |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 18 | if (fReg) { |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 19 | GMRegistry::Factory fact = fReg->factory(); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 20 | fReg = fReg->next(); |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 21 | return fact(0); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 22 | } |
| 23 | return NULL; |
| 24 | } |
| 25 | |
| 26 | static int Count() { |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 27 | const GMRegistry* reg = GMRegistry::Head(); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 28 | int count = 0; |
| 29 | while (reg) { |
| 30 | count += 1; |
| 31 | reg = reg->next(); |
| 32 | } |
| 33 | return count; |
| 34 | } |
| 35 | |
| 36 | private: |
| 37 | const GMRegistry* fReg; |
| 38 | }; |
| 39 | |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 40 | static SkString make_name(const char shortName[], const char configName[]) { |
| 41 | SkString name(shortName); |
| 42 | name.appendf("_%s", configName); |
| 43 | return name; |
| 44 | } |
| 45 | |
| 46 | static SkString make_filename(const char path[], const SkString& name) { |
| 47 | SkString filename(path); |
| 48 | if (filename.size() && filename[filename.size() - 1] != '/') { |
| 49 | filename.append("/"); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 50 | } |
reed@android.com | 25b0434 | 2009-06-21 01:40:31 +0000 | [diff] [blame^] | 51 | filename.appendf("%s.png", name.c_str()); |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 52 | return filename; |
| 53 | } |
| 54 | |
| 55 | static void compare(const SkBitmap& target, const SkBitmap& base, |
| 56 | const SkString& name) { |
| 57 | } |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 58 | |
| 59 | static const struct { |
| 60 | SkBitmap::Config fConfig; |
| 61 | bool fUsePicture; |
| 62 | const char* fName; |
| 63 | } gRec[] = { |
| 64 | { SkBitmap::kARGB_8888_Config, false, "8888" }, |
| 65 | { SkBitmap::kARGB_4444_Config, false, "4444" }, |
| 66 | { SkBitmap::kRGB_565_Config, false, "565" }, |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | int main (int argc, char * const argv[]) { |
| 70 | SkAutoGraphics ag; |
| 71 | |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 72 | const char* writePath = NULL; // if non-null, where we write the originals |
| 73 | const char* readPath = NULL; // if non-null, were we read from to compare |
| 74 | |
| 75 | char* const* stop = argv + argc; |
| 76 | for (++argv; argv < stop; ++argv) { |
| 77 | if (strcmp(*argv, "-w") == 0) { |
| 78 | argv++; |
| 79 | if (argv < stop && **argv) { |
| 80 | writePath = *argv; |
| 81 | } |
| 82 | } else if (strcmp(*argv, "-r") == 0) { |
| 83 | argv++; |
| 84 | if (argv < stop && **argv) { |
| 85 | readPath = *argv; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 90 | Iter iter; |
| 91 | GM* gm; |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 92 | |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 93 | while ((gm = iter.next()) != NULL) { |
| 94 | SkISize size = gm->getISize(); |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 95 | SkDebugf("---- %s [%d %d]\n", gm->shortName(), |
| 96 | size.width(), size.height()); |
| 97 | |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 98 | SkBitmap bitmap; |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 99 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) { |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 100 | bitmap.setConfig(gRec[i].fConfig, size.width(), size.height()); |
| 101 | bitmap.allocPixels(); |
| 102 | bitmap.eraseColor(0); |
| 103 | SkCanvas canvas(bitmap); |
| 104 | |
| 105 | gm->draw(&canvas); |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 106 | |
| 107 | SkString name = make_name(gm->shortName(), gRec[i].fName); |
| 108 | |
| 109 | if (writePath) { |
| 110 | SkString path = make_filename(writePath, name); |
reed@android.com | 25b0434 | 2009-06-21 01:40:31 +0000 | [diff] [blame^] | 111 | SkBitmap copy; |
| 112 | bitmap.copyTo(©, SkBitmap::kARGB_8888_Config); |
| 113 | bool success = SkImageEncoder::EncodeFile(path.c_str(), copy, |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 114 | SkImageEncoder::kPNG_Type, 100); |
| 115 | if (!success) { |
| 116 | fprintf(stderr, "FAILED to write %s\n", path.c_str()); |
| 117 | } |
| 118 | } else if (readPath) { |
| 119 | SkString path = make_filename(writePath, name); |
| 120 | SkBitmap orig; |
| 121 | bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig, |
| 122 | SkBitmap::kARGB_8888_Config, |
| 123 | SkImageDecoder::kDecodePixels_Mode, NULL); |
| 124 | if (success) { |
| 125 | compare(bitmap, orig, name); |
| 126 | } else { |
| 127 | fprintf(stderr, "FAILED to read %s\n", path.c_str()); |
| 128 | } |
| 129 | } |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 130 | } |
| 131 | SkDELETE(gm); |
| 132 | } |
| 133 | return 0; |
| 134 | } |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 135 | |
| 136 | /////////////////////////////////////////////////////////////////////////////// |
| 137 | |
| 138 | using namespace skiagm; |
| 139 | |
| 140 | GM::GM() {} |
| 141 | GM::~GM() {} |
| 142 | |
| 143 | void GM::draw(SkCanvas* canvas) { |
| 144 | this->onDraw(canvas); |
| 145 | } |
| 146 | |
| 147 | |