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