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