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 | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 254 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth, |
| 255 | size.fHeight); |
| 256 | gc.readPixels(bitmap, 0, 0); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 257 | } |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | static void generate_image_from_picture(GM* gm, const ConfigData& gRec, |
| 262 | SkPicture* pict, SkBitmap* bitmap) { |
| 263 | SkISize size = gm->getISize(); |
| 264 | setup_bitmap(gRec, size, bitmap); |
| 265 | SkCanvas canvas(*bitmap); |
| 266 | canvas.drawPicture(*pict); |
| 267 | } |
| 268 | |
| 269 | static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) { |
| 270 | #ifdef SK_SUPPORT_PDF |
| 271 | SkISize size = gm->getISize(); |
| 272 | SkMatrix identity; |
| 273 | identity.reset(); |
ctguil@chromium.org | 1526129 | 2011-04-29 17:54:16 +0000 | [diff] [blame] | 274 | SkPDFDevice* dev = new SkPDFDevice(size, size, identity); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 275 | SkAutoUnref aur(dev); |
| 276 | |
| 277 | SkCanvas c(dev); |
| 278 | gm->draw(&c); |
| 279 | |
| 280 | SkPDFDocument doc; |
| 281 | doc.appendPage(dev); |
| 282 | doc.emitPDF(&pdf); |
| 283 | #endif |
| 284 | } |
| 285 | |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 286 | static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) { |
| 287 | #ifdef SK_SUPPORT_XPS |
| 288 | SkISize size = gm->getISize(); |
| 289 | |
| 290 | SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()), |
| 291 | SkIntToScalar(size.height())); |
| 292 | static const double inchesPerMeter = 10000.0 / 254.0; |
| 293 | static const double upm = 72 * inchesPerMeter; |
| 294 | SkVector unitsPerMeter = SkPoint::Make(SkDoubleToScalar(upm), |
| 295 | SkDoubleToScalar(upm)); |
| 296 | static const double ppm = 200 * inchesPerMeter; |
| 297 | SkVector pixelsPerMeter = SkPoint::Make(SkDoubleToScalar(ppm), |
| 298 | SkDoubleToScalar(ppm)); |
| 299 | |
| 300 | SkXPSDevice* dev = new SkXPSDevice(); |
| 301 | SkAutoUnref aur(dev); |
| 302 | |
| 303 | SkCanvas c(dev); |
| 304 | dev->beginPortfolio(&xps); |
| 305 | dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize); |
| 306 | gm->draw(&c); |
| 307 | dev->endSheet(); |
| 308 | dev->endPortfolio(); |
| 309 | |
| 310 | #endif |
| 311 | } |
| 312 | |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 313 | static bool write_reference_image(const ConfigData& gRec, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 314 | const char writePath [], |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 315 | const char renderModeDescriptor [], |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 316 | const SkString& name, |
| 317 | SkBitmap& bitmap, |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 318 | SkDynamicMemoryWStream* document) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 319 | SkString path; |
| 320 | bool success = false; |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 321 | if (gRec.fBackend == kRaster_Backend || |
| 322 | gRec.fBackend == kGPU_Backend || |
| 323 | (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) { |
| 324 | |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 325 | path = make_filename(writePath, renderModeDescriptor, name, "png"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 326 | success = write_bitmap(path, bitmap); |
reed@google.com | 46cce91 | 2011-06-29 12:54:46 +0000 | [diff] [blame] | 327 | } |
| 328 | if (kPDF_Backend == gRec.fBackend) { |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 329 | path = make_filename(writePath, renderModeDescriptor, name, "pdf"); |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 330 | success = write_document(path, *document); |
| 331 | } |
| 332 | if (kXPS_Backend == gRec.fBackend) { |
| 333 | path = make_filename(writePath, renderModeDescriptor, name, "xps"); |
| 334 | success = write_document(path, *document); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 335 | } |
| 336 | if (!success) { |
| 337 | fprintf(stderr, "FAILED to write %s\n", path.c_str()); |
| 338 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 339 | return success; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 340 | } |
| 341 | |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 342 | static bool compare_to_reference_image(const SkString& name, |
| 343 | SkBitmap &bitmap, |
| 344 | const SkBitmap& comparisonBitmap, |
| 345 | const char diffPath [], |
| 346 | const char renderModeDescriptor []) { |
| 347 | bool success; |
| 348 | SkBitmap diffBitmap; |
| 349 | success = compare(bitmap, comparisonBitmap, name, renderModeDescriptor, |
| 350 | diffPath ? &diffBitmap : NULL); |
| 351 | if (!success && diffPath) { |
| 352 | SkString diffName = make_filename(diffPath, "", name, ".diff.png"); |
| 353 | write_bitmap(diffName, diffBitmap); |
| 354 | } |
| 355 | return success; |
| 356 | } |
| 357 | |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 358 | static bool compare_to_reference_image(const char readPath [], |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 359 | const SkString& name, |
| 360 | SkBitmap &bitmap, |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame] | 361 | const char diffPath [], |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 362 | const char renderModeDescriptor []) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 363 | SkString path = make_filename(readPath, "", name, "png"); |
| 364 | SkBitmap orig; |
| 365 | bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig, |
| 366 | SkBitmap::kARGB_8888_Config, |
| 367 | SkImageDecoder::kDecodePixels_Mode, NULL); |
| 368 | if (success) { |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 369 | success = compare_to_reference_image(name, bitmap, |
| 370 | orig, diffPath, |
| 371 | renderModeDescriptor); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 372 | } else { |
| 373 | fprintf(stderr, "FAILED to read %s\n", path.c_str()); |
reed@google.com | ac864a9 | 2011-06-27 18:11:17 +0000 | [diff] [blame] | 374 | // we lie here, and report succes, since we're just missing a master |
| 375 | // image. This way we can check in new tests, and not report failure. |
| 376 | // A real failure is to draw *differently* from the master image, but |
| 377 | // that's not the case here. |
| 378 | success = true; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 379 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 380 | return success; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 381 | } |
| 382 | |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 383 | static bool handle_test_results(GM* gm, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 384 | const ConfigData& gRec, |
| 385 | const char writePath [], |
| 386 | const char readPath [], |
| 387 | const char diffPath [], |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 388 | const char renderModeDescriptor [], |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 389 | SkBitmap& bitmap, |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 390 | SkDynamicMemoryWStream* pdf, |
| 391 | const SkBitmap* comparisonBitmap) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 392 | SkString name = make_name(gm->shortName(), gRec.fName); |
| 393 | |
| 394 | if (writePath) { |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 395 | write_reference_image(gRec, writePath, renderModeDescriptor, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 396 | name, bitmap, pdf); |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 397 | } else if (readPath && ( |
| 398 | gRec.fBackend == kRaster_Backend || |
| 399 | gRec.fBackend == kGPU_Backend || |
| 400 | (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF))) { |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 401 | return compare_to_reference_image(readPath, name, bitmap, |
tomhudson@google.com | ea32543 | 2011-06-09 20:30:03 +0000 | [diff] [blame] | 402 | diffPath, renderModeDescriptor); |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 403 | } else if (comparisonBitmap) { |
| 404 | return compare_to_reference_image(name, bitmap, |
| 405 | *comparisonBitmap, diffPath, |
| 406 | renderModeDescriptor); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 407 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 408 | return true; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | static SkPicture* generate_new_picture(GM* gm) { |
| 412 | // Pictures are refcounted so must be on heap |
| 413 | SkPicture* pict = new SkPicture; |
| 414 | SkCanvas* cv = pict->beginRecording(1000, 1000); |
| 415 | gm->draw(cv); |
| 416 | pict->endRecording(); |
| 417 | |
| 418 | return pict; |
| 419 | } |
| 420 | |
| 421 | static SkPicture* stream_to_new_picture(const SkPicture& src) { |
| 422 | |
| 423 | // To do in-memory commiunications with a stream, we need to: |
| 424 | // * create a dynamic memory stream |
| 425 | // * copy it into a buffer |
| 426 | // * create a read stream from it |
| 427 | // ?!?! |
| 428 | |
| 429 | SkDynamicMemoryWStream storage; |
| 430 | src.serialize(&storage); |
| 431 | |
| 432 | int streamSize = storage.getOffset(); |
| 433 | SkAutoMalloc dstStorage(streamSize); |
| 434 | void* dst = dstStorage.get(); |
| 435 | //char* dst = new char [streamSize]; |
| 436 | //@todo thudson 22 April 2011 when can we safely delete [] dst? |
| 437 | storage.copyTo(dst); |
| 438 | SkMemoryStream pictReadback(dst, streamSize); |
| 439 | SkPicture* retval = new SkPicture (&pictReadback); |
| 440 | return retval; |
| 441 | } |
| 442 | |
| 443 | // Test: draw into a bitmap or pdf. |
| 444 | // Depending on flags, possibly compare to an expected image |
| 445 | // and possibly output a diff image if it fails to match. |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 446 | static bool test_drawing(GM* gm, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 447 | const ConfigData& gRec, |
| 448 | const char writePath [], |
| 449 | const char readPath [], |
| 450 | const char diffPath [], |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 451 | GrContext* context, |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 452 | GrRenderTarget* rt, |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 453 | SkBitmap* bitmap) { |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 454 | SkDynamicMemoryWStream document; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 455 | |
| 456 | if (gRec.fBackend == kRaster_Backend || |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 457 | gRec.fBackend == kGPU_Backend) { |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 458 | // Early exit if we can't generate the image, but this is |
| 459 | // expected in some cases, so don't report a test failure. |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 460 | if (!generate_image(gm, gRec, context, rt, bitmap)) { |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 461 | return true; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 462 | } |
reed@google.com | 46cce91 | 2011-06-29 12:54:46 +0000 | [diff] [blame] | 463 | } else if (gRec.fBackend == kPDF_Backend) { |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 464 | generate_pdf(gm, document); |
reed@google.com | 46cce91 | 2011-06-29 12:54:46 +0000 | [diff] [blame] | 465 | #if CAN_IMAGE_PDF |
bungeman@google.com | 0f1541f | 2011-10-10 13:47:06 +0000 | [diff] [blame] | 466 | SkAutoDataUnref data(document.copyToData()); |
reed@google.com | 46cce91 | 2011-06-29 12:54:46 +0000 | [diff] [blame] | 467 | SkMemoryStream stream(data.data(), data.size()); |
| 468 | SkPDFDocumentToBitmap(&stream, bitmap); |
| 469 | #endif |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 470 | } else if (gRec.fBackend == kXPS_Backend) { |
| 471 | generate_xps(gm, document); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 472 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 473 | return handle_test_results(gm, gRec, writePath, readPath, diffPath, |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 474 | "", *bitmap, &document, NULL); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 475 | } |
| 476 | |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 477 | static bool test_picture_playback(GM* gm, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 478 | const ConfigData& gRec, |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 479 | const SkBitmap& comparisonBitmap, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 480 | const char readPath [], |
| 481 | const char diffPath []) { |
| 482 | SkPicture* pict = generate_new_picture(gm); |
| 483 | SkAutoUnref aur(pict); |
| 484 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 485 | if (kRaster_Backend == gRec.fBackend) { |
| 486 | SkBitmap bitmap; |
| 487 | generate_image_from_picture(gm, gRec, pict, &bitmap); |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 488 | return handle_test_results(gm, gRec, NULL, NULL, diffPath, |
| 489 | "-replay", bitmap, NULL, &comparisonBitmap); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 490 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 491 | return true; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 492 | } |
| 493 | |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 494 | static bool test_picture_serialization(GM* gm, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 495 | const ConfigData& gRec, |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 496 | const SkBitmap& comparisonBitmap, |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 497 | const char readPath [], |
| 498 | const char diffPath []) { |
| 499 | SkPicture* pict = generate_new_picture(gm); |
| 500 | SkAutoUnref aurp(pict); |
| 501 | SkPicture* repict = stream_to_new_picture(*pict); |
| 502 | SkAutoUnref aurr(repict); |
| 503 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 504 | if (kRaster_Backend == gRec.fBackend) { |
| 505 | SkBitmap bitmap; |
| 506 | generate_image_from_picture(gm, gRec, repict, &bitmap); |
tomhudson@google.com | cae6b3f | 2011-06-17 13:11:45 +0000 | [diff] [blame] | 507 | return handle_test_results(gm, gRec, NULL, NULL, diffPath, |
| 508 | "-serialize", bitmap, NULL, &comparisonBitmap); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 509 | } |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 510 | return true; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static void usage(const char * argv0) { |
| 514 | SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0); |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 515 | SkDebugf(" [--replay] [--serialize]\n"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 516 | SkDebugf(" writePath: directory to write rendered images in.\n"); |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 517 | SkDebugf( |
| 518 | " readPath: directory to read reference images from;\n" |
| 519 | " reports if any pixels mismatch between reference and new images\n"); |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 520 | SkDebugf(" diffPath: directory to write difference images in.\n"); |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 521 | SkDebugf(" --replay: exercise SkPicture replay.\n"); |
| 522 | SkDebugf( |
| 523 | " --serialize: exercise SkPicture serialization & deserialization.\n"); |
reed@google.com | e6a5c4d | 2011-07-25 14:30:54 +0000 | [diff] [blame] | 524 | 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] | 525 | #if SK_MESA |
| 526 | SkDebugf(" --mesagl will run using the osmesa sw gl rasterizer.\n"); |
| 527 | #endif |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static const ConfigData gRec[] = { |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 531 | { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" }, |
| 532 | { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" }, |
| 533 | { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" }, |
reed@google.com | 1a7e946 | 2011-06-20 13:21:24 +0000 | [diff] [blame] | 534 | #ifdef SK_SCALAR_IS_FLOAT |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 535 | { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" }, |
reed@google.com | 1a7e946 | 2011-06-20 13:21:24 +0000 | [diff] [blame] | 536 | #endif |
vandebo@chromium.org | 686abdf | 2011-02-03 23:00:40 +0000 | [diff] [blame] | 537 | #ifdef SK_SUPPORT_PDF |
| 538 | { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" }, |
| 539 | #endif |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 540 | #ifdef SK_SUPPORT_XPS |
| 541 | { SkBitmap::kARGB_8888_Config, kXPS_Backend, "xps" }, |
| 542 | #endif |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 543 | }; |
| 544 | |
reed@google.com | b2a5162 | 2011-10-31 16:30:04 +0000 | [diff] [blame] | 545 | static bool skip_name(const SkTDArray<const char*> array, const char name[]) { |
| 546 | if (0 == array.count()) { |
| 547 | // no names, so don't skip anything |
| 548 | return false; |
| 549 | } |
| 550 | for (int i = 0; i < array.count(); ++i) { |
| 551 | if (strstr(name, array[i])) { |
| 552 | // found the name, so don't skip |
| 553 | return false; |
| 554 | } |
| 555 | } |
| 556 | return true; |
| 557 | } |
| 558 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 559 | namespace skiagm { |
| 560 | static GrContext* gGrContext; |
| 561 | GrContext* GetGr() { |
| 562 | return gGrContext; |
| 563 | } |
| 564 | } |
| 565 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 566 | int main(int argc, char * const argv[]) { |
reed@android.com | 00dae86 | 2009-06-10 15:38:48 +0000 | [diff] [blame] | 567 | SkAutoGraphics ag; |
reed@google.com | d4dfd10 | 2011-01-18 21:05:42 +0000 | [diff] [blame] | 568 | |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 569 | const char* writePath = NULL; // if non-null, where we write the originals |
| 570 | 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] | 571 | 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] | 572 | |
reed@google.com | b2a5162 | 2011-10-31 16:30:04 +0000 | [diff] [blame] | 573 | SkTDArray<const char*> fMatches; |
| 574 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 575 | bool doPDF = true; |
reed@google.com | b8b0983 | 2011-05-26 15:57:56 +0000 | [diff] [blame] | 576 | bool doReplay = true; |
tomhudson@google.com | 6abfa49 | 2011-04-26 14:59:32 +0000 | [diff] [blame] | 577 | bool doSerialize = false; |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 578 | bool useMesa = false; |
| 579 | |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 580 | const char* const commandName = argv[0]; |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 581 | char* const* stop = argv + argc; |
| 582 | for (++argv; argv < stop; ++argv) { |
| 583 | if (strcmp(*argv, "-w") == 0) { |
| 584 | argv++; |
| 585 | if (argv < stop && **argv) { |
| 586 | writePath = *argv; |
| 587 | } |
| 588 | } else if (strcmp(*argv, "-r") == 0) { |
| 589 | argv++; |
| 590 | if (argv < stop && **argv) { |
| 591 | readPath = *argv; |
| 592 | } |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 593 | } else if (strcmp(*argv, "-d") == 0) { |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 594 | argv++; |
reed@google.com | 3d3f092 | 2010-12-20 21:10:29 +0000 | [diff] [blame] | 595 | if (argv < stop && **argv) { |
| 596 | diffPath = *argv; |
| 597 | } |
reed@google.com | b8b0983 | 2011-05-26 15:57:56 +0000 | [diff] [blame] | 598 | } else if (strcmp(*argv, "--noreplay") == 0) { |
| 599 | doReplay = false; |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 600 | } else if (strcmp(*argv, "--nopdf") == 0) { |
| 601 | doPDF = false; |
tomhudson@google.com | 73fb042 | 2011-04-25 19:20:54 +0000 | [diff] [blame] | 602 | } else if (strcmp(*argv, "--serialize") == 0) { |
| 603 | doSerialize = true; |
reed@google.com | ece2b02 | 2011-07-25 14:28:57 +0000 | [diff] [blame] | 604 | } else if (strcmp(*argv, "--match") == 0) { |
| 605 | ++argv; |
| 606 | if (argv < stop && **argv) { |
reed@google.com | b2a5162 | 2011-10-31 16:30:04 +0000 | [diff] [blame] | 607 | // just record the ptr, no need for a deep copy |
| 608 | *fMatches.append() = *argv; |
reed@google.com | ece2b02 | 2011-07-25 14:28:57 +0000 | [diff] [blame] | 609 | } |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 610 | #if SK_MESA |
| 611 | } else if (strcmp(*argv, "--mesagl") == 0) { |
| 612 | useMesa = true; |
| 613 | #endif |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 614 | } else { |
| 615 | usage(commandName); |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 616 | return -1; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | if (argv != stop) { |
| 620 | usage(commandName); |
tomhudson@google.com | 8e728d7 | 2011-04-26 20:22:57 +0000 | [diff] [blame] | 621 | return -1; |
tomhudson@google.com | 9875dd1 | 2011-04-25 15:49:53 +0000 | [diff] [blame] | 622 | } |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 623 | |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 624 | int maxW = -1; |
| 625 | int maxH = -1; |
| 626 | Iter iter; |
| 627 | GM* gm; |
| 628 | while ((gm = iter.next()) != NULL) { |
| 629 | SkISize size = gm->getISize(); |
| 630 | maxW = SkMax32(size.width(), maxW); |
| 631 | maxH = SkMax32(size.height(), maxH); |
| 632 | } |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 633 | // setup a GL context for drawing offscreen |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 634 | SkAutoTUnref<SkGLContext> glContext; |
| 635 | #if SK_MESA |
| 636 | if (useMesa) { |
| 637 | glContext.reset(new SkMesaGLContext()); |
| 638 | } else |
| 639 | #endif |
| 640 | { |
| 641 | glContext.reset(new SkNativeGLContext()); |
| 642 | } |
| 643 | |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 644 | GrRenderTarget* rt = NULL; |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 645 | if (glContext.get()->init(maxW, maxH)) { |
| 646 | GrPlatform3DContext ctx = |
| 647 | reinterpret_cast<GrPlatform3DContext>(glContext.get()->gl()); |
| 648 | gGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, ctx); |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 649 | if (NULL != gGrContext) { |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 650 | GrPlatformRenderTargetDesc desc; |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 651 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 652 | desc.fWidth = maxW; |
| 653 | desc.fHeight = maxH; |
| 654 | desc.fStencilBits = 8; |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 655 | desc.fRenderTargetHandle = glContext.get()->getFBOID(); |
| 656 | rt = gGrContext->createPlatformRenderTarget(desc); |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 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 | } |