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