epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 7 | |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 8 | #include "gm.h" |
epoger@google.com | 7bc13a6 | 2012-02-14 14:53:59 +0000 | [diff] [blame] | 9 | #include "system_preferences.h" |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 10 | #include "GrContext.h" |
| 11 | #include "GrRenderTarget.h" |
| 12 | |
reed@android.com | b9b9a18 | 2009-07-08 02:54:47 +0000 | [diff] [blame] | 13 | #include "SkColorPriv.h" |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 14 | #include "SkData.h" |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 15 | #include "SkDeferredCanvas.h" |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 16 | #include "SkDevice.h" |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 17 | #include "SkGpuCanvas.h" |
| 18 | #include "SkGpuDevice.h" |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 19 | #include "SkGraphics.h" |
| 20 | #include "SkImageDecoder.h" |
| 21 | #include "SkImageEncoder.h" |
tomhudson@google.com | 6bf38b5 | 2012-02-14 15:11:59 +0000 | [diff] [blame] | 22 | #include "gl/SkNativeGLContext.h" |
| 23 | #include "gl/SkMesaGLContext.h" |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 24 | #include "SkPicture.h" |
reed@google.com | 0770044 | 2010-12-20 19:46:07 +0000 | [diff] [blame] | 25 | #include "SkStream.h" |
| 26 | #include "SkRefCnt.h" |
| 27 | |
mike@reedtribe.org | 10afbef | 2011-12-30 16:02:53 +0000 | [diff] [blame] | 28 | static bool gForceBWtext; |
| 29 | |
reed@google.com | 8923c6c | 2011-11-08 14:59:38 +0000 | [diff] [blame] | 30 | extern bool gSkSuppressFontCachePurgeSpew; |
| 31 | |
reed@google.com | 0770044 | 2010-12-20 19:46:07 +0000 | [diff] [blame] | 32 | #ifdef SK_SUPPORT_PDF |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 33 | #include "SkPDFDevice.h" |
| 34 | #include "SkPDFDocument.h" |
reed@google.com | 0770044 | 2010-12-20 19:46:07 +0000 | [diff] [blame] | 35 | #endif |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 36 | |
epoger@google.com | e3cc2eb | 2012-01-18 20:11:13 +0000 | [diff] [blame] | 37 | // Until we resolve http://code.google.com/p/skia/issues/detail?id=455 , |
| 38 | // stop writing out XPS-format image baselines in gm. |
| 39 | #undef SK_SUPPORT_XPS |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 40 | #ifdef SK_SUPPORT_XPS |
| 41 | #include "SkXPSDevice.h" |
| 42 | #endif |
| 43 | |
reed@google.com | 46cce91 | 2011-06-29 12:54:46 +0000 | [diff] [blame] | 44 | #ifdef SK_BUILD_FOR_MAC |
| 45 | #include "SkCGUtils.h" |
bsalomon@google.com | 0a09eef | 2011-06-29 19:42:58 +0000 | [diff] [blame] | 46 | #define CAN_IMAGE_PDF 1 |
reed@google.com | 46cce91 | 2011-06-29 12:54:46 +0000 | [diff] [blame] | 47 | #else |
bsalomon@google.com | 0a09eef | 2011-06-29 19:42:58 +0000 | [diff] [blame] | 48 | #define CAN_IMAGE_PDF 0 |
reed@google.com | 46cce91 | 2011-06-29 12:54:46 +0000 | [diff] [blame] | 49 | #endif |
| 50 | |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 51 | typedef int ErrorBitfield; |
| 52 | const static ErrorBitfield ERROR_NONE = 0x00; |
| 53 | const static ErrorBitfield ERROR_NO_GPU_CONTEXT = 0x01; |
| 54 | const static ErrorBitfield ERROR_PIXEL_MISMATCH = 0x02; |
| 55 | const static ErrorBitfield ERROR_DIMENSION_MISMATCH = 0x04; |
| 56 | const static ErrorBitfield ERROR_READING_REFERENCE_IMAGE = 0x08; |
| 57 | const static ErrorBitfield ERROR_WRITING_REFERENCE_IMAGE = 0x10; |
| 58 | |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 59 | using namespace skiagm; |
| 60 | |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 61 | class Iter { |
| 62 | public: |
| 63 | Iter() { |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 64 | this->reset(); |
| 65 | } |
| 66 | |
| 67 | void reset() { |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 68 | fReg = GMRegistry::Head(); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 69 | } |
reed@google.com | d4dfd10 | 2011-01-18 21:05:42 +0000 | [diff] [blame] | 70 | |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 71 | GM* next() { |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 72 | if (fReg) { |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 73 | GMRegistry::Factory fact = fReg->factory(); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 74 | fReg = fReg->next(); |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 75 | return fact(0); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 76 | } |
| 77 | return NULL; |
| 78 | } |
reed@google.com | d4dfd10 | 2011-01-18 21:05:42 +0000 | [diff] [blame] | 79 | |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 80 | static int Count() { |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 81 | const GMRegistry* reg = GMRegistry::Head(); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 82 | int count = 0; |
| 83 | while (reg) { |
| 84 | count += 1; |
| 85 | reg = reg->next(); |
| 86 | } |
| 87 | return count; |
| 88 | } |
reed@google.com | d4dfd10 | 2011-01-18 21:05:42 +0000 | [diff] [blame] | 89 | |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 90 | private: |
| 91 | const GMRegistry* fReg; |
| 92 | }; |
| 93 | |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 94 | static SkString make_name(const char shortName[], const char configName[]) { |
| 95 | SkString name(shortName); |
| 96 | name.appendf("_%s", configName); |
| 97 | return name; |
| 98 | } |
| 99 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 100 | static SkString make_filename(const char path[], |
| 101 | const char pathSuffix[], |
| 102 | const SkString& name, |
| 103 | const char suffix[]) { |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 104 | SkString filename(path); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 105 | if (filename.endsWith("/")) { |
| 106 | filename.remove(filename.size() - 1, 1); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 107 | } |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 108 | filename.append(pathSuffix); |
| 109 | filename.append("/"); |
reed@google.com | 0770044 | 2010-12-20 19:46:07 +0000 | [diff] [blame] | 110 | filename.appendf("%s.%s", name.c_str(), suffix); |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 111 | return filename; |
| 112 | } |
| 113 | |
reed@android.com | b9b9a18 | 2009-07-08 02:54:47 +0000 | [diff] [blame] | 114 | /* since PNG insists on unpremultiplying our alpha, we take no precision chances |
| 115 | and force all pixels to be 100% opaque, otherwise on compare we may not get |
| 116 | a perfect match. |
| 117 | */ |
| 118 | static void force_all_opaque(const SkBitmap& bitmap) { |
| 119 | SkAutoLockPixels lock(bitmap); |
| 120 | for (int y = 0; y < bitmap.height(); y++) { |
| 121 | for (int x = 0; x < bitmap.width(); x++) { |
| 122 | *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) { |
| 128 | SkBitmap copy; |
| 129 | bitmap.copyTo(©, SkBitmap::kARGB_8888_Config); |
| 130 | force_all_opaque(copy); |
| 131 | return SkImageEncoder::EncodeFile(path.c_str(), copy, |
| 132 | SkImageEncoder::kPNG_Type, 100); |
| 133 | } |
| 134 | |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 135 | static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 136 | int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1); |
| 137 | int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1); |
| 138 | int db = SkGetPackedB32(c0) - SkGetPackedB32(c1); |
| 139 | return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db)); |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static void compute_diff(const SkBitmap& target, const SkBitmap& base, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 143 | SkBitmap* diff) { |
| 144 | SkAutoLockPixels alp(*diff); |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 145 | |
| 146 | const int w = target.width(); |
| 147 | const int h = target.height(); |
| 148 | for (int y = 0; y < h; y++) { |
| 149 | for (int x = 0; x < w; x++) { |
| 150 | SkPMColor c0 = *base.getAddr32(x, y); |
| 151 | SkPMColor c1 = *target.getAddr32(x, y); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 152 | SkPMColor d = 0; |
| 153 | if (c0 != c1) { |
| 154 | d = compute_diff_pmcolor(c0, c1); |
| 155 | } |
| 156 | *diff->getAddr32(x, y) = d; |
| 157 | } |
| 158 | } |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 159 | } |
| 160 | |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 161 | static ErrorBitfield compare(const SkBitmap& target, const SkBitmap& base, |
| 162 | const SkString& name, |
| 163 | const char* renderModeDescriptor, |
| 164 | SkBitmap* diff) { |
reed@android.com | b9b9a18 | 2009-07-08 02:54:47 +0000 | [diff] [blame] | 165 | SkBitmap copy; |
| 166 | const SkBitmap* bm = ⌖ |
| 167 | if (target.config() != SkBitmap::kARGB_8888_Config) { |
| 168 | target.copyTo(©, SkBitmap::kARGB_8888_Config); |
| 169 | bm = © |
| 170 | } |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 171 | SkBitmap baseCopy; |
| 172 | const SkBitmap* bp = &base; |
| 173 | if (base.config() != SkBitmap::kARGB_8888_Config) { |
| 174 | base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config); |
| 175 | bp = &baseCopy; |
| 176 | } |
reed@android.com | b9b9a18 | 2009-07-08 02:54:47 +0000 | [diff] [blame] | 177 | |
| 178 | force_all_opaque(*bm); |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 179 | force_all_opaque(*bp); |
reed@android.com | b9b9a18 | 2009-07-08 02:54:47 +0000 | [diff] [blame] | 180 | |
| 181 | const int w = bm->width(); |
| 182 | const int h = bm->height(); |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 183 | if (w != bp->width() || h != bp->height()) { |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 184 | SkDebugf( |
| 185 | "---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n", |
| 186 | renderModeDescriptor, name.c_str(), |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 187 | bp->width(), bp->height(), w, h); |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 188 | return ERROR_DIMENSION_MISMATCH; |
reed@android.com | b9b9a18 | 2009-07-08 02:54:47 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | SkAutoLockPixels bmLock(*bm); |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 192 | SkAutoLockPixels baseLock(*bp); |
reed@android.com | b9b9a18 | 2009-07-08 02:54:47 +0000 | [diff] [blame] | 193 | |
| 194 | for (int y = 0; y < h; y++) { |
| 195 | for (int x = 0; x < w; x++) { |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 196 | SkPMColor c0 = *bp->getAddr32(x, y); |
reed@android.com | b9b9a18 | 2009-07-08 02:54:47 +0000 | [diff] [blame] | 197 | SkPMColor c1 = *bm->getAddr32(x, y); |
| 198 | if (c0 != c1) { |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 199 | SkDebugf( |
| 200 | "----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n", |
| 201 | renderModeDescriptor, name.c_str(), x, y, c0, c1); |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 202 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 203 | if (diff) { |
| 204 | diff->setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 205 | diff->allocPixels(); |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 206 | compute_diff(*bm, *bp, diff); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 207 | } |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 208 | return ERROR_PIXEL_MISMATCH; |
reed@android.com | b9b9a18 | 2009-07-08 02:54:47 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 212 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 213 | // they're equal |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 214 | return ERROR_NONE; |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 215 | } |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 216 | |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 217 | static bool write_document(const SkString& path, |
| 218 | const SkDynamicMemoryWStream& document) { |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 219 | SkFILEWStream stream(path.c_str()); |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 220 | SkAutoDataUnref data(document.copyToData()); |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 221 | return stream.writeData(data.get()); |
reed@google.com | 0770044 | 2010-12-20 19:46:07 +0000 | [diff] [blame] | 222 | } |
| 223 | |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 224 | enum Backend { |
| 225 | kRaster_Backend, |
| 226 | kGPU_Backend, |
| 227 | kPDF_Backend, |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 228 | kXPS_Backend, |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 229 | }; |
| 230 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 231 | struct ConfigData { |
| 232 | SkBitmap::Config fConfig; |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 233 | Backend fBackend; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 234 | const char* fName; |
| 235 | }; |
| 236 | |
| 237 | /// Returns true if processing should continue, false to skip the |
| 238 | /// remainder of this config for this GM. |
| 239 | //@todo thudson 22 April 2011 - could refactor this to take in |
| 240 | // a factory to generate the context, always call readPixels() |
| 241 | // (logically a noop for rasters, if wasted time), and thus collapse the |
| 242 | // GPU special case and also let this be used for SkPicture testing. |
| 243 | static void setup_bitmap(const ConfigData& gRec, SkISize& size, |
| 244 | SkBitmap* bitmap) { |
| 245 | bitmap->setConfig(gRec.fConfig, size.width(), size.height()); |
| 246 | bitmap->allocPixels(); |
| 247 | bitmap->eraseColor(0); |
| 248 | } |
| 249 | |
mike@reedtribe.org | 10afbef | 2011-12-30 16:02:53 +0000 | [diff] [blame] | 250 | #include "SkDrawFilter.h" |
| 251 | class BWTextDrawFilter : public SkDrawFilter { |
| 252 | public: |
| 253 | virtual void filter(SkPaint*, Type) SK_OVERRIDE; |
| 254 | }; |
| 255 | void BWTextDrawFilter::filter(SkPaint* p, Type t) { |
| 256 | if (kText_Type == t) { |
| 257 | p->setAntiAlias(false); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | static void installFilter(SkCanvas* canvas) { |
| 262 | if (gForceBWtext) { |
| 263 | canvas->setDrawFilter(new BWTextDrawFilter)->unref(); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | static void invokeGM(GM* gm, SkCanvas* canvas) { |
| 268 | installFilter(canvas); |
| 269 | gm->draw(canvas); |
| 270 | canvas->setDrawFilter(NULL); |
| 271 | } |
| 272 | |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 273 | static ErrorBitfield generate_image(GM* gm, const ConfigData& gRec, |
| 274 | GrContext* context, |
| 275 | GrRenderTarget* rt, |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 276 | SkBitmap* bitmap, |
| 277 | bool deferred) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 278 | SkISize size (gm->getISize()); |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 279 | setup_bitmap(gRec, size, bitmap); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 280 | |
| 281 | if (gRec.fBackend == kRaster_Backend) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 282 | SkCanvas* canvas; |
| 283 | if (deferred) { |
| 284 | canvas = new SkDeferredCanvas; |
| 285 | canvas->setDevice(new SkDevice(*bitmap))->unref(); |
| 286 | } else { |
| 287 | canvas = new SkCanvas(*bitmap); |
| 288 | } |
| 289 | SkAutoUnref canvasUnref(canvas); |
| 290 | invokeGM(gm, canvas); |
junov@chromium.org | bf6c1e4 | 2012-01-30 14:53:22 +0000 | [diff] [blame] | 291 | canvas->flush(); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 292 | } else { // GPU |
| 293 | if (NULL == context) { |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 294 | return ERROR_NO_GPU_CONTEXT; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 295 | } |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 296 | SkCanvas* gc; |
| 297 | if (deferred) { |
| 298 | gc = new SkDeferredCanvas; |
| 299 | } else { |
| 300 | gc = new SkGpuCanvas(context, rt); |
| 301 | } |
| 302 | SkAutoUnref gcUnref(gc); |
| 303 | gc->setDevice(new SkGpuDevice(context, rt))->unref(); |
| 304 | invokeGM(gm, gc); |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 305 | // the device is as large as the current rendertarget, so we explicitly |
| 306 | // only readback the amount we expect (in size) |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 307 | // overwrite our previous allocation |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 308 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth, |
| 309 | size.fHeight); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 310 | gc->readPixels(bitmap, 0, 0); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 311 | } |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 312 | return ERROR_NONE; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static void generate_image_from_picture(GM* gm, const ConfigData& gRec, |
| 316 | SkPicture* pict, SkBitmap* bitmap) { |
| 317 | SkISize size = gm->getISize(); |
| 318 | setup_bitmap(gRec, size, bitmap); |
| 319 | SkCanvas canvas(*bitmap); |
mike@reedtribe.org | 10afbef | 2011-12-30 16:02:53 +0000 | [diff] [blame] | 320 | installFilter(&canvas); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 321 | canvas.drawPicture(*pict); |
| 322 | } |
| 323 | |
| 324 | static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) { |
| 325 | #ifdef SK_SUPPORT_PDF |
| 326 | SkISize size = gm->getISize(); |
| 327 | SkMatrix identity; |
| 328 | identity.reset(); |
ctguil@chromium.org | 1526129 | 2011-04-29 17:54:16 +0000 | [diff] [blame] | 329 | SkPDFDevice* dev = new SkPDFDevice(size, size, identity); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 330 | SkAutoUnref aur(dev); |
| 331 | |
| 332 | SkCanvas c(dev); |
mike@reedtribe.org | 10afbef | 2011-12-30 16:02:53 +0000 | [diff] [blame] | 333 | invokeGM(gm, &c); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 334 | |
| 335 | SkPDFDocument doc; |
| 336 | doc.appendPage(dev); |
| 337 | doc.emitPDF(&pdf); |
| 338 | #endif |
| 339 | } |
| 340 | |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 341 | static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) { |
| 342 | #ifdef SK_SUPPORT_XPS |
| 343 | SkISize size = gm->getISize(); |
| 344 | |
| 345 | SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()), |
| 346 | SkIntToScalar(size.height())); |
bungeman@google.com | dc9a695 | 2012-01-05 16:56:29 +0000 | [diff] [blame] | 347 | static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254); |
| 348 | static const SkScalar upm = 72 * inchesPerMeter; |
| 349 | SkVector unitsPerMeter = SkPoint::Make(upm, upm); |
| 350 | static const SkScalar ppm = 200 * inchesPerMeter; |
| 351 | SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm); |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 352 | |
| 353 | SkXPSDevice* dev = new SkXPSDevice(); |
| 354 | SkAutoUnref aur(dev); |
| 355 | |
| 356 | SkCanvas c(dev); |
| 357 | dev->beginPortfolio(&xps); |
| 358 | dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize); |
mike@reedtribe.org | 10afbef | 2011-12-30 16:02:53 +0000 | [diff] [blame] | 359 | invokeGM(gm, &c); |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 360 | dev->endSheet(); |
| 361 | dev->endPortfolio(); |
| 362 | |
| 363 | #endif |
| 364 | } |
| 365 | |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 366 | static ErrorBitfield write_reference_image(const ConfigData& gRec, |
| 367 | const char writePath [], |
| 368 | const char renderModeDescriptor [], |
| 369 | const SkString& name, |
| 370 | SkBitmap& bitmap, |
| 371 | SkDynamicMemoryWStream* document) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 372 | SkString path; |
| 373 | bool success = false; |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 374 | if (gRec.fBackend == kRaster_Backend || |
| 375 | gRec.fBackend == kGPU_Backend || |
| 376 | (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) { |
| 377 | |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 378 | path = make_filename(writePath, renderModeDescriptor, name, "png"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 379 | success = write_bitmap(path, bitmap); |
reed@google.com | 46cce91 | 2011-06-29 12:54:46 +0000 | [diff] [blame] | 380 | } |
| 381 | if (kPDF_Backend == gRec.fBackend) { |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 382 | path = make_filename(writePath, renderModeDescriptor, name, "pdf"); |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 383 | success = write_document(path, *document); |
| 384 | } |
| 385 | if (kXPS_Backend == gRec.fBackend) { |
| 386 | path = make_filename(writePath, renderModeDescriptor, name, "xps"); |
| 387 | success = write_document(path, *document); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 388 | } |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 389 | if (success) { |
| 390 | return ERROR_NONE; |
| 391 | } else { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 392 | fprintf(stderr, "FAILED to write %s\n", path.c_str()); |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 393 | return ERROR_WRITING_REFERENCE_IMAGE; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 397 | static ErrorBitfield compare_to_reference_image(const SkString& name, |
| 398 | SkBitmap &bitmap, |
| 399 | const SkBitmap& comparisonBitmap, |
| 400 | const char diffPath [], |
| 401 | const char renderModeDescriptor []) { |
| 402 | ErrorBitfield errors; |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 403 | SkBitmap diffBitmap; |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 404 | errors = compare(bitmap, comparisonBitmap, name, renderModeDescriptor, |
| 405 | diffPath ? &diffBitmap : NULL); |
| 406 | if ((ERROR_NONE == errors) && diffPath) { |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 407 | SkString diffName = make_filename(diffPath, "", name, ".diff.png"); |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 408 | if (!write_bitmap(diffName, diffBitmap)) { |
| 409 | errors |= ERROR_WRITING_REFERENCE_IMAGE; |
| 410 | } |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 411 | } |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 412 | return errors; |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 413 | } |
| 414 | |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 415 | static ErrorBitfield compare_to_reference_image(const char readPath [], |
| 416 | const SkString& name, |
| 417 | SkBitmap &bitmap, |
| 418 | const char diffPath [], |
| 419 | const char renderModeDescriptor []) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 420 | SkString path = make_filename(readPath, "", name, "png"); |
| 421 | SkBitmap orig; |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 422 | if (SkImageDecoder::DecodeFile(path.c_str(), &orig, |
| 423 | SkBitmap::kARGB_8888_Config, |
| 424 | SkImageDecoder::kDecodePixels_Mode, NULL)) { |
| 425 | return compare_to_reference_image(name, bitmap, |
| 426 | orig, diffPath, |
| 427 | renderModeDescriptor); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 428 | } else { |
| 429 | fprintf(stderr, "FAILED to read %s\n", path.c_str()); |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 430 | return ERROR_READING_REFERENCE_IMAGE; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 431 | } |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 432 | } |
| 433 | |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 434 | static ErrorBitfield handle_test_results(GM* gm, |
| 435 | const ConfigData& gRec, |
| 436 | const char writePath [], |
| 437 | const char readPath [], |
| 438 | const char diffPath [], |
| 439 | const char renderModeDescriptor [], |
| 440 | SkBitmap& bitmap, |
| 441 | SkDynamicMemoryWStream* pdf, |
| 442 | const SkBitmap* comparisonBitmap) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 443 | SkString name = make_name(gm->shortName(), gRec.fName); |
| 444 | |
| 445 | if (writePath) { |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 446 | return write_reference_image(gRec, writePath, renderModeDescriptor, |
| 447 | name, bitmap, pdf); |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 448 | } else if (readPath && ( |
| 449 | gRec.fBackend == kRaster_Backend || |
| 450 | gRec.fBackend == kGPU_Backend || |
| 451 | (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF))) { |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 452 | return compare_to_reference_image(readPath, name, bitmap, |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 453 | diffPath, renderModeDescriptor); |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 454 | } else if (comparisonBitmap) { |
| 455 | return compare_to_reference_image(name, bitmap, |
| 456 | *comparisonBitmap, diffPath, |
| 457 | renderModeDescriptor); |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 458 | } else { |
| 459 | return ERROR_NONE; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | static SkPicture* generate_new_picture(GM* gm) { |
| 464 | // Pictures are refcounted so must be on heap |
| 465 | SkPicture* pict = new SkPicture; |
| 466 | SkCanvas* cv = pict->beginRecording(1000, 1000); |
mike@reedtribe.org | 10afbef | 2011-12-30 16:02:53 +0000 | [diff] [blame] | 467 | invokeGM(gm, cv); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 468 | pict->endRecording(); |
| 469 | |
| 470 | return pict; |
| 471 | } |
| 472 | |
| 473 | static SkPicture* stream_to_new_picture(const SkPicture& src) { |
| 474 | |
| 475 | // To do in-memory commiunications with a stream, we need to: |
| 476 | // * create a dynamic memory stream |
| 477 | // * copy it into a buffer |
| 478 | // * create a read stream from it |
| 479 | // ?!?! |
| 480 | |
| 481 | SkDynamicMemoryWStream storage; |
| 482 | src.serialize(&storage); |
| 483 | |
| 484 | int streamSize = storage.getOffset(); |
| 485 | SkAutoMalloc dstStorage(streamSize); |
| 486 | void* dst = dstStorage.get(); |
| 487 | //char* dst = new char [streamSize]; |
| 488 | //@todo thudson 22 April 2011 when can we safely delete [] dst? |
| 489 | storage.copyTo(dst); |
| 490 | SkMemoryStream pictReadback(dst, streamSize); |
| 491 | SkPicture* retval = new SkPicture (&pictReadback); |
| 492 | return retval; |
| 493 | } |
| 494 | |
| 495 | // Test: draw into a bitmap or pdf. |
| 496 | // Depending on flags, possibly compare to an expected image |
| 497 | // and possibly output a diff image if it fails to match. |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 498 | static ErrorBitfield test_drawing(GM* gm, |
| 499 | const ConfigData& gRec, |
| 500 | const char writePath [], |
| 501 | const char readPath [], |
| 502 | const char diffPath [], |
| 503 | GrContext* context, |
| 504 | GrRenderTarget* rt, |
| 505 | SkBitmap* bitmap) { |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 506 | SkDynamicMemoryWStream document; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 507 | |
| 508 | if (gRec.fBackend == kRaster_Backend || |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 509 | gRec.fBackend == kGPU_Backend) { |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 510 | // Early exit if we can't generate the image. |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 511 | ErrorBitfield errors = generate_image(gm, gRec, context, rt, bitmap, |
| 512 | false); |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 513 | if (ERROR_NONE != errors) { |
| 514 | return errors; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 515 | } |
reed@google.com | 46cce91 | 2011-06-29 12:54:46 +0000 | [diff] [blame] | 516 | } else if (gRec.fBackend == kPDF_Backend) { |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 517 | generate_pdf(gm, document); |
reed@google.com | 46cce91 | 2011-06-29 12:54:46 +0000 | [diff] [blame] | 518 | #if CAN_IMAGE_PDF |
bungeman@google.com | 0f1541f | 2011-10-10 13:47:06 +0000 | [diff] [blame] | 519 | SkAutoDataUnref data(document.copyToData()); |
reed@google.com | 46cce91 | 2011-06-29 12:54:46 +0000 | [diff] [blame] | 520 | SkMemoryStream stream(data.data(), data.size()); |
| 521 | SkPDFDocumentToBitmap(&stream, bitmap); |
| 522 | #endif |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 523 | } else if (gRec.fBackend == kXPS_Backend) { |
| 524 | generate_xps(gm, document); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 525 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 526 | return handle_test_results(gm, gRec, writePath, readPath, diffPath, |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 527 | "", *bitmap, &document, NULL); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 528 | } |
| 529 | |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 530 | static ErrorBitfield test_deferred_drawing(GM* gm, |
| 531 | const ConfigData& gRec, |
| 532 | const SkBitmap& comparisonBitmap, |
| 533 | const char diffPath [], |
| 534 | GrContext* context, |
| 535 | GrRenderTarget* rt) { |
| 536 | SkDynamicMemoryWStream document; |
| 537 | |
| 538 | if (gRec.fBackend == kRaster_Backend || |
| 539 | gRec.fBackend == kGPU_Backend) { |
| 540 | SkBitmap bitmap; |
| 541 | // Early exit if we can't generate the image, but this is |
| 542 | // expected in some cases, so don't report a test failure. |
| 543 | if (!generate_image(gm, gRec, context, rt, &bitmap, true)) { |
| 544 | return ERROR_NONE; |
| 545 | } |
| 546 | return handle_test_results(gm, gRec, NULL, NULL, diffPath, |
| 547 | "-deferred", bitmap, NULL, &comparisonBitmap); |
| 548 | } |
| 549 | return ERROR_NONE; |
| 550 | } |
| 551 | |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 552 | static ErrorBitfield test_picture_playback(GM* gm, |
| 553 | const ConfigData& gRec, |
| 554 | const SkBitmap& comparisonBitmap, |
| 555 | const char readPath [], |
| 556 | const char diffPath []) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 557 | SkPicture* pict = generate_new_picture(gm); |
| 558 | SkAutoUnref aur(pict); |
| 559 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 560 | if (kRaster_Backend == gRec.fBackend) { |
| 561 | SkBitmap bitmap; |
| 562 | generate_image_from_picture(gm, gRec, pict, &bitmap); |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 563 | return handle_test_results(gm, gRec, NULL, NULL, diffPath, |
| 564 | "-replay", bitmap, NULL, &comparisonBitmap); |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 565 | } else { |
| 566 | return ERROR_NONE; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 570 | static ErrorBitfield test_picture_serialization(GM* gm, |
| 571 | const ConfigData& gRec, |
| 572 | const SkBitmap& comparisonBitmap, |
| 573 | const char readPath [], |
| 574 | const char diffPath []) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 575 | SkPicture* pict = generate_new_picture(gm); |
| 576 | SkAutoUnref aurp(pict); |
| 577 | SkPicture* repict = stream_to_new_picture(*pict); |
| 578 | SkAutoUnref aurr(repict); |
| 579 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 580 | if (kRaster_Backend == gRec.fBackend) { |
| 581 | SkBitmap bitmap; |
| 582 | generate_image_from_picture(gm, gRec, repict, &bitmap); |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 583 | return handle_test_results(gm, gRec, NULL, NULL, diffPath, |
| 584 | "-serialize", bitmap, NULL, &comparisonBitmap); |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 585 | } else { |
| 586 | return ERROR_NONE; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
| 590 | static void usage(const char * argv0) { |
junov@google.com | 77e498e | 2012-01-18 18:56:34 +0000 | [diff] [blame] | 591 | SkDebugf( |
robertphillips@google.com | 0a89c90 | 2012-03-20 15:13:04 +0000 | [diff] [blame] | 592 | "%s [-w writePath] [-r readPath] [-d diffPath] [--noreplay]\n" |
| 593 | " [--serialize] [--forceBWtext] [--nopdf] [--nodeferred]\n" |
| 594 | " [--match substring] [--notexturecache]" |
junov@google.com | 77e498e | 2012-01-18 18:56:34 +0000 | [diff] [blame] | 595 | #if SK_MESA |
| 596 | " [--mesagl]" |
| 597 | #endif |
| 598 | "\n\n", argv0); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 599 | SkDebugf(" writePath: directory to write rendered images in.\n"); |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 600 | SkDebugf( |
| 601 | " readPath: directory to read reference images from;\n" |
| 602 | " reports if any pixels mismatch between reference and new images\n"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 603 | SkDebugf(" diffPath: directory to write difference images in.\n"); |
junov@google.com | 77e498e | 2012-01-18 18:56:34 +0000 | [diff] [blame] | 604 | SkDebugf(" --noreplay: do not exercise SkPicture replay.\n"); |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 605 | SkDebugf( |
| 606 | " --serialize: exercise SkPicture serialization & deserialization.\n"); |
junov@google.com | 77e498e | 2012-01-18 18:56:34 +0000 | [diff] [blame] | 607 | SkDebugf(" --forceBWtext: disable text anti-aliasing.\n"); |
| 608 | SkDebugf(" --nopdf: skip the pdf rendering test pass.\n"); |
| 609 | SkDebugf(" --nodeferred: skip the deferred rendering test pass.\n"); |
reed@google.com | e6a5c4d | 2011-07-25 14:30:54 +0000 | [diff] [blame] | 610 | SkDebugf(" --match foo will only run tests that substring match foo.\n"); |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 611 | #if SK_MESA |
| 612 | SkDebugf(" --mesagl will run using the osmesa sw gl rasterizer.\n"); |
| 613 | #endif |
twiz@google.com | e24a079 | 2012-01-31 18:35:30 +0000 | [diff] [blame] | 614 | SkDebugf(" --notexturecache: disable the gpu texture cache.\n"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static const ConfigData gRec[] = { |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 618 | { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" }, |
| 619 | { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" }, |
| 620 | { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" }, |
reed@google.com | 1a7e946 | 2011-06-20 13:21:24 +0000 | [diff] [blame] | 621 | #ifdef SK_SCALAR_IS_FLOAT |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 622 | { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" }, |
reed@google.com | 1a7e946 | 2011-06-20 13:21:24 +0000 | [diff] [blame] | 623 | #endif |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 624 | #ifdef SK_SUPPORT_PDF |
| 625 | { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" }, |
| 626 | #endif |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 627 | #ifdef SK_SUPPORT_XPS |
| 628 | { SkBitmap::kARGB_8888_Config, kXPS_Backend, "xps" }, |
| 629 | #endif |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 630 | }; |
| 631 | |
reed@google.com | b2a5162 | 2011-10-31 16:30:04 +0000 | [diff] [blame] | 632 | static bool skip_name(const SkTDArray<const char*> array, const char name[]) { |
| 633 | if (0 == array.count()) { |
| 634 | // no names, so don't skip anything |
| 635 | return false; |
| 636 | } |
| 637 | for (int i = 0; i < array.count(); ++i) { |
| 638 | if (strstr(name, array[i])) { |
| 639 | // found the name, so don't skip |
| 640 | return false; |
| 641 | } |
| 642 | } |
| 643 | return true; |
| 644 | } |
| 645 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 646 | namespace skiagm { |
| 647 | static GrContext* gGrContext; |
| 648 | GrContext* GetGr() { |
| 649 | return gGrContext; |
| 650 | } |
| 651 | } |
| 652 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 653 | int main(int argc, char * const argv[]) { |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 654 | SkAutoGraphics ag; |
reed@google.com | 8923c6c | 2011-11-08 14:59:38 +0000 | [diff] [blame] | 655 | // we don't need to see this during a run |
| 656 | gSkSuppressFontCachePurgeSpew = true; |
reed@google.com | d4dfd10 | 2011-01-18 21:05:42 +0000 | [diff] [blame] | 657 | |
epoger@google.com | 7bc13a6 | 2012-02-14 14:53:59 +0000 | [diff] [blame] | 658 | setSystemPreferences(); |
| 659 | |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 660 | const char* writePath = NULL; // if non-null, where we write the originals |
| 661 | 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] | 662 | 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] | 663 | |
reed@google.com | b2a5162 | 2011-10-31 16:30:04 +0000 | [diff] [blame] | 664 | SkTDArray<const char*> fMatches; |
twiz@google.com | e24a079 | 2012-01-31 18:35:30 +0000 | [diff] [blame] | 665 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 666 | bool doPDF = true; |
reed@google.com | b8b0983 | 2011-05-26 15:57:56 +0000 | [diff] [blame] | 667 | bool doReplay = true; |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame] | 668 | bool doSerialize = false; |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 669 | bool useMesa = false; |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 670 | bool doDeferred = true; |
twiz@google.com | e24a079 | 2012-01-31 18:35:30 +0000 | [diff] [blame] | 671 | bool disableTextureCache = false; |
| 672 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 673 | const char* const commandName = argv[0]; |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 674 | char* const* stop = argv + argc; |
| 675 | for (++argv; argv < stop; ++argv) { |
| 676 | if (strcmp(*argv, "-w") == 0) { |
| 677 | argv++; |
| 678 | if (argv < stop && **argv) { |
| 679 | writePath = *argv; |
| 680 | } |
| 681 | } else if (strcmp(*argv, "-r") == 0) { |
| 682 | argv++; |
| 683 | if (argv < stop && **argv) { |
| 684 | readPath = *argv; |
| 685 | } |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 686 | } else if (strcmp(*argv, "-d") == 0) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 687 | argv++; |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 688 | if (argv < stop && **argv) { |
| 689 | diffPath = *argv; |
| 690 | } |
mike@reedtribe.org | 10afbef | 2011-12-30 16:02:53 +0000 | [diff] [blame] | 691 | } else if (strcmp(*argv, "--forceBWtext") == 0) { |
| 692 | gForceBWtext = true; |
reed@google.com | b8b0983 | 2011-05-26 15:57:56 +0000 | [diff] [blame] | 693 | } else if (strcmp(*argv, "--noreplay") == 0) { |
| 694 | doReplay = false; |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 695 | } else if (strcmp(*argv, "--nopdf") == 0) { |
| 696 | doPDF = false; |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 697 | } else if (strcmp(*argv, "--nodeferred") == 0) { |
| 698 | doDeferred = false; |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 699 | } else if (strcmp(*argv, "--serialize") == 0) { |
| 700 | doSerialize = true; |
reed@google.com | ece2b02 | 2011-07-25 14:28:57 +0000 | [diff] [blame] | 701 | } else if (strcmp(*argv, "--match") == 0) { |
| 702 | ++argv; |
| 703 | if (argv < stop && **argv) { |
reed@google.com | b2a5162 | 2011-10-31 16:30:04 +0000 | [diff] [blame] | 704 | // just record the ptr, no need for a deep copy |
| 705 | *fMatches.append() = *argv; |
reed@google.com | ece2b02 | 2011-07-25 14:28:57 +0000 | [diff] [blame] | 706 | } |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 707 | #if SK_MESA |
| 708 | } else if (strcmp(*argv, "--mesagl") == 0) { |
| 709 | useMesa = true; |
| 710 | #endif |
twiz@google.com | e24a079 | 2012-01-31 18:35:30 +0000 | [diff] [blame] | 711 | } else if (strcmp(*argv, "--notexturecache") == 0) { |
robertphillips@google.com | 0a89c90 | 2012-03-20 15:13:04 +0000 | [diff] [blame] | 712 | disableTextureCache = true; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 713 | } else { |
robertphillips@google.com | 0a89c90 | 2012-03-20 15:13:04 +0000 | [diff] [blame] | 714 | usage(commandName); |
| 715 | return -1; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | if (argv != stop) { |
robertphillips@google.com | 0a89c90 | 2012-03-20 15:13:04 +0000 | [diff] [blame] | 719 | usage(commandName); |
| 720 | return -1; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 721 | } |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 722 | |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 723 | int maxW = -1; |
| 724 | int maxH = -1; |
| 725 | Iter iter; |
| 726 | GM* gm; |
| 727 | while ((gm = iter.next()) != NULL) { |
| 728 | SkISize size = gm->getISize(); |
| 729 | maxW = SkMax32(size.width(), maxW); |
| 730 | maxH = SkMax32(size.height(), maxH); |
tomhudson@google.com | 7816a4e | 2012-03-15 13:39:51 +0000 | [diff] [blame] | 731 | // This fixes a memory leak, but we are churning gms; we could |
| 732 | // instead cache them if we have constructors with side-effects. |
| 733 | SkDELETE(gm); |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 734 | } |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 735 | // setup a GL context for drawing offscreen |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 736 | SkAutoTUnref<SkGLContext> glContext; |
| 737 | #if SK_MESA |
| 738 | if (useMesa) { |
| 739 | glContext.reset(new SkMesaGLContext()); |
| 740 | } else |
| 741 | #endif |
| 742 | { |
| 743 | glContext.reset(new SkNativeGLContext()); |
| 744 | } |
| 745 | |
bsalomon@google.com | 29d3501 | 2011-11-30 16:57:21 +0000 | [diff] [blame] | 746 | GrPlatformRenderTargetDesc rtDesc; |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 747 | if (glContext.get()->init(maxW, maxH)) { |
| 748 | GrPlatform3DContext ctx = |
| 749 | reinterpret_cast<GrPlatform3DContext>(glContext.get()->gl()); |
| 750 | gGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, ctx); |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 751 | if (NULL != gGrContext) { |
bsalomon@google.com | 29d3501 | 2011-11-30 16:57:21 +0000 | [diff] [blame] | 752 | rtDesc.fConfig = kSkia8888_PM_GrPixelConfig; |
| 753 | rtDesc.fStencilBits = 8; |
| 754 | rtDesc.fRenderTargetHandle = glContext.get()->getFBOID(); |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 755 | } |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 756 | } else { |
| 757 | fprintf(stderr, "could not create GL context.\n"); |
reed@google.com | 37df17d | 2010-12-23 20:20:51 +0000 | [diff] [blame] | 758 | } |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 759 | |
reed@android.com | 00f883e | 2010-12-14 17:46:14 +0000 | [diff] [blame] | 760 | if (readPath) { |
| 761 | fprintf(stderr, "reading from %s\n", readPath); |
| 762 | } else if (writePath) { |
| 763 | fprintf(stderr, "writing to %s\n", writePath); |
| 764 | } |
| 765 | |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 766 | // Accumulate success of all tests. |
| 767 | int testsRun = 0; |
| 768 | int testsPassed = 0; |
| 769 | int testsFailed = 0; |
| 770 | int testsMissingReferenceImages = 0; |
| 771 | |
twiz@google.com | e24a079 | 2012-01-31 18:35:30 +0000 | [diff] [blame] | 772 | if (disableTextureCache) { |
| 773 | skiagm::GetGr()->setTextureCacheLimits(0, 0); |
| 774 | } |
| 775 | |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 776 | iter.reset(); |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 777 | while ((gm = iter.next()) != NULL) { |
reed@google.com | ece2b02 | 2011-07-25 14:28:57 +0000 | [diff] [blame] | 778 | const char* shortName = gm->shortName(); |
reed@google.com | b2a5162 | 2011-10-31 16:30:04 +0000 | [diff] [blame] | 779 | if (skip_name(fMatches, shortName)) { |
reed@google.com | ece2b02 | 2011-07-25 14:28:57 +0000 | [diff] [blame] | 780 | SkDELETE(gm); |
| 781 | continue; |
| 782 | } |
| 783 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 784 | SkISize size = gm->getISize(); |
reed@google.com | ece2b02 | 2011-07-25 14:28:57 +0000 | [diff] [blame] | 785 | SkDebugf("drawing... %s [%d %d]\n", shortName, |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 786 | size.width(), size.height()); |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 787 | SkBitmap forwardRenderedBitmap; |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 788 | |
bsalomon@google.com | 29d3501 | 2011-11-30 16:57:21 +0000 | [diff] [blame] | 789 | // Above we created an fbo for the context at maxW x maxH size. |
| 790 | // Here we lie about the size of the rt. We claim it is the size |
| 791 | // desired by the test. The reason is that rasterization may change |
| 792 | // slightly when the viewport dimensions change. Previously, whenever |
| 793 | // a new test was checked in that bumped maxW or maxH several images |
| 794 | // would slightly change. |
| 795 | rtDesc.fWidth = size.width(); |
| 796 | rtDesc.fHeight = size.height(); |
| 797 | SkAutoTUnref<GrRenderTarget> rt; |
| 798 | if (gGrContext) { |
| 799 | rt.reset(gGrContext->createPlatformRenderTarget(rtDesc)); |
| 800 | } |
reed@google.com | fbc2117 | 2011-09-19 19:08:33 +0000 | [diff] [blame] | 801 | |
bsalomon@google.com | 29d3501 | 2011-11-30 16:57:21 +0000 | [diff] [blame] | 802 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) { |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 803 | // Skip any tests that we don't even need to try. |
bsalomon@google.com | 29d3501 | 2011-11-30 16:57:21 +0000 | [diff] [blame] | 804 | uint32_t gmFlags = gm->getFlags(); |
twiz@google.com | e24a079 | 2012-01-31 18:35:30 +0000 | [diff] [blame] | 805 | if ((kPDF_Backend == gRec[i].fBackend) && |
bungeman@google.com | 64e011a | 2011-09-19 19:31:04 +0000 | [diff] [blame] | 806 | (!doPDF || (gmFlags & GM::kSkipPDF_Flag))) |
| 807 | { |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 808 | continue; |
| 809 | } |
| 810 | |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 811 | // Now we know that we want to run this test and record its |
| 812 | // success or failure. |
| 813 | ErrorBitfield testErrors = ERROR_NONE; |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 814 | |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 815 | if ((ERROR_NONE == testErrors) && |
| 816 | (kGPU_Backend == gRec[i].fBackend) && |
| 817 | (NULL == rt.get())) { |
| 818 | fprintf(stderr, "Could not create render target for gpu.\n"); |
| 819 | testErrors |= ERROR_NO_GPU_CONTEXT; |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 820 | } |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 821 | |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 822 | if (ERROR_NONE == testErrors) { |
| 823 | testErrors |= test_drawing(gm, gRec[i], |
| 824 | writePath, readPath, diffPath, |
| 825 | gGrContext, |
| 826 | rt.get(), &forwardRenderedBitmap); |
| 827 | } |
| 828 | |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 829 | if (doDeferred && !testErrors && |
twiz@google.com | e24a079 | 2012-01-31 18:35:30 +0000 | [diff] [blame] | 830 | (kGPU_Backend == gRec[i].fBackend || |
robertphillips@google.com | 0a89c90 | 2012-03-20 15:13:04 +0000 | [diff] [blame] | 831 | kRaster_Backend == gRec[i].fBackend)) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 832 | testErrors |= test_deferred_drawing(gm, gRec[i], |
robertphillips@google.com | 0a89c90 | 2012-03-20 15:13:04 +0000 | [diff] [blame] | 833 | forwardRenderedBitmap, |
| 834 | diffPath, gGrContext, rt.get()); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 835 | } |
| 836 | |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 837 | if ((ERROR_NONE == testErrors) && doReplay && |
| 838 | !(gmFlags & GM::kSkipPicture_Flag)) { |
| 839 | testErrors |= test_picture_playback(gm, gRec[i], |
| 840 | forwardRenderedBitmap, |
| 841 | readPath, diffPath); |
| 842 | } |
| 843 | |
| 844 | if ((ERROR_NONE == testErrors) && doSerialize) { |
| 845 | testErrors |= test_picture_serialization(gm, gRec[i], |
| 846 | forwardRenderedBitmap, |
| 847 | readPath, diffPath); |
| 848 | } |
| 849 | |
| 850 | // Update overall results. |
| 851 | // We only tabulate the particular error types that we currently |
| 852 | // care about (e.g., missing reference images). Later on, if we |
| 853 | // want to also tabulate pixel mismatches vs dimension mistmatches |
| 854 | // (or whatever else), we can do so. |
| 855 | testsRun++; |
| 856 | if (ERROR_NONE == testErrors) { |
| 857 | testsPassed++; |
| 858 | } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) { |
| 859 | testsMissingReferenceImages++; |
| 860 | } else { |
| 861 | testsFailed++; |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 862 | } |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 863 | } |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 864 | SkDELETE(gm); |
| 865 | } |
epoger@google.com | c7cf2b3 | 2011-12-28 19:31:01 +0000 | [diff] [blame] | 866 | printf("Ran %d tests: %d passed, %d failed, %d missing reference images\n", |
| 867 | testsRun, testsPassed, testsFailed, testsMissingReferenceImages); |
| 868 | return (0 == testsFailed) ? 0 : -1; |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 869 | } |