Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright 2014 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  */
      7 
      8 #include "Test.h"
      9 
     10 #include "../include/core/SkBitmap.h"
     11 #include "../include/core/SkCanvas.h"
     12 #include "../include/core/SkPicture.h"
     13 #include "../include/core/SkStream.h"
     14 #include "../include/core/SkString.h"
     15 #include "../include/core/SkPictureRecorder.h"
     16 #include "../src/core/SkBlendModePriv.h"
     17 #include <cstring>
     18 
     19 // Verify that replay of a recording into a clipped canvas
     20 // produces the correct bitmap.
     21 // This arose from http://crbug.com/401593 which has
     22 // https://code.google.com/p/skia/issues/detail?id=1291 as its root cause.
     23 
     24 namespace {
     25 
     26 class Drawer {
     27  public:
     28     explicit Drawer() : fImageInfo(SkImageInfo::MakeN32Premul(200, 100)) {
     29         fCircleBM.allocPixels(SkImageInfo::MakeN32Premul(100, 100));
     30         SkCanvas canvas(fCircleBM);
     31         canvas.clear(0xffffffff);
     32         SkPaint circlePaint;
     33         circlePaint.setColor(0xff000000);
     34         canvas.drawCircle(50, 50, 50, circlePaint);
     35     }
     36 
     37     const SkImageInfo& imageInfo() const { return fImageInfo; }
     38 
     39     void draw(SkCanvas* canvas, const SkRect& clipRect, SkBlendMode mode) const {
     40         SkPaint greenPaint;
     41         greenPaint.setColor(0xff008000);
     42         SkPaint blackPaint;
     43         blackPaint.setColor(0xff000000);
     44         SkPaint whitePaint;
     45         whitePaint.setColor(0xffffffff);
     46         SkPaint layerPaint;
     47         layerPaint.setColor(0xff000000);
     48         layerPaint.setBlendMode(mode);
     49         SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fImageInfo.width()),
     50                                          SkIntToScalar(fImageInfo.height())));
     51 
     52         canvas->clipRect(clipRect);
     53         canvas->clear(0xff000000);
     54 
     55         canvas->saveLayer(nullptr, &blackPaint);
     56             canvas->drawRect(canvasRect, greenPaint);
     57             canvas->saveLayer(nullptr, &layerPaint);
     58                 canvas->drawBitmapRect(fCircleBM, SkRect::MakeXYWH(20,20,60,60), &blackPaint);
     59             canvas->restore();
     60         canvas->restore();
     61     }
     62 
     63  private:
     64     const SkImageInfo fImageInfo;
     65     SkBitmap fCircleBM;
     66 };
     67 
     68 class RecordingStrategy {
     69  public:
     70     virtual ~RecordingStrategy() {}
     71     virtual const SkBitmap& recordAndReplay(const Drawer& drawer,
     72                                             const SkRect& intoClip,
     73                                             SkBlendMode) = 0;
     74 };
     75 
     76 class BitmapBackedCanvasStrategy : public RecordingStrategy {
     77     // This version just draws into a bitmap-backed canvas.
     78  public:
     79     BitmapBackedCanvasStrategy(const SkImageInfo& imageInfo) {
     80         fBitmap.allocPixels(imageInfo);
     81     }
     82 
     83     const SkBitmap& recordAndReplay(const Drawer& drawer, const SkRect& intoClip,
     84                                     SkBlendMode mode) override {
     85         SkCanvas canvas(fBitmap);
     86         canvas.clear(0xffffffff);
     87         // Note that the scene is drawn just into the clipped region!
     88         canvas.clipRect(intoClip);
     89         drawer.draw(&canvas, intoClip, mode); // Shouild be canvas-wide...
     90         return fBitmap;
     91     }
     92 
     93  private:
     94     SkBitmap fBitmap;
     95 };
     96 
     97 class PictureStrategy : public RecordingStrategy {
     98     // This version draws the entire scene into an SkPictureRecorder.
     99     // Then it then replays the scene through a clip rectangle.
    100     // This backend proved to be buggy.
    101  public:
    102     PictureStrategy(const SkImageInfo& imageInfo) {
    103         fBitmap.allocPixels(imageInfo);
    104         fWidth  = imageInfo.width();
    105         fHeight = imageInfo.height();
    106     }
    107 
    108     const SkBitmap& recordAndReplay(const Drawer& drawer, const SkRect& intoClip,
    109                                     SkBlendMode mode) override {
    110         SkRTreeFactory factory;
    111         SkPictureRecorder recorder;
    112         SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fWidth),SkIntToScalar(fHeight)));
    113         SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(fWidth),
    114                                                    SkIntToScalar(fHeight),
    115                                                    &factory);
    116         drawer.draw(canvas, canvasRect, mode);
    117         sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
    118 
    119         SkCanvas replayCanvas(fBitmap);
    120         replayCanvas.clear(0xffffffff);
    121         replayCanvas.clipRect(intoClip);
    122         picture->playback(&replayCanvas);
    123         return fBitmap;
    124     }
    125 
    126  private:
    127     SkBitmap fBitmap;
    128     int fWidth;
    129     int fHeight;
    130 };
    131 
    132 } // namespace
    133 
    134 
    135 DEF_TEST(SkRecordingAccuracyXfermode, reporter) {
    136 #define FINEGRAIN 0
    137     const Drawer drawer;
    138 
    139     BitmapBackedCanvasStrategy golden(drawer.imageInfo());
    140     PictureStrategy picture(drawer.imageInfo());
    141 
    142 #if !FINEGRAIN
    143     unsigned numErrors = 0;
    144     SkString errors;
    145 #endif
    146 
    147     for (int iMode = 0; iMode < int(SkBlendMode::kLastMode); iMode++) {
    148         const SkRect& clip = SkRect::MakeXYWH(100, 0, 100, 100);
    149         SkBlendMode mode = SkBlendMode(iMode);
    150 
    151         const SkBitmap& goldenBM = golden.recordAndReplay(drawer, clip, mode);
    152         const SkBitmap& pictureBM = picture.recordAndReplay(drawer, clip, mode);
    153 
    154         size_t pixelsSize = goldenBM.computeByteSize();
    155         REPORTER_ASSERT(reporter, pixelsSize == pictureBM.computeByteSize());
    156 
    157         // The pixel arrays should match.
    158 #if FINEGRAIN
    159         REPORTER_ASSERT(reporter,
    160                         0 == memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize));
    161 #else
    162         if (memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize)) {
    163             numErrors++;
    164             errors.appendf("For SkXfermode %d %s:    SkPictureRecorder bitmap is wrong\n",
    165                            iMode, SkBlendMode_Name(mode));
    166         }
    167 #endif
    168     }
    169 
    170 #if !FINEGRAIN
    171     REPORTER_ASSERT(reporter, 0 == numErrors, errors.c_str());
    172 #endif
    173 }
    174