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 |
tomhudson@google.com | a87cd2a | 2011-06-15 16:50:27 +0000 | [diff] [blame] | 24 | template GMRegistry* SkTRegistry<GM*, void*>::gHead; |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 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 | } |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame^] | 219 | |
| 220 | // not a real object, so don't unref it |
| 221 | GrRenderTarget* rt = SkGpuDevice::Current3DApiRenderTarget(); |
| 222 | SkGpuCanvas gc(context, rt); |
| 223 | gc.setDevice(new SkGpuDevice(context, rt))->unref(); |
| 224 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 225 | gm->draw(&gc); |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame^] | 226 | // the device is as large as the current rendertarget, so we explicitly |
| 227 | // only readback the amount we expect (in size) |
| 228 | gc.readPixels(SkIRect::MakeSize(size), &bitmap); // overwrite our previous allocation |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 229 | } |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | static void generate_image_from_picture(GM* gm, const ConfigData& gRec, |
| 234 | SkPicture* pict, SkBitmap* bitmap) { |
| 235 | SkISize size = gm->getISize(); |
| 236 | setup_bitmap(gRec, size, bitmap); |
| 237 | SkCanvas canvas(*bitmap); |
| 238 | canvas.drawPicture(*pict); |
| 239 | } |
| 240 | |
| 241 | static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) { |
| 242 | #ifdef SK_SUPPORT_PDF |
| 243 | SkISize size = gm->getISize(); |
| 244 | SkMatrix identity; |
| 245 | identity.reset(); |
ctguil@chromium.org | 1526129 | 2011-04-29 17:54:16 +0000 | [diff] [blame] | 246 | SkPDFDevice* dev = new SkPDFDevice(size, size, identity); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 247 | SkAutoUnref aur(dev); |
| 248 | |
| 249 | SkCanvas c(dev); |
| 250 | gm->draw(&c); |
| 251 | |
| 252 | SkPDFDocument doc; |
| 253 | doc.appendPage(dev); |
| 254 | doc.emitPDF(&pdf); |
| 255 | #endif |
| 256 | } |
| 257 | |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 258 | static bool write_reference_image(const ConfigData& gRec, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 259 | const char writePath [], |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 260 | const char renderModeDescriptor [], |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 261 | const SkString& name, |
| 262 | SkBitmap& bitmap, |
| 263 | SkDynamicMemoryWStream* pdf) { |
| 264 | SkString path; |
| 265 | bool success = false; |
| 266 | if (gRec.fBackend != kPDF_Backend) { |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 267 | path = make_filename(writePath, renderModeDescriptor, name, "png"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 268 | success = write_bitmap(path, bitmap); |
| 269 | } else if (pdf) { |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 270 | path = make_filename(writePath, renderModeDescriptor, name, "pdf"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 271 | success = write_pdf(path, *pdf); |
| 272 | } |
| 273 | if (!success) { |
| 274 | fprintf(stderr, "FAILED to write %s\n", path.c_str()); |
| 275 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 276 | return success; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 277 | } |
| 278 | |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 279 | static bool compare_to_reference_image(const char readPath [], |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 280 | const SkString& name, |
| 281 | SkBitmap &bitmap, |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame] | 282 | const char diffPath [], |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 283 | const char renderModeDescriptor []) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 284 | SkString path = make_filename(readPath, "", name, "png"); |
| 285 | SkBitmap orig; |
| 286 | bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig, |
| 287 | SkBitmap::kARGB_8888_Config, |
| 288 | SkImageDecoder::kDecodePixels_Mode, NULL); |
| 289 | if (success) { |
| 290 | SkBitmap diffBitmap; |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 291 | success = compare(bitmap, orig, name, renderModeDescriptor, |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame] | 292 | diffPath ? &diffBitmap : NULL); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 293 | if (!success && diffPath) { |
| 294 | SkString diffName = make_filename(diffPath, "", name, ".diff.png"); |
| 295 | fprintf(stderr, "Writing %s\n", diffName.c_str()); |
| 296 | write_bitmap(diffName, diffBitmap); |
| 297 | } |
| 298 | } else { |
| 299 | fprintf(stderr, "FAILED to read %s\n", path.c_str()); |
| 300 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 301 | return success; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 302 | } |
| 303 | |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 304 | static bool handle_test_results(GM* gm, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 305 | const ConfigData& gRec, |
| 306 | const char writePath [], |
| 307 | const char readPath [], |
| 308 | const char diffPath [], |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 309 | const char renderModeDescriptor [], |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 310 | SkBitmap& bitmap, |
| 311 | SkDynamicMemoryWStream* pdf) { |
| 312 | SkString name = make_name(gm->shortName(), gRec.fName); |
| 313 | |
| 314 | if (writePath) { |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 315 | write_reference_image(gRec, writePath, renderModeDescriptor, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 316 | name, bitmap, pdf); |
| 317 | // TODO: Figure out a way to compare PDFs. |
| 318 | } else if (readPath && gRec.fBackend != kPDF_Backend) { |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 319 | return compare_to_reference_image(readPath, name, bitmap, |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 320 | diffPath, renderModeDescriptor); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 321 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 322 | return true; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static SkPicture* generate_new_picture(GM* gm) { |
| 326 | // Pictures are refcounted so must be on heap |
| 327 | SkPicture* pict = new SkPicture; |
| 328 | SkCanvas* cv = pict->beginRecording(1000, 1000); |
| 329 | gm->draw(cv); |
| 330 | pict->endRecording(); |
| 331 | |
| 332 | return pict; |
| 333 | } |
| 334 | |
| 335 | static SkPicture* stream_to_new_picture(const SkPicture& src) { |
| 336 | |
| 337 | // To do in-memory commiunications with a stream, we need to: |
| 338 | // * create a dynamic memory stream |
| 339 | // * copy it into a buffer |
| 340 | // * create a read stream from it |
| 341 | // ?!?! |
| 342 | |
| 343 | SkDynamicMemoryWStream storage; |
| 344 | src.serialize(&storage); |
| 345 | |
| 346 | int streamSize = storage.getOffset(); |
| 347 | SkAutoMalloc dstStorage(streamSize); |
| 348 | void* dst = dstStorage.get(); |
| 349 | //char* dst = new char [streamSize]; |
| 350 | //@todo thudson 22 April 2011 when can we safely delete [] dst? |
| 351 | storage.copyTo(dst); |
| 352 | SkMemoryStream pictReadback(dst, streamSize); |
| 353 | SkPicture* retval = new SkPicture (&pictReadback); |
| 354 | return retval; |
| 355 | } |
| 356 | |
| 357 | // Test: draw into a bitmap or pdf. |
| 358 | // Depending on flags, possibly compare to an expected image |
| 359 | // and possibly output a diff image if it fails to match. |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 360 | static bool test_drawing(GM* gm, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 361 | const ConfigData& gRec, |
| 362 | const char writePath [], |
| 363 | const char readPath [], |
| 364 | const char diffPath [], |
| 365 | GrContext* context) { |
| 366 | SkBitmap bitmap; |
| 367 | SkDynamicMemoryWStream pdf; |
| 368 | |
| 369 | if (gRec.fBackend == kRaster_Backend || |
| 370 | gRec.fBackend == kGPU_Backend) { |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 371 | // Early exit if we can't generate the image, but this is |
| 372 | // expected in some cases, so don't report a test failure. |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 373 | if (!generate_image(gm, gRec, context, bitmap)) { |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 374 | return true; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | // TODO: Figure out a way to compare PDFs. |
| 378 | if (gRec.fBackend == kPDF_Backend && writePath) { |
| 379 | generate_pdf(gm, pdf); |
| 380 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 381 | return handle_test_results(gm, gRec, writePath, readPath, diffPath, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 382 | "", bitmap, &pdf); |
| 383 | } |
| 384 | |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 385 | static bool test_picture_playback(GM* gm, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 386 | const ConfigData& gRec, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 387 | const char readPath [], |
| 388 | const char diffPath []) { |
| 389 | SkPicture* pict = generate_new_picture(gm); |
| 390 | SkAutoUnref aur(pict); |
| 391 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 392 | if (kRaster_Backend == gRec.fBackend) { |
| 393 | SkBitmap bitmap; |
| 394 | generate_image_from_picture(gm, gRec, pict, &bitmap); |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 395 | return handle_test_results(gm, gRec, NULL, readPath, diffPath, |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame] | 396 | "-replay", bitmap, NULL); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 397 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 398 | return true; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 399 | } |
| 400 | |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 401 | static bool test_picture_serialization(GM* gm, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 402 | const ConfigData& gRec, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 403 | const char readPath [], |
| 404 | const char diffPath []) { |
| 405 | SkPicture* pict = generate_new_picture(gm); |
| 406 | SkAutoUnref aurp(pict); |
| 407 | SkPicture* repict = stream_to_new_picture(*pict); |
| 408 | SkAutoUnref aurr(repict); |
| 409 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 410 | if (kRaster_Backend == gRec.fBackend) { |
| 411 | SkBitmap bitmap; |
| 412 | generate_image_from_picture(gm, gRec, repict, &bitmap); |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 413 | return handle_test_results(gm, gRec, NULL, readPath, diffPath, |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame] | 414 | "-serialize", bitmap, NULL); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 415 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 416 | return true; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static void usage(const char * argv0) { |
| 420 | SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0); |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 421 | SkDebugf(" [--replay] [--serialize]\n"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 422 | SkDebugf(" writePath: directory to write rendered images in.\n"); |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 423 | SkDebugf( |
| 424 | " readPath: directory to read reference images from;\n" |
| 425 | " reports if any pixels mismatch between reference and new images\n"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 426 | SkDebugf(" diffPath: directory to write difference images in.\n"); |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 427 | SkDebugf(" --replay: exercise SkPicture replay.\n"); |
| 428 | SkDebugf( |
| 429 | " --serialize: exercise SkPicture serialization & deserialization.\n"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static const ConfigData gRec[] = { |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 433 | { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" }, |
| 434 | { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" }, |
| 435 | { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" }, |
| 436 | { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" }, |
| 437 | #ifdef SK_SUPPORT_PDF |
| 438 | { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" }, |
| 439 | #endif |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 440 | }; |
| 441 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 442 | int main(int argc, char * const argv[]) { |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 443 | SkAutoGraphics ag; |
reed@google.com | d4dfd10 | 2011-01-18 21:05:42 +0000 | [diff] [blame] | 444 | |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 445 | const char* writePath = NULL; // if non-null, where we write the originals |
| 446 | 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] | 447 | 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] | 448 | |
reed@google.com | b8b0983 | 2011-05-26 15:57:56 +0000 | [diff] [blame] | 449 | bool doReplay = true; |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame] | 450 | bool doSerialize = false; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 451 | const char* const commandName = argv[0]; |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 452 | char* const* stop = argv + argc; |
| 453 | for (++argv; argv < stop; ++argv) { |
| 454 | if (strcmp(*argv, "-w") == 0) { |
| 455 | argv++; |
| 456 | if (argv < stop && **argv) { |
| 457 | writePath = *argv; |
| 458 | } |
| 459 | } else if (strcmp(*argv, "-r") == 0) { |
| 460 | argv++; |
| 461 | if (argv < stop && **argv) { |
| 462 | readPath = *argv; |
| 463 | } |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 464 | } else if (strcmp(*argv, "-d") == 0) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 465 | argv++; |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 466 | if (argv < stop && **argv) { |
| 467 | diffPath = *argv; |
| 468 | } |
reed@google.com | b8b0983 | 2011-05-26 15:57:56 +0000 | [diff] [blame] | 469 | } else if (strcmp(*argv, "--noreplay") == 0) { |
| 470 | doReplay = false; |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 471 | } else if (strcmp(*argv, "--serialize") == 0) { |
| 472 | doSerialize = true; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 473 | } else { |
| 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 | } |
| 477 | } |
| 478 | if (argv != stop) { |
| 479 | usage(commandName); |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 480 | return -1; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 481 | } |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 482 | |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 483 | int maxW = -1; |
| 484 | int maxH = -1; |
| 485 | Iter iter; |
| 486 | GM* gm; |
| 487 | while ((gm = iter.next()) != NULL) { |
| 488 | SkISize size = gm->getISize(); |
| 489 | maxW = SkMax32(size.width(), maxW); |
| 490 | maxH = SkMax32(size.height(), maxH); |
| 491 | } |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 492 | // setup a GL context for drawing offscreen |
reed@google.com | 37df17d | 2010-12-23 20:20:51 +0000 | [diff] [blame] | 493 | GrContext* context = NULL; |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 494 | SkEGLContext eglContext; |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 495 | if (eglContext.init(maxW, maxH)) { |
reed@google.com | 37df17d | 2010-12-23 20:20:51 +0000 | [diff] [blame] | 496 | context = GrContext::CreateGLShaderContext(); |
| 497 | } |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 498 | |
reed@android.com | 00f883e | 2010-12-14 17:46:14 +0000 | [diff] [blame] | 499 | |
| 500 | if (readPath) { |
| 501 | fprintf(stderr, "reading from %s\n", readPath); |
| 502 | } else if (writePath) { |
| 503 | fprintf(stderr, "writing to %s\n", writePath); |
| 504 | } |
| 505 | |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 506 | // Accumulate success of all tests so we can flag error in any |
| 507 | // one with the return value. |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 508 | iter.reset(); |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 509 | bool overallSuccess = true; |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 510 | while ((gm = iter.next()) != NULL) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 511 | SkISize size = gm->getISize(); |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 512 | SkDebugf("drawing... %s [%d %d]\n", gm->shortName(), |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 513 | size.width(), size.height()); |
| 514 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 515 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) { |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 516 | bool testSuccess = test_drawing(gm, gRec[i], |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 517 | writePath, readPath, diffPath, context); |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 518 | overallSuccess &= testSuccess; |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 519 | |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 520 | if (doReplay && testSuccess) { |
| 521 | testSuccess = test_picture_playback(gm, gRec[i], |
| 522 | readPath, diffPath); |
| 523 | overallSuccess &= testSuccess; |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 524 | } |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 525 | |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 526 | if (doSerialize && testSuccess) { |
| 527 | overallSuccess &= test_picture_serialization(gm, gRec[i], |
| 528 | readPath, diffPath); |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 529 | } |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 530 | } |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 531 | SkDELETE(gm); |
| 532 | } |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 533 | if (false == overallSuccess) { |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 534 | return -1; |
| 535 | } |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 536 | return 0; |
| 537 | } |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 538 | |
| 539 | /////////////////////////////////////////////////////////////////////////////// |
| 540 | |
| 541 | using namespace skiagm; |
| 542 | |
| 543 | GM::GM() {} |
| 544 | GM::~GM() {} |
| 545 | |
| 546 | void GM::draw(SkCanvas* canvas) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 547 | this->onDraw(canvas); |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 548 | } |