Home | History | Annotate | Download | only in dm
      1 #include "DMUtil.h"
      2 
      3 #include "SkColorPriv.h"
      4 #include "SkCommandLineFlags.h"
      5 #include "SkPicture.h"
      6 #include "SkPictureRecorder.h"
      7 
      8 DEFINE_string(matrix, "1 0 0 0 1 0 0 0 1",
      9               "Matrix to apply to the canvas before drawing.");
     10 
     11 namespace DM {
     12 
     13 void CanvasPreflight(SkCanvas* canvas) {
     14     if (FLAGS_matrix.count() == 9) {
     15         SkMatrix m;
     16         for (int i = 0; i < 9; i++) {
     17             m[i] = (SkScalar)atof(FLAGS_matrix[i]);
     18         }
     19         canvas->concat(m);
     20     }
     21 }
     22 
     23 SkString UnderJoin(const char* a, const char* b) {
     24     SkString s;
     25     s.appendf("%s_%s", a, b);
     26     return s;
     27 }
     28 
     29 SkString FileToTaskName(SkString filename) {
     30     for (size_t i = 0; i < filename.size(); i++) {
     31         if ('_' == filename[i]) { filename[i] = '-'; }
     32         if ('.' == filename[i]) { filename[i] = '_'; }
     33     }
     34     return filename;
     35 }
     36 
     37 SkPicture* RecordPicture(skiagm::GM* gm, SkBBHFactory* factory, bool skr) {
     38     const SkScalar w = SkIntToScalar(gm->getISize().width()),
     39                    h = SkIntToScalar(gm->getISize().height());
     40     SkPictureRecorder recorder;
     41 
     42     SkCanvas* canvas = skr ? recorder.EXPERIMENTAL_beginRecording(w, h, factory)
     43                            : recorder.  DEPRECATED_beginRecording(w, h, factory);
     44     CanvasPreflight(canvas);
     45     canvas->concat(gm->getInitialTransform());
     46     gm->draw(canvas);
     47     canvas->flush();
     48     return recorder.endRecording();
     49 }
     50 
     51 void AllocatePixels(SkColorType ct, int width, int height, SkBitmap* bitmap) {
     52     bitmap->allocPixels(SkImageInfo::Make(width, height, ct, kPremul_SkAlphaType));
     53     bitmap->eraseColor(0x00000000);
     54 }
     55 
     56 void AllocatePixels(const SkBitmap& reference, SkBitmap* bitmap) {
     57     AllocatePixels(reference.colorType(), reference.width(), reference.height(), bitmap);
     58 }
     59 
     60 void DrawPicture(const SkPicture& picture, SkBitmap* bitmap) {
     61     SkASSERT(bitmap != NULL);
     62     SkCanvas canvas(*bitmap);
     63     canvas.drawPicture(&picture);
     64     canvas.flush();
     65 }
     66 
     67 static void unpack_565(uint16_t pixel, unsigned* r, unsigned* g, unsigned* b) {
     68     *r = SkGetPackedR16(pixel);
     69     *g = SkGetPackedG16(pixel);
     70     *b = SkGetPackedB16(pixel);
     71 }
     72 
     73 // Returns |a-b|.
     74 static unsigned abs_diff(unsigned a, unsigned b) {
     75     return a > b ? a - b : b - a;
     76 }
     77 
     78 unsigned MaxComponentDifference(const SkBitmap& a, const SkBitmap& b) {
     79     if (a.info() != b.info()) {
     80         SkFAIL("Can't compare bitmaps of different shapes.");
     81     }
     82 
     83     unsigned max = 0;
     84 
     85     const SkAutoLockPixels lockA(a), lockB(b);
     86     if (a.info().colorType() == kRGB_565_SkColorType) {
     87         // 565 is special/annoying because its 3 components straddle 2 bytes.
     88         const uint16_t* aPixels = (const uint16_t*)a.getPixels();
     89         const uint16_t* bPixels = (const uint16_t*)b.getPixels();
     90         for (size_t i = 0; i < a.getSize() / 2; i++) {
     91             unsigned ar, ag, ab,
     92                      br, bg, bb;
     93             unpack_565(aPixels[i], &ar, &ag, &ab);
     94             unpack_565(bPixels[i], &br, &bg, &bb);
     95             max = SkTMax(max, abs_diff(ar, br));
     96             max = SkTMax(max, abs_diff(ag, bg));
     97             max = SkTMax(max, abs_diff(ab, bb));
     98         }
     99     } else {
    100         // Everything else we produce is byte aligned, so max component diff == max byte diff.
    101         const uint8_t* aBytes = (const uint8_t*)a.getPixels();
    102         const uint8_t* bBytes = (const uint8_t*)b.getPixels();
    103         for (size_t i = 0; i < a.getSize(); i++) {
    104             max = SkTMax(max, abs_diff(aBytes[i], bBytes[i]));
    105         }
    106     }
    107 
    108     return max;
    109 }
    110 
    111 bool BitmapsEqual(const SkBitmap& a, const SkBitmap& b) {
    112     if (a.info() != b.info()) {
    113         return false;
    114     }
    115     const SkAutoLockPixels lockA(a), lockB(b);
    116     return 0 == memcmp(a.getPixels(), b.getPixels(), a.getSize());
    117 }
    118 
    119 }  // namespace DM
    120