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