2009-06-10 15:38:48 +00:00
|
|
|
#include "gm.h"
|
2009-07-08 02:54:47 +00:00
|
|
|
#include "SkColorPriv.h"
|
2009-06-21 00:49:18 +00:00
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
#include "SkImageEncoder.h"
|
2010-12-20 19:46:07 +00:00
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkRefCnt.h"
|
|
|
|
|
2010-12-23 15:00:45 +00:00
|
|
|
#include "GrContext.h"
|
|
|
|
#include "SkGpuCanvas.h"
|
2011-01-18 21:05:42 +00:00
|
|
|
#include "SkGpuDevice.h"
|
2010-12-23 15:00:45 +00:00
|
|
|
#include "SkEGLContext.h"
|
|
|
|
#include "SkDevice.h"
|
|
|
|
|
2010-12-20 19:46:07 +00:00
|
|
|
#ifdef SK_SUPPORT_PDF
|
|
|
|
#include "SkPDFDevice.h"
|
|
|
|
#include "SkPDFDocument.h"
|
|
|
|
#endif
|
2009-06-10 15:38:48 +00:00
|
|
|
|
|
|
|
using namespace skiagm;
|
|
|
|
|
|
|
|
// need to explicitly declare this, or we get some weird infinite loop llist
|
|
|
|
template GMRegistry* GMRegistry::gHead;
|
|
|
|
|
|
|
|
class Iter {
|
|
|
|
public:
|
|
|
|
Iter() {
|
2009-06-20 02:38:16 +00:00
|
|
|
fReg = GMRegistry::Head();
|
2009-06-10 15:38:48 +00:00
|
|
|
}
|
2011-01-18 21:05:42 +00:00
|
|
|
|
2009-06-20 02:38:16 +00:00
|
|
|
GM* next() {
|
2009-06-10 15:38:48 +00:00
|
|
|
if (fReg) {
|
2009-06-20 02:38:16 +00:00
|
|
|
GMRegistry::Factory fact = fReg->factory();
|
2009-06-10 15:38:48 +00:00
|
|
|
fReg = fReg->next();
|
2009-06-20 02:38:16 +00:00
|
|
|
return fact(0);
|
2009-06-10 15:38:48 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-01-18 21:05:42 +00:00
|
|
|
|
2009-06-10 15:38:48 +00:00
|
|
|
static int Count() {
|
2009-06-20 02:38:16 +00:00
|
|
|
const GMRegistry* reg = GMRegistry::Head();
|
2009-06-10 15:38:48 +00:00
|
|
|
int count = 0;
|
|
|
|
while (reg) {
|
|
|
|
count += 1;
|
|
|
|
reg = reg->next();
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2011-01-18 21:05:42 +00:00
|
|
|
|
2009-06-10 15:38:48 +00:00
|
|
|
private:
|
|
|
|
const GMRegistry* fReg;
|
|
|
|
};
|
|
|
|
|
2009-06-21 00:49:18 +00:00
|
|
|
static SkString make_name(const char shortName[], const char configName[]) {
|
|
|
|
SkString name(shortName);
|
|
|
|
name.appendf("_%s", configName);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2010-12-20 19:46:07 +00:00
|
|
|
static SkString make_filename(const char path[], const SkString& name, const char suffix[]) {
|
2009-06-21 00:49:18 +00:00
|
|
|
SkString filename(path);
|
|
|
|
if (filename.size() && filename[filename.size() - 1] != '/') {
|
|
|
|
filename.append("/");
|
2009-06-10 15:38:48 +00:00
|
|
|
}
|
2010-12-20 19:46:07 +00:00
|
|
|
filename.appendf("%s.%s", name.c_str(), suffix);
|
2009-06-21 00:49:18 +00:00
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2009-07-08 02:54:47 +00:00
|
|
|
/* since PNG insists on unpremultiplying our alpha, we take no precision chances
|
|
|
|
and force all pixels to be 100% opaque, otherwise on compare we may not get
|
|
|
|
a perfect match.
|
|
|
|
*/
|
|
|
|
static void force_all_opaque(const SkBitmap& bitmap) {
|
|
|
|
SkAutoLockPixels lock(bitmap);
|
|
|
|
for (int y = 0; y < bitmap.height(); y++) {
|
|
|
|
for (int x = 0; x < bitmap.width(); x++) {
|
|
|
|
*bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
|
|
|
|
SkBitmap copy;
|
|
|
|
bitmap.copyTo(©, SkBitmap::kARGB_8888_Config);
|
|
|
|
force_all_opaque(copy);
|
|
|
|
return SkImageEncoder::EncodeFile(path.c_str(), copy,
|
|
|
|
SkImageEncoder::kPNG_Type, 100);
|
|
|
|
}
|
|
|
|
|
2010-12-20 21:10:29 +00:00
|
|
|
static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
|
|
|
|
int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
|
|
|
|
int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
|
|
|
|
int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
|
|
|
|
return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void compute_diff(const SkBitmap& target, const SkBitmap& base,
|
|
|
|
SkBitmap* diff) {
|
|
|
|
SkAutoLockPixels alp(*diff);
|
|
|
|
|
|
|
|
const int w = target.width();
|
|
|
|
const int h = target.height();
|
|
|
|
for (int y = 0; y < h; y++) {
|
|
|
|
for (int x = 0; x < w; x++) {
|
|
|
|
SkPMColor c0 = *base.getAddr32(x, y);
|
|
|
|
SkPMColor c1 = *target.getAddr32(x, y);
|
|
|
|
SkPMColor d = 0;
|
|
|
|
if (c0 != c1) {
|
|
|
|
d = compute_diff_pmcolor(c0, c1);
|
|
|
|
}
|
|
|
|
*diff->getAddr32(x, y) = d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool compare(const SkBitmap& target, const SkBitmap& base,
|
|
|
|
const SkString& name, SkBitmap* diff) {
|
2009-07-08 02:54:47 +00:00
|
|
|
SkBitmap copy;
|
|
|
|
const SkBitmap* bm = ⌖
|
|
|
|
if (target.config() != SkBitmap::kARGB_8888_Config) {
|
|
|
|
target.copyTo(©, SkBitmap::kARGB_8888_Config);
|
|
|
|
bm = ©
|
|
|
|
}
|
|
|
|
|
|
|
|
force_all_opaque(*bm);
|
|
|
|
|
|
|
|
const int w = bm->width();
|
|
|
|
const int h = bm->height();
|
|
|
|
if (w != base.width() || h != base.height()) {
|
|
|
|
SkDebugf("---- dimensions mismatch for %s base [%d %d] current [%d %d]\n",
|
|
|
|
name.c_str(), base.width(), base.height(), w, h);
|
2010-12-20 21:10:29 +00:00
|
|
|
return false;
|
2009-07-08 02:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkAutoLockPixels bmLock(*bm);
|
|
|
|
SkAutoLockPixels baseLock(base);
|
|
|
|
|
|
|
|
for (int y = 0; y < h; y++) {
|
|
|
|
for (int x = 0; x < w; x++) {
|
|
|
|
SkPMColor c0 = *base.getAddr32(x, y);
|
|
|
|
SkPMColor c1 = *bm->getAddr32(x, y);
|
|
|
|
if (c0 != c1) {
|
|
|
|
SkDebugf("----- pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
|
|
|
|
name.c_str(), x, y, c0, c1);
|
2010-12-20 21:10:29 +00:00
|
|
|
|
|
|
|
if (diff) {
|
|
|
|
diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
|
|
|
|
diff->allocPixels();
|
|
|
|
compute_diff(*bm, base, diff);
|
|
|
|
}
|
|
|
|
return false;
|
2009-07-08 02:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-20 21:10:29 +00:00
|
|
|
|
|
|
|
// they're equal
|
|
|
|
return true;
|
2009-06-21 00:49:18 +00:00
|
|
|
}
|
2009-06-10 15:38:48 +00:00
|
|
|
|
2011-02-03 23:00:40 +00:00
|
|
|
static bool write_pdf(const SkString& path, const SkDynamicMemoryWStream& pdf) {
|
|
|
|
SkFILEWStream stream(path.c_str());
|
|
|
|
return stream.write(pdf.getStream(), pdf.getOffset());
|
2010-12-20 19:46:07 +00:00
|
|
|
}
|
|
|
|
|
2011-02-03 23:00:40 +00:00
|
|
|
enum Backend {
|
|
|
|
kRaster_Backend,
|
|
|
|
kGPU_Backend,
|
|
|
|
kPDF_Backend,
|
|
|
|
};
|
|
|
|
|
2009-06-10 15:38:48 +00:00
|
|
|
static const struct {
|
|
|
|
SkBitmap::Config fConfig;
|
2011-02-03 23:00:40 +00:00
|
|
|
Backend fBackend;
|
2009-06-10 15:38:48 +00:00
|
|
|
const char* fName;
|
|
|
|
} gRec[] = {
|
2011-02-03 23:00:40 +00:00
|
|
|
{ SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
|
|
|
|
{ SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
|
|
|
|
{ SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
|
|
|
|
{ SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
|
|
|
|
#ifdef SK_SUPPORT_PDF
|
|
|
|
{ SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
|
|
|
|
#endif
|
2009-06-10 15:38:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int main (int argc, char * const argv[]) {
|
|
|
|
SkAutoGraphics ag;
|
2011-01-18 21:05:42 +00:00
|
|
|
|
2009-06-21 00:49:18 +00:00
|
|
|
const char* writePath = NULL; // if non-null, where we write the originals
|
|
|
|
const char* readPath = NULL; // if non-null, were we read from to compare
|
2010-12-20 21:10:29 +00:00
|
|
|
const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
|
2009-06-21 00:49:18 +00:00
|
|
|
|
|
|
|
char* const* stop = argv + argc;
|
|
|
|
for (++argv; argv < stop; ++argv) {
|
|
|
|
if (strcmp(*argv, "-w") == 0) {
|
|
|
|
argv++;
|
|
|
|
if (argv < stop && **argv) {
|
|
|
|
writePath = *argv;
|
|
|
|
}
|
|
|
|
} else if (strcmp(*argv, "-r") == 0) {
|
|
|
|
argv++;
|
|
|
|
if (argv < stop && **argv) {
|
|
|
|
readPath = *argv;
|
|
|
|
}
|
2010-12-20 21:10:29 +00:00
|
|
|
} else if (strcmp(*argv, "-d") == 0) {
|
|
|
|
argv++;
|
|
|
|
if (argv < stop && **argv) {
|
|
|
|
diffPath = *argv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-23 15:00:45 +00:00
|
|
|
|
|
|
|
// setup a GL context for drawing offscreen
|
2010-12-23 20:20:51 +00:00
|
|
|
GrContext* context = NULL;
|
2010-12-23 15:00:45 +00:00
|
|
|
SkEGLContext eglContext;
|
2010-12-23 20:20:51 +00:00
|
|
|
if (eglContext.init(1024, 1024)) {
|
|
|
|
context = GrContext::CreateGLShaderContext();
|
|
|
|
}
|
2010-12-23 15:00:45 +00:00
|
|
|
|
2009-06-10 15:38:48 +00:00
|
|
|
Iter iter;
|
|
|
|
GM* gm;
|
2010-12-14 17:46:14 +00:00
|
|
|
|
|
|
|
if (readPath) {
|
|
|
|
fprintf(stderr, "reading from %s\n", readPath);
|
|
|
|
} else if (writePath) {
|
|
|
|
fprintf(stderr, "writing to %s\n", writePath);
|
|
|
|
}
|
|
|
|
|
2009-06-10 15:38:48 +00:00
|
|
|
while ((gm = iter.next()) != NULL) {
|
|
|
|
SkISize size = gm->getISize();
|
2010-12-20 21:10:29 +00:00
|
|
|
SkDebugf("drawing... %s [%d %d]\n", gm->shortName(),
|
2009-06-21 00:49:18 +00:00
|
|
|
size.width(), size.height());
|
|
|
|
|
2009-06-10 15:38:48 +00:00
|
|
|
SkBitmap bitmap;
|
2011-02-03 23:00:40 +00:00
|
|
|
SkDynamicMemoryWStream pdf;
|
2009-06-20 02:38:16 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
|
2011-02-03 23:00:40 +00:00
|
|
|
if (gRec[i].fBackend == kRaster_Backend ||
|
|
|
|
gRec[i].fBackend == kGPU_Backend) {
|
|
|
|
bitmap.setConfig(gRec[i].fConfig, size.width(), size.height());
|
|
|
|
bitmap.allocPixels();
|
|
|
|
bitmap.eraseColor(0);
|
|
|
|
SkCanvas canvas(bitmap);
|
|
|
|
|
|
|
|
if (gRec[i].fBackend == kRaster_Backend) {
|
|
|
|
gm->draw(&canvas);
|
|
|
|
} else { // GPU
|
|
|
|
if (NULL == context) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
SkGpuCanvas gc(context,
|
|
|
|
SkGpuDevice::Current3DApiRenderTarget());
|
|
|
|
gc.setDevice(gc.createDevice(bitmap.config(),
|
|
|
|
bitmap.width(),
|
|
|
|
bitmap.height(),
|
|
|
|
bitmap.isOpaque(),
|
|
|
|
false))->unref();
|
|
|
|
gm->draw(&gc);
|
|
|
|
gc.readPixels(&bitmap); // overwrite our previous allocation
|
2010-12-23 20:20:51 +00:00
|
|
|
}
|
2011-02-03 23:00:40 +00:00
|
|
|
}
|
|
|
|
// TODO: Figure out a way to compare PDFs.
|
|
|
|
if (gRec[i].fBackend == kPDF_Backend && writePath) {
|
|
|
|
#ifdef SK_SUPPORT_PDF
|
|
|
|
SkISize size = gm->getISize();
|
2011-04-11 23:24:18 +00:00
|
|
|
SkMatrix identity;
|
|
|
|
identity.reset();
|
|
|
|
SkPDFDevice* dev = new SkPDFDevice(size.width(), size.height(),
|
|
|
|
identity);
|
2011-02-03 23:00:40 +00:00
|
|
|
SkAutoUnref aur(dev);
|
|
|
|
|
|
|
|
SkCanvas c(dev);
|
|
|
|
gm->draw(&c);
|
|
|
|
|
|
|
|
SkPDFDocument doc;
|
|
|
|
doc.appendPage(dev);
|
|
|
|
doc.emitPDF(&pdf);
|
|
|
|
#endif
|
2010-12-23 15:00:45 +00:00
|
|
|
}
|
2009-06-21 00:49:18 +00:00
|
|
|
SkString name = make_name(gm->shortName(), gRec[i].fName);
|
|
|
|
|
|
|
|
if (writePath) {
|
2011-02-03 23:00:40 +00:00
|
|
|
SkString path;
|
|
|
|
bool success;
|
|
|
|
if (gRec[i].fBackend != kPDF_Backend) {
|
|
|
|
path = make_filename(writePath, name, "png");
|
|
|
|
success = write_bitmap(path, bitmap);
|
|
|
|
} else {
|
|
|
|
path = make_filename(writePath, name, "pdf");
|
|
|
|
success = write_pdf(path, pdf);
|
2009-06-21 00:49:18 +00:00
|
|
|
}
|
2011-02-03 23:00:40 +00:00
|
|
|
if (!success)
|
|
|
|
fprintf(stderr, "FAILED to write %s\n", path.c_str());
|
|
|
|
// TODO: Figure out a way to compare PDFs.
|
|
|
|
} else if (readPath && gRec[i].fBackend != kPDF_Backend) {
|
2010-12-20 19:46:07 +00:00
|
|
|
SkString path = make_filename(readPath, name, "png");
|
2009-06-21 00:49:18 +00:00
|
|
|
SkBitmap orig;
|
|
|
|
bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig,
|
|
|
|
SkBitmap::kARGB_8888_Config,
|
|
|
|
SkImageDecoder::kDecodePixels_Mode, NULL);
|
|
|
|
if (success) {
|
2010-12-20 21:10:29 +00:00
|
|
|
SkBitmap diffBitmap;
|
|
|
|
success = compare(bitmap, orig, name, diffPath ? &diffBitmap : NULL);
|
|
|
|
if (!success && diffPath) {
|
|
|
|
SkString diffName = make_filename(diffPath, name, ".diff.png");
|
|
|
|
fprintf(stderr, "Writing %s\n", diffName.c_str());
|
|
|
|
write_bitmap(diffName, diffBitmap);
|
|
|
|
}
|
2009-06-21 00:49:18 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "FAILED to read %s\n", path.c_str());
|
|
|
|
}
|
|
|
|
}
|
2009-06-10 15:38:48 +00:00
|
|
|
}
|
|
|
|
SkDELETE(gm);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2009-06-20 02:38:16 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
using namespace skiagm;
|
|
|
|
|
|
|
|
GM::GM() {}
|
|
|
|
GM::~GM() {}
|
|
|
|
|
|
|
|
void GM::draw(SkCanvas* canvas) {
|
|
|
|
this->onDraw(canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
|