reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 1 | #include "gm.h" |
reed@android.com | b9b9a18 | 2009-07-08 02:54:47 +0000 | [diff] [blame] | 2 | #include "SkColorPriv.h" |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 3 | #include "SkGraphics.h" |
| 4 | #include "SkImageDecoder.h" |
| 5 | #include "SkImageEncoder.h" |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 6 | #include "SkPicture.h" |
reed@google.com | 0770044 | 2010-12-20 19:46:07 +0000 | [diff] [blame] | 7 | #include "SkStream.h" |
| 8 | #include "SkRefCnt.h" |
| 9 | |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 10 | #include "GrContext.h" |
| 11 | #include "SkGpuCanvas.h" |
reed@google.com | d4dfd10 | 2011-01-18 21:05:42 +0000 | [diff] [blame] | 12 | #include "SkGpuDevice.h" |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 13 | #include "SkEGLContext.h" |
| 14 | #include "SkDevice.h" |
| 15 | |
reed@google.com | 0770044 | 2010-12-20 19:46:07 +0000 | [diff] [blame] | 16 | #ifdef SK_SUPPORT_PDF |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 17 | #include "SkPDFDevice.h" |
| 18 | #include "SkPDFDocument.h" |
reed@google.com | 0770044 | 2010-12-20 19:46:07 +0000 | [diff] [blame] | 19 | #endif |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace skiagm; |
| 22 | |
| 23 | // need to explicitly declare this, or we get some weird infinite loop llist |
| 24 | template GMRegistry* GMRegistry::gHead; |
| 25 | |
| 26 | class Iter { |
| 27 | public: |
| 28 | Iter() { |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 29 | fReg = GMRegistry::Head(); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 30 | } |
reed@google.com | d4dfd10 | 2011-01-18 21:05:42 +0000 | [diff] [blame] | 31 | |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 32 | GM* next() { |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 33 | if (fReg) { |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 34 | GMRegistry::Factory fact = fReg->factory(); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 35 | fReg = fReg->next(); |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 36 | return fact(0); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 37 | } |
| 38 | return NULL; |
| 39 | } |
reed@google.com | d4dfd10 | 2011-01-18 21:05:42 +0000 | [diff] [blame] | 40 | |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 41 | static int Count() { |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 42 | const GMRegistry* reg = GMRegistry::Head(); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 43 | int count = 0; |
| 44 | while (reg) { |
| 45 | count += 1; |
| 46 | reg = reg->next(); |
| 47 | } |
| 48 | return count; |
| 49 | } |
reed@google.com | d4dfd10 | 2011-01-18 21:05:42 +0000 | [diff] [blame] | 50 | |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 51 | private: |
| 52 | const GMRegistry* fReg; |
| 53 | }; |
| 54 | |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 55 | static SkString make_name(const char shortName[], const char configName[]) { |
| 56 | SkString name(shortName); |
| 57 | name.appendf("_%s", configName); |
| 58 | return name; |
| 59 | } |
| 60 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 61 | static SkString make_filename(const char path[], |
| 62 | const char pathSuffix[], |
| 63 | const SkString& name, |
| 64 | const char suffix[]) { |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 65 | SkString filename(path); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 66 | if (filename.endsWith("/")) { |
| 67 | filename.remove(filename.size() - 1, 1); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 68 | } |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 69 | filename.append(pathSuffix); |
| 70 | filename.append("/"); |
reed@google.com | 0770044 | 2010-12-20 19:46:07 +0000 | [diff] [blame] | 71 | filename.appendf("%s.%s", name.c_str(), suffix); |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 72 | return filename; |
| 73 | } |
| 74 | |
reed@android.com | b9b9a18 | 2009-07-08 02:54:47 +0000 | [diff] [blame] | 75 | /* since PNG insists on unpremultiplying our alpha, we take no precision chances |
| 76 | and force all pixels to be 100% opaque, otherwise on compare we may not get |
| 77 | a perfect match. |
| 78 | */ |
| 79 | static void force_all_opaque(const SkBitmap& bitmap) { |
| 80 | SkAutoLockPixels lock(bitmap); |
| 81 | for (int y = 0; y < bitmap.height(); y++) { |
| 82 | for (int x = 0; x < bitmap.width(); x++) { |
| 83 | *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) { |
| 89 | SkBitmap copy; |
| 90 | bitmap.copyTo(©, SkBitmap::kARGB_8888_Config); |
| 91 | force_all_opaque(copy); |
| 92 | return SkImageEncoder::EncodeFile(path.c_str(), copy, |
| 93 | SkImageEncoder::kPNG_Type, 100); |
| 94 | } |
| 95 | |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 96 | static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 97 | int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1); |
| 98 | int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1); |
| 99 | int db = SkGetPackedB32(c0) - SkGetPackedB32(c1); |
| 100 | return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db)); |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static void compute_diff(const SkBitmap& target, const SkBitmap& base, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 104 | SkBitmap* diff) { |
| 105 | SkAutoLockPixels alp(*diff); |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 106 | |
| 107 | const int w = target.width(); |
| 108 | const int h = target.height(); |
| 109 | for (int y = 0; y < h; y++) { |
| 110 | for (int x = 0; x < w; x++) { |
| 111 | SkPMColor c0 = *base.getAddr32(x, y); |
| 112 | SkPMColor c1 = *target.getAddr32(x, y); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 113 | SkPMColor d = 0; |
| 114 | if (c0 != c1) { |
| 115 | d = compute_diff_pmcolor(c0, c1); |
| 116 | } |
| 117 | *diff->getAddr32(x, y) = d; |
| 118 | } |
| 119 | } |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static bool compare(const SkBitmap& target, const SkBitmap& base, |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 123 | const SkString& name, const char* renderModeDescriptor, |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame] | 124 | SkBitmap* diff) { |
reed@android.com | b9b9a18 | 2009-07-08 02:54:47 +0000 | [diff] [blame] | 125 | SkBitmap copy; |
| 126 | const SkBitmap* bm = ⌖ |
| 127 | if (target.config() != SkBitmap::kARGB_8888_Config) { |
| 128 | target.copyTo(©, SkBitmap::kARGB_8888_Config); |
| 129 | bm = © |
| 130 | } |
| 131 | |
| 132 | force_all_opaque(*bm); |
| 133 | |
| 134 | const int w = bm->width(); |
| 135 | const int h = bm->height(); |
| 136 | if (w != base.width() || h != base.height()) { |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 137 | SkDebugf( |
| 138 | "---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n", |
| 139 | renderModeDescriptor, name.c_str(), |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame] | 140 | base.width(), base.height(), w, h); |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 141 | return false; |
reed@android.com | b9b9a18 | 2009-07-08 02:54:47 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | SkAutoLockPixels bmLock(*bm); |
| 145 | SkAutoLockPixels baseLock(base); |
| 146 | |
| 147 | for (int y = 0; y < h; y++) { |
| 148 | for (int x = 0; x < w; x++) { |
| 149 | SkPMColor c0 = *base.getAddr32(x, y); |
| 150 | SkPMColor c1 = *bm->getAddr32(x, y); |
| 151 | if (c0 != c1) { |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 152 | SkDebugf( |
| 153 | "----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n", |
| 154 | renderModeDescriptor, name.c_str(), x, y, c0, c1); |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 155 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 156 | if (diff) { |
| 157 | diff->setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 158 | diff->allocPixels(); |
| 159 | compute_diff(*bm, base, diff); |
| 160 | } |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 161 | return false; |
reed@android.com | b9b9a18 | 2009-07-08 02:54:47 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | } |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 165 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 166 | // they're equal |
| 167 | return true; |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 168 | } |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 169 | |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 170 | static bool write_pdf(const SkString& path, const SkDynamicMemoryWStream& pdf) { |
| 171 | SkFILEWStream stream(path.c_str()); |
| 172 | return stream.write(pdf.getStream(), pdf.getOffset()); |
reed@google.com | 0770044 | 2010-12-20 19:46:07 +0000 | [diff] [blame] | 173 | } |
| 174 | |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 175 | enum Backend { |
| 176 | kRaster_Backend, |
| 177 | kGPU_Backend, |
| 178 | kPDF_Backend, |
| 179 | }; |
| 180 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 181 | struct ConfigData { |
| 182 | SkBitmap::Config fConfig; |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 183 | Backend fBackend; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 184 | const char* fName; |
| 185 | }; |
| 186 | |
| 187 | /// Returns true if processing should continue, false to skip the |
| 188 | /// remainder of this config for this GM. |
| 189 | //@todo thudson 22 April 2011 - could refactor this to take in |
| 190 | // a factory to generate the context, always call readPixels() |
| 191 | // (logically a noop for rasters, if wasted time), and thus collapse the |
| 192 | // GPU special case and also let this be used for SkPicture testing. |
| 193 | static void setup_bitmap(const ConfigData& gRec, SkISize& size, |
| 194 | SkBitmap* bitmap) { |
| 195 | bitmap->setConfig(gRec.fConfig, size.width(), size.height()); |
| 196 | bitmap->allocPixels(); |
| 197 | bitmap->eraseColor(0); |
| 198 | } |
| 199 | |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 200 | // Returns true if the test should continue, false if the test should |
| 201 | // halt. |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 202 | static bool generate_image(GM* gm, const ConfigData& gRec, |
| 203 | GrContext* context, |
| 204 | SkBitmap& bitmap) { |
| 205 | SkISize size (gm->getISize()); |
| 206 | setup_bitmap(gRec, size, &bitmap); |
| 207 | SkCanvas canvas(bitmap); |
| 208 | |
| 209 | if (gRec.fBackend == kRaster_Backend) { |
| 210 | gm->draw(&canvas); |
| 211 | } else { // GPU |
| 212 | if (NULL == context) { |
| 213 | return false; |
| 214 | } |
| 215 | SkGpuCanvas gc(context, |
| 216 | SkGpuDevice::Current3DApiRenderTarget()); |
| 217 | gc.setDevice(gc.createDevice(bitmap.config(), |
| 218 | bitmap.width(), |
| 219 | bitmap.height(), |
| 220 | bitmap.isOpaque(), |
| 221 | false))->unref(); |
| 222 | gm->draw(&gc); |
| 223 | gc.readPixels(&bitmap); // overwrite our previous allocation |
| 224 | } |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | static void generate_image_from_picture(GM* gm, const ConfigData& gRec, |
| 229 | SkPicture* pict, SkBitmap* bitmap) { |
| 230 | SkISize size = gm->getISize(); |
| 231 | setup_bitmap(gRec, size, bitmap); |
| 232 | SkCanvas canvas(*bitmap); |
| 233 | canvas.drawPicture(*pict); |
| 234 | } |
| 235 | |
| 236 | static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) { |
| 237 | #ifdef SK_SUPPORT_PDF |
| 238 | SkISize size = gm->getISize(); |
| 239 | SkMatrix identity; |
| 240 | identity.reset(); |
ctguil@chromium.org | 1526129 | 2011-04-29 17:54:16 +0000 | [diff] [blame] | 241 | SkPDFDevice* dev = new SkPDFDevice(size, size, identity); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 242 | SkAutoUnref aur(dev); |
| 243 | |
| 244 | SkCanvas c(dev); |
| 245 | gm->draw(&c); |
| 246 | |
| 247 | SkPDFDocument doc; |
| 248 | doc.appendPage(dev); |
| 249 | doc.emitPDF(&pdf); |
| 250 | #endif |
| 251 | } |
| 252 | |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 253 | static bool write_reference_image(const ConfigData& gRec, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 254 | const char writePath [], |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 255 | const char renderModeDescriptor [], |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 256 | const SkString& name, |
| 257 | SkBitmap& bitmap, |
| 258 | SkDynamicMemoryWStream* pdf) { |
| 259 | SkString path; |
| 260 | bool success = false; |
| 261 | if (gRec.fBackend != kPDF_Backend) { |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 262 | path = make_filename(writePath, renderModeDescriptor, name, "png"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 263 | success = write_bitmap(path, bitmap); |
| 264 | } else if (pdf) { |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 265 | path = make_filename(writePath, renderModeDescriptor, name, "pdf"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 266 | success = write_pdf(path, *pdf); |
| 267 | } |
| 268 | if (!success) { |
| 269 | fprintf(stderr, "FAILED to write %s\n", path.c_str()); |
| 270 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 271 | return success; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 272 | } |
| 273 | |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 274 | static bool compare_to_reference_image(const char readPath [], |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 275 | const SkString& name, |
| 276 | SkBitmap &bitmap, |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame] | 277 | const char diffPath [], |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 278 | const char renderModeDescriptor []) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 279 | SkString path = make_filename(readPath, "", name, "png"); |
| 280 | SkBitmap orig; |
| 281 | bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig, |
| 282 | SkBitmap::kARGB_8888_Config, |
| 283 | SkImageDecoder::kDecodePixels_Mode, NULL); |
| 284 | if (success) { |
| 285 | SkBitmap diffBitmap; |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 286 | success = compare(bitmap, orig, name, renderModeDescriptor, |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame] | 287 | diffPath ? &diffBitmap : NULL); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 288 | if (!success && diffPath) { |
| 289 | SkString diffName = make_filename(diffPath, "", name, ".diff.png"); |
| 290 | fprintf(stderr, "Writing %s\n", diffName.c_str()); |
| 291 | write_bitmap(diffName, diffBitmap); |
| 292 | } |
| 293 | } else { |
| 294 | fprintf(stderr, "FAILED to read %s\n", path.c_str()); |
| 295 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 296 | return success; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 297 | } |
| 298 | |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 299 | static bool handle_test_results(GM* gm, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 300 | const ConfigData& gRec, |
| 301 | const char writePath [], |
| 302 | const char readPath [], |
| 303 | const char diffPath [], |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 304 | const char renderModeDescriptor [], |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 305 | SkBitmap& bitmap, |
| 306 | SkDynamicMemoryWStream* pdf) { |
| 307 | SkString name = make_name(gm->shortName(), gRec.fName); |
| 308 | |
| 309 | if (writePath) { |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 310 | write_reference_image(gRec, writePath, renderModeDescriptor, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 311 | name, bitmap, pdf); |
| 312 | // TODO: Figure out a way to compare PDFs. |
| 313 | } else if (readPath && gRec.fBackend != kPDF_Backend) { |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 314 | return compare_to_reference_image(readPath, name, bitmap, |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 315 | diffPath, renderModeDescriptor); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 316 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 317 | return true; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | static SkPicture* generate_new_picture(GM* gm) { |
| 321 | // Pictures are refcounted so must be on heap |
| 322 | SkPicture* pict = new SkPicture; |
| 323 | SkCanvas* cv = pict->beginRecording(1000, 1000); |
| 324 | gm->draw(cv); |
| 325 | pict->endRecording(); |
| 326 | |
| 327 | return pict; |
| 328 | } |
| 329 | |
| 330 | static SkPicture* stream_to_new_picture(const SkPicture& src) { |
| 331 | |
| 332 | // To do in-memory commiunications with a stream, we need to: |
| 333 | // * create a dynamic memory stream |
| 334 | // * copy it into a buffer |
| 335 | // * create a read stream from it |
| 336 | // ?!?! |
| 337 | |
| 338 | SkDynamicMemoryWStream storage; |
| 339 | src.serialize(&storage); |
| 340 | |
| 341 | int streamSize = storage.getOffset(); |
| 342 | SkAutoMalloc dstStorage(streamSize); |
| 343 | void* dst = dstStorage.get(); |
| 344 | //char* dst = new char [streamSize]; |
| 345 | //@todo thudson 22 April 2011 when can we safely delete [] dst? |
| 346 | storage.copyTo(dst); |
| 347 | SkMemoryStream pictReadback(dst, streamSize); |
| 348 | SkPicture* retval = new SkPicture (&pictReadback); |
| 349 | return retval; |
| 350 | } |
| 351 | |
| 352 | // Test: draw into a bitmap or pdf. |
| 353 | // Depending on flags, possibly compare to an expected image |
| 354 | // and possibly output a diff image if it fails to match. |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 355 | static bool test_drawing(GM* gm, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 356 | const ConfigData& gRec, |
| 357 | const char writePath [], |
| 358 | const char readPath [], |
| 359 | const char diffPath [], |
| 360 | GrContext* context) { |
| 361 | SkBitmap bitmap; |
| 362 | SkDynamicMemoryWStream pdf; |
| 363 | |
| 364 | if (gRec.fBackend == kRaster_Backend || |
| 365 | gRec.fBackend == kGPU_Backend) { |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 366 | // Early exit if we can't generate the image, but this is |
| 367 | // expected in some cases, so don't report a test failure. |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 368 | if (!generate_image(gm, gRec, context, bitmap)) { |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 369 | return true; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | // TODO: Figure out a way to compare PDFs. |
| 373 | if (gRec.fBackend == kPDF_Backend && writePath) { |
| 374 | generate_pdf(gm, pdf); |
| 375 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 376 | return handle_test_results(gm, gRec, writePath, readPath, diffPath, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 377 | "", bitmap, &pdf); |
| 378 | } |
| 379 | |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 380 | static bool test_picture_playback(GM* gm, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 381 | const ConfigData& gRec, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 382 | const char readPath [], |
| 383 | const char diffPath []) { |
| 384 | SkPicture* pict = generate_new_picture(gm); |
| 385 | SkAutoUnref aur(pict); |
| 386 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 387 | if (kRaster_Backend == gRec.fBackend) { |
| 388 | SkBitmap bitmap; |
| 389 | generate_image_from_picture(gm, gRec, pict, &bitmap); |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 390 | return handle_test_results(gm, gRec, NULL, readPath, diffPath, |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame] | 391 | "-replay", bitmap, NULL); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 392 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 393 | return true; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 394 | } |
| 395 | |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 396 | static bool test_picture_serialization(GM* gm, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 397 | const ConfigData& gRec, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 398 | const char readPath [], |
| 399 | const char diffPath []) { |
| 400 | SkPicture* pict = generate_new_picture(gm); |
| 401 | SkAutoUnref aurp(pict); |
| 402 | SkPicture* repict = stream_to_new_picture(*pict); |
| 403 | SkAutoUnref aurr(repict); |
| 404 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 405 | if (kRaster_Backend == gRec.fBackend) { |
| 406 | SkBitmap bitmap; |
| 407 | generate_image_from_picture(gm, gRec, repict, &bitmap); |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 408 | return handle_test_results(gm, gRec, NULL, readPath, diffPath, |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame] | 409 | "-serialize", bitmap, NULL); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 410 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 411 | return true; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | static void usage(const char * argv0) { |
| 415 | SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0); |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 416 | SkDebugf(" [--replay] [--serialize]\n"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 417 | SkDebugf(" writePath: directory to write rendered images in.\n"); |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 418 | SkDebugf( |
| 419 | " readPath: directory to read reference images from;\n" |
| 420 | " reports if any pixels mismatch between reference and new images\n"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 421 | SkDebugf(" diffPath: directory to write difference images in.\n"); |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 422 | SkDebugf(" --replay: exercise SkPicture replay.\n"); |
| 423 | SkDebugf( |
| 424 | " --serialize: exercise SkPicture serialization & deserialization.\n"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static const ConfigData gRec[] = { |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 428 | { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" }, |
| 429 | { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" }, |
| 430 | { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" }, |
| 431 | { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" }, |
| 432 | #ifdef SK_SUPPORT_PDF |
| 433 | { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" }, |
| 434 | #endif |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 435 | }; |
| 436 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 437 | int main(int argc, char * const argv[]) { |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 438 | SkAutoGraphics ag; |
reed@google.com | d4dfd10 | 2011-01-18 21:05:42 +0000 | [diff] [blame] | 439 | |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 440 | const char* writePath = NULL; // if non-null, where we write the originals |
| 441 | const char* readPath = NULL; // if non-null, were we read from to compare |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 442 | const char* diffPath = NULL; // if non-null, where we write our diffs (from compare) |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 443 | |
reed@google.com | b8b0983 | 2011-05-26 15:57:56 +0000 | [diff] [blame] | 444 | bool doReplay = true; |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame] | 445 | bool doSerialize = false; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 446 | const char* const commandName = argv[0]; |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 447 | char* const* stop = argv + argc; |
| 448 | for (++argv; argv < stop; ++argv) { |
| 449 | if (strcmp(*argv, "-w") == 0) { |
| 450 | argv++; |
| 451 | if (argv < stop && **argv) { |
| 452 | writePath = *argv; |
| 453 | } |
| 454 | } else if (strcmp(*argv, "-r") == 0) { |
| 455 | argv++; |
| 456 | if (argv < stop && **argv) { |
| 457 | readPath = *argv; |
| 458 | } |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 459 | } else if (strcmp(*argv, "-d") == 0) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 460 | argv++; |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 461 | if (argv < stop && **argv) { |
| 462 | diffPath = *argv; |
| 463 | } |
reed@google.com | b8b0983 | 2011-05-26 15:57:56 +0000 | [diff] [blame] | 464 | } else if (strcmp(*argv, "--noreplay") == 0) { |
| 465 | doReplay = false; |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 466 | } else if (strcmp(*argv, "--serialize") == 0) { |
| 467 | doSerialize = true; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 468 | } else { |
| 469 | usage(commandName); |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 470 | return -1; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | if (argv != stop) { |
| 474 | usage(commandName); |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 475 | return -1; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 476 | } |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 477 | |
| 478 | // setup a GL context for drawing offscreen |
reed@google.com | 37df17d | 2010-12-23 20:20:51 +0000 | [diff] [blame] | 479 | GrContext* context = NULL; |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 480 | SkEGLContext eglContext; |
reed@google.com | 37df17d | 2010-12-23 20:20:51 +0000 | [diff] [blame] | 481 | if (eglContext.init(1024, 1024)) { |
| 482 | context = GrContext::CreateGLShaderContext(); |
| 483 | } |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 484 | |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 485 | Iter iter; |
| 486 | GM* gm; |
reed@android.com | 00f883e | 2010-12-14 17:46:14 +0000 | [diff] [blame] | 487 | |
| 488 | if (readPath) { |
| 489 | fprintf(stderr, "reading from %s\n", readPath); |
| 490 | } else if (writePath) { |
| 491 | fprintf(stderr, "writing to %s\n", writePath); |
| 492 | } |
| 493 | |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 494 | // Accumulate success of all tests so we can flag error in any |
| 495 | // one with the return value. |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 496 | bool overallSuccess = true; |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 497 | while ((gm = iter.next()) != NULL) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 498 | SkISize size = gm->getISize(); |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 499 | SkDebugf("drawing... %s [%d %d]\n", gm->shortName(), |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 500 | size.width(), size.height()); |
| 501 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 502 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) { |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 503 | bool testSuccess = test_drawing(gm, gRec[i], |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 504 | writePath, readPath, diffPath, context); |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 505 | overallSuccess &= testSuccess; |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 506 | |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 507 | if (doReplay && testSuccess) { |
| 508 | testSuccess = test_picture_playback(gm, gRec[i], |
| 509 | readPath, diffPath); |
| 510 | overallSuccess &= testSuccess; |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 511 | } |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 512 | |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 513 | if (doSerialize && testSuccess) { |
| 514 | overallSuccess &= test_picture_serialization(gm, gRec[i], |
| 515 | readPath, diffPath); |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 516 | } |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 517 | } |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 518 | SkDELETE(gm); |
| 519 | } |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame^] | 520 | if (false == overallSuccess) { |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 521 | return -1; |
| 522 | } |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 523 | return 0; |
| 524 | } |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 525 | |
| 526 | /////////////////////////////////////////////////////////////////////////////// |
| 527 | |
| 528 | using namespace skiagm; |
| 529 | |
| 530 | GM::GM() {} |
| 531 | GM::~GM() {} |
| 532 | |
| 533 | void GM::draw(SkCanvas* canvas) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 534 | this->onDraw(canvas); |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 535 | } |