blob: 5ba3a804026bd6e26d76a7cc664604ad8c5a85c6 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.com971d0c82011-08-19 17:22:05 +00007
bungeman@google.comb29c8832011-10-10 13:19:10 +00008#include "gm.h"
epoger@google.com7bc13a62012-02-14 14:53:59 +00009#include "system_preferences.h"
reed@android.comb9b9a182009-07-08 02:54:47 +000010#include "SkColorPriv.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000011#include "SkData.h"
junov@google.com4370aed2012-01-18 16:21:08 +000012#include "SkDeferredCanvas.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000013#include "SkDevice.h"
scroggo@google.com5af9b202012-06-04 17:17:36 +000014#include "SkGPipe.h"
reed@android.com8015dd82009-06-21 00:49:18 +000015#include "SkGraphics.h"
16#include "SkImageDecoder.h"
17#include "SkImageEncoder.h"
tomhudson@google.com9875dd12011-04-25 15:49:53 +000018#include "SkPicture.h"
robertphillips@google.com977b9c82012-06-05 19:35:09 +000019#include "SkRefCnt.h"
scroggo@google.com72c96722012-06-06 21:07:10 +000020#include "SkStream.h"
bsalomon@google.com2a48c3a2012-08-03 14:54:45 +000021#include "SkTArray.h"
scroggo@google.com72c96722012-06-06 21:07:10 +000022#include "SamplePipeControllers.h"
reed@google.com07700442010-12-20 19:46:07 +000023
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000024#if SK_SUPPORT_GPU
25#include "GrContextFactory.h"
26#include "GrRenderTarget.h"
27#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000028typedef GrContextFactory::GLContextType GLContextType;
29#else
30class GrContext;
31class GrRenderTarget;
32typedef int GLContextType;
33#endif
34
mike@reedtribe.org10afbef2011-12-30 16:02:53 +000035static bool gForceBWtext;
36
reed@google.com8923c6c2011-11-08 14:59:38 +000037extern bool gSkSuppressFontCachePurgeSpew;
38
reed@google.com07700442010-12-20 19:46:07 +000039#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000040 #include "SkPDFDevice.h"
41 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000042#endif
reed@android.com00dae862009-06-10 15:38:48 +000043
epoger@google.come3cc2eb2012-01-18 20:11:13 +000044// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
45// stop writing out XPS-format image baselines in gm.
46#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000047#ifdef SK_SUPPORT_XPS
48 #include "SkXPSDevice.h"
49#endif
50
reed@google.com46cce912011-06-29 12:54:46 +000051#ifdef SK_BUILD_FOR_MAC
52 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000053 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000054#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000055 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000056#endif
57
epoger@google.comc7cf2b32011-12-28 19:31:01 +000058typedef int ErrorBitfield;
59const static ErrorBitfield ERROR_NONE = 0x00;
60const static ErrorBitfield ERROR_NO_GPU_CONTEXT = 0x01;
61const static ErrorBitfield ERROR_PIXEL_MISMATCH = 0x02;
62const static ErrorBitfield ERROR_DIMENSION_MISMATCH = 0x04;
63const static ErrorBitfield ERROR_READING_REFERENCE_IMAGE = 0x08;
64const static ErrorBitfield ERROR_WRITING_REFERENCE_IMAGE = 0x10;
65
epoger@google.comf28dd8a2012-10-25 16:27:34 +000066// TODO: This should be defined as "\\" on Windows, but this is the way this
67// file has been working for a long time. We can fix it later.
68const static char* PATH_SEPARATOR = "/";
69
reed@google.come8fcb502012-05-17 15:28:20 +000070// If true, emit a messange when we can't find a reference image to compare
71static bool gNotifyMissingReadReference;
72
reed@android.com00dae862009-06-10 15:38:48 +000073using namespace skiagm;
74
reed@android.com00dae862009-06-10 15:38:48 +000075class Iter {
76public:
77 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000078 this->reset();
79 }
80
81 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000082 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000083 }
reed@google.comd4dfd102011-01-18 21:05:42 +000084
reed@android.comdd0ac282009-06-20 02:38:16 +000085 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000086 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000087 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000088 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000089 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000090 }
91 return NULL;
92 }
reed@google.comd4dfd102011-01-18 21:05:42 +000093
reed@android.com00dae862009-06-10 15:38:48 +000094 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000095 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000096 int count = 0;
97 while (reg) {
98 count += 1;
99 reg = reg->next();
100 }
101 return count;
102 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000103
reed@android.com00dae862009-06-10 15:38:48 +0000104private:
105 const GMRegistry* fReg;
106};
107
reed@android.com8015dd82009-06-21 00:49:18 +0000108static SkString make_name(const char shortName[], const char configName[]) {
109 SkString name(shortName);
110 name.appendf("_%s", configName);
111 return name;
112}
113
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000114static SkString make_filename(const char path[],
115 const char pathSuffix[],
116 const SkString& name,
117 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +0000118 SkString filename(path);
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000119 if (filename.endsWith(PATH_SEPARATOR)) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000120 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +0000121 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000122 filename.append(pathSuffix);
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000123 filename.append(PATH_SEPARATOR);
reed@google.com07700442010-12-20 19:46:07 +0000124 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +0000125 return filename;
126}
127
reed@android.comb9b9a182009-07-08 02:54:47 +0000128/* since PNG insists on unpremultiplying our alpha, we take no precision chances
129 and force all pixels to be 100% opaque, otherwise on compare we may not get
130 a perfect match.
131 */
132static void force_all_opaque(const SkBitmap& bitmap) {
133 SkAutoLockPixels lock(bitmap);
134 for (int y = 0; y < bitmap.height(); y++) {
135 for (int x = 0; x < bitmap.width(); x++) {
136 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
137 }
138 }
139}
140
141static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
142 SkBitmap copy;
143 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
144 force_all_opaque(copy);
145 return SkImageEncoder::EncodeFile(path.c_str(), copy,
146 SkImageEncoder::kPNG_Type, 100);
147}
148
reed@google.com3d3f0922010-12-20 21:10:29 +0000149static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000150 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
151 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
152 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
153 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000154}
155
156static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000157 SkBitmap* diff) {
158 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000159
160 const int w = target.width();
161 const int h = target.height();
162 for (int y = 0; y < h; y++) {
163 for (int x = 0; x < w; x++) {
164 SkPMColor c0 = *base.getAddr32(x, y);
165 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000166 SkPMColor d = 0;
167 if (c0 != c1) {
168 d = compute_diff_pmcolor(c0, c1);
169 }
170 *diff->getAddr32(x, y) = d;
171 }
172 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000173}
174
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000175// Compares "target" and "base" bitmaps, returning the result (ERROR_NONE
176// if the two bitmaps are identical).
177//
178// If a "diff" bitmap is passed in, pixel diffs (if any) will be written
179// into it.
180//
181// The "name" and "renderModeDescriptor" arguments are only used in the debug
182// output.
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000183static ErrorBitfield compare(const SkBitmap& target, const SkBitmap& base,
184 const SkString& name,
185 const char* renderModeDescriptor,
scroggo@google.com30efb8a2012-10-04 22:02:46 +0000186 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000187 SkBitmap copy;
188 const SkBitmap* bm = &target;
189 if (target.config() != SkBitmap::kARGB_8888_Config) {
190 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
191 bm = &copy;
192 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000193 SkBitmap baseCopy;
194 const SkBitmap* bp = &base;
195 if (base.config() != SkBitmap::kARGB_8888_Config) {
196 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
197 bp = &baseCopy;
198 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000199
200 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000201 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000202
203 const int w = bm->width();
204 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000205 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000206 SkDebugf(
207"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
208 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000209 bp->width(), bp->height(), w, h);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000210 return ERROR_DIMENSION_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000211 }
212
213 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000214 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000215
216 for (int y = 0; y < h; y++) {
217 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000218 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000219 SkPMColor c1 = *bm->getAddr32(x, y);
220 if (c0 != c1) {
scroggo@google.com30efb8a2012-10-04 22:02:46 +0000221 SkDebugf(
tomhudson@google.comea325432011-06-09 20:30:03 +0000222"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
223 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000224
scroggo@google.com30efb8a2012-10-04 22:02:46 +0000225 if (diff) {
226 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
227 diff->allocPixels();
228 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000229 }
scroggo@google.com30efb8a2012-10-04 22:02:46 +0000230 return ERROR_PIXEL_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000231 }
232 }
233 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000234
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000235 // they're equal
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000236 return ERROR_NONE;
reed@android.com8015dd82009-06-21 00:49:18 +0000237}
reed@android.com00dae862009-06-10 15:38:48 +0000238
bungeman@google.comb29c8832011-10-10 13:19:10 +0000239static bool write_document(const SkString& path,
240 const SkDynamicMemoryWStream& document) {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000241 SkFILEWStream stream(path.c_str());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000242 SkAutoDataUnref data(document.copyToData());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000243 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000244}
245
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000246enum Backend {
247 kRaster_Backend,
248 kGPU_Backend,
249 kPDF_Backend,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000250 kXPS_Backend,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000251};
252
bsalomon@google.com7361f542012-04-19 19:15:35 +0000253enum ConfigFlags {
254 kNone_ConfigFlag = 0x0,
255 /* Write GM images if a write path is provided. */
256 kWrite_ConfigFlag = 0x1,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000257 /* Read reference GM images if a read path is provided. */
bsalomon@google.com7361f542012-04-19 19:15:35 +0000258 kRead_ConfigFlag = 0x2,
259 kRW_ConfigFlag = (kWrite_ConfigFlag | kRead_ConfigFlag),
260};
261
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000262struct ConfigData {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000263 SkBitmap::Config fConfig;
264 Backend fBackend;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000265 GLContextType fGLContextType; // GPU backend only
bsalomon@google.com7361f542012-04-19 19:15:35 +0000266 int fSampleCnt; // GPU backend only
267 ConfigFlags fFlags;
268 const char* fName;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000269};
270
271/// Returns true if processing should continue, false to skip the
272/// remainder of this config for this GM.
273//@todo thudson 22 April 2011 - could refactor this to take in
274// a factory to generate the context, always call readPixels()
275// (logically a noop for rasters, if wasted time), and thus collapse the
276// GPU special case and also let this be used for SkPicture testing.
277static void setup_bitmap(const ConfigData& gRec, SkISize& size,
278 SkBitmap* bitmap) {
279 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
280 bitmap->allocPixels();
281 bitmap->eraseColor(0);
282}
283
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000284#include "SkDrawFilter.h"
285class BWTextDrawFilter : public SkDrawFilter {
286public:
287 virtual void filter(SkPaint*, Type) SK_OVERRIDE;
288};
289void BWTextDrawFilter::filter(SkPaint* p, Type t) {
290 if (kText_Type == t) {
291 p->setAntiAlias(false);
292 }
293}
294
295static void installFilter(SkCanvas* canvas) {
296 if (gForceBWtext) {
297 canvas->setDrawFilter(new BWTextDrawFilter)->unref();
298 }
299}
300
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000301static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF = false) {
reed@google.com778e1632012-06-04 20:00:01 +0000302 SkAutoCanvasRestore acr(canvas, true);
303
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000304 if (!isPDF) {
reed@google.com778e1632012-06-04 20:00:01 +0000305 canvas->concat(gm->getInitialTransform());
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000306 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000307 installFilter(canvas);
308 gm->draw(canvas);
309 canvas->setDrawFilter(NULL);
310}
311
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000312static ErrorBitfield generate_image(GM* gm, const ConfigData& gRec,
313 GrContext* context,
314 GrRenderTarget* rt,
junov@google.com4370aed2012-01-18 16:21:08 +0000315 SkBitmap* bitmap,
316 bool deferred) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000317 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000318 setup_bitmap(gRec, size, bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000319
reed@google.com44a42ea2012-10-01 17:54:05 +0000320 SkAutoTUnref<SkCanvas> canvas;
321
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000322 if (gRec.fBackend == kRaster_Backend) {
reed@google.com44a42ea2012-10-01 17:54:05 +0000323 SkAutoTUnref<SkDevice> device(new SkDevice(*bitmap));
junov@google.com4370aed2012-01-18 16:21:08 +0000324 if (deferred) {
reed@google.com44a42ea2012-10-01 17:54:05 +0000325 canvas.reset(new SkDeferredCanvas(device));
junov@google.com4370aed2012-01-18 16:21:08 +0000326 } else {
reed@google.com44a42ea2012-10-01 17:54:05 +0000327 canvas.reset(new SkCanvas(device));
junov@google.com4370aed2012-01-18 16:21:08 +0000328 }
junov@google.com4370aed2012-01-18 16:21:08 +0000329 invokeGM(gm, canvas);
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000330 canvas->flush();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000331 }
332#if SK_SUPPORT_GPU
333 else { // GPU
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000334 if (NULL == context) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000335 return ERROR_NO_GPU_CONTEXT;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000336 }
reed@google.com44a42ea2012-10-01 17:54:05 +0000337 SkAutoTUnref<SkDevice> device(new SkGpuDevice(context, rt));
junov@google.com4370aed2012-01-18 16:21:08 +0000338 if (deferred) {
reed@google.com44a42ea2012-10-01 17:54:05 +0000339 canvas.reset(new SkDeferredCanvas(device));
junov@google.com4370aed2012-01-18 16:21:08 +0000340 } else {
reed@google.com44a42ea2012-10-01 17:54:05 +0000341 canvas.reset(new SkCanvas(device));
junov@google.com4370aed2012-01-18 16:21:08 +0000342 }
reed@google.com44a42ea2012-10-01 17:54:05 +0000343 invokeGM(gm, canvas);
reed@google.comaf951c92011-06-16 19:10:39 +0000344 // the device is as large as the current rendertarget, so we explicitly
345 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000346 // overwrite our previous allocation
bsalomon@google.comc6980972011-11-02 19:57:21 +0000347 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
348 size.fHeight);
reed@google.com44a42ea2012-10-01 17:54:05 +0000349 canvas->readPixels(bitmap, 0, 0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000350 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000351#endif
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000352 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000353}
354
355static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
356 SkPicture* pict, SkBitmap* bitmap) {
357 SkISize size = gm->getISize();
358 setup_bitmap(gRec, size, bitmap);
359 SkCanvas canvas(*bitmap);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000360 installFilter(&canvas);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000361 canvas.drawPicture(*pict);
362}
363
364static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
365#ifdef SK_SUPPORT_PDF
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000366 SkMatrix initialTransform = gm->getInitialTransform();
367 SkISize pageSize = gm->getISize();
368 SkPDFDevice* dev = NULL;
369 if (initialTransform.isIdentity()) {
370 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
371 } else {
372 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
373 SkIntToScalar(pageSize.height()));
374 initialTransform.mapRect(&content);
375 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
376 SkIntToScalar(pageSize.height()));
377 SkISize contentSize =
378 SkISize::Make(SkScalarRoundToInt(content.width()),
379 SkScalarRoundToInt(content.height()));
380 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
381 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000382 SkAutoUnref aur(dev);
383
384 SkCanvas c(dev);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000385 invokeGM(gm, &c, true);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000386
387 SkPDFDocument doc;
388 doc.appendPage(dev);
389 doc.emitPDF(&pdf);
390#endif
391}
392
bungeman@google.comb29c8832011-10-10 13:19:10 +0000393static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
394#ifdef SK_SUPPORT_XPS
395 SkISize size = gm->getISize();
chudy@google.comf32f6e82012-07-12 15:42:37 +0000396
bungeman@google.comb29c8832011-10-10 13:19:10 +0000397 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
398 SkIntToScalar(size.height()));
bungeman@google.comdc9a6952012-01-05 16:56:29 +0000399 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
400 static const SkScalar upm = 72 * inchesPerMeter;
401 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
402 static const SkScalar ppm = 200 * inchesPerMeter;
403 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000404
405 SkXPSDevice* dev = new SkXPSDevice();
406 SkAutoUnref aur(dev);
407
408 SkCanvas c(dev);
409 dev->beginPortfolio(&xps);
410 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000411 invokeGM(gm, &c);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000412 dev->endSheet();
413 dev->endPortfolio();
414
415#endif
416}
417
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000418static ErrorBitfield write_reference_image(const ConfigData& gRec,
419 const char writePath [],
420 const char renderModeDescriptor [],
421 const SkString& name,
422 SkBitmap& bitmap,
423 SkDynamicMemoryWStream* document) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000424 SkString path;
425 bool success = false;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000426 if (gRec.fBackend == kRaster_Backend ||
427 gRec.fBackend == kGPU_Backend ||
428 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
chudy@google.comf32f6e82012-07-12 15:42:37 +0000429
tomhudson@google.comea325432011-06-09 20:30:03 +0000430 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000431 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000432 }
433 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000434 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000435 success = write_document(path, *document);
436 }
437 if (kXPS_Backend == gRec.fBackend) {
438 path = make_filename(writePath, renderModeDescriptor, name, "xps");
439 success = write_document(path, *document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000440 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000441 if (success) {
442 return ERROR_NONE;
443 } else {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000444 fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000445 return ERROR_WRITING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000446 }
447}
448
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000449// Compares bitmap "bitmap" to "referenceBitmap"; if they are
450// different, writes out "bitmap" (in PNG format) within the diffPath subdir.
451//
452// Returns the ErrorBitfield from compare(), describing any differences
453// between "bitmap" and "referenceBitmap" (or ERROR_NONE if there are none).
454static ErrorBitfield compare_to_reference_image_in_memory(const SkString& name,
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000455 SkBitmap &bitmap,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000456 const SkBitmap& referenceBitmap,
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000457 const char diffPath [],
scroggo@google.com30efb8a2012-10-04 22:02:46 +0000458 const char renderModeDescriptor []) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000459 ErrorBitfield errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000460 SkBitmap diffBitmap;
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000461 errors = compare(bitmap, referenceBitmap, name, renderModeDescriptor,
scroggo@google.com30efb8a2012-10-04 22:02:46 +0000462 diffPath ? &diffBitmap : NULL);
reed@google.com8e529b72012-04-09 20:20:10 +0000463 if ((ERROR_NONE != errors) && diffPath) {
464 // write out the generated image
465 SkString genName = make_filename(diffPath, "", name, "png");
466 if (!write_bitmap(genName, bitmap)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000467 errors |= ERROR_WRITING_REFERENCE_IMAGE;
468 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000469 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000470 return errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000471}
472
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000473// Compares bitmap "bitmap" to a reference bitmap read from disk; if they are
474// different, writes out "bitmap" (in PNG format) within the diffPath subdir.
475//
476// Returns a description of the difference between "bitmap" and the reference
477// bitmap, or ERROR_READING_REFERENCE_IMAGE if unable to read the reference
478// bitmap from disk.
479static ErrorBitfield compare_to_reference_image_on_disk(const char readPath [],
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000480 const SkString& name,
481 SkBitmap &bitmap,
482 const char diffPath [],
483 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000484 SkString path = make_filename(readPath, "", name, "png");
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000485 SkBitmap referenceBitmap;
486 if (SkImageDecoder::DecodeFile(path.c_str(), &referenceBitmap,
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000487 SkBitmap::kARGB_8888_Config,
488 SkImageDecoder::kDecodePixels_Mode, NULL)) {
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000489 return compare_to_reference_image_in_memory(name, bitmap,
490 referenceBitmap, diffPath,
491 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000492 } else {
reed@google.come8fcb502012-05-17 15:28:20 +0000493 if (gNotifyMissingReadReference) {
494 fprintf(stderr, "FAILED to read %s\n", path.c_str());
495 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000496 return ERROR_READING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000497 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000498}
499
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000500// NOTE: As far as I can tell, this function is NEVER called with a
501// non-blank renderModeDescriptor, EXCEPT with readPath and writePath are
502// both NULL (and thus no images are read from or written to disk).
503// So I don't trust that the renderModeDescriptor is being used for
504// anything other than debug output these days.
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000505static ErrorBitfield handle_test_results(GM* gm,
506 const ConfigData& gRec,
507 const char writePath [],
508 const char readPath [],
509 const char diffPath [],
510 const char renderModeDescriptor [],
511 SkBitmap& bitmap,
512 SkDynamicMemoryWStream* pdf,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000513 const SkBitmap* referenceBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000514 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000515 ErrorBitfield retval = ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000516
bsalomon@google.com7361f542012-04-19 19:15:35 +0000517 if (readPath && (gRec.fFlags & kRead_ConfigFlag)) {
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000518 retval |= compare_to_reference_image_on_disk(readPath, name, bitmap,
519 diffPath, renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000520 }
bsalomon@google.com7361f542012-04-19 19:15:35 +0000521 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com9284ccd2012-04-18 13:36:54 +0000522 retval |= write_reference_image(gRec, writePath, renderModeDescriptor,
523 name, bitmap, pdf);
524 }
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000525 if (referenceBitmap) {
526 retval |= compare_to_reference_image_in_memory(name, bitmap,
527 *referenceBitmap, diffPath,
528 renderModeDescriptor);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000529 }
530 return retval;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000531}
532
533static SkPicture* generate_new_picture(GM* gm) {
534 // Pictures are refcounted so must be on heap
535 SkPicture* pict = new SkPicture;
scroggo@google.com977e6622012-09-21 19:12:32 +0000536 SkISize size = gm->getISize();
537 SkCanvas* cv = pict->beginRecording(size.width(), size.height());
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000538 invokeGM(gm, cv);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000539 pict->endRecording();
540
541 return pict;
542}
543
544static SkPicture* stream_to_new_picture(const SkPicture& src) {
545
546 // To do in-memory commiunications with a stream, we need to:
547 // * create a dynamic memory stream
548 // * copy it into a buffer
549 // * create a read stream from it
550 // ?!?!
551
552 SkDynamicMemoryWStream storage;
scroggo@google.com30efb8a2012-10-04 22:02:46 +0000553 src.serialize(&storage);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000554
555 int streamSize = storage.getOffset();
556 SkAutoMalloc dstStorage(streamSize);
557 void* dst = dstStorage.get();
558 //char* dst = new char [streamSize];
559 //@todo thudson 22 April 2011 when can we safely delete [] dst?
560 storage.copyTo(dst);
561 SkMemoryStream pictReadback(dst, streamSize);
scroggo@google.com30efb8a2012-10-04 22:02:46 +0000562 SkPicture* retval = new SkPicture (&pictReadback);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000563 return retval;
564}
565
566// Test: draw into a bitmap or pdf.
567// Depending on flags, possibly compare to an expected image
568// and possibly output a diff image if it fails to match.
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000569static ErrorBitfield test_drawing(GM* gm,
570 const ConfigData& gRec,
571 const char writePath [],
572 const char readPath [],
573 const char diffPath [],
574 GrContext* context,
575 GrRenderTarget* rt,
576 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000577 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000578
579 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000580 gRec.fBackend == kGPU_Backend) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000581 // Early exit if we can't generate the image.
junov@google.com4370aed2012-01-18 16:21:08 +0000582 ErrorBitfield errors = generate_image(gm, gRec, context, rt, bitmap,
583 false);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000584 if (ERROR_NONE != errors) {
585 return errors;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000586 }
reed@google.com46cce912011-06-29 12:54:46 +0000587 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000588 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000589#if CAN_IMAGE_PDF
bungeman@google.com0f1541f2011-10-10 13:47:06 +0000590 SkAutoDataUnref data(document.copyToData());
robertphillips@google.com5c0b3132012-07-10 17:50:00 +0000591 SkMemoryStream stream(data->data(), data->size());
reed@google.com46cce912011-06-29 12:54:46 +0000592 SkPDFDocumentToBitmap(&stream, bitmap);
593#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000594 } else if (gRec.fBackend == kXPS_Backend) {
595 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000596 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000597 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000598 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000599}
600
junov@google.com4370aed2012-01-18 16:21:08 +0000601static ErrorBitfield test_deferred_drawing(GM* gm,
602 const ConfigData& gRec,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000603 const SkBitmap& referenceBitmap,
junov@google.com4370aed2012-01-18 16:21:08 +0000604 const char diffPath [],
605 GrContext* context,
606 GrRenderTarget* rt) {
607 SkDynamicMemoryWStream document;
608
609 if (gRec.fBackend == kRaster_Backend ||
610 gRec.fBackend == kGPU_Backend) {
611 SkBitmap bitmap;
612 // Early exit if we can't generate the image, but this is
613 // expected in some cases, so don't report a test failure.
614 if (!generate_image(gm, gRec, context, rt, &bitmap, true)) {
615 return ERROR_NONE;
616 }
617 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000618 "-deferred", bitmap, NULL, &referenceBitmap);
junov@google.com4370aed2012-01-18 16:21:08 +0000619 }
620 return ERROR_NONE;
621}
622
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000623static ErrorBitfield test_picture_playback(GM* gm,
624 const ConfigData& gRec,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000625 const SkBitmap& referenceBitmap,
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000626 const char readPath [],
627 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000628 SkPicture* pict = generate_new_picture(gm);
629 SkAutoUnref aur(pict);
630
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000631 if (kRaster_Backend == gRec.fBackend) {
632 SkBitmap bitmap;
633 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000634 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000635 "-replay", bitmap, NULL, &referenceBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000636 } else {
637 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000638 }
639}
640
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000641static ErrorBitfield test_picture_serialization(GM* gm,
642 const ConfigData& gRec,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000643 const SkBitmap& referenceBitmap,
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000644 const char readPath [],
645 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000646 SkPicture* pict = generate_new_picture(gm);
647 SkAutoUnref aurp(pict);
648 SkPicture* repict = stream_to_new_picture(*pict);
649 SkAutoUnref aurr(repict);
650
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000651 if (kRaster_Backend == gRec.fBackend) {
652 SkBitmap bitmap;
653 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000654 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000655 "-serialize", bitmap, NULL, &referenceBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000656 } else {
657 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000658 }
659}
660
scroggo@google.com565254b2012-06-28 15:41:32 +0000661struct PipeFlagComboData {
662 const char* name;
663 uint32_t flags;
664};
665
666static PipeFlagComboData gPipeWritingFlagCombos[] = {
667 { "", 0 },
668 { " cross-process", SkGPipeWriter::kCrossProcess_Flag },
scroggob3c0f482012-07-02 19:07:57 +0000669 { " cross-process, shared address", SkGPipeWriter::kCrossProcess_Flag
scroggo@google.com15011ee2012-07-26 20:03:32 +0000670 | SkGPipeWriter::kSharedAddressSpace_Flag }
scroggo@google.com565254b2012-06-28 15:41:32 +0000671};
672
scroggo@google.com5af9b202012-06-04 17:17:36 +0000673static ErrorBitfield test_pipe_playback(GM* gm,
674 const ConfigData& gRec,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000675 const SkBitmap& referenceBitmap,
scroggo@google.com5af9b202012-06-04 17:17:36 +0000676 const char readPath [],
677 const char diffPath []) {
678 if (kRaster_Backend != gRec.fBackend) {
679 return ERROR_NONE;
680 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000681 ErrorBitfield errors = ERROR_NONE;
682 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
683 SkBitmap bitmap;
684 SkISize size = gm->getISize();
685 setup_bitmap(gRec, size, &bitmap);
686 SkCanvas canvas(bitmap);
687 PipeController pipeController(&canvas);
688 SkGPipeWriter writer;
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000689 SkCanvas* pipeCanvas = writer.startRecording(
690 &pipeController, gPipeWritingFlagCombos[i].flags);
scroggo@google.com565254b2012-06-28 15:41:32 +0000691 invokeGM(gm, pipeCanvas);
692 writer.endRecording();
693 SkString string("-pipe");
694 string.append(gPipeWritingFlagCombos[i].name);
695 errors |= handle_test_results(gm, gRec, NULL, NULL, diffPath,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000696 string.c_str(), bitmap, NULL,
697 &referenceBitmap);
scroggo@google.com565254b2012-06-28 15:41:32 +0000698 if (errors != ERROR_NONE) {
699 break;
700 }
701 }
702 return errors;
scroggo@google.com5af9b202012-06-04 17:17:36 +0000703}
704
scroggo@google.com72c96722012-06-06 21:07:10 +0000705static ErrorBitfield test_tiled_pipe_playback(GM* gm,
706 const ConfigData& gRec,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000707 const SkBitmap& referenceBitmap,
scroggo@google.com72c96722012-06-06 21:07:10 +0000708 const char readPath [],
709 const char diffPath []) {
710 if (kRaster_Backend != gRec.fBackend) {
711 return ERROR_NONE;
712 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000713 ErrorBitfield errors = ERROR_NONE;
714 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
715 SkBitmap bitmap;
716 SkISize size = gm->getISize();
717 setup_bitmap(gRec, size, &bitmap);
718 SkCanvas canvas(bitmap);
719 TiledPipeController pipeController(bitmap);
720 SkGPipeWriter writer;
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000721 SkCanvas* pipeCanvas = writer.startRecording(
722 &pipeController, gPipeWritingFlagCombos[i].flags);
scroggo@google.com565254b2012-06-28 15:41:32 +0000723 invokeGM(gm, pipeCanvas);
724 writer.endRecording();
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000725 SkString string("-tiled pipe");
scroggo@google.com565254b2012-06-28 15:41:32 +0000726 string.append(gPipeWritingFlagCombos[i].name);
727 errors |= handle_test_results(gm, gRec, NULL, NULL, diffPath,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000728 string.c_str(), bitmap, NULL,
729 &referenceBitmap);
scroggo@google.com565254b2012-06-28 15:41:32 +0000730 if (errors != ERROR_NONE) {
731 break;
732 }
733 }
734 return errors;
scroggo@google.com72c96722012-06-06 21:07:10 +0000735}
736
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000737static void write_picture_serialization(GM* gm, const ConfigData& rec,
738 const char writePicturePath[]) {
739 // only do this once, so we pick raster
740 if (kRaster_Backend == rec.fBackend &&
741 SkBitmap::kARGB_8888_Config == rec.fConfig) {
742
743 SkAutoTUnref<SkPicture> pict(generate_new_picture(gm));
chudy@google.comf32f6e82012-07-12 15:42:37 +0000744
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000745 const char* pictureSuffix = "skp";
746 SkString path = make_filename(writePicturePath, "",
747 SkString(gm->shortName()), pictureSuffix);
chudy@google.comf32f6e82012-07-12 15:42:37 +0000748
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000749 SkFILEWStream stream(path.c_str());
750 pict->serialize(&stream);
751 }
752}
753
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000754#if SK_SUPPORT_GPU
755static const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_GLContextType;
756#else
757static const GLContextType kDontCare_GLContextType = 0;
758#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +0000759
760// If the platform does not support writing PNGs of PDFs then there will be no
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000761// reference images to read. However, we can always write the .pdf files
bsalomon@google.com7361f542012-04-19 19:15:35 +0000762static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
763 kWrite_ConfigFlag;
764
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000765static const ConfigData gRec[] = {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000766 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888" },
767 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444" },
768 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000769#if defined(SK_SCALAR_IS_FLOAT) && SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +0000770 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu" },
robertphillips@google.coma73e8602012-08-02 17:56:02 +0000771#ifndef SK_BUILD_FOR_ANDROID
772 // currently we don't want to run MSAA tests on Android
bsalomon@google.com7361f542012-04-19 19:15:35 +0000773 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16" },
robertphillips@google.coma73e8602012-08-02 17:56:02 +0000774#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +0000775 /* The debug context does not generate images */
776 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "debug" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000777#if SK_ANGLE
bsalomon@google.com7361f542012-04-19 19:15:35 +0000778 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle" },
779 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000780#endif // SK_ANGLE
781#ifdef SK_MESA
bsalomon@google.com7361f542012-04-19 19:15:35 +0000782 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000783#endif // SK_MESA
robertphillips@google.coma73e8602012-08-02 17:56:02 +0000784#endif // defined(SK_SCALAR_IS_FLOAT) && SK_SUPPORT_GPU
bungeman@google.comb29c8832011-10-10 13:19:10 +0000785#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +0000786 /* At present we have no way of comparing XPS files (either natively or by converting to PNG). */
787 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps" },
robertphillips@google.coma73e8602012-08-02 17:56:02 +0000788#endif // SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +0000789#ifdef SK_SUPPORT_PDF
790 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000791#endif // SK_SUPPORT_PDF
reed@android.com00dae862009-06-10 15:38:48 +0000792};
793
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000794static void usage(const char * argv0) {
795 SkDebugf("%s\n", argv0);
796 SkDebugf(" [-w writePath] [-r readPath] [-d diffPath] [-i resourcePath]\n");
chudy@google.comf32f6e82012-07-12 15:42:37 +0000797 SkDebugf(" [-wp writePicturePath]\n");
reed@google.come5f48b92012-06-22 15:27:39 +0000798 SkDebugf(" [--config ");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000799 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
800 if (i > 0) {
801 SkDebugf("|");
802 }
803 SkDebugf(gRec[i].fName);
804 }
805 SkDebugf(" ]\n");
scroggo@google.com39362522012-09-14 17:37:46 +0000806 SkDebugf(" [--noreplay] [--nopipe] [--noserialize] [--forceBWtext] [--nopdf] \n"
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000807 " [--tiledPipe] \n"
808 " [--nodeferred] [--match substring] [--notexturecache]\n"
809 " [-h|--help]\n"
810 );
811 SkDebugf(" writePath: directory to write rendered images in.\n");
chudy@google.comf32f6e82012-07-12 15:42:37 +0000812 SkDebugf(" writePicturePath: directory to write images to in .skp format.\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000813 SkDebugf(
814 " readPath: directory to read reference images from;\n"
815 " reports if any pixels mismatch between reference and new images\n");
816 SkDebugf(" diffPath: directory to write difference images in.\n");
817 SkDebugf(" resourcePath: directory that stores image resources.\n");
818 SkDebugf(" --noreplay: do not exercise SkPicture replay.\n");
scroggo@google.com565254b2012-06-28 15:41:32 +0000819 SkDebugf(" --nopipe: Skip SkGPipe replay.\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000820 SkDebugf(" --tiledPipe: Exercise tiled SkGPipe replay.\n");
821 SkDebugf(
scroggo@google.com39362522012-09-14 17:37:46 +0000822 " --noserialize: do not exercise SkPicture serialization & deserialization.\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000823 SkDebugf(" --forceBWtext: disable text anti-aliasing.\n");
824 SkDebugf(" --nopdf: skip the pdf rendering test pass.\n");
825 SkDebugf(" --nodeferred: skip the deferred rendering test pass.\n");
826 SkDebugf(" --match foo: will only run tests that substring match foo.\n");
827 SkDebugf(" --notexturecache: disable the gpu texture cache.\n");
828 SkDebugf(" -h|--help : Show this help message. \n");
829}
830
831static int findConfig(const char config[]) {
832 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
833 if (!strcmp(config, gRec[i].fName)) {
834 return i;
835 }
836 }
837 return -1;
838}
839
reed@google.comb2a51622011-10-31 16:30:04 +0000840static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
841 if (0 == array.count()) {
842 // no names, so don't skip anything
843 return false;
844 }
845 for (int i = 0; i < array.count(); ++i) {
846 if (strstr(name, array[i])) {
847 // found the name, so don't skip
848 return false;
849 }
850 }
851 return true;
852}
853
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000854namespace skiagm {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000855#if SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +0000856SkAutoTUnref<GrContext> gGrContext;
857/**
858 * Sets the global GrContext, accessible by indivual GMs
859 */
caryclark@google.com13130862012-06-06 12:10:45 +0000860static void SetGr(GrContext* grContext) {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000861 SkSafeRef(grContext);
862 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000863}
bsalomon@google.com7361f542012-04-19 19:15:35 +0000864
865/**
866 * Gets the global GrContext, can be called by GM tests.
867 */
caryclark@google.com13130862012-06-06 12:10:45 +0000868GrContext* GetGr();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000869GrContext* GetGr() {
870 return gGrContext.get();
871}
872
873/**
874 * Sets the global GrContext and then resets it to its previous value at
875 * destruction.
876 */
877class AutoResetGr : SkNoncopyable {
878public:
879 AutoResetGr() : fOld(NULL) {}
880 void set(GrContext* context) {
881 SkASSERT(NULL == fOld);
882 fOld = GetGr();
883 SkSafeRef(fOld);
884 SetGr(context);
885 }
886 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
887private:
888 GrContext* fOld;
889};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000890#else
891GrContext* GetGr() { return NULL; }
892#endif
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000893}
894
caryclark@google.com5987f582012-10-02 18:33:14 +0000895int tool_main(int argc, char** argv);
896int tool_main(int argc, char** argv) {
robertphillips@google.comb74af872012-06-27 19:41:42 +0000897
898#ifdef SK_ENABLE_INST_COUNT
899 gPrintInstCount = true;
900#endif
901
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000902 SkGraphics::Init();
reed@google.com8923c6c2011-11-08 14:59:38 +0000903 // we don't need to see this during a run
904 gSkSuppressFontCachePurgeSpew = true;
reed@google.comd4dfd102011-01-18 21:05:42 +0000905
epoger@google.com7bc13a62012-02-14 14:53:59 +0000906 setSystemPreferences();
907
reed@android.com8015dd82009-06-21 00:49:18 +0000908 const char* writePath = NULL; // if non-null, where we write the originals
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000909 const char* writePicturePath = NULL; // if non-null, where we write serialized pictures
reed@android.com8015dd82009-06-21 00:49:18 +0000910 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000911 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000912 const char* resourcePath = NULL;// if non-null, where we read from for image resources
reed@android.com8015dd82009-06-21 00:49:18 +0000913
reed@google.comb2a51622011-10-31 16:30:04 +0000914 SkTDArray<const char*> fMatches;
twiz@google.come24a0792012-01-31 18:35:30 +0000915
reed@google.comab973972011-09-19 19:01:38 +0000916 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000917 bool doReplay = true;
scroggo@google.com565254b2012-06-28 15:41:32 +0000918 bool doPipe = true;
scroggo@google.com72c96722012-06-06 21:07:10 +0000919 bool doTiledPipe = false;
scroggo@google.com39362522012-09-14 17:37:46 +0000920 bool doSerialize = true;
junov@google.com4370aed2012-01-18 16:21:08 +0000921 bool doDeferred = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000922 bool disableTextureCache = false;
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000923 SkTDArray<size_t> configs;
924 bool userConfig = false;
twiz@google.come24a0792012-01-31 18:35:30 +0000925
reed@google.comae7b8f32012-10-18 21:30:57 +0000926 int moduloIndex = -1;
927 int moduloCount = -1;
928
reed@google.come8fcb502012-05-17 15:28:20 +0000929 gNotifyMissingReadReference = true;
930
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000931 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000932 char* const* stop = argv + argc;
933 for (++argv; argv < stop; ++argv) {
934 if (strcmp(*argv, "-w") == 0) {
935 argv++;
936 if (argv < stop && **argv) {
937 writePath = *argv;
938 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000939 } else if (strcmp(*argv, "-wp") == 0) {
940 argv++;
941 if (argv < stop && **argv) {
942 writePicturePath = *argv;
943 }
reed@android.com8015dd82009-06-21 00:49:18 +0000944 } else if (strcmp(*argv, "-r") == 0) {
945 argv++;
946 if (argv < stop && **argv) {
947 readPath = *argv;
948 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000949 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000950 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000951 if (argv < stop && **argv) {
952 diffPath = *argv;
953 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000954 } else if (strcmp(*argv, "-i") == 0) {
955 argv++;
956 if (argv < stop && **argv) {
957 resourcePath = *argv;
958 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000959 } else if (strcmp(*argv, "--forceBWtext") == 0) {
960 gForceBWtext = true;
scroggo@google.com565254b2012-06-28 15:41:32 +0000961 } else if (strcmp(*argv, "--nopipe") == 0) {
962 doPipe = false;
scroggo@google.com72c96722012-06-06 21:07:10 +0000963 } else if (strcmp(*argv, "--tiledPipe") == 0) {
964 doTiledPipe = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000965 } else if (strcmp(*argv, "--noreplay") == 0) {
966 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000967 } else if (strcmp(*argv, "--nopdf") == 0) {
968 doPDF = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000969 } else if (strcmp(*argv, "--nodeferred") == 0) {
970 doDeferred = false;
reed@google.comae7b8f32012-10-18 21:30:57 +0000971 } else if (strcmp(*argv, "--modulo") == 0) {
972 ++argv;
973 if (argv >= stop) {
974 continue;
975 }
976 moduloIndex = atoi(*argv);
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000977
reed@google.comae7b8f32012-10-18 21:30:57 +0000978 ++argv;
979 if (argv >= stop) {
980 continue;
981 }
982 moduloCount = atoi(*argv);
reed@google.come8fcb502012-05-17 15:28:20 +0000983 } else if (strcmp(*argv, "--disable-missing-warning") == 0) {
984 gNotifyMissingReadReference = false;
985 } else if (strcmp(*argv, "--enable-missing-warning") == 0) {
986 gNotifyMissingReadReference = true;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000987 } else if (strcmp(*argv, "--serialize") == 0) {
scroggo@google.com39362522012-09-14 17:37:46 +0000988 // Leaving in this option so that a user need not modify their command line arguments
989 // to still run.
borenet@google.com14ca1d32012-06-15 13:46:44 +0000990 doSerialize = true;
scroggo@google.com39362522012-09-14 17:37:46 +0000991 } else if (strcmp(*argv, "--noserialize") == 0) {
992 doSerialize = false;
reed@google.comece2b022011-07-25 14:28:57 +0000993 } else if (strcmp(*argv, "--match") == 0) {
994 ++argv;
995 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000996 // just record the ptr, no need for a deep copy
997 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000998 }
twiz@google.come24a0792012-01-31 18:35:30 +0000999 } else if (strcmp(*argv, "--notexturecache") == 0) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001000 disableTextureCache = true;
reed@google.come5f48b92012-06-22 15:27:39 +00001001 } else if (strcmp(*argv, "--config") == 0) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001002 argv++;
1003 if (argv < stop) {
1004 int index = findConfig(*argv);
1005 if (index >= 0) {
1006 *configs.append() = index;
1007 userConfig = true;
1008 } else {
1009 SkString str;
1010 str.printf("unrecognized config %s\n", *argv);
1011 SkDebugf(str.c_str());
1012 usage(commandName);
1013 return -1;
1014 }
1015 } else {
reed@google.come5f48b92012-06-22 15:27:39 +00001016 SkDebugf("missing arg for --config\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001017 usage(commandName);
1018 return -1;
1019 }
1020 } else if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
1021 usage(commandName);
1022 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001023 } else {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001024 usage(commandName);
1025 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001026 }
1027 }
1028 if (argv != stop) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001029 usage(commandName);
1030 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001031 }
reed@google.com873cb1e2010-12-23 15:00:45 +00001032
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001033 if (!userConfig) {
1034 // if no config is specified by user, we add them all.
1035 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1036 *configs.append() = i;
1037 }
1038 }
1039
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001040 GM::SetResourcePath(resourcePath);
1041
reed@android.com00f883e2010-12-14 17:46:14 +00001042 if (readPath) {
1043 fprintf(stderr, "reading from %s\n", readPath);
chudy@google.comf32f6e82012-07-12 15:42:37 +00001044 }
epoger@google.com9284ccd2012-04-18 13:36:54 +00001045 if (writePath) {
reed@android.com00f883e2010-12-14 17:46:14 +00001046 fprintf(stderr, "writing to %s\n", writePath);
1047 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +00001048 if (writePicturePath) {
1049 fprintf(stderr, "writing pictures to %s\n", writePicturePath);
1050 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001051 if (resourcePath) {
1052 fprintf(stderr, "reading resources from %s\n", resourcePath);
1053 }
1054
reed@google.comae7b8f32012-10-18 21:30:57 +00001055 if (moduloCount <= 0) {
1056 moduloIndex = -1;
1057 }
1058 if (moduloIndex < 0 || moduloIndex >= moduloCount) {
1059 moduloIndex = -1;
1060 }
1061
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001062 // Accumulate success of all tests.
1063 int testsRun = 0;
1064 int testsPassed = 0;
1065 int testsFailed = 0;
1066 int testsMissingReferenceImages = 0;
1067
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001068#if SK_SUPPORT_GPU
1069 GrContextFactory* grFactory = new GrContextFactory;
twiz@google.come24a0792012-01-31 18:35:30 +00001070 if (disableTextureCache) {
1071 skiagm::GetGr()->setTextureCacheLimits(0, 0);
1072 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001073#endif
twiz@google.come24a0792012-01-31 18:35:30 +00001074
robertphillips@google.coma2f80082012-08-02 16:22:47 +00001075 SkTArray<SkString> failedTests;
1076
reed@google.comae7b8f32012-10-18 21:30:57 +00001077 int gmIndex = -1;
1078 SkString moduloStr;
1079
bsalomon@google.com7361f542012-04-19 19:15:35 +00001080 Iter iter;
1081 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +00001082 while ((gm = iter.next()) != NULL) {
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +00001083
reed@google.comae7b8f32012-10-18 21:30:57 +00001084 ++gmIndex;
1085 if (moduloIndex >= 0) {
1086 if ((gmIndex % moduloCount) != moduloIndex) {
1087 continue;
1088 }
1089 moduloStr.printf("[%d % %d] ", gmIndex, moduloCount);
1090 }
1091
reed@google.comece2b022011-07-25 14:28:57 +00001092 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +00001093 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +00001094 SkDELETE(gm);
1095 continue;
1096 }
1097
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001098 SkISize size = gm->getISize();
reed@google.comae7b8f32012-10-18 21:30:57 +00001099 SkDebugf("%sdrawing... %s [%d %d]\n", moduloStr.c_str(), shortName,
reed@android.com8015dd82009-06-21 00:49:18 +00001100 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +00001101 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +00001102
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001103 for (int i = 0; i < configs.count(); i++) {
1104 ConfigData config = gRec[configs[i]];
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001105 // Skip any tests that we don't even need to try.
bsalomon@google.com29d35012011-11-30 16:57:21 +00001106 uint32_t gmFlags = gm->getFlags();
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001107 if ((kPDF_Backend == config.fBackend) &&
bungeman@google.com64e011a2011-09-19 19:31:04 +00001108 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
1109 {
reed@google.comab973972011-09-19 19:01:38 +00001110 continue;
1111 }
reed@google.com1b6c73d2012-10-10 15:17:24 +00001112 if ((gmFlags & GM::kSkip565_Flag) &&
1113 (kRaster_Backend == config.fBackend) &&
1114 (SkBitmap::kRGB_565_Config == config.fConfig)) {
1115 continue;
1116 }
reed@google.comab973972011-09-19 19:01:38 +00001117
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001118 // Now we know that we want to run this test and record its
1119 // success or failure.
1120 ErrorBitfield testErrors = ERROR_NONE;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001121 GrRenderTarget* renderTarget = NULL;
1122#if SK_SUPPORT_GPU
1123 SkAutoTUnref<GrRenderTarget> rt;
1124 AutoResetGr autogr;
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001125 if ((ERROR_NONE == testErrors) &&
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001126 kGPU_Backend == config.fBackend) {
1127 GrContext* gr = grFactory->get(config.fGLContextType);
1128 bool grSuccess = false;
1129 if (gr) {
1130 // create a render target to back the device
1131 GrTextureDesc desc;
1132 desc.fConfig = kSkia8888_PM_GrPixelConfig;
1133 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1134 desc.fWidth = gm->getISize().width();
1135 desc.fHeight = gm->getISize().height();
1136 desc.fSampleCnt = config.fSampleCnt;
1137 GrTexture* tex = gr->createUncachedTexture(desc, NULL, 0);
1138 if (tex) {
1139 rt.reset(tex->asRenderTarget());
1140 rt.get()->ref();
1141 tex->unref();
1142 autogr.set(gr);
1143 renderTarget = rt.get();
1144 grSuccess = NULL != renderTarget;
1145 }
1146 }
1147 if (!grSuccess) {
1148 testErrors |= ERROR_NO_GPU_CONTEXT;
1149 }
tomhudson@google.com73fb0422011-04-25 19:20:54 +00001150 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001151#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +00001152
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001153 if (ERROR_NONE == testErrors) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001154 testErrors |= test_drawing(gm, config,
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001155 writePath, readPath, diffPath,
bsalomon@google.com7361f542012-04-19 19:15:35 +00001156 GetGr(),
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001157 renderTarget, &forwardRenderedBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001158 }
1159
junov@google.com4370aed2012-01-18 16:21:08 +00001160 if (doDeferred && !testErrors &&
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001161 (kGPU_Backend == config.fBackend ||
1162 kRaster_Backend == config.fBackend)) {
1163 testErrors |= test_deferred_drawing(gm, config,
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001164 forwardRenderedBitmap,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001165 diffPath, GetGr(), renderTarget);
junov@google.com4370aed2012-01-18 16:21:08 +00001166 }
1167
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001168 if ((ERROR_NONE == testErrors) && doReplay &&
1169 !(gmFlags & GM::kSkipPicture_Flag)) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001170 testErrors |= test_picture_playback(gm, config,
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001171 forwardRenderedBitmap,
1172 readPath, diffPath);
1173 }
1174
scroggo@google.com5af9b202012-06-04 17:17:36 +00001175 if ((ERROR_NONE == testErrors) && doPipe &&
1176 !(gmFlags & GM::kSkipPipe_Flag)) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001177 testErrors |= test_pipe_playback(gm, config,
scroggo@google.com5af9b202012-06-04 17:17:36 +00001178 forwardRenderedBitmap,
1179 readPath, diffPath);
1180 }
1181
scroggo@google.com72c96722012-06-06 21:07:10 +00001182 if ((ERROR_NONE == testErrors) && doTiledPipe &&
scroggo@google.com63258862012-08-15 16:32:19 +00001183 !SkToBool(gmFlags & (GM::kSkipPipe_Flag | GM::kSkipTiled_Flag))) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001184 testErrors |= test_tiled_pipe_playback(gm, config,
scroggo@google.com72c96722012-06-06 21:07:10 +00001185 forwardRenderedBitmap,
1186 readPath, diffPath);
1187 }
1188
djsollen@google.coma2ca41e2012-03-23 19:00:34 +00001189 if ((ERROR_NONE == testErrors) && doSerialize &&
1190 !(gmFlags & GM::kSkipPicture_Flag)) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001191 testErrors |= test_picture_serialization(gm, config,
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001192 forwardRenderedBitmap,
1193 readPath, diffPath);
1194 }
chudy@google.comf32f6e82012-07-12 15:42:37 +00001195
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +00001196 if (!(gmFlags & GM::kSkipPicture_Flag) && writePicturePath) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001197 write_picture_serialization(gm, config, writePicturePath);
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +00001198 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001199
1200 // Update overall results.
1201 // We only tabulate the particular error types that we currently
1202 // care about (e.g., missing reference images). Later on, if we
1203 // want to also tabulate pixel mismatches vs dimension mistmatches
1204 // (or whatever else), we can do so.
1205 testsRun++;
1206 if (ERROR_NONE == testErrors) {
1207 testsPassed++;
1208 } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) {
1209 testsMissingReferenceImages++;
1210 } else {
1211 testsFailed++;
robertphillips@google.coma2f80082012-08-02 16:22:47 +00001212
1213 failedTests.push_back(make_name(shortName, config.fName));
tomhudson@google.com73fb0422011-04-25 19:20:54 +00001214 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001215 }
reed@android.com00dae862009-06-10 15:38:48 +00001216 SkDELETE(gm);
1217 }
robertphillips@google.coma2f80082012-08-02 16:22:47 +00001218 SkDebugf("Ran %d tests: %d passed, %d failed, %d missing reference images\n",
1219 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
1220 for (int i = 0; i < failedTests.count(); ++i) {
1221 SkDebugf("\t\t%s\n", failedTests[i].c_str());
1222 }
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001223
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001224#if SK_SUPPORT_GPU
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001225
robertphillips@google.com59552022012-08-31 13:07:37 +00001226#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001227 for (int i = 0; i < configs.count(); i++) {
1228 ConfigData config = gRec[configs[i]];
1229
1230 if (kGPU_Backend == config.fBackend) {
1231 GrContext* gr = grFactory->get(config.fGLContextType);
1232
1233 SkDebugf("config: %s %x\n", config.fName, gr);
1234 gr->printCacheStats();
1235 }
1236 }
1237#endif
1238
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001239 delete grFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001240#endif
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001241 SkGraphics::Term();
1242
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001243 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +00001244}
caryclark@google.com5987f582012-10-02 18:33:14 +00001245
1246#if !defined SK_BUILD_FOR_IOS
1247int main(int argc, char * const argv[]) {
1248 return tool_main(argc, (char**) argv);
1249}
1250#endif