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 | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame^] | 123 | const SkString& name, const char* modeDescriptor, |
| 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 | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame^] | 137 | SkDebugf("---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n", |
| 138 | modeDescriptor, name.c_str(), |
| 139 | base.width(), base.height(), w, h); |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 140 | return false; |
reed@android.com | b9b9a18 | 2009-07-08 02:54:47 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | SkAutoLockPixels bmLock(*bm); |
| 144 | SkAutoLockPixels baseLock(base); |
| 145 | |
| 146 | for (int y = 0; y < h; y++) { |
| 147 | for (int x = 0; x < w; x++) { |
| 148 | SkPMColor c0 = *base.getAddr32(x, y); |
| 149 | SkPMColor c1 = *bm->getAddr32(x, y); |
| 150 | if (c0 != c1) { |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame^] | 151 | SkDebugf("----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n", |
| 152 | modeDescriptor, name.c_str(), x, y, c0, c1); |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 153 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 154 | if (diff) { |
| 155 | diff->setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 156 | diff->allocPixels(); |
| 157 | compute_diff(*bm, base, diff); |
| 158 | } |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 159 | return false; |
reed@android.com | b9b9a18 | 2009-07-08 02:54:47 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | } |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 163 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 164 | // they're equal |
| 165 | return true; |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 166 | } |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 167 | |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 168 | static bool write_pdf(const SkString& path, const SkDynamicMemoryWStream& pdf) { |
| 169 | SkFILEWStream stream(path.c_str()); |
| 170 | return stream.write(pdf.getStream(), pdf.getOffset()); |
reed@google.com | 0770044 | 2010-12-20 19:46:07 +0000 | [diff] [blame] | 171 | } |
| 172 | |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 173 | enum Backend { |
| 174 | kRaster_Backend, |
| 175 | kGPU_Backend, |
| 176 | kPDF_Backend, |
| 177 | }; |
| 178 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 179 | struct ConfigData { |
| 180 | SkBitmap::Config fConfig; |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 181 | Backend fBackend; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 182 | const char* fName; |
| 183 | }; |
| 184 | |
| 185 | /// Returns true if processing should continue, false to skip the |
| 186 | /// remainder of this config for this GM. |
| 187 | //@todo thudson 22 April 2011 - could refactor this to take in |
| 188 | // a factory to generate the context, always call readPixels() |
| 189 | // (logically a noop for rasters, if wasted time), and thus collapse the |
| 190 | // GPU special case and also let this be used for SkPicture testing. |
| 191 | static void setup_bitmap(const ConfigData& gRec, SkISize& size, |
| 192 | SkBitmap* bitmap) { |
| 193 | bitmap->setConfig(gRec.fConfig, size.width(), size.height()); |
| 194 | bitmap->allocPixels(); |
| 195 | bitmap->eraseColor(0); |
| 196 | } |
| 197 | |
| 198 | static bool generate_image(GM* gm, const ConfigData& gRec, |
| 199 | GrContext* context, |
| 200 | SkBitmap& bitmap) { |
| 201 | SkISize size (gm->getISize()); |
| 202 | setup_bitmap(gRec, size, &bitmap); |
| 203 | SkCanvas canvas(bitmap); |
| 204 | |
| 205 | if (gRec.fBackend == kRaster_Backend) { |
| 206 | gm->draw(&canvas); |
| 207 | } else { // GPU |
| 208 | if (NULL == context) { |
| 209 | return false; |
| 210 | } |
| 211 | SkGpuCanvas gc(context, |
| 212 | SkGpuDevice::Current3DApiRenderTarget()); |
| 213 | gc.setDevice(gc.createDevice(bitmap.config(), |
| 214 | bitmap.width(), |
| 215 | bitmap.height(), |
| 216 | bitmap.isOpaque(), |
| 217 | false))->unref(); |
| 218 | gm->draw(&gc); |
| 219 | gc.readPixels(&bitmap); // overwrite our previous allocation |
| 220 | } |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | static void generate_image_from_picture(GM* gm, const ConfigData& gRec, |
| 225 | SkPicture* pict, SkBitmap* bitmap) { |
| 226 | SkISize size = gm->getISize(); |
| 227 | setup_bitmap(gRec, size, bitmap); |
| 228 | SkCanvas canvas(*bitmap); |
| 229 | canvas.drawPicture(*pict); |
| 230 | } |
| 231 | |
| 232 | static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) { |
| 233 | #ifdef SK_SUPPORT_PDF |
| 234 | SkISize size = gm->getISize(); |
| 235 | SkMatrix identity; |
| 236 | identity.reset(); |
| 237 | SkPDFDevice* dev = new SkPDFDevice(size.width(), size.height(), |
| 238 | identity); |
| 239 | SkAutoUnref aur(dev); |
| 240 | |
| 241 | SkCanvas c(dev); |
| 242 | gm->draw(&c); |
| 243 | |
| 244 | SkPDFDocument doc; |
| 245 | doc.appendPage(dev); |
| 246 | doc.emitPDF(&pdf); |
| 247 | #endif |
| 248 | } |
| 249 | |
| 250 | static void write_reference_image(const ConfigData& gRec, |
| 251 | const char writePath [], |
| 252 | const char writePathSuffix [], |
| 253 | const SkString& name, |
| 254 | SkBitmap& bitmap, |
| 255 | SkDynamicMemoryWStream* pdf) { |
| 256 | SkString path; |
| 257 | bool success = false; |
| 258 | if (gRec.fBackend != kPDF_Backend) { |
| 259 | path = make_filename(writePath, writePathSuffix, name, "png"); |
| 260 | success = write_bitmap(path, bitmap); |
| 261 | } else if (pdf) { |
| 262 | path = make_filename(writePath, writePathSuffix, name, "pdf"); |
| 263 | success = write_pdf(path, *pdf); |
| 264 | } |
| 265 | if (!success) { |
| 266 | fprintf(stderr, "FAILED to write %s\n", path.c_str()); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | static void compare_to_reference_image(const char readPath [], |
| 271 | const SkString& name, |
| 272 | SkBitmap &bitmap, |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame^] | 273 | const char diffPath [], |
| 274 | const char modeDescriptor []) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 275 | SkString path = make_filename(readPath, "", name, "png"); |
| 276 | SkBitmap orig; |
| 277 | bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig, |
| 278 | SkBitmap::kARGB_8888_Config, |
| 279 | SkImageDecoder::kDecodePixels_Mode, NULL); |
| 280 | if (success) { |
| 281 | SkBitmap diffBitmap; |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame^] | 282 | success = compare(bitmap, orig, name, modeDescriptor, |
| 283 | diffPath ? &diffBitmap : NULL); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 284 | if (!success && diffPath) { |
| 285 | SkString diffName = make_filename(diffPath, "", name, ".diff.png"); |
| 286 | fprintf(stderr, "Writing %s\n", diffName.c_str()); |
| 287 | write_bitmap(diffName, diffBitmap); |
| 288 | } |
| 289 | } else { |
| 290 | fprintf(stderr, "FAILED to read %s\n", path.c_str()); |
| 291 | } |
| 292 | |
| 293 | } |
| 294 | |
| 295 | static void handle_test_results(GM* gm, |
| 296 | const ConfigData& gRec, |
| 297 | const char writePath [], |
| 298 | const char readPath [], |
| 299 | const char diffPath [], |
| 300 | const char writePathSuffix [], |
| 301 | SkBitmap& bitmap, |
| 302 | SkDynamicMemoryWStream* pdf) { |
| 303 | SkString name = make_name(gm->shortName(), gRec.fName); |
| 304 | |
| 305 | if (writePath) { |
| 306 | write_reference_image(gRec, writePath, writePathSuffix, |
| 307 | name, bitmap, pdf); |
| 308 | // TODO: Figure out a way to compare PDFs. |
| 309 | } else if (readPath && gRec.fBackend != kPDF_Backend) { |
| 310 | compare_to_reference_image(readPath, name, bitmap, |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame^] | 311 | diffPath, writePathSuffix); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | static SkPicture* generate_new_picture(GM* gm) { |
| 316 | // Pictures are refcounted so must be on heap |
| 317 | SkPicture* pict = new SkPicture; |
| 318 | SkCanvas* cv = pict->beginRecording(1000, 1000); |
| 319 | gm->draw(cv); |
| 320 | pict->endRecording(); |
| 321 | |
| 322 | return pict; |
| 323 | } |
| 324 | |
| 325 | static SkPicture* stream_to_new_picture(const SkPicture& src) { |
| 326 | |
| 327 | // To do in-memory commiunications with a stream, we need to: |
| 328 | // * create a dynamic memory stream |
| 329 | // * copy it into a buffer |
| 330 | // * create a read stream from it |
| 331 | // ?!?! |
| 332 | |
| 333 | SkDynamicMemoryWStream storage; |
| 334 | src.serialize(&storage); |
| 335 | |
| 336 | int streamSize = storage.getOffset(); |
| 337 | SkAutoMalloc dstStorage(streamSize); |
| 338 | void* dst = dstStorage.get(); |
| 339 | //char* dst = new char [streamSize]; |
| 340 | //@todo thudson 22 April 2011 when can we safely delete [] dst? |
| 341 | storage.copyTo(dst); |
| 342 | SkMemoryStream pictReadback(dst, streamSize); |
| 343 | SkPicture* retval = new SkPicture (&pictReadback); |
| 344 | return retval; |
| 345 | } |
| 346 | |
| 347 | // Test: draw into a bitmap or pdf. |
| 348 | // Depending on flags, possibly compare to an expected image |
| 349 | // and possibly output a diff image if it fails to match. |
| 350 | static void test_drawing(GM* gm, |
| 351 | const ConfigData& gRec, |
| 352 | const char writePath [], |
| 353 | const char readPath [], |
| 354 | const char diffPath [], |
| 355 | GrContext* context) { |
| 356 | SkBitmap bitmap; |
| 357 | SkDynamicMemoryWStream pdf; |
| 358 | |
| 359 | if (gRec.fBackend == kRaster_Backend || |
| 360 | gRec.fBackend == kGPU_Backend) { |
| 361 | if (!generate_image(gm, gRec, context, bitmap)) { |
| 362 | return; |
| 363 | } |
| 364 | } |
| 365 | // TODO: Figure out a way to compare PDFs. |
| 366 | if (gRec.fBackend == kPDF_Backend && writePath) { |
| 367 | generate_pdf(gm, pdf); |
| 368 | } |
| 369 | handle_test_results(gm, gRec, writePath, readPath, diffPath, |
| 370 | "", bitmap, &pdf); |
| 371 | } |
| 372 | |
| 373 | static void test_picture_playback(GM* gm, |
| 374 | const ConfigData& gRec, |
| 375 | const char writePath [], |
| 376 | const char readPath [], |
| 377 | const char diffPath []) { |
| 378 | SkPicture* pict = generate_new_picture(gm); |
| 379 | SkAutoUnref aur(pict); |
| 380 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 381 | if (kRaster_Backend == gRec.fBackend) { |
| 382 | SkBitmap bitmap; |
| 383 | generate_image_from_picture(gm, gRec, pict, &bitmap); |
| 384 | handle_test_results(gm, gRec, writePath, readPath, diffPath, |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame^] | 385 | "-replay", bitmap, NULL); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
| 389 | static void test_picture_serialization(GM* gm, |
| 390 | const ConfigData& gRec, |
| 391 | const char writePath [], |
| 392 | const char readPath [], |
| 393 | const char diffPath []) { |
| 394 | SkPicture* pict = generate_new_picture(gm); |
| 395 | SkAutoUnref aurp(pict); |
| 396 | SkPicture* repict = stream_to_new_picture(*pict); |
| 397 | SkAutoUnref aurr(repict); |
| 398 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 399 | if (kRaster_Backend == gRec.fBackend) { |
| 400 | SkBitmap bitmap; |
| 401 | generate_image_from_picture(gm, gRec, repict, &bitmap); |
| 402 | handle_test_results(gm, gRec, writePath, readPath, diffPath, |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame^] | 403 | "-serialize", bitmap, NULL); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| 407 | static void usage(const char * argv0) { |
| 408 | SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0); |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 409 | SkDebugf(" [--replay] [--serialize]\n"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 410 | SkDebugf(" writePath: directory to write rendered images in.\n"); |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 411 | SkDebugf( |
| 412 | " readPath: directory to read reference images from;\n" |
| 413 | " reports if any pixels mismatch between reference and new images\n"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 414 | SkDebugf(" diffPath: directory to write difference images in.\n"); |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 415 | SkDebugf(" --replay: exercise SkPicture replay.\n"); |
| 416 | SkDebugf( |
| 417 | " --serialize: exercise SkPicture serialization & deserialization.\n"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | static const ConfigData gRec[] = { |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 421 | { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" }, |
| 422 | { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" }, |
| 423 | { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" }, |
| 424 | { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" }, |
| 425 | #ifdef SK_SUPPORT_PDF |
| 426 | { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" }, |
| 427 | #endif |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 428 | }; |
| 429 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 430 | int main(int argc, char * const argv[]) { |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 431 | SkAutoGraphics ag; |
reed@google.com | d4dfd10 | 2011-01-18 21:05:42 +0000 | [diff] [blame] | 432 | |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 433 | const char* writePath = NULL; // if non-null, where we write the originals |
| 434 | 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] | 435 | 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] | 436 | |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 437 | bool doReplay = false; |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame^] | 438 | bool doSerialize = false; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 439 | const char* const commandName = argv[0]; |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 440 | char* const* stop = argv + argc; |
| 441 | for (++argv; argv < stop; ++argv) { |
| 442 | if (strcmp(*argv, "-w") == 0) { |
| 443 | argv++; |
| 444 | if (argv < stop && **argv) { |
| 445 | writePath = *argv; |
| 446 | } |
| 447 | } else if (strcmp(*argv, "-r") == 0) { |
| 448 | argv++; |
| 449 | if (argv < stop && **argv) { |
| 450 | readPath = *argv; |
| 451 | } |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 452 | } else if (strcmp(*argv, "-d") == 0) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 453 | argv++; |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 454 | if (argv < stop && **argv) { |
| 455 | diffPath = *argv; |
| 456 | } |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 457 | } else if (strcmp(*argv, "--replay") == 0) { |
| 458 | doReplay = true; |
| 459 | } else if (strcmp(*argv, "--serialize") == 0) { |
| 460 | doSerialize = true; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 461 | } else { |
| 462 | usage(commandName); |
| 463 | return 0; |
| 464 | } |
| 465 | } |
| 466 | if (argv != stop) { |
| 467 | usage(commandName); |
| 468 | return 0; |
| 469 | } |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 470 | |
| 471 | // setup a GL context for drawing offscreen |
reed@google.com | 37df17d | 2010-12-23 20:20:51 +0000 | [diff] [blame] | 472 | GrContext* context = NULL; |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 473 | SkEGLContext eglContext; |
reed@google.com | 37df17d | 2010-12-23 20:20:51 +0000 | [diff] [blame] | 474 | if (eglContext.init(1024, 1024)) { |
| 475 | context = GrContext::CreateGLShaderContext(); |
| 476 | } |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 477 | |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 478 | Iter iter; |
| 479 | GM* gm; |
reed@android.com | 00f883e | 2010-12-14 17:46:14 +0000 | [diff] [blame] | 480 | |
| 481 | if (readPath) { |
| 482 | fprintf(stderr, "reading from %s\n", readPath); |
| 483 | } else if (writePath) { |
| 484 | fprintf(stderr, "writing to %s\n", writePath); |
| 485 | } |
| 486 | |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 487 | while ((gm = iter.next()) != NULL) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 488 | SkISize size = gm->getISize(); |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 489 | SkDebugf("drawing... %s [%d %d]\n", gm->shortName(), |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 490 | size.width(), size.height()); |
| 491 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 492 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) { |
| 493 | test_drawing(gm, gRec[i], |
| 494 | writePath, readPath, diffPath, context); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 495 | |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 496 | if (doReplay) { |
| 497 | test_picture_playback(gm, gRec[i], |
| 498 | writePath, readPath, diffPath); |
| 499 | } |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 500 | |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 501 | if (doSerialize) { |
| 502 | test_picture_serialization(gm, gRec[i], |
| 503 | writePath, readPath, diffPath); |
| 504 | } |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 505 | } |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 506 | SkDELETE(gm); |
| 507 | } |
| 508 | return 0; |
| 509 | } |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 510 | |
| 511 | /////////////////////////////////////////////////////////////////////////////// |
| 512 | |
| 513 | using namespace skiagm; |
| 514 | |
| 515 | GM::GM() {} |
| 516 | GM::~GM() {} |
| 517 | |
| 518 | void GM::draw(SkCanvas* canvas) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 519 | this->onDraw(canvas); |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 520 | } |