2013-10-16 13:02:15 +00:00
|
|
|
#include "DMUtil.h"
|
|
|
|
|
2014-05-16 13:03:46 +00:00
|
|
|
#include "SkColorPriv.h"
|
2014-09-11 19:30:12 +00:00
|
|
|
#include "SkCommandLineFlags.h"
|
2013-10-21 18:40:25 +00:00
|
|
|
#include "SkPicture.h"
|
2014-04-18 18:04:41 +00:00
|
|
|
#include "SkPictureRecorder.h"
|
2013-10-21 18:40:25 +00:00
|
|
|
|
2014-09-11 19:30:12 +00:00
|
|
|
DEFINE_string(matrix, "1 0 0 0 1 0 0 0 1",
|
|
|
|
"Matrix to apply to the canvas before drawing.");
|
|
|
|
|
2013-10-16 13:02:15 +00:00
|
|
|
namespace DM {
|
|
|
|
|
2014-09-11 19:30:12 +00:00
|
|
|
void CanvasPreflight(SkCanvas* canvas) {
|
|
|
|
if (FLAGS_matrix.count() == 9) {
|
|
|
|
SkMatrix m;
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
m[i] = (SkScalar)atof(FLAGS_matrix[i]);
|
|
|
|
}
|
|
|
|
canvas->concat(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-21 18:40:25 +00:00
|
|
|
SkString UnderJoin(const char* a, const char* b) {
|
2013-10-16 13:02:15 +00:00
|
|
|
SkString s;
|
|
|
|
s.appendf("%s_%s", a, b);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-06-06 16:28:43 +00:00
|
|
|
SkString FileToTaskName(SkString filename) {
|
|
|
|
for (size_t i = 0; i < filename.size(); i++) {
|
|
|
|
if ('_' == filename[i]) { filename[i] = '-'; }
|
|
|
|
if ('.' == filename[i]) { filename[i] = '_'; }
|
|
|
|
}
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2014-09-29 15:44:46 +00:00
|
|
|
SkPicture* RecordPicture(skiagm::GM* gm, SkBBHFactory* factory) {
|
2014-08-29 15:03:56 +00:00
|
|
|
const SkScalar w = SkIntToScalar(gm->getISize().width()),
|
|
|
|
h = SkIntToScalar(gm->getISize().height());
|
2014-04-17 23:35:06 +00:00
|
|
|
SkPictureRecorder recorder;
|
2014-06-24 19:28:34 +00:00
|
|
|
|
2014-09-29 15:44:46 +00:00
|
|
|
SkCanvas* canvas = recorder.beginRecording(w, h, factory);
|
2014-09-11 19:30:12 +00:00
|
|
|
CanvasPreflight(canvas);
|
2013-10-21 18:40:25 +00:00
|
|
|
canvas->concat(gm->getInitialTransform());
|
|
|
|
gm->draw(canvas);
|
|
|
|
canvas->flush();
|
2014-04-13 19:09:42 +00:00
|
|
|
return recorder.endRecording();
|
2013-10-21 18:40:25 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 17:33:31 +00:00
|
|
|
void AllocatePixels(SkColorType ct, int width, int height, SkBitmap* bitmap) {
|
2014-02-26 23:01:57 +00:00
|
|
|
bitmap->allocPixels(SkImageInfo::Make(width, height, ct, kPremul_SkAlphaType));
|
2013-10-21 18:40:25 +00:00
|
|
|
bitmap->eraseColor(0x00000000);
|
|
|
|
}
|
|
|
|
|
2014-05-15 17:33:31 +00:00
|
|
|
void AllocatePixels(const SkBitmap& reference, SkBitmap* bitmap) {
|
|
|
|
AllocatePixels(reference.colorType(), reference.width(), reference.height(), bitmap);
|
2014-05-14 17:55:32 +00:00
|
|
|
}
|
|
|
|
|
2014-06-24 19:28:34 +00:00
|
|
|
void DrawPicture(const SkPicture& picture, SkBitmap* bitmap) {
|
2013-10-21 18:40:25 +00:00
|
|
|
SkASSERT(bitmap != NULL);
|
|
|
|
SkCanvas canvas(*bitmap);
|
2014-06-24 19:28:34 +00:00
|
|
|
canvas.drawPicture(&picture);
|
2013-10-21 18:40:25 +00:00
|
|
|
canvas.flush();
|
|
|
|
}
|
|
|
|
|
2014-05-16 13:03:46 +00:00
|
|
|
static void unpack_565(uint16_t pixel, unsigned* r, unsigned* g, unsigned* b) {
|
|
|
|
*r = SkGetPackedR16(pixel);
|
|
|
|
*g = SkGetPackedG16(pixel);
|
|
|
|
*b = SkGetPackedB16(pixel);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns |a-b|.
|
|
|
|
static unsigned abs_diff(unsigned a, unsigned b) {
|
|
|
|
return a > b ? a - b : b - a;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned MaxComponentDifference(const SkBitmap& a, const SkBitmap& b) {
|
|
|
|
if (a.info() != b.info()) {
|
|
|
|
SkFAIL("Can't compare bitmaps of different shapes.");
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned max = 0;
|
|
|
|
|
2013-10-21 18:40:25 +00:00
|
|
|
const SkAutoLockPixels lockA(a), lockB(b);
|
2014-05-16 13:03:46 +00:00
|
|
|
if (a.info().colorType() == kRGB_565_SkColorType) {
|
|
|
|
// 565 is special/annoying because its 3 components straddle 2 bytes.
|
|
|
|
const uint16_t* aPixels = (const uint16_t*)a.getPixels();
|
|
|
|
const uint16_t* bPixels = (const uint16_t*)b.getPixels();
|
|
|
|
for (size_t i = 0; i < a.getSize() / 2; i++) {
|
|
|
|
unsigned ar, ag, ab,
|
|
|
|
br, bg, bb;
|
|
|
|
unpack_565(aPixels[i], &ar, &ag, &ab);
|
|
|
|
unpack_565(bPixels[i], &br, &bg, &bb);
|
|
|
|
max = SkTMax(max, abs_diff(ar, br));
|
|
|
|
max = SkTMax(max, abs_diff(ag, bg));
|
|
|
|
max = SkTMax(max, abs_diff(ab, bb));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Everything else we produce is byte aligned, so max component diff == max byte diff.
|
|
|
|
const uint8_t* aBytes = (const uint8_t*)a.getPixels();
|
|
|
|
const uint8_t* bBytes = (const uint8_t*)b.getPixels();
|
|
|
|
for (size_t i = 0; i < a.getSize(); i++) {
|
|
|
|
max = SkTMax(max, abs_diff(aBytes[i], bBytes[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BitmapsEqual(const SkBitmap& a, const SkBitmap& b) {
|
2014-06-03 20:56:54 +00:00
|
|
|
if (a.info() != b.info()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const SkAutoLockPixels lockA(a), lockB(b);
|
|
|
|
return 0 == memcmp(a.getPixels(), b.getPixels(), a.getSize());
|
2013-10-21 18:40:25 +00:00
|
|
|
}
|
|
|
|
|
2013-10-16 13:02:15 +00:00
|
|
|
} // namespace DM
|