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