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